This repository has been archived by the owner on Nov 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
handler.go
287 lines (241 loc) · 6.72 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
/*
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 gotftp
import (
"errors"
"io"
"net"
"os"
"strconv"
"time"
)
// 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.
}
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) error {
p := packetERROR{
errorCode: err.Code,
errorMessage: message,
}
return s.write(&p)
}
// 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) (packet, error) {
var err error
for i := 0; i < 3; i++ {
err = s.write(p)
if err != nil {
return nil, 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 == ErrTimeout {
break
}
if err != nil {
_ = s.writeError(tftpErrNotDefined, err.Error())
return nil, err
}
// Check validity of packet
if v(p) {
return p, nil
}
}
}
return nil, 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
}
// Lower and upper bound from RFC 2348.
if i < 8 {
s.blksize = 8
} else if i > 65464 {
s.blksize = 65464
} else {
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.
if i < 1 {
s.timeout = 1
} else if i > 255 {
s.timeout = 255
} else {
s.timeout = i
}
oack["timeout"] = strconv.Itoa(s.timeout)
}
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 {
switch err {
case os.ErrNotExist:
_ = s.writeError(tftpErrNotFound, err.Error())
case os.ErrPermission:
_ = s.writeError(tftpErrAccessViolation, err.Error())
default:
_ = s.writeError(tftpErrNotDefined, err.Error())
}
return
}
defer func() {
// This is called from an anonymous function to make errcheck happy.
_ = rc.Close()
}()
if len(p.options) > 0 {
options, err := s.negotiate(p.options)
if err != nil {
_ = s.writeError(tftpErrOptionNegotiation, err.Error())
return
}
p := &packetOACK{options: options}
_, err = s.writeAndWaitForPacket(p, ackValidator(0))
if err != nil {
return
}
}
// Proceed to send the file
var buf = make([]byte, s.blksize)
var n int
var readErr, writeErr error
for blockNr := uint16(1); readErr == 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, readErr = io.ReadAtLeast(rc, buf, s.blksize)
switch readErr {
case nil:
// All is good.
case io.EOF, io.ErrUnexpectedEOF:
// Treat them as one and the same.
readErr = io.EOF
default:
_ = s.writeError(tftpErrNotDefined, readErr.Error())
return
}
p := &packetDATA{
blockNr: blockNr,
data: buf[:n],
}
_, writeErr = s.writeAndWaitForPacket(p, ackValidator(blockNr))
if writeErr != nil {
return
}
}
}
func (s *session) serveWRQ(p *packetWRQ) {
_ = s.writeError(tftpErrNotDefined, "not supported")
}