modules/admin-full: switch static lease page to /etc/config/dhcp, add better labels...
[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
15 local uci = require "luci.model.uci".cursor()
16 local sys = require "luci.sys"
17 local wa  = require "luci.tools.webadmin"
18 local fs  = require "nixio.fs"
19
20 m2 = Map("dhcp", translate("DHCP Leases"),
21         translate("Static leases are used to assign fixed IP addresses and symbolic hostnames to " ..
22                 "DHCP clients. They are also required for non-dynamic interface configurations where " ..
23                 "only hosts with a corresponding lease are served."))
24
25 local leasefn, leasefp, leases
26 uci:foreach("dhcp", "dnsmasq",
27  function(section)
28         leasefn = section.leasefile
29  end
30
31 local leasefp = leasefn and fs.access(leasefn) and io.lines(leasefn)
32 if leasefp then
33         leases = {}
34         for lease in leasefp do
35                 table.insert(leases, luci.util.split(lease, " "))
36         end
37 end
38
39 if leases then
40         v = m2:section(Table, leases, translate("Active Leases"))
41         ip = v:option(DummyValue, 3, translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
42         
43         mac  = v:option(DummyValue, 2, translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"))
44         
45         ltime = v:option(DummyValue, 1, translate("Leasetime remaining"))
46         function ltime.cfgvalue(self, ...)
47                 local value = DummyValue.cfgvalue(self, ...)
48                 return wa.date_format(os.difftime(tonumber(value), os.time()))
49         end
50 end
51
52 s = m2:section(TypedSection, "host", translate("Static Leases"),
53         translate("Use the <em>Add</em> Button to add a new lease entry. The <em>MAC-Address</em> " ..
54                 "indentifies the host, the <em>IPv4-Address</em> specifies to the fixed address to " ..
55                 "use and the <em>Hostname</em> is assigned as symbolic name to the requesting host."))
56
57 s.addremove = true
58 s.anonymous = true
59 s.template = "cbi/tblsection"
60
61 name = s:option(Value, "name", translate("Hostname"))
62 mac = s:option(Value, "mac", translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"))
63 ip = s:option(Value, "ip", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
64 sys.net.arptable(function(entry)
65         ip:value(entry["IP address"])
66         mac:value(
67                 entry["HW address"],
68                 entry["HW address"] .. " (" .. entry["IP address"] .. ")"
69         )
70 end)
71
72         
73 return m2