dhcp.lua 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
  3. -- Licensed to the public under the Apache License 2.0.
  4. local uci = require "luci.model.uci".cursor()
  5. local sys = require "luci.sys"
  6. local wa = require "luci.tools.webadmin"
  7. local fs = require "nixio.fs"
  8. m = Map("dhcp", "DHCP")
  9. s = m:section(TypedSection, "dhcp", "DHCP-Server")
  10. s.anonymous = true
  11. s.addremove = false
  12. s.dynamic = false
  13. s:depends("interface", "lan")
  14. enable = s:option(ListValue, "ignore", translate("enable"), "")
  15. enable:value(0, translate("enable"))
  16. enable:value(1, translate("disable"))
  17. start = s:option(Value, "start", translate("First leased address"))
  18. start.rmempty = true
  19. start:depends("ignore", "0")
  20. limit = s:option(Value, "limit", translate("Number of leased addresses"), "")
  21. limit:depends("ignore", "0")
  22. function limit.cfgvalue(self, section)
  23. local value = Value.cfgvalue(self, section)
  24. if value then
  25. return tonumber(value) + 1
  26. end
  27. end
  28. function limit.write(self, section, value)
  29. value = tonumber(value) - 1
  30. return Value.write(self, section, value)
  31. end
  32. limit.rmempty = true
  33. time = s:option(Value, "leasetime")
  34. time:depends("ignore", "0")
  35. time.rmempty = true
  36. local leasefn, leasefp, leases
  37. uci:foreach("dhcp", "dnsmasq",
  38. function(section)
  39. leasefn = section.leasefile
  40. end
  41. )
  42. local leasefp = leasefn and fs.access(leasefn) and io.lines(leasefn)
  43. if leasefp then
  44. leases = {}
  45. for lease in leasefp do
  46. table.insert(leases, luci.util.split(lease, " "))
  47. end
  48. end
  49. if leases then
  50. v = m:section(Table, leases, translate("Active Leases"))
  51. name = v:option(DummyValue, 4, translate("Hostname"))
  52. function name.cfgvalue(self, ...)
  53. local value = DummyValue.cfgvalue(self, ...)
  54. return (value == "*") and "?" or value
  55. end
  56. ip = v:option(DummyValue, 3, translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
  57. mac = v:option(DummyValue, 2, translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"))
  58. ltime = v:option(DummyValue, 1, translate("Leasetime remaining"))
  59. function ltime.cfgvalue(self, ...)
  60. local value = DummyValue.cfgvalue(self, ...)
  61. return wa.date_format(os.difftime(tonumber(value), os.time()))
  62. end
  63. end
  64. s2 = m:section(TypedSection, "host", translate("Static Leases"))
  65. s2.addremove = true
  66. s2.anonymous = true
  67. s2.template = "cbi/tblsection"
  68. name = s2:option(Value, "name", translate("Hostname"))
  69. mac = s2:option(Value, "mac", translate("<abbr title=\"Media Access Control\">MAC</abbr>-Address"))
  70. ip = s2:option(Value, "ip", translate("<abbr title=\"Internet Protocol Version 4\">IPv4</abbr>-Address"))
  71. sys.host_hints(function(m, v4, v6, name)
  72. if m and v4 then
  73. ip:value(v4)
  74. mac:value(m, "%s (%s)" %{ m, name or v4 })
  75. end
  76. end)
  77. return m