-
Notifications
You must be signed in to change notification settings - Fork 8
/
datagram.go
357 lines (302 loc) · 8.9 KB
/
datagram.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright (C) 2017 Kale Blankenship. All rights reserved.
// This software may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details
package tftp // import "pack.ag/tftp"
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"strings"
)
type opcode uint16
func (o opcode) String() string {
name, ok := opcodeStrings[o]
if ok {
return name
}
return fmt.Sprintf("UNKNOWN_OPCODE_%v", uint16(o))
}
// ErrorCode is a TFTP error code as defined in RFC 1350
type ErrorCode uint16
func (e ErrorCode) String() string {
name, ok := errorStrings[e]
if ok {
return name
}
return fmt.Sprintf("UNKNOWN_ERROR_%v", uint16(e))
}
const (
opCodeRRQ opcode = 0x1 // Read Request
opCodeWRQ opcode = 0x2 // Write Request
opCodeDATA opcode = 0x3 // Data
opCodeACK opcode = 0x4 // Acknowledgement
opCodeERROR opcode = 0x5 // Error
opCodeOACK opcode = 0x6 // Option Acknowledgement
// ErrCodeNotDefined - Not defined, see error message (if any).
ErrCodeNotDefined ErrorCode = 0x0
// ErrCodeFileNotFound - File not found.
ErrCodeFileNotFound ErrorCode = 0x1
// ErrCodeAccessViolation - Access violation.
ErrCodeAccessViolation ErrorCode = 0x2
// ErrCodeDiskFull - Disk full or allocation exceeded.
ErrCodeDiskFull ErrorCode = 0x3
// ErrCodeIllegalOperation - Illegal TFTP operation.
ErrCodeIllegalOperation ErrorCode = 0x4
// ErrCodeUnknownTransferID - Unknown transfer ID.
ErrCodeUnknownTransferID ErrorCode = 0x5
// ErrCodeFileAlreadyExists - File already exists.
ErrCodeFileAlreadyExists ErrorCode = 0x6
// ErrCodeNoSuchUser - No such user.
ErrCodeNoSuchUser ErrorCode = 0x7
// ModeNetASCII is the string for netascii transfer mode
ModeNetASCII TransferMode = "netascii"
// ModeOctet is the string for octet/binary transfer mode
ModeOctet TransferMode = "octet"
modeMail TransferMode = "mail"
optBlocksize = "blksize"
optTimeout = "timeout"
optTransferSize = "tsize"
optWindowSize = "windowsize"
)
// TransferMode is a TFTP transer mode
type TransferMode string
var (
errorStrings = map[ErrorCode]string{
ErrCodeNotDefined: "NOT_DEFINED",
ErrCodeFileNotFound: "FILE_NOT_FOUND",
ErrCodeAccessViolation: "ACCESS_VIOLATION",
ErrCodeDiskFull: "DISK_FULL",
ErrCodeIllegalOperation: "ILLEGAL_OPERATION",
ErrCodeUnknownTransferID: "UNKNOWN_TRANSFER_ID",
ErrCodeFileAlreadyExists: "FILE_ALREADY_EXISTS",
ErrCodeNoSuchUser: "NO_SUCH_USER",
}
opcodeStrings = map[opcode]string{
opCodeRRQ: "READ_REQUEST",
opCodeWRQ: "WRITE_REQUEST",
opCodeDATA: "DATA",
opCodeACK: "ACK",
opCodeERROR: "ERROR",
opCodeOACK: "OPTION_ACK",
}
)
type datagram struct {
buf []byte
offset int
}
func (d datagram) String() string {
if err := d.validate(); err != nil {
return fmt.Sprintf("INVALID_DATAGRAM[Error: %q]", err.Error())
}
switch o := d.opcode(); o {
case opCodeRRQ, opCodeWRQ:
return fmt.Sprintf("%s[Filename: %q; Mode: %q; Options: %s]", o, d.filename(), d.mode(), d.options())
case opCodeDATA:
return fmt.Sprintf("%s[Block: %d; Data Length: %d]", o, d.block(), len(d.data()))
case opCodeOACK:
return fmt.Sprintf("%s[Options: %s]", o, d.options())
case opCodeACK:
return fmt.Sprintf("%s[Block: %d]", o, d.block())
case opCodeERROR:
return fmt.Sprintf("%s[Code: %s; Message: %q]", o, d.errorCode(), d.errMsg())
default:
return o.String()
}
}
// Sets the buffer from raw bytes
func (d *datagram) setBytes(b []byte) {
d.buf = b
d.offset = len(b)
}
// Returns the allocated bytes
func (d *datagram) bytes() []byte {
return d.buf[:d.offset]
}
// Resets the byte buffer.
// If requested size is larger than allocated the buffer is reallocated.
func (d *datagram) reset(size int) {
if len(d.buf) < size {
d.buf = make([]byte, size)
}
d.offset = 0
}
// DATAGRAM CONSTRUCTORS
func (d *datagram) writeAck(block uint16) {
d.reset(2 + 2)
d.writeUint16(uint16(opCodeACK))
d.writeUint16(block)
}
func (d *datagram) writeData(block uint16, data []byte) {
d.reset(2 + 2 + len(data))
d.writeUint16(uint16(opCodeDATA))
d.writeUint16(block)
d.writeBytes(data)
}
func (d *datagram) writeError(code ErrorCode, msg string) {
d.reset(2 + 2 + len(msg) + 1)
d.writeUint16(uint16(opCodeERROR))
d.writeUint16(uint16(code))
d.writeString(msg)
d.writeNull()
}
func (d *datagram) writeReadReq(filename string, mode TransferMode, options map[string]string) {
d.writeReq(opCodeRRQ, filename, mode, options)
}
func (d *datagram) writeWriteReq(filename string, mode TransferMode, options map[string]string) {
d.writeReq(opCodeWRQ, filename, mode, options)
}
func (d *datagram) writeOptionAck(options map[string]string) {
optLen := 0
for opt, val := range options {
optLen += len(opt) + 1 + len(val) + 1
}
d.reset(2 + optLen)
d.writeUint16(uint16(opCodeOACK))
for opt, val := range options {
d.writeOption(opt, val)
}
}
// Combines duplicate logic from RRQ and WRQ
func (d *datagram) writeReq(o opcode, filename string, mode TransferMode, options map[string]string) {
// This is ugly, could just set buf to 512
// or use a bytes buffer. Intend to switch to bytes buffer
// after implementing all RFCs so that perf can be compared
// with a reasonable block and window size
optLen := 0
for opt, val := range options {
optLen += len(opt) + 1 + len(val) + 1
}
d.reset(2 + len(filename) + 1 + len(mode) + 1 + optLen)
d.writeUint16(uint16(o))
d.writeString(filename)
d.writeNull()
d.writeString(string(mode))
d.writeNull()
for opt, val := range options {
d.writeOption(opt, val)
}
}
// FIELD ACCESSORS
// Block # from DATA and ACK datagrams
func (d *datagram) block() uint16 {
return binary.BigEndian.Uint16(d.buf[2:4])
}
// Data from DATA datagram
func (d *datagram) data() []byte {
return d.buf[4:d.offset]
}
// ErrorCode from ERROR datagram
func (d *datagram) errorCode() ErrorCode {
return ErrorCode(binary.BigEndian.Uint16(d.buf[2:4]))
}
// ErrMsg from ERROR datagram
func (d *datagram) errMsg() string {
end := d.offset - 1
return string(d.buf[4:end])
}
// Filename from RRQ and WRQ datagrams
func (d *datagram) filename() string {
offset := bytes.IndexByte(d.buf[2:], 0x0) + 2
return string(d.buf[2:offset])
}
// Mode from RRQ and WRQ datagrams
func (d *datagram) mode() TransferMode {
fields := bytes.Split(d.buf[2:], []byte{0x0})
return TransferMode(fields[1])
}
// Opcode from all datagrams
func (d *datagram) opcode() opcode {
return opcode(binary.BigEndian.Uint16(d.buf[:2]))
}
type options map[string]string
func (o options) String() string {
opts := make([]string, 0, len(o))
for k, v := range o {
opts = append(opts, fmt.Sprintf("%q: %q", k, v))
}
return "{" + strings.Join(opts, "; ") + "}"
}
func (d *datagram) options() options {
options := make(options)
optSlice := bytes.Split(d.buf[2:d.offset-1], []byte{0x0}) // d.buf[2:d.offset-1] = file -> just before final NULL
if op := d.opcode(); op == opCodeRRQ || op == opCodeWRQ {
optSlice = optSlice[2:] // Remove filename, mode
}
for i := 0; i < len(optSlice); i += 2 {
options[string(optSlice[i])] = string(optSlice[i+1])
}
return options
}
// BUFFER WRITING FUNCTIONS
func (d *datagram) writeBytes(b []byte) {
copy(d.buf[d.offset:], b)
d.offset += len(b)
}
func (d *datagram) writeNull() {
d.buf[d.offset] = 0x0
d.offset++
}
func (d *datagram) writeString(str string) {
d.writeBytes([]byte(str))
}
func (d *datagram) writeUint16(i uint16) {
binary.BigEndian.PutUint16(d.buf[d.offset:], i)
d.offset += 2
}
func (d *datagram) writeOption(o string, v string) {
d.writeString(o)
d.writeNull()
d.writeString(v)
d.writeNull()
}
// VALIDATION
func (d *datagram) validate() error {
switch {
case d.offset < 2:
return errors.New("Datagram has no opcode")
case d.opcode() > 6:
return errors.New("Invalid opcode")
}
switch d.opcode() {
case opCodeRRQ, opCodeWRQ:
switch {
case len(d.filename()) < 1:
return errors.New("No filename provided")
case d.buf[d.offset-1] != 0x0: // End with NULL
return fmt.Errorf("Corrupt %v datagram", d.opcode())
case bytes.Count(d.buf[2:d.offset], []byte{0x0})%2 != 0: // Number of NULL chars is not even
return fmt.Errorf("Corrupt %v datagram", d.opcode())
default:
switch d.mode() {
case ModeNetASCII, ModeOctet:
break
case modeMail:
return errors.New("MAIL transfer mode is unsupported")
default:
return errors.New("Invalid transfer mode")
}
}
case opCodeACK, opCodeDATA:
if d.offset < 4 {
return errors.New("Corrupt block number")
}
case opCodeERROR:
switch {
case d.offset < 5:
return errors.New("Corrupt ERROR datagram")
case d.buf[d.offset-1] != 0x0:
return errors.New("Corrupt ERROR datagram")
case bytes.Count(d.buf[4:d.offset], []byte{0x0}) > 1:
return errors.New("Corrupt ERROR datagram")
}
case opCodeOACK:
switch {
case d.buf[d.offset-1] != 0x0:
return errors.New("Corrupt OACK datagram")
case bytes.Count(d.buf[2:d.offset], []byte{0x0})%2 != 0: // Number of NULL chars is not even
return errors.New("Corrupt OACK datagram")
}
}
return nil
}