forked from devyte/nodemcu-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tftpd.lua
171 lines (160 loc) · 4.41 KB
/
tftpd.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
return function(port)
if port == nil then
port = 69 --default
end
local _tblk=0 --block counter
local _lock=nil --lock for operations
local _fn={} --filename store between packets
local _conn = nil -- connection store
local _tmrid = 6
local _retry = 5
local _tmrtimeout = 1000
local function reset()
_tblk=0
_lock = nil
_fn = {}
tmr.stop(_tmrid)
_conn = nil
_retry = 5
collectgarbage() --gc when finished
end
local function sendack(c,n)
local msb = n/256 --assumes int firmware
local lsb = n - msb*256
c:send("\0\4"..string.char(msb, lsb))
end
local function sendblk(c)
local msb = _tblk/256 --assumes int firmware
local lsb = _tblk - msb*256
local b=string.char(msb, lsb)
if(file.open(_fn,"r")==nil) then
c:send("\0\5\0\1\0") --Error: 1=file not found
reset()
return
end
local r = ""
if(file.seek("set", (_tblk-1)*512)~=nil) then
r = file.read(512)
end
file.close()
if(r == nil) then
r = ""
end
c:send("\0\3"..b..r)
if(r:len() ~= 512) then
_lock=4 -- done, wait for ack
end
end
local function timeoutCB()
tmr.stop(_tmrid)
_retry=_retry-1
if(_retry~=0) then
uart.write(0,"*")
if(_lock==2) then
sendack(_conn, _tblk-1) --retransmit last ACK
else -- _lock is 1 or 4
sendblk(_conn) --retransmit data
end
tmr.alarm(_tmrid, _tmrtimeout, 0, timeoutCB)
return
end
print("Connection timed out")
if(_lock == 2) then
file.remove(_fn) --remove incomplete file
end
reset()
end
local function alarmstop()
tmr.stop(_tmrid)
end
local function alarmstart()
tmr.alarm(_tmrid, _tmrtimeout, 0, timeoutCB)
end
local s=net.createServer(net.UDP)
s:on("receive", function(c,r)
local op=r:byte(2)
if(op==1 or op==2) then
if(_lock) then
return
end
_conn=c
_fn=string.match(r,"..(%Z+)")
_lock=op
_tblk=1
elseif(op==3 or op==4) then
local b=r:byte(3)*256+r:byte(4)
if(b~=_tblk) then
return
end
alarmstop()
_retry=5
end
if(op==1) then
--RRQ
uart.write(0,"TFTP RRQ '".._fn.."': ")
if(not file.exists(_fn)) then
c:send("\0\5\0\1\0") --Error: 1=file not found
reset()
return
end
sendblk(c)
elseif(op==2) then
--WRQ
uart.write(0,"TFTP WRQ '".._fn.."': ")
-- overwrite file...
if(file.open(_fn,"w")==nil) then
c:send("\0\5\0\2\0") --Error: 2=access violation
reset()
return
end
file.close()
sendack(c,0)
elseif(op==3) then
--DATA received for a WRQ
if(_lock~=2) then
return
end
local sz=r:len()-4
if(file.open(_fn,"a")==nil) then
c:send("\0\5\0\1\0") --Error: 1=file not found
reset()
return
end
if(file.write(r:sub(5))==nil) then
c:send("\0\5\0\3\0") --Error: 3=no space left
reset()
return
end
sendack(c,_tblk)
uart.write(0,"#")
_tblk=_tblk+1
if(sz~=512) then
print(" done!")
reset()
end
elseif(op==4) then
--ACK received for a RRQ
if(_lock~=1 and _lock~=4) then
return
end
uart.write(0,"#")
_tblk=_tblk+1
if(_lock==1) then
sendblk(c)
else
print(" done!")
reset()
end
else
--ERROR: 4=illegal op
c:send("\0\5\0\4\0")
return
end
if (_lock) then
alarmstart()
end
end)
s:listen(port)
print("TFTP server running on port "..tostring(port))
return s
end