segment.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package kcp
  2. import (
  3. "v2ray.com/core/common/buf"
  4. "v2ray.com/core/common/serial"
  5. )
  6. // Command is a KCP command that indicate the purpose of a Segment.
  7. type Command byte
  8. const (
  9. // CommandACK indicates a 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() int
  27. Bytes() buf.Supplier
  28. }
  29. const (
  30. DataSegmentOverhead = 18
  31. )
  32. type DataSegment struct {
  33. Conv uint16
  34. Option SegmentOption
  35. Timestamp uint32
  36. Number uint32
  37. SendingNext uint32
  38. payload *buf.Buffer
  39. timeout uint32
  40. transmit uint32
  41. }
  42. func NewDataSegment() *DataSegment {
  43. return new(DataSegment)
  44. }
  45. func (s *DataSegment) Conversation() uint16 {
  46. return s.Conv
  47. }
  48. func (*DataSegment) Command() Command {
  49. return CommandData
  50. }
  51. func (s *DataSegment) Detach() *buf.Buffer {
  52. r := s.payload
  53. s.payload = nil
  54. return r
  55. }
  56. func (s *DataSegment) Data() *buf.Buffer {
  57. if s.payload == nil {
  58. s.payload = buf.New()
  59. }
  60. return s.payload
  61. }
  62. func (s *DataSegment) Bytes() buf.Supplier {
  63. return func(b []byte) (int, error) {
  64. b = serial.Uint16ToBytes(s.Conv, b[:0])
  65. b = append(b, byte(CommandData), byte(s.Option))
  66. b = serial.Uint32ToBytes(s.Timestamp, b)
  67. b = serial.Uint32ToBytes(s.Number, b)
  68. b = serial.Uint32ToBytes(s.SendingNext, b)
  69. b = serial.Uint16ToBytes(uint16(s.payload.Len()), b)
  70. b = append(b, s.payload.Bytes()...)
  71. return len(b), nil
  72. }
  73. }
  74. func (s *DataSegment) ByteSize() int {
  75. return 2 + 1 + 1 + 4 + 4 + 4 + 2 + s.payload.Len()
  76. }
  77. func (s *DataSegment) Release() {
  78. s.payload.Release()
  79. s.payload = nil
  80. }
  81. type AckSegment struct {
  82. Conv uint16
  83. Option SegmentOption
  84. ReceivingWindow uint32
  85. ReceivingNext uint32
  86. Timestamp uint32
  87. NumberList []uint32
  88. }
  89. const ackNumberLimit = 128
  90. func NewAckSegment() *AckSegment {
  91. return &AckSegment{
  92. NumberList: make([]uint32, 0, ackNumberLimit),
  93. }
  94. }
  95. func (s *AckSegment) Conversation() uint16 {
  96. return s.Conv
  97. }
  98. func (*AckSegment) Command() Command {
  99. return CommandACK
  100. }
  101. func (s *AckSegment) PutTimestamp(timestamp uint32) {
  102. if timestamp-s.Timestamp < 0x7FFFFFFF {
  103. s.Timestamp = timestamp
  104. }
  105. }
  106. func (s *AckSegment) PutNumber(number uint32) {
  107. s.NumberList = append(s.NumberList, number)
  108. }
  109. func (s *AckSegment) IsFull() bool {
  110. return len(s.NumberList) == ackNumberLimit
  111. }
  112. func (s *AckSegment) IsEmpty() bool {
  113. return len(s.NumberList) == 0
  114. }
  115. func (s *AckSegment) ByteSize() int {
  116. return 2 + 1 + 1 + 4 + 4 + 4 + 1 + len(s.NumberList)*4
  117. }
  118. func (s *AckSegment) Bytes() buf.Supplier {
  119. return func(b []byte) (int, error) {
  120. b = serial.Uint16ToBytes(s.Conv, b[:0])
  121. b = append(b, byte(CommandACK), byte(s.Option))
  122. b = serial.Uint32ToBytes(s.ReceivingWindow, b)
  123. b = serial.Uint32ToBytes(s.ReceivingNext, b)
  124. b = serial.Uint32ToBytes(s.Timestamp, b)
  125. count := byte(len(s.NumberList))
  126. b = append(b, count)
  127. for _, number := range s.NumberList {
  128. b = serial.Uint32ToBytes(number, b)
  129. }
  130. return s.ByteSize(), nil
  131. }
  132. }
  133. func (s *AckSegment) Release() {
  134. s.NumberList = nil
  135. }
  136. type CmdOnlySegment struct {
  137. Conv uint16
  138. Cmd Command
  139. Option SegmentOption
  140. SendingNext uint32
  141. ReceivingNext uint32
  142. PeerRTO uint32
  143. }
  144. func NewCmdOnlySegment() *CmdOnlySegment {
  145. return new(CmdOnlySegment)
  146. }
  147. func (s *CmdOnlySegment) Conversation() uint16 {
  148. return s.Conv
  149. }
  150. func (s *CmdOnlySegment) Command() Command {
  151. return s.Cmd
  152. }
  153. func (*CmdOnlySegment) ByteSize() int {
  154. return 2 + 1 + 1 + 4 + 4 + 4
  155. }
  156. func (s *CmdOnlySegment) Bytes() buf.Supplier {
  157. return func(b []byte) (int, error) {
  158. b = serial.Uint16ToBytes(s.Conv, b[:0])
  159. b = append(b, byte(s.Cmd), byte(s.Option))
  160. b = serial.Uint32ToBytes(s.SendingNext, b)
  161. b = serial.Uint32ToBytes(s.ReceivingNext, b)
  162. b = serial.Uint32ToBytes(s.PeerRTO, b)
  163. return len(b), nil
  164. }
  165. }
  166. func (*CmdOnlySegment) Release() {}
  167. func ReadSegment(buf []byte) (Segment, []byte) {
  168. if len(buf) < 4 {
  169. return nil, nil
  170. }
  171. conv := serial.BytesToUint16(buf)
  172. buf = buf[2:]
  173. cmd := Command(buf[0])
  174. opt := SegmentOption(buf[1])
  175. buf = buf[2:]
  176. if cmd == CommandData {
  177. seg := NewDataSegment()
  178. seg.Conv = conv
  179. seg.Option = opt
  180. if len(buf) < 15 {
  181. return nil, nil
  182. }
  183. seg.Timestamp = serial.BytesToUint32(buf)
  184. buf = buf[4:]
  185. seg.Number = serial.BytesToUint32(buf)
  186. buf = buf[4:]
  187. seg.SendingNext = serial.BytesToUint32(buf)
  188. buf = buf[4:]
  189. dataLen := int(serial.BytesToUint16(buf))
  190. buf = buf[2:]
  191. if len(buf) < dataLen {
  192. return nil, nil
  193. }
  194. seg.Data().Clear()
  195. seg.Data().Append(buf[:dataLen])
  196. buf = buf[dataLen:]
  197. return seg, buf
  198. }
  199. if cmd == CommandACK {
  200. seg := NewAckSegment()
  201. seg.Conv = conv
  202. seg.Option = opt
  203. if len(buf) < 13 {
  204. return nil, nil
  205. }
  206. seg.ReceivingWindow = serial.BytesToUint32(buf)
  207. buf = buf[4:]
  208. seg.ReceivingNext = serial.BytesToUint32(buf)
  209. buf = buf[4:]
  210. seg.Timestamp = serial.BytesToUint32(buf)
  211. buf = buf[4:]
  212. count := int(buf[0])
  213. buf = buf[1:]
  214. if len(buf) < count*4 {
  215. return nil, nil
  216. }
  217. for i := 0; i < count; i++ {
  218. seg.PutNumber(serial.BytesToUint32(buf))
  219. buf = buf[4:]
  220. }
  221. return seg, buf
  222. }
  223. seg := NewCmdOnlySegment()
  224. seg.Conv = conv
  225. seg.Cmd = cmd
  226. seg.Option = opt
  227. if len(buf) < 12 {
  228. return nil, nil
  229. }
  230. seg.SendingNext = serial.BytesToUint32(buf)
  231. buf = buf[4:]
  232. seg.ReceivingNext = serial.BytesToUint32(buf)
  233. buf = buf[4:]
  234. seg.PeerRTO = serial.BytesToUint32(buf)
  235. buf = buf[4:]
  236. return seg, buf
  237. }