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