2020-03-17 10:11:53 -04:00
local computer = require " computer "
local minitel = require " minitel "
2020-03-17 23:03:45 -04:00
local event = require " event "
2020-03-17 10:11:53 -04:00
local rpc = require " rpc "
local netutil = { }
2020-05-10 11:00:13 -04:00
function netutil . importfs ( host , rpath , lpath ) -- string string string -- boolean -- Import filesystem *rpath* from *host* and attach it to *lpath*.
2023-09-19 10:01:44 -04:00
local px = rpc.proxy ( host , " fs_ " .. rpath .. " _ " )
2020-03-17 10:11:53 -04:00
function px . getLabel ( )
return host .. " : " .. rpath
end
2020-03-26 02:37:14 -04:00
px.address = host .. " : " .. rpath
2020-03-17 23:06:40 -04:00
return fs.mount ( lpath , px )
2020-03-17 10:11:53 -04:00
end
2020-05-10 11:00:13 -04:00
function netutil . exportfs ( path ) -- string -- boolean -- Export the directory *path* over RPC.
2020-03-17 10:11:53 -04:00
local path = " / " .. table.concat ( fs.segments ( path ) , " / " )
2020-06-11 02:18:32 -04:00
local px = require ( " unionfs " ) . create ( path )
2023-10-07 21:41:44 -04:00
function px . dirstat ( p )
local rt = { }
for k , v in ipairs ( px.list ( p ) ) do
local fp = p .. " / " .. v
rt [ v ] = { px.isDirectory ( fp ) , px.size ( fp ) , px.lastModified ( fp ) }
end
return rt
end
2020-03-17 10:11:53 -04:00
for k , v in pairs ( px ) do
2023-09-19 10:01:44 -04:00
rpc.register ( " fs_ " .. path .. " _ " .. k , v )
print ( " fs_ " .. path .. " _ " .. k )
2020-03-17 10:11:53 -04:00
end
2020-03-17 23:03:45 -04:00
return true
2020-03-17 10:11:53 -04:00
end
2020-05-10 11:00:13 -04:00
function netutil . ping ( addr , times , timeout , silent ) -- string number number boolean -- boolean number number number -- Request acknowledgment from *addr*, waiting *timeout* seconds each try, and try *times* times. If *silent* is true, don't print status. Returns true if there was at least one successful ping, the number of successes, the number of failures, and the average round trip time.
2020-03-17 10:11:53 -04:00
local times , timeout = times or 5 , timeout or 30
2020-03-17 23:03:45 -04:00
local success , fail , time , avg = 0 , 0 , 0 , 0
2020-03-17 10:11:53 -04:00
for i = 1 , times do
local ipt = computer.uptime ( )
local pid = minitel.genPacketID ( )
computer.pushSignal ( " net_send " , 1 , addr , 0 , " ping " , pid )
local t , a = event.pull ( timeout , " net_ack " )
if t == " net_ack " and a == pid then
2020-03-17 23:03:45 -04:00
if not silent then print ( " Ping reply: " .. tostring ( computer.uptime ( ) - ipt ) .. " seconds. " ) end
success = success + 1
time = time + computer.uptime ( ) - ipt
avg = time / success
2020-03-17 10:11:53 -04:00
else
2020-03-17 23:03:45 -04:00
if not silent then print ( " Timed out. " ) end
fail = fail + 1
2020-03-17 10:11:53 -04:00
end
end
2020-03-17 23:03:45 -04:00
if not silent then print ( string.format ( " %d packets transmitted, %d received, %0.0f%% packet loss, %0.1fs " , times , success , fail / times * 100 , time ) ) end
return success > 0 , success , fail , avg
2020-03-17 10:11:53 -04:00
end
2020-05-10 11:00:13 -04:00
function netutil . nc ( host , port ) -- string number -- boolean -- Starts an interactive Minitel socket connection to *host* on *port*, primarily for remote login. Returns whether the attempt was successful.
2020-04-14 23:33:27 -04:00
port = port or 22
local socket = minitel.open ( host , port )
if not socket then return false end
local b = " "
os.spawn ( function ( )
repeat
local b = socket : read ( " *a " )
if b and b : len ( ) > 0 then
io.write ( b )
end
coroutine.yield ( )
until socket.state ~= " open "
end )
repeat
local b = io.read ( )
if b and b : len ( ) > 0 then
socket : write ( b .. " \n " )
end
until socket.state ~= " open "
2020-05-10 11:00:13 -04:00
return true
2020-04-14 23:33:27 -04:00
end
2020-03-17 10:11:53 -04:00
return netutil