splash: fix exception when listing whitelist entries
[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
16 local sys = require "luci.sys"
17 local wa  = require "luci.tools.webadmin"
18 local fs  = require "nixio.fs"
19
20 local netstate = luci.model.uci.cursor_state():get_all("network")
21 m = Map("network", translate("Interfaces"))
22
23 local created
24 local netstat = sys.net.deviceinfo()
25
26 s = m:section(TypedSection, "interface", "")
27 s.addremove = true
28 s.anonymous = false
29 s.extedit   = luci.dispatcher.build_url("admin", "network", "network") .. "/%s"
30 s.template  = "cbi/tblsection"
31 s.override_scheme = true
32
33 function s.filter(self, section)
34         return section ~= "loopback" and section
35 end
36
37 function s.create(self, section)
38         if TypedSection.create(self, section) then
39                 created = section
40         else
41                 self.invalid_cts = true
42         end
43 end
44
45 function s.parse(self, ...)
46         TypedSection.parse(self, ...)
47         if created then
48                 m.uci:save("network")
49                 luci.http.redirect(luci.dispatcher.build_url("admin", "network", "network")
50                  .. "/" .. created)
51         end
52 end
53
54 up = s:option(Flag, "up")
55 function up.cfgvalue(self, section)
56         return netstate[section] and netstate[section].up or "0"
57 end
58
59 function up.write(self, section, value)
60         local call
61         if value == "1" then
62                 call = "ifup"
63         elseif value == "0" then
64                 call = "ifdown"
65         end
66         os.execute(call .. " " .. section .. " >/dev/null 2>&1")
67 end
68
69 ifname = s:option(DummyValue, "ifname", translate("Device"))
70 function ifname.cfgvalue(self, section)
71         return netstate[section] and netstate[section].ifname
72 end
73
74 ifname.titleref = luci.dispatcher.build_url("admin", "network", "vlan")
75
76
77 if luci.model.uci.cursor():load("firewall") then
78         zone = s:option(DummyValue, "_zone", translate("Zone"))
79         zone.titleref = luci.dispatcher.build_url("admin", "network", "firewall", "zones")
80
81         function zone.cfgvalue(self, section)
82                 return table.concat(wa.network_get_zones(section) or { "-" }, ", ")
83         end
84 end
85
86 hwaddr = s:option(DummyValue, "_hwaddr")
87 function hwaddr.cfgvalue(self, section)
88         local ix = self.map:get(section, "ifname") or ""
89         local mac = fs.readfile("/sys/class/net/" .. ix .. "/address")
90
91         if not mac then
92                 mac = luci.util.exec("ifconfig " .. ix)
93                 mac = mac and mac:match(" ([A-F0-9:]+)%s*\n")
94         end
95
96         if mac and #mac > 0 then
97                 return mac:upper()
98         end
99
100         return "?"
101 end
102
103
104 ipaddr = s:option(DummyValue, "ipaddr", translate("Addresses"))
105 function ipaddr.cfgvalue(self, section)
106         return table.concat(wa.network_get_addresses(section), ", ")
107 end
108
109 txrx = s:option(DummyValue, "_txrx")
110
111 function txrx.cfgvalue(self, section)
112         local ix = self.map:get(section, "ifname")
113
114         local rx = netstat and netstat[ix] and netstat[ix][1]
115         rx = rx and wa.byte_format(tonumber(rx)) or "-"
116
117         local tx = netstat and netstat[ix] and netstat[ix][9]
118         tx = tx and wa.byte_format(tonumber(tx)) or "-"
119
120         return string.format("%s / %s", tx, rx)
121 end
122
123 errors = s:option(DummyValue, "_err")
124
125 function errors.cfgvalue(self, section)
126         local ix = self.map:get(section, "ifname")
127
128         local rx = netstat and netstat[ix] and netstat[ix][3]
129         local tx = netstat and netstat[ix] and netstat[ix][11]
130
131         rx = rx and tostring(rx) or "-"
132         tx = tx and tostring(tx) or "-"
133
134         return string.format("%s / %s", tx, rx)
135 end
136
137 return m