2003-05-25 03:54:13 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- TFTP support for the Lua language
|
2003-06-26 20:47:49 +02:00
|
|
|
-- LuaSocket toolkit.
|
2003-05-25 03:54:13 +02:00
|
|
|
-- Author: Diego Nehab
|
|
|
|
-- RCS ID: $Id$
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
2004-06-16 03:02:07 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Load required files
|
|
|
|
-----------------------------------------------------------------------------
|
2005-06-14 06:29:23 +02:00
|
|
|
local base = _G
|
2004-11-27 08:58:04 +01:00
|
|
|
local table = require("table")
|
|
|
|
local math = require("math")
|
|
|
|
local string = require("string")
|
2004-06-16 03:02:07 +02:00
|
|
|
local socket = require("socket")
|
|
|
|
local ltn12 = require("ltn12")
|
2004-10-11 08:18:57 +02:00
|
|
|
local url = require("socket.url")
|
2004-12-23 23:32:12 +01:00
|
|
|
module("socket.tftp")
|
2004-06-16 03:02:07 +02:00
|
|
|
|
2003-05-25 03:54:13 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Program constants
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
local char = string.char
|
|
|
|
local byte = string.byte
|
|
|
|
|
2004-06-16 03:02:07 +02:00
|
|
|
PORT = 69
|
|
|
|
local OP_RRQ = 1
|
|
|
|
local OP_WRQ = 2
|
|
|
|
local OP_DATA = 3
|
|
|
|
local OP_ACK = 4
|
|
|
|
local OP_ERROR = 5
|
|
|
|
local OP_INV = {"RRQ", "WRQ", "DATA", "ACK", "ERROR"}
|
2003-05-25 03:54:13 +02:00
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Packet creation functions
|
|
|
|
-----------------------------------------------------------------------------
|
2004-06-16 03:02:07 +02:00
|
|
|
local function RRQ(source, mode)
|
2011-05-25 22:57:22 +02:00
|
|
|
return char(0, OP_RRQ) .. source .. char(0) .. mode .. char(0)
|
2003-05-25 03:54:13 +02:00
|
|
|
end
|
|
|
|
|
2004-06-16 03:02:07 +02:00
|
|
|
local function WRQ(source, mode)
|
2011-05-25 22:57:22 +02:00
|
|
|
return char(0, OP_RRQ) .. source .. char(0) .. mode .. char(0)
|
2003-05-25 03:54:13 +02:00
|
|
|
end
|
|
|
|
|
2004-06-16 03:02:07 +02:00
|
|
|
local function ACK(block)
|
2011-05-25 22:57:22 +02:00
|
|
|
local low, high
|
|
|
|
low = math.mod(block, 256)
|
|
|
|
high = (block - low)/256
|
|
|
|
return char(0, OP_ACK, high, low)
|
2003-05-25 03:54:13 +02:00
|
|
|
end
|
|
|
|
|
2004-06-16 03:02:07 +02:00
|
|
|
local function get_OP(dgram)
|
2003-05-25 03:54:13 +02:00
|
|
|
local op = byte(dgram, 1)*256 + byte(dgram, 2)
|
|
|
|
return op
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Packet analysis functions
|
|
|
|
-----------------------------------------------------------------------------
|
2004-06-16 03:02:07 +02:00
|
|
|
local function split_DATA(dgram)
|
2011-05-25 22:57:22 +02:00
|
|
|
local block = byte(dgram, 3)*256 + byte(dgram, 4)
|
|
|
|
local data = string.sub(dgram, 5)
|
|
|
|
return block, data
|
2003-05-25 03:54:13 +02:00
|
|
|
end
|
|
|
|
|
2004-06-16 03:02:07 +02:00
|
|
|
local function get_ERROR(dgram)
|
2011-05-25 22:57:22 +02:00
|
|
|
local code = byte(dgram, 3)*256 + byte(dgram, 4)
|
|
|
|
local msg
|
|
|
|
_,_, msg = string.find(dgram, "(.*)\000", 5)
|
|
|
|
return string.format("error code %d: %s", code, msg)
|
2003-05-25 03:54:13 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2005-11-22 09:33:29 +01:00
|
|
|
-- The real work
|
2003-05-25 03:54:13 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2004-06-16 03:02:07 +02:00
|
|
|
local function tget(gett)
|
2003-05-25 03:54:13 +02:00
|
|
|
local retries, dgram, sent, datahost, dataport, code
|
|
|
|
local last = 0
|
2004-11-27 08:58:04 +01:00
|
|
|
socket.try(gett.host, "missing host")
|
2011-05-25 22:57:22 +02:00
|
|
|
local con = socket.try(socket.udp())
|
2004-07-01 05:32:09 +02:00
|
|
|
local try = socket.newtry(function() con:close() end)
|
2003-05-25 03:54:13 +02:00
|
|
|
-- convert from name to ip if needed
|
2011-05-25 22:57:22 +02:00
|
|
|
gett.host = try(socket.dns.toip(gett.host))
|
|
|
|
con:settimeout(1)
|
2003-05-25 03:54:13 +02:00
|
|
|
-- first packet gives data host/port to be used for data transfers
|
2005-11-22 09:33:29 +01:00
|
|
|
local path = string.gsub(gett.path or "", "^/", "")
|
2004-11-27 08:58:04 +01:00
|
|
|
path = url.unescape(path)
|
2003-05-25 03:54:13 +02:00
|
|
|
retries = 0
|
2011-05-25 22:57:22 +02:00
|
|
|
repeat
|
|
|
|
sent = try(con:sendto(RRQ(path, "octet"), gett.host, gett.port))
|
|
|
|
dgram, datahost, dataport = con:receivefrom()
|
2003-05-25 03:54:13 +02:00
|
|
|
retries = retries + 1
|
2011-05-25 22:57:22 +02:00
|
|
|
until dgram or datahost ~= "timeout" or retries > 5
|
|
|
|
try(dgram, datahost)
|
2003-05-25 03:54:13 +02:00
|
|
|
-- associate socket with data host/port
|
2011-05-25 22:57:22 +02:00
|
|
|
try(con:setpeername(datahost, dataport))
|
2004-06-16 03:02:07 +02:00
|
|
|
-- default sink
|
|
|
|
local sink = gett.sink or ltn12.sink.null()
|
2003-05-25 03:54:13 +02:00
|
|
|
-- process all data packets
|
2011-05-25 22:57:22 +02:00
|
|
|
while 1 do
|
2003-05-25 03:54:13 +02:00
|
|
|
-- decode packet
|
2011-05-25 22:57:22 +02:00
|
|
|
code = get_OP(dgram)
|
|
|
|
try(code ~= OP_ERROR, get_ERROR(dgram))
|
2004-07-01 05:32:09 +02:00
|
|
|
try(code == OP_DATA, "unhandled opcode " .. code)
|
2003-05-25 03:54:13 +02:00
|
|
|
-- get data packet parts
|
2011-05-25 22:57:22 +02:00
|
|
|
local block, data = split_DATA(dgram)
|
2003-05-25 03:54:13 +02:00
|
|
|
-- if not repeated, write
|
|
|
|
if block == last+1 then
|
2011-05-25 22:57:22 +02:00
|
|
|
try(sink(data))
|
2003-05-25 03:54:13 +02:00
|
|
|
last = block
|
|
|
|
end
|
|
|
|
-- last packet brings less than 512 bytes of data
|
2011-05-25 22:57:22 +02:00
|
|
|
if string.len(data) < 512 then
|
2004-07-01 05:32:09 +02:00
|
|
|
try(con:send(ACK(block)))
|
|
|
|
try(con:close())
|
|
|
|
try(sink(nil))
|
2004-06-16 03:02:07 +02:00
|
|
|
return 1
|
2003-05-25 03:54:13 +02:00
|
|
|
end
|
|
|
|
-- get the next packet
|
|
|
|
retries = 0
|
2011-05-25 22:57:22 +02:00
|
|
|
repeat
|
|
|
|
sent = try(con:send(ACK(last)))
|
|
|
|
dgram, err = con:receive()
|
2003-05-25 03:54:13 +02:00
|
|
|
retries = retries + 1
|
2011-05-25 22:57:22 +02:00
|
|
|
until dgram or err ~= "timeout" or retries > 5
|
|
|
|
try(dgram, err)
|
|
|
|
end
|
2003-05-25 03:54:13 +02:00
|
|
|
end
|
2004-06-16 03:02:07 +02:00
|
|
|
|
|
|
|
local default = {
|
|
|
|
port = PORT,
|
|
|
|
path ="/",
|
|
|
|
scheme = "tftp"
|
|
|
|
}
|
|
|
|
|
|
|
|
local function parse(u)
|
|
|
|
local t = socket.try(url.parse(u, default))
|
|
|
|
socket.try(t.scheme == "tftp", "invalid scheme '" .. t.scheme .. "'")
|
|
|
|
socket.try(t.host, "invalid host")
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
local function sget(u)
|
2005-11-22 09:33:29 +01:00
|
|
|
local gett = parse(u)
|
2004-06-16 03:02:07 +02:00
|
|
|
local t = {}
|
|
|
|
gett.sink = ltn12.sink.table(t)
|
|
|
|
tget(gett)
|
|
|
|
return table.concat(t)
|
|
|
|
end
|
|
|
|
|
|
|
|
get = socket.protect(function(gett)
|
2004-11-27 08:58:04 +01:00
|
|
|
if base.type(gett) == "string" then return sget(gett)
|
2004-06-16 03:02:07 +02:00
|
|
|
else return tget(gett) end
|
|
|
|
end)
|
2004-11-27 08:58:04 +01:00
|
|
|
|