applications: add luci-ahcp, support for the AHCP server package
[project/luci.git] / applications / luci-ahcp / luasrc / controller / ahcp.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2011 Jo-Philipp Wich <xm@subsignal.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: init.lua 6731 2011-01-14 19:44:03Z soma $
13 ]]--
14
15 module("luci.controller.ahcp", package.seeall)
16
17 function index()
18 if not nixio.fs.access("/etc/config/ahcpd") then
19 return
20 end
21
22 require("luci.i18n")
23 luci.i18n.loadc("ahcp")
24
25 entry({"admin", "network", "ahcpd"}, cbi("ahcp"), luci.i18n.translate("AHCP Server"), 90).i18n = "ahcp"
26 entry({"admin", "network", "ahcpd", "status"}, call("ahcp_status"))
27 end
28
29 function ahcp_status()
30 local nfs = require "nixio.fs"
31 local uci = require "luci.model.uci".cursor()
32 local lsd = uci:get_first("ahcpd", "ahcpd", "lease_dir") or "/var/lib/leases"
33 local idf = uci:get_first("ahcpd", "ahcpd", "id_file") or "/var/lib/ahcpd-unique-id"
34
35 local rv = {
36 uid = "00:00:00:00:00:00:00:00",
37 leases = { }
38 }
39
40 idf = nfs.readfile(idf)
41 if idf and #idf == 8 then
42 rv.uid = "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X" %{ idf:byte(1, 8) }
43 end
44
45 local itr = nfs.dir(lsd)
46 if itr then
47 local addr
48 for addr in itr do
49 if addr:match("^%d+%.%d+%.%d+%.%d+$") then
50 local s = nfs.stat(lsd .. "/" .. addr)
51 rv.leases[#rv.leases+1] = {
52 addr = addr,
53 age = s and (os.time() - s.mtime) or 0
54 }
55 end
56 end
57 end
58
59 table.sort(rv.leases, function(a, b) return a.age < b.age end)
60
61 luci.http.prepare_content("application/json")
62 luci.http.write_json(rv)
63 end