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