2018-02-16 21:36:00 +11:00
|
|
|
--[[
|
|
|
|
packet format:
|
|
|
|
packetID: random string to differentiate
|
|
|
|
packetType:
|
|
|
|
- 0: unreliable
|
|
|
|
- 1: reliable, requires ack
|
|
|
|
- 2: ack packet
|
|
|
|
destination: end destination hostname
|
|
|
|
sender: original sender of packet
|
|
|
|
data: the actual packet data, duh.
|
|
|
|
]]--
|
|
|
|
|
|
|
|
local listeners = {}
|
|
|
|
local timers = {}
|
|
|
|
|
2018-07-27 10:43:56 +10:00
|
|
|
local cfg = {}
|
|
|
|
|
2018-02-16 21:36:00 +11:00
|
|
|
local event = require "event"
|
|
|
|
local component = require "component"
|
|
|
|
local computer = require "computer"
|
2018-07-27 10:43:56 +10:00
|
|
|
local serial = require "serialization"
|
2019-04-18 19:35:31 +10:00
|
|
|
|
2018-07-27 10:43:56 +10:00
|
|
|
local hostname = computer.address():sub(1,8)
|
2018-02-16 21:36:00 +11:00
|
|
|
local modems = {}
|
2019-04-18 19:35:31 +10:00
|
|
|
|
|
|
|
cfg.debug = false
|
2018-07-27 10:43:56 +10:00
|
|
|
cfg.port = 4096
|
|
|
|
cfg.retry = 10
|
2020-03-23 12:09:53 +11:00
|
|
|
cfg.retrycount = 3
|
2018-07-27 10:43:56 +10:00
|
|
|
cfg.route = true
|
2018-02-16 21:36:00 +11:00
|
|
|
|
|
|
|
--[[
|
|
|
|
LKR format:
|
|
|
|
address {
|
|
|
|
local hardware address
|
|
|
|
remote hardware address
|
|
|
|
time last received
|
|
|
|
}
|
|
|
|
]]--
|
2018-07-27 10:43:56 +10:00
|
|
|
cfg.sroutes = {}
|
|
|
|
local rcache = setmetatable({},{__index=cfg.sroutes})
|
|
|
|
cfg.rctime = 15
|
2018-02-16 21:36:00 +11:00
|
|
|
|
|
|
|
--[[
|
|
|
|
packet queue format:
|
|
|
|
{
|
|
|
|
packetID,
|
|
|
|
packetType
|
|
|
|
destination,
|
|
|
|
data,
|
|
|
|
timestamp,
|
|
|
|
attempts
|
|
|
|
}
|
|
|
|
]]--
|
|
|
|
local pqueue = {}
|
|
|
|
|
|
|
|
-- packet cache: [packet ID]=uptime
|
|
|
|
local pcache = {}
|
2018-07-27 10:43:56 +10:00
|
|
|
cfg.pctime = 30
|
2018-02-16 21:36:00 +11:00
|
|
|
|
|
|
|
local function dprint(...)
|
2018-07-27 10:43:56 +10:00
|
|
|
if cfg.debug then
|
2018-02-16 21:36:00 +11:00
|
|
|
print(...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-27 10:43:56 +10:00
|
|
|
local function saveconfig()
|
|
|
|
local f = io.open("/etc/minitel.cfg","wb")
|
|
|
|
if f then
|
|
|
|
f:write(serial.serialize(cfg))
|
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local function loadconfig()
|
|
|
|
local f = io.open("/etc/minitel.cfg","rb")
|
|
|
|
if f then
|
|
|
|
local newcfg = serial.unserialize(f:read("*a"))
|
|
|
|
f:close()
|
|
|
|
for k,v in pairs(newcfg) do
|
|
|
|
cfg[k] = v
|
|
|
|
end
|
|
|
|
else
|
|
|
|
saveconfig()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-16 21:36:00 +11:00
|
|
|
function start()
|
2018-07-27 10:43:56 +10:00
|
|
|
loadconfig()
|
2018-02-16 21:36:00 +11:00
|
|
|
local f=io.open("/etc/hostname","rb")
|
|
|
|
if f then
|
|
|
|
hostname = f:read()
|
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
print("Hostname: "..hostname)
|
2019-04-18 19:35:31 +10:00
|
|
|
|
|
|
|
if next(listeners) ~= nil then return end
|
|
|
|
|
2018-04-15 21:42:57 +10:00
|
|
|
modems={}
|
2018-02-16 21:36:00 +11:00
|
|
|
for a,t in component.list("modem") do
|
|
|
|
modems[#modems+1] = component.proxy(a)
|
|
|
|
end
|
|
|
|
for k,v in ipairs(modems) do
|
2018-07-27 10:43:56 +10:00
|
|
|
v.open(cfg.port)
|
|
|
|
print("Opened port "..cfg.port.." on "..v.address)
|
2018-02-16 21:36:00 +11:00
|
|
|
end
|
2018-03-16 17:09:12 +11:00
|
|
|
for a,t in component.list("tunnel") do
|
|
|
|
modems[#modems+1] = component.proxy(a)
|
|
|
|
end
|
2018-02-16 21:36:00 +11:00
|
|
|
|
|
|
|
local function genPacketID()
|
|
|
|
local npID = ""
|
|
|
|
for i = 1, 16 do
|
|
|
|
npID = npID .. string.char(math.random(32,126))
|
|
|
|
end
|
|
|
|
return npID
|
|
|
|
end
|
|
|
|
|
2019-04-18 19:35:31 +10:00
|
|
|
local function sendPacket(packetID,packetType,dest,sender,vPort,data,repeatingFrom)
|
2018-02-16 21:36:00 +11:00
|
|
|
if rcache[dest] then
|
2018-07-27 10:43:56 +10:00
|
|
|
dprint("Cached", rcache[dest][1],"send",rcache[dest][2],cfg.port,packetID,packetType,dest,sender,vPort,data)
|
2018-03-16 17:09:12 +11:00
|
|
|
if component.type(rcache[dest][1]) == "modem" then
|
2018-07-27 10:43:56 +10:00
|
|
|
component.invoke(rcache[dest][1],"send",rcache[dest][2],cfg.port,packetID,packetType,dest,sender,vPort,data)
|
2018-03-16 17:15:40 +11:00
|
|
|
elseif component.type(rcache[dest][1]) == "tunnel" then
|
2018-07-27 10:43:56 +10:00
|
|
|
component.invoke(rcache[dest][1],"send",packetID,packetType,dest,sender,vPort,data)
|
2018-03-16 17:09:12 +11:00
|
|
|
end
|
2018-02-16 21:36:00 +11:00
|
|
|
else
|
2018-07-27 10:43:56 +10:00
|
|
|
dprint("Not cached", cfg.port,packetID,packetType,dest,sender,vPort,data)
|
2018-02-16 21:36:00 +11:00
|
|
|
for k,v in pairs(modems) do
|
2019-04-18 19:35:31 +10:00
|
|
|
-- do not send message back to the wired or linked modem it came from
|
|
|
|
-- the check for tunnels is for short circuiting `v.isWireless()`, which does not exist for tunnels
|
|
|
|
if v.address ~= repeatingFrom or (v.type ~= "tunnel" and v.isWireless()) then
|
|
|
|
if v.type == "modem" then
|
|
|
|
v.broadcast(cfg.port,packetID,packetType,dest,sender,vPort,data)
|
|
|
|
elseif v.type == "tunnel" then
|
|
|
|
v.send(packetID,packetType,dest,sender,vPort,data)
|
|
|
|
end
|
2018-03-16 17:09:12 +11:00
|
|
|
end
|
2018-02-16 21:36:00 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function pruneCache()
|
|
|
|
for k,v in pairs(rcache) do
|
|
|
|
dprint(k,v[3],computer.uptime())
|
|
|
|
if v[3] < computer.uptime() then
|
|
|
|
rcache[k] = nil
|
|
|
|
dprint("pruned "..k.." from routing cache")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for k,v in pairs(pcache) do
|
|
|
|
if v < computer.uptime() then
|
|
|
|
pcache[k] = nil
|
|
|
|
dprint("pruned "..k.." from packet cache")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function checkPCache(packetID)
|
|
|
|
dprint(packetID)
|
|
|
|
for k,v in pairs(pcache) do
|
|
|
|
dprint(k)
|
|
|
|
if k == packetID then return true end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2018-07-27 10:43:56 +10:00
|
|
|
local function processPacket(_,localModem,from,pport,_,packetID,packetType,dest,sender,vPort,data)
|
2018-02-16 21:36:00 +11:00
|
|
|
pruneCache()
|
2018-07-27 10:43:56 +10:00
|
|
|
if pport == cfg.port or pport == 0 then -- for linked cards
|
|
|
|
dprint(cfg.port,vPort,packetType,dest)
|
2018-02-16 21:36:00 +11:00
|
|
|
if checkPCache(packetID) then return end
|
|
|
|
if dest == hostname then
|
|
|
|
if packetType == 1 then
|
2018-07-27 10:43:56 +10:00
|
|
|
sendPacket(genPacketID(),2,sender,hostname,vPort,packetID)
|
2018-02-16 21:36:00 +11:00
|
|
|
end
|
|
|
|
if packetType == 2 then
|
|
|
|
dprint("Dropping "..data.." from queue")
|
|
|
|
pqueue[data] = nil
|
|
|
|
computer.pushSignal("net_ack",data)
|
|
|
|
end
|
|
|
|
if packetType ~= 2 then
|
2018-07-27 10:43:56 +10:00
|
|
|
computer.pushSignal("net_msg",sender,vPort,data)
|
2018-02-16 21:36:00 +11:00
|
|
|
end
|
2018-02-21 10:46:53 +11:00
|
|
|
elseif dest:sub(1,1) == "~" then -- broadcasts start with ~
|
2018-07-27 10:43:56 +10:00
|
|
|
computer.pushSignal("net_broadcast",sender,vPort,data)
|
|
|
|
elseif cfg.route then -- repeat packets if route is enabled
|
2019-04-18 19:35:31 +10:00
|
|
|
sendPacket(packetID,packetType,dest,sender,vPort,data,localModem)
|
2018-02-16 21:36:00 +11:00
|
|
|
end
|
2018-02-21 10:46:53 +11:00
|
|
|
if not rcache[sender] then -- add the sender to the rcache
|
2018-02-16 21:36:00 +11:00
|
|
|
dprint("rcache: "..sender..":", localModem,from,computer.uptime())
|
2018-07-27 10:43:56 +10:00
|
|
|
rcache[sender] = {localModem,from,computer.uptime()+cfg.rctime}
|
2018-02-16 21:36:00 +11:00
|
|
|
end
|
2018-02-21 10:46:53 +11:00
|
|
|
if not pcache[packetID] then -- add the packet ID to the pcache
|
2018-07-27 10:43:56 +10:00
|
|
|
pcache[packetID] = computer.uptime()+cfg.pctime
|
2018-02-16 21:36:00 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
listeners["modem_message"]=processPacket
|
|
|
|
event.listen("modem_message",processPacket)
|
|
|
|
print("Started packet listening daemon: "..tostring(processPacket))
|
|
|
|
|
2018-07-27 10:43:56 +10:00
|
|
|
local function queuePacket(_,ptype,to,vPort,data,npID)
|
2018-02-17 14:56:06 +11:00
|
|
|
npID = npID or genPacketID()
|
2018-05-17 16:32:11 +10:00
|
|
|
if to == hostname or to == "localhost" then
|
2018-07-27 10:43:56 +10:00
|
|
|
computer.pushSignal("net_msg",to,vPort,data)
|
2018-05-17 16:32:11 +10:00
|
|
|
computer.pushSignal("net_ack",npID)
|
|
|
|
return
|
|
|
|
end
|
2018-07-27 10:43:56 +10:00
|
|
|
pqueue[npID] = {ptype,to,vPort,data,0,0}
|
2018-02-16 21:36:00 +11:00
|
|
|
dprint(npID,table.unpack(pqueue[npID]))
|
|
|
|
end
|
|
|
|
|
|
|
|
listeners["net_send"]=queuePacket
|
|
|
|
event.listen("net_send",queuePacket)
|
|
|
|
print("Started packet queueing daemon: "..tostring(queuePacket))
|
|
|
|
|
|
|
|
local function packetPusher()
|
|
|
|
for k,v in pairs(pqueue) do
|
|
|
|
if v[5] < computer.uptime() then
|
|
|
|
dprint(k,v[1],v[2],hostname,v[3],v[4])
|
|
|
|
sendPacket(k,v[1],v[2],hostname,v[3],v[4])
|
2018-07-27 10:43:56 +10:00
|
|
|
if v[1] ~= 1 or v[6] == cfg.retrycount then
|
2018-02-16 21:36:00 +11:00
|
|
|
pqueue[k] = nil
|
|
|
|
else
|
2018-07-27 10:43:56 +10:00
|
|
|
pqueue[k][5]=computer.uptime()+cfg.retry
|
2018-02-16 21:36:00 +11:00
|
|
|
pqueue[k][6]=pqueue[k][6]+1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
timers[#timers+1]=event.timer(0,packetPusher,math.huge)
|
|
|
|
print("Started packet pusher: "..tostring(timers[#timers]))
|
|
|
|
|
2018-02-17 14:56:06 +11:00
|
|
|
listeners["net_ack"]=dprint
|
2018-02-16 21:36:00 +11:00
|
|
|
event.listen("net_ack",dprint)
|
|
|
|
end
|
|
|
|
|
|
|
|
function stop()
|
|
|
|
for k,v in pairs(listeners) do
|
|
|
|
event.ignore(k,v)
|
2019-04-18 19:35:31 +10:00
|
|
|
listeners[k] = nil
|
2018-02-16 21:36:00 +11:00
|
|
|
print("Stopped listener: "..tostring(v))
|
|
|
|
end
|
|
|
|
for k,v in pairs(timers) do
|
|
|
|
event.cancel(v)
|
2019-04-18 19:35:31 +10:00
|
|
|
timers[k] = nil
|
2018-02-16 21:36:00 +11:00
|
|
|
print("Stopped timer: "..tostring(v))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-28 05:52:37 +10:00
|
|
|
function set(k,v)
|
|
|
|
if type(cfg[k]) == "string" then
|
|
|
|
cfg[k] = v
|
|
|
|
elseif type(cfg[k]) == "number" then
|
|
|
|
cfg[k] = tonumber(v)
|
|
|
|
elseif type(cfg[k]) == "boolean" then
|
|
|
|
if v:lower():sub(1,1) == "t" then
|
|
|
|
cfg[k] = true
|
|
|
|
else
|
|
|
|
cfg[k] = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
print("cfg."..k.." = "..tostring(cfg[k]))
|
2018-07-28 05:54:27 +10:00
|
|
|
saveconfig()
|
2018-02-16 21:36:00 +11:00
|
|
|
end
|
2018-07-28 05:52:37 +10:00
|
|
|
|
2018-02-16 22:33:08 +11:00
|
|
|
function set_route(to,laddr,raddr)
|
2018-07-27 10:43:56 +10:00
|
|
|
cfg.sroutes[to] = {laddr,raddr,0}
|
2019-09-23 17:34:04 +10:00
|
|
|
saveconfig()
|
2018-02-16 22:33:08 +11:00
|
|
|
end
|
|
|
|
function del_route(to)
|
2018-07-27 10:43:56 +10:00
|
|
|
cfg.sroutes[to] = nil
|
2019-09-23 17:34:04 +10:00
|
|
|
saveconfig()
|
2018-02-16 22:33:08 +11:00
|
|
|
end
|