dstunreach.go 1.5 KB

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