segment.go 6.1 KB

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