Advertisement
ladyDia

Default Networked Device

Nov 14th, 2024 (edited)
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.68 KB | None | 0 0
  1. --Variables
  2. local networkDeviceSide = "left" --Change this to where network device is
  3. deviceType = "default"
  4. ipAddress = {-1,-1,-1} --We aren't working with subnets yet
  5. routerID = -1 --The computer ID of the router, allowing for responses to be returned
  6. message = {}
  7. packet = {}
  8. localFunctions = {}
  9.  
  10. --[[
  11.     All data packets have a set structure made of four sections
  12.     First Section - IP Address of target system
  13.     Second Section - IP Address of sending system
  14.     Third Section - Type of packet being sent (what protocol does packet follow)
  15.     Fourth Section - The main data the packet is transporting
  16.  
  17.     For testing, the IP address will be the same as the device ID.
  18.     In the future this will be changed to allow for proper routing and
  19.     security measures.
  20.  
  21.     The protocol the packet follows is a string in all caps
  22.     (CDP,ERP,DRP,UPP,etc) to allow for universal understanding (and style)
  23.  
  24.     An IP address of 0 for the target system means that the packet is for
  25.     the router to work with. Trying to assign a different device this IP will fail
  26.     as the router will intercept all packets with that IP.
  27. --]]
  28.  
  29. local function sendPacket (
  30.         targetIP, --Array
  31.         protocol, --String
  32.         data --Any
  33. )
  34.     --[[
  35.         This function sends a packet to an IP address in the network.
  36.         Each packet needs to follow the rules of packet construction.
  37.         If the target IP is 0 then the packet will be sent to the default router.
  38.         If the sending IP is -1 then that means the IP hasn't been set yet
  39.         and will need to be to recieve packets from devices other than the router.
  40.     --]]
  41.     --Checking if there is a default router to send packet to, otherwise skip all code
  42.     if routerID == -1 then
  43.         print("Default router not set")
  44.         goto skip
  45.     end
  46.     --Checking if packet components have the right data types, otherwise skip all code
  47.     if not (type(targetIP) == "table") or not (type(protocol) == "string") then
  48.         print("Improperly formatted packet")
  49.         goto skip
  50.     end
  51.     --Packet construction
  52.     local packet = {
  53.         targetIP,
  54.         ipAddress,
  55.         protocol,
  56.         data
  57.     }
  58.     --Sending packet
  59.     print(targetIP[1].."."..targetIP[2].."."..targetIP[3].. " " .. ipAddress[1].."."..ipAddress[2].."."..ipAddress[3] .. " " .. protocol .. " " .. data)
  60.     rednet.send(routerID,packet)
  61.     ::skip::
  62. end
  63.  
  64.  local function responseRDP ()
  65.     --[[
  66.     Router Discovery Protocol
  67.     Used by a router to discover other routers and build up a internetwork routing table.
  68.     After recieving a routing table, start adding it to router routing table
  69.     Routing table info:
  70.     1 - IP of router
  71.     2 - ID of next hop
  72.     3 - IP of next hop? (for future tracert)
  73.     4 - Number of hops to IP (To find shortest route)
  74.    
  75.     Routers will not have themselves on their own routing tables
  76.     --]]
  77. end
  78.  
  79. local function responseCDP ()
  80.     --[[
  81.     Computer Discovery Protocol
  82.     Used by a router to discover devices on the network then assign them IP addresses
  83.     and set itself as the device's default router. Devices already with an IP and
  84.     default router do not respond to CDP packets to avoid confusion on the side of
  85.     the router.
  86.     --]]
  87.     --If the device already has an IP and assigned Router then skip all code
  88.     if not (ipAddress == {-1,-1,-1})  and not (routerID == -1) then goto skip end
  89.     --Sets the device's default router to the device ID that sent the packet
  90.     routerID = message[1]
  91.     --Send CDP packet to router (IP 0) with the ID of the device
  92.     sendPacket({0,0,0},"CDP",os.getComputerID())
  93.     --An IP packet should be sent in the near future to this device to assign IP
  94.     ::skip::
  95. end
  96.  
  97. local function responseERP ()
  98.     --[[
  99.         Echo Request Protocol is used to see if an IP address is responding.
  100.         Sending an ERP packet will cause the device to send a return packet back to the
  101.         source device.
  102.     --]]
  103.     if packet[4] == "Ping" then
  104.         sendPacket(packet[2],"ERP","pong")
  105.     end
  106. end
  107.  
  108. local function responseDRP ()
  109.     --Device Request Protocol
  110. end
  111.  
  112. local function responseUPP ()
  113.     --Unknown Packet Protocol
  114. end
  115.  
  116. local function responseIP ()
  117.     --[[
  118.     Internet Protocol
  119.     Used by router to give a device an IP address. Will only be accepted if the
  120.     router is the default router the device knows. If the default router is
  121.     unassigned then the IP packet is dropped. The data on the packet should
  122.     be a number.
  123.     --]]
  124.     --Checking if routerID is assigned
  125.     print(routerID)
  126.     if routerID == -1 then goto skip end
  127.     --Checking if routerID is the same as the message sender
  128.     if not (routerID == message[1]) then goto skip end
  129.     --Setting the IP of the device to the one sent by the router
  130.     ipAddress = packet[4]
  131.    
  132.     --Sending IP packet with the device type to the router
  133.     sendPacket({0,0,0},"IP",deviceType)
  134.     ::skip::
  135. end
  136.  
  137. local function responseDP ()
  138.     --Data  Packet
  139. end
  140.  
  141. local function responseAP ()
  142.     --Acknowledgement Packet
  143. end
  144.  
  145. function mainLoop ()
  146.     --This function runs continuously and allows the device to respond to requests and packets
  147.     while true do
  148.         print("Ready to recieve message")
  149.         --Wait until rednet message recieved
  150.         message = {rednet.receive()}
  151.         --Pulling out the packet inside of the message
  152.         packet = (message[2])
  153.        
  154.         --Checking if packet is properly formatted, if it is not then drop packet and continue loop
  155.         if #packet < 4 then goto continue end
  156.         if packet[3] == "RDP" then goto continue end
  157.         if not (packet[3] == "CDP") then
  158.             print("Packet Type: " .. packet[3])
  159.         end
  160.         if localFunctions["response"..packet[3]] then
  161.             --If protocol exists
  162.             if not (packet[3] == "CDP") then
  163.                 print("Running response "..packet[3])
  164.             end
  165.             localFunctions["response"..packet[3]]()
  166.             --table.insert(dataQueue,packet)
  167.         else  
  168.             --Send UPP packet to source device if protocol is unknown
  169.             print("Unknown")
  170.             sendPacket(packet[1],"UPP","Unknown Packet Type: "..packet[3])
  171.         end
  172.         --]]
  173.         ::continue::
  174.         --End of cycle upkeep
  175.         --Resetting last received message and packet back to their default states
  176.         message = {}
  177.         packet = {}
  178.     end
  179. end
  180.  
  181. --Finding all of the local functions and storing them
  182. index = 1
  183. while true do
  184.     key, data = debug.getlocal(1,index)
  185.     if not key then break end
  186.     --print(key)
  187.     if type(data) == "function" then
  188.         if debug.getinfo(data, "S").what == "Lua" then
  189.             localFunctions[key] = data
  190.         end
  191.     end
  192.     index = index + 1
  193. end
  194.  
  195. --Turning on the network capabilities of the device
  196. peripheralNames = peripheral.getNames()
  197. for i = 1, #peripheralNames, 1 do
  198.     if peripheral.hasType(peripheralNames[i],"modem") then
  199.         rednet.open(peripheralNames[i])
  200.     end
  201. end
  202. --Calling the mainLoop to start program
  203. print("Starting mainLoop")
  204. mainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement