300535d6dab29993b5cb86c437a002a4e9eaafbe
[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 s.override_scheme = true
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 else
38 self.invalid_cts = true
39 end
40 end
41
42 function s.parse(self, ...)
43 TypedSection.parse(self, ...)
44 if created then
45 m.uci:save("network")
46 luci.http.redirect(luci.dispatcher.build_url("admin", "network", "network")
47 .. "/" .. created)
48 end
49 end
50
51 up = s:option(Flag, "up")
52 function up.cfgvalue(self, section)
53 return netstate[section] and netstate[section].up or "0"
54 end
55
56 function up.write(self, section, value)
57 local call
58 if value == "1" then
59 call = "ifup"
60 elseif value == "0" then
61 call = "ifdown"
62 end
63 os.execute(call .. " " .. section .. " >/dev/null 2>&1")
64 end
65
66 ifname = s:option(DummyValue, "ifname", translate("device"))
67 function ifname.cfgvalue(self, section)
68 return netstate[section] and netstate[section].ifname
69 end
70
71 ifname.titleref = luci.dispatcher.build_url("admin", "network", "vlan")
72
73
74 if luci.model.uci.cursor():load("firewall") then
75 zone = s:option(DummyValue, "_zone", translate("zone"))
76 zone.titleref = luci.dispatcher.build_url("admin", "network", "firewall", "zones")
77
78 function zone.cfgvalue(self, section)
79 local zones = luci.tools.webadmin.network_get_zones(section)
80 return zones and table.concat(zones, ", ") or "-"
81 end
82 end
83
84 hwaddr = s:option(DummyValue, "_hwaddr")
85 function hwaddr.cfgvalue(self, section)
86 local ix = self.map:get(section, "ifname") or ""
87 return luci.fs.readfile("/sys/class/net/" .. ix .. "/address")
88 or luci.util.exec("ifconfig " .. ix):match(" ([A-F0-9:]+)%s*\n")
89 or "n/a"
90
91 end
92
93
94 ipaddr = s:option(DummyValue, "ipaddr", translate("addresses"))
95 function ipaddr.cfgvalue(self, section)
96 local addr = luci.tools.webadmin.network_get_addresses(section)
97 return table.concat(addr, ", ")
98 end
99
100 txrx = s:option(DummyValue, "_txrx")
101
102 function txrx.cfgvalue(self, section)
103 local ix = self.map:get(section, "ifname")
104
105 local rx = netstat and netstat[ix] and netstat[ix][1]
106 rx = rx and luci.tools.webadmin.byte_format(tonumber(rx)) or "-"
107
108 local tx = netstat and netstat[ix] and netstat[ix][9]
109 tx = tx and luci.tools.webadmin.byte_format(tonumber(tx)) or "-"
110
111 return string.format("%s / %s", tx, rx)
112 end
113
114 errors = s:option(DummyValue, "_err")
115
116 function errors.cfgvalue(self, section)
117 local ix = self.map:get(section, "ifname")
118
119 local rx = netstat and netstat[ix] and netstat[ix][3]
120 local tx = netstat and netstat[ix] and netstat[ix][11]
121
122 rx = rx and tostring(rx) or "-"
123 tx = tx and tostring(tx) or "-"
124
125 return string.format("%s / %s", tx, rx)
126 end
127
128 return m