paramprob.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. import "golang.org/x/net/internal/iana"
  6. // A ParamProb represents an ICMP parameter problem message body.
  7. type ParamProb struct {
  8. Pointer uintptr // offset within the data where the error was detected
  9. Data []byte // data
  10. }
  11. // Len implements the Len method of MessageBody interface.
  12. func (p *ParamProb) Len() int {
  13. if p == nil {
  14. return 0
  15. }
  16. return 4 + len(p.Data)
  17. }
  18. // Marshal implements the Marshal method of MessageBody interface.
  19. func (p *ParamProb) Marshal(proto int) ([]byte, error) {
  20. b := make([]byte, 4+len(p.Data))
  21. switch proto {
  22. case iana.ProtocolICMP:
  23. b[0] = byte(p.Pointer)
  24. case iana.ProtocolIPv6ICMP:
  25. b[0], b[1], b[2], b[3] = byte(p.Pointer>>24), byte(p.Pointer>>16), byte(p.Pointer>>8), byte(p.Pointer)
  26. }
  27. copy(b[4:], p.Data)
  28. return b, nil
  29. }
  30. // parseParamProb parses b as an ICMP parameter problem message body.
  31. func parseParamProb(proto int, b []byte) (MessageBody, error) {
  32. bodyLen := len(b)
  33. if bodyLen < 4 {
  34. return nil, errMessageTooShort
  35. }
  36. p := &ParamProb{}
  37. switch proto {
  38. case iana.ProtocolICMP:
  39. p.Pointer = uintptr(b[0])
  40. case iana.ProtocolIPv6ICMP:
  41. p.Pointer = uintptr(b[0])<<24 | uintptr(b[1])<<16 | uintptr(b[2])<<8 | uintptr(b[3])
  42. }
  43. if bodyLen > 4 {
  44. p.Data = make([]byte, bodyLen-4)
  45. copy(p.Data, b[4:])
  46. }
  47. return p, nil
  48. }