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