embed the Header in the ExtendedHeader

This commit is contained in:
Marten Seemann 2018-11-25 16:47:29 +07:00
parent af157408de
commit b740d57c61
12 changed files with 306 additions and 244 deletions

View file

@ -50,7 +50,7 @@ var _ = Describe("Client", func() {
acceptClientVersionPacket := func(connID protocol.ConnectionID) []byte {
b := &bytes.Buffer{}
Expect((&wire.ExtendedHeader{
DestConnectionID: connID,
Header: wire.Header{DestConnectionID: connID},
PacketNumber: 1,
PacketNumberLen: 1,
}).Write(b, protocol.VersionWhatever)).To(Succeed())
@ -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{
Header: wire.Header{
IsLongHeader: true,
Version: protocol.VersionTLS,
},
Type: protocol.PacketTypeRetry,
Token: []byte("foobar"),
OrigDestConnectionID: connID,
Version: protocol.VersionTLS,
},
})
}).AnyTimes()

View file

@ -13,20 +13,19 @@ 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
IsVersionNegotiation bool
SupportedVersions []protocol.VersionNumber // Version Number sent in a Version Negotiation Packet by the server
Type protocol.PacketType
IsLongHeader bool
KeyPhase int
Length protocol.ByteCount
Token []byte

View file

