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 // The following two constants are defined as part of the hash algorithm.
46 // see http://www.isthe.com/chongo/tech/comp/fnv/
47 // 309485009821345068724781371
48 const uint128
kPrime(16777216, 315);
49 // 144066263297769815596495629667062367629
50 const uint128
kOffset(GG_UINT64_C(7809847782465536322),
51 GG_UINT64_C(7113472399480571277));
53 const uint8
* octets
= reinterpret_cast<const uint8
*>(data
);
55 uint128 hash
= kOffset
;
57 for (int i
= 0; i
< len
; ++i
) {
58 hash
= hash
^ uint128(0, octets
[i
]);
66 bool QuicUtils::FindMutualTag(const QuicTagVector
& our_tags_vector
,
67 const QuicTag
* their_tags
,
68 size_t num_their_tags
,
72 if (our_tags_vector
.empty()) {
75 const size_t num_our_tags
= our_tags_vector
.size();
76 const QuicTag
* our_tags
= &our_tags_vector
[0];
78 size_t num_priority_tags
, num_inferior_tags
;
79 const QuicTag
* priority_tags
;
80 const QuicTag
* inferior_tags
;
81 if (priority
== LOCAL_PRIORITY
) {
82 num_priority_tags
= num_our_tags
;
83 priority_tags
= our_tags
;
84 num_inferior_tags
= num_their_tags
;
85 inferior_tags
= their_tags
;
87 num_priority_tags
= num_their_tags
;
88 priority_tags
= their_tags
;
89 num_inferior_tags
= num_our_tags
;
90 inferior_tags
= our_tags
;
93 for (size_t i
= 0; i
< num_priority_tags
; i
++) {
94 for (size_t j
= 0; j
< num_inferior_tags
; j
++) {
95 if (priority_tags
[i
] == inferior_tags
[j
]) {
96 *out_result
= priority_tags
[i
];
98 if (priority
== LOCAL_PRIORITY
) {
113 void QuicUtils::SerializeUint128(uint128 v
, uint8
* out
) {
114 const uint64 lo
= Uint128Low64(v
);
115 const uint64 hi
= Uint128High64(v
);
116 // This assumes that the system is little-endian.
117 memcpy(out
, &lo
, sizeof(lo
));
118 memcpy(out
+ sizeof(lo
), &hi
, sizeof(hi
));
122 void QuicUtils::SerializeUint128Short(uint128 v
, uint8
* out
) {
123 const uint64 lo
= Uint128Low64(v
);
124 const uint64 hi
= Uint128High64(v
);
125 // This assumes that the system is little-endian.
126 memcpy(out
, &lo
, sizeof(lo
));
127 memcpy(out
+ sizeof(lo
), &hi
, sizeof(hi
) / 2);
130 #define RETURN_STRING_LITERAL(x) \
135 const char* QuicUtils::StreamErrorToString(QuicRstStreamErrorCode error
) {
137 RETURN_STRING_LITERAL(QUIC_STREAM_NO_ERROR
);
138 RETURN_STRING_LITERAL(QUIC_STREAM_CONNECTION_ERROR
);
139 RETURN_STRING_LITERAL(QUIC_ERROR_PROCESSING_STREAM
);
140 RETURN_STRING_LITERAL(QUIC_MULTIPLE_TERMINATION_OFFSETS
);
141 RETURN_STRING_LITERAL(QUIC_BAD_APPLICATION_PAYLOAD
);
142 RETURN_STRING_LITERAL(QUIC_STREAM_PEER_GOING_AWAY
);
143 RETURN_STRING_LITERAL(QUIC_STREAM_CANCELLED
);
144 RETURN_STRING_LITERAL(QUIC_RST_ACKNOWLEDGEMENT
);
145 RETURN_STRING_LITERAL(QUIC_STREAM_LAST_ERROR
);
147 // Return a default value so that we return this when |error| doesn't match
148 // any of the QuicRstStreamErrorCodes. This can happen when the RstStream
149 // frame sent by the peer (attacker) has invalid error code.
150 return "INVALID_RST_STREAM_ERROR_CODE";
154 const char* QuicUtils::ErrorToString(QuicErrorCode error
) {
156 RETURN_STRING_LITERAL(QUIC_NO_ERROR
);
157 RETURN_STRING_LITERAL(QUIC_INTERNAL_ERROR
);
158 RETURN_STRING_LITERAL(QUIC_STREAM_DATA_AFTER_TERMINATION
);
159 RETURN_STRING_LITERAL(QUIC_INVALID_PACKET_HEADER
);
160 RETURN_STRING_LITERAL(QUIC_INVALID_FRAME_DATA
);
161 RETURN_STRING_LITERAL(QUIC_MISSING_PAYLOAD
);
162 RETURN_STRING_LITERAL(QUIC_INVALID_FEC_DATA
);
163 RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_DATA
);
164 RETURN_STRING_LITERAL(QUIC_UNENCRYPTED_STREAM_DATA
);
165 RETURN_STRING_LITERAL(QUIC_INVALID_RST_STREAM_DATA
);
166 RETURN_STRING_LITERAL(QUIC_INVALID_CONNECTION_CLOSE_DATA
);
167 RETURN_STRING_LITERAL(QUIC_INVALID_GOAWAY_DATA
);
168 RETURN_STRING_LITERAL(QUIC_INVALID_WINDOW_UPDATE_DATA
);
169 RETURN_STRING_LITERAL(QUIC_INVALID_BLOCKED_DATA
);
170 RETURN_STRING_LITERAL(QUIC_INVALID_STOP_WAITING_DATA
);
171 RETURN_STRING_LITERAL(QUIC_INVALID_ACK_DATA
);
172 RETURN_STRING_LITERAL(QUIC_INVALID_CONGESTION_FEEDBACK_DATA
);
173 RETURN_STRING_LITERAL(QUIC_INVALID_VERSION_NEGOTIATION_PACKET
);
174 RETURN_STRING_LITERAL(QUIC_INVALID_PUBLIC_RST_PACKET
);
175 RETURN_STRING_LITERAL(QUIC_DECRYPTION_FAILURE
);
176 RETURN_STRING_LITERAL(QUIC_ENCRYPTION_FAILURE
);
177 RETURN_STRING_LITERAL(QUIC_PACKET_TOO_LARGE
);
178 RETURN_STRING_LITERAL(QUIC_PACKET_FOR_NONEXISTENT_STREAM
);
179 RETURN_STRING_LITERAL(QUIC_PEER_GOING_AWAY
);
180 RETURN_STRING_LITERAL(QUIC_HANDSHAKE_FAILED
);
181 RETURN_STRING_LITERAL(QUIC_CRYPTO_TAGS_OUT_OF_ORDER
);
182 RETURN_STRING_LITERAL(QUIC_CRYPTO_TOO_MANY_ENTRIES
);
183 RETURN_STRING_LITERAL(QUIC_CRYPTO_TOO_MANY_REJECTS
);
184 RETURN_STRING_LITERAL(QUIC_CRYPTO_INVALID_VALUE_LENGTH
)
185 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE
);
186 RETURN_STRING_LITERAL(QUIC_CRYPTO_INTERNAL_ERROR
);
187 RETURN_STRING_LITERAL(QUIC_CRYPTO_VERSION_NOT_SUPPORTED
);
188 RETURN_STRING_LITERAL(QUIC_CRYPTO_NO_SUPPORT
);
189 RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_TYPE
);
190 RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER
);
191 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND
);
192 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP
);
193 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND
);
194 RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_ID
);
195 RETURN_STRING_LITERAL(QUIC_INVALID_PRIORITY
);
196 RETURN_STRING_LITERAL(QUIC_TOO_MANY_OPEN_STREAMS
);
197 RETURN_STRING_LITERAL(QUIC_TOO_MANY_UNFINISHED_STREAMS
);
198 RETURN_STRING_LITERAL(QUIC_PUBLIC_RESET
);
199 RETURN_STRING_LITERAL(QUIC_INVALID_VERSION
);
200 RETURN_STRING_LITERAL(QUIC_INVALID_HEADER_ID
);
201 RETURN_STRING_LITERAL(QUIC_INVALID_NEGOTIATED_VALUE
);
202 RETURN_STRING_LITERAL(QUIC_DECOMPRESSION_FAILURE
);
203 RETURN_STRING_LITERAL(QUIC_CONNECTION_TIMED_OUT
);
204 RETURN_STRING_LITERAL(QUIC_CONNECTION_OVERALL_TIMED_OUT
);
205 RETURN_STRING_LITERAL(QUIC_ERROR_MIGRATING_ADDRESS
);
206 RETURN_STRING_LITERAL(QUIC_PACKET_WRITE_ERROR
);
207 RETURN_STRING_LITERAL(QUIC_PACKET_READ_ERROR
);
208 RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_FRAME
);
209 RETURN_STRING_LITERAL(QUIC_INVALID_HEADERS_STREAM_DATA
);
210 RETURN_STRING_LITERAL(QUIC_FLOW_CONTROL_RECEIVED_TOO_MUCH_DATA
);
211 RETURN_STRING_LITERAL(QUIC_FLOW_CONTROL_SENT_TOO_MUCH_DATA
);
212 RETURN_STRING_LITERAL(QUIC_FLOW_CONTROL_INVALID_WINDOW
);
213 RETURN_STRING_LITERAL(QUIC_CONNECTION_IP_POOLED
);
214 RETURN_STRING_LITERAL(QUIC_PROOF_INVALID
);
215 RETURN_STRING_LITERAL(QUIC_CRYPTO_DUPLICATE_TAG
);
216 RETURN_STRING_LITERAL(QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT
);
217 RETURN_STRING_LITERAL(QUIC_CRYPTO_SERVER_CONFIG_EXPIRED
);
218 RETURN_STRING_LITERAL(QUIC_INVALID_CHANNEL_ID_SIGNATURE
);
219 RETURN_STRING_LITERAL(QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED
);
220 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_WHILE_VALIDATING_CLIENT_HELLO
);
221 RETURN_STRING_LITERAL(QUIC_CRYPTO_UPDATE_BEFORE_HANDSHAKE_COMPLETE
);
222 RETURN_STRING_LITERAL(QUIC_VERSION_NEGOTIATION_MISMATCH
);
223 RETURN_STRING_LITERAL(QUIC_TOO_MANY_OUTSTANDING_SENT_PACKETS
);
224 RETURN_STRING_LITERAL(QUIC_TOO_MANY_OUTSTANDING_RECEIVED_PACKETS
);
225 RETURN_STRING_LITERAL(QUIC_LAST_ERROR
);
226 // Intentionally have no default case, so we'll break the build
227 // if we add errors and don't put them here.
229 // Return a default value so that we return this when |error| doesn't match
230 // any of the QuicErrorCodes. This can happen when the ConnectionClose
231 // frame sent by the peer (attacker) has invalid error code.
232 return "INVALID_ERROR_CODE";
236 const char* QuicUtils::EncryptionLevelToString(EncryptionLevel level
) {
238 RETURN_STRING_LITERAL(ENCRYPTION_NONE
);
239 RETURN_STRING_LITERAL(ENCRYPTION_INITIAL
);
240 RETURN_STRING_LITERAL(ENCRYPTION_FORWARD_SECURE
);
241 RETURN_STRING_LITERAL(NUM_ENCRYPTION_LEVELS
);
243 return "INVALID_ENCRYPTION_LEVEL";
247 const char* QuicUtils::TransmissionTypeToString(TransmissionType type
) {
249 RETURN_STRING_LITERAL(NOT_RETRANSMISSION
);
250 RETURN_STRING_LITERAL(HANDSHAKE_RETRANSMISSION
);
251 RETURN_STRING_LITERAL(LOSS_RETRANSMISSION
);
252 RETURN_STRING_LITERAL(ALL_UNACKED_RETRANSMISSION
);
253 RETURN_STRING_LITERAL(ALL_INITIAL_RETRANSMISSION
);
254 RETURN_STRING_LITERAL(RTO_RETRANSMISSION
);
255 RETURN_STRING_LITERAL(TLP_RETRANSMISSION
);
257 return "INVALID_TRANSMISSION_TYPE";
261 string
QuicUtils::TagToString(QuicTag tag
) {
264 const QuicTag orig_tag
= tag
;
266 for (size_t i
= 0; i
< sizeof(chars
); i
++) {
268 if ((chars
[i
] == 0 || chars
[i
] == '\xff') && i
== 3) {
271 if (!isprint(static_cast<unsigned char>(chars
[i
]))) {
279 return string(chars
, sizeof(chars
));
282 return base::UintToString(orig_tag
);
286 QuicTagVector
QuicUtils::ParseQuicConnectionOptions(
287 const std::string
& connection_options
) {
288 QuicTagVector options
;
289 std::vector
<std::string
> tokens
;
290 base::SplitString(connection_options
, ',', &tokens
);
291 // Tokens are expected to be no more than 4 characters long, but we
292 // handle overflow gracefully.
293 for (const std::string
& token
: tokens
) {
295 for (char token_char
: base::Reversed(token
)) {
297 option
|= static_cast<unsigned char>(token_char
);
299 options
.push_back(option
);
305 string
QuicUtils::StringToHexASCIIDump(StringPiece in_buffer
) {
307 const int kBytesPerLine
= 16; // Max bytes dumped per line
308 const char* buf
= in_buffer
.data();
309 int bytes_remaining
= in_buffer
.size();
310 string s
; // our output
312 while (bytes_remaining
> 0) {
313 const int line_bytes
= std::min(bytes_remaining
, kBytesPerLine
);
314 base::StringAppendF(&s
, "0x%04x: ", offset
); // Do the line header
315 for (int i
= 0; i
< kBytesPerLine
; ++i
) {
316 if (i
< line_bytes
) {
317 base::StringAppendF(&s
, "%02x", static_cast<unsigned char>(p
[i
]));
319 s
+= " "; // two-space filler instead of two-space hex digits
324 for (int i
= 0; i
< line_bytes
; ++i
) { // Do the ASCII dump
325 s
+= (p
[i
] > 32 && p
[i
] < 127) ? p
[i
] : '.';
328 bytes_remaining
-= line_bytes
;
329 offset
+= line_bytes
;
337 QuicPriority
QuicUtils::LowestPriority() {
338 return QuicWriteBlockedList::kLowestPriority
;
342 QuicPriority
QuicUtils::HighestPriority() {
343 return QuicWriteBlockedList::kHighestPriority
;