github.com/daeuniverse/quic-go@v0.0.0-20240413031024-943f218e0810/internal/protocol/protocol.go (about)

     1  package protocol
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  // The PacketType is the Long Header Type
     9  type PacketType uint8
    10  
    11  const (
    12  	// PacketTypeInitial is the packet type of an Initial packet
    13  	PacketTypeInitial PacketType = 1 + iota
    14  	// PacketTypeRetry is the packet type of a Retry packet
    15  	PacketTypeRetry
    16  	// PacketTypeHandshake is the packet type of a Handshake packet
    17  	PacketTypeHandshake
    18  	// PacketType0RTT is the packet type of a 0-RTT packet
    19  	PacketType0RTT
    20  )
    21  
    22  func (t PacketType) String() string {
    23  	switch t {
    24  	case PacketTypeInitial:
    25  		return "Initial"
    26  	case PacketTypeRetry:
    27  		return "Retry"
    28  	case PacketTypeHandshake:
    29  		return "Handshake"
    30  	case PacketType0RTT:
    31  		return "0-RTT Protected"
    32  	default:
    33  		return fmt.Sprintf("unknown packet type: %d", t)
    34  	}
    35  }
    36  
    37  type ECN uint8
    38  
    39  const (
    40  	ECNUnsupported ECN = iota
    41  	ECNNon             // 00
    42  	ECT1               // 01
    43  	ECT0               // 10
    44  	ECNCE              // 11
    45  )
    46  
    47  func ParseECNHeaderBits(bits byte) ECN {
    48  	switch bits {
    49  	case 0:
    50  		return ECNNon
    51  	case 0b00000010:
    52  		return ECT0
    53  	case 0b00000001:
    54  		return ECT1
    55  	case 0b00000011:
    56  		return ECNCE
    57  	default:
    58  		panic("invalid ECN bits")
    59  	}
    60  }
    61  
    62  func (e ECN) ToHeaderBits() byte {
    63  	//nolint:exhaustive // There are only 4 values.
    64  	switch e {
    65  	case ECNNon:
    66  		return 0
    67  	case ECT0:
    68  		return 0b00000010
    69  	case ECT1:
    70  		return 0b00000001
    71  	case ECNCE:
    72  		return 0b00000011
    73  	default:
    74  		panic("ECN unsupported")
    75  	}
    76  }
    77  
    78  func (e ECN) String() string {
    79  	switch e {
    80  	case ECNUnsupported:
    81  		return "ECN unsupported"
    82  	case ECNNon:
    83  		return "Not-ECT"
    84  	case ECT1:
    85  		return "ECT(1)"
    86  	case ECT0:
    87  		return "ECT(0)"
    88  	case ECNCE:
    89  		return "CE"
    90  	default:
    91  		return fmt.Sprintf("invalid ECN value: %d", e)
    92  	}
    93  }
    94  
    95  // A ByteCount in QUIC
    96  type ByteCount int64
    97  
    98  // MaxByteCount is the maximum value of a ByteCount
    99  const MaxByteCount = ByteCount(1<<62 - 1)
   100  
   101  // InvalidByteCount is an invalid byte count
   102  const InvalidByteCount ByteCount = -1
   103  
   104  // A StatelessResetToken is a stateless reset token.
   105  type StatelessResetToken [16]byte
   106  
   107  // MaxPacketBufferSize maximum packet size of any QUIC packet, based on
   108  // ethernet's max size, minus the IP and UDP headers. IPv6 has a 40 byte header,
   109  // UDP adds an additional 8 bytes.  This is a total overhead of 48 bytes.
   110  // Ethernet's max packet size is 1500 bytes,  1500 - 48 = 1452.
   111  const MaxPacketBufferSize = 1452
   112  
   113  // MaxLargePacketBufferSize is used when using GSO
   114  const MaxLargePacketBufferSize = 20 * 1024
   115  
   116  // MinInitialPacketSize is the minimum size an Initial packet is required to have.
   117  const MinInitialPacketSize = 1200
   118  
   119  // MinUnknownVersionPacketSize is the minimum size a packet with an unknown version
   120  // needs to have in order to trigger a Version Negotiation packet.
   121  const MinUnknownVersionPacketSize = MinInitialPacketSize
   122  
   123  // MinStatelessResetSize is the minimum size of a stateless reset packet that we send
   124  const MinStatelessResetSize = 1 /* first byte */ + 20 /* max. conn ID length */ + 4 /* max. packet number length */ + 1 /* min. payload length */ + 16 /* token */
   125  
   126  // MinConnectionIDLenInitial is the minimum length of the destination connection ID on an Initial packet.
   127  const MinConnectionIDLenInitial = 8
   128  
   129  // DefaultAckDelayExponent is the default ack delay exponent
   130  const DefaultAckDelayExponent = 3
   131  
   132  // DefaultActiveConnectionIDLimit is the default active connection ID limit
   133  const DefaultActiveConnectionIDLimit = 2
   134  
   135  // MaxAckDelayExponent is the maximum ack delay exponent
   136  const MaxAckDelayExponent = 20
   137  
   138  // DefaultMaxAckDelay is the default max_ack_delay
   139  const DefaultMaxAckDelay = 25 * time.Millisecond
   140  
   141  // MaxMaxAckDelay is the maximum max_ack_delay
   142  const MaxMaxAckDelay = (1<<14 - 1) * time.Millisecond
   143  
   144  // MaxConnIDLen is the maximum length of the connection ID
   145  const MaxConnIDLen = 20
   146  
   147  // InvalidPacketLimitAES is the maximum number of packets that we can fail to decrypt when using
   148  // AEAD_AES_128_GCM or AEAD_AES_265_GCM.
   149  const InvalidPacketLimitAES = 1 << 52
   150  
   151  // InvalidPacketLimitChaCha is the maximum number of packets that we can fail to decrypt when using AEAD_CHACHA20_POLY1305.
   152  const InvalidPacketLimitChaCha = 1 << 36