segment.go 5.9 KB

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