messagebody.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright 2012 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 MessageBody represents an ICMP message body.
  6. type MessageBody interface {
  7. // Len returns the length of ICMP message body.
  8. // The provided proto must be either the ICMPv4 or ICMPv6
  9. // protocol number.
  10. Len(proto int) int
  11. // Marshal returns the binary encoding of ICMP message body.
  12. // The provided proto must be either the ICMPv4 or ICMPv6
  13. // protocol number.
  14. Marshal(proto int) ([]byte, error)
  15. }
  16. // A RawBody represents a raw message body.
  17. //
  18. // A raw message body is excluded from message processing and can be
  19. // used to construct applications such as protocol conformance
  20. // testing.
  21. type RawBody struct {
  22. Data []byte // data
  23. }
  24. // Len implements the Len method of MessageBody interface.
  25. func (p *RawBody) Len(proto int) int {
  26. if p == nil {
  27. return 0
  28. }
  29. return len(p.Data)
  30. }
  31. // Marshal implements the Marshal method of MessageBody interface.
  32. func (p *RawBody) Marshal(proto int) ([]byte, error) {
  33. return p.Data, nil
  34. }
  35. // parseRawBody parses b as an ICMP message body.
  36. func parseRawBody(proto int, b []byte) (MessageBody, error) {
  37. p := &RawBody{Data: make([]byte, len(b))}
  38. copy(p.Data, b)
  39. return p, nil
  40. }
  41. // A DefaultMessageBody represents the default message body.
  42. //
  43. // Deprecated: Use RawBody instead.
  44. type DefaultMessageBody = RawBody