messagebody.go 1.2 KB

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