segment.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package kcp
  2. import (
  3. "encoding/binary"
  4. "v2ray.com/core/common/buf"
  5. )
  6. // Command is a KCP command that indicate the purpose of a Segment.
  7. type Command byte
  8. const (
  9. // CommandACK indicates an 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. Serialize([]byte)
  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 = binary.BigEndian.Uint32(buf)
  53. buf = buf[4:]
  54. s.Number = binary.BigEndian.Uint32(buf)
  55. buf = buf[4:]
  56. s.SendingNext = binary.BigEndian.Uint32(buf)
  57. buf = buf[4:]
  58. dataLen := int(binary.BigEndian.Uint16(buf))
  59. buf = buf[2:]
  60. if len(buf) < dataLen {
  61. return false, nil
  62. }
  63. s.Data().Clear()
  64. s.Data().Write(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) Serialize(b []byte) {
  86. binary.BigEndian.PutUint16(b, s.Conv)
  87. b[2] = byte(CommandData)
  88. b[3] = byte(s.Option)
  89. binary.BigEndian.PutUint32(b[4:], s.Timestamp)
  90. binary.BigEndian.PutUint32(b[8:], s.Number)
  91. binary.BigEndian.PutUint32(b[12:], s.SendingNext)
  92. binary.BigEndian.PutUint16(b[16:], uint16(s.payload.Len()))
  93. copy(b[18:], s.payload.Bytes())
  94. }
  95. func (s *DataSegment) ByteSize() int32 {
  96. return 2 + 1 + 1 + 4 + 4 + 4 + 2 + s.payload.Len()
  97. }
  98. func (s *DataSegment) Release() {
  99. s.payload.Release()
  100. s.payload = nil
  101. }
  102. type AckSegment struct {
  103. Conv uint16
  104. Option SegmentOption
  105. ReceivingWindow uint32
  106. ReceivingNext uint32
  107. Timestamp uint32
  108. NumberList []uint32
  109. }
  110. const ackNumberLimit = 128
  111. func NewAckSegment() *AckSegment {
  112. return new(AckSegment)
  113. }
  114. func (s *AckSegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
  115. s.Conv = conv
  116. s.Option = opt
  117. if len(buf) < 13 {
  118. return false, nil
  119. }
  120. s.ReceivingWindow = binary.BigEndian.Uint32(buf)
  121. buf = buf[4:]
  122. s.ReceivingNext = binary.BigEndian.Uint32(buf)
  123. buf = buf[4:]
  124. s.Timestamp = binary.BigEndian.Uint32(buf)
  125. buf = buf[4:]
  126. count := int(buf[0])
  127. buf = buf[1:]
  128. if len(buf) < count*4 {
  129. return false, nil
  130. }
  131. for i := 0; i < count; i++ {
  132. s.PutNumber(binary.BigEndian.Uint32(buf))
  133. buf = buf[4:]
  134. }
  135. return true, buf
  136. }
  137. func (s *AckSegment) Conversation() uint16 {
  138. return s.Conv
  139. }
  140. func (*AckSegment) Command() Command {
  141. return CommandACK
  142. }
  143. func (s *AckSegment) PutTimestamp(timestamp uint32) {
  144. if timestamp-s.Timestamp < 0x7FFFFFFF {
  145. s.Timestamp = timestamp
  146. }
  147. }
  148. func (s *AckSegment) PutNumber(number uint32) {
  149. s.NumberList = append(s.NumberList, number)
  150. }
  151. func (s *AckSegment) IsFull() bool {
  152. return len(s.NumberList) == ackNumberLimit
  153. }
  154. func (s *AckSegment) IsEmpty() bool {
  155. return len(s.NumberList) == 0
  156. }
  157. func (s *AckSegment) ByteSize() int32 {
  158. return 2 + 1 + 1 + 4 + 4 + 4 + 1 + int32(len(s.NumberList)*4)
  159. }
  160. func (s *AckSegment) Serialize(b []byte) {
  161. binary.BigEndian.PutUint16(b, s.Conv)
  162. b[2] = byte(CommandACK)
  163. b[3] = byte(s.Option)
  164. binary.BigEndian.PutUint32(b[4:], s.ReceivingWindow)
  165. binary.BigEndian.PutUint32(b[8:], s.ReceivingNext)
  166. binary.BigEndian.PutUint32(b[12:], s.Timestamp)
  167. b[16] = byte(len(s.NumberList))
  168. n := 17
  169. for _, number := range s.NumberList {
  170. binary.BigEndian.PutUint32(b[n:], number)
  171. n += 4
  172. }
  173. }
  174. func (s *AckSegment) Release() {}
  175. type CmdOnlySegment struct {
  176. Conv uint16
  177. Cmd Command
  178. Option SegmentOption
  179. SendingNext uint32
  180. ReceivingNext uint32
  181. PeerRTO uint32
  182. }
  183. func NewCmdOnlySegment() *CmdOnlySegment {
  184. return new(CmdOnlySegment)
  185. }
  186. func (s *CmdOnlySegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
  187. s.Conv = conv
  188. s.Cmd = cmd
  189. s.Option = opt
  190. if len(buf) < 12 {
  191. return false, nil
  192. }
  193. s.SendingNext = binary.BigEndian.Uint32(buf)
  194. buf = buf[4:]
  195. s.ReceivingNext = binary.BigEndian.Uint32(buf)
  196. buf = buf[4:]
  197. s.PeerRTO = binary.BigEndian.Uint32(buf)
  198. buf = buf[4:]
  199. return true, buf
  200. }
  201. func (s *CmdOnlySegment) Conversation() uint16 {
  202. return s.Conv
  203. }
  204. func (s *CmdOnlySegment) Command() Command {
  205. return s.Cmd
  206. }
  207. func (*CmdOnlySegment) ByteSize() int32 {
  208. return 2 + 1 + 1 + 4 + 4 + 4
  209. }
  210. func (s *CmdOnlySegment) Serialize(b []byte) {
  211. binary.BigEndian.PutUint16(b, s.Conv)
  212. b[2] = byte(s.Cmd)
  213. b[3] = byte(s.Option)
  214. binary.BigEndian.PutUint32(b[4:], s.SendingNext)
  215. binary.BigEndian.PutUint32(b[8:], s.ReceivingNext)
  216. binary.BigEndian.PutUint32(b[12:], s.PeerRTO)
  217. }
  218. func (*CmdOnlySegment) Release() {}
  219. func ReadSegment(buf []byte) (Segment, []byte) {
  220. if len(buf) < 4 {
  221. return nil, nil
  222. }
  223. conv := binary.BigEndian.Uint16(buf)
  224. buf = buf[2:]
  225. cmd := Command(buf[0])
  226. opt := SegmentOption(buf[1])
  227. buf = buf[2:]
  228. var seg Segment
  229. switch cmd {
  230. case CommandData:
  231. seg = NewDataSegment()
  232. case CommandACK:
  233. seg = NewAckSegment()
  234. default:
  235. seg = NewCmdOnlySegment()
  236. }
  237. valid, extra := seg.parse(conv, cmd, opt, buf)
  238. if !valid {
  239. return nil, nil
  240. }
  241. return seg, extra
  242. }