File: version_negotiation.go

package info (click to toggle)
golang-github-lucas-clemente-quic-go 0.54.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,312 kB
  • sloc: sh: 54; makefile: 7
file content (53 lines) | stat: -rw-r--r-- 2,245 bytes parent folder | download | duplicates (2)
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
package wire

import (
	"crypto/rand"
	"encoding/binary"
	"errors"

	"github.com/quic-go/quic-go/internal/protocol"
)

// ParseVersionNegotiationPacket parses a Version Negotiation packet.
func ParseVersionNegotiationPacket(b []byte) (dest, src protocol.ArbitraryLenConnectionID, _ []protocol.Version, _ error) {
	n, dest, src, err := ParseArbitraryLenConnectionIDs(b)
	if err != nil {
		return nil, nil, nil, err
	}
	b = b[n:]
	if len(b) == 0 {
		//nolint:staticcheck // SA1021: the packet is called Version Negotiation packet
		return nil, nil, nil, errors.New("Version Negotiation packet has empty version list")
	}
	if len(b)%4 != 0 {
		//nolint:staticcheck // SA1021: the packet is called Version Negotiation packet
		return nil, nil, nil, errors.New("Version Negotiation packet has a version list with an invalid length")
	}
	versions := make([]protocol.Version, len(b)/4)
	for i := 0; len(b) > 0; i++ {
		versions[i] = protocol.Version(binary.BigEndian.Uint32(b[:4]))
		b = b[4:]
	}
	return dest, src, versions, nil
}

// ComposeVersionNegotiation composes a Version Negotiation
func ComposeVersionNegotiation(destConnID, srcConnID protocol.ArbitraryLenConnectionID, versions []protocol.Version) []byte {
	greasedVersions := protocol.GetGreasedVersions(versions)
	expectedLen := 1 /* type byte */ + 4 /* version field */ + 1 /* dest connection ID length field */ + destConnID.Len() + 1 /* src connection ID length field */ + srcConnID.Len() + len(greasedVersions)*4
	buf := make([]byte, 1+4 /* type byte and version field */, expectedLen)
	_, _ = rand.Read(buf[:1]) // ignore the error here. It is not critical to have perfect random here.
	// Setting the "QUIC bit" (0x40) is not required by the RFC,
	// but it allows clients to demultiplex QUIC with a long list of other protocols.
	// See RFC 9443 and https://mailarchive.ietf.org/arch/msg/quic/oR4kxGKY6mjtPC1CZegY1ED4beg/ for details.
	buf[0] |= 0xc0
	// The next 4 bytes are left at 0 (version number).
	buf = append(buf, uint8(destConnID.Len()))
	buf = append(buf, destConnID.Bytes()...)
	buf = append(buf, uint8(srcConnID.Len()))
	buf = append(buf, srcConnID.Bytes()...)
	for _, v := range greasedVersions {
		buf = binary.BigEndian.AppendUint32(buf, uint32(v))
	}
	return buf
}