Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component");
- local computer = require("computer" );
- local event = require("event" );
- local ip2b = {};
- ----------------------------------------
- local IFS = {};
- local ARP = {};
- local PORTS = {};
- function ip2b.valid(ip)
- if type(ip) ~= "string" then return false; end;
- local a,b = ip:find("%x%x%-%x%x");
- if a == 1 and b == 5 then return true; end;
- return false;
- end;
- function route(...)
- end;
- --RD
- event.listen("component_added",
- function(_,adrs,te)
- if te ~= "modem" then return; end;
- if not IFS[adrs] then IFS[adrs] = ""; component.invoke(adrs,"open",1); end;
- end);
- event.listen("component_removed",
- function(_,adrs,te)
- if te ~= "modem" then return; end;
- IFS[IFS[adrs]] = nil;
- IFS[adrs] = nil;
- end);
- event.listen("modem_message",
- function(_,adrs,sadrs,port,dis,...)
- if not ip2b.valid(IFS[adrs]) then return; end;
- local args = {...};
- if port ~= 1 then return; end;
- if args[1] == "ip2b:ip:g" and args[2] == IFS[adrs] then
- component.invoke(adrs,"send",sadrs,1,"ip2b:ip:s",IFS[adrs]);
- elseif args[1] == "ip2b:arp:g" and args[2] == IFS[adrs] then
- component.invoke(adrs,"send",sadrs,1,"ip2b:arp:s",IFS[adrs]);
- elseif args[1] == "ip2b:ping:g" and args[2] == IFS[adrs] then
- component.invoke(adrs,"send",sadrs,1,"ip2b:ping:s",IFS[adrs]);
- elseif args[1] == "ip2b:message" and ip2b.valid(args[2]) and ip2b.valid(args[3])
- and ip2b.valid(args[4]) and ip2b.valid(args[5]) and PORTS[args[6]] then
- if args[5] ~= IFS[adrs] then route(...); return; end;
- for i = 1, 5 do table.remove(args,1); end;
- event.push("ip2b_message",IFS[adrs],table.unpack(args));
- end;
- end);
- function ip2b.updateIFS()
- for k,d in pairs(IFS) do
- if ip2b.valid(k) and component.type(d) ~= "modem" then
- IFS[k] = nil; IFS[d] = nil;
- end;
- end;
- for adrs in component.list("modem") do
- if not IFS[adrs] then IFS[adrs] = ""; end;
- component.invoke(adrs,"open",1);
- end;
- end;
- function ip2b.setIP(ip,adrs)
- if not adrs then adrs = component.modem.address; end;
- if not ip2b.valid(ip) then return nil,"Incorrect IP address!"; end;
- if not IFS[adrs] then return nil,"Bad device!"; end;
- component.invoke(adrs,"broadcast",1,"ip2b:ip:g",ip);
- local ct = computer.uptime();
- repeat
- local ic = {event.pull(2,"modem_message")};
- if ic[1] and ic[6] == "ip2b:ip:s" and ic[7] == ip and ic[4] == 1 then
- return nil,"This IP is used!";
- end;
- until computer.uptime() - ct > 2;
- IFS[adrs] = ip;
- IFS[ip] = adrs;
- return true,"OK!";
- end;
- function ip2b.getIP(adrs)
- if not adrs then adrs = component.modem.address; end;
- if IFS[adrs] then
- return IFS[adrs]; end
- return nil,"Bad device!";
- end;
- function ip2b.delIP(adrs)
- if not adrs then adrs = component.modem.address; end;
- if IFS[adrs] then IFS[adrs] = nil; return true,"OK!" end;
- return nil,"Bad device!";
- end;
- function ip2b.getARP(ip)
- if not ip2b.valid(ip) then return nil; end;
- if ARP[ip] then return ARP[ip]; end;
- component.invoke(adrs,"broadcast",1,"ip2b:arp:g",ip);
- repeat
- local ic = {event.pull(2,"modem_message")};
- if ic[1] and ic[6] == "ip2b:arp:s" and ic[7] == ip and ic[4] == 1 then
- ARP[ip] = ic[3]; ARP[ic[3]] = ip;
- return ic[3];
- end;
- until computer.uptime() - ct > 2;
- return nil;
- end;
- function ip2b.ping(ip,adrs)
- if not adrs then adrs = component.modem.address; end;
- if not ip2b.valid(IFS[adrs]) then return nil,"Bad device!"; end;
- if not ip2b.valid(ip) then return nil,"Incorrect IP adress!"; end;
- component.invoke(adrs,"broadcast",1,"ip2b:ping:g",ip);
- local ct = computer.uptime();
- repeat
- local ic = {event.pull(2,"modem_message")};
- if ic[1] and ic[6] == "ip2b:ping:s" and ic[7] == ip and ic[4] == 1 then
- return computer.uptime() - ct;
- end;
- until computer.uptime() - ct > 2;
- end;
- function ip2b.listIFS()
- local i = pairs(IFS);
- return function()
- local k,d = i();
- while not ip2b.valid(k) do i(); end;
- if ip2b.valid(k) then return k,d; end;
- return nil;
- end;
- end;
- function ip2b.listARP()
- local i = pairs(ARP);
- return function()
- local k,d = i();
- while not ip2b.valid(k) do i(); end;
- if ip2b.valid(k) then return k,d; end;
- return nil;
- end;
- end;
- function ip2b.openPort(port)
- if math.floor(port) ~= port and port < 1 then return nil,"" end;
- PORTS[port] = true;
- end;
- function ip2b.closePort(port)
- if math.floor(port) ~= port and port < 1 then return nil,"" end;
- PORTS[port] = nil;
- end;
- function ip2b.send(ip,port,adrs)
- if math.floor(port) ~= port then return nil,""; end;
- if not adrs then adrs = component.modem.address; end;
- if not ip2b.valid(IFS[adrs]) then return nil,""; end;
- if not ip2b.valid(ip) then return nil,"Incorrect IP address!"; end;
- local sadrs = ip2b.getARP(ip);
- component.invoke(adrs,"send",sadrs,1,"ip2b:message",IFS[adrs],ip,IFS[adrs],ip);
- end;
- ip2b.updateIFS();
- -------------------------------------------
- return setmetatable({},{__index = ip2b});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement