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