segment.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. common.Releasable
  25. Conversation() uint16
  26. Command() Command
  27. ByteSize() int
  28. Bytes() buf.BytesWriter
  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(b []byte) {
  53. if v.Data == nil {
  54. v.Data = buf.NewSmallBuffer()
  55. }
  56. v.Data.Clear()
  57. v.Data.Append(b)
  58. }
  59. func (v *DataSegment) Bytes() buf.BytesWriter {
  60. return func(b []byte) int {
  61. b = serial.Uint16ToBytes(v.Conv, b[:0])
  62. b = append(b, byte(CommandData), byte(v.Option))
  63. b = serial.Uint32ToBytes(v.Timestamp, b)
  64. b = serial.Uint32ToBytes(v.Number, b)
  65. b = serial.Uint32ToBytes(v.SendingNext, b)
  66. b = serial.Uint16ToBytes(uint16(v.Data.Len()), b)
  67. b = append(b, v.Data.Bytes()...)
  68. return v.ByteSize()
  69. }
  70. }
  71. func (v *DataSegment) ByteSize() int {
  72. return 2 + 1 + 1 + 4 + 4 + 4 + 2 + v.Data.Len()
  73. }
  74. func (v *DataSegment) Release() {
  75. v.Data.Release()
  76. v.Data = nil
  77. }
  78. type AckSegment struct {
  79. Conv uint16
  80. Option SegmentOption
  81. ReceivingWindow uint32
  82. ReceivingNext uint32
  83. Timestamp uint32
  84. Count byte
  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.Count++
  106. v.NumberList = append(v.NumberList, number)
  107. }
  108. func (v *AckSegment) IsFull() bool {
  109. return v.Count == ackNumberLimit
  110. }
  111. func (v *AckSegment) ByteSize() int {
  112. return 2 + 1 + 1 + 4 + 4 + 4 + 1 + int(v.Count)*4
  113. }
  114. func (v *AckSegment) Bytes() buf.BytesWriter {
  115. return func(b []byte) int {
  116. b = serial.Uint16ToBytes(v.Conv, b[:0])
  117. b = append(b, byte(CommandACK), byte(v.Option))
  118. b = serial.Uint32ToBytes(v.ReceivingWindow, b)
  119. b = serial.Uint32ToBytes(v.ReceivingNext, b)
  120. b = serial.Uint32ToBytes(v.Timestamp, b)
  121. b = append(b, v.Count)
  122. for i := byte(0); i < v.Count; i++ {
  123. b = serial.Uint32ToBytes(v.NumberList[i], b)
  124. }
  125. return v.ByteSize()
  126. }
  127. }
  128. func (v *AckSegment) Release() {
  129. v.NumberList = nil
  130. }
  131. type CmdOnlySegment struct {
  132. Conv uint16
  133. Cmd Command
  134. Option SegmentOption
  135. SendingNext uint32
  136. ReceivinNext uint32
  137. PeerRTO uint32
  138. }
  139. func NewCmdOnlySegment() *CmdOnlySegment {
  140. return new(CmdOnlySegment)
  141. }
  142. func (v *CmdOnlySegment) Conversation() uint16 {
  143. return v.Conv
  144. }
  145. func (v *CmdOnlySegment) Command() Command {
  146. return v.Cmd
  147. }
  148. func (v *CmdOnlySegment) ByteSize() int {
  149. return 2 + 1 + 1 + 4 + 4 + 4
  150. }
  151. func (v *CmdOnlySegment) Bytes() buf.BytesWriter {
  152. return func(b []byte) int {
  153. b = serial.Uint16ToBytes(v.Conv, b[:0])
  154. b = append(b, byte(v.Cmd), byte(v.Option))
  155. b = serial.Uint32ToBytes(v.SendingNext, b)
  156. b = serial.Uint32ToBytes(v.ReceivinNext, b)
  157. b = serial.Uint32ToBytes(v.PeerRTO, b)
  158. return v.ByteSize()
  159. }
  160. }
  161. func (v *CmdOnlySegment) Release() {
  162. }
  163. func ReadSegment(buf []byte) (Segment, []byte) {
  164. if len(buf) < 4 {
  165. return nil, nil
  166. }
  167. conv := serial.BytesToUint16(buf)
  168. buf = buf[2:]
  169. cmd := Command(buf[0])
  170. opt := SegmentOption(buf[1])
  171. buf = buf[2:]
  172. if cmd == CommandData {
  173. seg := NewDataSegment()
  174. seg.Conv = conv
  175. seg.Option = opt
  176. if len(buf) < 16 {
  177. return nil, nil
  178. }
  179. seg.Timestamp = serial.BytesToUint32(buf)
  180. buf = buf[4:]
  181. seg.Number = serial.BytesToUint32(buf)
  182. buf = buf[4:]
  183. seg.SendingNext = serial.BytesToUint32(buf)
  184. buf = buf[4:]
  185. dataLen := int(serial.BytesToUint16(buf))
  186. buf = buf[2:]
  187. if len(buf) < dataLen {
  188. return nil, nil
  189. }
  190. seg.SetData(buf[:dataLen])
  191. buf = buf[dataLen:]
  192. return seg, buf
  193. }
  194. if cmd == CommandACK {
  195. seg := NewAckSegment()
  196. seg.Conv = conv
  197. seg.Option = opt
  198. if len(buf) < 13 {
  199. return nil, nil
  200. }
  201. seg.ReceivingWindow = serial.BytesToUint32(buf)
  202. buf = buf[4:]
  203. seg.ReceivingNext = serial.BytesToUint32(buf)
  204. buf = buf[4:]
  205. seg.Timestamp = serial.BytesToUint32(buf)
  206. buf = buf[4:]
  207. count := int(buf[0])
  208. buf = buf[1:]
  209. if len(buf) < count*4 {
  210. return nil, nil
  211. }
  212. for i := 0; i < count; i++ {
  213. seg.PutNumber(serial.BytesToUint32(buf))
  214. buf = buf[4:]
  215. }
  216. return seg, buf
  217. }
  218. seg := NewCmdOnlySegment()
  219. seg.Conv = conv
  220. seg.Cmd = cmd
  221. seg.Option = opt
  222. if len(buf) < 12 {
  223. return nil, nil
  224. }
  225. seg.SendingNext = serial.BytesToUint32(buf)
  226. buf = buf[4:]
  227. seg.ReceivinNext = serial.BytesToUint32(buf)
  228. buf = buf[4:]
  229. seg.PeerRTO = serial.BytesToUint32(buf)
  230. buf = buf[4:]
  231. return seg, buf
  232. }