From 7852d291c9623f81c4045ea3a2889bb907be3240 Mon Sep 17 00:00:00 2001 From: Qingsi Wang Date: Wed, 31 Oct 2018 11:17:07 -0700 Subject: [PATCH] Improve the documentation of MdnsResponderInterface and rename MDns.* to Mdns.*. MdnsResponderInterface now explicitly requires the reference counting of created names to allow the coexistence of multiple users of the same responder where one user would not remove identical names created by others. MDns.* is also renamed to Mdns.* per the style guide. TBR=aleloi@webrtc.org Bug: webrtc:9605 Change-Id: I047fc41f34de8d4e97c980409a7f373769c4c252 Reviewed-on: https://webrtc-review.googlesource.com/c/101921 Commit-Queue: Qingsi Wang Reviewed-by: Qingsi Wang Reviewed-by: Steve Anton Cr-Commit-Position: refs/heads/master@{#25458} --- p2p/base/mdns_message.cc | 110 ++++++++++----------- p2p/base/mdns_message.h | 60 ++++++------ p2p/base/mdns_message_unittest.cc | 114 +++++++++++----------- p2p/base/p2ptransportchannel_unittest.cc | 12 +-- p2p/base/port.cc | 4 +- p2p/client/basicportallocator_unittest.cc | 2 +- pc/peerconnectionfactory.cc | 9 +- rtc_base/fake_mdns_responder.h | 6 +- rtc_base/fakenetwork.h | 8 +- rtc_base/mdns_responder_interface.h | 29 +++--- rtc_base/network.cc | 8 +- rtc_base/network.h | 8 +- test/fuzzers/mdns_parser_fuzzer.cc | 2 +- 13 files changed, 187 insertions(+), 185 deletions(-) diff --git a/p2p/base/mdns_message.cc b/p2p/base/mdns_message.cc index 61af6f39f2..f14a0d117e 100644 --- a/p2p/base/mdns_message.cc +++ b/p2p/base/mdns_message.cc @@ -19,13 +19,13 @@ namespace { // RFC 1035, Section 4.1.1. // // QR bit. -constexpr uint16_t kMDnsFlagMaskQueryOrResponse = 0x8000; +constexpr uint16_t kMdnsFlagMaskQueryOrResponse = 0x8000; // AA bit. -constexpr uint16_t kMDnsFlagMaskAuthoritative = 0x0400; +constexpr uint16_t kMdnsFlagMaskAuthoritative = 0x0400; // RFC 1035, Section 4.1.2, QCLASS and RFC 6762, Section 18.12, repurposing of // top bit of QCLASS as the unicast response bit. -constexpr uint16_t kMDnsQClassMaskUnicastResponse = 0x8000; -constexpr size_t kMDnsHeaderSizeBytes = 12; +constexpr uint16_t kMdnsQClassMaskUnicastResponse = 0x8000; +constexpr size_t kMdnsHeaderSizeBytes = 12; bool ReadDomainName(MessageBufferReader* buf, std::string* name) { size_t name_start_pos = buf->CurrentOffset(); @@ -64,7 +64,7 @@ bool ReadDomainName(MessageBufferReader* buf, std::string* name) { // A legitimate pointer only refers to a prior occurrence of the same name, // and we should only move strictly backward to a prior name field after the // header. - if (pos_jump_to >= name_start_pos || pos_jump_to < kMDnsHeaderSizeBytes) { + if (pos_jump_to >= name_start_pos || pos_jump_to < kMdnsHeaderSizeBytes) { return false; } MessageBufferReader new_buf(buf->MessageData(), buf->MessageLength()); @@ -88,27 +88,27 @@ void WriteDomainName(rtc::ByteBufferWriter* buf, const std::string& name) { } // namespace -void MDnsHeader::SetQueryOrResponse(bool is_query) { +void MdnsHeader::SetQueryOrResponse(bool is_query) { if (is_query) { - flags &= ~kMDnsFlagMaskQueryOrResponse; + flags &= ~kMdnsFlagMaskQueryOrResponse; } else { - flags |= kMDnsFlagMaskQueryOrResponse; + flags |= kMdnsFlagMaskQueryOrResponse; } } -void MDnsHeader::SetAuthoritative(bool is_authoritative) { +void MdnsHeader::SetAuthoritative(bool is_authoritative) { if (is_authoritative) { - flags |= kMDnsFlagMaskAuthoritative; + flags |= kMdnsFlagMaskAuthoritative; } else { - flags &= ~kMDnsFlagMaskAuthoritative; + flags &= ~kMdnsFlagMaskAuthoritative; } } -bool MDnsHeader::IsAuthoritative() const { - return flags & kMDnsFlagMaskAuthoritative; +bool MdnsHeader::IsAuthoritative() const { + return flags & kMdnsFlagMaskAuthoritative; } -bool MDnsHeader::Read(MessageBufferReader* buf) { +bool MdnsHeader::Read(MessageBufferReader* buf) { if (!buf->ReadUInt16(&id) || !buf->ReadUInt16(&flags) || !buf->ReadUInt16(&qdcount) || !buf->ReadUInt16(&ancount) || !buf->ReadUInt16(&nscount) || !buf->ReadUInt16(&arcount)) { @@ -118,7 +118,7 @@ bool MDnsHeader::Read(MessageBufferReader* buf) { return true; } -void MDnsHeader::Write(rtc::ByteBufferWriter* buf) const { +void MdnsHeader::Write(rtc::ByteBufferWriter* buf) const { buf->WriteUInt16(id); buf->WriteUInt16(flags); buf->WriteUInt16(qdcount); @@ -127,15 +127,15 @@ void MDnsHeader::Write(rtc::ByteBufferWriter* buf) const { buf->WriteUInt16(arcount); } -bool MDnsHeader::IsQuery() const { - return !(flags & kMDnsFlagMaskQueryOrResponse); +bool MdnsHeader::IsQuery() const { + return !(flags & kMdnsFlagMaskQueryOrResponse); } -MDnsSectionEntry::MDnsSectionEntry() = default; -MDnsSectionEntry::~MDnsSectionEntry() = default; -MDnsSectionEntry::MDnsSectionEntry(const MDnsSectionEntry& other) = default; +MdnsSectionEntry::MdnsSectionEntry() = default; +MdnsSectionEntry::~MdnsSectionEntry() = default; +MdnsSectionEntry::MdnsSectionEntry(const MdnsSectionEntry& other) = default; -void MDnsSectionEntry::SetType(SectionEntryType type) { +void MdnsSectionEntry::SetType(SectionEntryType type) { switch (type) { case SectionEntryType::kA: type_ = 1; @@ -148,7 +148,7 @@ void MDnsSectionEntry::SetType(SectionEntryType type) { } } -SectionEntryType MDnsSectionEntry::GetType() const { +SectionEntryType MdnsSectionEntry::GetType() const { switch (type_) { case 1: return SectionEntryType::kA; @@ -159,7 +159,7 @@ SectionEntryType MDnsSectionEntry::GetType() const { } } -void MDnsSectionEntry::SetClass(SectionEntryClass cls) { +void MdnsSectionEntry::SetClass(SectionEntryClass cls) { switch (cls) { case SectionEntryClass::kIN: class_ = 1; @@ -169,7 +169,7 @@ void MDnsSectionEntry::SetClass(SectionEntryClass cls) { } } -SectionEntryClass MDnsSectionEntry::GetClass() const { +SectionEntryClass MdnsSectionEntry::GetClass() const { switch (class_) { case 1: return SectionEntryClass::kIN; @@ -178,11 +178,11 @@ SectionEntryClass MDnsSectionEntry::GetClass() const { } } -MDnsQuestion::MDnsQuestion() = default; -MDnsQuestion::MDnsQuestion(const MDnsQuestion& other) = default; -MDnsQuestion::~MDnsQuestion() = default; +MdnsQuestion::MdnsQuestion() = default; +MdnsQuestion::MdnsQuestion(const MdnsQuestion& other) = default; +MdnsQuestion::~MdnsQuestion() = default; -bool MDnsQuestion::Read(MessageBufferReader* buf) { +bool MdnsQuestion::Read(MessageBufferReader* buf) { if (!ReadDomainName(buf, &name_)) { RTC_LOG(LS_ERROR) << "Invalid name."; return false; @@ -194,31 +194,31 @@ bool MDnsQuestion::Read(MessageBufferReader* buf) { return true; } -bool MDnsQuestion::Write(rtc::ByteBufferWriter* buf) const { +bool MdnsQuestion::Write(rtc::ByteBufferWriter* buf) const { WriteDomainName(buf, name_); buf->WriteUInt16(type_); buf->WriteUInt16(class_); return true; } -void MDnsQuestion::SetUnicastResponse(bool should_unicast) { +void MdnsQuestion::SetUnicastResponse(bool should_unicast) { if (should_unicast) { - class_ |= kMDnsQClassMaskUnicastResponse; + class_ |= kMdnsQClassMaskUnicastResponse; } else { - class_ &= ~kMDnsQClassMaskUnicastResponse; + class_ &= ~kMdnsQClassMaskUnicastResponse; } } -bool MDnsQuestion::ShouldUnicastResponse() const { - return class_ & kMDnsQClassMaskUnicastResponse; +bool MdnsQuestion::ShouldUnicastResponse() const { + return class_ & kMdnsQClassMaskUnicastResponse; } -MDnsResourceRecord::MDnsResourceRecord() = default; -MDnsResourceRecord::MDnsResourceRecord(const MDnsResourceRecord& other) = +MdnsResourceRecord::MdnsResourceRecord() = default; +MdnsResourceRecord::MdnsResourceRecord(const MdnsResourceRecord& other) = default; -MDnsResourceRecord::~MDnsResourceRecord() = default; +MdnsResourceRecord::~MdnsResourceRecord() = default; -bool MDnsResourceRecord::Read(MessageBufferReader* buf) { +bool MdnsResourceRecord::Read(MessageBufferReader* buf) { if (!ReadDomainName(buf, &name_)) { return false; } @@ -239,17 +239,17 @@ bool MDnsResourceRecord::Read(MessageBufferReader* buf) { } return false; } -bool MDnsResourceRecord::ReadARData(MessageBufferReader* buf) { +bool MdnsResourceRecord::ReadARData(MessageBufferReader* buf) { // A RDATA contains a 32-bit IPv4 address. return buf->ReadString(&rdata_, 4); } -bool MDnsResourceRecord::ReadQuadARData(MessageBufferReader* buf) { +bool MdnsResourceRecord::ReadQuadARData(MessageBufferReader* buf) { // AAAA RDATA contains a 128-bit IPv6 address. return buf->ReadString(&rdata_, 16); } -bool MDnsResourceRecord::Write(rtc::ByteBufferWriter* buf) const { +bool MdnsResourceRecord::Write(rtc::ByteBufferWriter* buf) const { WriteDomainName(buf, name_); buf->WriteUInt16(type_); buf->WriteUInt16(class_); @@ -270,15 +270,15 @@ bool MDnsResourceRecord::Write(rtc::ByteBufferWriter* buf) const { return true; } -void MDnsResourceRecord::WriteARData(rtc::ByteBufferWriter* buf) const { +void MdnsResourceRecord::WriteARData(rtc::ByteBufferWriter* buf) const { buf->WriteString(rdata_); } -void MDnsResourceRecord::WriteQuadARData(rtc::ByteBufferWriter* buf) const { +void MdnsResourceRecord::WriteQuadARData(rtc::ByteBufferWriter* buf) const { buf->WriteString(rdata_); } -bool MDnsResourceRecord::SetIPAddressInRecordData( +bool MdnsResourceRecord::SetIPAddressInRecordData( const rtc::IPAddress& address) { int af = address.family(); if (af != AF_INET && af != AF_INET6) { @@ -293,7 +293,7 @@ bool MDnsResourceRecord::SetIPAddressInRecordData( return true; } -bool MDnsResourceRecord::GetIPAddressFromRecordData( +bool MdnsResourceRecord::GetIPAddressFromRecordData( rtc::IPAddress* address) const { if (GetType() != SectionEntryType::kA && GetType() != SectionEntryType::kAAAA) { @@ -310,16 +310,16 @@ bool MDnsResourceRecord::GetIPAddressFromRecordData( return rtc::IPFromString(std::string(out), address); } -MDnsMessage::MDnsMessage() = default; -MDnsMessage::~MDnsMessage() = default; +MdnsMessage::MdnsMessage() = default; +MdnsMessage::~MdnsMessage() = default; -bool MDnsMessage::Read(MessageBufferReader* buf) { +bool MdnsMessage::Read(MessageBufferReader* buf) { RTC_DCHECK_EQ(0u, buf->CurrentOffset()); if (!header_.Read(buf)) { return false; } - auto read_question = [&buf](std::vector* section, + auto read_question = [&buf](std::vector* section, uint16_t count) { section->resize(count); for (auto& question : (*section)) { @@ -329,7 +329,7 @@ bool MDnsMessage::Read(MessageBufferReader* buf) { } return true; }; - auto read_rr = [&buf](std::vector* section, + auto read_rr = [&buf](std::vector* section, uint16_t count) { section->resize(count); for (auto& rr : (*section)) { @@ -349,10 +349,10 @@ bool MDnsMessage::Read(MessageBufferReader* buf) { return true; } -bool MDnsMessage::Write(rtc::ByteBufferWriter* buf) const { +bool MdnsMessage::Write(rtc::ByteBufferWriter* buf) const { header_.Write(buf); - auto write_rr = [&buf](const std::vector& section) { + auto write_rr = [&buf](const std::vector& section) { for (auto rr : section) { if (!rr.Write(buf)) { return false; @@ -374,7 +374,7 @@ bool MDnsMessage::Write(rtc::ByteBufferWriter* buf) const { return true; } -bool MDnsMessage::ShouldUnicastResponse() const { +bool MdnsMessage::ShouldUnicastResponse() const { bool should_unicast = false; for (const auto& question : question_section_) { should_unicast |= question.ShouldUnicastResponse(); @@ -382,12 +382,12 @@ bool MDnsMessage::ShouldUnicastResponse() const { return should_unicast; } -void MDnsMessage::AddQuestion(const MDnsQuestion& question) { +void MdnsMessage::AddQuestion(const MdnsQuestion& question) { question_section_.push_back(question); header_.qdcount = question_section_.size(); } -void MDnsMessage::AddAnswerRecord(const MDnsResourceRecord& answer) { +void MdnsMessage::AddAnswerRecord(const MdnsResourceRecord& answer) { answer_section_.push_back(answer); header_.ancount = answer_section_.size(); } diff --git a/p2p/base/mdns_message.h b/p2p/base/mdns_message.h index f6dec20a95..7aa1b21039 100644 --- a/p2p/base/mdns_message.h +++ b/p2p/base/mdns_message.h @@ -15,7 +15,7 @@ // 6762 and RFC 1025 (DNS messages). Note that it is recommended by RFC 6762 to // use the name compression scheme defined in RFC 1035 whenever possible. We // currently only implement the capability of reading compressed names in mDNS -// messages in MDnsMessage::Read(); however, the MDnsMessage::Write() does not +// messages in MdnsMessage::Read(); however, the MdnsMessage::Write() does not // support name compression yet. // // Fuzzer tests (test/fuzzers/mdns_parser_fuzzer.cc) MUST always be performed @@ -50,7 +50,7 @@ enum class SectionEntryClass { }; // RFC 1035, Section 4.1.1. -class MDnsHeader final { +class MdnsHeader final { public: bool Read(MessageBufferReader* buf); void Write(rtc::ByteBufferWriter* buf) const; @@ -74,11 +74,11 @@ class MDnsHeader final { // Entries in each section after the header share a common structure. Note that // this is not a concept defined in RFC 1035. -class MDnsSectionEntry { +class MdnsSectionEntry { public: - MDnsSectionEntry(); - MDnsSectionEntry(const MDnsSectionEntry& other); - virtual ~MDnsSectionEntry(); + MdnsSectionEntry(); + MdnsSectionEntry(const MdnsSectionEntry& other); + virtual ~MdnsSectionEntry(); virtual bool Read(MessageBufferReader* buf) = 0; virtual bool Write(rtc::ByteBufferWriter* buf) const = 0; @@ -99,11 +99,11 @@ class MDnsSectionEntry { }; // RFC 1035, Section 4.1.2. -class MDnsQuestion final : public MDnsSectionEntry { +class MdnsQuestion final : public MdnsSectionEntry { public: - MDnsQuestion(); - MDnsQuestion(const MDnsQuestion& other); - ~MDnsQuestion() override; + MdnsQuestion(); + MdnsQuestion(const MdnsQuestion& other); + ~MdnsQuestion() override; bool Read(MessageBufferReader* buf) override; bool Write(rtc::ByteBufferWriter* buf) const override; @@ -113,11 +113,11 @@ class MDnsQuestion final : public MDnsSectionEntry { }; // RFC 1035, Section 4.1.3. -class MDnsResourceRecord final : public MDnsSectionEntry { +class MdnsResourceRecord final : public MdnsSectionEntry { public: - MDnsResourceRecord(); - MDnsResourceRecord(const MDnsResourceRecord& other); - ~MDnsResourceRecord() override; + MdnsResourceRecord(); + MdnsResourceRecord(const MdnsResourceRecord& other); + ~MdnsResourceRecord() override; bool Read(MessageBufferReader* buf) override; bool Write(rtc::ByteBufferWriter* buf) const override; @@ -145,17 +145,17 @@ class MDnsResourceRecord final : public MDnsSectionEntry { std::string rdata_; }; -class MDnsMessage final { +class MdnsMessage final { public: // RFC 1035, Section 4.1. enum class Section { kQuestion, kAnswer, kAuthority, kAdditional }; - MDnsMessage(); - ~MDnsMessage(); + MdnsMessage(); + ~MdnsMessage(); // Reads the mDNS message in |buf| and populates the corresponding fields in - // MDnsMessage. + // MdnsMessage. bool Read(MessageBufferReader* buf); - // Write an mDNS message to |buf| based on the fields in MDnsMessage. + // Write an mDNS message to |buf| based on the fields in MdnsMessage. // // TODO(qingsi): Implement name compression when writing mDNS messages. bool Write(rtc::ByteBufferWriter* buf) const; @@ -177,29 +177,29 @@ class MDnsMessage final { // preferred. False otherwise. bool ShouldUnicastResponse() const; - void AddQuestion(const MDnsQuestion& question); + void AddQuestion(const MdnsQuestion& question); // TODO(qingsi): Implement AddXRecord for name server and additional records. - void AddAnswerRecord(const MDnsResourceRecord& answer); + void AddAnswerRecord(const MdnsResourceRecord& answer); - const std::vector& question_section() const { + const std::vector& question_section() const { return question_section_; } - const std::vector& answer_section() const { + const std::vector& answer_section() const { return answer_section_; } - const std::vector& authority_section() const { + const std::vector& authority_section() const { return authority_section_; } - const std::vector& additional_section() const { + const std::vector& additional_section() const { return additional_section_; } private: - MDnsHeader header_; - std::vector question_section_; - std::vector answer_section_; - std::vector authority_section_; - std::vector additional_section_; + MdnsHeader header_; + std::vector question_section_; + std::vector answer_section_; + std::vector authority_section_; + std::vector additional_section_; }; } // namespace webrtc diff --git a/p2p/base/mdns_message_unittest.cc b/p2p/base/mdns_message_unittest.cc index fb4a3b1e7a..7f816f740b 100644 --- a/p2p/base/mdns_message_unittest.cc +++ b/p2p/base/mdns_message_unittest.cc @@ -19,9 +19,9 @@ #include "rtc_base/socketaddress.h" #include "test/gmock.h" -#define ReadMDnsMessage(X, Y) ReadMDnsMessageTestCase(X, Y, sizeof(Y)) -#define WriteMDnsMessageAndCompare(X, Y) \ - WriteMDnsMessageAndCompareWithTestCast(X, Y, sizeof(Y)) +#define ReadMdnsMessage(X, Y) ReadMdnsMessageTestCase(X, Y, sizeof(Y)) +#define WriteMdnsMessageAndCompare(X, Y) \ + WriteMdnsMessageAndCompareWithTestCast(X, Y, sizeof(Y)) using ::testing::ElementsAre; using ::testing::Pair; @@ -255,14 +255,14 @@ const uint8_t kCorruptedAnswerWithNameCompression2[] = { 0xc0, 0xA8, 0x00, 0x01, // 192.168.0.1 }; -bool ReadMDnsMessageTestCase(MDnsMessage* msg, +bool ReadMdnsMessageTestCase(MdnsMessage* msg, const uint8_t* testcase, size_t size) { MessageBufferReader buf(reinterpret_cast(testcase), size); return msg->Read(&buf); } -void WriteMDnsMessageAndCompareWithTestCast(MDnsMessage* msg, +void WriteMdnsMessageAndCompareWithTestCast(MdnsMessage* msg, const uint8_t* testcase, size_t size) { rtc::ByteBufferWriter out; @@ -276,7 +276,7 @@ void WriteMDnsMessageAndCompareWithTestCast(MDnsMessage* msg, EXPECT_EQ(testcase_bytes, bytes); } -bool GetQueriedNames(MDnsMessage* msg, std::set* names) { +bool GetQueriedNames(MdnsMessage* msg, std::set* names) { if (!msg->IsQuery() || msg->question_section().empty()) { return false; } @@ -286,7 +286,7 @@ bool GetQueriedNames(MDnsMessage* msg, std::set* names) { return true; } -bool GetResolution(MDnsMessage* msg, +bool GetResolution(MdnsMessage* msg, std::map* names) { if (msg->IsQuery() || msg->answer_section().empty()) { return false; @@ -303,10 +303,10 @@ bool GetResolution(MDnsMessage* msg, } // namespace -TEST(MDnsMessageTest, ReadSingleQuestionForIPv4Address) { - MDnsMessage msg; +TEST(MdnsMessageTest, ReadSingleQuestionForIPv4Address) { + MdnsMessage msg; ASSERT_TRUE( - ReadMDnsMessage(&msg, kSingleQuestionForIPv4AddrWithUnicastResponse)); + ReadMdnsMessage(&msg, kSingleQuestionForIPv4AddrWithUnicastResponse)); EXPECT_TRUE(msg.IsQuery()); EXPECT_EQ(0x1234, msg.GetId()); ASSERT_EQ(1u, msg.question_section().size()); @@ -323,9 +323,9 @@ TEST(MDnsMessageTest, ReadSingleQuestionForIPv4Address) { EXPECT_THAT(queried_names, ElementsAre("webrtc.org.")); } -TEST(MDnsMessageTest, ReadTwoQuestionsForIPv4AndIPv6Addr) { - MDnsMessage msg; - ASSERT_TRUE(ReadMDnsMessage( +TEST(MdnsMessageTest, ReadTwoQuestionsForIPv4AndIPv6Addr) { + MdnsMessage msg; + ASSERT_TRUE(ReadMdnsMessage( &msg, kTwoQuestionsForIPv4AndIPv6AddrWithMulticastResponse)); EXPECT_TRUE(msg.IsQuery()); EXPECT_EQ(0x1234, msg.GetId()); @@ -345,9 +345,9 @@ TEST(MDnsMessageTest, ReadTwoQuestionsForIPv4AndIPv6Addr) { UnorderedElementsAre("webrtc4.org.", "webrtc6.org.")); } -TEST(MDnsMessageTest, ReadTwoQuestionsForIPv4AndIPv6AddrWithNameCompression) { - MDnsMessage msg; - ASSERT_TRUE(ReadMDnsMessage( +TEST(MdnsMessageTest, ReadTwoQuestionsForIPv4AndIPv6AddrWithNameCompression) { + MdnsMessage msg; + ASSERT_TRUE(ReadMdnsMessage( &msg, kTwoQuestionsForIPv4AndIPv6AddrWithMulticastResponseAndNameCompression)); @@ -363,10 +363,10 @@ TEST(MDnsMessageTest, ReadTwoQuestionsForIPv4AndIPv6AddrWithNameCompression) { UnorderedElementsAre("www.webrtc.org.", "mdns.webrtc.org.")); } -TEST(MDnsMessageTest, ReadThreeQuestionsWithTwoPointersToTheSameNameSuffix) { - MDnsMessage msg; +TEST(MdnsMessageTest, ReadThreeQuestionsWithTwoPointersToTheSameNameSuffix) { + MdnsMessage msg; ASSERT_TRUE( - ReadMDnsMessage(&msg, kThreeQuestionsWithTwoPointersToTheSameNameSuffix)); + ReadMdnsMessage(&msg, kThreeQuestionsWithTwoPointersToTheSameNameSuffix)); ASSERT_EQ(3u, msg.question_section().size()); const auto& question1 = msg.question_section()[0]; @@ -383,10 +383,10 @@ TEST(MDnsMessageTest, ReadThreeQuestionsWithTwoPointersToTheSameNameSuffix) { "webrtc.org.")); } -TEST(MDnsMessageTest, +TEST(MdnsMessageTest, ReadThreeQuestionsWithPointerToNameSuffixContainingAnotherPointer) { - MDnsMessage msg; - ASSERT_TRUE(ReadMDnsMessage( + MdnsMessage msg; + ASSERT_TRUE(ReadMdnsMessage( &msg, kThreeQuestionsWithPointerToNameSuffixContainingAnotherPointer)); ASSERT_EQ(3u, msg.question_section().size()); @@ -404,16 +404,16 @@ TEST(MDnsMessageTest, "www.mdns.webrtc.org.")); } -TEST(MDnsMessageTest, +TEST(MdnsMessageTest, ReadQuestionWithCorruptedPointerInNameCompressionShouldFail) { - MDnsMessage msg; - EXPECT_FALSE(ReadMDnsMessage(&msg, kCorruptedQuestionWithNameCompression1)); - EXPECT_FALSE(ReadMDnsMessage(&msg, kCorruptedQuestionWithNameCompression2)); + MdnsMessage msg; + EXPECT_FALSE(ReadMdnsMessage(&msg, kCorruptedQuestionWithNameCompression1)); + EXPECT_FALSE(ReadMdnsMessage(&msg, kCorruptedQuestionWithNameCompression2)); } -TEST(MDnsMessageTest, ReadSingleAnswerForIPv4Addr) { - MDnsMessage msg; - ASSERT_TRUE(ReadMDnsMessage(&msg, kSingleAuthoritativeAnswerWithIPv4Addr)); +TEST(MdnsMessageTest, ReadSingleAnswerForIPv4Addr) { + MdnsMessage msg; + ASSERT_TRUE(ReadMdnsMessage(&msg, kSingleAuthoritativeAnswerWithIPv4Addr)); EXPECT_FALSE(msg.IsQuery()); EXPECT_TRUE(msg.IsAuthoritative()); EXPECT_EQ(0x1234, msg.GetId()); @@ -432,10 +432,10 @@ TEST(MDnsMessageTest, ReadSingleAnswerForIPv4Addr) { EXPECT_THAT(resolution, ElementsAre(Pair("webrtc.org.", expected_addr))); } -TEST(MDnsMessageTest, ReadTwoAnswersForIPv4AndIPv6Addr) { - MDnsMessage msg; +TEST(MdnsMessageTest, ReadTwoAnswersForIPv4AndIPv6Addr) { + MdnsMessage msg; ASSERT_TRUE( - ReadMDnsMessage(&msg, kTwoAuthoritativeAnswersWithIPv4AndIPv6Addr)); + ReadMdnsMessage(&msg, kTwoAuthoritativeAnswersWithIPv4AndIPv6Addr)); EXPECT_FALSE(msg.IsQuery()); EXPECT_TRUE(msg.IsAuthoritative()); EXPECT_EQ(0x1234, msg.GetId()); @@ -462,9 +462,9 @@ TEST(MDnsMessageTest, ReadTwoAnswersForIPv4AndIPv6Addr) { Pair("webrtc6.org.", expected_addr_ipv6))); } -TEST(MDnsMessageTest, ReadTwoAnswersForIPv4AndIPv6AddrWithNameCompression) { - MDnsMessage msg; - ASSERT_TRUE(ReadMDnsMessage( +TEST(MdnsMessageTest, ReadTwoAnswersForIPv4AndIPv6AddrWithNameCompression) { + MdnsMessage msg; + ASSERT_TRUE(ReadMdnsMessage( &msg, kTwoAuthoritativeAnswersWithIPv4AndIPv6AddrWithNameCompression)); std::map resolution; @@ -478,57 +478,57 @@ TEST(MDnsMessageTest, ReadTwoAnswersForIPv4AndIPv6AddrWithNameCompression) { Pair("webrtc.org.", expected_addr_ipv6))); } -TEST(MDnsMessageTest, +TEST(MdnsMessageTest, ReadAnswerWithCorruptedPointerInNameCompressionShouldFail) { - MDnsMessage msg; - EXPECT_FALSE(ReadMDnsMessage(&msg, kCorruptedAnswerWithNameCompression1)); - EXPECT_FALSE(ReadMDnsMessage(&msg, kCorruptedAnswerWithNameCompression2)); + MdnsMessage msg; + EXPECT_FALSE(ReadMdnsMessage(&msg, kCorruptedAnswerWithNameCompression1)); + EXPECT_FALSE(ReadMdnsMessage(&msg, kCorruptedAnswerWithNameCompression2)); } -TEST(MDnsMessageTest, WriteSingleQuestionForIPv4Addr) { - MDnsMessage msg; +TEST(MdnsMessageTest, WriteSingleQuestionForIPv4Addr) { + MdnsMessage msg; msg.SetId(0x1234); msg.SetQueryOrResponse(true); - MDnsQuestion question; + MdnsQuestion question; question.SetName("webrtc.org."); question.SetType(SectionEntryType::kA); question.SetClass(SectionEntryClass::kIN); question.SetUnicastResponse(true); msg.AddQuestion(question); - WriteMDnsMessageAndCompare(&msg, + WriteMdnsMessageAndCompare(&msg, kSingleQuestionForIPv4AddrWithUnicastResponse); } -TEST(MDnsMessageTest, WriteTwoQuestionsForIPv4AndIPv6Addr) { - MDnsMessage msg; +TEST(MdnsMessageTest, WriteTwoQuestionsForIPv4AndIPv6Addr) { + MdnsMessage msg; msg.SetId(0x1234); msg.SetQueryOrResponse(true); - MDnsQuestion question1; + MdnsQuestion question1; question1.SetName("webrtc4.org."); question1.SetType(SectionEntryType::kA); question1.SetClass(SectionEntryClass::kIN); msg.AddQuestion(question1); - MDnsQuestion question2; + MdnsQuestion question2; question2.SetName("webrtc6.org."); question2.SetType(SectionEntryType::kAAAA); question2.SetClass(SectionEntryClass::kIN); msg.AddQuestion(question2); - WriteMDnsMessageAndCompare( + WriteMdnsMessageAndCompare( &msg, kTwoQuestionsForIPv4AndIPv6AddrWithMulticastResponse); } -TEST(MDnsMessageTest, WriteSingleAnswerToIPv4Addr) { - MDnsMessage msg; +TEST(MdnsMessageTest, WriteSingleAnswerToIPv4Addr) { + MdnsMessage msg; msg.SetId(0x1234); msg.SetQueryOrResponse(false); msg.SetAuthoritative(true); - MDnsResourceRecord answer; + MdnsResourceRecord answer; answer.SetName("webrtc.org."); answer.SetType(SectionEntryType::kA); answer.SetClass(SectionEntryClass::kIN); @@ -537,16 +537,16 @@ TEST(MDnsMessageTest, WriteSingleAnswerToIPv4Addr) { answer.SetTtlSeconds(120); msg.AddAnswerRecord(answer); - WriteMDnsMessageAndCompare(&msg, kSingleAuthoritativeAnswerWithIPv4Addr); + WriteMdnsMessageAndCompare(&msg, kSingleAuthoritativeAnswerWithIPv4Addr); } -TEST(MDnsMessageTest, WriteTwoAnswersToIPv4AndIPv6Addr) { - MDnsMessage msg; +TEST(MdnsMessageTest, WriteTwoAnswersToIPv4AndIPv6Addr) { + MdnsMessage msg; msg.SetId(0x1234); msg.SetQueryOrResponse(false); msg.SetAuthoritative(true); - MDnsResourceRecord answer1; + MdnsResourceRecord answer1; answer1.SetName("webrtc4.org."); answer1.SetType(SectionEntryType::kA); answer1.SetClass(SectionEntryClass::kIN); @@ -555,7 +555,7 @@ TEST(MDnsMessageTest, WriteTwoAnswersToIPv4AndIPv6Addr) { answer1.SetTtlSeconds(60); msg.AddAnswerRecord(answer1); - MDnsResourceRecord answer2; + MdnsResourceRecord answer2; answer2.SetName("webrtc6.org."); answer2.SetType(SectionEntryType::kAAAA); answer2.SetClass(SectionEntryClass::kIN); @@ -564,7 +564,7 @@ TEST(MDnsMessageTest, WriteTwoAnswersToIPv4AndIPv6Addr) { answer2.SetTtlSeconds(120); msg.AddAnswerRecord(answer2); - WriteMDnsMessageAndCompare(&msg, kTwoAuthoritativeAnswersWithIPv4AndIPv6Addr); + WriteMdnsMessageAndCompare(&msg, kTwoAuthoritativeAnswersWithIPv4AndIPv6Addr); } } // namespace webrtc diff --git a/p2p/base/p2ptransportchannel_unittest.cc b/p2p/base/p2ptransportchannel_unittest.cc index 3cf0711ded..ce89b99e9b 100644 --- a/p2p/base/p2ptransportchannel_unittest.cc +++ b/p2p/base/p2ptransportchannel_unittest.cc @@ -4593,7 +4593,7 @@ TEST(P2PTransportChannelResolverTest, HostnameCandidateIsResolved) { // prflx candidate is updated to a host candidate after the name resolution is // done. TEST_F(P2PTransportChannelTest, - PeerReflexiveCandidateBeforeSignalingWithMDnsName) { + PeerReflexiveCandidateBeforeSignalingWithMdnsName) { rtc::MockAsyncResolver mock_async_resolver; webrtc::MockAsyncResolverFactory mock_async_resolver_factory; EXPECT_CALL(mock_async_resolver_factory, Create()) @@ -4604,7 +4604,7 @@ TEST_F(P2PTransportChannelTest, ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); // ICE parameter will be set up when creating the channels. set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); - GetEndpoint(0)->network_manager_.CreateMDnsResponder(); + GetEndpoint(0)->network_manager_.CreateMdnsResponder(); GetEndpoint(1)->async_resolver_factory_ = &mock_async_resolver_factory; CreateChannels(); // Pause sending candidates from both endpoints until we find out what port @@ -4659,7 +4659,7 @@ TEST_F(P2PTransportChannelTest, // a host candidate if the hostname candidate turns out to have the same IP // address after the resolution completes. TEST_F(P2PTransportChannelTest, - PeerReflexiveCandidateDuringResolvingHostCandidateWithMDnsName) { + PeerReflexiveCandidateDuringResolvingHostCandidateWithMdnsName) { NiceMock mock_async_resolver; webrtc::MockAsyncResolverFactory mock_async_resolver_factory; EXPECT_CALL(mock_async_resolver_factory, Create()) @@ -4670,7 +4670,7 @@ TEST_F(P2PTransportChannelTest, ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); // ICE parameter will be set up when creating the channels. set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); - GetEndpoint(0)->network_manager_.CreateMDnsResponder(); + GetEndpoint(0)->network_manager_.CreateMdnsResponder(); GetEndpoint(1)->async_resolver_factory_ = &mock_async_resolver_factory; CreateChannels(); // Pause sending candidates from both endpoints until we find out what port @@ -4726,7 +4726,7 @@ TEST_F(P2PTransportChannelTest, // Test that if we only gather and signal a host candidate, the IP address of // which is obfuscated by an mDNS name, and if the peer can complete the name // resolution with the correct IP address, we can have a p2p connection. -TEST_F(P2PTransportChannelTest, CanConnectWithHostCandidateWithMDnsName) { +TEST_F(P2PTransportChannelTest, CanConnectWithHostCandidateWithMdnsName) { NiceMock mock_async_resolver; webrtc::MockAsyncResolverFactory mock_async_resolver_factory; EXPECT_CALL(mock_async_resolver_factory, Create()) @@ -4737,7 +4737,7 @@ TEST_F(P2PTransportChannelTest, CanConnectWithHostCandidateWithMDnsName) { ConfigureEndpoints(OPEN, OPEN, kOnlyLocalPorts, kOnlyLocalPorts); // ICE parameter will be set up when creating the channels. set_remote_ice_parameter_source(FROM_SETICEPARAMETERS); - GetEndpoint(0)->network_manager_.CreateMDnsResponder(); + GetEndpoint(0)->network_manager_.CreateMdnsResponder(); GetEndpoint(1)->async_resolver_factory_ = &mock_async_resolver_factory; CreateChannels(); // Pause sending candidates from both endpoints until we find out what port diff --git a/p2p/base/port.cc b/p2p/base/port.cc index f5c84334e2..6a1c7b2e99 100644 --- a/p2p/base/port.cc +++ b/p2p/base/port.cc @@ -432,7 +432,7 @@ void Port::AddAddress(const rtc::SocketAddress& address, c.set_url(url); // TODO(bugs.webrtc.org/9723): Use a config to control the feature of IP // handling with mDNS. - if (network_->GetMDnsResponder() != nullptr) { + if (network_->GetMdnsResponder() != nullptr) { // Obfuscate the IP address of a host candidates by an mDNS hostname. if (type == LOCAL_PORT_TYPE) { auto weak_ptr = weak_factory_.GetWeakPtr(); @@ -451,7 +451,7 @@ void Port::AddAddress(const rtc::SocketAddress& address, weak_ptr->FinishAddingAddress(c, is_final); } }; - network_->GetMDnsResponder()->CreateNameForAddress(c.address().ipaddr(), + network_->GetMdnsResponder()->CreateNameForAddress(c.address().ipaddr(), callback); return; } diff --git a/p2p/client/basicportallocator_unittest.cc b/p2p/client/basicportallocator_unittest.cc index a46e53ba9d..5cb1596206 100644 --- a/p2p/client/basicportallocator_unittest.cc +++ b/p2p/client/basicportallocator_unittest.cc @@ -2258,7 +2258,7 @@ TEST_F(BasicPortAllocatorTest, HostCandidateAddressIsReplacedByHostname) { AddTurnServers(kTurnUdpIntIPv6Addr, kTurnTcpIntIPv6Addr); ASSERT_EQ(&network_manager_, allocator().network_manager()); - network_manager_.CreateMDnsResponder(); + network_manager_.CreateMdnsResponder(); AddInterface(kClientAddr); ASSERT_TRUE(CreateSession(ICE_CANDIDATE_COMPONENT_RTP)); session_->StartGettingPorts(); diff --git a/pc/peerconnectionfactory.cc b/pc/peerconnectionfactory.cc index 32b3980e0e..e039d4c7d6 100644 --- a/pc/peerconnectionfactory.cc +++ b/pc/peerconnectionfactory.cc @@ -360,9 +360,12 @@ PeerConnectionFactory::CreatePeerConnection( network_thread_); } if (!dependencies.allocator) { - dependencies.allocator.reset(new cricket::BasicPortAllocator( - default_network_manager_.get(), default_socket_factory_.get(), - configuration.turn_customizer)); + network_thread_->Invoke(RTC_FROM_HERE, [this, &configuration, + &dependencies]() { + dependencies.allocator = absl::make_unique( + default_network_manager_.get(), default_socket_factory_.get(), + configuration.turn_customizer); + }); } // TODO(zstein): Once chromium injects its own AsyncResolverFactory, set diff --git a/rtc_base/fake_mdns_responder.h b/rtc_base/fake_mdns_responder.h index 32d69ba683..e44d30e822 100644 --- a/rtc_base/fake_mdns_responder.h +++ b/rtc_base/fake_mdns_responder.h @@ -21,10 +21,10 @@ namespace webrtc { -class FakeMDnsResponder : public MDnsResponderInterface { +class FakeMdnsResponder : public MdnsResponderInterface { public: - FakeMDnsResponder() = default; - ~FakeMDnsResponder() = default; + FakeMdnsResponder() = default; + ~FakeMdnsResponder() = default; void CreateNameForAddress(const rtc::IPAddress& addr, NameCreatedCallback callback) override { diff --git a/rtc_base/fakenetwork.h b/rtc_base/fakenetwork.h index d5426a3e2c..f3de83b4ef 100644 --- a/rtc_base/fakenetwork.h +++ b/rtc_base/fakenetwork.h @@ -82,16 +82,16 @@ class FakeNetworkManager : public NetworkManagerBase, public MessageHandler { // MessageHandler interface. virtual void OnMessage(Message* msg) { DoUpdateNetworks(); } - void CreateMDnsResponder() { + void CreateMdnsResponder() { if (mdns_responder_ == nullptr) { - mdns_responder_ = absl::make_unique(); + mdns_responder_ = absl::make_unique(); } } using NetworkManagerBase::set_enumeration_permission; using NetworkManagerBase::set_default_local_addresses; - webrtc::MDnsResponderInterface* GetMDnsResponder() const override { + webrtc::MdnsResponderInterface* GetMdnsResponder() const override { return mdns_responder_.get(); } @@ -131,7 +131,7 @@ class FakeNetworkManager : public NetworkManagerBase, public MessageHandler { IPAddress default_local_ipv4_address_; IPAddress default_local_ipv6_address_; - std::unique_ptr mdns_responder_; + std::unique_ptr mdns_responder_; }; } // namespace rtc diff --git a/rtc_base/mdns_responder_interface.h b/rtc_base/mdns_responder_interface.h index 9dbaf56ad8..71938b2576 100644 --- a/rtc_base/mdns_responder_interface.h +++ b/rtc_base/mdns_responder_interface.h @@ -12,36 +12,35 @@ #define RTC_BASE_MDNS_RESPONDER_INTERFACE_H_ #include -#include -#include -#include #include #include "rtc_base/ipaddress.h" -#include "rtc_base/socketaddress.h" namespace webrtc { // Defines an mDNS responder that can be used in ICE candidate gathering, where -// the local IP addresses of host candidates are obfuscated by mDNS hostnames. -class MDnsResponderInterface { +// the local IP addresses of host candidates are replaced by mDNS hostnames. +class MdnsResponderInterface { public: using NameCreatedCallback = std::function; using NameRemovedCallback = std::function; - MDnsResponderInterface() = default; - virtual ~MDnsResponderInterface() = default; + MdnsResponderInterface() = default; + virtual ~MdnsResponderInterface() = default; - // Asynchronously creates a type-4 UUID hostname for an IP address. The - // created name should be given to |callback| with the address that it - // represents. + // Asynchronously creates and returns a new name via |callback| for |addr| if + // there is no name mapped to it by this responder, and initializes the + // reference count of this name to one. Otherwise the existing name mapped to + // |addr| is returned and its reference count is incremented by one. virtual void CreateNameForAddress(const rtc::IPAddress& addr, NameCreatedCallback callback) = 0; - // Removes the name mapped to the given address if there is such an - // name-address mapping previously created via CreateNameForAddress. The - // result of whether an associated name-address mapping is removed should be - // given to |callback|. + // Decrements the reference count of the mapped name of |addr|, if + // there is a map created previously via CreateNameForAddress; asynchronously + // removes the association between |addr| and its mapped name, and returns + // true via |callback| if the decremented reference count reaches zero. + // Otherwise no operation is done and false is returned via |callback| + // asynchronously. virtual void RemoveNameForAddress(const rtc::IPAddress& addr, NameRemovedCallback callback) = 0; }; diff --git a/rtc_base/network.cc b/rtc_base/network.cc index 2dce20a243..5c7b019d66 100644 --- a/rtc_base/network.cc +++ b/rtc_base/network.cc @@ -259,7 +259,7 @@ bool NetworkManager::GetDefaultLocalAddress(int family, IPAddress* addr) const { return false; } -webrtc::MDnsResponderInterface* NetworkManager::GetMDnsResponder() const { +webrtc::MdnsResponderInterface* NetworkManager::GetMdnsResponder() const { return nullptr; } @@ -285,7 +285,7 @@ void NetworkManagerBase::GetAnyAddressNetworks(NetworkList* networks) { new rtc::Network("any", "any", ipv4_any_address, 0, ADAPTER_TYPE_ANY)); ipv4_any_address_network_->set_default_local_address_provider(this); ipv4_any_address_network_->AddIP(ipv4_any_address); - ipv4_any_address_network_->SetMDnsResponder(GetMDnsResponder()); + ipv4_any_address_network_->SetMdnsResponder(GetMdnsResponder()); } networks->push_back(ipv4_any_address_network_.get()); @@ -296,7 +296,7 @@ void NetworkManagerBase::GetAnyAddressNetworks(NetworkList* networks) { "any", "any", ipv6_any_address, 0, ADAPTER_TYPE_ANY)); ipv6_any_address_network_->set_default_local_address_provider(this); ipv6_any_address_network_->AddIP(ipv6_any_address); - ipv6_any_address_network_->SetMDnsResponder(GetMDnsResponder()); + ipv6_any_address_network_->SetMdnsResponder(GetMdnsResponder()); } networks->push_back(ipv6_any_address_network_.get()); } @@ -386,7 +386,7 @@ void NetworkManagerBase::MergeNetworkList(const NetworkList& new_networks, delete net; } } - networks_map_[key]->SetMDnsResponder(GetMDnsResponder()); + networks_map_[key]->SetMdnsResponder(GetMdnsResponder()); } // It may still happen that the merged list is a subset of |networks_|. // To detect this change, we compare their sizes. diff --git a/rtc_base/network.h b/rtc_base/network.h index 601e39f027..8b1a5fcc5c 100644 --- a/rtc_base/network.h +++ b/rtc_base/network.h @@ -141,7 +141,7 @@ class NetworkManager : public DefaultLocalAddressProvider { // Returns the mDNS responder that can be used to obfuscate the local IP // addresses of ICE host candidates by mDNS hostnames. - virtual webrtc::MDnsResponderInterface* GetMDnsResponder() const; + virtual webrtc::MdnsResponderInterface* GetMdnsResponder() const; }; // Base class for NetworkManager implementations. @@ -367,11 +367,11 @@ class Network { // created name will be resolved by the responder. // // The mDNS responder, if not null, should outlive this rtc::Network. - void SetMDnsResponder(webrtc::MDnsResponderInterface* mdns_responder) { + void SetMdnsResponder(webrtc::MdnsResponderInterface* mdns_responder) { mdns_responder_ = mdns_responder; } // Returns the mDNS responder, which is null by default. - webrtc::MDnsResponderInterface* GetMDnsResponder() const { + webrtc::MdnsResponderInterface* GetMdnsResponder() const { return mdns_responder_; } @@ -446,7 +446,7 @@ class Network { int prefix_length_; std::string key_; std::vector ips_; - webrtc::MDnsResponderInterface* mdns_responder_ = nullptr; + webrtc::MdnsResponderInterface* mdns_responder_ = nullptr; int scope_id_; bool ignored_; AdapterType type_; diff --git a/test/fuzzers/mdns_parser_fuzzer.cc b/test/fuzzers/mdns_parser_fuzzer.cc index c3f765ef78..294f683226 100644 --- a/test/fuzzers/mdns_parser_fuzzer.cc +++ b/test/fuzzers/mdns_parser_fuzzer.cc @@ -19,7 +19,7 @@ namespace webrtc { void FuzzOneInput(const uint8_t* data, size_t size) { MessageBufferReader buf(reinterpret_cast(data), size); - auto mdns_msg = absl::make_unique(); + auto mdns_msg = absl::make_unique(); mdns_msg->Read(&buf); }