ccb5dac8987d490ed68bb351c5025e43c45da8a2
[project/luci.git] / modules / admin-mini / luasrc / model / cbi / mini / 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.tools.webadmin")
16 require("luci.sys")
17
18 local network = luci.model.uci.cursor_state():get_all("network")
19
20 local netstat = luci.sys.net.deviceinfo()
21 local ifaces = {}
22
23 for k, v in pairs(network) do
24         if v[".type"] == "interface" and k ~= "loopback" then
25                 table.insert(ifaces, v)
26         end
27 end
28
29 m = Map("network", translate("network"))
30 s = m:section(Table, ifaces, translate("status"))
31 s.parse = function() end
32
33 s:option(DummyValue, ".name", translate("network"))
34
35 hwaddr = s:option(DummyValue, "_hwaddr",
36  translate("network_interface_hwaddr"), translate("network_interface_hwaddr_desc"))
37 function hwaddr.cfgvalue(self, section)
38         local ix = self.map:get(section, "ifname") or ""
39         return luci.fs.readfile("/sys/class/net/" .. ix .. "/address")
40                 or luci.util.exec("ifconfig " .. ix):match(" ([A-F0-9:]+)%s*\n")
41                 or "n/a"
42 end
43
44
45 s:option(DummyValue, "ipaddr", translate("ipaddress"))
46
47 s:option(DummyValue, "netmask", translate("netmask"))
48
49
50 txrx = s:option(DummyValue, "_txrx",
51  translate("network_interface_txrx"), translate("network_interface_txrx_desc"))
52
53 function txrx.cfgvalue(self, section)
54         local ix = self.map:get(section, "ifname")
55         
56         local rx = netstat and netstat[ix] and netstat[ix][1]
57         rx = rx and luci.tools.webadmin.byte_format(tonumber(rx)) or "-"
58         
59         local tx = netstat and netstat[ix] and netstat[ix][9]
60         tx = tx and luci.tools.webadmin.byte_format(tonumber(tx)) or "-"
61         
62         return string.format("%s / %s", tx, rx)
63 end
64
65 errors = s:option(DummyValue, "_err",
66  translate("network_interface_err"), translate("network_interface_err_desc"))
67
68 function errors.cfgvalue(self, section)
69         local ix = self.map:get(section, "ifname")
70         
71         local rx = netstat and netstat[ix] and netstat[ix][3]
72         local tx = netstat and netstat[ix] and netstat[ix][11]
73         
74         rx = rx and tostring(rx) or "-"
75         tx = tx and tostring(tx) or "-"
76         
77         return string.format("%s / %s", tx, rx)
78 end
79
80
81
82 s = m:section(NamedSection, "lan", "interface", translate("m_n_local"))
83 s.addremove = false
84 s:option(Value, "ipaddr", translate("ipaddress"))
85
86 nm = s:option(Value, "netmask", translate("netmask"))
87 nm:value("255.255.255.0")
88 nm:value("255.255.0.0")
89 nm:value("255.0.0.0")
90
91 gw = s:option(Value, "gateway", translate("gateway") .. translate("cbi_optional"))
92 gw.rmempty = true
93 dns = s:option(Value, "dns", translate("dnsserver") .. translate("cbi_optional"))
94 dns.rmempty = true
95
96
97 s = m:section(NamedSection, "wan", "interface", translate("m_n_inet"))
98 s.addremove = false
99 p = s:option(ListValue, "proto", translate("protocol"))
100 p:value("none", "disabled")
101 p:value("static", translate("manual", "manual"))
102 p:value("dhcp", translate("automatic", "automatic"))
103 p:value("pppoe", "PPPoE")
104 p:value("pptp", "PPTP")
105
106 ip = s:option(Value, "ipaddr", translate("ipaddress"))
107 ip:depends("proto", "static")
108
109 nm = s:option(Value, "netmask", translate("netmask"))
110 nm:depends("proto", "static")
111
112 gw = s:option(Value, "gateway", translate("gateway"))
113 gw:depends("proto", "static")
114 gw.rmempty = true
115
116 dns = s:option(Value, "dns", translate("dnsserver"))
117 dns:depends("proto", "static")
118 dns.rmempty = true
119
120 usr = s:option(Value, "username", translate("username"))
121 usr:depends("proto", "pppoe")
122 usr:depends("proto", "pptp")
123
124 pwd = s:option(Value, "password", translate("password"))
125 pwd:depends("proto", "pppoe")
126 pwd:depends("proto", "pptp")
127
128 kea = s:option(Flag, "keepalive", translate("m_n_keepalive"))
129 kea:depends("proto", "pppoe")
130 kea:depends("proto", "pptp")
131 kea.rmempty = true
132 kea.enabled = "10"
133
134
135 cod = s:option(Value, "demand", translate("m_n_dialondemand"), "s")
136 cod:depends("proto", "pppoe")
137 cod:depends("proto", "pptp")
138 cod.rmempty = true
139
140 srv = s:option(Value, "server", translate("m_n_pptp_server"))
141 srv:depends("proto", "pptp")
142 srv.rmempty = true
143
144
145
146 return m