Advertisement
Guest User

dnsclient.lua

a guest
Jun 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.94 KB | None | 0 0
  1. local component=require("component")
  2. local computer=require("computer")
  3. local serial=require("serialization")
  4. local event=require("event")
  5. local fs=require("filesystem")
  6. local modem=component.modem
  7. modem.open(100)
  8. local liste={}
  9. local config
  10. local found=false
  11. function getTicks()
  12.   return ((os.time() * 1000) / 60 /60) - 6000
  13. end
  14.  
  15. function getServer()
  16.   local t1=getTicks()
  17.   local srun=true
  18.   modem.broadcast(100, config["dnsserver"])
  19.   while srun and not found do
  20.     local t2=getTicks()
  21.     if t2>t1+120 or t2<t1-99 then
  22.       srun=false
  23.     end
  24.     _,_,_, port, _, command, addr, rname=event.pull(1,"modem_message")
  25.     if port==100 and command=="req" and rname==config["dnsserver"] then
  26.       found=true
  27.      end
  28.   end
  29.   if found then
  30.     liste[config["dnsserver"]]=addr
  31.   else
  32.     print("Kein DNS im Netz gefunden")
  33.   end
  34. end
  35.  
  36. function register()
  37.   modem.send(liste[config[dnsserver]], 100, "set", myname)
  38. end
  39.  
  40. function loadConfig()
  41.   local file=io.open("/etc/dns.cfg")
  42.   local text=file:read("*all")
  43.   print(test)
  44.   file:close()
  45.   config=serial.unserialize(text)
  46. end
  47.  
  48. function request(name)
  49.   local t1=getTicks()
  50.   local srun=true
  51.   local found=false
  52.   modem.send(liste[config["dnsserver"]], 100, name)
  53.   while srun and not found do
  54.     local t2=getTicks()
  55.     if t2>t1+80 or t2<t1-79 then
  56.       srun=false
  57.     end
  58.     _, _, _, port, _, command, addr, rname=event.pull("modem_message")
  59.     if port==100 and command=="req" and rname==name then
  60.       found=true  
  61.     end
  62.   end
  63.   return addr    
  64. end
  65.  
  66. function ns(name, n)
  67.   if n or liste[name]==null or liste[name]=="" then
  68.     return request(name)
  69.   else
  70.     return liste[name]
  71.   end
  72. end
  73.  
  74. function nsDaemon(event, name, n)
  75.   computer.pushSignal("dnsReq", ns(name, n), name)  
  76. end
  77.  
  78. function init()
  79.   os.sleep(1)
  80.   loadConfig()
  81.   if config["dnsserver"]~=config["computername"] then
  82.     getServer()
  83.     if found then
  84.       register()
  85.       event.listen("dnsGet", nsDaemon)  
  86.     end
  87.   end
  88. end
  89.  
  90. function start()
  91.   init()
  92. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement