simplify writing of the gQUIC Version Negotiation Packet

This commit is contained in:
Marten Seemann 2018-03-28 05:48:28 +07:00
parent 5e82335005
commit 1e9b3f0bb2
3 changed files with 36 additions and 48 deletions

View file

@ -20,6 +20,9 @@ var (
// writePublicHeader writes a Public Header. // writePublicHeader writes a Public Header.
func (h *Header) writePublicHeader(b *bytes.Buffer, pers protocol.Perspective, _ protocol.VersionNumber) error { func (h *Header) writePublicHeader(b *bytes.Buffer, pers protocol.Perspective, _ protocol.VersionNumber) error {
if h.VersionFlag && pers == protocol.PerspectiveServer {
return errors.New("PublicHeader: Writing of Version Negotiation Packets not supported")
}
if h.VersionFlag && h.ResetFlag { if h.VersionFlag && h.ResetFlag {
return errResetAndVersionFlagSet return errResetAndVersionFlagSet
} }

View file

@ -242,47 +242,40 @@ var _ = Describe("Public Header", func() {
VersionFlag: true, VersionFlag: true,
ResetFlag: true, ResetFlag: true,
} }
err := hdr.writePublicHeader(b, protocol.PerspectiveServer, protocol.VersionWhatever) err := hdr.writePublicHeader(b, protocol.PerspectiveClient, protocol.VersionWhatever)
Expect(err).To(MatchError(errResetAndVersionFlagSet)) Expect(err).To(MatchError(errResetAndVersionFlagSet))
}) })
Context("Version Negotiation packets", func() { It("doesn't write Version Negotiation Packets", func() {
It("sets the Version Flag for packets sent as a server", func() { b := &bytes.Buffer{}
b := &bytes.Buffer{} hdr := Header{
hdr := Header{ VersionFlag: true,
VersionFlag: true, ConnectionID: 0x4cfa9f9b668619f6,
ConnectionID: 0x4cfa9f9b668619f6, PacketNumber: 2,
PacketNumber: 2, PacketNumberLen: protocol.PacketNumberLen6,
PacketNumberLen: protocol.PacketNumberLen6, }
} err := hdr.writePublicHeader(b, protocol.PerspectiveServer, protocol.VersionWhatever)
err := hdr.writePublicHeader(b, protocol.PerspectiveServer, protocol.VersionWhatever) Expect(err).To(MatchError("PublicHeader: Writing of Version Negotiation Packets not supported"))
Expect(err).ToNot(HaveOccurred()) })
// must be the first assertion
Expect(b.Len()).To(Equal(1 + 8)) // 1 FlagByte + 8 ConnectionID
firstByte, _ := b.ReadByte()
Expect(firstByte & 0x01).To(Equal(uint8(1)))
Expect(firstByte & 0x30).To(BeZero()) // no packet number present
})
It("sets the Version Flag for packets sent as a client, and adds a packet number", func() { It("writes packets with Version Flag, as a client", func() {
b := &bytes.Buffer{} b := &bytes.Buffer{}
hdr := Header{ hdr := Header{
VersionFlag: true, VersionFlag: true,
Version: protocol.Version39, Version: protocol.Version39,
ConnectionID: 0x4cfa9f9b668619f6, ConnectionID: 0x4cfa9f9b668619f6,
PacketNumber: 0x42, PacketNumber: 0x42,
PacketNumberLen: protocol.PacketNumberLen1, PacketNumberLen: protocol.PacketNumberLen1,
} }
err := hdr.writePublicHeader(b, protocol.PerspectiveClient, protocol.VersionWhatever) err := hdr.writePublicHeader(b, protocol.PerspectiveClient, protocol.VersionWhatever)
Expect(err).ToNot(HaveOccurred()) Expect(err).ToNot(HaveOccurred())
// must be the first assertion // must be the first assertion
Expect(b.Len()).To(Equal(1 + 8 + 4 + 1)) // 1 FlagByte + 8 ConnectionID + 4 version number + 1 PacketNumber Expect(b.Len()).To(Equal(1 + 8 + 4 + 1)) // 1 FlagByte + 8 ConnectionID + 4 version number + 1 PacketNumber
firstByte, _ := b.ReadByte() firstByte, _ := b.ReadByte()
Expect(firstByte & 0x01).To(Equal(uint8(1))) Expect(firstByte & 0x01).To(Equal(uint8(1)))
Expect(firstByte & 0x30).To(Equal(uint8(0x0))) Expect(firstByte & 0x30).To(Equal(uint8(0x0)))
Expect(string(b.Bytes()[8:12])).To(Equal("Q039")) Expect(string(b.Bytes()[8:12])).To(Equal("Q039"))
Expect(b.Bytes()[12:13]).To(Equal([]byte{0x42})) Expect(b.Bytes()[12:13]).To(Equal([]byte{0x42}))
})
}) })
Context("PublicReset packets", func() { Context("PublicReset packets", func() {

View file

@ -10,17 +10,9 @@ import (
// ComposeGQUICVersionNegotiation composes a Version Negotiation Packet for gQUIC // ComposeGQUICVersionNegotiation composes a Version Negotiation Packet for gQUIC
func ComposeGQUICVersionNegotiation(connID protocol.ConnectionID, versions []protocol.VersionNumber) []byte { func ComposeGQUICVersionNegotiation(connID protocol.ConnectionID, versions []protocol.VersionNumber) []byte {
buf := &bytes.Buffer{} buf := bytes.NewBuffer(make([]byte, 0, 1+8+len(versions)*4))
ph := Header{ buf.Write([]byte{0x1 | 0x8}) // type byte
ConnectionID: connID, utils.BigEndian.WriteUint64(buf, uint64(connID))
PacketNumber: 1,
VersionFlag: true,
IsVersionNegotiation: true,
}
if err := ph.writePublicHeader(buf, protocol.PerspectiveServer, protocol.VersionWhatever); err != nil {
utils.Errorf("error composing version negotiation packet: %s", err.Error())
return nil
}
for _, v := range versions { for _, v := range versions {
utils.BigEndian.WriteUint32(buf, uint32(v)) utils.BigEndian.WriteUint32(buf, uint32(v))
} }