| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
local base = _G |
| 11 |
local table = require("table") |
| 12 |
local math = require("math") |
| 13 |
local string = require("string") |
| 14 |
local socket = require("socket") |
| 15 |
local ltn12 = require("ltn12") |
| 16 |
local url = require("socket.url") |
| 17 |
module("socket.tftp") |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
local char = string.char |
| 23 |
local byte = string.byte |
| 24 |
|
| 25 |
PORT = 69 |
| 26 |
local OP_RRQ = 1 |
| 27 |
local OP_WRQ = 2 |
| 28 |
local OP_DATA = 3 |
| 29 |
local OP_ACK = 4 |
| 30 |
local OP_ERROR = 5 |
| 31 |
local OP_INV = {"RRQ", "WRQ", "DATA", "ACK", "ERROR"} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
local function RRQ(source, mode) |
| 37 |
return char(0, OP_RRQ) .. source .. char(0) .. mode .. char(0) |
| 38 |
end |
| 39 |
|
| 40 |
local function WRQ(source, mode) |
| 41 |
return char(0, OP_RRQ) .. source .. char(0) .. mode .. char(0) |
| 42 |
end |
| 43 |
|
| 44 |
local function ACK(block) |
| 45 |
local low, high |
| 46 |
low = math.mod(block, 256) |
| 47 |
high = (block - low)/256 |
| 48 |
return char(0, OP_ACK, high, low) |
| 49 |
end |
| 50 |
|
| 51 |
local function get_OP(dgram) |
| 52 |
local op = byte(dgram, 1)*256 + byte(dgram, 2) |
| 53 |
return op |
| 54 |
end |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
local function split_DATA(dgram) |
| 60 |
local block = byte(dgram, 3)*256 + byte(dgram, 4) |
| 61 |
local data = string.sub(dgram, 5) |
| 62 |
return block, data |
| 63 |
end |
| 64 |
|
| 65 |
local function get_ERROR(dgram) |
| 66 |
local code = byte(dgram, 3)*256 + byte(dgram, 4) |
| 67 |
local msg |
| 68 |
_,_, msg = string.find(dgram, "(.*)\000", 5) |
| 69 |
return string.format("error code %d: %s", code, msg) |
| 70 |
end |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
local function tget(gett) |
| 76 |
local retries, dgram, sent, datahost, dataport, code |
| 77 |
local last = 0 |
| 78 |
socket.try(gett.host, "missing host") |
| 79 |
local con = socket.try(socket.udp()) |
| 80 |
local try = socket.newtry(function() con:close() end) |
| 81 |
|
| 82 |
gett.host = try(socket.dns.toip(gett.host)) |
| 83 |
con:settimeout(1) |
| 84 |
|
| 85 |
local path = string.gsub(gett.path or "", "^/", "") |
| 86 |
path = url.unescape(path) |
| 87 |
retries = 0 |
| 88 |
repeat |
| 89 |
sent = try(con:sendto(RRQ(path, "octet"), gett.host, gett.port)) |
| 90 |
dgram, datahost, dataport = con:receivefrom() |
| 91 |
retries = retries + 1 |
| 92 |
until dgram or datahost ~= "timeout" or retries > 5 |
| 93 |
try(dgram, datahost) |
| 94 |
|
| 95 |
try(con:setpeername(datahost, dataport)) |
| 96 |
|
| 97 |
local sink = gett.sink or ltn12.sink.null() |
| 98 |
|
| 99 |
while 1 do |
| 100 |
|
| 101 |
code = get_OP(dgram) |
| 102 |
try(code ~= OP_ERROR, get_ERROR(dgram)) |
| 103 |
try(code == OP_DATA, "unhandled opcode " .. code) |
| 104 |
|
| 105 |
local block, data = split_DATA(dgram) |
| 106 |
|
| 107 |
if block == last+1 then |
| 108 |
try(sink(data)) |
| 109 |
last = block |
| 110 |
end |
| 111 |
|
| 112 |
if string.len(data) < 512 then |
| 113 |
try(con:send(ACK(block))) |
| 114 |
try(con:close()) |
| 115 |
try(sink(nil)) |
| 116 |
return 1 |
| 117 |
end |
| 118 |
|
| 119 |
retries = 0 |
| 120 |
repeat |
| 121 |
sent = try(con:send(ACK(last))) |
| 122 |
dgram, err = con:receive() |
| 123 |
retries = retries + 1 |
| 124 |
until dgram or err ~= "timeout" or retries > 5 |
| 125 |
try(dgram, err) |
| 126 |
end |
| 127 |
end |
| 128 |
|
| 129 |
local default = { |
| 130 |
port = PORT, |
| 131 |
path ="/", |
| 132 |
scheme = "tftp" |
| 133 |
} |
| 134 |
|
| 135 |
local function parse(u) |
| 136 |
local t = socket.try(url.parse(u, default)) |
| 137 |
socket.try(t.scheme == "tftp", "invalid scheme '" .. t.scheme .. "'") |
| 138 |
socket.try(t.host, "invalid host") |
| 139 |
return t |
| 140 |
end |
| 141 |
|
| 142 |
local function sget(u) |
| 143 |
local gett = parse(u) |
| 144 |
local t = {} |
| 145 |
gett.sink = ltn12.sink.table(t) |
| 146 |
tget(gett) |
| 147 |
return table.concat(t) |
| 148 |
end |
| 149 |
|
| 150 |
get = socket.protect(function(gett) |
| 151 |
if base.type(gett) == "string" then return sget(gett) |
| 152 |
else return tget(gett) end |
| 153 |
end) |
| 154 |
|