Register singleton's destruction using std::atexit
[folly.git] / folly / IPAddressV4.cpp
index 3e7704e01414f7d93f5d3752caa382aa8c2dc2b6..af55def4fa291ff0eff943190cf5b4e8e824065c 100644 (file)
@@ -29,7 +29,6 @@ using std::string;
 
 namespace folly {
 
-
 // free functions
 size_t hash_value(const IPAddressV4& addr) {
   return addr.hash();
@@ -45,14 +44,8 @@ void toAppend(IPAddressV4 addr, fbstring* result) {
   result->append(addr.str());
 }
 
-bool IPAddressV4::validate(StringPiece ip) {
-  constexpr size_t kStrMaxLen = INET_ADDRSTRLEN;
-  std::array<char, kStrMaxLen + 1> ip_cstr;
-  const size_t len = std::min(ip.size(), kStrMaxLen);
-  std::memcpy(ip_cstr.data(), ip.data(), len);
-  ip_cstr[len] = 0;
-  struct in_addr addr;
-  return 1 == inet_pton(AF_INET, ip_cstr.data(), &addr);
+bool IPAddressV4::validate(StringPiece ip) noexcept {
+  return tryFromString(ip).hasValue();
 }
 
 // public static
@@ -85,39 +78,60 @@ uint32_t IPAddressV4::toLongHBO(StringPiece ip) {
 }
 
 // public default constructor
-IPAddressV4::IPAddressV4() {
-}
+IPAddressV4::IPAddressV4() {}
 
 // ByteArray4 constructor
-IPAddressV4::IPAddressV4(const ByteArray4& src)
-  : addr_(src)
-{
-}
+IPAddressV4::IPAddressV4(const ByteArray4& src) noexcept : addr_(src) {}
 
 // public string constructor
-IPAddressV4::IPAddressV4(StringPiece addr)
-  : addr_()
-{
-  auto ip = addr.str();
-  if (inet_pton(AF_INET, ip.c_str(), &addr_.inAddr_) != 1) {
-    throw IPAddressFormatException(sformat("Invalid IPv4 address '{}'", addr));
+IPAddressV4::IPAddressV4(StringPiece addr) : addr_() {
+  auto maybeIp = tryFromString(addr);
+  if (maybeIp.hasError()) {
+    throw IPAddressFormatException(
+        to<std::string>("Invalid IPv4 address '", addr, "'"));
   }
+  *this = std::move(maybeIp.value());
+}
+
+Expected<IPAddressV4, IPAddressFormatError> IPAddressV4::tryFromString(
+    StringPiece str) noexcept {
+  struct in_addr inAddr;
+  if (inet_pton(AF_INET, str.str().c_str(), &inAddr) != 1) {
+    return makeUnexpected(IPAddressFormatError::INVALID_IP);
+  }
+  return IPAddressV4(inAddr);
 }
 
 // in_addr constructor
-IPAddressV4::IPAddressV4(const in_addr src)
-  : addr_(src)
-{
+IPAddressV4::IPAddressV4(const in_addr src) noexcept : addr_(src) {}
+
+IPAddressV4 IPAddressV4::fromBinary(ByteRange bytes) {
+  auto maybeIp = tryFromBinary(bytes);
+  if (maybeIp.hasError()) {
+    throw IPAddressFormatException(to<std::string>(
+        "Invalid IPv4 binary data: length must be 4 bytes, got ",
+        bytes.size()));
+  }
+  return maybeIp.value();
 }
 
-// public
-void IPAddressV4::setFromBinary(ByteRange bytes) {
+Expected<IPAddressV4, IPAddressFormatError> IPAddressV4::tryFromBinary(
+    ByteRange bytes) noexcept {
+  IPAddressV4 addr;
+  auto setResult = addr.trySetFromBinary(bytes);
+  if (setResult.hasError()) {
+    return makeUnexpected(std::move(setResult.error()));
+  }
+  return addr;
+}
+
+Expected<Unit, IPAddressFormatError> IPAddressV4::trySetFromBinary(
+    ByteRange bytes) noexcept {
   if (bytes.size() != 4) {
-    throw IPAddressFormatException(sformat(
-        "Invalid IPv4 binary data: length must be 4 bytes, got {}",
-        bytes.size()));
+    return makeUnexpected(IPAddressFormatError::INVALID_IP);
   }
   memcpy(&addr_.inAddr_.s_addr, bytes.data(), sizeof(in_addr));
+  return folly::unit;
 }
 
 // static
@@ -136,8 +150,6 @@ IPAddressV4 IPAddressV4::fromInverseArpaName(const std::string& arpaname) {
   // reverse 1.0.168.192 -> 192.168.0.1
   return IPAddressV4(join(".", pieces.rbegin(), pieces.rend()));
 }
-
-// public
 IPAddressV6 IPAddressV4::createIPv6() const {
   ByteArray16 ba{};
   ba[10] = 0xff;
@@ -172,11 +184,11 @@ bool IPAddressV4::inSubnet(StringPiece cidrNetwork) const {
 }
 
 // public
-bool IPAddressV4::inSubnetWithMask(const IPAddressV4& subnet,
-                                   const ByteArray4 cidrMask) const {
-  const ByteArray4 mask = detail::Bytes::mask(toByteArray(), cidrMask);
-  const ByteArray4 subMask = detail::Bytes::mask(subnet.toByteArray(),
-                                                 cidrMask);
+bool IPAddressV4::inSubnetWithMask(
+    const IPAddressV4& subnet,
+    const ByteArray4 cidrMask) const {
+  const auto mask = detail::Bytes::mask(toByteArray(), cidrMask);
+  const auto subMask = detail::Bytes::mask(subnet.toByteArray(), cidrMask);
   return (mask == subMask);
 }
 
@@ -196,24 +208,26 @@ bool IPAddressV4::isLinkLocal() const {
 bool IPAddressV4::isNonroutable() const {
   auto ip = toLongHBO();
   return isPrivate() ||
-      (ip <= 0x00FFFFFF)                     || // 0.0.0.0-0.255.255.255
+      (/* align */ true && ip <= 0x00FFFFFF) || // 0.0.0.0-0.255.255.255
       (ip >= 0xC0000000 && ip <= 0xC00000FF) || // 192.0.0.0-192.0.0.255
       (ip >= 0xC0000200 && ip <= 0xC00002FF) || // 192.0.2.0-192.0.2.255
       (ip >= 0xC6120000 && ip <= 0xC613FFFF) || // 198.18.0.0-198.19.255.255
       (ip >= 0xC6336400 && ip <= 0xC63364FF) || // 198.51.100.0-198.51.100.255
       (ip >= 0xCB007100 && ip <= 0xCB0071FF) || // 203.0.113.0-203.0.113.255
-      (ip >= 0xE0000000 && ip <= 0xFFFFFFFF);   // 224.0.0.0-255.255.255.255
+      (ip >= 0xE0000000 && ip <= 0xFFFFFFFF) || // 224.0.0.0-255.255.255.255
+      false;
 }
 
 // public
 bool IPAddressV4::isPrivate() const {
   auto ip = toLongHBO();
-  return
+  return // some ranges below
       (ip >= 0x0A000000 && ip <= 0x0AFFFFFF) || // 10.0.0.0-10.255.255.255
       (ip >= 0x7F000000 && ip <= 0x7FFFFFFF) || // 127.0.0.0-127.255.255.255
       (ip >= 0xA9FE0000 && ip <= 0xA9FEFFFF) || // 169.254.0.0-169.254.255.255
       (ip >= 0xAC100000 && ip <= 0xAC1FFFFF) || // 172.16.0.0-172.31.255.255
-      (ip >= 0xC0A80000 && ip <= 0xC0A8FFFF);   // 192.168.0.0-192.168.255.255
+      (ip >= 0xC0A80000 && ip <= 0xC0A8FFFF) || // 192.168.0.0-192.168.255.255
+      false;
 }
 
 // public