Advertisement
trapcodien

oc_dns-server

Aug 22nd, 2015
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local fs = require "filesystem"
  2. local text = require "text"
  3.  
  4. local dns = {}
  5.  
  6. function dns.read_hosts(path)
  7.   local file = io.open(path, "r")
  8.   local t = {}
  9.  
  10.   if not file then return nil end
  11.   for line in file:lines() do
  12.       table.insert(t, line)
  13.   end
  14.   io.close(file)
  15.   return t
  16. end
  17.  
  18. function dns.get(path, name)
  19.   local hosts = dns.read_hosts(path)
  20.  
  21.   if not hosts then return nil end
  22.  
  23.   for _,v in pairs(hosts) do
  24.     local tab = text.tokenize(v)
  25.     if tab[2] == name then return tab[1] end
  26.   end
  27.   return nil
  28. end
  29.  
  30. function dns.write_hosts(path, list)
  31.   local file = io.open(path, "w")
  32.  
  33.   for _,v in pairs(list) do
  34.     file:write(v)
  35.     file:write("\n")
  36.   end
  37.  
  38.   io.close(file)
  39. end
  40.  
  41. function dns.unregister(path, hosts, addr)
  42.   local file = io.open(path, "w")
  43.  
  44.   for _,v in pairs(hosts) do
  45.     if (text.tokenize(v))[1] ~= addr then
  46.       file:write(v)
  47.       file:write("\n")
  48.     end
  49.   end
  50.   io.close(file)
  51. end
  52.  
  53. function dns.register(path, hosts, addr, name)
  54.   table.insert(hosts, addr .." ".. name)
  55.   dns.write_hosts(path, hosts)
  56. end
  57.  
  58. function dns.check_errors(hosts, addr, name)
  59.   if not addr then return "dns_addr_error" end
  60.   if not name then return "dns_name_error" end
  61.   for _,v in pairs(hosts) do
  62.     v = text.tokenize(v)
  63.     if addr == v[1] then return "dns_addr_error" end
  64.     if name == v[2] then return "dns_name_error" end
  65.   end
  66.   return nil
  67. end
  68.  
  69. function dns.is_registered(hosts, addr)
  70.   for _,v in pairs(hosts) do
  71.     v = text.tokenize(v)
  72.     if addr == v[1] then return true end
  73.   end
  74.   return false
  75. end
  76.  
  77. -- [[                DNS DAEMON                                ]]
  78.  
  79. local event = require "event"
  80. local modem = require("component").modem
  81.  
  82. local state = false
  83. local path = "/etc/hosts.dns"
  84.  
  85. local function callback(_, to, from, port, dist, msg, name)
  86.   if port == 2 then
  87.     local res = "dns_error"
  88.     if msg == "dns_update" then
  89.       res = dns.read_hosts(path)
  90.       res = table.concat(res, "\n")
  91.     elseif msg == "dns_register" then
  92.       local hosts = dns.read_hosts(path)
  93.       local err = dns.check_errors(hosts, from, name)
  94.       if err then
  95.         res = err
  96.       else
  97.         dns.register(path, hosts, from, name)
  98.         res = "dns_register_ok"
  99.       end
  100.     elseif msg == "dns_unregister" then
  101.       local hosts = dns.read_hosts(path)
  102.       if dns.is_registered(hosts, from) then
  103.         dns.unregister(path, hosts, from)
  104.         res = "dns_unregister_ok"
  105.       else res = "dns_unregister_error"
  106.       end
  107.     end
  108.     modem.send(from, 2, res)
  109.   end
  110. end
  111.  
  112. function start(_)
  113.   if not fs.exists(path) then io.close(io.open(path, "w")) end
  114.   if state then
  115.     print("Server is already started.")
  116.     return
  117.   end
  118.   modem.open(2)
  119.   event.listen("modem_message", callback)
  120.   print("DNS Server started.")
  121.   state = true
  122. end
  123.  
  124. function stop(_)
  125.   if not state then
  126.     print("Server is already stopped.")
  127.     return
  128.   end
  129.   modem.close(2)
  130.   event.ignore("modem_message", callback)
  131.   print("DNS Server stopped.")
  132.   state = false
  133. end
  134.  
  135. function status(_)
  136.   if state == true then
  137.     print("DNS Server is ON")
  138.   else
  139.     print("DNS Server is OFF")
  140.   end
  141. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement