QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 1 | // Copyright (c) 2017 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/third_party/quiche/src/quic/quartc/quartc_dispatcher.h" |
| 6 | |
| 7 | #include "net/third_party/quiche/src/quic/core/quic_utils.h" |
| 8 | #include "net/third_party/quiche/src/quic/core/quic_versions.h" |
| 9 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
| 10 | #include "net/third_party/quiche/src/quic/quartc/quartc_factory.h" |
| 11 | |
| 12 | namespace quic { |
| 13 | |
| 14 | QuartcDispatcher::QuartcDispatcher( |
| 15 | std::unique_ptr<QuicConfig> config, |
| 16 | std::unique_ptr<QuicCryptoServerConfig> crypto_config, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 17 | QuicVersionManager* version_manager, |
| 18 | std::unique_ptr<QuicConnectionHelperInterface> helper, |
| 19 | std::unique_ptr<QuicCryptoServerStream::Helper> session_helper, |
| 20 | std::unique_ptr<QuicAlarmFactory> alarm_factory, |
| 21 | std::unique_ptr<QuartcPacketWriter> packet_writer, |
| 22 | Delegate* delegate) |
QUICHE team | 4d9d629 | 2019-03-11 14:25:33 -0700 | [diff] [blame] | 23 | : QuicDispatcher( |
| 24 | config.get(), |
| 25 | crypto_config.get(), |
| 26 | version_manager, |
| 27 | std::move(helper), |
| 28 | std::move(session_helper), |
| 29 | std::move(alarm_factory), |
| 30 | QuicUtils::CreateZeroConnectionId( |
| 31 | version_manager->GetSupportedVersions()[0].transport_version) |
| 32 | .length()), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 33 | owned_quic_config_(std::move(config)), |
| 34 | owned_crypto_config_(std::move(crypto_config)), |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 35 | delegate_(delegate), |
| 36 | packet_writer_(packet_writer.get()) { |
QUICHE team | 4d9d629 | 2019-03-11 14:25:33 -0700 | [diff] [blame] | 37 | // Allow incoming packets to set our expected connection ID length. |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 38 | SetShouldUpdateExpectedServerConnectionIdLength(true); |
QUICHE team | 963d57e | 2019-03-21 10:58:47 -0700 | [diff] [blame] | 39 | // Allow incoming packets with connection ID lengths shorter than allowed. |
dschinazi | 7b9278c | 2019-05-20 07:36:21 -0700 | [diff] [blame] | 40 | SetAllowShortInitialServerConnectionIds(true); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 41 | // QuicDispatcher takes ownership of the writer. |
| 42 | QuicDispatcher::InitializeWithWriter(packet_writer.release()); |
| 43 | // NB: This must happen *after* InitializeWithWriter. It can call us back |
| 44 | // with OnTransportCanWrite() immediately, and the dispatcher needs to be |
| 45 | // fully initialized to handle that. |
| 46 | packet_writer_->SetPacketTransportDelegate(this); |
| 47 | } |
| 48 | |
| 49 | QuartcDispatcher::~QuartcDispatcher() { |
| 50 | packet_writer_->SetPacketTransportDelegate(nullptr); |
| 51 | } |
| 52 | |
| 53 | QuartcSession* QuartcDispatcher::CreateQuicSession( |
| 54 | QuicConnectionId connection_id, |
| 55 | const QuicSocketAddress& client_address, |
dschinazi | 17d4242 | 2019-06-18 16:35:07 -0700 | [diff] [blame] | 56 | QuicStringPiece /*alpn*/, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 57 | const ParsedQuicVersion& version) { |
QUICHE team | 4d9d629 | 2019-03-11 14:25:33 -0700 | [diff] [blame] | 58 | // Make our expected connection ID non-mutable since we have a connection. |
dschinazi | 8ff7482 | 2019-05-28 16:37:20 -0700 | [diff] [blame] | 59 | SetShouldUpdateExpectedServerConnectionIdLength(false); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 60 | std::unique_ptr<QuicConnection> connection = CreateQuicConnection( |
| 61 | connection_id, client_address, helper(), alarm_factory(), writer(), |
| 62 | Perspective::IS_SERVER, ParsedQuicVersionVector{version}); |
| 63 | QuartcSession* session = new QuartcServerSession( |
| 64 | std::move(connection), /*visitor=*/this, config(), GetSupportedVersions(), |
| 65 | helper()->GetClock(), crypto_config(), compressed_certs_cache(), |
| 66 | session_helper()); |
| 67 | delegate_->OnSessionCreated(session); |
| 68 | return session; |
| 69 | } |
| 70 | |
| 71 | void QuartcDispatcher::OnTransportCanWrite() { |
| 72 | OnCanWrite(); |
| 73 | } |
| 74 | |
| 75 | void QuartcDispatcher::OnTransportReceived(const char* data, size_t data_len) { |
| 76 | // QuartcPacketTransport does not surface real peer addresses, so the |
| 77 | // dispatcher uses a dummy address when processing incoming packets. Note that |
| 78 | // the dispatcher refuses to process anything with port 0. |
| 79 | static const QuicSocketAddress* dummy_address = |
| 80 | new QuicSocketAddress(QuicIpAddress::Any4(), /*port=*/1); |
| 81 | |
| 82 | QuicReceivedPacket packet(data, data_len, helper()->GetClock()->Now()); |
| 83 | ProcessPacket(/*self_address=*/*dummy_address, |
| 84 | /*peer_address=*/*dummy_address, packet); |
| 85 | } |
| 86 | |
| 87 | } // namespace quic |