OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 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/port.h" | 11 #include "webrtc/p2p/base/port.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "webrtc/p2p/base/common.h" | |
17 #include "webrtc/p2p/base/portallocator.h" | |
18 #include "webrtc/base/base64.h" | 16 #include "webrtc/base/base64.h" |
19 #include "webrtc/base/checks.h" | 17 #include "webrtc/base/checks.h" |
20 #include "webrtc/base/crc32.h" | 18 #include "webrtc/base/crc32.h" |
21 #include "webrtc/base/helpers.h" | 19 #include "webrtc/base/helpers.h" |
22 #include "webrtc/base/logging.h" | 20 #include "webrtc/base/logging.h" |
23 #include "webrtc/base/messagedigest.h" | 21 #include "webrtc/base/messagedigest.h" |
24 #include "webrtc/base/network.h" | 22 #include "webrtc/base/network.h" |
| 23 #include "webrtc/base/ptr_util.h" |
25 #include "webrtc/base/stringencode.h" | 24 #include "webrtc/base/stringencode.h" |
26 #include "webrtc/base/stringutils.h" | 25 #include "webrtc/base/stringutils.h" |
| 26 #include "webrtc/p2p/base/common.h" |
| 27 #include "webrtc/p2p/base/portallocator.h" |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 // Determines whether we have seen at least the given maximum number of | 31 // Determines whether we have seen at least the given maximum number of |
31 // pings fail to have a response. | 32 // pings fail to have a response. |
32 inline bool TooManyFailures( | 33 inline bool TooManyFailures( |
33 const std::vector<cricket::Connection::SentPing>& pings_since_last_response, | 34 const std::vector<cricket::Connection::SentPing>& pings_since_last_response, |
34 uint32_t maximum_failures, | 35 uint32_t maximum_failures, |
35 int rtt_estimate, | 36 int rtt_estimate, |
36 int64_t now) { | 37 int64_t now) { |
(...skipping 547 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 | 585 |
585 // Fill in the response message. | 586 // Fill in the response message. |
586 StunMessage response; | 587 StunMessage response; |
587 response.SetType(STUN_BINDING_RESPONSE); | 588 response.SetType(STUN_BINDING_RESPONSE); |
588 response.SetTransactionID(request->transaction_id()); | 589 response.SetTransactionID(request->transaction_id()); |
589 const StunUInt32Attribute* retransmit_attr = | 590 const StunUInt32Attribute* retransmit_attr = |
590 request->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT); | 591 request->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT); |
591 if (retransmit_attr) { | 592 if (retransmit_attr) { |
592 // Inherit the incoming retransmit value in the response so the other side | 593 // Inherit the incoming retransmit value in the response so the other side |
593 // can see our view of lost pings. | 594 // can see our view of lost pings. |
594 response.AddAttribute(new StunUInt32Attribute( | 595 response.AddAttribute(rtc::MakeUnique<StunUInt32Attribute>( |
595 STUN_ATTR_RETRANSMIT_COUNT, retransmit_attr->value())); | 596 STUN_ATTR_RETRANSMIT_COUNT, retransmit_attr->value())); |
596 | 597 |
597 if (retransmit_attr->value() > CONNECTION_WRITE_CONNECT_FAILURES) { | 598 if (retransmit_attr->value() > CONNECTION_WRITE_CONNECT_FAILURES) { |
598 LOG_J(LS_INFO, this) | 599 LOG_J(LS_INFO, this) |
599 << "Received a remote ping with high retransmit count: " | 600 << "Received a remote ping with high retransmit count: " |
600 << retransmit_attr->value(); | 601 << retransmit_attr->value(); |
601 } | 602 } |
602 } | 603 } |
603 | 604 |
604 response.AddAttribute( | 605 response.AddAttribute(rtc::MakeUnique<StunXorAddressAttribute>( |
605 new StunXorAddressAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS, addr)); | 606 STUN_ATTR_XOR_MAPPED_ADDRESS, addr)); |
606 response.AddMessageIntegrity(password_); | 607 response.AddMessageIntegrity(password_); |
607 response.AddFingerprint(); | 608 response.AddFingerprint(); |
608 | 609 |
609 // Send the response message. | 610 // Send the response message. |
610 rtc::ByteBufferWriter buf; | 611 rtc::ByteBufferWriter buf; |
611 response.Write(&buf); | 612 response.Write(&buf); |
612 rtc::PacketOptions options(DefaultDscpValue()); | 613 rtc::PacketOptions options(DefaultDscpValue()); |
613 auto err = SendTo(buf.Data(), buf.Length(), addr, options, false); | 614 auto err = SendTo(buf.Data(), buf.Length(), addr, options, false); |
614 if (err < 0) { | 615 if (err < 0) { |
615 LOG_J(LS_ERROR, this) | 616 LOG_J(LS_ERROR, this) |
(...skipping 21 matching lines...) Expand all Loading... |
637 int error_code, const std::string& reason) { | 638 int error_code, const std::string& reason) { |
638 RTC_DCHECK(request->type() == STUN_BINDING_REQUEST); | 639 RTC_DCHECK(request->type() == STUN_BINDING_REQUEST); |
639 | 640 |
640 // Fill in the response message. | 641 // Fill in the response message. |
641 StunMessage response; | 642 StunMessage response; |
642 response.SetType(STUN_BINDING_ERROR_RESPONSE); | 643 response.SetType(STUN_BINDING_ERROR_RESPONSE); |
643 response.SetTransactionID(request->transaction_id()); | 644 response.SetTransactionID(request->transaction_id()); |
644 | 645 |
645 // When doing GICE, we need to write out the error code incorrectly to | 646 // When doing GICE, we need to write out the error code incorrectly to |
646 // maintain backwards compatiblility. | 647 // maintain backwards compatiblility. |
647 StunErrorCodeAttribute* error_attr = StunAttribute::CreateErrorCode(); | 648 auto error_attr = StunAttribute::CreateErrorCode(); |
648 error_attr->SetCode(error_code); | 649 error_attr->SetCode(error_code); |
649 error_attr->SetReason(reason); | 650 error_attr->SetReason(reason); |
650 response.AddAttribute(error_attr); | 651 response.AddAttribute(std::move(error_attr)); |
651 | 652 |
652 // Per Section 10.1.2, certain error cases don't get a MESSAGE-INTEGRITY, | 653 // Per Section 10.1.2, certain error cases don't get a MESSAGE-INTEGRITY, |
653 // because we don't have enough information to determine the shared secret. | 654 // because we don't have enough information to determine the shared secret. |
654 if (error_code != STUN_ERROR_BAD_REQUEST && | 655 if (error_code != STUN_ERROR_BAD_REQUEST && |
655 error_code != STUN_ERROR_UNAUTHORIZED) | 656 error_code != STUN_ERROR_UNAUTHORIZED) |
656 response.AddMessageIntegrity(password_); | 657 response.AddMessageIntegrity(password_); |
657 response.AddFingerprint(); | 658 response.AddFingerprint(); |
658 | 659 |
659 // Send the response message. | 660 // Send the response message. |
660 rtc::ByteBufferWriter buf; | 661 rtc::ByteBufferWriter buf; |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
769 | 770 |
770 virtual ~ConnectionRequest() { | 771 virtual ~ConnectionRequest() { |
771 } | 772 } |
772 | 773 |
773 void Prepare(StunMessage* request) override { | 774 void Prepare(StunMessage* request) override { |
774 request->SetType(STUN_BINDING_REQUEST); | 775 request->SetType(STUN_BINDING_REQUEST); |
775 std::string username; | 776 std::string username; |
776 connection_->port()->CreateStunUsername( | 777 connection_->port()->CreateStunUsername( |
777 connection_->remote_candidate().username(), &username); | 778 connection_->remote_candidate().username(), &username); |
778 request->AddAttribute( | 779 request->AddAttribute( |
779 new StunByteStringAttribute(STUN_ATTR_USERNAME, username)); | 780 rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_USERNAME, username)); |
780 | 781 |
781 // connection_ already holds this ping, so subtract one from count. | 782 // connection_ already holds this ping, so subtract one from count. |
782 if (connection_->port()->send_retransmit_count_attribute()) { | 783 if (connection_->port()->send_retransmit_count_attribute()) { |
783 request->AddAttribute(new StunUInt32Attribute( | 784 request->AddAttribute(rtc::MakeUnique<StunUInt32Attribute>( |
784 STUN_ATTR_RETRANSMIT_COUNT, | 785 STUN_ATTR_RETRANSMIT_COUNT, |
785 static_cast<uint32_t>(connection_->pings_since_last_response_.size() - | 786 static_cast<uint32_t>(connection_->pings_since_last_response_.size() - |
786 1))); | 787 1))); |
787 } | 788 } |
788 uint32_t network_info = connection_->port()->Network()->id(); | 789 uint32_t network_info = connection_->port()->Network()->id(); |
789 network_info = (network_info << 16) | connection_->port()->network_cost(); | 790 network_info = (network_info << 16) | connection_->port()->network_cost(); |
790 request->AddAttribute( | 791 request->AddAttribute(rtc::MakeUnique<StunUInt32Attribute>( |
791 new StunUInt32Attribute(STUN_ATTR_NETWORK_INFO, network_info)); | 792 STUN_ATTR_NETWORK_INFO, network_info)); |
792 | 793 |
793 // Adding ICE_CONTROLLED or ICE_CONTROLLING attribute based on the role. | 794 // Adding ICE_CONTROLLED or ICE_CONTROLLING attribute based on the role. |
794 if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLING) { | 795 if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLING) { |
795 request->AddAttribute(new StunUInt64Attribute( | 796 request->AddAttribute(rtc::MakeUnique<StunUInt64Attribute>( |
796 STUN_ATTR_ICE_CONTROLLING, connection_->port()->IceTiebreaker())); | 797 STUN_ATTR_ICE_CONTROLLING, connection_->port()->IceTiebreaker())); |
797 // We should have either USE_CANDIDATE attribute or ICE_NOMINATION | 798 // We should have either USE_CANDIDATE attribute or ICE_NOMINATION |
798 // attribute but not both. That was enforced in p2ptransportchannel. | 799 // attribute but not both. That was enforced in p2ptransportchannel. |
799 if (connection_->use_candidate_attr()) { | 800 if (connection_->use_candidate_attr()) { |
800 request->AddAttribute(new StunByteStringAttribute( | 801 request->AddAttribute( |
801 STUN_ATTR_USE_CANDIDATE)); | 802 rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_USE_CANDIDATE)); |
802 } | 803 } |
803 if (connection_->nomination() && | 804 if (connection_->nomination() && |
804 connection_->nomination() != connection_->acked_nomination()) { | 805 connection_->nomination() != connection_->acked_nomination()) { |
805 request->AddAttribute(new StunUInt32Attribute( | 806 request->AddAttribute(rtc::MakeUnique<StunUInt32Attribute>( |
806 STUN_ATTR_NOMINATION, connection_->nomination())); | 807 STUN_ATTR_NOMINATION, connection_->nomination())); |
807 } | 808 } |
808 } else if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLED) { | 809 } else if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLED) { |
809 request->AddAttribute(new StunUInt64Attribute( | 810 request->AddAttribute(rtc::MakeUnique<StunUInt64Attribute>( |
810 STUN_ATTR_ICE_CONTROLLED, connection_->port()->IceTiebreaker())); | 811 STUN_ATTR_ICE_CONTROLLED, connection_->port()->IceTiebreaker())); |
811 } else { | 812 } else { |
812 RTC_NOTREACHED(); | 813 RTC_NOTREACHED(); |
813 } | 814 } |
814 | 815 |
815 // Adding PRIORITY Attribute. | 816 // Adding PRIORITY Attribute. |
816 // Changing the type preference to Peer Reflexive and local preference | 817 // Changing the type preference to Peer Reflexive and local preference |
817 // and component id information is unchanged from the original priority. | 818 // and component id information is unchanged from the original priority. |
818 // priority = (2^24)*(type preference) + | 819 // priority = (2^24)*(type preference) + |
819 // (2^8)*(local preference) + | 820 // (2^8)*(local preference) + |
820 // (2^0)*(256 - component ID) | 821 // (2^0)*(256 - component ID) |
821 uint32_t type_preference = | 822 uint32_t type_preference = |
822 (connection_->local_candidate().protocol() == TCP_PROTOCOL_NAME) | 823 (connection_->local_candidate().protocol() == TCP_PROTOCOL_NAME) |
823 ? ICE_TYPE_PREFERENCE_PRFLX_TCP | 824 ? ICE_TYPE_PREFERENCE_PRFLX_TCP |
824 : ICE_TYPE_PREFERENCE_PRFLX; | 825 : ICE_TYPE_PREFERENCE_PRFLX; |
825 uint32_t prflx_priority = | 826 uint32_t prflx_priority = |
826 type_preference << 24 | | 827 type_preference << 24 | |
827 (connection_->local_candidate().priority() & 0x00FFFFFF); | 828 (connection_->local_candidate().priority() & 0x00FFFFFF); |
828 request->AddAttribute( | 829 request->AddAttribute(rtc::MakeUnique<StunUInt32Attribute>( |
829 new StunUInt32Attribute(STUN_ATTR_PRIORITY, prflx_priority)); | 830 STUN_ATTR_PRIORITY, prflx_priority)); |
830 | 831 |
831 // Adding Message Integrity attribute. | 832 // Adding Message Integrity attribute. |
832 request->AddMessageIntegrity(connection_->remote_candidate().password()); | 833 request->AddMessageIntegrity(connection_->remote_candidate().password()); |
833 // Adding Fingerprint. | 834 // Adding Fingerprint. |
834 request->AddFingerprint(); | 835 request->AddFingerprint(); |
835 } | 836 } |
836 | 837 |
837 void OnResponse(StunMessage* response) override { | 838 void OnResponse(StunMessage* response) override { |
838 connection_->OnConnectionRequestResponse(this, response); | 839 connection_->OnConnectionRequestResponse(this, response); |
839 } | 840 } |
(...skipping 772 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1612 RTC_DCHECK(sent < 0); | 1613 RTC_DCHECK(sent < 0); |
1613 error_ = port_->GetError(); | 1614 error_ = port_->GetError(); |
1614 stats_.sent_discarded_packets++; | 1615 stats_.sent_discarded_packets++; |
1615 } else { | 1616 } else { |
1616 send_rate_tracker_.AddSamples(sent); | 1617 send_rate_tracker_.AddSamples(sent); |
1617 } | 1618 } |
1618 return sent; | 1619 return sent; |
1619 } | 1620 } |
1620 | 1621 |
1621 } // namespace cricket | 1622 } // namespace cricket |
OLD | NEW |