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