xref: /aosp_15_r20/external/webrtc/p2p/base/ice_transport_internal.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2017 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 #include "p2p/base/ice_transport_internal.h"
12 
13 #include "absl/strings/string_view.h"
14 #include "p2p/base/p2p_constants.h"
15 
16 namespace cricket {
17 
18 using webrtc::RTCError;
19 using webrtc::RTCErrorType;
20 
VerifyCandidate(const Candidate & cand)21 RTCError VerifyCandidate(const Candidate& cand) {
22   // No address zero.
23   if (cand.address().IsNil() || cand.address().IsAnyIP()) {
24     return RTCError(RTCErrorType::INVALID_PARAMETER,
25                     "candidate has address of zero");
26   }
27 
28   // Disallow all ports below 1024, except for 80 and 443 on public addresses.
29   int port = cand.address().port();
30   if (cand.protocol() == cricket::TCP_PROTOCOL_NAME &&
31       (cand.tcptype() == cricket::TCPTYPE_ACTIVE_STR || port == 0)) {
32     // Expected for active-only candidates per
33     // http://tools.ietf.org/html/rfc6544#section-4.5 so no error.
34     // Libjingle clients emit port 0, in "active" mode.
35     return RTCError::OK();
36   }
37   if (port < 1024) {
38     if ((port != 80) && (port != 443)) {
39       return RTCError(RTCErrorType::INVALID_PARAMETER,
40                       "candidate has port below 1024, but not 80 or 443");
41     }
42 
43     if (cand.address().IsPrivateIP()) {
44       return RTCError(
45           RTCErrorType::INVALID_PARAMETER,
46           "candidate has port of 80 or 443 with private IP address");
47     }
48   }
49 
50   return RTCError::OK();
51 }
52 
VerifyCandidates(const Candidates & candidates)53 RTCError VerifyCandidates(const Candidates& candidates) {
54   for (const Candidate& candidate : candidates) {
55     RTCError error = VerifyCandidate(candidate);
56     if (!error.ok())
57       return error;
58   }
59   return RTCError::OK();
60 }
61 
62 IceConfig::IceConfig() = default;
63 
IceConfig(int receiving_timeout_ms,int backup_connection_ping_interval,ContinualGatheringPolicy gathering_policy,bool prioritize_most_likely_candidate_pairs,int stable_writable_connection_ping_interval_ms,bool presume_writable_when_fully_relayed,int regather_on_failed_networks_interval_ms,int receiving_switching_delay_ms)64 IceConfig::IceConfig(int receiving_timeout_ms,
65                      int backup_connection_ping_interval,
66                      ContinualGatheringPolicy gathering_policy,
67                      bool prioritize_most_likely_candidate_pairs,
68                      int stable_writable_connection_ping_interval_ms,
69                      bool presume_writable_when_fully_relayed,
70                      int regather_on_failed_networks_interval_ms,
71                      int receiving_switching_delay_ms)
72     : receiving_timeout(receiving_timeout_ms),
73       backup_connection_ping_interval(backup_connection_ping_interval),
74       continual_gathering_policy(gathering_policy),
75       prioritize_most_likely_candidate_pairs(
76           prioritize_most_likely_candidate_pairs),
77       stable_writable_connection_ping_interval(
78           stable_writable_connection_ping_interval_ms),
79       presume_writable_when_fully_relayed(presume_writable_when_fully_relayed),
80       regather_on_failed_networks_interval(
81           regather_on_failed_networks_interval_ms),
82       receiving_switching_delay(receiving_switching_delay_ms) {}
83 
84 IceConfig::~IceConfig() = default;
85 
receiving_timeout_or_default() const86 int IceConfig::receiving_timeout_or_default() const {
87   return receiving_timeout.value_or(RECEIVING_TIMEOUT);
88 }
backup_connection_ping_interval_or_default() const89 int IceConfig::backup_connection_ping_interval_or_default() const {
90   return backup_connection_ping_interval.value_or(
91       BACKUP_CONNECTION_PING_INTERVAL);
92 }
stable_writable_connection_ping_interval_or_default() const93 int IceConfig::stable_writable_connection_ping_interval_or_default() const {
94   return stable_writable_connection_ping_interval.value_or(
95       STRONG_AND_STABLE_WRITABLE_CONNECTION_PING_INTERVAL);
96 }
regather_on_failed_networks_interval_or_default() const97 int IceConfig::regather_on_failed_networks_interval_or_default() const {
98   return regather_on_failed_networks_interval.value_or(
99       REGATHER_ON_FAILED_NETWORKS_INTERVAL);
100 }
receiving_switching_delay_or_default() const101 int IceConfig::receiving_switching_delay_or_default() const {
102   return receiving_switching_delay.value_or(RECEIVING_SWITCHING_DELAY);
103 }
ice_check_interval_strong_connectivity_or_default() const104 int IceConfig::ice_check_interval_strong_connectivity_or_default() const {
105   return ice_check_interval_strong_connectivity.value_or(STRONG_PING_INTERVAL);
106 }
ice_check_interval_weak_connectivity_or_default() const107 int IceConfig::ice_check_interval_weak_connectivity_or_default() const {
108   return ice_check_interval_weak_connectivity.value_or(WEAK_PING_INTERVAL);
109 }
ice_check_min_interval_or_default() const110 int IceConfig::ice_check_min_interval_or_default() const {
111   return ice_check_min_interval.value_or(-1);
112 }
ice_unwritable_timeout_or_default() const113 int IceConfig::ice_unwritable_timeout_or_default() const {
114   return ice_unwritable_timeout.value_or(CONNECTION_WRITE_CONNECT_TIMEOUT);
115 }
ice_unwritable_min_checks_or_default() const116 int IceConfig::ice_unwritable_min_checks_or_default() const {
117   return ice_unwritable_min_checks.value_or(CONNECTION_WRITE_CONNECT_FAILURES);
118 }
ice_inactive_timeout_or_default() const119 int IceConfig::ice_inactive_timeout_or_default() const {
120   return ice_inactive_timeout.value_or(CONNECTION_WRITE_TIMEOUT);
121 }
stun_keepalive_interval_or_default() const122 int IceConfig::stun_keepalive_interval_or_default() const {
123   return stun_keepalive_interval.value_or(STUN_KEEPALIVE_INTERVAL);
124 }
125 
126 IceTransportInternal::IceTransportInternal() = default;
127 
128 IceTransportInternal::~IceTransportInternal() = default;
129 
SetIceCredentials(absl::string_view ice_ufrag,absl::string_view ice_pwd)130 void IceTransportInternal::SetIceCredentials(absl::string_view ice_ufrag,
131                                              absl::string_view ice_pwd) {
132   SetIceParameters(IceParameters(ice_ufrag, ice_pwd, false));
133 }
134 
SetRemoteIceCredentials(absl::string_view ice_ufrag,absl::string_view ice_pwd)135 void IceTransportInternal::SetRemoteIceCredentials(absl::string_view ice_ufrag,
136                                                    absl::string_view ice_pwd) {
137   SetRemoteIceParameters(IceParameters(ice_ufrag, ice_pwd, false));
138 }
139 
140 }  // namespace cricket
141