segment.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package kcp
  2. import (
  3. "v2ray.com/core/common"
  4. "v2ray.com/core/common/alloc"
  5. "v2ray.com/core/common/serial"
  6. )
  7. type Command byte
  8. const (
  9. CommandACK Command = 0
  10. CommandData Command = 1
  11. CommandTerminate Command = 2
  12. CommandPing Command = 3
  13. )
  14. type SegmentOption byte
  15. const (
  16. SegmentOptionClose SegmentOption = 1
  17. )
  18. type Segment interface {
  19. common.Releasable
  20. Conversation() uint16
  21. ByteSize() int
  22. Bytes([]byte) []byte
  23. }
  24. const (
  25. DataSegmentOverhead = 18
  26. )
  27. type DataSegment struct {
  28. Conv uint16
  29. Option SegmentOption
  30. Timestamp uint32
  31. Number uint32
  32. SendingNext uint32
  33. Data *alloc.Buffer
  34. timeout uint32
  35. transmit uint32
  36. }
  37. func NewDataSegment() *DataSegment {
  38. return new(DataSegment)
  39. }
  40. func (v *DataSegment) Conversation() uint16 {
  41. return v.Conv
  42. }
  43. func (v *DataSegment) SetData(b []byte) {
  44. if v.Data == nil {
  45. v.Data = alloc.NewSmallBuffer()
  46. }
  47. v.Data.Clear().Append(b)
  48. }
  49. func (v *DataSegment) Bytes(b []byte) []byte {
  50. b = serial.Uint16ToBytes(v.Conv, b)
  51. b = append(b, byte(CommandData), byte(v.Option))
  52. b = serial.Uint32ToBytes(v.Timestamp, b)
  53. b = serial.Uint32ToBytes(v.Number, b)
  54. b = serial.Uint32ToBytes(v.SendingNext, b)
  55. b = serial.Uint16ToBytes(uint16(v.Data.Len()), b)
  56. b = append(b, v.Data.Value...)
  57. return b
  58. }
  59. func (v *DataSegment) ByteSize() int {
  60. return 2 + 1 + 1 + 4 + 4 + 4 + 2 + v.Data.Len()
  61. }
  62. func (v *DataSegment) Release() {
  63. v.Data.Release()
  64. v.Data = nil
  65. }
  66. type AckSegment struct {
  67. Conv uint16
  68. Option SegmentOption
  69. ReceivingWindow uint32
  70. ReceivingNext uint32
  71. Timestamp uint32
  72. Count byte
  73. NumberList []uint32
  74. }
  75. const ackNumberLimit = 128
  76. func NewAckSegment() *AckSegment {
  77. return &AckSegment{
  78. NumberList: make([]uint32, 0, ackNumberLimit),
  79. }
  80. }
  81. func (v *AckSegment) Conversation() uint16 {
  82. return v.Conv
  83. }
  84. func (v *AckSegment) PutTimestamp(timestamp uint32) {
  85. if timestamp-v.Timestamp < 0x7FFFFFFF {
  86. v.Timestamp = timestamp
  87. }
  88. }
  89. func (v *AckSegment) PutNumber(number uint32) {
  90. v.Count++
  91. v.NumberList = append(v.NumberList, number)
  92. }
  93. func (v *AckSegment) IsFull() bool {
  94. return v.Count == ackNumberLimit
  95. }
  96. func (v *AckSegment) ByteSize() int {
  97. return 2 + 1 + 1 + 4 + 4 + 4 + 1 + int(v.Count)*4
  98. }
  99. func (v *AckSegment) Bytes(b []byte) []byte {
  100. b = serial.Uint16ToBytes(v.Conv, b)
  101. b = append(b, byte(CommandACK), byte(v.Option))
  102. b = serial.Uint32ToBytes(v.ReceivingWindow, b)
  103. b = serial.Uint32ToBytes(v.ReceivingNext, b)
  104. b = serial.Uint32ToBytes(v.Timestamp, b)
  105. b = append(b, v.Count)
  106. for i := byte(0); i < v.Count; i++ {
  107. b = serial.Uint32ToBytes(v.NumberList[i], b)
  108. }
  109. return b
  110. }
  111. func (v *AckSegment) Release() {
  112. v.NumberList = nil
  113. }
  114. type CmdOnlySegment struct {
  115. Conv uint16
  116. Command Command
  117. Option SegmentOption
  118. SendingNext uint32
  119. ReceivinNext uint32
  120. PeerRTO uint32
  121. }
  122. func NewCmdOnlySegment() *CmdOnlySegment {
  123. return new(CmdOnlySegment)
  124. }
  125. func (v *CmdOnlySegment) Conversation() uint16 {
  126. return v.Conv
  127. }
  128. func (v *CmdOnlySegment) ByteSize() int {
  129. return 2 + 1 + 1 + 4 + 4 + 4
  130. }
  131. func (v *CmdOnlySegment) Bytes(b []byte) []byte {
  132. b = serial.Uint16ToBytes(v.Conv, b)
  133. b = append(b, byte(v.Command), byte(v.Option))
  134. b = serial.Uint32ToBytes(v.SendingNext, b)
  135. b = serial.Uint32ToBytes(v.ReceivinNext, b)
  136. b = serial.Uint32ToBytes(v.PeerRTO, b)
  137. return b
  138. }
  139. func (v *CmdOnlySegment) Release() {
  140. }
  141. func ReadSegment(buf []byte) (Segment, []byte) {
  142. if len(buf) < 4 {
  143. return nil, nil
  144. }
  145. conv := serial.BytesToUint16(buf)
  146. buf = buf[2:]
  147. cmd := Command(buf[0])
  148. opt := SegmentOption(buf[1])
  149. buf = buf[2:]
  150. if cmd == CommandData {
  151. seg := NewDataSegment()
  152. seg.Conv = conv
  153. seg.Option = opt
  154. if len(buf) < 16 {
  155. return nil, nil
  156. }
  157. seg.Timestamp = serial.BytesToUint32(buf)
  158. buf = buf[4:]
  159. seg.Number = serial.BytesToUint32(buf)
  160. buf = buf[4:]
  161. seg.SendingNext = serial.BytesToUint32(buf)
  162. buf = buf[4:]
  163. dataLen := int(serial.BytesToUint16(buf))
  164. buf = buf[2:]
  165. if len(buf) < dataLen {
  166. return nil, nil
  167. }
  168. seg.SetData(buf[:dataLen])
  169. buf = buf[dataLen:]
  170. return seg, buf
  171. }
  172. if cmd == CommandACK {
  173. seg := NewAckSegment()
  174. seg.Conv = conv
  175. seg.Option = opt
  176. if len(buf) < 13 {
  177. return nil, nil
  178. }
  179. seg.ReceivingWindow = serial.BytesToUint32(buf)
  180. buf = buf[4:]
  181. seg.ReceivingNext = serial.BytesToUint32(buf)
  182. buf = buf[4:]
  183. seg.Timestamp = serial.BytesToUint32(buf)
  184. buf = buf[4:]
  185. count := int(buf[0])
  186. buf = buf[1:]
  187. if len(buf) < count*4 {
  188. return nil, nil
  189. }
  190. for i := 0; i < count; i++ {
  191. seg.PutNumber(serial.BytesToUint32(buf))
  192. buf = buf[4:]
  193. }
  194. return seg, buf
  195. }
  196. seg := NewCmdOnlySegment()
  197. seg.Conv = conv
  198. seg.Command = cmd
  199. seg.Option = opt
  200. if len(buf) < 12 {
  201. return nil, nil
  202. }
  203. seg.SendingNext = serial.BytesToUint32(buf)
  204. buf = buf[4:]
  205. seg.ReceivinNext = serial.BytesToUint32(buf)
  206. buf = buf[4:]
  207. seg.PeerRTO = serial.BytesToUint32(buf)
  208. buf = buf[4:]
  209. return seg, buf
  210. }