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