rAc - revert invalid suggestions to edit mode
[chromium-blink-merge.git] / net / quic / quic_utils.cc
blobce453c1db7bd068779882977c2c4b47e149d2177
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"
7 #include <ctype.h>
9 #include <algorithm>
11 #include "base/basictypes.h"
12 #include "base/logging.h"
13 #include "base/port.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "net/quic/quic_write_blocked_list.h"
18 using base::StringPiece;
19 using std::string;
21 namespace net {
23 // static
24 uint64 QuicUtils::FNV1a_64_Hash(const char* data, int len) {
25 static const uint64 kOffset = GG_UINT64_C(14695981039346656037);
26 static const uint64 kPrime = GG_UINT64_C(1099511628211);
28 const uint8* octets = reinterpret_cast<const uint8*>(data);
30 uint64 hash = kOffset;
32 for (int i = 0; i < len; ++i) {
33 hash = hash ^ octets[i];
34 hash = hash * kPrime;
37 return hash;
40 // static
41 uint128 QuicUtils::FNV1a_128_Hash(const char* data, int len) {
42 // The following two constants are defined as part of the hash algorithm.
43 // see http://www.isthe.com/chongo/tech/comp/fnv/
44 // 309485009821345068724781371
45 const uint128 kPrime(16777216, 315);
46 // 144066263297769815596495629667062367629
47 const uint128 kOffset(GG_UINT64_C(7809847782465536322),
48 GG_UINT64_C(7113472399480571277));
50 const uint8* octets = reinterpret_cast<const uint8*>(data);
52 uint128 hash = kOffset;
54 for (int i = 0; i < len; ++i) {
55 hash = hash ^ uint128(0, octets[i]);
56 hash = hash * kPrime;
59 return hash;
62 // static
63 bool QuicUtils::FindMutualTag(const QuicTagVector& our_tags_vector,
64 const QuicTag* their_tags,
65 size_t num_their_tags,
66 Priority priority,
67 QuicTag* out_result,
68 size_t* out_index) {
69 if (our_tags_vector.empty()) {
70 return false;
72 const size_t num_our_tags = our_tags_vector.size();
73 const QuicTag* our_tags = &our_tags_vector[0];
75 size_t num_priority_tags, num_inferior_tags;
76 const QuicTag* priority_tags;
77 const QuicTag* inferior_tags;
78 if (priority == LOCAL_PRIORITY) {
79 num_priority_tags = num_our_tags;
80 priority_tags = our_tags;
81 num_inferior_tags = num_their_tags;
82 inferior_tags = their_tags;
83 } else {
84 num_priority_tags = num_their_tags;
85 priority_tags = their_tags;
86 num_inferior_tags = num_our_tags;
87 inferior_tags = our_tags;
90 for (size_t i = 0; i < num_priority_tags; i++) {
91 for (size_t j = 0; j < num_inferior_tags; j++) {
92 if (priority_tags[i] == inferior_tags[j]) {
93 *out_result = priority_tags[i];
94 if (out_index) {
95 if (priority == LOCAL_PRIORITY) {
96 *out_index = j;
97 } else {
98 *out_index = i;
101 return true;
106 return false;
109 // static
110 void QuicUtils::SerializeUint128(uint128 v, uint8* out) {
111 const uint64 lo = Uint128Low64(v);
112 const uint64 hi = Uint128High64(v);
113 // This assumes that the system is little-endian.
114 memcpy(out, &lo, sizeof(lo));
115 memcpy(out + sizeof(lo), &hi, sizeof(hi));
118 // static
119 void QuicUtils::SerializeUint128Short(uint128 v, uint8* out) {
120 const uint64 lo = Uint128Low64(v);
121 const uint64 hi = Uint128High64(v);
122 // This assumes that the system is little-endian.
123 memcpy(out, &lo, sizeof(lo));
124 memcpy(out + sizeof(lo), &hi, sizeof(hi) / 2);
127 #define RETURN_STRING_LITERAL(x) \
128 case x: \
129 return #x;
131 // static
132 const char* QuicUtils::StreamErrorToString(QuicRstStreamErrorCode error) {
133 switch (error) {
134 RETURN_STRING_LITERAL(QUIC_STREAM_NO_ERROR);
135 RETURN_STRING_LITERAL(QUIC_STREAM_CONNECTION_ERROR);
136 RETURN_STRING_LITERAL(QUIC_ERROR_PROCESSING_STREAM);
137 RETURN_STRING_LITERAL(QUIC_MULTIPLE_TERMINATION_OFFSETS);
138 RETURN_STRING_LITERAL(QUIC_BAD_APPLICATION_PAYLOAD);
139 RETURN_STRING_LITERAL(QUIC_STREAM_PEER_GOING_AWAY);
140 RETURN_STRING_LITERAL(QUIC_STREAM_CANCELLED);
141 RETURN_STRING_LITERAL(QUIC_STREAM_LAST_ERROR);
143 // Return a default value so that we return this when |error| doesn't match
144 // any of the QuicRstStreamErrorCodes. This can happen when the RstStream
145 // frame sent by the peer (attacker) has invalid error code.
146 return "INVALID_RST_STREAM_ERROR_CODE";
149 // static
150 const char* QuicUtils::ErrorToString(QuicErrorCode error) {
151 switch (error) {
152 RETURN_STRING_LITERAL(QUIC_NO_ERROR);
153 RETURN_STRING_LITERAL(QUIC_INTERNAL_ERROR);
154 RETURN_STRING_LITERAL(QUIC_STREAM_DATA_AFTER_TERMINATION);
155 RETURN_STRING_LITERAL(QUIC_INVALID_PACKET_HEADER);
156 RETURN_STRING_LITERAL(QUIC_INVALID_FRAME_DATA);
157 RETURN_STRING_LITERAL(QUIC_MISSING_PAYLOAD);
158 RETURN_STRING_LITERAL(QUIC_INVALID_FEC_DATA);
159 RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_DATA);
160 RETURN_STRING_LITERAL(QUIC_INVALID_RST_STREAM_DATA);
161 RETURN_STRING_LITERAL(QUIC_INVALID_CONNECTION_CLOSE_DATA);
162 RETURN_STRING_LITERAL(QUIC_INVALID_GOAWAY_DATA);
163 RETURN_STRING_LITERAL(QUIC_INVALID_WINDOW_UPDATE_DATA);
164 RETURN_STRING_LITERAL(QUIC_INVALID_BLOCKED_DATA);
165 RETURN_STRING_LITERAL(QUIC_INVALID_STOP_WAITING_DATA);
166 RETURN_STRING_LITERAL(QUIC_INVALID_ACK_DATA);
167 RETURN_STRING_LITERAL(QUIC_INVALID_CONGESTION_FEEDBACK_DATA);
168 RETURN_STRING_LITERAL(QUIC_INVALID_VERSION_NEGOTIATION_PACKET);
169 RETURN_STRING_LITERAL(QUIC_INVALID_PUBLIC_RST_PACKET);
170 RETURN_STRING_LITERAL(QUIC_DECRYPTION_FAILURE);
171 RETURN_STRING_LITERAL(QUIC_ENCRYPTION_FAILURE);
172 RETURN_STRING_LITERAL(QUIC_PACKET_TOO_LARGE);
173 RETURN_STRING_LITERAL(QUIC_PACKET_FOR_NONEXISTENT_STREAM);
174 RETURN_STRING_LITERAL(QUIC_PEER_GOING_AWAY);
175 RETURN_STRING_LITERAL(QUIC_HANDSHAKE_FAILED);
176 RETURN_STRING_LITERAL(QUIC_CRYPTO_TAGS_OUT_OF_ORDER);
177 RETURN_STRING_LITERAL(QUIC_CRYPTO_TOO_MANY_ENTRIES);
178 RETURN_STRING_LITERAL(QUIC_CRYPTO_TOO_MANY_REJECTS);
179 RETURN_STRING_LITERAL(QUIC_CRYPTO_INVALID_VALUE_LENGTH)
180 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE);
181 RETURN_STRING_LITERAL(QUIC_CRYPTO_INTERNAL_ERROR);
182 RETURN_STRING_LITERAL(QUIC_CRYPTO_VERSION_NOT_SUPPORTED);
183 RETURN_STRING_LITERAL(QUIC_CRYPTO_NO_SUPPORT);
184 RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_TYPE);
185 RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER);
186 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND);
187 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP);
188 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND);
189 RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_ID);
190 RETURN_STRING_LITERAL(QUIC_INVALID_PRIORITY);
191 RETURN_STRING_LITERAL(QUIC_TOO_MANY_OPEN_STREAMS);
192 RETURN_STRING_LITERAL(QUIC_PUBLIC_RESET);
193 RETURN_STRING_LITERAL(QUIC_INVALID_VERSION);
194 RETURN_STRING_LITERAL(QUIC_STREAM_RST_BEFORE_HEADERS_DECOMPRESSED);
195 RETURN_STRING_LITERAL(QUIC_INVALID_HEADER_ID);
196 RETURN_STRING_LITERAL(QUIC_INVALID_NEGOTIATED_VALUE);
197 RETURN_STRING_LITERAL(QUIC_DECOMPRESSION_FAILURE);
198 RETURN_STRING_LITERAL(QUIC_CONNECTION_TIMED_OUT);
199 RETURN_STRING_LITERAL(QUIC_ERROR_MIGRATING_ADDRESS);
200 RETURN_STRING_LITERAL(QUIC_PACKET_WRITE_ERROR);
201 RETURN_STRING_LITERAL(QUIC_PACKET_READ_ERROR);
202 RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_FRAME);
203 RETURN_STRING_LITERAL(QUIC_INVALID_HEADERS_STREAM_DATA);
204 RETURN_STRING_LITERAL(QUIC_FLOW_CONTROL_ERROR);
205 RETURN_STRING_LITERAL(QUIC_PROOF_INVALID);
206 RETURN_STRING_LITERAL(QUIC_CRYPTO_DUPLICATE_TAG);
207 RETURN_STRING_LITERAL(QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT);
208 RETURN_STRING_LITERAL(QUIC_CRYPTO_SERVER_CONFIG_EXPIRED);
209 RETURN_STRING_LITERAL(QUIC_INVALID_CHANNEL_ID_SIGNATURE);
210 RETURN_STRING_LITERAL(QUIC_CRYPTO_SYMMETRIC_KEY_SETUP_FAILED);
211 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_WHILE_VALIDATING_CLIENT_HELLO);
212 RETURN_STRING_LITERAL(QUIC_VERSION_NEGOTIATION_MISMATCH);
213 RETURN_STRING_LITERAL(QUIC_LAST_ERROR);
214 // Intentionally have no default case, so we'll break the build
215 // if we add errors and don't put them here.
217 // Return a default value so that we return this when |error| doesn't match
218 // any of the QuicErrorCodes. This can happen when the ConnectionClose
219 // frame sent by the peer (attacker) has invalid error code.
220 return "INVALID_ERROR_CODE";
223 // static
224 const char* QuicUtils::EncryptionLevelToString(EncryptionLevel level) {
225 switch (level) {
226 RETURN_STRING_LITERAL(ENCRYPTION_NONE);
227 RETURN_STRING_LITERAL(ENCRYPTION_INITIAL);
228 RETURN_STRING_LITERAL(ENCRYPTION_FORWARD_SECURE);
229 RETURN_STRING_LITERAL(NUM_ENCRYPTION_LEVELS);
231 return "INVALID_ENCRYPTION_LEVEL";
234 // static
235 string QuicUtils::TagToString(QuicTag tag) {
236 char chars[4];
237 bool ascii = true;
238 const QuicTag orig_tag = tag;
240 for (size_t i = 0; i < sizeof(chars); i++) {
241 chars[i] = tag;
242 if ((chars[i] == 0 || chars[i] == '\xff') && i == 3) {
243 chars[i] = ' ';
245 if (!isprint(static_cast<unsigned char>(chars[i]))) {
246 ascii = false;
247 break;
249 tag >>= 8;
252 if (ascii) {
253 return string(chars, sizeof(chars));
256 return base::UintToString(orig_tag);
259 // static
260 string QuicUtils::StringToHexASCIIDump(StringPiece in_buffer) {
261 int offset = 0;
262 const int kBytesPerLine = 16; // Max bytes dumped per line
263 const char* buf = in_buffer.data();
264 int bytes_remaining = in_buffer.size();
265 string s; // our output
266 const char* p = buf;
267 while (bytes_remaining > 0) {
268 const int line_bytes = std::min(bytes_remaining, kBytesPerLine);
269 base::StringAppendF(&s, "0x%04x: ", offset); // Do the line header
270 for (int i = 0; i < kBytesPerLine; ++i) {
271 if (i < line_bytes) {
272 base::StringAppendF(&s, "%02x", static_cast<unsigned char>(p[i]));
273 } else {
274 s += " "; // two-space filler instead of two-space hex digits
276 if (i % 2) s += ' ';
278 s += ' ';
279 for (int i = 0; i < line_bytes; ++i) { // Do the ASCII dump
280 s+= (p[i] > 32 && p[i] < 127) ? p[i] : '.';
283 bytes_remaining -= line_bytes;
284 offset += line_bytes;
285 p += line_bytes;
286 s += '\n';
288 return s;
291 // static
292 QuicPriority QuicUtils::LowestPriority() {
293 return QuicWriteBlockedList::kLowestPriority;
296 // static
297 QuicPriority QuicUtils::HighestPriority() {
298 return QuicWriteBlockedList::kHighestPriority;
301 } // namespace net