libs/core: remove now obsolete uint() wrapper in network model
[project/luci.git] / libs / core / luasrc / model / network.lua
index 3e59ed5..09a58d7 100644 (file)
@@ -20,8 +20,7 @@ limitations under the License.
 local type, next, pairs, ipairs, loadfile, table
        = type, next, pairs, ipairs, loadfile, table
 
-local tonumber, tostring, math, i18n
-       = tonumber, tostring, math, luci.i18n
+local tonumber, tostring, math = tonumber, tostring, math
 
 local require = require
 
@@ -33,12 +32,13 @@ local sys = require "luci.sys"
 local utl = require "luci.util"
 local dsp = require "luci.dispatcher"
 local uci = require "luci.model.uci"
+local lng = require "luci.i18n"
 
 module "luci.model.network"
 
 
 IFACE_PATTERNS_VIRTUAL  = { }
-IFACE_PATTERNS_IGNORE   = { "^wmaster%d", "^wifi%d", "^hwsim%d", "^imq%d", "^ifb%d", "^mon%.wlan%d", "^sit%d", "^lo$" }
+IFACE_PATTERNS_IGNORE   = { "^wmaster%d", "^wifi%d", "^hwsim%d", "^imq%d", "^ifb%d", "^mon%.wlan%d", "^sit%d", "^gre%d", "^lo$" }
 IFACE_PATTERNS_WIRELESS = { "^wlan%d", "^wl%d", "^ath%d", "^%w+%.network%d" }
 
 
