Advertisement
Guest User

rootdns

a guest
Aug 23rd, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.51 KB | None | 0 0
  1. --[[
  2.     Root DNS server.
  3.     By Mitchfizz05. Licensed under the GNU General Pubic License v3.
  4. ]]
  5.  
  6. term.clear()
  7. term.setCursorPos(1,1)
  8.  
  9. print("Root DNS server starting...")
  10.  
  11. -- Scan for modem.
  12. print("Scanning for modem...")
  13. local modemSide = nil
  14. for n,side in ipairs(rs.getSides()) do
  15.     if peripheral.getType(side) == "modem" then
  16.         -- Modem detected!
  17.         modemSide = side
  18.         break
  19.     end
  20. end
  21.  
  22. if modemSide == nil then
  23.     -- No modem detected. Exit program.
  24.     print("No modem detected.")
  25.     return
  26. end
  27.  
  28. -- Open modem.
  29. rednet.open(modemSide)
  30. print("Opened modem on the " .. modemSide .. " side.")
  31.  
  32. -- Scan for other root DNS servers.
  33. print("Scanning for other DNS servers...")
  34. rednet.broadcast("dns.search")
  35. local timer = os.startTimer(1)
  36. while true do
  37.     local e,id,msg,proto = os.pullEvent()
  38.     if e == "rednet_message" then
  39.         if msg == "dns.pong" then
  40.             -- Another DNS server found!
  41.             print("Other DNS servers found on the network!")
  42.             print("Only 1 DNS server can be on a network at once.")
  43.             return
  44.         end
  45.     elseif e == "timer" and id == timer then
  46.         break
  47.     end
  48. end
  49. print("No other DNS servers found.")
  50.  
  51. -- Stores all current domain names.
  52. domains  = {}
  53.  
  54. -- Load domains from file.
  55. print("Reading DNS file...")
  56. local file = fs.open("data/zones", "r")
  57. if not file then
  58.     -- Fatal error! Zonefile cannot be read!
  59.     print("Fatal error! Zonefile cannot be read!")
  60.     return
  61. end
  62.  
  63. -- Iterate through lines.
  64. local line = ""
  65. while true do
  66.     -- Read line.
  67.     line = file:readLine()
  68.    
  69.     -- Check that the line was read.
  70.     if not line then
  71.         break
  72.     end
  73.    
  74.     -- Split the domain record.
  75.     local domain = string.sub(line, 0, string.find(line, ":") - 1)
  76.     local target = string.sub(line, string.find(line, ":") + 1)
  77.    
  78.     -- Register the domain into the domains array.
  79.     domains[domain] = target
  80. end
  81.  
  82. -- Close the zonefile.
  83. file:close()
  84.  
  85. -- Server started.
  86. print("Root DNS server started!")
  87.  
  88. -- Begin rednet loop.
  89. while true do
  90.     local id,msg = rednet.receive()
  91.     if msg == "ping" then
  92.         -- Ping request.
  93.         rednet.send(id, "pong")
  94.     elseif msg == "dns.search" then
  95.         -- DNS server scan.
  96.         rednet.send(id, "dns.pong")
  97.     elseif string.sub(msg, 0, #"dns.lookup:") == "dns.lookup:" then
  98.         -- DNS lookup.
  99.         local domain = string.sub(msg, #"dns.lookup:" + 1)
  100.        
  101.         print("Request to lookup domain: " .. domain)
  102.        
  103.         -- Check that the domain name exists.
  104.         if (domains[domain]) then
  105.             rednet.send(id, "dns.probe:" .. domains[domain])
  106.         else
  107.             -- Domain doesn't exist!
  108.             rednet.send(id, "dns.probe.failed")
  109.         end
  110.     end
  111. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement