Fix class member shadowing in folly::ProducerConsumerQueue
[folly.git] / folly / SocketAddress.h
index 4ff1fd18f6b9511192352ca9184683446b452feb..480a37d94941f02a577942f62e9373f716fd02e7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 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 {
 
@@ -153,7 +151,11 @@ class SocketAddress {
   bool isLoopbackAddress() const;
 
   void reset() {
-    prepFamilyChange(AF_UNSPEC);
+    if (external_) {
+      storage_.un.free();
+    }
+    storage_.addr = folly::IPAddress();
+    external_ = false;
   }
 
   /**
@@ -273,37 +275,49 @@ 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
-
   /**
    * 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.
@@ -378,7 +392,7 @@ class SocketAddress {
 
   sa_family_t getFamily() const {
     DCHECK(external_ || AF_UNIX != storage_.addr.family());
-    return external_ ? AF_UNIX : storage_.addr.family();
+    return external_ ? sa_family_t(AF_UNIX) : storage_.addr.family();
   }
 
   bool empty() const {
@@ -401,6 +415,11 @@ class SocketAddress {
    */
   void getAddressStr(char* buf, size_t buflen) const;
 
+  /**
+   * Return true if it is a valid IPv4 or IPv6 address.
+   */
+  bool isFamilyInet() const;
+
   /**
    * For v4 & v6 addresses, return the fully qualified address string
    */
@@ -529,7 +548,7 @@ class SocketAddress {
     static constexpr uint64_t kMagic = 0x1234faceb00c;
 
     socklen_t pathLength() const {
-      return len - offsetof(struct sockaddr_un, sun_path);
+      return socklen_t(len - offsetof(struct sockaddr_un, sun_path));
     }
 
     void init() {
@@ -542,7 +561,7 @@ class SocketAddress {
       addr = new sockaddr_un;
       magic = kMagic;
       len = other.len;
-      memcpy(addr, other.addr, len);
+      memcpy(addr, other.addr, size_t(len));
       // Fill the rest with 0s, just for safety
       memset(reinterpret_cast<char*>(addr) + len, 0,
              sizeof(struct sockaddr_un) - len);
@@ -550,7 +569,7 @@ class SocketAddress {
     void copy(const ExternalUnixAddr &other) {
       CHECK(magic == kMagic);
       len = other.len;
-      memcpy(addr, other.addr, len);
+      memcpy(addr, other.addr, size_t(len));
     }
     void free() {
       CHECK(magic == kMagic);
@@ -559,39 +578,16 @@ 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;
 
   void updateUnixAddressLength(socklen_t addrlen);
 
-  void prepFamilyChange(sa_family_t newFamily) {
-    if (newFamily != AF_UNIX) {
-      if (external_) {
-        storage_.un.free();
-        storage_.addr = folly::IPAddress();
-      }
-      external_ = false;
-    } else {
-      if (!external_) {
-        storage_.un.init();
-      }
-      external_ = true;
-    }
-  }
-
   /*
    * storage_ contains room for a full IPv4 or IPv6 address, so they can be
    * stored inline without a separate allocation on the heap.
@@ -599,9 +595,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_;