pandorrama

dns_client

Sep 9th, 2020 (edited)
965
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require("component")
  2. local modem = component.modem
  3. local event = require("event")
  4. local ttf = require("tableToFile")
  5.  
  6. local settingsLocation = "/dns/data/DNS_SETTINGS.cfg"
  7. local settings = ttf.load(settingsLocation)
  8.  
  9. local internal = {}
  10.  
  11. function internal.tableReply(rAddr, port, service, request, response, reason)
  12.   local reply = {
  13.     rAddr = rAddr,
  14.     port = port,
  15.     service = service,
  16.     request = request,
  17.     response = {
  18.       value = response,
  19.       reason = reason or nil
  20.     }
  21.   }
  22.   return reply
  23. end
  24.  
  25. function internal.checkDetails(details, reply)
  26.   if (details[1] == reply.service and details[2] == reply.request) then
  27.     return true
  28.   else
  29.     return false
  30.   end
  31. end
  32.  
  33. local dns_client = {}
  34.  
  35. function dns_client.discover()
  36.   local details = {"DNS", "DISCOVER"}
  37.   modem.broadcast(settings.port, details[1], details[2])
  38.   local _, _, rAddr, port, _, service, request, value, reason = event.pull(1, "modem_message")
  39.   local reply
  40.  
  41.   if (rAddr) then
  42.     reply = internal.tableReply(rAddr, port, service, request, value, reason)
  43.     if (internal.checkDetails(details, reply)) then
  44.       if (reply.response.value) then
  45.         settings.DNS_SERVER = reply.response.value
  46.         ttf.save(settings, settingsLocation)
  47.         return reply.response.value
  48.       end
  49.     else
  50.       return reply.response.value, reply.response.reason
  51.     end
  52.   else
  53.     return false, "TIMED_OUT"
  54.   end
  55. end
  56.  
  57. function internal.send(details, data)
  58.   if (settings.DNS_SERVER) then
  59.     modem.send(settings.DNS_SERVER, settings.port, details[1], details[2], data)
  60.   else
  61.     local tmpAddr = dns_client.discover()
  62.     if (tmpAddr) then
  63.       modem.send(tmpAddr, settings.port, details[1], details[2], data)
  64.     else
  65.       return false, "TIMED_OUT"
  66.     end
  67.   end
  68. end
  69.  
  70. function internal.request(details, data)
  71.   internal.send(details, data)
  72.   local _, _, rAddr, port, _, service, request, value, reason = event.pull(1, "modem_message")
  73.   local reply
  74.   if (rAddr) then
  75.     reply = internal.tableReply(rAddr, port, service, request, value, reason)
  76.     if (internal.checkDetails(details, reply)) then
  77.       return reply.response.value, reply.response.reason
  78.     end
  79.   else
  80.     return false, "TIMED_OUT"
  81.   end
  82. end
  83.  
  84. function dns_client.register(ip)
  85.   local details = {"DNS", "REGISTER"}
  86.   return internal.request(details, ip)
  87. end
  88.  
  89. function dns_client.lookup(ip)
  90.   local details = {"DNS", "LOOKUP"}
  91.   return internal.request(details, ip)
  92. end
  93.  
  94. function dns_client.rlookup(addr)
  95.   local details = {"DNS", "RLOOKUP"}
  96.   return internal.request(details, addr)
  97. end
  98.  
  99. function dns_client.start()
  100.   if (modem.open(settings.port)) then
  101.     return true
  102.   else
  103.     return false, "IN_USE"
  104.   end
  105. end
  106.  
  107. function dns_client.stop()
  108.   modem.close(settings.port)
  109.   return true
  110. end
  111.  
  112. return dns_client
RAW Paste Data