proto-shell: add dns server support
[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         eval "$1_setup \"$interface\" \"$ifname\""
37 }
38
39 proto_init_update() {
40         local ifname="$1"
41         local up="$2"
42         local external="$3"
43
44         PROTO_INIT=1
45         PROTO_IPADDR=
46         PROTO_IP6ADDR=
47         PROTO_ROUTE=
48         PROTO_ROUTE6=
49         json_init
50         json_add_int action 0
51         json_add_string "ifname" "$ifname"
52         json_add_boolean "link-up" "$up"
53         [ -n "$3" ] && json_add_boolean "address-external" "$external"
54 }
55
56 proto_add_dns_server() {
57         local address="$1"
58
59         jshn_append PROTO_DNS "$address"
60 }
61
62 proto_add_ipv4_address() {
63         local address="$1"
64         local mask="$2"
65
66         jshn_append PROTO_IPADDR "$address/$mask"
67 }
68
69 proto_add_ipv6_address() {
70         local address="$1"
71         local mask="$2"
72
73         jshn_append PROTO_IP6ADDR "$address/$mask"
74 }
75
76 proto_add_ipv4_route() {
77         local target="$1"
78         local mask="$2"
79         local gw="$3"
80
81         jshn_append PROTO_ROUTE "$target/$mask/$gw"
82 }
83
84 proto_add_ipv6_route() {
85         local target="$1"
86         local mask="$2"
87         local gw="$3"
88
89         jshn_append PROTO_ROUTE6 "$target/$mask/$gw"
90 }
91
92 _proto_push_ip() {
93         json_add_string "" "$1"
94 }
95
96 _proto_push_route() {
97         local str="$1";
98         local target="${str%%/*}"
99         str="${str#*/}"
100         local mask="${str%%/*}"
101         local gw="${str#*/}"
102
103         json_add_table ""
104         json_add_string target "$target"
105         json_add_integer mask "$mask"
106         json_add_string gateway "$gw"
107         json_close_table
108 }
109
110 _proto_push_array() {
111         local name="$1"
112         local val="$2"
113         local cb="$3"
114
115         [ -n "$val" ] || return 0
116         json_add_array "$name"
117         for item in $val; do
118                 eval "$cb \"\$item\""
119         done
120         json_close_array
121 }
122
123 _proto_notify() {
124         ubus call network.interface."$interface" notify_proto "$(json_dump)"
125 }
126
127 proto_send_update() {
128         local interface="$1"
129
130         _proto_push_array "ipaddr" "$PROTO_IPADDR" _proto_push_ip
131         _proto_push_array "ip6addr" "$PROTO_IP6ADDR" _proto_push_ip
132         _proto_push_array "route" "$PROTO_ROUTE" _proto_push_route
133         _proto_push_array "route6" "$PROTO_ROUTE6" _proto_push_route
134         _proto_push_array "dns" "$PROTO_DNS" _proto_push_ip
135         _proto_notify
136 }
137
138 proto_run_command() {
139         json_init
140         json_add_int action 1
141         json_add_array command
142         while [ $# -gt 0 ]; do
143                 json_add_string "" "$1"
144                 shift
145         done
146         _proto_notify
147 }
148
149 init_proto() {
150         proto="$1"; shift
151         cmd="$1"; shift
152
153         case "$cmd" in
154                 dump)
155                         add_protocol() {
156                                 no_device=0
157                                 available=0
158
159                                 add_default_handler "$1_init_config"
160
161                                 json_init
162                                 json_add_string "name" "$1"
163                                 eval "$1_init"
164                                 json_add_boolean no-device "$no_device"
165                                 json_add_boolean available "$available"
166                                 json_add_array "config"
167                                 eval "$1_init_config"
168                                 json_close_array
169                                 json_dump
170                         }
171                 ;;
172                 setup|teardown)
173                         interface="$1"; shift
174                         data="$1"; shift
175                         ifname="$1"; shift
176
177                         add_protocol() {
178                                 [[ "$proto" == "$1" ]] || return 0
179
180                                 case "$cmd" in
181                                         setup) _proto_do_setup "$1";;
182                                         teardown) _proto_do_teardown "$1" ;;
183                                         *) return 1 ;;
184                                 esac
185                         }
186                 ;;
187         esac
188 }