Source file src/vendor/golang.org/x/net/quic/quic.go

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package quic
     6  
     7  import (
     8  	"time"
     9  )
    10  
    11  // QUIC versions.
    12  // We only support v1 at this time.
    13  const (
    14  	quicVersion1 = 1
    15  	quicVersion2 = 0x6b3343cf // https://www.rfc-editor.org/rfc/rfc9369
    16  )
    17  
    18  // connIDLen is the length in bytes of connection IDs chosen by this package.
    19  // Since 1-RTT packets don't include a connection ID length field,
    20  // we use a consistent length for all our IDs.
    21  // https://www.rfc-editor.org/rfc/rfc9000.html#section-5.1-6
    22  const connIDLen = 8
    23  
    24  // Local values of various transport parameters.
    25  // https://www.rfc-editor.org/rfc/rfc9000.html#section-18.2
    26  const (
    27  	defaultMaxIdleTimeout = 30 * time.Second // max_idle_timeout
    28  
    29  	// The max_udp_payload_size transport parameter is the size of our
    30  	// network receive buffer.
    31  	//
    32  	// Set this to the largest UDP packet that can be sent over
    33  	// Ethernet without using jumbo frames: 1500 byte Ethernet frame,
    34  	// minus 20 byte IPv4 header and 8 byte UDP header.
    35  	//
    36  	// The maximum possible UDP payload is 65527 bytes. Supporting this
    37  	// without wasting memory in unused receive buffers will require some
    38  	// care. For now, just limit ourselves to the most common case.
    39  	maxUDPPayloadSize = 1472
    40  
    41  	ackDelayExponent = 3                     // ack_delay_exponent
    42  	maxAckDelay      = 25 * time.Millisecond // max_ack_delay
    43  
    44  	// The active_conn_id_limit transport parameter is the maximum
    45  	// number of connection IDs from the peer we're willing to store.
    46  	//
    47  	// maxPeerActiveConnIDLimit is the maximum number of connection IDs
    48  	// we're willing to send to the peer.
    49  	//
    50  	// https://www.rfc-editor.org/rfc/rfc9000.html#section-18.2-6.2.1
    51  	activeConnIDLimit        = 2
    52  	maxPeerActiveConnIDLimit = 4
    53  )
    54  
    55  // Time limit for completing the handshake.
    56  const defaultHandshakeTimeout = 10 * time.Second
    57  
    58  // Keep-alive ping frequency.
    59  const defaultKeepAlivePeriod = 0
    60  
    61  // Local timer granularity.
    62  // https://www.rfc-editor.org/rfc/rfc9002.html#section-6.1.2-6
    63  const timerGranularity = 1 * time.Millisecond
    64  
    65  // The smallest allowed maximum datagram size.
    66  // https://www.rfc-editor.org/rfc/rfc9000#section-14
    67  const smallestMaxDatagramSize = 1200
    68  
    69  // Minimum size of a UDP datagram sent by a client carrying an Initial packet,
    70  // or a server containing an ack-eliciting Initial packet.
    71  // https://www.rfc-editor.org/rfc/rfc9000#section-14.1
    72  const paddedInitialDatagramSize = smallestMaxDatagramSize
    73  
    74  // Maximum number of streams of a given type which may be created.
    75  // https://www.rfc-editor.org/rfc/rfc9000.html#section-4.6-2
    76  const maxStreamsLimit = 1 << 60
    77  
    78  // Maximum number of streams we will allow the peer to create implicitly.
    79  // A stream ID that is used out of order results in all streams of that type
    80  // with lower-numbered IDs also being opened. To limit the amount of work we
    81  // will do in response to a single frame, we cap the peer's stream limit to
    82  // this value.
    83  const implicitStreamLimit = 100
    84  
    85  // A connSide distinguishes between the client and server sides of a connection.
    86  type connSide int8
    87  
    88  const (
    89  	clientSide = connSide(iota)
    90  	serverSide
    91  )
    92  
    93  func (s connSide) String() string {
    94  	switch s {
    95  	case clientSide:
    96  		return "client"
    97  	case serverSide:
    98  		return "server"
    99  	default:
   100  		return "BUG"
   101  	}
   102  }
   103  
   104  func (s connSide) peer() connSide {
   105  	if s == clientSide {
   106  		return serverSide
   107  	} else {
   108  		return clientSide
   109  	}
   110  }
   111  
   112  // A numberSpace is the context in which a packet number applies.
   113  // https://www.rfc-editor.org/rfc/rfc9000.html#section-12.3-7
   114  type numberSpace byte
   115  
   116  const (
   117  	initialSpace = numberSpace(iota)
   118  	handshakeSpace
   119  	appDataSpace
   120  	numberSpaceCount
   121  )
   122  
   123  func (n numberSpace) String() string {
   124  	switch n {
   125  	case initialSpace:
   126  		return "Initial"
   127  	case handshakeSpace:
   128  		return "Handshake"
   129  	case appDataSpace:
   130  		return "AppData"
   131  	default:
   132  		return "BUG"
   133  	}
   134  }
   135  
   136  // A streamType is the type of a stream: bidirectional or unidirectional.
   137  type streamType uint8
   138  
   139  const (
   140  	bidiStream = streamType(iota)
   141  	uniStream
   142  	streamTypeCount
   143  )
   144  
   145  func (s streamType) qlogString() string {
   146  	switch s {
   147  	case bidiStream:
   148  		return "bidirectional"
   149  	case uniStream:
   150  		return "unidirectional"
   151  	default:
   152  		return "BUG"
   153  	}
   154  }
   155  
   156  func (s streamType) String() string {
   157  	switch s {
   158  	case bidiStream:
   159  		return "bidi"
   160  	case uniStream:
   161  		return "uni"
   162  	default:
   163  		return "BUG"
   164  	}
   165  }
   166  
   167  // A streamID is a QUIC stream ID.
   168  // https://www.rfc-editor.org/rfc/rfc9000.html#section-2.1
   169  type streamID uint64
   170  
   171  // The two least significant bits of a stream ID indicate the initiator
   172  // and directionality of the stream. The upper bits are the stream number.
   173  // Each of the four possible combinations of initiator and direction
   174  // each has a distinct number space.
   175  const (
   176  	clientInitiatedStreamBit = 0x0
   177  	serverInitiatedStreamBit = 0x1
   178  	initiatorStreamBitMask   = 0x1
   179  
   180  	bidiStreamBit    = 0x0
   181  	uniStreamBit     = 0x2
   182  	dirStreamBitMask = 0x2
   183  )
   184  
   185  func newStreamID(initiator connSide, typ streamType, num int64) streamID {
   186  	id := streamID(num << 2)
   187  	if typ == uniStream {
   188  		id |= uniStreamBit
   189  	}
   190  	if initiator == serverSide {
   191  		id |= serverInitiatedStreamBit
   192  	}
   193  	return id
   194  }
   195  
   196  func (s streamID) initiator() connSide {
   197  	if s&initiatorStreamBitMask == serverInitiatedStreamBit {
   198  		return serverSide
   199  	}
   200  	return clientSide
   201  }
   202  
   203  func (s streamID) num() int64 {
   204  	return int64(s) >> 2
   205  }
   206  
   207  func (s streamID) streamType() streamType {
   208  	if s&dirStreamBitMask == uniStreamBit {
   209  		return uniStream
   210  	}
   211  	return bidiStream
   212  }
   213  
   214  // packetFate is the fate of a sent packet: Either acknowledged by the peer,
   215  // or declared lost.
   216  type packetFate byte
   217  
   218  const (
   219  	packetLost = packetFate(iota)
   220  	packetAcked
   221  )
   222  

View as plain text