segment.go 5.0 KB

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