| 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 "p2p/base/turn_server.h" |
| 12 |
| 13 | #include <memory> |
| 14 | #include <tuple> // for std::tie |
| 15 | #include <utility> |
| 16 |
| 17 | #include "absl/algorithm/container.h" |
| 18 | #include "absl/memory/memory.h" |
| 19 | #include "api/packet_socket_factory.h" |
| 20 | #include "api/transport/stun.h" |
| 21 | #include "p2p/base/async_stun_tcp_socket.h" |
| 22 | #include "rtc_base/byte_buffer.h" |
| 23 | #include "rtc_base/checks.h" |
| 24 | #include "rtc_base/helpers.h" |
| 25 | #include "rtc_base/logging.h" |
| 26 | #include "rtc_base/message_digest.h" |
| 27 | #include "rtc_base/socket_adapters.h" |
| 28 | #include "rtc_base/strings/string_builder.h" |
| 29 | #include "rtc_base/task_utils/to_queued_task.h" |
| 30 | #include "rtc_base/thread.h" |
| 31 |
| 32 | namespace cricket { |
| 33 |
| 34 | // TODO(juberti): Move this all to a future turnmessage.h |
| 35 | // static const int IPPROTO_UDP = 17; |
| 36 | static const int kNonceTimeout = 60 * 60 * 1000; // 60 minutes |
| 37 | static const int kDefaultAllocationTimeout = 10 * 60 * 1000; // 10 minutes |
| 38 | static const int kPermissionTimeout = 5 * 60 * 1000; // 5 minutes |
| 39 | static const int kChannelTimeout = 10 * 60 * 1000; // 10 minutes |
| 40 |
| 41 | static const int kMinChannelNumber = 0x4000; |
| 42 | static const int kMaxChannelNumber = 0x7FFF; |
| 43 |
| 44 | static const size_t kNonceKeySize = 16; |
| 45 | static const size_t kNonceSize = 48; |
| 46 |
| 47 | static const size_t TURN_CHANNEL_HEADER_SIZE = 4U; |
| 48 |
| 49 | // TODO(mallinath) - Move these to a common place. |
| 50 | inline bool IsTurnChannelData(uint16_t msg_type) { |
| 51 | // The first two bits of a channel data message are 0b01. |
| 52 | return ((msg_type & 0xC000) == 0x4000); |
| 53 | } |
| 54 |
| 55 | // IDs used for posted messages for TurnServerAllocation. |
| 56 | enum { |
| 57 | MSG_ALLOCATION_TIMEOUT, |
| 58 | }; |
| 59 |
| 60 | // Encapsulates a TURN permission. |
| 61 | // The object is created when a create permission request is received by an |
| 62 | // allocation, and self-deletes when its lifetime timer expires. |
| 63 | class TurnServerAllocation::Permission : public rtc::MessageHandlerAutoCleanup { |
| 64 | public: |
| 65 | Permission(rtc::Thread* thread, const rtc::IPAddress& peer); |
| 66 | ~Permission() override; |
| 67 |
| 68 | const rtc::IPAddress& peer() const { return peer_; } |
| 69 | void Refresh(); |
| 70 |
| 71 | sigslot::signal1<Permission*> SignalDestroyed; |
| 72 |
| 73 | private: |
| 74 | void OnMessage(rtc::Message* msg) override; |
| 75 |
| 76 | rtc::Thread* thread_; |
| 77 | rtc::IPAddress peer_; |
| 78 | }; |
| 79 |
| 80 | // Encapsulates a TURN channel binding. |
| 81 | // The object is created when a channel bind request is received by an |
| 82 | // allocation, and self-deletes when its lifetime timer expires. |
| 83 | class TurnServerAllocation::Channel : public rtc::MessageHandlerAutoCleanup { |
| 84 | public: |
| 85 | Channel(rtc::Thread* thread, int id, const rtc::SocketAddress& peer); |
| 86 | ~Channel() override; |
| 87 |
| 88 | int id() const { return id_; } |
| 89 | const rtc::SocketAddress& peer() const { return peer_; } |
| 90 | void Refresh(); |
| 91 |
| 92 | sigslot::signal1<Channel*> SignalDestroyed; |
| 93 |
| 94 | private: |
| 95 | void OnMessage(rtc::Message* msg) override; |
| 96 |
| 97 | rtc::Thread* thread_; |
| 98 | int id_; |
| 99 | rtc::SocketAddress peer_; |
| 100 | }; |
| 101 |
| 102 | static bool InitResponse(const StunMessage* req, StunMessage* resp) { |
| 103 | int resp_type = (req) ? GetStunSuccessResponseType(req->type()) : -1; |
| 104 | if (resp_type == -1) |
| 105 | return false; |
| 106 | resp->SetType(resp_type); |
| 107 | resp->SetTransactionID(req->transaction_id()); |
| 108 | return true; |
| 109 | } |
| 110 |
| 111 | static bool InitErrorResponse(const StunMessage* req, |
| 112 | int code, |
| 113 | const std::string& reason, |
| 114 | StunMessage* resp) { |
| 115 | int resp_type = (req) ? GetStunErrorResponseType(req->type()) : -1; |
| 116 | if (resp_type == -1) |
| 117 | return false; |
| 118 | resp->SetType(resp_type); |
| 119 | resp->SetTransactionID(req->transaction_id()); |
| 120 | resp->AddAttribute(std::make_unique<cricket::StunErrorCodeAttribute>( |
| 121 | STUN_ATTR_ERROR_CODE, code, reason)); |
| 122 | return true; |
| 123 | } |
| 124 |
| 125 | TurnServer::TurnServer(rtc::Thread* thread) |
| 126 | : thread_(thread), |
| 127 | nonce_key_(rtc::CreateRandomString(kNonceKeySize)), |
| 128 | auth_hook_(NULL), |
| 129 | redirect_hook_(NULL), |
| 130 | enable_otu_nonce_(false) {} |
| 131 |
| 132 | TurnServer::~TurnServer() { |
| 133 | RTC_DCHECK_RUN_ON(thread_); |
| 134 | for (InternalSocketMap::iterator it = server_sockets_.begin(); |
| 135 | it != server_sockets_.end(); ++it) { |
| 136 | rtc::AsyncPacketSocket* socket = it->first; |
| 137 | delete socket; |
| 138 | } |
| 139 |
| 140 | for (ServerSocketMap::iterator it = server_listen_sockets_.begin(); |
| 141 | it != server_listen_sockets_.end(); ++it) { |
| 142 | rtc::Socket* socket = it->first; |
| 143 | delete socket; |
| 144 | } |
| 145 | } |
| 146 |
| 147 | void TurnServer::AddInternalSocket(rtc::AsyncPacketSocket* socket, |
| 148 | ProtocolType proto) { |
| 149 | RTC_DCHECK_RUN_ON(thread_); |
| 150 | RTC_DCHECK(server_sockets_.end() == server_sockets_.find(socket)); |
| 151 | server_sockets_[socket] = proto; |
| 152 | socket->SignalReadPacket.connect(this, &TurnServer::OnInternalPacket); |
| 153 | } |
| 154 |
| 155 | void TurnServer::AddInternalServerSocket( |
| 156 | rtc::Socket* socket, |
| 157 | ProtocolType proto, |
| 158 | std::unique_ptr<rtc::SSLAdapterFactory> ssl_adapter_factory) { |
| 159 | RTC_DCHECK_RUN_ON(thread_); |
| 160 |
| 161 | RTC_DCHECK(server_listen_sockets_.end() == |
| 162 | server_listen_sockets_.find(socket)); |
| 163 | server_listen_sockets_[socket] = {proto, std::move(ssl_adapter_factory)}; |
| 164 | socket->SignalReadEvent.connect(this, &TurnServer::OnNewInternalConnection); |
| 165 | } |
| 166 |
| 167 | void TurnServer::SetExternalSocketFactory( |
| 168 | rtc::PacketSocketFactory* factory, |
| 169 | const rtc::SocketAddress& external_addr) { |
| 170 | RTC_DCHECK_RUN_ON(thread_); |
| 171 | external_socket_factory_.reset(factory); |
| 172 | external_addr_ = external_addr; |
| 173 | } |
| 174 |
| 175 | void TurnServer::OnNewInternalConnection(rtc::Socket* socket) { |
| 176 | RTC_DCHECK_RUN_ON(thread_); |
| 177 | RTC_DCHECK(server_listen_sockets_.find(socket) != |
| 178 | server_listen_sockets_.end()); |
| 179 | AcceptConnection(socket); |
| 180 | } |
| 181 |
| 182 | void TurnServer::AcceptConnection(rtc::Socket* server_socket) { |
| 183 | // Check if someone is trying to connect to us. |
| 184 | rtc::SocketAddress accept_addr; |
| 185 | rtc::Socket* accepted_socket = server_socket->Accept(&accept_addr); |
| 186 | if (accepted_socket != NULL) { |
| 187 | const ServerSocketInfo& info = server_listen_sockets_[server_socket]; |
| 188 | if (info.ssl_adapter_factory) { |
| 189 | rtc::SSLAdapter* ssl_adapter = |
| 190 | info.ssl_adapter_factory->CreateAdapter(accepted_socket); |
| 191 | ssl_adapter->StartSSL(""); |
| 192 | accepted_socket = ssl_adapter; |
| 193 | } |
| 194 | cricket::AsyncStunTCPSocket* tcp_socket = |
| 195 | new cricket::AsyncStunTCPSocket(accepted_socket); |
| 196 |
| 197 | tcp_socket->SignalClose.connect(this, &TurnServer::OnInternalSocketClose); |
| 198 | // Finally add the socket so it can start communicating with the client. |
| 199 | AddInternalSocket(tcp_socket, info.proto); |
| 200 | } |
| 201 | } |
| 202 |
| 203 | void TurnServer::OnInternalSocketClose(rtc::AsyncPacketSocket* socket, |
| 204 | int err) { |
| 205 | RTC_DCHECK_RUN_ON(thread_); |
| 206 | DestroyInternalSocket(socket); |
| 207 | } |
| 208 |
| 209 | void TurnServer::OnInternalPacket(rtc::AsyncPacketSocket* socket, |
| 210 | const char* data, |
| 211 | size_t size, |
| 212 | const rtc::SocketAddress& addr, |
| 213 | const int64_t& /* packet_time_us */) { |
| 214 | RTC_DCHECK_RUN_ON(thread_); |
| 215 | // Fail if the packet is too small to even contain a channel header. |
| 216 | if (size < TURN_CHANNEL_HEADER_SIZE) { |
| 217 | return; |
| 218 | } |
| 219 | InternalSocketMap::iterator iter = server_sockets_.find(socket); |
| 220 | RTC_DCHECK(iter != server_sockets_.end()); |
| 221 | TurnServerConnection conn(addr, iter->second, socket); |
| 222 | uint16_t msg_type = rtc::GetBE16(data); |
| 223 | if (!IsTurnChannelData(msg_type)) { |
| 224 | // This is a STUN message. |
| 225 | HandleStunMessage(&conn, data, size); |
| 226 | } else { |
| 227 | // This is a channel message; let the allocation handle it. |
| 228 | TurnServerAllocation* allocation = FindAllocation(&conn); |
| 229 | if (allocation) { |
| 230 | allocation->HandleChannelData(data, size); |
| 231 | } |
| 232 | if (stun_message_observer_ != nullptr) { |
| 233 | stun_message_observer_->ReceivedChannelData(data, size); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 |
| 238 | void TurnServer::HandleStunMessage(TurnServerConnection* conn, |
| 239 | const char* data, |
| 240 | size_t size) { |
| 241 | TurnMessage msg; |
| 242 | rtc::ByteBufferReader buf(data, size); |
| 243 | if (!msg.Read(&buf) || (buf.Length() > 0)) { |
| 244 | RTC_LOG(LS_WARNING) << "Received invalid STUN message"; |
| 245 | return; |
| 246 | } |
| 247 |
| 248 | if (stun_message_observer_ != nullptr) { |
| 249 | stun_message_observer_->ReceivedMessage(&msg); |
| 250 | } |
| 251 |
| 252 | // If it's a STUN binding request, handle that specially. |
| 253 | if (msg.type() == STUN_BINDING_REQUEST) { |
| 254 | HandleBindingRequest(conn, &msg); |
| 255 | return; |
| 256 | } |
| 257 |
| 258 | if (redirect_hook_ != NULL && msg.type() == STUN_ALLOCATE_REQUEST) { |
| 259 | rtc::SocketAddress address; |
| 260 | if (redirect_hook_->ShouldRedirect(conn->src(), &address)) { |
| 261 | SendErrorResponseWithAlternateServer(conn, &msg, address); |
| 262 | return; |
| 263 | } |
| 264 | } |
| 265 |
| 266 | // Look up the key that we'll use to validate the M-I. If we have an |
| 267 | // existing allocation, the key will already be cached. |
| 268 | TurnServerAllocation* allocation = FindAllocation(conn); |
| 269 | std::string key; |
| 270 | if (!allocation) { |
| 271 | GetKey(&msg, &key); |
| 272 | } else { |
| 273 | key = allocation->key(); |
| 274 | } |
| 275 |
| 276 | // Ensure the message is authorized; only needed for requests. |
| 277 | if (IsStunRequestType(msg.type())) { |
| 278 | if (!CheckAuthorization(conn, &msg, data, size, key)) { |
| 279 | return; |
| 280 | } |
| 281 | } |
| 282 |
| 283 | if (!allocation && msg.type() == STUN_ALLOCATE_REQUEST) { |
| 284 | HandleAllocateRequest(conn, &msg, key); |
| 285 | } else if (allocation && |
| 286 | (msg.type() != STUN_ALLOCATE_REQUEST || |
| 287 | msg.transaction_id() == allocation->transaction_id())) { |
| 288 | // This is a non-allocate request, or a retransmit of an allocate. |
| 289 | // Check that the username matches the previous username used. |
| 290 | if (IsStunRequestType(msg.type()) && |
| 291 | msg.GetByteString(STUN_ATTR_USERNAME)->GetString() != |
| 292 | allocation->username()) { |
| 293 | SendErrorResponse(conn, &msg, STUN_ERROR_WRONG_CREDENTIALS, |
| 294 | STUN_ERROR_REASON_WRONG_CREDENTIALS); |
| 295 | return; |
| 296 | } |
| 297 | allocation->HandleTurnMessage(&msg); |
| 298 | } else { |
| 299 | // Allocation mismatch. |
| 300 | SendErrorResponse(conn, &msg, STUN_ERROR_ALLOCATION_MISMATCH, |
| 301 | STUN_ERROR_REASON_ALLOCATION_MISMATCH); |
| 302 | } |
| 303 | } |
| 304 |
| 305 | bool TurnServer::GetKey(const StunMessage* msg, std::string* key) { |
| 306 | const StunByteStringAttribute* username_attr = |
| 307 | msg->GetByteString(STUN_ATTR_USERNAME); |
| 308 | if (!username_attr) { |
| 309 | return false; |
| 310 | } |
| 311 |
| 312 | std::string username = username_attr->GetString(); |
| 313 | return (auth_hook_ != NULL && auth_hook_->GetKey(username, realm_, key)); |
| 314 | } |
| 315 |
| 316 | bool TurnServer::CheckAuthorization(TurnServerConnection* conn, |
| 317 | StunMessage* msg, |
| 318 | const char* data, |
| 319 | size_t size, |
| 320 | const std::string& key) { |
| 321 | // RFC 5389, 10.2.2. |
| 322 | RTC_DCHECK(IsStunRequestType(msg->type())); |
| 323 | const StunByteStringAttribute* mi_attr = |
| 324 | msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY); |
| 325 | const StunByteStringAttribute* username_attr = |
| 326 | msg->GetByteString(STUN_ATTR_USERNAME); |
| 327 | const StunByteStringAttribute* realm_attr = |
| 328 | msg->GetByteString(STUN_ATTR_REALM); |
| 329 | const StunByteStringAttribute* nonce_attr = |
| 330 | msg->GetByteString(STUN_ATTR_NONCE); |
| 331 |
| 332 | // Fail if no MESSAGE_INTEGRITY. |
| 333 | if (!mi_attr) { |
| 334 | SendErrorResponseWithRealmAndNonce(conn, msg, STUN_ERROR_UNAUTHORIZED, |
| 335 | STUN_ERROR_REASON_UNAUTHORIZED); |
| 336 | return false; |
| 337 | } |
| 338 |
| 339 | // Fail if there is MESSAGE_INTEGRITY but no username, nonce, or realm. |
| 340 | if (!username_attr || !realm_attr || !nonce_attr) { |
| 341 | SendErrorResponse(conn, msg, STUN_ERROR_BAD_REQUEST, |
| 342 | STUN_ERROR_REASON_BAD_REQUEST); |
| 343 | return false; |
| 344 | } |
| 345 |
| 346 | // Fail if bad nonce. |
| 347 | if (!ValidateNonce(nonce_attr->GetString())) { |
| 348 | SendErrorResponseWithRealmAndNonce(conn, msg, STUN_ERROR_STALE_NONCE, |
| 349 | STUN_ERROR_REASON_STALE_NONCE); |
| 350 | return false; |
| 351 | } |
| 352 |
| 353 | // Fail if bad MESSAGE_INTEGRITY. |
| 354 | if (key.empty() || msg->ValidateMessageIntegrity(key) != |
| 355 | StunMessage::IntegrityStatus::kIntegrityOk) { |
| 356 | SendErrorResponseWithRealmAndNonce(conn, msg, STUN_ERROR_UNAUTHORIZED, |
| 357 | STUN_ERROR_REASON_UNAUTHORIZED); |
| 358 | return false; |
| 359 | } |
| 360 |
| 361 | // Fail if one-time-use nonce feature is enabled. |
| 362 | TurnServerAllocation* allocation = FindAllocation(conn); |
| 363 | if (enable_otu_nonce_ && allocation && |
| 364 | allocation->last_nonce() == nonce_attr->GetString()) { |
| 365 | SendErrorResponseWithRealmAndNonce(conn, msg, STUN_ERROR_STALE_NONCE, |
| 366 | STUN_ERROR_REASON_STALE_NONCE); |
| 367 | return false; |
| 368 | } |
| 369 |
| 370 | if (allocation) { |
| 371 | allocation->set_last_nonce(nonce_attr->GetString()); |
| 372 | } |
| 373 | // Success. |
| 374 | return true; |
| 375 | } |
| 376 |
| 377 | void TurnServer::HandleBindingRequest(TurnServerConnection* conn, |
| 378 | const StunMessage* req) { |
| 379 | StunMessage response; |
| 380 | InitResponse(req, &response); |
| 381 |
| 382 | // Tell the user the address that we received their request from. |
| 383 | auto mapped_addr_attr = std::make_unique<StunXorAddressAttribute>( |
| 384 | STUN_ATTR_XOR_MAPPED_ADDRESS, conn->src()); |
| 385 | response.AddAttribute(std::move(mapped_addr_attr)); |
| 386 |
| 387 | SendStun(conn, &response); |
| 388 | } |
| 389 |
| 390 | void TurnServer::HandleAllocateRequest(TurnServerConnection* conn, |
| 391 | const TurnMessage* msg, |
| 392 | const std::string& key) { |
| 393 | // Check the parameters in the request. |
| 394 | const StunUInt32Attribute* transport_attr = |
| 395 | msg->GetUInt32(STUN_ATTR_REQUESTED_TRANSPORT); |
| 396 | if (!transport_attr) { |
| 397 | SendErrorResponse(conn, msg, STUN_ERROR_BAD_REQUEST, |
| 398 | STUN_ERROR_REASON_BAD_REQUEST); |
| 399 | return; |
| 400 | } |
| 401 |
| 402 | // Only UDP is supported right now. |
| 403 | int proto = transport_attr->value() >> 24; |
| 404 | if (proto != IPPROTO_UDP) { |
| 405 | SendErrorResponse(conn, msg, STUN_ERROR_UNSUPPORTED_PROTOCOL, |
| 406 | STUN_ERROR_REASON_UNSUPPORTED_PROTOCOL); |
| 407 | return; |
| 408 | } |
| 409 |
| 410 | // Create the allocation and let it send the success response. |
| 411 | // If the actual socket allocation fails, send an internal error. |
| 412 | TurnServerAllocation* alloc = CreateAllocation(conn, proto, key); |
| 413 | if (alloc) { |
| 414 | alloc->HandleTurnMessage(msg); |
| 415 | } else { |
| 416 | SendErrorResponse(conn, msg, STUN_ERROR_SERVER_ERROR, |
| 417 | "Failed to allocate socket"); |
| 418 | } |
| 419 | } |
| 420 |
| 421 | std::string TurnServer::GenerateNonce(int64_t now) const { |
| 422 | // Generate a nonce of the form hex(now + HMAC-MD5(nonce_key_, now)) |
| 423 | std::string input(reinterpret_cast<const char*>(&now), sizeof(now)); |
| 424 | std::string nonce = rtc::hex_encode(input.c_str(), input.size()); |
| 425 | nonce += rtc::ComputeHmac(rtc::DIGEST_MD5, nonce_key_, input); |
| 426 | RTC_DCHECK(nonce.size() == kNonceSize); |
| 427 |
| 428 | return nonce; |
| 429 | } |
| 430 |
| 431 | bool TurnServer::ValidateNonce(const std::string& nonce) const { |
| 432 | // Check the size. |
| 433 | if (nonce.size() != kNonceSize) { |
| 434 | return false; |
| 435 | } |
| 436 |
| 437 | // Decode the timestamp. |
| 438 | int64_t then; |
| 439 | char* p = reinterpret_cast<char*>(&then); |
| 440 | size_t len = |
| 441 | rtc::hex_decode(p, sizeof(then), nonce.substr(0, sizeof(then) * 2)); |
| 442 | if (len != sizeof(then)) { |
| 443 | return false; |
| 444 | } |
| 445 |
| 446 | // Verify the HMAC. |
| 447 | if (nonce.substr(sizeof(then) * 2) != |
| 448 | rtc::ComputeHmac(rtc::DIGEST_MD5, nonce_key_, |
| 449 | std::string(p, sizeof(then)))) { |
| 450 | return false; |
| 451 | } |
| 452 |
| 453 | // Validate the timestamp. |
| 454 | return rtc::TimeMillis() - then < kNonceTimeout; |
| 455 | } |
| 456 |
| 457 | TurnServerAllocation* TurnServer::FindAllocation(TurnServerConnection* conn) { |
| 458 | AllocationMap::const_iterator it = allocations_.find(*conn); |
| 459 | return (it != allocations_.end()) ? it->second.get() : nullptr; |
| 460 | } |
| 461 |
| 462 | TurnServerAllocation* TurnServer::CreateAllocation(TurnServerConnection* conn, |
| 463 | int proto, |
| 464 | const std::string& key) { |
| 465 | rtc::AsyncPacketSocket* external_socket = |
| 466 | (external_socket_factory_) |
| 467 | ? external_socket_factory_->CreateUdpSocket(external_addr_, 0, 0) |
| 468 | : NULL; |
| 469 | if (!external_socket) { |
| 470 | return NULL; |
| 471 | } |
| 472 |
| 473 | // The Allocation takes ownership of the socket. |
| 474 | TurnServerAllocation* allocation = |
| 475 | new TurnServerAllocation(this, thread_, *conn, external_socket, key); |
| 476 | allocation->SignalDestroyed.connect(this, &TurnServer::OnAllocationDestroyed); |
| 477 | allocations_[*conn].reset(allocation); |
| 478 | return allocation; |
| 479 | } |
| 480 |
| 481 | void TurnServer::SendErrorResponse(TurnServerConnection* conn, |
| 482 | const StunMessage* req, |
| 483 | int code, |
| 484 | const std::string& reason) { |
| 485 | RTC_DCHECK_RUN_ON(thread_); |
| 486 | TurnMessage resp; |
| 487 | InitErrorResponse(req, code, reason, &resp); |
| 488 | RTC_LOG(LS_INFO) << "Sending error response, type=" << resp.type() |
| 489 | << ", code=" << code << ", reason=" << reason; |
| 490 | SendStun(conn, &resp); |
| 491 | } |
| 492 |
| 493 | void TurnServer::SendErrorResponseWithRealmAndNonce(TurnServerConnection* conn, |
| 494 | const StunMessage* msg, |
| 495 | int code, |
| 496 | const std::string& reason) { |
| 497 | TurnMessage resp; |
| 498 | InitErrorResponse(msg, code, reason, &resp); |
| 499 |
| 500 | int64_t timestamp = rtc::TimeMillis(); |
| 501 | if (ts_for_next_nonce_) { |
| 502 | timestamp = ts_for_next_nonce_; |
| 503 | ts_for_next_nonce_ = 0; |
| 504 | } |
| 505 | resp.AddAttribute(std::make_unique<StunByteStringAttribute>( |
| 506 | STUN_ATTR_NONCE, GenerateNonce(timestamp))); |
| 507 | resp.AddAttribute( |
| 508 | std::make_unique<StunByteStringAttribute>(STUN_ATTR_REALM, realm_)); |
| 509 | SendStun(conn, &resp); |
| 510 | } |
| 511 |
| 512 | void TurnServer::SendErrorResponseWithAlternateServer( |
| 513 | TurnServerConnection* conn, |
| 514 | const StunMessage* msg, |
| 515 | const rtc::SocketAddress& addr) { |
| 516 | TurnMessage resp; |
| 517 | InitErrorResponse(msg, STUN_ERROR_TRY_ALTERNATE, |
| 518 | STUN_ERROR_REASON_TRY_ALTERNATE_SERVER, &resp); |
| 519 | resp.AddAttribute( |
| 520 | std::make_unique<StunAddressAttribute>(STUN_ATTR_ALTERNATE_SERVER, addr)); |
| 521 | SendStun(conn, &resp); |
| 522 | } |
| 523 |
| 524 | void TurnServer::SendStun(TurnServerConnection* conn, StunMessage* msg) { |
| 525 | RTC_DCHECK_RUN_ON(thread_); |
| 526 | rtc::ByteBufferWriter buf; |
| 527 | // Add a SOFTWARE attribute if one is set. |
| 528 | if (!software_.empty()) { |
| 529 | msg->AddAttribute(std::make_unique<StunByteStringAttribute>( |
| 530 | STUN_ATTR_SOFTWARE, software_)); |
| 531 | } |
| 532 | msg->Write(&buf); |
| 533 | Send(conn, buf); |
| 534 | } |
| 535 |
| 536 | void TurnServer::Send(TurnServerConnection* conn, |
| 537 | const rtc::ByteBufferWriter& buf) { |
| 538 | RTC_DCHECK_RUN_ON(thread_); |
| 539 | rtc::PacketOptions options; |
| 540 | conn->socket()->SendTo(buf.Data(), buf.Length(), conn->src(), options); |
| 541 | } |
| 542 |
| 543 | void TurnServer::OnAllocationDestroyed(TurnServerAllocation* allocation) { |
| 544 | // Removing the internal socket if the connection is not udp. |
| 545 | rtc::AsyncPacketSocket* socket = allocation->conn()->socket(); |
| 546 | InternalSocketMap::iterator iter = server_sockets_.find(socket); |
| 547 | // Skip if the socket serving this allocation is UDP, as this will be shared |
| 548 | // by all allocations. |
| 549 | // Note: We may not find a socket if it's a TCP socket that was closed, and |
| 550 | // the allocation is only now timing out. |
| 551 | if (iter != server_sockets_.end() && iter->second != cricket::PROTO_UDP) { |
| 552 | DestroyInternalSocket(socket); |
| 553 | } |
| 554 |
| 555 | AllocationMap::iterator it = allocations_.find(*(allocation->conn())); |
| 556 | if (it != allocations_.end()) { |
| 557 | it->second.release(); |
| 558 | allocations_.erase(it); |
| 559 | } |
| 560 | } |
| 561 |
| 562 | void TurnServer::DestroyInternalSocket(rtc::AsyncPacketSocket* socket) { |
| 563 | InternalSocketMap::iterator iter = server_sockets_.find(socket); |
| 564 | if (iter != server_sockets_.end()) { |
| 565 | rtc::AsyncPacketSocket* socket = iter->first; |
| 566 | socket->SignalReadPacket.disconnect(this); |
| 567 | server_sockets_.erase(iter); |
| 568 | std::unique_ptr<rtc::AsyncPacketSocket> socket_to_delete = |
| 569 | absl::WrapUnique(socket); |
| 570 | // We must destroy the socket async to avoid invalidating the sigslot |
| 571 | // callback list iterator inside a sigslot callback. (In other words, |
| 572 | // deleting an object from within a callback from that object). |
| 573 | thread_->PostTask(webrtc::ToQueuedTask( |
| 574 | [socket_to_delete = std::move(socket_to_delete)] {})); |
| 575 | } |
| 576 | } |
| 577 |
| 578 | TurnServerConnection::TurnServerConnection(const rtc::SocketAddress& src, |
| 579 | ProtocolType proto, |
| 580 | rtc::AsyncPacketSocket* socket) |
| 581 | : src_(src), |
| 582 | dst_(socket->GetRemoteAddress()), |
| 583 | proto_(proto), |
| 584 | socket_(socket) {} |
| 585 |
| 586 | bool TurnServerConnection::operator==(const TurnServerConnection& c) const { |
| 587 | return src_ == c.src_ && dst_ == c.dst_ && proto_ == c.proto_; |
| 588 | } |
| 589 |
| 590 | bool TurnServerConnection::operator<(const TurnServerConnection& c) const { |
| 591 | return std::tie(src_, dst_, proto_) < std::tie(c.src_, c.dst_, c.proto_); |
| 592 | } |
| 593 |
| 594 | std::string TurnServerConnection::ToString() const { |
| 595 | const char* const kProtos[] = {"unknown", "udp", "tcp", "ssltcp"}; |
| 596 | rtc::StringBuilder ost; |
| 597 | ost << src_.ToSensitiveString() << "-" << dst_.ToSensitiveString() << ":" |
| 598 | << kProtos[proto_]; |
| 599 | return ost.Release(); |
| 600 | } |
| 601 |
| 602 | TurnServerAllocation::TurnServerAllocation(TurnServer* server, |
| 603 | rtc::Thread* thread, |
| 604 | const TurnServerConnection& conn, |
| 605 | rtc::AsyncPacketSocket* socket, |
| 606 | const std::string& key) |
| 607 | : server_(server), |
| 608 | thread_(thread), |
| 609 | conn_(conn), |
| 610 | external_socket_(socket), |
| 611 | key_(key) { |
| 612 | external_socket_->SignalReadPacket.connect( |
| 613 | this, &TurnServerAllocation::OnExternalPacket); |
| 614 | } |
| 615 |
| 616 | TurnServerAllocation::~TurnServerAllocation() { |
| 617 | for (ChannelList::iterator it = channels_.begin(); it != channels_.end(); |
| 618 | ++it) { |
| 619 | delete *it; |
| 620 | } |
| 621 | for (PermissionList::iterator it = perms_.begin(); it != perms_.end(); ++it) { |
| 622 | delete *it; |
| 623 | } |
| 624 | thread_->Clear(this, MSG_ALLOCATION_TIMEOUT); |
| 625 | RTC_LOG(LS_INFO) << ToString() << ": Allocation destroyed"; |
| 626 | } |
| 627 |
| 628 | std::string TurnServerAllocation::ToString() const { |
| 629 | rtc::StringBuilder ost; |
| 630 | ost << "Alloc[" << conn_.ToString() << "]"; |
| 631 | return ost.Release(); |
| 632 | } |
| 633 |
| 634 | void TurnServerAllocation::HandleTurnMessage(const TurnMessage* msg) { |
| 635 | RTC_DCHECK(msg != NULL); |
| 636 | switch (msg->type()) { |
| 637 | case STUN_ALLOCATE_REQUEST: |
| 638 | HandleAllocateRequest(msg); |
| 639 | break; |
| 640 | case TURN_REFRESH_REQUEST: |
| 641 | HandleRefreshRequest(msg); |
| 642 | break; |
| 643 | case TURN_SEND_INDICATION: |
| 644 | HandleSendIndication(msg); |
| 645 | break; |
| 646 | case TURN_CREATE_PERMISSION_REQUEST: |
| 647 | HandleCreatePermissionRequest(msg); |
| 648 | break; |
| 649 | case TURN_CHANNEL_BIND_REQUEST: |
| 650 | HandleChannelBindRequest(msg); |
| 651 | break; |
| 652 | default: |
| 653 | // Not sure what to do with this, just eat it. |
| 654 | RTC_LOG(LS_WARNING) << ToString() |
| 655 | << ": Invalid TURN message type received: " |
| 656 | << msg->type(); |
| 657 | } |
| 658 | } |
| 659 |
| 660 | void TurnServerAllocation::HandleAllocateRequest(const TurnMessage* msg) { |
| 661 | // Copy the important info from the allocate request. |
| 662 | transaction_id_ = msg->transaction_id(); |
| 663 | const StunByteStringAttribute* username_attr = |
| 664 | msg->GetByteString(STUN_ATTR_USERNAME); |
| 665 | RTC_DCHECK(username_attr != NULL); |
| 666 | username_ = username_attr->GetString(); |
| 667 |
| 668 | // Figure out the lifetime and start the allocation timer. |
| 669 | int lifetime_secs = ComputeLifetime(msg); |
| 670 | thread_->PostDelayed(RTC_FROM_HERE, lifetime_secs * 1000, this, |
| 671 | MSG_ALLOCATION_TIMEOUT); |
| 672 |
| 673 | RTC_LOG(LS_INFO) << ToString() |
| 674 | << ": Created allocation with lifetime=" << lifetime_secs; |
| 675 |
| 676 | // We've already validated all the important bits; just send a response here. |
| 677 | TurnMessage response; |
| 678 | InitResponse(msg, &response); |
| 679 |
| 680 | auto mapped_addr_attr = std::make_unique<StunXorAddressAttribute>( |
| 681 | STUN_ATTR_XOR_MAPPED_ADDRESS, conn_.src()); |
| 682 | auto relayed_addr_attr = std::make_unique<StunXorAddressAttribute>( |
| 683 | STUN_ATTR_XOR_RELAYED_ADDRESS, external_socket_->GetLocalAddress()); |
| 684 | auto lifetime_attr = |
| 685 | std::make_unique<StunUInt32Attribute>(STUN_ATTR_LIFETIME, lifetime_secs); |
| 686 | response.AddAttribute(std::move(mapped_addr_attr)); |
| 687 | response.AddAttribute(std::move(relayed_addr_attr)); |
| 688 | response.AddAttribute(std::move(lifetime_attr)); |
| 689 |
| 690 | SendResponse(&response); |
| 691 | } |
| 692 |
| 693 | void TurnServerAllocation::HandleRefreshRequest(const TurnMessage* msg) { |
| 694 | // Figure out the new lifetime. |
| 695 | int lifetime_secs = ComputeLifetime(msg); |
| 696 |
| 697 | // Reset the expiration timer. |
| 698 | thread_->Clear(this, MSG_ALLOCATION_TIMEOUT); |
| 699 | thread_->PostDelayed(RTC_FROM_HERE, lifetime_secs * 1000, this, |
| 700 | MSG_ALLOCATION_TIMEOUT); |
| 701 |
| 702 | RTC_LOG(LS_INFO) << ToString() |
| 703 | << ": Refreshed allocation, lifetime=" << lifetime_secs; |
| 704 |
| 705 | // Send a success response with a LIFETIME attribute. |
| 706 | TurnMessage response; |
| 707 | InitResponse(msg, &response); |
| 708 |
| 709 | auto lifetime_attr = |
| 710 | std::make_unique<StunUInt32Attribute>(STUN_ATTR_LIFETIME, lifetime_secs); |
| 711 | response.AddAttribute(std::move(lifetime_attr)); |
| 712 |
| 713 | SendResponse(&response); |
| 714 | } |
| 715 |
| 716 | void TurnServerAllocation::HandleSendIndication(const TurnMessage* msg) { |
| 717 | // Check mandatory attributes. |
| 718 | const StunByteStringAttribute* data_attr = msg->GetByteString(STUN_ATTR_DATA); |
| 719 | const StunAddressAttribute* peer_attr = |
| 720 | msg->GetAddress(STUN_ATTR_XOR_PEER_ADDRESS); |
| 721 | if (!data_attr || !peer_attr) { |
| 722 | RTC_LOG(LS_WARNING) << ToString() << ": Received invalid send indication"; |
| 723 | return; |
| 724 | } |
| 725 |
| 726 | // If a permission exists, send the data on to the peer. |
| 727 | if (HasPermission(peer_attr->GetAddress().ipaddr())) { |
| 728 | SendExternal(data_attr->bytes(), data_attr->length(), |
| 729 | peer_attr->GetAddress()); |
| 730 | } else { |
| 731 | RTC_LOG(LS_WARNING) << ToString() |
| 732 | << ": Received send indication without permission" |
| 733 | " peer=" |
| 734 | << peer_attr->GetAddress().ToSensitiveString(); |
| 735 | } |
| 736 | } |
| 737 |
| 738 | void TurnServerAllocation::HandleCreatePermissionRequest( |
| 739 | const TurnMessage* msg) { |
| 740 | // Check mandatory attributes. |
| 741 | const StunAddressAttribute* peer_attr = |
| 742 | msg->GetAddress(STUN_ATTR_XOR_PEER_ADDRESS); |
| 743 | if (!peer_attr) { |
| 744 | SendBadRequestResponse(msg); |
| 745 | return; |
| 746 | } |
| 747 |
| 748 | if (server_->reject_private_addresses_ && |
| 749 | rtc::IPIsPrivate(peer_attr->GetAddress().ipaddr())) { |
| 750 | SendErrorResponse(msg, STUN_ERROR_FORBIDDEN, STUN_ERROR_REASON_FORBIDDEN); |
| 751 | return; |
| 752 | } |
| 753 |
| 754 | // Add this permission. |
| 755 | AddPermission(peer_attr->GetAddress().ipaddr()); |
| 756 |
| 757 | RTC_LOG(LS_INFO) << ToString() << ": Created permission, peer=" |
| 758 | << peer_attr->GetAddress().ToSensitiveString(); |
| 759 |
| 760 | // Send a success response. |
| 761 | TurnMessage response; |
| 762 | InitResponse(msg, &response); |
| 763 | SendResponse(&response); |
| 764 | } |
| 765 |
| 766 | void TurnServerAllocation::HandleChannelBindRequest(const TurnMessage* msg) { |
| 767 | // Check mandatory attributes. |
| 768 | const StunUInt32Attribute* channel_attr = |
| 769 | msg->GetUInt32(STUN_ATTR_CHANNEL_NUMBER); |
| 770 | const StunAddressAttribute* peer_attr = |
| 771 | msg->GetAddress(STUN_ATTR_XOR_PEER_ADDRESS); |
| 772 | if (!channel_attr || !peer_attr) { |
| 773 | SendBadRequestResponse(msg); |
| 774 | return; |
| 775 | } |
| 776 |
| 777 | // Check that channel id is valid. |
| 778 | int channel_id = channel_attr->value() >> 16; |
| 779 | if (channel_id < kMinChannelNumber || channel_id > kMaxChannelNumber) { |
| 780 | SendBadRequestResponse(msg); |
| 781 | return; |
| 782 | } |
| 783 |
| 784 | // Check that this channel id isn't bound to another transport address, and |
| 785 | // that this transport address isn't bound to another channel id. |
| 786 | Channel* channel1 = FindChannel(channel_id); |
| 787 | Channel* channel2 = FindChannel(peer_attr->GetAddress()); |
| 788 | if (channel1 != channel2) { |
| 789 | SendBadRequestResponse(msg); |
| 790 | return; |
| 791 | } |
| 792 |
| 793 | // Add or refresh this channel. |
| 794 | if (!channel1) { |
| 795 | channel1 = new Channel(thread_, channel_id, peer_attr->GetAddress()); |
| 796 | channel1->SignalDestroyed.connect( |
| 797 | this, &TurnServerAllocation::OnChannelDestroyed); |
| 798 | channels_.push_back(channel1); |
| 799 | } else { |
| 800 | channel1->Refresh(); |
| 801 | } |
| 802 |
| 803 | // Channel binds also refresh permissions. |
| 804 | AddPermission(peer_attr->GetAddress().ipaddr()); |
| 805 |
| 806 | RTC_LOG(LS_INFO) << ToString() << ": Bound channel, id=" << channel_id |
| 807 | << ", peer=" << peer_attr->GetAddress().ToSensitiveString(); |
| 808 |
| 809 | // Send a success response. |
| 810 | TurnMessage response; |
| 811 | InitResponse(msg, &response); |
| 812 | SendResponse(&response); |
| 813 | } |
| 814 |
| 815 | void TurnServerAllocation::HandleChannelData(const char* data, size_t size) { |
| 816 | // Extract the channel number from the data. |
| 817 | uint16_t channel_id = rtc::GetBE16(data); |
| 818 | Channel* channel = FindChannel(channel_id); |
| 819 | if (channel) { |
| 820 | // Send the data to the peer address. |
| 821 | SendExternal(data + TURN_CHANNEL_HEADER_SIZE, |
| 822 | size - TURN_CHANNEL_HEADER_SIZE, channel->peer()); |
| 823 | } else { |
| 824 | RTC_LOG(LS_WARNING) << ToString() |
| 825 | << ": Received channel data for invalid channel, id=" |
| 826 | << channel_id; |
| 827 | } |
| 828 | } |
| 829 |
| 830 | void TurnServerAllocation::OnExternalPacket( |
| 831 | rtc::AsyncPacketSocket* socket, |
| 832 | const char* data, |
| 833 | size_t size, |
| 834 | const rtc::SocketAddress& addr, |
| 835 | const int64_t& /* packet_time_us */) { |
| 836 | RTC_DCHECK(external_socket_.get() == socket); |
| 837 | Channel* channel = FindChannel(addr); |
| 838 | if (channel) { |
| 839 | // There is a channel bound to this address. Send as a channel message. |
| 840 | rtc::ByteBufferWriter buf; |
| 841 | buf.WriteUInt16(channel->id()); |
| 842 | buf.WriteUInt16(static_cast<uint16_t>(size)); |
| 843 | buf.WriteBytes(data, size); |
| 844 | server_->Send(&conn_, buf); |
| 845 | } else if (!server_->enable_permission_checks_ || |
| 846 | HasPermission(addr.ipaddr())) { |
| 847 | // No channel, but a permission exists. Send as a data indication. |
| 848 | TurnMessage msg; |
| 849 | msg.SetType(TURN_DATA_INDICATION); |
| 850 | msg.SetTransactionID(rtc::CreateRandomString(kStunTransactionIdLength)); |
| 851 | msg.AddAttribute(std::make_unique<StunXorAddressAttribute>( |
| 852 | STUN_ATTR_XOR_PEER_ADDRESS, addr)); |
| 853 | msg.AddAttribute( |
| 854 | std::make_unique<StunByteStringAttribute>(STUN_ATTR_DATA, data, size)); |
| 855 | server_->SendStun(&conn_, &msg); |
| 856 | } else { |
| 857 | RTC_LOG(LS_WARNING) |
| 858 | << ToString() << ": Received external packet without permission, peer=" |
| 859 | << addr.ToSensitiveString(); |
| 860 | } |
| 861 | } |
| 862 |
| 863 | int TurnServerAllocation::ComputeLifetime(const TurnMessage* msg) { |
| 864 | // Return the smaller of our default lifetime and the requested lifetime. |
| 865 | int lifetime = kDefaultAllocationTimeout / 1000; // convert to seconds |
| 866 | const StunUInt32Attribute* lifetime_attr = msg->GetUInt32(STUN_ATTR_LIFETIME); |
| 867 | if (lifetime_attr && static_cast<int>(lifetime_attr->value()) < lifetime) { |
| 868 | lifetime = static_cast<int>(lifetime_attr->value()); |
| 869 | } |
| 870 | return lifetime; |
| 871 | } |
| 872 |
| 873 | bool TurnServerAllocation::HasPermission(const rtc::IPAddress& addr) { |
| 874 | return (FindPermission(addr) != NULL); |
| 875 | } |
| 876 |
| 877 | void TurnServerAllocation::AddPermission(const rtc::IPAddress& addr) { |
| 878 | Permission* perm = FindPermission(addr); |
| 879 | if (!perm) { |
| 880 | perm = new Permission(thread_, addr); |
| 881 | perm->SignalDestroyed.connect(this, |
| 882 | &TurnServerAllocation::OnPermissionDestroyed); |
| 883 | perms_.push_back(perm); |
| 884 | } else { |
| 885 | perm->Refresh(); |
| 886 | } |
| 887 | } |
| 888 |
| 889 | TurnServerAllocation::Permission* TurnServerAllocation::FindPermission( |
| 890 | const rtc::IPAddress& addr) const { |
| 891 | for (PermissionList::const_iterator it = perms_.begin(); it != perms_.end(); |
| 892 | ++it) { |
| 893 | if ((*it)->peer() == addr) |
| 894 | return *it; |
| 895 | } |
| 896 | return NULL; |
| 897 | } |
| 898 |
| 899 | TurnServerAllocation::Channel* TurnServerAllocation::FindChannel( |
| 900 | int channel_id) const { |
| 901 | for (ChannelList::const_iterator it = channels_.begin(); |
| 902 | it != channels_.end(); ++it) { |
| 903 | if ((*it)->id() == channel_id) |
| 904 | return *it; |
| 905 | } |
| 906 | return NULL; |
| 907 | } |
| 908 |
| 909 | TurnServerAllocation::Channel* TurnServerAllocation::FindChannel( |
| 910 | const rtc::SocketAddress& addr) const { |
| 911 | for (ChannelList::const_iterator it = channels_.begin(); |
| 912 | it != channels_.end(); ++it) { |
| 913 | if ((*it)->peer() == addr) |
| 914 | return *it; |
| 915 | } |
| 916 | return NULL; |
| 917 | } |
| 918 |
| 919 | void TurnServerAllocation::SendResponse(TurnMessage* msg) { |
| 920 | // Success responses always have M-I. |
| 921 | msg->AddMessageIntegrity(key_); |
| 922 | server_->SendStun(&conn_, msg); |
| 923 | } |
| 924 |
| 925 | void TurnServerAllocation::SendBadRequestResponse(const TurnMessage* req) { |
| 926 | SendErrorResponse(req, STUN_ERROR_BAD_REQUEST, STUN_ERROR_REASON_BAD_REQUEST); |
| 927 | } |
| 928 |
| 929 | void TurnServerAllocation::SendErrorResponse(const TurnMessage* req, |
| 930 | int code, |
| 931 | const std::string& reason) { |
| 932 | server_->SendErrorResponse(&conn_, req, code, reason); |
| 933 | } |
| 934 |
| 935 | void TurnServerAllocation::SendExternal(const void* data, |
| 936 | size_t size, |
| 937 | const rtc::SocketAddress& peer) { |
| 938 | rtc::PacketOptions options; |
| 939 | external_socket_->SendTo(data, size, peer, options); |
| 940 | } |
| 941 |
| 942 | void TurnServerAllocation::OnMessage(rtc::Message* msg) { |
| 943 | RTC_DCHECK(msg->message_id == MSG_ALLOCATION_TIMEOUT); |
| 944 | SignalDestroyed(this); |
| 945 | delete this; |
| 946 | } |
| 947 |
| 948 | void TurnServerAllocation::OnPermissionDestroyed(Permission* perm) { |
| 949 | auto it = absl::c_find(perms_, perm); |
| 950 | RTC_DCHECK(it != perms_.end()); |
| 951 | perms_.erase(it); |
| 952 | } |
| 953 |
| 954 | void TurnServerAllocation::OnChannelDestroyed(Channel* channel) { |
| 955 | auto it = absl::c_find(channels_, channel); |
| 956 | RTC_DCHECK(it != channels_.end()); |
| 957 | channels_.erase(it); |
| 958 | } |
| 959 |
| 960 | TurnServerAllocation::Permission::Permission(rtc::Thread* thread, |
| 961 | const rtc::IPAddress& peer) |
| 962 | : thread_(thread), peer_(peer) { |
| 963 | Refresh(); |
| 964 | } |
| 965 |
| 966 | TurnServerAllocation::Permission::~Permission() { |
| 967 | thread_->Clear(this, MSG_ALLOCATION_TIMEOUT); |
| 968 | } |
| 969 |
| 970 | void TurnServerAllocation::Permission::Refresh() { |
| 971 | thread_->Clear(this, MSG_ALLOCATION_TIMEOUT); |
| 972 | thread_->PostDelayed(RTC_FROM_HERE, kPermissionTimeout, this, |
| 973 | MSG_ALLOCATION_TIMEOUT); |
| 974 | } |
| 975 |
| 976 | void TurnServerAllocation::Permission::OnMessage(rtc::Message* msg) { |
| 977 | RTC_DCHECK(msg->message_id == MSG_ALLOCATION_TIMEOUT); |
| 978 | SignalDestroyed(this); |
| 979 | delete this; |
| 980 | } |
| 981 |
| 982 | TurnServerAllocation::Channel::Channel(rtc::Thread* thread, |
| 983 | int id, |
| 984 | const rtc::SocketAddress& peer) |
| 985 | : thread_(thread), id_(id), peer_(peer) { |
| 986 | Refresh(); |
| 987 | } |
| 988 |
| 989 | TurnServerAllocation::Channel::~Channel() { |
| 990 | thread_->Clear(this, MSG_ALLOCATION_TIMEOUT); |
| 991 | } |
| 992 |
| 993 | void TurnServerAllocation::Channel::Refresh() { |
| 994 | thread_->Clear(this, MSG_ALLOCATION_TIMEOUT); |
| 995 | thread_->PostDelayed(RTC_FROM_HERE, kChannelTimeout, this, |
| 996 | MSG_ALLOCATION_TIMEOUT); |
| 997 | } |
| 998 |
| 999 | void TurnServerAllocation::Channel::OnMessage(rtc::Message* msg) { |
| 1000 | RTC_DCHECK(msg->message_id == MSG_ALLOCATION_TIMEOUT); |
| 1001 | SignalDestroyed(this); |
| 1002 | delete this; |
| 1003 | } |
| 1004 |
| 1005 | } // namespace cricket |
| 1006 |