segment.go 5.0 KB

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