11337c239e0c421337879e92961534f46b68d6f8
[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 "$1_teardown \"$interface\" \"$ifname\""
32 }
33
34 _proto_do_setup() {
35 json_load "$data"
36 _EXPORT_VAR=0
37 _EXPORT_VARS=
38 eval "$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_INIT=1
47 PROTO_IPADDR=
48 PROTO_IP6ADDR=
49 PROTO_ROUTE=
50 PROTO_ROUTE6=
51 PROTO_DNS=
52 PROTO_DNS_SEARCH=
53 json_init
54 json_add_int action 0
55 [ -n "$ifname" -a "*" != "$ifname" ] && json_add_string "ifname" "$ifname"
56 json_add_boolean "link-up" "$up"
57 [ -n "$3" ] && json_add_boolean "address-external" "$external"
58 }
59
60 proto_add_dns_server() {
61 local address="$1"
62
63 jshn_append PROTO_DNS "$address"
64 }
65
66 proto_add_dns_search() {
67 local address="$1"
68
69 jshn_append PROTO_DNS_SEARCH "$address"
70 }
71
72 proto_add_ipv4_address() {
73 local address="$1"
74 local mask="$2"
75
76 jshn_append PROTO_IPADDR "$address/$mask"
77 }
78
79 proto_add_ipv6_address() {
80 local address="$1"
81 local mask="$2"
82
83 jshn_append PROTO_IP6ADDR "$address/$mask"
84 }
85
86 proto_add_ipv4_route() {
87 local target="$1"
88 local mask="$2"
89 local gw="$3"
90
91 jshn_append PROTO_ROUTE "$target/$mask/$gw"
92 }
93
94 proto_add_ipv6_route() {
95 local target="$1"
96 local mask="$2"
97 local gw="$3"
98
99 jshn_append PROTO_ROUTE6 "$target/$mask/$gw"
100 }
101
102 _proto_push_ip() {
103 json_add_string "" "$1"
104 }
105
106 _proto_push_route() {
107 local str="$1";
108 local target="${str%%/*}"
109 str="${str#*/}"
110 local mask="${str%%/*}"
111 local gw="${str#*/}"
112
113 json_add_object ""
114 json_add_string target "$target"
115 json_add_string netmask "$mask"
116 json_add_string gateway "$gw"
117 json_close_object
118 }
119
120 _proto_push_array() {
121 local name="$1"
122 local val="$2"
123 local cb="$3"
124
125 [ -n "$val" ] || return 0
126 json_add_array "$name"
127 for item in $val; do
128 eval "$cb \"\$item\""
129 done
130 json_close_array
131 }
132
133 _proto_notify() {
134 local interface="$1"
135 ubus call network.interface."$interface" notify_proto "$(json_dump)"
136 }
137
138 proto_send_update() {
139 local interface="$1"
140
141 _proto_push_array "ipaddr" "$PROTO_IPADDR" _proto_push_ip
142 _proto_push_array "ip6addr" "$PROTO_IP6ADDR" _proto_push_ip
143 _proto_push_array "routes" "$PROTO_ROUTE" _proto_push_route
144 _proto_push_array "routes6" "$PROTO_ROUTE6" _proto_push_route
145 _proto_push_array "dns" "$PROTO_DNS" _proto_push_ip
146 _proto_push_array "dns_search" "$PROTO_DNS_SEARCH" _proto_push_ip
147 _proto_notify "$interface"
148 }
149
150 proto_export() {
151 local var="VAR${_EXPORT_VAR}"
152 _EXPORT_VAR="$(($_EXPORT_VAR + 1))"
153 export -- "$var=$1"
154 jshn_append _EXPORT_VARS "$var"
155 }
156
157 proto_run_command() {
158 local interface="$1"; shift
159
160 json_init
161 json_add_int action 1
162 json_add_array command
163 while [ $# -gt 0 ]; do
164 json_add_string "" "$1"
165 shift
166 done
167 json_close_array
168 [ -n "$_EXPORT_VARS" ] && {
169 json_add_array env
170 for var in $_EXPORT_VARS; do
171 eval "json_add_string \"\" \"\${$var}\""
172 done
173 json_close_array
174 }
175 _proto_notify "$interface"
176 }
177
178 proto_kill_command() {
179 local interface="$1"; shift
180
181 json_init
182 json_add_int action 2
183 [ -n "$1" ] && json_add_int signal "$1"
184 _proto_notify "$interface"
185 }
186
187 proto_notify_error() {
188 local interface="$1"; shift
189
190 json_init
191 json_add_int action 3
192 json_add_array error
193 while [ $# -gt 0 ]; do
194 json_add_string "" "$1"
195 shift
196 done
197 json_close_array
198 _proto_notify "$interface"
199 }
200
201 proto_block_restart() {
202 local interface="$1"; shift
203
204 json_init
205 json_add_int action 4
206 _proto_notify "$interface"
207 }
208
209 proto_set_available() {
210 local interface="$1"
211 local state="$2"
212 json_init
213 json_add_int action 5
214 json_add_boolean available "$state"
215 _proto_notify "$interface"
216 }
217
218 init_proto() {
219 proto="$1"; shift
220 cmd="$1"; shift
221
222 case "$cmd" in
223 dump)
224 add_protocol() {
225 no_device=0
226 available=0
227
228 add_default_handler "$1_init_config"
229
230 json_init
231 json_add_string "name" "$1"
232 eval "$1_init"
233 json_add_boolean no-device "$no_device"
234 json_add_boolean available "$available"
235 json_add_array "config"
236 eval "$1_init_config"
237 json_close_array
238 json_dump
239 }
240 ;;
241 setup|teardown)
242 interface="$1"; shift
243 data="$1"; shift
244 ifname="$1"; shift
245
246 add_protocol() {
247 [[ "$proto" == "$1" ]] || return 0
248
249 case "$cmd" in
250 setup) _proto_do_setup "$1";;
251 teardown) _proto_do_teardown "$1" ;;
252 *) return 1 ;;
253 esac
254 }
255 ;;
256 esac
257 }