segment.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. package kcp
  2. import (
  3. "sync"
  4. "v2ray.com/core/common/buf"
  5. "v2ray.com/core/common/serial"
  6. )
  7. // Command is a KCP command that indicate the purpose of a Segment.
  8. type Command byte
  9. const (
  10. // CommandACK indicates an AckSegment.
  11. CommandACK Command = 0
  12. // CommandData indicates a DataSegment.
  13. CommandData Command = 1
  14. // CommandTerminate indicates that peer terminates the connection.
  15. CommandTerminate Command = 2
  16. // CommandPing indicates a ping.
  17. CommandPing Command = 3
  18. )
  19. type SegmentOption byte
  20. const (
  21. SegmentOptionClose SegmentOption = 1
  22. )
  23. type Segment interface {
  24. Release()
  25. Conversation() uint16
  26. Command() Command
  27. ByteSize() int32
  28. Bytes() buf.Supplier
  29. parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte)
  30. }
  31. const (
  32. DataSegmentOverhead = 18
  33. )
  34. var dataSegmentPool = sync.Pool{
  35. New: func() interface{} {
  36. return new(DataSegment)
  37. },
  38. }
  39. type DataSegment struct {
  40. Conv uint16
  41. Option SegmentOption
  42. Timestamp uint32
  43. Number uint32
  44. SendingNext uint32
  45. payload *buf.Buffer
  46. timeout uint32
  47. transmit uint32
  48. }
  49. func NewDataSegment() *DataSegment {
  50. seg := dataSegmentPool.Get().(*DataSegment)
  51. seg.Conv = 0
  52. seg.timeout = 0
  53. seg.transmit = 0
  54. return seg
  55. }
  56. func (s *DataSegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
  57. s.Conv = conv
  58. s.Option = opt
  59. if len(buf) < 15 {
  60. return false, nil
  61. }
  62. s.Timestamp = serial.BytesToUint32(buf)
  63. buf = buf[4:]
  64. s.Number = serial.BytesToUint32(buf)
  65. buf = buf[4:]
  66. s.SendingNext = serial.BytesToUint32(buf)
  67. buf = buf[4:]
  68. dataLen := int(serial.BytesToUint16(buf))
  69. buf = buf[2:]
  70. if len(buf) < dataLen {
  71. return false, nil
  72. }
  73. s.Data().Clear()
  74. s.Data().Write(buf[:dataLen])
  75. buf = buf[dataLen:]
  76. return true, buf
  77. }
  78. func (s *DataSegment) Conversation() uint16 {
  79. return s.Conv
  80. }
  81. func (*DataSegment) Command() Command {
  82. return CommandData
  83. }
  84. func (s *DataSegment) Detach() *buf.Buffer {
  85. r := s.payload
  86. s.payload = nil
  87. return r
  88. }
  89. func (s *DataSegment) Data() *buf.Buffer {
  90. if s.payload == nil {
  91. s.payload = buf.New()
  92. }
  93. return s.payload
  94. }
  95. func (s *DataSegment) Bytes() buf.Supplier {
  96. return func(b []byte) (int, error) {
  97. b = serial.Uint16ToBytes(s.Conv, b[:0])
  98. b = append(b, byte(CommandData), byte(s.Option))
  99. b = serial.Uint32ToBytes(s.Timestamp, b)
  100. b = serial.Uint32ToBytes(s.Number, b)
  101. b = serial.Uint32ToBytes(s.SendingNext, b)
  102. b = serial.Uint16ToBytes(uint16(s.payload.Len()), b)
  103. b = append(b, s.payload.Bytes()...)
  104. return len(b), nil
  105. }
  106. }
  107. func (s *DataSegment) ByteSize() int32 {
  108. return 2 + 1 + 1 + 4 + 4 + 4 + 2 + s.payload.Len()
  109. }
  110. func (s *DataSegment) Release() {
  111. s.payload.Release()
  112. s.payload = nil
  113. dataSegmentPool.Put(s)
  114. }
  115. var ackSegmentPool = sync.Pool{
  116. New: func() interface{} {
  117. return &AckSegment{
  118. NumberList: make([]uint32, 0, 16),
  119. }
  120. },
  121. }
  122. type AckSegment struct {
  123. Conv uint16
  124. Option SegmentOption
  125. ReceivingWindow uint32
  126. ReceivingNext uint32
  127. Timestamp uint32
  128. NumberList []uint32
  129. }
  130. const ackNumberLimit = 128
  131. func NewAckSegment() *AckSegment {
  132. seg := ackSegmentPool.Get().(*AckSegment)
  133. seg.NumberList = seg.NumberList[:0]
  134. return seg
  135. }
  136. func (s *AckSegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
  137. s.Conv = conv
  138. s.Option = opt
  139. if len(buf) < 13 {
  140. return false, nil
  141. }
  142. s.ReceivingWindow = serial.BytesToUint32(buf)
  143. buf = buf[4:]
  144. s.ReceivingNext = serial.BytesToUint32(buf)
  145. buf = buf[4:]
  146. s.Timestamp = serial.BytesToUint32(buf)
  147. buf = buf[4:]
  148. count := int(buf[0])
  149. buf = buf[1:]
  150. if len(buf) < count*4 {
  151. return false, nil
  152. }
  153. for i := 0; i < count; i++ {
  154. s.PutNumber(serial.BytesToUint32(buf))
  155. buf = buf[4:]
  156. }
  157. return true, buf
  158. }
  159. func (s *AckSegment) Conversation() uint16 {
  160. return s.Conv
  161. }
  162. func (*AckSegment) Command() Command {
  163. return CommandACK
  164. }
  165. func (s *AckSegment) PutTimestamp(timestamp uint32) {
  166. if timestamp-s.Timestamp < 0x7FFFFFFF {
  167. s.Timestamp = timestamp
  168. }
  169. }
  170. func (s *AckSegment) PutNumber(number uint32) {
  171. s.NumberList = append(s.NumberList, number)
  172. }
  173. func (s *AckSegment) IsFull() bool {
  174. return len(s.NumberList) == ackNumberLimit
  175. }
  176. func (s *AckSegment) IsEmpty() bool {
  177. return len(s.NumberList) == 0
  178. }
  179. func (s *AckSegment) ByteSize() int32 {
  180. return 2 + 1 + 1 + 4 + 4 + 4 + 1 + int32(len(s.NumberList)*4)
  181. }
  182. func (s *AckSegment) Bytes() buf.Supplier {
  183. return func(b []byte) (int, error) {
  184. b = serial.Uint16ToBytes(s.Conv, b[:0])
  185. b = append(b, byte(CommandACK), byte(s.Option))
  186. b = serial.Uint32ToBytes(s.ReceivingWindow, b)
  187. b = serial.Uint32ToBytes(s.ReceivingNext, b)
  188. b = serial.Uint32ToBytes(s.Timestamp, b)
  189. count := byte(len(s.NumberList))
  190. b = append(b, count)
  191. for _, number := range s.NumberList {
  192. b = serial.Uint32ToBytes(number, b)
  193. }
  194. return int(s.ByteSize()), nil
  195. }
  196. }
  197. func (s *AckSegment) Release() {
  198. ackSegmentPool.Put(s)
  199. }
  200. type CmdOnlySegment struct {
  201. Conv uint16
  202. Cmd Command
  203. Option SegmentOption
  204. SendingNext uint32
  205. ReceivingNext uint32
  206. PeerRTO uint32
  207. }
  208. func NewCmdOnlySegment() *CmdOnlySegment {
  209. return new(CmdOnlySegment)
  210. }
  211. func (s *CmdOnlySegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
  212. s.Conv = conv
  213. s.Cmd = cmd
  214. s.Option = opt
  215. if len(buf) < 12 {
  216. return false, nil
  217. }
  218. s.SendingNext = serial.BytesToUint32(buf)
  219. buf = buf[4:]
  220. s.ReceivingNext = serial.BytesToUint32(buf)
  221. buf = buf[4:]
  222. s.PeerRTO = serial.BytesToUint32(buf)
  223. buf = buf[4:]
  224. return true, buf
  225. }
  226. func (s *CmdOnlySegment) Conversation() uint16 {
  227. return s.Conv
  228. }
  229. func (s *CmdOnlySegment) Command() Command {
  230. return s.Cmd
  231. }
  232. func (*CmdOnlySegment) ByteSize() int32 {
  233. return 2 + 1 + 1 + 4 + 4 + 4
  234. }
  235. func (s *CmdOnlySegment) Bytes() buf.Supplier {
  236. return func(b []byte) (int, error) {
  237. b = serial.Uint16ToBytes(s.Conv, b[:0])
  238. b = append(b, byte(s.Cmd), byte(s.Option))
  239. b = serial.Uint32ToBytes(s.SendingNext, b)
  240. b = serial.Uint32ToBytes(s.ReceivingNext, b)
  241. b = serial.Uint32ToBytes(s.PeerRTO, b)
  242. return len(b), nil
  243. }
  244. }
  245. func (*CmdOnlySegment) Release() {}
  246. func ReadSegment(buf []byte) (Segment, []byte) {
  247. if len(buf) < 4 {
  248. return nil, nil
  249. }
  250. conv := serial.BytesToUint16(buf)
  251. buf = buf[2:]
  252. cmd := Command(buf[0])
  253. opt := SegmentOption(buf[1])
  254. buf = buf[2:]
  255. var seg Segment
  256. switch cmd {
  257. case CommandData:
  258. seg = NewDataSegment()
  259. case CommandACK:
  260. seg = NewAckSegment()
  261. default:
  262. seg = NewCmdOnlySegment()
  263. }
  264. valid, extra := seg.parse(conv, cmd, opt, buf)
  265. if !valid {
  266. return nil, nil
  267. }
  268. return seg, extra
  269. }