| 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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 return true; | 106 return true; |
| 107 } | 107 } |
| 108 | 108 |
| 109 static bool InitErrorResponse(const StunMessage* req, int code, | 109 static bool InitErrorResponse(const StunMessage* req, int code, |
| 110 const std::string& reason, StunMessage* resp) { | 110 const std::string& reason, StunMessage* resp) { |
| 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 VERIFY(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 | 121 |
| 122 TurnServer::TurnServer(rtc::Thread* thread) | 122 TurnServer::TurnServer(rtc::Thread* thread) |
| 123 : thread_(thread), | 123 : thread_(thread), |
| 124 nonce_key_(rtc::CreateRandomString(kNonceKeySize)), | 124 nonce_key_(rtc::CreateRandomString(kNonceKeySize)), |
| 125 auth_hook_(NULL), | 125 auth_hook_(NULL), |
| 126 redirect_hook_(NULL), | 126 redirect_hook_(NULL), |
| 127 enable_otu_nonce_(false) { | 127 enable_otu_nonce_(false) { |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 349 | 349 |
| 350 void TurnServer::HandleBindingRequest(TurnServerConnection* conn, | 350 void TurnServer::HandleBindingRequest(TurnServerConnection* conn, |
| 351 const StunMessage* req) { | 351 const StunMessage* req) { |
| 352 StunMessage response; | 352 StunMessage response; |
| 353 InitResponse(req, &response); | 353 InitResponse(req, &response); |
| 354 | 354 |
| 355 // Tell the user the address that we received their request from. | 355 // Tell the user the address that we received their request from. |
| 356 StunAddressAttribute* mapped_addr_attr; | 356 StunAddressAttribute* mapped_addr_attr; |
| 357 mapped_addr_attr = new StunXorAddressAttribute( | 357 mapped_addr_attr = new StunXorAddressAttribute( |
| 358 STUN_ATTR_XOR_MAPPED_ADDRESS, conn->src()); | 358 STUN_ATTR_XOR_MAPPED_ADDRESS, conn->src()); |
| 359 VERIFY(response.AddAttribute(mapped_addr_attr)); | 359 response.AddAttribute(mapped_addr_attr); |
| 360 | 360 |
| 361 SendStun(conn, &response); | 361 SendStun(conn, &response); |
| 362 } | 362 } |
| 363 | 363 |
| 364 void TurnServer::HandleAllocateRequest(TurnServerConnection* conn, | 364 void TurnServer::HandleAllocateRequest(TurnServerConnection* conn, |
| 365 const TurnMessage* msg, | 365 const TurnMessage* msg, |
| 366 const std::string& key) { | 366 const std::string& key) { |
| 367 // Check the parameters in the request. | 367 // Check the parameters in the request. |
| 368 const StunUInt32Attribute* transport_attr = | 368 const StunUInt32Attribute* transport_attr = |
| 369 msg->GetUInt32(STUN_ATTR_REQUESTED_TRANSPORT); | 369 msg->GetUInt32(STUN_ATTR_REQUESTED_TRANSPORT); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 TurnServerConnection* conn, const StunMessage* msg, | 463 TurnServerConnection* conn, const StunMessage* msg, |
| 464 int code, const std::string& reason) { | 464 int code, const std::string& reason) { |
| 465 TurnMessage resp; | 465 TurnMessage resp; |
| 466 InitErrorResponse(msg, code, reason, &resp); | 466 InitErrorResponse(msg, code, reason, &resp); |
| 467 | 467 |
| 468 int64_t timestamp = rtc::TimeMillis(); | 468 int64_t timestamp = rtc::TimeMillis(); |
| 469 if (ts_for_next_nonce_) { | 469 if (ts_for_next_nonce_) { |
| 470 timestamp = ts_for_next_nonce_; | 470 timestamp = ts_for_next_nonce_; |
| 471 ts_for_next_nonce_ = 0; | 471 ts_for_next_nonce_ = 0; |
| 472 } | 472 } |
| 473 VERIFY(resp.AddAttribute( | 473 resp.AddAttribute( |
| 474 new StunByteStringAttribute(STUN_ATTR_NONCE, GenerateNonce(timestamp)))); | 474 new StunByteStringAttribute(STUN_ATTR_NONCE, GenerateNonce(timestamp))); |
| 475 VERIFY(resp.AddAttribute(new StunByteStringAttribute( | 475 resp.AddAttribute(new StunByteStringAttribute( |
| 476 STUN_ATTR_REALM, realm_))); | 476 STUN_ATTR_REALM, realm_)); |
| 477 SendStun(conn, &resp); | 477 SendStun(conn, &resp); |
| 478 } | 478 } |
| 479 | 479 |
| 480 void TurnServer::SendErrorResponseWithAlternateServer( | 480 void TurnServer::SendErrorResponseWithAlternateServer( |
| 481 TurnServerConnection* conn, const StunMessage* msg, | 481 TurnServerConnection* conn, const StunMessage* msg, |
| 482 const rtc::SocketAddress& addr) { | 482 const rtc::SocketAddress& addr) { |
| 483 TurnMessage resp; | 483 TurnMessage resp; |
| 484 InitErrorResponse(msg, STUN_ERROR_TRY_ALTERNATE, | 484 InitErrorResponse(msg, STUN_ERROR_TRY_ALTERNATE, |
| 485 STUN_ERROR_REASON_TRY_ALTERNATE_SERVER, &resp); | 485 STUN_ERROR_REASON_TRY_ALTERNATE_SERVER, &resp); |
| 486 VERIFY(resp.AddAttribute(new StunAddressAttribute( | 486 resp.AddAttribute(new StunAddressAttribute( |
| 487 STUN_ATTR_ALTERNATE_SERVER, addr))); | 487 STUN_ATTR_ALTERNATE_SERVER, addr)); |
| 488 SendStun(conn, &resp); | 488 SendStun(conn, &resp); |
| 489 } | 489 } |
| 490 | 490 |
| 491 void TurnServer::SendStun(TurnServerConnection* conn, StunMessage* msg) { | 491 void TurnServer::SendStun(TurnServerConnection* conn, StunMessage* msg) { |
| 492 rtc::ByteBufferWriter buf; | 492 rtc::ByteBufferWriter buf; |
| 493 // Add a SOFTWARE attribute if one is set. | 493 // Add a SOFTWARE attribute if one is set. |
| 494 if (!software_.empty()) { | 494 if (!software_.empty()) { |
| 495 VERIFY(msg->AddAttribute( | 495 msg->AddAttribute( |
| 496 new StunByteStringAttribute(STUN_ATTR_SOFTWARE, software_))); | 496 new StunByteStringAttribute(STUN_ATTR_SOFTWARE, software_)); |
| 497 } | 497 } |
| 498 msg->Write(&buf); | 498 msg->Write(&buf); |
| 499 Send(conn, buf); | 499 Send(conn, buf); |
| 500 } | 500 } |
| 501 | 501 |
| 502 void TurnServer::Send(TurnServerConnection* conn, | 502 void TurnServer::Send(TurnServerConnection* conn, |
| 503 const rtc::ByteBufferWriter& buf) { | 503 const rtc::ByteBufferWriter& buf) { |
| 504 rtc::PacketOptions options; | 504 rtc::PacketOptions options; |
| 505 conn->socket()->SendTo(buf.Data(), buf.Length(), conn->src(), options); | 505 conn->socket()->SendTo(buf.Data(), buf.Length(), conn->src(), options); |
| 506 } | 506 } |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 651 TurnMessage response; | 651 TurnMessage response; |
| 652 InitResponse(msg, &response); | 652 InitResponse(msg, &response); |
| 653 | 653 |
| 654 StunAddressAttribute* mapped_addr_attr = | 654 StunAddressAttribute* mapped_addr_attr = |
| 655 new StunXorAddressAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS, conn_.src()); | 655 new StunXorAddressAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS, conn_.src()); |
| 656 StunAddressAttribute* relayed_addr_attr = | 656 StunAddressAttribute* relayed_addr_attr = |
| 657 new StunXorAddressAttribute(STUN_ATTR_XOR_RELAYED_ADDRESS, | 657 new StunXorAddressAttribute(STUN_ATTR_XOR_RELAYED_ADDRESS, |
| 658 external_socket_->GetLocalAddress()); | 658 external_socket_->GetLocalAddress()); |
| 659 StunUInt32Attribute* lifetime_attr = | 659 StunUInt32Attribute* lifetime_attr = |
| 660 new StunUInt32Attribute(STUN_ATTR_LIFETIME, lifetime_secs); | 660 new StunUInt32Attribute(STUN_ATTR_LIFETIME, lifetime_secs); |
| 661 VERIFY(response.AddAttribute(mapped_addr_attr)); | 661 response.AddAttribute(mapped_addr_attr); |
| 662 VERIFY(response.AddAttribute(relayed_addr_attr)); | 662 response.AddAttribute(relayed_addr_attr); |
| 663 VERIFY(response.AddAttribute(lifetime_attr)); | 663 response.AddAttribute(lifetime_attr); |
| 664 | 664 |
| 665 SendResponse(&response); | 665 SendResponse(&response); |
| 666 } | 666 } |
| 667 | 667 |
| 668 void TurnServerAllocation::HandleRefreshRequest(const TurnMessage* msg) { | 668 void TurnServerAllocation::HandleRefreshRequest(const TurnMessage* msg) { |
| 669 // Figure out the new lifetime. | 669 // Figure out the new lifetime. |
| 670 int lifetime_secs = ComputeLifetime(msg); | 670 int lifetime_secs = ComputeLifetime(msg); |
| 671 | 671 |
| 672 // Reset the expiration timer. | 672 // Reset the expiration timer. |
| 673 thread_->Clear(this, MSG_ALLOCATION_TIMEOUT); | 673 thread_->Clear(this, MSG_ALLOCATION_TIMEOUT); |
| 674 thread_->PostDelayed(RTC_FROM_HERE, lifetime_secs * 1000, this, | 674 thread_->PostDelayed(RTC_FROM_HERE, lifetime_secs * 1000, this, |
| 675 MSG_ALLOCATION_TIMEOUT); | 675 MSG_ALLOCATION_TIMEOUT); |
| 676 | 676 |
| 677 LOG_J(LS_INFO, this) << "Refreshed allocation, lifetime=" << lifetime_secs; | 677 LOG_J(LS_INFO, this) << "Refreshed allocation, lifetime=" << lifetime_secs; |
| 678 | 678 |
| 679 // Send a success response with a LIFETIME attribute. | 679 // Send a success response with a LIFETIME attribute. |
| 680 TurnMessage response; | 680 TurnMessage response; |
| 681 InitResponse(msg, &response); | 681 InitResponse(msg, &response); |
| 682 | 682 |
| 683 StunUInt32Attribute* lifetime_attr = | 683 StunUInt32Attribute* lifetime_attr = |
| 684 new StunUInt32Attribute(STUN_ATTR_LIFETIME, lifetime_secs); | 684 new StunUInt32Attribute(STUN_ATTR_LIFETIME, lifetime_secs); |
| 685 VERIFY(response.AddAttribute(lifetime_attr)); | 685 response.AddAttribute(lifetime_attr); |
| 686 | 686 |
| 687 SendResponse(&response); | 687 SendResponse(&response); |
| 688 } | 688 } |
| 689 | 689 |
| 690 void TurnServerAllocation::HandleSendIndication(const TurnMessage* msg) { | 690 void TurnServerAllocation::HandleSendIndication(const TurnMessage* msg) { |
| 691 // Check mandatory attributes. | 691 // Check mandatory attributes. |
| 692 const StunByteStringAttribute* data_attr = msg->GetByteString(STUN_ATTR_DATA); | 692 const StunByteStringAttribute* data_attr = msg->GetByteString(STUN_ATTR_DATA); |
| 693 const StunAddressAttribute* peer_attr = | 693 const StunAddressAttribute* peer_attr = |
| 694 msg->GetAddress(STUN_ATTR_XOR_PEER_ADDRESS); | 694 msg->GetAddress(STUN_ATTR_XOR_PEER_ADDRESS); |
| 695 if (!data_attr || !peer_attr) { | 695 if (!data_attr || !peer_attr) { |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 812 buf.WriteUInt16(static_cast<uint16_t>(size)); | 812 buf.WriteUInt16(static_cast<uint16_t>(size)); |
| 813 buf.WriteBytes(data, size); | 813 buf.WriteBytes(data, size); |
| 814 server_->Send(&conn_, buf); | 814 server_->Send(&conn_, buf); |
| 815 } else if (!server_->enable_permission_checks_ || | 815 } else if (!server_->enable_permission_checks_ || |
| 816 HasPermission(addr.ipaddr())) { | 816 HasPermission(addr.ipaddr())) { |
| 817 // No channel, but a permission exists. Send as a data indication. | 817 // No channel, but a permission exists. Send as a data indication. |
| 818 TurnMessage msg; | 818 TurnMessage msg; |
| 819 msg.SetType(TURN_DATA_INDICATION); | 819 msg.SetType(TURN_DATA_INDICATION); |
| 820 msg.SetTransactionID( | 820 msg.SetTransactionID( |
| 821 rtc::CreateRandomString(kStunTransactionIdLength)); | 821 rtc::CreateRandomString(kStunTransactionIdLength)); |
| 822 VERIFY(msg.AddAttribute(new StunXorAddressAttribute( | 822 msg.AddAttribute(new StunXorAddressAttribute( |
| 823 STUN_ATTR_XOR_PEER_ADDRESS, addr))); | 823 STUN_ATTR_XOR_PEER_ADDRESS, addr)); |
| 824 VERIFY(msg.AddAttribute(new StunByteStringAttribute( | 824 msg.AddAttribute(new StunByteStringAttribute( |
| 825 STUN_ATTR_DATA, data, size))); | 825 STUN_ATTR_DATA, data, size)); |
| 826 server_->SendStun(&conn_, &msg); | 826 server_->SendStun(&conn_, &msg); |
| 827 } else { | 827 } else { |
| 828 LOG_J(LS_WARNING, this) << "Received external packet without permission, " | 828 LOG_J(LS_WARNING, this) << "Received external packet without permission, " |
| 829 << "peer=" << addr; | 829 << "peer=" << addr; |
| 830 } | 830 } |
| 831 } | 831 } |
| 832 | 832 |
| 833 int TurnServerAllocation::ComputeLifetime(const TurnMessage* msg) { | 833 int TurnServerAllocation::ComputeLifetime(const TurnMessage* msg) { |
| 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 |
| (...skipping 128 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 |