Add sa_len for sockaddr conversions
[folly.git] / folly / IPAddress.h
index b1c474a602b273da339be1709af5bf6c297dd631..c5f31c778336802610cbd601adfd4a7d6a4c57ab 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -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.
@@ -198,16 +201,23 @@ class IPAddress : boost::totally_ordered<IPAddress> {
     }
     memset(dest, 0, sizeof(sockaddr_storage));
     dest->ss_family = family();
+
     if (isV4()) {
       sockaddr_in *sin = reinterpret_cast<sockaddr_in*>(dest);
       sin->sin_addr = asV4().toAddr();
       sin->sin_port = port;
+#if defined(__APPLE__)
+      sin->sin_len = sizeof(*sin);
+#endif
       return sizeof(*sin);
     } else if (isV6()) {
       sockaddr_in6 *sin = reinterpret_cast<sockaddr_in6*>(dest);
       sin->sin6_addr = asV6().toAddr();
       sin->sin6_port = port;
       sin->sin6_scope_id = asV6().getScopeId();
+#if defined(__APPLE__)
+      sin->sin6_len = sizeof(*sin);
+#endif
       return sizeof(*sin);
     } else {
       throw InvalidAddressFamilyException(family());
@@ -318,6 +328,12 @@ class IPAddress : boost::totally_ordered<IPAddress> {
                   : asV6().isLoopback();
   }
 
+  // Return true if the address qualifies as link local
+  bool isLinkLocal() const {
+    return isV4() ? asV4().isLinkLocal()
+                  : asV6().isLinkLocal();
+  }
+
   // Return true if the address qualifies as broadcast.
   bool isLinkLocalBroadcast() const {
     return isV4() ? asV4().isLinkLocalBroadcast()
@@ -357,8 +373,8 @@ class IPAddress : boost::totally_ordered<IPAddress> {
    * @return IPAddress instance with bits set to 0
    */
   IPAddress mask(uint8_t numBits) const {
-    return isV4() ? IPAddress(std::move(asV4().mask(numBits)))
-                  : IPAddress(std::move(asV6().mask(numBits)));
+    return isV4() ? IPAddress(asV4().mask(numBits))
+                  : IPAddress(asV6().mask(numBits));
   }
 
   /**