Advertisement
Tapeline

unit

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