folly: add bser encode/decode for dynamic
[folly.git] / folly / IPAddressV4.cpp
index 144e4cce6d006b204ce95d52fddfd14f0682c9e5..e34f29897def1d75c9f826f130d45228bcec6e45 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.
@@ -28,6 +28,7 @@ using std::string;
 
 namespace folly {
 
+
 // free functions
 size_t hash_value(const IPAddressV4& addr) {
   return addr.hash();
@@ -110,13 +111,22 @@ void IPAddressV4::setFromBinary(ByteRange bytes) {
 
 // public
 IPAddressV6 IPAddressV4::createIPv6() const {
-  ByteArray16 ba{{0}};
+  ByteArray16 ba{};
   ba[10] = 0xff;
   ba[11] = 0xff;
   std::memcpy(&ba[12], bytes(), 4);
   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
 string IPAddressV4::toJson() const {
   return format(
@@ -143,6 +153,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();