85b2c86aad41f7d90002158c3019292a979d3123
[project/luci.git] / modules / admin-mini / luasrc / model / cbi / mini / dhcp.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.model.uci")
16 require("luci.sys")
17 require("luci.tools.webadmin")
18
19 m = Map("dhcp", "DHCP")
20
21 s = m:section(TypedSection, "dhcp", "DHCP-Server")
22 s.anonymous = true
23 s:depends("interface", "lan")
24
25 enable = s:option(ListValue, "ignore", translate("enable"), "")
26 enable:value(0, translate("enable"))
27 enable:value(1, translate("disable"))
28
29 start = s:option(Value, "start", translate("m_n_d_firstaddress"))
30 start.rmempty = true
31 start:depends("ignore", "0")
32
33
34 limit = s:option(Value, "limit", translate("m_n_d_numleases"), "")
35 limit:depends("ignore", "0")
36
37 function limit.cfgvalue(self, section)
38         local value = Value.cfgvalue(self, section)
39         
40         if value then
41                 return tonumber(value) + 1
42         end 
43 end
44
45 function limit.write(self, section, value)
46         value = tonumber(value) - 1
47         return Value.write(self, section, value) 
48 end
49
50 limit.rmempty = true
51
52 time = s:option(Value, "leasetime")
53 time:depends("ignore", "0")
54 time.rmempty = true
55
56
57
58 m2 = Map("luci_ethers", translate("dhcp_leases"))
59
60 local leasefn, leasefp, leases
61 luci.model.uci.foreach("dhcp", "dnsmasq",
62  function(section)
63         leasefn = section.leasefile
64  end
65
66 local leasefp = leasefn and luci.fs.access(leasefn) and io.lines(leasefn)
67 if leasefp then
68         leases = {}
69         for lease in leasefp do
70                 table.insert(leases, luci.util.split(lease, " "))
71         end
72 end
73
74 if leases then
75         v = m2:section(Table, leases, translate("dhcp_leases_active"))
76         ip = v:option(DummyValue, 3, translate("ipaddress"))
77         
78         mac  = v:option(DummyValue, 2, translate("macaddress"))
79         
80         ltime = v:option(DummyValue, 1, translate("dhcp_timeremain"))
81         function ltime.cfgvalue(self, ...)
82                 local value = DummyValue.cfgvalue(self, ...)
83                 return luci.tools.webadmin.date_format(
84                  os.difftime(tonumber(value), os.time())
85                 )
86         end
87 end
88
89 s = m2:section(TypedSection, "static_lease", translate("luci_ethers"))
90 s.addremove = true
91 s.anonymous = true
92 s.template = "cbi/tblsection"
93
94 mac = s:option(Value, "macaddr", translate("macaddress"))
95 ip = s:option(Value, "ipaddr", translate("ipaddress"))
96 for i, dataset in ipairs(luci.sys.net.arptable()) do
97         ip:value(dataset["IP address"])
98         mac:value(dataset["HW address"],
99          dataset["HW address"] .. " (" .. dataset["IP address"] .. ")")
100 end
101
102 return m, m2