2016-06-27 16:22:01 -04:00
|
|
|
package kcp
|
|
|
|
|
|
|
|
import (
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common"
|
|
|
|
"v2ray.com/core/common/alloc"
|
|
|
|
_ "v2ray.com/core/common/log"
|
|
|
|
"v2ray.com/core/common/serial"
|
2016-06-27 16:22:01 -04:00
|
|
|
)
|
|
|
|
|
2016-07-14 16:10:37 -04:00
|
|
|
type Command byte
|
2016-06-27 16:22:01 -04:00
|
|
|
|
|
|
|
const (
|
2016-07-14 16:10:37 -04:00
|
|
|
CommandACK Command = 0
|
|
|
|
CommandData Command = 1
|
|
|
|
CommandTerminate Command = 2
|
|
|
|
CommandPing Command = 3
|
2016-06-27 16:22:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type SegmentOption byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
SegmentOptionClose SegmentOption = 1
|
|
|
|
)
|
|
|
|
|
2016-07-04 08:17:42 -04:00
|
|
|
type Segment interface {
|
2016-06-27 16:54:59 -04:00
|
|
|
common.Releasable
|
2016-06-27 16:22:01 -04:00
|
|
|
ByteSize() int
|
|
|
|
Bytes([]byte) []byte
|
|
|
|
}
|
|
|
|
|
2016-06-29 06:52:23 -04:00
|
|
|
const (
|
|
|
|
DataSegmentOverhead = 18
|
|
|
|
)
|
|
|
|
|
2016-06-27 16:22:01 -04:00
|
|
|
type DataSegment struct {
|
2016-06-29 04:34:34 -04:00
|
|
|
Conv uint16
|
2016-07-14 16:52:00 -04:00
|
|
|
Option SegmentOption
|
2016-06-29 04:34:34 -04:00
|
|
|
Timestamp uint32
|
|
|
|
Number uint32
|
|
|
|
SendingNext uint32
|
|
|
|
Data *alloc.Buffer
|
2016-06-27 16:22:01 -04:00
|
|
|
|
|
|
|
timeout uint32
|
|
|
|
ackSkipped uint32
|
|
|
|
transmit uint32
|
|
|
|
}
|
|
|
|
|
2016-07-05 04:28:23 -04:00
|
|
|
func NewDataSegment() *DataSegment {
|
2016-07-14 15:42:00 -04:00
|
|
|
return new(DataSegment)
|
2016-07-05 04:28:23 -04:00
|
|
|
}
|
|
|
|
|
2016-06-27 16:22:01 -04:00
|
|
|
func (this *DataSegment) Bytes(b []byte) []byte {
|
|
|
|
b = serial.Uint16ToBytes(this.Conv, b)
|
2016-07-14 16:52:00 -04:00
|
|
|
b = append(b, byte(CommandData), byte(this.Option))
|
2016-06-27 16:22:01 -04:00
|
|
|
b = serial.Uint32ToBytes(this.Timestamp, b)
|
|
|
|
b = serial.Uint32ToBytes(this.Number, b)
|
2016-06-29 04:34:34 -04:00
|
|
|
b = serial.Uint32ToBytes(this.SendingNext, b)
|
2016-06-27 16:22:01 -04:00
|
|
|
b = serial.Uint16ToBytes(uint16(this.Data.Len()), b)
|
|
|
|
b = append(b, this.Data.Value...)
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *DataSegment) ByteSize() int {
|
2016-06-29 04:34:34 -04:00
|
|
|
return 2 + 1 + 1 + 4 + 4 + 4 + 2 + this.Data.Len()
|
2016-06-27 16:22:01 -04:00
|
|
|
}
|
|
|
|
|
2016-06-27 16:54:59 -04:00
|
|
|
func (this *DataSegment) Release() {
|
|
|
|
this.Data.Release()
|
2016-07-05 04:28:23 -04:00
|
|
|
this.Data = nil
|
2016-06-27 16:54:59 -04:00
|
|
|
}
|
|
|
|
|
2016-07-02 16:17:41 -04:00
|
|
|
type AckSegment struct {
|
2016-06-27 16:22:01 -04:00
|
|
|
Conv uint16
|
2016-07-14 16:52:00 -04:00
|
|
|
Option SegmentOption
|
2016-06-27 16:22:01 -04:00
|
|
|
ReceivingWindow uint32
|
2016-06-29 04:34:34 -04:00
|
|
|
ReceivingNext uint32
|
2016-08-05 14:59:33 -04:00
|
|
|
Timestamp uint32
|
2016-06-27 16:22:01 -04:00
|
|
|
Count byte
|
|
|
|
NumberList []uint32
|
|
|
|
}
|
|
|
|
|
2016-07-05 04:28:23 -04:00
|
|
|
func NewAckSegment() *AckSegment {
|
2016-07-14 15:42:00 -04:00
|
|
|
return new(AckSegment)
|
2016-07-05 04:28:23 -04:00
|
|
|
}
|
|
|
|
|
2016-08-05 14:59:33 -04:00
|
|
|
func (this *AckSegment) PutTimestamp(timestamp uint32) {
|
|
|
|
if timestamp-this.Timestamp < 0x7FFFFFFF {
|
|
|
|
this.Timestamp = timestamp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *AckSegment) PutNumber(number uint32) {
|
2016-07-05 04:33:11 -04:00
|
|
|
this.Count++
|
|
|
|
this.NumberList = append(this.NumberList, number)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *AckSegment) IsFull() bool {
|
|
|
|
return this.Count == 128
|
|
|
|
}
|
|
|
|
|
2016-07-02 16:17:41 -04:00
|
|
|
func (this *AckSegment) ByteSize() int {
|
2016-08-05 14:59:33 -04:00
|
|
|
return 2 + 1 + 1 + 4 + 4 + 4 + 1 + int(this.Count)*4
|
2016-06-27 16:22:01 -04:00
|
|
|
}
|
|
|
|
|
2016-07-02 16:17:41 -04:00
|
|
|
func (this *AckSegment) Bytes(b []byte) []byte {
|
2016-06-27 16:22:01 -04:00
|
|
|
b = serial.Uint16ToBytes(this.Conv, b)
|
2016-07-14 16:52:00 -04:00
|
|
|
b = append(b, byte(CommandACK), byte(this.Option))
|
2016-06-27 16:22:01 -04:00
|
|
|
b = serial.Uint32ToBytes(this.ReceivingWindow, b)
|
2016-06-29 04:34:34 -04:00
|
|
|
b = serial.Uint32ToBytes(this.ReceivingNext, b)
|
2016-08-05 14:59:33 -04:00
|
|
|
b = serial.Uint32ToBytes(this.Timestamp, b)
|
2016-06-27 16:22:01 -04:00
|
|
|
b = append(b, this.Count)
|
|
|
|
for i := byte(0); i < this.Count; i++ {
|
|
|
|
b = serial.Uint32ToBytes(this.NumberList[i], b)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2016-07-05 04:28:23 -04:00
|
|
|
func (this *AckSegment) Release() {
|
2016-07-14 15:42:00 -04:00
|
|
|
this.NumberList = nil
|
2016-07-05 04:28:23 -04:00
|
|
|
}
|
2016-06-27 16:54:59 -04:00
|
|
|
|
2016-06-29 04:34:34 -04:00
|
|
|
type CmdOnlySegment struct {
|
|
|
|
Conv uint16
|
2016-07-14 16:52:00 -04:00
|
|
|
Command Command
|
|
|
|
Option SegmentOption
|
2016-06-29 04:34:34 -04:00
|
|
|
SendingNext uint32
|
|
|
|
ReceivinNext uint32
|
2016-08-05 14:59:33 -04:00
|
|
|
PeerRTO uint32
|
2016-06-27 16:22:01 -04:00
|
|
|
}
|
|
|
|
|
2016-07-05 04:28:23 -04:00
|
|
|
func NewCmdOnlySegment() *CmdOnlySegment {
|
2016-07-14 15:42:00 -04:00
|
|
|
return new(CmdOnlySegment)
|
2016-07-05 04:28:23 -04:00
|
|
|
}
|
|
|
|
|
2016-06-29 04:34:34 -04:00
|
|
|
func (this *CmdOnlySegment) ByteSize() int {
|
2016-08-05 14:59:33 -04:00
|
|
|
return 2 + 1 + 1 + 4 + 4 + 4
|
2016-06-27 16:22:01 -04:00
|
|
|
}
|
|
|
|
|
2016-06-29 04:34:34 -04:00
|
|
|
func (this *CmdOnlySegment) Bytes(b []byte) []byte {
|
2016-06-27 16:22:01 -04:00
|
|
|
b = serial.Uint16ToBytes(this.Conv, b)
|
2016-07-14 16:52:00 -04:00
|
|
|
b = append(b, byte(this.Command), byte(this.Option))
|
2016-06-29 04:34:34 -04:00
|
|
|
b = serial.Uint32ToBytes(this.SendingNext, b)
|
|
|
|
b = serial.Uint32ToBytes(this.ReceivinNext, b)
|
2016-08-05 14:59:33 -04:00
|
|
|
b = serial.Uint32ToBytes(this.PeerRTO, b)
|
2016-06-27 16:22:01 -04:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2016-07-05 04:28:23 -04:00
|
|
|
func (this *CmdOnlySegment) Release() {
|
|
|
|
}
|
2016-06-27 16:54:59 -04:00
|
|
|
|
2016-07-04 08:17:42 -04:00
|
|
|
func ReadSegment(buf []byte) (Segment, []byte) {
|
2016-10-11 05:44:30 -04:00
|
|
|
if len(buf) < 4 {
|
2016-06-27 16:22:01 -04:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
conv := serial.BytesToUint16(buf)
|
|
|
|
buf = buf[2:]
|
|
|
|
|
2016-07-14 16:10:37 -04:00
|
|
|
cmd := Command(buf[0])
|
2016-06-27 16:22:01 -04:00
|
|
|
opt := SegmentOption(buf[1])
|
|
|
|
buf = buf[2:]
|
|
|
|
|
2016-07-14 16:10:37 -04:00
|
|
|
if cmd == CommandData {
|
2016-07-05 05:36:05 -04:00
|
|
|
seg := NewDataSegment()
|
|
|
|
seg.Conv = conv
|
2016-07-14 16:52:00 -04:00
|
|
|
seg.Option = opt
|
2016-06-29 18:12:36 -04:00
|
|
|
if len(buf) < 16 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-06-27 16:22:01 -04:00
|
|
|
seg.Timestamp = serial.BytesToUint32(buf)
|
|
|
|
buf = buf[4:]
|
|
|
|
|
|
|
|
seg.Number = serial.BytesToUint32(buf)
|
|
|
|
buf = buf[4:]
|
|
|
|
|
2016-06-29 04:34:34 -04:00
|
|
|
seg.SendingNext = serial.BytesToUint32(buf)
|
2016-06-27 16:22:01 -04:00
|
|
|
buf = buf[4:]
|
|
|
|
|
2016-06-29 18:12:36 -04:00
|
|
|
dataLen := int(serial.BytesToUint16(buf))
|
2016-06-27 16:22:01 -04:00
|
|
|
buf = buf[2:]
|
|
|
|
|
2016-06-29 18:12:36 -04:00
|
|
|
if len(buf) < dataLen {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-07-28 10:24:15 -04:00
|
|
|
seg.Data = AllocateBuffer().Clear().Append(buf[:dataLen])
|
2016-06-29 18:12:36 -04:00
|
|
|
buf = buf[dataLen:]
|
2016-06-27 16:22:01 -04:00
|
|
|
|
|
|
|
return seg, buf
|
|
|
|
}
|
|
|
|
|
2016-07-14 16:10:37 -04:00
|
|
|
if cmd == CommandACK {
|
2016-07-05 05:36:05 -04:00
|
|
|
seg := NewAckSegment()
|
|
|
|
seg.Conv = conv
|
2016-07-14 16:52:00 -04:00
|
|
|
seg.Option = opt
|
2016-08-05 14:59:33 -04:00
|
|
|
if len(buf) < 13 {
|
2016-06-29 18:12:36 -04:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-06-27 16:22:01 -04:00
|
|
|
seg.ReceivingWindow = serial.BytesToUint32(buf)
|
|
|
|
buf = buf[4:]
|
|
|
|
|
2016-06-29 04:34:34 -04:00
|
|
|
seg.ReceivingNext = serial.BytesToUint32(buf)
|
2016-06-27 16:22:01 -04:00
|
|
|
buf = buf[4:]
|
|
|
|
|
2016-08-05 14:59:33 -04:00
|
|
|
seg.Timestamp = serial.BytesToUint32(buf)
|
|
|
|
buf = buf[4:]
|
|
|
|
|
2016-07-14 15:42:00 -04:00
|
|
|
count := int(buf[0])
|
2016-06-27 16:22:01 -04:00
|
|
|
buf = buf[1:]
|
|
|
|
|
2016-08-05 14:59:33 -04:00
|
|
|
if len(buf) < count*4 {
|
2016-06-29 18:12:36 -04:00
|
|
|
return nil, nil
|
|
|
|
}
|
2016-07-14 15:42:00 -04:00
|
|
|
for i := 0; i < count; i++ {
|
2016-08-05 14:59:33 -04:00
|
|
|
seg.PutNumber(serial.BytesToUint32(buf))
|
|
|
|
buf = buf[4:]
|
2016-06-27 16:22:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return seg, buf
|
|
|
|
}
|
|
|
|
|
2016-07-05 05:36:05 -04:00
|
|
|
seg := NewCmdOnlySegment()
|
|
|
|
seg.Conv = conv
|
2016-07-14 16:52:00 -04:00
|
|
|
seg.Command = cmd
|
|
|
|
seg.Option = opt
|
2016-06-27 16:22:01 -04:00
|
|
|
|
2016-08-05 14:59:33 -04:00
|
|
|
if len(buf) < 12 {
|
2016-06-29 18:12:36 -04:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-06-29 04:34:34 -04:00
|
|
|
seg.SendingNext = serial.BytesToUint32(buf)
|
|
|
|
buf = buf[4:]
|
|
|
|
|
|
|
|
seg.ReceivinNext = serial.BytesToUint32(buf)
|
|
|
|
buf = buf[4:]
|
|
|
|
|
2016-08-05 14:59:33 -04:00
|
|
|
seg.PeerRTO = serial.BytesToUint32(buf)
|
|
|
|
buf = buf[4:]
|
|
|
|
|
2016-06-29 04:34:34 -04:00
|
|
|
return seg, buf
|
2016-06-27 16:22:01 -04:00
|
|
|
}
|