| 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 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 int resp_type = (req) ? GetStunErrorResponseType(req->type()) : -1; | 111 int resp_type = (req) ? GetStunErrorResponseType(req->type()) : -1; |
| 112 if (resp_type == -1) | 112 if (resp_type == -1) |
| 113 return false; | 113 return false; |
| 114 resp->SetType(resp_type); | 114 resp->SetType(resp_type); |
| 115 resp->SetTransactionID(req->transaction_id()); | 115 resp->SetTransactionID(req->transaction_id()); |
| 116 resp->AddAttribute(new cricket::StunErrorCodeAttribute( | 116 resp->AddAttribute(new cricket::StunErrorCodeAttribute( |
| 117 STUN_ATTR_ERROR_CODE, code, reason)); | 117 STUN_ATTR_ERROR_CODE, code, reason)); |
| 118 return true; | 118 return true; |
| 119 } | 119 } |
| 120 | 120 |
| 121 | |
| 122 TurnServer::TurnServer(rtc::Thread* thread) | 121 TurnServer::TurnServer(rtc::Thread* thread) |
| 123 : thread_(thread), | 122 : thread_(thread), |
| 124 nonce_key_(rtc::CreateRandomString(kNonceKeySize)), | 123 nonce_key_(rtc::CreateRandomString(kNonceKeySize)), |
| 125 auth_hook_(NULL), | 124 auth_hook_(nullptr), |
| 126 redirect_hook_(NULL), | 125 redirect_hook_(nullptr), |
| 127 enable_otu_nonce_(false) { | 126 enable_otu_nonce_(false) {} |
| 128 } | |
| 129 | 127 |
| 130 TurnServer::~TurnServer() { | 128 TurnServer::~TurnServer() { |
| 131 for (InternalSocketMap::iterator it = server_sockets_.begin(); | 129 for (InternalSocketMap::iterator it = server_sockets_.begin(); |
| 132 it != server_sockets_.end(); ++it) { | 130 it != server_sockets_.end(); ++it) { |
| 133 rtc::AsyncPacketSocket* socket = it->first; | 131 rtc::AsyncPacketSocket* socket = it->first; |
| 134 delete socket; | 132 delete socket; |
| 135 } | 133 } |
| 136 | 134 |
| 137 for (ServerSocketMap::iterator it = server_listen_sockets_.begin(); | 135 for (ServerSocketMap::iterator it = server_listen_sockets_.begin(); |
| 138 it != server_listen_sockets_.end(); ++it) { | 136 it != server_listen_sockets_.end(); ++it) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 166 void TurnServer::OnNewInternalConnection(rtc::AsyncSocket* socket) { | 164 void TurnServer::OnNewInternalConnection(rtc::AsyncSocket* socket) { |
| 167 RTC_DCHECK(server_listen_sockets_.find(socket) != | 165 RTC_DCHECK(server_listen_sockets_.find(socket) != |
| 168 server_listen_sockets_.end()); | 166 server_listen_sockets_.end()); |
| 169 AcceptConnection(socket); | 167 AcceptConnection(socket); |
| 170 } | 168 } |
| 171 | 169 |
| 172 void TurnServer::AcceptConnection(rtc::AsyncSocket* server_socket) { | 170 void TurnServer::AcceptConnection(rtc::AsyncSocket* server_socket) { |
| 173 // Check if someone is trying to connect to us. | 171 // Check if someone is trying to connect to us. |
| 174 rtc::SocketAddress accept_addr; | 172 rtc::SocketAddress accept_addr; |
| 175 rtc::AsyncSocket* accepted_socket = server_socket->Accept(&accept_addr); | 173 rtc::AsyncSocket* accepted_socket = server_socket->Accept(&accept_addr); |
| 176 if (accepted_socket != NULL) { | 174 if (accepted_socket != nullptr) { |
| 177 ProtocolType proto = server_listen_sockets_[server_socket]; | 175 ProtocolType proto = server_listen_sockets_[server_socket]; |
| 178 cricket::AsyncStunTCPSocket* tcp_socket = | 176 cricket::AsyncStunTCPSocket* tcp_socket = |
| 179 new cricket::AsyncStunTCPSocket(accepted_socket, false); | 177 new cricket::AsyncStunTCPSocket(accepted_socket, false); |
| 180 | 178 |
| 181 tcp_socket->SignalClose.connect(this, &TurnServer::OnInternalSocketClose); | 179 tcp_socket->SignalClose.connect(this, &TurnServer::OnInternalSocketClose); |
| 182 // Finally add the socket so it can start communicating with the client. | 180 // Finally add the socket so it can start communicating with the client. |
| 183 AddInternalSocket(tcp_socket, proto); | 181 AddInternalSocket(tcp_socket, proto); |
| 184 } | 182 } |
| 185 } | 183 } |
| 186 | 184 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 LOG(LS_WARNING) << "Received invalid STUN message"; | 219 LOG(LS_WARNING) << "Received invalid STUN message"; |
| 222 return; | 220 return; |
| 223 } | 221 } |
| 224 | 222 |
| 225 // If it's a STUN binding request, handle that specially. | 223 // If it's a STUN binding request, handle that specially. |
| 226 if (msg.type() == STUN_BINDING_REQUEST) { | 224 if (msg.type() == STUN_BINDING_REQUEST) { |
| 227 HandleBindingRequest(conn, &msg); | 225 HandleBindingRequest(conn, &msg); |
| 228 return; | 226 return; |
| 229 } | 227 } |
| 230 | 228 |
| 231 if (redirect_hook_ != NULL && msg.type() == STUN_ALLOCATE_REQUEST) { | 229 if (redirect_hook_ != nullptr && msg.type() == STUN_ALLOCATE_REQUEST) { |
| 232 rtc::SocketAddress address; | 230 rtc::SocketAddress address; |
| 233 if (redirect_hook_->ShouldRedirect(conn->src(), &address)) { | 231 if (redirect_hook_->ShouldRedirect(conn->src(), &address)) { |
| 234 SendErrorResponseWithAlternateServer( | 232 SendErrorResponseWithAlternateServer( |
| 235 conn, &msg, address); | 233 conn, &msg, address); |
| 236 return; | 234 return; |
| 237 } | 235 } |
| 238 } | 236 } |
| 239 | 237 |
| 240 // Look up the key that we'll use to validate the M-I. If we have an | 238 // Look up the key that we'll use to validate the M-I. If we have an |
| 241 // existing allocation, the key will already be cached. | 239 // existing allocation, the key will already be cached. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 } | 275 } |
| 278 | 276 |
| 279 bool TurnServer::GetKey(const StunMessage* msg, std::string* key) { | 277 bool TurnServer::GetKey(const StunMessage* msg, std::string* key) { |
| 280 const StunByteStringAttribute* username_attr = | 278 const StunByteStringAttribute* username_attr = |
| 281 msg->GetByteString(STUN_ATTR_USERNAME); | 279 msg->GetByteString(STUN_ATTR_USERNAME); |
| 282 if (!username_attr) { | 280 if (!username_attr) { |
| 283 return false; | 281 return false; |
| 284 } | 282 } |
| 285 | 283 |
| 286 std::string username = username_attr->GetString(); | 284 std::string username = username_attr->GetString(); |
| 287 return (auth_hook_ != NULL && auth_hook_->GetKey(username, realm_, key)); | 285 return (auth_hook_ != nullptr && auth_hook_->GetKey(username, realm_, key)); |
| 288 } | 286 } |
| 289 | 287 |
| 290 bool TurnServer::CheckAuthorization(TurnServerConnection* conn, | 288 bool TurnServer::CheckAuthorization(TurnServerConnection* conn, |
| 291 const StunMessage* msg, | 289 const StunMessage* msg, |
| 292 const char* data, size_t size, | 290 const char* data, size_t size, |
| 293 const std::string& key) { | 291 const std::string& key) { |
| 294 // RFC 5389, 10.2.2. | 292 // RFC 5389, 10.2.2. |
| 295 RTC_DCHECK(IsStunRequestType(msg->type())); | 293 RTC_DCHECK(IsStunRequestType(msg->type())); |
| 296 const StunByteStringAttribute* mi_attr = | 294 const StunByteStringAttribute* mi_attr = |
| 297 msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY); | 295 msg->GetByteString(STUN_ATTR_MESSAGE_INTEGRITY); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 } | 426 } |
| 429 | 427 |
| 430 TurnServerAllocation* TurnServer::FindAllocation(TurnServerConnection* conn) { | 428 TurnServerAllocation* TurnServer::FindAllocation(TurnServerConnection* conn) { |
| 431 AllocationMap::const_iterator it = allocations_.find(*conn); | 429 AllocationMap::const_iterator it = allocations_.find(*conn); |
| 432 return (it != allocations_.end()) ? it->second.get() : nullptr; | 430 return (it != allocations_.end()) ? it->second.get() : nullptr; |
| 433 } | 431 } |
| 434 | 432 |
| 435 TurnServerAllocation* TurnServer::CreateAllocation(TurnServerConnection* conn, | 433 TurnServerAllocation* TurnServer::CreateAllocation(TurnServerConnection* conn, |
| 436 int proto, | 434 int proto, |
| 437 const std::string& key) { | 435 const std::string& key) { |
| 438 rtc::AsyncPacketSocket* external_socket = (external_socket_factory_) ? | 436 rtc::AsyncPacketSocket* external_socket = |
| 439 external_socket_factory_->CreateUdpSocket(external_addr_, 0, 0) : NULL; | 437 (external_socket_factory_) |
| 438 ? external_socket_factory_->CreateUdpSocket(external_addr_, 0, 0) |
| 439 : nullptr; |
| 440 if (!external_socket) { | 440 if (!external_socket) { |
| 441 return NULL; | 441 return nullptr; |
| 442 } | 442 } |
| 443 | 443 |
| 444 // The Allocation takes ownership of the socket. | 444 // The Allocation takes ownership of the socket. |
| 445 TurnServerAllocation* allocation = new TurnServerAllocation(this, | 445 TurnServerAllocation* allocation = new TurnServerAllocation(this, |
| 446 thread_, *conn, external_socket, key); | 446 thread_, *conn, external_socket, key); |
| 447 allocation->SignalDestroyed.connect(this, &TurnServer::OnAllocationDestroyed); | 447 allocation->SignalDestroyed.connect(this, &TurnServer::OnAllocationDestroyed); |
| 448 allocations_[*conn].reset(allocation); | 448 allocations_[*conn].reset(allocation); |
| 449 return allocation; | 449 return allocation; |
| 450 } | 450 } |
| 451 | 451 |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 596 LOG_J(LS_INFO, this) << "Allocation destroyed"; | 596 LOG_J(LS_INFO, this) << "Allocation destroyed"; |
| 597 } | 597 } |
| 598 | 598 |
| 599 std::string TurnServerAllocation::ToString() const { | 599 std::string TurnServerAllocation::ToString() const { |
| 600 std::ostringstream ost; | 600 std::ostringstream ost; |
| 601 ost << "Alloc[" << conn_.ToString() << "]"; | 601 ost << "Alloc[" << conn_.ToString() << "]"; |
| 602 return ost.str(); | 602 return ost.str(); |
| 603 } | 603 } |
| 604 | 604 |
| 605 void TurnServerAllocation::HandleTurnMessage(const TurnMessage* msg) { | 605 void TurnServerAllocation::HandleTurnMessage(const TurnMessage* msg) { |
| 606 RTC_DCHECK(msg != NULL); | 606 RTC_DCHECK(msg != nullptr); |
| 607 switch (msg->type()) { | 607 switch (msg->type()) { |
| 608 case STUN_ALLOCATE_REQUEST: | 608 case STUN_ALLOCATE_REQUEST: |
| 609 HandleAllocateRequest(msg); | 609 HandleAllocateRequest(msg); |
| 610 break; | 610 break; |
| 611 case TURN_REFRESH_REQUEST: | 611 case TURN_REFRESH_REQUEST: |
| 612 HandleRefreshRequest(msg); | 612 HandleRefreshRequest(msg); |
| 613 break; | 613 break; |
| 614 case TURN_SEND_INDICATION: | 614 case TURN_SEND_INDICATION: |
| 615 HandleSendIndication(msg); | 615 HandleSendIndication(msg); |
| 616 break; | 616 break; |
| 617 case TURN_CREATE_PERMISSION_REQUEST: | 617 case TURN_CREATE_PERMISSION_REQUEST: |
| 618 HandleCreatePermissionRequest(msg); | 618 HandleCreatePermissionRequest(msg); |
| 619 break; | 619 break; |
| 620 case TURN_CHANNEL_BIND_REQUEST: | 620 case TURN_CHANNEL_BIND_REQUEST: |
| 621 HandleChannelBindRequest(msg); | 621 HandleChannelBindRequest(msg); |
| 622 break; | 622 break; |
| 623 default: | 623 default: |
| 624 // Not sure what to do with this, just eat it. | 624 // Not sure what to do with this, just eat it. |
| 625 LOG_J(LS_WARNING, this) << "Invalid TURN message type received: " | 625 LOG_J(LS_WARNING, this) << "Invalid TURN message type received: " |
| 626 << msg->type(); | 626 << msg->type(); |
| 627 } | 627 } |
| 628 } | 628 } |
| 629 | 629 |
| 630 void TurnServerAllocation::HandleAllocateRequest(const TurnMessage* msg) { | 630 void TurnServerAllocation::HandleAllocateRequest(const TurnMessage* msg) { |
| 631 // Copy the important info from the allocate request. | 631 // Copy the important info from the allocate request. |
| 632 transaction_id_ = msg->transaction_id(); | 632 transaction_id_ = msg->transaction_id(); |
| 633 const StunByteStringAttribute* username_attr = | 633 const StunByteStringAttribute* username_attr = |
| 634 msg->GetByteString(STUN_ATTR_USERNAME); | 634 msg->GetByteString(STUN_ATTR_USERNAME); |
| 635 RTC_DCHECK(username_attr != NULL); | 635 RTC_DCHECK(username_attr != nullptr); |
| 636 username_ = username_attr->GetString(); | 636 username_ = username_attr->GetString(); |
| 637 const StunByteStringAttribute* origin_attr = | 637 const StunByteStringAttribute* origin_attr = |
| 638 msg->GetByteString(STUN_ATTR_ORIGIN); | 638 msg->GetByteString(STUN_ATTR_ORIGIN); |
| 639 if (origin_attr) { | 639 if (origin_attr) { |
| 640 origin_ = origin_attr->GetString(); | 640 origin_ = origin_attr->GetString(); |
| 641 } | 641 } |
| 642 | 642 |
| 643 // Figure out the lifetime and start the allocation timer. | 643 // Figure out the lifetime and start the allocation timer. |
| 644 int lifetime_secs = ComputeLifetime(msg); | 644 int lifetime_secs = ComputeLifetime(msg); |
| 645 thread_->PostDelayed(RTC_FROM_HERE, lifetime_secs * 1000, this, | 645 thread_->PostDelayed(RTC_FROM_HERE, lifetime_secs * 1000, this, |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 834 // Return the smaller of our default lifetime and the requested lifetime. | 834 // Return the smaller of our default lifetime and the requested lifetime. |
| 835 int lifetime = kDefaultAllocationTimeout / 1000; // convert to seconds | 835 int lifetime = kDefaultAllocationTimeout / 1000; // convert to seconds |
| 836 const StunUInt32Attribute* lifetime_attr = msg->GetUInt32(STUN_ATTR_LIFETIME); | 836 const StunUInt32Attribute* lifetime_attr = msg->GetUInt32(STUN_ATTR_LIFETIME); |
| 837 if (lifetime_attr && static_cast<int>(lifetime_attr->value()) < lifetime) { | 837 if (lifetime_attr && static_cast<int>(lifetime_attr->value()) < lifetime) { |
| 838 lifetime = static_cast<int>(lifetime_attr->value()); | 838 lifetime = static_cast<int>(lifetime_attr->value()); |
| 839 } | 839 } |
| 840 return lifetime; | 840 return lifetime; |
| 841 } | 841 } |
| 842 | 842 |
| 843 bool TurnServerAllocation::HasPermission(const rtc::IPAddress& addr) { | 843 bool TurnServerAllocation::HasPermission(const rtc::IPAddress& addr) { |
| 844 return (FindPermission(addr) != NULL); | 844 return (FindPermission(addr) != nullptr); |
| 845 } | 845 } |
| 846 | 846 |
| 847 void TurnServerAllocation::AddPermission(const rtc::IPAddress& addr) { | 847 void TurnServerAllocation::AddPermission(const rtc::IPAddress& addr) { |
| 848 Permission* perm = FindPermission(addr); | 848 Permission* perm = FindPermission(addr); |
| 849 if (!perm) { | 849 if (!perm) { |
| 850 perm = new Permission(thread_, addr); | 850 perm = new Permission(thread_, addr); |
| 851 perm->SignalDestroyed.connect( | 851 perm->SignalDestroyed.connect( |
| 852 this, &TurnServerAllocation::OnPermissionDestroyed); | 852 this, &TurnServerAllocation::OnPermissionDestroyed); |
| 853 perms_.push_back(perm); | 853 perms_.push_back(perm); |
| 854 } else { | 854 } else { |
| 855 perm->Refresh(); | 855 perm->Refresh(); |
| 856 } | 856 } |
| 857 } | 857 } |
| 858 | 858 |
| 859 TurnServerAllocation::Permission* TurnServerAllocation::FindPermission( | 859 TurnServerAllocation::Permission* TurnServerAllocation::FindPermission( |
| 860 const rtc::IPAddress& addr) const { | 860 const rtc::IPAddress& addr) const { |
| 861 for (PermissionList::const_iterator it = perms_.begin(); | 861 for (PermissionList::const_iterator it = perms_.begin(); |
| 862 it != perms_.end(); ++it) { | 862 it != perms_.end(); ++it) { |
| 863 if ((*it)->peer() == addr) | 863 if ((*it)->peer() == addr) |
| 864 return *it; | 864 return *it; |
| 865 } | 865 } |
| 866 return NULL; | 866 return nullptr; |
| 867 } | 867 } |
| 868 | 868 |
| 869 TurnServerAllocation::Channel* TurnServerAllocation::FindChannel( | 869 TurnServerAllocation::Channel* TurnServerAllocation::FindChannel( |
| 870 int channel_id) const { | 870 int channel_id) const { |
| 871 for (ChannelList::const_iterator it = channels_.begin(); | 871 for (ChannelList::const_iterator it = channels_.begin(); |
| 872 it != channels_.end(); ++it) { | 872 it != channels_.end(); ++it) { |
| 873 if ((*it)->id() == channel_id) | 873 if ((*it)->id() == channel_id) |
| 874 return *it; | 874 return *it; |
| 875 } | 875 } |
| 876 return NULL; | 876 return nullptr; |
| 877 } | 877 } |
| 878 | 878 |
| 879 TurnServerAllocation::Channel* TurnServerAllocation::FindChannel( | 879 TurnServerAllocation::Channel* TurnServerAllocation::FindChannel( |
| 880 const rtc::SocketAddress& addr) const { | 880 const rtc::SocketAddress& addr) const { |
| 881 for (ChannelList::const_iterator it = channels_.begin(); | 881 for (ChannelList::const_iterator it = channels_.begin(); |
| 882 it != channels_.end(); ++it) { | 882 it != channels_.end(); ++it) { |
| 883 if ((*it)->peer() == addr) | 883 if ((*it)->peer() == addr) |
| 884 return *it; | 884 return *it; |
| 885 } | 885 } |
| 886 return NULL; | 886 return nullptr; |
| 887 } | 887 } |
| 888 | 888 |
| 889 void TurnServerAllocation::SendResponse(TurnMessage* msg) { | 889 void TurnServerAllocation::SendResponse(TurnMessage* msg) { |
| 890 // Success responses always have M-I. | 890 // Success responses always have M-I. |
| 891 msg->AddMessageIntegrity(key_); | 891 msg->AddMessageIntegrity(key_); |
| 892 server_->SendStun(&conn_, msg); | 892 server_->SendStun(&conn_, msg); |
| 893 } | 893 } |
| 894 | 894 |
| 895 void TurnServerAllocation::SendBadRequestResponse(const TurnMessage* req) { | 895 void TurnServerAllocation::SendBadRequestResponse(const TurnMessage* req) { |
| 896 SendErrorResponse(req, STUN_ERROR_BAD_REQUEST, STUN_ERROR_REASON_BAD_REQUEST); | 896 SendErrorResponse(req, STUN_ERROR_BAD_REQUEST, STUN_ERROR_REASON_BAD_REQUEST); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 964 MSG_ALLOCATION_TIMEOUT); | 964 MSG_ALLOCATION_TIMEOUT); |
| 965 } | 965 } |
| 966 | 966 |
| 967 void TurnServerAllocation::Channel::OnMessage(rtc::Message* msg) { | 967 void TurnServerAllocation::Channel::OnMessage(rtc::Message* msg) { |
| 968 RTC_DCHECK(msg->message_id == MSG_ALLOCATION_TIMEOUT); | 968 RTC_DCHECK(msg->message_id == MSG_ALLOCATION_TIMEOUT); |
| 969 SignalDestroyed(this); | 969 SignalDestroyed(this); |
| 970 delete this; | 970 delete this; |
| 971 } | 971 } |
| 972 | 972 |
| 973 } // namespace cricket | 973 } // namespace cricket |
| OLD | NEW |