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 | #ifndef QUICHE_QUIC_QUARTC_QUARTC_ENDPOINT_H_ |
| 6 | #define QUICHE_QUIC_QUARTC_QUARTC_ENDPOINT_H_ |
| 7 | |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | #include "net/third_party/quiche/src/quic/core/quic_alarm_factory.h" |
vasilvv | 89713d0 | 2020-02-11 14:33:26 -0800 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/quic/core/quic_clock.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 12 | #include "net/third_party/quiche/src/quic/core/quic_error_codes.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 13 | #include "net/third_party/quiche/src/quic/quartc/quartc_connection_helper.h" |
| 14 | #include "net/third_party/quiche/src/quic/quartc/quartc_crypto_helpers.h" |
| 15 | #include "net/third_party/quiche/src/quic/quartc/quartc_dispatcher.h" |
| 16 | #include "net/third_party/quiche/src/quic/quartc/quartc_factory.h" |
dmcardle | c60e87a | 2019-12-12 09:43:19 -0800 | [diff] [blame] | 17 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 18 | |
| 19 | namespace quic { |
| 20 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 21 | // Endpoint (client or server) in a peer-to-peer Quartc connection. |
| 22 | class QuartcEndpoint { |
| 23 | public: |
QUICHE team | 6939de5 | 2019-05-15 12:05:21 -0700 | [diff] [blame] | 24 | class Delegate : public QuartcSession::Delegate { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 25 | public: |
| 26 | virtual ~Delegate() = default; |
| 27 | |
| 28 | // Called when an endpoint creates a new session, before any packets are |
| 29 | // processed or sent. The callee should perform any additional |
QUICHE team | 6939de5 | 2019-05-15 12:05:21 -0700 | [diff] [blame] | 30 | // configuration required, such as setting up congestion control, before |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 31 | // returning. |session| is owned by the endpoint, but remains safe to use |
QUICHE team | 6939de5 | 2019-05-15 12:05:21 -0700 | [diff] [blame] | 32 | // until another call to |OnSessionCreated| or |OnConnectionClosed| occurs, |
| 33 | // at which point previous session may be destroyed. |
| 34 | // |
| 35 | // Callees must not change the |session|'s delegate. The Endpoint itself |
| 36 | // manages the delegate and will forward calls. |
| 37 | // |
| 38 | // New calls to |OnSessionCreated| will only occur prior to |
| 39 | // |OnConnectionWritable|, during initial connection negotiation. |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 40 | virtual void OnSessionCreated(QuartcSession* session) = 0; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | virtual ~QuartcEndpoint() = default; |
| 44 | |
| 45 | // Connects the endpoint using the given session config. After |Connect| is |
| 46 | // called, the endpoint will asynchronously create a session, then call |
| 47 | // |Delegate::OnSessionCreated|. |
| 48 | virtual void Connect(QuartcPacketTransport* packet_transport) = 0; |
| 49 | }; |
| 50 | |
| 51 | // Implementation of QuartcEndpoint which immediately (but asynchronously) |
| 52 | // creates a session by scheduling a QuicAlarm. Only suitable for use with the |
| 53 | // client perspective. |
QUICHE team | 6939de5 | 2019-05-15 12:05:21 -0700 | [diff] [blame] | 54 | class QuartcClientEndpoint : public QuartcEndpoint, |
| 55 | public QuartcSession::Delegate { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 56 | public: |
| 57 | // |alarm_factory|, |clock|, and |delegate| are owned by the caller and must |
| 58 | // outlive the endpoint. |
| 59 | QuartcClientEndpoint( |
| 60 | QuicAlarmFactory* alarm_factory, |
| 61 | const QuicClock* clock, |
QUICHE team | 73957f1 | 2019-04-18 16:21:52 -0700 | [diff] [blame] | 62 | QuicRandom* random, |
QUICHE team | 6939de5 | 2019-05-15 12:05:21 -0700 | [diff] [blame] | 63 | QuartcEndpoint::Delegate* delegate, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 64 | const QuartcSessionConfig& config, |
dmcardle | c60e87a | 2019-12-12 09:43:19 -0800 | [diff] [blame] | 65 | quiche::QuicheStringPiece serialized_server_config, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 66 | std::unique_ptr<QuicVersionManager> version_manager = nullptr); |
| 67 | |
| 68 | void Connect(QuartcPacketTransport* packet_transport) override; |
| 69 | |
QUICHE team | 6939de5 | 2019-05-15 12:05:21 -0700 | [diff] [blame] | 70 | // QuartcSession::Delegate overrides. |
| 71 | void OnCryptoHandshakeComplete() override; |
| 72 | void OnConnectionWritable() override; |
| 73 | void OnIncomingStream(QuartcStream* stream) override; |
| 74 | void OnCongestionControlChange(QuicBandwidth bandwidth_estimate, |
| 75 | QuicBandwidth pacing_rate, |
| 76 | QuicTime::Delta latest_rtt) override; |
fkastenholz | 5d880a9 | 2019-06-21 09:01:56 -0700 | [diff] [blame] | 77 | void OnConnectionClosed(const QuicConnectionCloseFrame& frame, |
QUICHE team | 6939de5 | 2019-05-15 12:05:21 -0700 | [diff] [blame] | 78 | ConnectionCloseSource source) override; |
dmcardle | c60e87a | 2019-12-12 09:43:19 -0800 | [diff] [blame] | 79 | void OnMessageReceived(quiche::QuicheStringPiece message) override; |
QUICHE team | 6939de5 | 2019-05-15 12:05:21 -0700 | [diff] [blame] | 80 | void OnMessageSent(int64_t datagram_id) override; |
QUICHE team | 68d15a8 | 2019-05-31 15:27:25 -0700 | [diff] [blame] | 81 | void OnMessageAcked(int64_t datagram_id, QuicTime receive_timestamp) override; |
| 82 | void OnMessageLost(int64_t datagram_id) override; |
QUICHE team | 6939de5 | 2019-05-15 12:05:21 -0700 | [diff] [blame] | 83 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 84 | private: |
| 85 | friend class CreateSessionDelegate; |
| 86 | class CreateSessionDelegate : public QuicAlarm::Delegate { |
| 87 | public: |
| 88 | CreateSessionDelegate(QuartcClientEndpoint* endpoint) |
| 89 | : endpoint_(endpoint) {} |
| 90 | |
| 91 | void OnAlarm() override { endpoint_->OnCreateSessionAlarm(); } |
| 92 | |
| 93 | private: |
| 94 | QuartcClientEndpoint* endpoint_; |
| 95 | }; |
| 96 | |
| 97 | // Callback which occurs when |create_session_alarm_| fires. |
| 98 | void OnCreateSessionAlarm(); |
| 99 | |
| 100 | // Implementation of QuicAlarmFactory used by this endpoint. Unowned. |
| 101 | QuicAlarmFactory* alarm_factory_; |
| 102 | |
| 103 | // Implementation of QuicClock used by this endpoint. Unowned. |
| 104 | const QuicClock* clock_; |
| 105 | |
| 106 | // Delegate which receives callbacks for newly created sessions. |
| 107 | QuartcEndpoint::Delegate* delegate_; |
| 108 | |
| 109 | // Server config. If valid, used to perform a 0-RTT connection. |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 110 | const std::string serialized_server_config_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 111 | |
| 112 | // Version manager. May be injected to control version negotiation in tests. |
| 113 | std::unique_ptr<QuicVersionManager> version_manager_; |
| 114 | |
QUICHE team | 6939de5 | 2019-05-15 12:05:21 -0700 | [diff] [blame] | 115 | // Versions to be used when the next session is created. The session will |
| 116 | // choose one of these versions for its connection attempt. |
| 117 | // |
| 118 | // If the connection does not succeed, the client session MAY try again using |
| 119 | // another version from this list, or it MAY simply fail with a |
| 120 | // QUIC_INVALID_VERSION error. The latter occurs when it is not possible to |
| 121 | // upgrade a connection in-place (for example, if the way stream ids are |
| 122 | // allocated changes between versions). This failure mode is handled by |
| 123 | // narrowing |current_versions_| to one of that is mutually-supported and |
| 124 | // reconnecting (with a new session). |
| 125 | ParsedQuicVersionVector current_versions_; |
| 126 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 127 | // Alarm for creating sessions asynchronously. The alarm is set when |
| 128 | // Connect() is called. When it fires, the endpoint creates a session and |
| 129 | // calls the delegate. |
| 130 | std::unique_ptr<QuicAlarm> create_session_alarm_; |
| 131 | |
QUICHE team | b114092 | 2019-03-22 05:24:05 -0700 | [diff] [blame] | 132 | // Helper used by QuicConnection. |
| 133 | std::unique_ptr<QuicConnectionHelperInterface> connection_helper_; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 134 | |
| 135 | // Config to be used for new sessions. |
| 136 | QuartcSessionConfig config_; |
| 137 | |
| 138 | // The currently-active session. Nullptr until |Connect| and |
| 139 | // |Delegate::OnSessionCreated| are called. |
| 140 | std::unique_ptr<QuartcSession> session_; |
| 141 | |
| 142 | QuartcPacketTransport* packet_transport_; |
| 143 | }; |
| 144 | |
| 145 | // Implementation of QuartcEndpoint which uses a QuartcDispatcher to listen for |
| 146 | // an incoming CHLO and create a session when one arrives. Only suitable for |
| 147 | // use with the server perspective. |
| 148 | class QuartcServerEndpoint : public QuartcEndpoint, |
| 149 | public QuartcDispatcher::Delegate { |
| 150 | public: |
| 151 | QuartcServerEndpoint( |
| 152 | QuicAlarmFactory* alarm_factory, |
| 153 | const QuicClock* clock, |
QUICHE team | 73957f1 | 2019-04-18 16:21:52 -0700 | [diff] [blame] | 154 | QuicRandom* random, |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 155 | QuartcEndpoint::Delegate* delegate, |
| 156 | const QuartcSessionConfig& config, |
| 157 | std::unique_ptr<QuicVersionManager> version_manager = nullptr); |
| 158 | |
| 159 | // Implements QuartcEndpoint. |
| 160 | void Connect(QuartcPacketTransport* packet_transport) override; |
| 161 | |
| 162 | // Implements QuartcDispatcher::Delegate. |
| 163 | void OnSessionCreated(QuartcSession* session) override; |
| 164 | |
| 165 | // Accessor to retrieve the server crypto config. May only be called after |
| 166 | // Connect(). |
dmcardle | c60e87a | 2019-12-12 09:43:19 -0800 | [diff] [blame] | 167 | quiche::QuicheStringPiece server_crypto_config() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 168 | return crypto_config_.serialized_crypto_config; |
| 169 | } |
| 170 | |
| 171 | const std::vector<ParsedQuicVersion> GetSupportedQuicVersions() const { |
| 172 | return version_manager_->GetSupportedVersions(); |
| 173 | } |
| 174 | |
| 175 | private: |
| 176 | // Implementation of QuicAlarmFactory used by this endpoint. Unowned. |
| 177 | QuicAlarmFactory* alarm_factory_; |
| 178 | |
| 179 | // Delegate which receives callbacks for newly created sessions. |
| 180 | QuartcEndpoint::Delegate* delegate_; |
| 181 | |
| 182 | // Config to be used for new sessions. |
| 183 | QuartcSessionConfig config_; |
| 184 | |
| 185 | // Version manager. May be injected to control version negotiation in tests. |
| 186 | std::unique_ptr<QuicVersionManager> version_manager_; |
| 187 | |
| 188 | // QuartcDispatcher waits for an incoming CHLO, then either rejects it or |
| 189 | // creates a session to respond to it. The dispatcher owns all sessions it |
| 190 | // creates. |
| 191 | std::unique_ptr<QuartcDispatcher> dispatcher_; |
| 192 | |
| 193 | // This field is only available before connection was started. |
| 194 | std::unique_ptr<QuartcConnectionHelper> pre_connection_helper_; |
| 195 | |
| 196 | // A configuration, containing public key, that may need to be passed to the |
| 197 | // client to enable 0rtt. |
| 198 | CryptoServerConfig crypto_config_; |
| 199 | }; |
| 200 | |
| 201 | } // namespace quic |
| 202 | |
| 203 | #endif // QUICHE_QUIC_QUARTC_QUARTC_ENDPOINT_H_ |