Advertisement
programcreator

IPnet: MainRouter

Jan 29th, 2015
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.55 KB | None | 0 0
  1. ------------IPnet Router v1.0--------
  2. ---------------Program---------------
  3. --------------by Creator-------------
  4. --Variables--
  5. adjascentRouters = {}
  6. subRouters = {}
  7. configuration = {}
  8. clientIPs = {}
  9. debugOpt = true
  10. local textutilsserialize = textutils.serialize
  11. local textutilsunserialize = textutils.unserialize
  12. local secretInt = math.random(100,9999)
  13. local publicPrime = 625210769
  14. local base = 11
  15. --Too many variables--
  16.  
  17. --Functions--
  18. function clear()
  19.     term.setCursorPos(1,1)
  20.     term.clear()
  21. end
  22.  
  23. function debugPrint(txt)
  24.     if debugOpt then
  25.         print(txt)
  26.     end
  27. end
  28.  
  29. function getModemSide()
  30.     local sides = {}
  31.     sides = rs.getSides()
  32.     local side = {}
  33.     for i,v in pairs(sides) do
  34.         if peripheral.isPresent(v) == true and peripheral.getType(v) == "modem" then
  35.             debugPrint("side:")
  36.             debugPrint(v)
  37.             return v
  38.         end
  39.     end
  40.     debugPrint("Got modem side")
  41.     if #side == 0 then return nil end
  42. end
  43.  
  44. function getIP()
  45.     local x , y , z = gps.locate()
  46.     ip = {x,y,z,os.getComputerID()}
  47.     debugPrint("Got ip: ")
  48.     debugPrint(textutilsserialize(ip))
  49.     return textutilsserialize(ip)
  50. end
  51.  
  52. function setConfig()
  53.     local configs = {}
  54.     configs = textutilsunserialize(getIP())
  55.     configs[5] = getModemSide()
  56.    
  57.     local configFile = fs.open("config","w")
  58.     configFile.write(textutilsserialize(configs))
  59.     configFile.close()
  60.     debugPrint("Wrote configs: ")
  61.     debugPrint(configs)
  62. end
  63.  
  64. function setUp()
  65.     if fs.exists("config") then
  66.         configFile = fs.open("config","r")
  67.         tempConfigs = textutilsunserialize(configFile.readAll())
  68.         configFile.close()
  69.         debugPrint("File exists")
  70.     else
  71.         setConfig()
  72.         configFile = fs.open("config","r")
  73.         tempConfigs = textutilsunserialize(configFile.readAll())
  74.         configFile.close()
  75.         debugPrint("File does not exist")
  76.     end
  77.     if tempConfigs[5] == nil then
  78.         setConfig()
  79.         configFile = fs.open("config","r")
  80.         tempConfigs = textutilsunserialize(configFile.readAll())
  81.         configFile.close()
  82.     elseif peripheral.isPresent(tempConfigs[5]) == false then
  83.         setConfig()
  84.         configFile = fs.open("config","r")
  85.         tempConfigs = textutilsunserialize(configFile.readAll())
  86.         configFile.close()
  87.     elseif peripheral.isPresent(tempConfigs[5]) == true and peripheral.getType(tempConfigs[5]) ~= "modem" then
  88.         setConfig()
  89.         configFile = fs.open("config","r")
  90.         tempConfigs = textutilsunserialize(configFile.readAll())
  91.         configFile.close()
  92.     end
  93.    
  94.     if tempConfigs[5] == nil then
  95.         print("No modem attached")
  96.         error()
  97.     end
  98.    
  99.     modem = peripheral.wrap(tempConfigs[5])
  100.     local idChannel = 65524
  101.     local msgChannel = 65525
  102.     local IPchannel = 65526
  103.     modem.open(idChannel)
  104.     modem.open(msgChannel)
  105.     modem.open(IPchannel)
  106.     debugPrint("opened channels: ")
  107.     debugPrint(idChannel)
  108.     debugPrint(msgChannel)
  109.     debugPrint(IPchannel)
  110.     configuration = {tempConfigs[1],tempConfigs[2],tempConfigs[3],tempConfigs[4]}
  111. end
  112.  
  113. function IPsender(a,b,c,d,e,f,g)
  114.     local serializedMsgBuffer = {a,b,c,d,e,f,g}
  115.     local serializedMsg = textutilsserialize(serializedMsgBuffer)
  116.     modem.transmit(65525,1,serializedMsg)
  117. end
  118.  
  119. function IPconfirm(a,b,c,d,e)
  120.     modem.transmit(65525,2,textutilsserialize({a,b,c,d,e}))
  121. end
  122.  
  123. function identification()
  124.     serilaizedConfig = textutilsserialize(configuration)
  125.     modem.transmit(65524,3,serilaizedConfig)
  126.     local pcIsregistered = false
  127.    
  128.     while true do
  129.         local event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
  130.         local ok, errorContent = pcall(textutilsunserialize,msg)
  131.         if ok then
  132.             if ch2 == 3 and ch1 == 65524 then
  133.                 modem.transmit(65524,4,serilaizedConfig)
  134.                 for i,v in pairs(adjascentRouters) do
  135.                     if  v == textutilsunserialize(msg) then
  136.                         pcIsRegistered = true
  137.                     end
  138.                 end
  139.                 if pcIsRegistered == false then
  140.                     adjascentRouters[#adjascentRouters + 1] = textutilsunserialize(msg)
  141.                 end
  142.                 debugPrint("adjacentrouter")
  143.                 debugPrint(textutilsserialize(adjascentRouters))
  144.             elseif ch2 == 4 and ch1 == 65524 then
  145.                 for i,v in pairs(adjascentRouters) do
  146.                     if  v == textutilsunserialize(msg) then
  147.                         pcIsregistered = true
  148.                     end
  149.                 end
  150.                 if pcIsregistered == false then
  151.                     adjascentRouters[#adjascentRouters + 1] = textutilsunserialize(msg)
  152.                 end
  153.                 debugPrint("adjacentrouter")
  154.                 debugPrint(textutilsserialize(adjascentRouters))
  155.             end
  156.         else
  157.             debugPrint(errorContent)
  158.         end
  159.     end
  160. end
  161.  
  162. function identificationClients()
  163.     local iBuffer = nil
  164.     local v9 = 0
  165.     serilaizedConfig = textutilsserialize(configuration)
  166.     modem.transmit(65524,7,serilaizedConfig)
  167.     local subRouterIsregistered = false
  168.     while true do
  169.         local event, side, ch1, ch2, msg, distance = nil, nil, nil, nil, nil, nil
  170.         repeat
  171.         local event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
  172.         until ch1 == 65524
  173.         if pcall(textutilsunserialize,msg) then
  174.             if ch2 == 6 then
  175.                 modem.transmit(65524,5,serilaizedConfig)
  176.                 subRouterIsregistered = false
  177.                 debugPrint("Identification message is: ")
  178.                 debugPrint(msg)
  179.                 local unserializedMsg = {}
  180.                 unserializedMsg = textutilsunserialize(msg)
  181.                 for i,v in pairs(Clients) do
  182.                     if  v[1] == unserializedMsg[1] and v[2] == unserializedMsg[2] and v[3] == unserializedMsg[3] and v[4] == unserializedMsg[4] then
  183.                         subRouterIsregistered = true
  184.                         v9 = v[9]
  185.                         iBuffer = i
  186.                     end
  187.                 end
  188.                 if iBuffer == nil then iBuffer = 1 end
  189.                 local msgBufferTemp = textutilsunserialize(msg)
  190.                 debugPrint("Return msg")
  191.                 debugPrint(msg)
  192.                 local msgBuffer = {
  193.                 msgBufferTemp[1],
  194.                 msgBufferTemp[2],
  195.                 msgBufferTemp[3],
  196.                 msgBufferTemp[4],
  197.                 #Clients + 50001,
  198.                 }
  199.                 debugPrint(textutilsserialize(msgBuffer))
  200.                 if subRouterIsregistered == false then
  201.                     subRouters[#subRouters + 1] = msgBuffer
  202.                     modem.transmit(65524,23,msgBuffer[5])
  203.                 end
  204.                 sleep(.05)
  205.                 modem.open(65505)
  206.                 modem.transmit(65505,1,textutilsserialize({publicPrime,base,myHalfOfTheSecret}))
  207.                 local tEvent, tSide, tCh1, tCh2, tMsg, tDistance = nil, nil, nil, nil, nil, nil
  208.                 repeat
  209.                     tEvent, tSide, tCh1, tCh2, tMsg, tDistance = os.pullEvent("modem_message")
  210.                 until tCh1 ~= 65505 or tCh2 ~= 2
  211.                 sharedSecret = baseToPowerMod(tMsg,secretInt,publicPrime)
  212.                 debugPrint(sharedSecret)
  213.                 modem.close(65505)
  214.                 secrets[iBuffer] = sharedSecret
  215.             elseif ch2 == 8 then
  216.                 local unserializedMsg = {}
  217.                 unserializedMsg = textutilsunserialize(msg)
  218.                 subRouterIsregistered = false
  219.                 debugPrint("Identification message is: ")
  220.                 debugPrint(msg)
  221.                 local unserializedMsg = {}
  222.                 unserializedMsg = textutilsunserialize(msg)
  223.                 for i,v in pairs(Clients) do
  224.                     if  v[1] == unserializedMsg[1] and v[2] == unserializedMsg[2] and v[3] == unserializedMsg[3] and v[4] == unserializedMsg[4] then
  225.                         subRouterIsregistered = true
  226.                         v9 = v[9]
  227.                         iBuffer = i
  228.                     end
  229.                 end
  230.                 if iBuffer == nil then iBuffer = 1 end
  231.                 local msgBufferTemp = textutilsunserialize(msg)
  232.                 debugPrint("Return msg")
  233.                 debugPrint(msg)
  234.                 local msgBuffer = {
  235.                 msgBufferTemp[1],
  236.                 msgBufferTemp[2],
  237.                 msgBufferTemp[3],
  238.                 msgBufferTemp[4],
  239.                 #Clients + 50001,
  240.                 }
  241.                 if subRouterIsregistered == false then
  242.                     subRouters[#subRouters + 1] = msgBuffer
  243.                     modem.transmit(65524,23,msgBuffer[5])
  244.                 end
  245.                 sleep(.05)
  246.                 modem.open(65505)
  247.                 modem.transmit(65505,1,textutilsserialize({publicPrime,base,myHalfOfTheSecret}))
  248.                 local tEvent, tSide, tCh1, tCh2, tMsg, tDistance = nil, nil, nil, nil, nil, nil
  249.                 repeat
  250.                     tEvent, tSide, tCh1, tCh2, tMsg, tDistance = os.pullEvent("modem_message")
  251.                 until tCh1 ~= 65505 or tCh2 ~= 2
  252.                 sharedSecret = baseToPowerMod(tMsg,secretInt,publicPrime)
  253.                 debugPrint(sharedSecret)
  254.                 modem.close(65505)
  255.                 secrets[iBuffer] = sharedSecret
  256.             end
  257.         end
  258.     end
  259. end
  260.  
  261. function checkAndSend(serializedMsgBuffer)
  262.     local msgBuffer = {}
  263.     local ok, errorContent = pcall(textutilsunserialize,serializedMsgBuffer)
  264.     if ok then
  265.         msgBuffer = textutilsunserialize(serializedMsgBuffer)
  266.         local destinationPc = {}
  267.         destinationPc = msgBuffer[1]
  268.         local sendingPc = {}
  269.         sendingPc = msgBuffer[2]
  270.         local toRouter = {}
  271.         toRouter = msgBuffer[3]
  272.         local sendingRouter = {}
  273.         sendingRouter = msgBuffer[4]
  274.         local msgNum = ""
  275.         msgNum = msgBuffer[5]
  276.         local msgToTransmit = ""
  277.         msgToTransmit = msgBuffer[6]
  278.         local port = {}
  279.         port = msgBuffer[7]
  280.         local clientIsPresent = false
  281.         local whichClient = nil
  282.         local t = fs.open("test","w")
  283.         t.write(textutilsserialize(clientIPs))
  284.         t.close()
  285.         for i,v in pairs(clientIPs) do
  286.             if destinationPc[1] == v[1] and destinationPc[2] == v[2] and destinationPc[3] == v[3] and destinationPc[4] == v[4] then
  287.                 clientIsPresent = true
  288.                 whichClient = i
  289.                 debugPrint("clientIsPresent")
  290.             end
  291.         end
  292.         if clientIsPresent == true then
  293.             local whichSubRouter = {clientIPs[whichClient][1],clientIPs[whichClient][2],clientIPs[whichClient][3],clientIPs[whichClient][4]}
  294.             modem.transmit(65525,1,textutilsserialize({destinationPc,sendingPc,configuration,whichSubRouter,msgNum,msgToTransmit,port}))
  295.         --[[else
  296.             possibleRouters = adjascentRouters
  297.             debugPrint(textutilsserialize(possibleRouters))
  298.             local vectorLenghtTable = {}
  299.                
  300.             for i,v in pairs(possibleRouters) do
  301.                 local xDist = math.abs(math.abs(destinationPc[1])-math.abs(v[1]))
  302.                 local yDist = math.abs(math.abs(destinationPc[2])-math.abs(v[2]))
  303.                 local zDist = math.abs(math.abs(destinationPc[3])-math.abs(v[3]))
  304.                 local vectorBuffer = vector.new(xDist,yDist,zDist)
  305.                 vectorLenghtTable[#vectorLenghtTable+1] = vectorBuffer:length()
  306.             end
  307.             local winner = {}
  308.             for i,v in pairs(vectorLenghtTable) do
  309.                 debugPrint(i)
  310.                 debugPrint(v)
  311.                 if i == 1 then
  312.                     winner = {i,v}
  313.                 else
  314.                     if v < winner[2] then
  315.                         winner = {i,v}
  316.                     end
  317.                 end
  318.             end
  319.             local closestRouter = possibleRouters[winner[1]]--
  320.             --[[IPsender(destinationPc,sendingPc,closestRouter,configuration,msgNum,msgToTransmit,port)
  321.             timer = os.startTimer(5)
  322.             local event, side, ch1, ch2, confirmMessageBuffer, distance = os.pullEvent("modem_message" or "timer")
  323.             local confirmMessage = {}
  324.             confirmMessage = textutilsunserialize(confirmMessageBuffer)
  325.             local ok = pcall(textutilsunserialize,confirmMessageBuffer)
  326.             if event == "modem_message" then
  327.                 if ch2 == 2 and confirmMessage[3] == configuration and confirmMessage[4] == closestRouter and confirmMessage[5] == msgNum then
  328.                     return true
  329.                 end
  330.             elseif event == "timer" and side == "timer" then
  331.                 for i,v in pairs(adjascentRouters) do
  332.                     if v == closestRouter then
  333.                         adjascentRouters[i] = nil
  334.                     end
  335.                 end
  336.                 local nilHappened = false
  337.                 for i,v in pairs(adjascentRouters) do
  338.                     if nilHappened then
  339.                         adjascentRouters[i-1] = v
  340.                     end
  341.                     if v == nil then
  342.                         nilHappened = true
  343.                     end
  344.                 end
  345.                 nilHappened = false
  346.                 return false
  347.             else
  348.                 return "invalid"
  349.             else
  350.                 debugPrint(errorContent)]]--
  351.         end
  352.     end
  353. end
  354.  
  355. function messageSender()
  356.     local messageNotDelivered = true
  357.     while true do
  358.         local event, side, ch1, ch2, message, distance = nil, nil, nil, nil, nil, nil
  359.         repeat
  360.             event, side, ch1, ch2, message, distance = os.pullEvent("modem_message")
  361.         until ch1 == 65525
  362.         debugPrint("newMessage")
  363.         --debugPrint(message)
  364.         if pcall(textutilsunserialize,message) then
  365.             if ch2 == 1 then
  366.                 debugPrint("ch1 and pcall")
  367.                 local msgBuffer = {}
  368.                 msgBuffer = textutilsunserialize(message)
  369.                 local destinationPc = {}
  370.                 destinationPc = msgBuffer[1]
  371.                 local sendingPc = {}
  372.                 sendingPc = msgBuffer[2]
  373.                 local toRouter = {}
  374.                 toRouter = msgBuffer[3]
  375.                 local sendingRouter = {}
  376.                 sendingRouter = msgBuffer[4]
  377.                 local msgNum = ""
  378.                 msgNum = msgBuffer[5]
  379.                 local ifSuccessInSending = nil
  380.                 debugPrint(textutilsserialize(toRouter))
  381.                 debugPrint(textutilsserialize(configuration))
  382.                 if toRouter == configuration then
  383.                     debugPrint("theMsgIsForMe")
  384.                     --IPconfirm(destinationPc,sendingPc,sendingRouter,configuration,msgNum)
  385.                     ifSuccessInSending = checkAndSend(message)
  386.                     while ifSuccessInSending == false or ifSuccessInSending == "invalid" do
  387.                         ifSuccessInSending = checkAndSend(message)
  388.                         if #adjascentRouters == 0 then
  389.                             print("No routers available...")
  390.                             identification()
  391.                             break
  392.                         end
  393.                     end
  394.                 end
  395.             elseif ch2 == 5 then
  396.                
  397.             end
  398.         else
  399.             debugPrint(errorContent)
  400.         end
  401.     end
  402. end
  403.  
  404. function getCientIPs()
  405.     while true do
  406.         local event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
  407.         if ch1 == 65526 and ch2 == 1 then
  408.             local clientsToAnalyse = {}
  409.             local possibleClients = {}
  410.             local ok, errorContent = pcall(textutilsunserialize,msg)
  411.             if ok then
  412.                 possibleClients = textutilsunserialize(msg)
  413.                 debugPrint("Possible clients: ")
  414.                 debugPrint(textutilsserialize(possibleClients))
  415.                 local clientsToAnalyse = {}
  416.                 if next(clientIPs) ~= nil then
  417.                     for i,v in pairs(clientIPs) do
  418.                         for k,m in pairs(possibleClients) do
  419.                             if  v[1] == m[1] and v[2] == m[2] and v[3] == m[3] and v[4] == m[4] then
  420.                                 debugPrint("analyzing...")
  421.                                 clientsToAnalyse[k] = true
  422.                             elseif clientsToAnalyse[k] ~= true then
  423.                                 clientsToAnalyse[k] = false
  424.                             end
  425.                         end
  426.                     end
  427.                     debugPrint("Clients to analyze: ")
  428.                     debugPrint(textutilsserialize(clientsToAnalyse))
  429.                     for i,v in pairs(clientsToAnalyse) do
  430.                         if v ~= true or v == nil then
  431.                             debugPrint("v: ")
  432.                             debugPrint(v)
  433.                             clientIPs[#clientIPs + 1] = possibleClients[i]
  434.                         end
  435.                     end
  436.                     debugPrint("ClientIPs: ")
  437.                     debugPrint(textutilsserialize(clientIPs))
  438.                 else
  439.                     clientIPs = possibleClients
  440.                     debugPrint("ClientIPs: ")
  441.                     debugPrint(textutilsserialize(clientIPs))
  442.                 end
  443.             else
  444.                 debugPrint(errorContent)
  445.             end
  446.         end
  447.     end
  448. end
  449.  
  450. function urgentMsg()
  451.     while true do
  452.         local event, side, ch1, ch2, message, distance = os.pullEvent("modem_message")
  453.         if ch2 == 2 then
  454.             local ok = pcall(textutilsunserialize,message)
  455.             if ok then
  456.                 local msg = textutilsunserialize(message)
  457.                 if msg[3] == nil then
  458.                     local clientIsPresent = false
  459.                     for i,v in pairs(clientIPs) do
  460.                         if v[1] == destinationPc[1] and v[2] == destinationPc[2] and v[3] == destinationPc[3] and v[4] == destinationPc[4] then
  461.                             modem.transmit(65525,1,textutilsserialize(destinationPc,sendingPc,{v[5],v[6],v[7],v[8]},configuration,msgNum,msg,port))
  462.                             modem.transmit(65525,4,textutilsserialize(destinationPc,sendingPc,msg[4],configuration,msgNum))
  463.                             clientIsPresent = true
  464.                         end
  465.                     end
  466.                 end
  467.             end
  468.         end
  469.     end
  470. end
  471. --Too many functions--
  472.  
  473. clear()
  474. setUp()
  475. parallel.waitForAny(identification,messageSender,identificationOfSubRouters,getCientIPs,urgentMsg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement