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