timeexceeded.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 (
  6. "golang.org/x/net/internal/iana"
  7. "golang.org/x/net/ipv4"
  8. "golang.org/x/net/ipv6"
  9. )
  10. // A TimeExceeded represents an ICMP time exceeded message body.
  11. type TimeExceeded struct {
  12. Data []byte // data, known as original datagram field
  13. Extensions []Extension // extensions
  14. }
  15. // Len implements the Len method of MessageBody interface.
  16. func (p *TimeExceeded) Len(proto int) int {
  17. if p == nil {
  18. return 0
  19. }
  20. l, _ := multipartMessageBodyDataLen(proto, true, p.Data, p.Extensions)
  21. return l
  22. }
  23. // Marshal implements the Marshal method of MessageBody interface.
  24. func (p *TimeExceeded) Marshal(proto int) ([]byte, error) {
  25. var typ Type
  26. switch proto {
  27. case iana.ProtocolICMP:
  28. typ = ipv4.ICMPTypeTimeExceeded
  29. case iana.ProtocolIPv6ICMP:
  30. typ = ipv6.ICMPTypeTimeExceeded
  31. default:
  32. return nil, errInvalidProtocol
  33. }
  34. if !validExtensions(typ, p.Extensions) {
  35. return nil, errInvalidExtension
  36. }
  37. return marshalMultipartMessageBody(proto, true, p.Data, p.Extensions)
  38. }
  39. // parseTimeExceeded parses b as an ICMP time exceeded message body.
  40. func parseTimeExceeded(proto int, typ Type, b []byte) (MessageBody, error) {
  41. if len(b) < 4 {
  42. return nil, errMessageTooShort
  43. }
  44. p := &TimeExceeded{}
  45. var err error
  46. p.Data, p.Extensions, err = parseMultipartMessageBody(proto, typ, b)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return p, nil
  51. }