segment.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. Bytes() buf.Supplier
  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) Bytes() buf.Supplier {
  86. return func(b []byte) (int, error) {
  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. n := copy(b[18:], s.payload.Bytes())
  95. return 18 + n, 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. binary.BigEndian.PutUint16(b, s.Conv)
  166. b[2] = byte(CommandACK)
  167. b[3] = byte(s.Option)
  168. binary.BigEndian.PutUint32(b[4:], s.ReceivingWindow)
  169. binary.BigEndian.PutUint32(b[8:], s.ReceivingNext)
  170. binary.BigEndian.PutUint32(b[12:], s.Timestamp)
  171. b[16] = byte(len(s.NumberList))
  172. n := 17
  173. for _, number := range s.NumberList {
  174. binary.BigEndian.PutUint32(b[n:], number)
  175. n += 4
  176. }
  177. return n, nil
  178. }
  179. }
  180. func (s *AckSegment) Release() {}
  181. type CmdOnlySegment struct {
  182. Conv uint16
  183. Cmd Command
  184. Option SegmentOption
  185. SendingNext uint32
  186. ReceivingNext uint32
  187. PeerRTO uint32
  188. }
  189. func NewCmdOnlySegment() *CmdOnlySegment {
  190. return new(CmdOnlySegment)
  191. }
  192. func (s *CmdOnlySegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
  193. s.Conv = conv
  194. s.Cmd = cmd
  195. s.Option = opt
  196. if len(buf) < 12 {
  197. return false, nil
  198. }
  199. s.SendingNext = binary.BigEndian.Uint32(buf)
  200. buf = buf[4:]
  201. s.ReceivingNext = binary.BigEndian.Uint32(buf)
  202. buf = buf[4:]
  203. s.PeerRTO = binary.BigEndian.Uint32(buf)
  204. buf = buf[4:]
  205. return true, buf
  206. }
  207. func (s *CmdOnlySegment) Conversation() uint16 {
  208. return s.Conv
  209. }
  210. func (s *CmdOnlySegment) Command() Command {
  211. return s.Cmd
  212. }
  213. func (*CmdOnlySegment) ByteSize() int32 {
  214. return 2 + 1 + 1 + 4 + 4 + 4
  215. }
  216. func (s *CmdOnlySegment) Bytes() buf.Supplier {
  217. return func(b []byte) (int, error) {
  218. binary.BigEndian.PutUint16(b, s.Conv)
  219. b[2] = byte(s.Cmd)
  220. b[3] = byte(s.Option)
  221. binary.BigEndian.PutUint32(b[4:], s.SendingNext)
  222. binary.BigEndian.PutUint32(b[8:], s.ReceivingNext)
  223. binary.BigEndian.PutUint32(b[12:], s.PeerRTO)
  224. return 16, nil
  225. }
  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()
  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. }