c0f66e965eb4be3b15fb8ae3b774d810ae756ae3
[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
19 m = Map("network", translate("interfaces"))
20 m.stateful = true
21
22 local created
23 local netstat = luci.sys.net.deviceinfo()
24
25 s = m:section(TypedSection, "interface", "")
26 s.addremove = true
27 s.extedit   = luci.http.getenv("REQUEST_URI") .. "/%s"
28 s.template  = "cbi/tblsection"
29
30 function s.filter(self, section)
31         return section ~= "loopback" and section
32 end
33
34 function s.create(self, section)
35         if TypedSection.create(self, section) then
36                 created = section
37         end
38 end
39
40 function s.parse(self, ...)
41         TypedSection.parse(self, ...)
42         if created then
43                 luci.http.redirect(luci.http.getenv("REQUEST_URI") .. "/" .. created)
44         end
45 end
46
47 up = s:option(Flag, "up")
48 function up.write(self, section, value)
49         local call = value == "1" and "ifup" or "ifdown"
50         os.execute(call .. " " .. section)
51 end
52
53 ifname = s:option(DummyValue, "ifname", translate("device"))
54 ifname.titleref = luci.dispatcher.build_url("admin", "network", "vlan")
55
56 if luci.model.uci.load("firewall") then
57         zone = s:option(DummyValue, "_zone", translate("zone"))
58         zone.titleref = luci.dispatcher.build_url("admin", "network", "firewall", "zones")
59
60         function zone.cfgvalue(self, section)
61                 local zones = luci.tools.webadmin.network_get_zones(section)
62                 return zones and table.concat(zones, ", ") or "-"
63         end
64 end
65
66 hwaddr = s:option(DummyValue, "_hwaddr")
67 function hwaddr.cfgvalue(self, section)
68         local ix = self.map:get(section, "ifname") or ""
69         return luci.fs.readfile("/sys/class/net/" .. ix .. "/address")
70          or luci.util.exec("ifconfig " .. ix):match(" ([A-F0-9:]+)%s*\n")
71          or "n/a"
72          
73 end
74
75
76 ipaddr = s:option(DummyValue, "ipaddr", translate("addresses"))
77
78 function ipaddr.cfgvalue(self, section)
79         local addr = luci.tools.webadmin.network_get_addresses(section)
80         return table.concat(addr, ", ")
81 end
82
83 txrx = s:option(DummyValue, "_txrx")
84
85 function txrx.cfgvalue(self, section)
86         local ix = self.map:get(section, "ifname")
87         
88         local rx = netstat and netstat[ix] and netstat[ix][1]
89         rx = rx and luci.tools.webadmin.byte_format(tonumber(rx)) or "-"
90         
91         local tx = netstat and netstat[ix] and netstat[ix][9]
92         tx = tx and luci.tools.webadmin.byte_format(tonumber(tx)) or "-"
93         
94         return string.format("%s / %s", tx, rx)
95 end
96
97 errors = s:option(DummyValue, "_err")
98
99 function errors.cfgvalue(self, section)
100         local ix = self.map:get(section, "ifname")
101         
102         local rx = netstat and netstat[ix] and netstat[ix][3]
103         local tx = netstat and netstat[ix] and netstat[ix][11]
104         
105         rx = rx and tostring(rx) or "-"
106         tx = tx and tostring(tx) or "-"
107         
108         return string.format("%s / %s", tx, rx)
109 end
110
111 return m