}
std::string SocketAddress::getFullyQualified() const {
- auto family = getFamily();
- if (family != AF_INET && family != AF_INET6) {
+ if (!isFamilyInet()) {
throw std::invalid_argument("Can't get address str for non ip address");
}
return storage_.addr.toFullyQualified();
}
std::string SocketAddress::getAddressStr() const {
- auto family = getFamily();
- if (family != AF_INET && family != AF_INET6) {
+ if (!isFamilyInet()) {
throw std::invalid_argument("Can't get address str for non ip address");
}
return storage_.addr.str();
}
+bool SocketAddress::isFamilyInet() const {
+ auto family = getFamily();
+ return family == AF_INET || family == AF_INET6;
+}
+
void SocketAddress::getAddressStr(char* buf, size_t buflen) const {
auto ret = getAddressStr();
size_t len = std::min(buflen - 1, ret.size());
*/
void getAddressStr(char* buf, size_t buflen) const;
+ /**
+ * Return true if it is a valid IPv4 or IPv6 address.
+ */
+ bool isFamilyInet() const;
+
/**
* For v4 & v6 addresses, return the fully qualified address string
*/
EXPECT_FALSE(addr.isInitialized());
EXPECT_TRUE(addr.empty());
}
+
+TEST(SocketAddress, ValidFamilyInet) {
+ SocketAddress addr;
+ EXPECT_FALSE(addr.isFamilyInet());
+ folly::IPAddress ipAddr("123.234.0.23");
+ addr.setFromIpAddrPort(ipAddr, 8888);
+ EXPECT_TRUE(addr.isFamilyInet());
+
+ folly::IPAddress ip6Addr("2620:0:1cfe:face:b00c::3");
+ SocketAddress addr6(ip6Addr, 8888);
+ EXPECT_TRUE(addr6.isFamilyInet());
+}