segment.go 5.9 KB

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