henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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/turnport.h" |
| 12 | |
| 13 | #include <functional> |
| 14 | |
| 15 | #include "webrtc/p2p/base/common.h" |
| 16 | #include "webrtc/p2p/base/stun.h" |
| 17 | #include "webrtc/base/asyncpacketsocket.h" |
| 18 | #include "webrtc/base/byteorder.h" |
| 19 | #include "webrtc/base/common.h" |
| 20 | #include "webrtc/base/logging.h" |
| 21 | #include "webrtc/base/nethelpers.h" |
| 22 | #include "webrtc/base/socketaddress.h" |
| 23 | #include "webrtc/base/stringencode.h" |
| 24 | |
| 25 | namespace cricket { |
| 26 | |
| 27 | // TODO(juberti): Move to stun.h when relay messages have been renamed. |
| 28 | static const int TURN_ALLOCATE_REQUEST = STUN_ALLOCATE_REQUEST; |
| 29 | |
| 30 | // TODO(juberti): Extract to turnmessage.h |
| 31 | static const int TURN_DEFAULT_PORT = 3478; |
| 32 | static const int TURN_CHANNEL_NUMBER_START = 0x4000; |
| 33 | static const int TURN_PERMISSION_TIMEOUT = 5 * 60 * 1000; // 5 minutes |
| 34 | |
| 35 | static const size_t TURN_CHANNEL_HEADER_SIZE = 4U; |
| 36 | |
| 37 | // Retry at most twice (i.e. three different ALLOCATE requests) on |
| 38 | // STUN_ERROR_ALLOCATION_MISMATCH error per rfc5766. |
| 39 | static const size_t MAX_ALLOCATE_MISMATCH_RETRIES = 2; |
| 40 | |
| 41 | inline bool IsTurnChannelData(uint16 msg_type) { |
| 42 | return ((msg_type & 0xC000) == 0x4000); // MSB are 0b01 |
| 43 | } |
| 44 | |
| 45 | static int GetRelayPreference(cricket::ProtocolType proto, bool secure) { |
| 46 | int relay_preference = ICE_TYPE_PREFERENCE_RELAY; |
| 47 | if (proto == cricket::PROTO_TCP) { |
| 48 | relay_preference -= 1; |
| 49 | if (secure) |
| 50 | relay_preference -= 1; |
| 51 | } |
| 52 | |
| 53 | ASSERT(relay_preference >= 0); |
| 54 | return relay_preference; |
| 55 | } |
| 56 | |
| 57 | class TurnAllocateRequest : public StunRequest { |
| 58 | public: |
| 59 | explicit TurnAllocateRequest(TurnPort* port); |
| 60 | virtual void Prepare(StunMessage* request); |
| 61 | virtual void OnResponse(StunMessage* response); |
| 62 | virtual void OnErrorResponse(StunMessage* response); |
| 63 | virtual void OnTimeout(); |
| 64 | |
| 65 | private: |
| 66 | // Handles authentication challenge from the server. |
| 67 | void OnAuthChallenge(StunMessage* response, int code); |
| 68 | void OnTryAlternate(StunMessage* response, int code); |
| 69 | void OnUnknownAttribute(StunMessage* response); |
| 70 | |
| 71 | TurnPort* port_; |
| 72 | }; |
| 73 | |
| 74 | class TurnRefreshRequest : public StunRequest { |
| 75 | public: |
| 76 | explicit TurnRefreshRequest(TurnPort* port); |
| 77 | virtual void Prepare(StunMessage* request); |
| 78 | virtual void OnResponse(StunMessage* response); |
| 79 | virtual void OnErrorResponse(StunMessage* response); |
| 80 | virtual void OnTimeout(); |
pthatcher@webrtc.org | fe672e3 | 2015-01-17 00:58:15 +0000 | [diff] [blame^] | 81 | void set_lifetime(int lifetime) { lifetime_ = lifetime; } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 82 | |
| 83 | private: |
| 84 | TurnPort* port_; |
pthatcher@webrtc.org | fe672e3 | 2015-01-17 00:58:15 +0000 | [diff] [blame^] | 85 | int lifetime_; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | class TurnCreatePermissionRequest : public StunRequest, |
| 89 | public sigslot::has_slots<> { |
| 90 | public: |
| 91 | TurnCreatePermissionRequest(TurnPort* port, TurnEntry* entry, |
| 92 | const rtc::SocketAddress& ext_addr); |
| 93 | virtual void Prepare(StunMessage* request); |
| 94 | virtual void OnResponse(StunMessage* response); |
| 95 | virtual void OnErrorResponse(StunMessage* response); |
| 96 | virtual void OnTimeout(); |
| 97 | |
| 98 | private: |
| 99 | void OnEntryDestroyed(TurnEntry* entry); |
| 100 | |
| 101 | TurnPort* port_; |
| 102 | TurnEntry* entry_; |
| 103 | rtc::SocketAddress ext_addr_; |
| 104 | }; |
| 105 | |
| 106 | class TurnChannelBindRequest : public StunRequest, |
| 107 | public sigslot::has_slots<> { |
| 108 | public: |
| 109 | TurnChannelBindRequest(TurnPort* port, TurnEntry* entry, int channel_id, |
| 110 | const rtc::SocketAddress& ext_addr); |
| 111 | virtual void Prepare(StunMessage* request); |
| 112 | virtual void OnResponse(StunMessage* response); |
| 113 | virtual void OnErrorResponse(StunMessage* response); |
| 114 | virtual void OnTimeout(); |
| 115 | |
| 116 | private: |
| 117 | void OnEntryDestroyed(TurnEntry* entry); |
| 118 | |
| 119 | TurnPort* port_; |
| 120 | TurnEntry* entry_; |
| 121 | int channel_id_; |
| 122 | rtc::SocketAddress ext_addr_; |
| 123 | }; |
| 124 | |
| 125 | // Manages a "connection" to a remote destination. We will attempt to bring up |
| 126 | // a channel for this remote destination to reduce the overhead of sending data. |
| 127 | class TurnEntry : public sigslot::has_slots<> { |
| 128 | public: |
| 129 | enum BindState { STATE_UNBOUND, STATE_BINDING, STATE_BOUND }; |
| 130 | TurnEntry(TurnPort* port, int channel_id, |
| 131 | const rtc::SocketAddress& ext_addr); |
| 132 | |
| 133 | TurnPort* port() { return port_; } |
| 134 | |
| 135 | int channel_id() const { return channel_id_; } |
| 136 | const rtc::SocketAddress& address() const { return ext_addr_; } |
| 137 | BindState state() const { return state_; } |
| 138 | |
| 139 | // Helper methods to send permission and channel bind requests. |
| 140 | void SendCreatePermissionRequest(); |
| 141 | void SendChannelBindRequest(int delay); |
| 142 | // Sends a packet to the given destination address. |
| 143 | // This will wrap the packet in STUN if necessary. |
| 144 | int Send(const void* data, size_t size, bool payload, |
| 145 | const rtc::PacketOptions& options); |
| 146 | |
| 147 | void OnCreatePermissionSuccess(); |
| 148 | void OnCreatePermissionError(StunMessage* response, int code); |
| 149 | void OnChannelBindSuccess(); |
| 150 | void OnChannelBindError(StunMessage* response, int code); |
| 151 | // Signal sent when TurnEntry is destroyed. |
| 152 | sigslot::signal1<TurnEntry*> SignalDestroyed; |
| 153 | |
| 154 | private: |
| 155 | TurnPort* port_; |
| 156 | int channel_id_; |
| 157 | rtc::SocketAddress ext_addr_; |
| 158 | BindState state_; |
| 159 | }; |
| 160 | |
| 161 | TurnPort::TurnPort(rtc::Thread* thread, |
| 162 | rtc::PacketSocketFactory* factory, |
| 163 | rtc::Network* network, |
| 164 | rtc::AsyncPacketSocket* socket, |
| 165 | const std::string& username, |
| 166 | const std::string& password, |
| 167 | const ProtocolAddress& server_address, |
| 168 | const RelayCredentials& credentials, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 169 | int server_priority, |
| 170 | const std::string& origin) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 171 | : Port(thread, factory, network, socket->GetLocalAddress().ipaddr(), |
| 172 | username, password), |
| 173 | server_address_(server_address), |
| 174 | credentials_(credentials), |
| 175 | socket_(socket), |
| 176 | resolver_(NULL), |
| 177 | error_(0), |
| 178 | request_manager_(thread), |
| 179 | next_channel_number_(TURN_CHANNEL_NUMBER_START), |
| 180 | connected_(false), |
| 181 | server_priority_(server_priority), |
| 182 | allocate_mismatch_retries_(0) { |
| 183 | request_manager_.SignalSendPacket.connect(this, &TurnPort::OnSendStunPacket); |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 184 | request_manager_.set_origin(origin); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | TurnPort::TurnPort(rtc::Thread* thread, |
| 188 | rtc::PacketSocketFactory* factory, |
| 189 | rtc::Network* network, |
| 190 | const rtc::IPAddress& ip, |
pkasting@chromium.org | 332331f | 2014-11-06 20:19:22 +0000 | [diff] [blame] | 191 | uint16 min_port, |
| 192 | uint16 max_port, |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 193 | const std::string& username, |
| 194 | const std::string& password, |
| 195 | const ProtocolAddress& server_address, |
| 196 | const RelayCredentials& credentials, |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 197 | int server_priority, |
| 198 | const std::string& origin) |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 199 | : Port(thread, RELAY_PORT_TYPE, factory, network, ip, min_port, max_port, |
| 200 | username, password), |
| 201 | server_address_(server_address), |
| 202 | credentials_(credentials), |
| 203 | socket_(NULL), |
| 204 | resolver_(NULL), |
| 205 | error_(0), |
| 206 | request_manager_(thread), |
| 207 | next_channel_number_(TURN_CHANNEL_NUMBER_START), |
| 208 | connected_(false), |
| 209 | server_priority_(server_priority), |
| 210 | allocate_mismatch_retries_(0) { |
| 211 | request_manager_.SignalSendPacket.connect(this, &TurnPort::OnSendStunPacket); |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 212 | request_manager_.set_origin(origin); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | TurnPort::~TurnPort() { |
| 216 | // TODO(juberti): Should this even be necessary? |
pthatcher@webrtc.org | fe672e3 | 2015-01-17 00:58:15 +0000 | [diff] [blame^] | 217 | |
| 218 | // release the allocation by sending a refresh with |
| 219 | // lifetime 0. |
| 220 | if (connected_) { |
| 221 | TurnRefreshRequest bye(this); |
| 222 | bye.set_lifetime(0); |
| 223 | SendRequest(&bye, 0); |
| 224 | } |
| 225 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 226 | while (!entries_.empty()) { |
| 227 | DestroyEntry(entries_.front()->address()); |
| 228 | } |
| 229 | if (resolver_) { |
| 230 | resolver_->Destroy(false); |
| 231 | } |
| 232 | if (!SharedSocket()) { |
| 233 | delete socket_; |
| 234 | } |
| 235 | } |
| 236 | |
pthatcher@webrtc.org | 0ba1533 | 2015-01-10 00:47:02 +0000 | [diff] [blame] | 237 | rtc::SocketAddress TurnPort::GetLocalAddress() const { |
| 238 | return socket_ ? socket_->GetLocalAddress() : rtc::SocketAddress(); |
| 239 | } |
| 240 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 241 | void TurnPort::PrepareAddress() { |
| 242 | if (credentials_.username.empty() || |
| 243 | credentials_.password.empty()) { |
| 244 | LOG(LS_ERROR) << "Allocation can't be started without setting the" |
| 245 | << " TURN server credentials for the user."; |
| 246 | OnAllocateError(); |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | if (!server_address_.address.port()) { |
| 251 | // We will set default TURN port, if no port is set in the address. |
| 252 | server_address_.address.SetPort(TURN_DEFAULT_PORT); |
| 253 | } |
| 254 | |
| 255 | if (server_address_.address.IsUnresolved()) { |
| 256 | ResolveTurnAddress(server_address_.address); |
| 257 | } else { |
| 258 | // If protocol family of server address doesn't match with local, return. |
| 259 | if (!IsCompatibleAddress(server_address_.address)) { |
| 260 | LOG(LS_ERROR) << "Server IP address family does not match with " |
| 261 | << "local host address family type"; |
| 262 | OnAllocateError(); |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | // Insert the current address to prevent redirection pingpong. |
| 267 | attempted_server_addresses_.insert(server_address_.address); |
| 268 | |
| 269 | LOG_J(LS_INFO, this) << "Trying to connect to TURN server via " |
| 270 | << ProtoToString(server_address_.proto) << " @ " |
| 271 | << server_address_.address.ToSensitiveString(); |
| 272 | if (!CreateTurnClientSocket()) { |
| 273 | OnAllocateError(); |
| 274 | } else if (server_address_.proto == PROTO_UDP) { |
| 275 | // If its UDP, send AllocateRequest now. |
| 276 | // For TCP and TLS AllcateRequest will be sent by OnSocketConnect. |
| 277 | SendRequest(new TurnAllocateRequest(this), 0); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | bool TurnPort::CreateTurnClientSocket() { |
| 283 | ASSERT(!socket_ || SharedSocket()); |
| 284 | |
| 285 | if (server_address_.proto == PROTO_UDP && !SharedSocket()) { |
| 286 | socket_ = socket_factory()->CreateUdpSocket( |
| 287 | rtc::SocketAddress(ip(), 0), min_port(), max_port()); |
| 288 | } else if (server_address_.proto == PROTO_TCP) { |
| 289 | ASSERT(!SharedSocket()); |
| 290 | int opts = rtc::PacketSocketFactory::OPT_STUN; |
| 291 | // If secure bit is enabled in server address, use TLS over TCP. |
| 292 | if (server_address_.secure) { |
| 293 | opts |= rtc::PacketSocketFactory::OPT_TLS; |
| 294 | } |
| 295 | socket_ = socket_factory()->CreateClientTcpSocket( |
| 296 | rtc::SocketAddress(ip(), 0), server_address_.address, |
| 297 | proxy(), user_agent(), opts); |
| 298 | } |
| 299 | |
| 300 | if (!socket_) { |
| 301 | error_ = SOCKET_ERROR; |
| 302 | return false; |
| 303 | } |
| 304 | |
| 305 | // Apply options if any. |
| 306 | for (SocketOptionsMap::iterator iter = socket_options_.begin(); |
| 307 | iter != socket_options_.end(); ++iter) { |
| 308 | socket_->SetOption(iter->first, iter->second); |
| 309 | } |
| 310 | |
| 311 | if (!SharedSocket()) { |
| 312 | // If socket is shared, AllocationSequence will receive the packet. |
| 313 | socket_->SignalReadPacket.connect(this, &TurnPort::OnReadPacket); |
| 314 | } |
| 315 | |
| 316 | socket_->SignalReadyToSend.connect(this, &TurnPort::OnReadyToSend); |
| 317 | |
| 318 | if (server_address_.proto == PROTO_TCP) { |
| 319 | socket_->SignalConnect.connect(this, &TurnPort::OnSocketConnect); |
| 320 | socket_->SignalClose.connect(this, &TurnPort::OnSocketClose); |
| 321 | } |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | void TurnPort::OnSocketConnect(rtc::AsyncPacketSocket* socket) { |
| 326 | ASSERT(server_address_.proto == PROTO_TCP); |
| 327 | // Do not use this port if the socket bound to a different address than |
| 328 | // the one we asked for. This is seen in Chrome, where TCP sockets cannot be |
| 329 | // given a binding address, and the platform is expected to pick the |
| 330 | // correct local address. |
guoweis@webrtc.org | 4fba293 | 2014-12-18 04:45:05 +0000 | [diff] [blame] | 331 | |
| 332 | // Further, to workaround issue 3927 in which a proxy is forcing TCP bound to |
| 333 | // localhost only, we're allowing Loopback IP even if it's not the same as the |
| 334 | // local Turn port. |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 335 | if (socket->GetLocalAddress().ipaddr() != ip()) { |
guoweis@webrtc.org | 4fba293 | 2014-12-18 04:45:05 +0000 | [diff] [blame] | 336 | if (socket->GetLocalAddress().IsLoopbackIP()) { |
| 337 | LOG(LS_WARNING) << "Socket is bound to a different address:" |
| 338 | << socket->GetLocalAddress().ipaddr().ToString() |
| 339 | << ", rather then the local port:" << ip().ToString() |
| 340 | << ". Still allowing it since it's localhost."; |
| 341 | } else { |
| 342 | LOG(LS_WARNING) << "Socket is bound to a different address:" |
| 343 | << socket->GetLocalAddress().ipaddr().ToString() |
| 344 | << ", rather then the local port:" << ip().ToString() |
| 345 | << ". Discarding TURN port."; |
| 346 | OnAllocateError(); |
| 347 | return; |
| 348 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | if (server_address_.address.IsUnresolved()) { |
| 352 | server_address_.address = socket_->GetRemoteAddress(); |
| 353 | } |
| 354 | |
| 355 | LOG(LS_INFO) << "TurnPort connected to " << socket->GetRemoteAddress() |
| 356 | << " using tcp."; |
| 357 | SendRequest(new TurnAllocateRequest(this), 0); |
| 358 | } |
| 359 | |
| 360 | void TurnPort::OnSocketClose(rtc::AsyncPacketSocket* socket, int error) { |
| 361 | LOG_J(LS_WARNING, this) << "Connection with server failed, error=" << error; |
guoweis@webrtc.org | 19e4e8d | 2015-01-10 02:41:32 +0000 | [diff] [blame] | 362 | ASSERT(socket == socket_); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 363 | if (!connected_) { |
| 364 | OnAllocateError(); |
| 365 | } |
pthatcher@webrtc.org | fe672e3 | 2015-01-17 00:58:15 +0000 | [diff] [blame^] | 366 | connected_ = false; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void TurnPort::OnAllocateMismatch() { |
| 370 | if (allocate_mismatch_retries_ >= MAX_ALLOCATE_MISMATCH_RETRIES) { |
| 371 | LOG_J(LS_WARNING, this) << "Giving up on the port after " |
| 372 | << allocate_mismatch_retries_ |
| 373 | << " retries for STUN_ERROR_ALLOCATION_MISMATCH"; |
| 374 | OnAllocateError(); |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | LOG_J(LS_INFO, this) << "Allocating a new socket after " |
| 379 | << "STUN_ERROR_ALLOCATION_MISMATCH, retry = " |
| 380 | << allocate_mismatch_retries_ + 1; |
| 381 | if (SharedSocket()) { |
| 382 | ResetSharedSocket(); |
| 383 | } else { |
| 384 | delete socket_; |
| 385 | } |
| 386 | socket_ = NULL; |
| 387 | |
| 388 | PrepareAddress(); |
| 389 | ++allocate_mismatch_retries_; |
| 390 | } |
| 391 | |
| 392 | Connection* TurnPort::CreateConnection(const Candidate& address, |
| 393 | CandidateOrigin origin) { |
| 394 | // TURN-UDP can only connect to UDP candidates. |
| 395 | if (address.protocol() != UDP_PROTOCOL_NAME) { |
| 396 | return NULL; |
| 397 | } |
| 398 | |
| 399 | if (!IsCompatibleAddress(address.address())) { |
| 400 | return NULL; |
| 401 | } |
| 402 | |
| 403 | // Create an entry, if needed, so we can get our permissions set up correctly. |
| 404 | CreateEntry(address.address()); |
| 405 | |
| 406 | // A TURN port will have two candiates, STUN and TURN. STUN may not |
| 407 | // present in all cases. If present stun candidate will be added first |
| 408 | // and TURN candidate later. |
| 409 | for (size_t index = 0; index < Candidates().size(); ++index) { |
| 410 | if (Candidates()[index].type() == RELAY_PORT_TYPE) { |
| 411 | ProxyConnection* conn = new ProxyConnection(this, index, address); |
| 412 | conn->SignalDestroyed.connect(this, &TurnPort::OnConnectionDestroyed); |
| 413 | AddConnection(conn); |
| 414 | return conn; |
| 415 | } |
| 416 | } |
| 417 | return NULL; |
| 418 | } |
| 419 | |
| 420 | int TurnPort::SetOption(rtc::Socket::Option opt, int value) { |
| 421 | if (!socket_) { |
| 422 | // If socket is not created yet, these options will be applied during socket |
| 423 | // creation. |
| 424 | socket_options_[opt] = value; |
| 425 | return 0; |
| 426 | } |
| 427 | return socket_->SetOption(opt, value); |
| 428 | } |
| 429 | |
| 430 | int TurnPort::GetOption(rtc::Socket::Option opt, int* value) { |
| 431 | if (!socket_) { |
| 432 | SocketOptionsMap::const_iterator it = socket_options_.find(opt); |
| 433 | if (it == socket_options_.end()) { |
| 434 | return -1; |
| 435 | } |
| 436 | *value = it->second; |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | return socket_->GetOption(opt, value); |
| 441 | } |
| 442 | |
| 443 | int TurnPort::GetError() { |
| 444 | return error_; |
| 445 | } |
| 446 | |
| 447 | int TurnPort::SendTo(const void* data, size_t size, |
| 448 | const rtc::SocketAddress& addr, |
| 449 | const rtc::PacketOptions& options, |
| 450 | bool payload) { |
| 451 | // Try to find an entry for this specific address; we should have one. |
| 452 | TurnEntry* entry = FindEntry(addr); |
| 453 | ASSERT(entry != NULL); |
| 454 | if (!entry) { |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | if (!connected()) { |
| 459 | error_ = EWOULDBLOCK; |
| 460 | return SOCKET_ERROR; |
| 461 | } |
| 462 | |
| 463 | // Send the actual contents to the server using the usual mechanism. |
| 464 | int sent = entry->Send(data, size, payload, options); |
| 465 | if (sent <= 0) { |
| 466 | return SOCKET_ERROR; |
| 467 | } |
| 468 | |
| 469 | // The caller of the function is expecting the number of user data bytes, |
| 470 | // rather than the size of the packet. |
| 471 | return static_cast<int>(size); |
| 472 | } |
| 473 | |
| 474 | void TurnPort::OnReadPacket( |
| 475 | rtc::AsyncPacketSocket* socket, const char* data, size_t size, |
| 476 | const rtc::SocketAddress& remote_addr, |
| 477 | const rtc::PacketTime& packet_time) { |
| 478 | ASSERT(socket == socket_); |
guoweis@webrtc.org | c51fb93 | 2014-12-18 00:30:55 +0000 | [diff] [blame] | 479 | |
| 480 | // This is to guard against a STUN response from previous server after |
| 481 | // alternative server redirection. TODO(guoweis): add a unit test for this |
| 482 | // race condition. |
| 483 | if (remote_addr != server_address_.address) { |
| 484 | LOG_J(LS_WARNING, this) << "Discarding TURN message from unknown address:" |
| 485 | << remote_addr.ToString() |
| 486 | << ", server_address_:" |
| 487 | << server_address_.address.ToString(); |
| 488 | return; |
| 489 | } |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 490 | |
| 491 | // The message must be at least the size of a channel header. |
| 492 | if (size < TURN_CHANNEL_HEADER_SIZE) { |
| 493 | LOG_J(LS_WARNING, this) << "Received TURN message that was too short"; |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | // Check the message type, to see if is a Channel Data message. |
| 498 | // The message will either be channel data, a TURN data indication, or |
| 499 | // a response to a previous request. |
| 500 | uint16 msg_type = rtc::GetBE16(data); |
| 501 | if (IsTurnChannelData(msg_type)) { |
| 502 | HandleChannelData(msg_type, data, size, packet_time); |
| 503 | } else if (msg_type == TURN_DATA_INDICATION) { |
| 504 | HandleDataIndication(data, size, packet_time); |
| 505 | } else { |
jiayl@webrtc.org | 511f8a8 | 2014-12-03 02:17:07 +0000 | [diff] [blame] | 506 | if (SharedSocket() && |
| 507 | (msg_type == STUN_BINDING_RESPONSE || |
| 508 | msg_type == STUN_BINDING_ERROR_RESPONSE)) { |
| 509 | LOG_J(LS_VERBOSE, this) << |
| 510 | "Ignoring STUN binding response message on shared socket."; |
| 511 | return; |
| 512 | } |
| 513 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 514 | // This must be a response for one of our requests. |
| 515 | // Check success responses, but not errors, for MESSAGE-INTEGRITY. |
| 516 | if (IsStunSuccessResponseType(msg_type) && |
| 517 | !StunMessage::ValidateMessageIntegrity(data, size, hash())) { |
| 518 | LOG_J(LS_WARNING, this) << "Received TURN message with invalid " |
| 519 | << "message integrity, msg_type=" << msg_type; |
| 520 | return; |
| 521 | } |
| 522 | request_manager_.CheckResponse(data, size); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | void TurnPort::OnReadyToSend(rtc::AsyncPacketSocket* socket) { |
| 527 | if (connected_) { |
| 528 | Port::OnReadyToSend(); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | |
| 533 | // Update current server address port with the alternate server address port. |
| 534 | bool TurnPort::SetAlternateServer(const rtc::SocketAddress& address) { |
| 535 | // Check if we have seen this address before and reject if we did. |
| 536 | AttemptedServerSet::iterator iter = attempted_server_addresses_.find(address); |
| 537 | if (iter != attempted_server_addresses_.end()) { |
| 538 | LOG_J(LS_WARNING, this) << "Redirection to [" |
| 539 | << address.ToSensitiveString() |
| 540 | << "] ignored, allocation failed."; |
| 541 | return false; |
| 542 | } |
| 543 | |
| 544 | // If protocol family of server address doesn't match with local, return. |
| 545 | if (!IsCompatibleAddress(address)) { |
| 546 | LOG(LS_WARNING) << "Server IP address family does not match with " |
| 547 | << "local host address family type"; |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | LOG_J(LS_INFO, this) << "Redirecting from TURN server [" |
| 552 | << server_address_.address.ToSensitiveString() |
| 553 | << "] to TURN server [" |
| 554 | << address.ToSensitiveString() |
| 555 | << "]"; |
| 556 | server_address_ = ProtocolAddress(address, server_address_.proto, |
| 557 | server_address_.secure); |
| 558 | |
| 559 | // Insert the current address to prevent redirection pingpong. |
| 560 | attempted_server_addresses_.insert(server_address_.address); |
| 561 | return true; |
| 562 | } |
| 563 | |
| 564 | void TurnPort::ResolveTurnAddress(const rtc::SocketAddress& address) { |
| 565 | if (resolver_) |
| 566 | return; |
| 567 | |
| 568 | resolver_ = socket_factory()->CreateAsyncResolver(); |
| 569 | resolver_->SignalDone.connect(this, &TurnPort::OnResolveResult); |
| 570 | resolver_->Start(address); |
| 571 | } |
| 572 | |
| 573 | void TurnPort::OnResolveResult(rtc::AsyncResolverInterface* resolver) { |
| 574 | ASSERT(resolver == resolver_); |
| 575 | // If DNS resolve is failed when trying to connect to the server using TCP, |
| 576 | // one of the reason could be due to DNS queries blocked by firewall. |
| 577 | // In such cases we will try to connect to the server with hostname, assuming |
| 578 | // socket layer will resolve the hostname through a HTTP proxy (if any). |
| 579 | if (resolver_->GetError() != 0 && server_address_.proto == PROTO_TCP) { |
| 580 | if (!CreateTurnClientSocket()) { |
| 581 | OnAllocateError(); |
| 582 | } |
| 583 | return; |
| 584 | } |
| 585 | |
| 586 | // Copy the original server address in |resolved_address|. For TLS based |
| 587 | // sockets we need hostname along with resolved address. |
| 588 | rtc::SocketAddress resolved_address = server_address_.address; |
| 589 | if (resolver_->GetError() != 0 || |
| 590 | !resolver_->GetResolvedAddress(ip().family(), &resolved_address)) { |
| 591 | LOG_J(LS_WARNING, this) << "TURN host lookup received error " |
| 592 | << resolver_->GetError(); |
| 593 | error_ = resolver_->GetError(); |
| 594 | OnAllocateError(); |
| 595 | return; |
| 596 | } |
| 597 | // Signal needs both resolved and unresolved address. After signal is sent |
| 598 | // we can copy resolved address back into |server_address_|. |
| 599 | SignalResolvedServerAddress(this, server_address_.address, |
| 600 | resolved_address); |
| 601 | server_address_.address = resolved_address; |
| 602 | PrepareAddress(); |
| 603 | } |
| 604 | |
| 605 | void TurnPort::OnSendStunPacket(const void* data, size_t size, |
| 606 | StunRequest* request) { |
| 607 | rtc::PacketOptions options(DefaultDscpValue()); |
| 608 | if (Send(data, size, options) < 0) { |
| 609 | LOG_J(LS_ERROR, this) << "Failed to send TURN message, err=" |
| 610 | << socket_->GetError(); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | void TurnPort::OnStunAddress(const rtc::SocketAddress& address) { |
| 615 | // STUN Port will discover STUN candidate, as it's supplied with first TURN |
| 616 | // server address. |
| 617 | // Why not using this address? - P2PTransportChannel will start creating |
| 618 | // connections after first candidate, which means it could start creating the |
| 619 | // connections before TURN candidate added. For that to handle, we need to |
| 620 | // supply STUN candidate from this port to UDPPort, and TurnPort should have |
| 621 | // handle to UDPPort to pass back the address. |
| 622 | } |
| 623 | |
| 624 | void TurnPort::OnAllocateSuccess(const rtc::SocketAddress& address, |
| 625 | const rtc::SocketAddress& stun_address) { |
| 626 | connected_ = true; |
| 627 | |
| 628 | rtc::SocketAddress related_address = stun_address; |
| 629 | if (!(candidate_filter() & CF_REFLEXIVE)) { |
| 630 | // If candidate filter only allows relay type of address, empty raddr to |
| 631 | // avoid local address leakage. |
| 632 | related_address = rtc::EmptySocketAddressWithFamily(stun_address.family()); |
| 633 | } |
| 634 | |
| 635 | // For relayed candidate, Base is the candidate itself. |
| 636 | AddAddress(address, // Candidate address. |
| 637 | address, // Base address. |
| 638 | related_address, // Related address. |
| 639 | UDP_PROTOCOL_NAME, |
| 640 | "", // TCP canddiate type, empty for turn candidates. |
| 641 | RELAY_PORT_TYPE, |
| 642 | GetRelayPreference(server_address_.proto, server_address_.secure), |
| 643 | server_priority_, |
| 644 | true); |
| 645 | } |
| 646 | |
| 647 | void TurnPort::OnAllocateError() { |
| 648 | // We will send SignalPortError asynchronously as this can be sent during |
| 649 | // port initialization. This way it will not be blocking other port |
| 650 | // creation. |
| 651 | thread()->Post(this, MSG_ERROR); |
| 652 | } |
| 653 | |
| 654 | void TurnPort::OnMessage(rtc::Message* message) { |
| 655 | if (message->message_id == MSG_ERROR) { |
| 656 | SignalPortError(this); |
| 657 | return; |
| 658 | } else if (message->message_id == MSG_ALLOCATE_MISMATCH) { |
| 659 | OnAllocateMismatch(); |
| 660 | return; |
guoweis@webrtc.org | 19e4e8d | 2015-01-10 02:41:32 +0000 | [diff] [blame] | 661 | } else if (message->message_id == MSG_TRY_ALTERNATE_SERVER) { |
| 662 | if (server_address().proto == PROTO_UDP) { |
| 663 | // Send another allocate request to alternate server, with the received |
| 664 | // realm and nonce values. |
| 665 | SendRequest(new TurnAllocateRequest(this), 0); |
| 666 | } else { |
| 667 | // Since it's TCP, we have to delete the connected socket and reconnect |
| 668 | // with the alternate server. PrepareAddress will send stun binding once |
| 669 | // the new socket is connected. |
| 670 | ASSERT(server_address().proto == PROTO_TCP); |
| 671 | ASSERT(!SharedSocket()); |
| 672 | delete socket_; |
| 673 | socket_ = NULL; |
| 674 | PrepareAddress(); |
| 675 | } |
| 676 | return; |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | Port::OnMessage(message); |
| 680 | } |
| 681 | |
| 682 | void TurnPort::OnAllocateRequestTimeout() { |
| 683 | OnAllocateError(); |
| 684 | } |
| 685 | |
| 686 | void TurnPort::HandleDataIndication(const char* data, size_t size, |
| 687 | const rtc::PacketTime& packet_time) { |
| 688 | // Read in the message, and process according to RFC5766, Section 10.4. |
| 689 | rtc::ByteBuffer buf(data, size); |
| 690 | TurnMessage msg; |
| 691 | if (!msg.Read(&buf)) { |
| 692 | LOG_J(LS_WARNING, this) << "Received invalid TURN data indication"; |
| 693 | return; |
| 694 | } |
| 695 | |
| 696 | // Check mandatory attributes. |
| 697 | const StunAddressAttribute* addr_attr = |
| 698 | msg.GetAddress(STUN_ATTR_XOR_PEER_ADDRESS); |
| 699 | if (!addr_attr) { |
| 700 | LOG_J(LS_WARNING, this) << "Missing STUN_ATTR_XOR_PEER_ADDRESS attribute " |
| 701 | << "in data indication."; |
| 702 | return; |
| 703 | } |
| 704 | |
| 705 | const StunByteStringAttribute* data_attr = |
| 706 | msg.GetByteString(STUN_ATTR_DATA); |
| 707 | if (!data_attr) { |
| 708 | LOG_J(LS_WARNING, this) << "Missing STUN_ATTR_DATA attribute in " |
| 709 | << "data indication."; |
| 710 | return; |
| 711 | } |
| 712 | |
| 713 | // Verify that the data came from somewhere we think we have a permission for. |
| 714 | rtc::SocketAddress ext_addr(addr_attr->GetAddress()); |
| 715 | if (!HasPermission(ext_addr.ipaddr())) { |
| 716 | LOG_J(LS_WARNING, this) << "Received TURN data indication with invalid " |
| 717 | << "peer address, addr=" |
| 718 | << ext_addr.ToSensitiveString(); |
| 719 | return; |
| 720 | } |
| 721 | |
| 722 | DispatchPacket(data_attr->bytes(), data_attr->length(), ext_addr, |
| 723 | PROTO_UDP, packet_time); |
| 724 | } |
| 725 | |
| 726 | void TurnPort::HandleChannelData(int channel_id, const char* data, |
| 727 | size_t size, |
| 728 | const rtc::PacketTime& packet_time) { |
| 729 | // Read the message, and process according to RFC5766, Section 11.6. |
| 730 | // 0 1 2 3 |
| 731 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 732 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 733 | // | Channel Number | Length | |
| 734 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 735 | // | | |
| 736 | // / Application Data / |
| 737 | // / / |
| 738 | // | | |
| 739 | // | +-------------------------------+ |
| 740 | // | | |
| 741 | // +-------------------------------+ |
| 742 | |
| 743 | // Extract header fields from the message. |
| 744 | uint16 len = rtc::GetBE16(data + 2); |
| 745 | if (len > size - TURN_CHANNEL_HEADER_SIZE) { |
| 746 | LOG_J(LS_WARNING, this) << "Received TURN channel data message with " |
| 747 | << "incorrect length, len=" << len; |
| 748 | return; |
| 749 | } |
| 750 | // Allowing messages larger than |len|, as ChannelData can be padded. |
| 751 | |
| 752 | TurnEntry* entry = FindEntry(channel_id); |
| 753 | if (!entry) { |
| 754 | LOG_J(LS_WARNING, this) << "Received TURN channel data message for invalid " |
| 755 | << "channel, channel_id=" << channel_id; |
| 756 | return; |
| 757 | } |
| 758 | |
| 759 | DispatchPacket(data + TURN_CHANNEL_HEADER_SIZE, len, entry->address(), |
| 760 | PROTO_UDP, packet_time); |
| 761 | } |
| 762 | |
| 763 | void TurnPort::DispatchPacket(const char* data, size_t size, |
| 764 | const rtc::SocketAddress& remote_addr, |
| 765 | ProtocolType proto, const rtc::PacketTime& packet_time) { |
| 766 | if (Connection* conn = GetConnection(remote_addr)) { |
| 767 | conn->OnReadPacket(data, size, packet_time); |
| 768 | } else { |
| 769 | Port::OnReadPacket(data, size, remote_addr, proto); |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | bool TurnPort::ScheduleRefresh(int lifetime) { |
| 774 | // Lifetime is in seconds; we schedule a refresh for one minute less. |
| 775 | if (lifetime < 2 * 60) { |
| 776 | LOG_J(LS_WARNING, this) << "Received response with lifetime that was " |
| 777 | << "too short, lifetime=" << lifetime; |
| 778 | return false; |
| 779 | } |
| 780 | |
| 781 | SendRequest(new TurnRefreshRequest(this), (lifetime - 60) * 1000); |
| 782 | return true; |
| 783 | } |
| 784 | |
| 785 | void TurnPort::SendRequest(StunRequest* req, int delay) { |
| 786 | request_manager_.SendDelayed(req, delay); |
| 787 | } |
| 788 | |
| 789 | void TurnPort::AddRequestAuthInfo(StunMessage* msg) { |
| 790 | // If we've gotten the necessary data from the server, add it to our request. |
| 791 | VERIFY(!hash_.empty()); |
| 792 | VERIFY(msg->AddAttribute(new StunByteStringAttribute( |
| 793 | STUN_ATTR_USERNAME, credentials_.username))); |
| 794 | VERIFY(msg->AddAttribute(new StunByteStringAttribute( |
| 795 | STUN_ATTR_REALM, realm_))); |
| 796 | VERIFY(msg->AddAttribute(new StunByteStringAttribute( |
| 797 | STUN_ATTR_NONCE, nonce_))); |
| 798 | VERIFY(msg->AddMessageIntegrity(hash())); |
| 799 | } |
| 800 | |
| 801 | int TurnPort::Send(const void* data, size_t len, |
| 802 | const rtc::PacketOptions& options) { |
| 803 | return socket_->SendTo(data, len, server_address_.address, options); |
| 804 | } |
| 805 | |
| 806 | void TurnPort::UpdateHash() { |
| 807 | VERIFY(ComputeStunCredentialHash(credentials_.username, realm_, |
| 808 | credentials_.password, &hash_)); |
| 809 | } |
| 810 | |
| 811 | bool TurnPort::UpdateNonce(StunMessage* response) { |
| 812 | // When stale nonce error received, we should update |
| 813 | // hash and store realm and nonce. |
| 814 | // Check the mandatory attributes. |
| 815 | const StunByteStringAttribute* realm_attr = |
| 816 | response->GetByteString(STUN_ATTR_REALM); |
| 817 | if (!realm_attr) { |
| 818 | LOG(LS_ERROR) << "Missing STUN_ATTR_REALM attribute in " |
| 819 | << "stale nonce error response."; |
| 820 | return false; |
| 821 | } |
| 822 | set_realm(realm_attr->GetString()); |
| 823 | |
| 824 | const StunByteStringAttribute* nonce_attr = |
| 825 | response->GetByteString(STUN_ATTR_NONCE); |
| 826 | if (!nonce_attr) { |
| 827 | LOG(LS_ERROR) << "Missing STUN_ATTR_NONCE attribute in " |
| 828 | << "stale nonce error response."; |
| 829 | return false; |
| 830 | } |
| 831 | set_nonce(nonce_attr->GetString()); |
| 832 | return true; |
| 833 | } |
| 834 | |
| 835 | static bool MatchesIP(TurnEntry* e, rtc::IPAddress ipaddr) { |
| 836 | return e->address().ipaddr() == ipaddr; |
| 837 | } |
| 838 | bool TurnPort::HasPermission(const rtc::IPAddress& ipaddr) const { |
| 839 | return (std::find_if(entries_.begin(), entries_.end(), |
| 840 | std::bind2nd(std::ptr_fun(MatchesIP), ipaddr)) != entries_.end()); |
| 841 | } |
| 842 | |
| 843 | static bool MatchesAddress(TurnEntry* e, rtc::SocketAddress addr) { |
| 844 | return e->address() == addr; |
| 845 | } |
| 846 | TurnEntry* TurnPort::FindEntry(const rtc::SocketAddress& addr) const { |
| 847 | EntryList::const_iterator it = std::find_if(entries_.begin(), entries_.end(), |
| 848 | std::bind2nd(std::ptr_fun(MatchesAddress), addr)); |
| 849 | return (it != entries_.end()) ? *it : NULL; |
| 850 | } |
| 851 | |
| 852 | static bool MatchesChannelId(TurnEntry* e, int id) { |
| 853 | return e->channel_id() == id; |
| 854 | } |
| 855 | TurnEntry* TurnPort::FindEntry(int channel_id) const { |
| 856 | EntryList::const_iterator it = std::find_if(entries_.begin(), entries_.end(), |
| 857 | std::bind2nd(std::ptr_fun(MatchesChannelId), channel_id)); |
| 858 | return (it != entries_.end()) ? *it : NULL; |
| 859 | } |
| 860 | |
| 861 | TurnEntry* TurnPort::CreateEntry(const rtc::SocketAddress& addr) { |
| 862 | ASSERT(FindEntry(addr) == NULL); |
| 863 | TurnEntry* entry = new TurnEntry(this, next_channel_number_++, addr); |
| 864 | entries_.push_back(entry); |
| 865 | return entry; |
| 866 | } |
| 867 | |
| 868 | void TurnPort::DestroyEntry(const rtc::SocketAddress& addr) { |
| 869 | TurnEntry* entry = FindEntry(addr); |
| 870 | ASSERT(entry != NULL); |
| 871 | entry->SignalDestroyed(entry); |
| 872 | entries_.remove(entry); |
| 873 | delete entry; |
| 874 | } |
| 875 | |
| 876 | void TurnPort::OnConnectionDestroyed(Connection* conn) { |
| 877 | // Destroying TurnEntry for the connection, which is already destroyed. |
| 878 | DestroyEntry(conn->remote_candidate().address()); |
| 879 | } |
| 880 | |
| 881 | TurnAllocateRequest::TurnAllocateRequest(TurnPort* port) |
| 882 | : StunRequest(new TurnMessage()), |
| 883 | port_(port) { |
| 884 | } |
| 885 | |
| 886 | void TurnAllocateRequest::Prepare(StunMessage* request) { |
| 887 | // Create the request as indicated in RFC 5766, Section 6.1. |
| 888 | request->SetType(TURN_ALLOCATE_REQUEST); |
| 889 | StunUInt32Attribute* transport_attr = StunAttribute::CreateUInt32( |
| 890 | STUN_ATTR_REQUESTED_TRANSPORT); |
| 891 | transport_attr->SetValue(IPPROTO_UDP << 24); |
| 892 | VERIFY(request->AddAttribute(transport_attr)); |
| 893 | if (!port_->hash().empty()) { |
| 894 | port_->AddRequestAuthInfo(request); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | void TurnAllocateRequest::OnResponse(StunMessage* response) { |
| 899 | // Check mandatory attributes as indicated in RFC5766, Section 6.3. |
| 900 | const StunAddressAttribute* mapped_attr = |
| 901 | response->GetAddress(STUN_ATTR_XOR_MAPPED_ADDRESS); |
| 902 | if (!mapped_attr) { |
| 903 | LOG_J(LS_WARNING, port_) << "Missing STUN_ATTR_XOR_MAPPED_ADDRESS " |
| 904 | << "attribute in allocate success response"; |
| 905 | return; |
| 906 | } |
| 907 | // Using XOR-Mapped-Address for stun. |
| 908 | port_->OnStunAddress(mapped_attr->GetAddress()); |
| 909 | |
| 910 | const StunAddressAttribute* relayed_attr = |
| 911 | response->GetAddress(STUN_ATTR_XOR_RELAYED_ADDRESS); |
| 912 | if (!relayed_attr) { |
| 913 | LOG_J(LS_WARNING, port_) << "Missing STUN_ATTR_XOR_RELAYED_ADDRESS " |
| 914 | << "attribute in allocate success response"; |
| 915 | return; |
| 916 | } |
| 917 | |
| 918 | const StunUInt32Attribute* lifetime_attr = |
| 919 | response->GetUInt32(STUN_ATTR_TURN_LIFETIME); |
| 920 | if (!lifetime_attr) { |
| 921 | LOG_J(LS_WARNING, port_) << "Missing STUN_ATTR_TURN_LIFETIME attribute in " |
| 922 | << "allocate success response"; |
| 923 | return; |
| 924 | } |
| 925 | // Notify the port the allocate succeeded, and schedule a refresh request. |
| 926 | port_->OnAllocateSuccess(relayed_attr->GetAddress(), |
| 927 | mapped_attr->GetAddress()); |
| 928 | port_->ScheduleRefresh(lifetime_attr->value()); |
| 929 | } |
| 930 | |
| 931 | void TurnAllocateRequest::OnErrorResponse(StunMessage* response) { |
| 932 | // Process error response according to RFC5766, Section 6.4. |
| 933 | const StunErrorCodeAttribute* error_code = response->GetErrorCode(); |
| 934 | switch (error_code->code()) { |
| 935 | case STUN_ERROR_UNAUTHORIZED: // Unauthrorized. |
| 936 | OnAuthChallenge(response, error_code->code()); |
| 937 | break; |
| 938 | case STUN_ERROR_TRY_ALTERNATE: |
| 939 | OnTryAlternate(response, error_code->code()); |
| 940 | break; |
| 941 | case STUN_ERROR_ALLOCATION_MISMATCH: |
| 942 | // We must handle this error async because trying to delete the socket in |
| 943 | // OnErrorResponse will cause a deadlock on the socket. |
| 944 | port_->thread()->Post(port_, TurnPort::MSG_ALLOCATE_MISMATCH); |
| 945 | break; |
| 946 | default: |
| 947 | LOG_J(LS_WARNING, port_) << "Allocate response error, code=" |
| 948 | << error_code->code(); |
| 949 | port_->OnAllocateError(); |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | void TurnAllocateRequest::OnTimeout() { |
| 954 | LOG_J(LS_WARNING, port_) << "Allocate request timeout"; |
| 955 | port_->OnAllocateRequestTimeout(); |
| 956 | } |
| 957 | |
| 958 | void TurnAllocateRequest::OnAuthChallenge(StunMessage* response, int code) { |
| 959 | // If we failed to authenticate even after we sent our credentials, fail hard. |
| 960 | if (code == STUN_ERROR_UNAUTHORIZED && !port_->hash().empty()) { |
| 961 | LOG_J(LS_WARNING, port_) << "Failed to authenticate with the server " |
| 962 | << "after challenge."; |
| 963 | port_->OnAllocateError(); |
| 964 | return; |
| 965 | } |
| 966 | |
| 967 | // Check the mandatory attributes. |
| 968 | const StunByteStringAttribute* realm_attr = |
| 969 | response->GetByteString(STUN_ATTR_REALM); |
| 970 | if (!realm_attr) { |
| 971 | LOG_J(LS_WARNING, port_) << "Missing STUN_ATTR_REALM attribute in " |
| 972 | << "allocate unauthorized response."; |
| 973 | return; |
| 974 | } |
| 975 | port_->set_realm(realm_attr->GetString()); |
| 976 | |
| 977 | const StunByteStringAttribute* nonce_attr = |
| 978 | response->GetByteString(STUN_ATTR_NONCE); |
| 979 | if (!nonce_attr) { |
| 980 | LOG_J(LS_WARNING, port_) << "Missing STUN_ATTR_NONCE attribute in " |
| 981 | << "allocate unauthorized response."; |
| 982 | return; |
| 983 | } |
| 984 | port_->set_nonce(nonce_attr->GetString()); |
| 985 | |
| 986 | // Send another allocate request, with the received realm and nonce values. |
| 987 | port_->SendRequest(new TurnAllocateRequest(port_), 0); |
| 988 | } |
| 989 | |
| 990 | void TurnAllocateRequest::OnTryAlternate(StunMessage* response, int code) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 991 | |
| 992 | // According to RFC 5389 section 11, there are use cases where |
| 993 | // authentication of response is not possible, we're not validating |
| 994 | // message integrity. |
| 995 | |
| 996 | // Get the alternate server address attribute value. |
| 997 | const StunAddressAttribute* alternate_server_attr = |
| 998 | response->GetAddress(STUN_ATTR_ALTERNATE_SERVER); |
| 999 | if (!alternate_server_attr) { |
| 1000 | LOG_J(LS_WARNING, port_) << "Missing STUN_ATTR_ALTERNATE_SERVER " |
| 1001 | << "attribute in try alternate error response"; |
| 1002 | port_->OnAllocateError(); |
| 1003 | return; |
| 1004 | } |
| 1005 | if (!port_->SetAlternateServer(alternate_server_attr->GetAddress())) { |
| 1006 | port_->OnAllocateError(); |
| 1007 | return; |
| 1008 | } |
| 1009 | |
| 1010 | // Check the attributes. |
| 1011 | const StunByteStringAttribute* realm_attr = |
| 1012 | response->GetByteString(STUN_ATTR_REALM); |
| 1013 | if (realm_attr) { |
| 1014 | LOG_J(LS_INFO, port_) << "Applying STUN_ATTR_REALM attribute in " |
| 1015 | << "try alternate error response."; |
| 1016 | port_->set_realm(realm_attr->GetString()); |
| 1017 | } |
| 1018 | |
| 1019 | const StunByteStringAttribute* nonce_attr = |
| 1020 | response->GetByteString(STUN_ATTR_NONCE); |
| 1021 | if (nonce_attr) { |
| 1022 | LOG_J(LS_INFO, port_) << "Applying STUN_ATTR_NONCE attribute in " |
| 1023 | << "try alternate error response."; |
| 1024 | port_->set_nonce(nonce_attr->GetString()); |
| 1025 | } |
| 1026 | |
guoweis@webrtc.org | 19e4e8d | 2015-01-10 02:41:32 +0000 | [diff] [blame] | 1027 | // For TCP, we can't close the original Tcp socket during handling a 300 as |
| 1028 | // we're still inside that socket's event handler. Doing so will cause |
| 1029 | // deadlock. |
| 1030 | port_->thread()->Post(port_, TurnPort::MSG_TRY_ALTERNATE_SERVER); |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | TurnRefreshRequest::TurnRefreshRequest(TurnPort* port) |
| 1034 | : StunRequest(new TurnMessage()), |
pthatcher@webrtc.org | fe672e3 | 2015-01-17 00:58:15 +0000 | [diff] [blame^] | 1035 | port_(port), |
| 1036 | lifetime_(-1) { |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | void TurnRefreshRequest::Prepare(StunMessage* request) { |
| 1040 | // Create the request as indicated in RFC 5766, Section 7.1. |
| 1041 | // No attributes need to be included. |
| 1042 | request->SetType(TURN_REFRESH_REQUEST); |
pthatcher@webrtc.org | fe672e3 | 2015-01-17 00:58:15 +0000 | [diff] [blame^] | 1043 | if (lifetime_ > -1) { |
| 1044 | VERIFY(request->AddAttribute(new StunUInt32Attribute( |
| 1045 | STUN_ATTR_LIFETIME, lifetime_))); |
| 1046 | } |
| 1047 | |
henrike@webrtc.org | 269fb4b | 2014-10-28 22:20:11 +0000 | [diff] [blame] | 1048 | port_->AddRequestAuthInfo(request); |
| 1049 | } |
| 1050 | |
| 1051 | void TurnRefreshRequest::OnResponse(StunMessage* response) { |
| 1052 | // Check mandatory attributes as indicated in RFC5766, Section 7.3. |
| 1053 | const StunUInt32Attribute* lifetime_attr = |
| 1054 | response->GetUInt32(STUN_ATTR_TURN_LIFETIME); |
| 1055 | if (!lifetime_attr) { |
| 1056 | LOG_J(LS_WARNING, port_) << "Missing STUN_ATTR_TURN_LIFETIME attribute in " |
| 1057 | << "refresh success response."; |
| 1058 | return; |
| 1059 | } |
| 1060 | |
| 1061 | // Schedule a refresh based on the returned lifetime value. |
| 1062 | port_->ScheduleRefresh(lifetime_attr->value()); |
| 1063 | } |
| 1064 | |
| 1065 | void TurnRefreshRequest::OnErrorResponse(StunMessage* response) { |
| 1066 | const StunErrorCodeAttribute* error_code = response->GetErrorCode(); |
| 1067 | LOG_J(LS_WARNING, port_) << "Refresh response error, code=" |
| 1068 | << error_code->code(); |
| 1069 | |
| 1070 | if (error_code->code() == STUN_ERROR_STALE_NONCE) { |
| 1071 | if (port_->UpdateNonce(response)) { |
| 1072 | // Send RefreshRequest immediately. |
| 1073 | port_->SendRequest(new TurnRefreshRequest(port_), 0); |
| 1074 | } |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | void TurnRefreshRequest::OnTimeout() { |
| 1079 | } |
| 1080 | |
| 1081 | TurnCreatePermissionRequest::TurnCreatePermissionRequest( |
| 1082 | TurnPort* port, TurnEntry* entry, |
| 1083 | const rtc::SocketAddress& ext_addr) |
| 1084 | : StunRequest(new TurnMessage()), |
| 1085 | port_(port), |
| 1086 | entry_(entry), |
| 1087 | ext_addr_(ext_addr) { |
| 1088 | entry_->SignalDestroyed.connect( |
| 1089 | this, &TurnCreatePermissionRequest::OnEntryDestroyed); |
| 1090 | } |
| 1091 | |
| 1092 | void TurnCreatePermissionRequest::Prepare(StunMessage* request) { |
| 1093 | // Create the request as indicated in RFC5766, Section 9.1. |
| 1094 | request->SetType(TURN_CREATE_PERMISSION_REQUEST); |
| 1095 | VERIFY(request->AddAttribute(new StunXorAddressAttribute( |
| 1096 | STUN_ATTR_XOR_PEER_ADDRESS, ext_addr_))); |
| 1097 | port_->AddRequestAuthInfo(request); |
| 1098 | } |
| 1099 | |
| 1100 | void TurnCreatePermissionRequest::OnResponse(StunMessage* response) { |
| 1101 | if (entry_) { |
| 1102 | entry_->OnCreatePermissionSuccess(); |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | void TurnCreatePermissionRequest::OnErrorResponse(StunMessage* response) { |
| 1107 | if (entry_) { |
| 1108 | const StunErrorCodeAttribute* error_code = response->GetErrorCode(); |
| 1109 | entry_->OnCreatePermissionError(response, error_code->code()); |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | void TurnCreatePermissionRequest::OnTimeout() { |
| 1114 | LOG_J(LS_WARNING, port_) << "Create permission timeout"; |
| 1115 | } |
| 1116 | |
| 1117 | void TurnCreatePermissionRequest::OnEntryDestroyed(TurnEntry* entry) { |
| 1118 | ASSERT(entry_ == entry); |
| 1119 | entry_ = NULL; |
| 1120 | } |
| 1121 | |
| 1122 | TurnChannelBindRequest::TurnChannelBindRequest( |
| 1123 | TurnPort* port, TurnEntry* entry, |
| 1124 | int channel_id, const rtc::SocketAddress& ext_addr) |
| 1125 | : StunRequest(new TurnMessage()), |
| 1126 | port_(port), |
| 1127 | entry_(entry), |
| 1128 | channel_id_(channel_id), |
| 1129 | ext_addr_(ext_addr) { |
| 1130 | entry_->SignalDestroyed.connect( |
| 1131 | this, &TurnChannelBindRequest::OnEntryDestroyed); |
| 1132 | } |
| 1133 | |
| 1134 | void TurnChannelBindRequest::Prepare(StunMessage* request) { |
| 1135 | // Create the request as indicated in RFC5766, Section 11.1. |
| 1136 | request->SetType(TURN_CHANNEL_BIND_REQUEST); |
| 1137 | VERIFY(request->AddAttribute(new StunUInt32Attribute( |
| 1138 | STUN_ATTR_CHANNEL_NUMBER, channel_id_ << 16))); |
| 1139 | VERIFY(request->AddAttribute(new StunXorAddressAttribute( |
| 1140 | STUN_ATTR_XOR_PEER_ADDRESS, ext_addr_))); |
| 1141 | port_->AddRequestAuthInfo(request); |
| 1142 | } |
| 1143 | |
| 1144 | void TurnChannelBindRequest::OnResponse(StunMessage* response) { |
| 1145 | if (entry_) { |
| 1146 | entry_->OnChannelBindSuccess(); |
| 1147 | // Refresh the channel binding just under the permission timeout |
| 1148 | // threshold. The channel binding has a longer lifetime, but |
| 1149 | // this is the easiest way to keep both the channel and the |
| 1150 | // permission from expiring. |
| 1151 | entry_->SendChannelBindRequest(TURN_PERMISSION_TIMEOUT - 60 * 1000); |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | void TurnChannelBindRequest::OnErrorResponse(StunMessage* response) { |
| 1156 | if (entry_) { |
| 1157 | const StunErrorCodeAttribute* error_code = response->GetErrorCode(); |
| 1158 | entry_->OnChannelBindError(response, error_code->code()); |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | void TurnChannelBindRequest::OnTimeout() { |
| 1163 | LOG_J(LS_WARNING, port_) << "Channel bind timeout"; |
| 1164 | } |
| 1165 | |
| 1166 | void TurnChannelBindRequest::OnEntryDestroyed(TurnEntry* entry) { |
| 1167 | ASSERT(entry_ == entry); |
| 1168 | entry_ = NULL; |
| 1169 | } |
| 1170 | |
| 1171 | TurnEntry::TurnEntry(TurnPort* port, int channel_id, |
| 1172 | const rtc::SocketAddress& ext_addr) |
| 1173 | : port_(port), |
| 1174 | channel_id_(channel_id), |
| 1175 | ext_addr_(ext_addr), |
| 1176 | state_(STATE_UNBOUND) { |
| 1177 | // Creating permission for |ext_addr_|. |
| 1178 | SendCreatePermissionRequest(); |
| 1179 | } |
| 1180 | |
| 1181 | void TurnEntry::SendCreatePermissionRequest() { |
| 1182 | port_->SendRequest(new TurnCreatePermissionRequest( |
| 1183 | port_, this, ext_addr_), 0); |
| 1184 | } |
| 1185 | |
| 1186 | void TurnEntry::SendChannelBindRequest(int delay) { |
| 1187 | port_->SendRequest(new TurnChannelBindRequest( |
| 1188 | port_, this, channel_id_, ext_addr_), delay); |
| 1189 | } |
| 1190 | |
| 1191 | int TurnEntry::Send(const void* data, size_t size, bool payload, |
| 1192 | const rtc::PacketOptions& options) { |
| 1193 | rtc::ByteBuffer buf; |
| 1194 | if (state_ != STATE_BOUND) { |
| 1195 | // If we haven't bound the channel yet, we have to use a Send Indication. |
| 1196 | TurnMessage msg; |
| 1197 | msg.SetType(TURN_SEND_INDICATION); |
| 1198 | msg.SetTransactionID( |
| 1199 | rtc::CreateRandomString(kStunTransactionIdLength)); |
| 1200 | VERIFY(msg.AddAttribute(new StunXorAddressAttribute( |
| 1201 | STUN_ATTR_XOR_PEER_ADDRESS, ext_addr_))); |
| 1202 | VERIFY(msg.AddAttribute(new StunByteStringAttribute( |
| 1203 | STUN_ATTR_DATA, data, size))); |
| 1204 | VERIFY(msg.Write(&buf)); |
| 1205 | |
| 1206 | // If we're sending real data, request a channel bind that we can use later. |
| 1207 | if (state_ == STATE_UNBOUND && payload) { |
| 1208 | SendChannelBindRequest(0); |
| 1209 | state_ = STATE_BINDING; |
| 1210 | } |
| 1211 | } else { |
| 1212 | // If the channel is bound, we can send the data as a Channel Message. |
| 1213 | buf.WriteUInt16(channel_id_); |
| 1214 | buf.WriteUInt16(static_cast<uint16>(size)); |
| 1215 | buf.WriteBytes(reinterpret_cast<const char*>(data), size); |
| 1216 | } |
| 1217 | return port_->Send(buf.Data(), buf.Length(), options); |
| 1218 | } |
| 1219 | |
| 1220 | void TurnEntry::OnCreatePermissionSuccess() { |
| 1221 | LOG_J(LS_INFO, port_) << "Create permission for " |
| 1222 | << ext_addr_.ToSensitiveString() |
| 1223 | << " succeeded"; |
| 1224 | // For success result code will be 0. |
| 1225 | port_->SignalCreatePermissionResult(port_, ext_addr_, 0); |
| 1226 | } |
| 1227 | |
| 1228 | void TurnEntry::OnCreatePermissionError(StunMessage* response, int code) { |
| 1229 | LOG_J(LS_WARNING, port_) << "Create permission for " |
| 1230 | << ext_addr_.ToSensitiveString() |
| 1231 | << " failed, code=" << code; |
| 1232 | if (code == STUN_ERROR_STALE_NONCE) { |
| 1233 | if (port_->UpdateNonce(response)) { |
| 1234 | SendCreatePermissionRequest(); |
| 1235 | } |
| 1236 | } else { |
| 1237 | // Send signal with error code. |
| 1238 | port_->SignalCreatePermissionResult(port_, ext_addr_, code); |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | void TurnEntry::OnChannelBindSuccess() { |
| 1243 | LOG_J(LS_INFO, port_) << "Channel bind for " << ext_addr_.ToSensitiveString() |
| 1244 | << " succeeded"; |
| 1245 | ASSERT(state_ == STATE_BINDING || state_ == STATE_BOUND); |
| 1246 | state_ = STATE_BOUND; |
| 1247 | } |
| 1248 | |
| 1249 | void TurnEntry::OnChannelBindError(StunMessage* response, int code) { |
| 1250 | // TODO(mallinath) - Implement handling of error response for channel |
| 1251 | // bind request as per http://tools.ietf.org/html/rfc5766#section-11.3 |
| 1252 | LOG_J(LS_WARNING, port_) << "Channel bind for " |
| 1253 | << ext_addr_.ToSensitiveString() |
| 1254 | << " failed, code=" << code; |
| 1255 | if (code == STUN_ERROR_STALE_NONCE) { |
| 1256 | if (port_->UpdateNonce(response)) { |
| 1257 | // Send channel bind request with fresh nonce. |
| 1258 | SendChannelBindRequest(0); |
| 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | } // namespace cricket |