segment.go 4.9 KB

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