1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-17 02:34:30 -04:00
v2fly/transport/internet/kcp/segment.go

304 lines
5.9 KiB
Go
Raw Normal View History

2016-06-27 16:22:01 -04:00
package kcp
import (
2018-11-02 10:47:58 -04:00
"encoding/binary"
2016-12-09 05:35:27 -05:00
"v2ray.com/core/common/buf"
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
2018-11-02 16:34:04 -04:00
Serialize([]byte)
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 {
2018-07-28 21:54:24 -04:00
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
}
2018-11-02 10:47:58 -04:00
s.Timestamp = binary.BigEndian.Uint32(buf)
2018-02-25 17:00:04 -05:00
buf = buf[4:]
2018-11-02 10:47:58 -04:00
s.Number = binary.BigEndian.Uint32(buf)
2018-02-25 17:00:04 -05:00
buf = buf[4:]
2018-11-02 10:47:58 -04:00
s.SendingNext = binary.BigEndian.Uint32(buf)
2018-02-25 17:00:04 -05:00
buf = buf[4:]
2018-11-02 10:47:58 -04:00
dataLen := int(binary.BigEndian.Uint16(buf))
2018-02-25 17:00:04 -05:00
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-11-02 16:34:04 -04:00
func (s *DataSegment) Serialize(b []byte) {
binary.BigEndian.PutUint16(b, s.Conv)
b[2] = byte(CommandData)
b[3] = byte(s.Option)
binary.BigEndian.PutUint32(b[4:], s.Timestamp)
binary.BigEndian.PutUint32(b[8:], s.Number)
binary.BigEndian.PutUint32(b[12:], s.SendingNext)
binary.BigEndian.PutUint16(b[16:], uint16(s.payload.Len()))
copy(b[18:], s.payload.Bytes())
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 {
2018-07-28 21:54:24 -04:00
return new(AckSegment)
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
}
2018-11-02 10:47:58 -04:00
s.ReceivingWindow = binary.BigEndian.Uint32(buf)
2018-02-25 17:00:04 -05:00
buf = buf[4:]
2018-11-02 10:47:58 -04:00
s.ReceivingNext = binary.BigEndian.Uint32(buf)
2018-02-25 17:00:04 -05:00
buf = buf[4:]
2018-11-02 10:47:58 -04:00
s.Timestamp = binary.BigEndian.Uint32(buf)
2018-02-25 17:00:04 -05:00
buf = buf[4:]
count := int(buf[0])
buf = buf[1:]
if len(buf) < count*4 {
return false, nil
}
for i := 0; i < count; i++ {
2018-11-02 10:47:58 -04:00
s.PutNumber(binary.BigEndian.Uint32(buf))
2018-02-25 17:00:04 -05:00
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-11-02 16:34:04 -04:00
func (s *AckSegment) Serialize(b []byte) {
binary.BigEndian.PutUint16(b, s.Conv)
b[2] = byte(CommandACK)
b[3] = byte(s.Option)
binary.BigEndian.PutUint32(b[4:], s.ReceivingWindow)
binary.BigEndian.PutUint32(b[8:], s.ReceivingNext)
binary.BigEndian.PutUint32(b[12:], s.Timestamp)
b[16] = byte(len(s.NumberList))
n := 17
for _, number := range s.NumberList {
binary.BigEndian.PutUint32(b[n:], number)
n += 4
2016-06-27 16:22:01 -04:00
}
}
2018-07-28 21:54:24 -04:00
func (s *AckSegment) Release() {}
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
}
2018-11-02 10:47:58 -04:00
s.SendingNext = binary.BigEndian.Uint32(buf)
2018-02-25 17:00:04 -05:00
buf = buf[4:]
2018-11-02 10:47:58 -04:00
s.ReceivingNext = binary.BigEndian.Uint32(buf)
2018-02-25 17:00:04 -05:00
buf = buf[4:]
2018-11-02 10:47:58 -04:00
s.PeerRTO = binary.BigEndian.Uint32(buf)
2018-02-25 17:00:04 -05:00
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-11-02 16:34:04 -04:00
func (s *CmdOnlySegment) Serialize(b []byte) {
binary.BigEndian.PutUint16(b, s.Conv)
b[2] = byte(s.Cmd)
b[3] = byte(s.Option)
binary.BigEndian.PutUint32(b[4:], s.SendingNext)
binary.BigEndian.PutUint32(b[8:], s.ReceivingNext)
binary.BigEndian.PutUint32(b[12:], s.PeerRTO)
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
}
2018-11-02 10:47:58 -04:00
conv := binary.BigEndian.Uint16(buf)
2016-06-27 16:22:01 -04:00
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
}