Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local event = require("event")
- local m = component.modem
- local ttf = require("ttf")
- local fs = require("filesystem")
- lookup = {}
- mac_me = ""
- for k,v in component.list("modem") do
- mac_me = k
- end
- function split(inputstr, sep)
- if sep == nil then
- sep = "%s"
- end
- local t={}
- for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
- table.insert(t, str)
- end
- return t
- end
- function getIPPart(a, t)
- local ip = split(a, ".")
- local len = 0
- for k,v in pairs(ip) do
- len = len + 1
- end
- if (t == "last") then
- return ip[len]
- else
- return ip[len - (tonumber(t) - 1)]
- end
- end
- function getNodeCount(a)
- local ip = split(a, ".")
- local len = 0
- for k,v in pairs(ip) do
- len = len + 1
- end
- return len
- end
- function getUnitParentIP(a)
- local ip = split(a, ".")
- local ret = ""
- for k,v in pairs(ip) do
- if (v == getIPPart(a, "last")) then
- break
- end
- if (v == getIPPart(a, "2")) then
- ret = ret .. v
- else
- ret = ret .. v .. "."
- end
- end
- return ret
- end
- function stop()
- event.ignore("touch", stop)
- event.ignore("modem_message", listen)
- print("(:) Stopping...")
- end
- function start()
- m.open(1110)
- print("(:) Start...")
- if (fs.exists("/home/QuarksRouter/lookup.table")) then
- lookup = ttf.load("/home/QuarksRouter/lookup.table")
- print("(:) Lookups loaded")
- else
- print("(X) No lookups and parent ID!")
- lookup[-1] = "ffad-ffad-ffad-ffad"
- lookup[0] = "1.1"
- end
- print("(:) Booted up!")
- print("(:) MAC: " .. mac_me .. " IP: " .. lookup[0])
- event.listen("touch", stop)
- event.listen("modem_message", listen)
- end
- function import()
- local f = fs.open("/home/qclient.conf")
- local fc = f:read(fs.size("/home/qclient.conf"))
- local s = split(fc, ";")
- lookup[-1] = s[1]
- lookup[0] = s[2]
- print("(:) Successfully imported settings from QClient! " .. fc)
- end
- function listen(_, _, from, _, _, msg)
- packet = split(msg, ";")
- print("(:) Received packet! Resolving packet type... [" .. msg .."]")
- if (packet[3] == "DATA") then
- print("(:) TYPE: DATA")
- handleData(from, packet, msg)
- elseif (packet[3] == "DNSLOOKUP") then
- print("(:) TYPE: DNSLOOKUP")
- handleLookup(from, packet, msg)
- elseif (packet[3] == "DNSANSWER") then
- print("(:) TYPE: DNSANSWER")
- handleAnswer(from, packet, msg)
- elseif (packet[3] == "DNSREG") then
- print("(:) TYPE: DNSREG")
- handleReg(from, packet, msg)
- elseif (packet[3] == "FINDPARENT") then
- print("(:) TYPE: FINDPARENT")
- handleFind(from, packet)
- else
- print("(X) Unknown packet type (" .. packet[3] .. ")")
- end
- end
- function handleData(from, p, mm)
- if (getUnitParentIP(p[2]) == lookup[0]) then
- print("(:) Rerouting packet to client in my subnet... [".. mm .."]")
- m.send(lookup[tonumber(getIPPart(p[2], "last"))], 1110, mm)
- elseif (lookup[0] == p[2]:sub( 0, lookup[0]:len() )) then
- local subip = p[2]:sub(lookup[0]:len() + 2)
- local node = split(subip, ".")[1]
- local mac = lookup[tonumber(node)]
- print("(:) Rerouting packet to child router in my subnet... (ID#" ..
- node .. " MAC:" .. mac .. " [".. mm .."]")
- m.send(mac, 1110, mm)
- else
- print("(:) Rerouting packet to parent router... [".. mm .."]")
- m.send(lookup[-1], 1110, mm)
- end
- end
- function handleLookup(from, p, mm)
- print("(:) Handled! ["..mm.."]")
- m.send(lookup[-1], 1110, mm)
- end
- function handleAnswer(from, p, mm)
- handleData(from, p, mm)
- print("(:) Handled! ["..mm.."]")
- end
- function handleReg(from, p, mm)
- print("(:) Handled! ["..mm.."]")
- m.send(lookup[-1], 1110, mm)
- end
- function handleFind(from, p)
- if (p[4] == "find") then
- m.send(from, 1110, "FINDROUTER:ME")
- elseif (p[4] == mac_me) then
- print("(:) Registering new client... (" .. from .. ")")
- local id = 0
- for k,v in pairs(lookup) do
- id = id + 1
- end
- local userip = lookup[0] .. "." .. tostring(id - 1)
- lookup[id - 1] = from
- print("(:) Allocated IP " .. userip .. " for MAC " .. from)
- m.send(from, 1110, userip)
- end
- print("(:) Handled FINDPARENT:[" .. p .. "]")
- end
- -- MAIN
- start()
- import()
- event.pull("touch")
- print("(:) Saving lookups...")
- ttf.save(lookup, "/home/QuarksRouter/lookup.table")
- print("(:) Stopped!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement