IPAddressV(46) methods to convert IPs to their inverse in-addr.arpa / ip6.arpa repres...
authorAngelo Failla <pallotron@fb.com>
Wed, 12 Apr 2017 04:13:21 +0000 (21:13 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 12 Apr 2017 04:22:24 +0000 (21:22 -0700)
Summary: As per the title, this adds methods to convert IPv(46) to their inverse representation, this is useful for applications that needs to calculate the inverse arpa representation, typically used when making PTR DNS requests.

Reviewed By: Orvid

Differential Revision: D4867502

fbshipit-source-id: 190e5c309b17a633e1c97b077f212ab38725860f

folly/IPAddressV4.cpp
folly/IPAddressV4.h
folly/IPAddressV6.cpp
folly/IPAddressV6.h
folly/test/IPAddressTest.cpp

index 55f2a7b861c7cb500fe21e6e0a0118cbd9dce2b2..62b05f7be795d9ff79e1f78d70dd2afb60640bde 100644 (file)
@@ -224,6 +224,16 @@ string IPAddressV4::str() const {
   return detail::fastIpv4ToString(addr_.inAddr_);
 }
 
+// public
+string IPAddressV4::toInverseArpaName() const {
+  return sformat(
+      "{}.{}.{}.{}.in-addr.arpa",
+      addr_.bytes_[3],
+      addr_.bytes_[2],
+      addr_.bytes_[1],
+      addr_.bytes_[0]);
+}
+
 // public
 uint8_t IPAddressV4::getNthMSByte(size_t byteIndex) const {
   const auto highestIndex = byteCount() - 1;
index 26a2dba38acdbf0efd1ecc049dd8754f755bd787..6603828a93b8e9bc44c5679946658e2fca7b42ff 100644 (file)
@@ -183,6 +183,8 @@ class IPAddressV4 {
   // @see IPAddress#str
   std::string str() const;
 
+  std::string toInverseArpaName() const;
+
   // return underlying in_addr structure
   in_addr toAddr() const { return addr_.inAddr_; }
 
index 5607a4bfcb37e61eef7d7f7b2e177a9594a4426a..203d14a88489746e77195a6d7ebdedcab266aee1 100644 (file)
@@ -397,6 +397,19 @@ string IPAddressV6::toFullyQualified() const {
   return detail::fastIpv6ToString(addr_.in6Addr_);
 }
 
+// public
+string IPAddressV6::toInverseArpaName() const {
+  constexpr folly::StringPiece lut = "0123456789abcdef";
+  std::array<char, 32> a;
+  int j = 0;
+  for (int i = 15; i >= 0; i--) {
+    a[j] = (lut[bytes()[i] & 0xf]);
+    a[j + 1] = (lut[bytes()[i] >> 4]);
+    j += 2;
+  }
+  return sformat("{}.ip6.arpa", join(".", a));
+}
+
 // public
 uint8_t IPAddressV6::getNthMSByte(size_t byteIndex) const {
   const auto highestIndex = byteCount() - 1;
index a1d0c7cbd3a9cd20d1e5cca951dccd64119fc939..c6ddc453235689659b556696b2b08a2c71396780 100644 (file)
@@ -274,6 +274,8 @@ class IPAddressV6 {
   // @see IPAddress#toFullyQualified
   std::string toFullyQualified() const;
 
+  std::string toInverseArpaName() const;
+
   // @see IPAddress#str
   std::string str() const;
 
index 12a588b1ddeb89975ab3ae9ec7c530daf1e6913b..2911a293454d5261fd82e3920fc8f8c53701ca1b 100644 (file)
@@ -405,6 +405,17 @@ TEST(IPAddress, ToString) {
                               " - ", addr_1, " - ", addr_10_1_2_3));
 }
 
+TEST(IPaddress, toInverseArpaName) {
+  IPAddressV4 addr_ipv4("10.0.0.1");
+  EXPECT_EQ("1.0.0.10.in-addr.arpa", addr_ipv4.toInverseArpaName());
+  IPAddressV6 addr_ipv6("2620:0000:1cfe:face:b00c:0000:0000:0003");
+  EXPECT_EQ(
+      sformat(
+          "{}.ip6.arpa",
+          "3.0.0.0.0.0.0.0.0.0.0.0.c.0.0.b.e.c.a.f.e.f.c.1.0.0.0.0.0.2.6.2"),
+      addr_ipv6.toInverseArpaName());
+}
+
 // Test that invalid string values are killed
 TEST_P(IPAddressCtorTest, InvalidCreation) {
   string addr = GetParam();