Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------IPnet Router v1.0--------
- ---------------Program---------------
- --------------by Creator-------------
- --Variables--
- adjascentRouters = {}
- subRouters = {}
- configuration = {}
- clientIPs = {}
- debugOpt = true
- local textutilsserialize = textutils.serialize
- local textutilsunserialize = textutils.unserialize
- local secretInt = math.random(100,9999)
- local publicPrime = 625210769
- local base = 11
- --Too many variables--
- --Functions--
- function clear()
- term.setCursorPos(1,1)
- term.clear()
- end
- function debugPrint(txt)
- if debugOpt then
- print(txt)
- end
- end
- function getModemSide()
- local sides = {}
- sides = rs.getSides()
- local side = {}
- for i,v in pairs(sides) do
- if peripheral.isPresent(v) == true and peripheral.getType(v) == "modem" then
- debugPrint("side:")
- debugPrint(v)
- return v
- end
- end
- debugPrint("Got modem side")
- if #side == 0 then return nil end
- end
- function getIP()
- local x , y , z = gps.locate()
- ip = {x,y,z,os.getComputerID()}
- debugPrint("Got ip: ")
- debugPrint(textutilsserialize(ip))
- return textutilsserialize(ip)
- end
- function setConfig()
- local configs = {}
- configs = textutilsunserialize(getIP())
- configs[5] = getModemSide()
- local configFile = fs.open("config","w")
- configFile.write(textutilsserialize(configs))
- configFile.close()
- debugPrint("Wrote configs: ")
- debugPrint(configs)
- end
- function setUp()
- if fs.exists("config") then
- configFile = fs.open("config","r")
- tempConfigs = textutilsunserialize(configFile.readAll())
- configFile.close()
- debugPrint("File exists")
- else
- setConfig()
- configFile = fs.open("config","r")
- tempConfigs = textutilsunserialize(configFile.readAll())
- configFile.close()
- debugPrint("File does not exist")
- end
- if tempConfigs[5] == nil then
- setConfig()
- configFile = fs.open("config","r")
- tempConfigs = textutilsunserialize(configFile.readAll())
- configFile.close()
- elseif peripheral.isPresent(tempConfigs[5]) == false then
- setConfig()
- configFile = fs.open("config","r")
- tempConfigs = textutilsunserialize(configFile.readAll())
- configFile.close()
- elseif peripheral.isPresent(tempConfigs[5]) == true and peripheral.getType(tempConfigs[5]) ~= "modem" then
- setConfig()
- configFile = fs.open("config","r")
- tempConfigs = textutilsunserialize(configFile.readAll())
- configFile.close()
- end
- if tempConfigs[5] == nil then
- print("No modem attached")
- error()
- end
- modem = peripheral.wrap(tempConfigs[5])
- local idChannel = 65524
- local msgChannel = 65525
- local IPchannel = 65526
- modem.open(idChannel)
- modem.open(msgChannel)
- modem.open(IPchannel)
- debugPrint("opened channels: ")
- debugPrint(idChannel)
- debugPrint(msgChannel)
- debugPrint(IPchannel)
- configuration = {tempConfigs[1],tempConfigs[2],tempConfigs[3],tempConfigs[4]}
- end
- function IPsender(a,b,c,d,e,f,g)
- local serializedMsgBuffer = {a,b,c,d,e,f,g}
- local serializedMsg = textutilsserialize(serializedMsgBuffer)
- modem.transmit(65525,1,serializedMsg)
- end
- function IPconfirm(a,b,c,d,e)
- modem.transmit(65525,2,textutilsserialize({a,b,c,d,e}))
- end
- function identification()
- serilaizedConfig = textutilsserialize(configuration)
- modem.transmit(65524,3,serilaizedConfig)
- local pcIsregistered = false
- while true do
- local event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
- local ok, errorContent = pcall(textutilsunserialize,msg)
- if ok then
- if ch2 == 3 and ch1 == 65524 then
- modem.transmit(65524,4,serilaizedConfig)
- for i,v in pairs(adjascentRouters) do
- if v == textutilsunserialize(msg) then
- pcIsRegistered = true
- end
- end
- if pcIsRegistered == false then
- adjascentRouters[#adjascentRouters + 1] = textutilsunserialize(msg)
- end
- debugPrint("adjacentrouter")
- debugPrint(textutilsserialize(adjascentRouters))
- elseif ch2 == 4 and ch1 == 65524 then
- for i,v in pairs(adjascentRouters) do
- if v == textutilsunserialize(msg) then
- pcIsregistered = true
- end
- end
- if pcIsregistered == false then
- adjascentRouters[#adjascentRouters + 1] = textutilsunserialize(msg)
- end
- debugPrint("adjacentrouter")
- debugPrint(textutilsserialize(adjascentRouters))
- end
- else
- debugPrint(errorContent)
- end
- end
- end
- function identificationClients()
- local iBuffer = nil
- local v9 = 0
- serilaizedConfig = textutilsserialize(configuration)
- modem.transmit(65524,7,serilaizedConfig)
- local subRouterIsregistered = false
- while true do
- local event, side, ch1, ch2, msg, distance = nil, nil, nil, nil, nil, nil
- repeat
- local event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
- until ch1 == 65524
- if pcall(textutilsunserialize,msg) then
- if ch2 == 6 then
- modem.transmit(65524,5,serilaizedConfig)
- subRouterIsregistered = false
- debugPrint("Identification message is: ")
- debugPrint(msg)
- local unserializedMsg = {}
- unserializedMsg = textutilsunserialize(msg)
- for i,v in pairs(Clients) do
- if v[1] == unserializedMsg[1] and v[2] == unserializedMsg[2] and v[3] == unserializedMsg[3] and v[4] == unserializedMsg[4] then
- subRouterIsregistered = true
- v9 = v[9]
- iBuffer = i
- end
- end
- if iBuffer == nil then iBuffer = 1 end
- local msgBufferTemp = textutilsunserialize(msg)
- debugPrint("Return msg")
- debugPrint(msg)
- local msgBuffer = {
- msgBufferTemp[1],
- msgBufferTemp[2],
- msgBufferTemp[3],
- msgBufferTemp[4],
- #Clients + 50001,
- }
- debugPrint(textutilsserialize(msgBuffer))
- if subRouterIsregistered == false then
- subRouters[#subRouters + 1] = msgBuffer
- modem.transmit(65524,23,msgBuffer[5])
- end
- sleep(.05)
- modem.open(65505)
- modem.transmit(65505,1,textutilsserialize({publicPrime,base,myHalfOfTheSecret}))
- local tEvent, tSide, tCh1, tCh2, tMsg, tDistance = nil, nil, nil, nil, nil, nil
- repeat
- tEvent, tSide, tCh1, tCh2, tMsg, tDistance = os.pullEvent("modem_message")
- until tCh1 ~= 65505 or tCh2 ~= 2
- sharedSecret = baseToPowerMod(tMsg,secretInt,publicPrime)
- debugPrint(sharedSecret)
- modem.close(65505)
- secrets[iBuffer] = sharedSecret
- elseif ch2 == 8 then
- local unserializedMsg = {}
- unserializedMsg = textutilsunserialize(msg)
- subRouterIsregistered = false
- debugPrint("Identification message is: ")
- debugPrint(msg)
- local unserializedMsg = {}
- unserializedMsg = textutilsunserialize(msg)
- for i,v in pairs(Clients) do
- if v[1] == unserializedMsg[1] and v[2] == unserializedMsg[2] and v[3] == unserializedMsg[3] and v[4] == unserializedMsg[4] then
- subRouterIsregistered = true
- v9 = v[9]
- iBuffer = i
- end
- end
- if iBuffer == nil then iBuffer = 1 end
- local msgBufferTemp = textutilsunserialize(msg)
- debugPrint("Return msg")
- debugPrint(msg)
- local msgBuffer = {
- msgBufferTemp[1],
- msgBufferTemp[2],
- msgBufferTemp[3],
- msgBufferTemp[4],
- #Clients + 50001,
- }
- if subRouterIsregistered == false then
- subRouters[#subRouters + 1] = msgBuffer
- modem.transmit(65524,23,msgBuffer[5])
- end
- sleep(.05)
- modem.open(65505)
- modem.transmit(65505,1,textutilsserialize({publicPrime,base,myHalfOfTheSecret}))
- local tEvent, tSide, tCh1, tCh2, tMsg, tDistance = nil, nil, nil, nil, nil, nil
- repeat
- tEvent, tSide, tCh1, tCh2, tMsg, tDistance = os.pullEvent("modem_message")
- until tCh1 ~= 65505 or tCh2 ~= 2
- sharedSecret = baseToPowerMod(tMsg,secretInt,publicPrime)
- debugPrint(sharedSecret)
- modem.close(65505)
- secrets[iBuffer] = sharedSecret
- end
- end
- end
- end
- function checkAndSend(serializedMsgBuffer)
- local msgBuffer = {}
- local ok, errorContent = pcall(textutilsunserialize,serializedMsgBuffer)
- if ok then
- msgBuffer = textutilsunserialize(serializedMsgBuffer)
- local destinationPc = {}
- destinationPc = msgBuffer[1]
- local sendingPc = {}
- sendingPc = msgBuffer[2]
- local toRouter = {}
- toRouter = msgBuffer[3]
- local sendingRouter = {}
- sendingRouter = msgBuffer[4]
- local msgNum = ""
- msgNum = msgBuffer[5]
- local msgToTransmit = ""
- msgToTransmit = msgBuffer[6]
- local port = {}
- port = msgBuffer[7]
- local clientIsPresent = false
- local whichClient = nil
- local t = fs.open("test","w")
- t.write(textutilsserialize(clientIPs))
- t.close()
- for i,v in pairs(clientIPs) do
- if destinationPc[1] == v[1] and destinationPc[2] == v[2] and destinationPc[3] == v[3] and destinationPc[4] == v[4] then
- clientIsPresent = true
- whichClient = i
- debugPrint("clientIsPresent")
- end
- end
- if clientIsPresent == true then
- local whichSubRouter = {clientIPs[whichClient][1],clientIPs[whichClient][2],clientIPs[whichClient][3],clientIPs[whichClient][4]}
- modem.transmit(65525,1,textutilsserialize({destinationPc,sendingPc,configuration,whichSubRouter,msgNum,msgToTransmit,port}))
- --[[else
- possibleRouters = adjascentRouters
- debugPrint(textutilsserialize(possibleRouters))
- local vectorLenghtTable = {}
- for i,v in pairs(possibleRouters) do
- local xDist = math.abs(math.abs(destinationPc[1])-math.abs(v[1]))
- local yDist = math.abs(math.abs(destinationPc[2])-math.abs(v[2]))
- local zDist = math.abs(math.abs(destinationPc[3])-math.abs(v[3]))
- local vectorBuffer = vector.new(xDist,yDist,zDist)
- vectorLenghtTable[#vectorLenghtTable+1] = vectorBuffer:length()
- end
- local winner = {}
- for i,v in pairs(vectorLenghtTable) do
- debugPrint(i)
- debugPrint(v)
- if i == 1 then
- winner = {i,v}
- else
- if v < winner[2] then
- winner = {i,v}
- end
- end
- end
- local closestRouter = possibleRouters[winner[1]]--
- --[[IPsender(destinationPc,sendingPc,closestRouter,configuration,msgNum,msgToTransmit,port)
- timer = os.startTimer(5)
- local event, side, ch1, ch2, confirmMessageBuffer, distance = os.pullEvent("modem_message" or "timer")
- local confirmMessage = {}
- confirmMessage = textutilsunserialize(confirmMessageBuffer)
- local ok = pcall(textutilsunserialize,confirmMessageBuffer)
- if event == "modem_message" then
- if ch2 == 2 and confirmMessage[3] == configuration and confirmMessage[4] == closestRouter and confirmMessage[5] == msgNum then
- return true
- end
- elseif event == "timer" and side == "timer" then
- for i,v in pairs(adjascentRouters) do
- if v == closestRouter then
- adjascentRouters[i] = nil
- end
- end
- local nilHappened = false
- for i,v in pairs(adjascentRouters) do
- if nilHappened then
- adjascentRouters[i-1] = v
- end
- if v == nil then
- nilHappened = true
- end
- end
- nilHappened = false
- return false
- else
- return "invalid"
- else
- debugPrint(errorContent)]]--
- end
- end
- end
- function messageSender()
- local messageNotDelivered = true
- while true do
- local event, side, ch1, ch2, message, distance = nil, nil, nil, nil, nil, nil
- repeat
- event, side, ch1, ch2, message, distance = os.pullEvent("modem_message")
- until ch1 == 65525
- debugPrint("newMessage")
- --debugPrint(message)
- if pcall(textutilsunserialize,message) then
- if ch2 == 1 then
- debugPrint("ch1 and pcall")
- local msgBuffer = {}
- msgBuffer = textutilsunserialize(message)
- local destinationPc = {}
- destinationPc = msgBuffer[1]
- local sendingPc = {}
- sendingPc = msgBuffer[2]
- local toRouter = {}
- toRouter = msgBuffer[3]
- local sendingRouter = {}
- sendingRouter = msgBuffer[4]
- local msgNum = ""
- msgNum = msgBuffer[5]
- local ifSuccessInSending = nil
- debugPrint(textutilsserialize(toRouter))
- debugPrint(textutilsserialize(configuration))
- if toRouter == configuration then
- debugPrint("theMsgIsForMe")
- --IPconfirm(destinationPc,sendingPc,sendingRouter,configuration,msgNum)
- ifSuccessInSending = checkAndSend(message)
- while ifSuccessInSending == false or ifSuccessInSending == "invalid" do
- ifSuccessInSending = checkAndSend(message)
- if #adjascentRouters == 0 then
- print("No routers available...")
- identification()
- break
- end
- end
- end
- elseif ch2 == 5 then
- end
- else
- debugPrint(errorContent)
- end
- end
- end
- function getCientIPs()
- while true do
- local event, side, ch1, ch2, msg, distance = os.pullEvent("modem_message")
- if ch1 == 65526 and ch2 == 1 then
- local clientsToAnalyse = {}
- local possibleClients = {}
- local ok, errorContent = pcall(textutilsunserialize,msg)
- if ok then
- possibleClients = textutilsunserialize(msg)
- debugPrint("Possible clients: ")
- debugPrint(textutilsserialize(possibleClients))
- local clientsToAnalyse = {}
- if next(clientIPs) ~= nil then
- for i,v in pairs(clientIPs) do
- for k,m in pairs(possibleClients) do
- if v[1] == m[1] and v[2] == m[2] and v[3] == m[3] and v[4] == m[4] then
- debugPrint("analyzing...")
- clientsToAnalyse[k] = true
- elseif clientsToAnalyse[k] ~= true then
- clientsToAnalyse[k] = false
- end
- end
- end
- debugPrint("Clients to analyze: ")
- debugPrint(textutilsserialize(clientsToAnalyse))
- for i,v in pairs(clientsToAnalyse) do
- if v ~= true or v == nil then
- debugPrint("v: ")
- debugPrint(v)
- clientIPs[#clientIPs + 1] = possibleClients[i]
- end
- end
- debugPrint("ClientIPs: ")
- debugPrint(textutilsserialize(clientIPs))
- else
- clientIPs = possibleClients
- debugPrint("ClientIPs: ")
- debugPrint(textutilsserialize(clientIPs))
- end
- else
- debugPrint(errorContent)
- end
- end
- end
- end
- function urgentMsg()
- while true do
- local event, side, ch1, ch2, message, distance = os.pullEvent("modem_message")
- if ch2 == 2 then
- local ok = pcall(textutilsunserialize,message)
- if ok then
- local msg = textutilsunserialize(message)
- if msg[3] == nil then
- local clientIsPresent = false
- for i,v in pairs(clientIPs) do
- if v[1] == destinationPc[1] and v[2] == destinationPc[2] and v[3] == destinationPc[3] and v[4] == destinationPc[4] then
- modem.transmit(65525,1,textutilsserialize(destinationPc,sendingPc,{v[5],v[6],v[7],v[8]},configuration,msgNum,msg,port))
- modem.transmit(65525,4,textutilsserialize(destinationPc,sendingPc,msg[4],configuration,msgNum))
- clientIsPresent = true
- end
- end
- end
- end
- end
- end
- end
- --Too many functions--
- clear()
- setUp()
- parallel.waitForAny(identification,messageSender,identificationOfSubRouters,getCientIPs,urgentMsg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement