segment.go 5.5 KB

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