modules/admin-{mini,full}: display client hostnames in lease overview
[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
16 local uci = require "luci.model.uci".cursor()
17 local sys = require "luci.sys"
18 local wa  = require "luci.tools.webadmin"
19 local fs  = require "nixio.fs"
20
21 m = Map("dhcp", "DHCP")
22
23 s = m:section(TypedSection, "dhcp", "DHCP-Server")
24 s.anonymous = true
25 s.addremove = false
26 s.dynamic = false
27
28 s:depends("interface", "lan")
29
30 enable = s:option(ListValue, "ignore", translate("enable"), "")
31 enable:value(0, translate("enable"))
32 enable:value(1, translate("disable"))
33
34 start = s:option(Value, "start", translate("First leased address"))
35 start.rmempty = true
36 start:depends("ignore", "0")
37
38
39 limit = s:option(Value, "limit", translate("Number of leased addresses"), "")
40 limit:depends("ignore", "0")
41
42 function limit.cfgvalue(self, section)
43         local value = Value.cfgvalue(self, section)
44         
45         if value then
46                 return tonumber(value) + 1
47         end 
48 end
49
50 function limit.write(self, section, value)
51         value = tonumber(value) - 1
52         return Value.write(self, section, value) 
53 end
54
55 limit.rmempty = true
56
57 time = s:option(Value, "leasetime")
58 time:depends("ignore", "0")
59 time.rmempty = true
60
61
62 local leasefn, leasefp, leases
63 uci:foreach("dhcp", "dnsmasq",
64  function(section)
65         leasefn = section.leasefile
66  end
67
68 local leasefp = leasefn and fs.access(leasefn) and io.lines(leasefn)
69 if leasefp then
70         leases = {}
71         for lease in leasefp do
72                 table.insert(leases, luci.util.split(lease, " "))
73         end
74 end
75
76 if leases then
77         v = m:section(Table, leases, translate("Active Leases"))
78         name = v:option(DummyValue, 4, translate("Hostname"))
79         function name.cfgvalue(self, ...)
80                 local value = DummyValue.cfgvalue(self, ...)
81                 return (value == "*") and "?" or value
82         end
83         ip = v:option(DummyValue, 3, translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
84         mac  = v:option(DummyValue, 2, translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"))
85         ltime = v:option(DummyValue, 1, translate("Leasetime remaining"))
86         function ltime.cfgvalue(self, ...)
87                 local value = DummyValue.cfgvalue(self, ...)
88                 return wa.date_format(os.difftime(tonumber(value), os.time()))
89         end
90 end
91
92 s2 = m:section(TypedSection, "host", translate("Static Leases"))
93 s2.addremove = true
94 s2.anonymous = true
95 s2.template = "cbi/tblsection"
96
97 name = s2:option(Value, "name", translate("Hostname"))
98 mac = s2:option(Value, "mac", translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"))
99 ip = s2:option(Value, "ip", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
100 sys.net.arptable(function(entry)
101         ip:value(entry["IP address"])
102         mac:value(
103                 entry["HW address"],
104                 entry["HW address"] .. " (" .. entry["IP address"] .. ")"
105         )
106 end)
107
108 return m
109