|
| 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 | +// packetType is a QUIC packet type. |
| 8 | +// https://www.rfc-editor.org/rfc/rfc9000.html#section-17 |
| 9 | +type packetType byte |
| 10 | + |
| 11 | +const ( |
| 12 | + packetTypeInvalid = packetType(iota) |
| 13 | + packetTypeInitial |
| 14 | + packetType0RTT |
| 15 | + packetTypeHandshake |
| 16 | + packetTypeRetry |
| 17 | + packetType1RTT |
| 18 | + packetTypeVersionNegotiation |
| 19 | +) |
| 20 | + |
| 21 | +// Bits set in the first byte of a packet. |
| 22 | +const ( |
| 23 | + headerFormLong = 0x80 // https://www.rfc-editor.org/rfc/rfc9000.html#section-17.2-3.2.1 |
| 24 | + headerFormShort = 0x00 // https://www.rfc-editor.org/rfc/rfc9000.html#section-17.3.1-4.2.1 |
| 25 | + fixedBit = 0x40 // https://www.rfc-editor.org/rfc/rfc9000.html#section-17.2-3.4.1 |
| 26 | + reservedBits = 0x0c // https://www.rfc-editor.org/rfc/rfc9000#section-17.2-8.2.1 |
| 27 | +) |
| 28 | + |
| 29 | +// Long Packet Type bits. |
| 30 | +// https://www.rfc-editor.org/rfc/rfc9000.html#section-17.2-3.6.1 |
| 31 | +const ( |
| 32 | + longPacketTypeInitial = 0 << 4 |
| 33 | + longPacketType0RTT = 1 << 4 |
| 34 | + longPacketTypeHandshake = 2 << 4 |
| 35 | + longPacketTypeRetry = 3 << 4 |
| 36 | +) |
| 37 | + |
| 38 | +// Frame types. |
| 39 | +// https://www.rfc-editor.org/rfc/rfc9000.html#section-19 |
| 40 | +const ( |
| 41 | + frameTypePadding = 0x00 |
| 42 | + frameTypePing = 0x01 |
| 43 | + frameTypeAck = 0x02 |
| 44 | + frameTypeAckECN = 0x03 |
| 45 | + frameTypeResetStream = 0x04 |
| 46 | + frameTypeStopSending = 0x05 |
| 47 | + frameTypeCrypto = 0x06 |
| 48 | + frameTypeNewToken = 0x07 |
| 49 | + frameTypeStreamBase = 0x08 // low three bits carry stream flags |
| 50 | + frameTypeMaxData = 0x10 |
| 51 | + frameTypeMaxStreamData = 0x11 |
| 52 | + frameTypeMaxStreamsBidi = 0x12 |
| 53 | + frameTypeMaxStreamsUni = 0x13 |
| 54 | + frameTypeDataBlocked = 0x14 |
| 55 | + frameTypeStreamDataBlocked = 0x15 |
| 56 | + frameTypeStreamsBlockedBidi = 0x16 |
| 57 | + frameTypeStreamsBlockedUni = 0x17 |
| 58 | + frameTypeNewConnectionID = 0x18 |
| 59 | + frameTypeRetireConnectionID = 0x19 |
| 60 | + frameTypePathChallenge = 0x1a |
| 61 | + frameTypePathResponse = 0x1b |
| 62 | + frameTypeConnectionCloseTransport = 0x1c |
| 63 | + frameTypeConnectionCloseApplication = 0x1d |
| 64 | + frameTypeHandshakeDone = 0x1e |
| 65 | +) |
| 66 | + |
| 67 | +// The low three bits of STREAM frames. |
| 68 | +// https://www.rfc-editor.org/rfc/rfc9000.html#section-19.8 |
| 69 | +const ( |
| 70 | + streamOffBit = 0x04 |
| 71 | + streamLenBit = 0x02 |
| 72 | + streamFinBit = 0x01 |
| 73 | +) |
| 74 | + |
| 75 | +// isLongHeader returns true if b is the first byte of a long header. |
| 76 | +func isLongHeader(b byte) bool { |
| 77 | + return b&headerFormLong == headerFormLong |
| 78 | +} |
| 79 | + |
| 80 | +// getPacketType returns the type of a packet. |
| 81 | +func getPacketType(b []byte) packetType { |
| 82 | + if len(b) == 0 { |
| 83 | + return packetTypeInvalid |
| 84 | + } |
| 85 | + if !isLongHeader(b[0]) { |
| 86 | + if b[0]&fixedBit != fixedBit { |
| 87 | + return packetTypeInvalid |
| 88 | + } |
| 89 | + return packetType1RTT |
| 90 | + } |
| 91 | + if len(b) < 5 { |
| 92 | + return packetTypeInvalid |
| 93 | + } |
| 94 | + if b[1] == 0 && b[2] == 0 && b[3] == 0 && b[4] == 0 { |
| 95 | + // Version Negotiation packets don't necessarily set the fixed bit. |
| 96 | + return packetTypeVersionNegotiation |
| 97 | + } |
| 98 | + if b[0]&fixedBit != fixedBit { |
| 99 | + return packetTypeInvalid |
| 100 | + } |
| 101 | + switch b[0] & 0x30 { |
| 102 | + case longPacketTypeInitial: |
| 103 | + return packetTypeInitial |
| 104 | + case longPacketType0RTT: |
| 105 | + return packetType0RTT |
| 106 | + case longPacketTypeHandshake: |
| 107 | + return packetTypeHandshake |
| 108 | + case longPacketTypeRetry: |
| 109 | + return packetTypeRetry |
| 110 | + } |
| 111 | + return packetTypeInvalid |
| 112 | +} |
| 113 | + |
| 114 | +// dstConnIDForDatagram returns the destination connection ID field of the |
| 115 | +// first QUIC packet in a datagram. |
| 116 | +func dstConnIDForDatagram(pkt []byte) (id []byte, ok bool) { |
| 117 | + if len(pkt) < 1 { |
| 118 | + return nil, false |
| 119 | + } |
| 120 | + var n int |
| 121 | + var b []byte |
| 122 | + if isLongHeader(pkt[0]) { |
| 123 | + if len(pkt) < 6 { |
| 124 | + return nil, false |
| 125 | + } |
| 126 | + n = int(pkt[5]) |
| 127 | + b = pkt[6:] |
| 128 | + } else { |
| 129 | + n = connIDLen |
| 130 | + b = pkt[1:] |
| 131 | + } |
| 132 | + if len(b) < n { |
| 133 | + return nil, false |
| 134 | + } |
| 135 | + return b[:n], true |
| 136 | +} |
| 137 | + |
| 138 | +// A longPacket is a long header packet. |
| 139 | +type longPacket struct { |
| 140 | + ptype packetType |
| 141 | + reservedBits uint8 |
| 142 | + version uint32 |
| 143 | + num packetNumber |
| 144 | + dstConnID []byte |
| 145 | + srcConnID []byte |
| 146 | + payload []byte |
| 147 | + |
| 148 | + // The extra data depends on the packet type: |
| 149 | + // Initial: Token. |
| 150 | + // Retry: Retry token and integrity tag. |
| 151 | + extra []byte |
| 152 | +} |
| 153 | + |
| 154 | +// A shortPacket is a short header (1-RTT) packet. |
| 155 | +type shortPacket struct { |
| 156 | + reservedBits uint8 |
| 157 | + num packetNumber |
| 158 | + payload []byte |
| 159 | +} |
0 commit comments