1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-10-29 15:27:32 -04:00
v2fly/transport/internet/kcp/segment.go

312 lines
6.1 KiB
Go
Raw Normal View History

2016-06-27 16:22:01 -04:00
package kcp
import (
2016-12-09 05:35:27 -05:00
"v2ray.com/core/common/buf"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/serial"
2016-06-27 16:22:01 -04:00
)
2016-12-08 10:32:53 -05:00
// Command is a KCP command that indicate the purpose of a Segment.
2016-07-14 16:10:37 -04:00
type Command byte
2016-06-27 16:22:01 -04:00
const (
2018-04-02 03:52:16 -04:00
// CommandACK indicates an AckSegment.
2016-12-08 10:32:53 -05:00
CommandACK Command = 0
// CommandData indicates a DataSegment.
CommandData Command = 1
// CommandTerminate indicates that peer terminates the connection.
2016-07-14 16:10:37 -04:00
CommandTerminate Command = 2
2016-12-08 10:32:53 -05:00
// CommandPing indicates a ping.
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 {
2017-01-04 06:52:24 -05:00
Release()
2016-11-27 02:58:31 -05:00
Conversation() uint16
2016-12-08 10:27:41 -05:00
Command() Command
2018-04-02 14:00:50 -04:00
ByteSize() int32
2016-12-09 06:08:25 -05:00
Bytes() buf.Supplier
2018-02-25 17:00:04 -05:00
parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte)
2016-06-27 16:22:01 -04:00
}
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
2016-06-27 16:22:01 -04:00
2017-12-03 16:53:00 -05:00
payload *buf.Buffer
2016-10-11 07:17:57 -04:00
timeout uint32
transmit uint32
2016-06-27 16:22:01 -04:00
}
2016-07-05 04:28:23 -04:00
func NewDataSegment() *DataSegment {
return new(DataSegment)
2016-07-05 04:28:23 -04:00
}
2018-02-25 17:00:04 -05: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 16:56:55 -04:00
s.Data().Write(buf[:dataLen])
2018-02-25 17:00:04 -05:00
buf = buf[dataLen:]
return true, buf
}
2018-01-17 11:36:14 -05:00
func (s *DataSegment) Conversation() uint16 {
return s.Conv
2016-11-27 02:58:31 -05:00
}
2018-01-17 11:36:14 -05:00
func (*DataSegment) Command() Command {
2016-12-08 10:27:41 -05:00
return CommandData
}
2018-01-17 11:36:14 -05:00
func (s *DataSegment) Detach() *buf.Buffer {
r := s.payload
s.payload = nil
2017-12-03 16:53:00 -05:00
return r
}
2018-01-17 11:36:14 -05:00
func (s *DataSegment) Data() *buf.Buffer {
if s.payload == nil {
s.payload = buf.New()
2016-11-01 07:07:20 -04:00
}
2018-01-17 11:36:14 -05:00
return s.payload
2016-12-06 05:03:42 -05:00
}
2018-01-17 11:36:14 -05:00
func (s *DataSegment) Bytes() buf.Supplier {
2016-12-09 06:08:25 -05:00
return func(b []byte) (int, error) {
2018-01-17 11:36:14 -05: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 09:37:16 -05:00
return len(b), nil
2016-12-06 05:03:42 -05:00
}
2016-06-27 16:22:01 -04:00
}
2018-04-02 14:00:50 -04:00
func (s *DataSegment) ByteSize() int32 {
2018-01-17 11:36:14 -05:00
return 2 + 1 + 1 + 4 + 4 + 4 + 2 + s.payload.Len()
2016-06-27 16:22:01 -04:00
}
2018-01-17 11:36:14 -05:00
func (s *DataSegment) Release() {
s.payload.Release()
s.payload = 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
Timestamp uint32
2016-06-27 16:22:01 -04:00
NumberList []uint32
}
2016-11-30 16:24:06 -05:00
const ackNumberLimit = 128
2016-07-05 04:28:23 -04:00
func NewAckSegment() *AckSegment {
2016-11-30 16:24:06 -05:00
return &AckSegment{
NumberList: make([]uint32, 0, ackNumberLimit),
}
2016-07-05 04:28:23 -04:00
}
2018-02-25 17:00:04 -05: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 11:36:14 -05:00
func (s *AckSegment) Conversation() uint16 {
return s.Conv
2016-11-27 02:58:31 -05:00
}
2018-01-17 11:36:14 -05:00
func (*AckSegment) Command() Command {
2016-12-08 10:27:41 -05:00
return CommandACK
}
2018-01-17 11:36:14 -05:00
func (s *AckSegment) PutTimestamp(timestamp uint32) {
if timestamp-s.Timestamp < 0x7FFFFFFF {
s.Timestamp = timestamp
}
}
2018-01-17 11:36:14 -05:00
func (s *AckSegment) PutNumber(number uint32) {
s.NumberList = append(s.NumberList, number)
2016-07-05 04:33:11 -04:00
}
2018-01-17 11:36:14 -05:00
func (s *AckSegment) IsFull() bool {
return len(s.NumberList) == ackNumberLimit
2016-12-21 09:37:16 -05:00
}
2018-01-17 11:36:14 -05:00
func (s *AckSegment) IsEmpty() bool {
return len(s.NumberList) == 0
2016-07-05 04:33:11 -04:00
}
2018-04-02 14:00:50 -04:00
func (s *AckSegment) ByteSize() int32 {
return 2 + 1 + 1 + 4 + 4 + 4 + 1 + int32(len(s.NumberList)*4)
2016-06-27 16:22:01 -04:00
}
2018-01-17 11:36:14 -05:00
func (s *AckSegment) Bytes() buf.Supplier {
2016-12-09 06:08:25 -05:00
return func(b []byte) (int, error) {
2018-01-17 11:36:14 -05: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 09:37:16 -05:00
b = append(b, count)
2018-01-17 11:36:14 -05:00
for _, number := range s.NumberList {
2016-12-21 09:37:16 -05:00
b = serial.Uint32ToBytes(number, b)
2016-12-06 05:03:42 -05:00
}
2018-04-02 14:00:50 -04:00
return int(s.ByteSize()), nil
2016-06-27 16:22:01 -04:00
}
}
2018-01-17 11:36:14 -05:00
func (s *AckSegment) Release() {
s.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 {
2018-01-17 11:36:14 -05:00
Conv uint16
Cmd Command
Option SegmentOption
SendingNext uint32
ReceivingNext uint32
PeerRTO uint32
2016-06-27 16:22:01 -04:00
}
2016-07-05 04:28:23 -04:00
func NewCmdOnlySegment() *CmdOnlySegment {
return new(CmdOnlySegment)
2016-07-05 04:28:23 -04:00
}
2018-02-25 17:00:04 -05: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 11:36:14 -05:00
func (s *CmdOnlySegment) Conversation() uint16 {
return s.Conv
2016-11-27 02:58:31 -05:00
}
2018-01-17 11:36:14 -05:00
func (s *CmdOnlySegment) Command() Command {
return s.Cmd
2016-12-08 10:27:41 -05:00
}
2018-04-02 14:00:50 -04:00
func (*CmdOnlySegment) ByteSize() int32 {
return 2 + 1 + 1 + 4 + 4 + 4
2016-06-27 16:22:01 -04:00
}
2018-01-17 11:36:14 -05:00
func (s *CmdOnlySegment) Bytes() buf.Supplier {
2016-12-09 06:08:25 -05:00
return func(b []byte) (int, error) {
2018-01-17 11:36:14 -05: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 09:37:16 -05:00
return len(b), nil
2016-12-06 05:03:42 -05:00
}
2016-06-27 16:22:01 -04:00
}
2018-01-17 11:36:14 -05:00
func (*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:]
2018-02-25 17:00:04 -05:00
var seg Segment
switch cmd {
case CommandData:
seg = NewDataSegment()
case CommandACK:
seg = NewAckSegment()
default:
seg = NewCmdOnlySegment()
2016-06-27 16:22:01 -04:00
}
2018-02-25 17:00:04 -05:00
valid, extra := seg.parse(conv, cmd, opt, buf)
if !valid {
2016-06-29 18:12:36 -04:00
return nil, nil
}
2018-02-25 17:00:04 -05:00
return seg, extra
2016-06-27 16:22:01 -04:00
}