henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef P2P_BASE_PORT_H_ |
| 12 | #define P2P_BASE_PORT_H_ |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 13 | |
| 14 | #include <map> |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 15 | #include <memory> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 16 | #include <set> |
| 17 | #include <string> |
Eldar Rello | da13ea2 | 2019-06-01 12:23:43 +0300 | [diff] [blame] | 18 | #include <utility> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 19 | #include <vector> |
| 20 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 21 | #include "absl/types/optional.h" |
Patrik Höglund | e2d6a06 | 2017-10-05 14:53:33 +0200 | [diff] [blame] | 22 | #include "api/candidate.h" |
Patrik Höglund | 7d00342 | 2019-09-17 12:16:35 +0200 | [diff] [blame] | 23 | #include "api/packet_socket_factory.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 24 | #include "api/rtc_error.h" |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 25 | #include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair.h" |
| 26 | #include "logging/rtc_event_log/events/rtc_event_ice_candidate_pair_config.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 27 | #include "logging/rtc_event_log/ice_logger.h" |
| 28 | #include "p2p/base/candidate_pair_interface.h" |
Jonas Oreland | e8e7d7b | 2019-05-29 09:30:55 +0200 | [diff] [blame] | 29 | #include "p2p/base/connection.h" |
| 30 | #include "p2p/base/connection_info.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 31 | #include "p2p/base/p2p_constants.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 32 | #include "p2p/base/port_interface.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 33 | #include "p2p/base/stun.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 34 | #include "p2p/base/stun_request.h" |
| 35 | #include "rtc_base/async_packet_socket.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 36 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 37 | #include "rtc_base/net_helper.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 38 | #include "rtc_base/network.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 39 | #include "rtc_base/proxy_info.h" |
| 40 | #include "rtc_base/rate_tracker.h" |
| 41 | #include "rtc_base/socket_address.h" |
Mirko Bonadei | ac19414 | 2018-10-22 17:08:37 +0200 | [diff] [blame] | 42 | #include "rtc_base/system/rtc_export.h" |
Artem Titov | e41c433 | 2018-07-25 15:04:28 +0200 | [diff] [blame] | 43 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 44 | #include "rtc_base/thread.h" |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 45 | #include "rtc_base/weak_ptr.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 46 | |
| 47 | namespace cricket { |
| 48 | |
Mirko Bonadei | 66e7679 | 2019-04-02 11:33:59 +0200 | [diff] [blame] | 49 | RTC_EXPORT extern const char LOCAL_PORT_TYPE[]; |
| 50 | RTC_EXPORT extern const char STUN_PORT_TYPE[]; |
| 51 | RTC_EXPORT extern const char PRFLX_PORT_TYPE[]; |
Mirko Bonadei | ac19414 | 2018-10-22 17:08:37 +0200 | [diff] [blame] | 52 | RTC_EXPORT extern const char RELAY_PORT_TYPE[]; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 53 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 54 | // RFC 6544, TCP candidate encoding rules. |
| 55 | extern const int DISCARD_PORT; |
| 56 | extern const char TCPTYPE_ACTIVE_STR[]; |
| 57 | extern const char TCPTYPE_PASSIVE_STR[]; |
| 58 | extern const char TCPTYPE_SIMOPEN_STR[]; |
| 59 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 60 | enum IcePriorityValue { |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 61 | ICE_TYPE_PREFERENCE_RELAY_TLS = 0, |
| 62 | ICE_TYPE_PREFERENCE_RELAY_TCP = 1, |
| 63 | ICE_TYPE_PREFERENCE_RELAY_UDP = 2, |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 64 | ICE_TYPE_PREFERENCE_PRFLX_TCP = 80, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 65 | ICE_TYPE_PREFERENCE_HOST_TCP = 90, |
| 66 | ICE_TYPE_PREFERENCE_SRFLX = 100, |
| 67 | ICE_TYPE_PREFERENCE_PRFLX = 110, |
| 68 | ICE_TYPE_PREFERENCE_HOST = 126 |
| 69 | }; |
| 70 | |
Qingsi Wang | 3ea7b83 | 2018-11-06 17:51:02 -0800 | [diff] [blame] | 71 | enum class MdnsNameRegistrationStatus { |
| 72 | // IP concealment with mDNS is not enabled or the name registration process is |
| 73 | // not started yet. |
| 74 | kNotStarted, |
| 75 | // A request to create and register an mDNS name for a local IP address of a |
| 76 | // host candidate is sent to the mDNS responder. |
| 77 | kInProgress, |
| 78 | // The name registration is complete and the created name is returned by the |
| 79 | // mDNS responder. |
| 80 | kCompleted, |
| 81 | }; |
| 82 | |
Qingsi Wang | d5e0fcd | 2018-02-26 19:29:05 -0800 | [diff] [blame] | 83 | // Stats that we can return about the port of a STUN candidate. |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 84 | class StunStats { |
| 85 | public: |
| 86 | StunStats() = default; |
| 87 | StunStats(const StunStats&) = default; |
| 88 | ~StunStats() = default; |
| 89 | |
| 90 | StunStats& operator=(const StunStats& other) = default; |
| 91 | |
| 92 | int stun_binding_requests_sent = 0; |
| 93 | int stun_binding_responses_received = 0; |
| 94 | double stun_binding_rtt_ms_total = 0; |
| 95 | double stun_binding_rtt_ms_squared_total = 0; |
| 96 | }; |
| 97 | |
| 98 | // Stats that we can return about a candidate. |
| 99 | class CandidateStats { |
| 100 | public: |
| 101 | CandidateStats(); |
| 102 | explicit CandidateStats(Candidate candidate); |
| 103 | CandidateStats(const CandidateStats&); |
| 104 | ~CandidateStats(); |
| 105 | |
| 106 | Candidate candidate; |
| 107 | // STUN port stats if this candidate is a STUN candidate. |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 108 | absl::optional<StunStats> stun_stats; |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | typedef std::vector<CandidateStats> CandidateStatsList; |
| 112 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 113 | const char* ProtoToString(ProtocolType proto); |
| 114 | bool StringToProto(const char* value, ProtocolType* proto); |
| 115 | |
| 116 | struct ProtocolAddress { |
| 117 | rtc::SocketAddress address; |
| 118 | ProtocolType proto; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 119 | |
| 120 | ProtocolAddress(const rtc::SocketAddress& a, ProtocolType p) |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 121 | : address(a), proto(p) {} |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 122 | |
| 123 | bool operator==(const ProtocolAddress& o) const { |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 124 | return address == o.address && proto == o.proto; |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 125 | } |
| 126 | bool operator!=(const ProtocolAddress& o) const { return !(*this == o); } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
Eldar Rello | da13ea2 | 2019-06-01 12:23:43 +0300 | [diff] [blame] | 129 | struct IceCandidateErrorEvent { |
| 130 | IceCandidateErrorEvent() = default; |
| 131 | IceCandidateErrorEvent(std::string host_candidate, |
| 132 | std::string url, |
| 133 | int error_code, |
| 134 | std::string error_text) |
| 135 | : host_candidate(std::move(host_candidate)), |
| 136 | url(std::move(url)), |
| 137 | error_code(error_code), |
| 138 | error_text(std::move(error_text)) {} |
| 139 | |
| 140 | std::string host_candidate; |
| 141 | std::string url; |
| 142 | int error_code = 0; |
| 143 | std::string error_text; |
| 144 | }; |
| 145 | |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 146 | struct CandidatePairChangeEvent { |
Qingsi Wang | 7cdcda9 | 2019-08-28 09:18:37 -0700 | [diff] [blame] | 147 | CandidatePair selected_candidate_pair; |
Alex Drake | 00c7ecf | 2019-08-06 10:54:47 -0700 | [diff] [blame] | 148 | int64_t last_data_received_ms; |
| 149 | std::string reason; |
| 150 | }; |
| 151 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 152 | typedef std::set<rtc::SocketAddress> ServerAddresses; |
| 153 | |
| 154 | // Represents a local communication mechanism that can be used to create |
| 155 | // connections to similar mechanisms of the other client. Subclasses of this |
| 156 | // one add support for specific mechanisms like local UDP ports. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 157 | class Port : public PortInterface, |
| 158 | public rtc::MessageHandler, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 159 | public sigslot::has_slots<> { |
| 160 | public: |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 161 | // INIT: The state when a port is just created. |
| 162 | // KEEP_ALIVE_UNTIL_PRUNED: A port should not be destroyed even if no |
| 163 | // connection is using it. |
| 164 | // PRUNED: It will be destroyed if no connection is using it for a period of |
| 165 | // 30 seconds. |
| 166 | enum class State { INIT, KEEP_ALIVE_UNTIL_PRUNED, PRUNED }; |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 167 | Port(rtc::Thread* thread, |
Honghai Zhang | d00c057 | 2016-06-28 09:44:47 -0700 | [diff] [blame] | 168 | const std::string& type, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 169 | rtc::PacketSocketFactory* factory, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 170 | rtc::Network* network, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 171 | const std::string& username_fragment, |
| 172 | const std::string& password); |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 173 | Port(rtc::Thread* thread, |
| 174 | const std::string& type, |
| 175 | rtc::PacketSocketFactory* factory, |
| 176 | rtc::Network* network, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 177 | uint16_t min_port, |
| 178 | uint16_t max_port, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 179 | const std::string& username_fragment, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 180 | const std::string& password); |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 181 | ~Port() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 182 | |
Qingsi Wang | 4ff5443 | 2018-03-01 18:25:20 -0800 | [diff] [blame] | 183 | // Note that the port type does NOT uniquely identify different subclasses of |
| 184 | // Port. Use the 2-tuple of the port type AND the protocol (GetProtocol()) to |
| 185 | // uniquely identify subclasses. Whenever a new subclass of Port introduces a |
| 186 | // conflit in the value of the 2-tuple, make sure that the implementation that |
| 187 | // relies on this 2-tuple for RTTI is properly changed. |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 188 | const std::string& Type() const override; |
| 189 | rtc::Network* Network() const override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 190 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 191 | // Methods to set/get ICE role and tiebreaker values. |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 192 | IceRole GetIceRole() const override; |
| 193 | void SetIceRole(IceRole role) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 194 | |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 195 | void SetIceTiebreaker(uint64_t tiebreaker) override; |
| 196 | uint64_t IceTiebreaker() const override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 197 | |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 198 | bool SharedSocket() const override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 199 | void ResetSharedSocket() { shared_socket_ = false; } |
| 200 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 201 | // Should not destroy the port even if no connection is using it. Called when |
| 202 | // a port is ready to use. |
| 203 | void KeepAliveUntilPruned(); |
| 204 | // Allows a port to be destroyed if no connection is using it. |
| 205 | void Prune(); |
| 206 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 207 | // The thread on which this port performs its I/O. |
| 208 | rtc::Thread* thread() { return thread_; } |
| 209 | |
| 210 | // The factory used to create the sockets of this port. |
| 211 | rtc::PacketSocketFactory* socket_factory() const { return factory_; } |
| 212 | void set_socket_factory(rtc::PacketSocketFactory* factory) { |
| 213 | factory_ = factory; |
| 214 | } |
| 215 | |
| 216 | // For debugging purposes. |
| 217 | const std::string& content_name() const { return content_name_; } |
| 218 | void set_content_name(const std::string& content_name) { |
| 219 | content_name_ = content_name; |
| 220 | } |
| 221 | |
| 222 | int component() const { return component_; } |
| 223 | void set_component(int component) { component_ = component; } |
| 224 | |
| 225 | bool send_retransmit_count_attribute() const { |
| 226 | return send_retransmit_count_attribute_; |
| 227 | } |
| 228 | void set_send_retransmit_count_attribute(bool enable) { |
| 229 | send_retransmit_count_attribute_ = enable; |
| 230 | } |
| 231 | |
| 232 | // Identifies the generation that this port was created in. |
deadbeef | 14f97f5 | 2016-06-22 17:14:15 -0700 | [diff] [blame] | 233 | uint32_t generation() const { return generation_; } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 234 | void set_generation(uint32_t generation) { generation_ = generation; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 235 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 236 | const std::string username_fragment() const; |
| 237 | const std::string& password() const { return password_; } |
| 238 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 239 | // May be called when this port was initially created by a pooled |
| 240 | // PortAllocatorSession, and is now being assigned to an ICE transport. |
| 241 | // Updates the information for candidates as well. |
| 242 | void SetIceParameters(int component, |
| 243 | const std::string& username_fragment, |
| 244 | const std::string& password); |
| 245 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 246 | // Fired when candidates are discovered by the port. When all candidates |
| 247 | // are discovered that belong to port SignalAddressReady is fired. |
| 248 | sigslot::signal2<Port*, const Candidate&> SignalCandidateReady; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 249 | // Provides all of the above information in one handy object. |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 250 | const std::vector<Candidate>& Candidates() const override; |
Eldar Rello | da13ea2 | 2019-06-01 12:23:43 +0300 | [diff] [blame] | 251 | // Fired when candidate discovery failed using certain server. |
| 252 | sigslot::signal2<Port*, const IceCandidateErrorEvent&> SignalCandidateError; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 253 | |
| 254 | // SignalPortComplete is sent when port completes the task of candidates |
| 255 | // allocation. |
| 256 | sigslot::signal1<Port*> SignalPortComplete; |
| 257 | // This signal sent when port fails to allocate candidates and this port |
| 258 | // can't be used in establishing the connections. When port is in shared mode |
| 259 | // and port fails to allocate one of the candidates, port shouldn't send |
| 260 | // this signal as other candidates might be usefull in establishing the |
| 261 | // connection. |
| 262 | sigslot::signal1<Port*> SignalPortError; |
| 263 | |
| 264 | // Returns a map containing all of the connections of this port, keyed by the |
| 265 | // remote address. |
| 266 | typedef std::map<rtc::SocketAddress, Connection*> AddressMap; |
| 267 | const AddressMap& connections() { return connections_; } |
| 268 | |
| 269 | // Returns the connection to the given address or NULL if none exists. |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 270 | Connection* GetConnection(const rtc::SocketAddress& remote_addr) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 271 | |
| 272 | // Called each time a connection is created. |
| 273 | sigslot::signal2<Port*, Connection*> SignalConnectionCreated; |
| 274 | |
| 275 | // In a shared socket mode each port which shares the socket will decide |
| 276 | // to accept the packet based on the |remote_addr|. Currently only UDP |
| 277 | // port implemented this method. |
| 278 | // TODO(mallinath) - Make it pure virtual. |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 279 | virtual bool HandleIncomingPacket(rtc::AsyncPacketSocket* socket, |
| 280 | const char* data, |
| 281 | size_t size, |
| 282 | const rtc::SocketAddress& remote_addr, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 283 | int64_t packet_time_us); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 284 | |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 285 | // Shall the port handle packet from this |remote_addr|. |
| 286 | // This method is overridden by TurnPort. |
| 287 | virtual bool CanHandleIncomingPacketsFrom( |
| 288 | const rtc::SocketAddress& remote_addr) const; |
| 289 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 290 | // Sends a response message (normal or error) to the given request. One of |
| 291 | // these methods should be called as a response to SignalUnknownAddress. |
| 292 | // NOTE: You MUST call CreateConnection BEFORE SendBindingResponse. |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 293 | void SendBindingResponse(StunMessage* request, |
| 294 | const rtc::SocketAddress& addr) override; |
| 295 | void SendBindingErrorResponse(StunMessage* request, |
| 296 | const rtc::SocketAddress& addr, |
| 297 | int error_code, |
| 298 | const std::string& reason) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 299 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 300 | void set_proxy(const std::string& user_agent, const rtc::ProxyInfo& proxy) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 301 | user_agent_ = user_agent; |
| 302 | proxy_ = proxy; |
| 303 | } |
| 304 | const std::string& user_agent() { return user_agent_; } |
| 305 | const rtc::ProxyInfo& proxy() { return proxy_; } |
| 306 | |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 307 | void EnablePortPackets() override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 308 | |
| 309 | // Called if the port has no connections and is no longer useful. |
| 310 | void Destroy(); |
| 311 | |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 312 | void OnMessage(rtc::Message* pmsg) override; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 313 | |
| 314 | // Debugging description of this port |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 315 | std::string ToString() const override; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 316 | uint16_t min_port() { return min_port_; } |
| 317 | uint16_t max_port() { return max_port_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 318 | |
| 319 | // Timeout shortening function to speed up unit tests. |
| 320 | void set_timeout_delay(int delay) { timeout_delay_ = delay; } |
| 321 | |
| 322 | // This method will return local and remote username fragements from the |
| 323 | // stun username attribute if present. |
| 324 | bool ParseStunUsername(const StunMessage* stun_msg, |
| 325 | std::string* local_username, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 326 | std::string* remote_username) const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 327 | void CreateStunUsername(const std::string& remote_username, |
| 328 | std::string* stun_username_attr_str) const; |
| 329 | |
| 330 | bool MaybeIceRoleConflict(const rtc::SocketAddress& addr, |
| 331 | IceMessage* stun_msg, |
| 332 | const std::string& remote_ufrag); |
| 333 | |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 334 | // Called when a packet has been sent to the socket. |
Stefan Holmer | 55674ff | 2016-01-14 15:49:16 +0100 | [diff] [blame] | 335 | // This is made pure virtual to notify subclasses of Port that they MUST |
| 336 | // listen to AsyncPacketSocket::SignalSentPacket and then call |
| 337 | // PortInterface::OnSentPacket. |
| 338 | virtual void OnSentPacket(rtc::AsyncPacketSocket* socket, |
| 339 | const rtc::SentPacket& sent_packet) = 0; |
stefan | c1aeaf0 | 2015-10-15 07:26:07 -0700 | [diff] [blame] | 340 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 341 | // Called when the socket is currently able to send. |
| 342 | void OnReadyToSend(); |
| 343 | |
| 344 | // Called when the Connection discovers a local peer reflexive candidate. |
| 345 | // Returns the index of the new local candidate. |
| 346 | size_t AddPrflxCandidate(const Candidate& local); |
| 347 | |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 348 | int16_t network_cost() const { return network_cost_; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 349 | |
Nico Weber | 22f9925 | 2019-02-20 10:13:16 -0500 | [diff] [blame] | 350 | void GetStunStats(absl::optional<StunStats>* stats) override {} |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 351 | |
Jonas Oreland | e8e7d7b | 2019-05-29 09:30:55 +0200 | [diff] [blame] | 352 | // Foundation: An arbitrary string that is the same for two candidates |
| 353 | // that have the same type, base IP address, protocol (UDP, TCP, |
| 354 | // etc.), and STUN or TURN server. If any of these are different, |
| 355 | // then the foundation will be different. Two candidate pairs with |
| 356 | // the same foundation pairs are likely to have similar network |
| 357 | // characteristics. Foundations are used in the frozen algorithm. |
| 358 | static std::string ComputeFoundation(const std::string& type, |
| 359 | const std::string& protocol, |
| 360 | const std::string& relay_protocol, |
| 361 | const rtc::SocketAddress& base_address); |
| 362 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 363 | protected: |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 364 | enum { MSG_DESTROY_IF_DEAD = 0, MSG_FIRST_AVAILABLE }; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 365 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 366 | virtual void UpdateNetworkCost(); |
| 367 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 368 | void set_type(const std::string& type) { type_ = type; } |
| 369 | |
Peter Boström | 2758c66 | 2017-02-13 20:33:27 -0500 | [diff] [blame] | 370 | // Deprecated. Use the AddAddress() method below with "url" instead. |
| 371 | // TODO(zhihuang): Remove this after downstream applications stop using it. |
| 372 | void AddAddress(const rtc::SocketAddress& address, |
| 373 | const rtc::SocketAddress& base_address, |
| 374 | const rtc::SocketAddress& related_address, |
| 375 | const std::string& protocol, |
| 376 | const std::string& relay_protocol, |
| 377 | const std::string& tcptype, |
| 378 | const std::string& type, |
| 379 | uint32_t type_preference, |
| 380 | uint32_t relay_preference, |
Qingsi Wang | 3ea7b83 | 2018-11-06 17:51:02 -0800 | [diff] [blame] | 381 | bool is_final); |
Peter Boström | 2758c66 | 2017-02-13 20:33:27 -0500 | [diff] [blame] | 382 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 383 | void AddAddress(const rtc::SocketAddress& address, |
| 384 | const rtc::SocketAddress& base_address, |
| 385 | const rtc::SocketAddress& related_address, |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 386 | const std::string& protocol, |
| 387 | const std::string& relay_protocol, |
| 388 | const std::string& tcptype, |
| 389 | const std::string& type, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 390 | uint32_t type_preference, |
| 391 | uint32_t relay_preference, |
zhihuang | 26d99c2 | 2017-02-13 12:47:27 -0800 | [diff] [blame] | 392 | const std::string& url, |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 393 | bool is_final); |
| 394 | |
| 395 | void FinishAddingAddress(const Candidate& c, bool is_final); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 396 | |
Qingsi Wang | 3ea7b83 | 2018-11-06 17:51:02 -0800 | [diff] [blame] | 397 | virtual void PostAddAddress(bool is_final); |
| 398 | |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 399 | // Adds the given connection to the map keyed by the remote candidate address. |
| 400 | // If an existing connection has the same address, the existing one will be |
| 401 | // replaced and destroyed. |
| 402 | void AddOrReplaceConnection(Connection* conn); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 403 | |
| 404 | // Called when a packet is received from an unknown address that is not |
| 405 | // currently a connection. If this is an authenticated STUN binding request, |
| 406 | // then we will signal the client. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 407 | void OnReadPacket(const char* data, |
| 408 | size_t size, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 409 | const rtc::SocketAddress& addr, |
| 410 | ProtocolType proto); |
| 411 | |
| 412 | // If the given data comprises a complete and correct STUN message then the |
| 413 | // return value is true, otherwise false. If the message username corresponds |
| 414 | // with this port's username fragment, msg will contain the parsed STUN |
| 415 | // message. Otherwise, the function may send a STUN response internally. |
| 416 | // remote_username contains the remote fragment of the STUN username. |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 417 | bool GetStunMessage(const char* data, |
| 418 | size_t size, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 419 | const rtc::SocketAddress& addr, |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 420 | std::unique_ptr<IceMessage>* out_msg, |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 421 | std::string* out_username); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 422 | |
| 423 | // Checks if the address in addr is compatible with the port's ip. |
| 424 | bool IsCompatibleAddress(const rtc::SocketAddress& addr); |
| 425 | |
Tim Haloun | 6ca9836 | 2018-09-17 17:06:08 -0700 | [diff] [blame] | 426 | // Returns DSCP value packets generated by the port itself should use. |
| 427 | virtual rtc::DiffServCodePoint StunDscpValue() const; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 428 | |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 429 | // Extra work to be done in subclasses when a connection is destroyed. |
| 430 | virtual void HandleConnectionDestroyed(Connection* conn) {} |
| 431 | |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 432 | void CopyPortInformationToPacketInfo(rtc::PacketInfo* info) const; |
| 433 | |
Qingsi Wang | 3ea7b83 | 2018-11-06 17:51:02 -0800 | [diff] [blame] | 434 | MdnsNameRegistrationStatus mdns_name_registration_status() const { |
| 435 | return mdns_name_registration_status_; |
| 436 | } |
| 437 | void set_mdns_name_registration_status(MdnsNameRegistrationStatus status) { |
| 438 | mdns_name_registration_status_ = status; |
| 439 | } |
| 440 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 441 | private: |
| 442 | void Construct(); |
| 443 | // Called when one of our connections deletes itself. |
| 444 | void OnConnectionDestroyed(Connection* conn); |
| 445 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 446 | void OnNetworkTypeChanged(const rtc::Network* network); |
| 447 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 448 | rtc::Thread* thread_; |
| 449 | rtc::PacketSocketFactory* factory_; |
| 450 | std::string type_; |
| 451 | bool send_retransmit_count_attribute_; |
| 452 | rtc::Network* network_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 453 | uint16_t min_port_; |
| 454 | uint16_t max_port_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 455 | std::string content_name_; |
| 456 | int component_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 457 | uint32_t generation_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 458 | // In order to establish a connection to this Port (so that real data can be |
| 459 | // sent through), the other side must send us a STUN binding request that is |
| 460 | // authenticated with this username_fragment and password. |
| 461 | // PortAllocatorSession will provide these username_fragment and password. |
| 462 | // |
| 463 | // Note: we should always use username_fragment() instead of using |
| 464 | // |ice_username_fragment_| directly. For the details see the comment on |
| 465 | // username_fragment(). |
| 466 | std::string ice_username_fragment_; |
| 467 | std::string password_; |
| 468 | std::vector<Candidate> candidates_; |
| 469 | AddressMap connections_; |
| 470 | int timeout_delay_; |
| 471 | bool enable_port_packets_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 472 | IceRole ice_role_; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 473 | uint64_t tiebreaker_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 474 | bool shared_socket_; |
| 475 | // Information to use when going through a proxy. |
| 476 | std::string user_agent_; |
| 477 | rtc::ProxyInfo proxy_; |
| 478 | |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 479 | // A virtual cost perceived by the user, usually based on the network type |
| 480 | // (WiFi. vs. Cellular). It takes precedence over the priority when |
| 481 | // comparing two connections. |
Kári Tristan Helgason | 47d3a01 | 2017-10-24 15:28:51 +0200 | [diff] [blame] | 482 | int16_t network_cost_; |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 483 | State state_ = State::INIT; |
Honghai Zhang | b5db1ec | 2016-07-28 13:23:05 -0700 | [diff] [blame] | 484 | int64_t last_time_all_connections_removed_ = 0; |
Qingsi Wang | 3ea7b83 | 2018-11-06 17:51:02 -0800 | [diff] [blame] | 485 | MdnsNameRegistrationStatus mdns_name_registration_status_ = |
| 486 | MdnsNameRegistrationStatus::kNotStarted; |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 487 | |
Qingsi Wang | 0961933 | 2018-09-12 22:51:55 -0700 | [diff] [blame] | 488 | rtc::WeakPtrFactory<Port> weak_factory_; |
| 489 | |
Jeroen de Borst | 72d2ddd | 2018-11-27 13:20:39 -0800 | [diff] [blame] | 490 | bool MaybeObfuscateAddress(Candidate* c, |
| 491 | const std::string& type, |
| 492 | bool is_final); |
| 493 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 494 | friend class Connection; |
| 495 | }; |
| 496 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 497 | } // namespace cricket |
| 498 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 499 | #endif // P2P_BASE_PORT_H_ |