Advertisement
Guest User

ip

a guest
Oct 23rd, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.56 KB | None | 0 0
  1. --[[
  2. CC IP API has custom events:
  3.     IP_connect          connected with the network
  4.     IP_connect_time_out failed to connect to the network
  5.     IP_connection_lost  lost connection to the network
  6.     IP_message          a message is recieved
  7.         sender adress
  8.         IP message channel
  9.         IP message reply channel
  10.         message
  11.         is broadcast    (not implemented)
  12.     IP_host_resolved    hostname has been resolved
  13.         hostname
  14.         real adress
  15.     IP_host_error       hostname cannot be resolved
  16. ]]--
  17.  
  18. local function split(str, chr)
  19.     r = {}
  20.     at = 1
  21.     for i = 1, str:len() do
  22.         if string.sub(str, i, i) == chr then
  23.             at = at + 1
  24.         else
  25.             if r[at] == nil then
  26.                 r[at] = string.sub(str, i, i)
  27.             else
  28.                 r[at] = r[at].. string.sub(str, i, i)
  29.             end
  30.         end
  31.     end
  32.     return r
  33. end
  34.  
  35. reqport = 8976
  36. adress = "."
  37. adress_giver = "."
  38. modem = nil
  39. modem_side = nil
  40. connect_timer = nil
  41. check_timer = nil
  42. dns_timer = nil
  43. connect_time = 5
  44. check_time = 3
  45. dns_time = 3
  46. get_ip = false
  47. log = false
  48. lmid = -1
  49. dns_cache = {}
  50. message_queue = {}
  51.  
  52. function initialize(side)
  53.     if pocket then
  54.         side = "back"
  55.         if log then
  56.             print("pocket computer, forcing modem to \"back\"")
  57.         end
  58.     end
  59.     if peripheral.getType(side) == "modem" then
  60.         modem = peripheral.wrap(side)
  61.         modem_side = side
  62.         if log then
  63.             print("successfully initialised IP API with modem \"".. side.. "\"")
  64.         end
  65.         modem.open(reqport)
  66.         connect()
  67.     else
  68.         error("IP API: initialise: side ".. side.. " does not have a modem attached!")
  69.     end
  70. end
  71. function open(port)
  72.     modem.open(port + 1000)
  73.     if log then
  74.         print("opened port ".. port)
  75.     end
  76. end
  77. function close(port)
  78.     modem.close(port + 1000)
  79.     if log then
  80.         print("closed port ".. port)
  81.     end
  82. end
  83. function transmit(to, port, reply, message)
  84.     if not isIP(to) and to ~= "broadcast" then
  85.         if dns_cache[to] ~= nil then
  86.             to = dns_cache[to]
  87.         else
  88.             resolveHostname(to)
  89.             if message_queue[to] == nil then
  90.                 message_queue[to] = {}
  91.             end
  92.             message_queue[to][#message_queue[to] + 1] = {}
  93.             message_queue[to][#message_queue[to]].port = port
  94.             message_queue[to][#message_queue[to]].reply = reply
  95.             message_queue[to][#message_queue[to]].message = message
  96.             return
  97.         end
  98.     end
  99.     mmsgid = math.floor(math.random(0, 99999))
  100.     lmid = mmsgid
  101.     modem.transmit(port + 1000, reply + 1000, adress.. ":".. mmsgid.. ":".. to.. ":".. message)
  102. end
  103. function broadcast(port, reply, message)
  104.     transmit("broadcast", port, reply, message)
  105. end
  106. function doLogs(b)
  107.     log = b
  108.     if log then
  109.         print("logging set to true")
  110.     end
  111. end
  112. function deInitialize()
  113.     adress = "."
  114.     adress_giver = "."
  115.     modem = nil
  116.     connect_timer = nil
  117.     check_timer = nil
  118.     get_ip = false
  119.     if log then
  120.         print("successfully de-initialised IP API")
  121.     end
  122. end
  123. function getConnectionStatus()
  124.     r1 = adress ~= "."
  125.     r2 = adress
  126.     return r1, r2
  127. end
  128. function checkConnectStatus()
  129.     check_timer = os.startTimer(check_time)
  130.     connect_timer = os.startTimer(connect_time)
  131.     modem.transmit(reqport, reqport, adress.. ":".. adress.. ":".. adress_giver.. ":ping")
  132.     if log then
  133.         print("connection is OK")
  134.     end
  135. end
  136. function clearCache()
  137.     dns_cache = {}
  138. end
  139. function resolveHostname(name)
  140.     if isIP(name) then
  141.         os.queueEvent("IP_host_resolved", name, name)
  142.         return true
  143.     elseif dns_cache[name] ~= nil then
  144.         os.queueEvent("IP_host_resolved", name, dns_cache[name])
  145.         return true
  146.     else
  147.         dns_timer = os.startTimer(dns_time)
  148.         modem.transmit(reqport, reqport, adress.. ":".. adress.. ":DNS:get:".. name)
  149.         if log then
  150.             print("awaiting DNS")
  151.         end
  152.         return false
  153.     end
  154. end
  155. function isIP(name)
  156.     s = split(name, ".")
  157.     if #s < 4 then
  158.         return false
  159.     end
  160.     n1 = tonumber(s[1])
  161.     n2 = tonumber(s[1])
  162.     n3 = tonumber(s[1])
  163.     n4 = tonumber(s[1])
  164.     return n1 >= 0 and n1 % 1 == 0 and n2 >= 0 and n2 % 1 == 0 and n3 >= 0 and n3 % 1 == 0 and n4 >= 0 and n4 % 1 == 0
  165. end
  166. function checkEvents(event, p1, p2, p3, p4, p5)
  167.     if event == "IP_host_resolved" then
  168.         if message_queue[p1] ~= nil then
  169.             for i = 1, #message_queue[p1] do
  170.                 transmit(p1, message_queue[p1][i].port, message_queue[p1][i].reply, message_queue[p1][i].message)
  171.             end
  172.         end
  173.     elseif event == "timer" then
  174.         if p1 == dns_timer then
  175.             os.queueEvent("IP_host_error")
  176.         elseif p1 == check_timer then
  177.             checkConnectStatus()
  178.         elseif p1 == connect_timer then
  179.             if get_ip then
  180.                 get_ip = false
  181.                 os.queueEvent("IP_connect_time_out")
  182.                 if log then
  183.                     print("connection timed out!")
  184.                 end
  185.             else
  186.                 adress = "."
  187.                 os.queueEvent("IP_connection_lost")
  188.                 if log then
  189.                     print("connection lost!")
  190.                 end
  191.             end
  192.             if adress ~= "." then
  193.                 checkConnectStatus()
  194.             else
  195.                 connect()
  196.             end
  197.             return true
  198.         end
  199.     elseif event == "modem_message" then
  200.         if p1 == modem_side and p2 ~= reqport then
  201.             sendChannel = p2
  202.             replyChannel = p3
  203.             trace, mraw = decodeMessage(p4)
  204.             msg = mraw[3]
  205.             for i = 4, #mraw do
  206.                 msg = msg..":".. mraw[i]
  207.             end
  208.             tarr = split(trace, ">")
  209.             sendAdress = tarr[#tarr]
  210.             if (mraw[2] == adress or mraw[2] == "broadcast") and tonumber(mraw[1]) ~= lmid then
  211.                 os.queueEvent("IP_message", sendAdress, sendChannel - 1000, replyChannel - 1000, msg)
  212.                 lmid = tonumber(mraw[1])
  213.             end
  214.         end
  215.         if p1 == modem_side and p2 == reqport then
  216.             trace, msg = decodeMessage(p4)
  217.             if log then
  218.                 print("message trace: ".. trace)
  219.                 print("message:")
  220.                 for i = 1, #msg do
  221.                     print(msg[i])
  222.                 end
  223.             end
  224.             if #msg == 3 then
  225.                 if get_ip and msg[1] == tostring(os.getComputerID()) and msg[2] == "adress" then
  226.                     adress = msg[3]
  227.                     satr = split(trace, ">")
  228.                     adress_giver = satr[#satr]
  229.                     get_ip = false
  230.                     if log then
  231.                         print("connected and got adress ".. adress)
  232.                         print("from ".. adress_giver)
  233.                     end
  234.                     os.queueEvent("IP_connect", adress)
  235.                     check_timer = os.startTimer(check_time)
  236.                     connect_timer = nil
  237.                     return true
  238.                 end
  239.                 if not get_ip and msg[1] == adress_giver and msg[2] == adress and msg[3] == "pong" then
  240.                     ckeck_timer = os.startTimer(check_time)
  241.                     return true
  242.                 end
  243.             end
  244.             if #msg == 4 then
  245.                 if msg[1] == "DNS" and msg[2] == adress then
  246.                     dns_cache[msg[3]] = msg[4]
  247.                     os.queueEvent("IP_host_resolved", msg[3], msg[4])
  248.                 end
  249.             end
  250.             return true
  251.         end
  252.         return false
  253.     end
  254.     return false
  255. end
  256. function decodeMessage(raw)
  257.     arr = split(raw, ":")
  258.     sendTrace = arr[1]
  259.     message = {}
  260.     for i = 2, #arr do
  261.         message[i - 1] = arr[i]
  262.     end
  263.     return sendTrace, message
  264. end
  265. function connect()
  266.     get_ip = true
  267.     connect_timer = os.startTimer(connect_time)
  268.     modem.transmit(reqport, reqport, os.getComputerID().. ":getadress")
  269.     if log then
  270.         print("connecting...")
  271.     end
  272. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement