From d518637947606a50f9c89e7f74dc5d7b1e2c4166 Mon Sep 17 00:00:00 2001 From: edraft Date: Wed, 5 Nov 2025 23:35:45 +0100 Subject: [PATCH] Added new package --- dns.lua | 38 --------- etc/ocnet.conf | 5 ++ etc/ocsense.conf | 7 ++ etc/rc.d/ocnet.lua | 37 +++++++++ etc/rc.d/ocsense.lua | 125 ++++++++++++++++++++++++++++++ ocsense.lua | 60 -------------- usr/bin/dns.lua | 14 ++++ ocnet.lua => usr/lib/ocnetlib.lua | 2 - 8 files changed, 188 insertions(+), 100 deletions(-) delete mode 100644 dns.lua create mode 100644 etc/ocnet.conf create mode 100644 etc/ocsense.conf create mode 100644 etc/rc.d/ocnet.lua create mode 100644 etc/rc.d/ocsense.lua delete mode 100644 ocsense.lua create mode 100644 usr/bin/dns.lua rename ocnet.lua => usr/lib/ocnetlib.lua (87%) diff --git a/dns.lua b/dns.lua deleted file mode 100644 index dbe2edb..0000000 --- a/dns.lua +++ /dev/null @@ -1,38 +0,0 @@ -local event = require("event") -local minitel = require("minitel") -local computer = require("computer") - -local target = ... -if not target then - io.stderr:write("Usage: resolve \n") - return -end - -local GATEWAY = "home1" -local PORT = 5353 - --- eigenen Namen holen -local f = io.open("/etc/hostname", "r") -local me = f and f:read("*l") or "client" -if f then f:close() end - --- Anfrage senden -minitel.usend(GATEWAY, PORT, "Q " .. target .. " " .. me) - --- auf Antwort warten -local deadline = computer.uptime() + 3 -while computer.uptime() < deadline do - local _, from, port, data = event.pull(deadline - computer.uptime(), "net_msg") - if port == PORT and type(data) == "string" then - local cmd, a, b = data:match("^(%S+)%s+(%S+)%s*(%S*)") - if cmd == "A" and a == target then - print(b) -- b = Hardware-UUID - return - elseif cmd == "NX" and a == target then - print("NXDOMAIN: " .. target) - return - end - end -end - -print("Timeout") diff --git a/etc/ocnet.conf b/etc/ocnet.conf new file mode 100644 index 0000000..6ae6e85 --- /dev/null +++ b/etc/ocnet.conf @@ -0,0 +1,5 @@ +{ + gateway = "home", -- der lokale Router + port = 5353, + interval = 60 -- sekunden zwischen re-REG +} \ No newline at end of file diff --git a/etc/ocsense.conf b/etc/ocsense.conf new file mode 100644 index 0000000..015304c --- /dev/null +++ b/etc/ocsense.conf @@ -0,0 +1,7 @@ +{ + name = "home", + port = 5353, + parent = "net", + children = {}, + local_domain = "home" +} \ No newline at end of file diff --git a/etc/rc.d/ocnet.lua b/etc/rc.d/ocnet.lua new file mode 100644 index 0000000..ddd1e9d --- /dev/null +++ b/etc/rc.d/ocnet.lua @@ -0,0 +1,37 @@ +local component = require("component") +local minitel = require("minitel") + +local function loadConf() + local f = io.open("/etc/ocnet.conf", "r") + if not f then + return { gateway = "home", port = 5353 } + end + local txt = f:read("*a"); f:close() + local ok, t = pcall(load("return " .. txt)) + if ok and type(t) == "table" then + if not t.gateway then t.gateway = "home1" end + if not t.port then t.port = 5353 end + return t + end + return { gateway = "home1", port = 5353 } +end + +local conf = loadConf() + +local f = io.open("/etc/hostname", "r") +local hostname = f and f:read("*l") or "unknown" +if f then f:close() end + +local modemUUID +for addr in component.list("modem") do + modemUUID = addr + break +end + +if not modemUUID then + io.stderr:write("[ocnet] kein modem gefunden\n") + return +end + +print(string.format("[ocnet] REG %s (%s) -> %s:%d", hostname, modemUUID, conf.gateway, conf.port)) +minitel.usend(conf.gateway, conf.port, "REG " .. hostname .. " " .. modemUUID) \ No newline at end of file diff --git a/etc/rc.d/ocsense.lua b/etc/rc.d/ocsense.lua new file mode 100644 index 0000000..e925ca2 --- /dev/null +++ b/etc/rc.d/ocsense.lua @@ -0,0 +1,125 @@ +local event = require("event") +local minitel = require("minitel") + +local function loadConf() + local f = io.open("/etc/ocsense.conf", "r") + if not f then + return { + name = "home", + port = 5353, + parent = "net", + children = {}, + local_domain = "home", + } + end + local text = f:read("*a") + f:close() + local ok, t = pcall(load("return " .. text)) + if ok and type(t) == "table" then + if not t.port then t.port = 5353 end + if not t.children then t.children = {} end + return t + end + return { + name = "home1", + port = 5353, + parent = "net", + children = {}, + local_domain = "home", + } +end + +local cfg = loadConf() +local PORT = cfg.port +local records = {} +local running = true + +local local_suffixes = {} +if cfg.local_domain and cfg.local_domain ~= "" then + table.insert(local_suffixes, cfg.local_domain) + if cfg.parent and cfg.parent ~= "" then + table.insert(local_suffixes, cfg.local_domain .. "." .. cfg.parent) + end +end + +print(string.format("[ocsense] %s läuft auf port %d", cfg.name, cfg.port)) +if cfg.parent then + print("[ocsense] parent:", cfg.parent) +end +if #cfg.children > 0 then + print("[ocsense] children:", table.concat(cfg.children, ", ")) +end +print("[ocsense] lokale suffixe:", table.concat(local_suffixes, ", ")) +print("[ocsense] Strg+C zum Beenden") + +local function endsWith(str, suffix) + return suffix ~= "" and str:sub(-#suffix) == suffix +end + +local function stripLocalSuffixes(name) + for _, suf in ipairs(local_suffixes) do + if endsWith(name, suf) then + local cut = #name - #suf + if name:sub(cut, cut) == "." then + return name:sub(1, cut - 1) + else + return name:sub(1, cut) + end + end + end + return name +end + +while running do + local ev, fromName, port, data, fromAddr = event.pull() + if ev == "interrupted" then + print("[ocsense] beendet") + running = false + + elseif ev == "net_msg" and port == PORT and type(data) == "string" then + local cmd, a, b = data:match("^(%S+)%s*(%S*)%s*(%S*)") + + if cmd == "REG" and a ~= "" and b ~= "" then + records[a] = b + print("[ocsense] REG", a, "=>", b) + + elseif cmd == "Q" and a ~= "" then + local replyto = (b ~= "" and b) or fromName + local target = a + + local addr = records[target] + + if not addr then + local stripped = stripLocalSuffixes(target) + if stripped ~= target then + addr = records[stripped] + if not addr then + stripped = stripLocalSuffixes(stripped) + addr = records[stripped] + end + end + end + + if addr then + minitel.usend(replyto, PORT, "A " .. target .. " " .. addr) + else + local forwarded = false + for _, child in ipairs(cfg.children) do + if endsWith(target, child) then + minitel.usend(child, PORT, "Q " .. target .. " " .. replyto) + forwarded = true + break + end + end + + if not forwarded then + if cfg.parent and cfg.parent ~= "" then + minitel.usend(cfg.parent, PORT, "Q " .. target .. " " .. replyto) + else + minitel.usend(replyto, PORT, "NX " .. target) + end + end + end + end + end +end \ No newline at end of file diff --git a/ocsense.lua b/ocsense.lua deleted file mode 100644 index f18ab34..0000000 --- a/ocsense.lua +++ /dev/null @@ -1,60 +0,0 @@ -local event = require("event") -local minitel = require("minitel") -local serialization = require("serialization") - -local PORT = 5353 -local LOCAL_DOMAIN = "home1" -- oder "home1.net" -local records = {} -local running = true - -print("[ocsense] DNS/UUID-Server gestartet auf port " .. PORT) -print("[ocsense] Strg+C zum Beenden") - -local function stripLocalDomain(name) - if name:sub(-#LOCAL_DOMAIN) == LOCAL_DOMAIN then - local dot = name:sub(-( #LOCAL_DOMAIN + 1 ), -( #LOCAL_DOMAIN + 1 )) - if dot == "." then - return name:sub(1, -( #LOCAL_DOMAIN + 2 )) - else - return name:sub(1, -( #LOCAL_DOMAIN + 1 )) - end - end - return name -end - -while running do - local ev, fromName, port, data, fromAddr = event.pull() - if ev == "interrupted" then - print("[ocsense] beendet durch Strg+C") - running = false - - elseif ev == "net_msg" and port == PORT and type(data) == "string" then - local cmd, a, b = data:match("^(%S+)%s*(%S*)%s*(%S*)") - - if cmd == "REG" and a ~= "" and b ~= "" then - -- a = hostname, b = hardware UUID - records[a] = b - print("[ocsense] REG", a, "=>", b) - - elseif cmd == "Q" and a ~= "" then - local replyto = (b ~= "" and b) or fromName - local addr = records[a] - - -- lokale Domain abstreifen, falls nötig - if not addr then - local short = stripLocalDomain(a) - if short ~= a then - addr = records[short] - end - end - - if addr then - minitel.usend(replyto, PORT, "A " .. a .. " " .. addr) - print("[ocsense] A", a, "->", addr) - else - minitel.usend(replyto, PORT, "NX " .. a) - print("[ocsense] NX", a) - end - end - end -end diff --git a/usr/bin/dns.lua b/usr/bin/dns.lua new file mode 100644 index 0000000..0a86b5d --- /dev/null +++ b/usr/bin/dns.lua @@ -0,0 +1,14 @@ +local ocdns = require("ocdns") + +local name = ... +if not name then + io.stderr:write("Usage: resolve \n") + return +end + +local uuid, err = ocdns.resolve(name) +if uuid then + print(uuid) +else + print("error:", err) +end diff --git a/ocnet.lua b/usr/lib/ocnetlib.lua similarity index 87% rename from ocnet.lua rename to usr/lib/ocnetlib.lua index a6aa966..e803223 100644 --- a/ocnet.lua +++ b/usr/lib/ocnetlib.lua @@ -4,12 +4,10 @@ local minitel = require("minitel") local GATEWAY = "home1" local PORT = 5353 --- hostname aus /etc/hostname lesen local f = io.open("/etc/hostname", "r") local hostname = f and f:read("*l") or "unknown" if f then f:close() end --- Hardware-UUID der ersten Netzwerkkarte finden local modemUUID for addr, t in component.list("modem") do modemUUID = addr