paramheader.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package sctp
  4. import (
  5. "encoding/binary"
  6. "encoding/hex"
  7. "errors"
  8. "fmt"
  9. )
  10. type paramHeaderUnrecognizedAction byte
  11. type paramHeader struct {
  12. typ paramType
  13. unrecognizedAction paramHeaderUnrecognizedAction
  14. len int
  15. raw []byte
  16. }
  17. /*
  18. The Parameter Types are encoded such that the highest-order 2 bits specify
  19. the action that is taken if the processing endpoint does not recognize the
  20. Parameter Type.
  21. 00 - Stop processing this parameter and do not process any further parameters within this chunk.
  22. 01 - Stop processing this parameter, do not process any further parameters within this chunk, and
  23. report the unrecognized parameter, as described in Section 3.2.2.
  24. 10 - Skip this parameter and continue processing.
  25. 11 - Skip this parameter and continue processing, but report the unrecognized
  26. parameter, as described in Section 3.2.2.
  27. https://www.rfc-editor.org/rfc/rfc9260.html#section-3.2.1
  28. */
  29. const (
  30. paramHeaderUnrecognizedActionMask = 0b11000000
  31. paramHeaderUnrecognizedActionStop paramHeaderUnrecognizedAction = 0b00000000
  32. paramHeaderUnrecognizedActionStopAndReport paramHeaderUnrecognizedAction = 0b01000000
  33. paramHeaderUnrecognizedActionSkip paramHeaderUnrecognizedAction = 0b10000000
  34. paramHeaderUnrecognizedActionSkipAndReport paramHeaderUnrecognizedAction = 0b11000000
  35. paramHeaderLength = 4
  36. )
  37. // Parameter header parse errors
  38. var (
  39. ErrParamHeaderTooShort = errors.New("param header too short")
  40. ErrParamHeaderSelfReportedLengthShorter = errors.New("param self reported length is shorter than header length")
  41. ErrParamHeaderSelfReportedLengthLonger = errors.New("param self reported length is longer than header length")
  42. ErrParamHeaderParseFailed = errors.New("failed to parse param type")
  43. )
  44. func (p *paramHeader) marshal() ([]byte, error) {
  45. paramLengthPlusHeader := paramHeaderLength + len(p.raw)
  46. rawParam := make([]byte, paramLengthPlusHeader)
  47. binary.BigEndian.PutUint16(rawParam[0:], uint16(p.typ))
  48. binary.BigEndian.PutUint16(rawParam[2:], uint16(paramLengthPlusHeader))
  49. copy(rawParam[paramHeaderLength:], p.raw)
  50. return rawParam, nil
  51. }
  52. func (p *paramHeader) unmarshal(raw []byte) error {
  53. if len(raw) < paramHeaderLength {
  54. return ErrParamHeaderTooShort
  55. }
  56. paramLengthPlusHeader := binary.BigEndian.Uint16(raw[2:])
  57. if int(paramLengthPlusHeader) < paramHeaderLength {
  58. return fmt.Errorf("%w: param self reported length (%d) shorter than header length (%d)", ErrParamHeaderSelfReportedLengthShorter, int(paramLengthPlusHeader), paramHeaderLength)
  59. }
  60. if len(raw) < int(paramLengthPlusHeader) {
  61. return fmt.Errorf("%w: param length (%d) shorter than its self reported length (%d)", ErrParamHeaderSelfReportedLengthLonger, len(raw), int(paramLengthPlusHeader))
  62. }
  63. typ, err := parseParamType(raw[0:])
  64. if err != nil {
  65. return fmt.Errorf("%w: %v", ErrParamHeaderParseFailed, err) //nolint:errorlint
  66. }
  67. p.typ = typ
  68. p.unrecognizedAction = paramHeaderUnrecognizedAction(raw[0] & paramHeaderUnrecognizedActionMask)
  69. p.raw = raw[paramHeaderLength:paramLengthPlusHeader]
  70. p.len = int(paramLengthPlusHeader)
  71. return nil
  72. }
  73. func (p *paramHeader) length() int {
  74. return p.len
  75. }
  76. // String makes paramHeader printable
  77. func (p paramHeader) String() string {
  78. return fmt.Sprintf("%s (%d): %s", p.typ, p.len, hex.Dump(p.raw))
  79. }