Commit 39804eda authored by Emil Hesslow's avatar Emil Hesslow Committed by facebook-github-bot-1

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
parent ad7ca67b
...@@ -111,13 +111,22 @@ void IPAddressV4::setFromBinary(ByteRange bytes) { ...@@ -111,13 +111,22 @@ void IPAddressV4::setFromBinary(ByteRange bytes) {
// public // public
IPAddressV6 IPAddressV4::createIPv6() const { IPAddressV6 IPAddressV4::createIPv6() const {
ByteArray16 ba{{0}}; ByteArray16 ba{};
ba[10] = 0xff; ba[10] = 0xff;
ba[11] = 0xff; ba[11] = 0xff;
std::memcpy(&ba[12], bytes(), 4); std::memcpy(&ba[12], bytes(), 4);
return IPAddressV6(ba); 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 // public
string IPAddressV4::toJson() const { string IPAddressV4::toJson() const {
return format( return format(
......
...@@ -98,6 +98,11 @@ class IPAddressV4 : boost::totally_ordered<IPAddressV4> { ...@@ -98,6 +98,11 @@ class IPAddressV4 : boost::totally_ordered<IPAddressV4> {
// Return the V6 mapped representation of the address. // Return the V6 mapped representation of the address.
IPAddressV6 createIPv6() const; 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. // Return the long (network byte order) representation of the address.
uint32_t toLong() const { uint32_t toLong() const {
return toAddr().s_addr; return toAddr().s_addr;
......
...@@ -500,7 +500,24 @@ TEST(IPAddress, InSubnetWith6to4) { ...@@ -500,7 +500,24 @@ TEST(IPAddress, InSubnetWith6to4) {
EXPECT_TRUE(ip3.inSubnet(subnet3, 16)); 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}, {"127.0.0.1", 33},
{"::1", 129}, {"::1", 129},
}; };
...@@ -511,7 +528,7 @@ TEST(IPAddress, InvalidMask) { ...@@ -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}, {"::1", IPAddressV6::Type::NORMAL},
{"2620:0:1cfe:face:b00c::3", IPAddressV6::Type::NORMAL}, {"2620:0:1cfe:face:b00c::3", IPAddressV6::Type::NORMAL},
{"2001:0000:4136:e378:8000:63bf:3fff:fdd2", IPAddressV6::Type::TEREDO}, {"2001:0000:4136:e378:8000:63bf:3fff:fdd2", IPAddressV6::Type::TEREDO},
...@@ -555,7 +572,7 @@ TEST(IPAddress, V6Types) { ...@@ -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}, {"0.0.0.0", 0},
{"10.0.0.0", 167772160}, {"10.0.0.0", 167772160},
{"126.131.128.23", 2122547223}, {"126.131.128.23", 2122547223},
...@@ -614,7 +631,7 @@ TEST(IPAddress, fromBinaryV4) { ...@@ -614,7 +631,7 @@ TEST(IPAddress, fromBinaryV4) {
IPAddressFormatException); IPAddressFormatException);
} }
static vector<pair<string, vector<uint8_t> > > provideBinary16Bytes = { static const vector<pair<string, vector<uint8_t> > > provideBinary16Bytes = {
{"::0", {"::0",
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}},
...@@ -910,7 +927,7 @@ TEST(IPAddress, LongestCommonPrefix) { ...@@ -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("127.0.0.1", {127,0,0,1}, 4),
AddressData("69.63.189.16", {69,63,189,16}, 4), AddressData("69.63.189.16", {69,63,189,16}, 4),
AddressData("0.0.0.0", {0,0,0,0}, 4), AddressData("0.0.0.0", {0,0,0,0}, 4),
...@@ -920,7 +937,7 @@ static vector<AddressData> validAddressProvider = { ...@@ -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), {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", "foo",
"1.1.1.256", "1.1.1.256",
...@@ -930,7 +947,7 @@ static vector<string> invalidAddressProvider = { ...@@ -930,7 +947,7 @@ static vector<string> invalidAddressProvider = {
"[1234]", "[1234]",
}; };
static vector<ByteVector> invalidBinaryProvider = { static const vector<ByteVector> invalidBinaryProvider = {
{0x31, 0x32, 0x37, 0x2e, 0x30, 0x30, 0x2e, 0x30, 0x2e, 0x31}, {0x31, 0x32, 0x37, 0x2e, 0x30, 0x30, 0x2e, 0x30, 0x2e, 0x31},
// foo // foo
{0x66, 0x6f, 0x6f}, {0x66, 0x6f, 0x6f},
...@@ -1026,7 +1043,7 @@ static vector<AddressFlags> flagProvider = { ...@@ -1026,7 +1043,7 @@ static vector<AddressFlags> flagProvider = {
AddressFlags("ff02::1", 6, IS_NONROUTABLE | IS_LINK_LOCAL_BROADCAST), 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"}, {"::ffff:192.0.2.128", "192.0.2.128"},
{"192.0.2.128", "::ffff:192.0.2.128"}, {"192.0.2.128", "::ffff:192.0.2.128"},
{"::FFFF:129.144.52.38", "129.144.52.38"}, {"::FFFF:129.144.52.38", "129.144.52.38"},
...@@ -1035,7 +1052,7 @@ static vector<pair<string, string> > mapProvider = { ...@@ -1035,7 +1052,7 @@ static vector<pair<string, string> > mapProvider = {
{"::FFFF:222.1.41.90", "222.1.41.90"}, {"::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", 1, "128.0.0.0"),
MaskData("255.255.255.255", 2, "192.0.0.0"), MaskData("255.255.255.255", 2, "192.0.0.0"),
MaskData("192.0.2.42", 16, "192.0.0.0"), MaskData("192.0.2.42", 16, "192.0.0.0"),
...@@ -1079,7 +1096,7 @@ static vector<MaskData> masksProvider = { ...@@ -1079,7 +1096,7 @@ static vector<MaskData> masksProvider = {
MaskData("2620:0:1cfe:face:b00c::3", 0, "::") 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", 24, "10.1.1.1", true),
MaskBoundaryData("10.1.1.1", 8, "10.1.2.3", true), MaskBoundaryData("10.1.1.1", 8, "10.1.2.3", true),
MaskBoundaryData("2620:0:1cfe:face:b00c::1", 48, "2620:0:1cfe::", true), MaskBoundaryData("2620:0:1cfe:face:b00c::1", 48, "2620:0:1cfe::", true),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment