2022-07-20 00:42:51 -06:00
|
|
|
#!/usr/bin/lua
|
|
|
|
--[[
|
|
|
|
|
2024-05-29 01:45:25 -06:00
|
|
|
Part of AREDN® -- Used for creating Amateur Radio Emergency Data Networks
|
2023-12-12 21:01:23 -07:00
|
|
|
Copyright (C) 2022-2023 Tim Wilkinson
|
2022-07-20 00:42:51 -06:00
|
|
|
See Contributors file for additional contributors
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation version 3 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Additional Terms:
|
|
|
|
|
2024-05-29 01:45:25 -06:00
|
|
|
Additional use restrictions exist on the AREDN® trademark and logo.
|
2022-07-20 00:42:51 -06:00
|
|
|
See AREDNLicense.txt for more info.
|
|
|
|
|
2024-05-29 01:45:25 -06:00
|
|
|
Attributions to the AREDN® Project must be retained in the source code.
|
2022-07-20 00:42:51 -06:00
|
|
|
If importing this code into a new or existing project attribution
|
2024-05-29 01:45:25 -06:00
|
|
|
to the AREDN® project must be added to the source code.
|
2022-07-20 00:42:51 -06:00
|
|
|
|
|
|
|
You must not misrepresent the origin of the material contained within.
|
|
|
|
|
|
|
|
Modified versions must be modified to attribute to the original source
|
|
|
|
and be marked in reasonable ways as differentiate it from the original
|
|
|
|
version
|
|
|
|
|
|
|
|
--]]
|
|
|
|
|
2023-12-12 21:01:23 -07:00
|
|
|
require("uci")
|
|
|
|
require("aredn.services")
|
2022-07-20 00:42:51 -06:00
|
|
|
|
2023-12-12 21:01:23 -07:00
|
|
|
local cursor = uci.cursor()
|
|
|
|
|
2024-01-03 01:45:49 -07:00
|
|
|
-- Current nameservice state
|
2023-12-12 21:01:23 -07:00
|
|
|
local ns = cursor:get_all("olsrd", "nameservice")
|
|
|
|
if not ns then
|
|
|
|
print("Missing nameservice")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local cnames = {}
|
|
|
|
for _, name in ipairs(ns.name)
|
|
|
|
do
|
|
|
|
cnames[name] = true
|
|
|
|
end
|
|
|
|
local chosts = {}
|
2024-01-03 01:45:49 -07:00
|
|
|
for _, iphost in ipairs(ns.hosts or {})
|
|
|
|
do
|
|
|
|
chosts[iphost] = true
|
|
|
|
end
|
2023-12-12 21:01:23 -07:00
|
|
|
local cservices = {}
|
|
|
|
for _, service in ipairs(ns.service or {})
|
2022-07-20 00:42:51 -06:00
|
|
|
do
|
2023-12-12 21:01:23 -07:00
|
|
|
cservices[service] = true
|
2022-07-20 00:42:51 -06:00
|
|
|
end
|
|
|
|
|
2023-12-12 21:01:23 -07:00
|
|
|
-- Work out the differences between the validated state and the current state
|
|
|
|
local names, hosts, services = aredn.services.get(true)
|
|
|
|
local nnames = false
|
|
|
|
for _, name in ipairs(names)
|
|
|
|
do
|
|
|
|
if cnames[name] then
|
|
|
|
cnames[name] = nil
|
|
|
|
else
|
|
|
|
nnames = true
|
|
|
|
end
|
|
|
|
end
|
2024-01-03 01:45:49 -07:00
|
|
|
local nhosts = false
|
2023-12-12 21:01:23 -07:00
|
|
|
for _, host in ipairs(hosts)
|
2022-07-20 00:42:51 -06:00
|
|
|
do
|
2024-01-03 01:45:49 -07:00
|
|
|
local iphost = host.ip .. " " .. host.host
|
|
|
|
if chosts[iphost] then
|
|
|
|
chosts[iphost] = nil
|
2023-12-12 21:01:23 -07:00
|
|
|
else
|
2024-01-03 01:45:49 -07:00
|
|
|
nhosts = true
|
2023-12-12 21:01:23 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
local nservices = false
|
|
|
|
for _, service in ipairs(services)
|
|
|
|
do
|
|
|
|
if cservices[service] then
|
|
|
|
cservices[service] = nil
|
|
|
|
else
|
|
|
|
nservices = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function not_empty(h)
|
|
|
|
for _, _ in pairs(h)
|
|
|
|
do
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
2022-07-20 00:42:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
local change = false
|
2023-12-12 21:01:23 -07:00
|
|
|
|
|
|
|
-- Apply the changes
|
|
|
|
if nnames or not_empty(cnames) then
|
2024-01-03 01:45:49 -07:00
|
|
|
cursor:set("olsrd", "nameservice", "name", names)
|
2023-12-12 21:01:23 -07:00
|
|
|
change = true
|
|
|
|
end
|
2024-01-03 01:45:49 -07:00
|
|
|
if nhosts or not_empty(chosts) then
|
|
|
|
for i, host in ipairs(hosts)
|
2023-12-12 21:01:23 -07:00
|
|
|
do
|
2024-01-03 01:45:49 -07:00
|
|
|
hosts[i] = host.ip .. " " .. host.host
|
2022-07-20 00:42:51 -06:00
|
|
|
end
|
2024-05-21 22:02:47 -06:00
|
|
|
if #hosts > 0 then
|
|
|
|
cursor:set("olsrd", "nameservice", "hosts", hosts)
|
|
|
|
else
|
|
|
|
cursor:delete("olsrd", "nameservice", "hosts")
|
|
|
|
end
|
2023-12-12 21:01:23 -07:00
|
|
|
change = true
|
|
|
|
end
|
|
|
|
if nservices or not_empty(cservices) then
|
2024-05-21 22:02:47 -06:00
|
|
|
if #services > 0 then
|
|
|
|
cursor:set("olsrd", "nameservice", "service", services)
|
|
|
|
else
|
|
|
|
cursor:delete("olsrd", "nameservice", "service")
|
|
|
|
end
|
2023-12-12 21:01:23 -07:00
|
|
|
change = true
|
2022-07-20 00:42:51 -06:00
|
|
|
end
|
|
|
|
|
2023-12-12 21:01:23 -07:00
|
|
|
-- If services have changed we need to restart olsrd
|
2022-07-20 00:42:51 -06:00
|
|
|
if change then
|
2023-12-12 21:01:23 -07:00
|
|
|
cursor:commit("olsrd")
|
2022-07-20 00:42:51 -06:00
|
|
|
print("Change")
|
2024-01-03 01:45:49 -07:00
|
|
|
os.execute("/usr/local/bin/restart-services.sh --force olsrd")
|
2022-07-20 00:42:51 -06:00
|
|
|
else
|
|
|
|
print("Unchange")
|
|
|
|
end
|