Skip to content

Commit

Permalink
Merge pull request #2742 from Ansuel/odhcp-lease
Browse files Browse the repository at this point in the history
luci-base: handle dhcp lease from odhcpd
  • Loading branch information
jow- committed Jun 3, 2019
2 parents 4bbc033 + bb34a31 commit c8b7e76
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
40 changes: 39 additions & 1 deletion modules/luci-base/luasrc/sys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ end
net = {}

local function _nethints(what, callback)
local _, k, e, mac, ip, name
local _, k, e, mac, ip, name, duid, iaid
local cur = uci.cursor()
local ifn = { }
local hosts = { }
Expand Down Expand Up @@ -189,6 +189,24 @@ local function _nethints(what, callback)
end
end
)

cur:foreach("dhcp", "odhcpd",
function(s)
if type(s.leasefile) == "string" and fs.access(s.leasefile) then
for e in io.lines(s.leasefile) do
duid, iaid, name, _, ip = e:match("^# %S+ (%S+) (%S+) (%S+) (-?%d+) %S+ %S+ ([0-9a-f:.]+)/[0-9]+")
mac = net.duid_to_mac(duid)
if mac then
if ip and iaid == "ipv4" then
_add(what, mac, ip, nil, name ~= "*" and name)
elseif ip then
_add(what, mac, nil, ip, name ~= "*" and name)
end
end
end
end
end
)

cur:foreach("dhcp", "host",
function(s)
Expand Down Expand Up @@ -386,6 +404,26 @@ function net.devices()
return devs
end

function net.duid_to_mac(duid)
local b1, b2, b3, b4, b5, b6

if type(duid) == "string" then
-- DUID-LLT / Ethernet
if #duid == 28 then
b1, b2, b3, b4, b5, b6 = duid:match("^00010001(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)%x%x%x%x%x%x%x%x$")

-- DUID-LL / Ethernet
elseif #duid == 20 then
b1, b2, b3, b4, b5, b6 = duid:match("^00030001(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)$")

-- DUID-LL / Ethernet (Without Header)
elseif #duid == 12 then
b1, b2, b3, b4, b5, b6 = duid:match("^(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)$")
end
end

return b1 and luci.ip.checkmac(table.concat({ b1, b2, b3, b4, b5, b6 }, ":"))
end

process = {}

Expand Down
17 changes: 1 addition & 16 deletions modules/luci-base/luasrc/tools/status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,6 @@ module("luci.tools.status", package.seeall)
local uci = require "luci.model.uci".cursor()
local ipc = require "luci.ip"

local function duid_to_mac(duid)
local b1, b2, b3, b4, b5, b6

-- DUID-LLT / Ethernet
if type(duid) == "string" and #duid == 28 then
b1, b2, b3, b4, b5, b6 = duid:match("^00010001(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)%x%x%x%x%x%x%x%x$")

-- DUID-LL / Ethernet
elseif type(duid) == "string" and #duid == 20 then
b1, b2, b3, b4, b5, b6 = duid:match("^00030001(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)$")
end

return b1 and ipc.checkmac(table.concat({ b1, b2, b3, b4, b5, b6 }, ":"))
end

local function dhcp_leases_common(family)
local rv = { }
local nfs = require "nixio.fs"
Expand Down Expand Up @@ -93,7 +78,7 @@ local function dhcp_leases_common(family)
elseif ip and iaid == "ipv4" and family == 4 then
rv[#rv+1] = {
expires = (expire >= 0) and os.difftime(expire, os.time()),
macaddr = ipc.checkmac(duid:gsub("^(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)(%x%x)$", "%1:%2:%3:%4:%5:%6")) or "00:00:00:00:00:00",
macaddr = sys.net.duid_to_mac(duid) or "00:00:00:00:00:00",
ipaddr = ip,
hostname = (name ~= "-") and name
}
Expand Down

0 comments on commit c8b7e76

Please sign in to comment.