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