| 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 | |
| 53 | json_load "${__tmp:-{}}" |
| 54 | |
| 55 | if json_get_type __tmp route && [ "$__tmp" = array ]; then |
| 56 | |
| 57 | json_select route |
| 58 | |
| 59 | while json_get_type __tmp "$__idx" && [ "$__tmp" = object ]; do |
| 60 | |
| 61 | json_select "$((__idx++))" |
| 62 | json_get_var __tmp target |
| 63 | |
| 64 | case "${__family}/${__tmp}" in |
| 65 | 4/0.0.0.0|6/::) |
| 66 | json_get_var "$__var" nexthop |
| 67 | return $? |
| 68 | ;; |
| 69 | esac |
| 70 | |
| 71 | json_select ".." |
| 72 | |
| 73 | done |
| 74 | fi |
| 75 | |
| 76 | return 1 |
| 77 | } |
| 78 | |
| 79 | network_get_gateway() { __network_gateway "$1" "$2" 4; } |
| 80 | network_get_gateway6() { __network_gateway "$1" "$2" 6; } |
| 81 | |
| 82 | |
| 83 | __network_dns() { |
| 84 | local __var="$1" |
| 85 | local __iface="$2" |
| 86 | local __field="$3" |
| 87 | |
| 88 | local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)" |
| 89 | local __dns="" |
| 90 | local __idx=1 |
| 91 | |
| 92 | json_load "${__tmp:-{}}" |
| 93 | |
| 94 | if json_get_type __tmp "$__field" && [ "$__tmp" = array ]; then |
| 95 | |
| 96 | json_select "$__field" |
| 97 | |
| 98 | while json_get_type __tmp "$__idx" && [ "$__tmp" = string ]; do |
| 99 | |
| 100 | json_get_var __tmp "$((__idx++))" |
| 101 | __dns="${__dns:+$__dns }$__tmp" |
| 102 | |
| 103 | done |
| 104 | fi |
| 105 | |
| 106 | eval "export -- \"$__var=$__dns\"" |
| 107 | [ -n "$__dns" ] |
| 108 | } |
| 109 | |
| 110 | network_get_dnsserver() { __network_dns "$1" "$2" dns_server; } |
| 111 | network_get_dnssearch() { __network_dns "$1" "$2" dns_search; } |
| 112 | |
| 113 | |
| 114 | __network_wan() { |
| 115 | local __var="$1" |
| 116 | local __family="$2" |
| 117 | local __iface |
| 118 | |
| 119 | for __iface in $(ubus list | sed -ne 's/^network\.interface\.//p'); do |
| 120 | if __network_gateway "$__var" "$__iface" "$__family"; then |
| 121 | eval "export -- \"$__var=$__iface\"" |
| 122 | return 0 |
| 123 | fi |
| 124 | done |
| 125 | |
| 126 | eval "export -- \"$__var=\"" |
| 127 | return 1 |
| 128 | } |
| 129 | |
| 130 | network_find_wan() { __network_wan "$1" 4; } |
| 131 | network_find_wan6() { __network_wan "$1" 6; } |
| 132 | |
| 133 | |
| 134 | __network_device() |
| 135 | { |
| 136 | local __var="$1" |
| 137 | local __iface="$2" |
| 138 | local __field="$3" |
| 139 | |
| 140 | local __tmp="$(ubus call network.interface."$__iface" status 2>/dev/null)" |
| 141 | [ -n "$__tmp" ] || return 1 |
| 142 | |
| 143 | json_load "$__tmp" |
| 144 | json_get_var "$__var" "$__field" |
| 145 | } |
| 146 | |
| 147 | network_is_up() |
| 148 | { |
| 149 | local __up |
| 150 | __network_device __up "$1" up && [ $__up -eq 1 ] |
| 151 | } |
| 152 | |
| 153 | network_get_device() { __network_device "$1" "$2" l3_device; } |
| 154 | network_get_physdev() { __network_device "$1" "$2" device; } |
| 155 | |
| 156 | |
| 157 | __network_defer() |
| 158 | { |
| 159 | local __device="$1" |
| 160 | local __defer="$2" |
| 161 | |
| 162 | json_init |
| 163 | json_add_string name "$__device" |
| 164 | json_add_boolean defer "$__defer" |
| 165 | |
| 166 | ubus call network.device set_state "$(json_dump)" 2>/dev/null |
| 167 | } |
| 168 | |
| 169 | network_defer_device() { __network_defer "$1" 1; } |
| 170 | network_ready_device() { __network_defer "$1" 0; } |
| 171 | |