Move detail/FunctionalExcept to portability/BitsFunctexcept
[folly.git] / folly / SocketAddress.h
index 12d8cae7ffb7be85232cec0758042b29a4dbc5fb..788490d0e05b34694205584aacc6b0d0e2a074c5 100644 (file)
 #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 {
 
@@ -274,6 +271,25 @@ 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.
    *
@@ -285,22 +301,19 @@ class SocketAddress {
     setFromPath(StringPiece{path, length});
   }
 
-  // a typedef that allow us to compile against both winsock & POSIX sockets:
-  using SocketDesc = decltype(socket(0,0,0)); // POSIX: int, winsock: unsigned
-
   /**
    * Initialize this SocketAddress from a socket's peer address.
    *
    * Raises std::system_error on error.
    */
-  void setFromPeerAddress(SocketDesc socket);
+  void setFromPeerAddress(int socket);
 
   /**
    * Initialize this SocketAddress from a socket's local address.
    *
    * Raises std::system_error on error.
    */
-  void setFromLocalAddress(SocketDesc socket);
+  void setFromLocalAddress(int socket);
 
   /**
    * Initialize this folly::SocketAddress from a struct sockaddr.
@@ -556,19 +569,11 @@ class SocketAddress {
     }
   };
 
-  // a typedef that allow us to compile against both winsock & POSIX sockets:
-  // (both arg types and calling conventions differ for both)
-  // POSIX: void setFromSocket(int socket,
-  //                  int(*fn)(int, struct sockaddr*, socklen_t*));
-  // mingw: void setFromSocket(unsigned socket,
-  //                  int(*fn)(unsigned, struct sockaddr*, socklen_t*));
-  using GetPeerNameFunc = decltype(getpeername);
-
   struct addrinfo* getAddrInfo(const char* host, uint16_t port, int flags);
   struct addrinfo* getAddrInfo(const char* host, const char* port, int flags);
   void setFromAddrInfo(const struct addrinfo* results);
   void setFromLocalAddr(const struct addrinfo* results);
-  void setFromSocket(SocketDesc socket, GetPeerNameFunc fn);
+  void setFromSocket(int socket, int (*fn)(int, struct sockaddr*, socklen_t*));
   std::string getIpString(int flags) const;
   void getIpString(char *buf, size_t buflen, int flags) const;
 
@@ -596,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_;