src/socket_wrapper.c: always go through swrap_bind_symbol_all() protected by pthread_...
[socket_wrapper.git] / src / socket_wrapper.c
index 5b82e0c5c390374e4917c96720330694f3281a51..388b764018d2af105445dfe158902f9ed657e746 100644 (file)
@@ -66,6 +66,9 @@
 #include <sys/un.h>
 #include <netinet/in.h>
 #include <netinet/tcp.h>
+#ifdef HAVE_NETINET_TCP_FSM_H
+#include <netinet/tcp_fsm.h>
+#endif
 #include <arpa/inet.h>
 #include <fcntl.h>
 #include <stdlib.h>
@@ -176,11 +179,11 @@ enum swrap_dbglvl_e {
 #endif
 
 /* Add new global locks here please */
-# define SWRAP_LOCK_ALL \
-       swrap_mutex_lock(&libc_symbol_binding_mutex); \
+# define SWRAP_LOCK_ALL do { \
+} while(0)
 
-# define SWRAP_UNLOCK_ALL \
-       swrap_mutex_unlock(&libc_symbol_binding_mutex); \
+# define SWRAP_UNLOCK_ALL do { \
+} while(0)
 
 #define SOCKET_INFO_CONTAINER(si) \
        (struct socket_info_container *)(si)
@@ -264,6 +267,7 @@ struct socket_info
        int defer_connect;
        int pktinfo;
        int tcp_nodelay;
+       int listening;
 
        /* The unix path so we can unlink it on close() */
        struct sockaddr_un un_addr;
@@ -305,9 +309,6 @@ static size_t socket_fds_max = SOCKET_WRAPPER_MAX_SOCKETS_LIMIT;
 /* Hash table to map fds to corresponding socket_info index */
 static int *socket_fds_idx;
 
-/* Mutex to synchronize access to global libc.symbols */
-static pthread_mutex_t libc_symbol_binding_mutex = PTHREAD_MUTEX_INITIALIZER;
-
 /* Mutex for syncronizing port selection during swrap_auto_bind() */
 static pthread_mutex_t autobind_start_mutex;
 
@@ -330,7 +331,14 @@ static pthread_mutex_t mtu_update_mutex;
 
 bool socket_wrapper_enabled(void);
 
+#if ! defined(HAVE_CONSTRUCTOR_ATTRIBUTE) && defined(HAVE_PRAGMA_INIT)
+/* xlC and other oldschool compilers support (only) this */
+#pragma init (swrap_constructor)
+#endif
 void swrap_constructor(void) CONSTRUCTOR_ATTRIBUTE;
+#if ! defined(HAVE_DESTRUCTOR_ATTRIBUTE) && defined(HAVE_PRAGMA_FINI)
+#pragma fini (swrap_destructor)
+#endif
 void swrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
 
 #ifndef HAVE_GETPROGNAME
@@ -561,7 +569,6 @@ static char *socket_wrapper_dir(void);
 
 enum swrap_lib {
     SWRAP_LIBC,
-    SWRAP_LIBNSL,
     SWRAP_LIBSOCKET,
 };
 
@@ -570,8 +577,6 @@ static const char *swrap_str_lib(enum swrap_lib lib)
        switch (lib) {
        case SWRAP_LIBC:
                return "libc";
-       case SWRAP_LIBNSL:
-               return "libnsl";
        case SWRAP_LIBSOCKET:
                return "libsocket";
        }
@@ -609,7 +614,6 @@ static void *swrap_load_lib_handle(enum swrap_lib lib)
 #endif
 
        switch (lib) {
-       case SWRAP_LIBNSL:
        case SWRAP_LIBSOCKET:
 #ifdef HAVE_LIBSOCKET
                handle = swrap.libc.socket_handle;
@@ -658,7 +662,7 @@ static void *swrap_load_lib_handle(enum swrap_lib lib)
                handle = swrap.libc.handle = swrap.libc.socket_handle = RTLD_NEXT;
 #else
                SWRAP_LOG(SWRAP_LOG_ERROR,
-                         "Failed to dlopen library: %s\n",
+                         "Failed to dlopen library: %s",
                          dlerror());
                exit(-1);
 #endif
@@ -677,7 +681,7 @@ static void *_swrap_bind_symbol(enum swrap_lib lib, const char *fn_name)
        func = dlsym(handle, fn_name);
        if (func == NULL) {
                SWRAP_LOG(SWRAP_LOG_ERROR,
-                         "Failed to find %s: %s\n",
+                         "Failed to find %s: %s",
                          fn_name,
                          dlerror());
                exit(-1);
@@ -719,35 +723,18 @@ static void swrap_mutex_unlock(pthread_mutex_t *mutex)
  * This is an optimization to avoid locking each time we check if the symbol is
  * bound.
  */
+#define _swrap_bind_symbol_generic(lib, sym_name) do { \
+       swrap.libc.symbols._libc_##sym_name.obj = \
+               _swrap_bind_symbol(lib, #sym_name); \
+} while(0);
+
 #define swrap_bind_symbol_libc(sym_name) \
-       if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
-               swrap_mutex_lock(&libc_symbol_binding_mutex); \
-               if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
-                       swrap.libc.symbols._libc_##sym_name.obj = \
-                               _swrap_bind_symbol(SWRAP_LIBC, #sym_name); \
-               } \
-               swrap_mutex_unlock(&libc_symbol_binding_mutex); \
-       }
+       _swrap_bind_symbol_generic(SWRAP_LIBC, sym_name)
 
 #define swrap_bind_symbol_libsocket(sym_name) \
-       if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
-               swrap_mutex_lock(&libc_symbol_binding_mutex); \
-               if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
-                       swrap.libc.symbols._libc_##sym_name.obj = \
-                               _swrap_bind_symbol(SWRAP_LIBSOCKET, #sym_name); \
-               } \
-               swrap_mutex_unlock(&libc_symbol_binding_mutex); \
-       }
+       _swrap_bind_symbol_generic(SWRAP_LIBSOCKET, sym_name)
 
-#define swrap_bind_symbol_libnsl(sym_name) \
-       if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
-               swrap_mutex_lock(&libc_symbol_binding_mutex); \
-               if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
-                       swrap.libc.symbols._libc_##sym_name.obj = \
-                               _swrap_bind_symbol(SWRAP_LIBNSL, #sym_name); \
-               } \
-               swrap_mutex_unlock(&libc_symbol_binding_mutex); \
-       }
+static void swrap_bind_symbol_all(void);
 
 /****************************************************************************
  *                               IMPORTANT
@@ -766,7 +753,7 @@ static int libc_accept4(int sockfd,
                        socklen_t *addrlen,
                        int flags)
 {
-       swrap_bind_symbol_libsocket(accept4);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_accept4.f(sockfd, addr, addrlen, flags);
 }
@@ -775,7 +762,7 @@ static int libc_accept4(int sockfd,
 
 static int libc_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
 {
-       swrap_bind_symbol_libsocket(accept);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_accept.f(sockfd, addr, addrlen);
 }
@@ -785,14 +772,14 @@ static int libc_bind(int sockfd,
                     const struct sockaddr *addr,
                     socklen_t addrlen)
 {
-       swrap_bind_symbol_libsocket(bind);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_bind.f(sockfd, addr, addrlen);
 }
 
 static int libc_close(int fd)
 {
-       swrap_bind_symbol_libc(close);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_close.f(fd);
 }
@@ -801,21 +788,21 @@ static int libc_connect(int sockfd,
                        const struct sockaddr *addr,
                        socklen_t addrlen)
 {
-       swrap_bind_symbol_libsocket(connect);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_connect.f(sockfd, addr, addrlen);
 }
 
 static int libc_dup(int fd)
 {
-       swrap_bind_symbol_libc(dup);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_dup.f(fd);
 }
 
 static int libc_dup2(int oldfd, int newfd)
 {
-       swrap_bind_symbol_libc(dup2);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_dup2.f(oldfd, newfd);
 }
@@ -823,7 +810,7 @@ static int libc_dup2(int oldfd, int newfd)
 #ifdef HAVE_EVENTFD
 static int libc_eventfd(int count, int flags)
 {
-       swrap_bind_symbol_libc(eventfd);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_eventfd.f(count, flags);
 }
@@ -835,7 +822,7 @@ static int libc_vfcntl(int fd, int cmd, va_list ap)
        void *arg;
        int rc;
 
-       swrap_bind_symbol_libc(fcntl);
+       swrap_bind_symbol_all();
 
        arg = va_arg(ap, void *);
 
@@ -848,7 +835,7 @@ static int libc_getpeername(int sockfd,
                            struct sockaddr *addr,
                            socklen_t *addrlen)
 {
-       swrap_bind_symbol_libsocket(getpeername);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_getpeername.f(sockfd, addr, addrlen);
 }
@@ -857,7 +844,7 @@ static int libc_getsockname(int sockfd,
                            struct sockaddr *addr,
                            socklen_t *addrlen)
 {
-       swrap_bind_symbol_libsocket(getsockname);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_getsockname.f(sockfd, addr, addrlen);
 }
@@ -868,7 +855,7 @@ static int libc_getsockopt(int sockfd,
                           void *optval,
                           socklen_t *optlen)
 {
-       swrap_bind_symbol_libsocket(getsockopt);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_getsockopt.f(sockfd,
                                                     level,
@@ -883,7 +870,7 @@ static int libc_vioctl(int d, unsigned long int request, va_list ap)
        void *arg;
        int rc;
 
-       swrap_bind_symbol_libc(ioctl);
+       swrap_bind_symbol_all();
 
        arg = va_arg(ap, void *);
 
@@ -894,14 +881,14 @@ static int libc_vioctl(int d, unsigned long int request, va_list ap)
 
 static int libc_listen(int sockfd, int backlog)
 {
-       swrap_bind_symbol_libsocket(listen);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_listen.f(sockfd, backlog);
 }
 
 static FILE *libc_fopen(const char *name, const char *mode)
 {
-       swrap_bind_symbol_libc(fopen);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_fopen.f(name, mode);
 }
@@ -909,7 +896,7 @@ static FILE *libc_fopen(const char *name, const char *mode)
 #ifdef HAVE_FOPEN64
 static FILE *libc_fopen64(const char *name, const char *mode)
 {
-       swrap_bind_symbol_libc(fopen64);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_fopen64.f(name, mode);
 }
@@ -920,7 +907,7 @@ static int libc_vopen(const char *pathname, int flags, va_list ap)
        int mode = 0;
        int fd;
 
-       swrap_bind_symbol_libc(open);
+       swrap_bind_symbol_all();
 
        if (flags & O_CREAT) {
                mode = va_arg(ap, int);
@@ -948,7 +935,7 @@ static int libc_vopen64(const char *pathname, int flags, va_list ap)
        int mode = 0;
        int fd;
 
-       swrap_bind_symbol_libc(open64);
+       swrap_bind_symbol_all();
 
        if (flags & O_CREAT) {
                mode = va_arg(ap, int);
@@ -964,7 +951,7 @@ static int libc_vopenat(int dirfd, const char *path, int flags, va_list ap)
        int mode = 0;
        int fd;
 
-       swrap_bind_symbol_libc(openat);
+       swrap_bind_symbol_all();
 
        if (flags & O_CREAT) {
                mode = va_arg(ap, int);
@@ -993,28 +980,28 @@ static int libc_openat(int dirfd, const char *path, int flags, ...)
 
 static int libc_pipe(int pipefd[2])
 {
-       swrap_bind_symbol_libsocket(pipe);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_pipe.f(pipefd);
 }
 
 static int libc_read(int fd, void *buf, size_t count)
 {
-       swrap_bind_symbol_libc(read);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_read.f(fd, buf, count);
 }
 
 static ssize_t libc_readv(int fd, const struct iovec *iov, int iovcnt)
 {
-       swrap_bind_symbol_libsocket(readv);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_readv.f(fd, iov, iovcnt);
 }
 
 static int libc_recv(int sockfd, void *buf, size_t len, int flags)
 {
-       swrap_bind_symbol_libsocket(recv);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_recv.f(sockfd, buf, len, flags);
 }
@@ -1026,7 +1013,7 @@ static int libc_recvfrom(int sockfd,
                         struct sockaddr *src_addr,
                         socklen_t *addrlen)
 {
-       swrap_bind_symbol_libsocket(recvfrom);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_recvfrom.f(sockfd,
                                                   buf,
@@ -1038,21 +1025,21 @@ static int libc_recvfrom(int sockfd,
 
 static int libc_recvmsg(int sockfd, struct msghdr *msg, int flags)
 {
-       swrap_bind_symbol_libsocket(recvmsg);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_recvmsg.f(sockfd, msg, flags);
 }
 
 static int libc_send(int sockfd, const void *buf, size_t len, int flags)
 {
-       swrap_bind_symbol_libsocket(send);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_send.f(sockfd, buf, len, flags);
 }
 
 static int libc_sendmsg(int sockfd, const struct msghdr *msg, int flags)
 {
-       swrap_bind_symbol_libsocket(sendmsg);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_sendmsg.f(sockfd, msg, flags);
 }
@@ -1064,7 +1051,7 @@ static int libc_sendto(int sockfd,
                       const  struct sockaddr *dst_addr,
                       socklen_t addrlen)
 {
-       swrap_bind_symbol_libsocket(sendto);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_sendto.f(sockfd,
                                                 buf,
@@ -1080,7 +1067,7 @@ static int libc_setsockopt(int sockfd,
                           const void *optval,
                           socklen_t optlen)
 {
-       swrap_bind_symbol_libsocket(setsockopt);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_setsockopt.f(sockfd,
                                                     level,
@@ -1092,7 +1079,7 @@ static int libc_setsockopt(int sockfd,
 #ifdef HAVE_SIGNALFD
 static int libc_signalfd(int fd, const sigset_t *mask, int flags)
 {
-       swrap_bind_symbol_libsocket(signalfd);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_signalfd.f(fd, mask, flags);
 }
@@ -1100,14 +1087,14 @@ static int libc_signalfd(int fd, const sigset_t *mask, int flags)
 
 static int libc_socket(int domain, int type, int protocol)
 {
-       swrap_bind_symbol_libsocket(socket);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_socket.f(domain, type, protocol);
 }
 
 static int libc_socketpair(int domain, int type, int protocol, int sv[2])
 {
-       swrap_bind_symbol_libsocket(socketpair);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_socketpair.f(domain, type, protocol, sv);
 }
@@ -1115,7 +1102,7 @@ static int libc_socketpair(int domain, int type, int protocol, int sv[2])
 #ifdef HAVE_TIMERFD_CREATE
 static int libc_timerfd_create(int clockid, int flags)
 {
-       swrap_bind_symbol_libc(timerfd_create);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_timerfd_create.f(clockid, flags);
 }
@@ -1123,20 +1110,20 @@ static int libc_timerfd_create(int clockid, int flags)
 
 static ssize_t libc_write(int fd, const void *buf, size_t count)
 {
-       swrap_bind_symbol_libc(write);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_write.f(fd, buf, count);
 }
 
 static ssize_t libc_writev(int fd, const struct iovec *iov, int iovcnt)
 {
-       swrap_bind_symbol_libsocket(writev);
+       swrap_bind_symbol_all();
 
        return swrap.libc.symbols._libc_writev.f(fd, iov, iovcnt);
 }
 
 /* DO NOT call this function during library initialization! */
-static void swrap_bind_symbol_all(void)
+static void __swrap_bind_symbol_all_once(void)
 {
 #ifdef HAVE_ACCEPT4
        swrap_bind_symbol_libsocket(accept4);
@@ -1188,10 +1175,105 @@ static void swrap_bind_symbol_all(void)
        swrap_bind_symbol_libsocket(writev);
 }
 
+static void swrap_bind_symbol_all(void)
+{
+       static pthread_once_t all_symbol_binding_once = PTHREAD_ONCE_INIT;
+
+       pthread_once(&all_symbol_binding_once, __swrap_bind_symbol_all_once);
+}
+
 /*********************************************************
  * SWRAP HELPER FUNCTIONS
  *********************************************************/
 
+/*
+ * We return 127.0.0.0 (default) or 10.53.57.0.
+ *
+ * This can be controlled by:
+ * SOCKET_WRAPPER_IPV4_NETWORK=127.0.0.0 (default)
+ * or
+ * SOCKET_WRAPPER_IPV4_NETWORK=10.53.57.0
+ */
+static in_addr_t swrap_ipv4_net(void)
+{
+       static int initialized;
+       static in_addr_t hv;
+       const char *net_str = NULL;
+       struct in_addr nv;
+       int ret;
+
+       if (initialized) {
+               return hv;
+       }
+       initialized = 1;
+
+       net_str = getenv("SOCKET_WRAPPER_IPV4_NETWORK");
+       if (net_str == NULL) {
+               net_str = "127.0.0.0";
+       }
+
+       ret = inet_pton(AF_INET, net_str, &nv);
+       if (ret <= 0) {
+               SWRAP_LOG(SWRAP_LOG_ERROR,
+                         "INVALID IPv4 Network [%s]",
+                         net_str);
+               abort();
+       }
+
+       hv = ntohl(nv.s_addr);
+
+       switch (hv) {
+       case 0x7f000000:
+               /* 127.0.0.0 */
+               break;
+       case 0x0a353900:
+               /* 10.53.57.0 */
+               break;
+       default:
+               SWRAP_LOG(SWRAP_LOG_ERROR,
+                         "INVALID IPv4 Network [%s][0x%x] should be "
+                         "127.0.0.0 or 10.53.57.0",
+                         net_str, (unsigned)hv);
+               abort();
+       }
+
+       return hv;
+}
+
+/*
+ * This returns 127.255.255.255 or 10.255.255.255
+ */
+static in_addr_t swrap_ipv4_bcast(void)
+{
+       in_addr_t hv;
+
+       hv = swrap_ipv4_net();
+       hv |= IN_CLASSA_HOST;
+
+       return hv;
+}
+
+/*
+ * This returns 127.0.0.${iface} or 10.53.57.${iface}
+ */
+static in_addr_t swrap_ipv4_iface(unsigned int iface)
+{
+       in_addr_t hv;
+
+       if (iface == 0 || iface > MAX_WRAPPED_INTERFACES) {
+               SWRAP_LOG(SWRAP_LOG_ERROR,
+                         "swrap_ipv4_iface(%u) invalid!",
+                         iface);
+               abort();
+               return -1;
+       }
+
+       hv = swrap_ipv4_net();
+       hv |= iface;
+
+       return hv;
+}
+
 #ifdef HAVE_IPV6
 /*
  * FD00::5357:5FXX
@@ -1282,13 +1364,72 @@ static void swrap_set_next_free(struct socket_info *si, int next_free)
        sic->meta.next_free = next_free;
 }
 
+static int swrap_un_path(struct sockaddr_un *un,
+                        const char *swrap_dir,
+                        char type,
+                        unsigned int iface,
+                        unsigned int prt)
+{
+       int ret;
+
+       ret = snprintf(un->sun_path,
+                      sizeof(un->sun_path),
+                      "%s/"SOCKET_FORMAT,
+                      swrap_dir,
+                      type,
+                      iface,
+                      prt);
+       if ((size_t)ret >= sizeof(un->sun_path)) {
+               return ENAMETOOLONG;
+       }
+
+       return 0;
+}
+
+static int swrap_un_path_EINVAL(struct sockaddr_un *un,
+                               const char *swrap_dir)
+{
+       int ret;
+
+       ret = snprintf(un->sun_path,
+                      sizeof(un->sun_path),
+                      "%s/EINVAL",
+                      swrap_dir);
+
+       if ((size_t)ret >= sizeof(un->sun_path)) {
+               return ENAMETOOLONG;
+       }
+
+       return 0;
+}
+
+static bool swrap_dir_usable(const char *swrap_dir)
+{
+       struct sockaddr_un un;
+       int ret;
+
+       ret = swrap_un_path(&un, swrap_dir, SOCKET_TYPE_CHAR_TCP, 0, 0);
+       if (ret == 0) {
+               return true;
+       }
+
+       ret = swrap_un_path_EINVAL(&un, swrap_dir);
+       if (ret == 0) {
+               return true;
+       }
+
+       return false;
+}
+
 static char *socket_wrapper_dir(void)
 {
        char *swrap_dir = NULL;
        char *s = getenv("SOCKET_WRAPPER_DIR");
+       char *t;
+       bool ok;
 
        if (s == NULL) {
-               SWRAP_LOG(SWRAP_LOG_WARN, "SOCKET_WRAPPER_DIR not set\n");
+               SWRAP_LOG(SWRAP_LOG_WARN, "SOCKET_WRAPPER_DIR not set");
                return NULL;
        }
 
@@ -1297,9 +1438,43 @@ static char *socket_wrapper_dir(void)
                SWRAP_LOG(SWRAP_LOG_ERROR,
                          "Unable to resolve socket_wrapper dir path: %s",
                          strerror(errno));
-               return NULL;
+               abort();
+       }
+
+       ok = swrap_dir_usable(swrap_dir);
+       if (ok) {
+               goto done;
+       }
+
+       free(swrap_dir);
+
+       ok = swrap_dir_usable(s);
+       if (!ok) {
+               SWRAP_LOG(SWRAP_LOG_ERROR, "SOCKET_WRAPPER_DIR is too long");
+               abort();
+       }
+
+       t = getenv("SOCKET_WRAPPER_DIR_ALLOW_ORIG");
+       if (t == NULL) {
+               SWRAP_LOG(SWRAP_LOG_ERROR,
+                         "realpath(SOCKET_WRAPPER_DIR) too long and "
+                         "SOCKET_WRAPPER_DIR_ALLOW_ORIG not set");
+               abort();
+
+       }
+
+       swrap_dir = strdup(s);
+       if (swrap_dir == NULL) {
+               SWRAP_LOG(SWRAP_LOG_ERROR,
+                         "Unable to duplicate socket_wrapper dir path");
+               abort();
        }
 
+       SWRAP_LOG(SWRAP_LOG_WARN,
+                 "realpath(SOCKET_WRAPPER_DIR) too long, "
+                 "using original SOCKET_WRAPPER_DIR\n");
+
+done:
        SWRAP_LOG(SWRAP_LOG_TRACE, "socket_wrapper_dir: %s", swrap_dir);
        return swrap_dir;
 }
@@ -1435,6 +1610,8 @@ static void socket_wrapper_init_sockets(void)
        size_t i;
        int ret;
 
+       swrap_bind_symbol_all();
+
        swrap_mutex_lock(&sockets_mutex);
 
        if (sockets != NULL) {
@@ -1442,6 +1619,12 @@ static void socket_wrapper_init_sockets(void)
                return;
        }
 
+       /*
+        * Intialize the static cache early before
+        * any thread is able to start.
+        */
+       (void)swrap_ipv4_net();
+
        socket_wrapper_init_fds_idx();
 
        /* Needs to be called inside the sockets_mutex lock here. */
@@ -1536,6 +1719,9 @@ static unsigned int socket_wrapper_default_iface(void)
 
 static void set_socket_info_index(int fd, int idx)
 {
+       SWRAP_LOG(SWRAP_LOG_TRACE,
+                 "fd=%d idx=%d",
+                 fd, idx);
        socket_fds_idx[fd] = idx;
        /* This builtin issues a full memory barrier. */
        __sync_synchronize();
@@ -1543,6 +1729,9 @@ static void set_socket_info_index(int fd, int idx)
 
 static void reset_socket_info_index(int fd)
 {
+       SWRAP_LOG(SWRAP_LOG_TRACE,
+                 "fd=%d idx=%d",
+                 fd, -1);
        set_socket_info_index(fd, -1);
 }
 
@@ -1678,7 +1867,7 @@ static int convert_un_in(const struct sockaddr_un *un, struct sockaddr *in, sock
 
                memset(in2, 0, sizeof(*in2));
                in2->sin_family = AF_INET;
-               in2->sin_addr.s_addr = htonl((127<<24) | iface);
+               in2->sin_addr.s_addr = htonl(swrap_ipv4_iface(iface));
                in2->sin_port = htons(prt);
 
                *len = sizeof(*in2);
@@ -1731,6 +1920,8 @@ static int convert_in_un_remote(struct socket_info *si, const struct sockaddr *i
                char u_type = '\0';
                char b_type = '\0';
                char a_type = '\0';
+               const unsigned int sw_net_addr = swrap_ipv4_net();
+               const unsigned int sw_bcast_addr = swrap_ipv4_bcast();
 
                switch (si->type) {
                case SOCK_STREAM:
@@ -1742,7 +1933,7 @@ static int convert_in_un_remote(struct socket_info *si, const struct sockaddr *i
                        b_type = SOCKET_TYPE_CHAR_UDP;
                        break;
                default:
-                       SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
+                       SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!");
                        errno = ESOCKTNOSUPPORT;
                        return -1;
                }
@@ -1753,13 +1944,18 @@ static int convert_in_un_remote(struct socket_info *si, const struct sockaddr *i
                        is_bcast = 2;
                        type = a_type;
                        iface = socket_wrapper_default_iface();
-               } else if (b_type && addr == 0x7FFFFFFF) {
-                       /* 127.255.255.255 only udp */
+               } else if (b_type && addr == sw_bcast_addr) {
+                       /*
+                        * 127.255.255.255
+                        * or
+                        * 10.255.255.255
+                        * only udp
+                        */
                        is_bcast = 1;
                        type = b_type;
                        iface = socket_wrapper_default_iface();
-               } else if ((addr & 0xFFFFFF00) == 0x7F000000) {
-                       /* 127.0.0.X */
+               } else if ((addr & 0xFFFFFF00) == sw_net_addr) {
+                       /* 127.0.0.X or 10.53.57.X */
                        is_bcast = 0;
                        type = u_type;
                        iface = (addr & 0x000000FF);
@@ -1784,7 +1980,7 @@ static int convert_in_un_remote(struct socket_info *si, const struct sockaddr *i
                        type = SOCKET_TYPE_CHAR_UDP_V6;
                        break;
                default:
-                       SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
+                       SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!");
                        errno = ESOCKTNOSUPPORT;
                        return -1;
                }
@@ -1807,13 +2003,13 @@ static int convert_in_un_remote(struct socket_info *si, const struct sockaddr *i
        }
 #endif
        default:
-               SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family!\n");
+               SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family!");
                errno = ENETUNREACH;
                return -1;
        }
 
        if (prt == 0) {
-               SWRAP_LOG(SWRAP_LOG_WARN, "Port not set\n");
+               SWRAP_LOG(SWRAP_LOG_WARN, "Port not set");
                errno = EINVAL;
                return -1;
        }
@@ -1825,16 +2021,14 @@ static int convert_in_un_remote(struct socket_info *si, const struct sockaddr *i
        }
 
        if (is_bcast) {
-               snprintf(un->sun_path, sizeof(un->sun_path),
-                        "%s/EINVAL", swrap_dir);
+               swrap_un_path_EINVAL(un, swrap_dir);
                SWRAP_LOG(SWRAP_LOG_DEBUG, "un path [%s]", un->sun_path);
                SAFE_FREE(swrap_dir);
                /* the caller need to do more processing */
                return 0;
        }
 
-       snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT,
-                swrap_dir, type, iface, prt);
+       swrap_un_path(un, swrap_dir, type, iface, prt);
        SWRAP_LOG(SWRAP_LOG_DEBUG, "un path [%s]", un->sun_path);
 
        SAFE_FREE(swrap_dir);
@@ -1863,6 +2057,8 @@ static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *in
                char d_type = '\0';
                char b_type = '\0';
                char a_type = '\0';
+               const unsigned int sw_net_addr = swrap_ipv4_net();
+               const unsigned int sw_bcast_addr = swrap_ipv4_bcast();
 
                prt = ntohs(in->sin_port);
 
@@ -1878,7 +2074,7 @@ static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *in
                        b_type = SOCKET_TYPE_CHAR_UDP;
                        break;
                default:
-                       SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
+                       SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!");
                        errno = ESOCKTNOSUPPORT;
                        return -1;
                }
@@ -1893,12 +2089,12 @@ static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *in
                        is_bcast = 2;
                        type = a_type;
                        iface = socket_wrapper_default_iface();
-               } else if (b_type && addr == 0x7FFFFFFF) {
+               } else if (b_type && addr == sw_bcast_addr) {
                        /* 127.255.255.255 only udp */
                        is_bcast = 1;
                        type = b_type;
                        iface = socket_wrapper_default_iface();
-               } else if ((addr & 0xFFFFFF00) == 0x7F000000) {
+               } else if ((addr & 0xFFFFFF00) == sw_net_addr) {
                        /* 127.0.0.X */
                        is_bcast = 0;
                        type = u_type;
@@ -1916,8 +2112,7 @@ static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *in
                        ZERO_STRUCT(bind_in);
                        bind_in.sin_family = in->sin_family;
                        bind_in.sin_port = in->sin_port;
-                       bind_in.sin_addr.s_addr = htonl(0x7F000000 | iface);
-
+                       bind_in.sin_addr.s_addr = htonl(swrap_ipv4_iface(iface));
                        si->bindname.sa_socklen = blen;
                        memcpy(&si->bindname.sa.in, &bind_in, blen);
                }
@@ -1938,7 +2133,7 @@ static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *in
                        type = SOCKET_TYPE_CHAR_UDP_V6;
                        break;
                default:
-                       SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
+                       SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!");
                        errno = ESOCKTNOSUPPORT;
                        return -1;
                }
@@ -1979,7 +2174,7 @@ static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *in
        }
 #endif
        default:
-               SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family\n");
+               SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family");
                errno = EADDRNOTAVAIL;
                return -1;
        }
@@ -2001,8 +2196,7 @@ static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *in
        if (prt == 0) {
                /* handle auto-allocation of ephemeral ports */
                for (prt = 5001; prt < 10000; prt++) {
-                       snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT,
-                                swrap_dir, type, iface, prt);
+                       swrap_un_path(un, swrap_dir, type, iface, prt);
                        if (stat(un->sun_path, &st) == 0) continue;
 
                        set_port(si->family, prt, &si->myname);
@@ -2018,8 +2212,7 @@ static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *in
                }
        }
 
-       snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT,
-                swrap_dir, type, iface, prt);
+       swrap_un_path(un, swrap_dir, type, iface, prt);
        SWRAP_LOG(SWRAP_LOG_DEBUG, "un path [%s]", un->sun_path);
 
        SAFE_FREE(swrap_dir);
@@ -2221,7 +2414,7 @@ static int sockaddr_convert_to_un(struct socket_info *si,
                case SOCK_DGRAM:
                        break;
                default:
-                       SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
+                       SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!");
                        errno = ESOCKTNOSUPPORT;
                        return -1;
                }
@@ -2235,7 +2428,7 @@ static int sockaddr_convert_to_un(struct socket_info *si,
        }
 
        errno = EAFNOSUPPORT;
-       SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family\n");
+       SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family");
        return -1;
 }
 
@@ -2266,7 +2459,7 @@ static int sockaddr_convert_from_un(const struct socket_info *si,
                case SOCK_DGRAM:
                        break;
                default:
-                       SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!\n");
+                       SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown socket type!");
                        errno = ESOCKTNOSUPPORT;
                        return -1;
                }
@@ -2279,7 +2472,7 @@ static int sockaddr_convert_from_un(const struct socket_info *si,
                break;
        }
 
-       SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family\n");
+       SWRAP_LOG(SWRAP_LOG_ERROR, "Unknown address family");
        errno = EAFNOSUPPORT;
        return -1;
 }
@@ -2455,6 +2648,7 @@ static const char *swrap_pcap_init_file(void)
        if (strncmp(s, "./", 2) == 0) {
                s += 2;
        }
+       SWRAP_LOG(SWRAP_LOG_TRACE, "SOCKET_WRAPPER_PCAP_FILE: %s", s);
        return s;
 }
 
@@ -3140,7 +3334,15 @@ static int swrap_socket(int family, int type, int protocol)
        case AF_PACKET:
 #endif /* AF_PACKET */
        case AF_UNIX:
-               return libc_socket(family, type, protocol);
+               fd = libc_socket(family, type, protocol);
+               if (fd != -1) {
+                       /* Check if we have a stale fd and remove it */
+                       swrap_remove_stale(fd);
+                       SWRAP_LOG(SWRAP_LOG_TRACE,
+                                 "Unix socket fd=%d",
+                                 fd);
+               }
+               return fd;
        default:
                errno = EAFNOSUPPORT;
                return -1;