@ -32,14 +32,16 @@ var _ = Describe("Header", func() {
It("writes", func() {
Expect((&ExtendedHeader{
Header: Header{
IsLongHeader: true,
Type: 0x5,
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,
Version: 0x1020304,
}).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{
Header: Header{
IsLongHeader: true,
Type: 0x5,
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,
Version: 0x1020304,
}).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{
Header: Header{
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
Version: 0x1020304,
},
Type: 0x5,
PacketNumber: 0xdecafbad,
PacketNumberLen: protocol.PacketNumberLen4,
Version: 0x1020304,
}).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{
err := (&ExtendedHeader{
Header: Header{
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
Version: 0x1020304,
},
Type: 0x5,
PacketNumber: 0xdecafbad,
PacketNumberLen: protocol.PacketNumberLen4,
Version: 0x1020304,
}).Write(buf, versionIETFHeader)).To(Succeed())
}).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{
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{
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{
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,7 +155,9 @@ var _ = Describe("Header", func() {
Context("short header", func() {
It("writes a header with connection ID", func() {
Expect((&ExtendedHeader{
Header: Header{
DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37},
},
PacketNumberLen: protocol.PacketNumberLen1,
PacketNumber: 0x42,
}).Write(buf, versionIETFHeader)).To(Succeed())
@ -215,10 +230,12 @@ var _ = Describe("Header", func() {
It("has the right length for the Long Header, for a short length", func() {
h := &ExtendedHeader{
Header: Header{
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},
},
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 */
@ -229,10 +246,12 @@ var _ = Describe("Header", func() {
It("has the right length for the Long Header, for a long length", func() {
h := &ExtendedHeader{
Header: Header{
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},
},
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 */
@ -243,11 +262,13 @@ var _ = Describe("Header", func() {
It("has the right length for an Initial not containing a Token", func() {
h := &ExtendedHeader{
Type: protocol.PacketTypeInitial,
Header: Header{
IsLongHeader: true,
Length: 1500,
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 */
@ -258,11 +279,13 @@ var _ = Describe("Header", func() {
It("has the right length for an Initial containing a Token", func() {
h := &ExtendedHeader{
Type: protocol.PacketTypeInitial,
Header: Header{
IsLongHeader: true,
Length: 1500,
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"),
}
@ -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,
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{
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,
DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37},
SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad, 0x013, 0x37, 0x13, 0x37},
Version: 0xfeed,
}).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{
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,
DestConnectionID: protocol.ConnectionID{0xca, 0xfe, 0x13, 0x37},
SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad},
Version: 0xfeed,
}).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{
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,
DestConnectionID: protocol.ConnectionID{0xca, 0xfe, 0x13, 0x37},
SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad},
Version: 0xfeed,
}).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{
Header: Header{
IsLongHeader: true,
Type: protocol.PacketTypeRetry,
DestConnectionID: protocol.ConnectionID{0xca, 0xfe, 0x13, 0x37},
SrcConnectionID: protocol.ConnectionID{0xde, 0xca, 0xfb, 0xad},
Version: 0xfeed,
},
Type: protocol.PacketTypeRetry,
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{
Header: Header{
DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37},
},
KeyPhase: 1,
PacketNumber: 0x1337,
PacketNumberLen: 4,
DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37},
}).Log(logger)
Expect(buf.String()).To(ContainSubstring("Short Header{DestConnectionID: 0xdeadbeefcafe1337, PacketNumber: 0x1337, PacketNumberLen: 4, KeyPhase: 1}"))
})

View file

@ -17,6 +17,7 @@ type Header struct {
SupportedVersions []protocol.VersionNumber // sent in a Version Negotiation Packet
IsLongHeader bool
typeByte byte
len int // how many bytes were read while parsing this 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}
}

View file

@ -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{
Header: Header{
IsLongHeader: true,
Type: 42,
SrcConnectionID: srcConnID,
Version: 0x10203040,
},
Type: 42,
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())

View file

@ -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)
}

View file

@ -22,12 +22,14 @@ var _ = Describe("Packet Handler Map", func() {
getPacket := func(connID protocol.ConnectionID) []byte {
buf := &bytes.Buffer{}
Expect((&wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
DestConnectionID: connID,
PacketNumberLen: protocol.PacketNumberLen1,
Length: 1,
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{
Header: wire.Header{
IsLongHeader: true,
DestConnectionID: connID,
Version: protocol.VersionWhatever,
},
Type: protocol.PacketTypeHandshake,
Length: 1000,
DestConnectionID: connID,
PacketNumberLen: protocol.PacketNumberLen2,
Version: protocol.VersionWhatever,
}
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{
Header: wire.Header{
IsLongHeader: true,
DestConnectionID: connID,
Version: protocol.VersionWhatever,
},
Type: protocol.PacketTypeHandshake,
Length: 3,
DestConnectionID: connID,
PacketNumberLen: protocol.PacketNumberLen4,
Version: protocol.VersionWhatever,
}
buf := &bytes.Buffer{}
Expect(hdr.Write(buf, protocol.VersionWhatever)).To(Succeed())
@ -170,12 +176,14 @@ var _ = Describe("Packet Handler Map", func() {
})
hdr := &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
DestConnectionID: connID,
Version: protocol.VersionWhatever,
},
Type: protocol.PacketTypeHandshake,
Length: 456,
DestConnectionID: connID,
PacketNumberLen: protocol.PacketNumberLen1,
Version: protocol.VersionWhatever,
}
buf := &bytes.Buffer{}
Expect(hdr.Write(buf, protocol.VersionWhatever)).To(Succeed())

View file

@ -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

View file

@ -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{}

View file

@ -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,10 +35,12 @@ var _ = Describe("Server Session", func() {
// don't EXPECT any calls to handlePacket()
p := &receivedPacket{
extHdr: &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Version: protocol.VersionNumber(123),
DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef},
},
},
}
err := sess.handlePacketImpl(p)
Expect(err).ToNot(HaveOccurred())
@ -46,11 +50,13 @@ var _ = Describe("Server Session", func() {
qsess.EXPECT().GetVersion().Return(protocol.VersionNumber(100))
p := &receivedPacket{
extHdr: &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeRetry,
Version: protocol.VersionNumber(100),
DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef},
},
Type: protocol.PacketTypeRetry,
},
}
err := sess.handlePacketImpl(p)
Expect(err).To(MatchError("Received unsupported packet type: Retry"))
@ -59,11 +65,13 @@ var _ = Describe("Server Session", func() {
It("passes on Handshake packets", func() {
p := &receivedPacket{
extHdr: &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
Version: protocol.VersionNumber(100),
DestConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef},
},
Type: protocol.PacketTypeHandshake,
},
}
qsess.EXPECT().GetVersion().Return(protocol.VersionNumber(100))
qsess.EXPECT().handlePacket(p)

View file

@ -109,11 +109,13 @@ var _ = Describe("Server", func() {
It("drops Initial packets with a too short connection ID", func() {
serv.handlePacket(&receivedPacket{
extHdr: &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
DestConnectionID: protocol.ConnectionID{1, 2, 3, 4},
Version: serv.config.Versions[0],
},
Type: protocol.PacketTypeInitial,
},
})
Expect(conn.dataWritten.Len()).To(BeZero())
})
@ -121,11 +123,13 @@ var _ = Describe("Server", func() {
It("drops too small Initial", func() {
serv.handlePacket(&receivedPacket{
extHdr: &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeInitial,
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),
})
Consistently(conn.dataWritten.Len).Should(BeZero())
@ -133,11 +137,13 @@ var _ = Describe("Server", func() {
It("drops packets with a too short connection ID", func() {
hdr := &wire.ExtendedHeader{
Header: wire.Header{
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],
},
Type: protocol.PacketTypeInitial,
PacketNumberLen: protocol.PacketNumberLen1,
}
serv.handlePacket(&receivedPacket{
@ -151,8 +157,8 @@ var _ = Describe("Server", func() {
serv.logger.SetLogLevel(utils.LogLevelDebug)
serv.handlePacket(&receivedPacket{
extHdr: &wire.ExtendedHeader{
Header: wire.Header{Version: serv.config.Versions[0]},
Type: protocol.PacketTypeHandshake,
Version: serv.config.Versions[0],
},
data: []byte("invalid"),
})
@ -175,9 +181,9 @@ var _ = Describe("Server", func() {
serv.handlePacket(&receivedPacket{
remoteAddr: raddr,
extHdr: &wire.ExtendedHeader{
Header: wire.Header{Version: serv.config.Versions[0]},
Type: protocol.PacketTypeInitial,
Token: token,
Version: serv.config.Versions[0],
},
data: bytes.Repeat([]byte{0}, protocol.MinInitialPacketSize),
})
@ -199,9 +205,9 @@ var _ = Describe("Server", func() {
serv.handlePacket(&receivedPacket{
remoteAddr: raddr,
extHdr: &wire.ExtendedHeader{
Header: wire.Header{Version: serv.config.Versions[0]},
Type: protocol.PacketTypeInitial,
Token: []byte("foobar"),
Version: serv.config.Versions[0],
},
data: bytes.Repeat([]byte{0}, protocol.MinInitialPacketSize),
})
@ -213,12 +219,14 @@ var _ = Describe("Server", func() {
destConnID := protocol.ConnectionID{1, 2, 3, 4, 5, 6}
serv.handlePacket(&receivedPacket{
extHdr: &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
SrcConnectionID: srcConnID,
DestConnectionID: destConnID,
Type: protocol.PacketTypeInitial,
Version: 0x42,
},
Type: protocol.PacketTypeInitial,
},
})
Expect(conn.dataWritten.Len()).ToNot(BeZero())
hdr, err := wire.ParseHeader(bytes.NewReader(conn.dataWritten.Bytes()), 0)
@ -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,
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,
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,

View file

@ -518,19 +518,23 @@ var _ = Describe("Session", func() {
unpacker.EXPECT().Unpack(gomock.Any(), gomock.Any(), gomock.Any()).Return(&unpackedPacket{}, nil)
err := sess.handlePacketImpl(&receivedPacket{
extHdr: &wire.ExtendedHeader{
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{
Header: wire.Header{
IsLongHeader: true,
DestConnectionID: sess.destConnID,
SrcConnectionID: protocol.ConnectionID{0xde, 0xad, 0xbe, 0xef},
},
},
})
Expect(err).ToNot(HaveOccurred())
})
@ -1318,11 +1322,13 @@ var _ = Describe("Client Session", func() {
packer.EXPECT().ChangeDestConnectionID(newConnID)
err := sess.handlePacketImpl(&receivedPacket{
extHdr: &wire.ExtendedHeader{
Header: wire.Header{
IsLongHeader: true,
Type: protocol.PacketTypeHandshake,
SrcConnectionID: newConnID,
DestConnectionID: sess.srcConnID,
},
Type: protocol.PacketTypeHandshake,
},
data: []byte{0},
})
Expect(err).ToNot(HaveOccurred())