src/socket_wrapper.c: Improve checks and debug output of socket_wrapper_dir()
[socket_wrapper.git] / src / socket_wrapper.c
index ca0a833653b561da8a70bcef3bb7ea3b35e53677..89454ca34d6e90fcc175e6911a20c0966f227934 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>
@@ -175,12 +178,57 @@ enum swrap_dbglvl_e {
 # endif
 #endif
 
+#define socket_wrapper_init_mutex(m) \
+       _socket_wrapper_init_mutex(m, #m)
+
 /* Add new global locks here please */
-# define SWRAP_LOCK_ALL \
-       swrap_mutex_lock(&libc_symbol_binding_mutex); \
+# define SWRAP_REINIT_ALL do { \
+       size_t __i; \
+       int ret; \
+       ret = socket_wrapper_init_mutex(&sockets_mutex); \
+       if (ret != 0) exit(-1); \
+       ret = socket_wrapper_init_mutex(&socket_reset_mutex); \
+       if (ret != 0) exit(-1); \
+       ret = socket_wrapper_init_mutex(&first_free_mutex); \
+       if (ret != 0) exit(-1); \
+       for (__i = 0; (sockets != NULL) && __i < socket_info_max; __i++) { \
+               ret = socket_wrapper_init_mutex(&sockets[__i].meta.mutex); \
+               if (ret != 0) exit(-1); \
+       } \
+       ret = socket_wrapper_init_mutex(&autobind_start_mutex); \
+       if (ret != 0) exit(-1); \
+       ret = socket_wrapper_init_mutex(&pcap_dump_mutex); \
+       if (ret != 0) exit(-1); \
+       ret = socket_wrapper_init_mutex(&mtu_update_mutex); \
+       if (ret != 0) exit(-1); \
+} while(0)
 
-# define SWRAP_UNLOCK_ALL \
-       swrap_mutex_unlock(&libc_symbol_binding_mutex); \
+# define SWRAP_LOCK_ALL do { \
+       size_t __i; \
+       swrap_mutex_lock(&sockets_mutex); \
+       swrap_mutex_lock(&socket_reset_mutex); \
+       swrap_mutex_lock(&first_free_mutex); \
+       for (__i = 0; (sockets != NULL) && __i < socket_info_max; __i++) { \
+               swrap_mutex_lock(&sockets[__i].meta.mutex); \
+       } \
+       swrap_mutex_lock(&autobind_start_mutex); \
+       swrap_mutex_lock(&pcap_dump_mutex); \
+       swrap_mutex_lock(&mtu_update_mutex); \
+} while(0)
+
+# define SWRAP_UNLOCK_ALL do { \
+       size_t __s; \
+       swrap_mutex_unlock(&mtu_update_mutex); \
+       swrap_mutex_unlock(&pcap_dump_mutex); \
+       swrap_mutex_unlock(&autobind_start_mutex); \
+       for (__s = 0; (sockets != NULL) && __s < socket_info_max; __s++) { \
+               size_t __i = (socket_info_max - 1) - __s; \
+               swrap_mutex_unlock(&sockets[__i].meta.mutex); \
+       } \
+       swrap_mutex_unlock(&first_free_mutex); \
+       swrap_mutex_unlock(&socket_reset_mutex); \
+       swrap_mutex_unlock(&sockets_mutex); \
+} while(0)
 
 #define SOCKET_INFO_CONTAINER(si) \
        (struct socket_info_container *)(si)
@@ -250,7 +298,7 @@ struct swrap_address {
        } sa;
 };
 
