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

306 lines
5.9 KiB
Go
Raw Normal View History

2019-02-01 19:08:21 +00:00
// +build !confonly
2016-06-27 20:22:01 +00:00
package kcp
import (
2018-11-02 14:47:58 +00:00
"encoding/binary"
2016-12-09 10:35:27 +00:00
"v2ray.com/core/common/buf"
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
2018-11-02 20:34:04 +00:00
Serialize([]byte)
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 {
2018-07-29 01:54:24 +00:00
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
}
2018-11-02 14:47:58 +00:00
s.Timestamp = binary.BigEndian.Uint32(buf)
2018-02-25 22:00:04 +00:00
buf = buf[4:]
2018-11-02 14:47:58 +00:00
s.Number = binary.BigEndian.Uint32(buf)
2018-02-25 22:00:04 +00:00
buf = buf[4:]
2018-11-02 14:47:58 +00:00
s.SendingNext = binary.BigEndian.Uint32(buf)
2018-02-25 22:00:04 +00:00
buf = buf[4:]
2018-11-02 14:47:58 +00:00
dataLen := int(binary.BigEndian.Uint16(buf))
2018-02-25 22:00:04 +00:00
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-11-02 20:34:04 +00: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 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 {
2018-07-29 01:54:24 +00:00
return new(AckSegment)
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
}
2018-11-02 14:47:58 +00:00
s.ReceivingWindow = binary.BigEndian.Uint32(buf)
2018-02-25 22:00:04 +00:00
buf = buf[4:]
2018-11-02 14:47:58 +00:00
s.ReceivingNext = binary.BigEndian.Uint32(buf)
2018-02-25 22:00:04 +00:00
buf = buf[4:]
2018-11-02 14:47:58 +00:00
s.Timestamp = binary.BigEndian.Uint32(buf)
2018-02-25 22:00:04 +00: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 14:47:58 +00:00
s.PutNumber(binary.BigEndian.Uint32(buf))
2018-02-25 22:00:04 +00:00
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-11-02 20:34:04 +00: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 20:22:01 +00:00
}
}
2018-07-29 01:54:24 +00:00
func (s *AckSegment) Release() {}
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
}
2018-11-02 14:47:58 +00:00
s.SendingNext = binary.BigEndian.Uint32(buf)
2018-02-25 22:00:04 +00:00
buf = buf[4:]
2018-11-02 14:47:58 +00:00
s.ReceivingNext = binary.BigEndian.Uint32(buf)
2018-02-25 22:00:04 +00:00
buf = buf[4:]
2018-11-02 14:47:58 +00:00
s.PeerRTO = binary.BigEndian.Uint32(buf)
2018-02-25 22:00:04 +00:00
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-11-02 20:34:04 +00: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 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
}
2018-11-02 14:47:58 +00:00
conv := binary.BigEndian.Uint16(buf)
2016-06-27 20:22:01 +00:00
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
}