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