Add getIPv6For6To4 to IPAddressV4
authorEmil Hesslow <emil@fb.com>
Tue, 20 Oct 2015 21:52:07 +0000 (14:52 -0700)
committerfacebook-github-bot-1 <folly-bot@fb.com>
Wed, 21 Oct 2015 00:20:20 +0000 (17:20 -0700)
Summary: - I added this in PHP ( D2534138 ) so add it here too
- This is my first folly diff so I probably done something wrong :)

Reviewed By: yfeldblum

Differential Revision: D2549168

fb-gh-sync-id: 5e6a80097be01fb54342e0c9da5b69465f695f80

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

index a33386d5aafd8625dc485a6f767a3ba799adf8e1..e34f29897def1d75c9f826f130d45228bcec6e45 100644 (file)
@@ -111,13 +111,22 @@ void IPAddressV4::setFromBinary(ByteRange bytes) {
 
 // public
 IPAddressV6 IPAddressV4::createIPv6() const {
-  ByteArray16 ba{{0}};
+  ByteArray16 ba{};
   ba[10] = 0xff;
   ba[11] = 0xff;
   std::memcpy(&ba[12], bytes(), 4);
   return IPAddressV6(ba);
 }
 
+// public
+IPAddressV6 IPAddressV4::getIPv6For6To4() const {
+  ByteArray16 ba{};
+  ba[0] = (uint8_t)((IPAddressV6::PREFIX_6TO4 & 0xFF00) >> 8);
+  ba[1] = (uint8_t)(IPAddressV6::PREFIX_6TO4 & 0x00FF);
+  std::memcpy(&ba[2], bytes(), 4);
+  return IPAddressV6(ba);
+}
+
 // public
 string IPAddressV4::toJson() const {
   return format(
index 8b750fff7a25279138294008a2dc46ea83b70507..bc7be1cf842a3e40ec1f892b3d01f06ee6ebd093 100644 (file)
@@ -98,6 +98,11 @@ class IPAddressV4 : boost::totally_ordered<IPAddressV4> {
   // Return the V6 mapped representation of the address.
   IPAddressV6 createIPv6() const;
 
+  /**
+   * Return a V6 address in the format of an 6To4 address.
+   */
+  IPAddressV6 getIPv6For6To4() const;
+
   // Return the long (network byte order) representation of the address.
   uint32_t toLong() const {
     return toAddr().s_addr;
index ef7885683623c1e0744475949c30c997aabebf53..f603fe122e6d151f828699ca6b1508b2509ddb51 100644 (file)
@@ -500,7 +500,24 @@ TEST(IPAddress, InSubnetWith6to4) {
   EXPECT_TRUE(ip3.inSubnet(subnet3, 16));
 }
 
-static vector<pair<string, uint8_t> > invalidMasks = {
+static const vector<string> ipv4Strs = {
+  "127.0.0.1",
+  "198.168.0.1",
+  "8.8.0.0",
+};
+TEST(IPAddress, getIPv6For6To4) {
+  for (auto ipv4Str : ipv4Strs) {
+    auto ip = IPAddress(ipv4Str);
+    EXPECT_TRUE(ip.isV4());
+    IPAddressV4 ipv4 = ip.asV4();
+    auto ipv6 = ipv4.getIPv6For6To4();
+    EXPECT_EQ(ipv6.type(), IPAddressV6::Type::T6TO4);
+    auto ipv4New = ipv6.getIPv4For6To4();
+    EXPECT_TRUE(ipv4Str.compare(ipv4New.str()) == 0);
+  }
+}
+
+static const vector<pair<string, uint8_t> > invalidMasks = {
   {"127.0.0.1", 33},
   {"::1", 129},
 };
@@ -511,7 +528,7 @@ TEST(IPAddress, InvalidMask) {
   }
 }
 
-static vector<pair<string, IPAddressV6::Type> > v6types = {
+static const vector<pair<string, IPAddressV6::Type> > v6types = {
   {"::1", IPAddressV6::Type::NORMAL},
   {"2620:0:1cfe:face:b00c::3", IPAddressV6::Type::NORMAL},
   {"2001:0000:4136:e378:8000:63bf:3fff:fdd2", IPAddressV6::Type::TEREDO},
@@ -555,7 +572,7 @@ TEST(IPAddress, V6Types) {
   }
 }
 
-static vector<pair<string, uint32_t> > provideToLong = {
+static const vector<pair<string, uint32_t> > provideToLong = {
   {"0.0.0.0", 0},
   {"10.0.0.0", 167772160},
   {"126.131.128.23", 2122547223},
@@ -614,7 +631,7 @@ TEST(IPAddress, fromBinaryV4) {
                IPAddressFormatException);
 }
 
-static vector<pair<string, vector<uint8_t> > > provideBinary16Bytes = {
+static const vector<pair<string, vector<uint8_t> > > provideBinary16Bytes = {
   {"::0",
     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
@@ -910,7 +927,7 @@ TEST(IPAddress, LongestCommonPrefix) {
 
 }
 
-static vector<AddressData> validAddressProvider = {
+static const vector<AddressData> validAddressProvider = {
   AddressData("127.0.0.1", {127,0,0,1}, 4),
   AddressData("69.63.189.16", {69,63,189,16}, 4),
   AddressData("0.0.0.0", {0,0,0,0}, 4),
@@ -920,7 +937,7 @@ static vector<AddressData> validAddressProvider = {
               {38,32,0,0,28,254,250,206,176,12,0,0,0,0,0,3}, 6),
 };
 
-static vector<string> invalidAddressProvider = {
+static const vector<string> invalidAddressProvider = {
   "",
   "foo",
   "1.1.1.256",
@@ -930,7 +947,7 @@ static vector<string> invalidAddressProvider = {
   "[1234]",
 };
 
-static vector<ByteVector> invalidBinaryProvider = {
+static const vector<ByteVector> invalidBinaryProvider = {
   {0x31, 0x32, 0x37, 0x2e, 0x30, 0x30, 0x2e, 0x30, 0x2e, 0x31},
   // foo
   {0x66, 0x6f, 0x6f},
@@ -1026,7 +1043,7 @@ static vector<AddressFlags> flagProvider = {
   AddressFlags("ff02::1", 6, IS_NONROUTABLE | IS_LINK_LOCAL_BROADCAST),
 };
 
-static vector<pair<string, string> > mapProvider = {
+static const vector<pair<string, string> > mapProvider = {
   {"::ffff:192.0.2.128", "192.0.2.128"},
   {"192.0.2.128", "::ffff:192.0.2.128"},
   {"::FFFF:129.144.52.38", "129.144.52.38"},
@@ -1035,7 +1052,7 @@ static vector<pair<string, string> > mapProvider = {
   {"::FFFF:222.1.41.90", "222.1.41.90"},
 };
 
-static vector<MaskData> masksProvider = {
+static const vector<MaskData> masksProvider = {
   MaskData("255.255.255.255", 1, "128.0.0.0"),
   MaskData("255.255.255.255", 2, "192.0.0.0"),
   MaskData("192.0.2.42", 16, "192.0.0.0"),
@@ -1079,7 +1096,7 @@ static vector<MaskData> masksProvider = {
   MaskData("2620:0:1cfe:face:b00c::3", 0, "::")
 };
 
-static vector<MaskBoundaryData> maskBoundaryProvider = {
+static const vector<MaskBoundaryData> maskBoundaryProvider = {
   MaskBoundaryData("10.1.1.1", 24, "10.1.1.1", true),
   MaskBoundaryData("10.1.1.1", 8, "10.1.2.3", true),
   MaskBoundaryData("2620:0:1cfe:face:b00c::1", 48, "2620:0:1cfe::", true),