remove the LongHeader field from the wire.Header

This commit is contained in:
Marten Seemann 2022-12-27 12:03:38 +13:00
parent aca052dc7c
commit 86edf7fd4b
23 changed files with 154 additions and 363 deletions

View file

@ -878,7 +878,7 @@ func (s *connection) handlePacketImpl(rp *receivedPacket) bool {
}
if wire.IsLongHeaderPacket(p.data[0]) {
hdr, packetData, rest, err := wire.ParsePacket(p.data, s.srcConnIDLen)
hdr, packetData, rest, err := wire.ParsePacket(p.data)
if err != nil {
if s.tracer != nil {
dropReason := logging.PacketDropHeaderParseError
@ -1030,7 +1030,7 @@ func (s *connection) handleLongHeaderPacket(p *receivedPacket, hdr *wire.Header)
return false
}
if err := s.handleUnpackedPacket(packet, p.ecn, p.rcvTime, p.Size()); err != nil {
if err := s.handleUnpackedLongHeaderPacket(packet, p.ecn, p.rcvTime, p.Size()); err != nil {
s.closeLocal(err)
return false
}
@ -1193,7 +1193,7 @@ func (s *connection) handleVersionNegotiationPacket(p *receivedPacket) {
})
}
func (s *connection) handleUnpackedPacket(
func (s *connection) handleUnpackedLongHeaderPacket(
packet *unpackedPacket,
ecn protocol.ECN,
rcvTime time.Time,
@ -1212,7 +1212,7 @@ func (s *connection) handleUnpackedPacket(
s.tracer.NegotiatedVersion(s.version, clientVersions, serverVersions)
}
// The server can change the source connection ID with the first Handshake packet.
if s.perspective == protocol.PerspectiveClient && packet.hdr.IsLongHeader && packet.hdr.SrcConnectionID != s.handshakeDestConnID {
if s.perspective == protocol.PerspectiveClient && packet.hdr.SrcConnectionID != s.handshakeDestConnID {
cid := packet.hdr.SrcConnectionID
s.logger.Debugf("Received first packet. Switching destination connection ID to: %s", cid)
s.handshakeDestConnID = cid

View file

@ -66,7 +66,7 @@ var _ = Describe("Connection", func() {
if isLongHeader {
packet.longHdrPackets = []*longHeaderPacket{{
header: &wire.ExtendedHeader{
Header: wire.Header{IsLongHeader: true},
Header: wire.Header{},
PacketNumber: pn,
},
length: 6, // foobar
@ -659,7 +659,6 @@ var _ = Describe("Connection", func() {
}
getLongHeaderPacket := func(extHdr *wire.ExtendedHeader, data []byte) *receivedPacket {
ExpectWithOffset(1, extHdr.IsLongHeader).To(BeTrue())
buf := &bytes.Buffer{}
Expect(extHdr.Write(buf, conn.version)).To(Succeed())
return &receivedPacket{
@ -671,7 +670,6 @@ var _ = Describe("Connection", func() {
It("drops Retry packets", func() {
p := getLongHeaderPacket(&wire.ExtendedHeader{Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeRetry,
DestConnectionID: destConnID,
SrcConnectionID: srcConnID,
@ -698,9 +696,8 @@ var _ = Describe("Connection", func() {
It("drops packets for which header decryption fails", func() {
p := getLongHeaderPacket(&wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
Version: conn.version,
Type: protocol.PacketTypeHandshake,
Version: conn.version,
},
PacketNumberLen: protocol.PacketNumberLen2,
}, nil)
@ -712,9 +709,8 @@ var _ = Describe("Connection", func() {
It("drops packets for which the version is unsupported", func() {
p := getLongHeaderPacket(&wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
Version: conn.version + 1,
Type: protocol.PacketTypeHandshake,
Version: conn.version + 1,
},
PacketNumberLen: protocol.PacketNumberLen2,
}, nil)
@ -732,7 +728,6 @@ var _ = Describe("Connection", func() {
protocol.SupportedVersions = append(protocol.SupportedVersions, conn.version+1)
p := getLongHeaderPacket(&wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
DestConnectionID: destConnID,
SrcConnectionID: srcConnID,
@ -747,7 +742,6 @@ var _ = Describe("Connection", func() {
It("informs the ReceivedPacketHandler about non-ack-eliciting packets", func() {
hdr := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: srcConnID,
Version: protocol.Version1,
@ -819,7 +813,6 @@ var _ = Describe("Connection", func() {
expectReplaceWithClosed()
p := getLongHeaderPacket(&wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
DestConnectionID: srcConnID,
Version: conn.version,
@ -982,7 +975,6 @@ var _ = Describe("Connection", func() {
It("ignores packets with a different source connection ID", func() {
hdr1 := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: destConnID,
SrcConnectionID: srcConnID,
@ -994,7 +986,6 @@ var _ = Describe("Connection", func() {
}
hdr2 := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: destConnID,
SrcConnectionID: protocol.ParseConnectionID([]byte{0xde, 0xad, 0xbe, 0xef}),
@ -1026,7 +1017,6 @@ var _ = Describe("Connection", func() {
conn.handshakeComplete = false
hdr := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
DestConnectionID: destConnID,
SrcConnectionID: srcConnID,
@ -1060,7 +1050,6 @@ var _ = Describe("Connection", func() {
getPacketWithLength := func(connID protocol.ConnectionID, length protocol.ByteCount) (int /* header length */, *receivedPacket) {
hdr := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
DestConnectionID: connID,
SrcConnectionID: destConnID,
@ -1083,7 +1072,7 @@ var _ = Describe("Connection", func() {
return &unpackedPacket{
encryptionLevel: protocol.EncryptionHandshake,
data: []byte{0},
hdr: &wire.ExtendedHeader{Header: wire.Header{IsLongHeader: true}},
hdr: &wire.ExtendedHeader{Header: wire.Header{}},
}, nil
})
tracer.EXPECT().ReceivedLongHeaderPacket(gomock.Any(), protocol.ByteCount(len(packet.data)), gomock.Any())
@ -1099,7 +1088,7 @@ var _ = Describe("Connection", func() {
data: []byte{0},
hdr: &wire.ExtendedHeader{
PacketNumber: 1,
Header: wire.Header{SrcConnectionID: destConnID, IsLongHeader: true},
Header: wire.Header{SrcConnectionID: destConnID},
},
}, nil
})
@ -1111,7 +1100,7 @@ var _ = Describe("Connection", func() {
data: []byte{0},
hdr: &wire.ExtendedHeader{
PacketNumber: 2,
Header: wire.Header{SrcConnectionID: destConnID, IsLongHeader: true},
Header: wire.Header{SrcConnectionID: destConnID},
},
}, nil
})
@ -1134,7 +1123,7 @@ var _ = Describe("Connection", func() {
return &unpackedPacket{
encryptionLevel: protocol.EncryptionHandshake,
data: []byte{0},
hdr: &wire.ExtendedHeader{Header: wire.Header{IsLongHeader: true}},
hdr: &wire.ExtendedHeader{Header: wire.Header{}},
}, nil
}),
)
@ -1158,7 +1147,7 @@ var _ = Describe("Connection", func() {
return &unpackedPacket{
encryptionLevel: protocol.EncryptionHandshake,
data: []byte{0},
hdr: &wire.ExtendedHeader{Header: wire.Header{IsLongHeader: true}},
hdr: &wire.ExtendedHeader{Header: wire.Header{}},
}, nil
})
_, packet2 := getPacketWithLength(wrongConnID, 123)
@ -1761,20 +1750,14 @@ var _ = Describe("Connection", func() {
longHdrPackets: []*longHeaderPacket{
{
header: &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
},
Header: wire.Header{Type: protocol.PacketTypeInitial},
PacketNumber: 13,
},
length: 123,
},
{
header: &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
},
Header: wire.Header{Type: protocol.PacketTypeHandshake},
PacketNumber: 37,
},
length: 1234,
@ -2454,7 +2437,6 @@ var _ = Describe("Client Connection", func() {
newConnID := protocol.ParseConnectionID([]byte{1, 3, 3, 7, 1, 3, 3, 7})
p := getPacket(&wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
SrcConnectionID: newConnID,
DestConnectionID: srcConnID,
@ -2495,7 +2477,6 @@ var _ = Describe("Client Connection", func() {
}, nil
})
hdr := &wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
DestConnectionID: srcConnID,
SrcConnectionID: destConnID,
@ -2652,7 +2633,6 @@ var _ = Describe("Client Connection", func() {
JustBeforeEach(func() {
retryHdr = &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeRetry,
SrcConnectionID: protocol.ParseConnectionID([]byte{0xde, 0xad, 0xbe, 0xef}),
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}),
@ -2903,7 +2883,6 @@ var _ = Describe("Client Connection", func() {
hdr1 := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: destConnID,
SrcConnectionID: srcConnID,
@ -2915,7 +2894,6 @@ var _ = Describe("Client Connection", func() {
}
hdr2 := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: destConnID,
SrcConnectionID: protocol.ParseConnectionID([]byte{0xde, 0xad, 0xbe, 0xef}),
@ -2943,7 +2921,6 @@ var _ = Describe("Client Connection", func() {
It("ignores 0-RTT packets", func() {
p := getPacket(&wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketType0RTT,
DestConnectionID: srcConnID,
Length: 2 + 6,

View file

@ -30,7 +30,6 @@ func getVNP(src, dest protocol.ArbitraryLenConnectionID, numVersions int) []byte
func main() {
headers := []wire.Header{
{ // Initial without token
IsLongHeader: true,
SrcConnectionID: protocol.ParseConnectionID(getRandomData(3)),
DestConnectionID: protocol.ParseConnectionID(getRandomData(8)),
Type: protocol.PacketTypeInitial,
@ -38,14 +37,12 @@ func main() {
Version: version,
},
{ // Initial without token, with zero-length src conn id
IsLongHeader: true,
DestConnectionID: protocol.ParseConnectionID(getRandomData(8)),
Type: protocol.PacketTypeInitial,
Length: protocol.ByteCount(rand.Intn(1000)),
Version: version,
},
{ // Initial with Token
IsLongHeader: true,
SrcConnectionID: protocol.ParseConnectionID(getRandomData(10)),
DestConnectionID: protocol.ParseConnectionID(getRandomData(19)),
Type: protocol.PacketTypeInitial,
@ -54,7 +51,6 @@ func main() {
Token: getRandomData(25),
},
{ // Handshake packet
IsLongHeader: true,
SrcConnectionID: protocol.ParseConnectionID(getRandomData(5)),
DestConnectionID: protocol.ParseConnectionID(getRandomData(10)),
Type: protocol.PacketTypeHandshake,
@ -62,14 +58,12 @@ func main() {
Version: version,
},
{ // Handshake packet, with zero-length src conn id
IsLongHeader: true,
DestConnectionID: protocol.ParseConnectionID(getRandomData(12)),
Type: protocol.PacketTypeHandshake,
Length: protocol.ByteCount(rand.Intn(1000)),
Version: version,
},
{ // 0-RTT packet
IsLongHeader: true,
SrcConnectionID: protocol.ParseConnectionID(getRandomData(8)),
DestConnectionID: protocol.ParseConnectionID(getRandomData(9)),
Type: protocol.PacketType0RTT,
@ -77,7 +71,6 @@ func main() {
Version: version,
},
{ // Retry Packet, with empty orig dest conn id
IsLongHeader: true,
SrcConnectionID: protocol.ParseConnectionID(getRandomData(8)),
DestConnectionID: protocol.ParseConnectionID(getRandomData(9)),
Type: protocol.PacketTypeRetry,

View file

@ -30,8 +30,14 @@ func Fuzz(data []byte) int {
if err != nil {
return 0
}
if !wire.IsLongHeaderPacket(data[0]) {
wire.ParseShortHeader(data, connIDLen)
return 1
}
is0RTTPacket := wire.Is0RTTPacket(data)
hdr, _, _, err := wire.ParsePacket(data, connIDLen)
hdr, _, _, err := wire.ParsePacket(data)
if err != nil {
return 0
}
@ -42,11 +48,6 @@ func Fuzz(data []byte) int {
panic("inconsistent 0-RTT packet detection")
}
if !wire.IsLongHeaderPacket(data[0]) {
wire.ParseShortHeader(data, connIDLen)
return 1
}
var extHdr *wire.ExtendedHeader
// Parse the extended header, if this is not a Retry packet.
if hdr.Type == protocol.PacketTypeRetry {
@ -60,7 +61,7 @@ func Fuzz(data []byte) int {
}
// We always use a 2-byte encoding for the Length field in Long Header packets.
// Serializing the header will fail when using a higher value.
if hdr.IsLongHeader && hdr.Length > 16383 {
if hdr.Length > 16383 {
return 1
}
b := &bytes.Buffer{}

View file

@ -100,11 +100,10 @@ var _ = Describe("MITM test", func() {
defer ticker.Stop()
if wire.IsLongHeaderPacket(raw[0]) {
hdr, _, _, err := wire.ParsePacket(raw, connIDLen)
hdr, _, _, err := wire.ParsePacket(raw)
Expect(err).ToNot(HaveOccurred())
replyHdr := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: hdr.IsLongHeader,
DestConnectionID: hdr.DestConnectionID,
SrcConnectionID: hdr.SrcConnectionID,
Type: hdr.Type,
@ -340,7 +339,7 @@ var _ = Describe("MITM test", func() {
if dir == quicproxy.DirectionIncoming {
defer GinkgoRecover()
hdr, _, _, err := wire.ParsePacket(raw, connIDLen)
hdr, _, _, err := wire.ParsePacket(raw)
Expect(err).ToNot(HaveOccurred())
if hdr.Type != protocol.PacketTypeInitial {
@ -382,7 +381,7 @@ var _ = Describe("MITM test", func() {
defer GinkgoRecover()
defer close(done)
hdr, _, _, err := wire.ParsePacket(raw, connIDLen)
hdr, _, _, err := wire.ParsePacket(raw)
Expect(err).ToNot(HaveOccurred())
if hdr.Type != protocol.PacketTypeInitial {
@ -415,7 +414,7 @@ var _ = Describe("MITM test", func() {
if dir == quicproxy.DirectionIncoming {
defer GinkgoRecover()
hdr, _, _, err := wire.ParsePacket(raw, connIDLen)
hdr, _, _, err := wire.ParsePacket(raw)
Expect(err).ToNot(HaveOccurred())
if hdr.Type != protocol.PacketTypeInitial || injected {
return 0
@ -443,7 +442,7 @@ var _ = Describe("MITM test", func() {
if dir == quicproxy.DirectionIncoming {
defer GinkgoRecover()
hdr, _, _, err := wire.ParsePacket(raw, connIDLen)
hdr, _, _, err := wire.ParsePacket(raw)
Expect(err).ToNot(HaveOccurred())
if hdr.Type != protocol.PacketTypeInitial || injected {
return 0

View file

@ -38,7 +38,7 @@ var _ = Describe("0-RTT", func() {
if !wire.IsLongHeaderPacket(data[0]) {
break
}
hdr, _, rest, err := wire.ParsePacket(data, 0)
hdr, _, rest, err := wire.ParsePacket(data)
Expect(err).ToNot(HaveOccurred())
if hdr.Type == protocol.PacketType0RTT {
atomic.AddUint32(&num0RTTPackets, 1)
@ -351,7 +351,7 @@ var _ = Describe("0-RTT", func() {
RemoteAddr: fmt.Sprintf("localhost:%d", ln.Addr().(*net.UDPAddr).Port),
DelayPacket: func(_ quicproxy.Direction, data []byte) time.Duration {
if wire.IsLongHeaderPacket(data[0]) {
hdr, _, _, err := wire.ParsePacket(data, 0)
hdr, _, _, err := wire.ParsePacket(data)
Expect(err).ToNot(HaveOccurred())
if hdr.Type == protocol.PacketType0RTT {
atomic.AddUint32(&num0RTTPackets, 1)
@ -363,7 +363,7 @@ var _ = Describe("0-RTT", func() {
if !wire.IsLongHeaderPacket(data[0]) {
return false
}
hdr, _, _, err := wire.ParsePacket(data, 0)
hdr, _, _, err := wire.ParsePacket(data)
Expect(err).ToNot(HaveOccurred())
if hdr.Type == protocol.PacketType0RTT {
// drop 25% of the 0-RTT packets
@ -398,7 +398,7 @@ var _ = Describe("0-RTT", func() {
countZeroRTTBytes := func(data []byte) (n protocol.ByteCount) {
for len(data) > 0 {
hdr, _, rest, err := wire.ParsePacket(data, 0)
hdr, _, rest, err := wire.ParsePacket(data)
if err != nil {
return
}

View file

@ -31,7 +31,6 @@ var _ = Describe("QUIC Proxy", func() {
b := &bytes.Buffer{}
hdr := wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
Version: protocol.VersionTLS,
Length: 4 + protocol.ByteCount(len(payload)),
@ -48,7 +47,7 @@ var _ = Describe("QUIC Proxy", func() {
}
readPacketNumber := func(b []byte) protocol.PacketNumber {
hdr, data, _, err := wire.ParsePacket(b, 0)
hdr, data, _, err := wire.ParsePacket(b)
ExpectWithOffset(1, err).ToNot(HaveOccurred())
Expect(hdr.Type).To(Equal(protocol.PacketTypeInitial))
extHdr, err := hdr.ParseExtended(bytes.NewReader(data), protocol.VersionTLS)

View file

@ -50,7 +50,6 @@ func ComposeInitialPacket(srcConnID protocol.ConnectionID, destConnID protocol.C
length := payloadSize + int(pnLength) + sealer.Overhead()
hdr := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
SrcConnectionID: srcConnID,
DestConnectionID: destConnID,
@ -88,7 +87,6 @@ func ComposeRetryPacket(
) []byte {
hdr := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeRetry,
SrcConnectionID: srcConnID,
DestConnectionID: destConnID,

View file

@ -42,12 +42,7 @@ func (h *ExtendedHeader) parse(b *bytes.Reader, v protocol.VersionNumber) (bool
if _, err := b.Seek(int64(h.Header.ParsedLen())-1, io.SeekCurrent); err != nil {
return false, err
}
var reservedBitsValid bool
if h.IsLongHeader {
reservedBitsValid, err = h.parseLongHeader(b, v)
} else {
panic("parsed a short header packet")
}
reservedBitsValid, err := h.parseLongHeader(b, v)
if err != nil {
return false, err
}
@ -106,10 +101,7 @@ func (h *ExtendedHeader) Write(b *bytes.Buffer, ver protocol.VersionNumber) erro
if h.SrcConnectionID.Len() > protocol.MaxConnIDLen {
return fmt.Errorf("invalid connection ID length: %d bytes", h.SrcConnectionID.Len())
}
if h.IsLongHeader {
return h.writeLongHeader(b, ver)
}
panic("tried to write short extended header")
return h.writeLongHeader(b, ver)
}
func (h *ExtendedHeader) writeLongHeader(b *bytes.Buffer, version protocol.VersionNumber) error {
@ -171,39 +163,29 @@ func (h *ExtendedHeader) ParsedLen() protocol.ByteCount {
}
// GetLength determines the length of the Header.
func (h *ExtendedHeader) GetLength(v protocol.VersionNumber) protocol.ByteCount {
if h.IsLongHeader {
length := 1 /* type byte */ + 4 /* version */ + 1 /* dest conn ID len */ + protocol.ByteCount(h.DestConnectionID.Len()) + 1 /* src conn ID len */ + protocol.ByteCount(h.SrcConnectionID.Len()) + protocol.ByteCount(h.PacketNumberLen) + 2 /* length */
if h.Type == protocol.PacketTypeInitial {
length += quicvarint.Len(uint64(len(h.Token))) + protocol.ByteCount(len(h.Token))
}
return length
func (h *ExtendedHeader) GetLength(_ protocol.VersionNumber) protocol.ByteCount {
length := 1 /* type byte */ + 4 /* version */ + 1 /* dest conn ID len */ + protocol.ByteCount(h.DestConnectionID.Len()) + 1 /* src conn ID len */ + protocol.ByteCount(h.SrcConnectionID.Len()) + protocol.ByteCount(h.PacketNumberLen) + 2 /* length */
if h.Type == protocol.PacketTypeInitial {
length += quicvarint.Len(uint64(len(h.Token))) + protocol.ByteCount(len(h.Token))
}
length := protocol.ByteCount(1 /* type byte */ + h.DestConnectionID.Len())
length += protocol.ByteCount(h.PacketNumberLen)
return length
}
// Log logs the Header
func (h *ExtendedHeader) Log(logger utils.Logger) {
if h.IsLongHeader {
var token string
if h.Type == protocol.PacketTypeInitial || h.Type == protocol.PacketTypeRetry {
if len(h.Token) == 0 {
token = "Token: (empty), "
} else {
token = fmt.Sprintf("Token: %#x, ", h.Token)
}
if h.Type == protocol.PacketTypeRetry {
logger.Debugf("\tLong Header{Type: %s, DestConnectionID: %s, SrcConnectionID: %s, %sVersion: %s}", h.Type, h.DestConnectionID, h.SrcConnectionID, token, h.Version)
return
}
var token string
if h.Type == protocol.PacketTypeInitial || h.Type == protocol.PacketTypeRetry {
if len(h.Token) == 0 {
token = "Token: (empty), "
} else {
token = fmt.Sprintf("Token: %#x, ", h.Token)
}
if h.Type == protocol.PacketTypeRetry {
logger.Debugf("\tLong Header{Type: %s, DestConnectionID: %s, SrcConnectionID: %s, %sVersion: %s}", h.Type, h.DestConnectionID, h.SrcConnectionID, token, h.Version)
return
}
logger.Debugf("\tLong Header{Type: %s, DestConnectionID: %s, SrcConnectionID: %s, %sPacketNumber: %d, PacketNumberLen: %d, Length: %d, Version: %s}", h.Type, h.DestConnectionID, h.SrcConnectionID, token, h.PacketNumber, h.PacketNumberLen, h.Length, h.Version)
} else {
panic("logged short ExtendedHeader")
}
logger.Debugf("\tLong Header{Type: %s, DestConnectionID: %s, SrcConnectionID: %s, %sPacketNumber: %d, PacketNumberLen: %d, Length: %d, Version: %s}", h.Type, h.DestConnectionID, h.SrcConnectionID, token, h.PacketNumber, h.PacketNumberLen, h.Length, h.Version)
}
func writePacketNumber(b *bytes.Buffer, pn protocol.PacketNumber, pnLen protocol.PacketNumberLen) error {

View file

@ -14,7 +14,7 @@ import (
)
var _ = Describe("Header", func() {
const versionIETFHeader = protocol.VersionTLS // a QUIC version that uses the IETF Header format
const versionIETFHeader = protocol.Version1
Context("Writing", func() {
var buf *bytes.Buffer
@ -29,7 +29,6 @@ var _ = Describe("Header", func() {
It("writes", func() {
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
DestConnectionID: protocol.ParseConnectionID([]byte{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe}),
SrcConnectionID: protocol.ParseConnectionID([]byte{0xde, 0xca, 0xfb, 0xad, 0x0, 0x0, 0x13, 0x37}),
@ -55,7 +54,6 @@ var _ = Describe("Header", func() {
It("writes a header with a 20 byte connection ID", func() {
err := (&ExtendedHeader{
Header: Header{
IsLongHeader: true,
SrcConnectionID: srcConnID,
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}), // connection IDs must be at most 20 bytes long
Version: 0x1020304,
@ -72,10 +70,9 @@ var _ = Describe("Header", func() {
token := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.")
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Version: 0x1020304,
Type: protocol.PacketTypeInitial,
Token: token,
Version: 0x1020304,
Type: protocol.PacketTypeInitial,
Token: token,
},
PacketNumber: 0xdecafbad,
PacketNumberLen: protocol.PacketNumberLen4,
@ -88,10 +85,9 @@ var _ = Describe("Header", func() {
It("uses a 2-byte encoding for the length on Initial packets", func() {
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Version: 0x1020304,
Type: protocol.PacketTypeInitial,
Length: 37,
Version: 0x1020304,
Type: protocol.PacketTypeInitial,
Length: 37,
},
PacketNumber: 0xdecafbad,
PacketNumberLen: protocol.PacketNumberLen4,
@ -104,10 +100,9 @@ var _ = Describe("Header", func() {
It("writes a Retry packet", func() {
token := []byte("Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.")
Expect((&ExtendedHeader{Header: Header{
IsLongHeader: true,
Version: protocol.Version1,
Type: protocol.PacketTypeRetry,
Token: token,
Version: protocol.Version1,
Type: protocol.PacketTypeRetry,
Token: token,
}}).Write(buf, versionIETFHeader)).To(Succeed())
expected := []byte{0xc0 | 0b11<<4}
expected = appendVersion(expected, protocol.Version1)
@ -122,9 +117,8 @@ var _ = Describe("Header", func() {
It("writes an Initial", func() {
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Version: protocol.Version2,
Type: protocol.PacketTypeInitial,
Version: protocol.Version2,
Type: protocol.PacketTypeInitial,
},
PacketNumber: 0xdecafbad,
PacketNumberLen: protocol.PacketNumberLen4,
@ -135,10 +129,9 @@ var _ = Describe("Header", func() {
It("writes a Retry packet", func() {
token := []byte("Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.")
Expect((&ExtendedHeader{Header: Header{
IsLongHeader: true,
Version: protocol.Version2,
Type: protocol.PacketTypeRetry,
Token: token,
Version: protocol.Version2,
Type: protocol.PacketTypeRetry,
Token: token,
}}).Write(buf, versionIETFHeader)).To(Succeed())
expected := []byte{0xc0 | 0b11<<4}
expected = appendVersion(expected, protocol.Version2)
@ -151,9 +144,8 @@ var _ = Describe("Header", func() {
It("writes a Handshake Packet", func() {
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Version: protocol.Version2,
Type: protocol.PacketTypeHandshake,
Version: protocol.Version2,
Type: protocol.PacketTypeHandshake,
},
PacketNumber: 0xdecafbad,
PacketNumberLen: protocol.PacketNumberLen4,
@ -164,9 +156,8 @@ var _ = Describe("Header", func() {
It("writes a 0-RTT Packet", func() {
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Version: protocol.Version2,
Type: protocol.PacketType0RTT,
Version: protocol.Version2,
Type: protocol.PacketType0RTT,
},
PacketNumber: 0xdecafbad,
PacketNumberLen: protocol.PacketNumberLen4,
@ -186,7 +177,6 @@ var _ = Describe("Header", func() {
It("has the right length for the Long Header, for a short length", func() {
h := &ExtendedHeader{
Header: Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}),
SrcConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}),
@ -203,7 +193,6 @@ var _ = Describe("Header", func() {
It("has the right length for the Long Header, for a long length", func() {
h := &ExtendedHeader{
Header: Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}),
SrcConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}),
@ -220,7 +209,6 @@ var _ = Describe("Header", func() {
It("has the right length for an Initial that has a short length", func() {
h := &ExtendedHeader{
Header: Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}),
SrcConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4}),
@ -237,7 +225,6 @@ var _ = Describe("Header", func() {
It("has the right length for an Initial not containing a Token", func() {
h := &ExtendedHeader{
Header: Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}),
SrcConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4}),
@ -254,7 +241,6 @@ var _ = Describe("Header", func() {
It("has the right length for an Initial containing a Token", func() {
h := &ExtendedHeader{
Header: Header{
IsLongHeader: true,
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}),
SrcConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4}),
Type: protocol.PacketTypeInitial,
@ -290,7 +276,6 @@ var _ = Describe("Header", func() {
It("logs Long Headers", func() {
(&ExtendedHeader{
Header: Header{
IsLongHeader: true,
DestConnectionID: protocol.ParseConnectionID([]byte{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}),
SrcConnectionID: protocol.ParseConnectionID([]byte{0xde, 0xca, 0xfb, 0xad, 0x013, 0x37, 0x13, 0x37}),
Type: protocol.PacketTypeHandshake,
@ -306,7 +291,6 @@ var _ = Describe("Header", func() {
It("logs Initial Packets with a Token", func() {
(&ExtendedHeader{
Header: Header{
IsLongHeader: true,
DestConnectionID: protocol.ParseConnectionID([]byte{0xca, 0xfe, 0x13, 0x37}),
SrcConnectionID: protocol.ParseConnectionID([]byte{0xde, 0xca, 0xfb, 0xad}),
Type: protocol.PacketTypeInitial,
@ -323,7 +307,6 @@ var _ = Describe("Header", func() {
It("logs Initial packets without a Token", func() {
(&ExtendedHeader{
Header: Header{
IsLongHeader: true,
DestConnectionID: protocol.ParseConnectionID([]byte{0xca, 0xfe, 0x13, 0x37}),
SrcConnectionID: protocol.ParseConnectionID([]byte{0xde, 0xca, 0xfb, 0xad}),
Type: protocol.PacketTypeInitial,
@ -339,7 +322,6 @@ var _ = Describe("Header", func() {
It("logs Retry packets with a Token", func() {
(&ExtendedHeader{
Header: Header{
IsLongHeader: true,
DestConnectionID: protocol.ParseConnectionID([]byte{0xca, 0xfe, 0x13, 0x37}),
SrcConnectionID: protocol.ParseConnectionID([]byte{0xde, 0xca, 0xfb, 0xad}),
Type: protocol.PacketTypeRetry,

View file

@ -121,9 +121,8 @@ var ErrUnsupportedVersion = errors.New("unsupported version")
// The Header is the version independent part of the header
type Header struct {
IsLongHeader bool
typeByte byte
Type protocol.PacketType
typeByte byte
Type protocol.PacketType
Version protocol.VersionNumber
SrcConnectionID protocol.ConnectionID
@ -140,24 +139,22 @@ type Header struct {
// If the packet has a long header, the packet is cut according to the length field.
// If we understand the version, the packet is header up unto the packet number.
// Otherwise, only the invariant part of the header is parsed.
func ParsePacket(data []byte, shortHeaderConnIDLen int) (*Header, []byte /* packet data */, []byte /* rest */, error) {
hdr, err := parseHeader(bytes.NewReader(data), shortHeaderConnIDLen)
func ParsePacket(data []byte) (*Header, []byte, []byte, error) {
if len(data) == 0 || !IsLongHeaderPacket(data[0]) {
return nil, nil, nil, errors.New("not a long header packet")
}
hdr, err := parseHeader(bytes.NewReader(data))
if err != nil {
if err == ErrUnsupportedVersion {
return hdr, nil, nil, ErrUnsupportedVersion
}
return nil, nil, nil, err
}
var rest []byte
if hdr.IsLongHeader {
if protocol.ByteCount(len(data)) < hdr.ParsedLen()+hdr.Length {
return nil, nil, nil, fmt.Errorf("packet length (%d bytes) is smaller than the expected length (%d bytes)", len(data)-int(hdr.ParsedLen()), hdr.Length)
}
packetLen := int(hdr.ParsedLen() + hdr.Length)
rest = data[packetLen:]
data = data[:packetLen]
if protocol.ByteCount(len(data)) < hdr.ParsedLen()+hdr.Length {
return nil, nil, nil, fmt.Errorf("packet length (%d bytes) is smaller than the expected length (%d bytes)", len(data)-int(hdr.ParsedLen()), hdr.Length)
}
return hdr, data, rest, nil
packetLen := int(hdr.ParsedLen() + hdr.Length)
return hdr, data[:packetLen], data[packetLen:], nil
}
// ParseHeader parses the header.
@ -165,43 +162,17 @@ func ParsePacket(data []byte, shortHeaderConnIDLen int) (*Header, []byte /* pack
// For long header packets:
// * if we understand the version: up to the packet number
// * if not, only the invariant part of the header
func parseHeader(b *bytes.Reader, shortHeaderConnIDLen int) (*Header, error) {
func parseHeader(b *bytes.Reader) (*Header, error) {
startLen := b.Len()
h, err := parseHeaderImpl(b, shortHeaderConnIDLen)
if err != nil {
return h, err
}
h.parsedLen = protocol.ByteCount(startLen - b.Len())
return h, err
}
func parseHeaderImpl(b *bytes.Reader, shortHeaderConnIDLen int) (*Header, error) {
typeByte, err := b.ReadByte()
if err != nil {
return nil, err
}
h := &Header{
typeByte: typeByte,
IsLongHeader: IsLongHeaderPacket(typeByte),
}
if !h.IsLongHeader {
if h.typeByte&0x40 == 0 {
return nil, errors.New("not a QUIC packet")
}
if err := h.parseShortHeader(b, shortHeaderConnIDLen); err != nil {
return nil, err
}
return h, nil
}
return h, h.parseLongHeader(b)
}
func (h *Header) parseShortHeader(b *bytes.Reader, shortHeaderConnIDLen int) error {
var err error
h.DestConnectionID, err = protocol.ReadConnectionID(b, shortHeaderConnIDLen)
return err
h := &Header{typeByte: typeByte}
err = h.parseLongHeader(b)
h.parsedLen = protocol.ByteCount(startLen - b.Len())
return h, err
}
func (h *Header) parseLongHeader(b *bytes.Reader) error {
@ -321,8 +292,5 @@ func (h *Header) toExtendedHeader() *ExtendedHeader {
// PacketType is the type of the packet, for logging purposes
func (h *Header) PacketType() string {
if h.IsLongHeader {
return h.Type.String()
}
return "1-RTT"
return h.Type.String()
}

View file

@ -18,7 +18,6 @@ var _ = Describe("Header Parsing", func() {
buf := &bytes.Buffer{}
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
DestConnectionID: protocol.ParseConnectionID([]byte{0xde, 0xca, 0xfb, 0xad}),
SrcConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6}),
@ -35,7 +34,6 @@ var _ = Describe("Header Parsing", func() {
buf := &bytes.Buffer{}
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
DestConnectionID: protocol.ParseConnectionID([]byte{0xde, 0xca, 0xfb, 0xad, 0x13, 0x37}),
SrcConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 8, 9}),
@ -186,10 +184,9 @@ var _ = Describe("Header Parsing", func() {
data = append(data, []byte("foobar")...)
Expect(IsVersionNegotiationPacket(data)).To(BeFalse())
hdr, pdata, rest, err := ParsePacket(data, 0)
hdr, pdata, rest, err := ParsePacket(data)
Expect(err).ToNot(HaveOccurred())
Expect(pdata).To(Equal(data))
Expect(hdr.IsLongHeader).To(BeTrue())
Expect(hdr.DestConnectionID).To(Equal(destConnID))
Expect(hdr.SrcConnectionID).To(Equal(srcConnID))
Expect(hdr.Type).To(Equal(protocol.PacketTypeInitial))
@ -214,7 +211,7 @@ var _ = Describe("Header Parsing", func() {
0xde, 0xca, 0xfb, 0xad, // dest conn ID
0xde, 0xad, 0xbe, 0xef, // src conn ID
}
_, _, _, err := ParsePacket(data, 0)
_, _, _, err := ParsePacket(data)
Expect(err).To(MatchError("not a QUIC packet"))
})
@ -228,9 +225,8 @@ var _ = Describe("Header Parsing", func() {
0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, // src conn ID
'f', 'o', 'o', 'b', 'a', 'r', // unspecified bytes
}
hdr, _, rest, err := ParsePacket(data, 0)
hdr, _, rest, err := ParsePacket(data)
Expect(err).To(MatchError(ErrUnsupportedVersion))
Expect(hdr.IsLongHeader).To(BeTrue())
Expect(hdr.Version).To(Equal(protocol.VersionNumber(0xdeadbeef)))
Expect(hdr.DestConnectionID).To(Equal(protocol.ParseConnectionID([]byte{0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8})))
Expect(hdr.SrcConnectionID).To(Equal(protocol.ParseConnectionID([]byte{0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1})))
@ -245,7 +241,7 @@ var _ = Describe("Header Parsing", func() {
data = append(data, []byte{0xde, 0xad, 0xbe, 0xef}...) // source connection ID
data = append(data, encodeVarInt(0)...) // length
data = append(data, []byte{0xde, 0xca, 0xfb, 0xad}...)
hdr, _, _, err := ParsePacket(data, 0)
hdr, _, _, err := ParsePacket(data)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.Type).To(Equal(protocol.PacketType0RTT))
Expect(hdr.SrcConnectionID).To(Equal(protocol.ParseConnectionID([]byte{0xde, 0xad, 0xbe, 0xef})))
@ -260,7 +256,7 @@ var _ = Describe("Header Parsing", func() {
data = append(data, 0) // src conn ID len
data = append(data, encodeVarInt(0)...) // length
data = append(data, []byte{0xde, 0xca, 0xfb, 0xad}...)
hdr, _, _, err := ParsePacket(data, 0)
hdr, _, _, err := ParsePacket(data)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.SrcConnectionID).To(BeZero())
Expect(hdr.DestConnectionID).To(Equal(protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})))
@ -274,7 +270,7 @@ var _ = Describe("Header Parsing", func() {
data = append(data, 0x0) // src conn ID len
data = append(data, encodeVarInt(0)...) // length
data = append(data, []byte{0xde, 0xca, 0xfb, 0xad}...)
_, _, _, err := ParsePacket(data, 0)
_, _, _, err := ParsePacket(data)
Expect(err).To(MatchError(protocol.ErrInvalidConnectionIDLen))
})
@ -286,7 +282,7 @@ var _ = Describe("Header Parsing", func() {
data = append(data, encodeVarInt(0)...) // length
data = append(data, []byte{0x1, 0x23}...)
hdr, _, _, err := ParsePacket(data, 0)
hdr, _, _, err := ParsePacket(data)
Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data)
extHdr, err := hdr.ParseExtended(b, protocol.Version1)
@ -305,7 +301,7 @@ var _ = Describe("Header Parsing", func() {
data = append(data, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}...) // source connection ID
data = append(data, []byte{'f', 'o', 'o', 'b', 'a', 'r'}...) // token
data = append(data, []byte{16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}...)
hdr, pdata, rest, err := ParsePacket(data, 0)
hdr, pdata, rest, err := ParsePacket(data)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.Type).To(Equal(protocol.PacketTypeRetry))
Expect(hdr.Version).To(Equal(protocol.Version1))
@ -325,7 +321,7 @@ var _ = Describe("Header Parsing", func() {
data = append(data, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}...) // source connection ID
data = append(data, []byte{'f', 'o', 'o', 'b', 'a', 'r'}...) // token
data = append(data, []byte{16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}...)
hdr, pdata, rest, err := ParsePacket(data, 0)
hdr, pdata, rest, err := ParsePacket(data)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.Type).To(Equal(protocol.PacketTypeRetry))
Expect(hdr.Version).To(Equal(protocol.Version2))
@ -343,7 +339,7 @@ var _ = Describe("Header Parsing", func() {
data = append(data, []byte{'f', 'o', 'o', 'b', 'a', 'r'}...) // token
data = append(data, []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}...)
// this results in a token length of 0
_, _, _, err := ParsePacket(data, 0)
_, _, _, err := ParsePacket(data)
Expect(err).To(MatchError(io.EOF))
})
@ -355,7 +351,7 @@ var _ = Describe("Header Parsing", func() {
data = append(data, encodeVarInt(0x42)...) // length, 1 byte
data = append(data, []byte{0x12, 0x34}...) // packet number
_, _, _, err := ParsePacket(data, 0)
_, _, _, err := ParsePacket(data)
Expect(err).To(MatchError(io.EOF))
})
@ -365,7 +361,7 @@ var _ = Describe("Header Parsing", func() {
data = append(data, []byte{0x0, 0x0}...) // connection ID lengths
data = append(data, encodeVarInt(2)...) // length
data = append(data, []byte{0x12, 0x34}...) // packet number
hdr, _, _, err := ParsePacket(data, 0)
hdr, _, _, err := ParsePacket(data)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.Type).To(Equal(protocol.PacketTypeHandshake))
extHdr, err := hdr.ParseExtended(bytes.NewReader(data), protocol.Version1)
@ -381,8 +377,8 @@ var _ = Describe("Header Parsing", func() {
data = append(data, []byte{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}...) // dest conn ID
data = append(data, 0x8) // src conn ID len
data = append(data, []byte{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}...) // src conn ID
for i := 0; i < len(data); i++ {
_, _, _, err := ParsePacket(data[:i], 0)
for i := 1; i < len(data); i++ {
_, _, _, err := ParsePacket(data[:i])
Expect(err).To(Equal(io.EOF))
}
})
@ -396,7 +392,7 @@ var _ = Describe("Header Parsing", func() {
data = append(data, []byte{0xde, 0xad, 0xbe, 0xef}...) // packet number
for i := hdrLen; i < len(data); i++ {
data = data[:i]
hdr, _, _, err := ParsePacket(data, 0)
hdr, _, _, err := ParsePacket(data)
Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data)
_, err = hdr.ParseExtended(b, protocol.Version1)
@ -413,7 +409,7 @@ var _ = Describe("Header Parsing", func() {
hdrLen := len(data)
for i := hdrLen; i < len(data); i++ {
data = data[:i]
hdr, _, _, err := ParsePacket(data, 0)
hdr, _, _, err := ParsePacket(data)
Expect(err).ToNot(HaveOccurred())
b := bytes.NewReader(data)
_, err = hdr.ParseExtended(b, protocol.Version1)
@ -425,7 +421,6 @@ var _ = Describe("Header Parsing", func() {
It("cuts packets", func() {
buf := &bytes.Buffer{}
hdr := Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4}),
Length: 2 + 6,
@ -439,7 +434,7 @@ var _ = Describe("Header Parsing", func() {
hdrRaw := append([]byte{}, buf.Bytes()...)
buf.Write([]byte("foobar")) // payload of the first packet
buf.Write([]byte("raboof")) // second packet
parsedHdr, data, rest, err := ParsePacket(buf.Bytes(), 4)
parsedHdr, data, rest, err := ParsePacket(buf.Bytes())
Expect(err).ToNot(HaveOccurred())
Expect(parsedHdr.Type).To(Equal(hdr.Type))
Expect(parsedHdr.DestConnectionID).To(Equal(hdr.DestConnectionID))
@ -451,7 +446,6 @@ var _ = Describe("Header Parsing", func() {
buf := &bytes.Buffer{}
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4}),
Length: 3,
@ -460,7 +454,7 @@ var _ = Describe("Header Parsing", func() {
PacketNumber: 0x1337,
PacketNumberLen: 2,
}).Write(buf, protocol.Version1)).To(Succeed())
_, _, _, err := ParsePacket(buf.Bytes(), 4)
_, _, _, err := ParsePacket(buf.Bytes())
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("packet length (2 bytes) is smaller than the expected length (3 bytes)"))
})
@ -469,7 +463,6 @@ var _ = Describe("Header Parsing", func() {
buf := &bytes.Buffer{}
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4}),
Length: 1000,
@ -479,7 +472,7 @@ var _ = Describe("Header Parsing", func() {
PacketNumberLen: 2,
}).Write(buf, protocol.Version1)).To(Succeed())
buf.Write(make([]byte, 500-2 /* for packet number length */))
_, _, _, err := ParsePacket(buf.Bytes(), 4)
_, _, _, err := ParsePacket(buf.Bytes())
Expect(err).To(MatchError("packet length (500 bytes) is smaller than the expected length (1000 bytes)"))
})
})
@ -491,7 +484,7 @@ var _ = Describe("Header Parsing", func() {
})
It("tells its packet type for logging", func() {
Expect((&Header{IsLongHeader: true, Type: protocol.PacketTypeHandshake}).PacketType()).To(Equal("Handshake"))
Expect((&Header{}).PacketType()).To(Equal("1-RTT"))
Expect((&Header{Type: protocol.PacketTypeInitial}).PacketType()).To(Equal("Initial"))
Expect((&Header{Type: protocol.PacketTypeHandshake}).PacketType()).To(Equal("Handshake"))
})
})

View file

@ -6,9 +6,6 @@ import (
// PacketTypeFromHeader determines the packet type from a *wire.Header.
func PacketTypeFromHeader(hdr *Header) PacketType {
if !hdr.IsLongHeader {
return PacketType1RTT
}
if hdr.Version == 0 {
return PacketTypeVersionNegotiation
}

View file

@ -12,49 +12,38 @@ var _ = Describe("Packet Header", func() {
Context("determining the packet type from the header", func() {
It("recognizes Initial packets", func() {
Expect(PacketTypeFromHeader(&wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
Version: protocol.VersionTLS,
Type: protocol.PacketTypeInitial,
Version: protocol.VersionTLS,
})).To(Equal(PacketTypeInitial))
})
It("recognizes Handshake packets", func() {
Expect(PacketTypeFromHeader(&wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
Version: protocol.VersionTLS,
Type: protocol.PacketTypeHandshake,
Version: protocol.VersionTLS,
})).To(Equal(PacketTypeHandshake))
})
It("recognizes Retry packets", func() {
Expect(PacketTypeFromHeader(&wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeRetry,
Version: protocol.VersionTLS,
Type: protocol.PacketTypeRetry,
Version: protocol.VersionTLS,
})).To(Equal(PacketTypeRetry))
})
It("recognizes 0-RTT packets", func() {
Expect(PacketTypeFromHeader(&wire.Header{
IsLongHeader: true,
Type: protocol.PacketType0RTT,
Version: protocol.VersionTLS,
Type: protocol.PacketType0RTT,
Version: protocol.VersionTLS,
})).To(Equal(PacketType0RTT))
})
It("recognizes Version Negotiation packets", func() {
Expect(PacketTypeFromHeader(&wire.Header{IsLongHeader: true})).To(Equal(PacketTypeVersionNegotiation))
})
It("recognizes 1-RTT packets", func() {
Expect(PacketTypeFromHeader(&wire.Header{})).To(Equal(PacketType1RTT))
Expect(PacketTypeFromHeader(&wire.Header{})).To(Equal(PacketTypeVersionNegotiation))
})
It("handles unrecognized packet types", func() {
Expect(PacketTypeFromHeader(&wire.Header{
IsLongHeader: true,
Version: protocol.VersionTLS,
})).To(Equal(PacketTypeNotDetermined))
Expect(PacketTypeFromHeader(&wire.Header{Version: protocol.VersionTLS})).To(Equal(PacketTypeNotDetermined))
})
})
})

View file

@ -40,7 +40,6 @@ var _ = Describe("Packet Handler Map", func() {
buf := &bytes.Buffer{}
Expect((&wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: t,
DestConnectionID: connID,
Length: length,

View file

@ -69,9 +69,6 @@ type coalescedPacket struct {
}
func (p *longHeaderPacket) EncryptionLevel() protocol.EncryptionLevel {
if !p.header.IsLongHeader {
panic("this shouldn't happen any more")
}
//nolint:exhaustive // Will never be called for Retry packets (and they don't have encrypted data).
switch p.header.Type {
case protocol.PacketTypeInitial:
@ -335,9 +332,6 @@ func (p *packetPacker) packConnectionClose(
// It takes into account that packets that have a tiny payload need to be padded,
// such that len(payload) + packet number len >= 4 + AEAD overhead
func (p *packetPacker) longHeaderPacketLength(hdr *wire.ExtendedHeader, payload *payload) protocol.ByteCount {
if !hdr.IsLongHeader {
panic("wrong code path")
}
var paddingLen protocol.ByteCount
pnLen := protocol.ByteCount(hdr.PacketNumberLen)
if payload.length < 4-pnLen {
@ -780,7 +774,6 @@ func (p *packetPacker) getLongHeader(encLevel protocol.EncryptionLevel) *wire.Ex
PacketNumber: pn,
PacketNumberLen: pnLen,
}
hdr.IsLongHeader = true
hdr.Version = p.version
hdr.SrcConnectionID = p.srcConnID
hdr.DestConnectionID = p.getDestConnID()
@ -799,18 +792,13 @@ func (p *packetPacker) getLongHeader(encLevel protocol.EncryptionLevel) *wire.Ex
}
func (p *packetPacker) appendLongHeaderPacket(buffer *packetBuffer, header *wire.ExtendedHeader, payload *payload, padding protocol.ByteCount, encLevel protocol.EncryptionLevel, sealer sealer) (*longHeaderPacket, error) {
if !header.IsLongHeader {
panic("shouldn't have called appendLongHeaderPacket")
}
var paddingLen protocol.ByteCount
pnLen := protocol.ByteCount(header.PacketNumberLen)
if payload.length < 4-pnLen {
paddingLen = 4 - pnLen - payload.length
}
paddingLen += padding
if header.IsLongHeader {
header.Length = pnLen + protocol.ByteCount(sealer.Overhead()) + payload.length + paddingLen
}
header.Length = pnLen + protocol.ByteCount(sealer.Overhead()) + payload.length + paddingLen
raw := buffer.Data[len(buffer.Data):]
buf := bytes.NewBuffer(buffer.Data)

View file

@ -44,7 +44,7 @@ var _ = Describe("Packet packer", func() {
if !wire.IsLongHeaderPacket(data[0]) {
break
}
hdr, _, more, err := wire.ParsePacket(data, connID.Len())
hdr, _, more, err := wire.ParsePacket(data)
Expect(err).ToNot(HaveOccurred())
r := bytes.NewReader(data)
extHdr, err := hdr.ParseExtended(r, version)
@ -136,7 +136,6 @@ var _ = Describe("Packet packer", func() {
It("uses the Long Header format", func() {
pnManager.EXPECT().PeekPacketNumber(protocol.EncryptionHandshake).Return(protocol.PacketNumber(0x42), protocol.PacketNumberLen3)
h := packer.getLongHeader(protocol.EncryptionHandshake)
Expect(h.IsLongHeader).To(BeTrue())
Expect(h.PacketNumber).To(Equal(protocol.PacketNumber(0x42)))
Expect(h.PacketNumberLen).To(Equal(protocol.PacketNumberLen3))
Expect(h.Version).To(Equal(packer.version))
@ -657,7 +656,7 @@ var _ = Describe("Packet packer", func() {
Expect(packet.longHdrPackets).To(HaveLen(1))
// cut off the tag that the mock sealer added
// packet.buffer.Data = packet.buffer.Data[:packet.buffer.Len()-protocol.ByteCount(sealer.Overhead())]
hdr, _, _, err := wire.ParsePacket(packet.buffer.Data, packer.getDestConnID().Len())
hdr, _, _, err := wire.ParsePacket(packet.buffer.Data)
Expect(err).ToNot(HaveOccurred())
data := packet.buffer.Data
r := bytes.NewReader(data)
@ -1003,7 +1002,6 @@ var _ = Describe("Packet packer", func() {
Expect(err).ToNot(HaveOccurred())
Expect(p.longHdrPackets).To(HaveLen(1))
Expect(p.longHdrPackets[0].frames).To(HaveLen(1))
Expect(p.longHdrPackets[0].header.IsLongHeader).To(BeTrue())
Expect(p.buffer.Len()).To(BeEquivalentTo(packer.maxPacketSize))
parsePacket(p.buffer.Data)
})
@ -1206,13 +1204,11 @@ var _ = Describe("Packet packer", func() {
Expect(p.shortHdrPacket).ToNot(BeNil())
Expect(p.shortHdrPacket.Frames).To(HaveLen(1))
Expect(p.shortHdrPacket.Frames[0].Frame.(*wire.StreamFrame).Data).To(Equal([]byte("foobar")))
hdr, _, more, err := wire.ParsePacket(p.buffer.Data, 0)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.Type).To(Equal(protocol.PacketTypeHandshake))
hdr, _, more, err = wire.ParsePacket(more, 0)
Expect(err).ToNot(HaveOccurred())
Expect(hdr.IsLongHeader).To(BeFalse())
Expect(more).To(BeEmpty())
hdrs, more := parsePacket(p.buffer.Data)
Expect(hdrs).To(HaveLen(1))
Expect(hdrs[0].Type).To(Equal(protocol.PacketTypeHandshake))
Expect(more).ToNot(BeEmpty())
parseShortHeaderPacket(more)
})
It("doesn't add a coalesced packet if the remaining size is smaller than MaxCoalescedPacketSize", func() {
@ -1256,7 +1252,7 @@ var _ = Describe("Packet packer", func() {
Expect(packet.shortHdrPacket).To(BeNil())
// cut off the tag that the mock sealer added
// packet.buffer.Data = packet.buffer.Data[:packet.buffer.Len()-protocol.ByteCount(sealer.Overhead())]
hdr, _, _, err := wire.ParsePacket(packet.buffer.Data, packer.getDestConnID().Len())
hdr, _, _, err := wire.ParsePacket(packet.buffer.Data)
Expect(err).ToNot(HaveOccurred())
data := packet.buffer.Data
r := bytes.NewReader(data)
@ -1295,7 +1291,6 @@ var _ = Describe("Packet packer", func() {
Expect(p.longHdrPackets).To(HaveLen(1))
Expect(p.longHdrPackets[0].EncryptionLevel()).To(Equal(protocol.EncryptionInitial))
Expect(p.longHdrPackets[0].frames).To(Equal([]ackhandler.Frame{{Frame: f}}))
Expect(p.longHdrPackets[0].header.IsLongHeader).To(BeTrue())
})
It("sends an Initial packet containing only an ACK", func() {
@ -1565,7 +1560,7 @@ var _ = Describe("Packet packer", func() {
var _ = Describe("Converting to ackhandler.Packet", func() {
It("convert a packet", func() {
packet := &longHeaderPacket{
header: &wire.ExtendedHeader{Header: wire.Header{IsLongHeader: true, Type: protocol.PacketTypeInitial}},
header: &wire.ExtendedHeader{Header: wire.Header{Type: protocol.PacketTypeInitial}},
frames: []ackhandler.Frame{{Frame: &wire.MaxDataFrame{}}, {Frame: &wire.PingFrame{}}},
ack: &wire.AckFrame{AckRanges: []wire.AckRange{{Largest: 100, Smallest: 80}}},
length: 42,
@ -1580,7 +1575,7 @@ var _ = Describe("Converting to ackhandler.Packet", func() {
It("sets the LargestAcked to invalid, if the packet doesn't have an ACK frame", func() {
packet := &longHeaderPacket{
header: &wire.ExtendedHeader{Header: wire.Header{IsLongHeader: true, Type: protocol.PacketTypeHandshake}},
header: &wire.ExtendedHeader{Header: wire.Header{Type: protocol.PacketTypeHandshake}},
frames: []ackhandler.Frame{{Frame: &wire.MaxDataFrame{}}, {Frame: &wire.PingFrame{}}},
}
p := packet.ToAckHandlerPacket(time.Now(), nil)
@ -1604,8 +1599,8 @@ var _ = Describe("Converting to ackhandler.Packet", func() {
p.Frames[1].OnLost(nil)
Expect(pingLost).To(BeTrue())
},
Entry(protocol.EncryptionInitial.String(), wire.Header{IsLongHeader: true, Type: protocol.PacketTypeInitial}),
Entry(protocol.EncryptionHandshake.String(), wire.Header{IsLongHeader: true, Type: protocol.PacketTypeHandshake}),
Entry(protocol.Encryption0RTT.String(), wire.Header{IsLongHeader: true, Type: protocol.PacketType0RTT}),
Entry(protocol.EncryptionInitial.String(), wire.Header{Type: protocol.PacketTypeInitial}),
Entry(protocol.EncryptionHandshake.String(), wire.Header{Type: protocol.PacketTypeHandshake}),
Entry(protocol.Encryption0RTT.String(), wire.Header{Type: protocol.PacketType0RTT}),
)
})

View file

@ -28,14 +28,13 @@ var _ = Describe("Packet Unpacker", func() {
)
getLongHeader := func(extHdr *wire.ExtendedHeader) (*wire.Header, []byte) {
ExpectWithOffset(1, extHdr.IsLongHeader).To(BeTrue())
buf := &bytes.Buffer{}
ExpectWithOffset(1, extHdr.Write(buf, version)).To(Succeed())
hdrLen := buf.Len()
if extHdr.Length > protocol.ByteCount(extHdr.PacketNumberLen) {
buf.Write(make([]byte, int(extHdr.Length)-int(extHdr.PacketNumberLen)))
}
hdr, _, _, err := wire.ParsePacket(buf.Bytes(), connID.Len())
hdr, _, _, err := wire.ParsePacket(buf.Bytes())
ExpectWithOffset(1, err).ToNot(HaveOccurred())
return hdr, buf.Bytes()[:hdrLen]
}
@ -54,7 +53,6 @@ var _ = Describe("Packet Unpacker", func() {
It("errors when the packet is too small to obtain the header decryption sample, for long headers", func() {
extHdr := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
DestConnectionID: connID,
Version: version,
@ -87,7 +85,6 @@ var _ = Describe("Packet Unpacker", func() {
It("opens Initial packets", func() {
extHdr := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
Length: 3 + 6, // packet number len + payload
DestConnectionID: connID,
@ -113,7 +110,6 @@ var _ = Describe("Packet Unpacker", func() {
It("opens 0-RTT packets", func() {
extHdr := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketType0RTT,
Length: 3 + 6, // packet number len + payload
DestConnectionID: connID,
@ -164,7 +160,6 @@ var _ = Describe("Packet Unpacker", func() {
It("errors on empty packets, for long header packets", func() {
extHdr := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
DestConnectionID: connID,
Version: Version1,
@ -207,7 +202,6 @@ var _ = Describe("Packet Unpacker", func() {
It("returns the error when unpacking fails", func() {
extHdr := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
Length: 3, // packet number len
DestConnectionID: connID,
@ -230,7 +224,6 @@ var _ = Describe("Packet Unpacker", func() {
It("defends against the timing side-channel when the reserved bits are wrong, for long header packets", func() {
extHdr := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
DestConnectionID: connID,
Version: version,
@ -264,7 +257,6 @@ var _ = Describe("Packet Unpacker", func() {
It("returns the decryption error, when unpacking a packet with wrong reserved bits fails, for long headers", func() {
extHdr := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
DestConnectionID: connID,
Version: version,
@ -298,7 +290,6 @@ var _ = Describe("Packet Unpacker", func() {
It("decrypts the header", func() {
extHdr := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
Length: 3, // packet number len
DestConnectionID: connID,

View file

@ -32,30 +32,14 @@ var _ = Describe("Packet Header", func() {
checkEncoding(data, expected)
}
It("marshals a header for a 1-RTT packet", func() {
check(
&wire.ExtendedHeader{
PacketNumber: 42,
KeyPhase: protocol.KeyPhaseZero,
},
map[string]interface{}{
"packet_type": "1RTT",
"packet_number": 42,
"dcil": 0,
"key_phase_bit": "0",
},
)
})
It("marshals a header with a payload length", func() {
check(
&wire.ExtendedHeader{
PacketNumber: 42,
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
Length: 123,
Version: protocol.VersionNumber(0xdecafbad),
Type: protocol.PacketTypeInitial,
Length: 123,
Version: protocol.VersionNumber(0xdecafbad),
},
},
map[string]interface{}{
@ -73,11 +57,10 @@ var _ = Describe("Packet Header", func() {
&wire.ExtendedHeader{
PacketNumber: 4242,
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
Length: 123,
Version: protocol.VersionNumber(0xdecafbad),
Token: []byte{0xde, 0xad, 0xbe, 0xef},
Type: protocol.PacketTypeInitial,
Length: 123,
Version: protocol.VersionNumber(0xdecafbad),
Token: []byte{0xde, 0xad, 0xbe, 0xef},
},
},
map[string]interface{}{
@ -95,7 +78,6 @@ var _ = Describe("Packet Header", func() {
check(
&wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeRetry,
SrcConnectionID: protocol.ParseConnectionID([]byte{0x11, 0x22, 0x33, 0x44}),
Version: protocol.VersionNumber(0xdecafbad),
@ -118,9 +100,8 @@ var _ = Describe("Packet Header", func() {
&wire.ExtendedHeader{
PacketNumber: 0,
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
Version: protocol.VersionNumber(0xdecafbad),
Type: protocol.PacketTypeHandshake,
Version: protocol.VersionNumber(0xdecafbad),
},
},
map[string]interface{}{
@ -138,7 +119,6 @@ var _ = Describe("Packet Header", func() {
&wire.ExtendedHeader{
PacketNumber: 42,
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
SrcConnectionID: protocol.ParseConnectionID([]byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}),
Version: protocol.VersionNumber(0xdecafbad),
@ -154,22 +134,5 @@ var _ = Describe("Packet Header", func() {
},
)
})
It("marshals a 1-RTT header with a destination connection ID", func() {
check(
&wire.ExtendedHeader{
PacketNumber: 42,
Header: wire.Header{DestConnectionID: protocol.ParseConnectionID([]byte{0xde, 0xad, 0xbe, 0xef})},
KeyPhase: protocol.KeyPhaseOne,
},
map[string]interface{}{
"packet_type": "1RTT",
"packet_number": 42,
"dcil": 4,
"dcid": "deadbeef",
"key_phase_bit": "1",
},
)
})
})
})

View file

@ -29,8 +29,8 @@ func scaleDuration(t time.Duration) time.Duration {
func checkEncoding(data []byte, expected map[string]interface{}) {
// unmarshal the data
m := make(map[string]interface{})
ExpectWithOffset(1, json.Unmarshal(data, &m)).To(Succeed())
ExpectWithOffset(1, m).To(HaveLen(len(expected)))
ExpectWithOffset(2, json.Unmarshal(data, &m)).To(Succeed())
ExpectWithOffset(2, m).To(HaveLen(len(expected)))
for key, value := range expected {
switch v := value.(type) {
case bool, string, map[string]interface{}:

View file

@ -424,7 +424,6 @@ var _ = Describe("Tracing", func() {
tracer.SentLongHeaderPacket(
&logging.ExtendedHeader{
Header: logging.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}),
SrcConnectionID: protocol.ParseConnectionID([]byte{4, 3, 2, 1}),
@ -490,7 +489,6 @@ var _ = Describe("Tracing", func() {
tracer.ReceivedLongHeaderPacket(
&logging.ExtendedHeader{
Header: logging.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}),
SrcConnectionID: protocol.ParseConnectionID([]byte{4, 3, 2, 1}),
@ -561,7 +559,6 @@ var _ = Describe("Tracing", func() {
It("records a received Retry packet", func() {
tracer.ReceivedRetry(
&logging.Header{
IsLongHeader: true,
Type: protocol.PacketTypeRetry,
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}),
SrcConnectionID: protocol.ParseConnectionID([]byte{4, 3, 2, 1}),

View file

@ -348,7 +348,7 @@ func (s *baseServer) handlePacketImpl(p *receivedPacket) bool /* is the buffer s
}
// If we're creating a new connection, the packet will be passed to the connection.
// The header will then be parsed again.
hdr, _, _, err := wire.ParsePacket(p.data, s.config.ConnectionIDGenerator.ConnectionIDLen())
hdr, _, _, err := wire.ParsePacket(p.data)
if err != nil {
if s.config.Tracer != nil {
s.config.Tracer.DroppedPacket(p.remoteAddr, logging.PacketTypeNotDetermined, p.Size(), logging.PacketDropHeaderParseError)
@ -364,7 +364,7 @@ func (s *baseServer) handlePacketImpl(p *receivedPacket) bool /* is the buffer s
return false
}
if hdr.IsLongHeader && hdr.Type != protocol.PacketTypeInitial {
if hdr.Type != protocol.PacketTypeInitial {
// Drop long header packets.
// There's little point in sending a Stateless Reset, since the client
// might not have received the token yet.
@ -566,7 +566,6 @@ func (s *baseServer) sendRetry(remoteAddr net.Addr, hdr *wire.Header, info *pack
return err
}
replyHdr := &wire.ExtendedHeader{}
replyHdr.IsLongHeader = true
replyHdr.Type = protocol.PacketTypeRetry
replyHdr.Version = hdr.Version
replyHdr.SrcConnectionID = srcConnID
@ -635,7 +634,6 @@ func (s *baseServer) sendError(remoteAddr net.Addr, hdr *wire.Header, sealer han
ccf := &wire.ConnectionCloseFrame{ErrorCode: uint64(errorCode)}
replyHdr := &wire.ExtendedHeader{}
replyHdr.IsLongHeader = true
replyHdr.Type = protocol.PacketTypeInitial
replyHdr.Version = hdr.Version
replyHdr.SrcConnectionID = hdr.DestConnectionID

View file

@ -44,9 +44,7 @@ var _ = Describe("Server", func() {
getPacket := func(hdr *wire.Header, p []byte) *receivedPacket {
buffer := getPacketBuffer()
buf := bytes.NewBuffer(buffer.Data)
if hdr.IsLongHeader {
hdr.Length = 4 + protocol.ByteCount(len(p)) + 16
}
hdr.Length = 4 + protocol.ByteCount(len(p)) + 16
Expect((&wire.ExtendedHeader{
Header: *hdr,
PacketNumber: 0x42,
@ -69,7 +67,6 @@ var _ = Describe("Server", func() {
getInitial := func(destConnID protocol.ConnectionID) *receivedPacket {
senderAddr := &net.UDPAddr{IP: net.IPv4(1, 2, 3, 4), Port: 42}
hdr := &wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
SrcConnectionID: protocol.ParseConnectionID([]byte{5, 4, 3, 2, 1}),
DestConnectionID: destConnID,
@ -90,7 +87,7 @@ var _ = Describe("Server", func() {
}
parseHeader := func(data []byte) *wire.Header {
hdr, _, _, err := wire.ParsePacket(data, 0)
hdr, _, _, err := wire.ParsePacket(data)
Expect(err).ToNot(HaveOccurred())
return hdr
}
@ -202,7 +199,6 @@ var _ = Describe("Server", func() {
Context("handling packets", func() {
It("drops Initial packets with a too short connection ID", func() {
p := getPacket(&wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4}),
Version: serv.config.Versions[0],
@ -215,7 +211,6 @@ var _ = Describe("Server", func() {
It("drops too small Initial", func() {
p := getPacket(&wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8}),
Version: serv.config.Versions[0],
@ -229,9 +224,8 @@ var _ = Describe("Server", func() {
It("drops non-Initial packets", func() {
p := getPacket(&wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
Version: serv.config.Versions[0],
Type: protocol.PacketTypeHandshake,
Version: serv.config.Versions[0],
}, []byte("invalid"))
tracer.EXPECT().DroppedPacket(p.remoteAddr, logging.PacketTypeHandshake, p.Size(), logging.PacketDropUnexpectedPacket)
serv.handlePacket(p)
@ -249,7 +243,6 @@ var _ = Describe("Server", func() {
)
Expect(err).ToNot(HaveOccurred())
hdr := &wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
SrcConnectionID: protocol.ParseConnectionID([]byte{5, 4, 3, 2, 1}),
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
@ -326,7 +319,6 @@ var _ = Describe("Server", func() {
srcConnID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5})
destConnID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6})
packet := getPacket(&wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
SrcConnectionID: srcConnID,
DestConnectionID: destConnID,
@ -358,7 +350,6 @@ var _ = Describe("Server", func() {
srcConnID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5})
destConnID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6})
packet := getPacket(&wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
SrcConnectionID: srcConnID,
DestConnectionID: destConnID,
@ -397,7 +388,6 @@ var _ = Describe("Server", func() {
srcConnID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5})
destConnID := protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6})
p := getPacket(&wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
SrcConnectionID: srcConnID,
DestConnectionID: destConnID,
@ -419,7 +409,6 @@ var _ = Describe("Server", func() {
It("replies with a Retry packet, if a token is required", func() {
serv.config.RequireAddressValidation = func(net.Addr) bool { return true }
hdr := &wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
SrcConnectionID: protocol.ParseConnectionID([]byte{5, 4, 3, 2, 1}),
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
@ -451,7 +440,6 @@ var _ = Describe("Server", func() {
It("creates a connection, if no token is required", func() {
hdr := &wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
SrcConnectionID: protocol.ParseConnectionID([]byte{5, 4, 3, 2, 1}),
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
@ -663,7 +651,7 @@ var _ = Describe("Server", func() {
}
wg.Wait()
p := getInitialWithRandomDestConnID()
hdr, _, _, err := wire.ParsePacket(p.data, 0)
hdr, _, _, err := wire.ParsePacket(p.data)
Expect(err).ToNot(HaveOccurred())
tracer.EXPECT().SentPacket(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any())
done := make(chan struct{})
@ -767,10 +755,9 @@ var _ = Describe("Server", func() {
token, err := serv.tokenGenerator.NewRetryToken(raddr, protocol.ConnectionID{}, protocol.ConnectionID{})
Expect(err).ToNot(HaveOccurred())
packet := getPacket(&wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
Token: token,
Version: serv.config.Versions[0],
Type: protocol.PacketTypeInitial,
Token: token,
Version: serv.config.Versions[0],
}, make([]byte, protocol.MinInitialPacketSize))
packet.remoteAddr = raddr
conn.EXPECT().WriteTo(gomock.Any(), gomock.Any()).MaxTimes(1)
@ -787,7 +774,6 @@ var _ = Describe("Server", func() {
token, err := serv.tokenGenerator.NewRetryToken(&net.UDPAddr{}, protocol.ConnectionID{}, protocol.ConnectionID{})
Expect(err).ToNot(HaveOccurred())
hdr := &wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
SrcConnectionID: protocol.ParseConnectionID([]byte{5, 4, 3, 2, 1}),
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
@ -826,7 +812,6 @@ var _ = Describe("Server", func() {
Expect(err).ToNot(HaveOccurred())
time.Sleep(2 * time.Millisecond) // make sure the token is expired
hdr := &wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
SrcConnectionID: protocol.ParseConnectionID([]byte{5, 4, 3, 2, 1}),
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
@ -860,7 +845,6 @@ var _ = Describe("Server", func() {
token, err := serv.tokenGenerator.NewToken(&net.UDPAddr{IP: net.IPv4(192, 168, 0, 1), Port: 1337})
Expect(err).ToNot(HaveOccurred())
hdr := &wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
SrcConnectionID: protocol.ParseConnectionID([]byte{5, 4, 3, 2, 1}),
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
@ -892,7 +876,6 @@ var _ = Describe("Server", func() {
Expect(err).ToNot(HaveOccurred())
time.Sleep(2 * time.Millisecond) // make sure the token is expired
hdr := &wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
SrcConnectionID: protocol.ParseConnectionID([]byte{5, 4, 3, 2, 1}),
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
@ -918,7 +901,6 @@ var _ = Describe("Server", func() {
token, err := serv.tokenGenerator.NewRetryToken(&net.UDPAddr{}, protocol.ConnectionID{}, protocol.ConnectionID{})
Expect(err).ToNot(HaveOccurred())
hdr := &wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
SrcConnectionID: protocol.ParseConnectionID([]byte{5, 4, 3, 2, 1}),
DestConnectionID: protocol.ParseConnectionID([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),