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