From 9ab155789d6e61fb36742bebaadbaa2aa6af189f Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Sat, 29 Jul 2017 11:51:38 -0700 Subject: [PATCH] Compute masks in IPAddressV4 Summary: [Folly] Compute masks in `IPAddressV4`. Just like in `IPAddressV6`. Reviewed By: WillerZ Differential Revision: D5524197 fbshipit-source-id: ebeeab28304bff4f6150cf76216d170908e62aa4 --- folly/IPAddressV4.cpp | 43 ++++-------------------------------- folly/IPAddressV4.h | 2 -- folly/test/IPAddressTest.cpp | 22 ++++++++++++++++++ 3 files changed, 26 insertions(+), 41 deletions(-) diff --git a/folly/IPAddressV4.cpp b/folly/IPAddressV4.cpp index cd81d6d9..bcd582df 100644 --- a/folly/IPAddressV4.cpp +++ b/folly/IPAddressV4.cpp @@ -273,8 +273,10 @@ const ByteArray4 IPAddressV4::fetchMask(size_t numBits) { throw IPAddressFormatException( to("IPv4 addresses are 32 bits")); } - // masks_ is backed by an array so is zero indexed - return masks_[numBits]; + auto const val = Endian::big(uint32_t(~uint64_t(0) << (32 - numBits))); + ByteArray4 arr; + std::memcpy(arr.data(), &val, sizeof(val)); + return arr; } // public static CIDRNetworkV4 IPAddressV4::longestCommonPrefix( @@ -285,41 +287,4 @@ CIDRNetworkV4 IPAddressV4::longestCommonPrefix( return {IPAddressV4(prefix.first), prefix.second}; } -// static private -const std::array IPAddressV4::masks_ = {{ - {{0x00, 0x00, 0x00, 0x00}}, - {{0x80, 0x00, 0x00, 0x00}}, - {{0xc0, 0x00, 0x00, 0x00}}, - {{0xe0, 0x00, 0x00, 0x00}}, - {{0xf0, 0x00, 0x00, 0x00}}, - {{0xf8, 0x00, 0x00, 0x00}}, - {{0xfc, 0x00, 0x00, 0x00}}, - {{0xfe, 0x00, 0x00, 0x00}}, - {{0xff, 0x00, 0x00, 0x00}}, - {{0xff, 0x80, 0x00, 0x00}}, - {{0xff, 0xc0, 0x00, 0x00}}, - {{0xff, 0xe0, 0x00, 0x00}}, - {{0xff, 0xf0, 0x00, 0x00}}, - {{0xff, 0xf8, 0x00, 0x00}}, - {{0xff, 0xfc, 0x00, 0x00}}, - {{0xff, 0xfe, 0x00, 0x00}}, - {{0xff, 0xff, 0x00, 0x00}}, - {{0xff, 0xff, 0x80, 0x00}}, - {{0xff, 0xff, 0xc0, 0x00}}, - {{0xff, 0xff, 0xe0, 0x00}}, - {{0xff, 0xff, 0xf0, 0x00}}, - {{0xff, 0xff, 0xf8, 0x00}}, - {{0xff, 0xff, 0xfc, 0x00}}, - {{0xff, 0xff, 0xfe, 0x00}}, - {{0xff, 0xff, 0xff, 0x00}}, - {{0xff, 0xff, 0xff, 0x80}}, - {{0xff, 0xff, 0xff, 0xc0}}, - {{0xff, 0xff, 0xff, 0xe0}}, - {{0xff, 0xff, 0xff, 0xf0}}, - {{0xff, 0xff, 0xff, 0xf8}}, - {{0xff, 0xff, 0xff, 0xfc}}, - {{0xff, 0xff, 0xff, 0xfe}}, - {{0xff, 0xff, 0xff, 0xff}} -}}; - } // folly diff --git a/folly/IPAddressV4.h b/folly/IPAddressV4.h index 2de04f1c..1e2c65e7 100644 --- a/folly/IPAddressV4.h +++ b/folly/IPAddressV4.h @@ -269,8 +269,6 @@ class IPAddressV4 { explicit AddressStorage(const in_addr addr): inAddr_(addr) {} } addr_; - static const std::array masks_; - /** * Set the current IPAddressV4 object to have the address specified by bytes. * @throws IPAddressFormatException if bytes.size() is not 4. diff --git a/folly/test/IPAddressTest.cpp b/folly/test/IPAddressTest.cpp index 2f93e466..0d6bca5e 100644 --- a/folly/test/IPAddressTest.cpp +++ b/folly/test/IPAddressTest.cpp @@ -1272,6 +1272,28 @@ INSTANTIATE_TEST_CASE_P(IPAddress, IPAddressBitAccessorTest, ::testing::ValuesIn(validAddressProvider)); +TEST(IPAddressV4, fetchMask) { + struct X : private IPAddressV4 { + using IPAddressV4::fetchMask; + }; + + EXPECT_THAT( + X::fetchMask(0), + ::testing::ElementsAreArray(ByteArray4{{0x00, 0x00, 0x00, 0x00}})); + + EXPECT_THAT( + X::fetchMask(1), + ::testing::ElementsAreArray(ByteArray4{{0x80, 0x00, 0x00, 0x00}})); + + EXPECT_THAT( + X::fetchMask(31), + ::testing::ElementsAreArray(ByteArray4{{0xff, 0xff, 0xff, 0xfe}})); + + EXPECT_THAT( + X::fetchMask(32), + ::testing::ElementsAreArray(ByteArray4{{0xff, 0xff, 0xff, 0xff}})); +} + TEST(IPAddressV6, fetchMask) { using ByteArray8 = std::array; -- 2.34.1