chunk_selective_ack.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package sctp
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "fmt"
  6. )
  7. /*
  8. chunkSelectiveAck represents an SCTP Chunk of type SACK
  9. This chunk is sent to the peer endpoint to acknowledge received DATA
  10. chunks and to inform the peer endpoint of gaps in the received
  11. subsequences of DATA chunks as represented by their TSNs.
  12. 0 1 2 3
  13. 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
  14. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  15. | Type = 3 |Chunk Flags | Chunk Length |
  16. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  17. | Cumulative TSN Ack |
  18. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  19. | Advertised Receiver Window Credit (a_rwnd) |
  20. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  21. | Number of Gap Ack Blocks = N | Number of Duplicate TSNs = X |
  22. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  23. | Gap Ack Block #1 Start | Gap Ack Block #1 End |
  24. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  25. / /
  26. \ ... \
  27. / /
  28. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  29. | Gap Ack Block #N Start | Gap Ack Block #N End |
  30. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  31. | Duplicate TSN 1 |
  32. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  33. / /
  34. \ ... \
  35. / /
  36. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  37. | Duplicate TSN X |
  38. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  39. */
  40. type gapAckBlock struct {
  41. start uint16
  42. end uint16
  43. }
  44. var (
  45. errChunkTypeNotSack = errors.New("ChunkType is not of type SACK")
  46. errSackSizeNotLargeEnoughInfo = errors.New("SACK Chunk size is not large enough to contain header")
  47. errSackSizeNotMatchPredicted = errors.New("SACK Chunk size does not match predicted amount from header values")
  48. )
  49. // String makes gapAckBlock printable
  50. func (g gapAckBlock) String() string {
  51. return fmt.Sprintf("%d - %d", g.start, g.end)
  52. }
  53. type chunkSelectiveAck struct {
  54. chunkHeader
  55. cumulativeTSNAck uint32
  56. advertisedReceiverWindowCredit uint32
  57. gapAckBlocks []gapAckBlock
  58. duplicateTSN []uint32
  59. }
  60. const (
  61. selectiveAckHeaderSize = 12
  62. )
  63. func (s *chunkSelectiveAck) unmarshal(raw []byte) error {
  64. if err := s.chunkHeader.unmarshal(raw); err != nil {
  65. return err
  66. }
  67. if s.typ != ctSack {
  68. return fmt.Errorf("%w: actually is %s", errChunkTypeNotSack, s.typ.String())
  69. }
  70. if len(s.raw) < selectiveAckHeaderSize {
  71. return fmt.Errorf("%w: %v remaining, needs %v bytes", errSackSizeNotLargeEnoughInfo,
  72. len(s.raw), selectiveAckHeaderSize)
  73. }
  74. s.cumulativeTSNAck = binary.BigEndian.Uint32(s.raw[0:])
  75. s.advertisedReceiverWindowCredit = binary.BigEndian.Uint32(s.raw[4:])
  76. s.gapAckBlocks = make([]gapAckBlock, binary.BigEndian.Uint16(s.raw[8:]))
  77. s.duplicateTSN = make([]uint32, binary.BigEndian.Uint16(s.raw[10:]))
  78. if len(s.raw) != selectiveAckHeaderSize+(4*len(s.gapAckBlocks)+(4*len(s.duplicateTSN))) {
  79. return errSackSizeNotMatchPredicted
  80. }
  81. offset := selectiveAckHeaderSize
  82. for i := range s.gapAckBlocks {
  83. s.gapAckBlocks[i].start = binary.BigEndian.Uint16(s.raw[offset:])
  84. s.gapAckBlocks[i].end = binary.BigEndian.Uint16(s.raw[offset+2:])
  85. offset += 4
  86. }
  87. for i := range s.duplicateTSN {
  88. s.duplicateTSN[i] = binary.BigEndian.Uint32(s.raw[offset:])
  89. offset += 4
  90. }
  91. return nil
  92. }
  93. func (s *chunkSelectiveAck) marshal() ([]byte, error) {
  94. sackRaw := make([]byte, selectiveAckHeaderSize+(4*len(s.gapAckBlocks)+(4*len(s.duplicateTSN))))
  95. binary.BigEndian.PutUint32(sackRaw[0:], s.cumulativeTSNAck)
  96. binary.BigEndian.PutUint32(sackRaw[4:], s.advertisedReceiverWindowCredit)
  97. binary.BigEndian.PutUint16(sackRaw[8:], uint16(len(s.gapAckBlocks)))
  98. binary.BigEndian.PutUint16(sackRaw[10:], uint16(len(s.duplicateTSN)))
  99. offset := selectiveAckHeaderSize
  100. for _, g := range s.gapAckBlocks {
  101. binary.BigEndian.PutUint16(sackRaw[offset:], g.start)
  102. binary.BigEndian.PutUint16(sackRaw[offset+2:], g.end)
  103. offset += 4
  104. }
  105. for _, t := range s.duplicateTSN {
  106. binary.BigEndian.PutUint32(sackRaw[offset:], t)
  107. offset += 4
  108. }
  109. s.chunkHeader.typ = ctSack
  110. s.chunkHeader.raw = sackRaw
  111. return s.chunkHeader.marshal()
  112. }
  113. func (s *chunkSelectiveAck) check() (abort bool, err error) {
  114. return false, nil
  115. }
  116. // String makes chunkSelectiveAck printable
  117. func (s *chunkSelectiveAck) String() string {
  118. res := fmt.Sprintf("SACK cumTsnAck=%d arwnd=%d dupTsn=%d",
  119. s.cumulativeTSNAck,
  120. s.advertisedReceiverWindowCredit,
  121. s.duplicateTSN)
  122. for _, gap := range s.gapAckBlocks {
  123. res = fmt.Sprintf("%s\n gap ack: %s", res, gap)
  124. }
  125. return res
  126. }