libs/core: fix network is_empty() check for wifi-ifaces which are part of multiple...
[project/luci.git] / libs / core / luasrc / model / network.lua
index 3e59ed5c4ead412cad89fa0b42e33544146b5b3d..1764a3d41dc541dd15c7cdc92e0b2ab9f17cf503 100644 (file)
@@ -633,6 +633,7 @@ 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
@@ -731,7 +732,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 +742,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 +782,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 +798,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 +815,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)
@@ -908,7 +906,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
 
@@ -1094,24 +1097,25 @@ function interface.rx_packets(self)
 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
 
@@ -1431,10 +1435,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)