proto-shell: allow proto setups without ifname (if interface main dev is present)
[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         [ -n "$ifname" -a "*" != "$ifname" ] && 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         local interface="$1"
125         ubus call network.interface."$interface" notify_proto "$(json_dump)"
126 }
127
128 proto_send_update() {
129         local interface="$1"
130
131         _proto_push_array "ipaddr" "$PROTO_IPADDR" _proto_push_ip
132         _proto_push_array "ip6addr" "$PROTO_IP6ADDR" _proto_push_ip
133         _proto_push_array "route" "$PROTO_ROUTE" _proto_push_route
134         _proto_push_array "route6" "$PROTO_ROUTE6" _proto_push_route
135         _proto_push_array "dns" "$PROTO_DNS" _proto_push_ip
136         _proto_notify "$interface"
137 }
138
139 proto_run_command() {
140         local interface="$1"; shift
141
142         json_init
143         json_add_int action 1
144         json_add_array command
145         while [ $# -gt 0 ]; do
146                 json_add_string "" "$1"
147                 shift
148         done
149         _proto_notify "$interface"
150 }
151
152 proto_kill_command() {
153         local interface="$1"; shift
154
155         json_init
156         json_add_int action 2
157         [ -n "$1" ] && json_add_int signal "$1"
158         _proto_notify "$interface"
159 }
160
161 init_proto() {
162         proto="$1"; shift
163         cmd="$1"; shift
164
165         case "$cmd" in
166                 dump)
167                         add_protocol() {
168                                 no_device=0
169                                 available=0
170
171                                 add_default_handler "$1_init_config"
172
173                                 json_init
174                                 json_add_string "name" "$1"
175                                 eval "$1_init"
176                                 json_add_boolean no-device "$no_device"
177                                 json_add_boolean available "$available"
178                                 json_add_array "config"
179                                 eval "$1_init_config"
180                                 json_close_array
181                                 json_dump
182                         }
183                 ;;
184                 setup|teardown)
185                         interface="$1"; shift
186                         data="$1"; shift
187                         ifname="$1"; shift
188
189                         add_protocol() {
190                                 [[ "$proto" == "$1" ]] || return 0
191
192                                 case "$cmd" in
193                                         setup) _proto_do_setup "$1";;
194                                         teardown) _proto_do_teardown "$1" ;;
195                                         *) return 1 ;;
196                                 esac
197                         }
198                 ;;
199         esac
200 }