blob: a9d551075396383a20022deb8150ac418ab2f82e [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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
12namespace quic {
13
14QuartcDispatcher::QuartcDispatcher(
15 std::unique_ptr<QuicConfig> config,
16 std::unique_ptr<QuicCryptoServerConfig> crypto_config,
QUICHE teama6ef0a62019-03-07 20:34:33 -050017 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 team4d9d6292019-03-11 14:25:33 -070023 : 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 teama6ef0a62019-03-07 20:34:33 -050033 owned_quic_config_(std::move(config)),
34 owned_crypto_config_(std::move(crypto_config)),
QUICHE teama6ef0a62019-03-07 20:34:33 -050035 delegate_(delegate),
36 packet_writer_(packet_writer.get()) {
QUICHE team4d9d6292019-03-11 14:25:33 -070037 // Allow incoming packets to set our expected connection ID length.
dschinazi8ff74822019-05-28 16:37:20 -070038 SetShouldUpdateExpectedServerConnectionIdLength(true);
QUICHE team963d57e2019-03-21 10:58:47 -070039 // Allow incoming packets with connection ID lengths shorter than allowed.
dschinazi7b9278c2019-05-20 07:36:21 -070040 SetAllowShortInitialServerConnectionIds(true);
QUICHE teama6ef0a62019-03-07 20:34:33 -050041 // 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
49QuartcDispatcher::~QuartcDispatcher() {
50 packet_writer_->SetPacketTransportDelegate(nullptr);
51}
52
53QuartcSession* QuartcDispatcher::CreateQuicSession(
54 QuicConnectionId connection_id,
55 const QuicSocketAddress& client_address,
dschinazi17d42422019-06-18 16:35:07 -070056 QuicStringPiece /*alpn*/,
QUICHE teama6ef0a62019-03-07 20:34:33 -050057 const ParsedQuicVersion& version) {
QUICHE team4d9d6292019-03-11 14:25:33 -070058 // Make our expected connection ID non-mutable since we have a connection.
dschinazi8ff74822019-05-28 16:37:20 -070059 SetShouldUpdateExpectedServerConnectionIdLength(false);
QUICHE teama6ef0a62019-03-07 20:34:33 -050060 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
71void QuartcDispatcher::OnTransportCanWrite() {
72 OnCanWrite();
73}
74
75void 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