Fixed last commit
[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 ip6addr = s:option(DummyValue, "ip6addr", translate("ip6address"))
56 ip6addr.stateful = true
57
58 function ipaddr.cfgvalue(self, section)
59         local ip = self.map:stateget(section, "ipaddr")
60         local nm = self.map:stateget(section, "netmask")
61         
62         local parsed = ip and luci.ip.IPv4(ip, nm)
63         return parsed and parsed:string() or ""
64 end
65
66 rx = s:option(DummyValue, "_rx")
67
68 function rx.cfgvalue(self, section)
69         local ix = self.map:stateget(section, "ifname")
70         local bt = netstat and netstat[ix] and netstat[ix][1]
71         return bt and string.format("%.2f MB", tonumber(bt) / 1024 / 1024)
72 end
73
74 tx = s:option(DummyValue, "_tx")
75
76 function tx.cfgvalue(self, section)
77         local ix = self.map:stateget(section, "ifname")
78         local bt = netstat and netstat[ix] and netstat[ix][9]
79         return bt and string.format("%.2f MB", tonumber(bt) / 1024 / 1024)
80 end
81
82 return m