aboutsummaryrefslogtreecommitdiff
path: root/etc/tftp.lua
diff options
context:
space:
mode:
Diffstat (limited to 'etc/tftp.lua')
-rw-r--r--etc/tftp.lua154
1 files changed, 0 insertions, 154 deletions
diff --git a/etc/tftp.lua b/etc/tftp.lua
deleted file mode 100644
index ed99cd1..0000000
--- a/etc/tftp.lua
+++ /dev/null
@@ -1,154 +0,0 @@
1-----------------------------------------------------------------------------
2-- TFTP support for the Lua language
3-- LuaSocket toolkit.
4-- Author: Diego Nehab
5-----------------------------------------------------------------------------
6
7-----------------------------------------------------------------------------
8-- Load required files
9-----------------------------------------------------------------------------
10local base = _G
11local table = require("table")
12local math = require("math")
13local string = require("string")
14local socket = require("socket")
15local ltn12 = require("ltn12")
16local url = require("socket.url")
17module("socket.tftp")
18
19-----------------------------------------------------------------------------
20-- Program constants
21-----------------------------------------------------------------------------
22local char = string.char
23local byte = string.byte
24
25PORT = 69
26local OP_RRQ = 1
27local OP_WRQ = 2
28local OP_DATA = 3
29local OP_ACK = 4
30local OP_ERROR = 5
31local OP_INV = {"RRQ", "WRQ", "DATA", "ACK", "ERROR"}
32
33-----------------------------------------------------------------------------
34-- Packet creation functions
35-----------------------------------------------------------------------------
36local function RRQ(source, mode)
37 return char(0, OP_RRQ) .. source .. char(0) .. mode .. char(0)
38end
39
40local function WRQ(source, mode)
41 return char(0, OP_RRQ) .. source .. char(0) .. mode .. char(0)
42end
43
44local 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)
49end
50
51local function get_OP(dgram)
52 local op = byte(dgram, 1)*256 + byte(dgram, 2)
53 return op
54end
55
56-----------------------------------------------------------------------------
57-- Packet analysis functions
58-----------------------------------------------------------------------------
59local 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
63end
64
65local 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)
70end
71
72-----------------------------------------------------------------------------
73-- The real work
74-----------------------------------------------------------------------------
75local 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 -- convert from name to ip if needed
82 gett.host = try(socket.dns.toip(gett.host))
83 con:settimeout(1)
84 -- first packet gives data host/port to be used for data transfers
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 -- associate socket with data host/port
95 try(con:setpeername(datahost, dataport))
96 -- default sink
97 local sink = gett.sink or ltn12.sink.null()
98 -- process all data packets
99 while 1 do
100 -- decode packet
101 code = get_OP(dgram)
102 try(code ~= OP_ERROR, get_ERROR(dgram))
103 try(code == OP_DATA, "unhandled opcode " .. code)
104 -- get data packet parts
105 local block, data = split_DATA(dgram)
106 -- if not repeated, write
107 if block == last+1 then
108 try(sink(data))
109 last = block
110 end
111 -- last packet brings less than 512 bytes of data
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 -- get the next packet
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
127end
128
129local default = {
130 port = PORT,
131 path ="/",
132 scheme = "tftp"
133}
134
135local 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
140end
141
142local 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)
148end
149
150get = socket.protect(function(gett)
151 if base.type(gett) == "string" then return sget(gett)
152 else return tget(gett) end
153end)
154