segment.go 5.6 KB

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