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