...
1
2
3
4
5 package icmp
6
7 import "encoding/binary"
8
9
10 type PacketTooBig struct {
11 MTU int
12 Data []byte
13 }
14
15
16 func (p *PacketTooBig) Len(proto int) int {
17 if p == nil {
18 return 0
19 }
20 return 4 + len(p.Data)
21 }
22
23
24 func (p *PacketTooBig) Marshal(proto int) ([]byte, error) {
25 b := make([]byte, 4+len(p.Data))
26 binary.BigEndian.PutUint32(b[:4], uint32(p.MTU))
27 copy(b[4:], p.Data)
28 return b, nil
29 }
30
31
32 func parsePacketTooBig(proto int, _ Type, b []byte) (MessageBody, error) {
33 bodyLen := len(b)
34 if bodyLen < 4 {
35 return nil, errMessageTooShort
36 }
37 p := &PacketTooBig{MTU: int(binary.BigEndian.Uint32(b[:4]))}
38 if bodyLen > 4 {
39 p.Data = make([]byte, bodyLen-4)
40 copy(p.Data, b[4:])
41 }
42 return p, nil
43 }
44
View as plain text