More useful logging upon failed AsyncServerSocket::bind()
[folly.git] / folly / SocketAddress.h
index 4ff1fd18f6b9511192352ca9184683446b452feb..1953e2135574ee1aecd0d6796394d3dd9230e09c 100644 (file)
@@ -27,6 +27,7 @@
 
 #include <folly/IPAddress.h>
 #include <folly/Portability.h>
+#include <folly/Range.h>
 
 namespace folly {
 
@@ -273,21 +274,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);
-
   // a typedef that allow us to compile against both winsock & POSIX sockets:
   using SocketDesc = decltype(socket(0,0,0)); // POSIX: int, winsock: unsigned