forked from antrea-io/libOpenflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicmp.go
45 lines (38 loc) · 825 Bytes
/
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
package protocol
import (
"encoding/binary"
"errors"
)
type ICMP struct {
Type uint8
Code uint8
Checksum uint16
Data []byte
}
func NewICMP() *ICMP {
i := new(ICMP)
i.Data = make([]byte, 0)
return i
}
func (i *ICMP) Len() (n uint16) {
return uint16(4 + len(i.Data))
}
func (i *ICMP) MarshalBinary() (data []byte, err error) {
data = make([]byte, int(i.Len()))
data[0] = i.Type
data[1] = i.Code
binary.BigEndian.PutUint16(data[2:4], i.Checksum)
copy(data[4:], i.Data)
return
}
func (i *ICMP) UnmarshalBinary(data []byte) error {
if len(data) < 4 {
return errors.New("The []byte is too short to unmarshal a full ICMP message.")
}
i.Type = data[0]
i.Code = data[1]
i.Checksum = binary.BigEndian.Uint16(data[2:4])
i.Data = make([]byte, len(data)-4)
copy(i.Data, data[4:])
return nil
}