-int first_free;
+static int first_free;
 
 struct socket_info
 {
@@ -264,6 +312,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,32 +354,36 @@ 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;
+static pthread_mutex_t autobind_start_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /* Mutex to guard the initialization of array of socket_info structures */
-static pthread_mutex_t sockets_mutex;
+static pthread_mutex_t sockets_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /* Mutex to guard the socket reset in swrap_close() and swrap_remove_stale() */
-static pthread_mutex_t socket_reset_mutex;
+static pthread_mutex_t socket_reset_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /* Mutex to synchronize access to first free index in socket_info array */
-static pthread_mutex_t first_free_mutex;
+static pthread_mutex_t first_free_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /* Mutex to synchronize access to packet capture dump file */
-static pthread_mutex_t pcap_dump_mutex;
+static pthread_mutex_t pcap_dump_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /* Mutex for synchronizing mtu value fetch*/
-static pthread_mutex_t mtu_update_mutex;
+static pthread_mutex_t mtu_update_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /* Function prototypes */
 
 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
@@ -555,13 +608,12 @@ struct swrap {
 static struct swrap swrap;
 
 /* prototypes */
-static const char *socket_wrapper_dir(void);
+static char *socket_wrapper_dir(void);
 
 #define LIBC_NAME "libc.so"
 
 enum swrap_lib {
     SWRAP_LIBC,
-    SWRAP_LIBNSL,
     SWRAP_LIBSOCKET,
 };
 
@@ -570,8 +622,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";
        }
@@ -587,20 +637,28 @@ static void *swrap_load_lib_handle(enum swrap_lib lib)
        int i;
 
 #ifdef RTLD_DEEPBIND
-       const char *env = getenv("LD_PRELOAD");
+       const char *env_preload = getenv("LD_PRELOAD");
+       const char *env_deepbind = getenv("SOCKET_WRAPPER_DISABLE_DEEPBIND");
+       bool enable_deepbind = true;
 
        /* Don't do a deepbind if we run with libasan */
-       if (env != NULL && strlen(env) < 1024) {
-               const char *p = strstr(env, "libasan.so");
-               if (p == NULL) {
-                       flags |= RTLD_DEEPBIND;
+       if (env_preload != NULL && strlen(env_preload) < 1024) {
+               const char *p = strstr(env_preload, "libasan.so");
+               if (p != NULL) {
+                       enable_deepbind = false;
                }
        }
+
+       if (env_deepbind != NULL && strlen(env_deepbind) >= 1) {
+               enable_deepbind = false;
+       }
+
+       if (enable_deepbind) {
+               flags |= RTLD_DEEPBIND;
+       }
 #endif
 
        switch (lib) {
-       case SWRAP_LIBNSL:
-               FALL_THROUGH;
        case SWRAP_LIBSOCKET:
 #ifdef HAVE_LIBSOCKET
                handle = swrap.libc.socket_handle;
@@ -619,7 +677,6 @@ static void *swrap_load_lib_handle(enum swrap_lib lib)
                }
                break;
 #endif
-               FALL_THROUGH;
        case SWRAP_LIBC:
                handle = swrap.libc.handle;
 #ifdef LIBC_SO
@@ -650,7 +707,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
@@ -669,7 +726,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);
@@ -683,25 +740,27 @@ static void *_swrap_bind_symbol(enum swrap_lib lib, const char *fn_name)
        return func;
 }
 
-static void swrap_mutex_lock(pthread_mutex_t *mutex)
+#define swrap_mutex_lock(m) _swrap_mutex_lock(m, #m, __func__, __LINE__)
+static void _swrap_mutex_lock(pthread_mutex_t *mutex, const char *name, const char *caller, unsigned line)
 {
        int ret;
 
        ret = pthread_mutex_lock(mutex);
        if (ret != 0) {
-               SWRAP_LOG(SWRAP_LOG_ERROR, "Couldn't lock pthread mutex - %s",
-                         strerror(ret));
+               SWRAP_LOG(SWRAP_LOG_ERROR, "PID(%d):PPID(%d): %s(%u): Couldn't lock pthread mutex(%s) - %s",
+                         getpid(), getppid(), caller, line, name, strerror(ret));
        }
 }
 
-static void swrap_mutex_unlock(pthread_mutex_t *mutex)
+#define swrap_mutex_unlock(m) _swrap_mutex_unlock(m, #m, __func__, __LINE__)
+static void _swrap_mutex_unlock(pthread_mutex_t *mutex, const char *name, const char *caller, unsigned line)
 {
        int ret;
 
        ret = pthread_mutex_unlock(mutex);
        if (ret != 0) {
-               SWRAP_LOG(SWRAP_LOG_ERROR, "Couldn't unlock pthread mutex - %s",
-                         strerror(ret));
+               SWRAP_LOG(SWRAP_LOG_ERROR, "PID(%d):PPID(%d): %s(%u): Couldn't unlock pthread mutex(%s) - %s",
+                         getpid(), getppid(), caller, line, name, strerror(ret));
        }
 }
 
@@ -711,35 +770,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
@@ -758,7 +800,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);
 }
