OpenComputerScripts/libs/libnetwork.lua

287 lines
8.2 KiB
Lua
Raw Normal View History

2017-10-12 09:50:17 +08:00
--[[
Local netcard wrapper
This require should return a table like:
{
broadcast=function(port,...):bool
send=function(uuid,port,...):bool
open=function(port):bool
isOpen=function(port):bool
close=function(port):bool
}
--]]
2017-10-12 15:20:29 +08:00
local netcard = require("localNetcard")
local event = require("event")
local uuid = require("uuid")
require("vector")
2017-10-12 09:50:17 +08:00
-- Check Netcard
2017-10-12 15:20:29 +08:00
if
(netcard.broadcast == nil or netcard.send == nil or netcard.open == nil or netcard.isOpen == nil or
netcard.close == nil)
then
2017-10-12 09:50:17 +08:00
print("Error: netcard invalid.")
end
2017-10-11 22:20:32 +08:00
2017-10-12 15:20:29 +08:00
local sker = {
-- Socket Kernel Table
fdset = {} -- Store information of sockets
}
2017-10-12 09:50:17 +08:00
2017-10-12 15:20:29 +08:00
local arpker = {} -- ARP Kernel Table
2017-10-12 09:50:17 +08:00
2017-10-12 15:20:29 +08:00
local tcp_syn = string.pack("iii", 1, 1, 1)
local tcp_synack = string.pack("iii", 1, 1, 3)
local tcp_ack = string.pack("iii", 1, 1, 2)
local arp_broadcast = string.pack("iii", 1, 0, 0)
local arp_quest = string.pack("iii", 1, 0, 1)
local dhcp_quest = string.pack("iii", 0, 0, 1)
local dhcp_ack = string.pack("iii", 0, 0, 2)
local trans_data = string.pack("iii", 1, 2, 1)
local trans_ack = string.pack("iii", 1, 2, 2)
2017-10-12 09:50:17 +08:00
function _findFirstAvaliableSocket()
2017-10-12 15:20:29 +08:00
local i = 0
while (sker.fdset[i] ~= nil) do
i = i + 1
end
2017-10-12 09:50:17 +08:00
return i
2017-10-11 22:20:32 +08:00
end
2017-10-12 09:50:17 +08:00
function _doCreateSocket(sfd)
2017-10-12 15:20:29 +08:00
if (sker.fdset[sfd] ~= nil) then
return false, "sfd invalid"
2017-10-12 09:50:17 +08:00
else
2017-10-12 15:20:29 +08:00
sker.fdset[sfd] = {
status = "Ready",
-- remote_cookie
-- local_cookie
sendBus = Vector.new(),
recvBus = Vector.new()
}
return true, "Success"
2017-10-12 09:50:17 +08:00
end
end
function _getSocketStatus(sfd)
2017-10-12 15:20:29 +08:00
if (sker.fdset[sfd] == nil) then
return nil
2017-10-12 09:50:17 +08:00
else
return sker.fdset[sfd].status
end
end
2017-10-12 15:20:29 +08:00
function _setSocketStatus(sfd, status, ...)
if (sker.fdset[sfd] == nil) then
return false
2017-10-12 09:50:17 +08:00
else
2017-10-12 15:20:29 +08:00
sker.fdset[sfd].status = status
if (status == "Connected") then
sker.fdset[sfd].remote_cookie = arg[1]
2017-10-12 09:50:17 +08:00
end
return true
end
end
function _isPortValid(port)
2017-10-12 15:20:29 +08:00
return port > 0 and port < 65536
2017-10-12 09:50:17 +08:00
end
2017-10-12 15:20:29 +08:00
function _generateNewCookie() -- Generate a new cookie
return uuid.next()
end
function _doConnect(sfd, remote_uuid, port) -- Connect to port 10 of a remote device with virtual port
2017-10-12 09:50:17 +08:00
-- Send SYN package (1,1,1)
2017-10-12 15:20:29 +08:00
local syn = tcp_syn
if (not netcard.send(remote_uuid, 10, syn, port)) then
return -1, "Network error"
2017-10-12 09:50:17 +08:00
end
-- Wait 1.5 seconds for SYN+ACK (1,1,3)
2017-10-12 15:20:29 +08:00
local e, remote_cookie = event.pull(1.5, "net_synack")
if (e == nil) then
return -2, "Connection timed out"
2017-10-12 09:50:17 +08:00
end
--- Send ACK package (1,1,2)
2017-10-12 15:20:29 +08:00
local ack = tcp_ack
local newcookie = _generateNewCookie()
-- Update FDSet Cookie
sker.fdset[sfd].remote_cookie = remote_cookie
sker.fdset[sfd].local_cookie = newcookie
if (not netcard.send(remote_uuid, 10, ack, remote_cookie, newcookie)) then
return -1, "Network error"
2017-10-12 09:50:17 +08:00
end
-- Connection established
2017-10-12 15:20:29 +08:00
return 0, "Success", remote_cookie
2017-10-12 09:50:17 +08:00
end
function _isInArpCache(tag) -- Check ARP Check Table with tag for uuid
2017-10-12 15:20:29 +08:00
if (arpker[tag] ~= nil) then
return true, arpker[tag].uuid
2017-10-12 09:50:17 +08:00
else
return false
end
end
2017-10-12 15:20:29 +08:00
function _doArpBroadcastQuery(header, tag) -- Broadcast ARP quest to hardware port 9 (arp)
if (netcard.broadcast(9, header, tag)) then
return true, "Success"
else
return false, "Netcard error"
end
2017-10-12 09:50:17 +08:00
end
function socket() -- Allocate a new socket
2017-10-12 15:20:29 +08:00
local idx = _findFirstAvaliableSocket()
local ret, emsg = _doCreateSocket(idx)
if (not ret) then
return -1, emsg
2017-10-12 09:50:17 +08:00
else
2017-10-12 15:20:29 +08:00
return ret, emsg
2017-10-12 09:50:17 +08:00
end
end
2017-10-12 15:20:29 +08:00
function connect(sfd, remote_tag, port) -- Connect to a remote device
if (_getSocketStatus(sfd) ~= "Ready") then
return -1, "Socket not ready"
elseif (not _isPortValid(port)) then
return -2, "Port Invalid"
2017-10-12 09:50:17 +08:00
else
-- Try to resolve remote_tag to remote_uuid
2017-10-12 15:20:29 +08:00
local remote_uuid = do_arp_query(remote_tag)
if (remote_uuid == nil) then
return -3, "Tag can not be resolved into uuid"
2017-10-12 09:50:17 +08:00
else
2017-10-12 15:20:29 +08:00
local eret, emsg, remote_cookie = _doConnect(sfd, remote_uuid, port)
if (eret == 0) then
_setSocketStatus(sfd, "Connected", remote_cookie)
return 0, "Success"
2017-10-12 09:50:17 +08:00
else
2017-10-12 15:20:29 +08:00
return eret, emsg
2017-10-12 09:50:17 +08:00
end
end
end
2017-10-11 22:20:32 +08:00
end
2017-10-12 15:20:29 +08:00
function bind(sfd, port) -- Bind socket at specific port
2017-10-11 22:20:32 +08:00
end
2017-10-12 15:20:29 +08:00
function listen(sfd, sz) -- Set size of queue for waiting connections
2017-10-11 22:20:32 +08:00
end
function accept(sfd) -- Accept Connection
end
2017-10-12 15:20:29 +08:00
function send(sfd, ...) -- Standard Network I/O
2017-10-11 22:20:32 +08:00
end
function recv(sfd) -- Standard Network I/O
end
function shutdown(sfd) -- Close Socket
end
function close(sfd) -- Close Socket
end
2017-10-12 09:50:17 +08:00
function do_arp_query(tag) -- ARP: Query uuid with tag, might send arp-request
2017-10-12 15:20:29 +08:00
local ret, uuid = _isInArpCache(tag)
if (ret) then
return uuid
2017-10-12 09:50:17 +08:00
else
2017-10-12 15:20:29 +08:00
local questHeader = _generateARPQuestHeader()
for i = 1, 3, 1 do
if (_doArpBroadcastQuery(questHeader, tag)) then
local e, etag, euuid = event.pull(0.5, "net_newarp")
if (e ~= nil and etag == tag) then
return euuid
end
2017-10-12 09:50:17 +08:00
end
end
return nil -- Failed to query arp in 3 tries.
end
2017-10-11 22:20:32 +08:00
end
function run_arp() -- Start ARP Services in background
2017-10-12 15:20:29 +08:00
event.listen(
"modem_message",
function(_event, _receiver, sender, _port, _distance, ...)
if (_port == 9) then
if (arg[1] == arp_broadcast and arg[2] ~= nil) then
-- Received an ARP Broadcast
if (arpker[arg[2]] == nil) then
arpker[arg[2]] = sender
event.push("net_newarp", arg[2], sender)
elseif (arpker[arg[2]] ~= sender) then
arpker[arg[2]] = sender
event.push("net_arpchanged", arg[2], sender)
end -- arpker check
elseif (arg[1] == arp_quest and arg[2] ~= nil and arg[2] == arpker.thisDevice) then
-- Received an ARP Quest Broadcast and it points this device
netcard.send(sender, 9, arp_broadcast, arg[2])
end -- arg1 and arg2 check
end -- port check
end
) -- callback
netcard.open(9)
end
2017-10-11 22:20:32 +08:00
function run_dhcp_client() -- Start DHCP Client in background
2017-10-12 15:20:29 +08:00
local finished = false
event.listen(
"modem_message",
function(_event, _receiver, sender, port, _distance, ...)
if (port == 8 and arg[1] == dhcp_ack) then
-- Receive DHCP Answer
arpker.thisDevice = arg[2]
-- Close Local Port
network.close(7)
-- Change flag to stop running
finished = true
-- Unregister this callback
return false
end
end
)
network.open(7)
while not finished do
network.broadcast(8, dhcp_quest)
os.sleep(1)
end
2017-10-12 09:50:17 +08:00
end
function run_tcp() -- Start TCP Services in background
2017-10-12 15:20:29 +08:00
event.listen(
"modem_message",
function(_event, _receiver, sender, port, _distance, ...)
if (port == 10) then
if (arg[1] == tcp_syn) then --SYN
event.push("net_newsyn", sender, arg[2])
end
elseif (port == 11) then
if (arg[1] == tcp_synack) then --SYN/ACK
event.push("net_newsynack", sender)
end
end
end
)
event.listen(
"modem_message",
function(_event, _receiver, sender, port, _distance, ...)
if (port == 12) then
if (arg[1] == trans_data and arg[2] ~= nil) then
-- Find cookie and push data
for k, t in pairs(sker.fdset) do
if (t.local_cookie == arg[2]) then
t.recvBus.push_back(arg[3])
break
end
end
end
end
end
)
netcard.open(10)
netcard.open(11)
2017-10-12 12:04:50 +08:00
end