-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket_request.go
80 lines (69 loc) · 1.8 KB
/
packet_request.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
package tftp
import (
"encoding/binary"
"fmt"
"io"
)
// Request is a TFTP RRQ Packet
type Request struct {
Filename string
Mode string
Options Options
}
// ReadFrom implements the io.ReaderFrom interface
func (rq *Request) ReadFrom(reader io.Reader) (n int64, err error) {
// RFC 1350 The TFTP Protocol (Revision 2)
ecr := NewErrorCountReader(reader)
scanner := NewCStringScanner(ecr)
if scanner.Scan() {
rq.Filename = scanner.Text()
}
if scanner.Scan() {
rq.Mode = scanner.Text()
}
err = rq.Options.ScanFrom(scanner)
if err == nil {
err = scanner.Err()
}
if err == nil && ecr.Err() != io.EOF {
err = ecr.Err()
}
if rq.Filename == "" || rq.Mode == "" {
err = ErrInvalidPacket
}
return ecr.Count(), err
}
// WriteToWithOpcode implements the io.WriterFrom interface with an additional opcode parameter
func (rq *Request) opcodeWriteTo(writer io.Writer, opcode uint16) (n int64, err error) {
ecw := NewErrorCountWriter(writer)
binary.Write(ecw, binary.BigEndian, opcode)
fmt.Fprintf(ecw, "%s\x00%s\x00", rq.Filename, rq.Mode)
rq.Options.WriteTo(ecw)
return ecw.Count(), ecw.Err()
}
// ReadRequest is a TFTP RRQ packet
type ReadRequest struct {
Request
}
// Opcode gets the opcode
func (rq *ReadRequest) Opcode() uint16 {
return OpcodeReadRequest
}
// WriteTo implements the io.WriterTo interface
func (rq *ReadRequest) WriteTo(writer io.Writer) (n int64, err error) {
n, err = rq.opcodeWriteTo(writer, OpcodeReadRequest)
return
}
// WriteRequest is a TFTP WRQ packet
type WriteRequest struct {
Request
}
// Opcode gets the opcode
func (rq *WriteRequest) Opcode() uint16 {
return OpcodeWriteRequest
}
// WriteTo implements the io.WriterTo interface
func (rq *WriteRequest) WriteTo(writer io.Writer) (n int64, err error) {
n, err = rq.opcodeWriteTo(writer, OpcodeWriteRequest)
return
}