segment.go 5.2 KB

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