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