@@ -767,7 +809,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);
 }
@@ -777,14 +819,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);
 }
@@ -793,21 +835,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);
 }
@@ -815,7 +857,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);
 }
@@ -827,7 +869,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 *);
 
@@ -840,7 +882,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);
 }
@@ -849,7 +891,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);
 }
@@ -860,7 +902,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,
@@ -875,7 +917,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 *);
 
@@ -886,14 +928,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);
 }
@@ -901,7 +943,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);
 }
@@ -912,7 +954,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);
@@ -940,7 +982,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);
@@ -956,7 +998,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);
@@ -985,28 +1027,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);
 }
@@ -1018,7 +1060,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,
@@ -1030,21 +1072,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);
 }
@@ -1056,7 +1098,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,
@@ -1072,7 +1114,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,
@@ -1084,7 +1126,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);
 }
@@ -1092,14 +1134,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);
 }
@@ -1107,7 +1149,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);
 }
@@ -1115,20 +1157,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);
@@ -1180,10 +1222,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
@@ -1274,19 +1411,120 @@ static void swrap_set_next_free(struct socket_info *si, int next_free)
        sic->meta.next_free = next_free;
 }
 
-static const char *socket_wrapper_dir(void)
+static int swrap_un_path(struct sockaddr_un *un,
+                        const char *swrap_dir,
+                        char type,
+                        unsigned int iface,
+                        unsigned int prt)
 {
-       const char *s = getenv("SOCKET_WRAPPER_DIR");
-       if (s == NULL) {
+       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 || s[0] == '\0') {
+               SWRAP_LOG(SWRAP_LOG_WARN, "SOCKET_WRAPPER_DIR not set");
                return NULL;
        }
-       /* TODO use realpath(3) here, when we add support for threads */
-       if (strncmp(s, "./", 2) == 0) {
-               s += 2;
+
+       swrap_dir = realpath(s, NULL);
+       if (swrap_dir == NULL) {
+               SWRAP_LOG(SWRAP_LOG_ERROR,
+                         "Unable to resolve socket_wrapper dir path: %s - %s",
+                         s,
+                         strerror(errno));
+               abort();
        }
 
-       SWRAP_LOG(SWRAP_LOG_TRACE, "socket_wrapper_dir: %s", s);
-       return s;
+       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;
 }
 
 static unsigned int socket_wrapper_mtu(void)
@@ -1324,26 +1562,31 @@ done:
        return max_mtu;
 }
 
-static int socket_wrapper_init_mutex(pthread_mutex_t *m)
+static int _socket_wrapper_init_mutex(pthread_mutex_t *m, const char *name)
 {
        pthread_mutexattr_t ma;
-       int ret;
-
-       ret = pthread_mutexattr_init(&ma);
-       if (ret != 0) {
-               return ret;
-       }
-
-       ret = pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_ERRORCHECK);
-       if (ret != 0) {
-               goto done;
-       }
-
-       ret = pthread_mutex_init(m, &ma);
+       bool need_destroy = false;
+       int ret = 0;
+
+#define __CHECK(cmd) do { \
+       ret = cmd; \
+       if (ret != 0) { \
+               SWRAP_LOG(SWRAP_LOG_ERROR, \
+                         "%s: %s - failed %d", \
+                         name, #cmd, ret); \
+               goto done; \
+       } \
+} while(0)
 
+       *m = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
+       __CHECK(pthread_mutexattr_init(&ma));
+       need_destroy = true;
+       __CHECK(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_ERRORCHECK));
+       __CHECK(pthread_mutex_init(m, &ma));
 done:
