OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/quic_utils.h" | 5 #include "net/quic/quic_utils.h" |
6 | 6 |
| 7 #include <ctype.h> |
| 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "base/port.h" | 10 #include "base/port.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 |
| 13 using std::string; |
9 | 14 |
10 namespace net { | 15 namespace net { |
11 | 16 |
12 // static | 17 // static |
13 uint64 QuicUtils::FNV1a_64_Hash(const char* data, int len) { | 18 uint64 QuicUtils::FNV1a_64_Hash(const char* data, int len) { |
14 static const uint64 kOffset = GG_UINT64_C(14695981039346656037); | 19 static const uint64 kOffset = GG_UINT64_C(14695981039346656037); |
15 static const uint64 kPrime = GG_UINT64_C(1099511628211); | 20 static const uint64 kPrime = GG_UINT64_C(1099511628211); |
16 | 21 |
17 const uint8* octets = reinterpret_cast<const uint8*>(data); | 22 const uint8* octets = reinterpret_cast<const uint8*>(data); |
18 | 23 |
(...skipping 23 matching lines...) Expand all Loading... |
42 | 47 |
43 for (int i = 0; i < len; ++i) { | 48 for (int i = 0; i < len; ++i) { |
44 hash = hash ^ uint128(0, octets[i]); | 49 hash = hash ^ uint128(0, octets[i]); |
45 hash = hash * kPrime; | 50 hash = hash * kPrime; |
46 } | 51 } |
47 | 52 |
48 return hash; | 53 return hash; |
49 } | 54 } |
50 | 55 |
51 // static | 56 // static |
| 57 bool QuicUtils::FindMutualTag(const QuicTagVector& our_tags_vector, |
| 58 const QuicTag* their_tags, |
| 59 size_t num_their_tags, |
| 60 Priority priority, |
| 61 QuicTag* out_result, |
| 62 size_t* out_index) { |
| 63 if (our_tags_vector.empty()) { |
| 64 return false; |
| 65 } |
| 66 const size_t num_our_tags = our_tags_vector.size(); |
| 67 const QuicTag* our_tags = &our_tags_vector[0]; |
| 68 |
| 69 size_t num_priority_tags, num_inferior_tags; |
| 70 const QuicTag* priority_tags; |
| 71 const QuicTag* inferior_tags; |
| 72 if (priority == LOCAL_PRIORITY) { |
| 73 num_priority_tags = num_our_tags; |
| 74 priority_tags = our_tags; |
| 75 num_inferior_tags = num_their_tags; |
| 76 inferior_tags = their_tags; |
| 77 } else { |
| 78 num_priority_tags = num_their_tags; |
| 79 priority_tags = their_tags; |
| 80 num_inferior_tags = num_our_tags; |
| 81 inferior_tags = our_tags; |
| 82 } |
| 83 |
| 84 for (size_t i = 0; i < num_priority_tags; i++) { |
| 85 for (size_t j = 0; j < num_inferior_tags; j++) { |
| 86 if (priority_tags[i] == inferior_tags[j]) { |
| 87 *out_result = priority_tags[i]; |
| 88 if (out_index) { |
| 89 if (priority == LOCAL_PRIORITY) { |
| 90 *out_index = j; |
| 91 } else { |
| 92 *out_index = i; |
| 93 } |
| 94 } |
| 95 return true; |
| 96 } |
| 97 } |
| 98 } |
| 99 |
| 100 return false; |
| 101 } |
| 102 |
| 103 // static |
52 void QuicUtils::SerializeUint128(uint128 v, uint8* out) { | 104 void QuicUtils::SerializeUint128(uint128 v, uint8* out) { |
53 const uint64 lo = Uint128Low64(v); | 105 const uint64 lo = Uint128Low64(v); |
54 const uint64 hi = Uint128High64(v); | 106 const uint64 hi = Uint128High64(v); |
55 // This assumes that the system is little-endian. | 107 // This assumes that the system is little-endian. |
56 memcpy(out, &lo, sizeof(lo)); | 108 memcpy(out, &lo, sizeof(lo)); |
57 memcpy(out + sizeof(lo), &hi, sizeof(hi)); | 109 memcpy(out + sizeof(lo), &hi, sizeof(hi)); |
58 } | 110 } |
59 | 111 |
60 // static | 112 // static |
61 uint128 QuicUtils::ParseUint128(const uint8* in) { | 113 uint128 QuicUtils::ParseUint128(const uint8* in) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 RETURN_STRING_LITERAL(QUIC_INTERNAL_ERROR); | 145 RETURN_STRING_LITERAL(QUIC_INTERNAL_ERROR); |
94 RETURN_STRING_LITERAL(QUIC_STREAM_DATA_AFTER_TERMINATION); | 146 RETURN_STRING_LITERAL(QUIC_STREAM_DATA_AFTER_TERMINATION); |
95 RETURN_STRING_LITERAL(QUIC_INVALID_PACKET_HEADER); | 147 RETURN_STRING_LITERAL(QUIC_INVALID_PACKET_HEADER); |
96 RETURN_STRING_LITERAL(QUIC_INVALID_FRAME_DATA); | 148 RETURN_STRING_LITERAL(QUIC_INVALID_FRAME_DATA); |
97 RETURN_STRING_LITERAL(QUIC_INVALID_FEC_DATA); | 149 RETURN_STRING_LITERAL(QUIC_INVALID_FEC_DATA); |
98 RETURN_STRING_LITERAL(QUIC_INVALID_RST_STREAM_DATA); | 150 RETURN_STRING_LITERAL(QUIC_INVALID_RST_STREAM_DATA); |
99 RETURN_STRING_LITERAL(QUIC_INVALID_CONNECTION_CLOSE_DATA); | 151 RETURN_STRING_LITERAL(QUIC_INVALID_CONNECTION_CLOSE_DATA); |
100 RETURN_STRING_LITERAL(QUIC_INVALID_GOAWAY_DATA); | 152 RETURN_STRING_LITERAL(QUIC_INVALID_GOAWAY_DATA); |
101 RETURN_STRING_LITERAL(QUIC_INVALID_ACK_DATA); | 153 RETURN_STRING_LITERAL(QUIC_INVALID_ACK_DATA); |
102 RETURN_STRING_LITERAL(QUIC_INVALID_VERSION_NEGOTIATION_PACKET); | 154 RETURN_STRING_LITERAL(QUIC_INVALID_VERSION_NEGOTIATION_PACKET); |
| 155 RETURN_STRING_LITERAL(QUIC_INVALID_PUBLIC_RST_PACKET); |
103 RETURN_STRING_LITERAL(QUIC_DECRYPTION_FAILURE); | 156 RETURN_STRING_LITERAL(QUIC_DECRYPTION_FAILURE); |
104 RETURN_STRING_LITERAL(QUIC_ENCRYPTION_FAILURE); | 157 RETURN_STRING_LITERAL(QUIC_ENCRYPTION_FAILURE); |
105 RETURN_STRING_LITERAL(QUIC_PACKET_TOO_LARGE); | 158 RETURN_STRING_LITERAL(QUIC_PACKET_TOO_LARGE); |
106 RETURN_STRING_LITERAL(QUIC_PACKET_FOR_NONEXISTENT_STREAM); | 159 RETURN_STRING_LITERAL(QUIC_PACKET_FOR_NONEXISTENT_STREAM); |
107 RETURN_STRING_LITERAL(QUIC_PEER_GOING_AWAY); | 160 RETURN_STRING_LITERAL(QUIC_PEER_GOING_AWAY); |
108 RETURN_STRING_LITERAL(QUIC_CRYPTO_TAGS_OUT_OF_ORDER); | 161 RETURN_STRING_LITERAL(QUIC_CRYPTO_TAGS_OUT_OF_ORDER); |
109 RETURN_STRING_LITERAL(QUIC_CRYPTO_TOO_MANY_ENTRIES); | 162 RETURN_STRING_LITERAL(QUIC_CRYPTO_TOO_MANY_ENTRIES); |
110 RETURN_STRING_LITERAL(QUIC_CRYPTO_TOO_MANY_REJECTS); | 163 RETURN_STRING_LITERAL(QUIC_CRYPTO_TOO_MANY_REJECTS); |
111 RETURN_STRING_LITERAL(QUIC_CRYPTO_INVALID_VALUE_LENGTH) | 164 RETURN_STRING_LITERAL(QUIC_CRYPTO_INVALID_VALUE_LENGTH) |
112 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE); | 165 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE); |
113 RETURN_STRING_LITERAL(QUIC_CRYPTO_INTERNAL_ERROR); | 166 RETURN_STRING_LITERAL(QUIC_CRYPTO_INTERNAL_ERROR); |
114 RETURN_STRING_LITERAL(QUIC_CRYPTO_VERSION_NOT_SUPPORTED); | 167 RETURN_STRING_LITERAL(QUIC_CRYPTO_VERSION_NOT_SUPPORTED); |
115 RETURN_STRING_LITERAL(QUIC_CRYPTO_NO_SUPPORT); | 168 RETURN_STRING_LITERAL(QUIC_CRYPTO_NO_SUPPORT); |
116 RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_TYPE); | 169 RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_TYPE); |
117 RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER); | 170 RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_PARAMETER); |
118 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND); | 171 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND); |
119 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP); | 172 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP); |
120 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND); | 173 RETURN_STRING_LITERAL(QUIC_CRYPTO_MESSAGE_INDEX_NOT_FOUND); |
121 RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_ID); | 174 RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_ID); |
122 RETURN_STRING_LITERAL(QUIC_TOO_MANY_OPEN_STREAMS); | 175 RETURN_STRING_LITERAL(QUIC_TOO_MANY_OPEN_STREAMS); |
123 RETURN_STRING_LITERAL(QUIC_PUBLIC_RESET); | 176 RETURN_STRING_LITERAL(QUIC_PUBLIC_RESET); |
124 RETURN_STRING_LITERAL(QUIC_INVALID_VERSION); | 177 RETURN_STRING_LITERAL(QUIC_INVALID_VERSION); |
125 RETURN_STRING_LITERAL(QUIC_STREAM_RST_BEFORE_HEADERS_DECOMPRESSED); | 178 RETURN_STRING_LITERAL(QUIC_STREAM_RST_BEFORE_HEADERS_DECOMPRESSED); |
126 RETURN_STRING_LITERAL(QUIC_INVALID_HEADER_ID); | 179 RETURN_STRING_LITERAL(QUIC_INVALID_HEADER_ID); |
| 180 RETURN_STRING_LITERAL(QUIC_INVALID_NEGOTIATED_VALUE); |
| 181 RETURN_STRING_LITERAL(QUIC_DECOMPRESSION_FAILURE); |
127 RETURN_STRING_LITERAL(QUIC_CONNECTION_TIMED_OUT); | 182 RETURN_STRING_LITERAL(QUIC_CONNECTION_TIMED_OUT); |
| 183 RETURN_STRING_LITERAL(QUIC_ERROR_MIGRATING_ADDRESS); |
| 184 RETURN_STRING_LITERAL(QUIC_PACKET_WRITE_ERROR); |
128 RETURN_STRING_LITERAL(QUIC_PROOF_INVALID); | 185 RETURN_STRING_LITERAL(QUIC_PROOF_INVALID); |
129 RETURN_STRING_LITERAL(QUIC_CRYPTO_DUPLICATE_TAG); | 186 RETURN_STRING_LITERAL(QUIC_CRYPTO_DUPLICATE_TAG); |
130 RETURN_STRING_LITERAL(QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT); | 187 RETURN_STRING_LITERAL(QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT); |
131 RETURN_STRING_LITERAL(QUIC_CRYPTO_SERVER_CONFIG_EXPIRED); | 188 RETURN_STRING_LITERAL(QUIC_CRYPTO_SERVER_CONFIG_EXPIRED); |
132 RETURN_STRING_LITERAL(QUIC_LAST_ERROR); | 189 RETURN_STRING_LITERAL(QUIC_LAST_ERROR); |
133 // Intentionally have no default case, so we'll break the build | 190 // Intentionally have no default case, so we'll break the build |
134 // if we add errors and don't put them here. | 191 // if we add errors and don't put them here. |
135 } | 192 } |
136 // Return a default value so that we return this when |error| doesn't match | 193 // Return a default value so that we return this when |error| doesn't match |
137 // any of the QuicErrorCodes. This can happen when the ConnectionClose | 194 // any of the QuicErrorCodes. This can happen when the ConnectionClose |
138 // frame sent by the peer (attacker) has invalid error code. | 195 // frame sent by the peer (attacker) has invalid error code. |
139 return "INVALID_ERROR_CODE"; | 196 return "INVALID_ERROR_CODE"; |
140 } | 197 } |
141 | 198 |
| 199 // static |
| 200 string QuicUtils::TagToString(QuicTag tag) { |
| 201 char chars[4]; |
| 202 bool ascii = true; |
| 203 const QuicTag orig_tag = tag; |
| 204 |
| 205 for (size_t i = 0; i < sizeof(chars); i++) { |
| 206 chars[i] = tag; |
| 207 if (chars[i] == 0 && i == 3) { |
| 208 chars[i] = ' '; |
| 209 } |
| 210 if (!isprint(static_cast<unsigned char>(chars[i]))) { |
| 211 ascii = false; |
| 212 break; |
| 213 } |
| 214 tag >>= 8; |
| 215 } |
| 216 |
| 217 if (ascii) { |
| 218 return string(chars, sizeof(chars)); |
| 219 } |
| 220 |
| 221 return base::UintToString(orig_tag); |
| 222 } |
| 223 |
142 } // namespace net | 224 } // namespace net |
OLD | NEW |