github.com/contiv/libOpenflow@v0.0.0-20210609050114-d967b14cc688/protocol/icmp.go (about) 1 package protocol 2 3 import ( 4 "encoding/binary" 5 "errors" 6 ) 7 8 type ICMP struct { 9 Type uint8 10 Code uint8 11 Checksum uint16 12 Data []byte 13 } 14 15 func NewICMP() *ICMP { 16 i := new(ICMP) 17 i.Data = make([]byte, 0) 18 return i 19 } 20 21 func (i *ICMP) Len() (n uint16) { 22 return uint16(4 + len(i.Data)) 23 } 24 25 func (i *ICMP) MarshalBinary() (data []byte, err error) { 26 data = make([]byte, int(i.Len())) 27 data[0] = i.Type 28 data[1] = i.Code 29 binary.BigEndian.PutUint16(data[2:4], i.Checksum) 30 copy(data[4:], i.Data) 31 return 32 } 33 34 func (i *ICMP) UnmarshalBinary(data []byte) error { 35 if len(data) < 4 { 36 return errors.New("The []byte is too short to unmarshal a full ICMP message.") 37 } 38 i.Type = data[0] 39 i.Code = data[1] 40 i.Checksum = binary.BigEndian.Uint16(data[2:4]) 41 42 i.Data = make([]byte, len(data)-4) 43 copy(i.Data, data[4:]) 44 return nil 45 }