chunkheader.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package sctp
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "fmt"
  6. )
  7. /*
  8. chunkHeader represents a SCTP Chunk header, defined in https://tools.ietf.org/html/rfc4960#section-3.2
  9. The figure below illustrates the field format for the chunks to be
  10. transmitted in the SCTP packet. Each chunk is formatted with a Chunk
  11. Type field, a chunk-specific Flag field, a Chunk Length field, and a
  12. Value field.
  13. 0 1 2 3
  14. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  15. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  16. | Chunk Type | Chunk Flags | Chunk Length |
  17. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  18. | |
  19. | Chunk Value |
  20. | |
  21. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  22. */
  23. type chunkHeader struct {
  24. typ chunkType
  25. flags byte
  26. raw []byte
  27. }
  28. const (
  29. chunkHeaderSize = 4
  30. )
  31. var (
  32. errChunkHeaderTooSmall = errors.New("raw is too small for a SCTP chunk")
  33. errChunkHeaderNotEnoughSpace = errors.New("not enough data left in SCTP packet to satisfy requested length")
  34. errChunkHeaderPaddingNonZero = errors.New("chunk padding is non-zero at offset")
  35. )
  36. func (c *chunkHeader) unmarshal(raw []byte) error {
  37. if len(raw) < chunkHeaderSize {
  38. return fmt.Errorf("%w: raw only %d bytes, %d is the minimum length", errChunkHeaderTooSmall, len(raw), chunkHeaderSize)
  39. }
  40. c.typ = chunkType(raw[0])
  41. c.flags = raw[1]
  42. length := binary.BigEndian.Uint16(raw[2:])
  43. // Length includes Chunk header
  44. valueLength := int(length - chunkHeaderSize)
  45. lengthAfterValue := len(raw) - (chunkHeaderSize + valueLength)
  46. if lengthAfterValue < 0 {
  47. return fmt.Errorf("%w: remain %d req %d ", errChunkHeaderNotEnoughSpace, valueLength, len(raw)-chunkHeaderSize)
  48. } else if lengthAfterValue < 4 {
  49. // https://tools.ietf.org/html/rfc4960#section-3.2
  50. // The Chunk Length field does not count any chunk padding.
  51. // Chunks (including Type, Length, and Value fields) are padded out
  52. // by the sender with all zero bytes to be a multiple of 4 bytes
  53. // long. This padding MUST NOT be more than 3 bytes in total. The
  54. // Chunk Length value does not include terminating padding of the
  55. // chunk. However, it does include padding of any variable-length
  56. // parameter except the last parameter in the chunk. The receiver
  57. // MUST ignore the padding.
  58. for i := lengthAfterValue; i > 0; i-- {
  59. paddingOffset := chunkHeaderSize + valueLength + (i - 1)
  60. if raw[paddingOffset] != 0 {
  61. return fmt.Errorf("%w: %d ", errChunkHeaderPaddingNonZero, paddingOffset)
  62. }
  63. }
  64. }
  65. c.raw = raw[chunkHeaderSize : chunkHeaderSize+valueLength]
  66. return nil
  67. }
  68. func (c *chunkHeader) marshal() ([]byte, error) {
  69. raw := make([]byte, 4+len(c.raw))
  70. raw[0] = uint8(c.typ)
  71. raw[1] = c.flags
  72. binary.BigEndian.PutUint16(raw[2:], uint16(len(c.raw)+chunkHeaderSize))
  73. copy(raw[4:], c.raw)
  74. return raw, nil
  75. }
  76. func (c *chunkHeader) valueLength() int {
  77. return len(c.raw)
  78. }
  79. // String makes chunkHeader printable
  80. func (c chunkHeader) String() string {
  81. return c.typ.String()
  82. }