QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 5 | #include "quic/core/quic_packets.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 6 | |
bnc | 463f235 | 2019-10-10 04:49:34 -0700 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
vasilvv | 72b789c | 2020-10-27 17:39:17 -0700 | [diff] [blame] | 9 | #include "absl/strings/escaping.h" |
vasilvv | 9edb31e | 2020-12-03 19:32:58 -0800 | [diff] [blame] | 10 | #include "absl/strings/str_cat.h" |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 11 | #include "absl/strings/string_view.h" |
QUICHE team | 5be974e | 2020-12-29 18:35:24 -0500 | [diff] [blame] | 12 | #include "quic/core/quic_connection_id.h" |
| 13 | #include "quic/core/quic_types.h" |
| 14 | #include "quic/core/quic_utils.h" |
| 15 | #include "quic/core/quic_versions.h" |
| 16 | #include "quic/platform/api/quic_flag_utils.h" |
| 17 | #include "quic/platform/api/quic_flags.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 18 | |
| 19 | namespace quic { |
| 20 | |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 21 | QuicConnectionId GetServerConnectionIdAsRecipient( |
| 22 | const QuicPacketHeader& header, |
| 23 | Perspective perspective) { |
dschinazi | 5e1a7b2 | 2019-07-31 12:23:21 -0700 | [diff] [blame] | 24 | if (perspective == Perspective::IS_SERVER) { |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 25 | return header.destination_connection_id; |
| 26 | } |
| 27 | return header.source_connection_id; |
| 28 | } |
| 29 | |
dschinazi | ac6805d | 2019-05-30 09:44:27 -0700 | [diff] [blame] | 30 | QuicConnectionId GetClientConnectionIdAsRecipient( |
| 31 | const QuicPacketHeader& header, |
| 32 | Perspective perspective) { |
dschinazi | ac6805d | 2019-05-30 09:44:27 -0700 | [diff] [blame] | 33 | if (perspective == Perspective::IS_CLIENT) { |
| 34 | return header.destination_connection_id; |
| 35 | } |
| 36 | return header.source_connection_id; |
| 37 | } |
| 38 | |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 39 | QuicConnectionId GetServerConnectionIdAsSender(const QuicPacketHeader& header, |
| 40 | Perspective perspective) { |
dschinazi | 5e1a7b2 | 2019-07-31 12:23:21 -0700 | [diff] [blame] | 41 | if (perspective == Perspective::IS_CLIENT) { |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 42 | return header.destination_connection_id; |
| 43 | } |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 44 | return header.source_connection_id; |
| 45 | } |
| 46 | |
| 47 | QuicConnectionIdIncluded GetServerConnectionIdIncludedAsSender( |
| 48 | const QuicPacketHeader& header, |
| 49 | Perspective perspective) { |
dschinazi | 5e1a7b2 | 2019-07-31 12:23:21 -0700 | [diff] [blame] | 50 | if (perspective == Perspective::IS_CLIENT) { |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 51 | return header.destination_connection_id_included; |
| 52 | } |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 53 | return header.source_connection_id_included; |
| 54 | } |
| 55 | |
dschinazi | ac6805d | 2019-05-30 09:44:27 -0700 | [diff] [blame] | 56 | QuicConnectionId GetClientConnectionIdAsSender(const QuicPacketHeader& header, |
| 57 | Perspective perspective) { |
dschinazi | 5e1a7b2 | 2019-07-31 12:23:21 -0700 | [diff] [blame] | 58 | if (perspective == Perspective::IS_CLIENT) { |
dschinazi | ac6805d | 2019-05-30 09:44:27 -0700 | [diff] [blame] | 59 | return header.source_connection_id; |
| 60 | } |
dschinazi | ac6805d | 2019-05-30 09:44:27 -0700 | [diff] [blame] | 61 | return header.destination_connection_id; |
| 62 | } |
| 63 | |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 64 | QuicConnectionIdIncluded GetClientConnectionIdIncludedAsSender( |
| 65 | const QuicPacketHeader& header, |
| 66 | Perspective perspective) { |
dschinazi | 5e1a7b2 | 2019-07-31 12:23:21 -0700 | [diff] [blame] | 67 | if (perspective == Perspective::IS_CLIENT) { |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 68 | return header.source_connection_id_included; |
| 69 | } |
| 70 | return header.destination_connection_id_included; |
| 71 | } |
| 72 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 73 | QuicConnectionIdLength GetIncludedConnectionIdLength( |
| 74 | QuicConnectionId connection_id, |
| 75 | QuicConnectionIdIncluded connection_id_included) { |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 76 | QUICHE_DCHECK(connection_id_included == CONNECTION_ID_PRESENT || |
| 77 | connection_id_included == CONNECTION_ID_ABSENT); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 78 | return connection_id_included == CONNECTION_ID_PRESENT |
| 79 | ? static_cast<QuicConnectionIdLength>(connection_id.length()) |
| 80 | : PACKET_0BYTE_CONNECTION_ID; |
| 81 | } |
| 82 | |
| 83 | QuicConnectionIdLength GetIncludedDestinationConnectionIdLength( |
| 84 | const QuicPacketHeader& header) { |
| 85 | return GetIncludedConnectionIdLength( |
| 86 | header.destination_connection_id, |
| 87 | header.destination_connection_id_included); |
| 88 | } |
| 89 | |
| 90 | QuicConnectionIdLength GetIncludedSourceConnectionIdLength( |
| 91 | const QuicPacketHeader& header) { |
| 92 | return GetIncludedConnectionIdLength(header.source_connection_id, |
| 93 | header.source_connection_id_included); |
| 94 | } |
| 95 | |
| 96 | size_t GetPacketHeaderSize(QuicTransportVersion version, |
| 97 | const QuicPacketHeader& header) { |
| 98 | return GetPacketHeaderSize( |
| 99 | version, GetIncludedDestinationConnectionIdLength(header), |
| 100 | GetIncludedSourceConnectionIdLength(header), header.version_flag, |
| 101 | header.nonce != nullptr, header.packet_number_length, |
| 102 | header.retry_token_length_length, header.retry_token.length(), |
| 103 | header.length_length); |
| 104 | } |
| 105 | |
| 106 | size_t GetPacketHeaderSize( |
| 107 | QuicTransportVersion version, |
| 108 | QuicConnectionIdLength destination_connection_id_length, |
| 109 | QuicConnectionIdLength source_connection_id_length, |
| 110 | bool include_version, |
| 111 | bool include_diversification_nonce, |
| 112 | QuicPacketNumberLength packet_number_length, |
| 113 | QuicVariableLengthIntegerLength retry_token_length_length, |
| 114 | QuicByteCount retry_token_length, |
| 115 | QuicVariableLengthIntegerLength length_length) { |
fayang | d4291e4 | 2019-05-30 10:31:21 -0700 | [diff] [blame] | 116 | if (VersionHasIetfInvariantHeader(version)) { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 117 | if (include_version) { |
| 118 | // Long header. |
nharper | d43f1d6 | 2019-07-01 15:18:20 -0700 | [diff] [blame] | 119 | size_t size = kPacketHeaderTypeSize + kConnectionIdLengthSize + |
| 120 | destination_connection_id_length + |
fayang | 36825da | 2019-08-21 14:01:27 -0700 | [diff] [blame] | 121 | source_connection_id_length + packet_number_length + |
nharper | d43f1d6 | 2019-07-01 15:18:20 -0700 | [diff] [blame] | 122 | kQuicVersionSize; |
| 123 | if (include_diversification_nonce) { |
| 124 | size += kDiversificationNonceSize; |
| 125 | } |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 126 | if (VersionHasLengthPrefixedConnectionIds(version)) { |
| 127 | size += kConnectionIdLengthSize; |
| 128 | } |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 129 | QUICHE_DCHECK( |
| 130 | QuicVersionHasLongHeaderLengths(version) || |
| 131 | retry_token_length_length + retry_token_length + length_length == 0); |
nharper | 405f719 | 2019-09-11 08:28:06 -0700 | [diff] [blame] | 132 | if (QuicVersionHasLongHeaderLengths(version)) { |
nharper | d43f1d6 | 2019-07-01 15:18:20 -0700 | [diff] [blame] | 133 | size += retry_token_length_length + retry_token_length + length_length; |
| 134 | } |
| 135 | return size; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 136 | } |
| 137 | // Short header. |
| 138 | return kPacketHeaderTypeSize + destination_connection_id_length + |
| 139 | packet_number_length; |
| 140 | } |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 141 | // Google QUIC versions <= 43 can only carry one connection ID. |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 142 | QUICHE_DCHECK(destination_connection_id_length == 0 || |
| 143 | source_connection_id_length == 0); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 144 | return kPublicFlagsSize + destination_connection_id_length + |
QUICHE team | 2252b70 | 2019-05-14 23:55:14 -0400 | [diff] [blame] | 145 | source_connection_id_length + |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 146 | (include_version ? kQuicVersionSize : 0) + packet_number_length + |
| 147 | (include_diversification_nonce ? kDiversificationNonceSize : 0); |
| 148 | } |
| 149 | |
| 150 | size_t GetStartOfEncryptedData(QuicTransportVersion version, |
| 151 | const QuicPacketHeader& header) { |
| 152 | return GetPacketHeaderSize(version, header); |
| 153 | } |
| 154 | |
| 155 | size_t GetStartOfEncryptedData( |
| 156 | QuicTransportVersion version, |
| 157 | QuicConnectionIdLength destination_connection_id_length, |
| 158 | QuicConnectionIdLength source_connection_id_length, |
| 159 | bool include_version, |
| 160 | bool include_diversification_nonce, |
| 161 | QuicPacketNumberLength packet_number_length, |
| 162 | QuicVariableLengthIntegerLength retry_token_length_length, |
| 163 | QuicByteCount retry_token_length, |
| 164 | QuicVariableLengthIntegerLength length_length) { |
| 165 | // Encryption starts before private flags. |
| 166 | return GetPacketHeaderSize( |
| 167 | version, destination_connection_id_length, source_connection_id_length, |
| 168 | include_version, include_diversification_nonce, packet_number_length, |
| 169 | retry_token_length_length, retry_token_length, length_length); |
| 170 | } |
| 171 | |
| 172 | QuicPacketHeader::QuicPacketHeader() |
| 173 | : destination_connection_id(EmptyQuicConnectionId()), |
| 174 | destination_connection_id_included(CONNECTION_ID_PRESENT), |
| 175 | source_connection_id(EmptyQuicConnectionId()), |
| 176 | source_connection_id_included(CONNECTION_ID_ABSENT), |
| 177 | reset_flag(false), |
| 178 | version_flag(false), |
| 179 | has_possible_stateless_reset_token(false), |
| 180 | packet_number_length(PACKET_4BYTE_PACKET_NUMBER), |
| 181 | version(UnsupportedQuicVersion()), |
| 182 | nonce(nullptr), |
| 183 | form(GOOGLE_QUIC_PACKET), |
| 184 | long_packet_type(INITIAL), |
bnc | 1ccd0bc | 2021-04-07 10:20:17 -0700 | [diff] [blame] | 185 | possible_stateless_reset_token({}), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 186 | retry_token_length_length(VARIABLE_LENGTH_INTEGER_LENGTH_0), |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 187 | retry_token(absl::string_view()), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 188 | length_length(VARIABLE_LENGTH_INTEGER_LENGTH_0), |
| 189 | remaining_packet_length(0) {} |
| 190 | |
| 191 | QuicPacketHeader::QuicPacketHeader(const QuicPacketHeader& other) = default; |
| 192 | |
| 193 | QuicPacketHeader::~QuicPacketHeader() {} |
| 194 | |
danzh | ddf023a | 2019-12-20 13:33:36 -0800 | [diff] [blame] | 195 | QuicPacketHeader& QuicPacketHeader::operator=(const QuicPacketHeader& other) = |
| 196 | default; |
| 197 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 198 | QuicPublicResetPacket::QuicPublicResetPacket() |
| 199 | : connection_id(EmptyQuicConnectionId()), nonce_proof(0) {} |
| 200 | |
| 201 | QuicPublicResetPacket::QuicPublicResetPacket(QuicConnectionId connection_id) |
| 202 | : connection_id(connection_id), nonce_proof(0) {} |
| 203 | |
| 204 | QuicVersionNegotiationPacket::QuicVersionNegotiationPacket() |
| 205 | : connection_id(EmptyQuicConnectionId()) {} |
| 206 | |
| 207 | QuicVersionNegotiationPacket::QuicVersionNegotiationPacket( |
| 208 | QuicConnectionId connection_id) |
| 209 | : connection_id(connection_id) {} |
| 210 | |
| 211 | QuicVersionNegotiationPacket::QuicVersionNegotiationPacket( |
| 212 | const QuicVersionNegotiationPacket& other) = default; |
| 213 | |
| 214 | QuicVersionNegotiationPacket::~QuicVersionNegotiationPacket() {} |
| 215 | |
| 216 | QuicIetfStatelessResetPacket::QuicIetfStatelessResetPacket() |
bnc | 1ccd0bc | 2021-04-07 10:20:17 -0700 | [diff] [blame] | 217 | : stateless_reset_token({}) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 218 | |
| 219 | QuicIetfStatelessResetPacket::QuicIetfStatelessResetPacket( |
| 220 | const QuicPacketHeader& header, |
bnc | 1ccd0bc | 2021-04-07 10:20:17 -0700 | [diff] [blame] | 221 | StatelessResetToken token) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 222 | : header(header), stateless_reset_token(token) {} |
| 223 | |
| 224 | QuicIetfStatelessResetPacket::QuicIetfStatelessResetPacket( |
| 225 | const QuicIetfStatelessResetPacket& other) = default; |
| 226 | |
| 227 | QuicIetfStatelessResetPacket::~QuicIetfStatelessResetPacket() {} |
| 228 | |
| 229 | std::ostream& operator<<(std::ostream& os, const QuicPacketHeader& header) { |
| 230 | os << "{ destination_connection_id: " << header.destination_connection_id |
| 231 | << " (" |
| 232 | << (header.destination_connection_id_included == CONNECTION_ID_PRESENT |
| 233 | ? "present" |
| 234 | : "absent") |
| 235 | << "), source_connection_id: " << header.source_connection_id << " (" |
| 236 | << (header.source_connection_id_included == CONNECTION_ID_PRESENT |
| 237 | ? "present" |
| 238 | : "absent") |
nharper | 4437e40 | 2020-01-06 16:47:08 -0800 | [diff] [blame] | 239 | << "), packet_number_length: " |
| 240 | << static_cast<int>(header.packet_number_length) |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 241 | << ", reset_flag: " << header.reset_flag |
| 242 | << ", version_flag: " << header.version_flag; |
| 243 | if (header.version_flag) { |
| 244 | os << ", version: " << ParsedQuicVersionToString(header.version); |
| 245 | if (header.long_packet_type != INVALID_PACKET_TYPE) { |
| 246 | os << ", long_packet_type: " |
| 247 | << QuicUtils::QuicLongHeaderTypetoString(header.long_packet_type); |
| 248 | } |
| 249 | if (header.retry_token_length_length != VARIABLE_LENGTH_INTEGER_LENGTH_0) { |
| 250 | os << ", retry_token_length_length: " |
| 251 | << static_cast<int>(header.retry_token_length_length); |
| 252 | } |
| 253 | if (header.retry_token.length() != 0) { |
| 254 | os << ", retry_token_length: " << header.retry_token.length(); |
| 255 | } |
| 256 | if (header.length_length != VARIABLE_LENGTH_INTEGER_LENGTH_0) { |
| 257 | os << ", length_length: " << static_cast<int>(header.length_length); |
| 258 | } |
| 259 | if (header.remaining_packet_length != 0) { |
| 260 | os << ", remaining_packet_length: " << header.remaining_packet_length; |
| 261 | } |
| 262 | } |
| 263 | if (header.nonce != nullptr) { |
| 264 | os << ", diversification_nonce: " |
vasilvv | 72b789c | 2020-10-27 17:39:17 -0700 | [diff] [blame] | 265 | << absl::BytesToHexString( |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 266 | absl::string_view(header.nonce->data(), header.nonce->size())); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 267 | } |
| 268 | os << ", packet_number: " << header.packet_number << " }\n"; |
| 269 | return os; |
| 270 | } |
| 271 | |
| 272 | QuicData::QuicData(const char* buffer, size_t length) |
| 273 | : buffer_(buffer), length_(length), owns_buffer_(false) {} |
| 274 | |
| 275 | QuicData::QuicData(const char* buffer, size_t length, bool owns_buffer) |
| 276 | : buffer_(buffer), length_(length), owns_buffer_(owns_buffer) {} |
| 277 | |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 278 | QuicData::QuicData(absl::string_view packet_data) |
dschinazi | 4b5a68a | 2019-08-15 15:45:36 -0700 | [diff] [blame] | 279 | : buffer_(packet_data.data()), |
| 280 | length_(packet_data.length()), |
| 281 | owns_buffer_(false) {} |
| 282 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 283 | QuicData::~QuicData() { |
| 284 | if (owns_buffer_) { |
| 285 | delete[] const_cast<char*>(buffer_); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | QuicPacket::QuicPacket( |
| 290 | char* buffer, |
| 291 | size_t length, |
| 292 | bool owns_buffer, |
| 293 | QuicConnectionIdLength destination_connection_id_length, |
| 294 | QuicConnectionIdLength source_connection_id_length, |
| 295 | bool includes_version, |
| 296 | bool includes_diversification_nonce, |
| 297 | QuicPacketNumberLength packet_number_length, |
| 298 | QuicVariableLengthIntegerLength retry_token_length_length, |
| 299 | QuicByteCount retry_token_length, |
| 300 | QuicVariableLengthIntegerLength length_length) |
| 301 | : QuicData(buffer, length, owns_buffer), |
| 302 | buffer_(buffer), |
| 303 | destination_connection_id_length_(destination_connection_id_length), |
| 304 | source_connection_id_length_(source_connection_id_length), |
| 305 | includes_version_(includes_version), |
| 306 | includes_diversification_nonce_(includes_diversification_nonce), |
| 307 | packet_number_length_(packet_number_length), |
| 308 | retry_token_length_length_(retry_token_length_length), |
| 309 | retry_token_length_(retry_token_length), |
| 310 | length_length_(length_length) {} |
| 311 | |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 312 | QuicPacket::QuicPacket(QuicTransportVersion /*version*/, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 313 | char* buffer, |
| 314 | size_t length, |
| 315 | bool owns_buffer, |
| 316 | const QuicPacketHeader& header) |
| 317 | : QuicPacket(buffer, |
| 318 | length, |
| 319 | owns_buffer, |
| 320 | GetIncludedDestinationConnectionIdLength(header), |
| 321 | GetIncludedSourceConnectionIdLength(header), |
| 322 | header.version_flag, |
| 323 | header.nonce != nullptr, |
| 324 | header.packet_number_length, |
| 325 | header.retry_token_length_length, |
| 326 | header.retry_token.length(), |
| 327 | header.length_length) {} |
| 328 | |
| 329 | QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer, size_t length) |
| 330 | : QuicData(buffer, length) {} |
| 331 | |
| 332 | QuicEncryptedPacket::QuicEncryptedPacket(const char* buffer, |
| 333 | size_t length, |
| 334 | bool owns_buffer) |
| 335 | : QuicData(buffer, length, owns_buffer) {} |
| 336 | |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 337 | QuicEncryptedPacket::QuicEncryptedPacket(absl::string_view data) |
dschinazi | 4b5a68a | 2019-08-15 15:45:36 -0700 | [diff] [blame] | 338 | : QuicData(data) {} |
| 339 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 340 | std::unique_ptr<QuicEncryptedPacket> QuicEncryptedPacket::Clone() const { |
| 341 | char* buffer = new char[this->length()]; |
| 342 | memcpy(buffer, this->data(), this->length()); |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 343 | return std::make_unique<QuicEncryptedPacket>(buffer, this->length(), true); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | std::ostream& operator<<(std::ostream& os, const QuicEncryptedPacket& s) { |
| 347 | os << s.length() << "-byte data"; |
| 348 | return os; |
| 349 | } |
| 350 | |
| 351 | QuicReceivedPacket::QuicReceivedPacket(const char* buffer, |
| 352 | size_t length, |
| 353 | QuicTime receipt_time) |
| 354 | : QuicReceivedPacket(buffer, |
| 355 | length, |
| 356 | receipt_time, |
| 357 | false /* owns_buffer */) {} |
| 358 | |
| 359 | QuicReceivedPacket::QuicReceivedPacket(const char* buffer, |
| 360 | size_t length, |
| 361 | QuicTime receipt_time, |
| 362 | bool owns_buffer) |
| 363 | : QuicReceivedPacket(buffer, |
| 364 | length, |
| 365 | receipt_time, |
| 366 | owns_buffer, |
| 367 | 0 /* ttl */, |
| 368 | true /* ttl_valid */) {} |
| 369 | |
| 370 | QuicReceivedPacket::QuicReceivedPacket(const char* buffer, |
| 371 | size_t length, |
| 372 | QuicTime receipt_time, |
| 373 | bool owns_buffer, |
| 374 | int ttl, |
| 375 | bool ttl_valid) |
| 376 | : quic::QuicReceivedPacket(buffer, |
| 377 | length, |
| 378 | receipt_time, |
| 379 | owns_buffer, |
| 380 | ttl, |
| 381 | ttl_valid, |
| 382 | nullptr /* packet_headers */, |
| 383 | 0 /* headers_length */, |
| 384 | false /* owns_header_buffer */) {} |
| 385 | |
| 386 | QuicReceivedPacket::QuicReceivedPacket(const char* buffer, |
| 387 | size_t length, |
| 388 | QuicTime receipt_time, |
| 389 | bool owns_buffer, |
| 390 | int ttl, |
| 391 | bool ttl_valid, |
| 392 | char* packet_headers, |
| 393 | size_t headers_length, |
| 394 | bool owns_header_buffer) |
| 395 | : QuicEncryptedPacket(buffer, length, owns_buffer), |
| 396 | receipt_time_(receipt_time), |
| 397 | ttl_(ttl_valid ? ttl : -1), |
| 398 | packet_headers_(packet_headers), |
| 399 | headers_length_(headers_length), |
| 400 | owns_header_buffer_(owns_header_buffer) {} |
| 401 | |
| 402 | QuicReceivedPacket::~QuicReceivedPacket() { |
| 403 | if (owns_header_buffer_) { |
| 404 | delete[] static_cast<char*>(packet_headers_); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | std::unique_ptr<QuicReceivedPacket> QuicReceivedPacket::Clone() const { |
| 409 | char* buffer = new char[this->length()]; |
| 410 | memcpy(buffer, this->data(), this->length()); |
| 411 | if (this->packet_headers()) { |
| 412 | char* headers_buffer = new char[this->headers_length()]; |
| 413 | memcpy(headers_buffer, this->packet_headers(), this->headers_length()); |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 414 | return std::make_unique<QuicReceivedPacket>( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 415 | buffer, this->length(), receipt_time(), true, ttl(), ttl() >= 0, |
| 416 | headers_buffer, this->headers_length(), true); |
| 417 | } |
| 418 | |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 419 | return std::make_unique<QuicReceivedPacket>( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 420 | buffer, this->length(), receipt_time(), true, ttl(), ttl() >= 0); |
| 421 | } |
| 422 | |
| 423 | std::ostream& operator<<(std::ostream& os, const QuicReceivedPacket& s) { |
| 424 | os << s.length() << "-byte data"; |
| 425 | return os; |
| 426 | } |
| 427 | |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 428 | absl::string_view QuicPacket::AssociatedData( |
dmcardle | cf0bfcf | 2019-12-13 08:08:21 -0800 | [diff] [blame] | 429 | QuicTransportVersion version) const { |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 430 | return absl::string_view( |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 431 | data(), |
| 432 | GetStartOfEncryptedData(version, destination_connection_id_length_, |
| 433 | source_connection_id_length_, includes_version_, |
| 434 | includes_diversification_nonce_, |
| 435 | packet_number_length_, retry_token_length_length_, |
| 436 | retry_token_length_, length_length_)); |
| 437 | } |
| 438 | |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 439 | absl::string_view QuicPacket::Plaintext(QuicTransportVersion version) const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 440 | const size_t start_of_encrypted_data = GetStartOfEncryptedData( |
| 441 | version, destination_connection_id_length_, source_connection_id_length_, |
| 442 | includes_version_, includes_diversification_nonce_, packet_number_length_, |
| 443 | retry_token_length_length_, retry_token_length_, length_length_); |
vasilvv | c872ee4 | 2020-10-07 19:50:22 -0700 | [diff] [blame] | 444 | return absl::string_view(data() + start_of_encrypted_data, |
| 445 | length() - start_of_encrypted_data); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | SerializedPacket::SerializedPacket(QuicPacketNumber packet_number, |
| 449 | QuicPacketNumberLength packet_number_length, |
| 450 | const char* encrypted_buffer, |
| 451 | QuicPacketLength encrypted_length, |
| 452 | bool has_ack, |
| 453 | bool has_stop_waiting) |
| 454 | : encrypted_buffer(encrypted_buffer), |
| 455 | encrypted_length(encrypted_length), |
| 456 | has_crypto_handshake(NOT_HANDSHAKE), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 457 | packet_number(packet_number), |
| 458 | packet_number_length(packet_number_length), |
QUICHE team | 6987b4a | 2019-03-15 16:23:04 -0700 | [diff] [blame] | 459 | encryption_level(ENCRYPTION_INITIAL), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 460 | has_ack(has_ack), |
| 461 | has_stop_waiting(has_stop_waiting), |
fayang | fb5fb61 | 2019-10-21 13:19:14 -0700 | [diff] [blame] | 462 | transmission_type(NOT_RETRANSMISSION), |
fayang | 1504296 | 2020-07-01 12:14:29 -0700 | [diff] [blame] | 463 | has_ack_frame_copy(false), |
haoyuewang | 5b0f14f | 2020-09-18 14:31:54 -0700 | [diff] [blame] | 464 | has_ack_frequency(false), |
haoyuewang | 11a54a1 | 2020-09-21 14:17:29 -0700 | [diff] [blame] | 465 | has_message(false), |
fayang | 1504296 | 2020-07-01 12:14:29 -0700 | [diff] [blame] | 466 | fate(SEND_TO_WRITER) {} |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 467 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 468 | SerializedPacket::SerializedPacket(SerializedPacket&& other) |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 469 | : has_crypto_handshake(other.has_crypto_handshake), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 470 | packet_number(other.packet_number), |
| 471 | packet_number_length(other.packet_number_length), |
| 472 | encryption_level(other.encryption_level), |
| 473 | has_ack(other.has_ack), |
| 474 | has_stop_waiting(other.has_stop_waiting), |
| 475 | transmission_type(other.transmission_type), |
fayang | fb5fb61 | 2019-10-21 13:19:14 -0700 | [diff] [blame] | 476 | largest_acked(other.largest_acked), |
fayang | 1504296 | 2020-07-01 12:14:29 -0700 | [diff] [blame] | 477 | has_ack_frame_copy(other.has_ack_frame_copy), |
haoyuewang | 5b0f14f | 2020-09-18 14:31:54 -0700 | [diff] [blame] | 478 | has_ack_frequency(other.has_ack_frequency), |
haoyuewang | 11a54a1 | 2020-09-21 14:17:29 -0700 | [diff] [blame] | 479 | has_message(other.has_message), |
danzh | 051bf77 | 2020-08-24 12:30:36 -0700 | [diff] [blame] | 480 | fate(other.fate), |
| 481 | peer_address(other.peer_address) { |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 482 | if (this != &other) { |
| 483 | if (release_encrypted_buffer && encrypted_buffer != nullptr) { |
| 484 | release_encrypted_buffer(encrypted_buffer); |
| 485 | } |
| 486 | encrypted_buffer = other.encrypted_buffer; |
| 487 | encrypted_length = other.encrypted_length; |
| 488 | release_encrypted_buffer = std::move(other.release_encrypted_buffer); |
| 489 | other.release_encrypted_buffer = nullptr; |
| 490 | |
| 491 | retransmittable_frames.swap(other.retransmittable_frames); |
| 492 | nonretransmittable_frames.swap(other.nonretransmittable_frames); |
| 493 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 494 | } |
| 495 | |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 496 | SerializedPacket::~SerializedPacket() { |
| 497 | if (release_encrypted_buffer && encrypted_buffer != nullptr) { |
| 498 | release_encrypted_buffer(encrypted_buffer); |
| 499 | } |
| 500 | |
| 501 | if (!retransmittable_frames.empty()) { |
| 502 | DeleteFrames(&retransmittable_frames); |
| 503 | } |
| 504 | for (auto& frame : nonretransmittable_frames) { |
| 505 | if (!has_ack_frame_copy && frame.type == ACK_FRAME) { |
| 506 | // Do not delete ack frame if the packet does not own a copy of it. |
| 507 | continue; |
| 508 | } |
| 509 | DeleteFrame(&frame); |
| 510 | } |
| 511 | } |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 512 | |
fayang | fb5fb61 | 2019-10-21 13:19:14 -0700 | [diff] [blame] | 513 | SerializedPacket* CopySerializedPacket(const SerializedPacket& serialized, |
| 514 | QuicBufferAllocator* allocator, |
| 515 | bool copy_buffer) { |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 516 | SerializedPacket* copy = new SerializedPacket( |
| 517 | serialized.packet_number, serialized.packet_number_length, |
| 518 | serialized.encrypted_buffer, serialized.encrypted_length, |
| 519 | serialized.has_ack, serialized.has_stop_waiting); |
| 520 | copy->has_crypto_handshake = serialized.has_crypto_handshake; |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 521 | copy->encryption_level = serialized.encryption_level; |
| 522 | copy->transmission_type = serialized.transmission_type; |
| 523 | copy->largest_acked = serialized.largest_acked; |
haoyuewang | 5b0f14f | 2020-09-18 14:31:54 -0700 | [diff] [blame] | 524 | copy->has_ack_frequency = serialized.has_ack_frequency; |
haoyuewang | 11a54a1 | 2020-09-21 14:17:29 -0700 | [diff] [blame] | 525 | copy->has_message = serialized.has_message; |
fayang | 1504296 | 2020-07-01 12:14:29 -0700 | [diff] [blame] | 526 | copy->fate = serialized.fate; |
danzh | 051bf77 | 2020-08-24 12:30:36 -0700 | [diff] [blame] | 527 | copy->peer_address = serialized.peer_address; |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 528 | |
fayang | fb5fb61 | 2019-10-21 13:19:14 -0700 | [diff] [blame] | 529 | if (copy_buffer) { |
| 530 | copy->encrypted_buffer = CopyBuffer(serialized); |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 531 | copy->release_encrypted_buffer = [](const char* p) { delete[] p; }; |
fayang | fb5fb61 | 2019-10-21 13:19:14 -0700 | [diff] [blame] | 532 | } |
| 533 | // Copy underlying frames. |
| 534 | copy->retransmittable_frames = |
| 535 | CopyQuicFrames(allocator, serialized.retransmittable_frames); |
vasilvv | f803516 | 2021-02-01 14:49:14 -0800 | [diff] [blame] | 536 | QUICHE_DCHECK(copy->nonretransmittable_frames.empty()); |
fayang | fb5fb61 | 2019-10-21 13:19:14 -0700 | [diff] [blame] | 537 | for (const auto& frame : serialized.nonretransmittable_frames) { |
| 538 | if (frame.type == ACK_FRAME) { |
| 539 | copy->has_ack_frame_copy = true; |
| 540 | } |
| 541 | copy->nonretransmittable_frames.push_back(CopyQuicFrame(allocator, frame)); |
| 542 | } |
| 543 | return copy; |
| 544 | } |
| 545 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 546 | char* CopyBuffer(const SerializedPacket& packet) { |
wub | 8a5dafa | 2020-05-13 12:30:17 -0700 | [diff] [blame] | 547 | return CopyBuffer(packet.encrypted_buffer, packet.encrypted_length); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 548 | } |
| 549 | |
fayang | 58f7107 | 2019-11-05 08:47:02 -0800 | [diff] [blame] | 550 | char* CopyBuffer(const char* encrypted_buffer, |
| 551 | QuicPacketLength encrypted_length) { |
| 552 | char* dst_buffer = new char[encrypted_length]; |
| 553 | memcpy(dst_buffer, encrypted_buffer, encrypted_length); |
| 554 | return dst_buffer; |
| 555 | } |
| 556 | |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 557 | ReceivedPacketInfo::ReceivedPacketInfo(const QuicSocketAddress& self_address, |
| 558 | const QuicSocketAddress& peer_address, |
| 559 | const QuicReceivedPacket& packet) |
| 560 | : self_address(self_address), |
| 561 | peer_address(peer_address), |
| 562 | packet(packet), |
| 563 | form(GOOGLE_QUIC_PACKET), |
fayang | e3f2f7b | 2019-09-19 17:01:57 -0700 | [diff] [blame] | 564 | long_packet_type(INVALID_PACKET_TYPE), |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 565 | version_flag(false), |
dschinazi | 48ac919 | 2019-07-31 00:07:26 -0700 | [diff] [blame] | 566 | use_length_prefix(false), |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 567 | version_label(0), |
dschinazi | 5c1d7d8 | 2020-07-29 16:42:50 -0700 | [diff] [blame] | 568 | version(ParsedQuicVersion::Unsupported()), |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 569 | destination_connection_id(EmptyQuicConnectionId()), |
| 570 | source_connection_id(EmptyQuicConnectionId()) {} |
| 571 | |
| 572 | ReceivedPacketInfo::~ReceivedPacketInfo() {} |
| 573 | |
| 574 | std::string ReceivedPacketInfo::ToString() const { |
vasilvv | 9edb31e | 2020-12-03 19:32:58 -0800 | [diff] [blame] | 575 | std::string output = |
| 576 | absl::StrCat("{ self_address: ", self_address.ToString(), |
| 577 | ", peer_address: ", peer_address.ToString(), |
| 578 | ", packet_length: ", packet.length(), |
| 579 | ", header_format: ", form, ", version_flag: ", version_flag); |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 580 | if (version_flag) { |
vasilvv | 85577e0 | 2020-12-09 15:35:30 -0800 | [diff] [blame] | 581 | absl::StrAppend(&output, ", version: ", ParsedQuicVersionToString(version)); |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 582 | } |
vasilvv | 85577e0 | 2020-12-09 15:35:30 -0800 | [diff] [blame] | 583 | absl::StrAppend( |
fayang | 1ed1f76 | 2019-06-24 11:40:04 -0700 | [diff] [blame] | 584 | &output, |
| 585 | ", destination_connection_id: ", destination_connection_id.ToString(), |
| 586 | ", source_connection_id: ", source_connection_id.ToString(), " }\n"); |
| 587 | return output; |
| 588 | } |
| 589 | |
| 590 | std::ostream& operator<<(std::ostream& os, |
| 591 | const ReceivedPacketInfo& packet_info) { |
| 592 | os << packet_info.ToString(); |
| 593 | return os; |
| 594 | } |
| 595 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 596 | } // namespace quic |