mirror of
https://github.com/golang/net.git
synced 2026-03-31 18:37:08 +09:00
quic, internal/quic/quicwire: split wire encode/decode functions to new package
HTTP/3 also uses QUIC varints. Move the more general-purpose wire encoding/decoding functions into a new internal/quic/quicwire package so they can be shared. For golang/go#70914 Change-Id: Id888baf131e90a12247e15a6f7bc7dc37c6dc572 Reviewed-on: https://go-review.googlesource.com/c/net/+/641496 Auto-Submit: Damien Neil <dneil@google.com> Reviewed-by: Jonathan Amsterdam <jba@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
committed by
Gopher Robot
parent
0a5dcdd858
commit
35e1007cf8
@@ -4,20 +4,22 @@
|
||||
|
||||
//go:build go1.21
|
||||
|
||||
package quic
|
||||
// Package quicwire encodes and decode QUIC/HTTP3 wire encoding types,
|
||||
// particularly variable-length integers.
|
||||
package quicwire
|
||||
|
||||
import "encoding/binary"
|
||||
|
||||
const (
|
||||
maxVarintSize = 8 // encoded size in bytes
|
||||
maxVarint = (1 << 62) - 1
|
||||
MaxVarintSize = 8 // encoded size in bytes
|
||||
MaxVarint = (1 << 62) - 1
|
||||
)
|
||||
|
||||
// consumeVarint parses a variable-length integer, reporting its length.
|
||||
// ConsumeVarint parses a variable-length integer, reporting its length.
|
||||
// It returns a negative length upon an error.
|
||||
//
|
||||
// https://www.rfc-editor.org/rfc/rfc9000.html#section-16
|
||||
func consumeVarint(b []byte) (v uint64, n int) {
|
||||
func ConsumeVarint(b []byte) (v uint64, n int) {
|
||||
if len(b) < 1 {
|
||||
return 0, -1
|
||||
}
|
||||
@@ -45,16 +47,16 @@ func consumeVarint(b []byte) (v uint64, n int) {
|
||||
}
|
||||
|
||||
// consumeVarintInt64 parses a variable-length integer as an int64.
|
||||
func consumeVarintInt64(b []byte) (v int64, n int) {
|
||||
u, n := consumeVarint(b)
|
||||
func ConsumeVarintInt64(b []byte) (v int64, n int) {
|
||||
u, n := ConsumeVarint(b)
|
||||
// QUIC varints are 62-bits large, so this conversion can never overflow.
|
||||
return int64(u), n
|
||||
}
|
||||
|
||||
// appendVarint appends a variable-length integer to b.
|
||||
// AppendVarint appends a variable-length integer to b.
|
||||
//
|
||||
// https://www.rfc-editor.org/rfc/rfc9000.html#section-16
|
||||
func appendVarint(b []byte, v uint64) []byte {
|
||||
func AppendVarint(b []byte, v uint64) []byte {
|
||||
switch {
|
||||
case v <= 63:
|
||||
return append(b, byte(v))
|
||||
@@ -69,8 +71,8 @@ func appendVarint(b []byte, v uint64) []byte {
|
||||
}
|
||||
}
|
||||
|
||||
// sizeVarint returns the size of the variable-length integer encoding of f.
|
||||
func sizeVarint(v uint64) int {
|
||||
// SizeVarint returns the size of the variable-length integer encoding of f.
|
||||
func SizeVarint(v uint64) int {
|
||||
switch {
|
||||
case v <= 63:
|
||||
return 1
|
||||
@@ -85,28 +87,28 @@ func sizeVarint(v uint64) int {
|
||||
}
|
||||
}
|
||||
|
||||
// consumeUint32 parses a 32-bit fixed-length, big-endian integer, reporting its length.
|
||||
// ConsumeUint32 parses a 32-bit fixed-length, big-endian integer, reporting its length.
|
||||
// It returns a negative length upon an error.
|
||||
func consumeUint32(b []byte) (uint32, int) {
|
||||
func ConsumeUint32(b []byte) (uint32, int) {
|
||||
if len(b) < 4 {
|
||||
return 0, -1
|
||||
}
|
||||
return binary.BigEndian.Uint32(b), 4
|
||||
}
|
||||
|
||||
// consumeUint64 parses a 64-bit fixed-length, big-endian integer, reporting its length.
|
||||
// ConsumeUint64 parses a 64-bit fixed-length, big-endian integer, reporting its length.
|
||||
// It returns a negative length upon an error.
|
||||
func consumeUint64(b []byte) (uint64, int) {
|
||||
func ConsumeUint64(b []byte) (uint64, int) {
|
||||
if len(b) < 8 {
|
||||
return 0, -1
|
||||
}
|
||||
return binary.BigEndian.Uint64(b), 8
|
||||
}
|
||||
|
||||
// consumeUint8Bytes parses a sequence of bytes prefixed with an 8-bit length,
|
||||
// ConsumeUint8Bytes parses a sequence of bytes prefixed with an 8-bit length,
|
||||
// reporting the total number of bytes consumed.
|
||||
// It returns a negative length upon an error.
|
||||
func consumeUint8Bytes(b []byte) ([]byte, int) {
|
||||
func ConsumeUint8Bytes(b []byte) ([]byte, int) {
|
||||
if len(b) < 1 {
|
||||
return nil, -1
|
||||
}
|
||||
@@ -118,8 +120,8 @@ func consumeUint8Bytes(b []byte) ([]byte, int) {
|
||||
return b[n:][:size], size + n
|
||||
}
|
||||
|
||||
// appendUint8Bytes appends a sequence of bytes prefixed by an 8-bit length.
|
||||
func appendUint8Bytes(b, v []byte) []byte {
|
||||
// AppendUint8Bytes appends a sequence of bytes prefixed by an 8-bit length.
|
||||
func AppendUint8Bytes(b, v []byte) []byte {
|
||||
if len(v) > 0xff {
|
||||
panic("uint8-prefixed bytes too large")
|
||||
}
|
||||
@@ -128,11 +130,11 @@ func appendUint8Bytes(b, v []byte) []byte {
|
||||
return b
|
||||
}
|
||||
|
||||
// consumeVarintBytes parses a sequence of bytes preceded by a variable-length integer length,
|
||||
// ConsumeVarintBytes parses a sequence of bytes preceded by a variable-length integer length,
|
||||
// reporting the total number of bytes consumed.
|
||||
// It returns a negative length upon an error.
|
||||
func consumeVarintBytes(b []byte) ([]byte, int) {
|
||||
size, n := consumeVarint(b)
|
||||
func ConsumeVarintBytes(b []byte) ([]byte, int) {
|
||||
size, n := ConsumeVarint(b)
|
||||
if n < 0 {
|
||||
return nil, -1
|
||||
}
|
||||
@@ -142,9 +144,9 @@ func consumeVarintBytes(b []byte) ([]byte, int) {
|
||||
return b[n:][:size], int(size) + n
|
||||
}
|
||||
|
||||
// appendVarintBytes appends a sequence of bytes prefixed by a variable-length integer length.
|
||||
func appendVarintBytes(b, v []byte) []byte {
|
||||
b = appendVarint(b, uint64(len(v)))
|
||||
// AppendVarintBytes appends a sequence of bytes prefixed by a variable-length integer length.
|
||||
func AppendVarintBytes(b, v []byte) []byte {
|
||||
b = AppendVarint(b, uint64(len(v)))
|
||||
b = append(b, v...)
|
||||
return b
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
//go:build go1.21
|
||||
|
||||
package quic
|
||||
package quicwire
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -32,22 +32,22 @@ func TestConsumeVarint(t *testing.T) {
|
||||
{[]byte{0x25}, 37, 1},
|
||||
{[]byte{0x40, 0x25}, 37, 2},
|
||||
} {
|
||||
got, gotLen := consumeVarint(test.b)
|
||||
got, gotLen := ConsumeVarint(test.b)
|
||||
if got != test.want || gotLen != test.wantLen {
|
||||
t.Errorf("consumeVarint(%x) = %v, %v; want %v, %v", test.b, got, gotLen, test.want, test.wantLen)
|
||||
t.Errorf("ConsumeVarint(%x) = %v, %v; want %v, %v", test.b, got, gotLen, test.want, test.wantLen)
|
||||
}
|
||||
// Extra data in the buffer is ignored.
|
||||
b := append(test.b, 0)
|
||||
got, gotLen = consumeVarint(b)
|
||||
got, gotLen = ConsumeVarint(b)
|
||||
if got != test.want || gotLen != test.wantLen {
|
||||
t.Errorf("consumeVarint(%x) = %v, %v; want %v, %v", b, got, gotLen, test.want, test.wantLen)
|
||||
t.Errorf("ConsumeVarint(%x) = %v, %v; want %v, %v", b, got, gotLen, test.want, test.wantLen)
|
||||
}
|
||||
// Short buffer results in an error.
|
||||
for i := 1; i <= len(test.b); i++ {
|
||||
b = test.b[:len(test.b)-i]
|
||||
got, gotLen = consumeVarint(b)
|
||||
got, gotLen = ConsumeVarint(b)
|
||||
if got != 0 || gotLen >= 0 {
|
||||
t.Errorf("consumeVarint(%x) = %v, %v; want 0, -1", b, got, gotLen)
|
||||
t.Errorf("ConsumeVarint(%x) = %v, %v; want 0, -1", b, got, gotLen)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -69,11 +69,11 @@ func TestAppendVarint(t *testing.T) {
|
||||
{15293, []byte{0x7b, 0xbd}},
|
||||
{37, []byte{0x25}},
|
||||
} {
|
||||
got := appendVarint([]byte{}, test.v)
|
||||
got := AppendVarint([]byte{}, test.v)
|
||||
if !bytes.Equal(got, test.want) {
|
||||
t.Errorf("AppendVarint(nil, %v) = %x, want %x", test.v, got, test.want)
|
||||
}
|
||||
if gotLen, wantLen := sizeVarint(test.v), len(got); gotLen != wantLen {
|
||||
if gotLen, wantLen := SizeVarint(test.v), len(got); gotLen != wantLen {
|
||||
t.Errorf("SizeVarint(%v) = %v, want %v", test.v, gotLen, wantLen)
|
||||
}
|
||||
}
|
||||
@@ -88,8 +88,8 @@ func TestConsumeUint32(t *testing.T) {
|
||||
{[]byte{0x01, 0x02, 0x03, 0x04}, 0x01020304, 4},
|
||||
{[]byte{0x01, 0x02, 0x03}, 0, -1},
|
||||
} {
|
||||
if got, n := consumeUint32(test.b); got != test.want || n != test.wantLen {
|
||||
t.Errorf("consumeUint32(%x) = %v, %v; want %v, %v", test.b, got, n, test.want, test.wantLen)
|
||||
if got, n := ConsumeUint32(test.b); got != test.want || n != test.wantLen {
|
||||
t.Errorf("ConsumeUint32(%x) = %v, %v; want %v, %v", test.b, got, n, test.want, test.wantLen)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,8 +103,8 @@ func TestConsumeUint64(t *testing.T) {
|
||||
{[]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, 0x0102030405060708, 8},
|
||||
{[]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 0, -1},
|
||||
} {
|
||||
if got, n := consumeUint64(test.b); got != test.want || n != test.wantLen {
|
||||
t.Errorf("consumeUint32(%x) = %v, %v; want %v, %v", test.b, got, n, test.want, test.wantLen)
|
||||
if got, n := ConsumeUint64(test.b); got != test.want || n != test.wantLen {
|
||||
t.Errorf("ConsumeUint32(%x) = %v, %v; want %v, %v", test.b, got, n, test.want, test.wantLen)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -120,22 +120,22 @@ func TestConsumeVarintBytes(t *testing.T) {
|
||||
{[]byte{0x04, 0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}, 5},
|
||||
{[]byte{0x40, 0x04, 0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}, 6},
|
||||
} {
|
||||
got, gotLen := consumeVarintBytes(test.b)
|
||||
got, gotLen := ConsumeVarintBytes(test.b)
|
||||
if !bytes.Equal(got, test.want) || gotLen != test.wantLen {
|
||||
t.Errorf("consumeVarintBytes(%x) = {%x}, %v; want {%x}, %v", test.b, got, gotLen, test.want, test.wantLen)
|
||||
t.Errorf("ConsumeVarintBytes(%x) = {%x}, %v; want {%x}, %v", test.b, got, gotLen, test.want, test.wantLen)
|
||||
}
|
||||
// Extra data in the buffer is ignored.
|
||||
b := append(test.b, 0)
|
||||
got, gotLen = consumeVarintBytes(b)
|
||||
got, gotLen = ConsumeVarintBytes(b)
|
||||
if !bytes.Equal(got, test.want) || gotLen != test.wantLen {
|
||||
t.Errorf("consumeVarintBytes(%x) = {%x}, %v; want {%x}, %v", b, got, gotLen, test.want, test.wantLen)
|
||||
t.Errorf("ConsumeVarintBytes(%x) = {%x}, %v; want {%x}, %v", b, got, gotLen, test.want, test.wantLen)
|
||||
}
|
||||
// Short buffer results in an error.
|
||||
for i := 1; i <= len(test.b); i++ {
|
||||
b = test.b[:len(test.b)-i]
|
||||
got, gotLen := consumeVarintBytes(b)
|
||||
got, gotLen := ConsumeVarintBytes(b)
|
||||
if len(got) > 0 || gotLen > 0 {
|
||||
t.Errorf("consumeVarintBytes(%x) = {%x}, %v; want {}, -1", b, got, gotLen)
|
||||
t.Errorf("ConsumeVarintBytes(%x) = {%x}, %v; want {}, -1", b, got, gotLen)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,9 +147,9 @@ func TestConsumeVarintBytesErrors(t *testing.T) {
|
||||
{0x01},
|
||||
{0x40, 0x01},
|
||||
} {
|
||||
got, gotLen := consumeVarintBytes(b)
|
||||
got, gotLen := ConsumeVarintBytes(b)
|
||||
if len(got) > 0 || gotLen > 0 {
|
||||
t.Errorf("consumeVarintBytes(%x) = {%x}, %v; want {}, -1", b, got, gotLen)
|
||||
t.Errorf("ConsumeVarintBytes(%x) = {%x}, %v; want {}, -1", b, got, gotLen)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -164,22 +164,22 @@ func TestConsumeUint8Bytes(t *testing.T) {
|
||||
{[]byte{0x01, 0x00}, []byte{0x00}, 2},
|
||||
{[]byte{0x04, 0x01, 0x02, 0x03, 0x04}, []byte{0x01, 0x02, 0x03, 0x04}, 5},
|
||||
} {
|
||||
got, gotLen := consumeUint8Bytes(test.b)
|
||||
got, gotLen := ConsumeUint8Bytes(test.b)
|
||||
if !bytes.Equal(got, test.want) || gotLen != test.wantLen {
|
||||
t.Errorf("consumeUint8Bytes(%x) = {%x}, %v; want {%x}, %v", test.b, got, gotLen, test.want, test.wantLen)
|
||||
t.Errorf("ConsumeUint8Bytes(%x) = {%x}, %v; want {%x}, %v", test.b, got, gotLen, test.want, test.wantLen)
|
||||
}
|
||||
// Extra data in the buffer is ignored.
|
||||
b := append(test.b, 0)
|
||||
got, gotLen = consumeUint8Bytes(b)
|
||||
got, gotLen = ConsumeUint8Bytes(b)
|
||||
if !bytes.Equal(got, test.want) || gotLen != test.wantLen {
|
||||
t.Errorf("consumeUint8Bytes(%x) = {%x}, %v; want {%x}, %v", b, got, gotLen, test.want, test.wantLen)
|
||||
t.Errorf("ConsumeUint8Bytes(%x) = {%x}, %v; want {%x}, %v", b, got, gotLen, test.want, test.wantLen)
|
||||
}
|
||||
// Short buffer results in an error.
|
||||
for i := 1; i <= len(test.b); i++ {
|
||||
b = test.b[:len(test.b)-i]
|
||||
got, gotLen := consumeUint8Bytes(b)
|
||||
got, gotLen := ConsumeUint8Bytes(b)
|
||||
if len(got) > 0 || gotLen > 0 {
|
||||
t.Errorf("consumeUint8Bytes(%x) = {%x}, %v; want {}, -1", b, got, gotLen)
|
||||
t.Errorf("ConsumeUint8Bytes(%x) = {%x}, %v; want {}, -1", b, got, gotLen)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -191,35 +191,35 @@ func TestConsumeUint8BytesErrors(t *testing.T) {
|
||||
{0x01},
|
||||
{0x04, 0x01, 0x02, 0x03},
|
||||
} {
|
||||
got, gotLen := consumeUint8Bytes(b)
|
||||
got, gotLen := ConsumeUint8Bytes(b)
|
||||
if len(got) > 0 || gotLen > 0 {
|
||||
t.Errorf("consumeUint8Bytes(%x) = {%x}, %v; want {}, -1", b, got, gotLen)
|
||||
t.Errorf("ConsumeUint8Bytes(%x) = {%x}, %v; want {}, -1", b, got, gotLen)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendUint8Bytes(t *testing.T) {
|
||||
var got []byte
|
||||
got = appendUint8Bytes(got, []byte{})
|
||||
got = appendUint8Bytes(got, []byte{0xaa, 0xbb})
|
||||
got = AppendUint8Bytes(got, []byte{})
|
||||
got = AppendUint8Bytes(got, []byte{0xaa, 0xbb})
|
||||
want := []byte{
|
||||
0x00,
|
||||
0x02, 0xaa, 0xbb,
|
||||
}
|
||||
if !bytes.Equal(got, want) {
|
||||
t.Errorf("appendUint8Bytes {}, {aabb} = {%x}; want {%x}", got, want)
|
||||
t.Errorf("AppendUint8Bytes {}, {aabb} = {%x}; want {%x}", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendVarintBytes(t *testing.T) {
|
||||
var got []byte
|
||||
got = appendVarintBytes(got, []byte{})
|
||||
got = appendVarintBytes(got, []byte{0xaa, 0xbb})
|
||||
got = AppendVarintBytes(got, []byte{})
|
||||
got = AppendVarintBytes(got, []byte{0xaa, 0xbb})
|
||||
want := []byte{
|
||||
0x00,
|
||||
0x02, 0xaa, 0xbb,
|
||||
}
|
||||
if !bytes.Equal(got, want) {
|
||||
t.Errorf("appendVarintBytes {}, {aabb} = {%x}; want {%x}", got, want)
|
||||
t.Errorf("AppendVarintBytes {}, {aabb} = {%x}; want {%x}", got, want)
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
"log/slog"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/internal/quic/quicwire"
|
||||
)
|
||||
|
||||
// A Config structure configures a QUIC endpoint.
|
||||
@@ -134,15 +136,15 @@ func (c *Config) maxUniRemoteStreams() int64 {
|
||||
}
|
||||
|
||||
func (c *Config) maxStreamReadBufferSize() int64 {
|
||||
return configDefault(c.MaxStreamReadBufferSize, 1<<20, maxVarint)
|
||||
return configDefault(c.MaxStreamReadBufferSize, 1<<20, quicwire.MaxVarint)
|
||||
}
|
||||
|
||||
func (c *Config) maxStreamWriteBufferSize() int64 {
|
||||
return configDefault(c.MaxStreamWriteBufferSize, 1<<20, maxVarint)
|
||||
return configDefault(c.MaxStreamWriteBufferSize, 1<<20, quicwire.MaxVarint)
|
||||
}
|
||||
|
||||
func (c *Config) maxConnReadBufferSize() int64 {
|
||||
return configDefault(c.MaxConnReadBufferSize, 1<<20, maxVarint)
|
||||
return configDefault(c.MaxConnReadBufferSize, 1<<20, quicwire.MaxVarint)
|
||||
}
|
||||
|
||||
func (c *Config) handshakeTimeout() time.Duration {
|
||||
|
||||
@@ -9,6 +9,8 @@ package quic
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/net/internal/quic/quicwire"
|
||||
)
|
||||
|
||||
// packetType is a QUIC packet type.
|
||||
@@ -196,10 +198,10 @@ func parseVersionNegotiation(pkt []byte) (dstConnID, srcConnID, versions []byte)
|
||||
// appendVersionNegotiation appends a Version Negotiation packet to pkt,
|
||||
// returning the result.
|
||||
func appendVersionNegotiation(pkt, dstConnID, srcConnID []byte, versions ...uint32) []byte {
|
||||
pkt = append(pkt, headerFormLong|fixedBit) // header byte
|
||||
pkt = append(pkt, 0, 0, 0, 0) // Version (0 for Version Negotiation)
|
||||
pkt = appendUint8Bytes(pkt, dstConnID) // Destination Connection ID
|
||||
pkt = appendUint8Bytes(pkt, srcConnID) // Source Connection ID
|
||||
pkt = append(pkt, headerFormLong|fixedBit) // header byte
|
||||
pkt = append(pkt, 0, 0, 0, 0) // Version (0 for Version Negotiation)
|
||||
pkt = quicwire.AppendUint8Bytes(pkt, dstConnID) // Destination Connection ID
|
||||
pkt = quicwire.AppendUint8Bytes(pkt, srcConnID) // Source Connection ID
|
||||
for _, v := range versions {
|
||||
pkt = binary.BigEndian.AppendUint32(pkt, v) // Supported Version
|
||||
}
|
||||
@@ -243,21 +245,21 @@ func parseGenericLongHeaderPacket(b []byte) (p genericLongPacket, ok bool) {
|
||||
b = b[1:]
|
||||
// Version (32),
|
||||
var n int
|
||||
p.version, n = consumeUint32(b)
|
||||
p.version, n = quicwire.ConsumeUint32(b)
|
||||
if n < 0 {
|
||||
return genericLongPacket{}, false
|
||||
}
|
||||
b = b[n:]
|
||||
// Destination Connection ID Length (8),
|
||||
// Destination Connection ID (0..2048),
|
||||
p.dstConnID, n = consumeUint8Bytes(b)
|
||||
p.dstConnID, n = quicwire.ConsumeUint8Bytes(b)
|
||||
if n < 0 || len(p.dstConnID) > 2048/8 {
|
||||
return genericLongPacket{}, false
|
||||
}
|
||||
b = b[n:]
|
||||
// Source Connection ID Length (8),
|
||||
// Source Connection ID (0..2048),
|
||||
p.srcConnID, n = consumeUint8Bytes(b)
|
||||
p.srcConnID, n = quicwire.ConsumeUint8Bytes(b)
|
||||
if n < 0 || len(p.dstConnID) > 2048/8 {
|
||||
return genericLongPacket{}, false
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/internal/quic/quicwire"
|
||||
"golang.org/x/net/quic/qlog"
|
||||
)
|
||||
|
||||
@@ -736,7 +737,7 @@ func TestFrameDecodeErrors(t *testing.T) {
|
||||
name: "MAX_STREAMS with too many streams",
|
||||
b: func() []byte {
|
||||
// https://www.rfc-editor.org/rfc/rfc9000.html#section-19.11-5.2.1
|
||||
return appendVarint([]byte{frameTypeMaxStreamsBidi}, (1<<60)+1)
|
||||
return quicwire.AppendVarint([]byte{frameTypeMaxStreamsBidi}, (1<<60)+1)
|
||||
}(),
|
||||
}, {
|
||||
name: "NEW_CONNECTION_ID too small",
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
package quic
|
||||
|
||||
import "golang.org/x/net/internal/quic/quicwire"
|
||||
|
||||
// parseLongHeaderPacket parses a QUIC long header packet.
|
||||
//
|
||||
// It does not parse Version Negotiation packets.
|
||||
@@ -34,7 +36,7 @@ func parseLongHeaderPacket(pkt []byte, k fixedKeys, pnumMax packetNumber) (p lon
|
||||
}
|
||||
b = b[1:]
|
||||
// Version (32),
|
||||
p.version, n = consumeUint32(b)
|
||||
p.version, n = quicwire.ConsumeUint32(b)
|
||||
if n < 0 {
|
||||
return longPacket{}, -1
|
||||
}
|
||||
@@ -46,7 +48,7 @@ func parseLongHeaderPacket(pkt []byte, k fixedKeys, pnumMax packetNumber) (p lon
|
||||
|
||||
// Destination Connection ID Length (8),
|
||||
// Destination Connection ID (0..160),
|
||||
p.dstConnID, n = consumeUint8Bytes(b)
|
||||
p.dstConnID, n = quicwire.ConsumeUint8Bytes(b)
|
||||
if n < 0 || len(p.dstConnID) > maxConnIDLen {
|
||||
return longPacket{}, -1
|
||||
}
|
||||
@@ -54,7 +56,7 @@ func parseLongHeaderPacket(pkt []byte, k fixedKeys, pnumMax packetNumber) (p lon
|
||||
|
||||
// Source Connection ID Length (8),
|
||||
// Source Connection ID (0..160),
|
||||
p.srcConnID, n = consumeUint8Bytes(b)
|
||||
p.srcConnID, n = quicwire.ConsumeUint8Bytes(b)
|
||||
if n < 0 || len(p.dstConnID) > maxConnIDLen {
|
||||
return longPacket{}, -1
|
||||
}
|
||||
@@ -64,7 +66,7 @@ func parseLongHeaderPacket(pkt []byte, k fixedKeys, pnumMax packetNumber) (p lon
|
||||
case packetTypeInitial:
|
||||
// Token Length (i),
|
||||
// Token (..),
|
||||
p.extra, n = consumeVarintBytes(b)
|
||||
p.extra, n = quicwire.ConsumeVarintBytes(b)
|
||||
if n < 0 {
|
||||
return longPacket{}, -1
|
||||
}
|
||||
@@ -77,7 +79,7 @@ func parseLongHeaderPacket(pkt []byte, k fixedKeys, pnumMax packetNumber) (p lon
|
||||
}
|
||||
|
||||
// Length (i),
|
||||
payLen, n := consumeVarint(b)
|
||||
payLen, n := quicwire.ConsumeVarint(b)
|
||||
if n < 0 {
|
||||
return longPacket{}, -1
|
||||
}
|
||||
@@ -121,14 +123,14 @@ func skipLongHeaderPacket(pkt []byte) int {
|
||||
}
|
||||
if getPacketType(pkt) == packetTypeInitial {
|
||||
// Token length, token.
|
||||
_, nn := consumeVarintBytes(pkt[n:])
|
||||
_, nn := quicwire.ConsumeVarintBytes(pkt[n:])
|
||||
if nn < 0 {
|
||||
return -1
|
||||
}
|
||||
n += nn
|
||||
}
|
||||
// Length, packet number, payload.
|
||||
_, nn := consumeVarintBytes(pkt[n:])
|
||||
_, nn := quicwire.ConsumeVarintBytes(pkt[n:])
|
||||
if nn < 0 {
|
||||
return -1
|
||||
}
|
||||
@@ -160,20 +162,20 @@ func parse1RTTPacket(pkt []byte, k *updatingKeyPair, dstConnIDLen int, pnumMax p
|
||||
func consumeAckFrame(frame []byte, f func(rangeIndex int, start, end packetNumber)) (largest packetNumber, ackDelay unscaledAckDelay, n int) {
|
||||
b := frame[1:] // type
|
||||
|
||||
largestAck, n := consumeVarint(b)
|
||||
largestAck, n := quicwire.ConsumeVarint(b)
|
||||
if n < 0 {
|
||||
return 0, 0, -1
|
||||
}
|
||||
b = b[n:]
|
||||
|
||||
v, n := consumeVarintInt64(b)
|
||||
v, n := quicwire.ConsumeVarintInt64(b)
|
||||
if n < 0 {
|
||||
return 0, 0, -1
|
||||
}
|
||||
b = b[n:]
|
||||
ackDelay = unscaledAckDelay(v)
|
||||
|
||||
ackRangeCount, n := consumeVarint(b)
|
||||
ackRangeCount, n := quicwire.ConsumeVarint(b)
|
||||
if n < 0 {
|
||||
return 0, 0, -1
|
||||
}
|
||||
@@ -181,7 +183,7 @@ func consumeAckFrame(frame []byte, f func(rangeIndex int, start, end packetNumbe
|
||||
|
||||
rangeMax := packetNumber(largestAck)
|
||||
for i := uint64(0); ; i++ {
|
||||
rangeLen, n := consumeVarint(b)
|
||||
rangeLen, n := quicwire.ConsumeVarint(b)
|
||||
if n < 0 {
|
||||
return 0, 0, -1
|
||||
}
|
||||
@@ -196,7 +198,7 @@ func consumeAckFrame(frame []byte, f func(rangeIndex int, start, end packetNumbe
|
||||
break
|
||||
}
|
||||
|
||||
gap, n := consumeVarint(b)
|
||||
gap, n := quicwire.ConsumeVarint(b)
|
||||
if n < 0 {
|
||||
return 0, 0, -1
|
||||
}
|
||||
@@ -209,17 +211,17 @@ func consumeAckFrame(frame []byte, f func(rangeIndex int, start, end packetNumbe
|
||||
return packetNumber(largestAck), ackDelay, len(frame) - len(b)
|
||||
}
|
||||
|
||||
ect0Count, n := consumeVarint(b)
|
||||
ect0Count, n := quicwire.ConsumeVarint(b)
|
||||
if n < 0 {
|
||||
return 0, 0, -1
|
||||
}
|
||||
b = b[n:]
|
||||
ect1Count, n := consumeVarint(b)
|
||||
ect1Count, n := quicwire.ConsumeVarint(b)
|
||||
if n < 0 {
|
||||
return 0, 0, -1
|
||||
}
|
||||
b = b[n:]
|
||||
ecnCECount, n := consumeVarint(b)
|
||||
ecnCECount, n := quicwire.ConsumeVarint(b)
|
||||
if n < 0 {
|
||||
return 0, 0, -1
|
||||
}
|
||||
@@ -236,17 +238,17 @@ func consumeAckFrame(frame []byte, f func(rangeIndex int, start, end packetNumbe
|
||||
|
||||
func consumeResetStreamFrame(b []byte) (id streamID, code uint64, finalSize int64, n int) {
|
||||
n = 1
|
||||
idInt, nn := consumeVarint(b[n:])
|
||||
idInt, nn := quicwire.ConsumeVarint(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, 0, -1
|
||||
}
|
||||
n += nn
|
||||
code, nn = consumeVarint(b[n:])
|
||||
code, nn = quicwire.ConsumeVarint(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, 0, -1
|
||||
}
|
||||
n += nn
|
||||
v, nn := consumeVarint(b[n:])
|
||||
v, nn := quicwire.ConsumeVarint(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, 0, -1
|
||||
}
|
||||
@@ -257,12 +259,12 @@ func consumeResetStreamFrame(b []byte) (id streamID, code uint64, finalSize int6
|
||||
|
||||
func consumeStopSendingFrame(b []byte) (id streamID, code uint64, n int) {
|
||||
n = 1
|
||||
idInt, nn := consumeVarint(b[n:])
|
||||
idInt, nn := quicwire.ConsumeVarint(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, -1
|
||||
}
|
||||
n += nn
|
||||
code, nn = consumeVarint(b[n:])
|
||||
code, nn = quicwire.ConsumeVarint(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, -1
|
||||
}
|
||||
@@ -272,13 +274,13 @@ func consumeStopSendingFrame(b []byte) (id streamID, code uint64, n int) {
|
||||
|
||||
func consumeCryptoFrame(b []byte) (off int64, data []byte, n int) {
|
||||
n = 1
|
||||
v, nn := consumeVarint(b[n:])
|
||||
v, nn := quicwire.ConsumeVarint(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, nil, -1
|
||||
}
|
||||
off = int64(v)
|
||||
n += nn
|
||||
data, nn = consumeVarintBytes(b[n:])
|
||||
data, nn = quicwire.ConsumeVarintBytes(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, nil, -1
|
||||
}
|
||||
@@ -288,7 +290,7 @@ func consumeCryptoFrame(b []byte) (off int64, data []byte, n int) {
|
||||
|
||||
func consumeNewTokenFrame(b []byte) (token []byte, n int) {
|
||||
n = 1
|
||||
data, nn := consumeVarintBytes(b[n:])
|
||||
data, nn := quicwire.ConsumeVarintBytes(b[n:])
|
||||
if nn < 0 {
|
||||
return nil, -1
|
||||
}
|
||||
@@ -302,13 +304,13 @@ func consumeNewTokenFrame(b []byte) (token []byte, n int) {
|
||||
func consumeStreamFrame(b []byte) (id streamID, off int64, fin bool, data []byte, n int) {
|
||||
fin = (b[0] & 0x01) != 0
|
||||
n = 1
|
||||
idInt, nn := consumeVarint(b[n:])
|
||||
idInt, nn := quicwire.ConsumeVarint(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, false, nil, -1
|
||||
}
|
||||
n += nn
|
||||
if b[0]&0x04 != 0 {
|
||||
v, nn := consumeVarint(b[n:])
|
||||
v, nn := quicwire.ConsumeVarint(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, false, nil, -1
|
||||
}
|
||||
@@ -316,7 +318,7 @@ func consumeStreamFrame(b []byte) (id streamID, off int64, fin bool, data []byte
|
||||
off = int64(v)
|
||||
}
|
||||
if b[0]&0x02 != 0 {
|
||||
data, nn = consumeVarintBytes(b[n:])
|
||||
data, nn = quicwire.ConsumeVarintBytes(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, false, nil, -1
|
||||
}
|
||||
@@ -333,7 +335,7 @@ func consumeStreamFrame(b []byte) (id streamID, off int64, fin bool, data []byte
|
||||
|
||||
func consumeMaxDataFrame(b []byte) (max int64, n int) {
|
||||
n = 1
|
||||
v, nn := consumeVarint(b[n:])
|
||||
v, nn := quicwire.ConsumeVarint(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, -1
|
||||
}
|
||||
@@ -343,13 +345,13 @@ func consumeMaxDataFrame(b []byte) (max int64, n int) {
|
||||
|
||||
func consumeMaxStreamDataFrame(b []byte) (id streamID, max int64, n int) {
|
||||
n = 1
|
||||
v, nn := consumeVarint(b[n:])
|
||||
v, nn := quicwire.ConsumeVarint(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, -1
|
||||
}
|
||||
n += nn
|
||||
id = streamID(v)
|
||||
v, nn = consumeVarint(b[n:])
|
||||
v, nn = quicwire.ConsumeVarint(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, -1
|
||||
}
|
||||
@@ -368,7 +370,7 @@ func consumeMaxStreamsFrame(b []byte) (typ streamType, max int64, n int) {
|
||||
return 0, 0, -1
|
||||
}
|
||||
n = 1
|
||||
v, nn := consumeVarint(b[n:])
|
||||
v, nn := quicwire.ConsumeVarint(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, -1
|
||||
}
|
||||
@@ -381,13 +383,13 @@ func consumeMaxStreamsFrame(b []byte) (typ streamType, max int64, n int) {
|
||||
|
||||
func consumeStreamDataBlockedFrame(b []byte) (id streamID, max int64, n int) {
|
||||
n = 1
|
||||
v, nn := consumeVarint(b[n:])
|
||||
v, nn := quicwire.ConsumeVarint(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, -1
|
||||
}
|
||||
n += nn
|
||||
id = streamID(v)
|
||||
max, nn = consumeVarintInt64(b[n:])
|
||||
max, nn = quicwire.ConsumeVarintInt64(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, -1
|
||||
}
|
||||
@@ -397,7 +399,7 @@ func consumeStreamDataBlockedFrame(b []byte) (id streamID, max int64, n int) {
|
||||
|
||||
func consumeDataBlockedFrame(b []byte) (max int64, n int) {
|
||||
n = 1
|
||||
max, nn := consumeVarintInt64(b[n:])
|
||||
max, nn := quicwire.ConsumeVarintInt64(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, -1
|
||||
}
|
||||
@@ -412,7 +414,7 @@ func consumeStreamsBlockedFrame(b []byte) (typ streamType, max int64, n int) {
|
||||
typ = uniStream
|
||||
}
|
||||
n = 1
|
||||
max, nn := consumeVarintInt64(b[n:])
|
||||
max, nn := quicwire.ConsumeVarintInt64(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, -1
|
||||
}
|
||||
@@ -423,12 +425,12 @@ func consumeStreamsBlockedFrame(b []byte) (typ streamType, max int64, n int) {
|
||||
func consumeNewConnectionIDFrame(b []byte) (seq, retire int64, connID []byte, resetToken statelessResetToken, n int) {
|
||||
n = 1
|
||||
var nn int
|
||||
seq, nn = consumeVarintInt64(b[n:])
|
||||
seq, nn = quicwire.ConsumeVarintInt64(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, nil, statelessResetToken{}, -1
|
||||
}
|
||||
n += nn
|
||||
retire, nn = consumeVarintInt64(b[n:])
|
||||
retire, nn = quicwire.ConsumeVarintInt64(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, nil, statelessResetToken{}, -1
|
||||
}
|
||||
@@ -436,7 +438,7 @@ func consumeNewConnectionIDFrame(b []byte) (seq, retire int64, connID []byte, re
|
||||
if seq < retire {
|
||||
return 0, 0, nil, statelessResetToken{}, -1
|
||||
}
|
||||
connID, nn = consumeVarintBytes(b[n:])
|
||||
connID, nn = quicwire.ConsumeVarintBytes(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, nil, statelessResetToken{}, -1
|
||||
}
|
||||
@@ -455,7 +457,7 @@ func consumeNewConnectionIDFrame(b []byte) (seq, retire int64, connID []byte, re
|
||||
func consumeRetireConnectionIDFrame(b []byte) (seq int64, n int) {
|
||||
n = 1
|
||||
var nn int
|
||||
seq, nn = consumeVarintInt64(b[n:])
|
||||
seq, nn = quicwire.ConsumeVarintInt64(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, -1
|
||||
}
|
||||
@@ -481,18 +483,18 @@ func consumeConnectionCloseTransportFrame(b []byte) (code transportError, frameT
|
||||
n = 1
|
||||
var nn int
|
||||
var codeInt uint64
|
||||
codeInt, nn = consumeVarint(b[n:])
|
||||
codeInt, nn = quicwire.ConsumeVarint(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, "", -1
|
||||
}
|
||||
code = transportError(codeInt)
|
||||
n += nn
|
||||
frameType, nn = consumeVarint(b[n:])
|
||||
frameType, nn = quicwire.ConsumeVarint(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, "", -1
|
||||
}
|
||||
n += nn
|
||||
reasonb, nn := consumeVarintBytes(b[n:])
|
||||
reasonb, nn := quicwire.ConsumeVarintBytes(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, 0, "", -1
|
||||
}
|
||||
@@ -504,12 +506,12 @@ func consumeConnectionCloseTransportFrame(b []byte) (code transportError, frameT
|
||||
func consumeConnectionCloseApplicationFrame(b []byte) (code uint64, reason string, n int) {
|
||||
n = 1
|
||||
var nn int
|
||||
code, nn = consumeVarint(b[n:])
|
||||
code, nn = quicwire.ConsumeVarint(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, "", -1
|
||||
}
|
||||
n += nn
|
||||
reasonb, nn := consumeVarintBytes(b[n:])
|
||||
reasonb, nn := quicwire.ConsumeVarintBytes(b[n:])
|
||||
if nn < 0 {
|
||||
return 0, "", -1
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ package quic
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
||||
"golang.org/x/net/internal/quic/quicwire"
|
||||
)
|
||||
|
||||
// A packetWriter constructs QUIC datagrams.
|
||||
@@ -74,7 +76,7 @@ func (w *packetWriter) startProtectedLongHeaderPacket(pnumMaxAcked packetNumber,
|
||||
hdrSize += 1 + len(p.srcConnID)
|
||||
switch p.ptype {
|
||||
case packetTypeInitial:
|
||||
hdrSize += sizeVarint(uint64(len(p.extra))) + len(p.extra)
|
||||
hdrSize += quicwire.SizeVarint(uint64(len(p.extra))) + len(p.extra)
|
||||
}
|
||||
hdrSize += 2 // length, hardcoded to a 2-byte varint
|
||||
pnumOff := len(w.b) + hdrSize
|
||||
@@ -127,11 +129,11 @@ func (w *packetWriter) finishProtectedLongHeaderPacket(pnumMaxAcked packetNumber
|
||||
}
|
||||
hdr = append(hdr, headerFormLong|fixedBit|typeBits|byte(pnumLen-1))
|
||||
hdr = binary.BigEndian.AppendUint32(hdr, p.version)
|
||||
hdr = appendUint8Bytes(hdr, p.dstConnID)
|
||||
hdr = appendUint8Bytes(hdr, p.srcConnID)
|
||||
hdr = quicwire.AppendUint8Bytes(hdr, p.dstConnID)
|
||||
hdr = quicwire.AppendUint8Bytes(hdr, p.srcConnID)
|
||||
switch p.ptype {
|
||||
case packetTypeInitial:
|
||||
hdr = appendVarintBytes(hdr, p.extra) // token
|
||||
hdr = quicwire.AppendVarintBytes(hdr, p.extra) // token
|
||||
}
|
||||
|
||||
// Packet length, always encoded as a 2-byte varint.
|
||||
@@ -270,26 +272,26 @@ func (w *packetWriter) appendAckFrame(seen rangeset[packetNumber], delay unscale
|
||||
largest = uint64(seen.max())
|
||||
firstRange = uint64(seen[len(seen)-1].size() - 1)
|
||||
)
|
||||
if w.avail() < 1+sizeVarint(largest)+sizeVarint(uint64(delay))+1+sizeVarint(firstRange) {
|
||||
if w.avail() < 1+quicwire.SizeVarint(largest)+quicwire.SizeVarint(uint64(delay))+1+quicwire.SizeVarint(firstRange) {
|
||||
return false
|
||||
}
|
||||
w.b = append(w.b, frameTypeAck)
|
||||
w.b = appendVarint(w.b, largest)
|
||||
w.b = appendVarint(w.b, uint64(delay))
|
||||
w.b = quicwire.AppendVarint(w.b, largest)
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(delay))
|
||||
// The range count is technically a varint, but we'll reserve a single byte for it
|
||||
// and never add more than 62 ranges (the maximum varint that fits in a byte).
|
||||
rangeCountOff := len(w.b)
|
||||
w.b = append(w.b, 0)
|
||||
w.b = appendVarint(w.b, firstRange)
|
||||
w.b = quicwire.AppendVarint(w.b, firstRange)
|
||||
rangeCount := byte(0)
|
||||
for i := len(seen) - 2; i >= 0; i-- {
|
||||
gap := uint64(seen[i+1].start - seen[i].end - 1)
|
||||
size := uint64(seen[i].size() - 1)
|
||||
if w.avail() < sizeVarint(gap)+sizeVarint(size) || rangeCount > 62 {
|
||||
if w.avail() < quicwire.SizeVarint(gap)+quicwire.SizeVarint(size) || rangeCount > 62 {
|
||||
break
|
||||
}
|
||||
w.b = appendVarint(w.b, gap)
|
||||
w.b = appendVarint(w.b, size)
|
||||
w.b = quicwire.AppendVarint(w.b, gap)
|
||||
w.b = quicwire.AppendVarint(w.b, size)
|
||||
rangeCount++
|
||||
}
|
||||
w.b[rangeCountOff] = rangeCount
|
||||
@@ -299,34 +301,34 @@ func (w *packetWriter) appendAckFrame(seen rangeset[packetNumber], delay unscale
|
||||
}
|
||||
|
||||
func (w *packetWriter) appendNewTokenFrame(token []byte) (added bool) {
|
||||
if w.avail() < 1+sizeVarint(uint64(len(token)))+len(token) {
|
||||
if w.avail() < 1+quicwire.SizeVarint(uint64(len(token)))+len(token) {
|
||||
return false
|
||||
}
|
||||
w.b = append(w.b, frameTypeNewToken)
|
||||
w.b = appendVarintBytes(w.b, token)
|
||||
w.b = quicwire.AppendVarintBytes(w.b, token)
|
||||
return true
|
||||
}
|
||||
|
||||
func (w *packetWriter) appendResetStreamFrame(id streamID, code uint64, finalSize int64) (added bool) {
|
||||
if w.avail() < 1+sizeVarint(uint64(id))+sizeVarint(code)+sizeVarint(uint64(finalSize)) {
|
||||
if w.avail() < 1+quicwire.SizeVarint(uint64(id))+quicwire.SizeVarint(code)+quicwire.SizeVarint(uint64(finalSize)) {
|
||||
return false
|
||||
}
|
||||
w.b = append(w.b, frameTypeResetStream)
|
||||
w.b = appendVarint(w.b, uint64(id))
|
||||
w.b = appendVarint(w.b, code)
|
||||
w.b = appendVarint(w.b, uint64(finalSize))
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(id))
|
||||
w.b = quicwire.AppendVarint(w.b, code)
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(finalSize))
|
||||
w.sent.appendAckElicitingFrame(frameTypeResetStream)
|
||||
w.sent.appendInt(uint64(id))
|
||||
return true
|
||||
}
|
||||
|
||||
func (w *packetWriter) appendStopSendingFrame(id streamID, code uint64) (added bool) {
|
||||
if w.avail() < 1+sizeVarint(uint64(id))+sizeVarint(code) {
|
||||
if w.avail() < 1+quicwire.SizeVarint(uint64(id))+quicwire.SizeVarint(code) {
|
||||
return false
|
||||
}
|
||||
w.b = append(w.b, frameTypeStopSending)
|
||||
w.b = appendVarint(w.b, uint64(id))
|
||||
w.b = appendVarint(w.b, code)
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(id))
|
||||
w.b = quicwire.AppendVarint(w.b, code)
|
||||
w.sent.appendAckElicitingFrame(frameTypeStopSending)
|
||||
w.sent.appendInt(uint64(id))
|
||||
return true
|
||||
@@ -337,9 +339,9 @@ func (w *packetWriter) appendStopSendingFrame(id streamID, code uint64) (added b
|
||||
// The returned []byte may be smaller than size if the packet cannot hold all the data.
|
||||
func (w *packetWriter) appendCryptoFrame(off int64, size int) (_ []byte, added bool) {
|
||||
max := w.avail()
|
||||
max -= 1 // frame type
|
||||
max -= sizeVarint(uint64(off)) // offset
|
||||
max -= sizeVarint(uint64(size)) // maximum length
|
||||
max -= 1 // frame type
|
||||
max -= quicwire.SizeVarint(uint64(off)) // offset
|
||||
max -= quicwire.SizeVarint(uint64(size)) // maximum length
|
||||
if max <= 0 {
|
||||
return nil, false
|
||||
}
|
||||
@@ -347,8 +349,8 @@ func (w *packetWriter) appendCryptoFrame(off int64, size int) (_ []byte, added b
|
||||
size = max
|
||||
}
|
||||
w.b = append(w.b, frameTypeCrypto)
|
||||
w.b = appendVarint(w.b, uint64(off))
|
||||
w.b = appendVarint(w.b, uint64(size))
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(off))
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(size))
|
||||
start := len(w.b)
|
||||
w.b = w.b[:start+size]
|
||||
w.sent.appendAckElicitingFrame(frameTypeCrypto)
|
||||
@@ -363,12 +365,12 @@ func (w *packetWriter) appendStreamFrame(id streamID, off int64, size int, fin b
|
||||
typ := uint8(frameTypeStreamBase | streamLenBit)
|
||||
max := w.avail()
|
||||
max -= 1 // frame type
|
||||
max -= sizeVarint(uint64(id))
|
||||
max -= quicwire.SizeVarint(uint64(id))
|
||||
if off != 0 {
|
||||
max -= sizeVarint(uint64(off))
|
||||
max -= quicwire.SizeVarint(uint64(off))
|
||||
typ |= streamOffBit
|
||||
}
|
||||
max -= sizeVarint(uint64(size)) // maximum length
|
||||
max -= quicwire.SizeVarint(uint64(size)) // maximum length
|
||||
if max < 0 || (max == 0 && size > 0) {
|
||||
return nil, false
|
||||
}
|
||||
@@ -378,11 +380,11 @@ func (w *packetWriter) appendStreamFrame(id streamID, off int64, size int, fin b
|
||||
typ |= streamFinBit
|
||||
}
|
||||
w.b = append(w.b, typ)
|
||||
w.b = appendVarint(w.b, uint64(id))
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(id))
|
||||
if off != 0 {
|
||||
w.b = appendVarint(w.b, uint64(off))
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(off))
|
||||
}
|
||||
w.b = appendVarint(w.b, uint64(size))
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(size))
|
||||
start := len(w.b)
|
||||
w.b = w.b[:start+size]
|
||||
w.sent.appendAckElicitingFrame(typ & (frameTypeStreamBase | streamFinBit))
|
||||
@@ -392,29 +394,29 @@ func (w *packetWriter) appendStreamFrame(id streamID, off int64, size int, fin b
|
||||
}
|
||||
|
||||
func (w *packetWriter) appendMaxDataFrame(max int64) (added bool) {
|
||||
if w.avail() < 1+sizeVarint(uint64(max)) {
|
||||
if w.avail() < 1+quicwire.SizeVarint(uint64(max)) {
|
||||
return false
|
||||
}
|
||||
w.b = append(w.b, frameTypeMaxData)
|
||||
w.b = appendVarint(w.b, uint64(max))
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(max))
|
||||
w.sent.appendAckElicitingFrame(frameTypeMaxData)
|
||||
return true
|
||||
}
|
||||
|
||||
func (w *packetWriter) appendMaxStreamDataFrame(id streamID, max int64) (added bool) {
|
||||
if w.avail() < 1+sizeVarint(uint64(id))+sizeVarint(uint64(max)) {
|
||||
if w.avail() < 1+quicwire.SizeVarint(uint64(id))+quicwire.SizeVarint(uint64(max)) {
|
||||
return false
|
||||
}
|
||||
w.b = append(w.b, frameTypeMaxStreamData)
|
||||
w.b = appendVarint(w.b, uint64(id))
|
||||
w.b = appendVarint(w.b, uint64(max))
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(id))
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(max))
|
||||
w.sent.appendAckElicitingFrame(frameTypeMaxStreamData)
|
||||
w.sent.appendInt(uint64(id))
|
||||
return true
|
||||
}
|
||||
|
||||
func (w *packetWriter) appendMaxStreamsFrame(streamType streamType, max int64) (added bool) {
|
||||
if w.avail() < 1+sizeVarint(uint64(max)) {
|
||||
if w.avail() < 1+quicwire.SizeVarint(uint64(max)) {
|
||||
return false
|
||||
}
|
||||
var typ byte
|
||||
@@ -424,35 +426,35 @@ func (w *packetWriter) appendMaxStreamsFrame(streamType streamType, max int64) (
|
||||
typ = frameTypeMaxStreamsUni
|
||||
}
|
||||
w.b = append(w.b, typ)
|
||||
w.b = appendVarint(w.b, uint64(max))
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(max))
|
||||
w.sent.appendAckElicitingFrame(typ)
|
||||
return true
|
||||
}
|
||||
|
||||
func (w *packetWriter) appendDataBlockedFrame(max int64) (added bool) {
|
||||
if w.avail() < 1+sizeVarint(uint64(max)) {
|
||||
if w.avail() < 1+quicwire.SizeVarint(uint64(max)) {
|
||||
return false
|
||||
}
|
||||
w.b = append(w.b, frameTypeDataBlocked)
|
||||
w.b = appendVarint(w.b, uint64(max))
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(max))
|
||||
w.sent.appendAckElicitingFrame(frameTypeDataBlocked)
|
||||
return true
|
||||
}
|
||||
|
||||
func (w *packetWriter) appendStreamDataBlockedFrame(id streamID, max int64) (added bool) {
|
||||
if w.avail() < 1+sizeVarint(uint64(id))+sizeVarint(uint64(max)) {
|
||||
if w.avail() < 1+quicwire.SizeVarint(uint64(id))+quicwire.SizeVarint(uint64(max)) {
|
||||
return false
|
||||
}
|
||||
w.b = append(w.b, frameTypeStreamDataBlocked)
|
||||
w.b = appendVarint(w.b, uint64(id))
|
||||
w.b = appendVarint(w.b, uint64(max))
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(id))
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(max))
|
||||
w.sent.appendAckElicitingFrame(frameTypeStreamDataBlocked)
|
||||
w.sent.appendInt(uint64(id))
|
||||
return true
|
||||
}
|
||||
|
||||
func (w *packetWriter) appendStreamsBlockedFrame(typ streamType, max int64) (added bool) {
|
||||
if w.avail() < 1+sizeVarint(uint64(max)) {
|
||||
if w.avail() < 1+quicwire.SizeVarint(uint64(max)) {
|
||||
return false
|
||||
}
|
||||
var ftype byte
|
||||
@@ -462,19 +464,19 @@ func (w *packetWriter) appendStreamsBlockedFrame(typ streamType, max int64) (add
|
||||
ftype = frameTypeStreamsBlockedUni
|
||||
}
|
||||
w.b = append(w.b, ftype)
|
||||
w.b = appendVarint(w.b, uint64(max))
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(max))
|
||||
w.sent.appendAckElicitingFrame(ftype)
|
||||
return true
|
||||
}
|
||||
|
||||
func (w *packetWriter) appendNewConnectionIDFrame(seq, retirePriorTo int64, connID []byte, token [16]byte) (added bool) {
|
||||
if w.avail() < 1+sizeVarint(uint64(seq))+sizeVarint(uint64(retirePriorTo))+1+len(connID)+len(token) {
|
||||
if w.avail() < 1+quicwire.SizeVarint(uint64(seq))+quicwire.SizeVarint(uint64(retirePriorTo))+1+len(connID)+len(token) {
|
||||
return false
|
||||
}
|
||||
w.b = append(w.b, frameTypeNewConnectionID)
|
||||
w.b = appendVarint(w.b, uint64(seq))
|
||||
w.b = appendVarint(w.b, uint64(retirePriorTo))
|
||||
w.b = appendUint8Bytes(w.b, connID)
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(seq))
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(retirePriorTo))
|
||||
w.b = quicwire.AppendUint8Bytes(w.b, connID)
|
||||
w.b = append(w.b, token[:]...)
|
||||
w.sent.appendAckElicitingFrame(frameTypeNewConnectionID)
|
||||
w.sent.appendInt(uint64(seq))
|
||||
@@ -482,11 +484,11 @@ func (w *packetWriter) appendNewConnectionIDFrame(seq, retirePriorTo int64, conn
|
||||
}
|
||||
|
||||
func (w *packetWriter) appendRetireConnectionIDFrame(seq int64) (added bool) {
|
||||
if w.avail() < 1+sizeVarint(uint64(seq)) {
|
||||
if w.avail() < 1+quicwire.SizeVarint(uint64(seq)) {
|
||||
return false
|
||||
}
|
||||
w.b = append(w.b, frameTypeRetireConnectionID)
|
||||
w.b = appendVarint(w.b, uint64(seq))
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(seq))
|
||||
w.sent.appendAckElicitingFrame(frameTypeRetireConnectionID)
|
||||
w.sent.appendInt(uint64(seq))
|
||||
return true
|
||||
@@ -515,13 +517,13 @@ func (w *packetWriter) appendPathResponseFrame(data pathChallengeData) (added bo
|
||||
// appendConnectionCloseTransportFrame appends a CONNECTION_CLOSE frame
|
||||
// carrying a transport error code.
|
||||
func (w *packetWriter) appendConnectionCloseTransportFrame(code transportError, frameType uint64, reason string) (added bool) {
|
||||
if w.avail() < 1+sizeVarint(uint64(code))+sizeVarint(frameType)+sizeVarint(uint64(len(reason)))+len(reason) {
|
||||
if w.avail() < 1+quicwire.SizeVarint(uint64(code))+quicwire.SizeVarint(frameType)+quicwire.SizeVarint(uint64(len(reason)))+len(reason) {
|
||||
return false
|
||||
}
|
||||
w.b = append(w.b, frameTypeConnectionCloseTransport)
|
||||
w.b = appendVarint(w.b, uint64(code))
|
||||
w.b = appendVarint(w.b, frameType)
|
||||
w.b = appendVarintBytes(w.b, []byte(reason))
|
||||
w.b = quicwire.AppendVarint(w.b, uint64(code))
|
||||
w.b = quicwire.AppendVarint(w.b, frameType)
|
||||
w.b = quicwire.AppendVarintBytes(w.b, []byte(reason))
|
||||
// We don't record CONNECTION_CLOSE frames in w.sent, since they are never acked or
|
||||
// detected as lost.
|
||||
return true
|
||||
@@ -530,12 +532,12 @@ func (w *packetWriter) appendConnectionCloseTransportFrame(code transportError,
|
||||
// appendConnectionCloseApplicationFrame appends a CONNECTION_CLOSE frame
|
||||
// carrying an application protocol error code.
|
||||
func (w *packetWriter) appendConnectionCloseApplicationFrame(code uint64, reason string) (added bool) {
|
||||
if w.avail() < 1+sizeVarint(code)+sizeVarint(uint64(len(reason)))+len(reason) {
|
||||
if w.avail() < 1+quicwire.SizeVarint(code)+quicwire.SizeVarint(uint64(len(reason)))+len(reason) {
|
||||
return false
|
||||
}
|
||||
w.b = append(w.b, frameTypeConnectionCloseApplication)
|
||||
w.b = appendVarint(w.b, code)
|
||||
w.b = appendVarintBytes(w.b, []byte(reason))
|
||||
w.b = quicwire.AppendVarint(w.b, code)
|
||||
w.b = quicwire.AppendVarintBytes(w.b, []byte(reason))
|
||||
// We don't record CONNECTION_CLOSE frames in w.sent, since they are never acked or
|
||||
// detected as lost.
|
||||
return true
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/chacha20poly1305"
|
||||
"golang.org/x/net/internal/quic/quicwire"
|
||||
)
|
||||
|
||||
// AEAD and nonce used to compute the Retry Integrity Tag.
|
||||
@@ -133,7 +134,7 @@ func (rs *retryState) validateToken(now time.Time, token, srcConnID, dstConnID [
|
||||
|
||||
func (rs *retryState) additionalData(srcConnID []byte, addr netip.AddrPort) []byte {
|
||||
var additional []byte
|
||||
additional = appendUint8Bytes(additional, srcConnID)
|
||||
additional = quicwire.AppendUint8Bytes(additional, srcConnID)
|
||||
additional = append(additional, addr.Addr().AsSlice()...)
|
||||
additional = binary.BigEndian.AppendUint16(additional, addr.Port())
|
||||
return additional
|
||||
@@ -141,7 +142,7 @@ func (rs *retryState) additionalData(srcConnID []byte, addr netip.AddrPort) []by
|
||||
|
||||
func (e *Endpoint) validateInitialAddress(now time.Time, p genericLongPacket, peerAddr netip.AddrPort) (origDstConnID []byte, ok bool) {
|
||||
// The retry token is at the start of an Initial packet's data.
|
||||
token, n := consumeUint8Bytes(p.data)
|
||||
token, n := quicwire.ConsumeUint8Bytes(p.data)
|
||||
if n < 0 {
|
||||
// We've already validated that the packet is at least 1200 bytes long,
|
||||
// so there's no way for even a maximum size token to not fit.
|
||||
@@ -196,12 +197,12 @@ func encodeRetryPacket(originalDstConnID []byte, p retryPacket) []byte {
|
||||
// Create the pseudo-packet (including the original DCID), append the tag,
|
||||
// and return the Retry packet.
|
||||
var b []byte
|
||||
b = appendUint8Bytes(b, originalDstConnID) // Original Destination Connection ID
|
||||
start := len(b) // start of the Retry packet
|
||||
b = quicwire.AppendUint8Bytes(b, originalDstConnID) // Original Destination Connection ID
|
||||
start := len(b) // start of the Retry packet
|
||||
b = append(b, headerFormLong|fixedBit|longPacketTypeRetry)
|
||||
b = binary.BigEndian.AppendUint32(b, quicVersion1) // Version
|
||||
b = appendUint8Bytes(b, p.dstConnID) // Destination Connection ID
|
||||
b = appendUint8Bytes(b, p.srcConnID) // Source Connection ID
|
||||
b = quicwire.AppendUint8Bytes(b, p.dstConnID) // Destination Connection ID
|
||||
b = quicwire.AppendUint8Bytes(b, p.srcConnID) // Source Connection ID
|
||||
b = append(b, p.token...) // Token
|
||||
b = retryAEAD.Seal(b, retryNonce, nil, b) // Retry Integrity Tag
|
||||
return b[start:]
|
||||
@@ -222,7 +223,7 @@ func parseRetryPacket(b, origDstConnID []byte) (p retryPacket, ok bool) {
|
||||
// Create the pseudo-packet consisting of the original destination connection ID
|
||||
// followed by the Retry packet (less the integrity tag).
|
||||
// Use this to validate the packet integrity tag.
|
||||
pseudo := appendUint8Bytes(nil, origDstConnID)
|
||||
pseudo := quicwire.AppendUint8Bytes(nil, origDstConnID)
|
||||
pseudo = append(pseudo, b[:len(b)-retryIntegrityTagLength]...)
|
||||
wantTag := retryAEAD.Seal(nil, retryNonce, nil, pseudo)
|
||||
if !bytes.Equal(gotTag, wantTag) {
|
||||
|
||||
@@ -9,6 +9,8 @@ package quic
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/internal/quic/quicwire"
|
||||
)
|
||||
|
||||
// A sentPacket tracks state related to an in-flight packet we sent,
|
||||
@@ -78,12 +80,12 @@ func (sent *sentPacket) appendAckElicitingFrame(frameType byte) {
|
||||
}
|
||||
|
||||
func (sent *sentPacket) appendInt(v uint64) {
|
||||
sent.b = appendVarint(sent.b, v)
|
||||
sent.b = quicwire.AppendVarint(sent.b, v)
|
||||
}
|
||||
|
||||
func (sent *sentPacket) appendOffAndSize(start int64, size int) {
|
||||
sent.b = appendVarint(sent.b, uint64(start))
|
||||
sent.b = appendVarint(sent.b, uint64(size))
|
||||
sent.b = quicwire.AppendVarint(sent.b, uint64(start))
|
||||
sent.b = quicwire.AppendVarint(sent.b, uint64(size))
|
||||
}
|
||||
|
||||
// The next* methods read back information about frames in the packet.
|
||||
@@ -95,7 +97,7 @@ func (sent *sentPacket) next() (frameType byte) {
|
||||
}
|
||||
|
||||
func (sent *sentPacket) nextInt() uint64 {
|
||||
v, n := consumeVarint(sent.b[sent.n:])
|
||||
v, n := quicwire.ConsumeVarint(sent.b[sent.n:])
|
||||
sent.n += n
|
||||
return v
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
|
||||
"golang.org/x/net/internal/quic/quicwire"
|
||||
)
|
||||
|
||||
// A Stream is an ordered byte stream.
|
||||
@@ -585,8 +587,8 @@ func (s *Stream) resetInternal(code uint64, userClosed bool) {
|
||||
if s.outreset.isSet() {
|
||||
return
|
||||
}
|
||||
if code > maxVarint {
|
||||
code = maxVarint
|
||||
if code > quicwire.MaxVarint {
|
||||
code = quicwire.MaxVarint
|
||||
}
|
||||
// We could check here to see if the stream is closed and the
|
||||
// peer has acked all the data and the FIN, but sending an
|
||||
|
||||
@@ -15,6 +15,8 @@ import (
|
||||
"io"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/net/internal/quic/quicwire"
|
||||
)
|
||||
|
||||
func TestStreamWriteBlockedByOutputBuffer(t *testing.T) {
|
||||
@@ -1522,10 +1524,10 @@ func newRemoteStream(t *testing.T, tc *testConn, styp streamType) *Stream {
|
||||
func permissiveTransportParameters(p *transportParameters) {
|
||||
p.initialMaxStreamsBidi = maxStreamsLimit
|
||||
p.initialMaxStreamsUni = maxStreamsLimit
|
||||
p.initialMaxData = maxVarint
|
||||
p.initialMaxStreamDataBidiRemote = maxVarint
|
||||
p.initialMaxStreamDataBidiLocal = maxVarint
|
||||
p.initialMaxStreamDataUni = maxVarint
|
||||
p.initialMaxData = quicwire.MaxVarint
|
||||
p.initialMaxStreamDataBidiRemote = quicwire.MaxVarint
|
||||
p.initialMaxStreamDataBidiLocal = quicwire.MaxVarint
|
||||
p.initialMaxStreamDataUni = quicwire.MaxVarint
|
||||
}
|
||||
|
||||
func makeTestData(n int) []byte {
|
||||
|
||||
@@ -10,6 +10,8 @@ import (
|
||||
"encoding/binary"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/internal/quic/quicwire"
|
||||
)
|
||||
|
||||
// transportParameters transferred in the quic_transport_parameters TLS extension.
|
||||
@@ -77,89 +79,89 @@ const (
|
||||
func marshalTransportParameters(p transportParameters) []byte {
|
||||
var b []byte
|
||||
if v := p.originalDstConnID; v != nil {
|
||||
b = appendVarint(b, paramOriginalDestinationConnectionID)
|
||||
b = appendVarintBytes(b, v)
|
||||
b = quicwire.AppendVarint(b, paramOriginalDestinationConnectionID)
|
||||
b = quicwire.AppendVarintBytes(b, v)
|
||||
}
|
||||
if v := uint64(p.maxIdleTimeout / time.Millisecond); v != 0 {
|
||||
b = appendVarint(b, paramMaxIdleTimeout)
|
||||
b = appendVarint(b, uint64(sizeVarint(v)))
|
||||
b = appendVarint(b, uint64(v))
|
||||
b = quicwire.AppendVarint(b, paramMaxIdleTimeout)
|
||||
b = quicwire.AppendVarint(b, uint64(quicwire.SizeVarint(v)))
|
||||
b = quicwire.AppendVarint(b, uint64(v))
|
||||
}
|
||||
if v := p.statelessResetToken; v != nil {
|
||||
b = appendVarint(b, paramStatelessResetToken)
|
||||
b = appendVarintBytes(b, v)
|
||||
b = quicwire.AppendVarint(b, paramStatelessResetToken)
|
||||
b = quicwire.AppendVarintBytes(b, v)
|
||||
}
|
||||
if v := p.maxUDPPayloadSize; v != defaultParamMaxUDPPayloadSize {
|
||||
b = appendVarint(b, paramMaxUDPPayloadSize)
|
||||
b = appendVarint(b, uint64(sizeVarint(uint64(v))))
|
||||
b = appendVarint(b, uint64(v))
|
||||
b = quicwire.AppendVarint(b, paramMaxUDPPayloadSize)
|
||||
b = quicwire.AppendVarint(b, uint64(quicwire.SizeVarint(uint64(v))))
|
||||
b = quicwire.AppendVarint(b, uint64(v))
|
||||
}
|
||||
if v := p.initialMaxData; v != 0 {
|
||||
b = appendVarint(b, paramInitialMaxData)
|
||||
b = appendVarint(b, uint64(sizeVarint(uint64(v))))
|
||||
b = appendVarint(b, uint64(v))
|
||||
b = quicwire.AppendVarint(b, paramInitialMaxData)
|
||||
b = quicwire.AppendVarint(b, uint64(quicwire.SizeVarint(uint64(v))))
|
||||
b = quicwire.AppendVarint(b, uint64(v))
|
||||
}
|
||||
if v := p.initialMaxStreamDataBidiLocal; v != 0 {
|
||||
b = appendVarint(b, paramInitialMaxStreamDataBidiLocal)
|
||||
b = appendVarint(b, uint64(sizeVarint(uint64(v))))
|
||||
b = appendVarint(b, uint64(v))
|
||||
b = quicwire.AppendVarint(b, paramInitialMaxStreamDataBidiLocal)
|
||||
b = quicwire.AppendVarint(b, uint64(quicwire.SizeVarint(uint64(v))))
|
||||
b = quicwire.AppendVarint(b, uint64(v))
|
||||
}
|
||||
if v := p.initialMaxStreamDataBidiRemote; v != 0 {
|
||||
b = appendVarint(b, paramInitialMaxStreamDataBidiRemote)
|
||||
b = appendVarint(b, uint64(sizeVarint(uint64(v))))
|
||||
b = appendVarint(b, uint64(v))
|
||||
b = quicwire.AppendVarint(b, paramInitialMaxStreamDataBidiRemote)
|
||||
b = quicwire.AppendVarint(b, uint64(quicwire.SizeVarint(uint64(v))))
|
||||
b = quicwire.AppendVarint(b, uint64(v))
|
||||
}
|
||||
if v := p.initialMaxStreamDataUni; v != 0 {
|
||||
b = appendVarint(b, paramInitialMaxStreamDataUni)
|
||||
b = appendVarint(b, uint64(sizeVarint(uint64(v))))
|
||||
b = appendVarint(b, uint64(v))
|
||||
b = quicwire.AppendVarint(b, paramInitialMaxStreamDataUni)
|
||||
b = quicwire.AppendVarint(b, uint64(quicwire.SizeVarint(uint64(v))))
|
||||
b = quicwire.AppendVarint(b, uint64(v))
|
||||
}
|
||||
if v := p.initialMaxStreamsBidi; v != 0 {
|
||||
b = appendVarint(b, paramInitialMaxStreamsBidi)
|
||||
b = appendVarint(b, uint64(sizeVarint(uint64(v))))
|
||||
b = appendVarint(b, uint64(v))
|
||||
b = quicwire.AppendVarint(b, paramInitialMaxStreamsBidi)
|
||||
b = quicwire.AppendVarint(b, uint64(quicwire.SizeVarint(uint64(v))))
|
||||
b = quicwire.AppendVarint(b, uint64(v))
|
||||
}
|
||||
if v := p.initialMaxStreamsUni; v != 0 {
|
||||
b = appendVarint(b, paramInitialMaxStreamsUni)
|
||||
b = appendVarint(b, uint64(sizeVarint(uint64(v))))
|
||||
b = appendVarint(b, uint64(v))
|
||||
b = quicwire.AppendVarint(b, paramInitialMaxStreamsUni)
|
||||
b = quicwire.AppendVarint(b, uint64(quicwire.SizeVarint(uint64(v))))
|
||||
b = quicwire.AppendVarint(b, uint64(v))
|
||||
}
|
||||
if v := p.ackDelayExponent; v != defaultParamAckDelayExponent {
|
||||
b = appendVarint(b, paramAckDelayExponent)
|
||||
b = appendVarint(b, uint64(sizeVarint(uint64(v))))
|
||||
b = appendVarint(b, uint64(v))
|
||||
b = quicwire.AppendVarint(b, paramAckDelayExponent)
|
||||
b = quicwire.AppendVarint(b, uint64(quicwire.SizeVarint(uint64(v))))
|
||||
b = quicwire.AppendVarint(b, uint64(v))
|
||||
}
|
||||
if v := uint64(p.maxAckDelay / time.Millisecond); v != defaultParamMaxAckDelayMilliseconds {
|
||||
b = appendVarint(b, paramMaxAckDelay)
|
||||
b = appendVarint(b, uint64(sizeVarint(v)))
|
||||
b = appendVarint(b, v)
|
||||
b = quicwire.AppendVarint(b, paramMaxAckDelay)
|
||||
b = quicwire.AppendVarint(b, uint64(quicwire.SizeVarint(v)))
|
||||
b = quicwire.AppendVarint(b, v)
|
||||
}
|
||||
if p.disableActiveMigration {
|
||||
b = appendVarint(b, paramDisableActiveMigration)
|
||||
b = quicwire.AppendVarint(b, paramDisableActiveMigration)
|
||||
b = append(b, 0) // 0-length value
|
||||
}
|
||||
if p.preferredAddrConnID != nil {
|
||||
b = append(b, paramPreferredAddress)
|
||||
b = appendVarint(b, uint64(4+2+16+2+1+len(p.preferredAddrConnID)+16))
|
||||
b = quicwire.AppendVarint(b, uint64(4+2+16+2+1+len(p.preferredAddrConnID)+16))
|
||||
b = append(b, p.preferredAddrV4.Addr().AsSlice()...) // 4 bytes
|
||||
b = binary.BigEndian.AppendUint16(b, p.preferredAddrV4.Port()) // 2 bytes
|
||||
b = append(b, p.preferredAddrV6.Addr().AsSlice()...) // 16 bytes
|
||||
b = binary.BigEndian.AppendUint16(b, p.preferredAddrV6.Port()) // 2 bytes
|
||||
b = appendUint8Bytes(b, p.preferredAddrConnID) // 1 byte + len(conn_id)
|
||||
b = quicwire.AppendUint8Bytes(b, p.preferredAddrConnID) // 1 byte + len(conn_id)
|
||||
b = append(b, p.preferredAddrResetToken...) // 16 bytes
|
||||
}
|
||||
if v := p.activeConnIDLimit; v != defaultParamActiveConnIDLimit {
|
||||
b = appendVarint(b, paramActiveConnectionIDLimit)
|
||||
b = appendVarint(b, uint64(sizeVarint(uint64(v))))
|
||||
b = appendVarint(b, uint64(v))
|
||||
b = quicwire.AppendVarint(b, paramActiveConnectionIDLimit)
|
||||
b = quicwire.AppendVarint(b, uint64(quicwire.SizeVarint(uint64(v))))
|
||||
b = quicwire.AppendVarint(b, uint64(v))
|
||||
}
|
||||
if v := p.initialSrcConnID; v != nil {
|
||||
b = appendVarint(b, paramInitialSourceConnectionID)
|
||||
b = appendVarintBytes(b, v)
|
||||
b = quicwire.AppendVarint(b, paramInitialSourceConnectionID)
|
||||
b = quicwire.AppendVarintBytes(b, v)
|
||||
}
|
||||
if v := p.retrySrcConnID; v != nil {
|
||||
b = appendVarint(b, paramRetrySourceConnectionID)
|
||||
b = appendVarintBytes(b, v)
|
||||
b = quicwire.AppendVarint(b, paramRetrySourceConnectionID)
|
||||
b = quicwire.AppendVarintBytes(b, v)
|
||||
}
|
||||
return b
|
||||
}
|
||||
@@ -167,12 +169,12 @@ func marshalTransportParameters(p transportParameters) []byte {
|
||||
func unmarshalTransportParams(params []byte) (transportParameters, error) {
|
||||
p := defaultTransportParameters()
|
||||
for len(params) > 0 {
|
||||
id, n := consumeVarint(params)
|
||||
id, n := quicwire.ConsumeVarint(params)
|
||||
if n < 0 {
|
||||
return p, localTransportError{code: errTransportParameter}
|
||||
}
|
||||
params = params[n:]
|
||||
val, n := consumeVarintBytes(params)
|
||||
val, n := quicwire.ConsumeVarintBytes(params)
|
||||
if n < 0 {
|
||||
return p, localTransportError{code: errTransportParameter}
|
||||
}
|
||||
@@ -184,7 +186,7 @@ func unmarshalTransportParams(params []byte) (transportParameters, error) {
|
||||
n = len(val)
|
||||
case paramMaxIdleTimeout:
|
||||
var v uint64
|
||||
v, n = consumeVarint(val)
|
||||
v, n = quicwire.ConsumeVarint(val)
|
||||
// If this is unreasonably large, consider it as no timeout to avoid
|
||||
// time.Duration overflows.
|
||||
if v > 1<<32 {
|
||||
@@ -198,38 +200,38 @@ func unmarshalTransportParams(params []byte) (transportParameters, error) {
|
||||
p.statelessResetToken = val
|
||||
n = 16
|
||||
case paramMaxUDPPayloadSize:
|
||||
p.maxUDPPayloadSize, n = consumeVarintInt64(val)
|
||||
p.maxUDPPayloadSize, n = quicwire.ConsumeVarintInt64(val)
|
||||
if p.maxUDPPayloadSize < 1200 {
|
||||
return p, localTransportError{code: errTransportParameter}
|
||||
}
|
||||
case paramInitialMaxData:
|
||||
p.initialMaxData, n = consumeVarintInt64(val)
|
||||
p.initialMaxData, n = quicwire.ConsumeVarintInt64(val)
|
||||
case paramInitialMaxStreamDataBidiLocal:
|
||||
p.initialMaxStreamDataBidiLocal, n = consumeVarintInt64(val)
|
||||
p.initialMaxStreamDataBidiLocal, n = quicwire.ConsumeVarintInt64(val)
|
||||
case paramInitialMaxStreamDataBidiRemote:
|
||||
p.initialMaxStreamDataBidiRemote, n = consumeVarintInt64(val)
|
||||
p.initialMaxStreamDataBidiRemote, n = quicwire.ConsumeVarintInt64(val)
|
||||
case paramInitialMaxStreamDataUni:
|
||||
p.initialMaxStreamDataUni, n = consumeVarintInt64(val)
|
||||
p.initialMaxStreamDataUni, n = quicwire.ConsumeVarintInt64(val)
|
||||
case paramInitialMaxStreamsBidi:
|
||||
p.initialMaxStreamsBidi, n = consumeVarintInt64(val)
|
||||
p.initialMaxStreamsBidi, n = quicwire.ConsumeVarintInt64(val)
|
||||
if p.initialMaxStreamsBidi > maxStreamsLimit {
|
||||
return p, localTransportError{code: errTransportParameter}
|
||||
}
|
||||
case paramInitialMaxStreamsUni:
|
||||
p.initialMaxStreamsUni, n = consumeVarintInt64(val)
|
||||
p.initialMaxStreamsUni, n = quicwire.ConsumeVarintInt64(val)
|
||||
if p.initialMaxStreamsUni > maxStreamsLimit {
|
||||
return p, localTransportError{code: errTransportParameter}
|
||||
}
|
||||
case paramAckDelayExponent:
|
||||
var v uint64
|
||||
v, n = consumeVarint(val)
|
||||
v, n = quicwire.ConsumeVarint(val)
|
||||
if v > 20 {
|
||||
return p, localTransportError{code: errTransportParameter}
|
||||
}
|
||||
p.ackDelayExponent = int8(v)
|
||||
case paramMaxAckDelay:
|
||||
var v uint64
|
||||
v, n = consumeVarint(val)
|
||||
v, n = quicwire.ConsumeVarint(val)
|
||||
if v >= 1<<14 {
|
||||
return p, localTransportError{code: errTransportParameter}
|
||||
}
|
||||
@@ -251,7 +253,7 @@ func unmarshalTransportParams(params []byte) (transportParameters, error) {
|
||||
)
|
||||
val = val[16+2:]
|
||||
var nn int
|
||||
p.preferredAddrConnID, nn = consumeUint8Bytes(val)
|
||||
p.preferredAddrConnID, nn = quicwire.ConsumeUint8Bytes(val)
|
||||
if nn < 0 {
|
||||
return p, localTransportError{code: errTransportParameter}
|
||||
}
|
||||
@@ -262,7 +264,7 @@ func unmarshalTransportParams(params []byte) (transportParameters, error) {
|
||||
p.preferredAddrResetToken = val
|
||||
val = nil
|
||||
case paramActiveConnectionIDLimit:
|
||||
p.activeConnIDLimit, n = consumeVarintInt64(val)
|
||||
p.activeConnIDLimit, n = quicwire.ConsumeVarintInt64(val)
|
||||
if p.activeConnIDLimit < 2 {
|
||||
return p, localTransportError{code: errTransportParameter}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/internal/quic/quicwire"
|
||||
)
|
||||
|
||||
func TestTransportParametersMarshalUnmarshal(t *testing.T) {
|
||||
@@ -334,9 +336,9 @@ func TestTransportParameterMaxIdleTimeoutOverflowsDuration(t *testing.T) {
|
||||
tooManyMS := 1 + (math.MaxInt64 / uint64(time.Millisecond))
|
||||
|
||||
var enc []byte
|
||||
enc = appendVarint(enc, paramMaxIdleTimeout)
|
||||
enc = appendVarint(enc, uint64(sizeVarint(tooManyMS)))
|
||||
enc = appendVarint(enc, uint64(tooManyMS))
|
||||
enc = quicwire.AppendVarint(enc, paramMaxIdleTimeout)
|
||||
enc = quicwire.AppendVarint(enc, uint64(quicwire.SizeVarint(tooManyMS)))
|
||||
enc = quicwire.AppendVarint(enc, uint64(tooManyMS))
|
||||
|
||||
dec, err := unmarshalTransportParams(enc)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user