This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
forked from betawaffle/tftp-go
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhandler.go
301 lines (253 loc) · 7.18 KB
/
handler.go
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
Copyright (c) 2015 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tftp
import (
"errors"
"fmt"
"io"
"net"
"os"
"strconv"
"time"
)
var (
minBlockSize = 8 // as per RFC 2348
maxBlockSize = 1400 // fit within a standard MTU of 1500, even if encapsulated
)
// ReadCloser is what the Handler needs to implement to serve TFTP read requests.
type ReadCloser interface {
io.ReadCloser
}
// WriteCloser is what the Handler needs to implement to serve TFTP write requests.
type WriteCloser interface {
io.WriteCloser
}
// Conn provides context about the current "connection".
type Conn interface {
LocalAddr() net.Addr
RemoteAddr() net.Addr
}
// Handler is the interface a consumer of this library needs to implement to be
// able to serve TFTP requests.
type Handler interface {
ReadFile(c Conn, filename string) (ReadCloser, error)
WriteFile(c Conn, filename string) (WriteCloser, error)
}
// ErrTimeout is returned by the packetReader when it times out reading a packet.
var ErrTimeout = errors.New("timeout")
// packetReader is the interface that describes the function used for reading
// packets. The read function returns an error when it times out (ErrTimeout)
// or cannot deserialize a packet. In the latter case, the error is propagates
// from the routines responsible for deserialization.
type packetReader interface {
read(time.Duration) (x packet, err error)
}
// packetWriter is the interface that describes the function used for writing packets.
type packetWriter interface {
write(x packet) error
}
// packetValidator is type of the function that gets called from the function
// that writes a packet and waits for an acknowledgement from its peer.
type packetValidator func(p packet) bool
// session records the state for an exchange of UDP packets concerning a single
// TFTP request.
type session struct {
packetReader
packetWriter
h Handler
c Conn
blksize int // The payload size per data packet.
timeout int // The number of seconds before a retransmit takes place.
totsize int // The total size of the file.
}
func serve(c Conn, r packetReader, w packetWriter, h Handler) {
s := &session{
packetReader: r,
packetWriter: w,
h: h,
c: c,
blksize: 512,
timeout: 3,
}
s.serve()
}
func (s *session) writeError(err tftpError, message string) {
s.write(&packetERROR{errorCode: err.Code, errorMessage: message}) //nolint:errcheck // no one cares about errors when sending errors
}
// writeAndWaitForPacket sends the packet p to our peer and waits for it to
// reply with a packet that can be validated by the packet validator v.
//
// If no valid reply if received before the configured timeout expires, packet
// p will be sent again. The packet will be sent for a maximum of 3 times.
//
// When a non-timeout error occurs when reading a reply, this function sends an
// error packet with the error message back to the peer.
func (s *session) writeAndWaitForPacket(p packet, v packetValidator) error {
var err error
for i := 0; i < 3; i++ {
err = s.write(p)
if err != nil {
return fmt.Errorf("write failed: %w", err)
}
now := time.Now()
end := now.Add(time.Duration(s.timeout) * time.Second)
for ; now.Before(end); now = time.Now() {
timeout := end.Sub(now)
p, err := s.read(timeout)
if err != nil {
if errors.Is(err, ErrTimeout) {
break
}
s.writeError(tftpErrNotDefined, err.Error())
return fmt.Errorf("read failed: %w", err)
}
// Check validity of packet
if v(p) {
return nil
}
}
}
return ErrTimeout
}
func (s *session) serve() {
p, err := s.read(0)
if err != nil {
s.writeError(tftpErrNotDefined, err.Error())
return
}
switch px := p.(type) {
case *packetRRQ:
s.serveRRQ(px)
case *packetWRQ:
s.serveWRQ(px)
default:
s.writeError(tftpErrIllegalOperation, "")
}
}
func (s *session) negotiate(o map[string]string) (map[string]string, error) {
oack := make(map[string]string)
blksize, ok := o["blksize"]
if ok {
i, err := strconv.Atoi(blksize)
if err != nil {
return nil, err
}
switch {
case i > maxBlockSize:
s.blksize = maxBlockSize
case i < minBlockSize:
s.blksize = minBlockSize
default:
s.blksize = i
}
oack["blksize"] = strconv.Itoa(s.blksize)
}
timeout, ok := o["timeout"]
if ok {
i, err := strconv.Atoi(timeout)
if err != nil {
return nil, err
}
// Lower and upper bound from RFC 2349.
switch {
case i < 1:
s.timeout = 1
case i > 255:
s.timeout = 255
default:
s.timeout = i
}
oack["timeout"] = strconv.Itoa(s.timeout)
}
// HACK! For TianoCore
if _, ok := o["tsize"]; ok && s.totsize > 0 {
oack["tsize"] = strconv.Itoa(s.totsize)
}
return oack, nil
}
func ackValidator(blockNr uint16) packetValidator {
return func(p packet) bool {
ack, ok := p.(*packetACK)
return ok && ack.blockNr == blockNr
}
}
func (s *session) serveRRQ(p *packetRRQ) {
rc, err := s.h.ReadFile(s.c, p.filename)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
s.writeError(tftpErrNotFound, err.Error())
return
}
if errors.Is(err, os.ErrPermission) {
s.writeError(tftpErrAccessViolation, err.Error())
return
}
s.writeError(tftpErrNotDefined, err.Error())
return
}
defer func() {
// This is called from an anonymous function to make errcheck happy.
_ = rc.Close()
}()
type sizer interface {
Size() int
}
if sz, ok := rc.(sizer); ok {
s.totsize = sz.Size()
}
if len(p.options) > 0 {
options, err := s.negotiate(p.options)
if err != nil {
s.writeError(tftpErrOptionNegotiation, err.Error())
return
}
p := &packetOACK{options: options}
if err = s.writeAndWaitForPacket(p, ackValidator(0)); err != nil {
return
}
}
// Proceed to send the file
buf := make([]byte, s.blksize)
var n int
var rErr error
for blockNr := uint16(1); rErr == nil; blockNr++ {
// The semantics of ReadAtLeast are as follows:
//
// If == "blksize" bytes are read into buf, it will return with err == nil.
// If < "blksize" bytes are read into buf and an error occurs reading new
// bytes, it will return the number of bytes read and this error. If this
// error is io.EOF, it is rewritten to io.ErrUnexpectedEOF if > 0 bytes
// were already read.
n, rErr = io.ReadAtLeast(rc, buf, s.blksize)
if rErr != nil {
if errors.Is(rErr, io.EOF) || errors.Is(rErr, io.ErrUnexpectedEOF) {
// Treat errors as equivalent
rErr = io.EOF
} else {
s.writeError(tftpErrNotDefined, rErr.Error())
return
}
}
p := &packetDATA{
blockNr: blockNr,
data: buf[:n],
}
if err := s.writeAndWaitForPacket(p, ackValidator(blockNr)); err != nil {
// This seems very very suspicious
return
}
}
}
func (s *session) serveWRQ(_ *packetWRQ) {
s.writeError(tftpErrNotDefined, "not supported")
}