paramprob.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 (
  6. "encoding/binary"
  7. "golang.org/x/net/internal/iana"
  8. )
  9. // A ParamProb represents an ICMP parameter problem message body.
  10. type ParamProb struct {
  11. Pointer uintptr // offset within the data where the error was detected
  12. Data []byte // data, known as original datagram field
  13. Extensions []Extension // extensions
  14. }
  15. // Len implements the Len method of MessageBody interface.
  16. func (p *ParamProb) Len(proto int) int {
  17. if p == nil {
  18. return 0
  19. }
  20. l, _ := multipartMessageBodyDataLen(proto, p.Data, p.Extensions)
  21. return 4 + l
  22. }
  23. // Marshal implements the Marshal method of MessageBody interface.
  24. func (p *ParamProb) Marshal(proto int) ([]byte, error) {
  25. if proto == iana.ProtocolIPv6ICMP {
  26. b := make([]byte, p.Len(proto))
  27. binary.BigEndian.PutUint32(b[:4], uint32(p.Pointer))
  28. copy(b[4:], p.Data)
  29. return b, nil
  30. }
  31. b, err := marshalMultipartMessageBody(proto, p.Data, p.Extensions)
  32. if err != nil {
  33. return nil, err
  34. }
  35. b[0] = byte(p.Pointer)
  36. return b, nil
  37. }
  38. // parseParamProb parses b as an ICMP parameter problem message body.
  39. func parseParamProb(proto int, b []byte) (MessageBody, error) {
  40. if len(b) < 4 {
  41. return nil, errMessageTooShort
  42. }
  43. p := &ParamProb{}
  44. if proto == iana.ProtocolIPv6ICMP {
  45. p.Pointer = uintptr(binary.BigEndian.Uint32(b[:4]))
  46. p.Data = make([]byte, len(b)-4)
  47. copy(p.Data, b[4:])
  48. return p, nil
  49. }
  50. p.Pointer = uintptr(b[0])
  51. var err error
  52. p.Data, p.Extensions, err = parseMultipartMessageBody(proto, b)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return p, nil
  57. }