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 "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/port.h" | 8 #include "base/port.h" |
| 9 #include "net/quic/crypto/crypto_protocol.h" |
| 10 #include "net/quic/crypto/quic_random.h" |
| 11 #include "net/quic/quic_clock.h" |
| 12 |
| 13 using base::StringPiece; |
| 14 using std::string; |
9 | 15 |
10 namespace net { | 16 namespace net { |
11 | 17 |
12 // static | 18 // static |
13 size_t QuicUtils::StreamFramePacketOverhead(int num_frames) { | 19 size_t QuicUtils::StreamFramePacketOverhead(int num_frames) { |
14 // TODO(jar): Use sizeof(some name). | 20 // TODO(jar): Use sizeof(some name). |
15 return kPacketHeaderSize + | 21 return kPacketHeaderSize + |
16 1 + // frame count | 22 1 + // frame count |
17 (1 + // 8 bit type | 23 (1 + // 8 bit type |
18 kMinStreamFrameLength) * num_frames; | 24 kMinStreamFrameLength) * num_frames; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_TYPE); | 74 RETURN_STRING_LITERAL(QUIC_INVALID_CRYPTO_MESSAGE_TYPE); |
69 RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_ID); | 75 RETURN_STRING_LITERAL(QUIC_INVALID_STREAM_ID); |
70 RETURN_STRING_LITERAL(QUIC_TOO_MANY_OPEN_STREAMS); | 76 RETURN_STRING_LITERAL(QUIC_TOO_MANY_OPEN_STREAMS); |
71 RETURN_STRING_LITERAL(QUIC_CONNECTION_TIMED_OUT); | 77 RETURN_STRING_LITERAL(QUIC_CONNECTION_TIMED_OUT); |
72 // Intentionally have no default case, so we'll break the build | 78 // Intentionally have no default case, so we'll break the build |
73 // if we add errors and don't put them here. | 79 // if we add errors and don't put them here. |
74 } | 80 } |
75 return ""; | 81 return ""; |
76 } | 82 } |
77 | 83 |
| 84 void QuicUtils::GenerateNonce(const QuicClock* clock, |
| 85 QuicRandom* random_generator, |
| 86 string* nonce) { |
| 87 // a 4-byte timestamp + 28 random bytes. |
| 88 nonce->reserve(kNonceSize); |
| 89 nonce->resize(kNonceSize); |
| 90 QuicTime::Delta now = clock->NowAsDeltaSinceUnixEpoch(); |
| 91 uint32 gmt_unix_time = now.ToSeconds(); |
| 92 const size_t time_size = sizeof(gmt_unix_time); |
| 93 memcpy(&(*nonce)[0], &gmt_unix_time, time_size); |
| 94 random_generator->RandBytes(&(*nonce)[time_size], kNonceSize - time_size); |
| 95 } |
| 96 |
| 97 void QuicUtils::FillClientHelloMessage(const QuicClientHelloConfig& config, |
| 98 const string& nonce, |
| 99 CryptoHandshakeMessage* message) { |
| 100 message->tag = kCHLO; |
| 101 |
| 102 StringPiece value; |
| 103 |
| 104 // Version. |
| 105 value.set(&config.version, sizeof(config.version)); |
| 106 message->tag_value_map[kVERS] = value.as_string(); |
| 107 |
| 108 // Key exchange methods. |
| 109 value.set(&config.key_exchange[0], |
| 110 config.key_exchange.size() * sizeof(config.key_exchange[0])); |
| 111 message->tag_value_map[kKEXS] = value.as_string(); |
| 112 |
| 113 // Authenticated encryption algorithms. |
| 114 value.set(&config.aead[0], config.aead.size() * sizeof(config.aead[0])); |
| 115 message->tag_value_map[kAEAD] = value.as_string(); |
| 116 |
| 117 // Congestion control feedback types. |
| 118 value.set(&config.congestion_control[0], |
| 119 config.congestion_control.size() * |
| 120 sizeof(config.congestion_control[0])); |
| 121 message->tag_value_map[kCGST] = value.as_string(); |
| 122 |
| 123 // Idle connection state lifetime. |
| 124 value.set(&config.idle_connection_state_lifetime, |
| 125 sizeof(config.idle_connection_state_lifetime)); |
| 126 message->tag_value_map[kICSL] = value.as_string(); |
| 127 |
| 128 // Keepalive timeout. |
| 129 value.set(&config.keepalive_timeout, sizeof(config.keepalive_timeout)); |
| 130 message->tag_value_map[kKATO] = value.as_string(); |
| 131 |
| 132 // Connection nonce. |
| 133 message->tag_value_map[kNONC] = nonce; |
| 134 |
| 135 // Server name indication. |
| 136 // TODO(wtc): if server_hostname_ is a DNS name, store it in |
| 137 // message->tag_value_map[kSNI]. |
| 138 } |
| 139 |
78 } // namespace net | 140 } // namespace net |
OLD | NEW |