Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b9ded224a | |||
| d518637947 |
38
dns.lua
38
dns.lua
@@ -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 <hostname>\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")
|
||||
5
etc/ocnet.conf
Normal file
5
etc/ocnet.conf
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
gateway = "home", -- der lokale Router
|
||||
port = 5353,
|
||||
interval = 60 -- sekunden zwischen re-REG
|
||||
}
|
||||
7
etc/ocsense.conf
Normal file
7
etc/ocsense.conf
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
name = "home",
|
||||
port = 5353,
|
||||
parent = "net",
|
||||
children = {},
|
||||
local_domain = "home"
|
||||
}
|
||||
37
etc/rc.d/ocnet.lua
Normal file
37
etc/rc.d/ocnet.lua
Normal file
@@ -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)
|
||||
125
etc/rc.d/ocsense.lua
Normal file
125
etc/rc.d/ocsense.lua
Normal file
@@ -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
|
||||
60
ocsense.lua
60
ocsense.lua
@@ -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
|
||||
14
usr/bin/dns.lua
Normal file
14
usr/bin/dns.lua
Normal file
@@ -0,0 +1,14 @@
|
||||
local ocdns = require("ocdns")
|
||||
|
||||
local name = ...
|
||||
if not name then
|
||||
io.stderr:write("Usage: resolve <hostname>\n")
|
||||
return
|
||||
end
|
||||
|
||||
local uuid, err = ocdns.resolve(name)
|
||||
if uuid then
|
||||
print(uuid)
|
||||
else
|
||||
print("error:", err)
|
||||
end
|
||||
63
usr/bin/ping.lua
Normal file
63
usr/bin/ping.lua
Normal file
@@ -0,0 +1,63 @@
|
||||
local ocdns = require("ocdns")
|
||||
|
||||
local name = ...
|
||||
if not name then
|
||||
io.stderr:write("Usage: resolve <hostname>\n")
|
||||
return
|
||||
end
|
||||
|
||||
local uuid, err = ocdns.resolve(name)
|
||||
if uuid then
|
||||
print("error:", err)
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
local event = require("event")
|
||||
local minitel = require("minitel")
|
||||
|
||||
local function pingByUUID(targetUUID, count)
|
||||
count = count or 4
|
||||
local successCount = 0
|
||||
local totalTime = 0
|
||||
|
||||
print(string.format("PING %s via Minitel:", targetUUID))
|
||||
|
||||
for i = 1, count do
|
||||
local t1 = computer.uptime()
|
||||
local ok, err = pcall(function()
|
||||
minitel.send(targetUUID, 1, "ping") -- port 1 reserved for ping
|
||||
end)
|
||||
|
||||
if not ok then
|
||||
print(string.format("Error sending ping: %s", err))
|
||||
os.sleep(1)
|
||||
else
|
||||
local _, _, from, port, _, msg = event.pull(1, "minitel_message")
|
||||
local t2 = computer.uptime()
|
||||
|
||||
if msg == "pong" and from == targetUUID then
|
||||
local rtt = (t2 - t1) * 1000
|
||||
successCount = successCount + 1
|
||||
totalTime = totalTime + rtt
|
||||
print(string.format("Reply from %s: time=%.1f ms", from, rtt))
|
||||
else
|
||||
print("Request timed out.")
|
||||
end
|
||||
end
|
||||
os.sleep(1)
|
||||
end
|
||||
|
||||
print()
|
||||
print(string.format("Ping statistics for %s:", targetUUID))
|
||||
print(string.format(" Packets: Sent = %d, Received = %d, Lost = %d (%.0f%% loss)",
|
||||
count, successCount, count - successCount,
|
||||
((count - successCount) / count) * 100))
|
||||
|
||||
if successCount > 0 then
|
||||
print(string.format("Approx. round trip times in milli-seconds: avg = %.1f ms",
|
||||
totalTime / successCount))
|
||||
end
|
||||
end
|
||||
|
||||
pingByUUID(uuid, 4)
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user