henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [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 06:47:31 +0200 | [diff] [blame] | 11 | #include "p2p/base/port.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 12 | |
Raphael Kubo da Costa | 7f90e2c | 2017-10-13 15:49:32 +0200 | [diff] [blame] | 13 | #include <math.h> |
| 14 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 15 | #include <algorithm> |
Steve Anton | babf917 | 2017-11-29 10:19:02 -0800 | [diff] [blame] | 16 | #include <utility> |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "p2p/base/portallocator.h" |
| 20 | #include "rtc_base/base64.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/crc32.h" |
| 23 | #include "rtc_base/helpers.h" |
| 24 | #include "rtc_base/logging.h" |
| 25 | #include "rtc_base/messagedigest.h" |
| 26 | #include "rtc_base/network.h" |
Karl Wiberg | e40468b | 2017-11-22 10:42:26 +0100 | [diff] [blame] | 27 | #include "rtc_base/numerics/safe_minmax.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "rtc_base/ptr_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "rtc_base/stringencode.h" |
| 30 | #include "rtc_base/stringutils.h" |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 31 | |
| 32 | namespace { |
| 33 | |
| 34 | // Determines whether we have seen at least the given maximum number of |
| 35 | // pings fail to have a response. |
| 36 | inline bool TooManyFailures( |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 37 | const std::vector<cricket::Connection::SentPing>& pings_since_last_response, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 38 | uint32_t maximum_failures, |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 39 | int rtt_estimate, |
| 40 | int64_t now) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 41 | // If we haven't sent that many pings, then we can't have failed that many. |
| 42 | if (pings_since_last_response.size() < maximum_failures) |
| 43 | return false; |
| 44 | |
| 45 | // Check if the window in which we would expect a response to the ping has |
| 46 | // already elapsed. |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 47 | int64_t expected_response_time = |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 48 | pings_since_last_response[maximum_failures - 1].sent_time + rtt_estimate; |
| 49 | return now > expected_response_time; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // Determines whether we have gone too long without seeing any response. |
| 53 | inline bool TooLongWithoutResponse( |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 54 | const std::vector<cricket::Connection::SentPing>& pings_since_last_response, |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 55 | int64_t maximum_time, |
| 56 | int64_t now) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 57 | if (pings_since_last_response.size() == 0) |
| 58 | return false; |
| 59 | |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 60 | auto first = pings_since_last_response[0]; |
| 61 | return now > (first.sent_time + maximum_time); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 64 | // Helper methods for converting string values of log description fields to |
| 65 | // enum. |
| 66 | webrtc::IceCandidateType GetCandidateTypeByString(const std::string& type) { |
| 67 | if (type == cricket::LOCAL_PORT_TYPE) { |
| 68 | return webrtc::IceCandidateType::kLocal; |
| 69 | } else if (type == cricket::STUN_PORT_TYPE) { |
| 70 | return webrtc::IceCandidateType::kStun; |
| 71 | } else if (type == cricket::PRFLX_PORT_TYPE) { |
| 72 | return webrtc::IceCandidateType::kPrflx; |
| 73 | } else if (type == cricket::RELAY_PORT_TYPE) { |
| 74 | return webrtc::IceCandidateType::kRelay; |
| 75 | } |
| 76 | return webrtc::IceCandidateType::kUnknown; |
| 77 | } |
| 78 | |
| 79 | webrtc::IceCandidatePairProtocol GetProtocolByString( |
| 80 | const std::string& protocol) { |
| 81 | if (protocol == cricket::UDP_PROTOCOL_NAME) { |
| 82 | return webrtc::IceCandidatePairProtocol::kUdp; |
| 83 | } else if (protocol == cricket::TCP_PROTOCOL_NAME) { |
| 84 | return webrtc::IceCandidatePairProtocol::kTcp; |
| 85 | } else if (protocol == cricket::SSLTCP_PROTOCOL_NAME) { |
| 86 | return webrtc::IceCandidatePairProtocol::kSsltcp; |
| 87 | } else if (protocol == cricket::TLS_PROTOCOL_NAME) { |
| 88 | return webrtc::IceCandidatePairProtocol::kTls; |
| 89 | } |
| 90 | return webrtc::IceCandidatePairProtocol::kUnknown; |
| 91 | } |
| 92 | |
| 93 | webrtc::IceCandidatePairAddressFamily GetAddressFamilyByInt( |
| 94 | int address_family) { |
| 95 | if (address_family == AF_INET) { |
| 96 | return webrtc::IceCandidatePairAddressFamily::kIpv4; |
| 97 | } else if (address_family == AF_INET6) { |
| 98 | return webrtc::IceCandidatePairAddressFamily::kIpv6; |
| 99 | } |
| 100 | return webrtc::IceCandidatePairAddressFamily::kUnknown; |
| 101 | } |
| 102 | |
| 103 | webrtc::IceCandidateNetworkType ConvertNetworkType(rtc::AdapterType type) { |
| 104 | if (type == rtc::ADAPTER_TYPE_ETHERNET) { |
| 105 | return webrtc::IceCandidateNetworkType::kEthernet; |
| 106 | } else if (type == rtc::ADAPTER_TYPE_LOOPBACK) { |
| 107 | return webrtc::IceCandidateNetworkType::kLoopback; |
| 108 | } else if (type == rtc::ADAPTER_TYPE_WIFI) { |
| 109 | return webrtc::IceCandidateNetworkType::kWifi; |
| 110 | } else if (type == rtc::ADAPTER_TYPE_VPN) { |
| 111 | return webrtc::IceCandidateNetworkType::kVpn; |
| 112 | } else if (type == rtc::ADAPTER_TYPE_CELLULAR) { |
| 113 | return webrtc::IceCandidateNetworkType::kCellular; |
| 114 | } |
| 115 | return webrtc::IceCandidateNetworkType::kUnknown; |
| 116 | } |
| 117 | |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 118 | rtc::PacketInfoProtocolType ConvertProtocolTypeToPacketInfoProtocolType( |
| 119 | cricket::ProtocolType type) { |
| 120 | switch (type) { |
| 121 | case cricket::ProtocolType::PROTO_UDP: |
| 122 | return rtc::PacketInfoProtocolType::kUdp; |
| 123 | case cricket::ProtocolType::PROTO_TCP: |
| 124 | return rtc::PacketInfoProtocolType::kTcp; |
| 125 | case cricket::ProtocolType::PROTO_SSLTCP: |
| 126 | return rtc::PacketInfoProtocolType::kSsltcp; |
| 127 | case cricket::ProtocolType::PROTO_TLS: |
| 128 | return rtc::PacketInfoProtocolType::kTls; |
| 129 | default: |
| 130 | return rtc::PacketInfoProtocolType::kUnknown; |
| 131 | } |
| 132 | } |
| 133 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 134 | // We will restrict RTT estimates (when used for determining state) to be |
| 135 | // within a reasonable range. |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 136 | const int MINIMUM_RTT = 100; // 0.1 seconds |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 137 | const int MAXIMUM_RTT = 60000; // 60 seconds |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 138 | |
| 139 | // When we don't have any RTT data, we have to pick something reasonable. We |
| 140 | // use a large value just in case the connection is really slow. |
skvlad | 5107246 | 2017-02-02 11:50:14 -0800 | [diff] [blame] | 141 | const int DEFAULT_RTT = 3000; // 3 seconds |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 142 | |
| 143 | // Computes our estimate of the RTT given the current estimate. |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 144 | inline int ConservativeRTTEstimate(int rtt) { |
kwiberg | 0703856 | 2017-06-12 11:40:47 -0700 | [diff] [blame] | 145 | return rtc::SafeClamp(2 * rtt, MINIMUM_RTT, MAXIMUM_RTT); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // Weighting of the old rtt value to new data. |
| 149 | const int RTT_RATIO = 3; // 3 : 1 |
| 150 | |
pthatcher | 94a2f21 | 2017-02-08 14:42:22 -0800 | [diff] [blame] | 151 | // The delay before we begin checking if this port is useless. We set |
| 152 | // it to a little higher than a total STUN timeout. |
| 153 | const int kPortTimeoutDelay = cricket::STUN_TOTAL_TIMEOUT + 5000; |
zstein | abbacbf | 2017-03-20 10:53:12 -0700 | [diff] [blame] | 154 | |
| 155 | // For packet loss estimation. |
| 156 | const int64_t kConsiderPacketLostAfter = 3000; // 3 seconds |
| 157 | |
| 158 | // For packet loss estimation. |
| 159 | const int64_t kForgetPacketAfter = 30000; // 30 seconds |
| 160 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 161 | } // namespace |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 162 | |
| 163 | namespace cricket { |
| 164 | |
Qingsi Wang | dea6889 | 2018-03-27 10:55:21 -0700 | [diff] [blame] | 165 | using webrtc::RTCErrorType; |
| 166 | using webrtc::RTCError; |
| 167 | |
zhihuang | 38989e5 | 2017-03-21 11:04:53 -0700 | [diff] [blame] | 168 | // TODO(ronghuawu): Use "local", "srflx", "prflx" and "relay". But this requires |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 169 | // the signaling part be updated correspondingly as well. |
| 170 | const char LOCAL_PORT_TYPE[] = "local"; |
| 171 | const char STUN_PORT_TYPE[] = "stun"; |
| 172 | const char PRFLX_PORT_TYPE[] = "prflx"; |
| 173 | const char RELAY_PORT_TYPE[] = "relay"; |
| 174 | |
hnsl | 277b250 | 2016-12-13 05:17:23 -0800 | [diff] [blame] | 175 | static const char* const PROTO_NAMES[] = {UDP_PROTOCOL_NAME, TCP_PROTOCOL_NAME, |
| 176 | SSLTCP_PROTOCOL_NAME, |
| 177 | TLS_PROTOCOL_NAME}; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 178 | |
| 179 | const char* ProtoToString(ProtocolType proto) { |
| 180 | return PROTO_NAMES[proto]; |
| 181 | } |
| 182 | |
| 183 | bool StringToProto(const char* value, ProtocolType* proto) { |
| 184 | for (size_t i = 0; i <= PROTO_LAST; ++i) { |
| 185 | if (_stricmp(PROTO_NAMES[i], value) == 0) { |
| 186 | *proto = static_cast<ProtocolType>(i); |
| 187 | return true; |
| 188 | } |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | // RFC 6544, TCP candidate encoding rules. |
| 194 | const int DISCARD_PORT = 9; |
| 195 | const char TCPTYPE_ACTIVE_STR[] = "active"; |
| 196 | const char TCPTYPE_PASSIVE_STR[] = "passive"; |
| 197 | const char TCPTYPE_SIMOPEN_STR[] = "so"; |
| 198 | |
| 199 | // Foundation: An arbitrary string that is the same for two candidates |
| 200 | // that have the same type, base IP address, protocol (UDP, TCP, |
| 201 | // etc.), and STUN or TURN server. If any of these are different, |
| 202 | // then the foundation will be different. Two candidate pairs with |
| 203 | // the same foundation pairs are likely to have similar network |
| 204 | // characteristics. Foundations are used in the frozen algorithm. |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 205 | static std::string ComputeFoundation(const std::string& type, |
| 206 | const std::string& protocol, |
| 207 | const std::string& relay_protocol, |
| 208 | const rtc::SocketAddress& base_address) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 209 | std::ostringstream ost; |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 210 | ost << type << base_address.ipaddr().ToString() << protocol << relay_protocol; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 211 | return rtc::ToString<uint32_t>(rtc::ComputeCrc32(ost.str())); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 214 | CandidateStats::CandidateStats() = default; |
| 215 | |
| 216 | CandidateStats::CandidateStats(const CandidateStats&) = default; |
| 217 | |
| 218 | CandidateStats::CandidateStats(Candidate candidate) { |
| 219 | this->candidate = candidate; |
| 220 | } |
| 221 | |
| 222 | CandidateStats::~CandidateStats() = default; |
| 223 | |
Taylor Brandstetter | 6e2e7ce | 2017-12-19 10:26:23 -0800 | [diff] [blame] | 224 | ConnectionInfo::ConnectionInfo() |
| 225 | : best_connection(false), |
| 226 | writable(false), |
| 227 | receiving(false), |
| 228 | timeout(false), |
| 229 | new_connection(false), |
| 230 | rtt(0), |
| 231 | sent_total_bytes(0), |
| 232 | sent_bytes_second(0), |
| 233 | sent_discarded_packets(0), |
| 234 | sent_total_packets(0), |
| 235 | sent_ping_requests_total(0), |
| 236 | sent_ping_requests_before_first_response(0), |
| 237 | sent_ping_responses(0), |
| 238 | recv_total_bytes(0), |
| 239 | recv_bytes_second(0), |
| 240 | recv_ping_requests(0), |
| 241 | recv_ping_responses(0), |
| 242 | key(nullptr), |
| 243 | state(IceCandidatePairState::WAITING), |
| 244 | priority(0), |
| 245 | nominated(false), |
| 246 | total_round_trip_time_ms(0) {} |
| 247 | |
| 248 | ConnectionInfo::ConnectionInfo(const ConnectionInfo&) = default; |
| 249 | |
| 250 | ConnectionInfo::~ConnectionInfo() = default; |
| 251 | |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 252 | Port::Port(rtc::Thread* thread, |
Honghai Zhang | d00c057 | 2016-06-28 09:44:47 -0700 | [diff] [blame] | 253 | const std::string& type, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 254 | rtc::PacketSocketFactory* factory, |
| 255 | rtc::Network* network, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 256 | const std::string& username_fragment, |
| 257 | const std::string& password) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 258 | : thread_(thread), |
| 259 | factory_(factory), |
Honghai Zhang | d00c057 | 2016-06-28 09:44:47 -0700 | [diff] [blame] | 260 | type_(type), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 261 | send_retransmit_count_attribute_(false), |
| 262 | network_(network), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 263 | min_port_(0), |
| 264 | max_port_(0), |
| 265 | component_(ICE_CANDIDATE_COMPONENT_DEFAULT), |
| 266 | generation_(0), |
| 267 | ice_username_fragment_(username_fragment), |
| 268 | password_(password), |
| 269 | timeout_delay_(kPortTimeoutDelay), |
| 270 | enable_port_packets_(false), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 271 | ice_role_(ICEROLE_UNKNOWN), |
| 272 | tiebreaker_(0), |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 273 | shared_socket_(true) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 274 | Construct(); |
| 275 | } |
| 276 | |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 277 | Port::Port(rtc::Thread* thread, |
| 278 | const std::string& type, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 279 | rtc::PacketSocketFactory* factory, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 280 | rtc::Network* network, |
Steve Anton | f2737d2 | 2017-10-31 16:27:34 -0700 | [diff] [blame] | 281 | const rtc::IPAddress& ip, |
| 282 | const std::string& username_fragment, |
| 283 | const std::string& password) |
| 284 | : Port(thread, type, factory, network, username_fragment, password) {} |
| 285 | |
| 286 | Port::Port(rtc::Thread* thread, |
| 287 | const std::string& type, |
| 288 | rtc::PacketSocketFactory* factory, |
| 289 | rtc::Network* network, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 290 | uint16_t min_port, |
| 291 | uint16_t max_port, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 292 | const std::string& username_fragment, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 293 | const std::string& password) |
| 294 | : thread_(thread), |
| 295 | factory_(factory), |
| 296 | type_(type), |
| 297 | send_retransmit_count_attribute_(false), |
| 298 | network_(network), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 299 | min_port_(min_port), |
| 300 | max_port_(max_port), |
| 301 | component_(ICE_CANDIDATE_COMPONENT_DEFAULT), |
| 302 | generation_(0), |
| 303 | ice_username_fragment_(username_fragment), |
| 304 | password_(password), |
| 305 | timeout_delay_(kPortTimeoutDelay), |
| 306 | enable_port_packets_(false), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 307 | ice_role_(ICEROLE_UNKNOWN), |
| 308 | tiebreaker_(0), |
Taylor Brandstetter | 417eebe | 2016-05-23 16:02:19 -0700 | [diff] [blame] | 309 | shared_socket_(false) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 310 | RTC_DCHECK(factory_ != NULL); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 311 | Construct(); |
| 312 | } |
| 313 | |
| 314 | void Port::Construct() { |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 315 | // TODO(pthatcher): Remove this old behavior once we're sure no one |
| 316 | // relies on it. If the username_fragment and password are empty, |
| 317 | // we should just create one. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 318 | if (ice_username_fragment_.empty()) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 319 | RTC_DCHECK(password_.empty()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 320 | ice_username_fragment_ = rtc::CreateRandomString(ICE_UFRAG_LENGTH); |
| 321 | password_ = rtc::CreateRandomString(ICE_PWD_LENGTH); |
| 322 | } |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 323 | network_->SignalTypeChanged.connect(this, &Port::OnNetworkTypeChanged); |
| 324 | network_cost_ = network_->GetCost(); |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 325 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 326 | thread_->PostDelayed(RTC_FROM_HERE, timeout_delay_, this, |
| 327 | MSG_DESTROY_IF_DEAD); |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 328 | RTC_LOG(LS_INFO) << ToString() |
| 329 | << ": Port created with network cost " << network_cost_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | Port::~Port() { |
| 333 | // Delete all of the remaining connections. We copy the list up front |
| 334 | // because each deletion will cause it to be modified. |
| 335 | |
| 336 | std::vector<Connection*> list; |
| 337 | |
| 338 | AddressMap::iterator iter = connections_.begin(); |
| 339 | while (iter != connections_.end()) { |
| 340 | list.push_back(iter->second); |
| 341 | ++iter; |
| 342 | } |
| 343 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 344 | for (uint32_t i = 0; i < list.size(); i++) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 345 | delete list[i]; |
| 346 | } |
| 347 | |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 348 | const std::string& Port::Type() const { |
| 349 | return type_; |
| 350 | } |
| 351 | rtc::Network* Port::Network() const { |
| 352 | return network_; |
| 353 | } |
| 354 | |
| 355 | IceRole Port::GetIceRole() const { |
| 356 | return ice_role_; |
| 357 | } |
| 358 | |
| 359 | void Port::SetIceRole(IceRole role) { |
| 360 | ice_role_ = role; |
| 361 | } |
| 362 | |
| 363 | void Port::SetIceTiebreaker(uint64_t tiebreaker) { |
| 364 | tiebreaker_ = tiebreaker; |
| 365 | } |
| 366 | uint64_t Port::IceTiebreaker() const { |
| 367 | return tiebreaker_; |
| 368 | } |
| 369 | |
| 370 | bool Port::SharedSocket() const { |
| 371 | return shared_socket_; |
| 372 | } |
| 373 | |
Taylor Brandstetter | a1c3035 | 2016-05-13 08:15:11 -0700 | [diff] [blame] | 374 | void Port::SetIceParameters(int component, |
| 375 | const std::string& username_fragment, |
| 376 | const std::string& password) { |
| 377 | component_ = component; |
| 378 | ice_username_fragment_ = username_fragment; |
| 379 | password_ = password; |
| 380 | for (Candidate& c : candidates_) { |
| 381 | c.set_component(component); |
| 382 | c.set_username(username_fragment); |
| 383 | c.set_password(password); |
| 384 | } |
| 385 | } |
| 386 | |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 387 | const std::vector<Candidate>& Port::Candidates() const { |
| 388 | return candidates_; |
| 389 | } |
| 390 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 391 | Connection* Port::GetConnection(const rtc::SocketAddress& remote_addr) { |
| 392 | AddressMap::const_iterator iter = connections_.find(remote_addr); |
| 393 | if (iter != connections_.end()) |
| 394 | return iter->second; |
| 395 | else |
| 396 | return NULL; |
| 397 | } |
| 398 | |
| 399 | void Port::AddAddress(const rtc::SocketAddress& address, |
| 400 | const rtc::SocketAddress& base_address, |
| 401 | const rtc::SocketAddress& related_address, |
| 402 | const std::string& protocol, |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 403 | const std::string& relay_protocol, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 404 | const std::string& tcptype, |
| 405 | const std::string& type, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 406 | uint32_t type_preference, |
| 407 | uint32_t relay_preference, |
Peter Boström | 2758c66 | 2017-02-13 20:33:27 -0500 | [diff] [blame] | 408 | bool final) { |
| 409 | AddAddress(address, base_address, related_address, protocol, relay_protocol, |
| 410 | tcptype, type, type_preference, relay_preference, "", final); |
| 411 | } |
| 412 | |
| 413 | void Port::AddAddress(const rtc::SocketAddress& address, |
| 414 | const rtc::SocketAddress& base_address, |
| 415 | const rtc::SocketAddress& related_address, |
| 416 | const std::string& protocol, |
| 417 | const std::string& relay_protocol, |
| 418 | const std::string& tcptype, |
| 419 | const std::string& type, |
| 420 | uint32_t type_preference, |
| 421 | uint32_t relay_preference, |
zhihuang | 26d99c2 | 2017-02-13 12:47:27 -0800 | [diff] [blame] | 422 | const std::string& url, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 423 | bool final) { |
| 424 | if (protocol == TCP_PROTOCOL_NAME && type == LOCAL_PORT_TYPE) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 425 | RTC_DCHECK(!tcptype.empty()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 426 | } |
| 427 | |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 428 | std::string foundation = |
| 429 | ComputeFoundation(type, protocol, relay_protocol, base_address); |
| 430 | Candidate c(component_, protocol, address, 0U, username_fragment(), password_, |
| 431 | type, generation_, foundation, network_->id(), network_cost_); |
| 432 | c.set_priority( |
| 433 | c.GetPriority(type_preference, network_->preference(), relay_preference)); |
Guo-wei Shieh | 3d564c1 | 2015-08-19 16:51:15 -0700 | [diff] [blame] | 434 | c.set_relay_protocol(relay_protocol); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 435 | c.set_tcptype(tcptype); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 436 | c.set_network_name(network_->name()); |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 437 | c.set_network_type(network_->type()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 438 | c.set_related_address(related_address); |
zhihuang | 26d99c2 | 2017-02-13 12:47:27 -0800 | [diff] [blame] | 439 | c.set_url(url); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 440 | candidates_.push_back(c); |
| 441 | SignalCandidateReady(this, c); |
| 442 | |
| 443 | if (final) { |
| 444 | SignalPortComplete(this); |
| 445 | } |
| 446 | } |
| 447 | |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 448 | void Port::AddOrReplaceConnection(Connection* conn) { |
| 449 | auto ret = connections_.insert( |
| 450 | std::make_pair(conn->remote_candidate().address(), conn)); |
| 451 | // If there is a different connection on the same remote address, replace |
| 452 | // it with the new one and destroy the old one. |
| 453 | if (ret.second == false && ret.first->second != conn) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 454 | RTC_LOG(LS_WARNING) |
| 455 | << ToString() |
| 456 | << ": A new connection was created on an existing remote address. " |
| 457 | "New remote candidate: " |
| 458 | << conn->remote_candidate().ToString(); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 459 | ret.first->second->SignalDestroyed.disconnect(this); |
| 460 | ret.first->second->Destroy(); |
| 461 | ret.first->second = conn; |
| 462 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 463 | conn->SignalDestroyed.connect(this, &Port::OnConnectionDestroyed); |
| 464 | SignalConnectionCreated(this, conn); |
| 465 | } |
| 466 | |
| 467 | void Port::OnReadPacket( |
| 468 | const char* data, size_t size, const rtc::SocketAddress& addr, |
| 469 | ProtocolType proto) { |
| 470 | // If the user has enabled port packets, just hand this over. |
| 471 | if (enable_port_packets_) { |
| 472 | SignalReadPacket(this, data, size, addr); |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | // If this is an authenticated STUN request, then signal unknown address and |
| 477 | // send back a proper binding response. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 478 | std::unique_ptr<IceMessage> msg; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 479 | std::string remote_username; |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 480 | if (!GetStunMessage(data, size, addr, &msg, &remote_username)) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 481 | RTC_LOG(LS_ERROR) << ToString() |
| 482 | << ": Received non-STUN packet from unknown address: " |
| 483 | << addr.ToSensitiveString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 484 | } else if (!msg) { |
| 485 | // STUN message handled already |
| 486 | } else if (msg->type() == STUN_BINDING_REQUEST) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 487 | RTC_LOG(LS_INFO) << "Received STUN ping id=" |
| 488 | << rtc::hex_encode(msg->transaction_id()) |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 489 | << " from unknown address " << addr.ToSensitiveString(); |
Qingsi Wang | 2bd41f9 | 2018-03-23 14:28:37 -0700 | [diff] [blame] | 490 | // We need to signal an unknown address before we handle any role conflict |
| 491 | // below. Otherwise there would be no candidate pair and TURN entry created |
| 492 | // to send the error response in case of a role conflict. |
| 493 | SignalUnknownAddress(this, addr, proto, msg.get(), remote_username, false); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 494 | // Check for role conflicts. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 495 | if (!MaybeIceRoleConflict(addr, msg.get(), remote_username)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 496 | RTC_LOG(LS_INFO) << "Received conflicting role from the peer."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 497 | return; |
| 498 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 499 | } else { |
| 500 | // NOTE(tschmelcher): STUN_BINDING_RESPONSE is benign. It occurs if we |
| 501 | // pruned a connection for this port while it had STUN requests in flight, |
| 502 | // because we then get back responses for them, which this code correctly |
| 503 | // does not handle. |
| 504 | if (msg->type() != STUN_BINDING_RESPONSE) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 505 | RTC_LOG(LS_ERROR) << ToString() |
| 506 | << ": Received unexpected STUN message type: " |
| 507 | << msg->type() << " from unknown address: " |
| 508 | << addr.ToSensitiveString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | void Port::OnReadyToSend() { |
| 514 | AddressMap::iterator iter = connections_.begin(); |
| 515 | for (; iter != connections_.end(); ++iter) { |
| 516 | iter->second->OnReadyToSend(); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | size_t Port::AddPrflxCandidate(const Candidate& local) { |
| 521 | candidates_.push_back(local); |
| 522 | return (candidates_.size() - 1); |
| 523 | } |
| 524 | |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 525 | bool Port::GetStunMessage(const char* data, |
| 526 | size_t size, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 527 | const rtc::SocketAddress& addr, |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 528 | std::unique_ptr<IceMessage>* out_msg, |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 529 | std::string* out_username) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 530 | // NOTE: This could clearly be optimized to avoid allocating any memory. |
| 531 | // However, at the data rates we'll be looking at on the client side, |
| 532 | // this probably isn't worth worrying about. |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 533 | RTC_DCHECK(out_msg != NULL); |
| 534 | RTC_DCHECK(out_username != NULL); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 535 | out_username->clear(); |
| 536 | |
| 537 | // Don't bother parsing the packet if we can tell it's not STUN. |
| 538 | // In ICE mode, all STUN packets will have a valid fingerprint. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 539 | if (!StunMessage::ValidateFingerprint(data, size)) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 540 | return false; |
| 541 | } |
| 542 | |
| 543 | // Parse the request message. If the packet is not a complete and correct |
| 544 | // STUN message, then ignore it. |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 545 | std::unique_ptr<IceMessage> stun_msg(new IceMessage()); |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 546 | rtc::ByteBufferReader buf(data, size); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 547 | if (!stun_msg->Read(&buf) || (buf.Length() > 0)) { |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | if (stun_msg->type() == STUN_BINDING_REQUEST) { |
| 552 | // Check for the presence of USERNAME and MESSAGE-INTEGRITY (if ICE) first. |
| 553 | // If not present, fail with a 400 Bad Request. |
| 554 | if (!stun_msg->GetByteString(STUN_ATTR_USERNAME) || |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 555 | !stun_msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY)) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 556 | RTC_LOG(LS_ERROR) << ToString() |
| 557 | << ": Received STUN request without username/M-I from: " |
| 558 | << addr.ToSensitiveString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 559 | SendBindingErrorResponse(stun_msg.get(), addr, STUN_ERROR_BAD_REQUEST, |
| 560 | STUN_ERROR_REASON_BAD_REQUEST); |
| 561 | return true; |
| 562 | } |
| 563 | |
| 564 | // If the username is bad or unknown, fail with a 401 Unauthorized. |
| 565 | std::string local_ufrag; |
| 566 | std::string remote_ufrag; |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 567 | if (!ParseStunUsername(stun_msg.get(), &local_ufrag, &remote_ufrag) || |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 568 | local_ufrag != username_fragment()) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 569 | RTC_LOG(LS_ERROR) << ToString() |
| 570 | << ": Received STUN request with bad local username " |
| 571 | << local_ufrag << " from " << addr.ToSensitiveString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 572 | SendBindingErrorResponse(stun_msg.get(), addr, STUN_ERROR_UNAUTHORIZED, |
| 573 | STUN_ERROR_REASON_UNAUTHORIZED); |
| 574 | return true; |
| 575 | } |
| 576 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 577 | // If ICE, and the MESSAGE-INTEGRITY is bad, fail with a 401 Unauthorized |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 578 | if (!stun_msg->ValidateMessageIntegrity(data, size, password_)) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 579 | RTC_LOG(LS_ERROR) << ToString() |
| 580 | << ": Received STUN request with bad M-I from " |
| 581 | << addr.ToSensitiveString() |
| 582 | << ", password_=" << password_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 583 | SendBindingErrorResponse(stun_msg.get(), addr, STUN_ERROR_UNAUTHORIZED, |
| 584 | STUN_ERROR_REASON_UNAUTHORIZED); |
| 585 | return true; |
| 586 | } |
| 587 | out_username->assign(remote_ufrag); |
| 588 | } else if ((stun_msg->type() == STUN_BINDING_RESPONSE) || |
| 589 | (stun_msg->type() == STUN_BINDING_ERROR_RESPONSE)) { |
| 590 | if (stun_msg->type() == STUN_BINDING_ERROR_RESPONSE) { |
| 591 | if (const StunErrorCodeAttribute* error_code = stun_msg->GetErrorCode()) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 592 | RTC_LOG(LS_ERROR) << ToString() |
| 593 | << ": Received STUN binding error: class=" |
| 594 | << error_code->eclass() |
| 595 | << " number=" << error_code->number() << " reason='" |
| 596 | << error_code->reason() << "' from " |
| 597 | << addr.ToSensitiveString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 598 | // Return message to allow error-specific processing |
| 599 | } else { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 600 | RTC_LOG(LS_ERROR) |
| 601 | << ToString() |
| 602 | << ": Received STUN binding error without a error code from " |
| 603 | << addr.ToSensitiveString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 604 | return true; |
| 605 | } |
| 606 | } |
| 607 | // NOTE: Username should not be used in verifying response messages. |
| 608 | out_username->clear(); |
| 609 | } else if (stun_msg->type() == STUN_BINDING_INDICATION) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 610 | RTC_LOG(LS_VERBOSE) << ToString() |
| 611 | << ": Received STUN binding indication: from " |
| 612 | << addr.ToSensitiveString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 613 | out_username->clear(); |
| 614 | // No stun attributes will be verified, if it's stun indication message. |
| 615 | // Returning from end of the this method. |
| 616 | } else { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 617 | RTC_LOG(LS_ERROR) << ToString() |
| 618 | << ": Received STUN packet with invalid type (" |
| 619 | << stun_msg->type() << ") from " |
| 620 | << addr.ToSensitiveString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 621 | return true; |
| 622 | } |
| 623 | |
| 624 | // Return the STUN message found. |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 625 | *out_msg = std::move(stun_msg); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 626 | return true; |
| 627 | } |
| 628 | |
| 629 | bool Port::IsCompatibleAddress(const rtc::SocketAddress& addr) { |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 630 | // Get a representative IP for the Network this port is configured to use. |
| 631 | rtc::IPAddress ip = network_->GetBestIP(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 632 | // We use single-stack sockets, so families must match. |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 633 | if (addr.family() != ip.family()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 634 | return false; |
| 635 | } |
| 636 | // Link-local IPv6 ports can only connect to other link-local IPv6 ports. |
deadbeef | 5c3c104 | 2017-08-04 15:01:57 -0700 | [diff] [blame] | 637 | if (ip.family() == AF_INET6 && |
| 638 | (IPIsLinkLocal(ip) != IPIsLinkLocal(addr.ipaddr()))) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 639 | return false; |
| 640 | } |
| 641 | return true; |
| 642 | } |
| 643 | |
| 644 | bool Port::ParseStunUsername(const StunMessage* stun_msg, |
| 645 | std::string* local_ufrag, |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 646 | std::string* remote_ufrag) const { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 647 | // The packet must include a username that either begins or ends with our |
| 648 | // fragment. It should begin with our fragment if it is a request and it |
| 649 | // should end with our fragment if it is a response. |
| 650 | local_ufrag->clear(); |
| 651 | remote_ufrag->clear(); |
| 652 | const StunByteStringAttribute* username_attr = |
| 653 | stun_msg->GetByteString(STUN_ATTR_USERNAME); |
| 654 | if (username_attr == NULL) |
| 655 | return false; |
| 656 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 657 | // RFRAG:LFRAG |
| 658 | const std::string username = username_attr->GetString(); |
| 659 | size_t colon_pos = username.find(":"); |
| 660 | if (colon_pos == std::string::npos) { |
| 661 | return false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 662 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 663 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 664 | *local_ufrag = username.substr(0, colon_pos); |
| 665 | *remote_ufrag = username.substr(colon_pos + 1, username.size()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 666 | return true; |
| 667 | } |
| 668 | |
| 669 | bool Port::MaybeIceRoleConflict( |
| 670 | const rtc::SocketAddress& addr, IceMessage* stun_msg, |
| 671 | const std::string& remote_ufrag) { |
| 672 | // Validate ICE_CONTROLLING or ICE_CONTROLLED attributes. |
| 673 | bool ret = true; |
| 674 | IceRole remote_ice_role = ICEROLE_UNKNOWN; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 675 | uint64_t remote_tiebreaker = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 676 | const StunUInt64Attribute* stun_attr = |
| 677 | stun_msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING); |
| 678 | if (stun_attr) { |
| 679 | remote_ice_role = ICEROLE_CONTROLLING; |
| 680 | remote_tiebreaker = stun_attr->value(); |
| 681 | } |
| 682 | |
| 683 | // If |remote_ufrag| is same as port local username fragment and |
| 684 | // tie breaker value received in the ping message matches port |
| 685 | // tiebreaker value this must be a loopback call. |
| 686 | // We will treat this as valid scenario. |
| 687 | if (remote_ice_role == ICEROLE_CONTROLLING && |
| 688 | username_fragment() == remote_ufrag && |
| 689 | remote_tiebreaker == IceTiebreaker()) { |
| 690 | return true; |
| 691 | } |
| 692 | |
| 693 | stun_attr = stun_msg->GetUInt64(STUN_ATTR_ICE_CONTROLLED); |
| 694 | if (stun_attr) { |
| 695 | remote_ice_role = ICEROLE_CONTROLLED; |
| 696 | remote_tiebreaker = stun_attr->value(); |
| 697 | } |
| 698 | |
| 699 | switch (ice_role_) { |
| 700 | case ICEROLE_CONTROLLING: |
| 701 | if (ICEROLE_CONTROLLING == remote_ice_role) { |
| 702 | if (remote_tiebreaker >= tiebreaker_) { |
| 703 | SignalRoleConflict(this); |
| 704 | } else { |
| 705 | // Send Role Conflict (487) error response. |
| 706 | SendBindingErrorResponse(stun_msg, addr, |
| 707 | STUN_ERROR_ROLE_CONFLICT, STUN_ERROR_REASON_ROLE_CONFLICT); |
| 708 | ret = false; |
| 709 | } |
| 710 | } |
| 711 | break; |
| 712 | case ICEROLE_CONTROLLED: |
| 713 | if (ICEROLE_CONTROLLED == remote_ice_role) { |
| 714 | if (remote_tiebreaker < tiebreaker_) { |
| 715 | SignalRoleConflict(this); |
| 716 | } else { |
| 717 | // Send Role Conflict (487) error response. |
| 718 | SendBindingErrorResponse(stun_msg, addr, |
| 719 | STUN_ERROR_ROLE_CONFLICT, STUN_ERROR_REASON_ROLE_CONFLICT); |
| 720 | ret = false; |
| 721 | } |
| 722 | } |
| 723 | break; |
| 724 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 725 | RTC_NOTREACHED(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 726 | } |
| 727 | return ret; |
| 728 | } |
| 729 | |
| 730 | void Port::CreateStunUsername(const std::string& remote_username, |
| 731 | std::string* stun_username_attr_str) const { |
| 732 | stun_username_attr_str->clear(); |
| 733 | *stun_username_attr_str = remote_username; |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 734 | stun_username_attr_str->append(":"); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 735 | stun_username_attr_str->append(username_fragment()); |
| 736 | } |
| 737 | |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 738 | bool Port::HandleIncomingPacket(rtc::AsyncPacketSocket* socket, |
| 739 | const char* data, |
| 740 | size_t size, |
| 741 | const rtc::SocketAddress& remote_addr, |
| 742 | const rtc::PacketTime& packet_time) { |
| 743 | RTC_NOTREACHED(); |
| 744 | return false; |
| 745 | } |
| 746 | |
Jonas Oreland | 202994c | 2017-12-18 12:10:43 +0100 | [diff] [blame] | 747 | bool Port::CanHandleIncomingPacketsFrom(const rtc::SocketAddress&) const { |
| 748 | return false; |
| 749 | } |
| 750 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 751 | void Port::SendBindingResponse(StunMessage* request, |
| 752 | const rtc::SocketAddress& addr) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 753 | RTC_DCHECK(request->type() == STUN_BINDING_REQUEST); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 754 | |
| 755 | // Retrieve the username from the request. |
| 756 | const StunByteStringAttribute* username_attr = |
| 757 | request->GetByteString(STUN_ATTR_USERNAME); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 758 | RTC_DCHECK(username_attr != NULL); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 759 | if (username_attr == NULL) { |
| 760 | // No valid username, skip the response. |
| 761 | return; |
| 762 | } |
| 763 | |
| 764 | // Fill in the response message. |
| 765 | StunMessage response; |
| 766 | response.SetType(STUN_BINDING_RESPONSE); |
| 767 | response.SetTransactionID(request->transaction_id()); |
| 768 | const StunUInt32Attribute* retransmit_attr = |
| 769 | request->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT); |
| 770 | if (retransmit_attr) { |
| 771 | // Inherit the incoming retransmit value in the response so the other side |
| 772 | // can see our view of lost pings. |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 773 | response.AddAttribute(rtc::MakeUnique<StunUInt32Attribute>( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 774 | STUN_ATTR_RETRANSMIT_COUNT, retransmit_attr->value())); |
| 775 | |
| 776 | if (retransmit_attr->value() > CONNECTION_WRITE_CONNECT_FAILURES) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 777 | RTC_LOG(LS_INFO) |
| 778 | << ToString() |
| 779 | << ": Received a remote ping with high retransmit count: " |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 780 | << retransmit_attr->value(); |
| 781 | } |
| 782 | } |
| 783 | |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 784 | response.AddAttribute(rtc::MakeUnique<StunXorAddressAttribute>( |
| 785 | STUN_ATTR_XOR_MAPPED_ADDRESS, addr)); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 786 | response.AddMessageIntegrity(password_); |
| 787 | response.AddFingerprint(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 788 | |
| 789 | // Send the response message. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 790 | rtc::ByteBufferWriter buf; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 791 | response.Write(&buf); |
| 792 | rtc::PacketOptions options(DefaultDscpValue()); |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 793 | options.info_signaled_after_sent.packet_type = |
| 794 | rtc::PacketType::kIceConnectivityCheckResponse; |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 795 | auto err = SendTo(buf.Data(), buf.Length(), addr, options, false); |
| 796 | if (err < 0) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 797 | RTC_LOG(LS_ERROR) << ToString() |
| 798 | << ": Failed to send STUN ping response, to=" |
| 799 | << addr.ToSensitiveString() << ", err=" << err |
| 800 | << ", id=" << rtc::hex_encode(response.transaction_id()); |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 801 | } else { |
| 802 | // Log at LS_INFO if we send a stun ping response on an unwritable |
| 803 | // connection. |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 804 | Connection* conn = GetConnection(addr); |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 805 | rtc::LoggingSeverity sev = (conn && !conn->writable()) ? |
| 806 | rtc::LS_INFO : rtc::LS_VERBOSE; |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 807 | RTC_LOG_V(sev) << ToString() |
| 808 | << ": Sent STUN ping response, to=" |
| 809 | << addr.ToSensitiveString() |
| 810 | << ", id=" << rtc::hex_encode(response.transaction_id()); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 811 | |
| 812 | conn->stats_.sent_ping_responses++; |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 813 | conn->LogCandidatePairEvent( |
| 814 | webrtc::IceCandidatePairEventType::kCheckResponseSent); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 815 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | void Port::SendBindingErrorResponse(StunMessage* request, |
| 819 | const rtc::SocketAddress& addr, |
| 820 | int error_code, const std::string& reason) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 821 | RTC_DCHECK(request->type() == STUN_BINDING_REQUEST); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 822 | |
| 823 | // Fill in the response message. |
| 824 | StunMessage response; |
| 825 | response.SetType(STUN_BINDING_ERROR_RESPONSE); |
| 826 | response.SetTransactionID(request->transaction_id()); |
| 827 | |
| 828 | // When doing GICE, we need to write out the error code incorrectly to |
| 829 | // maintain backwards compatiblility. |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 830 | auto error_attr = StunAttribute::CreateErrorCode(); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 831 | error_attr->SetCode(error_code); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 832 | error_attr->SetReason(reason); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 833 | response.AddAttribute(std::move(error_attr)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 834 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 835 | // Per Section 10.1.2, certain error cases don't get a MESSAGE-INTEGRITY, |
| 836 | // because we don't have enough information to determine the shared secret. |
| 837 | if (error_code != STUN_ERROR_BAD_REQUEST && |
| 838 | error_code != STUN_ERROR_UNAUTHORIZED) |
| 839 | response.AddMessageIntegrity(password_); |
| 840 | response.AddFingerprint(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 841 | |
| 842 | // Send the response message. |
jbauch | f1f8720 | 2016-03-30 06:43:37 -0700 | [diff] [blame] | 843 | rtc::ByteBufferWriter buf; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 844 | response.Write(&buf); |
| 845 | rtc::PacketOptions options(DefaultDscpValue()); |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 846 | options.info_signaled_after_sent.packet_type = |
| 847 | rtc::PacketType::kIceConnectivityCheckResponse; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 848 | SendTo(buf.Data(), buf.Length(), addr, options, false); |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 849 | RTC_LOG(LS_INFO) << ToString() |
| 850 | << ": Sending STUN binding error: reason=" << reason |
| 851 | << " to " << addr.ToSensitiveString(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 854 | void Port::KeepAliveUntilPruned() { |
| 855 | // If it is pruned, we won't bring it up again. |
| 856 | if (state_ == State::INIT) { |
| 857 | state_ = State::KEEP_ALIVE_UNTIL_PRUNED; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | void Port::Prune() { |
| 862 | state_ = State::PRUNED; |
| 863 | thread_->Post(RTC_FROM_HERE, this, MSG_DESTROY_IF_DEAD); |
| 864 | } |
| 865 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 866 | void Port::OnMessage(rtc::Message *pmsg) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 867 | RTC_DCHECK(pmsg->message_id == MSG_DESTROY_IF_DEAD); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 868 | bool dead = |
| 869 | (state_ == State::INIT || state_ == State::PRUNED) && |
| 870 | connections_.empty() && |
| 871 | rtc::TimeMillis() - last_time_all_connections_removed_ >= timeout_delay_; |
| 872 | if (dead) { |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 873 | Destroy(); |
| 874 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 877 | void Port::OnNetworkTypeChanged(const rtc::Network* network) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 878 | RTC_DCHECK(network == network_); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 879 | |
| 880 | UpdateNetworkCost(); |
| 881 | } |
| 882 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 883 | std::string Port::ToString() const { |
| 884 | std::stringstream ss; |
honghaiz | e3c6c82 | 2016-02-17 13:00:28 -0800 | [diff] [blame] | 885 | ss << "Port[" << std::hex << this << std::dec << ":" << content_name_ << ":" |
| 886 | << component_ << ":" << generation_ << ":" << type_ << ":" |
| 887 | << network_->ToString() << "]"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 888 | return ss.str(); |
| 889 | } |
| 890 | |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 891 | // TODO(honghaiz): Make the network cost configurable from user setting. |
| 892 | void Port::UpdateNetworkCost() { |
| 893 | uint16_t new_cost = network_->GetCost(); |
| 894 | if (network_cost_ == new_cost) { |
| 895 | return; |
| 896 | } |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 897 | RTC_LOG(LS_INFO) << "Network cost changed from " << network_cost_ << " to " |
| 898 | << new_cost |
| 899 | << ". Number of candidates created: " << candidates_.size() |
| 900 | << ". Number of connections created: " |
| 901 | << connections_.size(); |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 902 | network_cost_ = new_cost; |
| 903 | for (cricket::Candidate& candidate : candidates_) { |
| 904 | candidate.set_network_cost(network_cost_); |
| 905 | } |
| 906 | // Network cost change will affect the connection selection criteria. |
| 907 | // Signal the connection state change on each connection to force a |
| 908 | // re-sort in P2PTransportChannel. |
| 909 | for (auto kv : connections_) { |
| 910 | Connection* conn = kv.second; |
| 911 | conn->SignalStateChange(conn); |
| 912 | } |
| 913 | } |
| 914 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 915 | void Port::EnablePortPackets() { |
| 916 | enable_port_packets_ = true; |
| 917 | } |
| 918 | |
| 919 | void Port::OnConnectionDestroyed(Connection* conn) { |
| 920 | AddressMap::iterator iter = |
| 921 | connections_.find(conn->remote_candidate().address()); |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 922 | RTC_DCHECK(iter != connections_.end()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 923 | connections_.erase(iter); |
honghaiz | 36f50e8 | 2016-06-01 15:57:03 -0700 | [diff] [blame] | 924 | HandleConnectionDestroyed(conn); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 925 | |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 926 | // Ports time out after all connections fail if it is not marked as |
| 927 | // "keep alive until pruned." |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 928 | // Note: If a new connection is added after this message is posted, but it |
| 929 | // fails and is removed before kPortTimeoutDelay, then this message will |
Honghai Zhang | b5db1ec | 2016-07-28 13:23:05 -0700 | [diff] [blame] | 930 | // not cause the Port to be destroyed. |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 931 | if (connections_.empty()) { |
Honghai Zhang | b5db1ec | 2016-07-28 13:23:05 -0700 | [diff] [blame] | 932 | last_time_all_connections_removed_ = rtc::TimeMillis(); |
Honghai Zhang | a74363c | 2016-07-28 18:06:15 -0700 | [diff] [blame] | 933 | thread_->PostDelayed(RTC_FROM_HERE, timeout_delay_, this, |
| 934 | MSG_DESTROY_IF_DEAD); |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 935 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | void Port::Destroy() { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 939 | RTC_DCHECK(connections_.empty()); |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 940 | RTC_LOG(LS_INFO) << ToString() << ": Port deleted"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 941 | SignalDestroyed(this); |
| 942 | delete this; |
| 943 | } |
| 944 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 945 | const std::string Port::username_fragment() const { |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 946 | return ice_username_fragment_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 949 | void Port::CopyPortInformationToPacketInfo(rtc::PacketInfo* info) const { |
| 950 | info->protocol = ConvertProtocolTypeToPacketInfoProtocolType(GetProtocol()); |
| 951 | info->network_id = Network()->id(); |
| 952 | } |
| 953 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 954 | // A ConnectionRequest is a simple STUN ping used to determine writability. |
| 955 | class ConnectionRequest : public StunRequest { |
| 956 | public: |
| 957 | explicit ConnectionRequest(Connection* connection) |
| 958 | : StunRequest(new IceMessage()), |
| 959 | connection_(connection) { |
| 960 | } |
| 961 | |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 962 | void Prepare(StunMessage* request) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 963 | request->SetType(STUN_BINDING_REQUEST); |
| 964 | std::string username; |
| 965 | connection_->port()->CreateStunUsername( |
| 966 | connection_->remote_candidate().username(), &username); |
| 967 | request->AddAttribute( |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 968 | rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_USERNAME, username)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 969 | |
| 970 | // connection_ already holds this ping, so subtract one from count. |
| 971 | if (connection_->port()->send_retransmit_count_attribute()) { |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 972 | request->AddAttribute(rtc::MakeUnique<StunUInt32Attribute>( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 973 | STUN_ATTR_RETRANSMIT_COUNT, |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 974 | static_cast<uint32_t>(connection_->pings_since_last_response_.size() - |
| 975 | 1))); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 976 | } |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 977 | uint32_t network_info = connection_->port()->Network()->id(); |
| 978 | network_info = (network_info << 16) | connection_->port()->network_cost(); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 979 | request->AddAttribute(rtc::MakeUnique<StunUInt32Attribute>( |
| 980 | STUN_ATTR_NETWORK_INFO, network_info)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 981 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 982 | // Adding ICE_CONTROLLED or ICE_CONTROLLING attribute based on the role. |
| 983 | if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLING) { |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 984 | request->AddAttribute(rtc::MakeUnique<StunUInt64Attribute>( |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 985 | STUN_ATTR_ICE_CONTROLLING, connection_->port()->IceTiebreaker())); |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 986 | // We should have either USE_CANDIDATE attribute or ICE_NOMINATION |
| 987 | // attribute but not both. That was enforced in p2ptransportchannel. |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 988 | if (connection_->use_candidate_attr()) { |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 989 | request->AddAttribute( |
| 990 | rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_USE_CANDIDATE)); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 991 | } |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 992 | if (connection_->nomination() && |
| 993 | connection_->nomination() != connection_->acked_nomination()) { |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 994 | request->AddAttribute(rtc::MakeUnique<StunUInt32Attribute>( |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 995 | STUN_ATTR_NOMINATION, connection_->nomination())); |
| 996 | } |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 997 | } else if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLED) { |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 998 | request->AddAttribute(rtc::MakeUnique<StunUInt64Attribute>( |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 999 | STUN_ATTR_ICE_CONTROLLED, connection_->port()->IceTiebreaker())); |
| 1000 | } else { |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 1001 | RTC_NOTREACHED(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1002 | } |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1003 | |
| 1004 | // Adding PRIORITY Attribute. |
| 1005 | // Changing the type preference to Peer Reflexive and local preference |
| 1006 | // and component id information is unchanged from the original priority. |
| 1007 | // priority = (2^24)*(type preference) + |
| 1008 | // (2^8)*(local preference) + |
| 1009 | // (2^0)*(256 - component ID) |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 1010 | uint32_t type_preference = |
| 1011 | (connection_->local_candidate().protocol() == TCP_PROTOCOL_NAME) |
| 1012 | ? ICE_TYPE_PREFERENCE_PRFLX_TCP |
| 1013 | : ICE_TYPE_PREFERENCE_PRFLX; |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1014 | uint32_t prflx_priority = |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 1015 | type_preference << 24 | |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1016 | (connection_->local_candidate().priority() & 0x00FFFFFF); |
zstein | f42cc9d | 2017-03-27 16:17:19 -0700 | [diff] [blame] | 1017 | request->AddAttribute(rtc::MakeUnique<StunUInt32Attribute>( |
| 1018 | STUN_ATTR_PRIORITY, prflx_priority)); |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1019 | |
| 1020 | // Adding Message Integrity attribute. |
| 1021 | request->AddMessageIntegrity(connection_->remote_candidate().password()); |
| 1022 | // Adding Fingerprint. |
| 1023 | request->AddFingerprint(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 1026 | void OnResponse(StunMessage* response) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1027 | connection_->OnConnectionRequestResponse(this, response); |
| 1028 | } |
| 1029 | |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 1030 | void OnErrorResponse(StunMessage* response) override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1031 | connection_->OnConnectionRequestErrorResponse(this, response); |
| 1032 | } |
| 1033 | |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 1034 | void OnTimeout() override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1035 | connection_->OnConnectionRequestTimeout(this); |
| 1036 | } |
| 1037 | |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 1038 | void OnSent() override { |
| 1039 | connection_->OnConnectionRequestSent(this); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1040 | // Each request is sent only once. After a single delay , the request will |
| 1041 | // time out. |
| 1042 | timeout_ = true; |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | int resend_delay() override { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1046 | return CONNECTION_RESPONSE_TIMEOUT; |
| 1047 | } |
| 1048 | |
| 1049 | private: |
| 1050 | Connection* connection_; |
| 1051 | }; |
| 1052 | |
| 1053 | // |
| 1054 | // Connection |
| 1055 | // |
| 1056 | |
guoweis@webrtc.org | 930e004 | 2014-11-17 19:42:14 +0000 | [diff] [blame] | 1057 | Connection::Connection(Port* port, |
| 1058 | size_t index, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1059 | const Candidate& remote_candidate) |
guoweis@webrtc.org | 930e004 | 2014-11-17 19:42:14 +0000 | [diff] [blame] | 1060 | : port_(port), |
| 1061 | local_candidate_index_(index), |
| 1062 | remote_candidate_(remote_candidate), |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 1063 | recv_rate_tracker_(100, 10u), |
| 1064 | send_rate_tracker_(100, 10u), |
guoweis@webrtc.org | 930e004 | 2014-11-17 19:42:14 +0000 | [diff] [blame] | 1065 | write_state_(STATE_WRITE_INIT), |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1066 | receiving_(false), |
guoweis@webrtc.org | 930e004 | 2014-11-17 19:42:14 +0000 | [diff] [blame] | 1067 | connected_(true), |
| 1068 | pruned_(false), |
| 1069 | use_candidate_attr_(false), |
| 1070 | remote_ice_mode_(ICEMODE_FULL), |
| 1071 | requests_(port->thread()), |
| 1072 | rtt_(DEFAULT_RTT), |
| 1073 | last_ping_sent_(0), |
| 1074 | last_ping_received_(0), |
| 1075 | last_data_received_(0), |
| 1076 | last_ping_response_received_(0), |
zstein | abbacbf | 2017-03-20 10:53:12 -0700 | [diff] [blame] | 1077 | packet_loss_estimator_(kConsiderPacketLostAfter, kForgetPacketAfter), |
guoweis@webrtc.org | 930e004 | 2014-11-17 19:42:14 +0000 | [diff] [blame] | 1078 | reported_(false), |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 1079 | state_(IceCandidatePairState::WAITING), |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1080 | time_created_ms_(rtc::TimeMillis()) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1081 | // All of our connections start in WAITING state. |
| 1082 | // TODO(mallinath) - Start connections from STATE_FROZEN. |
| 1083 | // Wire up to send stun packets |
| 1084 | requests_.SignalSendPacket.connect(this, &Connection::OnSendStunPacket); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 1085 | hash_ = static_cast<uint32_t>(std::hash<std::string>{}(ToString())); |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1086 | RTC_LOG(LS_INFO) << ToString() << ": Connection created"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | Connection::~Connection() { |
| 1090 | } |
| 1091 | |
| 1092 | const Candidate& Connection::local_candidate() const { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1093 | RTC_DCHECK(local_candidate_index_ < port_->Candidates().size()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1094 | return port_->Candidates()[local_candidate_index_]; |
| 1095 | } |
| 1096 | |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 1097 | const Candidate& Connection::remote_candidate() const { |
| 1098 | return remote_candidate_; |
| 1099 | } |
| 1100 | |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1101 | uint64_t Connection::priority() const { |
| 1102 | uint64_t priority = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1103 | // RFC 5245 - 5.7.2. Computing Pair Priority and Ordering Pairs |
| 1104 | // Let G be the priority for the candidate provided by the controlling |
| 1105 | // agent. Let D be the priority for the candidate provided by the |
| 1106 | // controlled agent. |
| 1107 | // pair priority = 2^32*MIN(G,D) + 2*MAX(G,D) + (G>D?1:0) |
| 1108 | IceRole role = port_->GetIceRole(); |
| 1109 | if (role != ICEROLE_UNKNOWN) { |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1110 | uint32_t g = 0; |
| 1111 | uint32_t d = 0; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1112 | if (role == ICEROLE_CONTROLLING) { |
| 1113 | g = local_candidate().priority(); |
| 1114 | d = remote_candidate_.priority(); |
| 1115 | } else { |
| 1116 | g = remote_candidate_.priority(); |
| 1117 | d = local_candidate().priority(); |
| 1118 | } |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 1119 | priority = std::min(g, d); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1120 | priority = priority << 32; |
andresp@webrtc.org | ff689be | 2015-02-12 11:54:26 +0000 | [diff] [blame] | 1121 | priority += 2 * std::max(g, d) + (g > d ? 1 : 0); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1122 | } |
| 1123 | return priority; |
| 1124 | } |
| 1125 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1126 | void Connection::set_write_state(WriteState value) { |
| 1127 | WriteState old_value = write_state_; |
| 1128 | write_state_ = value; |
| 1129 | if (value != old_value) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1130 | RTC_LOG(LS_VERBOSE) << ToString() |
| 1131 | << ": set_write_state from: " << old_value << " to " |
| 1132 | << value; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1133 | SignalStateChange(this); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 1137 | void Connection::UpdateReceiving(int64_t now) { |
Qingsi Wang | f82644c9 | 2018-04-16 16:47:32 -0700 | [diff] [blame] | 1138 | bool receiving; |
| 1139 | if (last_ping_sent() < last_ping_response_received()) { |
| 1140 | // We consider any candidate pair that has its last connectivity check |
| 1141 | // acknowledged by a response as receiving, particularly for backup |
| 1142 | // candidate pairs that send checks at a much slower pace than the selected |
| 1143 | // one. Otherwise, a backup candidate pair constantly becomes not receiving |
| 1144 | // as a side effect of a long ping interval, since we do not have a separate |
| 1145 | // receiving timeout for backup candidate pairs. See |
| 1146 | // IceConfig.ice_backup_candidate_pair_ping_interval, |
| 1147 | // IceConfig.ice_connection_receiving_timeout and their default value. |
| 1148 | receiving = true; |
| 1149 | } else { |
| 1150 | receiving = |
| 1151 | last_received() > 0 && now <= last_received() + receiving_timeout(); |
| 1152 | } |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 1153 | if (receiving_ == receiving) { |
| 1154 | return; |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1155 | } |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1156 | RTC_LOG(LS_VERBOSE) << ToString() << ": set_receiving to " |
| 1157 | << receiving; |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 1158 | receiving_ = receiving; |
| 1159 | receiving_unchanged_since_ = now; |
| 1160 | SignalStateChange(this); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 1163 | void Connection::set_state(IceCandidatePairState state) { |
| 1164 | IceCandidatePairState old_state = state_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1165 | state_ = state; |
| 1166 | if (state != old_state) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1167 | RTC_LOG(LS_VERBOSE) << ToString() << ": set_state"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | void Connection::set_connected(bool value) { |
| 1172 | bool old_value = connected_; |
| 1173 | connected_ = value; |
| 1174 | if (value != old_value) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1175 | RTC_LOG(LS_VERBOSE) << ToString() |
| 1176 | << ": Change connected_ to " << value; |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1177 | SignalStateChange(this); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | void Connection::set_use_candidate_attr(bool enable) { |
| 1182 | use_candidate_attr_ = enable; |
| 1183 | } |
| 1184 | |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 1185 | int Connection::unwritable_timeout() const { |
| 1186 | return unwritable_timeout_.value_or(CONNECTION_WRITE_CONNECT_TIMEOUT); |
| 1187 | } |
| 1188 | |
| 1189 | int Connection::unwritable_min_checks() const { |
| 1190 | return unwritable_min_checks_.value_or(CONNECTION_WRITE_CONNECT_FAILURES); |
| 1191 | } |
| 1192 | |
Qingsi Wang | 866e08d | 2018-03-22 17:54:23 -0700 | [diff] [blame] | 1193 | int Connection::receiving_timeout() const { |
| 1194 | return receiving_timeout_.value_or(WEAK_CONNECTION_RECEIVE_TIMEOUT); |
| 1195 | } |
| 1196 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1197 | void Connection::OnSendStunPacket(const void* data, size_t size, |
| 1198 | StunRequest* req) { |
| 1199 | rtc::PacketOptions options(port_->DefaultDscpValue()); |
Qingsi Wang | 6e641e6 | 2018-04-11 20:14:17 -0700 | [diff] [blame] | 1200 | options.info_signaled_after_sent.packet_type = |
| 1201 | rtc::PacketType::kIceConnectivityCheck; |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 1202 | auto err = port_->SendTo( |
| 1203 | data, size, remote_candidate_.address(), options, false); |
| 1204 | if (err < 0) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1205 | RTC_LOG(LS_WARNING) << ToString() |
| 1206 | << ": Failed to send STUN ping " |
| 1207 | " err=" |
| 1208 | << err << " id=" << rtc::hex_encode(req->id()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | void Connection::OnReadPacket( |
| 1213 | const char* data, size_t size, const rtc::PacketTime& packet_time) { |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 1214 | std::unique_ptr<IceMessage> msg; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1215 | std::string remote_ufrag; |
| 1216 | const rtc::SocketAddress& addr(remote_candidate_.address()); |
kwiberg | 6baec03 | 2016-03-15 11:09:39 -0700 | [diff] [blame] | 1217 | if (!port_->GetStunMessage(data, size, addr, &msg, &remote_ufrag)) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1218 | // The packet did not parse as a valid STUN message |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1219 | // This is a data packet, pass it along. |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1220 | last_data_received_ = rtc::TimeMillis(); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 1221 | UpdateReceiving(last_data_received_); |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1222 | recv_rate_tracker_.AddSamples(size); |
| 1223 | SignalReadPacket(this, data, size, packet_time); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1224 | |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1225 | // If timed out sending writability checks, start up again |
| 1226 | if (!pruned_ && (write_state_ == STATE_WRITE_TIMEOUT)) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1227 | RTC_LOG(LS_WARNING) |
| 1228 | << "Received a data packet on a timed-out Connection. " |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1229 | "Resetting state to STATE_WRITE_INIT."; |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1230 | set_write_state(STATE_WRITE_INIT); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1231 | } |
| 1232 | } else if (!msg) { |
| 1233 | // The packet was STUN, but failed a check and was handled internally. |
| 1234 | } else { |
| 1235 | // The packet is STUN and passed the Port checks. |
| 1236 | // Perform our own checks to ensure this packet is valid. |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 1237 | // If this is a STUN request, then update the receiving bit and respond. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1238 | // If this is a STUN response, then update the writable bit. |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 1239 | // Log at LS_INFO if we receive a ping on an unwritable connection. |
| 1240 | rtc::LoggingSeverity sev = (!writable() ? rtc::LS_INFO : rtc::LS_VERBOSE); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1241 | switch (msg->type()) { |
| 1242 | case STUN_BINDING_REQUEST: |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1243 | RTC_LOG_V(sev) << ToString() |
| 1244 | << ": Received STUN ping, id=" |
| 1245 | << rtc::hex_encode(msg->transaction_id()); |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 1246 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1247 | if (remote_ufrag == remote_candidate_.username()) { |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 1248 | HandleBindingRequest(msg.get()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1249 | } else { |
| 1250 | // The packet had the right local username, but the remote username |
| 1251 | // was not the right one for the remote address. |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1252 | RTC_LOG(LS_ERROR) |
| 1253 | << ToString() |
| 1254 | << ": Received STUN request with bad remote username " |
| 1255 | << remote_ufrag; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1256 | port_->SendBindingErrorResponse(msg.get(), addr, |
| 1257 | STUN_ERROR_UNAUTHORIZED, |
| 1258 | STUN_ERROR_REASON_UNAUTHORIZED); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1259 | } |
| 1260 | break; |
| 1261 | |
| 1262 | // Response from remote peer. Does it match request sent? |
| 1263 | // This doesn't just check, it makes callbacks if transaction |
| 1264 | // id's match. |
| 1265 | case STUN_BINDING_RESPONSE: |
| 1266 | case STUN_BINDING_ERROR_RESPONSE: |
Peter Thatcher | 7cbd188 | 2015-09-17 18:54:52 -0700 | [diff] [blame] | 1267 | if (msg->ValidateMessageIntegrity( |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1268 | data, size, remote_candidate().password())) { |
| 1269 | requests_.CheckResponse(msg.get()); |
| 1270 | } |
| 1271 | // Otherwise silently discard the response message. |
| 1272 | break; |
| 1273 | |
honghaiz | d0b3143 | 2015-09-30 12:42:17 -0700 | [diff] [blame] | 1274 | // Remote end point sent an STUN indication instead of regular binding |
| 1275 | // request. In this case |last_ping_received_| will be updated but no |
| 1276 | // response will be sent. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1277 | case STUN_BINDING_INDICATION: |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1278 | ReceivedPing(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1279 | break; |
| 1280 | |
| 1281 | default: |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 1282 | RTC_NOTREACHED(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1283 | break; |
| 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 1288 | void Connection::HandleBindingRequest(IceMessage* msg) { |
| 1289 | // This connection should now be receiving. |
| 1290 | ReceivedPing(); |
| 1291 | |
| 1292 | const rtc::SocketAddress& remote_addr = remote_candidate_.address(); |
| 1293 | const std::string& remote_ufrag = remote_candidate_.username(); |
| 1294 | // Check for role conflicts. |
| 1295 | if (!port_->MaybeIceRoleConflict(remote_addr, msg, remote_ufrag)) { |
| 1296 | // Received conflicting role from the peer. |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1297 | RTC_LOG(LS_INFO) << "Received conflicting role from the peer."; |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 1298 | return; |
| 1299 | } |
| 1300 | |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1301 | stats_.recv_ping_requests++; |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 1302 | LogCandidatePairEvent(webrtc::IceCandidatePairEventType::kCheckReceived); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1303 | |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 1304 | // This is a validated stun request from remote peer. |
| 1305 | port_->SendBindingResponse(msg, remote_addr); |
| 1306 | |
| 1307 | // If it timed out on writing check, start up again |
| 1308 | if (!pruned_ && write_state_ == STATE_WRITE_TIMEOUT) { |
| 1309 | set_write_state(STATE_WRITE_INIT); |
| 1310 | } |
| 1311 | |
| 1312 | if (port_->GetIceRole() == ICEROLE_CONTROLLED) { |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 1313 | const StunUInt32Attribute* nomination_attr = |
| 1314 | msg->GetUInt32(STUN_ATTR_NOMINATION); |
| 1315 | uint32_t nomination = 0; |
| 1316 | if (nomination_attr) { |
| 1317 | nomination = nomination_attr->value(); |
| 1318 | if (nomination == 0) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1319 | RTC_LOG(LS_ERROR) << "Invalid nomination: " << nomination; |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 1320 | } |
| 1321 | } else { |
| 1322 | const StunByteStringAttribute* use_candidate_attr = |
| 1323 | msg->GetByteString(STUN_ATTR_USE_CANDIDATE); |
| 1324 | if (use_candidate_attr) { |
| 1325 | nomination = 1; |
| 1326 | } |
| 1327 | } |
| 1328 | // We don't un-nominate a connection, so we only keep a larger nomination. |
| 1329 | if (nomination > remote_nomination_) { |
| 1330 | set_remote_nomination(nomination); |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 1331 | SignalNominated(this); |
| 1332 | } |
| 1333 | } |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1334 | // Set the remote cost if the network_info attribute is available. |
| 1335 | // Note: If packets are re-ordered, we may get incorrect network cost |
| 1336 | // temporarily, but it should get the correct value shortly after that. |
| 1337 | const StunUInt32Attribute* network_attr = |
| 1338 | msg->GetUInt32(STUN_ATTR_NETWORK_INFO); |
| 1339 | if (network_attr) { |
| 1340 | uint32_t network_info = network_attr->value(); |
| 1341 | uint16_t network_cost = static_cast<uint16_t>(network_info); |
| 1342 | if (network_cost != remote_candidate_.network_cost()) { |
| 1343 | remote_candidate_.set_network_cost(network_cost); |
| 1344 | // Network cost change will affect the connection ranking, so signal |
| 1345 | // state change to force a re-sort in P2PTransportChannel. |
| 1346 | SignalStateChange(this); |
| 1347 | } |
| 1348 | } |
honghaiz | 9b5ee9c | 2015-11-11 13:19:17 -0800 | [diff] [blame] | 1349 | } |
| 1350 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1351 | void Connection::OnReadyToSend() { |
deadbeef | dd7fb43 | 2016-09-30 15:16:48 -0700 | [diff] [blame] | 1352 | SignalReadyToSend(this); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
| 1355 | void Connection::Prune() { |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 1356 | if (!pruned_ || active()) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1357 | RTC_LOG(LS_INFO) << ToString() << ": Connection pruned"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1358 | pruned_ = true; |
| 1359 | requests_.Clear(); |
| 1360 | set_write_state(STATE_WRITE_TIMEOUT); |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | void Connection::Destroy() { |
nisse | 7eaa4ea | 2017-05-08 05:25:41 -0700 | [diff] [blame] | 1365 | // TODO(deadbeef, nisse): This may leak if an application closes a |
| 1366 | // PeerConnection and then quickly destroys the PeerConnectionFactory (along |
| 1367 | // with the networking thread on which this message is posted). Also affects |
| 1368 | // tests, with a workaround in |
| 1369 | // AutoSocketServerThread::~AutoSocketServerThread. |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1370 | RTC_LOG(LS_VERBOSE) << ToString() |
| 1371 | << ": Connection destroyed"; |
Taylor Brandstetter | 5d97a9a | 2016-06-10 14:17:27 -0700 | [diff] [blame] | 1372 | port_->thread()->Post(RTC_FROM_HERE, this, MSG_DELETE); |
Bjorn Terelius | 59b4e3e | 2018-05-30 17:14:08 +0200 | [diff] [blame] | 1373 | LogCandidatePairConfig(webrtc::IceCandidatePairConfigType::kDestroyed); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
deadbeef | 376e123 | 2015-11-25 09:00:08 -0800 | [diff] [blame] | 1376 | void Connection::FailAndDestroy() { |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 1377 | set_state(IceCandidatePairState::FAILED); |
deadbeef | 376e123 | 2015-11-25 09:00:08 -0800 | [diff] [blame] | 1378 | Destroy(); |
| 1379 | } |
| 1380 | |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 1381 | void Connection::FailAndPrune() { |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 1382 | set_state(IceCandidatePairState::FAILED); |
honghaiz | 079a7a1 | 2016-06-22 16:26:29 -0700 | [diff] [blame] | 1383 | Prune(); |
| 1384 | } |
| 1385 | |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 1386 | void Connection::PrintPingsSinceLastResponse(std::string* s, size_t max) { |
| 1387 | std::ostringstream oss; |
| 1388 | oss << std::boolalpha; |
| 1389 | if (pings_since_last_response_.size() > max) { |
| 1390 | for (size_t i = 0; i < max; i++) { |
| 1391 | const SentPing& ping = pings_since_last_response_[i]; |
| 1392 | oss << rtc::hex_encode(ping.id) << " "; |
| 1393 | } |
| 1394 | oss << "... " << (pings_since_last_response_.size() - max) << " more"; |
| 1395 | } else { |
| 1396 | for (const SentPing& ping : pings_since_last_response_) { |
| 1397 | oss << rtc::hex_encode(ping.id) << " "; |
| 1398 | } |
| 1399 | } |
| 1400 | *s = oss.str(); |
| 1401 | } |
| 1402 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 1403 | void Connection::UpdateState(int64_t now) { |
| 1404 | int rtt = ConservativeRTTEstimate(rtt_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1405 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1406 | if (RTC_LOG_CHECK_LEVEL(LS_VERBOSE)) { |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 1407 | std::string pings; |
| 1408 | PrintPingsSinceLastResponse(&pings, 5); |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1409 | RTC_LOG(LS_VERBOSE) << ToString() |
| 1410 | << ": UpdateState()" |
| 1411 | ", ms since last received response=" |
| 1412 | << now - last_ping_response_received_ |
| 1413 | << ", ms since last received data=" |
| 1414 | << now - last_data_received_ << ", rtt=" << rtt |
| 1415 | << ", pings_since_last_response=" << pings; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1416 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1417 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1418 | // Check the writable state. (The order of these checks is important.) |
| 1419 | // |
| 1420 | // Before becoming unwritable, we allow for a fixed number of pings to fail |
| 1421 | // (i.e., receive no response). We also have to give the response time to |
| 1422 | // get back, so we include a conservative estimate of this. |
| 1423 | // |
| 1424 | // Before timing out writability, we give a fixed amount of time. This is to |
| 1425 | // allow for changes in network conditions. |
| 1426 | |
| 1427 | if ((write_state_ == STATE_WRITABLE) && |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 1428 | TooManyFailures(pings_since_last_response_, unwritable_min_checks(), rtt, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1429 | now) && |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 1430 | TooLongWithoutResponse(pings_since_last_response_, unwritable_timeout(), |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1431 | now)) { |
Qingsi Wang | 22e623a | 2018-03-13 10:53:57 -0700 | [diff] [blame] | 1432 | uint32_t max_pings = unwritable_min_checks(); |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1433 | RTC_LOG(LS_INFO) << ToString() << ": Unwritable after " |
| 1434 | << max_pings << " ping failures and " |
| 1435 | << now - pings_since_last_response_[0].sent_time |
| 1436 | << " ms without a response," |
| 1437 | " ms since last received ping=" |
| 1438 | << now - last_ping_received_ |
| 1439 | << " ms since last received data=" |
| 1440 | << now - last_data_received_ << " rtt=" << rtt; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1441 | set_write_state(STATE_WRITE_UNRELIABLE); |
| 1442 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1443 | if ((write_state_ == STATE_WRITE_UNRELIABLE || |
| 1444 | write_state_ == STATE_WRITE_INIT) && |
| 1445 | TooLongWithoutResponse(pings_since_last_response_, |
| 1446 | CONNECTION_WRITE_TIMEOUT, |
| 1447 | now)) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1448 | RTC_LOG(LS_INFO) << ToString() << ": Timed out after " |
| 1449 | << now - pings_since_last_response_[0].sent_time |
| 1450 | << " ms without a response, rtt=" << rtt; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1451 | set_write_state(STATE_WRITE_TIMEOUT); |
| 1452 | } |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1453 | |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 1454 | // Update the receiving state. |
| 1455 | UpdateReceiving(now); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 1456 | if (dead(now)) { |
| 1457 | Destroy(); |
| 1458 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 1461 | void Connection::Ping(int64_t now) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1462 | last_ping_sent_ = now; |
Bjorn Terelius | 59b4e3e | 2018-05-30 17:14:08 +0200 | [diff] [blame] | 1463 | ConnectionRequest* req = new ConnectionRequest(this); |
deadbeef | 86c40a1 | 2017-06-28 09:37:23 -0700 | [diff] [blame] | 1464 | // If not using renomination, we use "1" to mean "nominated" and "0" to mean |
| 1465 | // "not nominated". If using renomination, values greater than 1 are used for |
| 1466 | // re-nominated pairs. |
| 1467 | int nomination = use_candidate_attr_ ? 1 : 0; |
| 1468 | if (nomination_ > 0) { |
| 1469 | nomination = nomination_; |
| 1470 | } |
| 1471 | pings_since_last_response_.push_back(SentPing(req->id(), now, nomination)); |
zstein | abbacbf | 2017-03-20 10:53:12 -0700 | [diff] [blame] | 1472 | packet_loss_estimator_.ExpectResponse(req->id(), now); |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1473 | RTC_LOG(LS_VERBOSE) << ToString() |
| 1474 | << ": Sending STUN ping, id=" |
| 1475 | << rtc::hex_encode(req->id()) |
| 1476 | << ", nomination=" << nomination_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1477 | requests_.Send(req); |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 1478 | state_ = IceCandidatePairState::IN_PROGRESS; |
honghaiz | 524ecc2 | 2016-05-25 12:48:31 -0700 | [diff] [blame] | 1479 | num_pings_sent_++; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | void Connection::ReceivedPing() { |
nisse | 1bffc1d | 2016-05-02 08:18:55 -0700 | [diff] [blame] | 1483 | last_ping_received_ = rtc::TimeMillis(); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 1484 | UpdateReceiving(last_ping_received_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 1487 | void Connection::ReceivedPingResponse(int rtt, const std::string& request_id) { |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1488 | RTC_DCHECK_GE(rtt, 0); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 1489 | // We've already validated that this is a STUN binding response with |
| 1490 | // the correct local and remote username for this connection. |
| 1491 | // So if we're not already, become writable. We may be bringing a pruned |
| 1492 | // connection back to life, but if we don't really want it, we can always |
| 1493 | // prune it again. |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 1494 | auto iter = std::find_if( |
| 1495 | pings_since_last_response_.begin(), pings_since_last_response_.end(), |
| 1496 | [request_id](const SentPing& ping) { return ping.id == request_id; }); |
| 1497 | if (iter != pings_since_last_response_.end() && |
| 1498 | iter->nomination > acked_nomination_) { |
| 1499 | acked_nomination_ = iter->nomination; |
| 1500 | } |
| 1501 | |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1502 | total_round_trip_time_ms_ += rtt; |
Oskar Sundbom | 903dcd7 | 2017-11-16 10:55:57 +0100 | [diff] [blame] | 1503 | current_round_trip_time_ms_ = static_cast<uint32_t>(rtt); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1504 | |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 1505 | pings_since_last_response_.clear(); |
honghaiz | 9ad0db5 | 2016-07-14 19:30:28 -0700 | [diff] [blame] | 1506 | last_ping_response_received_ = rtc::TimeMillis(); |
| 1507 | UpdateReceiving(last_ping_response_received_); |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 1508 | set_write_state(STATE_WRITABLE); |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 1509 | set_state(IceCandidatePairState::SUCCEEDED); |
skvlad | d030912 | 2017-02-02 17:18:37 -0800 | [diff] [blame] | 1510 | if (rtt_samples_ > 0) { |
Qingsi Wang | 72a43a1 | 2018-02-20 16:03:18 -0800 | [diff] [blame] | 1511 | rtt_ = rtc::GetNextMovingAverage(rtt_, rtt, RTT_RATIO); |
skvlad | d030912 | 2017-02-02 17:18:37 -0800 | [diff] [blame] | 1512 | } else { |
| 1513 | rtt_ = rtt; |
| 1514 | } |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 1515 | rtt_samples_++; |
Peter Thatcher | 1fe120a | 2015-06-10 11:33:17 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 1518 | bool Connection::dead(int64_t now) const { |
honghaiz | 37389b4 | 2016-01-04 21:57:33 -0800 | [diff] [blame] | 1519 | if (last_received() > 0) { |
| 1520 | // If it has ever received anything, we keep it alive until it hasn't |
| 1521 | // received anything for DEAD_CONNECTION_RECEIVE_TIMEOUT. This covers the |
| 1522 | // normal case of a successfully used connection that stops working. This |
| 1523 | // also allows a remote peer to continue pinging over a locally inactive |
| 1524 | // (pruned) connection. |
| 1525 | return (now > (last_received() + DEAD_CONNECTION_RECEIVE_TIMEOUT)); |
| 1526 | } |
| 1527 | |
| 1528 | if (active()) { |
| 1529 | // If it has never received anything, keep it alive as long as it is |
| 1530 | // actively pinging and not pruned. Otherwise, the connection might be |
| 1531 | // deleted before it has a chance to ping. This is the normal case for a |
| 1532 | // new connection that is pinging but hasn't received anything yet. |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 1533 | return false; |
| 1534 | } |
| 1535 | |
honghaiz | 37389b4 | 2016-01-04 21:57:33 -0800 | [diff] [blame] | 1536 | // If it has never received anything and is not actively pinging (pruned), we |
| 1537 | // keep it around for at least MIN_CONNECTION_LIFETIME to prevent connections |
| 1538 | // from being pruned too quickly during a network change event when two |
| 1539 | // networks would be up simultaneously but only for a brief period. |
| 1540 | return now > (time_created_ms_ + MIN_CONNECTION_LIFETIME); |
Honghai Zhang | 2b342bf | 2015-09-30 09:51:58 -0700 | [diff] [blame] | 1541 | } |
| 1542 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1543 | bool Connection::stable(int64_t now) const { |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 1544 | // A connection is stable if it's RTT has converged and it isn't missing any |
| 1545 | // responses. We should send pings at a higher rate until the RTT converges |
| 1546 | // and whenever a ping response is missing (so that we can detect |
| 1547 | // unwritability faster) |
| 1548 | return rtt_converged() && !missing_responses(now); |
| 1549 | } |
| 1550 | |
guoweis@webrtc.org | 8c9ff20 | 2014-12-04 07:56:02 +0000 | [diff] [blame] | 1551 | std::string Connection::ToDebugId() const { |
| 1552 | std::stringstream ss; |
| 1553 | ss << std::hex << this; |
| 1554 | return ss.str(); |
| 1555 | } |
| 1556 | |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 1557 | uint32_t Connection::ComputeNetworkCost() const { |
| 1558 | // TODO(honghaiz): Will add rtt as part of the network cost. |
Honghai Zhang | 351d77b | 2016-05-20 15:08:29 -0700 | [diff] [blame] | 1559 | return port()->network_cost() + remote_candidate_.network_cost(); |
honghaiz | e1a0c94 | 2016-02-16 14:54:56 -0800 | [diff] [blame] | 1560 | } |
| 1561 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1562 | std::string Connection::ToString() const { |
| 1563 | const char CONNECT_STATE_ABBREV[2] = { |
| 1564 | '-', // not connected (false) |
| 1565 | 'C', // connected (true) |
| 1566 | }; |
Peter Thatcher | 04ac81f | 2015-09-21 11:48:28 -0700 | [diff] [blame] | 1567 | const char RECEIVE_STATE_ABBREV[2] = { |
| 1568 | '-', // not receiving (false) |
| 1569 | 'R', // receiving (true) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1570 | }; |
| 1571 | const char WRITE_STATE_ABBREV[4] = { |
| 1572 | 'W', // STATE_WRITABLE |
| 1573 | 'w', // STATE_WRITE_UNRELIABLE |
| 1574 | '-', // STATE_WRITE_INIT |
| 1575 | 'x', // STATE_WRITE_TIMEOUT |
| 1576 | }; |
| 1577 | const std::string ICESTATE[4] = { |
| 1578 | "W", // STATE_WAITING |
| 1579 | "I", // STATE_INPROGRESS |
| 1580 | "S", // STATE_SUCCEEDED |
| 1581 | "F" // STATE_FAILED |
| 1582 | }; |
Qingsi Wang | 10a0e51 | 2018-05-16 13:37:03 -0700 | [diff] [blame] | 1583 | const std::string SELECTED_STATE_ABBREV[2] = { |
| 1584 | "-", // candidate pair not selected (false) |
| 1585 | "S", // selected (true) |
| 1586 | }; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1587 | const Candidate& local = local_candidate(); |
| 1588 | const Candidate& remote = remote_candidate(); |
| 1589 | std::stringstream ss; |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 1590 | ss << "Conn[" << ToDebugId() << ":" << port_->content_name() << ":" |
Qingsi Wang | 10a0e51 | 2018-05-16 13:37:03 -0700 | [diff] [blame] | 1591 | << port_->Network()->ToString() << ":" << local.id() << ":" |
| 1592 | << local.component() << ":" << local.generation() << ":" << local.type() |
| 1593 | << ":" << local.protocol() << ":" << local.address().ToSensitiveString() |
| 1594 | << "->" << remote.id() << ":" << remote.component() << ":" |
| 1595 | << remote.priority() << ":" << remote.type() << ":" << remote.protocol() |
| 1596 | << ":" << remote.address().ToSensitiveString() << "|" |
| 1597 | << CONNECT_STATE_ABBREV[connected()] << RECEIVE_STATE_ABBREV[receiving()] |
| 1598 | << WRITE_STATE_ABBREV[write_state()] << ICESTATE[static_cast<int>(state())] |
| 1599 | << "|" << SELECTED_STATE_ABBREV[selected()] << "|" << remote_nomination() |
| 1600 | << "|" << nomination() << "|" << priority() << "|"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1601 | if (rtt_ < DEFAULT_RTT) { |
| 1602 | ss << rtt_ << "]"; |
| 1603 | } else { |
| 1604 | ss << "-]"; |
| 1605 | } |
| 1606 | return ss.str(); |
| 1607 | } |
| 1608 | |
| 1609 | std::string Connection::ToSensitiveString() const { |
| 1610 | return ToString(); |
| 1611 | } |
| 1612 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 1613 | const webrtc::IceCandidatePairDescription& Connection::ToLogDescription() { |
| 1614 | if (log_description_.has_value()) { |
| 1615 | return log_description_.value(); |
| 1616 | } |
| 1617 | const Candidate& local = local_candidate(); |
| 1618 | const Candidate& remote = remote_candidate(); |
| 1619 | const rtc::Network* network = port()->Network(); |
| 1620 | log_description_ = webrtc::IceCandidatePairDescription(); |
| 1621 | log_description_->local_candidate_type = |
| 1622 | GetCandidateTypeByString(local.type()); |
| 1623 | log_description_->local_relay_protocol = |
| 1624 | GetProtocolByString(local.relay_protocol()); |
| 1625 | log_description_->local_network_type = ConvertNetworkType(network->type()); |
| 1626 | log_description_->local_address_family = |
| 1627 | GetAddressFamilyByInt(local.address().family()); |
| 1628 | log_description_->remote_candidate_type = |
| 1629 | GetCandidateTypeByString(remote.type()); |
| 1630 | log_description_->remote_address_family = |
| 1631 | GetAddressFamilyByInt(remote.address().family()); |
| 1632 | log_description_->candidate_pair_protocol = |
| 1633 | GetProtocolByString(local.protocol()); |
| 1634 | return log_description_.value(); |
| 1635 | } |
| 1636 | |
Bjorn Terelius | 59b4e3e | 2018-05-30 17:14:08 +0200 | [diff] [blame] | 1637 | void Connection::LogCandidatePairConfig( |
| 1638 | webrtc::IceCandidatePairConfigType type) { |
| 1639 | if (ice_event_log_ == nullptr) { |
| 1640 | return; |
| 1641 | } |
| 1642 | ice_event_log_->LogCandidatePairConfig(type, hash(), ToLogDescription()); |
| 1643 | } |
| 1644 | |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 1645 | void Connection::LogCandidatePairEvent(webrtc::IceCandidatePairEventType type) { |
| 1646 | if (ice_event_log_ == nullptr) { |
| 1647 | return; |
| 1648 | } |
Bjorn Terelius | 59b4e3e | 2018-05-30 17:14:08 +0200 | [diff] [blame] | 1649 | ice_event_log_->LogCandidatePairEvent(type, hash()); |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 1650 | } |
| 1651 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1652 | void Connection::OnConnectionRequestResponse(ConnectionRequest* request, |
| 1653 | StunMessage* response) { |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 1654 | // Log at LS_INFO if we receive a ping response on an unwritable |
| 1655 | // connection. |
| 1656 | rtc::LoggingSeverity sev = !writable() ? rtc::LS_INFO : rtc::LS_VERBOSE; |
| 1657 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 1658 | int rtt = request->Elapsed(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1659 | |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1660 | if (RTC_LOG_CHECK_LEVEL_V(sev)) { |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 1661 | std::string pings; |
| 1662 | PrintPingsSinceLastResponse(&pings, 5); |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1663 | RTC_LOG_V(sev) << ToString() |
| 1664 | << ": Received STUN ping response, id=" |
| 1665 | << rtc::hex_encode(request->id()) |
| 1666 | << ", code=0" // Makes logging easier to parse. |
| 1667 | ", rtt=" |
| 1668 | << rtt << ", pings_since_last_response=" << pings; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1669 | } |
Honghai Zhang | 8cd8f81 | 2016-08-03 19:50:41 -0700 | [diff] [blame] | 1670 | ReceivedPingResponse(rtt, request->id()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1671 | |
zstein | abbacbf | 2017-03-20 10:53:12 -0700 | [diff] [blame] | 1672 | int64_t time_received = rtc::TimeMillis(); |
| 1673 | packet_loss_estimator_.ReceivedResponse(request->id(), time_received); |
| 1674 | |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1675 | stats_.recv_ping_responses++; |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 1676 | LogCandidatePairEvent( |
| 1677 | webrtc::IceCandidatePairEventType::kCheckResponseReceived); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1678 | |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 1679 | MaybeUpdateLocalCandidate(request, response); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | void Connection::OnConnectionRequestErrorResponse(ConnectionRequest* request, |
| 1683 | StunMessage* response) { |
deadbeef | 996fc6b | 2017-04-26 09:21:22 -0700 | [diff] [blame] | 1684 | int error_code = response->GetErrorCodeValue(); |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1685 | RTC_LOG(LS_WARNING) << ToString() |
| 1686 | << ": Received STUN error response id=" |
| 1687 | << rtc::hex_encode(request->id()) |
| 1688 | << " code=" << error_code |
| 1689 | << " rtt=" << request->Elapsed(); |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 1690 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1691 | if (error_code == STUN_ERROR_UNKNOWN_ATTRIBUTE || |
| 1692 | error_code == STUN_ERROR_SERVER_ERROR || |
| 1693 | error_code == STUN_ERROR_UNAUTHORIZED) { |
| 1694 | // Recoverable error, retry |
| 1695 | } else if (error_code == STUN_ERROR_STALE_CREDENTIALS) { |
| 1696 | // Race failure, retry |
| 1697 | } else if (error_code == STUN_ERROR_ROLE_CONFLICT) { |
| 1698 | HandleRoleConflictFromPeer(); |
| 1699 | } else { |
| 1700 | // This is not a valid connection. |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1701 | RTC_LOG(LS_ERROR) << ToString() |
| 1702 | << ": Received STUN error response, code=" << error_code |
| 1703 | << "; killing connection"; |
deadbeef | 376e123 | 2015-11-25 09:00:08 -0800 | [diff] [blame] | 1704 | FailAndDestroy(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | void Connection::OnConnectionRequestTimeout(ConnectionRequest* request) { |
| 1709 | // Log at LS_INFO if we miss a ping on a writable connection. |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 1710 | rtc::LoggingSeverity sev = writable() ? rtc::LS_INFO : rtc::LS_VERBOSE; |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1711 | RTC_LOG_V(sev) << ToString() << ": Timing-out STUN ping " |
| 1712 | << rtc::hex_encode(request->id()) << " after " |
| 1713 | << request->Elapsed() << " ms"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 1716 | void Connection::OnConnectionRequestSent(ConnectionRequest* request) { |
| 1717 | // Log at LS_INFO if we send a ping on an unwritable connection. |
| 1718 | rtc::LoggingSeverity sev = !writable() ? rtc::LS_INFO : rtc::LS_VERBOSE; |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1719 | RTC_LOG_V(sev) << ToString() |
| 1720 | << ": Sent STUN ping, id=" << rtc::hex_encode(request->id()) |
| 1721 | << ", use_candidate=" << use_candidate_attr() |
| 1722 | << ", nomination=" << nomination(); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1723 | stats_.sent_ping_requests_total++; |
Qingsi Wang | 93a8439 | 2018-01-30 17:13:09 -0800 | [diff] [blame] | 1724 | LogCandidatePairEvent(webrtc::IceCandidatePairEventType::kCheckSent); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1725 | if (stats_.recv_ping_responses == 0) { |
| 1726 | stats_.sent_ping_requests_before_first_response++; |
| 1727 | } |
Peter Thatcher | 1cf6f81 | 2015-05-15 10:40:45 -0700 | [diff] [blame] | 1728 | } |
| 1729 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1730 | void Connection::HandleRoleConflictFromPeer() { |
| 1731 | port_->SignalRoleConflict(port_); |
| 1732 | } |
| 1733 | |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1734 | void Connection::MaybeSetRemoteIceParametersAndGeneration( |
| 1735 | const IceParameters& ice_params, |
Taylor Brandstetter | 0a1bc53 | 2016-04-19 18:03:26 -0700 | [diff] [blame] | 1736 | int generation) { |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1737 | if (remote_candidate_.username() == ice_params.ufrag && |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1738 | remote_candidate_.password().empty()) { |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1739 | remote_candidate_.set_password(ice_params.pwd); |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1740 | } |
Taylor Brandstetter | 0a1bc53 | 2016-04-19 18:03:26 -0700 | [diff] [blame] | 1741 | // TODO(deadbeef): A value of '0' for the generation is used for both |
| 1742 | // generation 0 and "generation unknown". It should be changed to an |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame^] | 1743 | // absl::optional to fix this. |
Honghai Zhang | 4cedf2b | 2016-08-31 08:18:11 -0700 | [diff] [blame] | 1744 | if (remote_candidate_.username() == ice_params.ufrag && |
| 1745 | remote_candidate_.password() == ice_params.pwd && |
Taylor Brandstetter | 0a1bc53 | 2016-04-19 18:03:26 -0700 | [diff] [blame] | 1746 | remote_candidate_.generation() == 0) { |
| 1747 | remote_candidate_.set_generation(generation); |
| 1748 | } |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | void Connection::MaybeUpdatePeerReflexiveCandidate( |
| 1752 | const Candidate& new_candidate) { |
| 1753 | if (remote_candidate_.type() == PRFLX_PORT_TYPE && |
| 1754 | new_candidate.type() != PRFLX_PORT_TYPE && |
| 1755 | remote_candidate_.protocol() == new_candidate.protocol() && |
| 1756 | remote_candidate_.address() == new_candidate.address() && |
| 1757 | remote_candidate_.username() == new_candidate.username() && |
| 1758 | remote_candidate_.password() == new_candidate.password() && |
| 1759 | remote_candidate_.generation() == new_candidate.generation()) { |
| 1760 | remote_candidate_ = new_candidate; |
| 1761 | } |
| 1762 | } |
| 1763 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1764 | void Connection::OnMessage(rtc::Message *pmsg) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1765 | RTC_DCHECK(pmsg->message_id == MSG_DELETE); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1766 | RTC_LOG(LS_INFO) << "Connection deleted with number of pings sent: " |
| 1767 | << num_pings_sent_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1768 | SignalDestroyed(this); |
| 1769 | delete this; |
| 1770 | } |
| 1771 | |
honghaiz | 34b11eb | 2016-03-16 08:55:44 -0700 | [diff] [blame] | 1772 | int64_t Connection::last_received() const { |
Peter Thatcher | 5436051 | 2015-07-08 11:08:35 -0700 | [diff] [blame] | 1773 | return std::max(last_data_received_, |
| 1774 | std::max(last_ping_received_, last_ping_response_received_)); |
| 1775 | } |
| 1776 | |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1777 | ConnectionInfo Connection::stats() { |
| 1778 | stats_.recv_bytes_second = round(recv_rate_tracker_.ComputeRate()); |
| 1779 | stats_.recv_total_bytes = recv_rate_tracker_.TotalSampleCount(); |
| 1780 | stats_.sent_bytes_second = round(send_rate_tracker_.ComputeRate()); |
| 1781 | stats_.sent_total_bytes = send_rate_tracker_.TotalSampleCount(); |
hbos | 06495bc | 2017-01-02 08:08:18 -0800 | [diff] [blame] | 1782 | stats_.receiving = receiving_; |
| 1783 | stats_.writable = write_state_ == STATE_WRITABLE; |
| 1784 | stats_.timeout = write_state_ == STATE_WRITE_TIMEOUT; |
| 1785 | stats_.new_connection = !reported_; |
| 1786 | stats_.rtt = rtt_; |
| 1787 | stats_.local_candidate = local_candidate(); |
| 1788 | stats_.remote_candidate = remote_candidate(); |
| 1789 | stats_.key = this; |
| 1790 | stats_.state = state_; |
| 1791 | stats_.priority = priority(); |
hbos | 92eaec6 | 2017-02-27 01:38:08 -0800 | [diff] [blame] | 1792 | stats_.nominated = nominated(); |
hbos | bf8d3e5 | 2017-02-28 06:34:47 -0800 | [diff] [blame] | 1793 | stats_.total_round_trip_time_ms = total_round_trip_time_ms_; |
| 1794 | stats_.current_round_trip_time_ms = current_round_trip_time_ms_; |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1795 | return stats_; |
guoweis@webrtc.org | 930e004 | 2014-11-17 19:42:14 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 1798 | void Connection::MaybeUpdateLocalCandidate(ConnectionRequest* request, |
| 1799 | StunMessage* response) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1800 | // RFC 5245 |
| 1801 | // The agent checks the mapped address from the STUN response. If the |
| 1802 | // transport address does not match any of the local candidates that the |
| 1803 | // agent knows about, the mapped address represents a new candidate -- a |
| 1804 | // peer reflexive candidate. |
| 1805 | const StunAddressAttribute* addr = |
| 1806 | response->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); |
| 1807 | if (!addr) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1808 | RTC_LOG(LS_WARNING) |
| 1809 | << "Connection::OnConnectionRequestResponse - " |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1810 | "No MAPPED-ADDRESS or XOR-MAPPED-ADDRESS found in the " |
| 1811 | "stun response message"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1812 | return; |
| 1813 | } |
| 1814 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1815 | for (size_t i = 0; i < port_->Candidates().size(); ++i) { |
| 1816 | if (port_->Candidates()[i].address() == addr->GetAddress()) { |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 1817 | if (local_candidate_index_ != i) { |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1818 | RTC_LOG(LS_INFO) << ToString() |
| 1819 | << ": Updating local candidate type to srflx."; |
Taylor Brandstetter | 62351c9 | 2016-08-11 16:05:07 -0700 | [diff] [blame] | 1820 | local_candidate_index_ = i; |
| 1821 | // SignalStateChange to force a re-sort in P2PTransportChannel as this |
| 1822 | // Connection's local candidate has changed. |
| 1823 | SignalStateChange(this); |
| 1824 | } |
| 1825 | return; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1826 | } |
| 1827 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1828 | |
| 1829 | // RFC 5245 |
| 1830 | // Its priority is set equal to the value of the PRIORITY attribute |
| 1831 | // in the Binding request. |
| 1832 | const StunUInt32Attribute* priority_attr = |
| 1833 | request->msg()->GetUInt32(STUN_ATTR_PRIORITY); |
| 1834 | if (!priority_attr) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 1835 | RTC_LOG(LS_WARNING) << "Connection::OnConnectionRequestResponse - " |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1836 | "No STUN_ATTR_PRIORITY found in the " |
| 1837 | "stun response message"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1838 | return; |
| 1839 | } |
Peter Boström | 0c4e06b | 2015-10-07 12:23:21 +0200 | [diff] [blame] | 1840 | const uint32_t priority = priority_attr->value(); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1841 | std::string id = rtc::CreateRandomString(8); |
| 1842 | |
| 1843 | Candidate new_local_candidate; |
| 1844 | new_local_candidate.set_id(id); |
| 1845 | new_local_candidate.set_component(local_candidate().component()); |
| 1846 | new_local_candidate.set_type(PRFLX_PORT_TYPE); |
| 1847 | new_local_candidate.set_protocol(local_candidate().protocol()); |
| 1848 | new_local_candidate.set_address(addr->GetAddress()); |
| 1849 | new_local_candidate.set_priority(priority); |
| 1850 | new_local_candidate.set_username(local_candidate().username()); |
| 1851 | new_local_candidate.set_password(local_candidate().password()); |
| 1852 | new_local_candidate.set_network_name(local_candidate().network_name()); |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 1853 | new_local_candidate.set_network_type(local_candidate().network_type()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1854 | new_local_candidate.set_related_address(local_candidate().address()); |
Taylor Brandstetter | f7c15a9 | 2016-06-22 13:13:55 -0700 | [diff] [blame] | 1855 | new_local_candidate.set_generation(local_candidate().generation()); |
Honghai Zhang | 80f1db9 | 2016-01-27 11:54:45 -0800 | [diff] [blame] | 1856 | new_local_candidate.set_foundation(ComputeFoundation( |
| 1857 | PRFLX_PORT_TYPE, local_candidate().protocol(), |
| 1858 | local_candidate().relay_protocol(), local_candidate().address())); |
honghaiz | a0c44ea | 2016-03-23 16:07:48 -0700 | [diff] [blame] | 1859 | new_local_candidate.set_network_id(local_candidate().network_id()); |
| 1860 | new_local_candidate.set_network_cost(local_candidate().network_cost()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1861 | |
| 1862 | // Change the local candidate of this Connection to the new prflx candidate. |
Jonas Olsson | d7d762d | 2018-03-28 09:47:51 +0200 | [diff] [blame] | 1863 | RTC_LOG(LS_INFO) << ToString() |
| 1864 | << ": Updating local candidate type to prflx."; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1865 | local_candidate_index_ = port_->AddPrflxCandidate(new_local_candidate); |
| 1866 | |
| 1867 | // SignalStateChange to force a re-sort in P2PTransportChannel as this |
| 1868 | // Connection's local candidate has changed. |
| 1869 | SignalStateChange(this); |
| 1870 | } |
| 1871 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1872 | bool Connection::rtt_converged() const { |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 1873 | return rtt_samples_ > (RTT_RATIO + 1); |
| 1874 | } |
| 1875 | |
Taylor Brandstetter | b825aee | 2016-06-29 13:07:16 -0700 | [diff] [blame] | 1876 | bool Connection::missing_responses(int64_t now) const { |
zhihuang | 435264a | 2016-06-21 11:28:38 -0700 | [diff] [blame] | 1877 | if (pings_since_last_response_.empty()) { |
| 1878 | return false; |
| 1879 | } |
| 1880 | |
| 1881 | int64_t waiting = now - pings_since_last_response_[0].sent_time; |
| 1882 | return waiting > 2 * rtt(); |
| 1883 | } |
| 1884 | |
deadbeef | 376e123 | 2015-11-25 09:00:08 -0800 | [diff] [blame] | 1885 | ProxyConnection::ProxyConnection(Port* port, |
| 1886 | size_t index, |
| 1887 | const Candidate& remote_candidate) |
| 1888 | : Connection(port, index, remote_candidate) {} |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1889 | |
| 1890 | int ProxyConnection::Send(const void* data, size_t size, |
| 1891 | const rtc::PacketOptions& options) { |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1892 | stats_.sent_total_packets++; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1893 | int sent = port_->SendTo(data, size, remote_candidate_.address(), |
| 1894 | options, true); |
| 1895 | if (sent <= 0) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 1896 | RTC_DCHECK(sent < 0); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1897 | error_ = port_->GetError(); |
zhihuang | 5ecf16c | 2016-06-01 17:09:15 -0700 | [diff] [blame] | 1898 | stats_.sent_discarded_packets++; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1899 | } else { |
Tim Psiaki | 6304626 | 2015-09-14 10:38:08 -0700 | [diff] [blame] | 1900 | send_rate_tracker_.AddSamples(sent); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1901 | } |
| 1902 | return sent; |
| 1903 | } |
| 1904 | |
Steve Anton | 1cf1b7d | 2017-10-30 10:00:15 -0700 | [diff] [blame] | 1905 | int ProxyConnection::GetError() { |
| 1906 | return error_; |
| 1907 | } |
| 1908 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1909 | } // namespace cricket |