ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / net / quic / quic_config.cc
blobf3e93aa3ed72a5ad42b16d934b93f1312a4e6d53
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;
49 QuicConfigValue::QuicConfigValue(QuicTag tag,
50 QuicConfigPresence presence)
51 : tag_(tag),
52 presence_(presence) {
54 QuicConfigValue::~QuicConfigValue() {}
56 QuicNegotiableValue::QuicNegotiableValue(QuicTag tag,
57 QuicConfigPresence presence)
58 : QuicConfigValue(tag, presence),
59 negotiated_(false) {
61 QuicNegotiableValue::~QuicNegotiableValue() {}
63 QuicNegotiableUint32::QuicNegotiableUint32(QuicTag tag,
64 QuicConfigPresence presence)
65 : QuicNegotiableValue(tag, presence),
66 max_value_(0),
67 default_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 QuicTag QuicNegotiableTag::GetTag() const {
136 if (negotiated()) {
137 return negotiated_tag_;
139 return default_value_;
142 void QuicNegotiableTag::ToHandshakeMessage(CryptoHandshakeMessage* out) const {
143 if (negotiated()) {
144 // Because of the way we serialize and parse handshake messages we can
145 // serialize this as value and still parse it as a vector.
146 out->SetValue(tag_, negotiated_tag_);
147 } else {
148 out->SetVector(tag_, possible_values_);
152 QuicErrorCode QuicNegotiableTag::ReadVector(
153 const CryptoHandshakeMessage& msg,
154 const QuicTag** out,
155 size_t* out_length,
156 string* error_details) const {
157 DCHECK(error_details != nullptr);
158 QuicErrorCode error = msg.GetTaglist(tag_, out, out_length);
159 switch (error) {
160 case QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND:
161 if (presence_ == PRESENCE_REQUIRED) {
162 *error_details = "Missing " + QuicUtils::TagToString(tag_);
163 break;
165 error = QUIC_NO_ERROR;
166 *out_length = 1;
167 *out = &default_value_;
169 case QUIC_NO_ERROR:
170 break;
171 default:
172 *error_details = "Bad " + QuicUtils::TagToString(tag_);
173 break;
175 return error;
178 QuicErrorCode QuicNegotiableTag::ProcessPeerHello(
179 const CryptoHandshakeMessage& peer_hello,
180 HelloType hello_type,
181 string* error_details) {
182 DCHECK(!negotiated());
183 DCHECK(error_details != nullptr);
184 const QuicTag* received_tags;
185 size_t received_tags_length;
186 QuicErrorCode error = ReadVector(peer_hello, &received_tags,
187 &received_tags_length, error_details);
188 if (error != QUIC_NO_ERROR) {
189 return error;
192 if (hello_type == SERVER) {
193 if (received_tags_length != 1 ||
194 !ContainsQuicTag(possible_values_, *received_tags)) {
195 *error_details = "Invalid " + QuicUtils::TagToString(tag_);
196 return QUIC_INVALID_NEGOTIATED_VALUE;
198 negotiated_tag_ = *received_tags;
199 } else {
200 QuicTag negotiated_tag;
201 if (!QuicUtils::FindMutualTag(possible_values_,
202 received_tags,
203 received_tags_length,
204 QuicUtils::LOCAL_PRIORITY,
205 &negotiated_tag,
206 nullptr)) {
207 *error_details = "Unsupported " + QuicUtils::TagToString(tag_);
208 return QUIC_CRYPTO_MESSAGE_PARAMETER_NO_OVERLAP;
210 negotiated_tag_ = negotiated_tag;
213 set_negotiated(true);
214 return QUIC_NO_ERROR;
217 QuicFixedUint32::QuicFixedUint32(QuicTag tag, QuicConfigPresence presence)
218 : QuicConfigValue(tag, presence),
219 has_send_value_(false),
220 has_receive_value_(false) {
222 QuicFixedUint32::~QuicFixedUint32() {}
224 bool QuicFixedUint32::HasSendValue() const {
225 return has_send_value_;
228 uint32 QuicFixedUint32::GetSendValue() const {
229 LOG_IF(DFATAL, !has_send_value_)
230 << "No send value to get for tag:" << QuicUtils::TagToString(tag_);
231 return send_value_;
234 void QuicFixedUint32::SetSendValue(uint32 value) {
235 has_send_value_ = true;
236 send_value_ = value;
239 bool QuicFixedUint32::HasReceivedValue() const {
240 return has_receive_value_;
243 uint32 QuicFixedUint32::GetReceivedValue() const {
244 LOG_IF(DFATAL, !has_receive_value_)
245 << "No receive value to get for tag:" << QuicUtils::TagToString(tag_);
246 return receive_value_;
249 void QuicFixedUint32::SetReceivedValue(uint32 value) {
250 has_receive_value_ = true;
251 receive_value_ = value;
254 void QuicFixedUint32::ToHandshakeMessage(CryptoHandshakeMessage* out) const {
255 if (has_send_value_) {
256 out->SetValue(tag_, send_value_);
260 QuicErrorCode QuicFixedUint32::ProcessPeerHello(
261 const CryptoHandshakeMessage& peer_hello,
262 HelloType hello_type,
263 string* error_details) {
264 DCHECK(error_details != nullptr);
265 QuicErrorCode error = peer_hello.GetUint32(tag_, &receive_value_);
266 switch (error) {
267 case QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND:
268 if (presence_ == PRESENCE_OPTIONAL) {
269 return QUIC_NO_ERROR;
271 *error_details = "Missing " + QuicUtils::TagToString(tag_);
272 break;
273 case QUIC_NO_ERROR:
274 has_receive_value_ = true;
275 break;
276 default:
277 *error_details = "Bad " + QuicUtils::TagToString(tag_);
278 break;
280 return error;
283 QuicFixedTagVector::QuicFixedTagVector(QuicTag name,
284 QuicConfigPresence presence)
285 : QuicConfigValue(name, presence),
286 has_send_values_(false),
287 has_receive_values_(false) {
290 QuicFixedTagVector::~QuicFixedTagVector() {}
292 bool QuicFixedTagVector::HasSendValues() const {
293 return has_send_values_;
296 QuicTagVector QuicFixedTagVector::GetSendValues() const {
297 LOG_IF(DFATAL, !has_send_values_)
298 << "No send values to get for tag:" << QuicUtils::TagToString(tag_);
299 return send_values_;
302 void QuicFixedTagVector::SetSendValues(const QuicTagVector& values) {
303 has_send_values_ = true;
304 send_values_ = values;
307 bool QuicFixedTagVector::HasReceivedValues() const {
308 return has_receive_values_;
311 QuicTagVector QuicFixedTagVector::GetReceivedValues() const {
312 LOG_IF(DFATAL, !has_receive_values_)
313 << "No receive value to get for tag:" << QuicUtils::TagToString(tag_);
314 return receive_values_;
317 void QuicFixedTagVector::SetReceivedValues(const QuicTagVector& values) {
318 has_receive_values_ = true;
319 receive_values_ = values;
322 void QuicFixedTagVector::ToHandshakeMessage(CryptoHandshakeMessage* out) const {
323 if (has_send_values_) {
324 out->SetVector(tag_, send_values_);
328 QuicErrorCode QuicFixedTagVector::ProcessPeerHello(
329 const CryptoHandshakeMessage& peer_hello,
330 HelloType hello_type,
331 string* error_details) {
332 DCHECK(error_details != nullptr);
333 const QuicTag* received_tags;
334 size_t received_tags_length;
335 QuicErrorCode error =
336 peer_hello.GetTaglist(tag_, &received_tags, &received_tags_length);
337 switch (error) {
338 case QUIC_CRYPTO_MESSAGE_PARAMETER_NOT_FOUND:
339 if (presence_ == PRESENCE_OPTIONAL) {
340 return QUIC_NO_ERROR;
342 *error_details = "Missing " + QuicUtils::TagToString(tag_);
343 break;
344 case QUIC_NO_ERROR:
345 DVLOG(1) << "Received Connection Option tags from receiver.";
346 has_receive_values_ = true;
347 for (size_t i = 0; i < received_tags_length; ++i) {
348 receive_values_.push_back(received_tags[i]);
350 break;
351 default:
352 *error_details = "Bad " + QuicUtils::TagToString(tag_);
353 break;
355 return error;
358 QuicConfig::QuicConfig()
359 : max_time_before_crypto_handshake_(QuicTime::Delta::Zero()),
360 max_idle_time_before_crypto_handshake_(QuicTime::Delta::Zero()),
361 max_undecryptable_packets_(0),
362 congestion_feedback_(kCGST, PRESENCE_OPTIONAL),
363 connection_options_(kCOPT, PRESENCE_OPTIONAL),
364 idle_connection_state_lifetime_seconds_(kICSL, PRESENCE_REQUIRED),
365 silent_close_(kSCLS, PRESENCE_OPTIONAL),
366 max_streams_per_connection_(kMSPC, PRESENCE_REQUIRED),
367 bytes_for_connection_id_(kTCID, PRESENCE_OPTIONAL),
368 initial_round_trip_time_us_(kIRTT, PRESENCE_OPTIONAL),
369 initial_stream_flow_control_window_bytes_(kSFCW, PRESENCE_OPTIONAL),
370 initial_session_flow_control_window_bytes_(kCFCW, PRESENCE_OPTIONAL),
371 socket_receive_buffer_(kSRBF, PRESENCE_OPTIONAL) {
372 SetDefaults();
375 QuicConfig::~QuicConfig() {}
377 void QuicConfig::SetConnectionOptionsToSend(
378 const QuicTagVector& connection_options) {
379 connection_options_.SetSendValues(connection_options);
382 bool QuicConfig::HasReceivedConnectionOptions() const {
383 return connection_options_.HasReceivedValues();
386 QuicTagVector QuicConfig::ReceivedConnectionOptions() const {
387 return connection_options_.GetReceivedValues();
390 bool QuicConfig::HasSendConnectionOptions() const {
391 return connection_options_.HasSendValues();
394 QuicTagVector QuicConfig::SendConnectionOptions() const {
395 return connection_options_.GetSendValues();
398 void QuicConfig::SetIdleConnectionStateLifetime(
399 QuicTime::Delta max_idle_connection_state_lifetime,
400 QuicTime::Delta default_idle_conection_state_lifetime) {
401 idle_connection_state_lifetime_seconds_.set(
402 static_cast<uint32>(max_idle_connection_state_lifetime.ToSeconds()),
403 static_cast<uint32>(default_idle_conection_state_lifetime.ToSeconds()));
406 QuicTime::Delta QuicConfig::IdleConnectionStateLifetime() const {
407 return QuicTime::Delta::FromSeconds(
408 idle_connection_state_lifetime_seconds_.GetUint32());
411 // TODO(ianswett) Use this for silent close on mobile, or delete.
412 void QuicConfig::SetSilentClose(bool silent_close) {
413 silent_close_.set(silent_close ? 1 : 0, silent_close ? 1 : 0);
416 bool QuicConfig::SilentClose() const {
417 return silent_close_.GetUint32() > 0;
420 void QuicConfig::SetMaxStreamsPerConnection(size_t max_streams,
421 size_t default_streams) {
422 max_streams_per_connection_.set(max_streams, default_streams);
425 uint32 QuicConfig::MaxStreamsPerConnection() const {
426 return max_streams_per_connection_.GetUint32();
429 bool QuicConfig::HasSetBytesForConnectionIdToSend() const {
430 return bytes_for_connection_id_.HasSendValue();
433 void QuicConfig::SetBytesForConnectionIdToSend(uint32 bytes) {
434 bytes_for_connection_id_.SetSendValue(bytes);
437 bool QuicConfig::HasReceivedBytesForConnectionId() const {
438 return bytes_for_connection_id_.HasReceivedValue();
441 uint32 QuicConfig::ReceivedBytesForConnectionId() const {
442 return bytes_for_connection_id_.GetReceivedValue();
445 void QuicConfig::SetInitialRoundTripTimeUsToSend(uint32 rtt) {
446 initial_round_trip_time_us_.SetSendValue(rtt);
449 bool QuicConfig::HasReceivedInitialRoundTripTimeUs() const {
450 return initial_round_trip_time_us_.HasReceivedValue();
453 uint32 QuicConfig::ReceivedInitialRoundTripTimeUs() const {
454 return initial_round_trip_time_us_.GetReceivedValue();
457 bool QuicConfig::HasInitialRoundTripTimeUsToSend() const {
458 return initial_round_trip_time_us_.HasSendValue();
461 uint32 QuicConfig::GetInitialRoundTripTimeUsToSend() const {
462 return initial_round_trip_time_us_.GetSendValue();
465 void QuicConfig::SetInitialStreamFlowControlWindowToSend(uint32 window_bytes) {
466 if (window_bytes < kMinimumFlowControlSendWindow) {
467 LOG(DFATAL) << "Initial stream flow control receive window ("
468 << window_bytes << ") cannot be set lower than default ("
469 << kMinimumFlowControlSendWindow << ").";
470 window_bytes = kMinimumFlowControlSendWindow;
472 initial_stream_flow_control_window_bytes_.SetSendValue(window_bytes);
475 uint32 QuicConfig::GetInitialStreamFlowControlWindowToSend() const {
476 return initial_stream_flow_control_window_bytes_.GetSendValue();
479 bool QuicConfig::HasReceivedInitialStreamFlowControlWindowBytes() const {
480 return initial_stream_flow_control_window_bytes_.HasReceivedValue();
483 uint32 QuicConfig::ReceivedInitialStreamFlowControlWindowBytes() const {
484 return initial_stream_flow_control_window_bytes_.GetReceivedValue();
487 void QuicConfig::SetInitialSessionFlowControlWindowToSend(uint32 window_bytes) {
488 if (window_bytes < kMinimumFlowControlSendWindow) {
489 LOG(DFATAL) << "Initial session flow control receive window ("
490 << window_bytes << ") cannot be set lower than default ("
491 << kMinimumFlowControlSendWindow << ").";
492 window_bytes = kMinimumFlowControlSendWindow;
494 initial_session_flow_control_window_bytes_.SetSendValue(window_bytes);
497 uint32 QuicConfig::GetInitialSessionFlowControlWindowToSend() const {
498 return initial_session_flow_control_window_bytes_.GetSendValue();
501 bool QuicConfig::HasReceivedInitialSessionFlowControlWindowBytes() const {
502 return initial_session_flow_control_window_bytes_.HasReceivedValue();
505 uint32 QuicConfig::ReceivedInitialSessionFlowControlWindowBytes() const {
506 return initial_session_flow_control_window_bytes_.GetReceivedValue();
509 void QuicConfig::SetSocketReceiveBufferToSend(uint32 tcp_receive_window) {
510 socket_receive_buffer_.SetSendValue(tcp_receive_window);
513 bool QuicConfig::HasReceivedSocketReceiveBuffer() const {
514 return socket_receive_buffer_.HasReceivedValue();
517 uint32 QuicConfig::ReceivedSocketReceiveBuffer() const {
518 return socket_receive_buffer_.GetReceivedValue();
521 bool QuicConfig::negotiated() const {
522 // TODO(ianswett): Add the negotiated parameters once and iterate over all
523 // of them in negotiated, ToHandshakeMessage, ProcessClientHello, and
524 // ProcessServerHello.
525 return idle_connection_state_lifetime_seconds_.negotiated() &&
526 max_streams_per_connection_.negotiated();
529 void QuicConfig::SetDefaults() {
530 QuicTagVector congestion_feedback;
531 // TODO(alyssar) stop sending this once QUIC_VERSION_23 is sunset.
532 // This field was required until version 22 was removed but by the time
533 // QUIC_VERSION_23 is sunset, no users of QUIC_VERSION_24 should be expecting
534 // it.
535 congestion_feedback.push_back(kQBIC);
536 congestion_feedback_.set(congestion_feedback, kQBIC);
537 idle_connection_state_lifetime_seconds_.set(kMaximumIdleTimeoutSecs,
538 kDefaultIdleTimeoutSecs);
539 silent_close_.set(1, 0);
540 SetMaxStreamsPerConnection(kDefaultMaxStreamsPerConnection,
541 kDefaultMaxStreamsPerConnection);
542 max_time_before_crypto_handshake_ =
543 QuicTime::Delta::FromSeconds(kMaxTimeForCryptoHandshakeSecs);
544 max_idle_time_before_crypto_handshake_ =
545 QuicTime::Delta::FromSeconds(kInitialIdleTimeoutSecs);
546 max_undecryptable_packets_ = kDefaultMaxUndecryptablePackets;
548 SetInitialStreamFlowControlWindowToSend(kMinimumFlowControlSendWindow);
549 SetInitialSessionFlowControlWindowToSend(kMinimumFlowControlSendWindow);
552 void QuicConfig::ToHandshakeMessage(CryptoHandshakeMessage* out) const {
553 congestion_feedback_.ToHandshakeMessage(out);
554 idle_connection_state_lifetime_seconds_.ToHandshakeMessage(out);
555 silent_close_.ToHandshakeMessage(out);
556 max_streams_per_connection_.ToHandshakeMessage(out);
557 bytes_for_connection_id_.ToHandshakeMessage(out);
558 initial_round_trip_time_us_.ToHandshakeMessage(out);
559 initial_stream_flow_control_window_bytes_.ToHandshakeMessage(out);
560 initial_session_flow_control_window_bytes_.ToHandshakeMessage(out);
561 socket_receive_buffer_.ToHandshakeMessage(out);
562 connection_options_.ToHandshakeMessage(out);
565 QuicErrorCode QuicConfig::ProcessPeerHello(
566 const CryptoHandshakeMessage& peer_hello,
567 HelloType hello_type,
568 string* error_details) {
569 DCHECK(error_details != nullptr);
571 QuicErrorCode error = QUIC_NO_ERROR;
572 if (error == QUIC_NO_ERROR) {
573 error = idle_connection_state_lifetime_seconds_.ProcessPeerHello(
574 peer_hello, hello_type, error_details);
576 if (error == QUIC_NO_ERROR) {
577 error =
578 silent_close_.ProcessPeerHello(peer_hello, hello_type, error_details);
580 if (error == QUIC_NO_ERROR) {
581 error = max_streams_per_connection_.ProcessPeerHello(
582 peer_hello, hello_type, error_details);
584 if (error == QUIC_NO_ERROR) {
585 error = bytes_for_connection_id_.ProcessPeerHello(
586 peer_hello, hello_type, error_details);
588 if (error == QUIC_NO_ERROR) {
589 error = initial_round_trip_time_us_.ProcessPeerHello(
590 peer_hello, hello_type, error_details);
592 if (error == QUIC_NO_ERROR) {
593 error = initial_stream_flow_control_window_bytes_.ProcessPeerHello(
594 peer_hello, hello_type, error_details);
596 if (error == QUIC_NO_ERROR) {
597 error = initial_session_flow_control_window_bytes_.ProcessPeerHello(
598 peer_hello, hello_type, error_details);
600 if (error == QUIC_NO_ERROR) {
601 error = socket_receive_buffer_.ProcessPeerHello(
602 peer_hello, hello_type, error_details);
604 if (error == QUIC_NO_ERROR) {
605 error = connection_options_.ProcessPeerHello(
606 peer_hello, hello_type, error_details);
608 return error;
611 } // namespace net