messagebody.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. Len() int
  9. // Marshal returns the binary enconding of ICMP message body.
  10. // Proto must be either the ICMPv4 or ICMPv6 protocol number.
  11. Marshal(proto int) ([]byte, error)
  12. }
  13. // A DefaultMessageBody represents the default message body.
  14. type DefaultMessageBody struct {
  15. Data []byte // data
  16. }
  17. // Len implements the Len method of MessageBody interface.
  18. func (p *DefaultMessageBody) Len() int {
  19. if p == nil {
  20. return 0
  21. }
  22. return len(p.Data)
  23. }
  24. // Marshal implements the Marshal method of MessageBody interface.
  25. func (p *DefaultMessageBody) Marshal(proto int) ([]byte, error) {
  26. return p.Data, nil
  27. }
  28. // parseDefaultMessageBody parses b as an ICMP message body.
  29. func parseDefaultMessageBody(proto int, b []byte) (MessageBody, error) {
  30. p := &DefaultMessageBody{Data: make([]byte, len(b))}
  31. copy(p.Data, b)
  32. return p, nil
  33. }