xuma202

Untitled

Aug 4th, 2012
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. --DNS
  2. term_x, term_y = term.getSize()
  3. id = os.computerID()
  4. version = "v 1.0"
  5. modemSide = ""
  6. sides = {"top", "bottom", "left", "right", "front", "back"}
  7. privateKey = 30679223
  8. modN = 43888321
  9. PackageIdentifier = "_GOO.pack"
  10. pack_pos = 1;
  11.  
  12.  
  13. function findModem()
  14. for i = 1, #sides, 1 do
  15. if (peripheral.getType(sides[i]) == "modem") then
  16. modemSide = sides[i]
  17. end
  18. end
  19. if (modemSide == "") then return false end
  20. return true
  21. end
  22.  
  23. function readBytesFromPack(d, b)
  24. s = string.sub(d, pack_pos, pack_pos + b)
  25. pack_pos = pack_pos +b +1
  26. return s
  27. end
  28.  
  29. function isValidPackage(s)
  30. head = readBytesFromPack(s, string.len(PackageIdentifier))
  31. if (head == PackageIdentifier) then
  32. return true
  33. end
  34. return false
  35. end
  36.  
  37. function resolveAddr(a)
  38. f = fs.open("servers.ls", "r")
  39. for line in file:lines() do
  40. s,e = string.find (line, a)
  41. if (s ~= nil) then
  42. return string.sub(line, e+1)
  43. end
  44. end
  45.  
  46. end
  47.  
  48. function parsePackage(data, sId, dis)
  49. packId = toNumber(readBytesFromPack(data, 3))
  50. if (packId == 01) then --DNS REQ
  51. l = toNumber(readBytesFromPack(data, 2))
  52. addr = readBytesFromPack(data, l)
  53. res = resolveAddr(addr)
  54. print(res)
  55. end
  56. end
  57.  
  58. ------------------------------------
  59.  
  60. term.clear()
  61. term.setCursorPos(1,1)
  62. print("Google DNS id: "..id)
  63. if (findModem() == false) then print("No modem found!") os.shutdown() end
  64. print("Found modem at side: "..modemSide);
  65. rednet.open(modemSide)
  66. print("Rednet opened")
  67. print("operating...")
  68.  
  69. while true do
  70. _event, _senderId, _data, _distance = os.pullEvent("rednet_message")
  71. pack_pos = 1;
  72. if (isValidPackage(_data)) then
  73. parsePackage(_data, _senderId, _distance)
  74. end
  75. end
Advertisement
Add Comment
Please, Sign In to add comment