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" |
| 23 | #include "webrtc/base/scoped_ptr.h" |
| 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( |
| 32 | const std::vector<uint32>& pings_since_last_response, |
| 33 | uint32 maximum_failures, |
| 34 | uint32 rtt_estimate, |
| 35 | uint32 now) { |
| 36 | |
| 37 | // If we haven't sent that many pings, then we can't have failed that many. |
| 38 | if (pings_since_last_response.size() < maximum_failures) |
| 39 | return false; |
| 40 | |
| 41 | // Check if the window in which we would expect a response to the ping has |
| 42 | // already elapsed. |
| 43 | return pings_since_last_response[maximum_failures - 1] + rtt_estimate < now; |
| 44 | } |
| 45 | |
| 46 | // Determines whether we have gone too long without seeing any response. |
| 47 | inline bool TooLongWithoutResponse( |
| 48 | const std::vector<uint32>& pings_since_last_response, |
| 49 | uint32 maximum_time, |
| 50 | uint32 now) { |
| 51 | |
| 52 | if (pings_since_last_response.size() == 0) |
| 53 | return false; |
| 54 | |
| 55 | return pings_since_last_response[0] + maximum_time < now; |
| 56 | } |
| 57 | |
| 58 | // GICE(ICEPROTO_GOOGLE) requires different username for RTP and RTCP. |
| 59 | // This function generates a different username by +1 on the last character of |
| 60 | // the given username (|rtp_ufrag|). |
| 61 | std::string GetRtcpUfragFromRtpUfrag(const std::string& rtp_ufrag) { |
| 62 | ASSERT(!rtp_ufrag.empty()); |
| 63 | if (rtp_ufrag.empty()) { |
| 64 | return rtp_ufrag; |
| 65 | } |
| 66 | // Change the last character to the one next to it in the base64 table. |
| 67 | char new_last_char; |
| 68 | if (!rtc::Base64::GetNextBase64Char(rtp_ufrag[rtp_ufrag.size() - 1], |
| 69 | &new_last_char)) { |
| 70 | // Should not be here. |
| 71 | ASSERT(false); |
| 72 | } |
| 73 | std::string rtcp_ufrag = rtp_ufrag; |
| 74 | rtcp_ufrag[rtcp_ufrag.size() - 1] = new_last_char; |
| 75 | ASSERT(rtcp_ufrag != rtp_ufrag); |
| 76 | return rtcp_ufrag; |
| 77 | } |
| 78 | |
| 79 | // We will restrict RTT estimates (when used for determining state) to be |
| 80 | // within a reasonable range. |
| 81 | const uint32 MINIMUM_RTT = 100; // 0.1 seconds |
| 82 | const uint32 MAXIMUM_RTT = 3000; // 3 seconds |
| 83 | |
| 84 | // When we don't have any RTT data, we have to pick something reasonable. We |
| 85 | // use a large value just in case the connection is really slow. |
| 86 | const uint32 DEFAULT_RTT = MAXIMUM_RTT; |
| 87 | |
| 88 | // Computes our estimate of the RTT given the current estimate. |
| 89 | inline uint32 ConservativeRTTEstimate(uint32 rtt) { |
| 90 | return rtc::_max(MINIMUM_RTT, rtc::_min(MAXIMUM_RTT, 2 * rtt)); |
| 91 | } |
| 92 | |
| 93 | // Weighting of the old rtt value to new data. |
| 94 | const int RTT_RATIO = 3; // 3 : 1 |
| 95 | |
| 96 | // The delay before we begin checking if this port is useless. |
| 97 | const int kPortTimeoutDelay = 30 * 1000; // 30 seconds |
| 98 | |
| 99 | // Used by the Connection. |
| 100 | const uint32 MSG_DELETE = 1; |
| 101 | } |
| 102 | |
| 103 | namespace cricket { |
| 104 | |
| 105 | // TODO(ronghuawu): Use "host", "srflx", "prflx" and "relay". But this requires |
| 106 | // the signaling part be updated correspondingly as well. |
| 107 | const char LOCAL_PORT_TYPE[] = "local"; |
| 108 | const char STUN_PORT_TYPE[] = "stun"; |
| 109 | const char PRFLX_PORT_TYPE[] = "prflx"; |
| 110 | const char RELAY_PORT_TYPE[] = "relay"; |
| 111 | |
| 112 | const char UDP_PROTOCOL_NAME[] = "udp"; |
| 113 | const char TCP_PROTOCOL_NAME[] = "tcp"; |
| 114 | const char SSLTCP_PROTOCOL_NAME[] = "ssltcp"; |
| 115 | |
| 116 | static const char* const PROTO_NAMES[] = { UDP_PROTOCOL_NAME, |
| 117 | TCP_PROTOCOL_NAME, |
| 118 | SSLTCP_PROTOCOL_NAME }; |
| 119 | |
| 120 | const char* ProtoToString(ProtocolType proto) { |
| 121 | return PROTO_NAMES[proto]; |
| 122 | } |
| 123 | |
| 124 | bool StringToProto(const char* value, ProtocolType* proto) { |
| 125 | for (size_t i = 0; i <= PROTO_LAST; ++i) { |
| 126 | if (_stricmp(PROTO_NAMES[i], value) == 0) { |
| 127 | *proto = static_cast<ProtocolType>(i); |
| 128 | return true; |
| 129 | } |
| 130 | } |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | // RFC 6544, TCP candidate encoding rules. |
| 135 | const int DISCARD_PORT = 9; |
| 136 | const char TCPTYPE_ACTIVE_STR[] = "active"; |
| 137 | const char TCPTYPE_PASSIVE_STR[] = "passive"; |
| 138 | const char TCPTYPE_SIMOPEN_STR[] = "so"; |
| 139 | |
| 140 | // Foundation: An arbitrary string that is the same for two candidates |
| 141 | // that have the same type, base IP address, protocol (UDP, TCP, |
| 142 | // etc.), and STUN or TURN server. If any of these are different, |
| 143 | // then the foundation will be different. Two candidate pairs with |
| 144 | // the same foundation pairs are likely to have similar network |
| 145 | // characteristics. Foundations are used in the frozen algorithm. |
| 146 | static std::string ComputeFoundation( |
| 147 | const std::string& type, |
| 148 | const std::string& protocol, |
| 149 | const rtc::SocketAddress& base_address) { |
| 150 | std::ostringstream ost; |
| 151 | ost << type << base_address.ipaddr().ToString() << protocol; |
| 152 | return rtc::ToString<uint32>(rtc::ComputeCrc32(ost.str())); |
| 153 | } |
| 154 | |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 155 | Port::Port(rtc::Thread* thread, |
| 156 | rtc::PacketSocketFactory* factory, |
| 157 | rtc::Network* network, |
| 158 | const rtc::IPAddress& ip, |
| 159 | const std::string& username_fragment, |
| 160 | const std::string& password) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 161 | : thread_(thread), |
| 162 | factory_(factory), |
| 163 | send_retransmit_count_attribute_(false), |
| 164 | network_(network), |
| 165 | ip_(ip), |
| 166 | min_port_(0), |
| 167 | max_port_(0), |
| 168 | component_(ICE_CANDIDATE_COMPONENT_DEFAULT), |
| 169 | generation_(0), |
| 170 | ice_username_fragment_(username_fragment), |
| 171 | password_(password), |
| 172 | timeout_delay_(kPortTimeoutDelay), |
| 173 | enable_port_packets_(false), |
| 174 | ice_protocol_(ICEPROTO_HYBRID), |
| 175 | ice_role_(ICEROLE_UNKNOWN), |
| 176 | tiebreaker_(0), |
| 177 | shared_socket_(true), |
| 178 | candidate_filter_(CF_ALL) { |
| 179 | Construct(); |
| 180 | } |
| 181 | |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 182 | Port::Port(rtc::Thread* thread, |
| 183 | const std::string& type, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 184 | rtc::PacketSocketFactory* factory, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 185 | rtc::Network* network, |
| 186 | const rtc::IPAddress& ip, |
| 187 | uint16 min_port, |
| 188 | uint16 max_port, |
| 189 | const std::string& username_fragment, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 190 | const std::string& password) |
| 191 | : thread_(thread), |
| 192 | factory_(factory), |
| 193 | type_(type), |
| 194 | send_retransmit_count_attribute_(false), |
| 195 | network_(network), |
| 196 | ip_(ip), |
| 197 | min_port_(min_port), |
| 198 | max_port_(max_port), |
| 199 | component_(ICE_CANDIDATE_COMPONENT_DEFAULT), |
| 200 | generation_(0), |
| 201 | ice_username_fragment_(username_fragment), |
| 202 | password_(password), |
| 203 | timeout_delay_(kPortTimeoutDelay), |
| 204 | enable_port_packets_(false), |
| 205 | ice_protocol_(ICEPROTO_HYBRID), |
| 206 | ice_role_(ICEROLE_UNKNOWN), |
| 207 | tiebreaker_(0), |
| 208 | shared_socket_(false), |
| 209 | candidate_filter_(CF_ALL) { |
| 210 | ASSERT(factory_ != NULL); |
| 211 | Construct(); |
| 212 | } |
| 213 | |
| 214 | void Port::Construct() { |
| 215 | // If the username_fragment and password are empty, we should just create one. |
| 216 | if (ice_username_fragment_.empty()) { |
| 217 | ASSERT(password_.empty()); |
| 218 | ice_username_fragment_ = rtc::CreateRandomString(ICE_UFRAG_LENGTH); |
| 219 | password_ = rtc::CreateRandomString(ICE_PWD_LENGTH); |
| 220 | } |
| 221 | LOG_J(LS_INFO, this) << "Port created"; |
| 222 | } |
| 223 | |
| 224 | Port::~Port() { |
| 225 | // Delete all of the remaining connections. We copy the list up front |
| 226 | // because each deletion will cause it to be modified. |
| 227 | |
| 228 | std::vector<Connection*> list; |
| 229 | |
| 230 | AddressMap::iterator iter = connections_.begin(); |
| 231 | while (iter != connections_.end()) { |
| 232 | list.push_back(iter->second); |
| 233 | ++iter; |
| 234 | } |
| 235 | |
| 236 | for (uint32 i = 0; i < list.size(); i++) |
| 237 | delete list[i]; |
| 238 | } |
| 239 | |
| 240 | Connection* Port::GetConnection(const rtc::SocketAddress& remote_addr) { |
| 241 | AddressMap::const_iterator iter = connections_.find(remote_addr); |
| 242 | if (iter != connections_.end()) |
| 243 | return iter->second; |
| 244 | else |
| 245 | return NULL; |
| 246 | } |
| 247 | |
| 248 | void Port::AddAddress(const rtc::SocketAddress& address, |
| 249 | const rtc::SocketAddress& base_address, |
| 250 | const rtc::SocketAddress& related_address, |
| 251 | const std::string& protocol, |
| 252 | const std::string& tcptype, |
| 253 | const std::string& type, |
| 254 | uint32 type_preference, |
| 255 | uint32 relay_preference, |
| 256 | bool final) { |
| 257 | if (protocol == TCP_PROTOCOL_NAME && type == LOCAL_PORT_TYPE) { |
| 258 | ASSERT(!tcptype.empty()); |
| 259 | } |
| 260 | |
| 261 | Candidate c; |
| 262 | c.set_id(rtc::CreateRandomString(8)); |
| 263 | c.set_component(component_); |
| 264 | c.set_type(type); |
| 265 | c.set_protocol(protocol); |
| 266 | c.set_tcptype(tcptype); |
| 267 | c.set_address(address); |
| 268 | c.set_priority(c.GetPriority(type_preference, network_->preference(), |
| 269 | relay_preference)); |
| 270 | c.set_username(username_fragment()); |
| 271 | c.set_password(password_); |
| 272 | c.set_network_name(network_->name()); |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 273 | c.set_network_type(network_->type()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 274 | c.set_generation(generation_); |
| 275 | c.set_related_address(related_address); |
| 276 | c.set_foundation(ComputeFoundation(type, protocol, base_address)); |
| 277 | candidates_.push_back(c); |
| 278 | SignalCandidateReady(this, c); |
| 279 | |
| 280 | if (final) { |
| 281 | SignalPortComplete(this); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | void Port::AddConnection(Connection* conn) { |
| 286 | connections_[conn->remote_candidate().address()] = conn; |
| 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. |
| 302 | rtc::scoped_ptr<IceMessage> msg; |
| 303 | std::string remote_username; |
| 304 | if (!GetStunMessage(data, size, addr, msg.accept(), &remote_username)) { |
| 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) { |
| 310 | // Check for role conflicts. |
| 311 | if (IsStandardIce() && |
| 312 | !MaybeIceRoleConflict(addr, msg.get(), remote_username)) { |
| 313 | LOG(LS_INFO) << "Received conflicting role from the peer."; |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | SignalUnknownAddress(this, addr, proto, msg.get(), remote_username, false); |
| 318 | } else { |
| 319 | // NOTE(tschmelcher): STUN_BINDING_RESPONSE is benign. It occurs if we |
| 320 | // pruned a connection for this port while it had STUN requests in flight, |
| 321 | // because we then get back responses for them, which this code correctly |
| 322 | // does not handle. |
| 323 | if (msg->type() != STUN_BINDING_RESPONSE) { |
| 324 | LOG_J(LS_ERROR, this) << "Received unexpected STUN message type (" |
| 325 | << msg->type() << ") from unknown address (" |
| 326 | << addr.ToSensitiveString() << ")"; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | void Port::OnReadyToSend() { |
| 332 | AddressMap::iterator iter = connections_.begin(); |
| 333 | for (; iter != connections_.end(); ++iter) { |
| 334 | iter->second->OnReadyToSend(); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | size_t Port::AddPrflxCandidate(const Candidate& local) { |
| 339 | candidates_.push_back(local); |
| 340 | return (candidates_.size() - 1); |
| 341 | } |
| 342 | |
| 343 | bool Port::IsStandardIce() const { |
| 344 | return (ice_protocol_ == ICEPROTO_RFC5245); |
| 345 | } |
| 346 | |
| 347 | bool Port::IsGoogleIce() const { |
| 348 | return (ice_protocol_ == ICEPROTO_GOOGLE); |
| 349 | } |
| 350 | |
| 351 | bool Port::IsHybridIce() const { |
| 352 | return (ice_protocol_ == ICEPROTO_HYBRID); |
| 353 | } |
| 354 | |
| 355 | bool Port::GetStunMessage(const char* data, size_t size, |
| 356 | const rtc::SocketAddress& addr, |
| 357 | IceMessage** out_msg, std::string* out_username) { |
| 358 | // NOTE: This could clearly be optimized to avoid allocating any memory. |
| 359 | // However, at the data rates we'll be looking at on the client side, |
| 360 | // this probably isn't worth worrying about. |
| 361 | ASSERT(out_msg != NULL); |
| 362 | ASSERT(out_username != NULL); |
| 363 | *out_msg = NULL; |
| 364 | out_username->clear(); |
| 365 | |
| 366 | // Don't bother parsing the packet if we can tell it's not STUN. |
| 367 | // In ICE mode, all STUN packets will have a valid fingerprint. |
| 368 | if (IsStandardIce() && !StunMessage::ValidateFingerprint(data, size)) { |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | // Parse the request message. If the packet is not a complete and correct |
| 373 | // STUN message, then ignore it. |
| 374 | rtc::scoped_ptr<IceMessage> stun_msg(new IceMessage()); |
| 375 | rtc::ByteBuffer buf(data, size); |
| 376 | if (!stun_msg->Read(&buf) || (buf.Length() > 0)) { |
| 377 | return false; |
| 378 | } |
| 379 | |
| 380 | if (stun_msg->type() == STUN_BINDING_REQUEST) { |
| 381 | // Check for the presence of USERNAME and MESSAGE-INTEGRITY (if ICE) first. |
| 382 | // If not present, fail with a 400 Bad Request. |
| 383 | if (!stun_msg->GetByteString(STUN_ATTR_USERNAME) || |
| 384 | (IsStandardIce() && |
| 385 | !stun_msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY))) { |
| 386 | LOG_J(LS_ERROR, this) << "Received STUN request without username/M-I " |
| 387 | << "from " << addr.ToSensitiveString(); |
| 388 | SendBindingErrorResponse(stun_msg.get(), addr, STUN_ERROR_BAD_REQUEST, |
| 389 | STUN_ERROR_REASON_BAD_REQUEST); |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | // If the username is bad or unknown, fail with a 401 Unauthorized. |
| 394 | std::string local_ufrag; |
| 395 | std::string remote_ufrag; |
| 396 | IceProtocolType remote_protocol_type; |
| 397 | if (!ParseStunUsername(stun_msg.get(), &local_ufrag, &remote_ufrag, |
| 398 | &remote_protocol_type) || |
| 399 | local_ufrag != username_fragment()) { |
| 400 | LOG_J(LS_ERROR, this) << "Received STUN request with bad local username " |
| 401 | << local_ufrag << " from " |
| 402 | << addr.ToSensitiveString(); |
| 403 | SendBindingErrorResponse(stun_msg.get(), addr, STUN_ERROR_UNAUTHORIZED, |
| 404 | STUN_ERROR_REASON_UNAUTHORIZED); |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | // Port is initialized to GOOGLE-ICE protocol type. If pings from remote |
| 409 | // are received before the signal message, protocol type may be different. |
| 410 | // Based on the STUN username, we can determine what's the remote protocol. |
| 411 | // This also enables us to send the response back using the same protocol |
| 412 | // as the request. |
| 413 | if (IsHybridIce()) { |
| 414 | SetIceProtocolType(remote_protocol_type); |
| 415 | } |
| 416 | |
| 417 | // If ICE, and the MESSAGE-INTEGRITY is bad, fail with a 401 Unauthorized |
| 418 | if (IsStandardIce() && |
| 419 | !stun_msg->ValidateMessageIntegrity(data, size, password_)) { |
| 420 | 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] | 421 | << "from " << addr.ToSensitiveString() |
| 422 | << ", password_=" << password_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 423 | SendBindingErrorResponse(stun_msg.get(), addr, STUN_ERROR_UNAUTHORIZED, |
| 424 | STUN_ERROR_REASON_UNAUTHORIZED); |
| 425 | return true; |
| 426 | } |
| 427 | out_username->assign(remote_ufrag); |
| 428 | } else if ((stun_msg->type() == STUN_BINDING_RESPONSE) || |
| 429 | (stun_msg->type() == STUN_BINDING_ERROR_RESPONSE)) { |
| 430 | if (stun_msg->type() == STUN_BINDING_ERROR_RESPONSE) { |
| 431 | if (const StunErrorCodeAttribute* error_code = stun_msg->GetErrorCode()) { |
| 432 | LOG_J(LS_ERROR, this) << "Received STUN binding error:" |
| 433 | << " class=" << error_code->eclass() |
| 434 | << " number=" << error_code->number() |
| 435 | << " reason='" << error_code->reason() << "'" |
| 436 | << " from " << addr.ToSensitiveString(); |
| 437 | // Return message to allow error-specific processing |
| 438 | } else { |
| 439 | LOG_J(LS_ERROR, this) << "Received STUN binding error without a error " |
| 440 | << "code from " << addr.ToSensitiveString(); |
| 441 | return true; |
| 442 | } |
| 443 | } |
| 444 | // NOTE: Username should not be used in verifying response messages. |
| 445 | out_username->clear(); |
| 446 | } else if (stun_msg->type() == STUN_BINDING_INDICATION) { |
| 447 | LOG_J(LS_VERBOSE, this) << "Received STUN binding indication:" |
| 448 | << " from " << addr.ToSensitiveString(); |
| 449 | out_username->clear(); |
| 450 | // No stun attributes will be verified, if it's stun indication message. |
| 451 | // Returning from end of the this method. |
| 452 | } else { |
| 453 | LOG_J(LS_ERROR, this) << "Received STUN packet with invalid type (" |
| 454 | << stun_msg->type() << ") from " |
| 455 | << addr.ToSensitiveString(); |
| 456 | return true; |
| 457 | } |
| 458 | |
| 459 | // Return the STUN message found. |
| 460 | *out_msg = stun_msg.release(); |
| 461 | return true; |
| 462 | } |
| 463 | |
| 464 | bool Port::IsCompatibleAddress(const rtc::SocketAddress& addr) { |
| 465 | int family = ip().family(); |
| 466 | // We use single-stack sockets, so families must match. |
| 467 | if (addr.family() != family) { |
| 468 | return false; |
| 469 | } |
| 470 | // Link-local IPv6 ports can only connect to other link-local IPv6 ports. |
| 471 | if (family == AF_INET6 && (IPIsPrivate(ip()) != IPIsPrivate(addr.ipaddr()))) { |
| 472 | return false; |
| 473 | } |
| 474 | return true; |
| 475 | } |
| 476 | |
| 477 | bool Port::ParseStunUsername(const StunMessage* stun_msg, |
| 478 | std::string* local_ufrag, |
| 479 | std::string* remote_ufrag, |
| 480 | IceProtocolType* remote_protocol_type) const { |
| 481 | // The packet must include a username that either begins or ends with our |
| 482 | // fragment. It should begin with our fragment if it is a request and it |
| 483 | // should end with our fragment if it is a response. |
| 484 | local_ufrag->clear(); |
| 485 | remote_ufrag->clear(); |
| 486 | const StunByteStringAttribute* username_attr = |
| 487 | stun_msg->GetByteString(STUN_ATTR_USERNAME); |
| 488 | if (username_attr == NULL) |
| 489 | return false; |
| 490 | |
| 491 | const std::string username_attr_str = username_attr->GetString(); |
| 492 | size_t colon_pos = username_attr_str.find(":"); |
| 493 | // If we are in hybrid mode set the appropriate ice protocol type based on |
| 494 | // the username argument style. |
| 495 | if (IsHybridIce()) { |
| 496 | *remote_protocol_type = (colon_pos != std::string::npos) ? |
| 497 | ICEPROTO_RFC5245 : ICEPROTO_GOOGLE; |
| 498 | } else { |
| 499 | *remote_protocol_type = ice_protocol_; |
| 500 | } |
| 501 | if (*remote_protocol_type == ICEPROTO_RFC5245) { |
| 502 | if (colon_pos != std::string::npos) { // RFRAG:LFRAG |
| 503 | *local_ufrag = username_attr_str.substr(0, colon_pos); |
| 504 | *remote_ufrag = username_attr_str.substr( |
| 505 | colon_pos + 1, username_attr_str.size()); |
| 506 | } else { |
| 507 | return false; |
| 508 | } |
| 509 | } else if (*remote_protocol_type == ICEPROTO_GOOGLE) { |
| 510 | int remote_frag_len = static_cast<int>(username_attr_str.size()); |
| 511 | remote_frag_len -= static_cast<int>(username_fragment().size()); |
| 512 | if (remote_frag_len < 0) |
| 513 | return false; |
| 514 | |
| 515 | *local_ufrag = username_attr_str.substr(0, username_fragment().size()); |
| 516 | *remote_ufrag = username_attr_str.substr( |
| 517 | username_fragment().size(), username_attr_str.size()); |
| 518 | } |
| 519 | return true; |
| 520 | } |
| 521 | |
| 522 | bool Port::MaybeIceRoleConflict( |
| 523 | const rtc::SocketAddress& addr, IceMessage* stun_msg, |
| 524 | const std::string& remote_ufrag) { |
| 525 | // Validate ICE_CONTROLLING or ICE_CONTROLLED attributes. |
| 526 | bool ret = true; |
| 527 | IceRole remote_ice_role = ICEROLE_UNKNOWN; |
| 528 | uint64 remote_tiebreaker = 0; |
| 529 | const StunUInt64Attribute* stun_attr = |
| 530 | stun_msg->GetUInt64(STUN_ATTR_ICE_CONTROLLING); |
| 531 | if (stun_attr) { |
| 532 | remote_ice_role = ICEROLE_CONTROLLING; |
| 533 | remote_tiebreaker = stun_attr->value(); |
| 534 | } |
| 535 | |
| 536 | // If |remote_ufrag| is same as port local username fragment and |
| 537 | // tie breaker value received in the ping message matches port |
| 538 | // tiebreaker value this must be a loopback call. |
| 539 | // We will treat this as valid scenario. |
| 540 | if (remote_ice_role == ICEROLE_CONTROLLING && |
| 541 | username_fragment() == remote_ufrag && |
| 542 | remote_tiebreaker == IceTiebreaker()) { |
| 543 | return true; |
| 544 | } |
| 545 | |
| 546 | stun_attr = stun_msg->GetUInt64(STUN_ATTR_ICE_CONTROLLED); |
| 547 | if (stun_attr) { |
| 548 | remote_ice_role = ICEROLE_CONTROLLED; |
| 549 | remote_tiebreaker = stun_attr->value(); |
| 550 | } |
| 551 | |
| 552 | switch (ice_role_) { |
| 553 | case ICEROLE_CONTROLLING: |
| 554 | if (ICEROLE_CONTROLLING == remote_ice_role) { |
| 555 | if (remote_tiebreaker >= tiebreaker_) { |
| 556 | SignalRoleConflict(this); |
| 557 | } else { |
| 558 | // Send Role Conflict (487) error response. |
| 559 | SendBindingErrorResponse(stun_msg, addr, |
| 560 | STUN_ERROR_ROLE_CONFLICT, STUN_ERROR_REASON_ROLE_CONFLICT); |
| 561 | ret = false; |
| 562 | } |
| 563 | } |
| 564 | break; |
| 565 | case ICEROLE_CONTROLLED: |
| 566 | if (ICEROLE_CONTROLLED == remote_ice_role) { |
| 567 | if (remote_tiebreaker < tiebreaker_) { |
| 568 | SignalRoleConflict(this); |
| 569 | } else { |
| 570 | // Send Role Conflict (487) error response. |
| 571 | SendBindingErrorResponse(stun_msg, addr, |
| 572 | STUN_ERROR_ROLE_CONFLICT, STUN_ERROR_REASON_ROLE_CONFLICT); |
| 573 | ret = false; |
| 574 | } |
| 575 | } |
| 576 | break; |
| 577 | default: |
| 578 | ASSERT(false); |
| 579 | } |
| 580 | return ret; |
| 581 | } |
| 582 | |
| 583 | void Port::CreateStunUsername(const std::string& remote_username, |
| 584 | std::string* stun_username_attr_str) const { |
| 585 | stun_username_attr_str->clear(); |
| 586 | *stun_username_attr_str = remote_username; |
| 587 | if (IsStandardIce()) { |
| 588 | // Connectivity checks from L->R will have username RFRAG:LFRAG. |
| 589 | stun_username_attr_str->append(":"); |
| 590 | } |
| 591 | stun_username_attr_str->append(username_fragment()); |
| 592 | } |
| 593 | |
| 594 | void Port::SendBindingResponse(StunMessage* request, |
| 595 | const rtc::SocketAddress& addr) { |
| 596 | ASSERT(request->type() == STUN_BINDING_REQUEST); |
| 597 | |
| 598 | // Retrieve the username from the request. |
| 599 | const StunByteStringAttribute* username_attr = |
| 600 | request->GetByteString(STUN_ATTR_USERNAME); |
| 601 | ASSERT(username_attr != NULL); |
| 602 | if (username_attr == NULL) { |
| 603 | // No valid username, skip the response. |
| 604 | return; |
| 605 | } |
| 606 | |
| 607 | // Fill in the response message. |
| 608 | StunMessage response; |
| 609 | response.SetType(STUN_BINDING_RESPONSE); |
| 610 | response.SetTransactionID(request->transaction_id()); |
| 611 | const StunUInt32Attribute* retransmit_attr = |
| 612 | request->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT); |
| 613 | if (retransmit_attr) { |
| 614 | // Inherit the incoming retransmit value in the response so the other side |
| 615 | // can see our view of lost pings. |
| 616 | response.AddAttribute(new StunUInt32Attribute( |
| 617 | STUN_ATTR_RETRANSMIT_COUNT, retransmit_attr->value())); |
| 618 | |
| 619 | if (retransmit_attr->value() > CONNECTION_WRITE_CONNECT_FAILURES) { |
| 620 | LOG_J(LS_INFO, this) |
| 621 | << "Received a remote ping with high retransmit count: " |
| 622 | << retransmit_attr->value(); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | // Only GICE messages have USERNAME and MAPPED-ADDRESS in the response. |
| 627 | // ICE messages use XOR-MAPPED-ADDRESS, and add MESSAGE-INTEGRITY. |
| 628 | if (IsStandardIce()) { |
| 629 | response.AddAttribute( |
| 630 | new StunXorAddressAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS, addr)); |
| 631 | response.AddMessageIntegrity(password_); |
| 632 | response.AddFingerprint(); |
| 633 | } else if (IsGoogleIce()) { |
| 634 | response.AddAttribute( |
| 635 | new StunAddressAttribute(STUN_ATTR_MAPPED_ADDRESS, addr)); |
| 636 | response.AddAttribute(new StunByteStringAttribute( |
| 637 | STUN_ATTR_USERNAME, username_attr->GetString())); |
| 638 | } |
| 639 | |
| 640 | // Send the response message. |
| 641 | rtc::ByteBuffer buf; |
| 642 | response.Write(&buf); |
| 643 | rtc::PacketOptions options(DefaultDscpValue()); |
| 644 | if (SendTo(buf.Data(), buf.Length(), addr, options, false) < 0) { |
| 645 | LOG_J(LS_ERROR, this) << "Failed to send STUN ping response to " |
| 646 | << addr.ToSensitiveString(); |
| 647 | } |
| 648 | |
| 649 | // The fact that we received a successful request means that this connection |
| 650 | // (if one exists) should now be readable. |
| 651 | Connection* conn = GetConnection(addr); |
| 652 | ASSERT(conn != NULL); |
| 653 | if (conn) |
| 654 | conn->ReceivedPing(); |
| 655 | } |
| 656 | |
| 657 | void Port::SendBindingErrorResponse(StunMessage* request, |
| 658 | const rtc::SocketAddress& addr, |
| 659 | int error_code, const std::string& reason) { |
| 660 | ASSERT(request->type() == STUN_BINDING_REQUEST); |
| 661 | |
| 662 | // Fill in the response message. |
| 663 | StunMessage response; |
| 664 | response.SetType(STUN_BINDING_ERROR_RESPONSE); |
| 665 | response.SetTransactionID(request->transaction_id()); |
| 666 | |
| 667 | // When doing GICE, we need to write out the error code incorrectly to |
| 668 | // maintain backwards compatiblility. |
| 669 | StunErrorCodeAttribute* error_attr = StunAttribute::CreateErrorCode(); |
| 670 | if (IsStandardIce()) { |
| 671 | error_attr->SetCode(error_code); |
| 672 | } else if (IsGoogleIce()) { |
| 673 | error_attr->SetClass(error_code / 256); |
| 674 | error_attr->SetNumber(error_code % 256); |
| 675 | } |
| 676 | error_attr->SetReason(reason); |
| 677 | response.AddAttribute(error_attr); |
| 678 | |
| 679 | if (IsStandardIce()) { |
| 680 | // Per Section 10.1.2, certain error cases don't get a MESSAGE-INTEGRITY, |
| 681 | // because we don't have enough information to determine the shared secret. |
| 682 | if (error_code != STUN_ERROR_BAD_REQUEST && |
| 683 | error_code != STUN_ERROR_UNAUTHORIZED) |
| 684 | response.AddMessageIntegrity(password_); |
| 685 | response.AddFingerprint(); |
| 686 | } else if (IsGoogleIce()) { |
| 687 | // GICE responses include a username, if one exists. |
| 688 | const StunByteStringAttribute* username_attr = |
| 689 | request->GetByteString(STUN_ATTR_USERNAME); |
| 690 | if (username_attr) |
| 691 | response.AddAttribute(new StunByteStringAttribute( |
| 692 | STUN_ATTR_USERNAME, username_attr->GetString())); |
| 693 | } |
| 694 | |
| 695 | // Send the response message. |
| 696 | rtc::ByteBuffer buf; |
| 697 | response.Write(&buf); |
| 698 | rtc::PacketOptions options(DefaultDscpValue()); |
| 699 | SendTo(buf.Data(), buf.Length(), addr, options, false); |
| 700 | LOG_J(LS_INFO, this) << "Sending STUN binding error: reason=" << reason |
| 701 | << " to " << addr.ToSensitiveString(); |
| 702 | } |
| 703 | |
| 704 | void Port::OnMessage(rtc::Message *pmsg) { |
| 705 | ASSERT(pmsg->message_id == MSG_CHECKTIMEOUT); |
| 706 | CheckTimeout(); |
| 707 | } |
| 708 | |
| 709 | std::string Port::ToString() const { |
| 710 | std::stringstream ss; |
| 711 | ss << "Port[" << content_name_ << ":" << component_ |
| 712 | << ":" << generation_ << ":" << type_ |
| 713 | << ":" << network_->ToString() << "]"; |
| 714 | return ss.str(); |
| 715 | } |
| 716 | |
| 717 | void Port::EnablePortPackets() { |
| 718 | enable_port_packets_ = true; |
| 719 | } |
| 720 | |
| 721 | void Port::OnConnectionDestroyed(Connection* conn) { |
| 722 | AddressMap::iterator iter = |
| 723 | connections_.find(conn->remote_candidate().address()); |
| 724 | ASSERT(iter != connections_.end()); |
| 725 | connections_.erase(iter); |
| 726 | |
| 727 | // On the controlled side, ports time out, but only after all connections |
| 728 | // fail. Note: If a new connection is added after this message is posted, |
| 729 | // but it fails and is removed before kPortTimeoutDelay, then this message |
| 730 | // will still cause the Port to be destroyed. |
| 731 | if (ice_role_ == ICEROLE_CONTROLLED) |
| 732 | thread_->PostDelayed(timeout_delay_, this, MSG_CHECKTIMEOUT); |
| 733 | } |
| 734 | |
| 735 | void Port::Destroy() { |
| 736 | ASSERT(connections_.empty()); |
| 737 | LOG_J(LS_INFO, this) << "Port deleted"; |
| 738 | SignalDestroyed(this); |
| 739 | delete this; |
| 740 | } |
| 741 | |
| 742 | void Port::CheckTimeout() { |
| 743 | ASSERT(ice_role_ == ICEROLE_CONTROLLED); |
| 744 | // If this port has no connections, then there's no reason to keep it around. |
| 745 | // When the connections time out (both read and write), they will delete |
| 746 | // themselves, so if we have any connections, they are either readable or |
| 747 | // writable (or still connecting). |
| 748 | if (connections_.empty()) |
| 749 | Destroy(); |
| 750 | } |
| 751 | |
| 752 | const std::string Port::username_fragment() const { |
| 753 | if (!IsStandardIce() && |
| 754 | component_ == ICE_CANDIDATE_COMPONENT_RTCP) { |
| 755 | // In GICE mode, we should adjust username fragment for rtcp component. |
| 756 | return GetRtcpUfragFromRtpUfrag(ice_username_fragment_); |
| 757 | } else { |
| 758 | return ice_username_fragment_; |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | // A ConnectionRequest is a simple STUN ping used to determine writability. |
| 763 | class ConnectionRequest : public StunRequest { |
| 764 | public: |
| 765 | explicit ConnectionRequest(Connection* connection) |
| 766 | : StunRequest(new IceMessage()), |
| 767 | connection_(connection) { |
| 768 | } |
| 769 | |
| 770 | virtual ~ConnectionRequest() { |
| 771 | } |
| 772 | |
| 773 | virtual void Prepare(StunMessage* request) { |
| 774 | request->SetType(STUN_BINDING_REQUEST); |
| 775 | std::string username; |
| 776 | connection_->port()->CreateStunUsername( |
| 777 | connection_->remote_candidate().username(), &username); |
| 778 | request->AddAttribute( |
| 779 | new StunByteStringAttribute(STUN_ATTR_USERNAME, username)); |
| 780 | |
| 781 | // connection_ already holds this ping, so subtract one from count. |
| 782 | if (connection_->port()->send_retransmit_count_attribute()) { |
| 783 | request->AddAttribute(new StunUInt32Attribute( |
| 784 | STUN_ATTR_RETRANSMIT_COUNT, |
| 785 | static_cast<uint32>( |
| 786 | connection_->pings_since_last_response_.size() - 1))); |
| 787 | } |
| 788 | |
| 789 | // Adding ICE-specific attributes to the STUN request message. |
| 790 | if (connection_->port()->IsStandardIce()) { |
| 791 | // Adding ICE_CONTROLLED or ICE_CONTROLLING attribute based on the role. |
| 792 | if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLING) { |
| 793 | request->AddAttribute(new StunUInt64Attribute( |
| 794 | STUN_ATTR_ICE_CONTROLLING, connection_->port()->IceTiebreaker())); |
| 795 | // Since we are trying aggressive nomination, sending USE-CANDIDATE |
| 796 | // attribute in every ping. |
| 797 | // If we are dealing with a ice-lite end point, nomination flag |
| 798 | // in Connection will be set to false by default. Once the connection |
| 799 | // becomes "best connection", nomination flag will be turned on. |
| 800 | if (connection_->use_candidate_attr()) { |
| 801 | request->AddAttribute(new StunByteStringAttribute( |
| 802 | STUN_ATTR_USE_CANDIDATE)); |
| 803 | } |
| 804 | } else if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLED) { |
| 805 | request->AddAttribute(new StunUInt64Attribute( |
| 806 | STUN_ATTR_ICE_CONTROLLED, connection_->port()->IceTiebreaker())); |
| 807 | } else { |
| 808 | ASSERT(false); |
| 809 | } |
| 810 | |
| 811 | // Adding PRIORITY Attribute. |
| 812 | // Changing the type preference to Peer Reflexive and local preference |
| 813 | // and component id information is unchanged from the original priority. |
| 814 | // priority = (2^24)*(type preference) + |
| 815 | // (2^8)*(local preference) + |
| 816 | // (2^0)*(256 - component ID) |
| 817 | uint32 prflx_priority = ICE_TYPE_PREFERENCE_PRFLX << 24 | |
| 818 | (connection_->local_candidate().priority() & 0x00FFFFFF); |
| 819 | request->AddAttribute( |
| 820 | new StunUInt32Attribute(STUN_ATTR_PRIORITY, prflx_priority)); |
| 821 | |
| 822 | // Adding Message Integrity attribute. |
| 823 | request->AddMessageIntegrity(connection_->remote_candidate().password()); |
| 824 | // Adding Fingerprint. |
| 825 | request->AddFingerprint(); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | virtual void OnResponse(StunMessage* response) { |
| 830 | connection_->OnConnectionRequestResponse(this, response); |
| 831 | } |
| 832 | |
| 833 | virtual void OnErrorResponse(StunMessage* response) { |
| 834 | connection_->OnConnectionRequestErrorResponse(this, response); |
| 835 | } |
| 836 | |
| 837 | virtual void OnTimeout() { |
| 838 | connection_->OnConnectionRequestTimeout(this); |
| 839 | } |
| 840 | |
| 841 | virtual int GetNextDelay() { |
| 842 | // Each request is sent only once. After a single delay , the request will |
| 843 | // time out. |
| 844 | timeout_ = true; |
| 845 | return CONNECTION_RESPONSE_TIMEOUT; |
| 846 | } |
| 847 | |
| 848 | private: |
| 849 | Connection* connection_; |
| 850 | }; |
| 851 | |
| 852 | // |
| 853 | // Connection |
| 854 | // |
| 855 | |
guoweis@webrtc.org | 930e004 | 2014-11-17 19:42:14 +0000 | [diff] [blame] | 856 | Connection::Connection(Port* port, |
| 857 | size_t index, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 858 | const Candidate& remote_candidate) |
guoweis@webrtc.org | 930e004 | 2014-11-17 19:42:14 +0000 | [diff] [blame] | 859 | : port_(port), |
| 860 | local_candidate_index_(index), |
| 861 | remote_candidate_(remote_candidate), |
| 862 | read_state_(STATE_READ_INIT), |
| 863 | write_state_(STATE_WRITE_INIT), |
| 864 | connected_(true), |
| 865 | pruned_(false), |
| 866 | use_candidate_attr_(false), |
| 867 | remote_ice_mode_(ICEMODE_FULL), |
| 868 | requests_(port->thread()), |
| 869 | rtt_(DEFAULT_RTT), |
| 870 | last_ping_sent_(0), |
| 871 | last_ping_received_(0), |
| 872 | last_data_received_(0), |
| 873 | last_ping_response_received_(0), |
| 874 | sent_packets_discarded_(0), |
| 875 | sent_packets_total_(0), |
| 876 | reported_(false), |
| 877 | state_(STATE_WAITING) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 878 | // All of our connections start in WAITING state. |
| 879 | // TODO(mallinath) - Start connections from STATE_FROZEN. |
| 880 | // Wire up to send stun packets |
| 881 | requests_.SignalSendPacket.connect(this, &Connection::OnSendStunPacket); |
| 882 | LOG_J(LS_INFO, this) << "Connection created"; |
| 883 | } |
| 884 | |
| 885 | Connection::~Connection() { |
| 886 | } |
| 887 | |
| 888 | const Candidate& Connection::local_candidate() const { |
| 889 | ASSERT(local_candidate_index_ < port_->Candidates().size()); |
| 890 | return port_->Candidates()[local_candidate_index_]; |
| 891 | } |
| 892 | |
| 893 | uint64 Connection::priority() const { |
| 894 | uint64 priority = 0; |
| 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) { |
| 902 | uint32 g = 0; |
| 903 | uint32 d = 0; |
| 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 | } |
| 911 | priority = rtc::_min(g, d); |
| 912 | priority = priority << 32; |
| 913 | priority += 2 * rtc::_max(g, d) + (g > d ? 1 : 0); |
| 914 | } |
| 915 | return priority; |
| 916 | } |
| 917 | |
| 918 | void Connection::set_read_state(ReadState value) { |
| 919 | ReadState old_value = read_state_; |
| 920 | read_state_ = value; |
| 921 | if (value != old_value) { |
| 922 | LOG_J(LS_VERBOSE, this) << "set_read_state"; |
| 923 | SignalStateChange(this); |
| 924 | CheckTimeout(); |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | void Connection::set_write_state(WriteState value) { |
| 929 | WriteState old_value = write_state_; |
| 930 | write_state_ = value; |
| 931 | if (value != old_value) { |
guoweis@webrtc.org | 8c9ff20 | 2014-12-04 07:56:02 +0000 | [diff] [blame] | 932 | LOG_J(LS_VERBOSE, this) << "set_write_state from: " << old_value << " to " |
| 933 | << value; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 934 | SignalStateChange(this); |
| 935 | CheckTimeout(); |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | void Connection::set_state(State state) { |
| 940 | State old_state = state_; |
| 941 | state_ = state; |
| 942 | if (state != old_state) { |
| 943 | LOG_J(LS_VERBOSE, this) << "set_state"; |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | void Connection::set_connected(bool value) { |
| 948 | bool old_value = connected_; |
| 949 | connected_ = value; |
| 950 | if (value != old_value) { |
| 951 | LOG_J(LS_VERBOSE, this) << "set_connected"; |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | void Connection::set_use_candidate_attr(bool enable) { |
| 956 | use_candidate_attr_ = enable; |
| 957 | } |
| 958 | |
| 959 | void Connection::OnSendStunPacket(const void* data, size_t size, |
| 960 | StunRequest* req) { |
| 961 | rtc::PacketOptions options(port_->DefaultDscpValue()); |
| 962 | if (port_->SendTo(data, size, remote_candidate_.address(), |
| 963 | options, false) < 0) { |
| 964 | LOG_J(LS_WARNING, this) << "Failed to send STUN ping " << req->id(); |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | void Connection::OnReadPacket( |
| 969 | const char* data, size_t size, const rtc::PacketTime& packet_time) { |
| 970 | rtc::scoped_ptr<IceMessage> msg; |
| 971 | std::string remote_ufrag; |
| 972 | const rtc::SocketAddress& addr(remote_candidate_.address()); |
| 973 | if (!port_->GetStunMessage(data, size, addr, msg.accept(), &remote_ufrag)) { |
| 974 | // The packet did not parse as a valid STUN message |
| 975 | |
| 976 | // If this connection is readable, then pass along the packet. |
| 977 | if (read_state_ == STATE_READABLE) { |
| 978 | // readable means data from this address is acceptable |
| 979 | // Send it on! |
| 980 | |
| 981 | last_data_received_ = rtc::Time(); |
| 982 | recv_rate_tracker_.Update(size); |
| 983 | SignalReadPacket(this, data, size, packet_time); |
| 984 | |
| 985 | // If timed out sending writability checks, start up again |
| 986 | if (!pruned_ && (write_state_ == STATE_WRITE_TIMEOUT)) { |
| 987 | LOG(LS_WARNING) << "Received a data packet on a timed-out Connection. " |
| 988 | << "Resetting state to STATE_WRITE_INIT."; |
| 989 | set_write_state(STATE_WRITE_INIT); |
| 990 | } |
| 991 | } else { |
| 992 | // Not readable means the remote address hasn't sent a valid |
| 993 | // binding request yet. |
| 994 | |
| 995 | LOG_J(LS_WARNING, this) |
| 996 | << "Received non-STUN packet from an unreadable connection."; |
| 997 | } |
| 998 | } else if (!msg) { |
| 999 | // The packet was STUN, but failed a check and was handled internally. |
| 1000 | } else { |
| 1001 | // The packet is STUN and passed the Port checks. |
| 1002 | // Perform our own checks to ensure this packet is valid. |
| 1003 | // If this is a STUN request, then update the readable bit and respond. |
| 1004 | // If this is a STUN response, then update the writable bit. |
| 1005 | switch (msg->type()) { |
| 1006 | case STUN_BINDING_REQUEST: |
| 1007 | if (remote_ufrag == remote_candidate_.username()) { |
| 1008 | // Check for role conflicts. |
| 1009 | if (port_->IsStandardIce() && |
| 1010 | !port_->MaybeIceRoleConflict(addr, msg.get(), remote_ufrag)) { |
| 1011 | // Received conflicting role from the peer. |
| 1012 | LOG(LS_INFO) << "Received conflicting role from the peer."; |
| 1013 | return; |
| 1014 | } |
| 1015 | |
| 1016 | // Incoming, validated stun request from remote peer. |
| 1017 | // This call will also set the connection readable. |
| 1018 | port_->SendBindingResponse(msg.get(), addr); |
| 1019 | |
| 1020 | // If timed out sending writability checks, start up again |
| 1021 | if (!pruned_ && (write_state_ == STATE_WRITE_TIMEOUT)) |
| 1022 | set_write_state(STATE_WRITE_INIT); |
| 1023 | |
| 1024 | if ((port_->IsStandardIce()) && |
| 1025 | (port_->GetIceRole() == ICEROLE_CONTROLLED)) { |
| 1026 | const StunByteStringAttribute* use_candidate_attr = |
| 1027 | msg->GetByteString(STUN_ATTR_USE_CANDIDATE); |
| 1028 | if (use_candidate_attr) |
| 1029 | SignalUseCandidate(this); |
| 1030 | } |
| 1031 | } else { |
| 1032 | // The packet had the right local username, but the remote username |
| 1033 | // was not the right one for the remote address. |
| 1034 | LOG_J(LS_ERROR, this) |
| 1035 | << "Received STUN request with bad remote username " |
| 1036 | << remote_ufrag; |
| 1037 | port_->SendBindingErrorResponse(msg.get(), addr, |
| 1038 | STUN_ERROR_UNAUTHORIZED, |
| 1039 | STUN_ERROR_REASON_UNAUTHORIZED); |
| 1040 | |
| 1041 | } |
| 1042 | break; |
| 1043 | |
| 1044 | // Response from remote peer. Does it match request sent? |
| 1045 | // This doesn't just check, it makes callbacks if transaction |
| 1046 | // id's match. |
| 1047 | case STUN_BINDING_RESPONSE: |
| 1048 | case STUN_BINDING_ERROR_RESPONSE: |
| 1049 | if (port_->IsGoogleIce() || |
| 1050 | msg->ValidateMessageIntegrity( |
| 1051 | data, size, remote_candidate().password())) { |
| 1052 | requests_.CheckResponse(msg.get()); |
| 1053 | } |
| 1054 | // Otherwise silently discard the response message. |
| 1055 | break; |
| 1056 | |
| 1057 | // Remote end point sent an STUN indication instead of regular |
| 1058 | // binding request. In this case |last_ping_received_| will be updated. |
| 1059 | // Otherwise we can mark connection to read timeout. No response will be |
| 1060 | // sent in this scenario. |
| 1061 | case STUN_BINDING_INDICATION: |
| 1062 | if (port_->IsStandardIce() && read_state_ == STATE_READABLE) { |
| 1063 | ReceivedPing(); |
| 1064 | } else { |
| 1065 | LOG_J(LS_WARNING, this) << "Received STUN binding indication " |
| 1066 | << "from an unreadable connection."; |
| 1067 | } |
| 1068 | break; |
| 1069 | |
| 1070 | default: |
| 1071 | ASSERT(false); |
| 1072 | break; |
| 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | void Connection::OnReadyToSend() { |
| 1078 | if (write_state_ == STATE_WRITABLE) { |
| 1079 | SignalReadyToSend(this); |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | void Connection::Prune() { |
| 1084 | if (!pruned_) { |
| 1085 | LOG_J(LS_VERBOSE, this) << "Connection pruned"; |
| 1086 | pruned_ = true; |
| 1087 | requests_.Clear(); |
| 1088 | set_write_state(STATE_WRITE_TIMEOUT); |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | void Connection::Destroy() { |
| 1093 | LOG_J(LS_VERBOSE, this) << "Connection destroyed"; |
| 1094 | set_read_state(STATE_READ_TIMEOUT); |
| 1095 | set_write_state(STATE_WRITE_TIMEOUT); |
| 1096 | } |
| 1097 | |
| 1098 | void Connection::UpdateState(uint32 now) { |
| 1099 | uint32 rtt = ConservativeRTTEstimate(rtt_); |
| 1100 | |
| 1101 | std::string pings; |
| 1102 | for (size_t i = 0; i < pings_since_last_response_.size(); ++i) { |
| 1103 | char buf[32]; |
| 1104 | rtc::sprintfn(buf, sizeof(buf), "%u", |
| 1105 | pings_since_last_response_[i]); |
| 1106 | pings.append(buf).append(" "); |
| 1107 | } |
guoweis@webrtc.org | 8c9ff20 | 2014-12-04 07:56:02 +0000 | [diff] [blame] | 1108 | LOG_J(LS_VERBOSE, this) << "UpdateState(): pings_since_last_response_=" |
| 1109 | << pings << ", rtt=" << rtt << ", now=" << now |
| 1110 | << ", last ping received: " << last_ping_received_ |
| 1111 | << ", last data_received: " << last_data_received_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1112 | |
| 1113 | // Check the readable state. |
| 1114 | // |
| 1115 | // Since we don't know how many pings the other side has attempted, the best |
| 1116 | // test we can do is a simple window. |
| 1117 | // If other side has not sent ping after connection has become readable, use |
| 1118 | // |last_data_received_| as the indication. |
| 1119 | // If remote endpoint is doing RFC 5245, it's not required to send ping |
| 1120 | // after connection is established. If this connection is serving a data |
| 1121 | // channel, it may not be in a position to send media continuously. Do not |
| 1122 | // mark connection timeout if it's in RFC5245 mode. |
| 1123 | // Below check will be performed with end point if it's doing google-ice. |
| 1124 | if (port_->IsGoogleIce() && (read_state_ == STATE_READABLE) && |
| 1125 | (last_ping_received_ + CONNECTION_READ_TIMEOUT <= now) && |
| 1126 | (last_data_received_ + CONNECTION_READ_TIMEOUT <= now)) { |
| 1127 | LOG_J(LS_INFO, this) << "Unreadable after " |
| 1128 | << now - last_ping_received_ |
| 1129 | << " ms without a ping," |
| 1130 | << " ms since last received response=" |
| 1131 | << now - last_ping_response_received_ |
| 1132 | << " ms since last received data=" |
| 1133 | << now - last_data_received_ |
| 1134 | << " rtt=" << rtt; |
| 1135 | set_read_state(STATE_READ_TIMEOUT); |
| 1136 | } |
| 1137 | |
| 1138 | // Check the writable state. (The order of these checks is important.) |
| 1139 | // |
| 1140 | // Before becoming unwritable, we allow for a fixed number of pings to fail |
| 1141 | // (i.e., receive no response). We also have to give the response time to |
| 1142 | // get back, so we include a conservative estimate of this. |
| 1143 | // |
| 1144 | // Before timing out writability, we give a fixed amount of time. This is to |
| 1145 | // allow for changes in network conditions. |
| 1146 | |
| 1147 | if ((write_state_ == STATE_WRITABLE) && |
| 1148 | TooManyFailures(pings_since_last_response_, |
| 1149 | CONNECTION_WRITE_CONNECT_FAILURES, |
| 1150 | rtt, |
| 1151 | now) && |
| 1152 | TooLongWithoutResponse(pings_since_last_response_, |
| 1153 | CONNECTION_WRITE_CONNECT_TIMEOUT, |
| 1154 | now)) { |
| 1155 | uint32 max_pings = CONNECTION_WRITE_CONNECT_FAILURES; |
| 1156 | LOG_J(LS_INFO, this) << "Unwritable after " << max_pings |
| 1157 | << " ping failures and " |
| 1158 | << now - pings_since_last_response_[0] |
| 1159 | << " ms without a response," |
| 1160 | << " ms since last received ping=" |
| 1161 | << now - last_ping_received_ |
| 1162 | << " ms since last received data=" |
| 1163 | << now - last_data_received_ |
| 1164 | << " rtt=" << rtt; |
| 1165 | set_write_state(STATE_WRITE_UNRELIABLE); |
| 1166 | } |
| 1167 | |
| 1168 | if ((write_state_ == STATE_WRITE_UNRELIABLE || |
| 1169 | write_state_ == STATE_WRITE_INIT) && |
| 1170 | TooLongWithoutResponse(pings_since_last_response_, |
| 1171 | CONNECTION_WRITE_TIMEOUT, |
| 1172 | now)) { |
| 1173 | LOG_J(LS_INFO, this) << "Timed out after " |
| 1174 | << now - pings_since_last_response_[0] |
| 1175 | << " ms without a response, rtt=" << rtt; |
| 1176 | set_write_state(STATE_WRITE_TIMEOUT); |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | void Connection::Ping(uint32 now) { |
| 1181 | ASSERT(connected_); |
| 1182 | last_ping_sent_ = now; |
| 1183 | pings_since_last_response_.push_back(now); |
| 1184 | ConnectionRequest *req = new ConnectionRequest(this); |
| 1185 | LOG_J(LS_VERBOSE, this) << "Sending STUN ping " << req->id() << " at " << now; |
| 1186 | requests_.Send(req); |
| 1187 | state_ = STATE_INPROGRESS; |
| 1188 | } |
| 1189 | |
| 1190 | void Connection::ReceivedPing() { |
| 1191 | last_ping_received_ = rtc::Time(); |
| 1192 | set_read_state(STATE_READABLE); |
| 1193 | } |
| 1194 | |
guoweis@webrtc.org | 8c9ff20 | 2014-12-04 07:56:02 +0000 | [diff] [blame] | 1195 | std::string Connection::ToDebugId() const { |
| 1196 | std::stringstream ss; |
| 1197 | ss << std::hex << this; |
| 1198 | return ss.str(); |
| 1199 | } |
| 1200 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1201 | std::string Connection::ToString() const { |
| 1202 | const char CONNECT_STATE_ABBREV[2] = { |
| 1203 | '-', // not connected (false) |
| 1204 | 'C', // connected (true) |
| 1205 | }; |
| 1206 | const char READ_STATE_ABBREV[3] = { |
| 1207 | '-', // STATE_READ_INIT |
| 1208 | 'R', // STATE_READABLE |
| 1209 | 'x', // STATE_READ_TIMEOUT |
| 1210 | }; |
| 1211 | const char WRITE_STATE_ABBREV[4] = { |
| 1212 | 'W', // STATE_WRITABLE |
| 1213 | 'w', // STATE_WRITE_UNRELIABLE |
| 1214 | '-', // STATE_WRITE_INIT |
| 1215 | 'x', // STATE_WRITE_TIMEOUT |
| 1216 | }; |
| 1217 | const std::string ICESTATE[4] = { |
| 1218 | "W", // STATE_WAITING |
| 1219 | "I", // STATE_INPROGRESS |
| 1220 | "S", // STATE_SUCCEEDED |
| 1221 | "F" // STATE_FAILED |
| 1222 | }; |
| 1223 | const Candidate& local = local_candidate(); |
| 1224 | const Candidate& remote = remote_candidate(); |
| 1225 | std::stringstream ss; |
guoweis@webrtc.org | 8c9ff20 | 2014-12-04 07:56:02 +0000 | [diff] [blame] | 1226 | ss << "Conn[" << ToDebugId() |
| 1227 | << ":" << port_->content_name() |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1228 | << ":" << local.id() << ":" << local.component() |
| 1229 | << ":" << local.generation() |
| 1230 | << ":" << local.type() << ":" << local.protocol() |
| 1231 | << ":" << local.address().ToSensitiveString() |
| 1232 | << "->" << remote.id() << ":" << remote.component() |
| 1233 | << ":" << remote.priority() |
| 1234 | << ":" << remote.type() << ":" |
| 1235 | << remote.protocol() << ":" << remote.address().ToSensitiveString() << "|" |
| 1236 | << CONNECT_STATE_ABBREV[connected()] |
| 1237 | << READ_STATE_ABBREV[read_state()] |
| 1238 | << WRITE_STATE_ABBREV[write_state()] |
| 1239 | << ICESTATE[state()] << "|" |
| 1240 | << priority() << "|"; |
| 1241 | if (rtt_ < DEFAULT_RTT) { |
| 1242 | ss << rtt_ << "]"; |
| 1243 | } else { |
| 1244 | ss << "-]"; |
| 1245 | } |
| 1246 | return ss.str(); |
| 1247 | } |
| 1248 | |
| 1249 | std::string Connection::ToSensitiveString() const { |
| 1250 | return ToString(); |
| 1251 | } |
| 1252 | |
| 1253 | void Connection::OnConnectionRequestResponse(ConnectionRequest* request, |
| 1254 | StunMessage* response) { |
| 1255 | // We've already validated that this is a STUN binding response with |
| 1256 | // the correct local and remote username for this connection. |
| 1257 | // So if we're not already, become writable. We may be bringing a pruned |
| 1258 | // connection back to life, but if we don't really want it, we can always |
| 1259 | // prune it again. |
| 1260 | uint32 rtt = request->Elapsed(); |
| 1261 | set_write_state(STATE_WRITABLE); |
| 1262 | set_state(STATE_SUCCEEDED); |
| 1263 | |
| 1264 | if (remote_ice_mode_ == ICEMODE_LITE) { |
| 1265 | // A ice-lite end point never initiates ping requests. This will allow |
| 1266 | // us to move to STATE_READABLE. |
| 1267 | ReceivedPing(); |
| 1268 | } |
| 1269 | |
| 1270 | std::string pings; |
| 1271 | for (size_t i = 0; i < pings_since_last_response_.size(); ++i) { |
| 1272 | char buf[32]; |
| 1273 | rtc::sprintfn(buf, sizeof(buf), "%u", |
| 1274 | pings_since_last_response_[i]); |
| 1275 | pings.append(buf).append(" "); |
| 1276 | } |
| 1277 | |
| 1278 | rtc::LoggingSeverity level = |
| 1279 | (pings_since_last_response_.size() > CONNECTION_WRITE_CONNECT_FAILURES) ? |
| 1280 | rtc::LS_INFO : rtc::LS_VERBOSE; |
| 1281 | |
| 1282 | LOG_JV(level, this) << "Received STUN ping response " << request->id() |
| 1283 | << ", pings_since_last_response_=" << pings |
| 1284 | << ", rtt=" << rtt; |
| 1285 | |
| 1286 | pings_since_last_response_.clear(); |
| 1287 | last_ping_response_received_ = rtc::Time(); |
| 1288 | rtt_ = (RTT_RATIO * rtt_ + rtt) / (RTT_RATIO + 1); |
| 1289 | |
| 1290 | // Peer reflexive candidate is only for RFC 5245 ICE. |
| 1291 | if (port_->IsStandardIce()) { |
| 1292 | MaybeAddPrflxCandidate(request, response); |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | void Connection::OnConnectionRequestErrorResponse(ConnectionRequest* request, |
| 1297 | StunMessage* response) { |
| 1298 | const StunErrorCodeAttribute* error_attr = response->GetErrorCode(); |
| 1299 | int error_code = STUN_ERROR_GLOBAL_FAILURE; |
| 1300 | if (error_attr) { |
| 1301 | if (port_->IsGoogleIce()) { |
| 1302 | // When doing GICE, the error code is written out incorrectly, so we need |
| 1303 | // to unmunge it here. |
| 1304 | error_code = error_attr->eclass() * 256 + error_attr->number(); |
| 1305 | } else { |
| 1306 | error_code = error_attr->code(); |
| 1307 | } |
| 1308 | } |
| 1309 | |
| 1310 | if (error_code == STUN_ERROR_UNKNOWN_ATTRIBUTE || |
| 1311 | error_code == STUN_ERROR_SERVER_ERROR || |
| 1312 | error_code == STUN_ERROR_UNAUTHORIZED) { |
| 1313 | // Recoverable error, retry |
| 1314 | } else if (error_code == STUN_ERROR_STALE_CREDENTIALS) { |
| 1315 | // Race failure, retry |
| 1316 | } else if (error_code == STUN_ERROR_ROLE_CONFLICT) { |
| 1317 | HandleRoleConflictFromPeer(); |
| 1318 | } else { |
| 1319 | // This is not a valid connection. |
| 1320 | LOG_J(LS_ERROR, this) << "Received STUN error response, code=" |
| 1321 | << error_code << "; killing connection"; |
| 1322 | set_state(STATE_FAILED); |
| 1323 | set_write_state(STATE_WRITE_TIMEOUT); |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | void Connection::OnConnectionRequestTimeout(ConnectionRequest* request) { |
| 1328 | // Log at LS_INFO if we miss a ping on a writable connection. |
| 1329 | rtc::LoggingSeverity sev = (write_state_ == STATE_WRITABLE) ? |
| 1330 | rtc::LS_INFO : rtc::LS_VERBOSE; |
| 1331 | LOG_JV(sev, this) << "Timing-out STUN ping " << request->id() |
| 1332 | << " after " << request->Elapsed() << " ms"; |
| 1333 | } |
| 1334 | |
| 1335 | void Connection::CheckTimeout() { |
| 1336 | // If both read and write have timed out or read has never initialized, then |
| 1337 | // this connection can contribute no more to p2p socket unless at some later |
| 1338 | // date readability were to come back. However, we gave readability a long |
| 1339 | // time to timeout, so at this point, it seems fair to get rid of this |
| 1340 | // connection. |
| 1341 | if ((read_state_ == STATE_READ_TIMEOUT || |
| 1342 | read_state_ == STATE_READ_INIT) && |
| 1343 | write_state_ == STATE_WRITE_TIMEOUT) { |
| 1344 | port_->thread()->Post(this, MSG_DELETE); |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | void Connection::HandleRoleConflictFromPeer() { |
| 1349 | port_->SignalRoleConflict(port_); |
| 1350 | } |
| 1351 | |
jiayl@webrtc.org | dacdd94 | 2015-01-23 17:33:34 +0000 | [diff] [blame] | 1352 | void Connection::MaybeSetRemoteIceCredentials(const std::string& ice_ufrag, |
| 1353 | const std::string& ice_pwd) { |
| 1354 | if (remote_candidate_.username() == ice_ufrag && |
| 1355 | remote_candidate_.password().empty()) { |
| 1356 | remote_candidate_.set_password(ice_pwd); |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | void Connection::MaybeUpdatePeerReflexiveCandidate( |
| 1361 | const Candidate& new_candidate) { |
| 1362 | if (remote_candidate_.type() == PRFLX_PORT_TYPE && |
| 1363 | new_candidate.type() != PRFLX_PORT_TYPE && |
| 1364 | remote_candidate_.protocol() == new_candidate.protocol() && |
| 1365 | remote_candidate_.address() == new_candidate.address() && |
| 1366 | remote_candidate_.username() == new_candidate.username() && |
| 1367 | remote_candidate_.password() == new_candidate.password() && |
| 1368 | remote_candidate_.generation() == new_candidate.generation()) { |
| 1369 | remote_candidate_ = new_candidate; |
| 1370 | } |
| 1371 | } |
| 1372 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1373 | void Connection::OnMessage(rtc::Message *pmsg) { |
| 1374 | ASSERT(pmsg->message_id == MSG_DELETE); |
| 1375 | |
henrike@webrtc.org | 43e033e | 2014-11-10 19:40:29 +0000 | [diff] [blame] | 1376 | LOG_J(LS_INFO, this) << "Connection deleted due to read or write timeout"; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1377 | SignalDestroyed(this); |
| 1378 | delete this; |
| 1379 | } |
| 1380 | |
| 1381 | size_t Connection::recv_bytes_second() { |
| 1382 | return recv_rate_tracker_.units_second(); |
| 1383 | } |
| 1384 | |
| 1385 | size_t Connection::recv_total_bytes() { |
| 1386 | return recv_rate_tracker_.total_units(); |
| 1387 | } |
| 1388 | |
| 1389 | size_t Connection::sent_bytes_second() { |
| 1390 | return send_rate_tracker_.units_second(); |
| 1391 | } |
| 1392 | |
| 1393 | size_t Connection::sent_total_bytes() { |
| 1394 | return send_rate_tracker_.total_units(); |
| 1395 | } |
| 1396 | |
guoweis@webrtc.org | 930e004 | 2014-11-17 19:42:14 +0000 | [diff] [blame] | 1397 | size_t Connection::sent_discarded_packets() { |
| 1398 | return sent_packets_discarded_; |
| 1399 | } |
| 1400 | |
| 1401 | size_t Connection::sent_total_packets() { |
| 1402 | return sent_packets_total_; |
| 1403 | } |
| 1404 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1405 | void Connection::MaybeAddPrflxCandidate(ConnectionRequest* request, |
| 1406 | StunMessage* response) { |
| 1407 | // RFC 5245 |
| 1408 | // The agent checks the mapped address from the STUN response. If the |
| 1409 | // transport address does not match any of the local candidates that the |
| 1410 | // agent knows about, the mapped address represents a new candidate -- a |
| 1411 | // peer reflexive candidate. |
| 1412 | const StunAddressAttribute* addr = |
| 1413 | response->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); |
| 1414 | if (!addr) { |
| 1415 | LOG(LS_WARNING) << "Connection::OnConnectionRequestResponse - " |
| 1416 | << "No MAPPED-ADDRESS or XOR-MAPPED-ADDRESS found in the " |
| 1417 | << "stun response message"; |
| 1418 | return; |
| 1419 | } |
| 1420 | |
| 1421 | bool known_addr = false; |
| 1422 | for (size_t i = 0; i < port_->Candidates().size(); ++i) { |
| 1423 | if (port_->Candidates()[i].address() == addr->GetAddress()) { |
| 1424 | known_addr = true; |
| 1425 | break; |
| 1426 | } |
| 1427 | } |
| 1428 | if (known_addr) { |
| 1429 | return; |
| 1430 | } |
| 1431 | |
| 1432 | // RFC 5245 |
| 1433 | // Its priority is set equal to the value of the PRIORITY attribute |
| 1434 | // in the Binding request. |
| 1435 | const StunUInt32Attribute* priority_attr = |
| 1436 | request->msg()->GetUInt32(STUN_ATTR_PRIORITY); |
| 1437 | if (!priority_attr) { |
| 1438 | LOG(LS_WARNING) << "Connection::OnConnectionRequestResponse - " |
| 1439 | << "No STUN_ATTR_PRIORITY found in the " |
| 1440 | << "stun response message"; |
| 1441 | return; |
| 1442 | } |
| 1443 | const uint32 priority = priority_attr->value(); |
| 1444 | std::string id = rtc::CreateRandomString(8); |
| 1445 | |
| 1446 | Candidate new_local_candidate; |
| 1447 | new_local_candidate.set_id(id); |
| 1448 | new_local_candidate.set_component(local_candidate().component()); |
| 1449 | new_local_candidate.set_type(PRFLX_PORT_TYPE); |
| 1450 | new_local_candidate.set_protocol(local_candidate().protocol()); |
| 1451 | new_local_candidate.set_address(addr->GetAddress()); |
| 1452 | new_local_candidate.set_priority(priority); |
| 1453 | new_local_candidate.set_username(local_candidate().username()); |
| 1454 | new_local_candidate.set_password(local_candidate().password()); |
| 1455 | new_local_candidate.set_network_name(local_candidate().network_name()); |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 1456 | new_local_candidate.set_network_type(local_candidate().network_type()); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1457 | new_local_candidate.set_related_address(local_candidate().address()); |
| 1458 | new_local_candidate.set_foundation( |
| 1459 | ComputeFoundation(PRFLX_PORT_TYPE, local_candidate().protocol(), |
| 1460 | local_candidate().address())); |
| 1461 | |
| 1462 | // Change the local candidate of this Connection to the new prflx candidate. |
| 1463 | local_candidate_index_ = port_->AddPrflxCandidate(new_local_candidate); |
| 1464 | |
| 1465 | // SignalStateChange to force a re-sort in P2PTransportChannel as this |
| 1466 | // Connection's local candidate has changed. |
| 1467 | SignalStateChange(this); |
| 1468 | } |
| 1469 | |
| 1470 | ProxyConnection::ProxyConnection(Port* port, size_t index, |
| 1471 | const Candidate& candidate) |
| 1472 | : Connection(port, index, candidate), error_(0) { |
| 1473 | } |
| 1474 | |
| 1475 | int ProxyConnection::Send(const void* data, size_t size, |
| 1476 | const rtc::PacketOptions& options) { |
| 1477 | if (write_state_ == STATE_WRITE_INIT || write_state_ == STATE_WRITE_TIMEOUT) { |
| 1478 | error_ = EWOULDBLOCK; |
| 1479 | return SOCKET_ERROR; |
| 1480 | } |
guoweis@webrtc.org | 930e004 | 2014-11-17 19:42:14 +0000 | [diff] [blame] | 1481 | sent_packets_total_++; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1482 | int sent = port_->SendTo(data, size, remote_candidate_.address(), |
| 1483 | options, true); |
| 1484 | if (sent <= 0) { |
| 1485 | ASSERT(sent < 0); |
| 1486 | error_ = port_->GetError(); |
guoweis@webrtc.org | 930e004 | 2014-11-17 19:42:14 +0000 | [diff] [blame] | 1487 | sent_packets_discarded_++; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1488 | } else { |
| 1489 | send_rate_tracker_.Update(sent); |
| 1490 | } |
| 1491 | return sent; |
| 1492 | } |
| 1493 | |
| 1494 | } // namespace cricket |