paramprob.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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, known as original datagram field
  10. Extensions []Extension // extensions
  11. }
  12. // Len implements the Len method of MessageBody interface.
  13. func (p *ParamProb) Len() int {
  14. if p == nil {
  15. return 0
  16. }
  17. return 4 + len(p.Data)
  18. }
  19. // Marshal implements the Marshal method of MessageBody interface.
  20. func (p *ParamProb) Marshal(proto int) ([]byte, error) {
  21. b := make([]byte, 4+len(p.Data))
  22. switch proto {
  23. case iana.ProtocolICMP:
  24. b[0] = byte(p.Pointer)
  25. case iana.ProtocolIPv6ICMP:
  26. b[0], b[1], b[2], b[3] = byte(p.Pointer>>24), byte(p.Pointer>>16), byte(p.Pointer>>8), byte(p.Pointer)
  27. }
  28. copy(b[4:], p.Data)
  29. return b, nil
  30. }
  31. // parseParamProb parses b as an ICMP parameter problem message body.
  32. func parseParamProb(proto int, b []byte) (MessageBody, error) {
  33. bodyLen := len(b)
  34. if bodyLen < 4 {
  35. return nil, errMessageTooShort
  36. }
  37. p := &ParamProb{}
  38. switch proto {
  39. case iana.ProtocolICMP:
  40. p.Pointer = uintptr(b[0])
  41. case iana.ProtocolIPv6ICMP:
  42. p.Pointer = uintptr(b[0])<<24 | uintptr(b[1])<<16 | uintptr(b[2])<<8 | uintptr(b[3])
  43. }
  44. if bodyLen > 4 {
  45. p.Data = make([]byte, bodyLen-4)
  46. copy(p.Data, b[4:])
  47. }
  48. return p, nil
  49. }