segment.go 6.1 KB

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