segment.go 5.5 KB

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