header.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package utp
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "fmt"
  6. )
  7. const (
  8. extensionTypeSelectiveAck = 1
  9. )
  10. type extensionField struct {
  11. Type byte
  12. Bytes []byte
  13. }
  14. type header struct {
  15. Type st
  16. Version int
  17. ConnID uint16
  18. Timestamp uint32
  19. TimestampDiff uint32
  20. WndSize uint32
  21. SeqNr uint16
  22. AckNr uint16
  23. Extensions []extensionField
  24. }
  25. func unmarshalExtensions(_type byte, b []byte) (n int, ef []extensionField, err error) {
  26. for _type != 0 {
  27. if _type != extensionTypeSelectiveAck {
  28. // An extension type that is not known to us. Generally we're
  29. // unmarshalling an packet that isn't actually uTP but we don't
  30. // yet know for sure until we try to deliver it.
  31. // logonce.Stderr.Printf("utp extension %d", _type)
  32. }
  33. if len(b) < 2 || len(b) < int(b[1])+2 {
  34. err = fmt.Errorf("buffer ends prematurely: %x", b)
  35. return
  36. }
  37. ef = append(ef, extensionField{
  38. Type: _type,
  39. Bytes: append([]byte(nil), b[2:int(b[1])+2]...),
  40. })
  41. _type = b[0]
  42. n += 2 + int(b[1])
  43. b = b[2+int(b[1]):]
  44. }
  45. return
  46. }
  47. var errInvalidHeader = errors.New("invalid header")
  48. func (h *header) Unmarshal(b []byte) (n int, err error) {
  49. h.Type = st(b[0] >> 4)
  50. h.Version = int(b[0] & 0xf)
  51. if h.Type > stMax || h.Version != 1 {
  52. err = errInvalidHeader
  53. return
  54. }
  55. n, h.Extensions, err = unmarshalExtensions(b[1], b[20:])
  56. if err != nil {
  57. return
  58. }
  59. h.ConnID = binary.BigEndian.Uint16(b[2:4])
  60. h.Timestamp = binary.BigEndian.Uint32(b[4:8])
  61. h.TimestampDiff = binary.BigEndian.Uint32(b[8:12])
  62. h.WndSize = binary.BigEndian.Uint32(b[12:16])
  63. h.SeqNr = binary.BigEndian.Uint16(b[16:18])
  64. h.AckNr = binary.BigEndian.Uint16(b[18:20])
  65. n += 20
  66. return
  67. }
  68. func (h *header) Marshal(p []byte) (n int) {
  69. n = 20 + func() (ret int) {
  70. for _, ext := range h.Extensions {
  71. ret += 2 + len(ext.Bytes)
  72. }
  73. return
  74. }()
  75. p = p[:n]
  76. p[0] = byte(h.Type<<4 | 1)
  77. binary.BigEndian.PutUint16(p[2:4], h.ConnID)
  78. binary.BigEndian.PutUint32(p[4:8], h.Timestamp)
  79. binary.BigEndian.PutUint32(p[8:12], h.TimestampDiff)
  80. binary.BigEndian.PutUint32(p[12:16], h.WndSize)
  81. binary.BigEndian.PutUint16(p[16:18], h.SeqNr)
  82. binary.BigEndian.PutUint16(p[18:20], h.AckNr)
  83. // Pointer to the last type field so the next extension can set it.
  84. _type := &p[1]
  85. // We're done with the basic header.
  86. p = p[20:]
  87. for _, ext := range h.Extensions {
  88. *_type = ext.Type
  89. // The next extension's type will go here.
  90. _type = &p[0]
  91. p[1] = uint8(len(ext.Bytes))
  92. if int(p[1]) != copy(p[2:], ext.Bytes) {
  93. panic("unexpected extension length")
  94. }
  95. p = p[2+len(ext.Bytes):]
  96. }
  97. *_type = 0
  98. if len(p) != 0 {
  99. panic("header length changed")
  100. }
  101. return
  102. }
  103. type selectiveAckBitmask struct {
  104. Bytes []byte
  105. }
  106. func (me *selectiveAckBitmask) expandBytesForBit(index int) {
  107. minLen := (3 + (index / 8) + 1) / 4 * 4
  108. for len(me.Bytes) < minLen {
  109. me.Bytes = append(me.Bytes, 0)
  110. }
  111. }
  112. func (me *selectiveAckBitmask) NumBits() int {
  113. return len(me.Bytes) * 8
  114. }
  115. func (me *selectiveAckBitmask) SetBit(index int) {
  116. me.expandBytesForBit(index)
  117. me.Bytes[index/8] |= 1 << uint(index%8)
  118. }
  119. func (me *selectiveAckBitmask) BitIsSet(index int) bool {
  120. return me.Bytes[index/8]>>uint(index%8)&1 == 1
  121. }