libs/core: fix ubus corner cases in network model

This commit is contained in:
Jo-Philipp Wich 2012-05-31 17:24:13 +00:00
parent 100b544f3b
commit 22ce33a5a6

View file

@ -611,9 +611,10 @@ function protocol._ubus(self, field)
_ubusnetcache[self.sid] = _ubus:call("network.interface.%s" % self.sid, _ubusnetcache[self.sid] = _ubus:call("network.interface.%s" % self.sid,
"status", { }) "status", { })
end end
if _ubusnetcache[self.sid] and field then
return _ubusnetcache[self.sid] and (field and _ubusnetcache[self.sid][field] return _ubusnetcache[self.sid][field]
or _ubusnetcache[self.sid]) end
return _ubusnetcache[self.sid]
end end
function protocol.get(self, opt) function protocol.get(self, opt)
@ -625,20 +626,13 @@ function protocol.set(self, opt, val)
end end
function protocol.ifname(self) function protocol.ifname(self)
local p = self:proto() local ifname
if self:is_bridge() then if self:is_floating() then
return "br-" .. self.sid ifname = self:_ubus("l3_device")
elseif self:is_virtual() then
return p .. "-" .. self.sid
else else
local num = { } ifname = self:_ubus("device")
local dev = _uci_real:get("network", self.sid, "ifname") or end
_uci_state:get("network", self.sid, "ifname") if not ifname then
dev = (type(dev) == "table") and dev[1] or dev
dev = (dev ~= nil) and dev:match("%S+")
if not dev then
_uci_real:foreach("wireless", "wifi-iface", _uci_real:foreach("wireless", "wifi-iface",
function(s) function(s)
if s.device then if s.device then
@ -646,15 +640,13 @@ function protocol.ifname(self)
and num[s.device] + 1 or 1 and num[s.device] + 1 or 1
if s.network == self.sid then if s.network == self.sid then
dev = "%s.network%d" %{ s.device, num[s.device] } ifname = "%s.network%d" %{ s.device, num[s.device] }
return false return false
end end
end end
end) end)
end end
return ifname
return dev
end
end end
function protocol.proto(self) function protocol.proto(self)
@ -713,7 +705,7 @@ end
function protocol.gwaddr(self) function protocol.gwaddr(self)
local _, route local _, route
for _, route in ipairs(self:_ubus("route")) do for _, route in ipairs(self:_ubus("route") or { }) do
if route.target == "0.0.0.0" and route.mask == 0 then if route.target == "0.0.0.0" and route.mask == 0 then
return route.nexthop return route.nexthop
end end
@ -723,7 +715,7 @@ end
function protocol.dnsaddrs(self) function protocol.dnsaddrs(self)
local dns = { } local dns = { }
local _, addr local _, addr
for _, addr in ipairs(self:_ubus("dns-server")) do for _, addr in ipairs(self:_ubus("dns-server") or { }) do
if not addr:match(":") then if not addr:match(":") then
dns[#dns+1] = addr dns[#dns+1] = addr
end end
@ -947,9 +939,10 @@ function interface._ubus(self, field)
_ubusdevcache[self.ifname] = _ubus:call("network.device", "status", _ubusdevcache[self.ifname] = _ubus:call("network.device", "status",
{ name = self.ifname }) { name = self.ifname })
end end
return _ubusdevcache[self.ifname] and if _ubusdevcache[self.ifname] and field then
(field and _ubusdevcache[self.ifname][field] or return _ubusdevcache[self.ifname][field]
_ubusdevcache[self.ifname]) end
return _ubusdevcache[self.ifname]
end end
function interface.name(self) function interface.name(self)
@ -1074,7 +1067,10 @@ function interface.is_bridgeport(self)
end end
local function uint(x) local function uint(x)
if x then
return (x < 0) and ((2^32) + x) or x return (x < 0) and ((2^32) + x) or x
end
return 0
end end
function interface.tx_bytes(self) function interface.tx_bytes(self)