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