messagebody.go 1.0 KB

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