packettoobig.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package icmp
  5. import "encoding/binary"
  6. // A PacketTooBig represents an ICMP packet too big message body.
  7. type PacketTooBig struct {
  8. MTU int // maximum transmission unit of the nexthop link
  9. Data []byte // data, known as original datagram field
  10. }
  11. // Len implements the Len method of MessageBody interface.
  12. func (p *PacketTooBig) Len(proto int) int {
  13. if p == nil {
  14. return 0
  15. }
  16. return 4 + len(p.Data)
  17. }
  18. // Marshal implements the Marshal method of MessageBody interface.
  19. func (p *PacketTooBig) Marshal(proto int) ([]byte, error) {
  20. b := make([]byte, 4+len(p.Data))
  21. binary.BigEndian.PutUint32(b[:4], uint32(p.MTU))
  22. copy(b[4:], p.Data)
  23. return b, nil
  24. }
  25. // parsePacketTooBig parses b as an ICMP packet too big message body.
  26. func parsePacketTooBig(proto int, _ Type, b []byte) (MessageBody, error) {
  27. bodyLen := len(b)
  28. if bodyLen < 4 {
  29. return nil, errMessageTooShort
  30. }
  31. p := &PacketTooBig{MTU: int(binary.BigEndian.Uint32(b[:4]))}
  32. if bodyLen > 4 {
  33. p.Data = make([]byte, bodyLen-4)
  34. copy(p.Data, b[4:])
  35. }
  36. return p, nil
  37. }