folly::FunctionScheduler: Adding support for uniform interval distribution
[folly.git] / folly / IPAddressV4.cpp
index e42416b984204160c8d90f5bc60fbf06e7bc5663..a33386d5aafd8625dc485a6f767a3ba799adf8e1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * limitations under the License.
  */
 
-#include "IPAddressV4.h"
+#include <folly/IPAddressV4.h>
 
 #include <ostream>
 #include <string>
 
-#include "folly/Format.h"
-#include "folly/IPAddress.h"
-#include "folly/IPAddressV6.h"
+#include <folly/Format.h>
+#include <folly/IPAddress.h>
+#include <folly/IPAddressV6.h>
 
 using std::ostream;
 using std::string;
 
 namespace folly {
 
+
 // free functions
 size_t hash_value(const IPAddressV4& addr) {
   return addr.hash();
@@ -143,6 +144,18 @@ bool IPAddressV4::inSubnetWithMask(const IPAddressV4& subnet,
   return (mask == subMask);
 }
 
+// public
+bool IPAddressV4::isLoopback() const {
+  static IPAddressV4 loopback_addr("127.0.0.0");
+  return inSubnetWithMask(loopback_addr, fetchMask(8));
+}
+
+// public
+bool IPAddressV4::isLinkLocal() const {
+  static IPAddressV4 linklocal_addr("169.254.0.0");
+  return inSubnetWithMask(linklocal_addr, fetchMask(16));
+}
+
 // public
 bool IPAddressV4::isNonroutable() const {
   auto ip = toLongHBO();
@@ -185,35 +198,8 @@ IPAddressV4 IPAddressV4::mask(size_t numBits) const {
 }
 
 // public
-// Taken from TSocketAddress::getAddressStrIPv4Fast
 string IPAddressV4::str() const {
-  char buf[INET_ADDRSTRLEN] = {0};
-  const uint8_t* ip = addr_.bytes_.data();
-  int pos = 0;
-  for (int k = 0; k < 4; ++k) {
-    uint8_t num = ip[k];
-
-    if (num >= 200) {
-      buf[pos++] = '2';
-      num -= 200;
-    } else if (num >= 100) {
-      buf[pos++] = '1';
-      num -= 100;
-    }
-
-    // num < 100
-    if (ip[k] >= 10) {
-      buf[pos++] = '0' + num / 10;
-      buf[pos++] = '0' + num % 10;
-    } else {
-      buf[pos++] = '0' + num;
-    }
-
-    buf[pos++] = '.';
-  }
-  buf[pos-1] = '\0';
-  string ipAddr(buf);
-  return std::move(ipAddr);
+  return detail::fastIpv4ToString(addr_.inAddr_);
 }
 
 // public