From: Emil Hesslow Date: Tue, 20 Oct 2015 21:52:07 +0000 (-0700) Subject: Add getIPv6For6To4 to IPAddressV4 X-Git-Tag: deprecate-dynamic-initializer~310 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=39804eda219803f1ee57b87ce1492eaab7d84d78;p=folly.git Add getIPv6For6To4 to IPAddressV4 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 --- diff --git a/folly/IPAddressV4.cpp b/folly/IPAddressV4.cpp index a33386d5..e34f2989 100644 --- a/folly/IPAddressV4.cpp +++ b/folly/IPAddressV4.cpp @@ -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( diff --git a/folly/IPAddressV4.h b/folly/IPAddressV4.h index 8b750fff..bc7be1cf 100644 --- a/folly/IPAddressV4.h +++ b/folly/IPAddressV4.h @@ -98,6 +98,11 @@ class IPAddressV4 : boost::totally_ordered { // 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; diff --git a/folly/test/IPAddressTest.cpp b/folly/test/IPAddressTest.cpp index ef788568..f603fe12 100644 --- a/folly/test/IPAddressTest.cpp +++ b/folly/test/IPAddressTest.cpp @@ -500,7 +500,24 @@ TEST(IPAddress, InSubnetWith6to4) { EXPECT_TRUE(ip3.inSubnet(subnet3, 16)); } -static vector > invalidMasks = { +static const vector 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 > invalidMasks = { {"127.0.0.1", 33}, {"::1", 129}, }; @@ -511,7 +528,7 @@ TEST(IPAddress, InvalidMask) { } } -static vector > v6types = { +static const vector > 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 > provideToLong = { +static const vector > 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 > > provideBinary16Bytes = { +static const vector > > 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 validAddressProvider = { +static const vector 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 validAddressProvider = { {38,32,0,0,28,254,250,206,176,12,0,0,0,0,0,3}, 6), }; -static vector invalidAddressProvider = { +static const vector invalidAddressProvider = { "", "foo", "1.1.1.256", @@ -930,7 +947,7 @@ static vector invalidAddressProvider = { "[1234]", }; -static vector invalidBinaryProvider = { +static const vector invalidBinaryProvider = { {0x31, 0x32, 0x37, 0x2e, 0x30, 0x30, 0x2e, 0x30, 0x2e, 0x31}, // foo {0x66, 0x6f, 0x6f}, @@ -1026,7 +1043,7 @@ static vector flagProvider = { AddressFlags("ff02::1", 6, IS_NONROUTABLE | IS_LINK_LOCAL_BROADCAST), }; -static vector > mapProvider = { +static const vector > 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 > mapProvider = { {"::FFFF:222.1.41.90", "222.1.41.90"}, }; -static vector masksProvider = { +static const vector 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 masksProvider = { MaskData("2620:0:1cfe:face:b00c::3", 0, "::") }; -static vector maskBoundaryProvider = { +static const vector 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),