segment.go 5.6 KB

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