blob: 5bd59d0e4dc790e6207d0982548b16f6365ad7ad [file] [log] [blame]
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +00001/*
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 06:47:31 +020011#ifndef P2P_BASE_PORT_H_
12#define P2P_BASE_PORT_H_
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000013
14#include <map>
kwiberg3ec46792016-04-27 07:22:53 -070015#include <memory>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000016#include <set>
17#include <string>
Eldar Relloda13ea22019-06-01 12:23:43 +030018#include <utility>
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000019#include <vector>
20
Danil Chapovalov00c71832018-06-15 15:58:38 +020021#include "absl/types/optional.h"
Patrik Höglunde2d6a062017-10-05 14:53:33 +020022#include "api/candidate.h"
Patrik Höglund7d003422019-09-17 12:16:35 +020023#include "api/packet_socket_factory.h"
Steve Anton10542f22019-01-11 09:11:00 -080024#include "api/rtc_error.h"
Qingsi Wang93a84392018-01-30 17:13:09 -080025#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 Anton10542f22019-01-11 09:11:00 -080027#include "logging/rtc_event_log/ice_logger.h"
28#include "p2p/base/candidate_pair_interface.h"
Jonas Orelande8e7d7b2019-05-29 09:30:55 +020029#include "p2p/base/connection.h"
30#include "p2p/base/connection_info.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "p2p/base/p2p_constants.h"
Steve Anton10542f22019-01-11 09:11:00 -080032#include "p2p/base/port_interface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "p2p/base/stun.h"
Steve Anton10542f22019-01-11 09:11:00 -080034#include "p2p/base/stun_request.h"
35#include "rtc_base/async_packet_socket.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020036#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080037#include "rtc_base/net_helper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "rtc_base/network.h"
Steve Anton10542f22019-01-11 09:11:00 -080039#include "rtc_base/proxy_info.h"
40#include "rtc_base/rate_tracker.h"
41#include "rtc_base/socket_address.h"
Mirko Bonadeiac194142018-10-22 17:08:37 +020042#include "rtc_base/system/rtc_export.h"
Artem Titove41c4332018-07-25 15:04:28 +020043#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "rtc_base/thread.h"
Qingsi Wang09619332018-09-12 22:51:55 -070045#include "rtc_base/weak_ptr.h"
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000046
47namespace cricket {
48
Mirko Bonadei66e76792019-04-02 11:33:59 +020049RTC_EXPORT extern const char LOCAL_PORT_TYPE[];
50RTC_EXPORT extern const char STUN_PORT_TYPE[];
51RTC_EXPORT extern const char PRFLX_PORT_TYPE[];
Mirko Bonadeiac194142018-10-22 17:08:37 +020052RTC_EXPORT extern const char RELAY_PORT_TYPE[];
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000053
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000054// RFC 6544, TCP candidate encoding rules.
55extern const int DISCARD_PORT;
56extern const char TCPTYPE_ACTIVE_STR[];
57extern const char TCPTYPE_PASSIVE_STR[];
58extern const char TCPTYPE_SIMOPEN_STR[];
59
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000060enum IcePriorityValue {
hnsl277b2502016-12-13 05:17:23 -080061 ICE_TYPE_PREFERENCE_RELAY_TLS = 0,
62 ICE_TYPE_PREFERENCE_RELAY_TCP = 1,
63 ICE_TYPE_PREFERENCE_RELAY_UDP = 2,
Taylor Brandstetter62351c92016-08-11 16:05:07 -070064 ICE_TYPE_PREFERENCE_PRFLX_TCP = 80,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +000065 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 Wang3ea7b832018-11-06 17:51:02 -080071enum 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 Wangd5e0fcd2018-02-26 19:29:05 -080083// Stats that we can return about the port of a STUN candidate.
Qingsi Wang72a43a12018-02-20 16:03:18 -080084class 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.
99class 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 Chapovalov00c71832018-06-15 15:58:38 +0200108 absl::optional<StunStats> stun_stats;
Qingsi Wang72a43a12018-02-20 16:03:18 -0800109};
110
111typedef std::vector<CandidateStats> CandidateStatsList;
112
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000113const char* ProtoToString(ProtocolType proto);
114bool StringToProto(const char* value, ProtocolType* proto);
115
116struct ProtocolAddress {
117 rtc::SocketAddress address;
118 ProtocolType proto;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000119
120 ProtocolAddress(const rtc::SocketAddress& a, ProtocolType p)
hnsl277b2502016-12-13 05:17:23 -0800121 : address(a), proto(p) {}
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700122
123 bool operator==(const ProtocolAddress& o) const {
hnsl277b2502016-12-13 05:17:23 -0800124 return address == o.address && proto == o.proto;
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700125 }
126 bool operator!=(const ProtocolAddress& o) const { return !(*this == o); }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000127};
128
Eldar Relloda13ea22019-06-01 12:23:43 +0300129struct 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 Drake00c7ecf2019-08-06 10:54:47 -0700146struct CandidatePairChangeEvent {
Qingsi Wang7cdcda92019-08-28 09:18:37 -0700147 CandidatePair selected_candidate_pair;
Alex Drake00c7ecf2019-08-06 10:54:47 -0700148 int64_t last_data_received_ms;
149 std::string reason;
150};
151
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000152typedef 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 Gerey665174f2018-06-19 15:03:05 +0200157class Port : public PortInterface,
158 public rtc::MessageHandler,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000159 public sigslot::has_slots<> {
160 public:
Honghai Zhanga74363c2016-07-28 18:06:15 -0700161 // 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.org332331f2014-11-06 20:19:22 +0000167 Port(rtc::Thread* thread,
Honghai Zhangd00c0572016-06-28 09:44:47 -0700168 const std::string& type,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000169 rtc::PacketSocketFactory* factory,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000170 rtc::Network* network,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000171 const std::string& username_fragment,
172 const std::string& password);
deadbeef5c3c1042017-08-04 15:01:57 -0700173 Port(rtc::Thread* thread,
174 const std::string& type,
175 rtc::PacketSocketFactory* factory,
176 rtc::Network* network,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200177 uint16_t min_port,
178 uint16_t max_port,
pkasting@chromium.org332331f2014-11-06 20:19:22 +0000179 const std::string& username_fragment,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000180 const std::string& password);
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700181 ~Port() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000182
Qingsi Wang4ff54432018-03-01 18:25:20 -0800183 // 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 Anton1cf1b7d2017-10-30 10:00:15 -0700188 const std::string& Type() const override;
189 rtc::Network* Network() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000190
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000191 // Methods to set/get ICE role and tiebreaker values.
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700192 IceRole GetIceRole() const override;
193 void SetIceRole(IceRole role) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000194
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700195 void SetIceTiebreaker(uint64_t tiebreaker) override;
196 uint64_t IceTiebreaker() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000197
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700198 bool SharedSocket() const override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000199 void ResetSharedSocket() { shared_socket_ = false; }
200
Honghai Zhanga74363c2016-07-28 18:06:15 -0700201 // 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.org269fb4b2014-10-28 22:20:11 +0000207 // 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.
deadbeef14f97f52016-06-22 17:14:15 -0700233 uint32_t generation() const { return generation_; }
Peter Boström0c4e06b2015-10-07 12:23:21 +0200234 void set_generation(uint32_t generation) { generation_ = generation; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000235
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000236 const std::string username_fragment() const;
237 const std::string& password() const { return password_; }
238
Taylor Brandstettera1c30352016-05-13 08:15:11 -0700239 // 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.org269fb4b2014-10-28 22:20:11 +0000246 // 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.org269fb4b2014-10-28 22:20:11 +0000249 // Provides all of the above information in one handy object.
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700250 const std::vector<Candidate>& Candidates() const override;
Eldar Relloda13ea22019-06-01 12:23:43 +0300251 // Fired when candidate discovery failed using certain server.
252 sigslot::signal2<Port*, const IceCandidateErrorEvent&> SignalCandidateError;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000253
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 Anton1cf1b7d2017-10-30 10:00:15 -0700270 Connection* GetConnection(const rtc::SocketAddress& remote_addr) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000271
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 Anton1cf1b7d2017-10-30 10:00:15 -0700279 virtual bool HandleIncomingPacket(rtc::AsyncPacketSocket* socket,
280 const char* data,
281 size_t size,
282 const rtc::SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +0100283 int64_t packet_time_us);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000284
Jonas Oreland202994c2017-12-18 12:10:43 +0100285 // 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.org269fb4b2014-10-28 22:20:11 +0000290 // 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 Anton1cf1b7d2017-10-30 10:00:15 -0700293 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.org269fb4b2014-10-28 22:20:11 +0000299
Yves Gerey665174f2018-06-19 15:03:05 +0200300 void set_proxy(const std::string& user_agent, const rtc::ProxyInfo& proxy) {
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000301 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 Anton1cf1b7d2017-10-30 10:00:15 -0700307 void EnablePortPackets() override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000308
309 // Called if the port has no connections and is no longer useful.
310 void Destroy();
311
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700312 void OnMessage(rtc::Message* pmsg) override;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000313
314 // Debugging description of this port
Steve Anton1cf1b7d2017-10-30 10:00:15 -0700315 std::string ToString() const override;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200316 uint16_t min_port() { return min_port_; }
317 uint16_t max_port() { return max_port_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000318
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 Thatcher7cbd1882015-09-17 18:54:52 -0700326 std::string* remote_username) const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000327 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
stefanc1aeaf02015-10-15 07:26:07 -0700334 // Called when a packet has been sent to the socket.
Stefan Holmer55674ff2016-01-14 15:49:16 +0100335 // 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;
stefanc1aeaf02015-10-15 07:26:07 -0700340
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000341 // 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
honghaiza0c44ea2016-03-23 16:07:48 -0700348 int16_t network_cost() const { return network_cost_; }
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000349
Nico Weber22f99252019-02-20 10:13:16 -0500350 void GetStunStats(absl::optional<StunStats>* stats) override {}
Qingsi Wang72a43a12018-02-20 16:03:18 -0800351
Jonas Orelande8e7d7b2019-05-29 09:30:55 +0200352 // 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.org269fb4b2014-10-28 22:20:11 +0000363 protected:
Honghai Zhanga74363c2016-07-28 18:06:15 -0700364 enum { MSG_DESTROY_IF_DEAD = 0, MSG_FIRST_AVAILABLE };
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000365
Honghai Zhang351d77b2016-05-20 15:08:29 -0700366 virtual void UpdateNetworkCost();
367
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000368 void set_type(const std::string& type) { type_ = type; }
369
Peter Boström2758c662017-02-13 20:33:27 -0500370 // 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 Wang3ea7b832018-11-06 17:51:02 -0800381 bool is_final);
Peter Boström2758c662017-02-13 20:33:27 -0500382
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000383 void AddAddress(const rtc::SocketAddress& address,
384 const rtc::SocketAddress& base_address,
385 const rtc::SocketAddress& related_address,
Guo-wei Shieh3d564c12015-08-19 16:51:15 -0700386 const std::string& protocol,
387 const std::string& relay_protocol,
388 const std::string& tcptype,
389 const std::string& type,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200390 uint32_t type_preference,
391 uint32_t relay_preference,
zhihuang26d99c22017-02-13 12:47:27 -0800392 const std::string& url,
Qingsi Wang09619332018-09-12 22:51:55 -0700393 bool is_final);
394
395 void FinishAddingAddress(const Candidate& c, bool is_final);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000396
Qingsi Wang3ea7b832018-11-06 17:51:02 -0800397 virtual void PostAddAddress(bool is_final);
398
honghaiz36f50e82016-06-01 15:57:03 -0700399 // 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.org269fb4b2014-10-28 22:20:11 +0000403
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 Gerey665174f2018-06-19 15:03:05 +0200407 void OnReadPacket(const char* data,
408 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000409 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.
kwiberg6baec032016-03-15 11:09:39 -0700417 bool GetStunMessage(const char* data,
418 size_t size,
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000419 const rtc::SocketAddress& addr,
kwiberg3ec46792016-04-27 07:22:53 -0700420 std::unique_ptr<IceMessage>* out_msg,
kwiberg6baec032016-03-15 11:09:39 -0700421 std::string* out_username);
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000422
423 // Checks if the address in addr is compatible with the port's ip.
424 bool IsCompatibleAddress(const rtc::SocketAddress& addr);
425
Tim Haloun6ca98362018-09-17 17:06:08 -0700426 // Returns DSCP value packets generated by the port itself should use.
427 virtual rtc::DiffServCodePoint StunDscpValue() const;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000428
honghaiz36f50e82016-06-01 15:57:03 -0700429 // Extra work to be done in subclasses when a connection is destroyed.
430 virtual void HandleConnectionDestroyed(Connection* conn) {}
431
Qingsi Wang6e641e62018-04-11 20:14:17 -0700432 void CopyPortInformationToPacketInfo(rtc::PacketInfo* info) const;
433
Qingsi Wang3ea7b832018-11-06 17:51:02 -0800434 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.org269fb4b2014-10-28 22:20:11 +0000441 private:
442 void Construct();
443 // Called when one of our connections deletes itself.
444 void OnConnectionDestroyed(Connection* conn);
445
Honghai Zhang351d77b2016-05-20 15:08:29 -0700446 void OnNetworkTypeChanged(const rtc::Network* network);
447
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000448 rtc::Thread* thread_;
449 rtc::PacketSocketFactory* factory_;
450 std::string type_;
451 bool send_retransmit_count_attribute_;
452 rtc::Network* network_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200453 uint16_t min_port_;
454 uint16_t max_port_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000455 std::string content_name_;
456 int component_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200457 uint32_t generation_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000458 // 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.org269fb4b2014-10-28 22:20:11 +0000472 IceRole ice_role_;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200473 uint64_t tiebreaker_;
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000474 bool shared_socket_;
475 // Information to use when going through a proxy.
476 std::string user_agent_;
477 rtc::ProxyInfo proxy_;
478
honghaize1a0c942016-02-16 14:54:56 -0800479 // 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 Helgason47d3a012017-10-24 15:28:51 +0200482 int16_t network_cost_;
Honghai Zhanga74363c2016-07-28 18:06:15 -0700483 State state_ = State::INIT;
Honghai Zhangb5db1ec2016-07-28 13:23:05 -0700484 int64_t last_time_all_connections_removed_ = 0;
Qingsi Wang3ea7b832018-11-06 17:51:02 -0800485 MdnsNameRegistrationStatus mdns_name_registration_status_ =
486 MdnsNameRegistrationStatus::kNotStarted;
honghaize1a0c942016-02-16 14:54:56 -0800487
Qingsi Wang09619332018-09-12 22:51:55 -0700488 rtc::WeakPtrFactory<Port> weak_factory_;
489
Jeroen de Borst72d2ddd2018-11-27 13:20:39 -0800490 bool MaybeObfuscateAddress(Candidate* c,
491 const std::string& type,
492 bool is_final);
493
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000494 friend class Connection;
495};
496
henrike@webrtc.org269fb4b2014-10-28 22:20:11 +0000497} // namespace cricket
498
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200499#endif // P2P_BASE_PORT_H_