[package] base-files: implement network_get_dnsserver() and network_get_dnssearch...
[openwrt.git] / package / base-files / files / lib / functions / network.sh
1 . /usr/share/libubox/jshn.sh
2
3 __network_ipaddr()
4 {
5         local __var="$1"
6         local __iface="$2"
7         local __family="$3"
8         local __prefix="${4:-0}"
9
10         local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)"
11
12         json_load "${__tmp:-{}}"
13         json_get_type __tmp "ipv${__family}_address"
14
15         if [ "$__tmp" = array ]; then
16
17                 json_select "ipv${__family}_address"
18                 json_get_type __tmp 1
19
20                 if [ "$__tmp" = object ]; then
21
22                         json_select 1
23                         json_get_var $__var address
24
25                         [ $__prefix -gt 0 ] && {
26                                 json_get_var __tmp mask
27                                 eval "export -- \"$__var=\${$__var}/$__tmp\""
28                         }
29
30                         return 0
31                 fi
32         fi
33
34         return 1
35 }
36
37 network_get_ipaddr()  { __network_ipaddr "$1" "$2" 4 0; }
38 network_get_ipaddr6() { __network_ipaddr "$1" "$2" 6 0; }
39
40 network_get_subnet()  { __network_ipaddr "$1" "$2" 4 1; }
41 network_get_subnet6() { __network_ipaddr "$1" "$2" 6 1; }
42
43
44 __network_gateway()
45 {
46         local __var="$1"
47         local __iface="$2"
48         local __family="$3"
49
50         local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)"
51         local __idx=1
52         local __enabled
53
54         json_load "${__tmp:-{}}"
55
56         if json_get_type __tmp route && [ "$__tmp" = array ]; then
57
58                 json_select route
59
60                 while json_get_type __tmp "$__idx" && [ "$__tmp" = object ]; do
61
62                         json_select "$((__idx++))"
63                         json_get_var __tmp target
64                         json_get_var __enabled enabled
65
66                         case "${__enabled}/${__family}/${__tmp}" in
67                                 1/4/0.0.0.0|1/6/::)
68                                         json_get_var "$__var" nexthop
69                                         return $?
70                                 ;;
71                         esac
72
73                         json_select ".."
74
75                 done
76         fi
77
78         return 1
79 }
80
81 network_get_gateway()  { __network_gateway "$1" "$2" 4; }
82 network_get_gateway6() { __network_gateway "$1" "$2" 6; }
83
84
85 __network_dns() {
86         local __var="$1"
87         local __iface="$2"
88         local __field="$3"
89
90         local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)"
91         local __dns=""
92         local __idx=1
93
94         json_load "${__tmp:-{}}"
95
96         if json_get_type __tmp "$__field" && [ "$__tmp" = array ]; then
97
98                 json_select "$__field"
99
100                 while json_get_type __tmp "$__idx" && [ "$__tmp" = string ]; do
101
102                         json_get_var __tmp "$((__idx++))"
103                         __dns="${__dns:+$__dns }$__tmp"
104
105                 done
106         fi
107
108         eval "export -- \"$__var=$__dns\""
109         [ -n "$__dns" ]
110 }
111
112 network_get_dnsserver() { __network_dns "$1" "$2" dns_server; }
113 network_get_dnssearch() { __network_dns "$1" "$2" dns_search; }
114
115
116 __network_wan() {
117         local __var="$1"
118         local __family="$2"
119         local __iface
120
121         for __iface in $(ubus list | sed -ne 's/^network\.interface\.//p'); do
122                 if __network_gateway "$__var" "$__iface" "$__family"; then
123                         eval "export -- \"$__var=$__iface\""
124                         return 0
125                 fi
126         done
127
128         eval "export -- \"$__var=\""
129         return 1
130 }
131
132 network_find_wan()  { __network_wan "$1" 4; }
133 network_find_wan6() { __network_wan "$1" 6; }
134
135
136 __network_device()
137 {
138         local __var="$1"
139         local __iface="$2"
140         local __field="$3"
141
142         local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)"
143         [ -n "$__tmp" ] || return 1
144
145         json_load "$__tmp"
146         json_get_var "$__var" "$__field"
147 }
148
149 network_is_up()
150 {
151         local __up
152         __network_device __up "$1" up && [ $__up -eq 1 ]
153 }
154
155 network_get_device()  { __network_device "$1" "$2" l3_device; }
156 network_get_physdev() { __network_device "$1" "$2" device;    }
157
158
159 __network_defer()
160 {
161         local __device="$1"
162         local __defer="$2"
163
164         json_init
165         json_add_string name "$__device"
166         json_add_boolean defer "$__defer"
167
168         ubus call network.device set_state "$(json_dump)" 2>/dev/null
169 }
170
171 network_defer_device() { __network_defer "$1" 1; }
172 network_ready_device() { __network_defer "$1" 0; }