modules/admin-full: Network interface configuration optimization part #1
[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
17
18 m = Map("network", translate("interfaces"), translate("a_n_ifaces1"))
19
20 local created
21 local netstat = luci.sys.net.deviceinfo()
22
23 s = m:section(TypedSection, "interface", translate("interfaces"))
24 s.addremove = true
25 s.extedit = luci.http.getenv("REQUEST_URI") .. "/%s"
26 s.template = "cbi/tblsection"
27
28 function s.filter(self, section)
29 return section ~= "loopback" and section
30 end
31
32 function s.create(self, section)
33 if TypedSection.create(self, section) then
34 created = section
35 end
36 end
37
38 function s.parse(self, ...)
39 TypedSection.parse(self, ...)
40 if created then
41 luci.http.redirect(luci.http.getenv("REQUEST_URI") .. "/" .. created)
42 end
43 end
44
45 up = s:option(Flag, "up")
46 up.stateful = true
47 function up.write(self, section, value)
48 local call = value == "1" and "ifdown" or "ifup"
49 os.execute(call .. " " .. section)
50 end
51
52 ipaddr = s:option(DummyValue, "ipaddr", translate("ipaddress"))
53 ipaddr.stateful = true
54
55 function ipaddr.cfgvalue(self, section)
56 local ip = self.map:stateget(section, "ipaddr")
57 local nm = self.map:stateget(section, "netmask")
58
59 local parsed = ip and luci.ip.IPv4(ip, nm)
60 return parsed and parsed:string() or ""
61 end
62
63 rx = s:option(DummyValue, "_rx")
64
65 function rx.cfgvalue(self, section)
66 local ix = self.map:stateget(section, "ifname")
67 local bt = netstat and netstat[ix] and netstat[ix][1]
68 return bt and string.format("%.2f MB", tonumber(bt) / 1024 / 1024)
69 end
70
71 tx = s:option(DummyValue, "_tx")
72
73 function tx.cfgvalue(self, section)
74 local ix = self.map:stateget(section, "ifname")
75 local bt = netstat and netstat[ix] and netstat[ix][9]
76 return bt and string.format("%.2f MB", tonumber(bt) / 1024 / 1024)
77 end
78
79 return m