v2ray-core/transport/internet/kcp/segment.go

312 lines
6.1 KiB
Go
Raw Normal View History

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 (
2018-04-02 07:52:16 +00:00
// CommandACK indicates an AckSegment.
2016-12-08 15:32:53 +00:00
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
2018-04-02 18:00:50 +00:00
ByteSize() int32
2016-12-09 11:08:25 +00:00
Bytes() buf.Supplier
2018-02-25 22:00:04 +00:00
parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte)
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-06-27 20:22:01 +00:00
2017-12-03 21:53:00 +00:00
payload *buf.Buffer
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 {
return new(DataSegment)
2016-07-05 08:28:23 +00:00
}
2018-02-25 22:00:04 +00:00
func (s *DataSegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
s.Conv = conv
s.Option = opt
if len(buf) < 15 {
return false, nil
}
s.Timestamp = serial.BytesToUint32(buf)
buf = buf[4:]
s.Number = serial.BytesToUint32(buf)
buf = buf[4:]
s.SendingNext = serial.BytesToUint32(buf)
buf = buf[4:]
dataLen := int(serial.BytesToUint16(buf))
buf = buf[2:]
if len(buf) < dataLen {
return false, nil
}
s.Data().Clear()
2018-04-19 20:56:55 +00:00
s.Data().Write(buf[:dataLen])
2018-02-25 22:00:04 +00:00
buf = buf[dataLen:]
return true, buf
}
2018-01-17 16:36:14 +00:00
func (s *DataSegment) Conversation() uint16 {
return s.Conv
2016-11-27 07:58:31 +00:00
}
2018-01-17 16:36:14 +00:00
func (*DataSegment) Command() Command {
2016-12-08 15:27:41 +00:00
return CommandData
}
2018-01-17 16:36:14 +00:00
func (s *DataSegment) Detach() *buf.Buffer {
r := s.payload
s.payload = nil
2017-12-03 21:53:00 +00:00
return r
}
2018-01-17 16:36:14 +00:00
func (s *DataSegment) Data() *buf.Buffer {
if s.payload == nil {
s.payload = buf.New()
2016-11-01 11:07:20 +00:00
}
2018-01-17 16:36:14 +00:00
return s.payload
2016-12-06 10:03:42 +00:00
}
2018-01-17 16:36:14 +00:00
func (s *DataSegment) Bytes() buf.Supplier {
2016-12-09 11:08:25 +00:00
return func(b []byte) (int, error) {
2018-01-17 16:36:14 +00:00
b = serial.Uint16ToBytes(s.Conv, b[:0])
b = append(b, byte(CommandData), byte(s.Option))
b = serial.Uint32ToBytes(s.Timestamp, b)
b = serial.Uint32ToBytes(s.Number, b)
b = serial.Uint32ToBytes(s.SendingNext, b)
b = serial.Uint16ToBytes(uint16(s.payload.Len()), b)
b = append(b, s.payload.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
}
2018-04-02 18:00:50 +00:00
func (s *DataSegment) ByteSize() int32 {
2018-01-17 16:36:14 +00:00
return 2 + 1 + 1 + 4 + 4 + 4 + 2 + s.payload.Len()
2016-06-27 20:22:01 +00:00
}
2018-01-17 16:36:14 +00:00
func (s *DataSegment) Release() {
s.payload.Release()
s.payload = 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
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
}
2018-02-25 22:00:04 +00:00
func (s *AckSegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
s.Conv = conv
s.Option = opt
if len(buf) < 13 {
return false, nil
}
s.ReceivingWindow = serial.BytesToUint32(buf)
buf = buf[4:]
s.ReceivingNext = serial.BytesToUint32(buf)
buf = buf[4:]
s.Timestamp = serial.BytesToUint32(buf)
buf = buf[4:]
count := int(buf[0])
buf = buf[1:]
if len(buf) < count*4 {
return false, nil
}
for i := 0; i < count; i++ {
s.PutNumber(serial.BytesToUint32(buf))
buf = buf[4:]
}
return true, buf
}
2018-01-17 16:36:14 +00:00
func (s *AckSegment) Conversation() uint16 {
return s.Conv
2016-11-27 07:58:31 +00:00
}
2018-01-17 16:36:14 +00:00
func (*AckSegment) Command() Command {
2016-12-08 15:27:41 +00:00
return CommandACK
}
2018-01-17 16:36:14 +00:00
func (s *AckSegment) PutTimestamp(timestamp uint32) {
if timestamp-s.Timestamp < 0x7FFFFFFF {
s.Timestamp = timestamp
}
}
2018-01-17 16:36:14 +00:00
func (s *AckSegment) PutNumber(number uint32) {
s.NumberList = append(s.NumberList, number)
2016-07-05 08:33:11 +00:00
}
2018-01-17 16:36:14 +00:00
func (s *AckSegment) IsFull() bool {
return len(s.NumberList) == ackNumberLimit
2016-12-21 14:37:16 +00:00
}
2018-01-17 16:36:14 +00:00
func (s *AckSegment) IsEmpty() bool {
return len(s.NumberList) == 0
2016-07-05 08:33:11 +00:00
}
2018-04-02 18:00:50 +00:00
func (s *AckSegment) ByteSize() int32 {
return 2 + 1 + 1 + 4 + 4 + 4 + 1 + int32(len(s.NumberList)*4)
2016-06-27 20:22:01 +00:00
}
2018-01-17 16:36:14 +00:00
func (s *AckSegment) Bytes() buf.Supplier {
2016-12-09 11:08:25 +00:00
return func(b []byte) (int, error) {
2018-01-17 16:36:14 +00:00
b = serial.Uint16ToBytes(s.Conv, b[:0])
b = append(b, byte(CommandACK), byte(s.Option))
b = serial.Uint32ToBytes(s.ReceivingWindow, b)
b = serial.Uint32ToBytes(s.ReceivingNext, b)
b = serial.Uint32ToBytes(s.Timestamp, b)
count := byte(len(s.NumberList))
2016-12-21 14:37:16 +00:00
b = append(b, count)
2018-01-17 16:36:14 +00:00
for _, number := range s.NumberList {
2016-12-21 14:37:16 +00:00
b = serial.Uint32ToBytes(number, b)
2016-12-06 10:03:42 +00:00
}
2018-04-02 18:00:50 +00:00
return int(s.ByteSize()), nil
2016-06-27 20:22:01 +00:00
}
}
2018-01-17 16:36:14 +00:00
func (s *AckSegment) Release() {
s.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 {
2018-01-17 16:36:14 +00:00
Conv uint16
Cmd Command
Option SegmentOption
SendingNext uint32
ReceivingNext uint32
PeerRTO uint32
2016-06-27 20:22:01 +00:00
}
2016-07-05 08:28:23 +00:00
func NewCmdOnlySegment() *CmdOnlySegment {
return new(CmdOnlySegment)
2016-07-05 08:28:23 +00:00
}
2018-02-25 22:00:04 +00:00
func (s *CmdOnlySegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
s.Conv = conv
s.Cmd = cmd
s.Option = opt
if len(buf) < 12 {
return false, nil
}
s.SendingNext = serial.BytesToUint32(buf)
buf = buf[4:]
s.ReceivingNext = serial.BytesToUint32(buf)
buf = buf[4:]
s.PeerRTO = serial.BytesToUint32(buf)
buf = buf[4:]
return true, buf
}
2018-01-17 16:36:14 +00:00
func (s *CmdOnlySegment) Conversation() uint16 {
return s.Conv
2016-11-27 07:58:31 +00:00
}
2018-01-17 16:36:14 +00:00
func (s *CmdOnlySegment) Command() Command {
return s.Cmd
2016-12-08 15:27:41 +00:00
}
2018-04-02 18:00:50 +00:00
func (*CmdOnlySegment) ByteSize() int32 {
return 2 + 1 + 1 + 4 + 4 + 4
2016-06-27 20:22:01 +00:00
}
2018-01-17 16:36:14 +00:00
func (s *CmdOnlySegment) Bytes() buf.Supplier {
2016-12-09 11:08:25 +00:00
return func(b []byte) (int, error) {
2018-01-17 16:36:14 +00:00
b = serial.Uint16ToBytes(s.Conv, b[:0])
b = append(b, byte(s.Cmd), byte(s.Option))
b = serial.Uint32ToBytes(s.SendingNext, b)
b = serial.Uint32ToBytes(s.ReceivingNext, b)
b = serial.Uint32ToBytes(s.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
}
2018-01-17 16:36:14 +00:00
func (*CmdOnlySegment) Release() {}
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:]
2018-02-25 22:00:04 +00:00
var seg Segment
switch cmd {
case CommandData:
seg = NewDataSegment()
case CommandACK:
seg = NewAckSegment()
default:
seg = NewCmdOnlySegment()
2016-06-27 20:22:01 +00:00
}
2018-02-25 22:00:04 +00:00
valid, extra := seg.parse(conv, cmd, opt, buf)
if !valid {
2016-06-29 22:12:36 +00:00
return nil, nil
}
2018-02-25 22:00:04 +00:00
return seg, extra
2016-06-27 20:22:01 +00:00
}