Cleanup unused param in AddActiveSetupWorkItems
[chromium-blink-merge.git] / net / quic / quic_config.cc
blob675716c2ca5aa19f66805538efd686e36613ab09
1 // Copyright (c) 2013 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_config.h"
7 #include <algorithm>
9 #include "base/logging.h"
10 #include "net/quic/crypto/crypto_handshake_message.h"
11 #include "net/quic/crypto/crypto_protocol.h"
12 #include "net/quic/quic_utils.h"
14 using std::min;
15 using std::string;
17 namespace net {
19 // Reads the value corresponding to |name_| from |msg| into |out|. If the
20 // |name_| is absent in |msg| and |presence| is set to OPTIONAL |out| is set
21 // to |default_value|.
22 QuicErrorCode ReadUint32(const CryptoHandshakeMessage& msg,
23 QuicTag tag,
24 QuicConfigPresence presence,
25 uint32 default_value,
26 uint32* out,
27 string* error_details) {
28 DCHECK(error_details != nullptr);
29 QuicErrorCode error = msg.GetUint32(tag, out);
30 switch (error) {
31 case QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND:
32 if (presence == PRESENCE_REQUIRED) {
33 *error_details = "Missing " + QuicUtils::TagToString(tag);
34 break;
36 error = QUIC_NO_ERROR;
37 *out = default_value;
38 break;
39 case QUIC_NO_ERROR:
40 break;
41 default:
42 *error_details = "Bad " + QuicUtils::TagToString(tag);
43 break;
45 return error;
48 QuicConfigValue::QuicConfigValue(QuicTag tag,
49 QuicConfigPresence presence)
50 : tag_(tag),
51 presence_(presence) {
53 QuicConfigValue::~QuicConfigValue() {}
55 QuicNegotiableValue::QuicNegotiableValue(QuicTag tag,
56 QuicConfigPresence presence)
57 : QuicConfigValue(tag, presence),
58 negotiated_(false) {
60 QuicNegotiableValue::~QuicNegotiableValue() {}
62 QuicNegotiableUint32::QuicNegotiableUint32(QuicTag tag,
63 QuicConfigPresence presence)
64 : QuicNegotiableValue(tag, presence),
65 max_value_(0),
66 default_value_(0),
67 negotiated_value_(0) {
69 QuicNegotiableUint32::~QuicNegotiableUint32() {}
71 void QuicNegotiableUint32::set(uint32 max, uint32 default_value) {
72 DCHECK_LE(default_value, max);
73 max_value_ = max;
74 default_value_ = default_value;
77 uint32 QuicNegotiableUint32::GetUint32() const {
78 if (negotiated()) {
79 return negotiated_value_;
81 return default_value_;
84 void QuicNegotiableUint32::ToHandshakeMessage(
85 CryptoHandshakeMessage* out) const {
86 if (negotiated()) {
87 out->SetValue(tag_, negotiated_value_);
88 } else {
89 out->SetValue(tag_, max_value_);
93 QuicErrorCode QuicNegotiableUint32::ProcessPeerHello(
94 const CryptoHandshakeMessage& peer_hello,
95 HelloType hello_type,
96 string* error_details) {
97 DCHECK(!negotiated());
98 DCHECK(error_details != nullptr);
99 uint32 value;
100 QuicErrorCode error = ReadUint32(peer_hello,
101 tag_,
102 presence_,
103 default_value_,
104 &value,
105 error_details);
106 if (error != QUIC_NO_ERROR) {
107 return error;
109 if (hello_type == SERVER && value > max_value_) {
110 *error_details =
111 "Invalid value received for " + QuicUtils::TagToString(tag_);
112 return QUIC_INVALID_NEGOTIATED_VALUE;
115 set_negotiated(true);
116 negotiated_value_ = min(value, max_value_);
117 return QUIC_NO_ERROR;
120 QuicNegotiableTag::QuicNegotiableTag(QuicTag tag, QuicConfigPresence presence)
121 : QuicNegotiableValue(tag, presence),
122 negotiated_tag_(0),
123 default_value_(0) {
126 QuicNegotiableTag::~QuicNegotiableTag() {}
128 void QuicNegotiableTag::set(const QuicTagVector& possible,
129 QuicTag default_value) {
130 DCHECK(ContainsQuicTag(possible, default_value));
131 possible_values_ = possible;
132 default_value_ = default_value;
135 void QuicNegotiableTag::ToHandshakeMessage(CryptoHandshakeMessage* out) const {
136 if (negotiated()) {
137 // Because of the way we serialize and parse handshake messages we can
138 // serialize this as value and still parse it as a vector.
139 out->SetValue(tag_, negotiated_tag_);
140 } else {
141 out->SetVector(tag_, possible_values_);
145 QuicErrorCode QuicNegotiableTag::ReadVector(
146 const CryptoHandshakeMessage& msg,
147 const QuicTag** out,
148 size_t* out_length,
149 string* error_details) const {
150 DCHECK(error_details != nullptr);
151 QuicErrorCode error = msg.GetTaglist(tag_, out, out_length);
152 switch (error) {
153 case QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND:
154 if (presence_ == PRESENCE_REQUIRED) {
155 *error_details = "Missing " + QuicUtils::TagToString(tag_);
156 break;
158 error = QUIC_NO_ERROR;
159 *out_length = 1;
160 *out = &default_value_;
162 case QUIC_NO_ERROR:
163 break;
164 default:
165 *error_details = "Bad " + QuicUtils::TagToString(tag_);
166 break;
168 return error;
171 QuicErrorCode QuicNegotiableTag::ProcessPeerHello(
172 const CryptoHandshakeMessage& peer_hello,
173 HelloType hello_type,
174 string* error_details) {
175 DCHECK(!negotiated());
176 DCHECK(error_details != nullptr);
177 const QuicTag* received_tags;
178 size_t received_tags_length;
179 QuicErrorCode error = ReadVector(peer_hello, &received_tags,
180 &received_tags_length, error_details);
181 if (error != QUIC_NO_ERROR) {
182 return error;
185 if (hello_type == SERVER) {
186 if (received_tags_length != 1 ||
187 !ContainsQuicTag(possible_values_, *received_tags)) {
188 *error_details = "Invalid " + QuicUtils::TagToString(tag_);
189 return QUIC_INVALID_NEGOTIATED_VALUE;
191 negotiated_tag_ = *received_tags;
192 } else {
193 QuicTag negotiated_tag;
194 if (!QuicUtils::FindMutualTag(possible_values_,
195 received_tags,
196 received_tags_length,
197 QuicUtils::LOCAL_PRIORITY,
198 &negotiated_tag,
199 nullptr)) {
200 *error_details = "Unsupported " + QuicUtils::TagToString(tag_);
201 return QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP;
203 negotiated_tag_ = negotiated_tag;
206 set_negotiated(true);
207 return QUIC_NO_ERROR;
210 QuicFixedUint32::QuicFixedUint32(QuicTag tag, QuicConfigPresence presence)
211 : QuicConfigValue(tag, presence),
212 has_send_value_(false),
213 has_receive_value_(false) {
215 QuicFixedUint32::~QuicFixedUint32() {}
217 bool QuicFixedUint32::HasSendValue() const {
218 return has_send_value_;
221 uint32 QuicFixedUint32::GetSendValue() const {
222 LOG_IF(DFATAL, !has_send_value_)
223 << "No send value to get for tag:" << QuicUtils::TagToString(tag_);
224 return send_value_;
227 void QuicFixedUint32::SetSendValue(uint32 value) {
228 has_send_value_ = true;
229 send_value_ = value;
232 bool QuicFixedUint32::HasReceivedValue() const {
233 return has_receive_value_;
236 uint32 QuicFixedUint32::GetReceivedValue() const {
237 LOG_IF(DFATAL, !has_receive_value_)
238 << "No receive value to get for tag:" << QuicUtils::TagToString(tag_);
239 return receive_value_;
242 void QuicFixedUint32::SetReceivedValue(uint32 value) {
243 has_receive_value_ = true;
244 receive_value_ = value;
247 void QuicFixedUint32::ToHandshakeMessage(CryptoHandshakeMessage* out) const {
248 if (has_send_value_) {
249 out->SetValue(tag_, send_value_);
253 QuicErrorCode QuicFixedUint32::ProcessPeerHello(
254 const CryptoHandshakeMessage& peer_hello,
255 HelloType hello_type,
256 string* error_details) {
257 DCHECK(error_details != nullptr);
258 QuicErrorCode error = peer_hello.GetUint32(tag_, &receive_value_);
259 switch (error) {
260 case QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND:
261 if (presence_ == PRESENCE_OPTIONAL) {
262 return QUIC_NO_ERROR;
264 *error_details = "Missing " + QuicUtils::TagToString(tag_);
265 break;
266 case QUIC_NO_ERROR:
267 has_receive_value_ = true;
268 break;
269 default:
270 *error_details = "Bad " + QuicUtils::TagToString(tag_);
271 break;
273 return error;
276 QuicFixedTagVector::QuicFixedTagVector(QuicTag name,
277 QuicConfigPresence presence)
278 : QuicConfigValue(name, presence),
279 has_send_values_(false),
280 has_receive_values_(false) {
283 QuicFixedTagVector::~QuicFixedTagVector() {}
285 bool QuicFixedTagVector::HasSendValues() const {
286 return has_send_values_;
289 QuicTagVector QuicFixedTagVector::GetSendValues() const {
290 LOG_IF(DFATAL, !has_send_values_)
291 << "No send values to get for tag:" << QuicUtils::TagToString(tag_);
292 return send_values_;
295 void QuicFixedTagVector::SetSendValues(const QuicTagVector& values) {
296 has_send_values_ = true;
297 send_values_ = values;
300 bool QuicFixedTagVector::HasReceivedValues() const {
301 return has_receive_values_;
304 QuicTagVector QuicFixedTagVector::GetReceivedValues() const {
305 LOG_IF(DFATAL, !has_receive_values_)
306 << "No receive value to get for tag:" << QuicUtils::TagToString(tag_);
307 return receive_values_;
310 void QuicFixedTagVector::SetReceivedValues(const QuicTagVector& values) {
311 has_receive_values_ = true;
312 receive_values_ = values;
315 void QuicFixedTagVector::ToHandshakeMessage(CryptoHandshakeMessage* out) const {
316 if (has_send_values_) {
317 out->SetVector(tag_, send_values_);
321 QuicErrorCode QuicFixedTagVector::ProcessPeerHello(
322 const CryptoHandshakeMessage& peer_hello,
323 HelloType hello_type,
324 string* error_details) {
325 DCHECK(error_details != nullptr);
326 const QuicTag* received_tags;
327 size_t received_tags_length;
328 QuicErrorCode error =
329 peer_hello.GetTaglist(tag_, &received_tags, &received_tags_length);
330 switch (error) {
331 case QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND:
332 if (presence_ == PRESENCE_OPTIONAL) {
333 return QUIC_NO_ERROR;
335 *error_details = "Missing " + QuicUtils::TagToString(tag_);
336 break;
337 case QUIC_NO_ERROR:
338 DVLOG(1) << "Received Connection Option tags from receiver.";
339 has_receive_values_ = true;
340 for (size_t i = 0; i < received_tags_length; ++i) {
341 receive_values_.push_back(received_tags[i]);
343 break;
344 default:
345 *error_details = "Bad " + QuicUtils::TagToString(tag_);
346 break;
348 return error;
351 QuicConfig::QuicConfig()
352 : max_time_before_crypto_handshake_(QuicTime::Delta::Zero()),
353 max_idle_time_before_crypto_handshake_(QuicTime::Delta::Zero()),
354 max_undecryptable_packets_(0),
355 connection_options_(kCOPT, PRESENCE_OPTIONAL),
356 idle_connection_state_lifetime_seconds_(kICSL, PRESENCE_REQUIRED),
357 silent_close_(kSCLS, PRESENCE_OPTIONAL),
358 max_streams_per_connection_(kMSPC, PRESENCE_REQUIRED),
359 bytes_for_connection_id_(kTCID, PRESENCE_OPTIONAL),
360 initial_round_trip_time_us_(kIRTT, PRESENCE_OPTIONAL),
361 initial_stream_flow_control_window_bytes_(kSFCW, PRESENCE_OPTIONAL),
362 initial_session_flow_control_window_bytes_(kCFCW, PRESENCE_OPTIONAL),
363 socket_receive_buffer_(kSRBF, PRESENCE_OPTIONAL) {
364 SetDefaults();
367 QuicConfig::~QuicConfig() {}
369 void QuicConfig::SetConnectionOptionsToSend(
370 const QuicTagVector& connection_options) {
371 connection_options_.SetSendValues(connection_options);
374 bool QuicConfig::HasReceivedConnectionOptions() const {
375 return connection_options_.HasReceivedValues();
378 QuicTagVector QuicConfig::ReceivedConnectionOptions() const {
379 return connection_options_.GetReceivedValues();
382 bool QuicConfig::HasSendConnectionOptions() const {
383 return connection_options_.HasSendValues();
386 QuicTagVector QuicConfig::SendConnectionOptions() const {
387 return connection_options_.GetSendValues();
390 bool QuicConfig::HasClientSentConnectionOption(QuicTag tag,
391 Perspective perspective) const {
392 if (perspective == Perspective::IS_SERVER) {
393 if (HasReceivedConnectionOptions() &&
394 ContainsQuicTag(ReceivedConnectionOptions(), tag)) {
395 return true;
397 } else if (HasSendConnectionOptions() &&
398 ContainsQuicTag(SendConnectionOptions(), tag)) {
399 return true;
401 return false;
404 void QuicConfig::SetIdleConnectionStateLifetime(
405 QuicTime::Delta max_idle_connection_state_lifetime,
406 QuicTime::Delta default_idle_conection_state_lifetime) {
407 idle_connection_state_lifetime_seconds_.set(
408 static_cast<uint32>(max_idle_connection_state_lifetime.ToSeconds()),
409 static_cast<uint32>(default_idle_conection_state_lifetime.ToSeconds()));
412 QuicTime::Delta QuicConfig::IdleConnectionStateLifetime() const {
413 return QuicTime::Delta::FromSeconds(
414 idle_connection_state_lifetime_seconds_.GetUint32());
417 // TODO(ianswett) Use this for silent close on mobile, or delete.
418 void QuicConfig::SetSilentClose(bool silent_close) {
419 silent_close_.set(silent_close ? 1 : 0, silent_close ? 1 : 0);
422 bool QuicConfig::SilentClose() const {
423 return silent_close_.GetUint32() > 0;
426 void QuicConfig::SetMaxStreamsPerConnection(size_t max_streams,
427 size_t default_streams) {
428 max_streams_per_connection_.set(max_streams, default_streams);
431 uint32 QuicConfig::MaxStreamsPerConnection() const {
432 return max_streams_per_connection_.GetUint32();
435 bool QuicConfig::HasSetBytesForConnectionIdToSend() const {
436 return bytes_for_connection_id_.HasSendValue();
439 void QuicConfig::SetBytesForConnectionIdToSend(uint32 bytes) {
440 bytes_for_connection_id_.SetSendValue(bytes);
443 bool QuicConfig::HasReceivedBytesForConnectionId() const {
444 return bytes_for_connection_id_.HasReceivedValue();
447 uint32 QuicConfig::ReceivedBytesForConnectionId() const {
448 return bytes_for_connection_id_.GetReceivedValue();
451 void QuicConfig::SetInitialRoundTripTimeUsToSend(uint32 rtt) {
452 initial_round_trip_time_us_.SetSendValue(rtt);
455 bool QuicConfig::HasReceivedInitialRoundTripTimeUs() const {
456 return initial_round_trip_time_us_.HasReceivedValue();
459 uint32 QuicConfig::ReceivedInitialRoundTripTimeUs() const {
460 return initial_round_trip_time_us_.GetReceivedValue();
463 bool QuicConfig::HasInitialRoundTripTimeUsToSend() const {
464 return initial_round_trip_time_us_.HasSendValue();
467 uint32 QuicConfig::GetInitialRoundTripTimeUsToSend() const {
468 return initial_round_trip_time_us_.GetSendValue();
471 void QuicConfig::SetInitialStreamFlowControlWindowToSend(uint32 window_bytes) {
472 if (window_bytes < kMinimumFlowControlSendWindow) {
473 LOG(DFATAL) << "Initial stream flow control receive window ("
474 << window_bytes << ") cannot be set lower than default ("
475 << kMinimumFlowControlSendWindow << ").";
476 window_bytes = kMinimumFlowControlSendWindow;
478 initial_stream_flow_control_window_bytes_.SetSendValue(window_bytes);
481 uint32 QuicConfig::GetInitialStreamFlowControlWindowToSend() const {
482 return initial_stream_flow_control_window_bytes_.GetSendValue();
485 bool QuicConfig::HasReceivedInitialStreamFlowControlWindowBytes() const {
486 return initial_stream_flow_control_window_bytes_.HasReceivedValue();
489 uint32 QuicConfig::ReceivedInitialStreamFlowControlWindowBytes() const {
490 return initial_stream_flow_control_window_bytes_.GetReceivedValue();
493 void QuicConfig::SetInitialSessionFlowControlWindowToSend(uint32 window_bytes) {
494 if (window_bytes < kMinimumFlowControlSendWindow) {
495 LOG(DFATAL) << "Initial session flow control receive window ("
496 << window_bytes << ") cannot be set lower than default ("
497 << kMinimumFlowControlSendWindow << ").";
498 window_bytes = kMinimumFlowControlSendWindow;
500 initial_session_flow_control_window_bytes_.SetSendValue(window_bytes);
503 uint32 QuicConfig::GetInitialSessionFlowControlWindowToSend() const {
504 return initial_session_flow_control_window_bytes_.GetSendValue();
507 bool QuicConfig::HasReceivedInitialSessionFlowControlWindowBytes() const {
508 return initial_session_flow_control_window_bytes_.HasReceivedValue();
511 uint32 QuicConfig::ReceivedInitialSessionFlowControlWindowBytes() const {
512 return initial_session_flow_control_window_bytes_.GetReceivedValue();
515 void QuicConfig::SetSocketReceiveBufferToSend(uint32 tcp_receive_window) {
516 socket_receive_buffer_.SetSendValue(tcp_receive_window);
519 bool QuicConfig::HasReceivedSocketReceiveBuffer() const {
520 return socket_receive_buffer_.HasReceivedValue();
523 uint32 QuicConfig::ReceivedSocketReceiveBuffer() const {
524 return socket_receive_buffer_.GetReceivedValue();
527 bool QuicConfig::negotiated() const {
528 // TODO(ianswett): Add the negotiated parameters once and iterate over all
529 // of them in negotiated, ToHandshakeMessage, ProcessClientHello, and
530 // ProcessServerHello.
531 return idle_connection_state_lifetime_seconds_.negotiated() &&
532 max_streams_per_connection_.negotiated();
535 void QuicConfig::SetDefaults() {
536 idle_connection_state_lifetime_seconds_.set(kMaximumIdleTimeoutSecs,
537 kDefaultIdleTimeoutSecs);
538 silent_close_.set(1, 0);
539 SetMaxStreamsPerConnection(kDefaultMaxStreamsPerConnection,
540 kDefaultMaxStreamsPerConnection);
541 max_time_before_crypto_handshake_ =
542 QuicTime::Delta::FromSeconds(kMaxTimeForCryptoHandshakeSecs);
543 max_idle_time_before_crypto_handshake_ =
544 QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs);
545 max_undecryptable_packets_ = kDefaultMaxUndecryptablePackets;
547 SetInitialStreamFlowControlWindowToSend(kMinimumFlowControlSendWindow);
548 SetInitialSessionFlowControlWindowToSend(kMinimumFlowControlSendWindow);
551 void QuicConfig::ToHandshakeMessage(CryptoHandshakeMessage* out) const {
552 idle_connection_state_lifetime_seconds_.ToHandshakeMessage(out);
553 silent_close_.ToHandshakeMessage(out);
554 max_streams_per_connection_.ToHandshakeMessage(out);
555 bytes_for_connection_id_.ToHandshakeMessage(out);
556 initial_round_trip_time_us_.ToHandshakeMessage(out);
557 initial_stream_flow_control_window_bytes_.ToHandshakeMessage(out);
558 initial_session_flow_control_window_bytes_.ToHandshakeMessage(out);
559 socket_receive_buffer_.ToHandshakeMessage(out);
560 connection_options_.ToHandshakeMessage(out);
563 QuicErrorCode QuicConfig::ProcessPeerHello(
564 const CryptoHandshakeMessage& peer_hello,
565 HelloType hello_type,
566 string* error_details) {
567 DCHECK(error_details != nullptr);
569 QuicErrorCode error = QUIC_NO_ERROR;
570 if (error == QUIC_NO_ERROR) {
571 error = idle_connection_state_lifetime_seconds_.ProcessPeerHello(
572 peer_hello, hello_type, error_details);
574 if (error == QUIC_NO_ERROR) {
575 error =
576 silent_close_.ProcessPeerHello(peer_hello, hello_type, error_details);
578 if (error == QUIC_NO_ERROR) {
579 error = max_streams_per_connection_.ProcessPeerHello(
580 peer_hello, hello_type, error_details);
582 if (error == QUIC_NO_ERROR) {
583 error = bytes_for_connection_id_.ProcessPeerHello(
584 peer_hello, hello_type, error_details);
586 if (error == QUIC_NO_ERROR) {
587 error = initial_round_trip_time_us_.ProcessPeerHello(
588 peer_hello, hello_type, error_details);
590 if (error == QUIC_NO_ERROR) {
591 error = initial_stream_flow_control_window_bytes_.ProcessPeerHello(
592 peer_hello, hello_type, error_details);
594 if (error == QUIC_NO_ERROR) {
595 error = initial_session_flow_control_window_bytes_.ProcessPeerHello(
596 peer_hello, hello_type, error_details);
598 if (error == QUIC_NO_ERROR) {
599 error = socket_receive_buffer_.ProcessPeerHello(
600 peer_hello, hello_type, error_details);
602 if (error == QUIC_NO_ERROR) {
603 error = connection_options_.ProcessPeerHello(
604 peer_hello, hello_type, error_details);
606 return error;
609 } // namespace net