| 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 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 591 | 592 |
| 592 // Fill in the response message. | 593 // Fill in the response message. |
| 593 StunMessage response; | 594 StunMessage response; |
| 594 response.SetType(STUN_BINDING_RESPONSE); | 595 response.SetType(STUN_BINDING_RESPONSE); |
| 595 response.SetTransactionID(request->transaction_id()); | 596 response.SetTransactionID(request->transaction_id()); |
| 596 const StunUInt32Attribute* retransmit_attr = | 597 const StunUInt32Attribute* retransmit_attr = |
| 597 request->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT); | 598 request->GetUInt32(STUN_ATTR_RETRANSMIT_COUNT); |
| 598 if (retransmit_attr) { | 599 if (retransmit_attr) { |
| 599 // Inherit the incoming retransmit value in the response so the other side | 600 // Inherit the incoming retransmit value in the response so the other side |
| 600 // can see our view of lost pings. | 601 // can see our view of lost pings. |
| 601 response.AddAttribute(new StunUInt32Attribute( | 602 response.AddAttribute(rtc::MakeUnique<StunUInt32Attribute>( |
| 602 STUN_ATTR_RETRANSMIT_COUNT, retransmit_attr->value())); | 603 STUN_ATTR_RETRANSMIT_COUNT, retransmit_attr->value())); |
| 603 | 604 |
| 604 if (retransmit_attr->value() > CONNECTION_WRITE_CONNECT_FAILURES) { | 605 if (retransmit_attr->value() > CONNECTION_WRITE_CONNECT_FAILURES) { |
| 605 LOG_J(LS_INFO, this) | 606 LOG_J(LS_INFO, this) |
| 606 << "Received a remote ping with high retransmit count: " | 607 << "Received a remote ping with high retransmit count: " |
| 607 << retransmit_attr->value(); | 608 << retransmit_attr->value(); |
| 608 } | 609 } |
| 609 } | 610 } |
| 610 | 611 |
| 611 response.AddAttribute( | 612 response.AddAttribute(rtc::MakeUnique<StunXorAddressAttribute>( |
| 612 new StunXorAddressAttribute(STUN_ATTR_XOR_MAPPED_ADDRESS, addr)); | 613 STUN_ATTR_XOR_MAPPED_ADDRESS, addr)); |
| 613 response.AddMessageIntegrity(password_); | 614 response.AddMessageIntegrity(password_); |
| 614 response.AddFingerprint(); | 615 response.AddFingerprint(); |
| 615 | 616 |
| 616 // Send the response message. | 617 // Send the response message. |
| 617 rtc::ByteBufferWriter buf; | 618 rtc::ByteBufferWriter buf; |
| 618 response.Write(&buf); | 619 response.Write(&buf); |
| 619 rtc::PacketOptions options(DefaultDscpValue()); | 620 rtc::PacketOptions options(DefaultDscpValue()); |
| 620 auto err = SendTo(buf.Data(), buf.Length(), addr, options, false); | 621 auto err = SendTo(buf.Data(), buf.Length(), addr, options, false); |
| 621 if (err < 0) { | 622 if (err < 0) { |
| 622 LOG_J(LS_ERROR, this) | 623 LOG_J(LS_ERROR, this) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 644 int error_code, const std::string& reason) { | 645 int error_code, const std::string& reason) { |
| 645 RTC_DCHECK(request->type() == STUN_BINDING_REQUEST); | 646 RTC_DCHECK(request->type() == STUN_BINDING_REQUEST); |
| 646 | 647 |
| 647 // Fill in the response message. | 648 // Fill in the response message. |
| 648 StunMessage response; | 649 StunMessage response; |
| 649 response.SetType(STUN_BINDING_ERROR_RESPONSE); | 650 response.SetType(STUN_BINDING_ERROR_RESPONSE); |
| 650 response.SetTransactionID(request->transaction_id()); | 651 response.SetTransactionID(request->transaction_id()); |
| 651 | 652 |
| 652 // When doing GICE, we need to write out the error code incorrectly to | 653 // When doing GICE, we need to write out the error code incorrectly to |
| 653 // maintain backwards compatiblility. | 654 // maintain backwards compatiblility. |
| 654 StunErrorCodeAttribute* error_attr = StunAttribute::CreateErrorCode(); | 655 auto error_attr = StunAttribute::CreateErrorCode(); |
| 655 error_attr->SetCode(error_code); | 656 error_attr->SetCode(error_code); |
| 656 error_attr->SetReason(reason); | 657 error_attr->SetReason(reason); |
| 657 response.AddAttribute(error_attr); | 658 response.AddAttribute(std::move(error_attr)); |
| 658 | 659 |
| 659 // Per Section 10.1.2, certain error cases don't get a MESSAGE-INTEGRITY, | 660 // Per Section 10.1.2, certain error cases don't get a MESSAGE-INTEGRITY, |
| 660 // because we don't have enough information to determine the shared secret. | 661 // because we don't have enough information to determine the shared secret. |
| 661 if (error_code != STUN_ERROR_BAD_REQUEST && | 662 if (error_code != STUN_ERROR_BAD_REQUEST && |
| 662 error_code != STUN_ERROR_UNAUTHORIZED) | 663 error_code != STUN_ERROR_UNAUTHORIZED) |
| 663 response.AddMessageIntegrity(password_); | 664 response.AddMessageIntegrity(password_); |
| 664 response.AddFingerprint(); | 665 response.AddFingerprint(); |
| 665 | 666 |
| 666 // Send the response message. | 667 // Send the response message. |
| 667 rtc::ByteBufferWriter buf; | 668 rtc::ByteBufferWriter buf; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 776 | 777 |
| 777 virtual ~ConnectionRequest() { | 778 virtual ~ConnectionRequest() { |
| 778 } | 779 } |
| 779 | 780 |
| 780 void Prepare(StunMessage* request) override { | 781 void Prepare(StunMessage* request) override { |
| 781 request->SetType(STUN_BINDING_REQUEST); | 782 request->SetType(STUN_BINDING_REQUEST); |
| 782 std::string username; | 783 std::string username; |
| 783 connection_->port()->CreateStunUsername( | 784 connection_->port()->CreateStunUsername( |
| 784 connection_->remote_candidate().username(), &username); | 785 connection_->remote_candidate().username(), &username); |
| 785 request->AddAttribute( | 786 request->AddAttribute( |
| 786 new StunByteStringAttribute(STUN_ATTR_USERNAME, username)); | 787 rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_USERNAME, username)); |
| 787 | 788 |
| 788 // connection_ already holds this ping, so subtract one from count. | 789 // connection_ already holds this ping, so subtract one from count. |
| 789 if (connection_->port()->send_retransmit_count_attribute()) { | 790 if (connection_->port()->send_retransmit_count_attribute()) { |
| 790 request->AddAttribute(new StunUInt32Attribute( | 791 request->AddAttribute(rtc::MakeUnique<StunUInt32Attribute>( |
| 791 STUN_ATTR_RETRANSMIT_COUNT, | 792 STUN_ATTR_RETRANSMIT_COUNT, |
| 792 static_cast<uint32_t>(connection_->pings_since_last_response_.size() - | 793 static_cast<uint32_t>(connection_->pings_since_last_response_.size() - |
| 793 1))); | 794 1))); |
| 794 } | 795 } |
| 795 uint32_t network_info = connection_->port()->Network()->id(); | 796 uint32_t network_info = connection_->port()->Network()->id(); |
| 796 network_info = (network_info << 16) | connection_->port()->network_cost(); | 797 network_info = (network_info << 16) | connection_->port()->network_cost(); |
| 797 request->AddAttribute( | 798 request->AddAttribute(rtc::MakeUnique<StunUInt32Attribute>( |
| 798 new StunUInt32Attribute(STUN_ATTR_NETWORK_INFO, network_info)); | 799 STUN_ATTR_NETWORK_INFO, network_info)); |
| 799 | 800 |
| 800 // Adding ICE_CONTROLLED or ICE_CONTROLLING attribute based on the role. | 801 // Adding ICE_CONTROLLED or ICE_CONTROLLING attribute based on the role. |
| 801 if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLING) { | 802 if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLING) { |
| 802 request->AddAttribute(new StunUInt64Attribute( | 803 request->AddAttribute(rtc::MakeUnique<StunUInt64Attribute>( |
| 803 STUN_ATTR_ICE_CONTROLLING, connection_->port()->IceTiebreaker())); | 804 STUN_ATTR_ICE_CONTROLLING, connection_->port()->IceTiebreaker())); |
| 804 // We should have either USE_CANDIDATE attribute or ICE_NOMINATION | 805 // We should have either USE_CANDIDATE attribute or ICE_NOMINATION |
| 805 // attribute but not both. That was enforced in p2ptransportchannel. | 806 // attribute but not both. That was enforced in p2ptransportchannel. |
| 806 if (connection_->use_candidate_attr()) { | 807 if (connection_->use_candidate_attr()) { |
| 807 request->AddAttribute(new StunByteStringAttribute( | 808 request->AddAttribute( |
| 808 STUN_ATTR_USE_CANDIDATE)); | 809 rtc::MakeUnique<StunByteStringAttribute>(STUN_ATTR_USE_CANDIDATE)); |
| 809 } | 810 } |
| 810 if (connection_->nomination() && | 811 if (connection_->nomination() && |
| 811 connection_->nomination() != connection_->acked_nomination()) { | 812 connection_->nomination() != connection_->acked_nomination()) { |
| 812 request->AddAttribute(new StunUInt32Attribute( | 813 request->AddAttribute(rtc::MakeUnique<StunUInt32Attribute>( |
| 813 STUN_ATTR_NOMINATION, connection_->nomination())); | 814 STUN_ATTR_NOMINATION, connection_->nomination())); |
| 814 } | 815 } |
| 815 } else if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLED) { | 816 } else if (connection_->port()->GetIceRole() == ICEROLE_CONTROLLED) { |
| 816 request->AddAttribute(new StunUInt64Attribute( | 817 request->AddAttribute(rtc::MakeUnique<StunUInt64Attribute>( |
| 817 STUN_ATTR_ICE_CONTROLLED, connection_->port()->IceTiebreaker())); | 818 STUN_ATTR_ICE_CONTROLLED, connection_->port()->IceTiebreaker())); |
| 818 } else { | 819 } else { |
| 819 RTC_NOTREACHED(); | 820 RTC_NOTREACHED(); |
| 820 } | 821 } |
| 821 | 822 |
| 822 // Adding PRIORITY Attribute. | 823 // Adding PRIORITY Attribute. |
| 823 // Changing the type preference to Peer Reflexive and local preference | 824 // Changing the type preference to Peer Reflexive and local preference |
| 824 // and component id information is unchanged from the original priority. | 825 // and component id information is unchanged from the original priority. |
| 825 // priority = (2^24)*(type preference) + | 826 // priority = (2^24)*(type preference) + |
| 826 // (2^8)*(local preference) + | 827 // (2^8)*(local preference) + |
| 827 // (2^0)*(256 - component ID) | 828 // (2^0)*(256 - component ID) |
| 828 uint32_t type_preference = | 829 uint32_t type_preference = |
| 829 (connection_->local_candidate().protocol() == TCP_PROTOCOL_NAME) | 830 (connection_->local_candidate().protocol() == TCP_PROTOCOL_NAME) |
| 830 ? ICE_TYPE_PREFERENCE_PRFLX_TCP | 831 ? ICE_TYPE_PREFERENCE_PRFLX_TCP |
| 831 : ICE_TYPE_PREFERENCE_PRFLX; | 832 : ICE_TYPE_PREFERENCE_PRFLX; |
| 832 uint32_t prflx_priority = | 833 uint32_t prflx_priority = |
| 833 type_preference << 24 | | 834 type_preference << 24 | |
| 834 (connection_->local_candidate().priority() & 0x00FFFFFF); | 835 (connection_->local_candidate().priority() & 0x00FFFFFF); |
| 835 request->AddAttribute( | 836 request->AddAttribute(rtc::MakeUnique<StunUInt32Attribute>( |
| 836 new StunUInt32Attribute(STUN_ATTR_PRIORITY, prflx_priority)); | 837 STUN_ATTR_PRIORITY, prflx_priority)); |
| 837 | 838 |
| 838 // Adding Message Integrity attribute. | 839 // Adding Message Integrity attribute. |
| 839 request->AddMessageIntegrity(connection_->remote_candidate().password()); | 840 request->AddMessageIntegrity(connection_->remote_candidate().password()); |
| 840 // Adding Fingerprint. | 841 // Adding Fingerprint. |
| 841 request->AddFingerprint(); | 842 request->AddFingerprint(); |
| 842 } | 843 } |
| 843 | 844 |
| 844 void OnResponse(StunMessage* response) override { | 845 void OnResponse(StunMessage* response) override { |
| 845 connection_->OnConnectionRequestResponse(this, response); | 846 connection_->OnConnectionRequestResponse(this, response); |
| 846 } | 847 } |
| (...skipping 777 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1624 RTC_DCHECK(sent < 0); | 1625 RTC_DCHECK(sent < 0); |
| 1625 error_ = port_->GetError(); | 1626 error_ = port_->GetError(); |
| 1626 stats_.sent_discarded_packets++; | 1627 stats_.sent_discarded_packets++; |
| 1627 } else { | 1628 } else { |
| 1628 send_rate_tracker_.AddSamples(sent); | 1629 send_rate_tracker_.AddSamples(sent); |
| 1629 } | 1630 } |
| 1630 return sent; | 1631 return sent; |
| 1631 } | 1632 } |
| 1632 | 1633 |
| 1633 } // namespace cricket | 1634 } // namespace cricket |
| OLD | NEW |