| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2012 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/p2p/base/turnserver.h" | 11 #include "webrtc/p2p/base/turnserver.h" |
| 12 | 12 |
| 13 #include <tuple> // for std::tie |
| 14 |
| 13 #include "webrtc/p2p/base/asyncstuntcpsocket.h" | 15 #include "webrtc/p2p/base/asyncstuntcpsocket.h" |
| 14 #include "webrtc/p2p/base/common.h" | 16 #include "webrtc/p2p/base/common.h" |
| 15 #include "webrtc/p2p/base/packetsocketfactory.h" | 17 #include "webrtc/p2p/base/packetsocketfactory.h" |
| 16 #include "webrtc/p2p/base/stun.h" | 18 #include "webrtc/p2p/base/stun.h" |
| 17 #include "webrtc/base/bytebuffer.h" | 19 #include "webrtc/base/bytebuffer.h" |
| 18 #include "webrtc/base/helpers.h" | 20 #include "webrtc/base/helpers.h" |
| 19 #include "webrtc/base/logging.h" | 21 #include "webrtc/base/logging.h" |
| 20 #include "webrtc/base/messagedigest.h" | 22 #include "webrtc/base/messagedigest.h" |
| 21 #include "webrtc/base/socketadapters.h" | 23 #include "webrtc/base/socketadapters.h" |
| 22 #include "webrtc/base/stringencode.h" | 24 #include "webrtc/base/stringencode.h" |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 | 119 |
| 118 TurnServer::TurnServer(rtc::Thread* thread) | 120 TurnServer::TurnServer(rtc::Thread* thread) |
| 119 : thread_(thread), | 121 : thread_(thread), |
| 120 nonce_key_(rtc::CreateRandomString(kNonceKeySize)), | 122 nonce_key_(rtc::CreateRandomString(kNonceKeySize)), |
| 121 auth_hook_(NULL), | 123 auth_hook_(NULL), |
| 122 redirect_hook_(NULL), | 124 redirect_hook_(NULL), |
| 123 enable_otu_nonce_(false) { | 125 enable_otu_nonce_(false) { |
| 124 } | 126 } |
| 125 | 127 |
| 126 TurnServer::~TurnServer() { | 128 TurnServer::~TurnServer() { |
| 127 for (AllocationMap::iterator it = allocations_.begin(); | |
| 128 it != allocations_.end(); ++it) { | |
| 129 delete it->second; | |
| 130 } | |
| 131 | |
| 132 for (InternalSocketMap::iterator it = server_sockets_.begin(); | 129 for (InternalSocketMap::iterator it = server_sockets_.begin(); |
| 133 it != server_sockets_.end(); ++it) { | 130 it != server_sockets_.end(); ++it) { |
| 134 rtc::AsyncPacketSocket* socket = it->first; | 131 rtc::AsyncPacketSocket* socket = it->first; |
| 135 delete socket; | 132 delete socket; |
| 136 } | 133 } |
| 137 | 134 |
| 138 for (ServerSocketMap::iterator it = server_listen_sockets_.begin(); | 135 for (ServerSocketMap::iterator it = server_listen_sockets_.begin(); |
| 139 it != server_listen_sockets_.end(); ++it) { | 136 it != server_listen_sockets_.end(); ++it) { |
| 140 rtc::AsyncSocket* socket = it->first; | 137 rtc::AsyncSocket* socket = it->first; |
| 141 delete socket; | 138 delete socket; |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 rtc::DIGEST_MD5, nonce_key_, std::string(p, sizeof(then)))) { | 419 rtc::DIGEST_MD5, nonce_key_, std::string(p, sizeof(then)))) { |
| 423 return false; | 420 return false; |
| 424 } | 421 } |
| 425 | 422 |
| 426 // Validate the timestamp. | 423 // Validate the timestamp. |
| 427 return rtc::TimeMillis() - then < kNonceTimeout; | 424 return rtc::TimeMillis() - then < kNonceTimeout; |
| 428 } | 425 } |
| 429 | 426 |
| 430 TurnServerAllocation* TurnServer::FindAllocation(TurnServerConnection* conn) { | 427 TurnServerAllocation* TurnServer::FindAllocation(TurnServerConnection* conn) { |
| 431 AllocationMap::const_iterator it = allocations_.find(*conn); | 428 AllocationMap::const_iterator it = allocations_.find(*conn); |
| 432 return (it != allocations_.end()) ? it->second : NULL; | 429 return (it != allocations_.end()) ? it->second.get() : nullptr; |
| 433 } | 430 } |
| 434 | 431 |
| 435 TurnServerAllocation* TurnServer::CreateAllocation(TurnServerConnection* conn, | 432 TurnServerAllocation* TurnServer::CreateAllocation(TurnServerConnection* conn, |
| 436 int proto, | 433 int proto, |
| 437 const std::string& key) { | 434 const std::string& key) { |
| 438 rtc::AsyncPacketSocket* external_socket = (external_socket_factory_) ? | 435 rtc::AsyncPacketSocket* external_socket = (external_socket_factory_) ? |
| 439 external_socket_factory_->CreateUdpSocket(external_addr_, 0, 0) : NULL; | 436 external_socket_factory_->CreateUdpSocket(external_addr_, 0, 0) : NULL; |
| 440 if (!external_socket) { | 437 if (!external_socket) { |
| 441 return NULL; | 438 return NULL; |
| 442 } | 439 } |
| 443 | 440 |
| 444 // The Allocation takes ownership of the socket. | 441 // The Allocation takes ownership of the socket. |
| 445 TurnServerAllocation* allocation = new TurnServerAllocation(this, | 442 TurnServerAllocation* allocation = new TurnServerAllocation(this, |
| 446 thread_, *conn, external_socket, key); | 443 thread_, *conn, external_socket, key); |
| 447 allocation->SignalDestroyed.connect(this, &TurnServer::OnAllocationDestroyed); | 444 allocation->SignalDestroyed.connect(this, &TurnServer::OnAllocationDestroyed); |
| 448 allocations_[*conn] = allocation; | 445 allocations_[*conn].reset(allocation); |
| 449 return allocation; | 446 return allocation; |
| 450 } | 447 } |
| 451 | 448 |
| 452 void TurnServer::SendErrorResponse(TurnServerConnection* conn, | 449 void TurnServer::SendErrorResponse(TurnServerConnection* conn, |
| 453 const StunMessage* req, | 450 const StunMessage* req, |
| 454 int code, const std::string& reason) { | 451 int code, const std::string& reason) { |
| 455 TurnMessage resp; | 452 TurnMessage resp; |
| 456 InitErrorResponse(req, code, reason, &resp); | 453 InitErrorResponse(req, code, reason, &resp); |
| 457 LOG(LS_INFO) << "Sending error response, type=" << resp.type() | 454 LOG(LS_INFO) << "Sending error response, type=" << resp.type() |
| 458 << ", code=" << code << ", reason=" << reason; | 455 << ", code=" << code << ", reason=" << reason; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 511 InternalSocketMap::iterator iter = server_sockets_.find(socket); | 508 InternalSocketMap::iterator iter = server_sockets_.find(socket); |
| 512 // Skip if the socket serving this allocation is UDP, as this will be shared | 509 // Skip if the socket serving this allocation is UDP, as this will be shared |
| 513 // by all allocations. | 510 // by all allocations. |
| 514 // Note: We may not find a socket if it's a TCP socket that was closed, and | 511 // Note: We may not find a socket if it's a TCP socket that was closed, and |
| 515 // the allocation is only now timing out. | 512 // the allocation is only now timing out. |
| 516 if (iter != server_sockets_.end() && iter->second != cricket::PROTO_UDP) { | 513 if (iter != server_sockets_.end() && iter->second != cricket::PROTO_UDP) { |
| 517 DestroyInternalSocket(socket); | 514 DestroyInternalSocket(socket); |
| 518 } | 515 } |
| 519 | 516 |
| 520 AllocationMap::iterator it = allocations_.find(*(allocation->conn())); | 517 AllocationMap::iterator it = allocations_.find(*(allocation->conn())); |
| 521 if (it != allocations_.end()) | 518 if (it != allocations_.end()) { |
| 519 it->second.release(); |
| 522 allocations_.erase(it); | 520 allocations_.erase(it); |
| 521 } |
| 523 } | 522 } |
| 524 | 523 |
| 525 void TurnServer::DestroyInternalSocket(rtc::AsyncPacketSocket* socket) { | 524 void TurnServer::DestroyInternalSocket(rtc::AsyncPacketSocket* socket) { |
| 526 InternalSocketMap::iterator iter = server_sockets_.find(socket); | 525 InternalSocketMap::iterator iter = server_sockets_.find(socket); |
| 527 if (iter != server_sockets_.end()) { | 526 if (iter != server_sockets_.end()) { |
| 528 rtc::AsyncPacketSocket* socket = iter->first; | 527 rtc::AsyncPacketSocket* socket = iter->first; |
| 529 // We must destroy the socket async to avoid invalidating the sigslot | 528 // We must destroy the socket async to avoid invalidating the sigslot |
| 530 // callback list iterator inside a sigslot callback. | 529 // callback list iterator inside a sigslot callback. |
| 531 rtc::Thread::Current()->Dispose(socket); | 530 rtc::Thread::Current()->Dispose(socket); |
| 532 server_sockets_.erase(iter); | 531 server_sockets_.erase(iter); |
| 533 } | 532 } |
| 534 } | 533 } |
| 535 | 534 |
| 536 TurnServerConnection::TurnServerConnection(const rtc::SocketAddress& src, | 535 TurnServerConnection::TurnServerConnection(const rtc::SocketAddress& src, |
| 537 ProtocolType proto, | 536 ProtocolType proto, |
| 538 rtc::AsyncPacketSocket* socket) | 537 rtc::AsyncPacketSocket* socket) |
| 539 : src_(src), | 538 : src_(src), |
| 540 dst_(socket->GetRemoteAddress()), | 539 dst_(socket->GetRemoteAddress()), |
| 541 proto_(proto), | 540 proto_(proto), |
| 542 socket_(socket) { | 541 socket_(socket) { |
| 543 } | 542 } |
| 544 | 543 |
| 545 bool TurnServerConnection::operator==(const TurnServerConnection& c) const { | 544 bool TurnServerConnection::operator==(const TurnServerConnection& c) const { |
| 546 return src_ == c.src_ && dst_ == c.dst_ && proto_ == c.proto_; | 545 return src_ == c.src_ && dst_ == c.dst_ && proto_ == c.proto_; |
| 547 } | 546 } |
| 548 | 547 |
| 549 bool TurnServerConnection::operator<(const TurnServerConnection& c) const { | 548 bool TurnServerConnection::operator<(const TurnServerConnection& c) const { |
| 550 return src_ < c.src_ || dst_ < c.dst_ || proto_ < c.proto_; | 549 return std::tie(src_, dst_, proto_) < std::tie(c.src_, c.dst_, c.proto_); |
| 551 } | 550 } |
| 552 | 551 |
| 553 std::string TurnServerConnection::ToString() const { | 552 std::string TurnServerConnection::ToString() const { |
| 554 const char* const kProtos[] = { | 553 const char* const kProtos[] = { |
| 555 "unknown", "udp", "tcp", "ssltcp" | 554 "unknown", "udp", "tcp", "ssltcp" |
| 556 }; | 555 }; |
| 557 std::ostringstream ost; | 556 std::ostringstream ost; |
| 558 ost << src_.ToString() << "-" << dst_.ToString() << ":"<< kProtos[proto_]; | 557 ost << src_.ToString() << "-" << dst_.ToString() << ":"<< kProtos[proto_]; |
| 559 return ost.str(); | 558 return ost.str(); |
| 560 } | 559 } |
| (...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 954 MSG_ALLOCATION_TIMEOUT); | 953 MSG_ALLOCATION_TIMEOUT); |
| 955 } | 954 } |
| 956 | 955 |
| 957 void TurnServerAllocation::Channel::OnMessage(rtc::Message* msg) { | 956 void TurnServerAllocation::Channel::OnMessage(rtc::Message* msg) { |
| 958 ASSERT(msg->message_id == MSG_ALLOCATION_TIMEOUT); | 957 ASSERT(msg->message_id == MSG_ALLOCATION_TIMEOUT); |
| 959 SignalDestroyed(this); | 958 SignalDestroyed(this); |
| 960 delete this; | 959 delete this; |
| 961 } | 960 } |
| 962 | 961 |
| 963 } // namespace cricket | 962 } // namespace cricket |
| OLD | NEW |