74265818218847a20dba28fbb8fd10f466ad150b
[project/netifd.git] / dummy / netifd-proto.sh
1 . /usr/share/libubox/jshn.sh
2
3 proto_config_add_generic() {
4         json_add_array ""
5         json_add_string "" "$1"
6         json_add_int "" "$2"
7         json_close_array
8 }
9
10 proto_config_add_int() {
11         proto_config_add_generic "$1" 5
12 }
13
14 proto_config_add_string() {
15         proto_config_add_generic "$1" 3
16 }
17
18 proto_config_add_boolean() {
19         proto_config_add_generic "$1" 7
20 }
21
22 add_default_handler() {
23         case "$(type $1 2>/dev/null)" in
24                 *function*) return;;
25                 *) eval "$1() { return; }"
26         esac
27 }
28
29 _proto_do_teardown() {
30         json_load "$data"
31         eval "proto_$1_teardown \"$interface\" \"$ifname\""
32 }
33
34 _proto_do_setup() {
35         json_load "$data"
36         _EXPORT_VAR=0
37         _EXPORT_VARS=
38         eval "proto_$1_setup \"$interface\" \"$ifname\""
39 }
40
41 proto_init_update() {
42         local ifname="$1"
43         local up="$2"
44         local external="$3"
45
46         PROTO_KEEP=0
47         PROTO_INIT=1
48         PROTO_TUNNEL_OPEN=
49         PROTO_IPADDR=
50         PROTO_IP6ADDR=
51         PROTO_ROUTE=
52         PROTO_ROUTE6=
53         PROTO_DNS=
54         PROTO_DNS_SEARCH=
55         json_init
56         json_add_int action 0
57         [ -n "$ifname" -a "*" != "$ifname" ] && json_add_string "ifname" "$ifname"
58         json_add_boolean "link-up" "$up"
59         [ -n "$3" ] && json_add_boolean "address-external" "$external"
60 }
61
62 proto_set_keep() {
63         PROTO_KEEP="$1"
64 }
65
66 proto_close_nested() {
67         [ -n "$PROTO_NESTED_OPEN" ] && json_close_object
68         PROTO_NESTED_OPEN=
69 }
70
71 proto_add_nested() {
72         PROTO_NESTED_OPEN=1
73         json_add_object "$1"
74 }
75
76 proto_add_tunnel() {
77         proto_add_nested "tunnel"
78 }
79
80 proto_close_tunnel() {
81         proto_close_nested
82 }
83
84 proto_add_data() {
85         proto_add_nested "data"
86 }
87
88 proto_close_data() {
89         proto_close_nested
90 }
91
92 proto_add_dns_server() {
93         local address="$1"
94
95         jshn_append PROTO_DNS "$address"
96 }
97
98 proto_add_dns_search() {
99         local address="$1"
100
101         jshn_append PROTO_DNS_SEARCH "$address"
102 }
103
104 proto_add_ipv4_address() {
105         local address="$1"
106         local mask="$2"
107
108         jshn_append PROTO_IPADDR "$address/$mask"
109 }
110
111 proto_add_ipv6_address() {
112         local address="$1"
113         local mask="$2"
114
115         jshn_append PROTO_IP6ADDR "$address/$mask"
116 }
117
118 proto_add_ipv4_route() {
119         local target="$1"
120         local mask="$2"
121         local gw="$3"
122
123         jshn_append PROTO_ROUTE "$target/$mask/$gw"
124 }
125
126 proto_add_ipv6_route() {
127         local target="$1"
128         local mask="$2"
129         local gw="$3"
130
131         jshn_append PROTO_ROUTE6 "$target/$mask/$gw"
132 }
133
134 _proto_push_ip() {
135         json_add_string "" "$1"
136 }
137
138 _proto_push_route() {
139         local str="$1";
140         local target="${str%%/*}"
141         str="${str#*/}"
142         local mask="${str%%/*}"
143         local gw="${str#*/}"
144
145         json_add_object ""
146         json_add_string target "$target"
147         json_add_string netmask "$mask"
148         [ -n "$gw" ] && json_add_string gateway "$gw"
149         json_close_object
150 }
151
152 _proto_push_array() {
153         local name="$1"
154         local val="$2"
155         local cb="$3"
156
157         [ -n "$val" ] || return 0
158         json_add_array "$name"
159         for item in $val; do
160                 eval "$cb \"\$item\""
161         done
162         json_close_array
163 }
164
165 _proto_notify() {
166         local interface="$1"
167         local options="$2"
168         ubus $options call network.interface."$interface" notify_proto "$(json_dump)"
169 }
170
171 proto_send_update() {
172         local interface="$1"
173
174         proto_close_nested
175         json_add_boolean keep "$PROTO_KEEP"
176         _proto_push_array "ipaddr" "$PROTO_IPADDR" _proto_push_ip
177         _proto_push_array "ip6addr" "$PROTO_IP6ADDR" _proto_push_ip
178         _proto_push_array "routes" "$PROTO_ROUTE" _proto_push_route
179         _proto_push_array "routes6" "$PROTO_ROUTE6" _proto_push_route
180         _proto_push_array "dns" "$PROTO_DNS" _proto_push_ip
181         _proto_push_array "dns_search" "$PROTO_DNS_SEARCH" _proto_push_ip
182         _proto_notify "$interface"
183 }
184
185 proto_export() {
186         local var="VAR${_EXPORT_VAR}"
187         _EXPORT_VAR="$(($_EXPORT_VAR + 1))"
188         export -- "$var=$1"
189         jshn_append _EXPORT_VARS "$var"
190 }
191
192 proto_run_command() {
193         local interface="$1"; shift
194
195         json_init
196         json_add_int action 1
197         json_add_array command
198         while [ $# -gt 0 ]; do
199                 json_add_string "" "$1"
200                 shift
201         done
202         json_close_array
203         [ -n "$_EXPORT_VARS" ] && {
204                 json_add_array env
205                 for var in $_EXPORT_VARS; do
206                         eval "json_add_string \"\" \"\${$var}\""
207                 done
208                 json_close_array
209         }
210         _proto_notify "$interface"
211 }
212
213 proto_kill_command() {
214         local interface="$1"; shift
215
216         json_init
217         json_add_int action 2
218         [ -n "$1" ] && json_add_int signal "$1"
219         _proto_notify "$interface"
220 }
221
222 proto_notify_error() {
223         local interface="$1"; shift
224
225         json_init
226         json_add_int action 3
227         json_add_array error
228         while [ $# -gt 0 ]; do
229                 json_add_string "" "$1"
230                 shift
231         done
232         json_close_array
233         _proto_notify "$interface"
234 }
235
236 proto_block_restart() {
237         local interface="$1"; shift
238
239         json_init
240         json_add_int action 4
241         _proto_notify "$interface"
242 }
243
244 proto_set_available() {
245         local interface="$1"
246         local state="$2"
247         json_init
248         json_add_int action 5
249         json_add_boolean available "$state"
250         _proto_notify "$interface"
251 }
252
253 proto_add_host_dependency() {
254         local interface="$1"
255         local host="$2"
256
257         json_init
258         json_add_int action 6
259         json_add_string host "$host"
260         _proto_notify "$interface" -S
261 }
262
263 init_proto() {
264         proto="$1"; shift
265         cmd="$1"; shift
266
267         case "$cmd" in
268                 dump)
269                         add_protocol() {
270                                 no_device=0
271                                 available=0
272
273                                 add_default_handler "proto_$1_init_config"
274
275                                 json_init
276                                 json_add_string "name" "$1"
277                                 json_add_array "config"
278                                 eval "proto_$1_init_config"
279                                 json_close_array
280                                 json_add_boolean no-device "$no_device"
281                                 json_add_boolean available "$available"
282                                 json_dump
283                         }
284                 ;;
285                 setup|teardown)
286                         interface="$1"; shift
287                         data="$1"; shift
288                         ifname="$1"; shift
289
290                         add_protocol() {
291                                 [[ "$proto" == "$1" ]] || return 0
292
293                                 case "$cmd" in
294                                         setup) _proto_do_setup "$1";;
295                                         teardown) _proto_do_teardown "$1" ;;
296                                         *) return 1 ;;
297                                 esac
298                         }
299                 ;;
300         esac
301 }