blob: 66da9b841c3609c543785d7e4b148f09ff15d33f [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:111/*
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 Bonadei92ea95e2017-09-15 04:47:3111#ifndef P2P_BASE_PORT_H_
12#define P2P_BASE_PORT_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:1113
14#include <map>
kwiberg3ec46792016-04-27 14:22:5315#include <memory>
henrike@webrtc.org269fb4b2014-10-28 22:20:1116#include <set>
17#include <string>
Eldar Relloda13ea22019-06-01 09:23:4318#include <utility>
henrike@webrtc.org269fb4b2014-10-28 22:20:1119#include <vector>
20
Danil Chapovalov00c718362018-06-15 13:58:3821#include "absl/types/optional.h"
Patrik Höglunde2d6a062017-10-05 12:53:3322#include "api/candidate.h"
Patrik Höglund7d003422019-09-17 10:16:3523#include "api/packet_socket_factory.h"
Steve Anton10542f22019-01-11 17:11:0024#include "api/rtc_error.h"
Patrik Höglund56d94522019-11-18 14:53:3225#include "api/transport/stun.h"
Qingsi Wang93a84392018-01-31 01:13:0926#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 Anton10542f22019-01-11 17:11:0028#include "logging/rtc_event_log/ice_logger.h"
29#include "p2p/base/candidate_pair_interface.h"
Jonas Orelande8e7d7b2019-05-29 07:30:5530#include "p2p/base/connection.h"
31#include "p2p/base/connection_info.h"
Steve Anton10542f22019-01-11 17:11:0032#include "p2p/base/p2p_constants.h"
Steve Anton10542f22019-01-11 17:11:0033#include "p2p/base/port_interface.h"
Steve Anton10542f22019-01-11 17:11:0034#include "p2p/base/stun_request.h"
35#include "rtc_base/async_packet_socket.h"
Lahiru Ginnaliya Gamathige3ba7beb2021-02-01 10:06:1136#include "rtc_base/callback_list.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3137#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 17:11:0038#include "rtc_base/net_helper.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3139#include "rtc_base/network.h"
Steve Anton10542f22019-01-11 17:11:0040#include "rtc_base/proxy_info.h"
41#include "rtc_base/rate_tracker.h"
42#include "rtc_base/socket_address.h"
Mirko Bonadeiac194142018-10-22 15:08:3743#include "rtc_base/system/rtc_export.h"
Artem Titove41c4332018-07-25 13:04:2844#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3145#include "rtc_base/thread.h"
Qingsi Wang09619332018-09-13 05:51:5546#include "rtc_base/weak_ptr.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:1147
48namespace cricket {
49
Mirko Bonadei66e76792019-04-02 09:33:5950RTC_EXPORT extern const char LOCAL_PORT_TYPE[];
51RTC_EXPORT extern const char STUN_PORT_TYPE[];
52RTC_EXPORT extern const char PRFLX_PORT_TYPE[];
Mirko Bonadeiac194142018-10-22 15:08:3753RTC_EXPORT extern const char RELAY_PORT_TYPE[];
henrike@webrtc.org269fb4b2014-10-28 22:20:1154
henrike@webrtc.org269fb4b2014-10-28 22:20:1155// RFC 6544, TCP candidate encoding rules.
56extern const int DISCARD_PORT;
57extern const char TCPTYPE_ACTIVE_STR[];
58extern const char TCPTYPE_PASSIVE_STR[];
59extern const char TCPTYPE_SIMOPEN_STR[];
60
henrike@webrtc.org269fb4b2014-10-28 22:20:1161enum IcePriorityValue {
hnsl277b2502016-12-13 13:17:2362 ICE_TYPE_PREFERENCE_RELAY_TLS = 0,
63 ICE_TYPE_PREFERENCE_RELAY_TCP = 1,
64 ICE_TYPE_PREFERENCE_RELAY_UDP = 2,
Taylor Brandstetter62351c92016-08-11 23:05:0765 ICE_TYPE_PREFERENCE_PRFLX_TCP = 80,
henrike@webrtc.org269fb4b2014-10-28 22:20:1166 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 Wang3ea7b832018-11-07 01:51:0272enum 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 Wangd5e0fcd2018-02-27 03:29:0584// Stats that we can return about the port of a STUN candidate.
Qingsi Wang72a43a12018-02-21 00:03:1885class 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.
100class 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 Chapovalov00c718362018-06-15 13:58:38109 absl::optional<StunStats> stun_stats;
Qingsi Wang72a43a12018-02-21 00:03:18110};
111
112typedef std::vector<CandidateStats> CandidateStatsList;
113
henrike@webrtc.org269fb4b2014-10-28 22:20:11114const char* ProtoToString(ProtocolType proto);
115bool StringToProto(const char* value, ProtocolType* proto);
116
117struct ProtocolAddress {
118 rtc::SocketAddress address;
119 ProtocolType proto;
henrike@webrtc.org269fb4b2014-10-28 22:20:11120
121 ProtocolAddress(const rtc::SocketAddress& a, ProtocolType p)
hnsl277b2502016-12-13 13:17:23122 : address(a), proto(p) {}
Taylor Brandstettera1c30352016-05-13 15:15:11123
124 bool operator==(const ProtocolAddress& o) const {
hnsl277b2502016-12-13 13:17:23125 return address == o.address && proto == o.proto;
Taylor Brandstettera1c30352016-05-13 15:15:11126 }
127 bool operator!=(const ProtocolAddress& o) const { return !(*this == o); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11128};
129
Eldar Relloda13ea22019-06-01 09:23:43130struct IceCandidateErrorEvent {
131 IceCandidateErrorEvent() = default;
Eldar Rello0095d372019-12-02 20:22:07132 IceCandidateErrorEvent(std::string address,
133 int port,
Eldar Relloda13ea22019-06-01 09:23:43134 std::string url,
135 int error_code,
136 std::string error_text)
Eldar Rello0095d372019-12-02 20:22:07137 : address(std::move(address)),
138 port(port),
Eldar Relloda13ea22019-06-01 09:23:43139 url(std::move(url)),
140 error_code(error_code),
141 error_text(std::move(error_text)) {}
142
Eldar Rello0095d372019-12-02 20:22:07143 std::string address;
144 int port = 0;
Eldar Relloda13ea22019-06-01 09:23:43145 std::string url;
146 int error_code = 0;
147 std::string error_text;
148};
149
Alex Drake00c7ecf2019-08-06 17:54:47150struct CandidatePairChangeEvent {
Qingsi Wang7cdcda92019-08-28 16:18:37151 CandidatePair selected_candidate_pair;
Alex Drake00c7ecf2019-08-06 17:54:47152 int64_t last_data_received_ms;
153 std::string reason;
Jonas Oreland93a9d192020-08-20 10:25:04154 // How long do we estimate that we've been disconnected.
155 int64_t estimated_disconnected_time_ms;
Alex Drake00c7ecf2019-08-06 17:54:47156};
157
henrike@webrtc.org269fb4b2014-10-28 22:20:11158typedef 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 Gerey665174f2018-06-19 13:03:05163class Port : public PortInterface,
Tomas Gunnarsson91e41562020-11-23 12:03:33164 public rtc::MessageHandler,
henrike@webrtc.org269fb4b2014-10-28 22:20:11165 public sigslot::has_slots<> {
166 public:
Honghai Zhanga74363c2016-07-29 01:06:15167 // 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.org332331f2014-11-06 20:19:22173 Port(rtc::Thread* thread,
Honghai Zhangd00c0572016-06-28 16:44:47174 const std::string& type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11175 rtc::PacketSocketFactory* factory,
pkasting@chromium.org332331f2014-11-06 20:19:22176 rtc::Network* network,
pkasting@chromium.org332331f2014-11-06 20:19:22177 const std::string& username_fragment,
178 const std::string& password);
deadbeef5c3c1042017-08-04 22:01:57179 Port(rtc::Thread* thread,
180 const std::string& type,
181 rtc::PacketSocketFactory* factory,
182 rtc::Network* network,
Peter Boström0c4e06b2015-10-07 10:23:21183 uint16_t min_port,
184 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22185 const std::string& username_fragment,
henrike@webrtc.org269fb4b2014-10-28 22:20:11186 const std::string& password);
Steve Anton1cf1b7d2017-10-30 17:00:15187 ~Port() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11188
Qingsi Wang4ff54432018-03-02 02:25:20189 // 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 Anton1cf1b7d2017-10-30 17:00:15194 const std::string& Type() const override;
195 rtc::Network* Network() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11196
henrike@webrtc.org269fb4b2014-10-28 22:20:11197 // Methods to set/get ICE role and tiebreaker values.
Steve Anton1cf1b7d2017-10-30 17:00:15198 IceRole GetIceRole() const override;
199 void SetIceRole(IceRole role) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11200
Steve Anton1cf1b7d2017-10-30 17:00:15201 void SetIceTiebreaker(uint64_t tiebreaker) override;
202 uint64_t IceTiebreaker() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11203
Steve Anton1cf1b7d2017-10-30 17:00:15204 bool SharedSocket() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11205 void ResetSharedSocket() { shared_socket_ = false; }
206
Honghai Zhanga74363c2016-07-29 01:06:15207 // 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 Gunnarsson91e41562020-11-23 12:03:33213 // Call to stop any currently pending operations from running.
214 void CancelPendingTasks();
215
henrike@webrtc.org269fb4b2014-10-28 22:20:11216 // 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.
deadbeef14f97f52016-06-23 00:14:15242 uint32_t generation() const { return generation_; }
Peter Boström0c4e06b2015-10-07 10:23:21243 void set_generation(uint32_t generation) { generation_ = generation; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11244
henrike@webrtc.org269fb4b2014-10-28 22:20:11245 const std::string username_fragment() const;
246 const std::string& password() const { return password_; }
247
Taylor Brandstettera1c30352016-05-13 15:15:11248 // 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.org269fb4b2014-10-28 22:20:11255 // 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.org269fb4b2014-10-28 22:20:11258 // Provides all of the above information in one handy object.
Steve Anton1cf1b7d2017-10-30 17:00:15259 const std::vector<Candidate>& Candidates() const override;
Eldar Relloda13ea22019-06-01 09:23:43260 // Fired when candidate discovery failed using certain server.
261 sigslot::signal2<Port*, const IceCandidateErrorEvent&> SignalCandidateError;
henrike@webrtc.org269fb4b2014-10-28 22:20:11262
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 Gamathige3ba7beb2021-02-01 10:06:11273 void SubscribePortDestroyed(
274 std::function<void(PortInterface*)> callback) override;
275 void SendPortDestroyed(Port* port);
henrike@webrtc.org269fb4b2014-10-28 22:20:11276 // 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 Anton1cf1b7d2017-10-30 17:00:15282 Connection* GetConnection(const rtc::SocketAddress& remote_addr) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11283
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 Anton1cf1b7d2017-10-30 17:00:15291 virtual bool HandleIncomingPacket(rtc::AsyncPacketSocket* socket,
292 const char* data,
293 size_t size,
294 const rtc::SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 12:01:41295 int64_t packet_time_us);
henrike@webrtc.org269fb4b2014-10-28 22:20:11296
Jonas Oreland202994c2017-12-18 11:10:43297 // 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 Orelandd0036622019-11-29 14:58:13302 // Sends a response error to the given request.
Steve Anton1cf1b7d2017-10-30 17:00:15303 void SendBindingErrorResponse(StunMessage* request,
304 const rtc::SocketAddress& addr,
305 int error_code,
306 const std::string& reason) override;
Taylor Brandstetterfb4351b2020-03-23 23:00:31307 void SendUnknownAttributesErrorResponse(
308 StunMessage* request,
309 const rtc::SocketAddress& addr,
310 const std::vector<uint16_t>& unknown_types);
henrike@webrtc.org269fb4b2014-10-28 22:20:11311
Yves Gerey665174f2018-06-19 13:03:05312 void set_proxy(const std::string& user_agent, const rtc::ProxyInfo& proxy) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11313 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 Anton1cf1b7d2017-10-30 17:00:15319 void EnablePortPackets() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11320
321 // Called if the port has no connections and is no longer useful.
322 void Destroy();
323
Steve Anton1cf1b7d2017-10-30 17:00:15324 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11325
326 // Debugging description of this port
Steve Anton1cf1b7d2017-10-30 17:00:15327 std::string ToString() const override;
Peter Boström0c4e06b2015-10-07 10:23:21328 uint16_t min_port() { return min_port_; }
329 uint16_t max_port() { return max_port_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11330
331 // Timeout shortening function to speed up unit tests.
Tomas Gunnarsson91e41562020-11-23 12:03:33332 void set_timeout_delay(int delay);
henrike@webrtc.org269fb4b2014-10-28 22:20:11333
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 Thatcher7cbd1882015-09-18 01:54:52338 std::string* remote_username) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11339 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
stefanc1aeaf02015-10-15 14:26:07346 // Called when a packet has been sent to the socket.
Stefan Holmer55674ff2016-01-14 14:49:16347 // 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;
stefanc1aeaf02015-10-15 14:26:07352
henrike@webrtc.org269fb4b2014-10-28 22:20:11353 // 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
honghaiza0c44ea2016-03-23 23:07:48360 int16_t network_cost() const { return network_cost_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11361
Nico Weber22f99252019-02-20 15:13:16362 void GetStunStats(absl::optional<StunStats>* stats) override {}
Qingsi Wang72a43a12018-02-21 00:03:18363
Jonas Orelande8e7d7b2019-05-29 07:30:55364 // 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.org269fb4b2014-10-28 22:20:11375 protected:
Honghai Zhanga74363c2016-07-29 01:06:15376 enum { MSG_DESTROY_IF_DEAD = 0, MSG_FIRST_AVAILABLE };
henrike@webrtc.org269fb4b2014-10-28 22:20:11377
Honghai Zhang351d77b2016-05-20 22:08:29378 virtual void UpdateNetworkCost();
379
henrike@webrtc.org269fb4b2014-10-28 22:20:11380 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 Shieh3d564c12015-08-19 23:51:15385 const std::string& protocol,
386 const std::string& relay_protocol,
387 const std::string& tcptype,
388 const std::string& type,
Peter Boström0c4e06b2015-10-07 10:23:21389 uint32_t type_preference,
390 uint32_t relay_preference,
zhihuang26d99c22017-02-13 20:47:27391 const std::string& url,
Qingsi Wang09619332018-09-13 05:51:55392 bool is_final);
393
394 void FinishAddingAddress(const Candidate& c, bool is_final);
henrike@webrtc.org269fb4b2014-10-28 22:20:11395
Qingsi Wang3ea7b832018-11-07 01:51:02396 virtual void PostAddAddress(bool is_final);
397
honghaiz36f50e82016-06-01 22:57:03398 // 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.org269fb4b2014-10-28 22:20:11402
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 Gerey665174f2018-06-19 13:03:05406 void OnReadPacket(const char* data,
407 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11408 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.
kwiberg6baec032016-03-15 18:09:39416 bool GetStunMessage(const char* data,
417 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11418 const rtc::SocketAddress& addr,
kwiberg3ec46792016-04-27 14:22:53419 std::unique_ptr<IceMessage>* out_msg,
kwiberg6baec032016-03-15 18:09:39420 std::string* out_username);
henrike@webrtc.org269fb4b2014-10-28 22:20:11421
422 // Checks if the address in addr is compatible with the port's ip.
423 bool IsCompatibleAddress(const rtc::SocketAddress& addr);
424
Tim Haloun6ca98362018-09-18 00:06:08425 // Returns DSCP value packets generated by the port itself should use.
426 virtual rtc::DiffServCodePoint StunDscpValue() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11427
honghaiz36f50e82016-06-01 22:57:03428 // Extra work to be done in subclasses when a connection is destroyed.
429 virtual void HandleConnectionDestroyed(Connection* conn) {}
430
Qingsi Wang6e641e62018-04-12 03:14:17431 void CopyPortInformationToPacketInfo(rtc::PacketInfo* info) const;
432
Qingsi Wang3ea7b832018-11-07 01:51:02433 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.org269fb4b2014-10-28 22:20:11440 private:
441 void Construct();
442 // Called when one of our connections deletes itself.
443 void OnConnectionDestroyed(Connection* conn);
444
Honghai Zhang351d77b2016-05-20 22:08:29445 void OnNetworkTypeChanged(const rtc::Network* network);
446
Tomas Gunnarsson91e41562020-11-23 12:03:33447 rtc::Thread* const thread_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11448 rtc::PacketSocketFactory* factory_;
449 std::string type_;
450 bool send_retransmit_count_attribute_;
451 rtc::Network* network_;
Peter Boström0c4e06b2015-10-07 10:23:21452 uint16_t min_port_;
453 uint16_t max_port_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11454 std::string content_name_;
455 int component_;
Peter Boström0c4e06b2015-10-07 10:23:21456 uint32_t generation_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11457 // 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.org269fb4b2014-10-28 22:20:11471 IceRole ice_role_;
Peter Boström0c4e06b2015-10-07 10:23:21472 uint64_t tiebreaker_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11473 bool shared_socket_;
474 // Information to use when going through a proxy.
475 std::string user_agent_;
476 rtc::ProxyInfo proxy_;
477
honghaize1a0c942016-02-16 22:54:56478 // 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 Helgason47d3a012017-10-24 13:28:51481 int16_t network_cost_;
Honghai Zhanga74363c2016-07-29 01:06:15482 State state_ = State::INIT;
Honghai Zhangb5db1ec2016-07-28 20:23:05483 int64_t last_time_all_connections_removed_ = 0;
Qingsi Wang3ea7b832018-11-07 01:51:02484 MdnsNameRegistrationStatus mdns_name_registration_status_ =
485 MdnsNameRegistrationStatus::kNotStarted;
honghaize1a0c942016-02-16 22:54:56486
Qingsi Wang09619332018-09-13 05:51:55487 rtc::WeakPtrFactory<Port> weak_factory_;
488
Jeroen de Borst72d2ddd2018-11-27 21:20:39489 bool MaybeObfuscateAddress(Candidate* c,
490 const std::string& type,
491 bool is_final);
492
henrike@webrtc.org269fb4b2014-10-28 22:20:11493 friend class Connection;
Lahiru Ginnaliya Gamathige3ba7beb2021-02-01 10:06:11494 webrtc::CallbackList<PortInterface*> port_destroyed_callback_list_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11495};
496
henrike@webrtc.org269fb4b2014-10-28 22:20:11497} // namespace cricket
498
Mirko Bonadei92ea95e2017-09-15 04:47:31499#endif // P2P_BASE_PORT_H_