2018-02-17 14:56:06 +11:00
|
|
|
local computer = require "computer"
|
|
|
|
local event = require "event"
|
|
|
|
local net = {}
|
|
|
|
net.mtu = 4096
|
|
|
|
net.streamdelay = 60
|
|
|
|
net.minport = 32768
|
|
|
|
net.maxport = 65535
|
|
|
|
net.openports = {}
|
|
|
|
|
|
|
|
function net.genPacketID()
|
|
|
|
local npID = ""
|
|
|
|
for i = 1, 16 do
|
|
|
|
npID = npID .. string.char(math.random(32,126))
|
|
|
|
end
|
|
|
|
return npID
|
|
|
|
end
|
|
|
|
|
|
|
|
function net.usend(to,port,data,npID)
|
|
|
|
computer.pushSignal("net_send",0,to,port,data,npID)
|
|
|
|
end
|
|
|
|
|
|
|
|
function net.rsend(to,port,data)
|
|
|
|
local pid = net.genPacketID()
|
|
|
|
computer.pushSignal("net_send",1,to,port,data,pid)
|
|
|
|
repeat
|
|
|
|
_,rpid = event.pull("net_ack")
|
|
|
|
until rpid == pid
|
|
|
|
end
|
|
|
|
|
|
|
|
-- ordered packet delivery, layer 4?
|
|
|
|
|
|
|
|
function net.send(to,port,ldata)
|
|
|
|
local tdata = {}
|
|
|
|
if ldata:len() > net.mtu then
|
|
|
|
for i = 1, ldata:len(), net.mtu do
|
|
|
|
tdata[#tdata+1] = ldata:sub(1,net.mtu)
|
|
|
|
ldata = ldata:sub(net.mtu+1)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
tdata = {ldata}
|
|
|
|
end
|
|
|
|
for k,v in ipairs(tdata) do
|
|
|
|
net.rsend(to,port,v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- socket stuff, layer 5?
|
|
|
|
|
|
|
|
local function cwrite(self,data)
|
2018-02-17 15:20:09 +11:00
|
|
|
if self.state == "open" then
|
|
|
|
net.send(self.addr,self.port,data)
|
|
|
|
end
|
2018-02-17 14:56:06 +11:00
|
|
|
end
|
|
|
|
local function cread(self,length)
|
|
|
|
local rdata = ""
|
|
|
|
rdata = self.rbuffer:sub(1,length)
|
|
|
|
self.rbuffer = self.rbuffer:sub(length+1)
|
|
|
|
return rdata
|
|
|
|
end
|
|
|
|
|
2018-02-17 15:20:09 +11:00
|
|
|
local function socket(addr,port,sclose) -- todo, add remote closing of sockets
|
2018-02-17 14:56:06 +11:00
|
|
|
local conn = {}
|
|
|
|
conn.addr,conn.port = addr,tonumber(port)
|
|
|
|
conn.rbuffer = ""
|
|
|
|
conn.write = cwrite
|
|
|
|
conn.read = cread
|
|
|
|
conn.state = "open"
|
|
|
|
local function listener(_,f,p,d)
|
|
|
|
if f == conn.addr and p == conn.port then
|
2018-02-17 15:20:09 +11:00
|
|
|
if d == sclose then
|
|
|
|
conn:close()
|
|
|
|
else
|
|
|
|
conn.rbuffer = conn.rbuffer .. d
|
|
|
|
end
|
2018-02-17 14:56:06 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
event.listen("net_msg",listener)
|
|
|
|
function conn.close(self)
|
|
|
|
event.ignore("net_msg",listener)
|
|
|
|
conn.state = "closed"
|
2018-02-17 15:20:09 +11:00
|
|
|
net.rsend(addr,port,sclose)
|
2018-02-17 14:56:06 +11:00
|
|
|
end
|
|
|
|
return conn
|
|
|
|
end
|
|
|
|
|
|
|
|
function net.open(to,port)
|
|
|
|
net.rsend(to,port,"openstream")
|
|
|
|
local st = computer.uptime()+net.streamdelay
|
|
|
|
local est = false
|
|
|
|
while true do
|
|
|
|
_,from,rport,data = event.pull("net_msg")
|
|
|
|
if to == from and rport == port then
|
|
|
|
if tonumber(data) then
|
|
|
|
est = true
|
|
|
|
end
|
|
|
|
break
|
|
|
|
end
|
|
|
|
if st < computer.uptime() then
|
|
|
|
return nil, "timed out"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not est then
|
|
|
|
return nil, "refused"
|
|
|
|
end
|
2018-02-17 15:20:09 +11:00
|
|
|
data = tonumber(data)
|
|
|
|
sclose = ""
|
|
|
|
repeat
|
|
|
|
_,from,nport,sclose = event.pull("net_msg")
|
|
|
|
until from == to and nport == data
|
|
|
|
return socket(to,data,sclose)
|
2018-02-17 14:56:06 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
function net.listen(port)
|
|
|
|
repeat
|
|
|
|
_, from, rport, data = event.pull("net_msg")
|
2018-02-17 18:31:50 +11:00
|
|
|
until rport == port and data == "openstream"
|
2018-02-17 14:56:06 +11:00
|
|
|
local nport = math.random(net.minport,net.maxport)
|
2018-02-17 15:20:09 +11:00
|
|
|
local sclose = net.genPacketID()
|
2018-02-17 14:56:06 +11:00
|
|
|
net.rsend(from,rport,tostring(nport))
|
2018-02-17 15:20:09 +11:00
|
|
|
net.rsend(from,nport,sclose)
|
|
|
|
return socket(from,nport,sclose)
|
2018-02-17 14:56:06 +11:00
|
|
|
end
|
|
|
|
|
2018-02-17 18:31:50 +11:00
|
|
|
function net.flisten(port,listener)
|
|
|
|
local function helper(_,from,rport,data)
|
|
|
|
if rport == port and data == "openstream" then
|
|
|
|
local nport = math.random(net.minport,net.maxport)
|
|
|
|
local sclose = net.genPacketID()
|
|
|
|
net.rsend(from,rport,tostring(nport))
|
|
|
|
net.rsend(from,nport,sclose)
|
|
|
|
listener(socket(from,nport,sclose))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
event.listen("net_msg",helper)
|
|
|
|
return helper
|
|
|
|
end
|
|
|
|
|
2018-02-17 14:56:06 +11:00
|
|
|
return net
|