Advertisement
Tapeline

root

Oct 8th, 2021
65
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require("component")
  2. local event = require("event")
  3. local m = component.modem
  4. local ttf = require("ttf")
  5. local fs = require("filesystem")
  6.  
  7. lookup = {}
  8. dns = {}
  9. mac_me = ""
  10. for k,v in component.list("modem") do
  11.   mac_me = k
  12. end
  13.  
  14. function split(inputstr, sep)
  15.   if sep == nil then
  16.     sep = "%s"
  17.   end
  18.   local t={}
  19.   for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  20.     table.insert(t, str)
  21.   end
  22.   return t
  23. end
  24.  
  25. function getIPPart(a, t)
  26.   local ip = split(a, ".")
  27.   local len = 0
  28.   for k,v in pairs(ip) do
  29.     len = len + 1
  30.   end
  31.   if (t == "last") then
  32.     return ip[len]
  33.   else
  34.     return ip[len - (tonumber(t) - 1)]
  35.   end
  36. end
  37.  
  38. function getNodeCount(a)
  39.   local ip = split(a, ".")
  40.   local len = 0
  41.   for k,v in pairs(ip) do
  42.     len = len + 1
  43.   end
  44.   return len
  45. end
  46.  
  47. function getUnitParentIP(a)
  48.   local ip = split(a, ".")
  49.   local ret = ""
  50.   for k,v in pairs(ip) do
  51.     if (v == getIPPart(a, "last")) then
  52.       break
  53.     end
  54.     if (v == getIPPart(a, "2")) then
  55.       ret = ret .. v
  56.     else
  57.       ret = ret .. v .. "."
  58.     end
  59.   end
  60.   return ret
  61. end
  62.  
  63. function stop()
  64.   event.ignore("touch", stop)
  65.   event.ignore("modem_message", listen)
  66.   print("(:) Stopping...")
  67. end
  68.  
  69. function start()
  70.   m.open(1110)
  71.   print("(:) Start...")
  72.   if (fs.exists("/home/QuarksRouter/lookup.table")) then
  73.     lookup = ttf.load("/home/QuarksRouter/lookup.table")
  74.     print("(:) Lookups loaded")
  75.   else
  76.     print("(X) No lookups!")
  77.   end
  78.   if (fs.exists("/home/QuarksRouter/dns.table")) then
  79.     dns = ttf.load("/home/QuarksRouter/dns.table")
  80.     print("(:) DNS Lookups loaded!")
  81.   else
  82.     print("(X) No DNS lookups!")
  83.   end
  84.   print("(:) Booted up!")
  85.   print("(:) MAC: " .. mac_me)
  86.   event.listen("touch", stop)
  87.   event.listen("modem_message", listen)
  88. end
  89.  
  90. function listen(_, _, from, _, _, msg)
  91.   packet = split(msg, ";")
  92.   print("(:) Received packet! Resolving packet type... [" .. msg .."]")
  93.   if (packet[3] == "DATA") then
  94.     print("(:)       TYPE: DATA")
  95.     handleData(from, packet, msg)
  96.   elseif (packet[3] == "DNSLOOKUP") then
  97.     print("(:)       TYPE: DNSLOOKUP")
  98.     handleLookup(from, packet)
  99.   elseif (packet[3] == "DNSANSWER") then
  100.     print("(:)       TYPE: DNSANSWER")
  101.     handleAnswer(from, packet)
  102.   elseif (packet[3] == "DNSREG") then
  103.     print("(:)       TYPE: DNSREG")
  104.     handleReg(from, packet)
  105.   elseif (packet[3] == "FINDPARENT") then
  106.     print("(:)       TYPE: FINDPARENT")
  107.     handleFind(from, packet)
  108.   else
  109.     print("(X) Unknown packet type (" .. packet[3] .. ")")
  110.   end
  111. end
  112.  
  113. function handleData(from, p, mm)
  114.   print("(:) Rerouting packet to client in my subnet... (" ..
  115.   lookup[tonumber(split(p[2], ".")[1]) ].. ")[".. mm .."]")
  116.   m.send(lookup[tonumber(split(p[2], ".")[1])], 1110, mm)
  117. end
  118.  
  119. function handleLookup(from, p, mm)
  120.   if (dns[p[4]] == nil) then
  121.     m.send(from,1110,"0;".. p[1] ..";DNSANSWER;QDNS:DOMAINNOTFOUND")
  122.   else
  123.     ip = dns[p[4]]
  124.     m.send(from,1110,"0;".. p[1] ..";DNSANSWER;QNDS:".. ip)
  125.     print("(:) " .. p[4] .. " is " .. ip)
  126.   end
  127. end
  128.  
  129. function handleAnswer(from, p, mm)
  130.   print("(!) Skipping DNSANSWER packet. Does your network have cycles or it wasn't set up correctly?")  
  131. end
  132.  
  133. function handleReg(from, p, mm)
  134.   if (dns[p[4]] == nil) then
  135.     dns[p[4]] = p[1]
  136.     m.send(from,1110,"0;".. p[1] ..";DNSANSWER;QDNS:SUCCESSREG")
  137.   else
  138.     print("(X) Domain ".. p[4] .." already registered ")
  139.     m.send(from,1110,"0;".. p[1] ..";DNSANSWER;QDNS:ALREADYREGISTERED")
  140.   end
  141. end
  142.  
  143. function handleFind(from, p)
  144.   if (p[4] == "find") then
  145.     m.send(from, 1110, "FINDROUTER:ME")
  146.   elseif (p[4] == mac_me) then
  147.     print("(:) Registering new client... (" .. from .. ")")
  148.     local id = 1
  149.     for k,v in pairs(lookup) do
  150.       id = id + 1
  151.     end
  152.     local userip = tostring(id)
  153.     lookup[id] = from
  154.     print("(:) Allocated IP " .. userip .. " for MAC " .. from)
  155.     m.send(from, 1110, userip)
  156.   end
  157.   print("(:) Handled FINDPARENT:[" .. p .. "]")
  158. end
  159.  
  160.  
  161.  
  162. -- MAIN
  163. start()
  164. event.pull("touch")
  165. print("(:) Saving lookups...")
  166. ttf.save(lookup, "/home/QuarksRouter/lookup.table")
  167. ttf.save(dns, "/home/QuarksRouter/dns.table")
  168. print("(:) Stopped!")
Advertisement
RAW Paste Data Copied
Advertisement