[package] base-files: let network_get_device() return the l3 interface, introduce...
[openwrt/svn-archive/archive.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_device()
45 {
46 local __var="$1"
47 local __iface="$2"
48 local __field="$3"
49
50 local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)"
51 [ -n "$__tmp" ] || return 1
52
53 json_load "$__tmp"
54 json_get_var "$__var" "$__field"
55 }
56
57 network_get_device() { __network_device "$1" "$2" l3_device; }
58 network_get_physdev() { __network_device "$1" "$2" device; }