paramprob.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2014 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 ParamProb represents an ICMP parameter problem message body.
  6. type ParamProb struct {
  7. Pointer uintptr // offset within the data where the error was detected
  8. Data []byte // data
  9. }
  10. // Len implements the Len method of MessageBody interface.
  11. func (p *ParamProb) Len() int {
  12. if p == nil {
  13. return 0
  14. }
  15. return 4 + len(p.Data)
  16. }
  17. // Marshal implements the Marshal method of MessageBody interface.
  18. func (p *ParamProb) Marshal() ([]byte, error) {
  19. b := make([]byte, 4+len(p.Data))
  20. b[0], b[1], b[2], b[3] = byte(p.Pointer>>24), byte(p.Pointer>>16), byte(p.Pointer>>8), byte(p.Pointer)
  21. copy(b[4:], p.Data)
  22. return b, nil
  23. }
  24. // parseParamProb parses b as an ICMP parameter problem message body.
  25. func parseParamProb(b []byte) (MessageBody, error) {
  26. bodyLen := len(b)
  27. if bodyLen < 4 {
  28. return nil, errMessageTooShort
  29. }
  30. p := &ParamProb{Pointer: uintptr(b[0])<<24 | uintptr(b[1])<<16 | uintptr(b[2])<<8 | uintptr(b[3])}
  31. if bodyLen > 4 {
  32. p.Data = make([]byte, bodyLen-4)
  33. copy(p.Data, b[4:])
  34. }
  35. return p, nil
  36. }