- add sources.
[platform/framework/web/crosswalk.git] / src / net / quic / quic_utils.cc
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 <algorithm>
10
11 #include "base/logging.h"
12 #include "base/port.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/string_number_conversions.h"
15
16 using base::StringPiece;
17 using std::string;
18
19 namespace net {
20
21 // static
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);
25
26   const uint8* octets = reinterpret_cast<const uint8*>(data);
27
28   uint64 hash = kOffset;
29
30   for (int i = 0; i < len; ++i) {
31     hash = hash ^ octets[i];
32     hash = hash * kPrime;
33   }
34
35   return hash;
36 }
37
38 // static
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));
47
48   const uint8* octets = reinterpret_cast<const uint8*>(data);
49
50   uint128 hash = kOffset;
51
52   for (int i = 0; i < len; ++i) {
53     hash  = hash ^ uint128(0, octets[i]);
54     hash = hash * kPrime;
55   }
56
57   return hash;
58 }
59
60 // static
61 bool QuicUtils::FindMutualTag(const QuicTagVector& our_tags_vector,
62                               const QuicTag* their_tags,
63                               size_t num_their_tags,
64                               Priority priority,
65                               QuicTag* out_result,
66                               size_t* out_index) {
67   if (our_tags_vector.empty()) {
68     return false;
69   }
70   const size_t num_our_tags = our_tags_vector.size();
71   const QuicTag* our_tags = &our_tags_vector[0];
72
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;
81   } else {
82     num_priority_tags = num_their_tags;
83     priority_tags = their_tags;
84     num_inferior_tags = num_our_tags;
85     inferior_tags = our_tags;
86   }
87
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];
92         if (out_index) {
93           if (priority == LOCAL_PRIORITY) {
94             *out_index = j;
95           } else {
96             *out_index = i;
97           }
98         }
99         return true;
100       }
101     }
102   }
103
104   return false;
105 }
106
107 // static
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));
114 }
115
116 // static
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);
123 }
124
125 #define RETURN_STRING_LITERAL(x) \
126 case x: \
127 return #x;
128
129 // static
130 const char* QuicUtils::StreamErrorToString(QuicRstStreamErrorCode error) {
131   switch (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);
140   }
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";
145 }
146
147 // static
148 const char* QuicUtils::ErrorToString(QuicErrorCode error) {
149   switch (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.
207   }
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";
212 }
213
214 // static
215 const char* QuicUtils::EncryptionLevelToString(EncryptionLevel level) {
216   switch (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);
221   }
222   return "INVALID_ENCRYPTION_LEVEL";
223 }
224
225 // static
226 string QuicUtils::TagToString(QuicTag tag) {
227   char chars[4];
228   bool ascii = true;
229   const QuicTag orig_tag = tag;
230
231   for (size_t i = 0; i < sizeof(chars); i++) {
232     chars[i] = tag;
233     if ((chars[i] == 0 || chars[i] == '\xff') && i == 3) {
234       chars[i] = ' ';
235     }
236     if (!isprint(static_cast<unsigned char>(chars[i]))) {
237       ascii = false;
238       break;
239     }
240     tag >>= 8;
241   }
242
243   if (ascii) {
244     return string(chars, sizeof(chars));
245   }
246
247   return base::UintToString(orig_tag);
248 }
249
250 // static
251 string QuicUtils::StringToHexASCIIDump(StringPiece in_buffer) {
252   int offset = 0;
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
257   const char* p = buf;
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]));
264       } else {
265         s += "  ";    // two-space filler instead of two-space hex digits
266       }
267       if (i % 2) s += ' ';
268     }
269     s += ' ';
270     for (int i = 0; i < line_bytes; ++i) {  // Do the ASCII dump
271       s+= (p[i] >  32 && p[i] < 127) ? p[i] : '.';
272     }
273
274     bytes_remaining -= line_bytes;
275     offset += line_bytes;
276     p += line_bytes;
277     s += '\n';
278   }
279   return s;
280 }
281
282 }  // namespace net