1*d9f75844SAndroid Build Coastguard Worker /* 2*d9f75844SAndroid Build Coastguard Worker * Copyright 2004 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_PORT_H_ 12*d9f75844SAndroid Build Coastguard Worker #define P2P_BASE_PORT_H_ 13*d9f75844SAndroid Build Coastguard Worker 14*d9f75844SAndroid Build Coastguard Worker #include <map> 15*d9f75844SAndroid Build Coastguard Worker #include <memory> 16*d9f75844SAndroid Build Coastguard Worker #include <set> 17*d9f75844SAndroid Build Coastguard Worker #include <string> 18*d9f75844SAndroid Build Coastguard Worker #include <utility> 19*d9f75844SAndroid Build Coastguard Worker #include <vector> 20*d9f75844SAndroid Build Coastguard Worker 21*d9f75844SAndroid Build Coastguard Worker #include "absl/strings/string_view.h" 22*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h" 23*d9f75844SAndroid Build Coastguard Worker #include "api/candidate.h" 24*d9f75844SAndroid Build Coastguard Worker #include "api/field_trials_view.h" 25*d9f75844SAndroid Build Coastguard Worker #include "api/packet_socket_factory.h" 26*d9f75844SAndroid Build Coastguard Worker #include "api/rtc_error.h" 27*d9f75844SAndroid Build Coastguard Worker #include "api/task_queue/task_queue_base.h" 28*d9f75844SAndroid Build Coastguard Worker #include "api/transport/field_trial_based_config.h" 29*d9f75844SAndroid Build Coastguard Worker #include "api/transport/stun.h" 30*d9f75844SAndroid Build Coastguard Worker #include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair.h" 31*d9f75844SAndroid Build Coastguard Worker #include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair_config.h" 32*d9f75844SAndroid Build Coastguard Worker #include "logging/rtc_event_log/ice_logger.h" 33*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/candidate_pair_interface.h" 34*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/connection.h" 35*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/connection_info.h" 36*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/p2p_constants.h" 37*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/port_interface.h" 38*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/stun_request.h" 39*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/async_packet_socket.h" 40*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/callback_list.h" 41*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h" 42*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/memory/always_valid_pointer.h" 43*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/net_helper.h" 44*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/network.h" 45*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/proxy_info.h" 46*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/rate_tracker.h" 47*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/socket_address.h" 48*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system/rtc_export.h" 49*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/third_party/sigslot/sigslot.h" 50*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/weak_ptr.h" 51*d9f75844SAndroid Build Coastguard Worker 52*d9f75844SAndroid Build Coastguard Worker namespace cricket { 53*d9f75844SAndroid Build Coastguard Worker 54*d9f75844SAndroid Build Coastguard Worker RTC_EXPORT extern const char LOCAL_PORT_TYPE[]; 55*d9f75844SAndroid Build Coastguard Worker RTC_EXPORT extern const char STUN_PORT_TYPE[]; 56*d9f75844SAndroid Build Coastguard Worker RTC_EXPORT extern const char PRFLX_PORT_TYPE[]; 57*d9f75844SAndroid Build Coastguard Worker RTC_EXPORT extern const char RELAY_PORT_TYPE[]; 58*d9f75844SAndroid Build Coastguard Worker 59*d9f75844SAndroid Build Coastguard Worker // RFC 6544, TCP candidate encoding rules. 60*d9f75844SAndroid Build Coastguard Worker extern const int DISCARD_PORT; 61*d9f75844SAndroid Build Coastguard Worker extern const char TCPTYPE_ACTIVE_STR[]; 62*d9f75844SAndroid Build Coastguard Worker extern const char TCPTYPE_PASSIVE_STR[]; 63*d9f75844SAndroid Build Coastguard Worker extern const char TCPTYPE_SIMOPEN_STR[]; 64*d9f75844SAndroid Build Coastguard Worker 65*d9f75844SAndroid Build Coastguard Worker // The type preference MUST be an integer from 0 to 126 inclusive. 66*d9f75844SAndroid Build Coastguard Worker // https://datatracker.ietf.org/doc/html/rfc5245#section-4.1.2.1 67*d9f75844SAndroid Build Coastguard Worker enum IcePriorityValue : uint8_t { 68*d9f75844SAndroid Build Coastguard Worker ICE_TYPE_PREFERENCE_RELAY_TLS = 0, 69*d9f75844SAndroid Build Coastguard Worker ICE_TYPE_PREFERENCE_RELAY_TCP = 1, 70*d9f75844SAndroid Build Coastguard Worker ICE_TYPE_PREFERENCE_RELAY_UDP = 2, 71*d9f75844SAndroid Build Coastguard Worker ICE_TYPE_PREFERENCE_PRFLX_TCP = 80, 72*d9f75844SAndroid Build Coastguard Worker ICE_TYPE_PREFERENCE_HOST_TCP = 90, 73*d9f75844SAndroid Build Coastguard Worker ICE_TYPE_PREFERENCE_SRFLX = 100, 74*d9f75844SAndroid Build Coastguard Worker ICE_TYPE_PREFERENCE_PRFLX = 110, 75*d9f75844SAndroid Build Coastguard Worker ICE_TYPE_PREFERENCE_HOST = 126 76*d9f75844SAndroid Build Coastguard Worker }; 77*d9f75844SAndroid Build Coastguard Worker 78*d9f75844SAndroid Build Coastguard Worker enum class MdnsNameRegistrationStatus { 79*d9f75844SAndroid Build Coastguard Worker // IP concealment with mDNS is not enabled or the name registration process is 80*d9f75844SAndroid Build Coastguard Worker // not started yet. 81*d9f75844SAndroid Build Coastguard Worker kNotStarted, 82*d9f75844SAndroid Build Coastguard Worker // A request to create and register an mDNS name for a local IP address of a 83*d9f75844SAndroid Build Coastguard Worker // host candidate is sent to the mDNS responder. 84*d9f75844SAndroid Build Coastguard Worker kInProgress, 85*d9f75844SAndroid Build Coastguard Worker // The name registration is complete and the created name is returned by the 86*d9f75844SAndroid Build Coastguard Worker // mDNS responder. 87*d9f75844SAndroid Build Coastguard Worker kCompleted, 88*d9f75844SAndroid Build Coastguard Worker }; 89*d9f75844SAndroid Build Coastguard Worker 90*d9f75844SAndroid Build Coastguard Worker // Stats that we can return about the port of a STUN candidate. 91*d9f75844SAndroid Build Coastguard Worker class StunStats { 92*d9f75844SAndroid Build Coastguard Worker public: 93*d9f75844SAndroid Build Coastguard Worker StunStats() = default; 94*d9f75844SAndroid Build Coastguard Worker StunStats(const StunStats&) = default; 95*d9f75844SAndroid Build Coastguard Worker ~StunStats() = default; 96*d9f75844SAndroid Build Coastguard Worker 97*d9f75844SAndroid Build Coastguard Worker StunStats& operator=(const StunStats& other) = default; 98*d9f75844SAndroid Build Coastguard Worker 99*d9f75844SAndroid Build Coastguard Worker int stun_binding_requests_sent = 0; 100*d9f75844SAndroid Build Coastguard Worker int stun_binding_responses_received = 0; 101*d9f75844SAndroid Build Coastguard Worker double stun_binding_rtt_ms_total = 0; 102*d9f75844SAndroid Build Coastguard Worker double stun_binding_rtt_ms_squared_total = 0; 103*d9f75844SAndroid Build Coastguard Worker }; 104*d9f75844SAndroid Build Coastguard Worker 105*d9f75844SAndroid Build Coastguard Worker // Stats that we can return about a candidate. 106*d9f75844SAndroid Build Coastguard Worker class CandidateStats { 107*d9f75844SAndroid Build Coastguard Worker public: 108*d9f75844SAndroid Build Coastguard Worker CandidateStats() = default; 109*d9f75844SAndroid Build Coastguard Worker CandidateStats(const CandidateStats&) = default; 110*d9f75844SAndroid Build Coastguard Worker CandidateStats(CandidateStats&&) = default; 111*d9f75844SAndroid Build Coastguard Worker CandidateStats(Candidate candidate, 112*d9f75844SAndroid Build Coastguard Worker absl::optional<StunStats> stats = absl::nullopt) candidate_(std::move (candidate))113*d9f75844SAndroid Build Coastguard Worker : candidate_(std::move(candidate)), stun_stats_(std::move(stats)) {} 114*d9f75844SAndroid Build Coastguard Worker ~CandidateStats() = default; 115*d9f75844SAndroid Build Coastguard Worker 116*d9f75844SAndroid Build Coastguard Worker CandidateStats& operator=(const CandidateStats& other) = default; 117*d9f75844SAndroid Build Coastguard Worker candidate()118*d9f75844SAndroid Build Coastguard Worker const Candidate& candidate() const { return candidate_; } 119*d9f75844SAndroid Build Coastguard Worker stun_stats()120*d9f75844SAndroid Build Coastguard Worker const absl::optional<StunStats>& stun_stats() const { return stun_stats_; } 121*d9f75844SAndroid Build Coastguard Worker 122*d9f75844SAndroid Build Coastguard Worker private: 123*d9f75844SAndroid Build Coastguard Worker Candidate candidate_; 124*d9f75844SAndroid Build Coastguard Worker // STUN port stats if this candidate is a STUN candidate. 125*d9f75844SAndroid Build Coastguard Worker absl::optional<StunStats> stun_stats_; 126*d9f75844SAndroid Build Coastguard Worker }; 127*d9f75844SAndroid Build Coastguard Worker 128*d9f75844SAndroid Build Coastguard Worker typedef std::vector<CandidateStats> CandidateStatsList; 129*d9f75844SAndroid Build Coastguard Worker 130*d9f75844SAndroid Build Coastguard Worker const char* ProtoToString(ProtocolType proto); 131*d9f75844SAndroid Build Coastguard Worker absl::optional<ProtocolType> StringToProto(absl::string_view proto_name); 132*d9f75844SAndroid Build Coastguard Worker 133*d9f75844SAndroid Build Coastguard Worker struct ProtocolAddress { 134*d9f75844SAndroid Build Coastguard Worker rtc::SocketAddress address; 135*d9f75844SAndroid Build Coastguard Worker ProtocolType proto; 136*d9f75844SAndroid Build Coastguard Worker ProtocolAddressProtocolAddress137*d9f75844SAndroid Build Coastguard Worker ProtocolAddress(const rtc::SocketAddress& a, ProtocolType p) 138*d9f75844SAndroid Build Coastguard Worker : address(a), proto(p) {} 139*d9f75844SAndroid Build Coastguard Worker 140*d9f75844SAndroid Build Coastguard Worker bool operator==(const ProtocolAddress& o) const { 141*d9f75844SAndroid Build Coastguard Worker return address == o.address && proto == o.proto; 142*d9f75844SAndroid Build Coastguard Worker } 143*d9f75844SAndroid Build Coastguard Worker bool operator!=(const ProtocolAddress& o) const { return !(*this == o); } 144*d9f75844SAndroid Build Coastguard Worker }; 145*d9f75844SAndroid Build Coastguard Worker 146*d9f75844SAndroid Build Coastguard Worker struct IceCandidateErrorEvent { 147*d9f75844SAndroid Build Coastguard Worker IceCandidateErrorEvent() = default; IceCandidateErrorEventIceCandidateErrorEvent148*d9f75844SAndroid Build Coastguard Worker IceCandidateErrorEvent(absl::string_view address, 149*d9f75844SAndroid Build Coastguard Worker int port, 150*d9f75844SAndroid Build Coastguard Worker absl::string_view url, 151*d9f75844SAndroid Build Coastguard Worker int error_code, 152*d9f75844SAndroid Build Coastguard Worker absl::string_view error_text) 153*d9f75844SAndroid Build Coastguard Worker : address(std::move(address)), 154*d9f75844SAndroid Build Coastguard Worker port(port), 155*d9f75844SAndroid Build Coastguard Worker url(std::move(url)), 156*d9f75844SAndroid Build Coastguard Worker error_code(error_code), 157*d9f75844SAndroid Build Coastguard Worker error_text(std::move(error_text)) {} 158*d9f75844SAndroid Build Coastguard Worker 159*d9f75844SAndroid Build Coastguard Worker std::string address; 160*d9f75844SAndroid Build Coastguard Worker int port = 0; 161*d9f75844SAndroid Build Coastguard Worker std::string url; 162*d9f75844SAndroid Build Coastguard Worker int error_code = 0; 163*d9f75844SAndroid Build Coastguard Worker std::string error_text; 164*d9f75844SAndroid Build Coastguard Worker }; 165*d9f75844SAndroid Build Coastguard Worker 166*d9f75844SAndroid Build Coastguard Worker struct CandidatePairChangeEvent { 167*d9f75844SAndroid Build Coastguard Worker CandidatePair selected_candidate_pair; 168*d9f75844SAndroid Build Coastguard Worker int64_t last_data_received_ms; 169*d9f75844SAndroid Build Coastguard Worker std::string reason; 170*d9f75844SAndroid Build Coastguard Worker // How long do we estimate that we've been disconnected. 171*d9f75844SAndroid Build Coastguard Worker int64_t estimated_disconnected_time_ms; 172*d9f75844SAndroid Build Coastguard Worker }; 173*d9f75844SAndroid Build Coastguard Worker 174*d9f75844SAndroid Build Coastguard Worker typedef std::set<rtc::SocketAddress> ServerAddresses; 175*d9f75844SAndroid Build Coastguard Worker 176*d9f75844SAndroid Build Coastguard Worker // Represents a local communication mechanism that can be used to create 177*d9f75844SAndroid Build Coastguard Worker // connections to similar mechanisms of the other client. Subclasses of this 178*d9f75844SAndroid Build Coastguard Worker // one add support for specific mechanisms like local UDP ports. 179*d9f75844SAndroid Build Coastguard Worker class Port : public PortInterface, public sigslot::has_slots<> { 180*d9f75844SAndroid Build Coastguard Worker public: 181*d9f75844SAndroid Build Coastguard Worker // INIT: The state when a port is just created. 182*d9f75844SAndroid Build Coastguard Worker // KEEP_ALIVE_UNTIL_PRUNED: A port should not be destroyed even if no 183*d9f75844SAndroid Build Coastguard Worker // connection is using it. 184*d9f75844SAndroid Build Coastguard Worker // PRUNED: It will be destroyed if no connection is using it for a period of 185*d9f75844SAndroid Build Coastguard Worker // 30 seconds. 186*d9f75844SAndroid Build Coastguard Worker enum class State { INIT, KEEP_ALIVE_UNTIL_PRUNED, PRUNED }; 187*d9f75844SAndroid Build Coastguard Worker Port(webrtc::TaskQueueBase* thread, 188*d9f75844SAndroid Build Coastguard Worker absl::string_view type, 189*d9f75844SAndroid Build Coastguard Worker rtc::PacketSocketFactory* factory, 190*d9f75844SAndroid Build Coastguard Worker const rtc::Network* network, 191*d9f75844SAndroid Build Coastguard Worker absl::string_view username_fragment, 192*d9f75844SAndroid Build Coastguard Worker absl::string_view password, 193*d9f75844SAndroid Build Coastguard Worker const webrtc::FieldTrialsView* field_trials = nullptr); 194*d9f75844SAndroid Build Coastguard Worker Port(webrtc::TaskQueueBase* thread, 195*d9f75844SAndroid Build Coastguard Worker absl::string_view type, 196*d9f75844SAndroid Build Coastguard Worker rtc::PacketSocketFactory* factory, 197*d9f75844SAndroid Build Coastguard Worker const rtc::Network* network, 198*d9f75844SAndroid Build Coastguard Worker uint16_t min_port, 199*d9f75844SAndroid Build Coastguard Worker uint16_t max_port, 200*d9f75844SAndroid Build Coastguard Worker absl::string_view username_fragment, 201*d9f75844SAndroid Build Coastguard Worker absl::string_view password, 202*d9f75844SAndroid Build Coastguard Worker const webrtc::FieldTrialsView* field_trials = nullptr); 203*d9f75844SAndroid Build Coastguard Worker ~Port() override; 204*d9f75844SAndroid Build Coastguard Worker 205*d9f75844SAndroid Build Coastguard Worker // Note that the port type does NOT uniquely identify different subclasses of 206*d9f75844SAndroid Build Coastguard Worker // Port. Use the 2-tuple of the port type AND the protocol (GetProtocol()) to 207*d9f75844SAndroid Build Coastguard Worker // uniquely identify subclasses. Whenever a new subclass of Port introduces a 208*d9f75844SAndroid Build Coastguard Worker // conflit in the value of the 2-tuple, make sure that the implementation that 209*d9f75844SAndroid Build Coastguard Worker // relies on this 2-tuple for RTTI is properly changed. 210*d9f75844SAndroid Build Coastguard Worker const std::string& Type() const override; 211*d9f75844SAndroid Build Coastguard Worker const rtc::Network* Network() const override; 212*d9f75844SAndroid Build Coastguard Worker 213*d9f75844SAndroid Build Coastguard Worker // Methods to set/get ICE role and tiebreaker values. 214*d9f75844SAndroid Build Coastguard Worker IceRole GetIceRole() const override; 215*d9f75844SAndroid Build Coastguard Worker void SetIceRole(IceRole role) override; 216*d9f75844SAndroid Build Coastguard Worker 217*d9f75844SAndroid Build Coastguard Worker void SetIceTiebreaker(uint64_t tiebreaker) override; 218*d9f75844SAndroid Build Coastguard Worker uint64_t IceTiebreaker() const override; 219*d9f75844SAndroid Build Coastguard Worker 220*d9f75844SAndroid Build Coastguard Worker bool SharedSocket() const override; ResetSharedSocket()221*d9f75844SAndroid Build Coastguard Worker void ResetSharedSocket() { shared_socket_ = false; } 222*d9f75844SAndroid Build Coastguard Worker 223*d9f75844SAndroid Build Coastguard Worker // Should not destroy the port even if no connection is using it. Called when 224*d9f75844SAndroid Build Coastguard Worker // a port is ready to use. 225*d9f75844SAndroid Build Coastguard Worker void KeepAliveUntilPruned(); 226*d9f75844SAndroid Build Coastguard Worker // Allows a port to be destroyed if no connection is using it. 227*d9f75844SAndroid Build Coastguard Worker void Prune(); 228*d9f75844SAndroid Build Coastguard Worker 229*d9f75844SAndroid Build Coastguard Worker // Call to stop any currently pending operations from running. 230*d9f75844SAndroid Build Coastguard Worker void CancelPendingTasks(); 231*d9f75844SAndroid Build Coastguard Worker 232*d9f75844SAndroid Build Coastguard Worker // The thread on which this port performs its I/O. thread()233*d9f75844SAndroid Build Coastguard Worker webrtc::TaskQueueBase* thread() { return thread_; } 234*d9f75844SAndroid Build Coastguard Worker 235*d9f75844SAndroid Build Coastguard Worker // The factory used to create the sockets of this port. socket_factory()236*d9f75844SAndroid Build Coastguard Worker rtc::PacketSocketFactory* socket_factory() const { return factory_; } 237*d9f75844SAndroid Build Coastguard Worker 238*d9f75844SAndroid Build Coastguard Worker // For debugging purposes. content_name()239*d9f75844SAndroid Build Coastguard Worker const std::string& content_name() const { return content_name_; } set_content_name(absl::string_view content_name)240*d9f75844SAndroid Build Coastguard Worker void set_content_name(absl::string_view content_name) { 241*d9f75844SAndroid Build Coastguard Worker content_name_ = std::string(content_name); 242*d9f75844SAndroid Build Coastguard Worker } 243*d9f75844SAndroid Build Coastguard Worker component()244*d9f75844SAndroid Build Coastguard Worker int component() const { return component_; } set_component(int component)245*d9f75844SAndroid Build Coastguard Worker void set_component(int component) { component_ = component; } 246*d9f75844SAndroid Build Coastguard Worker send_retransmit_count_attribute()247*d9f75844SAndroid Build Coastguard Worker bool send_retransmit_count_attribute() const { 248*d9f75844SAndroid Build Coastguard Worker return send_retransmit_count_attribute_; 249*d9f75844SAndroid Build Coastguard Worker } set_send_retransmit_count_attribute(bool enable)250*d9f75844SAndroid Build Coastguard Worker void set_send_retransmit_count_attribute(bool enable) { 251*d9f75844SAndroid Build Coastguard Worker send_retransmit_count_attribute_ = enable; 252*d9f75844SAndroid Build Coastguard Worker } 253*d9f75844SAndroid Build Coastguard Worker 254*d9f75844SAndroid Build Coastguard Worker // Identifies the generation that this port was created in. generation()255*d9f75844SAndroid Build Coastguard Worker uint32_t generation() const { return generation_; } set_generation(uint32_t generation)256*d9f75844SAndroid Build Coastguard Worker void set_generation(uint32_t generation) { generation_ = generation; } 257*d9f75844SAndroid Build Coastguard Worker 258*d9f75844SAndroid Build Coastguard Worker const std::string username_fragment() const; password()259*d9f75844SAndroid Build Coastguard Worker const std::string& password() const { return password_; } 260*d9f75844SAndroid Build Coastguard Worker 261*d9f75844SAndroid Build Coastguard Worker // May be called when this port was initially created by a pooled 262*d9f75844SAndroid Build Coastguard Worker // PortAllocatorSession, and is now being assigned to an ICE transport. 263*d9f75844SAndroid Build Coastguard Worker // Updates the information for candidates as well. 264*d9f75844SAndroid Build Coastguard Worker void SetIceParameters(int component, 265*d9f75844SAndroid Build Coastguard Worker absl::string_view username_fragment, 266*d9f75844SAndroid Build Coastguard Worker absl::string_view password); 267*d9f75844SAndroid Build Coastguard Worker 268*d9f75844SAndroid Build Coastguard Worker // Fired when candidates are discovered by the port. When all candidates 269*d9f75844SAndroid Build Coastguard Worker // are discovered that belong to port SignalAddressReady is fired. 270*d9f75844SAndroid Build Coastguard Worker sigslot::signal2<Port*, const Candidate&> SignalCandidateReady; 271*d9f75844SAndroid Build Coastguard Worker // Provides all of the above information in one handy object. 272*d9f75844SAndroid Build Coastguard Worker const std::vector<Candidate>& Candidates() const override; 273*d9f75844SAndroid Build Coastguard Worker // Fired when candidate discovery failed using certain server. 274*d9f75844SAndroid Build Coastguard Worker sigslot::signal2<Port*, const IceCandidateErrorEvent&> SignalCandidateError; 275*d9f75844SAndroid Build Coastguard Worker 276*d9f75844SAndroid Build Coastguard Worker // SignalPortComplete is sent when port completes the task of candidates 277*d9f75844SAndroid Build Coastguard Worker // allocation. 278*d9f75844SAndroid Build Coastguard Worker sigslot::signal1<Port*> SignalPortComplete; 279*d9f75844SAndroid Build Coastguard Worker // This signal sent when port fails to allocate candidates and this port 280*d9f75844SAndroid Build Coastguard Worker // can't be used in establishing the connections. When port is in shared mode 281*d9f75844SAndroid Build Coastguard Worker // and port fails to allocate one of the candidates, port shouldn't send 282*d9f75844SAndroid Build Coastguard Worker // this signal as other candidates might be usefull in establishing the 283*d9f75844SAndroid Build Coastguard Worker // connection. 284*d9f75844SAndroid Build Coastguard Worker sigslot::signal1<Port*> SignalPortError; 285*d9f75844SAndroid Build Coastguard Worker 286*d9f75844SAndroid Build Coastguard Worker void SubscribePortDestroyed( 287*d9f75844SAndroid Build Coastguard Worker std::function<void(PortInterface*)> callback) override; 288*d9f75844SAndroid Build Coastguard Worker void SendPortDestroyed(Port* port); 289*d9f75844SAndroid Build Coastguard Worker // Returns a map containing all of the connections of this port, keyed by the 290*d9f75844SAndroid Build Coastguard Worker // remote address. 291*d9f75844SAndroid Build Coastguard Worker typedef std::map<rtc::SocketAddress, Connection*> AddressMap; connections()292*d9f75844SAndroid Build Coastguard Worker const AddressMap& connections() { return connections_; } 293*d9f75844SAndroid Build Coastguard Worker 294*d9f75844SAndroid Build Coastguard Worker // Returns the connection to the given address or NULL if none exists. 295*d9f75844SAndroid Build Coastguard Worker Connection* GetConnection(const rtc::SocketAddress& remote_addr) override; 296*d9f75844SAndroid Build Coastguard Worker 297*d9f75844SAndroid Build Coastguard Worker // Removes and deletes a connection object. `DestroyConnection` will 298*d9f75844SAndroid Build Coastguard Worker // delete the connection object directly whereas `DestroyConnectionAsync` 299*d9f75844SAndroid Build Coastguard Worker // defers the `delete` operation to when the call stack has been unwound. 300*d9f75844SAndroid Build Coastguard Worker // Async may be needed when deleting a connection object from within a 301*d9f75844SAndroid Build Coastguard Worker // callback. DestroyConnection(Connection * conn)302*d9f75844SAndroid Build Coastguard Worker void DestroyConnection(Connection* conn) { 303*d9f75844SAndroid Build Coastguard Worker DestroyConnectionInternal(conn, false); 304*d9f75844SAndroid Build Coastguard Worker } 305*d9f75844SAndroid Build Coastguard Worker DestroyConnectionAsync(Connection * conn)306*d9f75844SAndroid Build Coastguard Worker void DestroyConnectionAsync(Connection* conn) { 307*d9f75844SAndroid Build Coastguard Worker DestroyConnectionInternal(conn, true); 308*d9f75844SAndroid Build Coastguard Worker } 309*d9f75844SAndroid Build Coastguard Worker 310*d9f75844SAndroid Build Coastguard Worker // In a shared socket mode each port which shares the socket will decide 311*d9f75844SAndroid Build Coastguard Worker // to accept the packet based on the `remote_addr`. Currently only UDP 312*d9f75844SAndroid Build Coastguard Worker // port implemented this method. 313*d9f75844SAndroid Build Coastguard Worker // TODO(mallinath) - Make it pure virtual. 314*d9f75844SAndroid Build Coastguard Worker virtual bool HandleIncomingPacket(rtc::AsyncPacketSocket* socket, 315*d9f75844SAndroid Build Coastguard Worker const char* data, 316*d9f75844SAndroid Build Coastguard Worker size_t size, 317*d9f75844SAndroid Build Coastguard Worker const rtc::SocketAddress& remote_addr, 318*d9f75844SAndroid Build Coastguard Worker int64_t packet_time_us); 319*d9f75844SAndroid Build Coastguard Worker 320*d9f75844SAndroid Build Coastguard Worker // Shall the port handle packet from this `remote_addr`. 321*d9f75844SAndroid Build Coastguard Worker // This method is overridden by TurnPort. 322*d9f75844SAndroid Build Coastguard Worker virtual bool CanHandleIncomingPacketsFrom( 323*d9f75844SAndroid Build Coastguard Worker const rtc::SocketAddress& remote_addr) const; 324*d9f75844SAndroid Build Coastguard Worker 325*d9f75844SAndroid Build Coastguard Worker // Sends a response error to the given request. 326*d9f75844SAndroid Build Coastguard Worker void SendBindingErrorResponse(StunMessage* message, 327*d9f75844SAndroid Build Coastguard Worker const rtc::SocketAddress& addr, 328*d9f75844SAndroid Build Coastguard Worker int error_code, 329*d9f75844SAndroid Build Coastguard Worker absl::string_view reason) override; 330*d9f75844SAndroid Build Coastguard Worker void SendUnknownAttributesErrorResponse( 331*d9f75844SAndroid Build Coastguard Worker StunMessage* message, 332*d9f75844SAndroid Build Coastguard Worker const rtc::SocketAddress& addr, 333*d9f75844SAndroid Build Coastguard Worker const std::vector<uint16_t>& unknown_types); 334*d9f75844SAndroid Build Coastguard Worker set_proxy(absl::string_view user_agent,const rtc::ProxyInfo & proxy)335*d9f75844SAndroid Build Coastguard Worker void set_proxy(absl::string_view user_agent, const rtc::ProxyInfo& proxy) { 336*d9f75844SAndroid Build Coastguard Worker user_agent_ = std::string(user_agent); 337*d9f75844SAndroid Build Coastguard Worker proxy_ = proxy; 338*d9f75844SAndroid Build Coastguard Worker } user_agent()339*d9f75844SAndroid Build Coastguard Worker const std::string& user_agent() { return user_agent_; } proxy()340*d9f75844SAndroid Build Coastguard Worker const rtc::ProxyInfo& proxy() { return proxy_; } 341*d9f75844SAndroid Build Coastguard Worker 342*d9f75844SAndroid Build Coastguard Worker void EnablePortPackets() override; 343*d9f75844SAndroid Build Coastguard Worker 344*d9f75844SAndroid Build Coastguard Worker // Called if the port has no connections and is no longer useful. 345*d9f75844SAndroid Build Coastguard Worker void Destroy(); 346*d9f75844SAndroid Build Coastguard Worker 347*d9f75844SAndroid Build Coastguard Worker // Debugging description of this port 348*d9f75844SAndroid Build Coastguard Worker std::string ToString() const override; min_port()349*d9f75844SAndroid Build Coastguard Worker uint16_t min_port() { return min_port_; } max_port()350*d9f75844SAndroid Build Coastguard Worker uint16_t max_port() { return max_port_; } 351*d9f75844SAndroid Build Coastguard Worker 352*d9f75844SAndroid Build Coastguard Worker // Timeout shortening function to speed up unit tests. 353*d9f75844SAndroid Build Coastguard Worker void set_timeout_delay(int delay); 354*d9f75844SAndroid Build Coastguard Worker 355*d9f75844SAndroid Build Coastguard Worker // This method will return local and remote username fragements from the 356*d9f75844SAndroid Build Coastguard Worker // stun username attribute if present. 357*d9f75844SAndroid Build Coastguard Worker bool ParseStunUsername(const StunMessage* stun_msg, 358*d9f75844SAndroid Build Coastguard Worker std::string* local_username, 359*d9f75844SAndroid Build Coastguard Worker std::string* remote_username) const; 360*d9f75844SAndroid Build Coastguard Worker std::string CreateStunUsername(absl::string_view remote_username) const; 361*d9f75844SAndroid Build Coastguard Worker 362*d9f75844SAndroid Build Coastguard Worker bool MaybeIceRoleConflict(const rtc::SocketAddress& addr, 363*d9f75844SAndroid Build Coastguard Worker IceMessage* stun_msg, 364*d9f75844SAndroid Build Coastguard Worker absl::string_view remote_ufrag); 365*d9f75844SAndroid Build Coastguard Worker 366*d9f75844SAndroid Build Coastguard Worker // Called when a packet has been sent to the socket. 367*d9f75844SAndroid Build Coastguard Worker // This is made pure virtual to notify subclasses of Port that they MUST 368*d9f75844SAndroid Build Coastguard Worker // listen to AsyncPacketSocket::SignalSentPacket and then call 369*d9f75844SAndroid Build Coastguard Worker // PortInterface::OnSentPacket. 370*d9f75844SAndroid Build Coastguard Worker virtual void OnSentPacket(rtc::AsyncPacketSocket* socket, 371*d9f75844SAndroid Build Coastguard Worker const rtc::SentPacket& sent_packet) = 0; 372*d9f75844SAndroid Build Coastguard Worker 373*d9f75844SAndroid Build Coastguard Worker // Called when the socket is currently able to send. 374*d9f75844SAndroid Build Coastguard Worker void OnReadyToSend(); 375*d9f75844SAndroid Build Coastguard Worker 376*d9f75844SAndroid Build Coastguard Worker // Called when the Connection discovers a local peer reflexive candidate. 377*d9f75844SAndroid Build Coastguard Worker void AddPrflxCandidate(const Candidate& local); 378*d9f75844SAndroid Build Coastguard Worker network_cost()379*d9f75844SAndroid Build Coastguard Worker int16_t network_cost() const { return network_cost_; } 380*d9f75844SAndroid Build Coastguard Worker GetStunStats(absl::optional<StunStats> * stats)381*d9f75844SAndroid Build Coastguard Worker void GetStunStats(absl::optional<StunStats>* stats) override {} 382*d9f75844SAndroid Build Coastguard Worker 383*d9f75844SAndroid Build Coastguard Worker // Foundation: An arbitrary string that is the same for two candidates 384*d9f75844SAndroid Build Coastguard Worker // that have the same type, base IP address, protocol (UDP, TCP, 385*d9f75844SAndroid Build Coastguard Worker // etc.), and STUN or TURN server. If any of these are different, 386*d9f75844SAndroid Build Coastguard Worker // then the foundation will be different. Two candidate pairs with 387*d9f75844SAndroid Build Coastguard Worker // the same foundation pairs are likely to have similar network 388*d9f75844SAndroid Build Coastguard Worker // characteristics. Foundations are used in the frozen algorithm. 389*d9f75844SAndroid Build Coastguard Worker std::string ComputeFoundation(absl::string_view type, 390*d9f75844SAndroid Build Coastguard Worker absl::string_view protocol, 391*d9f75844SAndroid Build Coastguard Worker absl::string_view relay_protocol, 392*d9f75844SAndroid Build Coastguard Worker const rtc::SocketAddress& base_address); 393*d9f75844SAndroid Build Coastguard Worker 394*d9f75844SAndroid Build Coastguard Worker protected: 395*d9f75844SAndroid Build Coastguard Worker virtual void UpdateNetworkCost(); 396*d9f75844SAndroid Build Coastguard Worker set_type(absl::string_view type)397*d9f75844SAndroid Build Coastguard Worker void set_type(absl::string_view type) { type_ = std::string(type); } 398*d9f75844SAndroid Build Coastguard Worker NewWeakPtr()399*d9f75844SAndroid Build Coastguard Worker rtc::WeakPtr<Port> NewWeakPtr() { return weak_factory_.GetWeakPtr(); } 400*d9f75844SAndroid Build Coastguard Worker 401*d9f75844SAndroid Build Coastguard Worker void AddAddress(const rtc::SocketAddress& address, 402*d9f75844SAndroid Build Coastguard Worker const rtc::SocketAddress& base_address, 403*d9f75844SAndroid Build Coastguard Worker const rtc::SocketAddress& related_address, 404*d9f75844SAndroid Build Coastguard Worker absl::string_view protocol, 405*d9f75844SAndroid Build Coastguard Worker absl::string_view relay_protocol, 406*d9f75844SAndroid Build Coastguard Worker absl::string_view tcptype, 407*d9f75844SAndroid Build Coastguard Worker absl::string_view type, 408*d9f75844SAndroid Build Coastguard Worker uint32_t type_preference, 409*d9f75844SAndroid Build Coastguard Worker uint32_t relay_preference, 410*d9f75844SAndroid Build Coastguard Worker absl::string_view url, 411*d9f75844SAndroid Build Coastguard Worker bool is_final); 412*d9f75844SAndroid Build Coastguard Worker 413*d9f75844SAndroid Build Coastguard Worker void FinishAddingAddress(const Candidate& c, bool is_final) 414*d9f75844SAndroid Build Coastguard Worker RTC_RUN_ON(thread_); 415*d9f75844SAndroid Build Coastguard Worker 416*d9f75844SAndroid Build Coastguard Worker virtual void PostAddAddress(bool is_final); 417*d9f75844SAndroid Build Coastguard Worker 418*d9f75844SAndroid Build Coastguard Worker // Adds the given connection to the map keyed by the remote candidate address. 419*d9f75844SAndroid Build Coastguard Worker // If an existing connection has the same address, the existing one will be 420*d9f75844SAndroid Build Coastguard Worker // replaced and destroyed. 421*d9f75844SAndroid Build Coastguard Worker void AddOrReplaceConnection(Connection* conn); 422*d9f75844SAndroid Build Coastguard Worker 423*d9f75844SAndroid Build Coastguard Worker // Called when a packet is received from an unknown address that is not 424*d9f75844SAndroid Build Coastguard Worker // currently a connection. If this is an authenticated STUN binding request, 425*d9f75844SAndroid Build Coastguard Worker // then we will signal the client. 426*d9f75844SAndroid Build Coastguard Worker void OnReadPacket(const char* data, 427*d9f75844SAndroid Build Coastguard Worker size_t size, 428*d9f75844SAndroid Build Coastguard Worker const rtc::SocketAddress& addr, 429*d9f75844SAndroid Build Coastguard Worker ProtocolType proto); 430*d9f75844SAndroid Build Coastguard Worker 431*d9f75844SAndroid Build Coastguard Worker // If the given data comprises a complete and correct STUN message then the 432*d9f75844SAndroid Build Coastguard Worker // return value is true, otherwise false. If the message username corresponds 433*d9f75844SAndroid Build Coastguard Worker // with this port's username fragment, msg will contain the parsed STUN 434*d9f75844SAndroid Build Coastguard Worker // message. Otherwise, the function may send a STUN response internally. 435*d9f75844SAndroid Build Coastguard Worker // remote_username contains the remote fragment of the STUN username. 436*d9f75844SAndroid Build Coastguard Worker bool GetStunMessage(const char* data, 437*d9f75844SAndroid Build Coastguard Worker size_t size, 438*d9f75844SAndroid Build Coastguard Worker const rtc::SocketAddress& addr, 439*d9f75844SAndroid Build Coastguard Worker std::unique_ptr<IceMessage>* out_msg, 440*d9f75844SAndroid Build Coastguard Worker std::string* out_username); 441*d9f75844SAndroid Build Coastguard Worker 442*d9f75844SAndroid Build Coastguard Worker // Checks if the address in addr is compatible with the port's ip. 443*d9f75844SAndroid Build Coastguard Worker bool IsCompatibleAddress(const rtc::SocketAddress& addr); 444*d9f75844SAndroid Build Coastguard Worker 445*d9f75844SAndroid Build Coastguard Worker // Returns DSCP value packets generated by the port itself should use. 446*d9f75844SAndroid Build Coastguard Worker virtual rtc::DiffServCodePoint StunDscpValue() const; 447*d9f75844SAndroid Build Coastguard Worker 448*d9f75844SAndroid Build Coastguard Worker // Extra work to be done in subclasses when a connection is destroyed. HandleConnectionDestroyed(Connection * conn)449*d9f75844SAndroid Build Coastguard Worker virtual void HandleConnectionDestroyed(Connection* conn) {} 450*d9f75844SAndroid Build Coastguard Worker 451*d9f75844SAndroid Build Coastguard Worker void DestroyAllConnections(); 452*d9f75844SAndroid Build Coastguard Worker 453*d9f75844SAndroid Build Coastguard Worker void CopyPortInformationToPacketInfo(rtc::PacketInfo* info) const; 454*d9f75844SAndroid Build Coastguard Worker mdns_name_registration_status()455*d9f75844SAndroid Build Coastguard Worker MdnsNameRegistrationStatus mdns_name_registration_status() const { 456*d9f75844SAndroid Build Coastguard Worker return mdns_name_registration_status_; 457*d9f75844SAndroid Build Coastguard Worker } set_mdns_name_registration_status(MdnsNameRegistrationStatus status)458*d9f75844SAndroid Build Coastguard Worker void set_mdns_name_registration_status(MdnsNameRegistrationStatus status) { 459*d9f75844SAndroid Build Coastguard Worker mdns_name_registration_status_ = status; 460*d9f75844SAndroid Build Coastguard Worker } 461*d9f75844SAndroid Build Coastguard Worker field_trials()462*d9f75844SAndroid Build Coastguard Worker const webrtc::FieldTrialsView& field_trials() const { return *field_trials_; } 463*d9f75844SAndroid Build Coastguard Worker 464*d9f75844SAndroid Build Coastguard Worker private: 465*d9f75844SAndroid Build Coastguard Worker void Construct(); 466*d9f75844SAndroid Build Coastguard Worker 467*d9f75844SAndroid Build Coastguard Worker void PostDestroyIfDead(bool delayed); 468*d9f75844SAndroid Build Coastguard Worker void DestroyIfDead(); 469*d9f75844SAndroid Build Coastguard Worker 470*d9f75844SAndroid Build Coastguard Worker // Called internally when deleting a connection object. 471*d9f75844SAndroid Build Coastguard Worker // Returns true if the connection object was removed from the `connections_` 472*d9f75844SAndroid Build Coastguard Worker // list and the state updated accordingly. If the connection was not found 473*d9f75844SAndroid Build Coastguard Worker // in the list, the return value is false. Note that this may indicate 474*d9f75844SAndroid Build Coastguard Worker // incorrect behavior of external code that might be attempting to delete 475*d9f75844SAndroid Build Coastguard Worker // connection objects from within a 'on destroyed' callback notification 476*d9f75844SAndroid Build Coastguard Worker // for the connection object itself. 477*d9f75844SAndroid Build Coastguard Worker bool OnConnectionDestroyed(Connection* conn); 478*d9f75844SAndroid Build Coastguard Worker 479*d9f75844SAndroid Build Coastguard Worker // Private implementation of DestroyConnection to keep the async usage 480*d9f75844SAndroid Build Coastguard Worker // distinct. 481*d9f75844SAndroid Build Coastguard Worker void DestroyConnectionInternal(Connection* conn, bool async); 482*d9f75844SAndroid Build Coastguard Worker 483*d9f75844SAndroid Build Coastguard Worker void OnNetworkTypeChanged(const rtc::Network* network); 484*d9f75844SAndroid Build Coastguard Worker 485*d9f75844SAndroid Build Coastguard Worker webrtc::TaskQueueBase* const thread_; 486*d9f75844SAndroid Build Coastguard Worker rtc::PacketSocketFactory* const factory_; 487*d9f75844SAndroid Build Coastguard Worker std::string type_; 488*d9f75844SAndroid Build Coastguard Worker bool send_retransmit_count_attribute_; 489*d9f75844SAndroid Build Coastguard Worker const rtc::Network* network_; 490*d9f75844SAndroid Build Coastguard Worker uint16_t min_port_; 491*d9f75844SAndroid Build Coastguard Worker uint16_t max_port_; 492*d9f75844SAndroid Build Coastguard Worker std::string content_name_; 493*d9f75844SAndroid Build Coastguard Worker int component_; 494*d9f75844SAndroid Build Coastguard Worker uint32_t generation_; 495*d9f75844SAndroid Build Coastguard Worker // In order to establish a connection to this Port (so that real data can be 496*d9f75844SAndroid Build Coastguard Worker // sent through), the other side must send us a STUN binding request that is 497*d9f75844SAndroid Build Coastguard Worker // authenticated with this username_fragment and password. 498*d9f75844SAndroid Build Coastguard Worker // PortAllocatorSession will provide these username_fragment and password. 499*d9f75844SAndroid Build Coastguard Worker // 500*d9f75844SAndroid Build Coastguard Worker // Note: we should always use username_fragment() instead of using 501*d9f75844SAndroid Build Coastguard Worker // `ice_username_fragment_` directly. For the details see the comment on 502*d9f75844SAndroid Build Coastguard Worker // username_fragment(). 503*d9f75844SAndroid Build Coastguard Worker std::string ice_username_fragment_; 504*d9f75844SAndroid Build Coastguard Worker std::string password_; 505*d9f75844SAndroid Build Coastguard Worker std::vector<Candidate> candidates_ RTC_GUARDED_BY(thread_); 506*d9f75844SAndroid Build Coastguard Worker AddressMap connections_; 507*d9f75844SAndroid Build Coastguard Worker int timeout_delay_; 508*d9f75844SAndroid Build Coastguard Worker bool enable_port_packets_; 509*d9f75844SAndroid Build Coastguard Worker IceRole ice_role_; 510*d9f75844SAndroid Build Coastguard Worker uint64_t tiebreaker_; 511*d9f75844SAndroid Build Coastguard Worker bool shared_socket_; 512*d9f75844SAndroid Build Coastguard Worker // Information to use when going through a proxy. 513*d9f75844SAndroid Build Coastguard Worker std::string user_agent_; 514*d9f75844SAndroid Build Coastguard Worker rtc::ProxyInfo proxy_; 515*d9f75844SAndroid Build Coastguard Worker 516*d9f75844SAndroid Build Coastguard Worker // A virtual cost perceived by the user, usually based on the network type 517*d9f75844SAndroid Build Coastguard Worker // (WiFi. vs. Cellular). It takes precedence over the priority when 518*d9f75844SAndroid Build Coastguard Worker // comparing two connections. 519*d9f75844SAndroid Build Coastguard Worker int16_t network_cost_; 520*d9f75844SAndroid Build Coastguard Worker State state_ = State::INIT; 521*d9f75844SAndroid Build Coastguard Worker int64_t last_time_all_connections_removed_ = 0; 522*d9f75844SAndroid Build Coastguard Worker MdnsNameRegistrationStatus mdns_name_registration_status_ = 523*d9f75844SAndroid Build Coastguard Worker MdnsNameRegistrationStatus::kNotStarted; 524*d9f75844SAndroid Build Coastguard Worker 525*d9f75844SAndroid Build Coastguard Worker rtc::WeakPtrFactory<Port> weak_factory_; 526*d9f75844SAndroid Build Coastguard Worker webrtc::AlwaysValidPointer<const webrtc::FieldTrialsView, 527*d9f75844SAndroid Build Coastguard Worker webrtc::FieldTrialBasedConfig> 528*d9f75844SAndroid Build Coastguard Worker field_trials_; 529*d9f75844SAndroid Build Coastguard Worker 530*d9f75844SAndroid Build Coastguard Worker bool MaybeObfuscateAddress(Candidate* c, 531*d9f75844SAndroid Build Coastguard Worker absl::string_view type, 532*d9f75844SAndroid Build Coastguard Worker bool is_final) RTC_RUN_ON(thread_); 533*d9f75844SAndroid Build Coastguard Worker 534*d9f75844SAndroid Build Coastguard Worker friend class Connection; 535*d9f75844SAndroid Build Coastguard Worker webrtc::CallbackList<PortInterface*> port_destroyed_callback_list_; 536*d9f75844SAndroid Build Coastguard Worker }; 537*d9f75844SAndroid Build Coastguard Worker 538*d9f75844SAndroid Build Coastguard Worker } // namespace cricket 539*d9f75844SAndroid Build Coastguard Worker 540*d9f75844SAndroid Build Coastguard Worker #endif // P2P_BASE_PORT_H_ 541