1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 03:37:37 -05:00
v2fly/external/github.com/lucas-clemente/quic-go/internal/wire/version_negotiation.go

32 lines
1.1 KiB
Go
Raw Normal View History

2018-11-20 17:51:25 -05:00
package wire
import (
"bytes"
"crypto/rand"
2019-01-17 09:33:18 -05:00
"v2ray.com/core/external/github.com/lucas-clemente/quic-go/internal/protocol"
"v2ray.com/core/external/github.com/lucas-clemente/quic-go/internal/utils"
2018-11-20 17:51:25 -05:00
)
2018-11-23 11:04:53 -05:00
// ComposeVersionNegotiation composes a Version Negotiation
2018-11-20 17:51:25 -05:00
func ComposeVersionNegotiation(destConnID, srcConnID protocol.ConnectionID, versions []protocol.VersionNumber) ([]byte, error) {
greasedVersions := protocol.GetGreasedVersions(versions)
expectedLen := 1 /* type byte */ + 4 /* version field */ + 1 /* connection ID length field */ + destConnID.Len() + srcConnID.Len() + len(greasedVersions)*4
buf := bytes.NewBuffer(make([]byte, 0, expectedLen))
r := make([]byte, 1)
_, _ = rand.Read(r) // ignore the error here. It is not critical to have perfect random here.
buf.WriteByte(r[0] | 0x80)
utils.BigEndian.WriteUint32(buf, 0) // version 0
connIDLen, err := encodeConnIDLen(destConnID, srcConnID)
if err != nil {
return nil, err
}
buf.WriteByte(connIDLen)
buf.Write(destConnID)
buf.Write(srcConnID)
for _, v := range greasedVersions {
utils.BigEndian.WriteUint32(buf, uint32(v))
}
return buf.Bytes(), nil
}