/
public_reset.go
67 lines (59 loc) · 1.93 KB
/
public_reset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package wire
import (
"bytes"
"encoding/binary"
"errors"
"github.com/lucas-clemente/quic-go/internal/handshake"
"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/utils"
)
// 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 {
// TODO (QDC): a public reset should also contains the path ID
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) {
// TODO (QDC): a public reset should also contains the path ID
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
}