Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(402)

Side by Side Diff: webrtc/p2p/base/turnserver.cc

Issue 2757893003: Add MakeUnique from chromium and change StunMessage::AddAttribute to take a unique_ptr. (Closed)
Patch Set: add ptr_util.h to rtc_base_approved build target Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/p2p/base/turnport.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 13 #include <tuple> // for std::tie
14 14
15 #include "webrtc/p2p/base/asyncstuntcpsocket.h"
16 #include "webrtc/p2p/base/common.h"
17 #include "webrtc/p2p/base/packetsocketfactory.h"
18 #include "webrtc/p2p/base/stun.h"
19 #include "webrtc/base/bind.h" 15 #include "webrtc/base/bind.h"
20 #include "webrtc/base/bytebuffer.h" 16 #include "webrtc/base/bytebuffer.h"
21 #include "webrtc/base/checks.h" 17 #include "webrtc/base/checks.h"
22 #include "webrtc/base/helpers.h" 18 #include "webrtc/base/helpers.h"
23 #include "webrtc/base/logging.h" 19 #include "webrtc/base/logging.h"
24 #include "webrtc/base/messagedigest.h" 20 #include "webrtc/base/messagedigest.h"
21 #include "webrtc/base/ptr_util.h"
25 #include "webrtc/base/socketadapters.h" 22 #include "webrtc/base/socketadapters.h"
26 #include "webrtc/base/stringencode.h" 23 #include "webrtc/base/stringencode.h"
27 #include "webrtc/base/thread.h" 24 #include "webrtc/base/thread.h"
25 #include "webrtc/p2p/base/asyncstuntcpsocket.h"
26 #include "webrtc/p2p/base/common.h"
27 #include "webrtc/p2p/base/packetsocketfactory.h"
28 #include "webrtc/p2p/base/stun.h"
28 29
29 namespace cricket { 30 namespace cricket {
30 31
31 // TODO(juberti): Move this all to a future turnmessage.h 32 // TODO(juberti): Move this all to a future turnmessage.h
32 //static const int IPPROTO_UDP = 17; 33 //static const int IPPROTO_UDP = 17;
33 static const int kNonceTimeout = 60 * 60 * 1000; // 60 minutes 34 static const int kNonceTimeout = 60 * 60 * 1000; // 60 minutes
34 static const int kDefaultAllocationTimeout = 10 * 60 * 1000; // 10 minutes 35 static const int kDefaultAllocationTimeout = 10 * 60 * 1000; // 10 minutes
35 static const int kPermissionTimeout = 5 * 60 * 1000; // 5 minutes 36 static const int kPermissionTimeout = 5 * 60 * 1000; // 5 minutes
36 static const int kChannelTimeout = 10 * 60 * 1000; // 10 minutes 37 static const int kChannelTimeout = 10 * 60 * 1000; // 10 minutes
37 38
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 return true; 107 return true;
107 } 108 }
108 109
109 static bool InitErrorResponse(const StunMessage* req, int code, 110 static bool InitErrorResponse(const StunMessage* req, int code,
110 const std::string& reason, StunMessage* resp) { 111 const std::string& reason, StunMessage* resp) {
111 int resp_type = (req) ? GetStunErrorResponseType(req->type()) : -1; 112 int resp_type = (req) ? GetStunErrorResponseType(req->type()) : -1;
112 if (resp_type == -1) 113 if (resp_type == -1)
113 return false; 114 return false;
114 resp->SetType(resp_type); 115 resp->SetType(resp_type);
115 resp->SetTransactionID(req->transaction_id()); 116 resp->SetTransactionID(req->transaction_id());
116 resp->AddAttribute(new cricket::StunErrorCodeAttribute( 117 resp->AddAttribute(rtc::MakeUnique<cricket::StunErrorCodeAttribute>(
117 STUN_ATTR_ERROR_CODE, code, reason)); 118 STUN_ATTR_ERROR_CODE, code, reason));
118 return true; 119 return true;
119 } 120 }
120 121
121 122
122 TurnServer::TurnServer(rtc::Thread* thread) 123 TurnServer::TurnServer(rtc::Thread* thread)
123 : thread_(thread), 124 : thread_(thread),
124 nonce_key_(rtc::CreateRandomString(kNonceKeySize)), 125 nonce_key_(rtc::CreateRandomString(kNonceKeySize)),
125 auth_hook_(NULL), 126 auth_hook_(NULL),
126 redirect_hook_(NULL), 127 redirect_hook_(NULL),
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 // Success. 347 // Success.
347 return true; 348 return true;
348 } 349 }
349 350
350 void TurnServer::HandleBindingRequest(TurnServerConnection* conn, 351 void TurnServer::HandleBindingRequest(TurnServerConnection* conn,
351 const StunMessage* req) { 352 const StunMessage* req) {
352 StunMessage response; 353 StunMessage response;
353 InitResponse(req, &response); 354 InitResponse(req, &response);
354 355
355 // Tell the user the address that we received their request from. 356 // Tell the user the address that we received their request from.
356 StunAddressAttribute* mapped_addr_attr; 357 auto mapped_addr_attr = rtc::MakeUnique<StunXorAddressAttribute>(
357 mapped_addr_attr = new StunXorAddressAttribute(
358 STUN_ATTR_XOR_MAPPED_ADDRESS, conn->src()); 358 STUN_ATTR_XOR_MAPPED_ADDRESS, conn->src());
359 response.AddAttribute(mapped_addr_attr); 359 response.AddAttribute(std::move(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
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 resp.AddAttribute(rtc::MakeUnique<StunByteStringAttribute>(
474 STUN_ATTR_NONCE, GenerateNonce(timestamp)));
473 resp.AddAttribute( 475 resp.AddAttribute(
474 new StunByteStringAttribute(STUN_ATTR_NONCE, GenerateNonce(timestamp))); 476 rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_REALM, realm_));
475 resp.AddAttribute(new StunByteStringAttribute(
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 resp.AddAttribute(new StunAddressAttribute( 486 resp.AddAttribute(
487 STUN_ATTR_ALTERNATE_SERVER, addr)); 487 rtc::MakeUnique<StunAddressAttribute>(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 msg->AddAttribute( 495 msg->AddAttribute(rtc::MakeUnique<StunByteStringAttribute>(
496 new StunByteStringAttribute(STUN_ATTR_SOFTWARE, software_)); 496 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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
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,
646 MSG_ALLOCATION_TIMEOUT); 646 MSG_ALLOCATION_TIMEOUT);
647 647
648 LOG_J(LS_INFO, this) << "Created allocation, lifetime=" << lifetime_secs; 648 LOG_J(LS_INFO, this) << "Created allocation, lifetime=" << lifetime_secs;
649 649
650 // We've already validated all the important bits; just send a response here. 650 // We've already validated all the important bits; just send a response here.
651 TurnMessage response; 651 TurnMessage response;
652 InitResponse(msg, &response); 652 InitResponse(msg, &response);
653 653
654 StunAddressAttribute* mapped_addr_attr = 654 auto mapped_addr_attr = rtc::MakeUnique<StunXorAddressAttribute>(
655 new StunXorAddressAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS, conn_.src()); 655 STUN_ATTR_XOR_MAPPED_ADDRESS, conn_.src());
656 StunAddressAttribute* relayed_addr_attr = 656 auto relayed_addr_attr = rtc::MakeUnique<StunXorAddressAttribute>(
657 new StunXorAddressAttribute(STUN_ATTR_XOR_RELAYED_ADDRESS, 657 STUN_ATTR_XOR_RELAYED_ADDRESS, external_socket_->GetLocalAddress());
658 external_socket_->GetLocalAddress()); 658 auto lifetime_attr =
659 StunUInt32Attribute* lifetime_attr = 659 rtc::MakeUnique<StunUInt32Attribute>(STUN_ATTR_LIFETIME, lifetime_secs);
660 new StunUInt32Attribute(STUN_ATTR_LIFETIME, lifetime_secs); 660 response.AddAttribute(std::move(mapped_addr_attr));
661 response.AddAttribute(mapped_addr_attr); 661 response.AddAttribute(std::move(relayed_addr_attr));
662 response.AddAttribute(relayed_addr_attr); 662 response.AddAttribute(std::move(lifetime_attr));
663 response.AddAttribute(lifetime_attr);
664 663
665 SendResponse(&response); 664 SendResponse(&response);
666 } 665 }
667 666
668 void TurnServerAllocation::HandleRefreshRequest(const TurnMessage* msg) { 667 void TurnServerAllocation::HandleRefreshRequest(const TurnMessage* msg) {
669 // Figure out the new lifetime. 668 // Figure out the new lifetime.
670 int lifetime_secs = ComputeLifetime(msg); 669 int lifetime_secs = ComputeLifetime(msg);
671 670
672 // Reset the expiration timer. 671 // Reset the expiration timer.
673 thread_->Clear(this, MSG_ALLOCATION_TIMEOUT); 672 thread_->Clear(this, MSG_ALLOCATION_TIMEOUT);
674 thread_->PostDelayed(RTC_FROM_HERE, lifetime_secs * 1000, this, 673 thread_->PostDelayed(RTC_FROM_HERE, lifetime_secs * 1000, this,
675 MSG_ALLOCATION_TIMEOUT); 674 MSG_ALLOCATION_TIMEOUT);
676 675
677 LOG_J(LS_INFO, this) << "Refreshed allocation, lifetime=" << lifetime_secs; 676 LOG_J(LS_INFO, this) << "Refreshed allocation, lifetime=" << lifetime_secs;
678 677
679 // Send a success response with a LIFETIME attribute. 678 // Send a success response with a LIFETIME attribute.
680 TurnMessage response; 679 TurnMessage response;
681 InitResponse(msg, &response); 680 InitResponse(msg, &response);
682 681
683 StunUInt32Attribute* lifetime_attr = 682 auto lifetime_attr =
684 new StunUInt32Attribute(STUN_ATTR_LIFETIME, lifetime_secs); 683 rtc::MakeUnique<StunUInt32Attribute>(STUN_ATTR_LIFETIME, lifetime_secs);
685 response.AddAttribute(lifetime_attr); 684 response.AddAttribute(std::move(lifetime_attr));
686 685
687 SendResponse(&response); 686 SendResponse(&response);
688 } 687 }
689 688
690 void TurnServerAllocation::HandleSendIndication(const TurnMessage* msg) { 689 void TurnServerAllocation::HandleSendIndication(const TurnMessage* msg) {
691 // Check mandatory attributes. 690 // Check mandatory attributes.
692 const StunByteStringAttribute* data_attr = msg->GetByteString(STUN_ATTR_DATA); 691 const StunByteStringAttribute* data_attr = msg->GetByteString(STUN_ATTR_DATA);
693 const StunAddressAttribute* peer_attr = 692 const StunAddressAttribute* peer_attr =
694 msg->GetAddress(STUN_ATTR_XOR_PEER_ADDRESS); 693 msg->GetAddress(STUN_ATTR_XOR_PEER_ADDRESS);
695 if (!data_attr || !peer_attr) { 694 if (!data_attr || !peer_attr) {
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
812 buf.WriteUInt16(static_cast<uint16_t>(size)); 811 buf.WriteUInt16(static_cast<uint16_t>(size));
813 buf.WriteBytes(data, size); 812 buf.WriteBytes(data, size);
814 server_->Send(&conn_, buf); 813 server_->Send(&conn_, buf);
815 } else if (!server_->enable_permission_checks_ || 814 } else if (!server_->enable_permission_checks_ ||
816 HasPermission(addr.ipaddr())) { 815 HasPermission(addr.ipaddr())) {
817 // No channel, but a permission exists. Send as a data indication. 816 // No channel, but a permission exists. Send as a data indication.
818 TurnMessage msg; 817 TurnMessage msg;
819 msg.SetType(TURN_DATA_INDICATION); 818 msg.SetType(TURN_DATA_INDICATION);
820 msg.SetTransactionID( 819 msg.SetTransactionID(
821 rtc::CreateRandomString(kStunTransactionIdLength)); 820 rtc::CreateRandomString(kStunTransactionIdLength));
822 msg.AddAttribute(new StunXorAddressAttribute( 821 msg.AddAttribute(rtc::MakeUnique<StunXorAddressAttribute>(
823 STUN_ATTR_XOR_PEER_ADDRESS, addr)); 822 STUN_ATTR_XOR_PEER_ADDRESS, addr));
824 msg.AddAttribute(new StunByteStringAttribute( 823 msg.AddAttribute(
825 STUN_ATTR_DATA, data, size)); 824 rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_DATA, data, size));
826 server_->SendStun(&conn_, &msg); 825 server_->SendStun(&conn_, &msg);
827 } else { 826 } else {
828 LOG_J(LS_WARNING, this) << "Received external packet without permission, " 827 LOG_J(LS_WARNING, this) << "Received external packet without permission, "
829 << "peer=" << addr; 828 << "peer=" << addr;
830 } 829 }
831 } 830 }
832 831
833 int TurnServerAllocation::ComputeLifetime(const TurnMessage* msg) { 832 int TurnServerAllocation::ComputeLifetime(const TurnMessage* msg) {
834 // Return the smaller of our default lifetime and the requested lifetime. 833 // Return the smaller of our default lifetime and the requested lifetime.
835 int lifetime = kDefaultAllocationTimeout / 1000; // convert to seconds 834 int lifetime = kDefaultAllocationTimeout / 1000; // convert to seconds
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
964 MSG_ALLOCATION_TIMEOUT); 963 MSG_ALLOCATION_TIMEOUT);
965 } 964 }
966 965
967 void TurnServerAllocation::Channel::OnMessage(rtc::Message* msg) { 966 void TurnServerAllocation::Channel::OnMessage(rtc::Message* msg) {
968 RTC_DCHECK(msg->message_id == MSG_ALLOCATION_TIMEOUT); 967 RTC_DCHECK(msg->message_id == MSG_ALLOCATION_TIMEOUT);
969 SignalDestroyed(this); 968 SignalDestroyed(this);
970 delete this; 969 delete this;
971 } 970 }
972 971
973 } // namespace cricket 972 } // namespace cricket
OLDNEW
« no previous file with comments | « webrtc/p2p/base/turnport.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698