Advertisement
I_M_CODER

ip2b

Nov 27th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.90 KB | None | 0 0
  1. local component = require("component");
  2. local computer  = require("computer" );
  3. local event     = require("event"    );
  4.  
  5. local ip2b = {};
  6. ----------------------------------------
  7. local IFS = {};
  8. local ARP = {};
  9. local PORTS = {};
  10.  
  11. function ip2b.valid(ip)
  12.  if type(ip) ~= "string" then return false; end;
  13.  local a,b = ip:find("%x%x%-%x%x");
  14.  if a == 1 and b == 5 then return true; end;
  15.  return false;
  16. end;
  17.  
  18. function route(...)
  19. end;
  20.  
  21. --RD
  22. event.listen("component_added",
  23. function(_,adrs,te)
  24.  if te ~= "modem" then return; end;
  25.  if not IFS[adrs] then IFS[adrs] = ""; component.invoke(adrs,"open",1); end;
  26. end);
  27.  
  28. event.listen("component_removed",
  29. function(_,adrs,te)
  30.  if te ~= "modem" then return; end;
  31.  IFS[IFS[adrs]] = nil;
  32.  IFS[adrs] = nil;
  33. end);
  34.  
  35. event.listen("modem_message",
  36. function(_,adrs,sadrs,port,dis,...)
  37.  if not ip2b.valid(IFS[adrs]) then return; end;
  38.  local args = {...};
  39.  if port ~= 1 then return; end;
  40.  
  41.  if     args[1] == "ip2b:ip:g"    and args[2] == IFS[adrs] then
  42.   component.invoke(adrs,"send",sadrs,1,"ip2b:ip:s",IFS[adrs]);
  43.  elseif args[1] == "ip2b:arp:g"   and args[2] == IFS[adrs] then
  44.   component.invoke(adrs,"send",sadrs,1,"ip2b:arp:s",IFS[adrs]);
  45.  elseif args[1] == "ip2b:ping:g"  and args[2] == IFS[adrs] then
  46.   component.invoke(adrs,"send",sadrs,1,"ip2b:ping:s",IFS[adrs]);
  47.  elseif args[1] == "ip2b:message" and ip2b.valid(args[2]) and ip2b.valid(args[3])
  48.   and ip2b.valid(args[4]) and ip2b.valid(args[5]) and PORTS[args[6]] then
  49.   if args[5] ~= IFS[adrs] then route(...); return; end;
  50.   for i = 1, 5 do table.remove(args,1); end;
  51.   event.push("ip2b_message",IFS[adrs],table.unpack(args));
  52.  end;
  53. end);
  54.  
  55. function ip2b.updateIFS()
  56.  for k,d in pairs(IFS) do
  57.   if ip2b.valid(k) and component.type(d) ~= "modem" then
  58.    IFS[k] = nil; IFS[d] = nil;
  59.   end;
  60.  end;
  61.  
  62.  for adrs in component.list("modem") do
  63.   if not IFS[adrs] then IFS[adrs] = ""; end;
  64.   component.invoke(adrs,"open",1);
  65.  end;
  66. end;
  67.  
  68. function ip2b.setIP(ip,adrs)
  69.  if not adrs then adrs = component.modem.address; end;
  70.  if not ip2b.valid(ip) then return nil,"Incorrect IP address!"; end;
  71.  if not IFS[adrs] then return nil,"Bad device!"; end;
  72.  
  73.  component.invoke(adrs,"broadcast",1,"ip2b:ip:g",ip);
  74.  local ct = computer.uptime();
  75.  repeat
  76.   local ic = {event.pull(2,"modem_message")};
  77.   if ic[1] and ic[6] == "ip2b:ip:s" and ic[7] == ip and ic[4] == 1 then
  78.    return nil,"This IP is used!";
  79.   end;
  80.  until computer.uptime() - ct > 2;
  81.  
  82.  IFS[adrs] = ip;
  83.  IFS[ip]   = adrs;
  84.  return true,"OK!";
  85. end;
  86.  
  87. function ip2b.getIP(adrs)
  88.  if not adrs then adrs = component.modem.address; end;
  89.  if IFS[adrs] then
  90.   return IFS[adrs]; end
  91.  return nil,"Bad device!";
  92. end;
  93.  
  94. function ip2b.delIP(adrs)
  95.  if not adrs then adrs = component.modem.address; end;
  96.  if IFS[adrs] then IFS[adrs] = nil; return true,"OK!" end;
  97.  return nil,"Bad device!";
  98. end;
  99.  
  100. function ip2b.getARP(ip)
  101.  if not ip2b.valid(ip) then return nil; end;
  102.  if ARP[ip] then return ARP[ip]; end;
  103.  component.invoke(adrs,"broadcast",1,"ip2b:arp:g",ip);
  104.  repeat
  105.   local ic = {event.pull(2,"modem_message")};
  106.   if ic[1] and ic[6] == "ip2b:arp:s" and ic[7] == ip and ic[4] == 1 then
  107.    ARP[ip] = ic[3]; ARP[ic[3]] = ip;
  108.    return ic[3];
  109.   end;
  110.  until computer.uptime() - ct > 2;
  111.  return nil;
  112. end;
  113.  
  114. function ip2b.ping(ip,adrs)
  115.  if not adrs then adrs = component.modem.address; end;
  116.  if not ip2b.valid(IFS[adrs]) then return nil,"Bad device!"; end;
  117.  if not ip2b.valid(ip)        then return nil,"Incorrect IP adress!"; end;
  118.  
  119.  component.invoke(adrs,"broadcast",1,"ip2b:ping:g",ip);
  120.  local ct = computer.uptime();
  121.  repeat
  122.   local ic = {event.pull(2,"modem_message")};
  123.   if ic[1] and ic[6] == "ip2b:ping:s" and ic[7] == ip and ic[4] == 1 then
  124.    return computer.uptime() - ct;
  125.   end;
  126.  until computer.uptime() - ct > 2;
  127. end;
  128.  
  129. function ip2b.listIFS()
  130.  local i = pairs(IFS);
  131.  return function()
  132.   local k,d = i();
  133.   while not ip2b.valid(k) do i(); end;
  134.   if ip2b.valid(k) then return k,d; end;
  135.   return nil;
  136.  end;
  137. end;
  138.  
  139. function ip2b.listARP()
  140.  local i = pairs(ARP);
  141.  return function()
  142.   local k,d = i();
  143.   while not ip2b.valid(k) do i(); end;
  144.   if ip2b.valid(k) then return k,d; end;
  145.   return nil;
  146.  end;
  147. end;
  148.  
  149. function ip2b.openPort(port)
  150.  if math.floor(port) ~= port and port < 1 then return nil,"" end;
  151.  PORTS[port] = true;
  152. end;
  153.  
  154. function ip2b.closePort(port)
  155.  if math.floor(port) ~= port and port < 1 then return nil,"" end;
  156.  PORTS[port] = nil;
  157. end;
  158.  
  159. function ip2b.send(ip,port,adrs)
  160.  if math.floor(port) ~= port then return nil,""; end;
  161.  if not adrs then adrs = component.modem.address; end;
  162.  if not ip2b.valid(IFS[adrs]) then return nil,""; end;
  163.  if not ip2b.valid(ip) then return nil,"Incorrect IP address!"; end;
  164.  local sadrs = ip2b.getARP(ip);
  165.  component.invoke(adrs,"send",sadrs,1,"ip2b:message",IFS[adrs],ip,IFS[adrs],ip);
  166. end;
  167.  
  168. ip2b.updateIFS();
  169. -------------------------------------------
  170. return setmetatable({},{__index = ip2b});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement