Close AsyncServerSocket sockets in reverse order
[folly.git] / folly / SocketAddress.h
index ac1771c484064760d445605a3a8a7179e964b776..4f0d9cfc258539ad108d3c1b00ba7b9a606083c7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2015 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <netinet/in.h>
-#include <features.h>
 #include <netdb.h>
 #include <cstddef>
 #include <iostream>
 #include <string>
 
 #include <folly/IPAddress.h>
+#include <folly/Portability.h>
 
 namespace folly {
 
 class SocketAddress {
  public:
-  SocketAddress() {
-    storage_.addr = folly::IPAddress();
-  }
+  SocketAddress() = default;
 
   /**
    * Construct a SocketAddress from a hostname and port.
@@ -75,17 +73,22 @@ class SocketAddress {
     }
   }
 
+  SocketAddress(const IPAddress& ipAddr, uint16_t port) {
+    setFromIpAddrPort(ipAddr, port);
+  }
+
   SocketAddress(const SocketAddress& addr) {
-    storage_ = addr.storage_;
     port_ = addr.port_;
     if (addr.getFamily() == AF_UNIX) {
       storage_.un.init(addr.storage_.un);
+    } else {
+      storage_ = addr.storage_;
     }
     external_ = addr.external_;
   }
 
   SocketAddress& operator=(const SocketAddress& addr) {
-    if (getFamily() != AF_UNIX) {
+    if (!external_) {
       if (addr.getFamily() != AF_UNIX) {
         storage_ = addr.storage_;
       } else {
@@ -105,7 +108,7 @@ class SocketAddress {
     return *this;
   }
 
-  SocketAddress(SocketAddress&& addr) {
+  SocketAddress(SocketAddress&& addr) noexcept {
     storage_ = addr.storage_;
     port_ = addr.port_;
     external_ = addr.external_;
@@ -120,7 +123,7 @@ class SocketAddress {
   }
 
   ~SocketAddress() {
-    if (getFamily() == AF_UNIX) {
+    if (external_) {
       storage_.un.free();
     }
   }
@@ -191,6 +194,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.
    *
@@ -277,22 +288,25 @@ class SocketAddress {
 
   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(int socket);
+  void setFromPeerAddress(SocketDesc socket);
 
   /**
    * Initialize this SocketAddress from a socket's local address.
    *
    * Raises std::system_error on error.
    */
-  void setFromLocalAddress(int socket);
+  void setFromLocalAddress(SocketDesc socket);
 
   /**
-   * Initialize this TSocketAddress from a struct sockaddr.
+   * Initialize this folly::SocketAddress from a struct sockaddr.
    *
    * Raises std::system_error on error.
    *
@@ -349,7 +363,7 @@ class SocketAddress {
    * Returns the actual size of the storage used.
    */
   socklen_t getAddress(sockaddr_storage* addr) const {
-    if (getFamily() != AF_UNIX) {
+    if (!external_) {
       return storage_.addr.toSockaddrStorage(addr, htons(port_));
     } else {
       memcpy(addr, storage_.un.addr, sizeof(*storage_.un.addr));
@@ -363,6 +377,7 @@ class SocketAddress {
   socklen_t getActualSize() const;
 
   sa_family_t getFamily() const {
+    DCHECK(external_ || AF_UNIX != storage_.addr.family());
     return external_ ? AF_UNIX : storage_.addr.family();
   }
 
@@ -509,17 +524,23 @@ class SocketAddress {
     struct sockaddr_un *addr;
     socklen_t len;
 
+    /* For debugging only, will be removed */
+    uint64_t magic;
+    static constexpr uint64_t kMagic = 0x1234faceb00c;
+
     socklen_t pathLength() const {
       return len - offsetof(struct sockaddr_un, sun_path);
     }
 
     void init() {
       addr = new sockaddr_un;
+      magic = kMagic;
       addr->sun_family = AF_UNIX;
       len = 0;
     }
     void init(const ExternalUnixAddr &other) {
       addr = new sockaddr_un;
+      magic = kMagic;
       len = other.len;
       memcpy(addr, other.addr, len);
       // Fill the rest with 0s, just for safety
@@ -527,19 +548,30 @@ class SocketAddress {
              sizeof(struct sockaddr_un) - len);
     }
     void copy(const ExternalUnixAddr &other) {
+      CHECK(magic == kMagic);
       len = other.len;
       memcpy(addr, other.addr, len);
     }
     void free() {
+      CHECK(magic == kMagic);
       delete addr;
+      magic = 0;
     }
   };
 
+  // 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(int socket, int (*fn)(int, struct sockaddr*, socklen_t*));
+  void setFromSocket(SocketDesc socket, GetPeerNameFunc fn);
   std::string getIpString(int flags) const;
   void getIpString(char *buf, size_t buflen, int flags) const;
 
@@ -547,12 +579,13 @@ class SocketAddress {
 
   void prepFamilyChange(sa_family_t newFamily) {
     if (newFamily != AF_UNIX) {
-      if (getFamily() == AF_UNIX) {
+      if (external_) {
         storage_.un.free();
+        storage_.addr = folly::IPAddress();
       }
       external_ = false;
     } else {
-      if (getFamily() != AF_UNIX) {
+      if (!external_) {
         storage_.un.init();
       }
       external_ = true;
@@ -569,7 +602,7 @@ class SocketAddress {
   union {
     folly::IPAddress addr{};
     ExternalUnixAddr un;
-  } storage_;
+  } storage_{};
   // IPAddress class does nto save zone or port, and must be saved here
   uint16_t port_;