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"
11 #include "base/logging.h"
12 #include "base/port.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/string_number_conversions.h"
16 using base::StringPiece
;
22 uint64
QuicUtils::FNV1a_64_Hash(const char* data
, int len
) {
23 static const uint64 kOffset
= GG_UINT64_C(14695981039346656037);
24 static const uint64 kPrime
= GG_UINT64_C(1099511628211);
26 const uint8
* octets
= reinterpret_cast<const uint8
*>(data
);
28 uint64 hash
= kOffset
;
30 for (int i
= 0; i
< len
; ++i
) {
31 hash
= hash
^ octets
[i
];
39 uint128
QuicUtils::FNV1a_128_Hash(const char* data
, int len
) {
40 // The following two constants are defined as part of the hash algorithm.
41 // see http://www.isthe.com/chongo/tech/comp/fnv/
42 // 309485009821345068724781371
43 const uint128
kPrime(16777216, 315);
44 // 144066263297769815596495629667062367629
45 const uint128
kOffset(GG_UINT64_C(7809847782465536322),
46 GG_UINT64_C(7113472399480571277));
48 const uint8
* octets
= reinterpret_cast<const uint8
*>(data
);
50 uint128 hash
= kOffset
;
52 for (int i
= 0; i
< len
; ++i
) {
53 hash
= hash
^ uint128(0, octets
[i
]);
61 bool QuicUtils::FindMutualTag(const QuicTagVector
& our_tags_vector
,
62 const QuicTag
* their_tags
,
63 size_t num_their_tags
,
67 if (our_tags_vector
.empty()) {
70 const size_t num_our_tags
= our_tags_vector
.size();
71 const QuicTag
* our_tags
= &our_tags_vector
[0];
73 size_t num_priority_tags
, num_inferior_tags
;
74 const QuicTag
* priority_tags
;
75 const QuicTag
* inferior_tags
;
76 if (priority
== LOCAL_PRIORITY
) {
77 num_priority_tags
= num_our_tags
;
78 priority_tags
= our_tags
;
79 num_inferior_tags
= num_their_tags
;
80 inferior_tags
= their_tags
;
82 num_priority_tags
= num_their_tags
;
83 priority_tags
= their_tags
;
84 num_inferior_tags
= num_our_tags
;
85 inferior_tags
= our_tags
;
88 for (size_t i
= 0; i
< num_priority_tags
; i
++) {
89 for (size_t j
= 0; j
< num_inferior_tags
; j
++) {
90 if (priority_tags
[i
] == inferior_tags
[j
]) {
91 *out_result
= priority_tags
[i
];
93 if (priority
== LOCAL_PRIORITY
) {
108 void QuicUtils::SerializeUint128(uint128 v
, uint8
* out
) {
109 const uint64 lo
= Uint128Low64(v
);
110 const uint64 hi
= Uint128High64(v
);
111 // This assumes that the system is little-endian.
112 memcpy(out
, &lo
, sizeof(lo
));
113 memcpy(out
+ sizeof(lo
), &hi
, sizeof(hi
));
117 void QuicUtils::SerializeUint128Short(uint128 v
, uint8
* out
) {
118 const uint64 lo
= Uint128Low64(v
);
119 const uint64 hi
= Uint128High64(v
);
120 // This assumes that the system is little-endian.
121 memcpy(out
, &lo
, sizeof(lo
));
122 memcpy(out
+ sizeof(lo
), &hi
, sizeof(hi
) / 2);
125 #define RETURN_STRING_LITERAL(x) \
130 const char* QuicUtils::StreamErrorToString(QuicRstStreamErrorCode error
) {
132 RETURN_STRING_LITERAL(QUIC_STREAM_NO_ERROR
);
133 RETURN_STRING_LITERAL(QUIC_STREAM_CONNECTION_ERROR
);
134 RETURN_STRING_LITERAL(QUIC_ERROR_PROCESSING_STREAM
);
135 RETURN_STRING_LITERAL(QUIC_MULTIPLE_TERMINATION_OFFSETS
);
136 RETURN_STRING_LITERAL(QUIC_BAD_APPLICATION_PAYLOAD
);
137 RETURN_STRING_LITERAL(QUIC_STREAM_PEER_GOING_AWAY
);
138 RETURN_STRING_LITERAL(QUIC_STREAM_CANCELLED
);
139 RETURN_STRING_LITERAL(QUIC_STREAM_LAST_ERROR
);
141 // Return a default value so that we return this when |error| doesn't match
142 // any of the QuicRstStreamErrorCodes. This can happen when the RstStream
143 // frame sent by the peer (attacker) has invalid error code.
144 return "INVALID_RST_STREAM_ERROR_CODE";
148 const char* QuicUtils::ErrorToString(QuicErrorCode error
) {
150 RETURN_STRING_LITERAL(QUIC_NO_ERROR
);
151 RETURN_STRING_LITERAL(QUIC_INTERNAL_ERROR
);
152 RETURN_STRING_LITERAL(QUIC_STREAM_DATA_AFTER_TERMINATION
);
153 RETURN_STRING_LITERAL(QUIC_INVALID_PACKET_HEADER
);
154 RETURN_STRING_LITERAL(QUIC_INVALID_FRAME_DATA
);
155 RETURN_STRING_LITERAL(QUIC_MISSING_PAYLOAD
);
156 RETURN_STRING_LITERAL(QUIC_INVALID_FEC_DATA
);
157 RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_DATA
);
158 RETURN_STRING_LITERAL(QUIC_INVALID_RST_STREAM_DATA
);
159 RETURN_STRING_LITERAL(QUIC_INVALID_CONNECTION_CLOSE_DATA
);
160 RETURN_STRING_LITERAL(QUIC_INVALID_GOAWAY_DATA
);
161 RETURN_STRING_LITERAL(QUIC_INVALID_ACK_DATA
);
162 RETURN_STRING_LITERAL(QUIC_INVALID_CONGESTION_FEEDBACK_DATA
);
163 RETURN_STRING_LITERAL(QUIC_INVALID_VERSION_NEGOTIATION_PACKET
);
164 RETURN_STRING_LITERAL(QUIC_INVALID_PUBLIC_RST_PACKET
);
165 RETURN_STRING_LITERAL(QUIC_DECRYPTION_FAILURE
);
166 RETURN_STRING_LITERAL(QUIC_ENCRYPTION_FAILURE
);
167 RETURN_STRING_LITERAL(QUIC_PACKET_TOO_LARGE
);
168 RETURN_STRING_LITERAL(QUIC_PACKET_FOR_NONEXISTENT_STREAM
);
169 RETURN_STRING_LITERAL(QUIC_PEER_GOING_AWAY
);
170 RETURN_STRING_LITERAL(QUIC_HANDSHAKE_FAILED
);
171 RETURN_STRING_LITERAL(QUIC_CRYPTO_TAGS_OUT_OF_ORDER
);
172 RETURN_STRING_LITERAL(QUIC_CRYPTO_TOO_MANY_ENTRIES
);
173 RETURN_STRING_LITERAL(QUIC_CRYPTO_TOO_MANY_REJECTS
);
174 RETURN_STRING_LITERAL(QUIC_CRYPTO_INVALID_VALUE_LENGTH
)
175 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE
);
176 RETURN_STRING_LITERAL(QUIC_CRYPTO_INTERNAL_ERROR
);
177 RETURN_STRING_LITERAL(QUIC_CRYPTO_VERSION_NOT_SUPPORTED
);
178 RETURN_STRING_LITERAL(QUIC_CRYPTO_NO_SUPPORT
);
179 RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_TYPE
);
180 RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER
);
181 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND
);
182 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP
);
183 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND
);
184 RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_ID
);
185 RETURN_STRING_LITERAL(QUIC_INVALID_PRIORITY
);
186 RETURN_STRING_LITERAL(QUIC_TOO_MANY_OPEN_STREAMS
);
187 RETURN_STRING_LITERAL(QUIC_PUBLIC_RESET
);
188 RETURN_STRING_LITERAL(QUIC_INVALID_VERSION
);
189 RETURN_STRING_LITERAL(QUIC_STREAM_RST_BEFORE_HEADERS_DECOMPRESSED
);
190 RETURN_STRING_LITERAL(QUIC_INVALID_HEADER_ID
);
191 RETURN_STRING_LITERAL(QUIC_INVALID_NEGOTIATED_VALUE
);
192 RETURN_STRING_LITERAL(QUIC_DECOMPRESSION_FAILURE
);
193 RETURN_STRING_LITERAL(QUIC_CONNECTION_TIMED_OUT
);
194 RETURN_STRING_LITERAL(QUIC_ERROR_MIGRATING_ADDRESS
);
195 RETURN_STRING_LITERAL(QUIC_PACKET_WRITE_ERROR
);
196 RETURN_STRING_LITERAL(QUIC_PACKET_READ_ERROR
);
197 RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_FRAME
);
198 RETURN_STRING_LITERAL(QUIC_PROOF_INVALID
);
199 RETURN_STRING_LITERAL(QUIC_CRYPTO_DUPLICATE_TAG
);
200 RETURN_STRING_LITERAL(QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT
);
201 RETURN_STRING_LITERAL(QUIC_CRYPTO_SERVER_CONFIG_EXPIRED
);
202 RETURN_STRING_LITERAL(QUIC_INVALID_CHANNEL_ID_SIGNATURE
);
203 RETURN_STRING_LITERAL(QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED
);
204 RETURN_STRING_LITERAL(QUIC_LAST_ERROR
);
205 // Intentionally have no default case, so we'll break the build
206 // if we add errors and don't put them here.
208 // Return a default value so that we return this when |error| doesn't match
209 // any of the QuicErrorCodes. This can happen when the ConnectionClose
210 // frame sent by the peer (attacker) has invalid error code.
211 return "INVALID_ERROR_CODE";
215 const char* QuicUtils::EncryptionLevelToString(EncryptionLevel level
) {
217 RETURN_STRING_LITERAL(ENCRYPTION_NONE
);
218 RETURN_STRING_LITERAL(ENCRYPTION_INITIAL
);
219 RETURN_STRING_LITERAL(ENCRYPTION_FORWARD_SECURE
);
220 RETURN_STRING_LITERAL(NUM_ENCRYPTION_LEVELS
);
222 return "INVALID_ENCRYPTION_LEVEL";
226 string
QuicUtils::TagToString(QuicTag tag
) {
229 const QuicTag orig_tag
= tag
;
231 for (size_t i
= 0; i
< sizeof(chars
); i
++) {
233 if ((chars
[i
] == 0 || chars
[i
] == '\xff') && i
== 3) {
236 if (!isprint(static_cast<unsigned char>(chars
[i
]))) {
244 return string(chars
, sizeof(chars
));
247 return base::UintToString(orig_tag
);
251 string
QuicUtils::StringToHexASCIIDump(StringPiece in_buffer
) {
253 const int kBytesPerLine
= 16; // Max bytes dumped per line
254 const char* buf
= in_buffer
.data();
255 int bytes_remaining
= in_buffer
.size();
256 string s
; // our output
258 while (bytes_remaining
> 0) {
259 const int line_bytes
= std::min(bytes_remaining
, kBytesPerLine
);
260 base::StringAppendF(&s
, "0x%04x: ", offset
); // Do the line header
261 for (int i
= 0; i
< kBytesPerLine
; ++i
) {
262 if (i
< line_bytes
) {
263 base::StringAppendF(&s
, "%02x", static_cast<unsigned char>(p
[i
]));
265 s
+= " "; // two-space filler instead of two-space hex digits
270 for (int i
= 0; i
< line_bytes
; ++i
) { // Do the ASCII dump
271 s
+= (p
[i
] > 32 && p
[i
] < 127) ? p
[i
] : '.';
274 bytes_remaining
-= line_bytes
;
275 offset
+= line_bytes
;