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.
5 #include "net/quic/quic_utils.h"
12 #include "base/basictypes.h"
13 #include "base/containers/adapters.h"
14 #include "base/logging.h"
15 #include "base/port.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_split.h"
19 #include "net/quic/quic_write_blocked_list.h"
21 using base::StringPiece
;
27 uint64
QuicUtils::FNV1a_64_Hash(const char* data
, int len
) {
28 static const uint64 kOffset
= GG_UINT64_C(14695981039346656037);
29 static const uint64 kPrime
= GG_UINT64_C(1099511628211);
31 const uint8
* octets
= reinterpret_cast<const uint8
*>(data
);
33 uint64 hash
= kOffset
;
35 for (int i
= 0; i
< len
; ++i
) {
36 hash
= hash
^ octets
[i
];
44 uint128
QuicUtils::FNV1a_128_Hash(const char* data
, int len
) {
45 return FNV1a_128_Hash_Two(data
, len
, nullptr, 0);
49 uint128
QuicUtils::FNV1a_128_Hash_Two(const char* data1
,
53 // The two constants are defined as part of the hash algorithm.
54 // see http://www.isthe.com/chongo/tech/comp/fnv/
55 // 144066263297769815596495629667062367629
56 const uint128
kOffset(GG_UINT64_C(7809847782465536322),
57 GG_UINT64_C(7113472399480571277));
59 uint128 hash
= IncrementalHash(kOffset
, data1
, len1
);
60 if (data2
== nullptr) {
63 return IncrementalHash(hash
, data2
, len2
);
67 uint128
QuicUtils::IncrementalHash(uint128 hash
, const char* data
, size_t len
) {
68 // 309485009821345068724781371
69 const uint128
kPrime(16777216, 315);
70 const uint8
* octets
= reinterpret_cast<const uint8
*>(data
);
71 for (size_t i
= 0; i
< len
; ++i
) {
72 hash
= hash
^ uint128(0, octets
[i
]);
79 bool QuicUtils::FindMutualTag(const QuicTagVector
& our_tags_vector
,
80 const QuicTag
* their_tags
,
81 size_t num_their_tags
,
85 if (our_tags_vector
.empty()) {
88 const size_t num_our_tags
= our_tags_vector
.size();
89 const QuicTag
* our_tags
= &our_tags_vector
[0];
91 size_t num_priority_tags
, num_inferior_tags
;
92 const QuicTag
* priority_tags
;
93 const QuicTag
* inferior_tags
;
94 if (priority
== LOCAL_PRIORITY
) {
95 num_priority_tags
= num_our_tags
;
96 priority_tags
= our_tags
;
97 num_inferior_tags
= num_their_tags
;
98 inferior_tags
= their_tags
;
100 num_priority_tags
= num_their_tags
;
101 priority_tags
= their_tags
;
102 num_inferior_tags
= num_our_tags
;
103 inferior_tags
= our_tags
;
106 for (size_t i
= 0; i
< num_priority_tags
; i
++) {
107 for (size_t j
= 0; j
< num_inferior_tags
; j
++) {
108 if (priority_tags
[i
] == inferior_tags
[j
]) {
109 *out_result
= priority_tags
[i
];
111 if (priority
== LOCAL_PRIORITY
) {
126 void QuicUtils::SerializeUint128Short(uint128 v
, uint8
* out
) {
127 const uint64 lo
= Uint128Low64(v
);
128 const uint64 hi
= Uint128High64(v
);
129 // This assumes that the system is little-endian.
130 memcpy(out
, &lo
, sizeof(lo
));
131 memcpy(out
+ sizeof(lo
), &hi
, sizeof(hi
) / 2);
134 #define RETURN_STRING_LITERAL(x) \
139 const char* QuicUtils::StreamErrorToString(QuicRstStreamErrorCode error
) {
141 RETURN_STRING_LITERAL(QUIC_STREAM_NO_ERROR
);
142 RETURN_STRING_LITERAL(QUIC_STREAM_CONNECTION_ERROR
);
143 RETURN_STRING_LITERAL(QUIC_ERROR_PROCESSING_STREAM
);
144 RETURN_STRING_LITERAL(QUIC_MULTIPLE_TERMINATION_OFFSETS
);
145 RETURN_STRING_LITERAL(QUIC_BAD_APPLICATION_PAYLOAD
);
146 RETURN_STRING_LITERAL(QUIC_STREAM_PEER_GOING_AWAY
);
147 RETURN_STRING_LITERAL(QUIC_STREAM_CANCELLED
);
148 RETURN_STRING_LITERAL(QUIC_RST_ACKNOWLEDGEMENT
);
149 RETURN_STRING_LITERAL(QUIC_STREAM_LAST_ERROR
);
151 // Return a default value so that we return this when |error| doesn't match
152 // any of the QuicRstStreamErrorCodes. This can happen when the RstStream
153 // frame sent by the peer (attacker) has invalid error code.
154 return "INVALID_RST_STREAM_ERROR_CODE";
158 const char* QuicUtils::ErrorToString(QuicErrorCode error
) {
160 RETURN_STRING_LITERAL(QUIC_NO_ERROR
);
161 RETURN_STRING_LITERAL(QUIC_INTERNAL_ERROR
);
162 RETURN_STRING_LITERAL(QUIC_STREAM_DATA_AFTER_TERMINATION
);
163 RETURN_STRING_LITERAL(QUIC_INVALID_PACKET_HEADER
);
164 RETURN_STRING_LITERAL(QUIC_INVALID_FRAME_DATA
);
165 RETURN_STRING_LITERAL(QUIC_MISSING_PAYLOAD
);
166 RETURN_STRING_LITERAL(QUIC_INVALID_FEC_DATA
);
167 RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_DATA
);
168 RETURN_STRING_LITERAL(QUIC_UNENCRYPTED_STREAM_DATA
);
169 RETURN_STRING_LITERAL(QUIC_INVALID_RST_STREAM_DATA
);
170 RETURN_STRING_LITERAL(QUIC_INVALID_CONNECTION_CLOSE_DATA
);
171 RETURN_STRING_LITERAL(QUIC_INVALID_GOAWAY_DATA
);
172 RETURN_STRING_LITERAL(QUIC_INVALID_WINDOW_UPDATE_DATA
);
173 RETURN_STRING_LITERAL(QUIC_INVALID_BLOCKED_DATA
);
174 RETURN_STRING_LITERAL(QUIC_INVALID_STOP_WAITING_DATA
);
175 RETURN_STRING_LITERAL(QUIC_INVALID_ACK_DATA
);
176 RETURN_STRING_LITERAL(QUIC_INVALID_VERSION_NEGOTIATION_PACKET
);
177 RETURN_STRING_LITERAL(QUIC_INVALID_PUBLIC_RST_PACKET
);
178 RETURN_STRING_LITERAL(QUIC_DECRYPTION_FAILURE
);
179 RETURN_STRING_LITERAL(QUIC_ENCRYPTION_FAILURE
);
180 RETURN_STRING_LITERAL(QUIC_PACKET_TOO_LARGE
);
181 RETURN_STRING_LITERAL(QUIC_PACKET_FOR_NONEXISTENT_STREAM
);
182 RETURN_STRING_LITERAL(QUIC_PEER_GOING_AWAY
);
183 RETURN_STRING_LITERAL(QUIC_HANDSHAKE_FAILED
);
184 RETURN_STRING_LITERAL(QUIC_CRYPTO_TAGS_OUT_OF_ORDER
);
185 RETURN_STRING_LITERAL(QUIC_CRYPTO_TOO_MANY_ENTRIES
);
186 RETURN_STRING_LITERAL(QUIC_CRYPTO_TOO_MANY_REJECTS
);
187 RETURN_STRING_LITERAL(QUIC_CRYPTO_INVALID_VALUE_LENGTH
)
188 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE
);
189 RETURN_STRING_LITERAL(QUIC_CRYPTO_INTERNAL_ERROR
);
190 RETURN_STRING_LITERAL(QUIC_CRYPTO_VERSION_NOT_SUPPORTED
);
191 RETURN_STRING_LITERAL(QUIC_CRYPTO_NO_SUPPORT
);
192 RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_TYPE
);
193 RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER
);
194 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND
);
195 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP
);
196 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND
);
197 RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_ID
);
198 RETURN_STRING_LITERAL(QUIC_INVALID_PRIORITY
);
199 RETURN_STRING_LITERAL(QUIC_TOO_MANY_OPEN_STREAMS
);
200 RETURN_STRING_LITERAL(QUIC_TOO_MANY_UNFINISHED_STREAMS
);
201 RETURN_STRING_LITERAL(QUIC_PUBLIC_RESET
);
202 RETURN_STRING_LITERAL(QUIC_INVALID_VERSION
);
203 RETURN_STRING_LITERAL(QUIC_INVALID_HEADER_ID
);
204 RETURN_STRING_LITERAL(QUIC_INVALID_NEGOTIATED_VALUE
);
205 RETURN_STRING_LITERAL(QUIC_DECOMPRESSION_FAILURE
);
206 RETURN_STRING_LITERAL(QUIC_CONNECTION_TIMED_OUT
);
207 RETURN_STRING_LITERAL(QUIC_CONNECTION_OVERALL_TIMED_OUT
);
208 RETURN_STRING_LITERAL(QUIC_ERROR_MIGRATING_ADDRESS
);
209 RETURN_STRING_LITERAL(QUIC_PACKET_WRITE_ERROR
);
210 RETURN_STRING_LITERAL(QUIC_PACKET_READ_ERROR
);
211 RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_FRAME
);
212 RETURN_STRING_LITERAL(QUIC_INVALID_HEADERS_STREAM_DATA
);
213 RETURN_STRING_LITERAL(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA
);
214 RETURN_STRING_LITERAL(QUIC_FLOW_CONTROL_SENT_TOO_MUCH_DATA
);
215 RETURN_STRING_LITERAL(QUIC_FLOW_CONTROL_INVALID_WINDOW
);
216 RETURN_STRING_LITERAL(QUIC_CONNECTION_IP_POOLED
);
217 RETURN_STRING_LITERAL(QUIC_PROOF_INVALID
);
218 RETURN_STRING_LITERAL(QUIC_CRYPTO_DUPLICATE_TAG
);
219 RETURN_STRING_LITERAL(QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT
);
220 RETURN_STRING_LITERAL(QUIC_CRYPTO_SERVER_CONFIG_EXPIRED
);
221 RETURN_STRING_LITERAL(QUIC_INVALID_CHANNEL_ID_SIGNATURE
);
222 RETURN_STRING_LITERAL(QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED
);
223 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_WHILE_VALIDATING_CLIENT_HELLO
);
224 RETURN_STRING_LITERAL(QUIC_CRYPTO_UPDATE_BEFORE_HANDSHAKE_COMPLETE
);
225 RETURN_STRING_LITERAL(QUIC_VERSION_NEGOTIATION_MISMATCH
);
226 RETURN_STRING_LITERAL(QUIC_TOO_MANY_OUTSTANDING_SENT_PACKETS
);
227 RETURN_STRING_LITERAL(QUIC_TOO_MANY_OUTSTANDING_RECEIVED_PACKETS
);
228 RETURN_STRING_LITERAL(QUIC_CONNECTION_CANCELLED
);
229 RETURN_STRING_LITERAL(QUIC_BAD_PACKET_LOSS_RATE
);
230 RETURN_STRING_LITERAL(QUIC_LAST_ERROR
);
231 // Intentionally have no default case, so we'll break the build
232 // if we add errors and don't put them here.
234 // Return a default value so that we return this when |error| doesn't match
235 // any of the QuicErrorCodes. This can happen when the ConnectionClose
236 // frame sent by the peer (attacker) has invalid error code.
237 return "INVALID_ERROR_CODE";
241 const char* QuicUtils::EncryptionLevelToString(EncryptionLevel level
) {
243 RETURN_STRING_LITERAL(ENCRYPTION_NONE
);
244 RETURN_STRING_LITERAL(ENCRYPTION_INITIAL
);
245 RETURN_STRING_LITERAL(ENCRYPTION_FORWARD_SECURE
);
246 RETURN_STRING_LITERAL(NUM_ENCRYPTION_LEVELS
);
248 return "INVALID_ENCRYPTION_LEVEL";
252 const char* QuicUtils::TransmissionTypeToString(TransmissionType type
) {
254 RETURN_STRING_LITERAL(NOT_RETRANSMISSION
);
255 RETURN_STRING_LITERAL(HANDSHAKE_RETRANSMISSION
);
256 RETURN_STRING_LITERAL(LOSS_RETRANSMISSION
);
257 RETURN_STRING_LITERAL(ALL_UNACKED_RETRANSMISSION
);
258 RETURN_STRING_LITERAL(ALL_INITIAL_RETRANSMISSION
);
259 RETURN_STRING_LITERAL(RTO_RETRANSMISSION
);
260 RETURN_STRING_LITERAL(TLP_RETRANSMISSION
);
262 return "INVALID_TRANSMISSION_TYPE";
266 string
QuicUtils::TagToString(QuicTag tag
) {
267 char chars
[sizeof tag
];
269 const QuicTag orig_tag
= tag
;
271 for (size_t i
= 0; i
< arraysize(chars
); i
++) {
272 chars
[i
] = static_cast<char>(tag
);
273 if ((chars
[i
] == 0 || chars
[i
] == '\xff') && i
== arraysize(chars
) - 1) {
276 if (!isprint(static_cast<unsigned char>(chars
[i
]))) {
284 return string(chars
, sizeof(chars
));
287 return base::UintToString(orig_tag
);
291 QuicTagVector
QuicUtils::ParseQuicConnectionOptions(
292 const std::string
& connection_options
) {
293 QuicTagVector options
;
294 std::vector
<std::string
> tokens
;
295 base::SplitString(connection_options
, ',', &tokens
);
296 // Tokens are expected to be no more than 4 characters long, but we
297 // handle overflow gracefully.
298 for (const std::string
& token
: tokens
) {
300 for (char token_char
: base::Reversed(token
)) {
302 option
|= static_cast<unsigned char>(token_char
);
304 options
.push_back(option
);
310 string
QuicUtils::StringToHexASCIIDump(StringPiece in_buffer
) {
312 const int kBytesPerLine
= 16; // Max bytes dumped per line
313 const char* buf
= in_buffer
.data();
314 int bytes_remaining
= in_buffer
.size();
315 string s
; // our output
317 while (bytes_remaining
> 0) {
318 const int line_bytes
= std::min(bytes_remaining
, kBytesPerLine
);
319 base::StringAppendF(&s
, "0x%04x: ", offset
); // Do the line header
320 for (int i
= 0; i
< kBytesPerLine
; ++i
) {
321 if (i
< line_bytes
) {
322 base::StringAppendF(&s
, "%02x", static_cast<unsigned char>(p
[i
]));
324 s
+= " "; // two-space filler instead of two-space hex digits
329 for (int i
= 0; i
< line_bytes
; ++i
) { // Do the ASCII dump
330 s
+= (p
[i
] > 32 && p
[i
] < 127) ? p
[i
] : '.';
333 bytes_remaining
-= line_bytes
;
334 offset
+= line_bytes
;
342 QuicPriority
QuicUtils::HighestPriority() {
343 return QuicWriteBlockedList::kHighestPriority
;