Jonas Oreland | e8e7d7b | 2019-05-29 09:30:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef P2P_BASE_CONNECTION_H_ |
| 12 | #define P2P_BASE_CONNECTION_H_ |
| 13 | |
| 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
| 17 | #include "absl/types/optional.h" |
| 18 | #include "api/candidate.h" |
| 19 | #include "logging/rtc_event_log/ice_logger.h" |
| 20 | #include "p2p/base/candidate_pair_interface.h" |
| 21 | #include "p2p/base/connection_info.h" |
| 22 | #include "p2p/base/stun.h" |
| 23 | #include "p2p/base/stun_request.h" |
| 24 | #include "p2p/base/transport_description.h" |
| 25 | #include "rtc_base/async_packet_socket.h" |
| 26 | #include "rtc_base/message_handler.h" |
| 27 | #include "rtc_base/rate_tracker.h" |
| 28 | #include "rtc_base/third_party/sigslot/sigslot.h" |
| 29 | |
| 30 | namespace cricket { |
| 31 | |
| 32 | // Connection and Port has circular dependencies. |
| 33 | // So we use forward declaration rather than include. |
| 34 | class Port; |
| 35 | |
| 36 | // Forward declaration so that a ConnectionRequest can contain a Connection. |
| 37 | class Connection; |
| 38 | |
Qingsi Wang | e5defb1 | 2019-08-15 11:01:53 -0700 | [diff] [blame] | 39 | struct CandidatePair final : public CandidatePairInterface { |
| 40 | ~CandidatePair() override = default; |
| 41 | |
| 42 | const Candidate& local_candidate() const override { return local; } |
| 43 | const Candidate& remote_candidate() const override { return remote; } |
| 44 | |
| 45 | Candidate local; |
| 46 | Candidate remote; |
| 47 | }; |
| 48 | |
Jonas Oreland | e8e7d7b | 2019-05-29 09:30:55 +0200 | [diff] [blame] | 49 | // A ConnectionRequest is a simple STUN ping used to determine writability. |
| 50 | class ConnectionRequest : public StunRequest { |
| 51 | public: |
| 52 | explicit ConnectionRequest(Connection* connection); |
| 53 | void Prepare(StunMessage* request) override; |
| 54 | void OnResponse(StunMessage* response) override; |
| 55 | void OnErrorResponse(StunMessage* response) override; |
| 56 | void OnTimeout() override; |
| 57 | void OnSent() override; |
| 58 | int resend_delay() override; |
| 59 | |
| 60 | private: |
| 61 | Connection* connection_; |
| 62 | }; |
| 63 | |
| 64 | // Represents a communication link between a port on the local client and a |
| 65 | // port on the remote client. |
| 66 | class Connection : public CandidatePairInterface, |
| 67 | public rtc::MessageHandler, |
| 68 | public sigslot::has_slots<> { |
| 69 | public: |
| 70 | struct SentPing { |
| 71 | SentPing(const std::string id, int64_t sent_time, uint32_t nomination) |
| 72 | : id(id), sent_time(sent_time), nomination(nomination) {} |
| 73 | |
| 74 | std::string id; |
| 75 | int64_t sent_time; |
| 76 | uint32_t nomination; |
| 77 | }; |
| 78 | |
| 79 | ~Connection() override; |
| 80 | |
| 81 | // A unique ID assigned when the connection is created. |
| 82 | uint32_t id() { return id_; } |
| 83 | |
| 84 | // The local port where this connection sends and receives packets. |
| 85 | Port* port() { return port_; } |
| 86 | const Port* port() const { return port_; } |
| 87 | |
| 88 | // Implementation of virtual methods in CandidatePairInterface. |
| 89 | // Returns the description of the local port |
| 90 | const Candidate& local_candidate() const override; |
| 91 | // Returns the description of the remote port to which we communicate. |
| 92 | const Candidate& remote_candidate() const override; |
| 93 | |
| 94 | // Returns the pair priority. |
| 95 | uint64_t priority() const; |
| 96 | |
| 97 | enum WriteState { |
| 98 | STATE_WRITABLE = 0, // we have received ping responses recently |
| 99 | STATE_WRITE_UNRELIABLE = 1, // we have had a few ping failures |
| 100 | STATE_WRITE_INIT = 2, // we have yet to receive a ping response |
| 101 | STATE_WRITE_TIMEOUT = 3, // we have had a large number of ping failures |
| 102 | }; |
| 103 | |
| 104 | WriteState write_state() const { return write_state_; } |
| 105 | bool writable() const { return write_state_ == STATE_WRITABLE; } |
| 106 | bool receiving() const { return receiving_; } |
| 107 | |
| 108 | // Determines whether the connection has finished connecting. This can only |
| 109 | // be false for TCP connections. |
| 110 | bool connected() const { return connected_; } |
| 111 | bool weak() const { return !(writable() && receiving() && connected()); } |
| 112 | bool active() const { return write_state_ != STATE_WRITE_TIMEOUT; } |
| 113 | |
| 114 | // A connection is dead if it can be safely deleted. |
| 115 | bool dead(int64_t now) const; |
| 116 | |
| 117 | // Estimate of the round-trip time over this connection. |
| 118 | int rtt() const { return rtt_; } |
| 119 | |
| 120 | int unwritable_timeout() const; |
| 121 | void set_unwritable_timeout(const absl::optional<int>& value_ms) { |
| 122 | unwritable_timeout_ = value_ms; |
| 123 | } |
| 124 | int unwritable_min_checks() const; |
| 125 | void set_unwritable_min_checks(const absl::optional<int>& value) { |
| 126 | unwritable_min_checks_ = value; |
| 127 | } |
| 128 | int inactive_timeout() const; |
| 129 | void set_inactive_timeout(const absl::optional<int>& value) { |
| 130 | inactive_timeout_ = value; |
| 131 | } |
| 132 | |
| 133 | // Gets the |ConnectionInfo| stats, where |best_connection| has not been |
| 134 | // populated (default value false). |
| 135 | ConnectionInfo stats(); |
| 136 | |
| 137 | sigslot::signal1<Connection*> SignalStateChange; |
| 138 | |
| 139 | // Sent when the connection has decided that it is no longer of value. It |
| 140 | // will delete itself immediately after this call. |
| 141 | sigslot::signal1<Connection*> SignalDestroyed; |
| 142 | |
| 143 | // The connection can send and receive packets asynchronously. This matches |
| 144 | // the interface of AsyncPacketSocket, which may use UDP or TCP under the |
| 145 | // covers. |
| 146 | virtual int Send(const void* data, |
| 147 | size_t size, |
| 148 | const rtc::PacketOptions& options) = 0; |
| 149 | |
| 150 | // Error if Send() returns < 0 |
| 151 | virtual int GetError() = 0; |
| 152 | |
| 153 | sigslot::signal4<Connection*, const char*, size_t, int64_t> SignalReadPacket; |
| 154 | |
| 155 | sigslot::signal1<Connection*> SignalReadyToSend; |
| 156 | |
| 157 | // Called when a packet is received on this connection. |
| 158 | void OnReadPacket(const char* data, size_t size, int64_t packet_time_us); |
| 159 | |
| 160 | // Called when the socket is currently able to send. |
| 161 | void OnReadyToSend(); |
| 162 | |
| 163 | // Called when a connection is determined to be no longer useful to us. We |
| 164 | // still keep it around in case the other side wants to use it. But we can |
| 165 | // safely stop pinging on it and we can allow it to time out if the other |
| 166 | // side stops using it as well. |
| 167 | bool pruned() const { return pruned_; } |
| 168 | void Prune(); |
| 169 | |
| 170 | bool use_candidate_attr() const { return use_candidate_attr_; } |
| 171 | void set_use_candidate_attr(bool enable); |
| 172 | |
| 173 | void set_nomination(uint32_t value) { nomination_ = value; } |
| 174 | |
| 175 | uint32_t remote_nomination() const { return remote_nomination_; } |
| 176 | // One or several pairs may be nominated based on if Regular or Aggressive |
| 177 | // Nomination is used. https://tools.ietf.org/html/rfc5245#section-8 |
| 178 | // |nominated| is defined both for the controlling or controlled agent based |
| 179 | // on if a nomination has been pinged or acknowledged. The controlled agent |
| 180 | // gets its |remote_nomination_| set when pinged by the controlling agent with |
| 181 | // a nomination value. The controlling agent gets its |acked_nomination_| set |
| 182 | // when receiving a response to a nominating ping. |
| 183 | bool nominated() const { return acked_nomination_ || remote_nomination_; } |
| 184 | // Public for unit tests. |
| 185 | void set_remote_nomination(uint32_t remote_nomination) { |
| 186 | remote_nomination_ = remote_nomination; |
| 187 | } |
| 188 | // Public for unit tests. |
| 189 | uint32_t acked_nomination() const { return acked_nomination_; } |
| 190 | |
| 191 | void set_remote_ice_mode(IceMode mode) { remote_ice_mode_ = mode; } |
| 192 | |
| 193 | int receiving_timeout() const; |
| 194 | void set_receiving_timeout(absl::optional<int> receiving_timeout_ms) { |
| 195 | receiving_timeout_ = receiving_timeout_ms; |
| 196 | } |
| 197 | |
| 198 | // Makes the connection go away. |
| 199 | void Destroy(); |
| 200 | |
| 201 | // Makes the connection go away, in a failed state. |
| 202 | void FailAndDestroy(); |
| 203 | |
| 204 | // Prunes the connection and sets its state to STATE_FAILED, |
| 205 | // It will not be used or send pings although it can still receive packets. |
| 206 | void FailAndPrune(); |
| 207 | |
| 208 | // Checks that the state of this connection is up-to-date. The argument is |
| 209 | // the current time, which is compared against various timeouts. |
| 210 | void UpdateState(int64_t now); |
| 211 | |
| 212 | // Called when this connection should try checking writability again. |
| 213 | int64_t last_ping_sent() const { return last_ping_sent_; } |
| 214 | void Ping(int64_t now); |
Qingsi Wang | 0894f0f | 2019-06-18 14:11:36 -0700 | [diff] [blame] | 215 | void ReceivedPingResponse( |
| 216 | int rtt, |
| 217 | const std::string& request_id, |
| 218 | const absl::optional<uint32_t>& nomination = absl::nullopt); |
Jonas Oreland | e8e7d7b | 2019-05-29 09:30:55 +0200 | [diff] [blame] | 219 | int64_t last_ping_response_received() const { |
| 220 | return last_ping_response_received_; |
| 221 | } |
Qingsi Wang | 0894f0f | 2019-06-18 14:11:36 -0700 | [diff] [blame] | 222 | const absl::optional<std::string>& last_ping_id_received() const { |
| 223 | return last_ping_id_received_; |
| 224 | } |
Jonas Oreland | e8e7d7b | 2019-05-29 09:30:55 +0200 | [diff] [blame] | 225 | // Used to check if any STUN ping response has been received. |
| 226 | int rtt_samples() const { return rtt_samples_; } |
| 227 | |
| 228 | // Called whenever a valid ping is received on this connection. This is |
| 229 | // public because the connection intercepts the first ping for us. |
| 230 | int64_t last_ping_received() const { return last_ping_received_; } |
Qingsi Wang | 0894f0f | 2019-06-18 14:11:36 -0700 | [diff] [blame] | 231 | void ReceivedPing( |
| 232 | const absl::optional<std::string>& request_id = absl::nullopt); |
Jonas Oreland | e8e7d7b | 2019-05-29 09:30:55 +0200 | [diff] [blame] | 233 | // Handles the binding request; sends a response if this is a valid request. |
| 234 | void HandleBindingRequest(IceMessage* msg); |
Qingsi Wang | 0894f0f | 2019-06-18 14:11:36 -0700 | [diff] [blame] | 235 | // Handles the piggyback acknowledgement of the lastest connectivity check |
| 236 | // that the remote peer has received, if it is indicated in the incoming |
| 237 | // connectivity check from the peer. |
| 238 | void HandlePiggybackCheckAcknowledgementIfAny(StunMessage* msg); |
Jonas Oreland | e8e7d7b | 2019-05-29 09:30:55 +0200 | [diff] [blame] | 239 | int64_t last_data_received() const { return last_data_received_; } |
| 240 | |
| 241 | // Debugging description of this connection |
| 242 | std::string ToDebugId() const; |
| 243 | std::string ToString() const; |
| 244 | std::string ToSensitiveString() const; |
| 245 | // Structured description of this candidate pair. |
| 246 | const webrtc::IceCandidatePairDescription& ToLogDescription(); |
| 247 | void set_ice_event_log(webrtc::IceEventLog* ice_event_log) { |
| 248 | ice_event_log_ = ice_event_log; |
| 249 | } |
| 250 | // Prints pings_since_last_response_ into a string. |
| 251 | void PrintPingsSinceLastResponse(std::string* pings, size_t max); |
| 252 | |
| 253 | bool reported() const { return reported_; } |
| 254 | void set_reported(bool reported) { reported_ = reported; } |
| 255 | // The following two methods are only used for logging in ToString above, and |
| 256 | // this flag is set true by P2PTransportChannel for its selected candidate |
| 257 | // pair. |
| 258 | bool selected() const { return selected_; } |
| 259 | void set_selected(bool selected) { selected_ = selected; } |
| 260 | |
| 261 | // This signal will be fired if this connection is nominated by the |
| 262 | // controlling side. |
| 263 | sigslot::signal1<Connection*> SignalNominated; |
| 264 | |
| 265 | // Invoked when Connection receives STUN error response with 487 code. |
| 266 | void HandleRoleConflictFromPeer(); |
| 267 | |
| 268 | IceCandidatePairState state() const { return state_; } |
| 269 | |
| 270 | int num_pings_sent() const { return num_pings_sent_; } |
| 271 | |
| 272 | IceMode remote_ice_mode() const { return remote_ice_mode_; } |
| 273 | |
| 274 | uint32_t ComputeNetworkCost() const; |
| 275 | |
| 276 | // Update the ICE password and/or generation of the remote candidate if the |
| 277 | // ufrag in |params| matches the candidate's ufrag, and the |
| 278 | // candidate's password and/or ufrag has not been set. |
| 279 | void MaybeSetRemoteIceParametersAndGeneration(const IceParameters& params, |
| 280 | int generation); |
| 281 | |
| 282 | // If |remote_candidate_| is peer reflexive and is equivalent to |
| 283 | // |new_candidate| except the type, update |remote_candidate_| to |
| 284 | // |new_candidate|. |
| 285 | void MaybeUpdatePeerReflexiveCandidate(const Candidate& new_candidate); |
| 286 | |
| 287 | // Returns the last received time of any data, stun request, or stun |
| 288 | // response in milliseconds |
| 289 | int64_t last_received() const; |
| 290 | // Returns the last time when the connection changed its receiving state. |
| 291 | int64_t receiving_unchanged_since() const { |
| 292 | return receiving_unchanged_since_; |
| 293 | } |
| 294 | |
| 295 | bool stable(int64_t now) const; |
| 296 | |
Jonas Oreland | c6404a1 | 2019-10-14 15:52:15 +0200 | [diff] [blame] | 297 | // Check if we sent |val| pings without receving a response. |
| 298 | bool TooManyOutstandingPings(const absl::optional<int>& val) const; |
| 299 | |
Jonas Oreland | e8e7d7b | 2019-05-29 09:30:55 +0200 | [diff] [blame] | 300 | protected: |
| 301 | enum { MSG_DELETE = 0, MSG_FIRST_AVAILABLE }; |
| 302 | |
| 303 | // Constructs a new connection to the given remote port. |
| 304 | Connection(Port* port, size_t index, const Candidate& candidate); |
| 305 | |
| 306 | // Called back when StunRequestManager has a stun packet to send |
| 307 | void OnSendStunPacket(const void* data, size_t size, StunRequest* req); |
| 308 | |
| 309 | // Callbacks from ConnectionRequest |
| 310 | virtual void OnConnectionRequestResponse(ConnectionRequest* req, |
| 311 | StunMessage* response); |
| 312 | void OnConnectionRequestErrorResponse(ConnectionRequest* req, |
| 313 | StunMessage* response); |
| 314 | void OnConnectionRequestTimeout(ConnectionRequest* req); |
| 315 | void OnConnectionRequestSent(ConnectionRequest* req); |
| 316 | |
| 317 | bool rtt_converged() const; |
| 318 | |
| 319 | // If the response is not received within 2 * RTT, the response is assumed to |
| 320 | // be missing. |
| 321 | bool missing_responses(int64_t now) const; |
| 322 | |
| 323 | // Changes the state and signals if necessary. |
| 324 | void set_write_state(WriteState value); |
| 325 | void UpdateReceiving(int64_t now); |
| 326 | void set_state(IceCandidatePairState state); |
| 327 | void set_connected(bool value); |
| 328 | |
| 329 | uint32_t nomination() const { return nomination_; } |
| 330 | |
| 331 | void OnMessage(rtc::Message* pmsg) override; |
| 332 | |
| 333 | uint32_t id_; |
| 334 | Port* port_; |
| 335 | size_t local_candidate_index_; |
| 336 | Candidate remote_candidate_; |
| 337 | |
| 338 | ConnectionInfo stats_; |
| 339 | rtc::RateTracker recv_rate_tracker_; |
| 340 | rtc::RateTracker send_rate_tracker_; |
| 341 | |
| 342 | private: |
| 343 | // Update the local candidate based on the mapped address attribute. |
| 344 | // If the local candidate changed, fires SignalStateChange. |
| 345 | void MaybeUpdateLocalCandidate(ConnectionRequest* request, |
| 346 | StunMessage* response); |
| 347 | |
Jonas Oreland | e8e7d7b | 2019-05-29 09:30:55 +0200 | [diff] [blame] | 348 | void LogCandidatePairConfig(webrtc::IceCandidatePairConfigType type); |
| 349 | void LogCandidatePairEvent(webrtc::IceCandidatePairEventType type, |
| 350 | uint32_t transaction_id); |
| 351 | |
| 352 | WriteState write_state_; |
| 353 | bool receiving_; |
| 354 | bool connected_; |
| 355 | bool pruned_; |
| 356 | bool selected_ = false; |
| 357 | // By default |use_candidate_attr_| flag will be true, |
| 358 | // as we will be using aggressive nomination. |
| 359 | // But when peer is ice-lite, this flag "must" be initialized to false and |
| 360 | // turn on when connection becomes "best connection". |
| 361 | bool use_candidate_attr_; |
| 362 | // Used by the controlling side to indicate that this connection will be |
| 363 | // selected for transmission if the peer supports ICE-renomination when this |
| 364 | // value is positive. A larger-value indicates that a connection is nominated |
| 365 | // later and should be selected by the controlled side with higher precedence. |
| 366 | // A zero-value indicates not nominating this connection. |
| 367 | uint32_t nomination_ = 0; |
| 368 | // The last nomination that has been acknowledged. |
| 369 | uint32_t acked_nomination_ = 0; |
| 370 | // Used by the controlled side to remember the nomination value received from |
Qingsi Wang | 0894f0f | 2019-06-18 14:11:36 -0700 | [diff] [blame] | 371 | // the controlling side. When the peer does not support ICE re-nomination, its |
| 372 | // value will be 1 if the connection has been nominated. |
Jonas Oreland | e8e7d7b | 2019-05-29 09:30:55 +0200 | [diff] [blame] | 373 | uint32_t remote_nomination_ = 0; |
| 374 | |
| 375 | IceMode remote_ice_mode_; |
| 376 | StunRequestManager requests_; |
| 377 | int rtt_; |
| 378 | int rtt_samples_ = 0; |
| 379 | // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-totalroundtriptime |
| 380 | uint64_t total_round_trip_time_ms_ = 0; |
| 381 | // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentroundtriptime |
| 382 | absl::optional<uint32_t> current_round_trip_time_ms_; |
| 383 | int64_t last_ping_sent_; // last time we sent a ping to the other side |
| 384 | int64_t last_ping_received_; // last time we received a ping from the other |
| 385 | // side |
| 386 | int64_t last_data_received_; |
| 387 | int64_t last_ping_response_received_; |
| 388 | int64_t receiving_unchanged_since_ = 0; |
| 389 | std::vector<SentPing> pings_since_last_response_; |
Qingsi Wang | 0894f0f | 2019-06-18 14:11:36 -0700 | [diff] [blame] | 390 | // Transaction ID of the last connectivity check received. Null if having not |
| 391 | // received a ping yet. |
| 392 | absl::optional<std::string> last_ping_id_received_; |
Jonas Oreland | e8e7d7b | 2019-05-29 09:30:55 +0200 | [diff] [blame] | 393 | |
| 394 | absl::optional<int> unwritable_timeout_; |
| 395 | absl::optional<int> unwritable_min_checks_; |
| 396 | absl::optional<int> inactive_timeout_; |
| 397 | |
| 398 | bool reported_; |
| 399 | IceCandidatePairState state_; |
| 400 | // Time duration to switch from receiving to not receiving. |
| 401 | absl::optional<int> receiving_timeout_; |
| 402 | int64_t time_created_ms_; |
| 403 | int num_pings_sent_ = 0; |
| 404 | |
| 405 | absl::optional<webrtc::IceCandidatePairDescription> log_description_; |
| 406 | webrtc::IceEventLog* ice_event_log_ = nullptr; |
| 407 | |
| 408 | friend class Port; |
| 409 | friend class ConnectionRequest; |
| 410 | }; |
| 411 | |
| 412 | // ProxyConnection defers all the interesting work to the port. |
| 413 | class ProxyConnection : public Connection { |
| 414 | public: |
| 415 | ProxyConnection(Port* port, size_t index, const Candidate& remote_candidate); |
| 416 | |
| 417 | int Send(const void* data, |
| 418 | size_t size, |
| 419 | const rtc::PacketOptions& options) override; |
| 420 | int GetError() override; |
| 421 | |
| 422 | private: |
| 423 | int error_ = 0; |
| 424 | }; |
| 425 | |
| 426 | } // namespace cricket |
| 427 | |
| 428 | #endif // P2P_BASE_CONNECTION_H_ |