modules/admin-full: Reworked DHCP configuration
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / dhcpleases.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14 require("luci.sys")
15 require("luci.tools.webadmin")
16 m2 = Map("luci_ethers", translate("dhcp_leases"))
17
18 local leasefn, leasefp, leases
19 luci.model.uci.foreach("dhcp", "dnsmasq",
20  function(section)
21         leasefn = section.leasefile
22  end
23
24 local leasefp = leasefn and luci.fs.access(leasefn) and io.lines(leasefn)
25 if leasefp then
26         leases = {}
27         for lease in leasefp do
28                 table.insert(leases, luci.util.split(lease, " "))
29         end
30 end
31
32 if leases then
33         v = m2:section(TypedSection, "_virtual", translate("dhcp_leases_active"))
34         v.anonymous = true
35         v.rowcolors = true
36         v.template  = "cbi/tblsection"
37         
38         function v.cfgsections(self)
39                 local sections = {}
40                 for i=1,#leases do
41                         table.insert(sections, i)
42                 end
43                 return sections
44         end
45         
46         ip = v:option(DummyValue, "ip", translate("ipaddress"))
47         function ip.cfgvalue(self, section)
48                 return leases[section][3]
49         end
50         
51         mac  = v:option(DummyValue, "mac", translate("macaddress"))
52         function mac.cfgvalue(self, section)
53                 return leases[section][2]
54         end
55         
56         ltime = v:option(DummyValue, "time", translate("dhcp_timeremain"))
57         function ltime.cfgvalue(self, section)
58                 return luci.tools.webadmin.date_format(
59                  os.difftime(tonumber(leases[section][1]), os.time())
60                 )
61         end
62 end
63
64 s = m2:section(TypedSection, "static_lease", translate("luci_ethers"))
65 s.addremove = true
66 s.anonymous = true
67 s.template = "cbi/tblsection"
68
69 mac = s:option(Value, "macaddr", translate("macaddress"))
70 ip = s:option(Value, "ipaddr", translate("ipaddress"))
71 for i, dataset in ipairs(luci.sys.net.arptable()) do
72         ip:value(dataset["IP address"])
73         mac:value(dataset["HW address"],
74          dataset["HW address"] .. " (" .. dataset["IP address"] .. ")")
75 end
76
77         
78 return m2