segment.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package kcp
  2. import (
  3. "encoding/binary"
  4. "github.com/xtls/xray-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. Limit int
  110. }
  111. const ackNumberLimit = 128
  112. func NewAckSegment(limit int) *AckSegment {
  113. if limit <= 0 {
  114. limit = 1
  115. }
  116. if limit > ackNumberLimit {
  117. limit = ackNumberLimit
  118. }
  119. return &AckSegment{
  120. Limit: limit,
  121. }
  122. }
  123. func (s *AckSegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
  124. s.Conv = conv
  125. s.Option = opt
  126. if len(buf) < 13 {
  127. return false, nil
  128. }
  129. s.ReceivingWindow = binary.BigEndian.Uint32(buf)
  130. buf = buf[4:]
  131. s.ReceivingNext = binary.BigEndian.Uint32(buf)
  132. buf = buf[4:]
  133. s.Timestamp = binary.BigEndian.Uint32(buf)
  134. buf = buf[4:]
  135. count := int(buf[0])
  136. buf = buf[1:]
  137. if len(buf) < count*4 {
  138. return false, nil
  139. }
  140. for i := 0; i < count; i++ {
  141. s.PutNumber(binary.BigEndian.Uint32(buf))
  142. buf = buf[4:]
  143. }
  144. return true, buf
  145. }
  146. func (s *AckSegment) Conversation() uint16 {
  147. return s.Conv
  148. }
  149. func (*AckSegment) Command() Command {
  150. return CommandACK
  151. }
  152. func (s *AckSegment) PutTimestamp(timestamp uint32) {
  153. if timestamp-s.Timestamp < 0x7FFFFFFF {
  154. s.Timestamp = timestamp
  155. }
  156. }
  157. func (s *AckSegment) PutNumber(number uint32) {
  158. s.NumberList = append(s.NumberList, number)
  159. }
  160. func (s *AckSegment) IsFull() bool {
  161. return len(s.NumberList) == s.Limit
  162. }
  163. func (s *AckSegment) IsEmpty() bool {
  164. return len(s.NumberList) == 0
  165. }
  166. func (s *AckSegment) ByteSize() int32 {
  167. return 2 + 1 + 1 + 4 + 4 + 4 + 1 + int32(len(s.NumberList)*4)
  168. }
  169. func (s *AckSegment) Serialize(b []byte) {
  170. binary.BigEndian.PutUint16(b, s.Conv)
  171. b[2] = byte(CommandACK)
  172. b[3] = byte(s.Option)
  173. binary.BigEndian.PutUint32(b[4:], s.ReceivingWindow)
  174. binary.BigEndian.PutUint32(b[8:], s.ReceivingNext)
  175. binary.BigEndian.PutUint32(b[12:], s.Timestamp)
  176. b[16] = byte(len(s.NumberList))
  177. n := 17
  178. for _, number := range s.NumberList {
  179. binary.BigEndian.PutUint32(b[n:], number)
  180. n += 4
  181. }
  182. }
  183. func (s *AckSegment) Release() {}
  184. type CmdOnlySegment struct {
  185. Conv uint16
  186. Cmd Command
  187. Option SegmentOption
  188. SendingNext uint32
  189. ReceivingNext uint32
  190. PeerRTO uint32
  191. }
  192. func NewCmdOnlySegment() *CmdOnlySegment {
  193. return new(CmdOnlySegment)
  194. }
  195. func (s *CmdOnlySegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
  196. s.Conv = conv
  197. s.Cmd = cmd
  198. s.Option = opt
  199. if len(buf) < 12 {
  200. return false, nil
  201. }
  202. s.SendingNext = binary.BigEndian.Uint32(buf)
  203. buf = buf[4:]
  204. s.ReceivingNext = binary.BigEndian.Uint32(buf)
  205. buf = buf[4:]
  206. s.PeerRTO = binary.BigEndian.Uint32(buf)
  207. buf = buf[4:]
  208. return true, buf
  209. }
  210. func (s *CmdOnlySegment) Conversation() uint16 {
  211. return s.Conv
  212. }
  213. func (s *CmdOnlySegment) Command() Command {
  214. return s.Cmd
  215. }
  216. func (*CmdOnlySegment) ByteSize() int32 {
  217. return 2 + 1 + 1 + 4 + 4 + 4
  218. }
  219. func (s *CmdOnlySegment) Serialize(b []byte) {
  220. binary.BigEndian.PutUint16(b, s.Conv)
  221. b[2] = byte(s.Cmd)
  222. b[3] = byte(s.Option)
  223. binary.BigEndian.PutUint32(b[4:], s.SendingNext)
  224. binary.BigEndian.PutUint32(b[8:], s.ReceivingNext)
  225. binary.BigEndian.PutUint32(b[12:], s.PeerRTO)
  226. }
  227. func (*CmdOnlySegment) Release() {}
  228. func ReadSegment(buf []byte) (Segment, []byte) {
  229. if len(buf) < 4 {
  230. return nil, nil
  231. }
  232. conv := binary.BigEndian.Uint16(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(128)
  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. }