-       pthread_mutexattr_destroy(&ma);
-
+       if (need_destroy) {
+               pthread_mutexattr_destroy(&ma);
+       }
        return ret;
 }
 
@@ -1418,7 +1661,9 @@ static void socket_wrapper_init_sockets(void)
 {
        size_t max_sockets;
        size_t i;
-       int ret;
+       int ret = 0;
+
+       swrap_bind_symbol_all();
 
        swrap_mutex_lock(&sockets_mutex);
 
@@ -1427,6 +1672,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. */
@@ -1449,10 +1700,14 @@ static void socket_wrapper_init_sockets(void)
 
        for (i = 0; i < max_sockets; i++) {
                swrap_set_next_free(&sockets[i].info, i+1);
+               sockets[i].meta.mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
+       }
+
+       for (i = 0; i < max_sockets; i++) {
                ret = socket_wrapper_init_mutex(&sockets[i].meta.mutex);
                if (ret != 0) {
                        SWRAP_LOG(SWRAP_LOG_ERROR,
-                                 "Failed to initialize pthread mutex");
+                                 "Failed to initialize pthread mutex i=%zu", i);
                        goto done;
                }
        }
@@ -1460,27 +1715,6 @@ static void socket_wrapper_init_sockets(void)
        /* mark the end of the free list */
        swrap_set_next_free(&sockets[max_sockets-1].info, -1);
 
-       ret = socket_wrapper_init_mutex(&autobind_start_mutex);
-       if (ret != 0) {
-               SWRAP_LOG(SWRAP_LOG_ERROR,
-                         "Failed to initialize pthread mutex");
-               goto done;
-       }
-
-       ret = socket_wrapper_init_mutex(&pcap_dump_mutex);
-       if (ret != 0) {
-               SWRAP_LOG(SWRAP_LOG_ERROR,
-                         "Failed to initialize pthread mutex");
-               goto done;
-       }
-
-       ret = socket_wrapper_init_mutex(&mtu_update_mutex);
-       if (ret != 0) {
-               SWRAP_LOG(SWRAP_LOG_ERROR,
-                         "Failed to initialize pthread mutex");
-               goto done;
-       }
-
 done:
        swrap_mutex_unlock(&first_free_mutex);
        swrap_mutex_unlock(&sockets_mutex);
@@ -1491,12 +1725,14 @@ done:
 
 bool socket_wrapper_enabled(void)
 {
-       const char *s = socket_wrapper_dir();
+       char *s = socket_wrapper_dir();
 
        if (s == NULL) {
                return false;
        }
 
+       SAFE_FREE(s);
+
        socket_wrapper_init_sockets();
 
        return true;
@@ -1519,6 +1755,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();
@@ -1526,6 +1765,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);
 }
 
@@ -1661,7 +1903,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);
@@ -1702,6 +1944,7 @@ static int convert_in_un_remote(struct socket_info *si, const struct sockaddr *i
        unsigned int prt;
        unsigned int iface;
        int is_bcast = 0;
+       char *swrap_dir = NULL;
 
        if (bcast) *bcast = 0;
 
@@ -1713,6 +1956,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:
@@ -1724,7 +1969,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;
                }
@@ -1735,13 +1980,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);
@@ -1766,7 +2016,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;
                }
@@ -1789,29 +2039,36 @@ 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;
+       }
+
+       swrap_dir = socket_wrapper_dir();
+       if (swrap_dir == NULL) {
                errno = EINVAL;
                return -1;
        }
 
        if (is_bcast) {
-               snprintf(un->sun_path, sizeof(un->sun_path), "%s/EINVAL",
-                        socket_wrapper_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,
-                socket_wrapper_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);
+
        return 0;
 }
 
@@ -1823,6 +2080,7 @@ static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *in
        unsigned int iface;
        struct stat st;
        int is_bcast = 0;
+       char *swrap_dir = NULL;
 
        if (bcast) *bcast = 0;
 
@@ -1835,6 +2093,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);
 
@@ -1850,7 +2110,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;
                }
@@ -1865,12 +2125,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;
@@ -1888,8 +2148,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);
                }
