Commit 01dbb37f authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot 1

IPAddress::validate

Summary: [Folly] `IPAddress::validate`.

Reviewed By: igorsugak

Differential Revision: D3308683

fbshipit-source-id: 48af18d6930f16718372021a4cc08062bf17327e
parent 87e5b407
......@@ -44,6 +44,10 @@ void toAppend(IPAddress addr, fbstring* result) {
result->append(addr.str());
}
bool IPAddress::validate(StringPiece ip) {
return IPAddressV4::validate(ip) || IPAddressV6::validate(ip);
}
// public static
IPAddressV4 IPAddress::createIPv4(const IPAddress& addr) {
if (addr.isV4()) {
......
......@@ -70,6 +70,9 @@ typedef std::pair<IPAddress, uint8_t> CIDRNetwork;
*/
class IPAddress : boost::totally_ordered<IPAddress> {
public:
// returns true iff the input string can be parsed as an ip-address
static bool validate(StringPiece ip);
// return the V4 representation of the address, converting it from V6 to V4 if
// needed. Note that this will throw an IPAddressFormatException if the V6
// address is not IPv4Mapped.
......
......@@ -53,6 +53,7 @@ typedef std::array<uint8_t, 4> ByteArray4;
*/
class IPAddressV4 : boost::totally_ordered<IPAddressV4> {
public:
// returns true iff the input string can be parsed as an ipv4-address
static bool validate(StringPiece ip);
// create an IPAddressV4 instance from a uint32_t (network byte order)
......
......@@ -86,6 +86,7 @@ class IPAddressV6 : boost::totally_ordered<IPAddressV6> {
static constexpr size_t kToFullyQualifiedSize =
8 /*words*/ * 4 /*hex chars per word*/ + 7 /*separators*/;
// returns true iff the input string can be parsed as an ipv6-address
static bool validate(StringPiece ip);
/**
......
......@@ -235,6 +235,12 @@ TEST(IPAddressV6, validate) {
IPAddressV6::validate("2620:0000:1cfe:face:b00c:0000:127.127.127.127"));
}
TEST(IPAddress, validate) {
EXPECT_TRUE(IPAddress::validate("0.0.0.0"));
EXPECT_TRUE(IPAddress::validate("::"));
EXPECT_FALSE(IPAddress::validate("asdf"));
}
// Test addresses constructed using a in[6]_addr value
TEST_P(IPAddressTest, CtorAddress) {
AddressData param = GetParam();
......
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