Move detail/FunctionalExcept to portability/BitsFunctexcept
[folly.git] / folly / SocketAddress.h
index 41f41f2d0e7004d4ddf66838bffa14f32ba5f1c7..788490d0e05b34694205584aacc6b0d0e2a074c5 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.
 #pragma once
 
 #include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <netinet/in.h>
-#include <netdb.h>
 #include <cstddef>
-#include <iostream>
+#include <iosfwd>
 #include <string>
 
 #include <folly/IPAddress.h>
 #include <folly/Portability.h>
+#include <folly/Range.h>
+#include <folly/portability/Sockets.h>
 
 namespace folly {
 
 class SocketAddress {
  public:
-  SocketAddress() {}
+  SocketAddress() = default;
 
   /**
    * Construct a SocketAddress from a hostname and port.
@@ -73,6 +71,10 @@ class SocketAddress {
     }
   }
 
+  SocketAddress(const IPAddress& ipAddr, uint16_t port) {
+    setFromIpAddrPort(ipAddr, port);
+  }
+
   SocketAddress(const SocketAddress& addr) {
     port_ = addr.port_;
     if (addr.getFamily() == AF_UNIX) {
@@ -190,6 +192,14 @@ class SocketAddress {
     setFromIpPort(ip.c_str(), port);
   }
 
+  /**
+   * Initialize this SocketAddress from an IPAddress struct and port.
+   *
+   * @param ip The IP address in IPAddress format
+   * @param port The port (in host byte order)
+   */
+  void setFromIpAddrPort(const IPAddress& ip, uint16_t port);
+
   /**
    * Initialize this SocketAddress from a local port number.
    *
@@ -261,21 +271,36 @@ class SocketAddress {
     return setFromHostPort(hostAndPort.c_str());
   }
 
+  /**
+   * Returns the port number from the given socketaddr structure.
+   *
+   * Currently only IPv4 and IPv6 are supported.
+   *
+   * Returns -1 for unsupported socket families.
+   */
+  static int getPortFrom(const struct sockaddr* address);
+
+  /**
+   * Returns the family name from the given socketaddr structure (e.g.: AF_INET6
+   * for IPv6).
+   *
+   * Returns `defaultResult` for unsupported socket families.
+   */
+  static const char* getFamilyNameFrom(
+      const struct sockaddr* address,
+      const char* defaultResult = nullptr);
+
   /**
    * Initialize this SocketAddress from a local unix path.
    *
    * Raises std::invalid_argument on error.
    */
-  void setFromPath(const char* path) {
-    setFromPath(path, strlen(path));
-  }
+  void setFromPath(StringPiece path);
 
-  void setFromPath(const std::string& path) {
-    setFromPath(path.data(), path.length());
+  void setFromPath(const char* path, size_t length) {
+    setFromPath(StringPiece{path, length});
   }
 
-  void setFromPath(const char* path, size_t length);
-
   /**
    * Initialize this SocketAddress from a socket's peer address.
    *
@@ -291,7 +316,7 @@ class SocketAddress {
   void setFromLocalAddress(int socket);
 
   /**
-   * Initialize this TSocketAddress from a struct sockaddr.
+   * Initialize this folly::SocketAddress from a struct sockaddr.
    *
    * Raises std::system_error on error.
    *
@@ -576,9 +601,10 @@ class SocketAddress {
    * If we need to store a Unix socket address, ExternalUnixAddr is a shim to
    * track a struct sockaddr_un allocated separately on the heap.
    */
-  union {
-    folly::IPAddress addr{};
+  union AddrStorage {
+    folly::IPAddress addr;
     ExternalUnixAddr un;
+    AddrStorage() : addr() {}
   } storage_{};
   // IPAddress class does nto save zone or port, and must be saved here
   uint16_t port_;