segment.go 4.7 KB

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