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