diff options
author | btolsch <btolsch@chromium.org> | 2021-02-10 11:22:44 -0800 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2021-02-10 20:09:06 +0000 |
commit | 144746d4a6bd3df27095d321a97b01820c7c2e71 (patch) | |
tree | 88ee862ccc421805ad8e804849760fbd349f86c8 | |
parent | 9d9da7b805d37b9e1bb890cf0200e8e505ef8b39 (diff) | |
download | openscreen-144746d4a6bd3df27095d321a97b01820c7c2e71.tar.gz |
Remove static initializers from discovery/
This change removes IPAddress and std::vector static initializers from
//discovery/mdns to comply with Chromium rules about no static
initializers. IPAddress constructors were made constexpr and
std::vector was changed to std::array.
Bug: 1159043, 1159045, 1159046
Change-Id: I5935242da3b80836111bc43caa418c871ee95c0b
Reviewed-on: https://chromium-review.googlesource.com/c/openscreen/+/2685482
Commit-Queue: Brandon Tolsch <btolsch@chromium.org>
Reviewed-by: Ryan Keane <rwkeane@google.com>
-rw-r--r-- | discovery/mdns/mdns_querier.cc | 17 | ||||
-rw-r--r-- | discovery/mdns/mdns_responder.cc | 3 | ||||
-rw-r--r-- | discovery/mdns/public/mdns_constants.h | 24 | ||||
-rw-r--r-- | platform/base/ip_address.cc | 83 | ||||
-rw-r--r-- | platform/base/ip_address.h | 93 |
5 files changed, 97 insertions, 123 deletions
diff --git a/discovery/mdns/mdns_querier.cc b/discovery/mdns/mdns_querier.cc index 25887ef2..d6aa413d 100644 --- a/discovery/mdns/mdns_querier.cc +++ b/discovery/mdns/mdns_querier.cc @@ -5,6 +5,7 @@ #include "discovery/mdns/mdns_querier.h" #include <algorithm> +#include <array> #include <bitset> #include <memory> #include <unordered_set> @@ -22,7 +23,7 @@ namespace openscreen { namespace discovery { namespace { -const std::vector<DnsType> kTranslatedNsecAnyQueryTypes = { +constexpr std::array<DnsType, 5> kTranslatedNsecAnyQueryTypes = { DnsType::kA, DnsType::kPTR, DnsType::kTXT, DnsType::kAAAA, DnsType::kSRV}; bool IsNegativeResponseFor(const MdnsRecord& record, DnsType type) { @@ -643,21 +644,27 @@ void MdnsQuerier::ProcessRecord(const MdnsRecord& record) { // NSEC records this will be all records which the record dictates the // nonexistence of. std::vector<DnsType> types; - const std::vector<DnsType>* types_ptr = &types; + int types_count = 0; + const DnsType* types_ptr = nullptr; if (record.dns_type() == DnsType::kNSEC) { const auto& nsec_rdata = absl::get<NsecRecordRdata>(record.rdata()); if (std::find(nsec_rdata.types().begin(), nsec_rdata.types().end(), DnsType::kANY) != nsec_rdata.types().end()) { - types_ptr = &kTranslatedNsecAnyQueryTypes; + types_ptr = kTranslatedNsecAnyQueryTypes.data(); + types_count = kTranslatedNsecAnyQueryTypes.size(); } else { - types_ptr = &nsec_rdata.types(); + types_ptr = nsec_rdata.types().data(); + types_count = nsec_rdata.types().size(); } } else { types.push_back(record.dns_type()); + types_ptr = types.data(); + types_count = types.size(); } // Apply the update for each type that the record is associated with. - for (DnsType dns_type : *types_ptr) { + for (int i = 0; i < types_count; ++i) { + DnsType dns_type = types_ptr[i]; switch (record.record_type()) { case RecordType::kShared: { ProcessSharedRecord(record, dns_type); diff --git a/discovery/mdns/mdns_responder.cc b/discovery/mdns/mdns_responder.cc index 4486eacf..7f829e36 100644 --- a/discovery/mdns/mdns_responder.cc +++ b/discovery/mdns/mdns_responder.cc @@ -4,6 +4,7 @@ #include "discovery/mdns/mdns_responder.h" +#include <array> #include <string> #include <utility> @@ -20,7 +21,7 @@ namespace openscreen { namespace discovery { namespace { -const std::array<std::string, 3> kServiceEnumerationDomainLabels{ +constexpr std::array<const char*, 3> kServiceEnumerationDomainLabels{ "_services", "_dns-sd", "_udp"}; enum AddResult { kNonePresent = 0, kAdded, kAlreadyKnown }; diff --git a/discovery/mdns/public/mdns_constants.h b/discovery/mdns/public/mdns_constants.h index c828d3a0..00decc1a 100644 --- a/discovery/mdns/public/mdns_constants.h +++ b/discovery/mdns/public/mdns_constants.h @@ -43,21 +43,21 @@ constexpr uint16_t kDefaultMulticastPort = 5353; // IPv4 group address for sending mDNS messages, given as byte array in // network-order. This is a link-local multicast address, so messages will not // be forwarded outside local network. See RFC 6762, section 3. -const IPAddress kDefaultMulticastGroupIPv4{224, 0, 0, 251}; +constexpr IPAddress kDefaultMulticastGroupIPv4{224, 0, 0, 251}; // IPv6 group address for sending mDNS messages. This is a link-local // multicast address, so messages will not be forwarded outside local network. // See RFC 6762, section 3. -const IPAddress kDefaultMulticastGroupIPv6{ +constexpr IPAddress kDefaultMulticastGroupIPv6{ 0xFF02, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x00FB, }; // The send address for multicast mDNS should be the any address (0.*) on the // default mDNS multicast port. -const IPEndpoint kMulticastSendIPv4Endpoint{kDefaultMulticastGroupIPv4, - kDefaultMulticastPort}; -const IPEndpoint kMulticastSendIPv6Endpoint{kDefaultMulticastGroupIPv6, - kDefaultMulticastPort}; +constexpr IPEndpoint kMulticastSendIPv4Endpoint{kDefaultMulticastGroupIPv4, + kDefaultMulticastPort}; +constexpr IPEndpoint kMulticastSendIPv6Endpoint{kDefaultMulticastGroupIPv6, + kDefaultMulticastPort}; // IPv4 group address for joining cast-specific site-local mDNS multicast group, // given as byte array in network-order. This is a site-local multicast address, @@ -75,9 +75,9 @@ const IPEndpoint kMulticastSendIPv6Endpoint{kDefaultMulticastGroupIPv6, // NOTE: For now the group address is the same group address used for SSDP // discovery, albeit using the MDNS port rather than SSDP port. -const IPAddress kDefaultSiteLocalGroupIPv4{239, 255, 255, 250}; -const IPEndpoint kDefaultSiteLocalGroupIPv4Endpoint{kDefaultSiteLocalGroupIPv4, - kDefaultMulticastPort}; +constexpr IPAddress kDefaultSiteLocalGroupIPv4{239, 255, 255, 250}; +constexpr IPEndpoint kDefaultSiteLocalGroupIPv4Endpoint{ + kDefaultSiteLocalGroupIPv4, kDefaultMulticastPort}; // IPv6 group address for joining cast-specific site-local mDNS multicast group, // give as byte array in network-order. See comments for IPv4 group address for @@ -85,11 +85,11 @@ const IPEndpoint kDefaultSiteLocalGroupIPv4Endpoint{kDefaultSiteLocalGroupIPv4, // 0xFF05 is site-local. See RFC 7346. // FF0X:0:0:0:0:0:0:C is variable scope multicast addresses for SSDP. See // https://www.iana.org/assignments/ipv6-multicast-addresses -const IPAddress kDefaultSiteLocalGroupIPv6{ +constexpr IPAddress kDefaultSiteLocalGroupIPv6{ 0xFF05, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x000C, }; -const IPEndpoint kDefaultSiteLocalGroupIPv6Endpoint{kDefaultSiteLocalGroupIPv6, - kDefaultMulticastPort}; +constexpr IPEndpoint kDefaultSiteLocalGroupIPv6Endpoint{ + kDefaultSiteLocalGroupIPv6, kDefaultMulticastPort}; // Maximum MTU size (1500) minus the UDP header size (8) and IP header size // (20). If any packets are larger than this size, the responder or sender diff --git a/platform/base/ip_address.cc b/platform/base/ip_address.cc index ca2f1143..49fd6ddd 100644 --- a/platform/base/ip_address.cc +++ b/platform/base/ip_address.cc @@ -18,26 +18,6 @@ namespace openscreen { -// static -const IPAddress IPAddress::kAnyV4() { - return IPAddress{0, 0, 0, 0}; -} - -// static -const IPAddress IPAddress::kAnyV6() { - return IPAddress{0, 0, 0, 0, 0, 0, 0, 0}; -} - -// static -const IPAddress IPAddress::kV4LoopbackAddress() { - return IPAddress{127, 0, 0, 1}; -} - -// static -const IPAddress IPAddress::kV6LoopbackAddress() { - return IPAddress{0, 0, 0, 0, 0, 0, 0, 1}; -} - IPAddress::IPAddress(Version version, const uint8_t* b) : version_(version) { if (version_ == Version::kV4) { bytes_ = {{b[0], b[1], b[2], b[3]}}; @@ -47,69 +27,6 @@ IPAddress::IPAddress(Version version, const uint8_t* b) : version_(version) { } } -IPAddress::IPAddress(const std::array<uint8_t, 4>& bytes) - : version_(Version::kV4), - bytes_{{bytes[0], bytes[1], bytes[2], bytes[3]}} {} - -IPAddress::IPAddress(const uint8_t (&b)[4]) - : version_(Version::kV4), bytes_{{b[0], b[1], b[2], b[3]}} {} - -IPAddress::IPAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4) - : version_(Version::kV4), bytes_{{b1, b2, b3, b4}} {} - -IPAddress::IPAddress(const std::array<uint16_t, 8>& hextets) - : IPAddress(hextets[0], - hextets[1], - hextets[2], - hextets[3], - hextets[4], - hextets[5], - hextets[6], - hextets[7]) {} - -IPAddress::IPAddress(const uint16_t (&hextets)[8]) - : IPAddress(hextets[0], - hextets[1], - hextets[2], - hextets[3], - hextets[4], - hextets[5], - hextets[6], - hextets[7]) {} - -IPAddress::IPAddress(uint16_t h0, - uint16_t h1, - uint16_t h2, - uint16_t h3, - uint16_t h4, - uint16_t h5, - uint16_t h6, - uint16_t h7) - : version_(Version::kV6), - bytes_{{ - static_cast<uint8_t>(h0 >> 8), - static_cast<uint8_t>(h0), - static_cast<uint8_t>(h1 >> 8), - static_cast<uint8_t>(h1), - static_cast<uint8_t>(h2 >> 8), - static_cast<uint8_t>(h2), - static_cast<uint8_t>(h3 >> 8), - static_cast<uint8_t>(h3), - static_cast<uint8_t>(h4 >> 8), - static_cast<uint8_t>(h4), - static_cast<uint8_t>(h5 >> 8), - static_cast<uint8_t>(h5), - static_cast<uint8_t>(h6 >> 8), - static_cast<uint8_t>(h6), - static_cast<uint8_t>(h7 >> 8), - static_cast<uint8_t>(h7), - }} {} - -IPAddress::IPAddress(const IPAddress& o) noexcept = default; -IPAddress::IPAddress(IPAddress&& o) noexcept = default; -IPAddress& IPAddress::operator=(const IPAddress& o) noexcept = default; -IPAddress& IPAddress::operator=(IPAddress&& o) noexcept = default; - bool IPAddress::operator==(const IPAddress& o) const { if (version_ != o.version_) return false; diff --git a/platform/base/ip_address.h b/platform/base/ip_address.h index 999a0265..e1ca5c1f 100644 --- a/platform/base/ip_address.h +++ b/platform/base/ip_address.h @@ -22,10 +22,16 @@ class IPAddress { kV6, }; - static const IPAddress kAnyV4(); - static const IPAddress kAnyV6(); - static const IPAddress kV4LoopbackAddress(); - static const IPAddress kV6LoopbackAddress(); + static constexpr IPAddress kAnyV4() { return IPAddress{0, 0, 0, 0}; } + static constexpr IPAddress kAnyV6() { + return IPAddress{0, 0, 0, 0, 0, 0, 0, 0}; + } + static constexpr IPAddress kV4LoopbackAddress() { + return IPAddress{127, 0, 0, 1}; + } + static constexpr IPAddress kV6LoopbackAddress() { + return IPAddress{0, 0, 0, 0, 0, 0, 0, 1}; + } static constexpr size_t kV4Size = 4; static constexpr size_t kV6Size = 16; @@ -36,28 +42,71 @@ class IPAddress { IPAddress(Version version, const uint8_t* bytes); // IPv4 constructors (IPAddress from 4 octets). - explicit IPAddress(const std::array<uint8_t, 4>& bytes); - explicit IPAddress(const uint8_t (&b)[4]); - IPAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4); + explicit constexpr IPAddress(const std::array<uint8_t, 4>& bytes) + : version_(Version::kV4), + bytes_{{bytes[0], bytes[1], bytes[2], bytes[3]}} {} + + explicit constexpr IPAddress(const uint8_t (&b)[4]) + : version_(Version::kV4), bytes_{{b[0], b[1], b[2], b[3]}} {} + + constexpr IPAddress(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4) + : version_(Version::kV4), bytes_{{b1, b2, b3, b4}} {} // IPv6 constructors (IPAddress from 8 hextets). - explicit IPAddress(const std::array<uint16_t, 8>& hextets); - explicit IPAddress(const uint16_t (&hextets)[8]); - IPAddress(uint16_t h1, - uint16_t h2, - uint16_t h3, - uint16_t h4, - uint16_t h5, - uint16_t h6, - uint16_t h7, - uint16_t h8); - - IPAddress(const IPAddress& o) noexcept; - IPAddress(IPAddress&& o) noexcept; + explicit constexpr IPAddress(const std::array<uint16_t, 8>& hextets) + : IPAddress(hextets[0], + hextets[1], + hextets[2], + hextets[3], + hextets[4], + hextets[5], + hextets[6], + hextets[7]) {} + + explicit constexpr IPAddress(const uint16_t (&hextets)[8]) + : IPAddress(hextets[0], + hextets[1], + hextets[2], + hextets[3], + hextets[4], + hextets[5], + hextets[6], + hextets[7]) {} + + constexpr IPAddress(uint16_t h0, + uint16_t h1, + uint16_t h2, + uint16_t h3, + uint16_t h4, + uint16_t h5, + uint16_t h6, + uint16_t h7) + : version_(Version::kV6), + bytes_{{ + static_cast<uint8_t>(h0 >> 8), + static_cast<uint8_t>(h0), + static_cast<uint8_t>(h1 >> 8), + static_cast<uint8_t>(h1), + static_cast<uint8_t>(h2 >> 8), + static_cast<uint8_t>(h2), + static_cast<uint8_t>(h3 >> 8), + static_cast<uint8_t>(h3), + static_cast<uint8_t>(h4 >> 8), + static_cast<uint8_t>(h4), + static_cast<uint8_t>(h5 >> 8), + static_cast<uint8_t>(h5), + static_cast<uint8_t>(h6 >> 8), + static_cast<uint8_t>(h6), + static_cast<uint8_t>(h7 >> 8), + static_cast<uint8_t>(h7), + }} {} + + constexpr IPAddress(const IPAddress& o) noexcept = default; + constexpr IPAddress(IPAddress&& o) noexcept = default; ~IPAddress() = default; - IPAddress& operator=(const IPAddress& o) noexcept; - IPAddress& operator=(IPAddress&& o) noexcept; + constexpr IPAddress& operator=(const IPAddress& o) noexcept = default; + constexpr IPAddress& operator=(IPAddress&& o) noexcept = default; bool operator==(const IPAddress& o) const; bool operator!=(const IPAddress& o) const; |