From 1e8d1453a8995e50ca73a18d456381843a473c5c Mon Sep 17 00:00:00 2001 From: Lucas Clemente Date: Fri, 29 Apr 2016 16:12:17 +0200 Subject: [PATCH] implement public reset packet writing --- handshake/tags.go | 7 +++++++ public_reset.go | 28 ++++++++++++++++++++++++++++ public_reset_test.go | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 public_reset.go create mode 100644 public_reset_test.go diff --git a/handshake/tags.go b/handshake/tags.go index 3173c110..6bbb6583 100644 --- a/handshake/tags.go +++ b/handshake/tags.go @@ -71,4 +71,11 @@ const ( // TagSHLO is the server hello TagSHLO Tag = 'S' + 'H'<<8 + 'L'<<16 + 'O'<<24 + + // TagPRST is the public reset tag + TagPRST Tag = 'P' + 'R'<<8 + 'S'<<16 + 'T'<<24 + // TagRSEQ is the public reset rejected packet number + TagRSEQ Tag = 'R' + 'S'<<8 + 'E'<<16 + 'Q'<<24 + // TagRNON is the public reset nonce + TagRNON Tag = 'R' + 'N'<<8 + 'O'<<16 + 'N'<<24 ) diff --git a/public_reset.go b/public_reset.go new file mode 100644 index 00000000..c54b8edd --- /dev/null +++ b/public_reset.go @@ -0,0 +1,28 @@ +package quic + +import ( + "bytes" + + "github.com/lucas-clemente/quic-go/handshake" + "github.com/lucas-clemente/quic-go/protocol" + "github.com/lucas-clemente/quic-go/utils" +) + +type publicResetPacket struct { + connectionID protocol.ConnectionID + rejectedPacketNumber protocol.PacketNumber + nonceProof uint64 +} + +func (p *publicResetPacket) Write(b *bytes.Buffer) { + b.WriteByte(0x0a) + utils.WriteUint64(b, uint64(p.connectionID)) + utils.WriteUint32(b, uint32(handshake.TagPRST)) + utils.WriteUint32(b, 2) + utils.WriteUint32(b, uint32(handshake.TagRNON)) + utils.WriteUint32(b, 8) + utils.WriteUint32(b, uint32(handshake.TagRSEQ)) + utils.WriteUint32(b, 16) + utils.WriteUint64(b, p.nonceProof) + utils.WriteUint64(b, uint64(p.rejectedPacketNumber)) +} diff --git a/public_reset_test.go b/public_reset_test.go new file mode 100644 index 00000000..42a6fa0d --- /dev/null +++ b/public_reset_test.go @@ -0,0 +1,34 @@ +package quic + +import ( + "bytes" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("public reset", func() { + Context("writing", func() { + It("writes public reset packets", func() { + packet := &publicResetPacket{ + connectionID: 0xdeadbeef, + rejectedPacketNumber: 0x8badf00d, + nonceProof: 0xdecafbad, + } + b := &bytes.Buffer{} + packet.Write(b) + Expect(b.Bytes()).To(Equal([]byte{ + 0x0a, + 0xef, 0xbe, 0xad, 0xde, 0x00, 0x00, 0x00, 0x00, + 'P', 'R', 'S', 'T', + 0x02, 0x00, 0x00, 0x00, + 'R', 'N', 'O', 'N', + 0x08, 0x00, 0x00, 0x00, + 'R', 'S', 'E', 'Q', + 0x10, 0x00, 0x00, 0x00, + 0xad, 0xfb, 0xca, 0xde, 0x0, 0x0, 0x0, 0x0, + 0x0d, 0xf0, 0xad, 0x8b, 0x0, 0x0, 0x0, 0x0, + })) + }) + }) +})