@@ -3384,6 +3586,9 @@ static int swrap_accept(int s,
 
        fd = ret;
 
+       /* Check if we have a stale fd and remove it */
+       swrap_remove_stale(fd);
+
        SWRAP_LOCK_SI(parent_si);
 
        ret = sockaddr_convert_from_un(parent_si,
@@ -3536,8 +3741,8 @@ static int swrap_auto_bind(int fd, struct socket_info *si, int family)
 
                memset(&in, 0, sizeof(in));
                in.sin_family = AF_INET;
-               in.sin_addr.s_addr = htonl(127<<24 |
-                                          socket_wrapper_default_iface());
+               in.sin_addr.s_addr = htonl(swrap_ipv4_iface(
+                                          socket_wrapper_default_iface()));
 
                si->myname = (struct swrap_address) {
                        .sa_socklen = sizeof(in),
@@ -3599,9 +3804,11 @@ static int swrap_auto_bind(int fd, struct socket_info *si, int family)
 
        for (i = 0; i < SOCKET_MAX_SOCKETS; i++) {
                port = autobind_start + i;
-               snprintf(un_addr.sa.un.sun_path, sizeof(un_addr.sa.un.sun_path),
-                        "%s/"SOCKET_FORMAT, swrap_dir, type,
-                        socket_wrapper_default_iface(), port);
+               swrap_un_path(&un_addr.sa.un,
+                             swrap_dir,
+                             type,
+                             socket_wrapper_default_iface(),
+                             port);
                if (stat(un_addr.sa.un.sun_path, &st) == 0) continue;
 
                ret = libc_bind(fd, &un_addr.sa.s, un_addr.sa_socklen);
@@ -3666,6 +3873,9 @@ static int swrap_connect(int s, const struct sockaddr *serv_addr,
        }
 
        if (si->family != serv_addr->sa_family) {
+               SWRAP_LOG(SWRAP_LOG_ERROR,
+                         "called for fd=%d (family=%d) called with invalid family=%d",
+                         s, si->family, serv_addr->sa_family);
                errno = EINVAL;
                ret = -1;
                goto done;
@@ -3974,6 +4184,9 @@ static int swrap_listen(int s, int backlog)
        }
 
        ret = libc_listen(s, backlog);
+       if (ret == 0) {
+               si->listening = 1;
+       }
 
 out:
        SWRAP_UNLOCK_SI(si);
@@ -4323,6 +4536,56 @@ static int swrap_getsockopt(int s, int level, int optname,
                        ret = 0;
                        goto done;
 #endif /* TCP_NODELAY */
+#ifdef TCP_INFO
+               case TCP_INFO: {
+                       struct tcp_info info;
+                       socklen_t ilen = sizeof(info);
+
+#ifdef HAVE_NETINET_TCP_FSM_H
+/* This is FreeBSD */
+# define __TCP_LISTEN TCPS_LISTEN
+# define __TCP_ESTABLISHED TCPS_ESTABLISHED
+# define __TCP_CLOSE TCPS_CLOSED
+#else
+/* This is Linux */
+# define __TCP_LISTEN TCP_LISTEN
+# define __TCP_ESTABLISHED TCP_ESTABLISHED
+# define __TCP_CLOSE TCP_CLOSE
+#endif
+
+                       ZERO_STRUCT(info);
+                       if (si->listening) {
+                               info.tcpi_state = __TCP_LISTEN;
+                       } else if (si->connected) {
+                               /*
+                                * For now we just fake a few values
+                                * supported both by FreeBSD and Linux
+                                */
+                               info.tcpi_state = __TCP_ESTABLISHED;
+                               info.tcpi_rto = 200000;  /* 200 msec */
+                               info.tcpi_rtt = 5000;    /* 5 msec */
+                               info.tcpi_rttvar = 5000; /* 5 msec */
+                       } else {
+                               info.tcpi_state = __TCP_CLOSE;
+                               info.tcpi_rto = 1000000;  /* 1 sec */
+                               info.tcpi_rtt = 0;
+                               info.tcpi_rttvar = 250000; /* 250 msec */
+                       }
+
+                       if (optval == NULL || optlen == NULL ||
+                           *optlen < (socklen_t)ilen) {
+                               errno = EINVAL;
+                               ret = -1;
+                               goto done;
+                       }
+
+                       *optlen = ilen;
+                       memcpy(optval, &info, ilen);
+
+                       ret = 0;
+                       goto done;
+               }
+#endif /* TCP_INFO */
                default:
                        break;
                }
@@ -4455,7 +4718,7 @@ static int swrap_vioctl(int s, unsigned long int r, va_list va)
 {
        struct socket_info *si = find_socket_info(s);
        va_list ap;
-       int value;
+       int *value_ptr = NULL;
        int rc;
 
        if (!si) {
@@ -4470,14 +4733,34 @@ static int swrap_vioctl(int s, unsigned long int r, va_list va)
 
        switch (r) {
        case FIONREAD:
-               value = *((int *)va_arg(ap, int *));
+               if (rc == 0) {
+                       value_ptr = ((int *)va_arg(ap, int *));
+               }
 
                if (rc == -1 && errno != EAGAIN && errno != ENOBUFS) {
                        swrap_pcap_dump_packet(si, NULL, SWRAP_PENDING_RST, NULL, 0);
-               } else if (value == 0) { /* END OF FILE */
+               } else if (value_ptr != NULL && *value_ptr == 0) { /* END OF FILE */
                        swrap_pcap_dump_packet(si, NULL, SWRAP_PENDING_RST, NULL, 0);
                }
                break;
+#ifdef FIONWRITE
+       case FIONWRITE:
+               /* this is FreeBSD */
+               FALL_THROUGH; /* to TIOCOUTQ */
+#endif /* FIONWRITE */
+       case TIOCOUTQ: /* same as SIOCOUTQ on Linux */
+               /*
+                * This may return more bytes then the application
+                * sent into the socket, for tcp it should
+                * return the number of unacked bytes.
+                *
+                * On AF_UNIX, all bytes are immediately acked!
+                */
+               if (rc == 0) {
+                       value_ptr = ((int *)va_arg(ap, int *));
+                       *value_ptr = 0;
+               }
+               break;
        }
 
        va_end(ap);
@@ -5406,9 +5689,11 @@ static ssize_t swrap_sendto(int s, const void *buf, size_t len, int flags,
                }
 
                for(iface=0; iface <= MAX_WRAPPED_INTERFACES; iface++) {
-                       snprintf(un_addr.sa.un.sun_path,
-                                sizeof(un_addr.sa.un.sun_path),
-                                "%s/"SOCKET_FORMAT, swrap_dir, type, iface, prt);
+                       swrap_un_path(&un_addr.sa.un,
+                                     swrap_dir,
+                                     type,
+                                     iface,
+                                     prt);
                        if (stat(un_addr.sa.un.sun_path, &st) != 0) continue;
 
                        /* ignore the any errors in broadcast sends */
@@ -5910,12 +6195,12 @@ static ssize_t swrap_sendmsg(int s, const struct msghdr *omsg, int flags)
 
                swrap_dir = socket_wrapper_dir();
                if (swrap_dir == NULL) {
+                       free(buf);
                        return -1;
                }
 
                for(iface=0; iface <= MAX_WRAPPED_INTERFACES; iface++) {
-                       snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), "%s/"SOCKET_FORMAT,
-                                swrap_dir, type, iface, prt);
+                       swrap_un_path(&un_addr, swrap_dir, type, iface, prt);
                        if (stat(un_addr.sun_path, &st) != 0) continue;
 
                        msg.msg_name = &un_addr;           /* optional address */
@@ -6075,6 +6360,7 @@ static int swrap_close(int fd)
                return libc_close(fd);
        }
 
+       SWRAP_LOG(SWRAP_LOG_TRACE, "Close wrapper for fd=%d", fd);
        reset_socket_info_index(fd);
 
        si = swrap_get_socket_info(si_index);
@@ -6409,3 +6695,54 @@ void swrap_destructor(void)
                dlclose(swrap.libc.socket_handle);
        }
 }
+
+#if defined(HAVE__SOCKET) && defined(HAVE__CLOSE)
+/*
+ * On FreeBSD 12 (and maybe other platforms)
+ * system libraries like libresolv prefix there
+ * syscalls with '_' in order to always use
+ * the symbols from libc.
+ *
+ * In the interaction with resolv_wrapper,
+ * we need to inject socket wrapper into libresolv,
+ * which means we need to private all socket
+ * related syscalls also with the '_' prefix.
+ *
+ * This is tested in Samba's 'make test',
+ * there we noticed that providing '_read'
+ * and '_open' would cause errors, which
+ * means we skip '_read', '_write' and
+ * all non socket related calls without
+ * further analyzing the problem.
+ */
+#define SWRAP_SYMBOL_ALIAS(__sym, __aliassym) \
+       extern typeof(__sym) __aliassym __attribute__ ((alias(#__sym)))
+
+#ifdef HAVE_ACCEPT4
+SWRAP_SYMBOL_ALIAS(accept4, _accept4);
+#endif
+SWRAP_SYMBOL_ALIAS(accept, _accept);
+SWRAP_SYMBOL_ALIAS(bind, _bind);
+SWRAP_SYMBOL_ALIAS(close, _close);
+SWRAP_SYMBOL_ALIAS(connect, _connect);
+SWRAP_SYMBOL_ALIAS(dup, _dup);
+SWRAP_SYMBOL_ALIAS(dup2, _dup2);
+SWRAP_SYMBOL_ALIAS(fcntl, _fcntl);
+SWRAP_SYMBOL_ALIAS(getpeername, _getpeername);
+SWRAP_SYMBOL_ALIAS(getsockname, _getsockname);
+SWRAP_SYMBOL_ALIAS(getsockopt, _getsockopt);
+SWRAP_SYMBOL_ALIAS(ioctl, _ioctl);
+SWRAP_SYMBOL_ALIAS(listen, _listen);
+SWRAP_SYMBOL_ALIAS(readv, _readv);
+SWRAP_SYMBOL_ALIAS(recv, _recv);
+SWRAP_SYMBOL_ALIAS(recvfrom, _recvfrom);
+SWRAP_SYMBOL_ALIAS(recvmsg, _recvmsg);
+SWRAP_SYMBOL_ALIAS(send, _send);
+SWRAP_SYMBOL_ALIAS(sendmsg, _sendmsg);
+SWRAP_SYMBOL_ALIAS(sendto, _sendto);
+SWRAP_SYMBOL_ALIAS(setsockopt, _setsockopt);
+SWRAP_SYMBOL_ALIAS(socket, _socket);
+SWRAP_SYMBOL_ALIAS(socketpair, _socketpair);
+SWRAP_SYMBOL_ALIAS(writev, _writev);
+
+#endif /* SOCKET_WRAPPER_EXPORT_UNDERSCORE_SYMBOLS */