github.com/sagernet/quic-go@v0.43.1-beta.1/internal/wire/version_negotiation.go (about) 1 package wire 2 3 import ( 4 "crypto/rand" 5 "encoding/binary" 6 "errors" 7 8 "github.com/sagernet/quic-go/internal/protocol" 9 ) 10 11 // ParseVersionNegotiationPacket parses a Version Negotiation packet. 12 func ParseVersionNegotiationPacket(b []byte) (dest, src protocol.ArbitraryLenConnectionID, _ []protocol.Version, _ error) { 13 n, dest, src, err := ParseArbitraryLenConnectionIDs(b) 14 if err != nil { 15 return nil, nil, nil, err 16 } 17 b = b[n:] 18 if len(b) == 0 { 19 //nolint:stylecheck 20 return nil, nil, nil, errors.New("Version Negotiation packet has empty version list") 21 } 22 if len(b)%4 != 0 { 23 //nolint:stylecheck 24 return nil, nil, nil, errors.New("Version Negotiation packet has a version list with an invalid length") 25 } 26 versions := make([]protocol.Version, len(b)/4) 27 for i := 0; len(b) > 0; i++ { 28 versions[i] = protocol.Version(binary.BigEndian.Uint32(b[:4])) 29 b = b[4:] 30 } 31 return dest, src, versions, nil 32 } 33 34 // ComposeVersionNegotiation composes a Version Negotiation 35 func ComposeVersionNegotiation(destConnID, srcConnID protocol.ArbitraryLenConnectionID, versions []protocol.Version) []byte { 36 greasedVersions := protocol.GetGreasedVersions(versions) 37 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 38 buf := make([]byte, 1+4 /* type byte and version field */, expectedLen) 39 _, _ = rand.Read(buf[:1]) // ignore the error here. It is not critical to have perfect random here. 40 // Setting the "QUIC bit" (0x40) is not required by the RFC, 41 // but it allows clients to demultiplex QUIC with a long list of other protocols. 42 // See RFC 9443 and https://mailarchive.ietf.org/arch/msg/quic/oR4kxGKY6mjtPC1CZegY1ED4beg/ for details. 43 buf[0] |= 0xc0 44 // The next 4 bytes are left at 0 (version number). 45 buf = append(buf, uint8(destConnID.Len())) 46 buf = append(buf, destConnID.Bytes()...) 47 buf = append(buf, uint8(srcConnID.Len())) 48 buf = append(buf, srcConnID.Bytes()...) 49 for _, v := range greasedVersions { 50 buf = binary.BigEndian.AppendUint32(buf, uint32(v)) 51 } 52 return buf 53 }