segment.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package kcp
  2. import (
  3. "v2ray.com/core/common"
  4. "v2ray.com/core/common/alloc"
  5. _ "v2ray.com/core/common/log"
  6. "v2ray.com/core/common/serial"
  7. )
  8. type Command byte
  9. const (
  10. CommandACK Command = 0
  11. CommandData Command = 1
  12. CommandTerminate Command = 2
  13. CommandPing Command = 3
  14. )
  15. type SegmentOption byte
  16. const (
  17. SegmentOptionClose SegmentOption = 1
  18. )
  19. type Segment interface {
  20. common.Releasable
  21. ByteSize() int
  22. Bytes([]byte) []byte
  23. }
  24. const (
  25. DataSegmentOverhead = 18
  26. )
  27. type DataSegment struct {
  28. Conv uint16
  29. Option SegmentOption
  30. Timestamp uint32
  31. Number uint32
  32. SendingNext uint32
  33. Data *alloc.Buffer
  34. timeout uint32
  35. transmit uint32
  36. }
  37. func NewDataSegment() *DataSegment {
  38. return new(DataSegment)
  39. }
  40. func (this *DataSegment) Bytes(b []byte) []byte {
  41. b = serial.Uint16ToBytes(this.Conv, b)
  42. b = append(b, byte(CommandData), byte(this.Option))
  43. b = serial.Uint32ToBytes(this.Timestamp, b)
  44. b = serial.Uint32ToBytes(this.Number, b)
  45. b = serial.Uint32ToBytes(this.SendingNext, b)
  46. b = serial.Uint16ToBytes(uint16(this.Data.Len()), b)
  47. b = append(b, this.Data.Value...)
  48. return b
  49. }
  50. func (this *DataSegment) ByteSize() int {
  51. return 2 + 1 + 1 + 4 + 4 + 4 + 2 + this.Data.Len()
  52. }
  53. func (this *DataSegment) Release() {
  54. this.Data.Release()
  55. this.Data = nil
  56. }
  57. type AckSegment struct {
  58. Conv uint16
  59. Option SegmentOption
  60. ReceivingWindow uint32
  61. ReceivingNext uint32
  62. Timestamp uint32
  63. Count byte
  64. NumberList []uint32
  65. }
  66. func NewAckSegment() *AckSegment {
  67. return new(AckSegment)
  68. }
  69. func (this *AckSegment) PutTimestamp(timestamp uint32) {
  70. if timestamp-this.Timestamp < 0x7FFFFFFF {
  71. this.Timestamp = timestamp
  72. }
  73. }
  74. func (this *AckSegment) PutNumber(number uint32) {
  75. this.Count++
  76. this.NumberList = append(this.NumberList, number)
  77. }
  78. func (this *AckSegment) IsFull() bool {
  79. return this.Count == 128
  80. }
  81. func (this *AckSegment) ByteSize() int {
  82. return 2 + 1 + 1 + 4 + 4 + 4 + 1 + int(this.Count)*4
  83. }
  84. func (this *AckSegment) Bytes(b []byte) []byte {
  85. b = serial.Uint16ToBytes(this.Conv, b)
  86. b = append(b, byte(CommandACK), byte(this.Option))
  87. b = serial.Uint32ToBytes(this.ReceivingWindow, b)
  88. b = serial.Uint32ToBytes(this.ReceivingNext, b)
  89. b = serial.Uint32ToBytes(this.Timestamp, b)
  90. b = append(b, this.Count)
  91. for i := byte(0); i < this.Count; i++ {
  92. b = serial.Uint32ToBytes(this.NumberList[i], b)
  93. }
  94. return b
  95. }
  96. func (this *AckSegment) Release() {
  97. this.NumberList = nil
  98. }
  99. type CmdOnlySegment struct {
  100. Conv uint16
  101. Command Command
  102. Option SegmentOption
  103. SendingNext uint32
  104. ReceivinNext uint32
  105. PeerRTO uint32
  106. }
  107. func NewCmdOnlySegment() *CmdOnlySegment {
  108. return new(CmdOnlySegment)
  109. }
  110. func (this *CmdOnlySegment) ByteSize() int {
  111. return 2 + 1 + 1 + 4 + 4 + 4
  112. }
  113. func (this *CmdOnlySegment) Bytes(b []byte) []byte {
  114. b = serial.Uint16ToBytes(this.Conv, b)
  115. b = append(b, byte(this.Command), byte(this.Option))
  116. b = serial.Uint32ToBytes(this.SendingNext, b)
  117. b = serial.Uint32ToBytes(this.ReceivinNext, b)
  118. b = serial.Uint32ToBytes(this.PeerRTO, b)
  119. return b
  120. }
  121. func (this *CmdOnlySegment) Release() {
  122. }
  123. func ReadSegment(buf []byte) (Segment, []byte) {
  124. if len(buf) < 4 {
  125. return nil, nil
  126. }
  127. conv := serial.BytesToUint16(buf)
  128. buf = buf[2:]
  129. cmd := Command(buf[0])
  130. opt := SegmentOption(buf[1])
  131. buf = buf[2:]
  132. if cmd == CommandData {
  133. seg := NewDataSegment()
  134. seg.Conv = conv
  135. seg.Option = opt
  136. if len(buf) < 16 {
  137. return nil, nil
  138. }
  139. seg.Timestamp = serial.BytesToUint32(buf)
  140. buf = buf[4:]
  141. seg.Number = serial.BytesToUint32(buf)
  142. buf = buf[4:]
  143. seg.SendingNext = serial.BytesToUint32(buf)
  144. buf = buf[4:]
  145. dataLen := int(serial.BytesToUint16(buf))
  146. buf = buf[2:]
  147. if len(buf) < dataLen {
  148. return nil, nil
  149. }
  150. seg.Data = AllocateBuffer().Clear().Append(buf[:dataLen])
  151. buf = buf[dataLen:]
  152. return seg, buf
  153. }
  154. if cmd == CommandACK {
  155. seg := NewAckSegment()
  156. seg.Conv = conv
  157. seg.Option = opt
  158. if len(buf) < 13 {
  159. return nil, nil
  160. }
  161. seg.ReceivingWindow = serial.BytesToUint32(buf)
  162. buf = buf[4:]
  163. seg.ReceivingNext = serial.BytesToUint32(buf)
  164. buf = buf[4:]
  165. seg.Timestamp = serial.BytesToUint32(buf)
  166. buf = buf[4:]
  167. count := int(buf[0])
  168. buf = buf[1:]
  169. if len(buf) < count*4 {
  170. return nil, nil
  171. }
  172. for i := 0; i < count; i++ {
  173. seg.PutNumber(serial.BytesToUint32(buf))
  174. buf = buf[4:]
  175. }
  176. return seg, buf
  177. }
  178. seg := NewCmdOnlySegment()
  179. seg.Conv = conv
  180. seg.Command = cmd
  181. seg.Option = opt
  182. if len(buf) < 12 {
  183. return nil, nil
  184. }
  185. seg.SendingNext = serial.BytesToUint32(buf)
  186. buf = buf[4:]
  187. seg.ReceivinNext = serial.BytesToUint32(buf)
  188. buf = buf[4:]
  189. seg.PeerRTO = serial.BytesToUint32(buf)
  190. buf = buf[4:]
  191. return seg, buf
  192. }