messagebody.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 DefaultMessageBody represents the default message body.
  17. type DefaultMessageBody struct {
  18. Data []byte // data
  19. }
  20. // Len implements the Len method of MessageBody interface.
  21. func (p *DefaultMessageBody) Len(proto int) int {
  22. if p == nil {
  23. return 0
  24. }
  25. return len(p.Data)
  26. }
  27. // Marshal implements the Marshal method of MessageBody interface.
  28. func (p *DefaultMessageBody) Marshal(proto int) ([]byte, error) {
  29. return p.Data, nil
  30. }
  31. // parseDefaultMessageBody parses b as an ICMP message body.
  32. func parseDefaultMessageBody(proto int, b []byte) (MessageBody, error) {
  33. p := &DefaultMessageBody{Data: make([]byte, len(b))}
  34. copy(p.Data, b)
  35. return p, nil
  36. }