dschinazi | 22e23c7 | 2019-12-17 17:16:15 -0800 | [diff] [blame] | 1 | // Copyright 2019 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/masque/masque_dispatcher.h" |
| 6 | #include "net/third_party/quiche/src/quic/masque/masque_server_session.h" |
| 7 | |
| 8 | namespace quic { |
| 9 | |
| 10 | MasqueDispatcher::MasqueDispatcher( |
| 11 | const QuicConfig* config, |
| 12 | const QuicCryptoServerConfig* crypto_config, |
| 13 | QuicVersionManager* version_manager, |
| 14 | std::unique_ptr<QuicConnectionHelperInterface> helper, |
nharper | 5f23a2d | 2020-02-20 10:44:09 -0800 | [diff] [blame] | 15 | std::unique_ptr<QuicCryptoServerStreamBase::Helper> session_helper, |
dschinazi | 22e23c7 | 2019-12-17 17:16:15 -0800 | [diff] [blame] | 16 | std::unique_ptr<QuicAlarmFactory> alarm_factory, |
| 17 | MasqueServerBackend* masque_server_backend, |
| 18 | uint8_t expected_server_connection_id_length) |
| 19 | : QuicSimpleDispatcher(config, |
| 20 | crypto_config, |
| 21 | version_manager, |
| 22 | std::move(helper), |
| 23 | std::move(session_helper), |
| 24 | std::move(alarm_factory), |
| 25 | masque_server_backend, |
| 26 | expected_server_connection_id_length), |
| 27 | masque_server_backend_(masque_server_backend) {} |
| 28 | |
| 29 | std::unique_ptr<QuicSession> MasqueDispatcher::CreateQuicSession( |
| 30 | QuicConnectionId connection_id, |
| 31 | const QuicSocketAddress& client_address, |
| 32 | quiche::QuicheStringPiece /*alpn*/, |
| 33 | const ParsedQuicVersion& version) { |
| 34 | // The MasqueServerSession takes ownership of |connection| below. |
| 35 | QuicConnection* connection = new QuicConnection( |
| 36 | connection_id, client_address, helper(), alarm_factory(), writer(), |
| 37 | /*owns_writer=*/false, Perspective::IS_SERVER, |
| 38 | ParsedQuicVersionVector{version}); |
| 39 | |
| 40 | auto session = std::make_unique<MasqueServerSession>( |
| 41 | config(), GetSupportedVersions(), connection, this, this, |
| 42 | session_helper(), crypto_config(), compressed_certs_cache(), |
| 43 | masque_server_backend_); |
| 44 | session->Initialize(); |
| 45 | return session; |
| 46 | } |
| 47 | |
| 48 | bool MasqueDispatcher::OnFailedToDispatchPacket( |
| 49 | const ReceivedPacketInfo& packet_info) { |
| 50 | auto connection_id_registration = client_connection_id_registrations_.find( |
| 51 | packet_info.destination_connection_id); |
| 52 | if (connection_id_registration == client_connection_id_registrations_.end()) { |
| 53 | QUIC_DLOG(INFO) << "MasqueDispatcher failed to dispatch " << packet_info; |
| 54 | return false; |
| 55 | } |
| 56 | MasqueServerSession* masque_server_session = |
| 57 | connection_id_registration->second; |
| 58 | masque_server_session->HandlePacketFromServer(packet_info); |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | void MasqueDispatcher::RegisterClientConnectionId( |
| 63 | QuicConnectionId client_connection_id, |
| 64 | MasqueServerSession* masque_server_session) { |
| 65 | QUIC_DLOG(INFO) << "Registering encapsulated " << client_connection_id |
| 66 | << " to MASQUE session " |
| 67 | << masque_server_session->connection_id(); |
| 68 | |
| 69 | // Make sure we don't try to overwrite an existing registration with a |
| 70 | // different session. |
| 71 | QUIC_BUG_IF(client_connection_id_registrations_.find(client_connection_id) != |
| 72 | client_connection_id_registrations_.end() && |
| 73 | client_connection_id_registrations_[client_connection_id] != |
| 74 | masque_server_session) |
| 75 | << "Overwriting existing registration for " << client_connection_id; |
| 76 | client_connection_id_registrations_[client_connection_id] = |
| 77 | masque_server_session; |
| 78 | } |
| 79 | |
| 80 | void MasqueDispatcher::UnregisterClientConnectionId( |
| 81 | QuicConnectionId client_connection_id) { |
| 82 | QUIC_DLOG(INFO) << "Unregistering " << client_connection_id; |
| 83 | client_connection_id_registrations_.erase(client_connection_id); |
| 84 | } |
| 85 | |
| 86 | } // namespace quic |