@@ -1910,7 +2169,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;
                }
@@ -1951,7 +2210,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;
        }
@@ -1964,11 +2223,16 @@ static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *in
                return -1;
        }
 
+       swrap_dir = socket_wrapper_dir();
+       if (swrap_dir == NULL) {
+               errno = EINVAL;
+               return -1;
+       }
+
        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,
-                                socket_wrapper_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);
@@ -1976,15 +2240,19 @@ static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *in
 
                        break;
                }
+
                if (prt == 10000) {
                        errno = ENFILE;
+                       SAFE_FREE(swrap_dir);
                        return -1;
                }
        }
 
-       snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT,
-                socket_wrapper_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);
+
        return 0;
 }
 
@@ -2182,7 +2450,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;
                }
@@ -2196,7 +2464,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;
 }
 
@@ -2227,7 +2495,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;
                }
@@ -2240,7 +2508,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;
 }
@@ -2416,6 +2684,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;
 }
 
@@ -2431,14 +2700,20 @@ static uint8_t *swrap_pcap_packet_init(struct timeval *tval,
                                       int unreachable,
                                       size_t *_packet_len)
 {
-       uint8_t *base;
-       uint8_t *buf;
-       struct swrap_packet_frame *frame;
-       union swrap_packet_ip *ip;
+       uint8_t *base = NULL;
+       uint8_t *buf = NULL;
+       union {
+               uint8_t *ptr;
+               struct swrap_packet_frame *frame;
+       } f;
+       union {
+               uint8_t *ptr;
+               union swrap_packet_ip *ip;
+       } i;
        union swrap_packet_payload *pay;
        size_t packet_len;
        size_t alloc_len;
-       size_t nonwire_len = sizeof(*frame);
+       size_t nonwire_len = sizeof(struct swrap_packet_frame);
        size_t wire_hdr_len = 0;
        size_t wire_len = 0;
        size_t ip_hdr_len = 0;
@@ -2460,7 +2735,7 @@ static uint8_t *swrap_pcap_packet_init(struct timeval *tval,
                dest_in = (const struct sockaddr_in *)(const void *)dest;
                src_port = src_in->sin_port;
                dest_port = dest_in->sin_port;
-               ip_hdr_len = sizeof(ip->v4);
+               ip_hdr_len = sizeof(i.ip->v4);
                break;
 #ifdef HAVE_IPV6
        case AF_INET6:
@@ -2468,7 +2743,7 @@ static uint8_t *swrap_pcap_packet_init(struct timeval *tval,
                dest_in6 = (const struct sockaddr_in6 *)(const void *)dest;
                src_port = src_in6->sin6_port;
                dest_port = dest_in6->sin6_port;
-               ip_hdr_len = sizeof(ip->v6);
+               ip_hdr_len = sizeof(i.ip->v6);
                break;
 #endif
        default:
@@ -2509,7 +2784,6 @@ static uint8_t *swrap_pcap_packet_init(struct timeval *tval,
                if (wire_len > 64 ) {
                        icmp_truncate_len = wire_len - 64;
                }
-               wire_hdr_len += icmp_hdr_len;
                wire_len += icmp_hdr_len;
        }
 
@@ -2525,39 +2799,50 @@ static uint8_t *swrap_pcap_packet_init(struct timeval *tval,
        }
 
        buf = base;
+       f.ptr = buf;
+
+       f.frame->seconds                = tval->tv_sec;
+       f.frame->micro_seconds  = tval->tv_usec;
+       f.frame->recorded_length        = wire_len - icmp_truncate_len;
+       f.frame->full_length    = wire_len - icmp_truncate_len;
 
-       frame = (struct swrap_packet_frame *)(void *)buf;
-       frame->seconds          = tval->tv_sec;
-       frame->micro_seconds    = tval->tv_usec;
-       frame->recorded_length  = wire_len - icmp_truncate_len;
-       frame->full_length      = wire_len - icmp_truncate_len;
        buf += SWRAP_PACKET_FRAME_SIZE;
 
-       ip = (union swrap_packet_ip *)(void *)buf;
+       i.ptr = buf;
        switch (src->sa_family) {
        case AF_INET:
-               ip->v4.ver_hdrlen       = 0x45; /* version 4 and 5 * 32 bit words */
-               ip->v4.tos              = 0x00;
-               ip->v4.packet_length    = htons(wire_len - icmp_truncate_len);
-               ip->v4.identification   = htons(0xFFFF);
-               ip->v4.flags            = 0x40; /* BIT 1 set - means don't fragment */
-               ip->v4.fragment         = htons(0x0000);
-               ip->v4.ttl              = 0xFF;
-               ip->v4.protocol         = protocol;
-               ip->v4.hdr_checksum     = htons(0x0000);
-               ip->v4.src_addr         = src_in->sin_addr.s_addr;
-               ip->v4.dest_addr        = dest_in->sin_addr.s_addr;
+               if (src_in == NULL || dest_in == NULL) {
+                       SAFE_FREE(base);
+                       return NULL;
+               }
+
+               i.ip->v4.ver_hdrlen     = 0x45; /* version 4 and 5 * 32 bit words */
+               i.ip->v4.tos            = 0x00;
+               i.ip->v4.packet_length  = htons(wire_len - icmp_truncate_len);
+               i.ip->v4.identification = htons(0xFFFF);
+               i.ip->v4.flags          = 0x40; /* BIT 1 set - means don't fragment */
+               i.ip->v4.fragment       = htons(0x0000);
+               i.ip->v4.ttl            = 0xFF;
+               i.ip->v4.protocol       = protocol;
+               i.ip->v4.hdr_checksum   = htons(0x0000);
+               i.ip->v4.src_addr       = src_in->sin_addr.s_addr;
+               i.ip->v4.dest_addr      = dest_in->sin_addr.s_addr;
                buf += SWRAP_PACKET_IP_V4_SIZE;
                break;
 #ifdef HAVE_IPV6
        case AF_INET6:
-               ip->v6.ver_prio         = 0x60; /* version 4 and 5 * 32 bit words */
-               ip->v6.flow_label_high  = 0x00;
-               ip->v6.flow_label_low   = 0x0000;
-               ip->v6.payload_length   = htons(wire_len - icmp_truncate_len); /* TODO */
-               ip->v6.next_header      = protocol;
-               memcpy(ip->v6.src_addr, src_in6->sin6_addr.s6_addr, 16);
-               memcpy(ip->v6.dest_addr, dest_in6->sin6_addr.s6_addr, 16);
+               if (src_in6 == NULL || dest_in6 == NULL) {
+                       SAFE_FREE(base);
+                       return NULL;
+               }
+
+               i.ip->v6.ver_prio               = 0x60; /* version 4 and 5 * 32 bit words */
+               i.ip->v6.flow_label_high        = 0x00;
+               i.ip->v6.flow_label_low = 0x0000;
+               i.ip->v6.payload_length = htons(wire_len - icmp_truncate_len); /* TODO */
+               i.ip->v6.next_header    = protocol;
+               memcpy(i.ip->v6.src_addr, src_in6->sin6_addr.s6_addr, 16);
+               memcpy(i.ip->v6.dest_addr, dest_in6->sin6_addr.s6_addr, 16);
                buf += SWRAP_PACKET_IP_V6_SIZE;
                break;
 #endif
@@ -2571,21 +2856,23 @@ static uint8_t *swrap_pcap_packet_init(struct timeval *tval,
                        pay->icmp4.code         = 0x01; /* host unreachable */
                        pay->icmp4.checksum     = htons(0x0000);
                        pay->icmp4.unused       = htonl(0x00000000);
+
                        buf += SWRAP_PACKET_PAYLOAD_ICMP4_SIZE;
 
                        /* set the ip header in the ICMP payload */
-                       ip = (union swrap_packet_ip *)(void *)buf;
-                       ip->v4.ver_hdrlen       = 0x45; /* version 4 and 5 * 32 bit words */
-                       ip->v4.tos              = 0x00;
-                       ip->v4.packet_length    = htons(wire_len - icmp_hdr_len);
-                       ip->v4.identification   = htons(0xFFFF);
-                       ip->v4.flags            = 0x40; /* BIT 1 set - means don't fragment */
-                       ip->v4.fragment         = htons(0x0000);
-                       ip->v4.ttl              = 0xFF;
-                       ip->v4.protocol         = icmp_protocol;
-                       ip->v4.hdr_checksum     = htons(0x0000);
-                       ip->v4.src_addr         = dest_in->sin_addr.s_addr;
-                       ip->v4.dest_addr        = src_in->sin_addr.s_addr;
+                       i.ptr = buf;
+                       i.ip->v4.ver_hdrlen     = 0x45; /* version 4 and 5 * 32 bit words */
+                       i.ip->v4.tos            = 0x00;
+                       i.ip->v4.packet_length  = htons(wire_len - icmp_hdr_len);
+                       i.ip->v4.identification = htons(0xFFFF);
+                       i.ip->v4.flags          = 0x40; /* BIT 1 set - means don't fragment */
+                       i.ip->v4.fragment       = htons(0x0000);
+                       i.ip->v4.ttl            = 0xFF;
+                       i.ip->v4.protocol       = icmp_protocol;
+                       i.ip->v4.hdr_checksum   = htons(0x0000);
+                       i.ip->v4.src_addr       = dest_in->sin_addr.s_addr;
+                       i.ip->v4.dest_addr      = src_in->sin_addr.s_addr;
+
                        buf += SWRAP_PACKET_IP_V4_SIZE;
 
                        src_port = dest_in->sin_port;
@@ -2600,14 +2887,15 @@ static uint8_t *swrap_pcap_packet_init(struct timeval *tval,
                        buf += SWRAP_PACKET_PAYLOAD_ICMP6_SIZE;
 
                        /* set the ip header in the ICMP payload */
-                       ip = (union swrap_packet_ip *)(void *)buf;
-                       ip->v6.ver_prio         = 0x60; /* version 4 and 5 * 32 bit words */
-                       ip->v6.flow_label_high  = 0x00;
-                       ip->v6.flow_label_low   = 0x0000;
-                       ip->v6.payload_length   = htons(wire_len - icmp_truncate_len); /* TODO */
-                       ip->v6.next_header      = protocol;
-                       memcpy(ip->v6.src_addr, dest_in6->sin6_addr.s6_addr, 16);
-                       memcpy(ip->v6.dest_addr, src_in6->sin6_addr.s6_addr, 16);
+                       i.ptr = buf;
+                       i.ip->v6.ver_prio               = 0x60; /* version 4 and 5 * 32 bit words */
+                       i.ip->v6.flow_label_high        = 0x00;
+                       i.ip->v6.flow_label_low = 0x0000;
+                       i.ip->v6.payload_length = htons(wire_len - icmp_truncate_len); /* TODO */
+                       i.ip->v6.next_header    = protocol;
+                       memcpy(i.ip->v6.src_addr, dest_in6->sin6_addr.s6_addr, 16);
+                       memcpy(i.ip->v6.dest_addr, src_in6->sin6_addr.s6_addr, 16);
+
                        buf += SWRAP_PACKET_IP_V6_SIZE;
 
                        src_port = dest_in6->sin6_port;
@@ -3082,7 +3370,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;
@@ -3326,6 +3622,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,
@@ -3446,6 +3745,7 @@ static int swrap_auto_bind(int fd, struct socket_info *si, int family)
        int ret;
        int port;
        struct stat st;
+       char *swrap_dir = NULL;
 
        swrap_mutex_lock(&autobind_start_mutex);
 
@@ -3477,8 +3777,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),
@@ -3531,11 +3831,20 @@ static int swrap_auto_bind(int fd, struct socket_info *si, int family)
                autobind_start = 10000;
        }
 
+       swrap_dir = socket_wrapper_dir();
+       if (swrap_dir == NULL) {
+               errno = EINVAL;
+               ret = -1;
+               goto done;
+       }
+
        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, socket_wrapper_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);
@@ -3567,6 +3876,7 @@ static int swrap_auto_bind(int fd, struct socket_info *si, int family)
        ret = 0;
 
 done:
+       SAFE_FREE(swrap_dir);
        swrap_mutex_unlock(&autobind_start_mutex);
        return ret;
 }
@@ -3599,6 +3909,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;
@@ -3907,6 +4220,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);
@@ -4256,6 +4572,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;
                }