@@ -384,7 +384,17 @@ function del_network(self, n)
 
                _uci_real:foreach("wireless", "wifi-iface",
                        function(s)
-                               if s.network == n then
+                               local net
+                               local rest = { }
+                               for net in utl.imatch(s.network) do
+                                       if net ~= n then
+                                               rest[#rest+1] = net
+                                       end
+                               end
+                               if #rest > 0 then
+                                       _uci_real:set("wireless", s['.name'], "network",
+                                                     table.concat(rest, " "))
+                               else
                                        _uci_real:delete("wireless", s['.name'], "network")
                                end
                        end)
@@ -421,8 +431,18 @@ function rename_network(self, old, new)
 
                        _uci_real:foreach("wireless", "wifi-iface",
                                function(s)
-                                       if s.network == old then
-                                               _uci_real:set("wireless", s['.name'], "network", new)
+                                       local net
+                                       local list = { }
+                                       for net in utl.imatch(s.network) do
+                                               if net == old then
+                                                       list[#list+1] = new
+                                               else
+                                                       list[#list+1] = net
+                                               end
+                                       end
+                                       if #list > 0 then
+                                               _uci_real:set("wireless", s['.name'], "network",
+                                                             table.concat(list, " "))
                                        end
                                end)
 
@@ -585,6 +605,70 @@ function del_wifinet(self, net)
        return false
 end
 
+function get_status_by_route(self, addr, mask)
+       local _, object
+       for _, object in ipairs(_ubus:objects()) do
+               local net = object:match("^network%.interface%.(.+)")
+               if net then
+                       local s = _ubus:call(object, "status", {})
+                       if s and s.route then
+                               local rt
+                               for _, rt in ipairs(s.route) do
+                                       if rt.target == addr and rt.mask == mask then
+                                               return net, s
+                                       end
+                               end
+                       end
+               end
+       end
+end
+
+function get_status_by_address(self, addr)
+       local _, object
+       for _, object in ipairs(_ubus:objects()) do
+               local net = object:match("^network%.interface%.(.+)")
+               if net then
+                       local s = _ubus:call(object, "status", {})
+                       if s and s['ipv4-address'] then
+                               local a
+                               for _, a in ipairs(s['ipv4-address']) do
+                                       if a.address == addr then
+                                               return net, s
+                                       end
+                               end
+                       end
+                       if s and s['ipv6-address'] then
+                               local a
+                               for _, a in ipairs(s['ipv6-address']) do
+                                       if a.address == addr then
+                                               return net, s
+                                       end
+                               end
+                       end
+               end
+       end
+end
+
+function get_wannet(self)
+       local net = self:get_status_by_route("0.0.0.0", 0)
+       return net and network(net)
+end
+
+function get_wandev(self)
+       local _, stat = self:get_status_by_route("0.0.0.0", 0)
+       return stat and interface(stat.l3_device or stat.device)
+end
+
+function get_wan6net(self)
+       local net = self:get_status_by_route("::", 0)
+       return net and network(net)
+end
+
+function get_wan6dev(self)
+       local _, stat = self:get_status_by_route("::", 0)
+       return stat and interface(stat.l3_device or stat.device)
+end
+
 
 function network(name, proto)
        if name then
@@ -633,15 +717,19 @@ function protocol.ifname(self)
                ifname = self:_ubus("device")
        end
        if not ifname then
+               local num = { }
                _uci_real:foreach("wireless", "wifi-iface",
                        function(s)
                                if s.device then
                                        num[s.device] = num[s.device]
                                                and num[s.device] + 1 or 1
 
-                                       if s.network == self.sid then
-                                               ifname = "%s.network%d" %{ s.device, num[s.device] }
-                                               return false
+                                       local net
+                                       for net in utl.imatch(s.network) do
+                                               if net == self.sid then
+                                                       ifname = "%s.network%d" %{ s.device, num[s.device] }
+                                                       return false
+                                               end
                                        end
                                end
                        end)
@@ -656,13 +744,13 @@ end
 function protocol.get_i18n(self)
        local p = self:proto()
        if p == "none" then
-               return i18n.translate("Unmanaged")
+               return lng.translate("Unmanaged")
        elseif p == "static" then
-               return i18n.translate("Static address")
+               return lng.translate("Static address")
        elseif p == "dhcp" then
-               return i18n.translate("DHCP client")
+               return lng.translate("DHCP client")
        else
-               return i18n.translate("Unknown")
+               return lng.translate("Unknown")
        end
 end
 
@@ -731,7 +819,7 @@ end
 
 function protocol.gw6addr(self)
        local _, route
-       for _, route in ipairs(self:_ubus("route")) do
+       for _, route in ipairs(self:_ubus("route") or { }) do
                if route.target == "::" and route.mask == 0 then
                        return ipc.IPv6(route.nexthop):string()
                end
@@ -741,7 +829,7 @@ end
 function protocol.dns6addrs(self)
        local dns = { }
        local _, addr
-       for _, addr in ipairs(self:_ubus("dns-server")) do
+       for _, addr in ipairs(self:_ubus("dns-server") or { }) do
                if addr:match(":") then
                        dns[#dns+1] = addr
                end
@@ -781,9 +869,12 @@ function protocol.is_empty(self)
 
                _uci_real:foreach("wireless", "wifi-iface",
                        function(s)
-                               if s.network == self.sid then
-                                       rv = false
-                                       return false
+                               local n
+                               for n in utl.imatch(s.network) do
+                                       if n == self.sid then
+                                               rv = false
+                                               return false
+                                       end
                                end
                        end)
 
@@ -794,16 +885,10 @@ end
 function protocol.add_interface(self, ifname)
        ifname = _M:ifnameof(ifname)
        if ifname and not self:is_floating() then
-               -- remove the interface from all ifaces
-               _uci_real:foreach("network", "interface",
-                       function(s)
-                               _filter("network", s['.name'], "ifname", ifname)
-                       end)
-
                -- if its a wifi interface, change its network option
                local wif = _wifi_lookup(ifname)
                if wif then
-                       _uci_real:set("wireless", wif, "network", self.sid)
+                       _append("wireless", wif, "network", self.sid)
 
                -- add iface to our iface list
                else
@@ -817,7 +902,7 @@ function protocol.del_interface(self, ifname)
        if ifname and not self:is_floating() then
                -- if its a wireless interface, clear its network option
                local wif = _wifi_lookup(ifname)
-               if wif then     _uci_real:delete("wireless", wif, "network") end
+               if wif then _filter("wireless", wif, "network", self.sid) end
 
                -- remove the interface
                _filter("network", self.sid, "ifname", ifname)
@@ -843,9 +928,13 @@ function protocol.get_interface(self)
                        function(s)
                                if s.device then
                                        num[s.device] = num[s.device] and num[s.device] + 1 or 1
-                                       if s.network == self.sid then
-                                               ifn = "%s.network%d" %{ s.device, num[s.device] }
-                                               return false
+
+                                       local net
+                                       for net in utl.imatch(s.network) do
+                                               if net == self.sid then
+                                                       ifn = "%s.network%d" %{ s.device, num[s.device] }
+                                                       return false
+                                               end
                                        end
                                end
                        end)
@@ -874,9 +963,13 @@ function protocol.get_interfaces(self)
                        function(s)
                                if s.device then
                                        num[s.device] = num[s.device] and num[s.device] + 1 or 1
-                                       if s.network == self.sid then
-                                               ifn = "%s.network%d" %{ s.device, num[s.device] }
-                                               wfs[ifn] = interface(ifn, self)
+
+                                       local net
+                                       for net in utl.imatch(s.network) do
+                                               if net == self.sid then
+                                                       ifn = "%s.network%d" %{ s.device, num[s.device] }
+                                                       wfs[ifn] = interface(ifn, self)
+                                               end
                                        end
                                end
                        end)
@@ -908,7 +1001,12 @@ function protocol.contains_interface(self, ifname)
 
                local wif = _wifi_lookup(ifname)
                if wif then
-                       return (_uci_real:get("wireless", wif, "network") == self.sid)
+                       local n
+                       for n in utl.imatch(_uci_real:get("wireless", wif, "network")) do
+                               if n == self.sid then
+                                       return true
+                               end
+                       end
                end
        end
 
@@ -991,7 +1089,7 @@ end
 function interface.get_i18n(self)
        if self.wif then
                return "%s: %s %q" %{
-                       i18n.translate("Wireless Network"),
+                       lng.translate("Wireless Network"),
                        self.wif:active_mode(),
                        self.wif:active_ssid() or self.wif:active_bssid()
                }
@@ -1003,17 +1101,17 @@ end
 function interface.get_type_i18n(self)
        local x = self:type()
        if x == "wifi" then
-               return i18n.translate("Wireless Adapter")
+               return lng.translate("Wireless Adapter")
        elseif x == "bridge" then
-               return i18n.translate("Bridge")
+               return lng.translate("Bridge")
        elseif x == "switch" then
-               return i18n.translate("Ethernet Switch")
+               return lng.translate("Ethernet Switch")
        elseif x == "vlan" then
-               return i18n.translate("VLAN Interface")
+               return lng.translate("VLAN Interface")
        elseif x == "tunnel" then
-               return i18n.translate("Tunnel Interface")
+               return lng.translate("Tunnel Interface")
        else
-               return i18n.translate("Ethernet Adapter")
+               return lng.translate("Ethernet Adapter")
        end
 end
 
@@ -1066,52 +1164,46 @@ function interface.is_bridgeport(self)
        return self.dev and self.dev.bridge and true or false
 end
 
-local function uint(x)
-       if x then
-               return (x < 0) and ((2^32) + x) or x
-       end
-       return 0
-end
-
 function interface.tx_bytes(self)
        local stat = self:_ubus("statistics")
-       return stat and uint(stat.tx_bytes) or 0
+       return stat and stat.tx_bytes or 0
 end
 
 function interface.rx_bytes(self)
        local stat = self:_ubus("statistics")
-       return stat and uint(stat.rx_bytes) or 0
+       return stat and stat.rx_bytes or 0
 end
 
 function interface.tx_packets(self)
        local stat = self:_ubus("statistics")
-       return stat and uint(stat.tx_packets) or 0
+       return stat and stat.tx_packets or 0
 end
 
 function interface.rx_packets(self)
        local stat = self:_ubus("statistics")
-       return stat and uint(stat.rx_packets) or 0
+       return stat and stat.rx_packets or 0
 end
 
 function interface.get_network(self)
-       if not self.network then
-               if self.dev and self.dev.network then
-                       self.network = _M:get_network(self.dev.network)
-               end
-       end
+       return self:get_networks()[1]
+end
 
-       if not self.network then
-               local net
+function interface.get_networks(self)
+       if not self.networks then
+               local nets = { }
+               local _, net
                for _, net in ipairs(_M:get_networks()) do
                        if net:contains_interface(self.ifname) or
                           net:ifname() == self.ifname
                        then
-                               self.network = net
-                               return net
+                               nets[#nets+1] = net
                        end
                end
+               table.sort(nets, function(a, b) return a.sid < b.sid end)
+               self.networks = nets
+               return nets
        else
-               return self.network
+               return self.networks
        end
 end
 
@@ -1323,7 +1415,7 @@ function wifinet.active_mode(self)
 end
 
 function wifinet.active_mode_i18n(self)
-       return i18n.translate(self:active_mode())
+       return lng.translate(self:active_mode())
 end
 
 function wifinet.active_ssid(self)
@@ -1412,15 +1504,15 @@ end
 
 function wifinet.shortname(self)
        return "%s %q" %{
-               i18n.translate(self:active_mode()),
+               lng.translate(self:active_mode()),
                self:active_ssid() or self:active_bssid()
        }
 end
 
 function wifinet.get_i18n(self)
        return "%s: %s %q (%s)" %{
-               i18n.translate("Wireless Network"),
-               i18n.translate(self:active_mode()),
+               lng.translate("Wireless Network"),
+               lng.translate(self:active_mode()),
                self:active_ssid() or self:active_bssid(),
                self:ifname()
        }
@@ -1431,10 +1523,19 @@ function wifinet.adminlink(self)
 end
 
 function wifinet.get_network(self)
-       local net = tostring(self.iwdata.network)
-       if net and _uci_real:get("network", net) == "interface" then
-               return network(net)
+       return self:get_networks()[1]
+end
+
+function wifinet.get_networks(self)
+       local nets = { }
+       local net
+       for net in utl.imatch(tostring(self.iwdata.network)) do
+               if _uci_real:get("network", net) == "interface" then
+                       nets[#nets+1] = network(net)
+               end
        end
+       table.sort(nets, function(a, b) return a.sid < b.sid end)
+       return nets
 end
 
 function wifinet.get_interface(self)