1*d9f75844SAndroid Build Coastguard Worker /* 2*d9f75844SAndroid Build Coastguard Worker * Copyright 2019 The WebRTC Project Authors. All rights reserved. 3*d9f75844SAndroid Build Coastguard Worker * 4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license 5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source 6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found 7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may 8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree. 9*d9f75844SAndroid Build Coastguard Worker */ 10*d9f75844SAndroid Build Coastguard Worker 11*d9f75844SAndroid Build Coastguard Worker #ifndef P2P_BASE_CONNECTION_H_ 12*d9f75844SAndroid Build Coastguard Worker #define P2P_BASE_CONNECTION_H_ 13*d9f75844SAndroid Build Coastguard Worker 14*d9f75844SAndroid Build Coastguard Worker #include <memory> 15*d9f75844SAndroid Build Coastguard Worker #include <string> 16*d9f75844SAndroid Build Coastguard Worker #include <vector> 17*d9f75844SAndroid Build Coastguard Worker 18*d9f75844SAndroid Build Coastguard Worker #include "absl/strings/string_view.h" 19*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h" 20*d9f75844SAndroid Build Coastguard Worker #include "api/candidate.h" 21*d9f75844SAndroid Build Coastguard Worker #include "api/transport/stun.h" 22*d9f75844SAndroid Build Coastguard Worker #include "logging/rtc_event_log/ice_logger.h" 23*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/candidate_pair_interface.h" 24*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/connection_info.h" 25*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/p2p_transport_channel_ice_field_trials.h" 26*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/stun_request.h" 27*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/transport_description.h" 28*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/async_packet_socket.h" 29*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/network.h" 30*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/numerics/event_based_exponential_moving_average.h" 31*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/rate_tracker.h" 32*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/weak_ptr.h" 33*d9f75844SAndroid Build Coastguard Worker 34*d9f75844SAndroid Build Coastguard Worker namespace cricket { 35*d9f75844SAndroid Build Coastguard Worker 36*d9f75844SAndroid Build Coastguard Worker // Version number for GOOG_PING, this is added to have the option of 37*d9f75844SAndroid Build Coastguard Worker // adding other flavors in the future. 38*d9f75844SAndroid Build Coastguard Worker constexpr int kGoogPingVersion = 1; 39*d9f75844SAndroid Build Coastguard Worker 40*d9f75844SAndroid Build Coastguard Worker // Connection and Port has circular dependencies. 41*d9f75844SAndroid Build Coastguard Worker // So we use forward declaration rather than include. 42*d9f75844SAndroid Build Coastguard Worker class Port; 43*d9f75844SAndroid Build Coastguard Worker 44*d9f75844SAndroid Build Coastguard Worker // Forward declaration so that a ConnectionRequest can contain a Connection. 45*d9f75844SAndroid Build Coastguard Worker class Connection; 46*d9f75844SAndroid Build Coastguard Worker 47*d9f75844SAndroid Build Coastguard Worker struct CandidatePair final : public CandidatePairInterface { 48*d9f75844SAndroid Build Coastguard Worker ~CandidatePair() override = default; 49*d9f75844SAndroid Build Coastguard Worker local_candidatefinal50*d9f75844SAndroid Build Coastguard Worker const Candidate& local_candidate() const override { return local; } remote_candidatefinal51*d9f75844SAndroid Build Coastguard Worker const Candidate& remote_candidate() const override { return remote; } 52*d9f75844SAndroid Build Coastguard Worker 53*d9f75844SAndroid Build Coastguard Worker Candidate local; 54*d9f75844SAndroid Build Coastguard Worker Candidate remote; 55*d9f75844SAndroid Build Coastguard Worker }; 56*d9f75844SAndroid Build Coastguard Worker 57*d9f75844SAndroid Build Coastguard Worker // Represents a communication link between a port on the local client and a 58*d9f75844SAndroid Build Coastguard Worker // port on the remote client. 59*d9f75844SAndroid Build Coastguard Worker class Connection : public CandidatePairInterface { 60*d9f75844SAndroid Build Coastguard Worker public: 61*d9f75844SAndroid Build Coastguard Worker struct SentPing { SentPingSentPing62*d9f75844SAndroid Build Coastguard Worker SentPing(absl::string_view id, int64_t sent_time, uint32_t nomination) 63*d9f75844SAndroid Build Coastguard Worker : id(id), sent_time(sent_time), nomination(nomination) {} 64*d9f75844SAndroid Build Coastguard Worker 65*d9f75844SAndroid Build Coastguard Worker std::string id; 66*d9f75844SAndroid Build Coastguard Worker int64_t sent_time; 67*d9f75844SAndroid Build Coastguard Worker uint32_t nomination; 68*d9f75844SAndroid Build Coastguard Worker }; 69*d9f75844SAndroid Build Coastguard Worker 70*d9f75844SAndroid Build Coastguard Worker ~Connection() override; 71*d9f75844SAndroid Build Coastguard Worker 72*d9f75844SAndroid Build Coastguard Worker // A unique ID assigned when the connection is created. id()73*d9f75844SAndroid Build Coastguard Worker uint32_t id() const { return id_; } 74*d9f75844SAndroid Build Coastguard Worker 75*d9f75844SAndroid Build Coastguard Worker webrtc::TaskQueueBase* network_thread() const; 76*d9f75844SAndroid Build Coastguard Worker 77*d9f75844SAndroid Build Coastguard Worker // Implementation of virtual methods in CandidatePairInterface. 78*d9f75844SAndroid Build Coastguard Worker // Returns the description of the local port 79*d9f75844SAndroid Build Coastguard Worker const Candidate& local_candidate() const override; 80*d9f75844SAndroid Build Coastguard Worker // Returns the description of the remote port to which we communicate. 81*d9f75844SAndroid Build Coastguard Worker const Candidate& remote_candidate() const override; 82*d9f75844SAndroid Build Coastguard Worker 83*d9f75844SAndroid Build Coastguard Worker // Return local network for this connection. 84*d9f75844SAndroid Build Coastguard Worker virtual const rtc::Network* network() const; 85*d9f75844SAndroid Build Coastguard Worker // Return generation for this connection. 86*d9f75844SAndroid Build Coastguard Worker virtual int generation() const; 87*d9f75844SAndroid Build Coastguard Worker 88*d9f75844SAndroid Build Coastguard Worker // Returns the pair priority. 89*d9f75844SAndroid Build Coastguard Worker virtual uint64_t priority() const; 90*d9f75844SAndroid Build Coastguard Worker 91*d9f75844SAndroid Build Coastguard Worker enum WriteState { 92*d9f75844SAndroid Build Coastguard Worker STATE_WRITABLE = 0, // we have received ping responses recently 93*d9f75844SAndroid Build Coastguard Worker STATE_WRITE_UNRELIABLE = 1, // we have had a few ping failures 94*d9f75844SAndroid Build Coastguard Worker STATE_WRITE_INIT = 2, // we have yet to receive a ping response 95*d9f75844SAndroid Build Coastguard Worker STATE_WRITE_TIMEOUT = 3, // we have had a large number of ping failures 96*d9f75844SAndroid Build Coastguard Worker }; 97*d9f75844SAndroid Build Coastguard Worker 98*d9f75844SAndroid Build Coastguard Worker WriteState write_state() const; 99*d9f75844SAndroid Build Coastguard Worker bool writable() const; 100*d9f75844SAndroid Build Coastguard Worker bool receiving() const; 101*d9f75844SAndroid Build Coastguard Worker port()102*d9f75844SAndroid Build Coastguard Worker const Port* port() const { 103*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(network_thread_); 104*d9f75844SAndroid Build Coastguard Worker return port_.get(); 105*d9f75844SAndroid Build Coastguard Worker } 106*d9f75844SAndroid Build Coastguard Worker 107*d9f75844SAndroid Build Coastguard Worker // Determines whether the connection has finished connecting. This can only 108*d9f75844SAndroid Build Coastguard Worker // be false for TCP connections. 109*d9f75844SAndroid Build Coastguard Worker bool connected() const; 110*d9f75844SAndroid Build Coastguard Worker bool weak() const; 111*d9f75844SAndroid Build Coastguard Worker bool active() const; 112*d9f75844SAndroid Build Coastguard Worker 113*d9f75844SAndroid Build Coastguard Worker // A connection is dead if it can be safely deleted. 114*d9f75844SAndroid Build Coastguard Worker bool dead(int64_t now) const; 115*d9f75844SAndroid Build Coastguard Worker 116*d9f75844SAndroid Build Coastguard Worker // Estimate of the round-trip time over this connection. 117*d9f75844SAndroid Build Coastguard Worker int rtt() const; 118*d9f75844SAndroid Build Coastguard Worker 119*d9f75844SAndroid Build Coastguard Worker int unwritable_timeout() const; 120*d9f75844SAndroid Build Coastguard Worker void set_unwritable_timeout(const absl::optional<int>& value_ms); 121*d9f75844SAndroid Build Coastguard Worker int unwritable_min_checks() const; 122*d9f75844SAndroid Build Coastguard Worker void set_unwritable_min_checks(const absl::optional<int>& value); 123*d9f75844SAndroid Build Coastguard Worker int inactive_timeout() const; 124*d9f75844SAndroid Build Coastguard Worker void set_inactive_timeout(const absl::optional<int>& value); 125*d9f75844SAndroid Build Coastguard Worker 126*d9f75844SAndroid Build Coastguard Worker // Gets the `ConnectionInfo` stats, where `best_connection` has not been 127*d9f75844SAndroid Build Coastguard Worker // populated (default value false). 128*d9f75844SAndroid Build Coastguard Worker ConnectionInfo stats(); 129*d9f75844SAndroid Build Coastguard Worker 130*d9f75844SAndroid Build Coastguard Worker sigslot::signal1<Connection*> SignalStateChange; 131*d9f75844SAndroid Build Coastguard Worker 132*d9f75844SAndroid Build Coastguard Worker // Sent when the connection has decided that it is no longer of value. It 133*d9f75844SAndroid Build Coastguard Worker // will delete itself immediately after this call. 134*d9f75844SAndroid Build Coastguard Worker sigslot::signal1<Connection*> SignalDestroyed; 135*d9f75844SAndroid Build Coastguard Worker 136*d9f75844SAndroid Build Coastguard Worker // The connection can send and receive packets asynchronously. This matches 137*d9f75844SAndroid Build Coastguard Worker // the interface of AsyncPacketSocket, which may use UDP or TCP under the 138*d9f75844SAndroid Build Coastguard Worker // covers. 139*d9f75844SAndroid Build Coastguard Worker virtual int Send(const void* data, 140*d9f75844SAndroid Build Coastguard Worker size_t size, 141*d9f75844SAndroid Build Coastguard Worker const rtc::PacketOptions& options) = 0; 142*d9f75844SAndroid Build Coastguard Worker 143*d9f75844SAndroid Build Coastguard Worker // Error if Send() returns < 0 144*d9f75844SAndroid Build Coastguard Worker virtual int GetError() = 0; 145*d9f75844SAndroid Build Coastguard Worker 146*d9f75844SAndroid Build Coastguard Worker sigslot::signal4<Connection*, const char*, size_t, int64_t> SignalReadPacket; 147*d9f75844SAndroid Build Coastguard Worker 148*d9f75844SAndroid Build Coastguard Worker sigslot::signal1<Connection*> SignalReadyToSend; 149*d9f75844SAndroid Build Coastguard Worker 150*d9f75844SAndroid Build Coastguard Worker // Called when a packet is received on this connection. 151*d9f75844SAndroid Build Coastguard Worker void OnReadPacket(const char* data, size_t size, int64_t packet_time_us); 152*d9f75844SAndroid Build Coastguard Worker 153*d9f75844SAndroid Build Coastguard Worker // Called when the socket is currently able to send. 154*d9f75844SAndroid Build Coastguard Worker void OnReadyToSend(); 155*d9f75844SAndroid Build Coastguard Worker 156*d9f75844SAndroid Build Coastguard Worker // Called when a connection is determined to be no longer useful to us. We 157*d9f75844SAndroid Build Coastguard Worker // still keep it around in case the other side wants to use it. But we can 158*d9f75844SAndroid Build Coastguard Worker // safely stop pinging on it and we can allow it to time out if the other 159*d9f75844SAndroid Build Coastguard Worker // side stops using it as well. 160*d9f75844SAndroid Build Coastguard Worker bool pruned() const; 161*d9f75844SAndroid Build Coastguard Worker void Prune(); 162*d9f75844SAndroid Build Coastguard Worker 163*d9f75844SAndroid Build Coastguard Worker bool use_candidate_attr() const; 164*d9f75844SAndroid Build Coastguard Worker void set_use_candidate_attr(bool enable); 165*d9f75844SAndroid Build Coastguard Worker 166*d9f75844SAndroid Build Coastguard Worker void set_nomination(uint32_t value); 167*d9f75844SAndroid Build Coastguard Worker 168*d9f75844SAndroid Build Coastguard Worker uint32_t remote_nomination() const; 169*d9f75844SAndroid Build Coastguard Worker // One or several pairs may be nominated based on if Regular or Aggressive 170*d9f75844SAndroid Build Coastguard Worker // Nomination is used. https://tools.ietf.org/html/rfc5245#section-8 171*d9f75844SAndroid Build Coastguard Worker // `nominated` is defined both for the controlling or controlled agent based 172*d9f75844SAndroid Build Coastguard Worker // on if a nomination has been pinged or acknowledged. The controlled agent 173*d9f75844SAndroid Build Coastguard Worker // gets its `remote_nomination_` set when pinged by the controlling agent with 174*d9f75844SAndroid Build Coastguard Worker // a nomination value. The controlling agent gets its `acked_nomination_` set 175*d9f75844SAndroid Build Coastguard Worker // when receiving a response to a nominating ping. 176*d9f75844SAndroid Build Coastguard Worker bool nominated() const; 177*d9f75844SAndroid Build Coastguard Worker 178*d9f75844SAndroid Build Coastguard Worker int receiving_timeout() const; 179*d9f75844SAndroid Build Coastguard Worker void set_receiving_timeout(absl::optional<int> receiving_timeout_ms); 180*d9f75844SAndroid Build Coastguard Worker 181*d9f75844SAndroid Build Coastguard Worker // Deletes a `Connection` instance is by calling the `DestroyConnection` 182*d9f75844SAndroid Build Coastguard Worker // method in `Port`. 183*d9f75844SAndroid Build Coastguard Worker // Note: When the function returns, the object has been deleted. 184*d9f75844SAndroid Build Coastguard Worker void Destroy(); 185*d9f75844SAndroid Build Coastguard Worker 186*d9f75844SAndroid Build Coastguard Worker // Signals object destruction, releases outstanding references and performs 187*d9f75844SAndroid Build Coastguard Worker // final logging. 188*d9f75844SAndroid Build Coastguard Worker // The function will return `true` when shutdown was performed, signals 189*d9f75844SAndroid Build Coastguard Worker // emitted and outstanding references released. If the function returns 190*d9f75844SAndroid Build Coastguard Worker // `false`, `Shutdown()` has previously been called. 191*d9f75844SAndroid Build Coastguard Worker bool Shutdown(); 192*d9f75844SAndroid Build Coastguard Worker 193*d9f75844SAndroid Build Coastguard Worker // Prunes the connection and sets its state to STATE_FAILED, 194*d9f75844SAndroid Build Coastguard Worker // It will not be used or send pings although it can still receive packets. 195*d9f75844SAndroid Build Coastguard Worker void FailAndPrune(); 196*d9f75844SAndroid Build Coastguard Worker 197*d9f75844SAndroid Build Coastguard Worker // Checks that the state of this connection is up-to-date. The argument is 198*d9f75844SAndroid Build Coastguard Worker // the current time, which is compared against various timeouts. 199*d9f75844SAndroid Build Coastguard Worker void UpdateState(int64_t now); 200*d9f75844SAndroid Build Coastguard Worker 201*d9f75844SAndroid Build Coastguard Worker void UpdateLocalIceParameters(int component, 202*d9f75844SAndroid Build Coastguard Worker absl::string_view username_fragment, 203*d9f75844SAndroid Build Coastguard Worker absl::string_view password); 204*d9f75844SAndroid Build Coastguard Worker 205*d9f75844SAndroid Build Coastguard Worker // Called when this connection should try checking writability again. 206*d9f75844SAndroid Build Coastguard Worker int64_t last_ping_sent() const; 207*d9f75844SAndroid Build Coastguard Worker void Ping(int64_t now); 208*d9f75844SAndroid Build Coastguard Worker void ReceivedPingResponse( 209*d9f75844SAndroid Build Coastguard Worker int rtt, 210*d9f75844SAndroid Build Coastguard Worker absl::string_view request_id, 211*d9f75844SAndroid Build Coastguard Worker const absl::optional<uint32_t>& nomination = absl::nullopt); 212*d9f75844SAndroid Build Coastguard Worker std::unique_ptr<IceMessage> BuildPingRequest() RTC_RUN_ON(network_thread_); 213*d9f75844SAndroid Build Coastguard Worker 214*d9f75844SAndroid Build Coastguard Worker int64_t last_ping_response_received() const; 215*d9f75844SAndroid Build Coastguard Worker const absl::optional<std::string>& last_ping_id_received() const; 216*d9f75844SAndroid Build Coastguard Worker 217*d9f75844SAndroid Build Coastguard Worker // Used to check if any STUN ping response has been received. 218*d9f75844SAndroid Build Coastguard Worker int rtt_samples() const; 219*d9f75844SAndroid Build Coastguard Worker 220*d9f75844SAndroid Build Coastguard Worker // Called whenever a valid ping is received on this connection. This is 221*d9f75844SAndroid Build Coastguard Worker // public because the connection intercepts the first ping for us. 222*d9f75844SAndroid Build Coastguard Worker int64_t last_ping_received() const; 223*d9f75844SAndroid Build Coastguard Worker 224*d9f75844SAndroid Build Coastguard Worker void ReceivedPing( 225*d9f75844SAndroid Build Coastguard Worker const absl::optional<std::string>& request_id = absl::nullopt); 226*d9f75844SAndroid Build Coastguard Worker // Handles the binding request; sends a response if this is a valid request. 227*d9f75844SAndroid Build Coastguard Worker void HandleStunBindingOrGoogPingRequest(IceMessage* msg); 228*d9f75844SAndroid Build Coastguard Worker // Handles the piggyback acknowledgement of the lastest connectivity check 229*d9f75844SAndroid Build Coastguard Worker // that the remote peer has received, if it is indicated in the incoming 230*d9f75844SAndroid Build Coastguard Worker // connectivity check from the peer. 231*d9f75844SAndroid Build Coastguard Worker void HandlePiggybackCheckAcknowledgementIfAny(StunMessage* msg); 232*d9f75844SAndroid Build Coastguard Worker // Timestamp when data was last sent (or attempted to be sent). 233*d9f75844SAndroid Build Coastguard Worker int64_t last_send_data() const; 234*d9f75844SAndroid Build Coastguard Worker int64_t last_data_received() const; 235*d9f75844SAndroid Build Coastguard Worker 236*d9f75844SAndroid Build Coastguard Worker // Debugging description of this connection 237*d9f75844SAndroid Build Coastguard Worker std::string ToDebugId() const; 238*d9f75844SAndroid Build Coastguard Worker std::string ToString() const; 239*d9f75844SAndroid Build Coastguard Worker std::string ToSensitiveString() const; 240*d9f75844SAndroid Build Coastguard Worker // Structured description of this candidate pair. 241*d9f75844SAndroid Build Coastguard Worker const webrtc::IceCandidatePairDescription& ToLogDescription(); 242*d9f75844SAndroid Build Coastguard Worker void set_ice_event_log(webrtc::IceEventLog* ice_event_log); 243*d9f75844SAndroid Build Coastguard Worker 244*d9f75844SAndroid Build Coastguard Worker // Prints pings_since_last_response_ into a string. 245*d9f75844SAndroid Build Coastguard Worker void PrintPingsSinceLastResponse(std::string* pings, size_t max); 246*d9f75844SAndroid Build Coastguard Worker 247*d9f75844SAndroid Build Coastguard Worker // `set_selected` is only used for logging in ToString above. The flag is 248*d9f75844SAndroid Build Coastguard Worker // set true by P2PTransportChannel for its selected candidate pair. 249*d9f75844SAndroid Build Coastguard Worker // TODO(tommi): Remove `selected()` once not referenced downstream. 250*d9f75844SAndroid Build Coastguard Worker bool selected() const; 251*d9f75844SAndroid Build Coastguard Worker void set_selected(bool selected); 252*d9f75844SAndroid Build Coastguard Worker 253*d9f75844SAndroid Build Coastguard Worker // This signal will be fired if this connection is nominated by the 254*d9f75844SAndroid Build Coastguard Worker // controlling side. 255*d9f75844SAndroid Build Coastguard Worker sigslot::signal1<Connection*> SignalNominated; 256*d9f75844SAndroid Build Coastguard Worker 257*d9f75844SAndroid Build Coastguard Worker IceCandidatePairState state() const; 258*d9f75844SAndroid Build Coastguard Worker 259*d9f75844SAndroid Build Coastguard Worker int num_pings_sent() const; 260*d9f75844SAndroid Build Coastguard Worker 261*d9f75844SAndroid Build Coastguard Worker uint32_t ComputeNetworkCost() const; 262*d9f75844SAndroid Build Coastguard Worker 263*d9f75844SAndroid Build Coastguard Worker // Update the ICE password and/or generation of the remote candidate if the 264*d9f75844SAndroid Build Coastguard Worker // ufrag in `params` matches the candidate's ufrag, and the 265*d9f75844SAndroid Build Coastguard Worker // candidate's password and/or ufrag has not been set. 266*d9f75844SAndroid Build Coastguard Worker void MaybeSetRemoteIceParametersAndGeneration(const IceParameters& params, 267*d9f75844SAndroid Build Coastguard Worker int generation); 268*d9f75844SAndroid Build Coastguard Worker 269*d9f75844SAndroid Build Coastguard Worker // If `remote_candidate_` is peer reflexive and is equivalent to 270*d9f75844SAndroid Build Coastguard Worker // `new_candidate` except the type, update `remote_candidate_` to 271*d9f75844SAndroid Build Coastguard Worker // `new_candidate`. 272*d9f75844SAndroid Build Coastguard Worker void MaybeUpdatePeerReflexiveCandidate(const Candidate& new_candidate); 273*d9f75844SAndroid Build Coastguard Worker 274*d9f75844SAndroid Build Coastguard Worker // Returns the last received time of any data, stun request, or stun 275*d9f75844SAndroid Build Coastguard Worker // response in milliseconds 276*d9f75844SAndroid Build Coastguard Worker int64_t last_received() const; 277*d9f75844SAndroid Build Coastguard Worker // Returns the last time when the connection changed its receiving state. 278*d9f75844SAndroid Build Coastguard Worker int64_t receiving_unchanged_since() const; 279*d9f75844SAndroid Build Coastguard Worker 280*d9f75844SAndroid Build Coastguard Worker // Constructs the prflx priority as described in 281*d9f75844SAndroid Build Coastguard Worker // https://datatracker.ietf.org/doc/html/rfc5245#section-4.1.2.1 282*d9f75844SAndroid Build Coastguard Worker uint32_t prflx_priority() const; 283*d9f75844SAndroid Build Coastguard Worker 284*d9f75844SAndroid Build Coastguard Worker bool stable(int64_t now) const; 285*d9f75844SAndroid Build Coastguard Worker 286*d9f75844SAndroid Build Coastguard Worker // Check if we sent `val` pings without receving a response. 287*d9f75844SAndroid Build Coastguard Worker bool TooManyOutstandingPings(const absl::optional<int>& val) const; 288*d9f75844SAndroid Build Coastguard Worker 289*d9f75844SAndroid Build Coastguard Worker // Called by Port when the network cost changes. 290*d9f75844SAndroid Build Coastguard Worker void SetLocalCandidateNetworkCost(uint16_t cost); 291*d9f75844SAndroid Build Coastguard Worker 292*d9f75844SAndroid Build Coastguard Worker void SetIceFieldTrials(const IceFieldTrials* field_trials); GetRttEstimate()293*d9f75844SAndroid Build Coastguard Worker const rtc::EventBasedExponentialMovingAverage& GetRttEstimate() const { 294*d9f75844SAndroid Build Coastguard Worker return rtt_estimate_; 295*d9f75844SAndroid Build Coastguard Worker } 296*d9f75844SAndroid Build Coastguard Worker 297*d9f75844SAndroid Build Coastguard Worker // Reset the connection to a state of a newly connected. 298*d9f75844SAndroid Build Coastguard Worker // - STATE_WRITE_INIT 299*d9f75844SAndroid Build Coastguard Worker // - receving = false 300*d9f75844SAndroid Build Coastguard Worker // - throw away all pending request 301*d9f75844SAndroid Build Coastguard Worker // - reset RttEstimate 302*d9f75844SAndroid Build Coastguard Worker // 303*d9f75844SAndroid Build Coastguard Worker // Keep the following unchanged: 304*d9f75844SAndroid Build Coastguard Worker // - connected 305*d9f75844SAndroid Build Coastguard Worker // - remote_candidate 306*d9f75844SAndroid Build Coastguard Worker // - statistics 307*d9f75844SAndroid Build Coastguard Worker // 308*d9f75844SAndroid Build Coastguard Worker // Does not trigger SignalStateChange 309*d9f75844SAndroid Build Coastguard Worker void ForgetLearnedState(); 310*d9f75844SAndroid Build Coastguard Worker 311*d9f75844SAndroid Build Coastguard Worker void SendStunBindingResponse(const StunMessage* message); 312*d9f75844SAndroid Build Coastguard Worker void SendGoogPingResponse(const StunMessage* message); 313*d9f75844SAndroid Build Coastguard Worker void SendResponseMessage(const StunMessage& response); 314*d9f75844SAndroid Build Coastguard Worker 315*d9f75844SAndroid Build Coastguard Worker // An accessor for unit tests. PortForTest()316*d9f75844SAndroid Build Coastguard Worker Port* PortForTest() { return port_.get(); } PortForTest()317*d9f75844SAndroid Build Coastguard Worker const Port* PortForTest() const { return port_.get(); } 318*d9f75844SAndroid Build Coastguard Worker 319*d9f75844SAndroid Build Coastguard Worker // Public for unit tests. 320*d9f75844SAndroid Build Coastguard Worker uint32_t acked_nomination() const; 321*d9f75844SAndroid Build Coastguard Worker void set_remote_nomination(uint32_t remote_nomination); 322*d9f75844SAndroid Build Coastguard Worker 323*d9f75844SAndroid Build Coastguard Worker protected: 324*d9f75844SAndroid Build Coastguard Worker // A ConnectionRequest is a simple STUN ping used to determine writability. 325*d9f75844SAndroid Build Coastguard Worker class ConnectionRequest; 326*d9f75844SAndroid Build Coastguard Worker 327*d9f75844SAndroid Build Coastguard Worker // Constructs a new connection to the given remote port. 328*d9f75844SAndroid Build Coastguard Worker Connection(rtc::WeakPtr<Port> port, size_t index, const Candidate& candidate); 329*d9f75844SAndroid Build Coastguard Worker 330*d9f75844SAndroid Build Coastguard Worker // Called back when StunRequestManager has a stun packet to send 331*d9f75844SAndroid Build Coastguard Worker void OnSendStunPacket(const void* data, size_t size, StunRequest* req); 332*d9f75844SAndroid Build Coastguard Worker 333*d9f75844SAndroid Build Coastguard Worker // Callbacks from ConnectionRequest 334*d9f75844SAndroid Build Coastguard Worker virtual void OnConnectionRequestResponse(StunRequest* req, 335*d9f75844SAndroid Build Coastguard Worker StunMessage* response); 336*d9f75844SAndroid Build Coastguard Worker void OnConnectionRequestErrorResponse(ConnectionRequest* req, 337*d9f75844SAndroid Build Coastguard Worker StunMessage* response) 338*d9f75844SAndroid Build Coastguard Worker RTC_RUN_ON(network_thread_); 339*d9f75844SAndroid Build Coastguard Worker void OnConnectionRequestTimeout(ConnectionRequest* req) 340*d9f75844SAndroid Build Coastguard Worker RTC_RUN_ON(network_thread_); 341*d9f75844SAndroid Build Coastguard Worker void OnConnectionRequestSent(ConnectionRequest* req) 342*d9f75844SAndroid Build Coastguard Worker RTC_RUN_ON(network_thread_); 343*d9f75844SAndroid Build Coastguard Worker 344*d9f75844SAndroid Build Coastguard Worker bool rtt_converged() const; 345*d9f75844SAndroid Build Coastguard Worker 346*d9f75844SAndroid Build Coastguard Worker // If the response is not received within 2 * RTT, the response is assumed to 347*d9f75844SAndroid Build Coastguard Worker // be missing. 348*d9f75844SAndroid Build Coastguard Worker bool missing_responses(int64_t now) const; 349*d9f75844SAndroid Build Coastguard Worker 350*d9f75844SAndroid Build Coastguard Worker // Changes the state and signals if necessary. 351*d9f75844SAndroid Build Coastguard Worker void set_write_state(WriteState value); 352*d9f75844SAndroid Build Coastguard Worker void UpdateReceiving(int64_t now); 353*d9f75844SAndroid Build Coastguard Worker void set_state(IceCandidatePairState state); 354*d9f75844SAndroid Build Coastguard Worker void set_connected(bool value); 355*d9f75844SAndroid Build Coastguard Worker 356*d9f75844SAndroid Build Coastguard Worker // The local port where this connection sends and receives packets. port()357*d9f75844SAndroid Build Coastguard Worker Port* port() { return port_.get(); } 358*d9f75844SAndroid Build Coastguard Worker 359*d9f75844SAndroid Build Coastguard Worker // NOTE: A pointer to the network thread is held by `port_` so in theory we 360*d9f75844SAndroid Build Coastguard Worker // shouldn't need to hold on to this pointer here, but rather defer to 361*d9f75844SAndroid Build Coastguard Worker // port_->thread(). However, some tests delete the classes in the wrong order 362*d9f75844SAndroid Build Coastguard Worker // so `port_` may be deleted before an instance of this class is deleted. 363*d9f75844SAndroid Build Coastguard Worker // TODO(tommi): This ^^^ should be fixed. 364*d9f75844SAndroid Build Coastguard Worker webrtc::TaskQueueBase* const network_thread_; 365*d9f75844SAndroid Build Coastguard Worker const uint32_t id_; 366*d9f75844SAndroid Build Coastguard Worker rtc::WeakPtr<Port> port_; 367*d9f75844SAndroid Build Coastguard Worker Candidate local_candidate_ RTC_GUARDED_BY(network_thread_); 368*d9f75844SAndroid Build Coastguard Worker Candidate remote_candidate_; 369*d9f75844SAndroid Build Coastguard Worker 370*d9f75844SAndroid Build Coastguard Worker ConnectionInfo stats_; 371*d9f75844SAndroid Build Coastguard Worker rtc::RateTracker recv_rate_tracker_; 372*d9f75844SAndroid Build Coastguard Worker rtc::RateTracker send_rate_tracker_; 373*d9f75844SAndroid Build Coastguard Worker int64_t last_send_data_ = 0; 374*d9f75844SAndroid Build Coastguard Worker 375*d9f75844SAndroid Build Coastguard Worker private: 376*d9f75844SAndroid Build Coastguard Worker // Update the local candidate based on the mapped address attribute. 377*d9f75844SAndroid Build Coastguard Worker // If the local candidate changed, fires SignalStateChange. 378*d9f75844SAndroid Build Coastguard Worker void MaybeUpdateLocalCandidate(StunRequest* request, StunMessage* response) 379*d9f75844SAndroid Build Coastguard Worker RTC_RUN_ON(network_thread_); 380*d9f75844SAndroid Build Coastguard Worker 381*d9f75844SAndroid Build Coastguard Worker void LogCandidatePairConfig(webrtc::IceCandidatePairConfigType type) 382*d9f75844SAndroid Build Coastguard Worker RTC_RUN_ON(network_thread_); 383*d9f75844SAndroid Build Coastguard Worker void LogCandidatePairEvent(webrtc::IceCandidatePairEventType type, 384*d9f75844SAndroid Build Coastguard Worker uint32_t transaction_id) 385*d9f75844SAndroid Build Coastguard Worker RTC_RUN_ON(network_thread_); 386*d9f75844SAndroid Build Coastguard Worker 387*d9f75844SAndroid Build Coastguard Worker // Check if this IceMessage is identical 388*d9f75844SAndroid Build Coastguard Worker // to last message ack:ed STUN_BINDING_REQUEST. 389*d9f75844SAndroid Build Coastguard Worker bool ShouldSendGoogPing(const StunMessage* message) 390*d9f75844SAndroid Build Coastguard Worker RTC_RUN_ON(network_thread_); 391*d9f75844SAndroid Build Coastguard Worker 392*d9f75844SAndroid Build Coastguard Worker WriteState write_state_ RTC_GUARDED_BY(network_thread_); 393*d9f75844SAndroid Build Coastguard Worker bool receiving_ RTC_GUARDED_BY(network_thread_); 394*d9f75844SAndroid Build Coastguard Worker bool connected_ RTC_GUARDED_BY(network_thread_); 395*d9f75844SAndroid Build Coastguard Worker bool pruned_ RTC_GUARDED_BY(network_thread_); 396*d9f75844SAndroid Build Coastguard Worker bool selected_ RTC_GUARDED_BY(network_thread_) = false; 397*d9f75844SAndroid Build Coastguard Worker // By default `use_candidate_attr_` flag will be true, 398*d9f75844SAndroid Build Coastguard Worker // as we will be using aggressive nomination. 399*d9f75844SAndroid Build Coastguard Worker // But when peer is ice-lite, this flag "must" be initialized to false and 400*d9f75844SAndroid Build Coastguard Worker // turn on when connection becomes "best connection". 401*d9f75844SAndroid Build Coastguard Worker bool use_candidate_attr_ RTC_GUARDED_BY(network_thread_); 402*d9f75844SAndroid Build Coastguard Worker // Used by the controlling side to indicate that this connection will be 403*d9f75844SAndroid Build Coastguard Worker // selected for transmission if the peer supports ICE-renomination when this 404*d9f75844SAndroid Build Coastguard Worker // value is positive. A larger-value indicates that a connection is nominated 405*d9f75844SAndroid Build Coastguard Worker // later and should be selected by the controlled side with higher precedence. 406*d9f75844SAndroid Build Coastguard Worker // A zero-value indicates not nominating this connection. 407*d9f75844SAndroid Build Coastguard Worker uint32_t nomination_ RTC_GUARDED_BY(network_thread_) = 0; 408*d9f75844SAndroid Build Coastguard Worker // The last nomination that has been acknowledged. 409*d9f75844SAndroid Build Coastguard Worker uint32_t acked_nomination_ RTC_GUARDED_BY(network_thread_) = 0; 410*d9f75844SAndroid Build Coastguard Worker // Used by the controlled side to remember the nomination value received from 411*d9f75844SAndroid Build Coastguard Worker // the controlling side. When the peer does not support ICE re-nomination, its 412*d9f75844SAndroid Build Coastguard Worker // value will be 1 if the connection has been nominated. 413*d9f75844SAndroid Build Coastguard Worker uint32_t remote_nomination_ RTC_GUARDED_BY(network_thread_) = 0; 414*d9f75844SAndroid Build Coastguard Worker 415*d9f75844SAndroid Build Coastguard Worker StunRequestManager requests_ RTC_GUARDED_BY(network_thread_); 416*d9f75844SAndroid Build Coastguard Worker int rtt_ RTC_GUARDED_BY(network_thread_); 417*d9f75844SAndroid Build Coastguard Worker int rtt_samples_ RTC_GUARDED_BY(network_thread_) = 0; 418*d9f75844SAndroid Build Coastguard Worker // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-totalroundtriptime 419*d9f75844SAndroid Build Coastguard Worker uint64_t total_round_trip_time_ms_ RTC_GUARDED_BY(network_thread_) = 0; 420*d9f75844SAndroid Build Coastguard Worker // https://w3c.github.io/webrtc-stats/#dom-rtcicecandidatepairstats-currentroundtriptime 421*d9f75844SAndroid Build Coastguard Worker absl::optional<uint32_t> current_round_trip_time_ms_ 422*d9f75844SAndroid Build Coastguard Worker RTC_GUARDED_BY(network_thread_); 423*d9f75844SAndroid Build Coastguard Worker int64_t last_ping_sent_ RTC_GUARDED_BY( 424*d9f75844SAndroid Build Coastguard Worker network_thread_); // last time we sent a ping to the other side 425*d9f75844SAndroid Build Coastguard Worker int64_t last_ping_received_ 426*d9f75844SAndroid Build Coastguard Worker RTC_GUARDED_BY(network_thread_); // last time we received a ping from the 427*d9f75844SAndroid Build Coastguard Worker // other side 428*d9f75844SAndroid Build Coastguard Worker int64_t last_data_received_ RTC_GUARDED_BY(network_thread_); 429*d9f75844SAndroid Build Coastguard Worker int64_t last_ping_response_received_ RTC_GUARDED_BY(network_thread_); 430*d9f75844SAndroid Build Coastguard Worker int64_t receiving_unchanged_since_ RTC_GUARDED_BY(network_thread_) = 0; 431*d9f75844SAndroid Build Coastguard Worker std::vector<SentPing> pings_since_last_response_ 432*d9f75844SAndroid Build Coastguard Worker RTC_GUARDED_BY(network_thread_); 433*d9f75844SAndroid Build Coastguard Worker // Transaction ID of the last connectivity check received. Null if having not 434*d9f75844SAndroid Build Coastguard Worker // received a ping yet. 435*d9f75844SAndroid Build Coastguard Worker absl::optional<std::string> last_ping_id_received_ 436*d9f75844SAndroid Build Coastguard Worker RTC_GUARDED_BY(network_thread_); 437*d9f75844SAndroid Build Coastguard Worker 438*d9f75844SAndroid Build Coastguard Worker absl::optional<int> unwritable_timeout_ RTC_GUARDED_BY(network_thread_); 439*d9f75844SAndroid Build Coastguard Worker absl::optional<int> unwritable_min_checks_ RTC_GUARDED_BY(network_thread_); 440*d9f75844SAndroid Build Coastguard Worker absl::optional<int> inactive_timeout_ RTC_GUARDED_BY(network_thread_); 441*d9f75844SAndroid Build Coastguard Worker 442*d9f75844SAndroid Build Coastguard Worker IceCandidatePairState state_ RTC_GUARDED_BY(network_thread_); 443*d9f75844SAndroid Build Coastguard Worker // Time duration to switch from receiving to not receiving. 444*d9f75844SAndroid Build Coastguard Worker absl::optional<int> receiving_timeout_ RTC_GUARDED_BY(network_thread_); 445*d9f75844SAndroid Build Coastguard Worker const int64_t time_created_ms_ RTC_GUARDED_BY(network_thread_); 446*d9f75844SAndroid Build Coastguard Worker const int64_t delta_internal_unix_epoch_ms_ RTC_GUARDED_BY(network_thread_); 447*d9f75844SAndroid Build Coastguard Worker int num_pings_sent_ RTC_GUARDED_BY(network_thread_) = 0; 448*d9f75844SAndroid Build Coastguard Worker 449*d9f75844SAndroid Build Coastguard Worker absl::optional<webrtc::IceCandidatePairDescription> log_description_ 450*d9f75844SAndroid Build Coastguard Worker RTC_GUARDED_BY(network_thread_); 451*d9f75844SAndroid Build Coastguard Worker webrtc::IceEventLog* ice_event_log_ RTC_GUARDED_BY(network_thread_) = nullptr; 452*d9f75844SAndroid Build Coastguard Worker 453*d9f75844SAndroid Build Coastguard Worker // GOOG_PING_REQUEST is sent in place of STUN_BINDING_REQUEST 454*d9f75844SAndroid Build Coastguard Worker // if configured via field trial, the remote peer supports it (signaled 455*d9f75844SAndroid Build Coastguard Worker // in STUN_BINDING) and if the last STUN BINDING is identical to the one 456*d9f75844SAndroid Build Coastguard Worker // that is about to be sent. 457*d9f75844SAndroid Build Coastguard Worker absl::optional<bool> remote_support_goog_ping_ 458*d9f75844SAndroid Build Coastguard Worker RTC_GUARDED_BY(network_thread_); 459*d9f75844SAndroid Build Coastguard Worker std::unique_ptr<StunMessage> cached_stun_binding_ 460*d9f75844SAndroid Build Coastguard Worker RTC_GUARDED_BY(network_thread_); 461*d9f75844SAndroid Build Coastguard Worker 462*d9f75844SAndroid Build Coastguard Worker const IceFieldTrials* field_trials_; 463*d9f75844SAndroid Build Coastguard Worker rtc::EventBasedExponentialMovingAverage rtt_estimate_ 464*d9f75844SAndroid Build Coastguard Worker RTC_GUARDED_BY(network_thread_); 465*d9f75844SAndroid Build Coastguard Worker }; 466*d9f75844SAndroid Build Coastguard Worker 467*d9f75844SAndroid Build Coastguard Worker // ProxyConnection defers all the interesting work to the port. 468*d9f75844SAndroid Build Coastguard Worker class ProxyConnection : public Connection { 469*d9f75844SAndroid Build Coastguard Worker public: 470*d9f75844SAndroid Build Coastguard Worker ProxyConnection(rtc::WeakPtr<Port> port, 471*d9f75844SAndroid Build Coastguard Worker size_t index, 472*d9f75844SAndroid Build Coastguard Worker const Candidate& remote_candidate); 473*d9f75844SAndroid Build Coastguard Worker 474*d9f75844SAndroid Build Coastguard Worker int Send(const void* data, 475*d9f75844SAndroid Build Coastguard Worker size_t size, 476*d9f75844SAndroid Build Coastguard Worker const rtc::PacketOptions& options) override; 477*d9f75844SAndroid Build Coastguard Worker int GetError() override; 478*d9f75844SAndroid Build Coastguard Worker 479*d9f75844SAndroid Build Coastguard Worker private: 480*d9f75844SAndroid Build Coastguard Worker int error_ = 0; 481*d9f75844SAndroid Build Coastguard Worker }; 482*d9f75844SAndroid Build Coastguard Worker 483*d9f75844SAndroid Build Coastguard Worker } // namespace cricket 484*d9f75844SAndroid Build Coastguard Worker 485*d9f75844SAndroid Build Coastguard Worker #endif // P2P_BASE_CONNECTION_H_ 486