github.com/tumi8/quic-go@v0.37.4-tum/config.go (about)

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