Small bug fixes
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / network.lua
index c79dc83..c0f66e9 100644 (file)
@@ -13,14 +13,16 @@ You may obtain a copy of the License at
 $Id$
 ]]--
 require("luci.sys")
+require("luci.tools.webadmin")
 
 
-m = Map("network", translate("interfaces"), translate("a_n_ifaces1"))
+m = Map("network", translate("interfaces"))
+m.stateful = true
 
 local created
 local netstat = luci.sys.net.deviceinfo()
 
-s = m:section(TypedSection, "interface", translate("interfaces"))
+s = m:section(TypedSection, "interface", "")
 s.addremove = true
 s.extedit   = luci.http.getenv("REQUEST_URI") .. "/%s"
 s.template  = "cbi/tblsection"
@@ -43,37 +45,67 @@ function s.parse(self, ...)
 end
 
 up = s:option(Flag, "up")
-up.stateful = true
 function up.write(self, section, value)
-       local call = value == "1" and "ifdown" or "ifup"
+       local call = value == "1" and "ifup" or "ifdown"
        os.execute(call .. " " .. section)
 end
 
-ipaddr = s:option(DummyValue, "ipaddr", translate("ipaddress"))
-ipaddr.stateful = true
+ifname = s:option(DummyValue, "ifname", translate("device"))
+ifname.titleref = luci.dispatcher.build_url("admin", "network", "vlan")
+
+if luci.model.uci.load("firewall") then
+       zone = s:option(DummyValue, "_zone", translate("zone"))
+       zone.titleref = luci.dispatcher.build_url("admin", "network", "firewall", "zones")
+
+       function zone.cfgvalue(self, section)
+               local zones = luci.tools.webadmin.network_get_zones(section)
+               return zones and table.concat(zones, ", ") or "-"
+       end
+end
+
+hwaddr = s:option(DummyValue, "_hwaddr")
+function hwaddr.cfgvalue(self, section)
+       local ix = self.map:get(section, "ifname") or ""
+       return luci.fs.readfile("/sys/class/net/" .. ix .. "/address")
+        or luci.util.exec("ifconfig " .. ix):match(" ([A-F0-9:]+)%s*\n")
+        or "n/a"
+        
+end
+
+
+ipaddr = s:option(DummyValue, "ipaddr", translate("addresses"))
 
 function ipaddr.cfgvalue(self, section)
-       local ip = self.map:stateget(section, "ipaddr")
-       local nm = self.map:stateget(section, "netmask")
-       
-       local parsed = ip and luci.ip.IPv4(ip, nm)
-       return parsed and parsed:string() or ""
+       local addr = luci.tools.webadmin.network_get_addresses(section)
+       return table.concat(addr, ", ")
 end
 
-rx = s:option(DummyValue, "_rx")
+txrx = s:option(DummyValue, "_txrx")
 
-function rx.cfgvalue(self, section)
-       local ix = self.map:stateget(section, "ifname")
-       local bt = netstat and netstat[ix] and netstat[ix][1]
-       return bt and string.format("%.2f MB", tonumber(bt) / 1024 / 1024)
+function txrx.cfgvalue(self, section)
+       local ix = self.map:get(section, "ifname")
+       
+       local rx = netstat and netstat[ix] and netstat[ix][1]
+       rx = rx and luci.tools.webadmin.byte_format(tonumber(rx)) or "-"
+       
+       local tx = netstat and netstat[ix] and netstat[ix][9]
+       tx = tx and luci.tools.webadmin.byte_format(tonumber(tx)) or "-"
+       
+       return string.format("%s / %s", tx, rx)
 end
 
-tx = s:option(DummyValue, "_tx")
+errors = s:option(DummyValue, "_err")
 
-function tx.cfgvalue(self, section)
-       local ix = self.map:stateget(section, "ifname")
-       local bt = netstat and netstat[ix] and netstat[ix][9]
-       return bt and string.format("%.2f MB", tonumber(bt) / 1024 / 1024)
+function errors.cfgvalue(self, section)
+       local ix = self.map:get(section, "ifname")
+       
+       local rx = netstat and netstat[ix] and netstat[ix][3]
+       local tx = netstat and netstat[ix] and netstat[ix][11]
+       
+       rx = rx and tostring(rx) or "-"
+       tx = tx and tostring(tx) or "-"
+       
+       return string.format("%s / %s", tx, rx)
 end
 
 return m
\ No newline at end of file