UCI API changes
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / network.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15 require("luci.sys")
16 require("luci.tools.webadmin")
17
18 local netstate = luci.model.uci.cursor_state():get_all("network")
19 m = Map("network", translate("interfaces"))
20
21 local created
22 local netstat = luci.sys.net.deviceinfo()
23
24 s = m:section(TypedSection, "interface", "")
25 s.addremove = true
26 s.extedit   = luci.dispatcher.build_url("admin", "network", "network") .. "/%s"
27 s.template  = "cbi/tblsection"
28
29 function s.filter(self, section)
30         return section ~= "loopback" and section
31 end
32
33 function s.create(self, section)
34         if TypedSection.create(self, section) then
35                 created = section
36         end
37 end
38
39 function s.parse(self, ...)
40         TypedSection.parse(self, ...)
41         if created then
42                 luci.http.redirect(luci.dispatcher.build_url("admin", "network", "network")
43                  .. "/" .. created)
44         end
45 end
46
47 up = s:option(Flag, "up")
48 function up.cfgvalue(self, section)
49         return netstate[section] and netstate[section].up or "0"
50 end
51
52 function up.write(self, section, value)
53         local call
54         if value == "1" then
55                 call = "ifup"
56         elseif value == "0" then
57                 call = "ifdown"
58         end
59         os.execute(call .. " " .. section .. " >/dev/null 2>&1")
60 end
61
62 ifname = s:option(DummyValue, "ifname", translate("device"))
63 function ifname.cfgvalue(self, section)
64         return netstate[section] and netstate[section].ifname
65 end
66
67 ifname.titleref = luci.dispatcher.build_url("admin", "network", "vlan")
68
69
70 if luci.model.uci.cursor():load("firewall") then
71         zone = s:option(DummyValue, "_zone", translate("zone"))
72         zone.titleref = luci.dispatcher.build_url("admin", "network", "firewall", "zones")
73
74         function zone.cfgvalue(self, section)
75                 local zones = luci.tools.webadmin.network_get_zones(section)
76                 return zones and table.concat(zones, ", ") or "-"
77         end
78 end
79
80 hwaddr = s:option(DummyValue, "_hwaddr")
81 function hwaddr.cfgvalue(self, section)
82         local ix = self.map:get(section, "ifname") or ""
83         return luci.fs.readfile("/sys/class/net/" .. ix .. "/address")
84          or luci.util.exec("ifconfig " .. ix):match(" ([A-F0-9:]+)%s*\n")
85          or "n/a"
86          
87 end
88
89
90 ipaddr = s:option(DummyValue, "ipaddr", translate("addresses"))
91 function ipaddr.cfgvalue(self, section)
92         local addr = luci.tools.webadmin.network_get_addresses(section)
93         return table.concat(addr, ", ")
94 end
95
96 txrx = s:option(DummyValue, "_txrx")
97
98 function txrx.cfgvalue(self, section)
99         local ix = self.map:get(section, "ifname")
100         
101         local rx = netstat and netstat[ix] and netstat[ix][1]
102         rx = rx and luci.tools.webadmin.byte_format(tonumber(rx)) or "-"
103         
104         local tx = netstat and netstat[ix] and netstat[ix][9]
105         tx = tx and luci.tools.webadmin.byte_format(tonumber(tx)) or "-"
106         
107         return string.format("%s / %s", tx, rx)
108 end
109
110 errors = s:option(DummyValue, "_err")
111
112 function errors.cfgvalue(self, section)
113         local ix = self.map:get(section, "ifname")
114         
115         local rx = netstat and netstat[ix] and netstat[ix][3]
116         local tx = netstat and netstat[ix] and netstat[ix][11]
117         
118         rx = rx and tostring(rx) or "-"
119         tx = tx and tostring(tx) or "-"
120         
121         return string.format("%s / %s", tx, rx)
122 end
123
124 return m