Skip to content
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/shared-state-odhcpd_leases/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
include ../../libremesh.mk

define Package/$(PKG_NAME)
SECTION:=lime
CATEGORY:=LibreMesh
TITLE:=odhcpd leases module for shared-state
DEPENDS:=+lua +luci-lib-jsonc +shared-state-async +odhcpd
PKGARCH:=all
endef

define Package/$(PKG_NAME)/description
Synchronize external DHCP leases between LibreMesh nodes by
watching odhcpd’s lease file, publishing updates over shared-state-async
and injecting remote leases locally.
endef

$(eval $(call BuildPackage,$(PKG_NAME)))
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh
CRDT='odhcpd-leases'
LEASEFILE='/tmp/ethers.mesh'
TRIGGERFILE='usr/share/shared-state/publishers/shared-state-publish_odhcpd_leases'
mSc='odhcpd_leases'

uci -q set shared-state.$mSc=dataType
uci -q set shared-state.$mSc.name="$CRDT"
uci -q set shared-state.$mSc.scope='community'
uci -q set shared-state.$mSc.update_interval='120'
uci -q set shared-state.$mSc.ttl='1200'
uci commit shared-state

uci -q set dhcp.odhcpd.leasetrigger="$TRIGGERFILE"
uci -q set dhcp.odhcpd.maindhcp='1'
uci commit dhcp

[ -e /etc/ethers ] || ln -s "$LEASEFILE" /etc/ethers
grep -q 'sync odhcpd-leases' /etc/crontabs/root || \
echo '*/5 * * * * shared-state-async sync odhcpd-leases >/dev/null 2>&1' >> /etc/crontabs/root
Comment thread
AguTrachta marked this conversation as resolved.
Outdated

/etc/init.d/cron restart
/etc/init.d/odhcpd reload
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/lua

local JSON = require('luci.jsonc')

local OUTPUT_FILE = '/tmp/ethers.mesh'
local TMP_FILE = OUTPUT_FILE .. '.new'

local leases = JSON.parse(io.stdin:read('*a')) or {}

local hostname_file = io.open('/proc/sys/kernel/hostname')
local node_hostname = hostname_file:read('*l')
hostname_file:close()

local out_handle = io.open(TMP_FILE, 'w')

for ip, data in pairs(leases) do

if data and data.mData and data.mData.mac and data.mAuthor ~= node_hostname then
out_handle:write(string.format('%s %s\n', data.mData.mac, ip)) -- Format: MAC IP
end
end
out_handle:close()
os.execute('mv "' .. TMP_FILE .. '" "' .. OUTPUT_FILE .. '"')

os.execute('/etc/init.d/odhcpd reload')
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/lua

local JSON = require("luci.jsonc")
local CRDT = "odhcpd-leases"


local handle = io.popen("ubus call dhcp ipv4leases '{}' 2>/dev/null")
local ubus_output = handle:read("*a")
handle:close()


local ubus_data = JSON.parse(ubus_output or "{}")

local output_table = {}


if ubus_data and ubus_data.device then

for device_name, device_data in pairs(ubus_data.device) do
if device_data and device_data.leases then

for _, lease in ipairs(device_data.leases) do

if lease.address and lease.mac then

output_table[lease.address] = {
hostname = lease.hostname or "",
mac = lease.mac
}
end
end
end
end
end


local final_json_string = JSON.stringify(output_table)


local pipe = io.popen("shared-state-async insert " .. CRDT, "w")
if pipe then
pipe:write(final_json_string)
pipe:close()
end


os.execute("/usr/sbin/odhcpd-update >/dev/null 2>&1")