github.com/quic-go/quic-go@v0.44.0/config.go (about)

     1  package quic
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/quic-go/quic-go/internal/protocol"
     8  	"github.com/quic-go/quic-go/quicvarint"
     9  )
    10  
    11  // Clone clones a Config
    12  func (c *Config) Clone() *Config {
    13  	copy := *c
    14  	return &copy
    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  	if config.InitialPacketSize > 0 && config.InitialPacketSize < protocol.MinInitialPacketSize {
    43  		config.InitialPacketSize = protocol.MinInitialPacketSize
    44  	}
    45  	if config.InitialPacketSize > protocol.MaxPacketBufferSize {
    46  		config.InitialPacketSize = protocol.MaxPacketBufferSize
    47  	}
    48  	// check that all QUIC versions are actually supported
    49  	for _, v := range config.Versions {
    50  		if !protocol.IsValidVersion(v) {
    51  			return fmt.Errorf("invalid QUIC version: %s", v)
    52  		}
    53  	}
    54  	return nil
    55  }
    56  
    57  // populateConfig populates fields in the quic.Config with their default values, if none are set
    58  // it may be called with nil
    59  func populateConfig(config *Config) *Config {
    60  	if config == nil {
    61  		config = &Config{}
    62  	}
    63  	versions := config.Versions
    64  	if len(versions) == 0 {
    65  		versions = protocol.SupportedVersions
    66  	}
    67  	handshakeIdleTimeout := protocol.DefaultHandshakeIdleTimeout
    68  	if config.HandshakeIdleTimeout != 0 {
    69  		handshakeIdleTimeout = config.HandshakeIdleTimeout
    70  	}
    71  	idleTimeout := protocol.DefaultIdleTimeout
    72  	if config.MaxIdleTimeout != 0 {
    73  		idleTimeout = config.MaxIdleTimeout
    74  	}
    75  	initialStreamReceiveWindow := config.InitialStreamReceiveWindow
    76  	if initialStreamReceiveWindow == 0 {
    77  		initialStreamReceiveWindow = protocol.DefaultInitialMaxStreamData
    78  	}
    79  	maxStreamReceiveWindow := config.MaxStreamReceiveWindow
    80  	if maxStreamReceiveWindow == 0 {
    81  		maxStreamReceiveWindow = protocol.DefaultMaxReceiveStreamFlowControlWindow
    82  	}
    83  	initialConnectionReceiveWindow := config.InitialConnectionReceiveWindow
    84  	if initialConnectionReceiveWindow == 0 {
    85  		initialConnectionReceiveWindow = protocol.DefaultInitialMaxData
    86  	}
    87  	maxConnectionReceiveWindow := config.MaxConnectionReceiveWindow
    88  	if maxConnectionReceiveWindow == 0 {
    89  		maxConnectionReceiveWindow = protocol.DefaultMaxReceiveConnectionFlowControlWindow
    90  	}
    91  	maxIncomingStreams := config.MaxIncomingStreams
    92  	if maxIncomingStreams == 0 {
    93  		maxIncomingStreams = protocol.DefaultMaxIncomingStreams
    94  	} else if maxIncomingStreams < 0 {
    95  		maxIncomingStreams = 0
    96  	}
    97  	maxIncomingUniStreams := config.MaxIncomingUniStreams
    98  	if maxIncomingUniStreams == 0 {
    99  		maxIncomingUniStreams = protocol.DefaultMaxIncomingUniStreams
   100  	} else if maxIncomingUniStreams < 0 {
   101  		maxIncomingUniStreams = 0
   102  	}
   103  	initialPacketSize := config.InitialPacketSize
   104  	if initialPacketSize == 0 {
   105  		initialPacketSize = protocol.InitialPacketSize
   106  	}
   107  
   108  	return &Config{
   109  		GetConfigForClient:             config.GetConfigForClient,
   110  		Versions:                       versions,
   111  		HandshakeIdleTimeout:           handshakeIdleTimeout,
   112  		MaxIdleTimeout:                 idleTimeout,
   113  		KeepAlivePeriod:                config.KeepAlivePeriod,
   114  		InitialStreamReceiveWindow:     initialStreamReceiveWindow,
   115  		MaxStreamReceiveWindow:         maxStreamReceiveWindow,
   116  		InitialConnectionReceiveWindow: initialConnectionReceiveWindow,
   117  		MaxConnectionReceiveWindow:     maxConnectionReceiveWindow,
   118  		AllowConnectionWindowIncrease:  config.AllowConnectionWindowIncrease,
   119  		MaxIncomingStreams:             maxIncomingStreams,
   120  		MaxIncomingUniStreams:          maxIncomingUniStreams,
   121  		TokenStore:                     config.TokenStore,
   122  		EnableDatagrams:                config.EnableDatagrams,
   123  		InitialPacketSize:              initialPacketSize,
   124  		DisablePathMTUDiscovery:        config.DisablePathMTUDiscovery,
   125  		Allow0RTT:                      config.Allow0RTT,
   126  		Tracer:                         config.Tracer,
   127  	}
   128  }