github.com/sagernet/quic-go@v0.43.1-beta.1/config.go (about) 1 package quic 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/sagernet/quic-go/internal/protocol" 8 "github.com/sagernet/quic-go/quicvarint" 9 ) 10 11 // Clone clones a Config 12 func (c *Config) Clone() *Config { 13 copy := *c 14 return © 15 } 16 17 func (c *Config) handshakeTimeout() time.Duration { 18 return 2 * c.HandshakeIdleTimeout 19 } 20 21 func (c *Config) maxRetryTokenAge() time.Duration { 22 return c.handshakeTimeout() 23 } 24 25 func validateConfig(config *Config) error { 26 if config == nil { 27 return nil 28 } 29 const maxStreams = 1 << 60 30 if config.MaxIncomingStreams > maxStreams { 31 config.MaxIncomingStreams = maxStreams 32 } 33 if config.MaxIncomingUniStreams > maxStreams { 34 config.MaxIncomingUniStreams = maxStreams 35 } 36 if config.MaxStreamReceiveWindow > quicvarint.Max { 37 config.MaxStreamReceiveWindow = quicvarint.Max 38 } 39 if config.MaxConnectionReceiveWindow > quicvarint.Max { 40 config.MaxConnectionReceiveWindow = quicvarint.Max 41 } 42 // check that all QUIC versions are actually supported 43 for _, v := range config.Versions { 44 if !protocol.IsValidVersion(v) { 45 return fmt.Errorf("invalid QUIC version: %s", v) 46 } 47 } 48 return nil 49 } 50 51 // populateConfig populates fields in the quic.Config with their default values, if none are set 52 // it may be called with nil 53 func populateConfig(config *Config) *Config { 54 if config == nil { 55 config = &Config{} 56 } 57 versions := config.Versions 58 if len(versions) == 0 { 59 versions = protocol.SupportedVersions 60 } 61 handshakeIdleTimeout := protocol.DefaultHandshakeIdleTimeout 62 if config.HandshakeIdleTimeout != 0 { 63 handshakeIdleTimeout = config.HandshakeIdleTimeout 64 } 65 idleTimeout := protocol.DefaultIdleTimeout 66 if config.MaxIdleTimeout != 0 { 67 idleTimeout = config.MaxIdleTimeout 68 } 69 initialStreamReceiveWindow := config.InitialStreamReceiveWindow 70 if initialStreamReceiveWindow == 0 { 71 initialStreamReceiveWindow = protocol.DefaultInitialMaxStreamData 72 } 73 maxStreamReceiveWindow := config.MaxStreamReceiveWindow 74 if maxStreamReceiveWindow == 0 { 75 maxStreamReceiveWindow = protocol.DefaultMaxReceiveStreamFlowControlWindow 76 } 77 initialConnectionReceiveWindow := config.InitialConnectionReceiveWindow 78 if initialConnectionReceiveWindow == 0 { 79 initialConnectionReceiveWindow = protocol.DefaultInitialMaxData 80 } 81 maxConnectionReceiveWindow := config.MaxConnectionReceiveWindow 82 if maxConnectionReceiveWindow == 0 { 83 maxConnectionReceiveWindow = protocol.DefaultMaxReceiveConnectionFlowControlWindow 84 } 85 maxIncomingStreams := config.MaxIncomingStreams 86 if maxIncomingStreams == 0 { 87 maxIncomingStreams = protocol.DefaultMaxIncomingStreams 88 } else if maxIncomingStreams < 0 { 89 maxIncomingStreams = 0 90 } 91 maxIncomingUniStreams := config.MaxIncomingUniStreams 92 if maxIncomingUniStreams == 0 { 93 maxIncomingUniStreams = protocol.DefaultMaxIncomingUniStreams 94 } else if maxIncomingUniStreams < 0 { 95 maxIncomingUniStreams = 0 96 } 97 98 return &Config{ 99 GetConfigForClient: config.GetConfigForClient, 100 Versions: versions, 101 HandshakeIdleTimeout: handshakeIdleTimeout, 102 MaxIdleTimeout: idleTimeout, 103 KeepAlivePeriod: config.KeepAlivePeriod, 104 InitialStreamReceiveWindow: initialStreamReceiveWindow, 105 MaxStreamReceiveWindow: maxStreamReceiveWindow, 106 InitialConnectionReceiveWindow: initialConnectionReceiveWindow, 107 MaxConnectionReceiveWindow: maxConnectionReceiveWindow, 108 AllowConnectionWindowIncrease: config.AllowConnectionWindowIncrease, 109 MaxIncomingStreams: maxIncomingStreams, 110 MaxIncomingUniStreams: maxIncomingUniStreams, 111 TokenStore: config.TokenStore, 112 EnableDatagrams: config.EnableDatagrams, 113 DisablePathMTUDiscovery: config.DisablePathMTUDiscovery, 114 Allow0RTT: config.Allow0RTT, 115 Tracer: config.Tracer, 116 MaxDatagramFrameSize: config.MaxDatagramFrameSize, 117 } 118 }