-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket_error.go
42 lines (37 loc) · 969 Bytes
/
packet_error.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
package tftp
import (
"encoding/binary"
"fmt"
"io"
)
// Error is an ERR TFTP Packet
type Error struct {
ErrorCode uint16
ErrorMessage string
}
// Opcode gets the opcode
func (e *Error) Opcode() uint16 {
return OpcodeError
}
// ReadFrom implements the io.ReaderFrom interface
func (e *Error) ReadFrom(reader io.Reader) (n int64, err error) {
ecr := NewErrorCountReader(reader)
binary.Read(ecr, binary.BigEndian, &e.ErrorCode)
scanner := NewCStringScanner(ecr)
if scanner.Scan() {
e.ErrorMessage = scanner.Text()
}
err = scanner.Err()
if err == nil && ecr.Err() != io.EOF {
err = ecr.Err()
}
return ecr.Count(), err
}
// WriteTo implements the io.WriterTo interface
func (e *Error) WriteTo(writer io.Writer) (n int64, err error) {
ecw := NewErrorCountWriter(writer)
binary.Write(ecw, binary.BigEndian, OpcodeError)
binary.Write(ecw, binary.BigEndian, e.ErrorCode)
fmt.Fprintf(ecw, "%s\x00", e.ErrorMessage)
return ecw.Count(), ecw.Err()
}