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