add a command for allowing the setup task to schedule a restart
[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 local broadcast="$3"
108 local ptp="$4"
109
110 jshn_append PROTO_IPADDR "$address/$mask/$broadcast/$ptp"
111 }
112
113 proto_add_ipv6_address() {
114 local address="$1"
115 local mask="$2"
116
117 jshn_append PROTO_IP6ADDR "$address/$mask"
118 }
119
120 proto_add_ipv4_route() {
121 local target="$1"
122 local mask="$2"
123 local gw="$3"
124
125 jshn_append PROTO_ROUTE "$target/$mask/$gw"
126 }
127
128 proto_add_ipv6_route() {
129 local target="$1"
130 local mask="$2"
131 local gw="$3"
132
133 jshn_append PROTO_ROUTE6 "$target/$mask/$gw"
134 }
135
136 _proto_push_ipv4_addr() {
137 local str="$1"
138 local address mask broadcast ptp
139
140 address="${str%%/*}"
141 str="${str#*/}"
142 mask="${str%%/*}"
143 str="${str#*/}"
144 broadcast="${str%%/*}"
145 str="${str#*/}"
146 ptp="$str"
147
148 json_add_object ""
149 json_add_string ipaddr "$address"
150 [ -n "$mask" ] && json_add_string mask "$mask"
151 [ -n "$broadcast" ] && json_add_string broadcast "$broadcast"
152 [ -n "$ptp" ] && json_add_string ptp "$ptp"
153 json_close_object
154 }
155
156 _proto_push_ipv6_addr() {
157 local str="$1"
158 local address mask
159
160 address="${str%%/*}"
161 str="${str#*/}"
162 mask="$str"
163
164 json_add_object ""
165 json_add_string ipaddr "$address"
166 [ -n "$mask" ] && json_add_string mask "$mask"
167 json_close_object
168 }
169
170 _proto_push_string() {
171 json_add_string "" "$1"
172 }
173
174 _proto_push_route() {
175 local str="$1";
176 local target="${str%%/*}"
177 str="${str#*/}"
178 local mask="${str%%/*}"
179 local gw="${str#*/}"
180
181 json_add_object ""
182 json_add_string target "$target"
183 json_add_string netmask "$mask"
184 [ -n "$gw" ] && json_add_string gateway "$gw"
185 json_close_object
186 }
187
188 _proto_push_array() {
189 local name="$1"
190 local val="$2"
191 local cb="$3"
192
193 [ -n "$val" ] || return 0
194 json_add_array "$name"
195 for item in $val; do
196 eval "$cb \"\$item\""
197 done
198 json_close_array
199 }
200
201 _proto_notify() {
202 local interface="$1"
203 local options="$2"
204 ubus $options call network.interface."$interface" notify_proto "$(json_dump)"
205 }
206
207 proto_send_update() {
208 local interface="$1"
209
210 proto_close_nested
211 json_add_boolean keep "$PROTO_KEEP"
212 _proto_push_array "ipaddr" "$PROTO_IPADDR" _proto_push_ipv4_addr
213 _proto_push_array "ip6addr" "$PROTO_IP6ADDR" _proto_push_ipv6_addr
214 _proto_push_array "routes" "$PROTO_ROUTE" _proto_push_route
215 _proto_push_array "routes6" "$PROTO_ROUTE6" _proto_push_route
216 _proto_push_array "dns" "$PROTO_DNS" _proto_push_string
217 _proto_push_array "dns_search" "$PROTO_DNS_SEARCH" _proto_push_string
218 _proto_notify "$interface"
219 }
220
221 proto_export() {
222 local var="VAR${_EXPORT_VAR}"
223 _EXPORT_VAR="$(($_EXPORT_VAR + 1))"
224 export -- "$var=$1"
225 jshn_append _EXPORT_VARS "$var"
226 }
227
228 proto_run_command() {
229 local interface="$1"; shift
230
231 json_init
232 json_add_int action 1
233 json_add_array command
234 while [ $# -gt 0 ]; do
235 json_add_string "" "$1"
236 shift
237 done
238 json_close_array
239 [ -n "$_EXPORT_VARS" ] && {
240 json_add_array env
241 for var in $_EXPORT_VARS; do
242 eval "json_add_string \"\" \"\${$var}\""
243 done
244 json_close_array
245 }
246 _proto_notify "$interface"
247 }
248
249 proto_kill_command() {
250 local interface="$1"; shift
251
252 json_init
253 json_add_int action 2
254 [ -n "$1" ] && json_add_int signal "$1"
255 _proto_notify "$interface"
256 }
257
258 proto_notify_error() {
259 local interface="$1"; shift
260
261 json_init
262 json_add_int action 3
263 json_add_array error
264 while [ $# -gt 0 ]; do
265 json_add_string "" "$1"
266 shift
267 done
268 json_close_array
269 _proto_notify "$interface"
270 }
271
272 proto_block_restart() {
273 local interface="$1"; shift
274
275 json_init
276 json_add_int action 4
277 _proto_notify "$interface"
278 }
279
280 proto_set_available() {
281 local interface="$1"
282 local state="$2"
283 json_init
284 json_add_int action 5
285 json_add_boolean available "$state"
286 _proto_notify "$interface"
287 }
288
289 proto_add_host_dependency() {
290 local interface="$1"
291 local host="$2"
292
293 json_init
294 json_add_int action 6
295 json_add_string host "$host"
296 _proto_notify "$interface" -S
297 }
298
299 proto_setup_failed() {
300 local interface="$1"
301 json_init
302 json_add_int action 7
303 _proto_notify "$interface"
304 }
305
306 init_proto() {
307 proto="$1"; shift
308 cmd="$1"; shift
309
310 case "$cmd" in
311 dump)
312 add_protocol() {
313 no_device=0
314 available=0
315
316 add_default_handler "proto_$1_init_config"
317
318 json_init
319 json_add_string "name" "$1"
320 json_add_array "config"
321 eval "proto_$1_init_config"
322 json_close_array
323 json_add_boolean no-device "$no_device"
324 json_add_boolean available "$available"
325 json_dump
326 }
327 ;;
328 setup|teardown)
329 interface="$1"; shift
330 data="$1"; shift
331 ifname="$1"; shift
332
333 add_protocol() {
334 [[ "$proto" == "$1" ]] || return 0
335
336 case "$cmd" in
337 setup) _proto_do_setup "$1";;
338 teardown) _proto_do_teardown "$1" ;;
339 *) return 1 ;;
340 esac
341 }
342 ;;
343 esac
344 }