-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp.go
56 lines (47 loc) · 1.19 KB
/
udp.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
package packemon
import (
"bytes"
"encoding/binary"
)
type UDP struct {
SrcPort uint16
DstPort uint16
Length uint16
Checksum uint16 // TODO: 後で計算用メソッドを。そもそも他のヘッダのchecksumと同じ計算っぽいから、独立させるかも
Data []byte
}
func ParsedUDP(payload []byte) *UDP {
return &UDP{
SrcPort: binary.BigEndian.Uint16(payload[0:2]),
DstPort: binary.BigEndian.Uint16(payload[2:4]),
Length: binary.BigEndian.Uint16(payload[4:6]),
Checksum: binary.BigEndian.Uint16(payload[6:8]),
Data: payload[8:],
}
}
func (u *UDP) Len() {
length := 0
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, u.SrcPort)
length += len(b)
b = make([]byte, 2)
binary.BigEndian.PutUint16(b, u.DstPort)
length += len(b)
b = make([]byte, 2)
binary.BigEndian.PutUint16(b, u.Length)
length += len(b)
b = make([]byte, 2)
binary.BigEndian.PutUint16(b, u.Checksum)
length += len(b)
length += len(u.Data)
u.Length = uint16(length)
}
func (u *UDP) Bytes() []byte {
buf := &bytes.Buffer{}
WriteUint16(buf, u.SrcPort)
WriteUint16(buf, u.DstPort)
WriteUint16(buf, u.Length)
WriteUint16(buf, u.Checksum)
buf.Write(u.Data)
return buf.Bytes()
}