@@ -4388,7 +4754,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) {
@@ -4403,14 +4769,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);
@@ -5329,14 +5715,21 @@ static ssize_t swrap_sendto(int s, const void *buf, size_t len, int flags,
                unsigned int iface;
                unsigned int prt = ntohs(((const struct sockaddr_in *)(const void *)to)->sin_port);
                char type;
+               char *swrap_dir = NULL;
 
                type = SOCKET_TYPE_CHAR_UDP;
 
+               swrap_dir = socket_wrapper_dir();
+               if (swrap_dir == NULL) {
+                       return -1;
+               }
+
                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,
-                                socket_wrapper_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 */
@@ -5348,6 +5741,8 @@ static ssize_t swrap_sendto(int s, const void *buf, size_t len, int flags,
                                    un_addr.sa_socklen);
                }
 
+               SAFE_FREE(swrap_dir);
+
                SWRAP_LOCK_SI(si);
 
                swrap_pcap_dump_packet(si, to, SWRAP_SENDTO, buf, len);
@@ -5808,6 +6203,7 @@ static ssize_t swrap_sendmsg(int s, const struct msghdr *omsg, int flags)
                off_t ofs = 0;
                size_t avail = 0;
                size_t remain;
+               char *swrap_dir = NULL;
 
                for (i = 0; i < (size_t)msg.msg_iovlen; i++) {
                        avail += msg.msg_iov[i].iov_len;
@@ -5833,9 +6229,14 @@ static ssize_t swrap_sendmsg(int s, const struct msghdr *omsg, int flags)
 
                type = SOCKET_TYPE_CHAR_UDP;
 
+               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,
-                                socket_wrapper_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 */
@@ -5845,6 +6246,8 @@ static ssize_t swrap_sendmsg(int s, const struct msghdr *omsg, int flags)
                        libc_sendmsg(s, &msg, flags);
                }
 
