packettoobig.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. // A PacketTooBig represents an ICMP packet too big message body.
  6. type PacketTooBig struct {
  7. MTU int // maximum transmission unit of the nexthop link
  8. Data []byte // data
  9. }
  10. // Len implements the Len method of MessageBody interface.
  11. func (p *PacketTooBig) Len() int {
  12. if p == nil {
  13. return 0
  14. }
  15. return 4 + len(p.Data)
  16. }
  17. // Marshal implements the Marshal method of MessageBody interface.
  18. func (p *PacketTooBig) Marshal() ([]byte, error) {
  19. b := make([]byte, 4+len(p.Data))
  20. b[0], b[1], b[2], b[3] = byte(p.MTU>>24), byte(p.MTU>>16), byte(p.MTU>>8), byte(p.MTU)
  21. copy(b[4:], p.Data)
  22. return b, nil
  23. }
  24. // parsePacketTooBig parses b as an ICMP packet too big message body.
  25. func parsePacketTooBig(b []byte) (MessageBody, error) {
  26. bodyLen := len(b)
  27. if bodyLen < 4 {
  28. return nil, errMessageTooShort
  29. }
  30. p := &PacketTooBig{MTU: int(b[0])<<24 | int(b[1])<<16 | int(b[2])<<8 | int(b[3])}
  31. if bodyLen > 4 {
  32. p.Data = make([]byte, bodyLen-4)
  33. copy(p.Data, b[4:])
  34. }
  35. return p, nil
  36. }