2016-06-27 20:22:01 +00:00
|
|
|
package kcp
|
|
|
|
|
|
|
|
import (
|
2016-12-09 10:35:27 +00:00
|
|
|
"v2ray.com/core/common/buf"
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/common/serial"
|
2016-06-27 20:22:01 +00:00
|
|
|
)
|
|
|
|
|
2016-12-08 15:32:53 +00:00
|
|
|
// Command is a KCP command that indicate the purpose of a Segment.
|
2016-07-14 20:10:37 +00:00
|
|
|
type Command byte
|
2016-06-27 20:22:01 +00:00
|
|
|
|
|
|
|
const (
|
2016-12-08 15:32:53 +00:00
|
|
|
// CommandACK indicates a AckSegment.
|
|
|
|
CommandACK Command = 0
|
|
|
|
// CommandData indicates a DataSegment.
|
|
|
|
CommandData Command = 1
|
|
|
|
// CommandTerminate indicates that peer terminates the connection.
|
2016-07-14 20:10:37 +00:00
|
|
|
CommandTerminate Command = 2
|
2016-12-08 15:32:53 +00:00
|
|
|
// CommandPing indicates a ping.
|
|
|
|
CommandPing Command = 3
|
2016-06-27 20:22:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SegmentOption byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
SegmentOptionClose SegmentOption = 1
|
|
|
|
)
|
|
|
|
|
2016-07-04 12:17:42 +00:00
|
|
|
type Segment interface {
|
2017-01-04 11:52:24 +00:00
|
|
|
Release()
|
2016-11-27 07:58:31 +00:00
|
|
|
Conversation() uint16
|
2016-12-08 15:27:41 +00:00
|
|
|
Command() Command
|
2016-06-27 20:22:01 +00:00
|
|
|
ByteSize() int
|
2016-12-09 11:08:25 +00:00
|
|
|
Bytes() buf.Supplier
|
2016-06-27 20:22:01 +00:00
|
|
|
}
|
|
|
|
|
2016-06-29 10:52:23 +00:00
|
|
|
const (
|
|
|
|
DataSegmentOverhead = 18
|
|
|
|
)
|
|
|
|
|
2016-06-27 20:22:01 +00:00
|
|
|
type DataSegment struct {
|
2016-06-29 08:34:34 +00:00
|
|
|
Conv uint16
|
2016-07-14 20:52:00 +00:00
|
|
|
Option SegmentOption
|
2016-06-29 08:34:34 +00:00
|
|
|
Timestamp uint32
|
|
|
|
Number uint32
|
|
|
|
SendingNext uint32
|
2016-12-09 10:35:27 +00:00
|
|
|
Data *buf.Buffer
|
2016-06-27 20:22:01 +00:00
|
|
|
|
2016-10-11 11:17:57 +00:00
|
|
|
timeout uint32
|
|
|
|
transmit uint32
|
2016-06-27 20:22:01 +00:00
|
|
|
}
|
|
|
|
|
2016-07-05 08:28:23 +00:00
|
|
|
func NewDataSegment() *DataSegment {
|
2016-07-14 19:42:00 +00:00
|
|
|
return new(DataSegment)
|
2016-07-05 08:28:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *DataSegment) Conversation() uint16 {
|
|
|
|
return v.Conv
|
2016-11-27 07:58:31 +00:00
|
|
|
}
|
|
|
|
|
2016-12-08 15:27:41 +00:00
|
|
|
func (v *DataSegment) Command() Command {
|
|
|
|
return CommandData
|
|
|
|
}
|
|
|
|
|
2017-02-06 23:38:46 +00:00
|
|
|
func (v *DataSegment) SetData(data []byte) {
|
2016-11-27 20:39:09 +00:00
|
|
|
if v.Data == nil {
|
2017-04-15 19:19:21 +00:00
|
|
|
v.Data = buf.New()
|
2016-11-01 11:07:20 +00:00
|
|
|
}
|
2017-02-06 23:38:46 +00:00
|
|
|
v.Data.Reset(func(b []byte) (int, error) {
|
|
|
|
return copy(b, data), nil
|
|
|
|
})
|
2016-12-06 10:03:42 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 11:08:25 +00:00
|
|
|
func (v *DataSegment) Bytes() buf.Supplier {
|
|
|
|
return func(b []byte) (int, error) {
|
2016-12-06 10:03:42 +00:00
|
|
|
b = serial.Uint16ToBytes(v.Conv, b[:0])
|
|
|
|
b = append(b, byte(CommandData), byte(v.Option))
|
|
|
|
b = serial.Uint32ToBytes(v.Timestamp, b)
|
|
|
|
b = serial.Uint32ToBytes(v.Number, b)
|
|
|
|
b = serial.Uint32ToBytes(v.SendingNext, b)
|
|
|
|
b = serial.Uint16ToBytes(uint16(v.Data.Len()), b)
|
|
|
|
b = append(b, v.Data.Bytes()...)
|
2016-12-21 14:37:16 +00:00
|
|
|
return len(b), nil
|
2016-12-06 10:03:42 +00:00
|
|
|
}
|
2016-06-27 20:22:01 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *DataSegment) ByteSize() int {
|
|
|
|
return 2 + 1 + 1 + 4 + 4 + 4 + 2 + v.Data.Len()
|
2016-06-27 20:22:01 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *DataSegment) Release() {
|
|
|
|
v.Data.Release()
|
|
|
|
v.Data = nil
|
2016-06-27 20:54:59 +00:00
|
|
|
}
|
|
|
|
|
2016-07-02 20:17:41 +00:00
|
|
|
type AckSegment struct {
|
2016-06-27 20:22:01 +00:00
|
|
|
Conv uint16
|
2016-07-14 20:52:00 +00:00
|
|
|
Option SegmentOption
|
2016-06-27 20:22:01 +00:00
|
|
|
ReceivingWindow uint32
|
2016-06-29 08:34:34 +00:00
|
|
|
ReceivingNext uint32
|
2016-08-05 18:59:33 +00:00
|
|
|
Timestamp uint32
|
2016-06-27 20:22:01 +00:00
|
|
|
NumberList []uint32
|
|
|
|
}
|
|
|
|
|
2016-11-30 21:24:06 +00:00
|
|
|
const ackNumberLimit = 128
|
|
|
|
|
2016-07-05 08:28:23 +00:00
|
|
|
func NewAckSegment() *AckSegment {
|
2016-11-30 21:24:06 +00:00
|
|
|
return &AckSegment{
|
|
|
|
NumberList: make([]uint32, 0, ackNumberLimit),
|
|
|
|
}
|
2016-07-05 08:28:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *AckSegment) Conversation() uint16 {
|
|
|
|
return v.Conv
|
2016-11-27 07:58:31 +00:00
|
|
|
}
|
|
|
|
|
2016-12-08 15:27:41 +00:00
|
|
|
func (v *AckSegment) Command() Command {
|
|
|
|
return CommandACK
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *AckSegment) PutTimestamp(timestamp uint32) {
|
|
|
|
if timestamp-v.Timestamp < 0x7FFFFFFF {
|
|
|
|
v.Timestamp = timestamp
|
2016-08-05 18:59:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *AckSegment) PutNumber(number uint32) {
|
|
|
|
v.NumberList = append(v.NumberList, number)
|
2016-07-05 08:33:11 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *AckSegment) IsFull() bool {
|
2016-12-21 14:37:16 +00:00
|
|
|
return len(v.NumberList) == ackNumberLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *AckSegment) IsEmpty() bool {
|
|
|
|
return len(v.NumberList) == 0
|
2016-07-05 08:33:11 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *AckSegment) ByteSize() int {
|
2016-12-21 14:37:16 +00:00
|
|
|
return 2 + 1 + 1 + 4 + 4 + 4 + 1 + len(v.NumberList)*4
|
2016-06-27 20:22:01 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 11:08:25 +00:00
|
|
|
func (v *AckSegment) Bytes() buf.Supplier {
|
|
|
|
return func(b []byte) (int, error) {
|
2016-12-06 10:03:42 +00:00
|
|
|
b = serial.Uint16ToBytes(v.Conv, b[:0])
|
|
|
|
b = append(b, byte(CommandACK), byte(v.Option))
|
|
|
|
b = serial.Uint32ToBytes(v.ReceivingWindow, b)
|
|
|
|
b = serial.Uint32ToBytes(v.ReceivingNext, b)
|
|
|
|
b = serial.Uint32ToBytes(v.Timestamp, b)
|
2016-12-21 14:37:16 +00:00
|
|
|
count := byte(len(v.NumberList))
|
|
|
|
b = append(b, count)
|
|
|
|
for _, number := range v.NumberList {
|
|
|
|
b = serial.Uint32ToBytes(number, b)
|
2016-12-06 10:03:42 +00:00
|
|
|
}
|
2016-12-09 11:08:25 +00:00
|
|
|
return v.ByteSize(), nil
|
2016-06-27 20:22:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *AckSegment) Release() {
|
|
|
|
v.NumberList = nil
|
2016-07-05 08:28:23 +00:00
|
|
|
}
|
2016-06-27 20:54:59 +00:00
|
|
|
|
2016-06-29 08:34:34 +00:00
|
|
|
type CmdOnlySegment struct {
|
|
|
|
Conv uint16
|
2016-12-08 15:27:41 +00:00
|
|
|
Cmd Command
|
2016-07-14 20:52:00 +00:00
|
|
|
Option SegmentOption
|
2016-06-29 08:34:34 +00:00
|
|
|
SendingNext uint32
|
|
|
|
ReceivinNext uint32
|
2016-08-05 18:59:33 +00:00
|
|
|
PeerRTO uint32
|
2016-06-27 20:22:01 +00:00
|
|
|
}
|
|
|
|
|
2016-07-05 08:28:23 +00:00
|
|
|
func NewCmdOnlySegment() *CmdOnlySegment {
|
2016-07-14 19:42:00 +00:00
|
|
|
return new(CmdOnlySegment)
|
2016-07-05 08:28:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *CmdOnlySegment) Conversation() uint16 {
|
|
|
|
return v.Conv
|
2016-11-27 07:58:31 +00:00
|
|
|
}
|
|
|
|
|
2016-12-08 15:27:41 +00:00
|
|
|
func (v *CmdOnlySegment) Command() Command {
|
|
|
|
return v.Cmd
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *CmdOnlySegment) ByteSize() int {
|
2016-08-05 18:59:33 +00:00
|
|
|
return 2 + 1 + 1 + 4 + 4 + 4
|
2016-06-27 20:22:01 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 11:08:25 +00:00
|
|
|
func (v *CmdOnlySegment) Bytes() buf.Supplier {
|
|
|
|
return func(b []byte) (int, error) {
|
2016-12-06 10:03:42 +00:00
|
|
|
b = serial.Uint16ToBytes(v.Conv, b[:0])
|
2016-12-08 15:27:41 +00:00
|
|
|
b = append(b, byte(v.Cmd), byte(v.Option))
|
2016-12-06 10:03:42 +00:00
|
|
|
b = serial.Uint32ToBytes(v.SendingNext, b)
|
|
|
|
b = serial.Uint32ToBytes(v.ReceivinNext, b)
|
|
|
|
b = serial.Uint32ToBytes(v.PeerRTO, b)
|
2016-12-21 14:37:16 +00:00
|
|
|
return len(b), nil
|
2016-12-06 10:03:42 +00:00
|
|
|
}
|
2016-06-27 20:22:01 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *CmdOnlySegment) Release() {
|
2016-07-05 08:28:23 +00:00
|
|
|
}
|
2016-06-27 20:54:59 +00:00
|
|
|
|
2016-07-04 12:17:42 +00:00
|
|
|
func ReadSegment(buf []byte) (Segment, []byte) {
|
2016-10-11 09:44:30 +00:00
|
|
|
if len(buf) < 4 {
|
2016-06-27 20:22:01 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
conv := serial.BytesToUint16(buf)
|
|
|
|
buf = buf[2:]
|
|
|
|
|
2016-07-14 20:10:37 +00:00
|
|
|
cmd := Command(buf[0])
|
2016-06-27 20:22:01 +00:00
|
|
|
opt := SegmentOption(buf[1])
|
|
|
|
buf = buf[2:]
|
|
|
|
|
2016-07-14 20:10:37 +00:00
|
|
|
if cmd == CommandData {
|
2016-07-05 09:36:05 +00:00
|
|
|
seg := NewDataSegment()
|
|
|
|
seg.Conv = conv
|
2016-07-14 20:52:00 +00:00
|
|
|
seg.Option = opt
|
2017-01-04 16:23:41 +00:00
|
|
|
if len(buf) < 15 {
|
2016-06-29 22:12:36 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2016-06-27 20:22:01 +00:00
|
|
|
seg.Timestamp = serial.BytesToUint32(buf)
|
|
|
|
buf = buf[4:]
|
|
|
|
|
|
|
|
seg.Number = serial.BytesToUint32(buf)
|
|
|
|
buf = buf[4:]
|
|
|
|
|
2016-06-29 08:34:34 +00:00
|
|
|
seg.SendingNext = serial.BytesToUint32(buf)
|
2016-06-27 20:22:01 +00:00
|
|
|
buf = buf[4:]
|
|
|
|
|
2016-06-29 22:12:36 +00:00
|
|
|
dataLen := int(serial.BytesToUint16(buf))
|
2016-06-27 20:22:01 +00:00
|
|
|
buf = buf[2:]
|
|
|
|
|
2016-06-29 22:12:36 +00:00
|
|
|
if len(buf) < dataLen {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2016-11-01 11:07:20 +00:00
|
|
|
seg.SetData(buf[:dataLen])
|
2016-06-29 22:12:36 +00:00
|
|
|
buf = buf[dataLen:]
|
2016-06-27 20:22:01 +00:00
|
|
|
|
|
|
|
return seg, buf
|
|
|
|
}
|
|
|
|
|
2016-07-14 20:10:37 +00:00
|
|
|
if cmd == CommandACK {
|
2016-07-05 09:36:05 +00:00
|
|
|
seg := NewAckSegment()
|
|
|
|
seg.Conv = conv
|
2016-07-14 20:52:00 +00:00
|
|
|
seg.Option = opt
|
2016-08-05 18:59:33 +00:00
|
|
|
if len(buf) < 13 {
|
2016-06-29 22:12:36 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-06-27 20:22:01 +00:00
|
|
|
seg.ReceivingWindow = serial.BytesToUint32(buf)
|
|
|
|
buf = buf[4:]
|
|
|
|
|
2016-06-29 08:34:34 +00:00
|
|
|
seg.ReceivingNext = serial.BytesToUint32(buf)
|
2016-06-27 20:22:01 +00:00
|
|
|
buf = buf[4:]
|
|
|
|
|
2016-08-05 18:59:33 +00:00
|
|
|
seg.Timestamp = serial.BytesToUint32(buf)
|
|
|
|
buf = buf[4:]
|
|
|
|
|
2016-07-14 19:42:00 +00:00
|
|
|
count := int(buf[0])
|
2016-06-27 20:22:01 +00:00
|
|
|
buf = buf[1:]
|
|
|
|
|
2016-08-05 18:59:33 +00:00
|
|
|
if len(buf) < count*4 {
|
2016-06-29 22:12:36 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2016-07-14 19:42:00 +00:00
|
|
|
for i := 0; i < count; i++ {
|
2016-08-05 18:59:33 +00:00
|
|
|
seg.PutNumber(serial.BytesToUint32(buf))
|
|
|
|
buf = buf[4:]
|
2016-06-27 20:22:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return seg, buf
|
|
|
|
}
|
|
|
|
|
2016-07-05 09:36:05 +00:00
|
|
|
seg := NewCmdOnlySegment()
|
|
|
|
seg.Conv = conv
|
2016-12-08 15:27:41 +00:00
|
|
|
seg.Cmd = cmd
|
2016-07-14 20:52:00 +00:00
|
|
|
seg.Option = opt
|
2016-06-27 20:22:01 +00:00
|
|
|
|
2016-08-05 18:59:33 +00:00
|
|
|
if len(buf) < 12 {
|
2016-06-29 22:12:36 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-06-29 08:34:34 +00:00
|
|
|
seg.SendingNext = serial.BytesToUint32(buf)
|
|
|
|
buf = buf[4:]
|
|
|
|
|
|
|
|
seg.ReceivinNext = serial.BytesToUint32(buf)
|
|
|
|
buf = buf[4:]
|
|
|
|
|
2016-08-05 18:59:33 +00:00
|
|
|
seg.PeerRTO = serial.BytesToUint32(buf)
|
|
|
|
buf = buf[4:]
|
|
|
|
|
2016-06-29 08:34:34 +00:00
|
|
|
return seg, buf
|
2016-06-27 20:22:01 +00:00
|
|
|
}
|