paramprob.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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(proto int) int {
  14. if p == nil {
  15. return 0
  16. }
  17. l, _ := multipartMessageBodyDataLen(proto, p.Data, p.Extensions)
  18. return 4 + l
  19. }
  20. // Marshal implements the Marshal method of MessageBody interface.
  21. func (p *ParamProb) Marshal(proto int) ([]byte, error) {
  22. if proto == iana.ProtocolIPv6ICMP {
  23. b := make([]byte, p.Len(proto))
  24. b[0], b[1], b[2], b[3] = byte(p.Pointer>>24), byte(p.Pointer>>16), byte(p.Pointer>>8), byte(p.Pointer)
  25. copy(b[4:], p.Data)
  26. return b, nil
  27. }
  28. b, err := marshalMultipartMessageBody(proto, p.Data, p.Extensions)
  29. if err != nil {
  30. return nil, err
  31. }
  32. b[0] = byte(p.Pointer)
  33. return b, nil
  34. }
  35. // parseParamProb parses b as an ICMP parameter problem message body.
  36. func parseParamProb(proto int, b []byte) (MessageBody, error) {
  37. if len(b) < 4 {
  38. return nil, errMessageTooShort
  39. }
  40. p := &ParamProb{}
  41. if proto == iana.ProtocolIPv6ICMP {
  42. p.Pointer = uintptr(b[0])<<24 | uintptr(b[1])<<16 | uintptr(b[2])<<8 | uintptr(b[3])
  43. p.Data = make([]byte, len(b)-4)
  44. copy(p.Data, b[4:])
  45. return p, nil
  46. }
  47. p.Pointer = uintptr(b[0])
  48. var err error
  49. p.Data, p.Extensions, err = parseMultipartMessageBody(proto, b)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return p, nil
  54. }