-
Notifications
You must be signed in to change notification settings - Fork 0
/
payload_ipv4_icmp.go
51 lines (42 loc) · 939 Bytes
/
payload_ipv4_icmp.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
package exu
type ICMPType uint8
const (
ICMPTypeEchoReply ICMPType = 0
ICMPTypeEcho ICMPType = 8
)
type ICMPPayload struct {
Type ICMPType
Code uint8
Checksum uint16
Data []byte
}
func (i *ICMPPayload) MarshalBinary() ([]byte, error) {
res := make([]byte, 4+len(i.Data))
res[0] = byte(i.Type)
res[1] = i.Code
res[2] = byte(i.Checksum >> 8)
res[3] = byte(i.Checksum)
copy(res[4:], i.Data)
return res, nil
}
func (i *ICMPPayload) UnmarshalBinary(data []byte) error {
i.Type = ICMPType(data[0])
i.Code = data[1]
i.Checksum = uint16(data[2])<<8 | uint16(data[3])
i.Data = data[4:]
return nil
}
func (i *ICMPPayload) CalculateChecksum() uint16 {
data := i.Data
if len(data)%2 != 0 {
data = append(data, 0)
}
var sum uint32
for i := 0; i < len(data); i += 2 {
sum += uint32(data[i])<<8 | uint32(data[i+1])
}
for sum > 0xffff {
sum = (sum >> 16) + (sum & 0xffff)
}
return uint16(^sum)
}