| 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_connection.h" | 5 #include "net/quic/quic_connection.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 #include <sys/types.h> | 8 #include <sys/types.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 const PacketWriterFactory& writer_factory, | 189 const PacketWriterFactory& writer_factory, |
| 190 bool owns_writer, | 190 bool owns_writer, |
| 191 bool is_server, | 191 bool is_server, |
| 192 const QuicVersionVector& supported_versions) | 192 const QuicVersionVector& supported_versions) |
| 193 : framer_(supported_versions, helper->GetClock()->ApproximateNow(), | 193 : framer_(supported_versions, helper->GetClock()->ApproximateNow(), |
| 194 is_server), | 194 is_server), |
| 195 helper_(helper), | 195 helper_(helper), |
| 196 writer_(writer_factory.Create(this)), | 196 writer_(writer_factory.Create(this)), |
| 197 owns_writer_(owns_writer), | 197 owns_writer_(owns_writer), |
| 198 encryption_level_(ENCRYPTION_NONE), | 198 encryption_level_(ENCRYPTION_NONE), |
| 199 has_forward_secure_encrypter_(false), |
| 200 first_required_forward_secure_packet_(0), |
| 199 clock_(helper->GetClock()), | 201 clock_(helper->GetClock()), |
| 200 random_generator_(helper->GetRandomGenerator()), | 202 random_generator_(helper->GetRandomGenerator()), |
| 201 connection_id_(connection_id), | 203 connection_id_(connection_id), |
| 202 peer_address_(address), | 204 peer_address_(address), |
| 203 migrating_peer_port_(0), | 205 migrating_peer_port_(0), |
| 204 last_packet_decrypted_(false), | 206 last_packet_decrypted_(false), |
| 205 last_packet_revived_(false), | 207 last_packet_revived_(false), |
| 206 last_size_(0), | 208 last_size_(0), |
| 207 last_decrypted_packet_level_(ENCRYPTION_NONE), | 209 last_decrypted_packet_level_(ENCRYPTION_NONE), |
| 208 largest_seen_packet_with_ack_(0), | 210 largest_seen_packet_with_ack_(0), |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 278 sent_packet_manager_.SetFromConfig(config); | 280 sent_packet_manager_.SetFromConfig(config); |
| 279 if (FLAGS_allow_truncated_connection_ids_for_quic && | 281 if (FLAGS_allow_truncated_connection_ids_for_quic && |
| 280 config.HasReceivedBytesForConnectionId() && | 282 config.HasReceivedBytesForConnectionId() && |
| 281 can_truncate_connection_ids_) { | 283 can_truncate_connection_ids_) { |
| 282 packet_generator_.SetConnectionIdLength( | 284 packet_generator_.SetConnectionIdLength( |
| 283 config.ReceivedBytesForConnectionId()); | 285 config.ReceivedBytesForConnectionId()); |
| 284 } | 286 } |
| 285 max_undecryptable_packets_ = config.max_undecryptable_packets(); | 287 max_undecryptable_packets_ = config.max_undecryptable_packets(); |
| 286 } | 288 } |
| 287 | 289 |
| 290 void QuicConnection::SetNumOpenStreams(size_t num_streams) { |
| 291 sent_packet_manager_.SetNumOpenStreams(num_streams); |
| 292 } |
| 293 |
| 288 bool QuicConnection::SelectMutualVersion( | 294 bool QuicConnection::SelectMutualVersion( |
| 289 const QuicVersionVector& available_versions) { | 295 const QuicVersionVector& available_versions) { |
| 290 // Try to find the highest mutual version by iterating over supported | 296 // Try to find the highest mutual version by iterating over supported |
| 291 // versions, starting with the highest, and breaking out of the loop once we | 297 // versions, starting with the highest, and breaking out of the loop once we |
| 292 // find a matching version in the provided available_versions vector. | 298 // find a matching version in the provided available_versions vector. |
| 293 const QuicVersionVector& supported_versions = framer_.supported_versions(); | 299 const QuicVersionVector& supported_versions = framer_.supported_versions(); |
| 294 for (size_t i = 0; i < supported_versions.size(); ++i) { | 300 for (size_t i = 0; i < supported_versions.size(); ++i) { |
| 295 const QuicVersion& version = supported_versions[i]; | 301 const QuicVersion& version = supported_versions[i]; |
| 296 if (std::find(available_versions.begin(), available_versions.end(), | 302 if (std::find(available_versions.begin(), available_versions.end(), |
| 297 version) != available_versions.end()) { | 303 version) != available_versions.end()) { |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 return true; | 456 return true; |
| 451 } | 457 } |
| 452 | 458 |
| 453 bool QuicConnection::OnUnauthenticatedHeader(const QuicPacketHeader& header) { | 459 bool QuicConnection::OnUnauthenticatedHeader(const QuicPacketHeader& header) { |
| 454 return true; | 460 return true; |
| 455 } | 461 } |
| 456 | 462 |
| 457 void QuicConnection::OnDecryptedPacket(EncryptionLevel level) { | 463 void QuicConnection::OnDecryptedPacket(EncryptionLevel level) { |
| 458 last_decrypted_packet_level_ = level; | 464 last_decrypted_packet_level_ = level; |
| 459 last_packet_decrypted_ = true; | 465 last_packet_decrypted_ = true; |
| 466 // If this packet was foward-secure encrypted and the forward-secure encrypter |
| 467 // is not being used, start using it. |
| 468 if (FLAGS_enable_quic_delay_forward_security && |
| 469 encryption_level_ != ENCRYPTION_FORWARD_SECURE && |
| 470 has_forward_secure_encrypter_ && |
| 471 level == ENCRYPTION_FORWARD_SECURE) { |
| 472 SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 473 } |
| 460 } | 474 } |
| 461 | 475 |
| 462 bool QuicConnection::OnPacketHeader(const QuicPacketHeader& header) { | 476 bool QuicConnection::OnPacketHeader(const QuicPacketHeader& header) { |
| 463 if (debug_visitor_.get() != nullptr) { | 477 if (debug_visitor_.get() != nullptr) { |
| 464 debug_visitor_->OnPacketHeader(header); | 478 debug_visitor_->OnPacketHeader(header); |
| 465 } | 479 } |
| 466 | 480 |
| 467 if (!ProcessValidatedPacket()) { | 481 if (!ProcessValidatedPacket()) { |
| 468 return false; | 482 return false; |
| 469 } | 483 } |
| (...skipping 1076 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1546 | 1560 |
| 1547 void QuicConnection::OnWriteError(int error_code) { | 1561 void QuicConnection::OnWriteError(int error_code) { |
| 1548 DVLOG(1) << ENDPOINT << "Write failed with error: " << error_code | 1562 DVLOG(1) << ENDPOINT << "Write failed with error: " << error_code |
| 1549 << " (" << ErrorToString(error_code) << ")"; | 1563 << " (" << ErrorToString(error_code) << ")"; |
| 1550 // We can't send an error as the socket is presumably borked. | 1564 // We can't send an error as the socket is presumably borked. |
| 1551 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); | 1565 CloseConnection(QUIC_PACKET_WRITE_ERROR, false); |
| 1552 } | 1566 } |
| 1553 | 1567 |
| 1554 void QuicConnection::OnSerializedPacket( | 1568 void QuicConnection::OnSerializedPacket( |
| 1555 const SerializedPacket& serialized_packet) { | 1569 const SerializedPacket& serialized_packet) { |
| 1570 // If a forward-secure encrypter is available but is not being used and this |
| 1571 // packet's sequence number is after the first packet which requires |
| 1572 // forward security, start using the forward-secure encrypter. |
| 1573 if (FLAGS_enable_quic_delay_forward_security && |
| 1574 encryption_level_ != ENCRYPTION_FORWARD_SECURE && |
| 1575 has_forward_secure_encrypter_ && |
| 1576 serialized_packet.sequence_number >= |
| 1577 first_required_forward_secure_packet_) { |
| 1578 SetDefaultEncryptionLevel(ENCRYPTION_FORWARD_SECURE); |
| 1579 } |
| 1556 if (serialized_packet.retransmittable_frames) { | 1580 if (serialized_packet.retransmittable_frames) { |
| 1557 serialized_packet.retransmittable_frames-> | 1581 serialized_packet.retransmittable_frames-> |
| 1558 set_encryption_level(encryption_level_); | 1582 set_encryption_level(encryption_level_); |
| 1559 } | 1583 } |
| 1560 SendOrQueuePacket(QueuedPacket(serialized_packet, encryption_level_)); | 1584 SendOrQueuePacket(QueuedPacket(serialized_packet, encryption_level_)); |
| 1561 } | 1585 } |
| 1562 | 1586 |
| 1563 void QuicConnection::OnCongestionWindowChange(QuicByteCount congestion_window) { | 1587 void QuicConnection::OnCongestionWindowChange() { |
| 1564 packet_generator_.OnCongestionWindowChange(congestion_window); | 1588 packet_generator_.OnCongestionWindowChange( |
| 1589 sent_packet_manager_.GetCongestionWindow()); |
| 1565 visitor_->OnCongestionWindowChange(clock_->ApproximateNow()); | 1590 visitor_->OnCongestionWindowChange(clock_->ApproximateNow()); |
| 1566 } | 1591 } |
| 1567 | 1592 |
| 1568 void QuicConnection::OnHandshakeComplete() { | 1593 void QuicConnection::OnHandshakeComplete() { |
| 1569 sent_packet_manager_.SetHandshakeConfirmed(); | 1594 sent_packet_manager_.SetHandshakeConfirmed(); |
| 1570 } | 1595 } |
| 1571 | 1596 |
| 1572 void QuicConnection::SendOrQueuePacket(QueuedPacket packet) { | 1597 void QuicConnection::SendOrQueuePacket(QueuedPacket packet) { |
| 1573 // The caller of this function is responsible for checking CanWrite(). | 1598 // The caller of this function is responsible for checking CanWrite(). |
| 1574 if (packet.serialized_packet.packet == nullptr) { | 1599 if (packet.serialized_packet.packet == nullptr) { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1646 QuicTime rto_timeout = sent_packet_manager_.GetRetransmissionTime(); | 1671 QuicTime rto_timeout = sent_packet_manager_.GetRetransmissionTime(); |
| 1647 if (rto_timeout.IsInitialized()) { | 1672 if (rto_timeout.IsInitialized()) { |
| 1648 retransmission_alarm_->Set(rto_timeout); | 1673 retransmission_alarm_->Set(rto_timeout); |
| 1649 } | 1674 } |
| 1650 } | 1675 } |
| 1651 } | 1676 } |
| 1652 | 1677 |
| 1653 void QuicConnection::SetEncrypter(EncryptionLevel level, | 1678 void QuicConnection::SetEncrypter(EncryptionLevel level, |
| 1654 QuicEncrypter* encrypter) { | 1679 QuicEncrypter* encrypter) { |
| 1655 framer_.SetEncrypter(level, encrypter); | 1680 framer_.SetEncrypter(level, encrypter); |
| 1681 if (FLAGS_enable_quic_delay_forward_security && |
| 1682 level == ENCRYPTION_FORWARD_SECURE) { |
| 1683 has_forward_secure_encrypter_ = true; |
| 1684 first_required_forward_secure_packet_ = |
| 1685 sequence_number_of_last_sent_packet_ + |
| 1686 // 3 times the current congestion window (in slow start) should cover |
| 1687 // about two full round trips worth of packets, which should be |
| 1688 // sufficient. |
| 1689 3 * sent_packet_manager_.GetCongestionWindow() / max_packet_length(); |
| 1690 } |
| 1656 } | 1691 } |
| 1657 | 1692 |
| 1658 const QuicEncrypter* QuicConnection::encrypter(EncryptionLevel level) const { | 1693 const QuicEncrypter* QuicConnection::encrypter(EncryptionLevel level) const { |
| 1659 return framer_.encrypter(level); | 1694 return framer_.encrypter(level); |
| 1660 } | 1695 } |
| 1661 | 1696 |
| 1662 void QuicConnection::SetDefaultEncryptionLevel(EncryptionLevel level) { | 1697 void QuicConnection::SetDefaultEncryptionLevel(EncryptionLevel level) { |
| 1663 encryption_level_ = level; | 1698 encryption_level_ = level; |
| 1664 packet_generator_.set_encryption_level(level); | 1699 packet_generator_.set_encryption_level(level); |
| 1665 } | 1700 } |
| (...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2073 } | 2108 } |
| 2074 for (size_t i = 0; i < retransmittable_frames->frames().size(); ++i) { | 2109 for (size_t i = 0; i < retransmittable_frames->frames().size(); ++i) { |
| 2075 if (retransmittable_frames->frames()[i].type == CONNECTION_CLOSE_FRAME) { | 2110 if (retransmittable_frames->frames()[i].type == CONNECTION_CLOSE_FRAME) { |
| 2076 return true; | 2111 return true; |
| 2077 } | 2112 } |
| 2078 } | 2113 } |
| 2079 return false; | 2114 return false; |
| 2080 } | 2115 } |
| 2081 | 2116 |
| 2082 } // namespace net | 2117 } // namespace net |
| OLD | NEW |