diff --git a/client_test.go b/client_test.go index 67755533..083ec617 100644 --- a/client_test.go +++ b/client_test.go @@ -50,9 +50,9 @@ var _ = Describe("Client", func() { acceptClientVersionPacket := func(connID protocol.ConnectionID) []byte { b := &bytes.Buffer{} Expect((&wire.ExtendedHeader{ - DestConnectionID: connID, - PacketNumber: 1, - PacketNumberLen: 1, + Header: wire.Header{DestConnectionID: connID}, + PacketNumber: 1, + PacketNumberLen: 1, }).Write(b, protocol.VersionWhatever)).To(Succeed()) return b.Bytes() } @@ -516,7 +516,7 @@ var _ = Describe("Client", func() { DestConnectionID: id, }, extHdr: &wire.ExtendedHeader{ - IsLongHeader: true, + Header: wire.Header{IsLongHeader: true}, Type: protocol.PacketTypeRetry, Token: []byte("foobar"), OrigDestConnectionID: connID, @@ -580,11 +580,13 @@ var _ = Describe("Client", func() { Version: cl.version, }, extHdr: &wire.ExtendedHeader{ - IsLongHeader: true, + Header: wire.Header{ + IsLongHeader: true, + Version: protocol.VersionTLS, + }, Type: protocol.PacketTypeRetry, Token: []byte("foobar"), OrigDestConnectionID: connID, - Version: protocol.VersionTLS, }, }) }).AnyTimes() diff --git a/internal/wire/extended_header.go b/internal/wire/extended_header.go index 19b216b9..528ad9de 100644 --- a/internal/wire/extended_header.go +++ b/internal/wire/extended_header.go @@ -13,23 +13,22 @@ import ( // ExtendedHeader is the header of a QUIC packet. type ExtendedHeader struct { + Header + Raw []byte - Version protocol.VersionNumber - - DestConnectionID protocol.ConnectionID - SrcConnectionID protocol.ConnectionID OrigDestConnectionID protocol.ConnectionID // only needed in the Retry packet PacketNumberLen protocol.PacketNumberLen PacketNumber protocol.PacketNumber - typeByte byte - Type protocol.PacketType - IsLongHeader bool - KeyPhase int - Length protocol.ByteCount - Token []byte + IsVersionNegotiation bool + SupportedVersions []protocol.VersionNumber // Version Number sent in a Version Negotiation Packet by the server + + Type protocol.PacketType + KeyPhase int + Length protocol.ByteCount + Token []byte } func (h *ExtendedHeader) parse(b *bytes.Reader, v protocol.VersionNumber) (*ExtendedHeader, error) { diff --git a/internal/wire/extended_header_test.go b/internal/wire/extended_header_test.go index a09209af..dcd8c542 100644 --- a/internal/wire/extended_header_test.go +++ b/internal/wire/extended_header_test.go @@ -32,14 +32,16 @@ var _ = Describe("Header", func() { It("writes", func() { Expect((&ExtendedHeader{ - IsLongHeader: true, - Type: 0x5, - DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe}, - SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad, 0x0, 0x0, 0x13, 0x37}, - Length: 0xcafe, - PacketNumber: 0xdecaf, - PacketNumberLen: protocol.PacketNumberLen4, - Version: 0x1020304, + Header: Header{ + IsLongHeader: true, + DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe}, + SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad, 0x0, 0x0, 0x13, 0x37}, + Version: 0x1020304, + }, + Length: 0xcafe, + Type: 0x5, + PacketNumber: 0xdecaf, + PacketNumberLen: protocol.PacketNumberLen4, }).Write(buf, versionIETFHeader)).To(Succeed()) expected := []byte{ 0x80 ^ 0x5, @@ -55,68 +57,77 @@ var _ = Describe("Header", func() { It("refuses to write a header with a too short connection ID", func() { err := (&ExtendedHeader{ - IsLongHeader: true, - Type: 0x5, - SrcConnectionID: srcConnID, - DestConnectionID: protocol.ConnectionID{1, 2, 3}, // connection IDs must be at least 4 bytes long - PacketNumber: 0xdecafbad, - PacketNumberLen: protocol.PacketNumberLen4, - Version: 0x1020304, + Header: Header{ + IsLongHeader: true, + SrcConnectionID: srcConnID, + DestConnectionID: protocol.ConnectionID{1, 2, 3}, // connection IDs must be at least 4 bytes long + Version: 0x1020304, + }, + Type: 0x5, + PacketNumber: 0xdecafbad, + PacketNumberLen: protocol.PacketNumberLen4, }).Write(buf, versionIETFHeader) Expect(err).To(MatchError("invalid connection ID length: 3 bytes")) }) It("refuses to write a header with a too long connection ID", func() { err := (&ExtendedHeader{ - IsLongHeader: true, - Type: 0x5, - SrcConnectionID: srcConnID, - DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, // connection IDs must be at most 18 bytes long - PacketNumber: 0xdecafbad, - PacketNumberLen: protocol.PacketNumberLen4, - Version: 0x1020304, + Header: Header{ + IsLongHeader: true, + SrcConnectionID: srcConnID, + DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, // connection IDs must be at most 18 bytes long + Version: 0x1020304, + }, + Type: 0x5, + PacketNumber: 0xdecafbad, + PacketNumberLen: protocol.PacketNumberLen4, }).Write(buf, versionIETFHeader) Expect(err).To(MatchError("invalid connection ID length: 19 bytes")) }) It("writes a header with an 18 byte connection ID", func() { - Expect((&ExtendedHeader{ - IsLongHeader: true, - Type: 0x5, - SrcConnectionID: srcConnID, - DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}, // connection IDs must be at most 18 bytes long - PacketNumber: 0xdecafbad, - PacketNumberLen: protocol.PacketNumberLen4, - Version: 0x1020304, - }).Write(buf, versionIETFHeader)).To(Succeed()) + err := (&ExtendedHeader{ + Header: Header{ + IsLongHeader: true, + SrcConnectionID: srcConnID, + DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}, // connection IDs must be at most 18 bytes long + Version: 0x1020304, + }, + Type: 0x5, + PacketNumber: 0xdecafbad, + PacketNumberLen: protocol.PacketNumberLen4, + }).Write(buf, versionIETFHeader) + Expect(err).ToNot(HaveOccurred()) Expect(buf.Bytes()).To(ContainSubstring(string([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18}))) }) It("writes an Initial containing a token", func() { token := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.") - err := (&ExtendedHeader{ - IsLongHeader: true, + Expect((&ExtendedHeader{ + Header: Header{ + IsLongHeader: true, + Version: 0x1020304, + }, Type: protocol.PacketTypeInitial, Token: token, PacketNumber: 0xdecafbad, PacketNumberLen: protocol.PacketNumberLen4, - Version: 0x1020304, - }).Write(buf, versionIETFHeader) - Expect(err).ToNot(HaveOccurred()) + }).Write(buf, versionIETFHeader)).To(Succeed()) expectedSubstring := append(encodeVarInt(uint64(len(token))), token...) Expect(buf.Bytes()).To(ContainSubstring(string(expectedSubstring))) }) 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.") - err := (&ExtendedHeader{ - IsLongHeader: true, + Expect((&ExtendedHeader{ + Header: Header{ + IsLongHeader: true, + Version: 0x1020304, + }, Type: protocol.PacketTypeRetry, Token: token, OrigDestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9}, - Version: 0x1020304, - }).Write(buf, versionIETFHeader) - Expect(err).ToNot(HaveOccurred()) + }).Write(buf, versionIETFHeader)).To(Succeed()) Expect(buf.Bytes()[:6]).To(Equal([]byte{ 0x80 ^ uint8(protocol.PacketTypeRetry), 0x1, 0x2, 0x3, 0x4, // version number @@ -129,11 +140,13 @@ var _ = Describe("Header", func() { It("refuses to write a Retry packet with an invalid Orig Destination Connection ID length", func() { err := (&ExtendedHeader{ - IsLongHeader: true, + Header: Header{ + IsLongHeader: true, + Version: 0x1020304, + }, Type: protocol.PacketTypeRetry, Token: []byte("foobar"), OrigDestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, // connection IDs must be at most 18 bytes long - Version: 0x1020304, }).Write(buf, versionIETFHeader) Expect(err).To(MatchError("invalid connection ID length: 19 bytes")) }) @@ -142,9 +155,11 @@ var _ = Describe("Header", func() { Context("short header", func() { It("writes a header with connection ID", func() { Expect((&ExtendedHeader{ - DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}, - PacketNumberLen: protocol.PacketNumberLen1, - PacketNumber: 0x42, + Header: Header{ + DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}, + }, + PacketNumberLen: protocol.PacketNumberLen1, + PacketNumber: 0x42, }).Write(buf, versionIETFHeader)).To(Succeed()) Expect(buf.Bytes()).To(Equal([]byte{ 0x30, @@ -215,11 +230,13 @@ var _ = Describe("Header", func() { It("has the right length for the Long Header, for a short length", func() { h := &ExtendedHeader{ - IsLongHeader: true, - Length: 1, - DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, - SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, - PacketNumberLen: protocol.PacketNumberLen1, + Header: Header{ + IsLongHeader: true, + DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, + SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, + }, + Length: 1, + PacketNumberLen: protocol.PacketNumberLen1, } expectedLen := 1 /* type byte */ + 4 /* version */ + 1 /* conn ID len */ + 8 /* dest conn id */ + 8 /* src conn id */ + 1 /* short len */ + 1 /* packet number */ Expect(h.GetLength(versionIETFHeader)).To(BeEquivalentTo(expectedLen)) @@ -229,11 +246,13 @@ var _ = Describe("Header", func() { It("has the right length for the Long Header, for a long length", func() { h := &ExtendedHeader{ - IsLongHeader: true, - Length: 1500, - DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, - SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, - PacketNumberLen: protocol.PacketNumberLen2, + Header: Header{ + IsLongHeader: true, + DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, + SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, + }, + Length: 1500, + PacketNumberLen: protocol.PacketNumberLen2, } expectedLen := 1 /* type byte */ + 4 /* version */ + 1 /* conn ID len */ + 8 /* dest conn id */ + 8 /* src conn id */ + 2 /* long len */ + 2 /* packet number */ Expect(h.GetLength(versionIETFHeader)).To(BeEquivalentTo(expectedLen)) @@ -243,12 +262,14 @@ var _ = Describe("Header", func() { It("has the right length for an Initial not containing a Token", func() { h := &ExtendedHeader{ - Type: protocol.PacketTypeInitial, - IsLongHeader: true, - Length: 1500, - DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, - SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4}, - PacketNumberLen: protocol.PacketNumberLen2, + Header: Header{ + IsLongHeader: true, + DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, + SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4}, + }, + Length: 1500, + Type: protocol.PacketTypeInitial, + PacketNumberLen: protocol.PacketNumberLen2, } expectedLen := 1 /* type byte */ + 4 /* version */ + 1 /* conn ID len */ + 8 /* dest conn id */ + 4 /* src conn id */ + 1 /* token length */ + 2 /* long len */ + 2 /* packet number */ Expect(h.GetLength(versionIETFHeader)).To(BeEquivalentTo(expectedLen)) @@ -258,13 +279,15 @@ var _ = Describe("Header", func() { It("has the right length for an Initial containing a Token", func() { h := &ExtendedHeader{ - Type: protocol.PacketTypeInitial, - IsLongHeader: true, - Length: 1500, - DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, - SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4}, - PacketNumberLen: protocol.PacketNumberLen2, - Token: []byte("foo"), + Header: Header{ + IsLongHeader: true, + DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, + SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4}, + }, + Type: protocol.PacketTypeInitial, + Length: 1500, + PacketNumberLen: protocol.PacketNumberLen2, + Token: []byte("foo"), } expectedLen := 1 /* type byte */ + 4 /* version */ + 1 /* conn ID len */ + 8 /* dest conn id */ + 4 /* src conn id */ + 1 /* token length */ + 3 /* token */ + 2 /* long len */ + 2 /* packet number */ Expect(h.GetLength(versionIETFHeader)).To(BeEquivalentTo(expectedLen)) @@ -274,8 +297,10 @@ var _ = Describe("Header", func() { It("has the right length for a Short Header containing a connection ID", func() { h := &ExtendedHeader{ - PacketNumberLen: protocol.PacketNumberLen1, - DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, + Header: Header{ + DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, + }, + PacketNumberLen: protocol.PacketNumberLen1, } Expect(h.GetLength(versionIETFHeader)).To(Equal(protocol.ByteCount(1 + 8 + 1))) Expect(h.Write(buf, versionIETFHeader)).To(Succeed()) @@ -323,66 +348,76 @@ var _ = Describe("Header", func() { It("logs Long Headers", func() { (&ExtendedHeader{ - IsLongHeader: true, - Type: protocol.PacketTypeHandshake, - PacketNumber: 0x1337, - PacketNumberLen: protocol.PacketNumberLen2, - Length: 54321, - DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}, - SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad, 0x013, 0x37, 0x13, 0x37}, - Version: 0xfeed, + Header: Header{ + IsLongHeader: true, + DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}, + SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad, 0x013, 0x37, 0x13, 0x37}, + Version: 0xfeed, + }, + Type: protocol.PacketTypeHandshake, + PacketNumber: 0x1337, + PacketNumberLen: protocol.PacketNumberLen2, + Length: 54321, }).Log(logger) Expect(buf.String()).To(ContainSubstring("Long Header{Type: Handshake, DestConnectionID: 0xdeadbeefcafe1337, SrcConnectionID: 0xdecafbad13371337, PacketNumber: 0x1337, PacketNumberLen: 2, Length: 54321, Version: 0xfeed}")) }) It("logs Initial Packets with a Token", func() { (&ExtendedHeader{ - IsLongHeader: true, - Type: protocol.PacketTypeInitial, - Token: []byte{0xde, 0xad, 0xbe, 0xef}, - PacketNumber: 0x42, - PacketNumberLen: protocol.PacketNumberLen2, - Length: 100, - DestConnectionID: protocol.ConnectionID{0xca, 0xfe, 0x13, 0x37}, - SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad}, - Version: 0xfeed, + Header: Header{ + IsLongHeader: true, + DestConnectionID: protocol.ConnectionID{0xca, 0xfe, 0x13, 0x37}, + SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad}, + Version: 0xfeed, + }, + Type: protocol.PacketTypeInitial, + Token: []byte{0xde, 0xad, 0xbe, 0xef}, + PacketNumber: 0x42, + PacketNumberLen: protocol.PacketNumberLen2, + Length: 100, }).Log(logger) Expect(buf.String()).To(ContainSubstring("Long Header{Type: Initial, DestConnectionID: 0xcafe1337, SrcConnectionID: 0xdecafbad, Token: 0xdeadbeef, PacketNumber: 0x42, PacketNumberLen: 2, Length: 100, Version: 0xfeed}")) }) It("logs Initial Packets without a Token", func() { (&ExtendedHeader{ - IsLongHeader: true, - Type: protocol.PacketTypeInitial, - PacketNumber: 0x42, - PacketNumberLen: protocol.PacketNumberLen2, - Length: 100, - DestConnectionID: protocol.ConnectionID{0xca, 0xfe, 0x13, 0x37}, - SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad}, - Version: 0xfeed, + Header: Header{ + IsLongHeader: true, + DestConnectionID: protocol.ConnectionID{0xca, 0xfe, 0x13, 0x37}, + SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad}, + Version: 0xfeed, + }, + Type: protocol.PacketTypeInitial, + PacketNumber: 0x42, + PacketNumberLen: protocol.PacketNumberLen2, + Length: 100, }).Log(logger) Expect(buf.String()).To(ContainSubstring("Long Header{Type: Initial, DestConnectionID: 0xcafe1337, SrcConnectionID: 0xdecafbad, Token: (empty), PacketNumber: 0x42, PacketNumberLen: 2, Length: 100, Version: 0xfeed}")) }) It("logs Initial Packets with a Token", func() { (&ExtendedHeader{ - IsLongHeader: true, + Header: Header{ + IsLongHeader: true, + DestConnectionID: protocol.ConnectionID{0xca, 0xfe, 0x13, 0x37}, + SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad}, + Version: 0xfeed, + }, Type: protocol.PacketTypeRetry, - DestConnectionID: protocol.ConnectionID{0xca, 0xfe, 0x13, 0x37}, - SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad}, OrigDestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef}, Token: []byte{0x12, 0x34, 0x56}, - Version: 0xfeed, }).Log(logger) Expect(buf.String()).To(ContainSubstring("Long Header{Type: Retry, DestConnectionID: 0xcafe1337, SrcConnectionID: 0xdecafbad, Token: 0x123456, OrigDestConnectionID: 0xdeadbeef, Version: 0xfeed}")) }) It("logs Short Headers containing a connection ID", func() { (&ExtendedHeader{ - KeyPhase: 1, - PacketNumber: 0x1337, - PacketNumberLen: 4, - DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}, + Header: Header{ + DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37}, + }, + KeyPhase: 1, + PacketNumber: 0x1337, + PacketNumberLen: 4, }).Log(logger) Expect(buf.String()).To(ContainSubstring("Short Header{DestConnectionID: 0xdeadbeefcafe1337, PacketNumber: 0x1337, PacketNumberLen: 4, KeyPhase: 1}")) }) diff --git a/internal/wire/header.go b/internal/wire/header.go index d06aabc6..bace2dbd 100644 --- a/internal/wire/header.go +++ b/internal/wire/header.go @@ -17,8 +17,9 @@ type Header struct { SupportedVersions []protocol.VersionNumber // sent in a Version Negotiation Packet - typeByte byte - len int // how many bytes were read while parsing this header + IsLongHeader bool + typeByte byte + len int // how many bytes were read while parsing this header } // ParseHeader parses the version independent part of the header @@ -38,10 +39,12 @@ func parseHeaderImpl(b *bytes.Reader, shortHeaderConnIDLen int) (*Header, error) return nil, err } - h := &Header{typeByte: typeByte} + h := &Header{ + typeByte: typeByte, + IsLongHeader: typeByte&0x80 > 0, + } - // If this is not a Long Header, it could either be a Public Header or a Short Header. - if !h.IsLongHeader() { + if !h.IsLongHeader { var err error h.DestConnectionID, err = protocol.ReadConnectionID(b, shortHeaderConnIDLen) if err != nil { @@ -84,14 +87,9 @@ func parseHeaderImpl(b *bytes.Reader, shortHeaderConnIDLen int) (*Header, error) return h, nil } -// IsLongHeader says if this is a long header -func (h *Header) IsLongHeader() bool { - return h.typeByte&0x80 > 0 -} - // IsVersionNegotiation says if this a version negotiation packet func (h *Header) IsVersionNegotiation() bool { - return h.IsLongHeader() && h.Version == 0 + return h.IsLongHeader && h.Version == 0 } // ParseExtended parses the version dependent part of the header. @@ -104,11 +102,5 @@ func (h *Header) ParseExtended(b *bytes.Reader, ver protocol.VersionNumber) (*Ex } func (h *Header) toExtendedHeader() *ExtendedHeader { - return &ExtendedHeader{ - IsLongHeader: h.IsLongHeader(), - typeByte: h.typeByte, - DestConnectionID: h.DestConnectionID, - SrcConnectionID: h.SrcConnectionID, - Version: h.Version, - } + return &ExtendedHeader{Header: *h} } diff --git a/internal/wire/header_test.go b/internal/wire/header_test.go index c7e6a737..6054d40e 100644 --- a/internal/wire/header_test.go +++ b/internal/wire/header_test.go @@ -30,7 +30,7 @@ var _ = Describe("Header Parsing", func() { Expect(err).ToNot(HaveOccurred()) Expect(hdr.DestConnectionID).To(Equal(destConnID)) Expect(hdr.SrcConnectionID).To(Equal(srcConnID)) - Expect(hdr.IsLongHeader()).To(BeTrue()) + Expect(hdr.IsLongHeader).To(BeTrue()) Expect(hdr.IsVersionNegotiation()).To(BeTrue()) Expect(hdr.Version).To(BeZero()) for _, v := range versions { @@ -80,7 +80,7 @@ var _ = Describe("Header Parsing", func() { hdr, err := ParseHeader(bytes.NewReader(data), 0) Expect(err).ToNot(HaveOccurred()) - Expect(hdr.IsLongHeader()).To(BeTrue()) + Expect(hdr.IsLongHeader).To(BeTrue()) Expect(hdr.IsVersionNegotiation()).To(BeFalse()) Expect(hdr.DestConnectionID).To(Equal(destConnID)) Expect(hdr.SrcConnectionID).To(Equal(srcConnID)) @@ -173,10 +173,12 @@ var _ = Describe("Header Parsing", func() { srcConnID := protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8} buf := &bytes.Buffer{} Expect((&ExtendedHeader{ - IsLongHeader: true, + Header: Header{ + IsLongHeader: true, + SrcConnectionID: srcConnID, + Version: 0x10203040, + }, Type: 42, - SrcConnectionID: srcConnID, - Version: 0x10203040, PacketNumber: 1, PacketNumberLen: protocol.PacketNumberLen1, }).Write(buf, protocol.VersionTLS)).To(Succeed()) @@ -264,7 +266,7 @@ var _ = Describe("Header Parsing", func() { data = appendPacketNumber(data, 0x42, protocol.PacketNumberLen1) hdr, err := ParseHeader(bytes.NewReader(data), 8) Expect(err).ToNot(HaveOccurred()) - Expect(hdr.IsLongHeader()).To(BeFalse()) + Expect(hdr.IsLongHeader).To(BeFalse()) Expect(hdr.IsVersionNegotiation()).To(BeFalse()) Expect(hdr.DestConnectionID).To(Equal(connID)) b := bytes.NewReader(data) @@ -283,7 +285,7 @@ var _ = Describe("Header Parsing", func() { data = appendPacketNumber(data, 0x42, protocol.PacketNumberLen1) hdr, err := ParseHeader(bytes.NewReader(data), 5) Expect(err).ToNot(HaveOccurred()) - Expect(hdr.IsLongHeader()).To(BeFalse()) + Expect(hdr.IsLongHeader).To(BeFalse()) Expect(hdr.DestConnectionID).To(Equal(connID)) b := bytes.NewReader(data) extHdr, err := hdr.ParseExtended(b, versionIETFFrames) @@ -302,7 +304,7 @@ var _ = Describe("Header Parsing", func() { data = appendPacketNumber(data, 11, protocol.PacketNumberLen1) hdr, err := ParseHeader(bytes.NewReader(data), 6) Expect(err).ToNot(HaveOccurred()) - Expect(hdr.IsLongHeader()).To(BeFalse()) + Expect(hdr.IsLongHeader).To(BeFalse()) b := bytes.NewReader(data) extHdr, err := hdr.ParseExtended(b, versionIETFFrames) Expect(err).ToNot(HaveOccurred()) diff --git a/packet_handler_map.go b/packet_handler_map.go index 57994bf1..31b4d8b3 100644 --- a/packet_handler_map.go +++ b/packet_handler_map.go @@ -183,7 +183,7 @@ func (h *packetHandlerMap) handlePacket(addr net.Addr, data []byte) error { handlePacket = handler.handlePacket } else { // no session found // this might be a stateless reset - if !hdr.IsLongHeader() { + if !hdr.IsLongHeader { if len(data) >= protocol.MinStatelessResetSize { var token [16]byte copy(token[:], data[len(data)-16:]) @@ -217,7 +217,7 @@ func (h *packetHandlerMap) handlePacket(addr net.Addr, data []byte) error { extHdr.Raw = data[:len(data)-r.Len()] packetData = data[len(data)-r.Len():] - if hdr.IsLongHeader() { + if hdr.IsLongHeader { if extHdr.Length < protocol.ByteCount(extHdr.PacketNumberLen) { return fmt.Errorf("packet length (%d bytes) shorter than packet number (%d bytes)", extHdr.Length, extHdr.PacketNumberLen) } diff --git a/packet_handler_map_test.go b/packet_handler_map_test.go index 32da7c24..08f85411 100644 --- a/packet_handler_map_test.go +++ b/packet_handler_map_test.go @@ -22,12 +22,14 @@ var _ = Describe("Packet Handler Map", func() { getPacket := func(connID protocol.ConnectionID) []byte { buf := &bytes.Buffer{} Expect((&wire.ExtendedHeader{ - IsLongHeader: true, - Type: protocol.PacketTypeHandshake, - DestConnectionID: connID, - PacketNumberLen: protocol.PacketNumberLen1, - Length: 1, - Version: protocol.VersionWhatever, + Header: wire.Header{ + IsLongHeader: true, + DestConnectionID: connID, + Version: protocol.VersionWhatever, + }, + Length: 1, + Type: protocol.PacketTypeHandshake, + PacketNumberLen: protocol.PacketNumberLen1, }).Write(buf, protocol.VersionWhatever)).To(Succeed()) return buf.Bytes() } @@ -127,12 +129,14 @@ var _ = Describe("Packet Handler Map", func() { packetHandler.EXPECT().GetVersion().Return(protocol.VersionWhatever) handler.Add(connID, packetHandler) hdr := &wire.ExtendedHeader{ - IsLongHeader: true, - Type: protocol.PacketTypeHandshake, - Length: 1000, - DestConnectionID: connID, - PacketNumberLen: protocol.PacketNumberLen2, - Version: protocol.VersionWhatever, + Header: wire.Header{ + IsLongHeader: true, + DestConnectionID: connID, + Version: protocol.VersionWhatever, + }, + Type: protocol.PacketTypeHandshake, + Length: 1000, + PacketNumberLen: protocol.PacketNumberLen2, } buf := &bytes.Buffer{} Expect(hdr.Write(buf, protocol.VersionWhatever)).To(Succeed()) @@ -148,12 +152,14 @@ var _ = Describe("Packet Handler Map", func() { packetHandler.EXPECT().GetVersion().Return(protocol.VersionWhatever) handler.Add(connID, packetHandler) hdr := &wire.ExtendedHeader{ - IsLongHeader: true, - Type: protocol.PacketTypeHandshake, - Length: 3, - DestConnectionID: connID, - PacketNumberLen: protocol.PacketNumberLen4, - Version: protocol.VersionWhatever, + Header: wire.Header{ + IsLongHeader: true, + DestConnectionID: connID, + Version: protocol.VersionWhatever, + }, + Type: protocol.PacketTypeHandshake, + Length: 3, + PacketNumberLen: protocol.PacketNumberLen4, } buf := &bytes.Buffer{} Expect(hdr.Write(buf, protocol.VersionWhatever)).To(Succeed()) @@ -170,12 +176,14 @@ var _ = Describe("Packet Handler Map", func() { }) hdr := &wire.ExtendedHeader{ - IsLongHeader: true, - Type: protocol.PacketTypeHandshake, - Length: 456, - DestConnectionID: connID, - PacketNumberLen: protocol.PacketNumberLen1, - Version: protocol.VersionWhatever, + Header: wire.Header{ + IsLongHeader: true, + DestConnectionID: connID, + Version: protocol.VersionWhatever, + }, + Type: protocol.PacketTypeHandshake, + Length: 456, + PacketNumberLen: protocol.PacketNumberLen1, } buf := &bytes.Buffer{} Expect(hdr.Write(buf, protocol.VersionWhatever)).To(Succeed()) diff --git a/packet_packer.go b/packet_packer.go index 352c39ce..a87fcc80 100644 --- a/packet_packer.go +++ b/packet_packer.go @@ -399,12 +399,11 @@ func (p *packetPacker) composeNextPacket( func (p *packetPacker) getHeader(encLevel protocol.EncryptionLevel) *wire.ExtendedHeader { pn, pnLen := p.pnManager.PeekPacketNumber() - header := &wire.ExtendedHeader{ - PacketNumber: pn, - PacketNumberLen: pnLen, - Version: p.version, - DestConnectionID: p.destConnID, - } + header := &wire.ExtendedHeader{} + header.PacketNumber = pn + header.PacketNumberLen = pnLen + header.Version = p.version + header.DestConnectionID = p.destConnID if encLevel != protocol.Encryption1RTT { header.IsLongHeader = true diff --git a/server.go b/server.go index 86eba275..52beab58 100644 --- a/server.go +++ b/server.go @@ -431,15 +431,14 @@ func (s *server) sendRetry(remoteAddr net.Addr, hdr *wire.ExtendedHeader) error if err != nil { return err } - replyHdr := &wire.ExtendedHeader{ - IsLongHeader: true, - Type: protocol.PacketTypeRetry, - Version: hdr.Version, - SrcConnectionID: connID, - DestConnectionID: hdr.SrcConnectionID, - OrigDestConnectionID: hdr.DestConnectionID, - Token: token, - } + replyHdr := &wire.ExtendedHeader{} + replyHdr.IsLongHeader = true + replyHdr.Type = protocol.PacketTypeRetry + replyHdr.Version = hdr.Version + replyHdr.SrcConnectionID = connID + replyHdr.DestConnectionID = hdr.SrcConnectionID + replyHdr.OrigDestConnectionID = hdr.DestConnectionID + replyHdr.Token = token s.logger.Debugf("Changing connection ID to %s.\n-> Sending Retry", connID) replyHdr.Log(s.logger) buf := &bytes.Buffer{} diff --git a/server_session_test.go b/server_session_test.go index 5eb81637..e91b3382 100644 --- a/server_session_test.go +++ b/server_session_test.go @@ -22,7 +22,9 @@ var _ = Describe("Server Session", func() { It("handles packets", func() { p := &receivedPacket{ - extHdr: &wire.ExtendedHeader{DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5}}, + extHdr: &wire.ExtendedHeader{ + Header: wire.Header{DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5}}, + }, } qsess.EXPECT().handlePacket(p) sess.handlePacket(p) @@ -33,9 +35,11 @@ var _ = Describe("Server Session", func() { // don't EXPECT any calls to handlePacket() p := &receivedPacket{ extHdr: &wire.ExtendedHeader{ - IsLongHeader: true, - Version: protocol.VersionNumber(123), - DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef}, + Header: wire.Header{ + IsLongHeader: true, + Version: protocol.VersionNumber(123), + DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef}, + }, }, } err := sess.handlePacketImpl(p) @@ -46,10 +50,12 @@ var _ = Describe("Server Session", func() { qsess.EXPECT().GetVersion().Return(protocol.VersionNumber(100)) p := &receivedPacket{ extHdr: &wire.ExtendedHeader{ - IsLongHeader: true, - Type: protocol.PacketTypeRetry, - Version: protocol.VersionNumber(100), - DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef}, + Header: wire.Header{ + IsLongHeader: true, + Version: protocol.VersionNumber(100), + DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef}, + }, + Type: protocol.PacketTypeRetry, }, } err := sess.handlePacketImpl(p) @@ -59,10 +65,12 @@ var _ = Describe("Server Session", func() { It("passes on Handshake packets", func() { p := &receivedPacket{ extHdr: &wire.ExtendedHeader{ - IsLongHeader: true, - Type: protocol.PacketTypeHandshake, - Version: protocol.VersionNumber(100), - DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef}, + Header: wire.Header{ + IsLongHeader: true, + Version: protocol.VersionNumber(100), + DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef}, + }, + Type: protocol.PacketTypeHandshake, }, } qsess.EXPECT().GetVersion().Return(protocol.VersionNumber(100)) diff --git a/server_test.go b/server_test.go index abd8a6d5..9a24037e 100644 --- a/server_test.go +++ b/server_test.go @@ -109,10 +109,12 @@ var _ = Describe("Server", func() { It("drops Initial packets with a too short connection ID", func() { serv.handlePacket(&receivedPacket{ extHdr: &wire.ExtendedHeader{ - IsLongHeader: true, - Type: protocol.PacketTypeInitial, - DestConnectionID: protocol.ConnectionID{1, 2, 3, 4}, - Version: serv.config.Versions[0], + Header: wire.Header{ + IsLongHeader: true, + DestConnectionID: protocol.ConnectionID{1, 2, 3, 4}, + Version: serv.config.Versions[0], + }, + Type: protocol.PacketTypeInitial, }, }) Expect(conn.dataWritten.Len()).To(BeZero()) @@ -121,10 +123,12 @@ var _ = Describe("Server", func() { It("drops too small Initial", func() { serv.handlePacket(&receivedPacket{ extHdr: &wire.ExtendedHeader{ - IsLongHeader: true, - Type: protocol.PacketTypeInitial, - DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, - Version: serv.config.Versions[0], + Header: wire.Header{ + IsLongHeader: true, + DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, + Version: serv.config.Versions[0], + }, + Type: protocol.PacketTypeInitial, }, data: bytes.Repeat([]byte{0}, protocol.MinInitialPacketSize-100), }) @@ -133,12 +137,14 @@ var _ = Describe("Server", func() { It("drops packets with a too short connection ID", func() { hdr := &wire.ExtendedHeader{ - IsLongHeader: true, - Type: protocol.PacketTypeInitial, - SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, - DestConnectionID: protocol.ConnectionID{1, 2, 3, 4}, - Version: serv.config.Versions[0], - PacketNumberLen: protocol.PacketNumberLen1, + Header: wire.Header{ + IsLongHeader: true, + SrcConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8}, + DestConnectionID: protocol.ConnectionID{1, 2, 3, 4}, + Version: serv.config.Versions[0], + }, + Type: protocol.PacketTypeInitial, + PacketNumberLen: protocol.PacketNumberLen1, } serv.handlePacket(&receivedPacket{ extHdr: hdr, @@ -151,8 +157,8 @@ var _ = Describe("Server", func() { serv.logger.SetLogLevel(utils.LogLevelDebug) serv.handlePacket(&receivedPacket{ extHdr: &wire.ExtendedHeader{ - Type: protocol.PacketTypeHandshake, - Version: serv.config.Versions[0], + Header: wire.Header{Version: serv.config.Versions[0]}, + Type: protocol.PacketTypeHandshake, }, data: []byte("invalid"), }) @@ -175,9 +181,9 @@ var _ = Describe("Server", func() { serv.handlePacket(&receivedPacket{ remoteAddr: raddr, extHdr: &wire.ExtendedHeader{ - Type: protocol.PacketTypeInitial, - Token: token, - Version: serv.config.Versions[0], + Header: wire.Header{Version: serv.config.Versions[0]}, + Type: protocol.PacketTypeInitial, + Token: token, }, data: bytes.Repeat([]byte{0}, protocol.MinInitialPacketSize), }) @@ -199,9 +205,9 @@ var _ = Describe("Server", func() { serv.handlePacket(&receivedPacket{ remoteAddr: raddr, extHdr: &wire.ExtendedHeader{ - Type: protocol.PacketTypeInitial, - Token: []byte("foobar"), - Version: serv.config.Versions[0], + Header: wire.Header{Version: serv.config.Versions[0]}, + Type: protocol.PacketTypeInitial, + Token: []byte("foobar"), }, data: bytes.Repeat([]byte{0}, protocol.MinInitialPacketSize), }) @@ -213,11 +219,13 @@ var _ = Describe("Server", func() { destConnID := protocol.ConnectionID{1, 2, 3, 4, 5, 6} serv.handlePacket(&receivedPacket{ extHdr: &wire.ExtendedHeader{ - IsLongHeader: true, - SrcConnectionID: srcConnID, - DestConnectionID: destConnID, - Type: protocol.PacketTypeInitial, - Version: 0x42, + Header: wire.Header{ + IsLongHeader: true, + SrcConnectionID: srcConnID, + DestConnectionID: destConnID, + Version: 0x42, + }, + Type: protocol.PacketTypeInitial, }, }) Expect(conn.dataWritten.Len()).ToNot(BeZero()) @@ -232,10 +240,12 @@ var _ = Describe("Server", func() { It("replies with a Retry packet, if a Cookie is required", func() { serv.config.AcceptCookie = func(_ net.Addr, _ *Cookie) bool { return false } hdr := &wire.ExtendedHeader{ - Type: protocol.PacketTypeInitial, - SrcConnectionID: protocol.ConnectionID{5, 4, 3, 2, 1}, - DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, - Version: protocol.VersionTLS, + Header: wire.Header{ + SrcConnectionID: protocol.ConnectionID{5, 4, 3, 2, 1}, + DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + Version: protocol.VersionTLS, + }, + Type: protocol.PacketTypeInitial, } serv.handleInitial(&receivedPacket{ remoteAddr: &net.UDPAddr{}, @@ -254,10 +264,12 @@ var _ = Describe("Server", func() { It("creates a session, if no Cookie is required", func() { serv.config.AcceptCookie = func(_ net.Addr, _ *Cookie) bool { return true } hdr := &wire.ExtendedHeader{ - Type: protocol.PacketTypeInitial, - SrcConnectionID: protocol.ConnectionID{5, 4, 3, 2, 1}, - DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, - Version: protocol.VersionTLS, + Header: wire.Header{ + SrcConnectionID: protocol.ConnectionID{5, 4, 3, 2, 1}, + DestConnectionID: protocol.ConnectionID{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, + Version: protocol.VersionTLS, + }, + Type: protocol.PacketTypeInitial, } p := &receivedPacket{ extHdr: hdr, diff --git a/session_test.go b/session_test.go index cc4637ec..255f8a22 100644 --- a/session_test.go +++ b/session_test.go @@ -518,18 +518,22 @@ var _ = Describe("Session", func() { unpacker.EXPECT().Unpack(gomock.Any(), gomock.Any(), gomock.Any()).Return(&unpackedPacket{}, nil) err := sess.handlePacketImpl(&receivedPacket{ extHdr: &wire.ExtendedHeader{ - IsLongHeader: true, - DestConnectionID: sess.destConnID, - SrcConnectionID: sess.srcConnID, + Header: wire.Header{ + IsLongHeader: true, + DestConnectionID: sess.destConnID, + SrcConnectionID: sess.srcConnID, + }, }, }) Expect(err).ToNot(HaveOccurred()) // The next packet has to be ignored, since the source connection ID doesn't match. err = sess.handlePacketImpl(&receivedPacket{ extHdr: &wire.ExtendedHeader{ - IsLongHeader: true, - DestConnectionID: sess.destConnID, - SrcConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef}, + Header: wire.Header{ + IsLongHeader: true, + DestConnectionID: sess.destConnID, + SrcConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef}, + }, }, }) Expect(err).ToNot(HaveOccurred()) @@ -1318,10 +1322,12 @@ var _ = Describe("Client Session", func() { packer.EXPECT().ChangeDestConnectionID(newConnID) err := sess.handlePacketImpl(&receivedPacket{ extHdr: &wire.ExtendedHeader{ - IsLongHeader: true, - Type: protocol.PacketTypeHandshake, - SrcConnectionID: newConnID, - DestConnectionID: sess.srcConnID, + Header: wire.Header{ + IsLongHeader: true, + SrcConnectionID: newConnID, + DestConnectionID: sess.srcConnID, + }, + Type: protocol.PacketTypeHandshake, }, data: []byte{0}, })