move the Public Reset to the wire package

This commit is contained in:
Marten Seemann
2017-08-29 23:58:27 +07:00
parent ed522ebbce
commit 524ecb5827
7 changed files with 39 additions and 36 deletions

View File

@@ -0,0 +1,65 @@
package wire
import (
"bytes"
"encoding/binary"
"errors"
"github.com/lucas-clemente/quic-go/handshake"
"github.com/lucas-clemente/quic-go/internal/utils"
"github.com/lucas-clemente/quic-go/protocol"
)
// A PublicReset is a PUBLIC_RESET
type PublicReset struct {
RejectedPacketNumber protocol.PacketNumber
Nonce uint64
}
// WritePublicReset writes a Public Reset
func WritePublicReset(connectionID protocol.ConnectionID, rejectedPacketNumber protocol.PacketNumber, nonceProof uint64) []byte {
b := &bytes.Buffer{}
b.WriteByte(0x0a)
utils.LittleEndian.WriteUint64(b, uint64(connectionID))
utils.LittleEndian.WriteUint32(b, uint32(handshake.TagPRST))
utils.LittleEndian.WriteUint32(b, 2)
utils.LittleEndian.WriteUint32(b, uint32(handshake.TagRNON))
utils.LittleEndian.WriteUint32(b, 8)
utils.LittleEndian.WriteUint32(b, uint32(handshake.TagRSEQ))
utils.LittleEndian.WriteUint32(b, 16)
utils.LittleEndian.WriteUint64(b, nonceProof)
utils.LittleEndian.WriteUint64(b, uint64(rejectedPacketNumber))
return b.Bytes()
}
// ParsePublicReset parses a Public Reset
func ParsePublicReset(r *bytes.Reader) (*PublicReset, error) {
pr := PublicReset{}
msg, err := handshake.ParseHandshakeMessage(r)
if err != nil {
return nil, err
}
if msg.Tag != handshake.TagPRST {
return nil, errors.New("wrong public reset tag")
}
rseq, ok := msg.Data[handshake.TagRSEQ]
if !ok {
return nil, errors.New("RSEQ missing")
}
if len(rseq) != 8 {
return nil, errors.New("invalid RSEQ tag")
}
pr.RejectedPacketNumber = protocol.PacketNumber(binary.LittleEndian.Uint64(rseq))
rnon, ok := msg.Data[handshake.TagRNON]
if !ok {
return nil, errors.New("RNON missing")
}
if len(rnon) != 8 {
return nil, errors.New("invalid RNON tag")
}
pr.Nonce = binary.LittleEndian.Uint64(rnon)
return &pr, nil
}

View File

@@ -0,0 +1,95 @@
package wire
import (
"bytes"
"io"
"github.com/lucas-clemente/quic-go/handshake"
"github.com/lucas-clemente/quic-go/protocol"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("public reset", func() {
Context("writing", func() {
It("writes public reset packets", func() {
Expect(WritePublicReset(0xdeadbeef, 0x8badf00d, 0xdecafbad)).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,
}))
})
})
Context("parsing", func() {
var b *bytes.Buffer
BeforeEach(func() {
b = &bytes.Buffer{}
})
It("parses a public reset", func() {
packet := WritePublicReset(0xdeadbeef, 0x8badf00d, 0xdecafbad)
pr, err := ParsePublicReset(bytes.NewReader(packet[9:])) // 1 byte Public Flag, 8 bytes connection ID
Expect(err).ToNot(HaveOccurred())
Expect(pr.Nonce).To(Equal(uint64(0xdecafbad)))
Expect(pr.RejectedPacketNumber).To(Equal(protocol.PacketNumber(0x8badf00d)))
})
It("rejects packets that it can't parse", func() {
_, err := ParsePublicReset(bytes.NewReader([]byte{}))
Expect(err).To(MatchError(io.EOF))
})
It("rejects packets with the wrong tag", func() {
handshake.HandshakeMessage{Tag: handshake.TagREJ, Data: nil}.Write(b)
_, err := ParsePublicReset(bytes.NewReader(b.Bytes()))
Expect(err).To(MatchError("wrong public reset tag"))
})
It("rejects packets missing the nonce", func() {
data := map[handshake.Tag][]byte{
handshake.TagRSEQ: []byte{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37},
}
handshake.HandshakeMessage{Tag: handshake.TagPRST, Data: data}.Write(b)
_, err := ParsePublicReset(bytes.NewReader(b.Bytes()))
Expect(err).To(MatchError("RNON missing"))
})
It("rejects packets with a wrong length nonce", func() {
data := map[handshake.Tag][]byte{
handshake.TagRSEQ: []byte{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37},
handshake.TagRNON: []byte{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13},
}
handshake.HandshakeMessage{Tag: handshake.TagPRST, Data: data}.Write(b)
_, err := ParsePublicReset(bytes.NewReader(b.Bytes()))
Expect(err).To(MatchError("invalid RNON tag"))
})
It("rejects packets missing the rejected packet number", func() {
data := map[handshake.Tag][]byte{
handshake.TagRNON: []byte{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37},
}
handshake.HandshakeMessage{Tag: handshake.TagPRST, Data: data}.Write(b)
_, err := ParsePublicReset(bytes.NewReader(b.Bytes()))
Expect(err).To(MatchError("RSEQ missing"))
})
It("rejects packets with a wrong length rejected packet number", func() {
data := map[handshake.Tag][]byte{
handshake.TagRSEQ: []byte{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13},
handshake.TagRNON: []byte{0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x13, 0x37},
}
handshake.HandshakeMessage{Tag: handshake.TagPRST, Data: data}.Write(b)
_, err := ParsePublicReset(bytes.NewReader(b.Bytes()))
Expect(err).To(MatchError("invalid RSEQ tag"))
})
})
})