+               SAFE_FREE(swrap_dir);
+
                SWRAP_LOCK_SI(si);
 
                swrap_pcap_dump_packet(si, to, SWRAP_SENDTO, buf, len);
@@ -5993,6 +6396,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);
@@ -6256,7 +6660,7 @@ static void swrap_thread_parent(void)
 
 static void swrap_thread_child(void)
 {
-       SWRAP_UNLOCK_ALL;
+       SWRAP_REINIT_ALL;
 }
 
 /****************************
@@ -6264,7 +6668,7 @@ static void swrap_thread_child(void)
  ***************************/
 void swrap_constructor(void)
 {
-       int ret;
+       SWRAP_REINIT_ALL;
 
        /*
        * If we hold a lock and the application forks, then the child
@@ -6274,27 +6678,6 @@ void swrap_constructor(void)
        pthread_atfork(&swrap_thread_prepare,
                       &swrap_thread_parent,
                       &swrap_thread_child);
-
-       ret = socket_wrapper_init_mutex(&sockets_mutex);
-       if (ret != 0) {
-               SWRAP_LOG(SWRAP_LOG_ERROR,
-                         "Failed to initialize pthread mutex");
-               exit(-1);
-       }
-
-       ret = socket_wrapper_init_mutex(&socket_reset_mutex);
-       if (ret != 0) {
-               SWRAP_LOG(SWRAP_LOG_ERROR,
-                         "Failed to initialize pthread mutex");
-               exit(-1);
-       }
-
-       ret = socket_wrapper_init_mutex(&first_free_mutex);
-       if (ret != 0) {
-               SWRAP_LOG(SWRAP_LOG_ERROR,
-                         "Failed to initialize pthread mutex");
-               exit(-1);
-       }
 }
 
 /****************************
@@ -6327,3 +6710,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 */