1 . /usr/share/libubox/jshn.sh
3 __network_switch_inactive()
7 if [ "$1" = 0 ] || [ "$1" = false ]; then
11 json_get_type __tmp "inactive"
13 if [ "$__tmp" = object ]; then
14 json_select "inactive"
20 __NETWORK_CACHE="${__NETWORK_CACHE:+$__NETWORK_CACHE }__NETWORK_CV_$1"
21 eval "__NETWORK_CV_$1='\$$2'"
26 eval "[ -n \"\${__NETWORK_CV_$1+x}\" ] && export -- \"$2=\$__NETWORK_CV_$1\""
35 local __key="ipaddr_${2}_${3}_${4}"
37 __network_get_cache "$__key" "$__var" && return 0
39 local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)"
41 json_load "${__tmp:-{}}"
42 json_get_type __tmp "ipv${__family}_address"
44 if [ "$__tmp" = array ]; then
46 json_select "ipv${__family}_address"
49 if [ "$__tmp" = object ]; then
52 json_get_var $__var address
54 [ $__prefix -gt 0 ] && {
55 json_get_var __tmp mask
56 eval "export -- \"$__var=\${$__var}/$__tmp\""
59 __network_set_cache "$__key" "$__var"
67 # determine IPv4 address of given logical interface
68 # 1: destination variable
70 network_get_ipaddr() { __network_ipaddr "$1" "$2" 4 0; }
72 # determine IPv6 address of given logical interface
73 # 1: destination variable
75 network_get_ipaddr6() { __network_ipaddr "$1" "$2" 6 0; }
77 # determine IPv4 subnet of given logical interface
78 # 1: destination variable
80 network_get_subnet() { __network_ipaddr "$1" "$2" 4 1; }
82 # determine IPv6 subnet of given logical interface
83 # 1: destination variable
85 network_get_subnet6() { __network_ipaddr "$1" "$2" 6 1; }
93 local __key="gateway_${2}_${3}"
95 __network_get_cache "$__key" "$__var" && return 0
97 local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)"
99 json_load "${__tmp:-{}}"
103 if json_get_type __tmp route && [ "$__tmp" = array ]; then
108 while json_get_type __tmp "$__idx" && [ "$__tmp" = object ]; do
110 json_select "$((__idx++))"
111 json_get_var __tmp target
113 case "${__family}/${__tmp}" in
115 json_get_var "$__var" nexthop
116 __network_set_cache "$__key" "$__var"
129 __network_switch_inactive "$4" || break
136 # determine IPv4 gateway of given logical interface
137 # 1: destination variable
139 # 3: consider inactive gateway if "true" (optional)
140 network_get_gateway() { __network_gateway "$1" "$2" 4 "${3:-0}"; }
142 # determine IPv6 gateway of given logical interface
143 # 1: destination variable
145 # 3: consider inactive gateway if "true" (optional)
146 network_get_gateway6() { __network_gateway "$1" "$2" 6 "${3:-0}"; }
153 local __key="dns_${2}_${3}"
155 __network_get_cache "$__key" "$__var" && return 0
157 local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)"
160 json_load "${__tmp:-{}}"
164 if json_get_type __tmp "$__field" && [ "$__tmp" = array ]; then
166 json_select "$__field"
169 while json_get_type __tmp "$__idx" && [ "$__tmp" = string ]; do
171 json_get_var __tmp "$((__idx++))"
172 __dns="${__dns:+$__dns }$__tmp"
179 __network_switch_inactive "$4" || break
183 if [ -n "$__dns" ]; then
184 eval "export -- \"$__var=$__dns\""
185 __network_set_cache "$__key" "$__var"
189 # determine the DNS servers of the given logical interface
190 # 1: destination variable
192 # 3: consider inactive servers if "true" (optional)
193 network_get_dnsserver() { __network_dns "$1" "$2" dns_server "${3:-0}"; }
195 # determine the domains of the given logical interface
196 # 1: destination variable
198 # 3: consider inactive servers if "true" (optional)
199 network_get_dnssearch() { __network_dns "$1" "$2" dns_search "${3:-0}"; }
205 local __key="wan_${2}"
208 __network_get_cache "$__key" "$__var" && return 0
210 for __iface in $(ubus list | sed -ne 's/^network\.interface\.//p'); do
211 if __network_gateway "$__var" "$__iface" "$__family"; then
212 eval "export -- \"$__var=$__iface\""
213 __network_set_cache "$__key" "$__var"
218 eval "export -- \"$__var=\""
222 # find the logical interface which holds the current IPv4 default route
223 # 1: destination variable
224 network_find_wan() { __network_wan "$1" 4; }
226 # find the logical interface which holds the current IPv6 default route
227 # 1: destination variable
228 network_find_wan6() { __network_wan "$1" 6; }
236 local __key="device_${2}_${3}"
238 __network_get_cache "$__key" "$__var" && return 0
240 local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)"
241 [ -n "$__tmp" ] || return 1
244 json_get_var "$__var" "$__field" && __network_set_cache "$__key" "$__var"
247 # test whether the given logical interface is running
252 __network_device __up "$1" up && [ $__up -eq 1 ]
255 # determine the layer 3 linux network device of the given logical interface
256 # 1: destination variable
258 network_get_device() { __network_device "$1" "$2" l3_device; }
260 # determine the layer 2 linux network device of the given logical interface
261 # 1: destination variable
263 network_get_physdev() { __network_device "$1" "$2" device; }
272 json_add_string name "$__device"
273 json_add_boolean defer "$__defer"
275 ubus call network.device set_state "$(json_dump)" 2>/dev/null
278 # defer netifd actions on the given linux network device
280 network_defer_device() { __network_defer "$1" 1; }
282 # continue netifd actions on the given linux network device
284 network_ready_device() { __network_defer "$1" 0; }
286 # flush the internal value cache to force re-reading values from ubus
287 network_flush_cache()
290 for __tmp in $__NETWORK_CACHE __NETWORK_CACHE; do