2021-08-03 09:04:02 +00:00
|
|
|
package quic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/lucas-clemente/quic-go/internal/utils"
|
|
|
|
|
|
|
|
"github.com/lucas-clemente/quic-go/internal/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Clone clones a Config
|
|
|
|
func (c *Config) Clone() *Config {
|
|
|
|
copy := *c
|
|
|
|
return ©
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) handshakeTimeout() time.Duration {
|
|
|
|
return utils.MaxDuration(protocol.DefaultHandshakeTimeout, 2*c.HandshakeIdleTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateConfig(config *Config) error {
|
|
|
|
if config == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if config.MaxIncomingStreams > 1<<60 {
|
|
|
|
return errors.New("invalid value for Config.MaxIncomingStreams")
|
|
|
|
}
|
|
|
|
if config.MaxIncomingUniStreams > 1<<60 {
|
|
|
|
return errors.New("invalid value for Config.MaxIncomingUniStreams")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// populateServerConfig populates fields in the quic.Config with their default values, if none are set
|
|
|
|
// it may be called with nil
|
|
|
|
func populateServerConfig(config *Config) *Config {
|
2022-08-10 13:19:03 +00:00
|
|
|
config = populateConfig(config, protocol.DefaultConnectionIDLength)
|
2021-08-03 09:04:02 +00:00
|
|
|
if config.AcceptToken == nil {
|
|
|
|
config.AcceptToken = defaultAcceptToken
|
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
// populateClientConfig populates fields in the quic.Config with their default values, if none are set
|
|
|
|
// it may be called with nil
|
|
|
|
func populateClientConfig(config *Config, createdPacketConn bool) *Config {
|
2022-08-10 13:19:03 +00:00
|
|
|
var defaultConnIdLen = protocol.DefaultConnectionIDLength
|
|
|
|
if createdPacketConn {
|
|
|
|
defaultConnIdLen = 0
|
2021-08-03 09:04:02 +00:00
|
|
|
}
|
2022-08-10 13:19:03 +00:00
|
|
|
|
|
|
|
config = populateConfig(config, defaultConnIdLen)
|
2021-08-03 09:04:02 +00:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2022-08-10 13:19:03 +00:00
|
|
|
func populateConfig(config *Config, defaultConnIDLen int) *Config {
|
2021-08-03 09:04:02 +00:00
|
|
|
if config == nil {
|
|
|
|
config = &Config{}
|
|
|
|
}
|
|
|
|
versions := config.Versions
|
|
|
|
if len(versions) == 0 {
|
|
|
|
versions = protocol.SupportedVersions
|
|
|
|
}
|
2022-08-10 13:19:03 +00:00
|
|
|
if config.ConnectionIDLength == 0 {
|
|
|
|
config.ConnectionIDLength = defaultConnIDLen
|
|
|
|
}
|
2021-08-03 09:04:02 +00:00
|
|
|
handshakeIdleTimeout := protocol.DefaultHandshakeIdleTimeout
|
|
|
|
if config.HandshakeIdleTimeout != 0 {
|
|
|
|
handshakeIdleTimeout = config.HandshakeIdleTimeout
|
|
|
|
}
|
|
|
|
idleTimeout := protocol.DefaultIdleTimeout
|
|
|
|
if config.MaxIdleTimeout != 0 {
|
|
|
|
idleTimeout = config.MaxIdleTimeout
|
|
|
|
}
|
|
|
|
initialStreamReceiveWindow := config.InitialStreamReceiveWindow
|
|
|
|
if initialStreamReceiveWindow == 0 {
|
|
|
|
initialStreamReceiveWindow = protocol.DefaultInitialMaxStreamData
|
|
|
|
}
|
|
|
|
maxStreamReceiveWindow := config.MaxStreamReceiveWindow
|
|
|
|
if maxStreamReceiveWindow == 0 {
|
|
|
|
maxStreamReceiveWindow = protocol.DefaultMaxReceiveStreamFlowControlWindow
|
|
|
|
}
|
|
|
|
initialConnectionReceiveWindow := config.InitialConnectionReceiveWindow
|
|
|
|
if initialConnectionReceiveWindow == 0 {
|
|
|
|
initialConnectionReceiveWindow = protocol.DefaultInitialMaxData
|
|
|
|
}
|
|
|
|
maxConnectionReceiveWindow := config.MaxConnectionReceiveWindow
|
|
|
|
if maxConnectionReceiveWindow == 0 {
|
|
|
|
maxConnectionReceiveWindow = protocol.DefaultMaxReceiveConnectionFlowControlWindow
|
|
|
|
}
|
|
|
|
maxIncomingStreams := config.MaxIncomingStreams
|
|
|
|
if maxIncomingStreams == 0 {
|
|
|
|
maxIncomingStreams = protocol.DefaultMaxIncomingStreams
|
|
|
|
} else if maxIncomingStreams < 0 {
|
|
|
|
maxIncomingStreams = 0
|
|
|
|
}
|
|
|
|
maxIncomingUniStreams := config.MaxIncomingUniStreams
|
|
|
|
if maxIncomingUniStreams == 0 {
|
|
|
|
maxIncomingUniStreams = protocol.DefaultMaxIncomingUniStreams
|
|
|
|
} else if maxIncomingUniStreams < 0 {
|
|
|
|
maxIncomingUniStreams = 0
|
|
|
|
}
|
2022-01-06 12:17:10 +00:00
|
|
|
maxDatagrameFrameSize := config.MaxDatagramFrameSize
|
|
|
|
if maxDatagrameFrameSize == 0 {
|
|
|
|
maxDatagrameFrameSize = int64(protocol.DefaultMaxDatagramFrameSize)
|
|
|
|
}
|
2022-08-10 13:19:03 +00:00
|
|
|
connIDGenerator := config.ConnectionIDGenerator
|
|
|
|
if connIDGenerator == nil {
|
|
|
|
connIDGenerator = &protocol.DefaultConnectionIDGenerator{ConnLen: config.ConnectionIDLength}
|
|
|
|
}
|
2021-08-03 09:04:02 +00:00
|
|
|
|
|
|
|
return &Config{
|
2021-08-27 11:26:00 +00:00
|
|
|
Versions: versions,
|
|
|
|
HandshakeIdleTimeout: handshakeIdleTimeout,
|
|
|
|
MaxIdleTimeout: idleTimeout,
|
|
|
|
AcceptToken: config.AcceptToken,
|
2022-06-06 13:15:35 +00:00
|
|
|
KeepAlivePeriod: config.KeepAlivePeriod,
|
2021-08-27 11:26:00 +00:00
|
|
|
InitialStreamReceiveWindow: initialStreamReceiveWindow,
|
|
|
|
MaxStreamReceiveWindow: maxStreamReceiveWindow,
|
|
|
|
InitialConnectionReceiveWindow: initialConnectionReceiveWindow,
|
|
|
|
MaxConnectionReceiveWindow: maxConnectionReceiveWindow,
|
2022-06-06 13:15:35 +00:00
|
|
|
AllowConnectionWindowIncrease: config.AllowConnectionWindowIncrease,
|
2021-08-27 11:26:00 +00:00
|
|
|
MaxIncomingStreams: maxIncomingStreams,
|
|
|
|
MaxIncomingUniStreams: maxIncomingUniStreams,
|
|
|
|
ConnectionIDLength: config.ConnectionIDLength,
|
2022-08-10 13:19:03 +00:00
|
|
|
ConnectionIDGenerator: connIDGenerator,
|
2021-08-27 11:26:00 +00:00
|
|
|
StatelessResetKey: config.StatelessResetKey,
|
|
|
|
TokenStore: config.TokenStore,
|
|
|
|
EnableDatagrams: config.EnableDatagrams,
|
2022-01-06 12:17:10 +00:00
|
|
|
MaxDatagramFrameSize: maxDatagrameFrameSize,
|
2021-08-27 11:26:00 +00:00
|
|
|
DisablePathMTUDiscovery: config.DisablePathMTUDiscovery,
|
|
|
|
DisableVersionNegotiationPackets: config.DisableVersionNegotiationPackets,
|
|
|
|
Tracer: config.Tracer,
|
2021-08-03 09:04:02 +00:00
|
|
|
}
|
|
|
|
}
|