r8671: use much shorter names for the selftest directory and socket wrapper
[bbaumbach/samba-autobuild/.git] / source4 / lib / socket_wrapper / socket_wrapper.c
index b7b0aa07b6f06812612a3986eea0091f58d61945..d0d08d22b9364a1b08ea752cc7585d3743ecb092 100644 (file)
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
-#ifdef _SAMBA_BUILD
+#ifdef _SAMBA_BUILD_
 #include "includes.h"
+#undef SOCKET_WRAPPER
 #include "system/network.h"
+#include "system/filesys.h"
 #else
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <sys/socket.h>
 #include <errno.h>
 #include <sys/un.h>
@@ -33,8 +36,8 @@
 #include <unistd.h>
 #include <string.h>
 #include <stdio.h>
-#include "dlinklist.h"
 #endif
+#include "dlinklist.h"
 
 /* LD_PRELOAD doesn't work yet, so REWRITE_CALLS is all we support
  * for now */
 #define real_close close
 #endif
 
+/* we need to use a very terse format here as IRIX 6.4 silently
+   truncates names to 16 chars, so if we use a longer name then we
+   can't tell which port a packet came from with recvfrom() 
+   
+   with this format we have 8 chars left for the directory name
+*/
+#define SOCKET_FORMAT "%u_%u"
+
 static struct sockaddr *sockaddr_dup(const void *data, socklen_t len)
 {
        struct sockaddr *ret = (struct sockaddr *)malloc(len);
@@ -95,11 +106,11 @@ static int convert_un_in(const struct sockaddr_un *un, struct sockaddr_in *in, s
        }
 
        in->sin_family = AF_INET;
-       in->sin_port = 1025; /* Default to 1025 */
-       p = strchr(un->sun_path, '/');
+       in->sin_port = htons(1025); /* Default to 1025 */
+       p = strrchr(un->sun_path, '/');
        if (p) p++; else p = un->sun_path;
 
-       if (sscanf(p, "sock_ip_%d_%u", &type, &prt) == 2) {
+       if (sscanf(p, SOCKET_FORMAT, &type, &prt) == 2) {
                in->sin_port = htons(prt);
        }
        in->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
@@ -107,10 +118,21 @@ static int convert_un_in(const struct sockaddr_un *un, struct sockaddr_in *in, s
        return 0;
 }
 
-static int convert_in_un(int type, const struct sockaddr_in *in, struct sockaddr_un *un)
+static int convert_in_un(struct socket_info *si, const struct sockaddr_in *in, struct sockaddr_un *un)
 {
+       int type = si->type;
        uint16_t prt = ntohs(in->sin_port);
-       snprintf(un->sun_path, sizeof(un->sun_path), "%s/sock_ip_%d_%u", 
+       if (prt == 0) {
+               struct stat st;
+               /* handle auto-allocation of ephemeral ports */
+               prt = 5000;
+               do {
+                       snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT, 
+                                getenv("SOCKET_WRAPPER_DIR"), type, ++prt);
+               } while (stat(un->sun_path, &st) == 0 && prt < 10000);
+               ((struct sockaddr_in *)si->myname)->sin_port = htons(prt);
+       } 
+       snprintf(un->sun_path, sizeof(un->sun_path), "%s/"SOCKET_FORMAT, 
                 getenv("SOCKET_WRAPPER_DIR"), type, prt);
        return 0;
 }
@@ -126,7 +148,7 @@ static struct socket_info *find_socket_info(int fd)
        return NULL;
 }
 
-static int sockaddr_convert_to_un(const struct socket_info *si, const struct sockaddr *in_addr, socklen_t in_len, 
+static int sockaddr_convert_to_un(struct socket_info *si, const struct sockaddr *in_addr, socklen_t in_len, 
                                         struct sockaddr_un *out_addr)
 {
        if (!out_addr)
@@ -136,7 +158,7 @@ static int sockaddr_convert_to_un(const struct socket_info *si, const struct soc
 
        switch (in_addr->sa_family) {
        case AF_INET:
-               return convert_in_un(si->type, (const struct sockaddr_in *)in_addr, out_addr);
+               return convert_in_un(si, (const struct sockaddr_in *)in_addr, out_addr);
        case AF_UNIX:
                memcpy(out_addr, in_addr, sizeof(*out_addr));
                return 0;
@@ -216,6 +238,8 @@ int swrap_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
                return real_accept(s, addr, addrlen);
        }
 
+       memset(&un_addr, 0, sizeof(un_addr));
+
        ret = real_accept(s, (struct sockaddr *)&un_addr, &un_addrlen);
        if (ret == -1) return ret;
 
@@ -229,15 +253,58 @@ int swrap_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
        memset(child_si, 0, sizeof(*child_si));
 
        child_si->fd = fd;
+       child_si->bound = 1;
 
-       if (addr && addrlen) {
-               child_si->myname_len = *addrlen;
-               child_si->myname = sockaddr_dup(addr, *addrlen);
-       }
+       child_si->myname_len = parent_si->myname_len;
+       child_si->myname = sockaddr_dup(parent_si->myname, parent_si->myname_len);
+
+       child_si->peername_len = *addrlen;
+       child_si->peername = sockaddr_dup(addr, *addrlen);
+
+       DLIST_ADD(sockets, child_si);
 
        return fd;
 }
 
+/* using sendto() or connect() on an unbound socket would give the
+   recipient no way to reply, as unlike UDP and TCP, a unix domain
+   socket can't auto-assign emphemeral port numbers, so we need to
+   assign it here */
+static int swrap_auto_bind(struct socket_info *si)
+{
+       struct sockaddr_un un_addr;
+       struct sockaddr_in in;
+       int i;
+       
+       un_addr.sun_family = AF_UNIX;
+       
+       for (i=0;i<1000;i++) {
+               snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), 
+                        "%s/"SOCKET_FORMAT, getenv("SOCKET_WRAPPER_DIR"), 
+                        SOCK_DGRAM, i + 10000);
+               if (bind(si->fd, (struct sockaddr *)&un_addr, 
+                        sizeof(un_addr)) == 0) {
+                       si->tmp_path = strdup(un_addr.sun_path);
+                       si->bound = 1;
+                       break;
+               }
+       }
+       if (i == 1000) {
+               return -1;
+       }
+       
+       memset(&in, 0, sizeof(in));
+       in.sin_family = AF_INET;
+       in.sin_port   = htons(i);
+       in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+       
+       si->myname_len = sizeof(in);
+       si->myname = sockaddr_dup(&in, si->myname_len);
+       si->bound = 1;
+       return 0;
+}
+
+
 int swrap_connect(int s, const struct sockaddr *serv_addr, socklen_t addrlen)
 {
        int ret;
@@ -249,12 +316,18 @@ int swrap_connect(int s, const struct sockaddr *serv_addr, socklen_t addrlen)
        }
 
        /* only allow pseudo loopback connections */
-       if (((const struct sockaddr_in *)serv_addr)->sin_addr.s_addr != 
+       if (serv_addr->sa_family == AF_INET &&
+               ((const struct sockaddr_in *)serv_addr)->sin_addr.s_addr != 
            htonl(INADDR_LOOPBACK)) {
                errno = ENETUNREACH;
                return -1;
        }
 
+       if (si->bound == 0 && si->domain != AF_UNIX) {
+               ret = swrap_auto_bind(si);
+               if (ret == -1) return -1;
+       }
+
        ret = sockaddr_convert_to_un(si, (const struct sockaddr *)serv_addr, addrlen, &un_addr);
        if (ret == -1) return -1;
 
@@ -279,6 +352,14 @@ int swrap_bind(int s, const struct sockaddr *myaddr, socklen_t addrlen)
                return real_bind(s, myaddr, addrlen);
        }
 
+       si->myname_len = addrlen;
+       si->myname = sockaddr_dup(myaddr, addrlen);
+
+       if (myaddr->sa_family == AF_INET &&
+           ((const struct sockaddr_in *)myaddr)->sin_addr.s_addr == 0) {
+               ((struct sockaddr_in *)si->myname)->sin_addr.s_addr = 
+                       htonl(INADDR_LOOPBACK);
+       }
        ret = sockaddr_convert_to_un(si, (const struct sockaddr *)myaddr, addrlen, &un_addr);
        if (ret == -1) return -1;
 
@@ -288,8 +369,6 @@ int swrap_bind(int s, const struct sockaddr *myaddr, socklen_t addrlen)
                        sizeof(struct sockaddr_un));
 
        if (ret == 0) {
-               si->myname_len = addrlen;
-               si->myname = sockaddr_dup(myaddr, addrlen);
                si->bound = 1;
        }
 
@@ -321,7 +400,7 @@ int swrap_getsockname(int s, struct sockaddr *name, socklen_t *addrlen)
        struct socket_info *si = find_socket_info(s);
 
        if (!si) {
-               return real_getpeername(s, name, addrlen);
+               return real_getsockname(s, name, addrlen);
        }
 
        memcpy(name, si->myname, si->myname_len);
@@ -389,6 +468,8 @@ ssize_t swrap_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr
                return real_recvfrom(s, buf, len, flags, from, fromlen);
        }
 
+       /* irix 6.4 forgets to null terminate the sun_path string :-( */
+       memset(&un_addr, 0, sizeof(un_addr));
        ret = real_recvfrom(s, buf, len, flags, (struct sockaddr *)&un_addr, &un_addrlen);
        if (ret == -1) 
                return ret;
@@ -401,6 +482,7 @@ ssize_t swrap_recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr
        return ret;
 }
 
+
 ssize_t swrap_sendto(int  s,  const  void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen)
 {
        struct sockaddr_un un_addr;
@@ -411,31 +493,10 @@ ssize_t swrap_sendto(int  s,  const  void *buf, size_t len, int flags, const str
                return real_sendto(s, buf, len, flags, to, tolen);
        }
 
-       /* using sendto() on an unbound DGRAM socket would give the
-          recipient no way to reply, as unlike UDP, a unix domain socket
-          can't auto-assign emphemeral port numbers, so we need to assign
-          it here */
-       if (si->bound == 0 && si->type == SOCK_DGRAM) {
-               int i;
-
-               un_addr.sun_family = AF_UNIX;
-
-               for (i=0;i<1000;i++) {
-                       snprintf(un_addr.sun_path, sizeof(un_addr.sun_path), 
-                                "%s/sock_ip_%u_%u", getenv("SOCKET_WRAPPER_DIR"), 
-                                SOCK_DGRAM, i + 10000);
-                       if (bind(si->fd, (struct sockaddr *)&un_addr, 
-                                sizeof(un_addr)) == 0) {
-                               si->tmp_path = strdup(un_addr.sun_path);
-                               si->bound = 1;
-                               break;
-                       }
-               }
-               if (i == 1000) {
-                       return -1;
-               }
+       if (si->bound == 0 && si->domain != AF_UNIX) {
+               ret = swrap_auto_bind(si);
+               if (ret == -1) return -1;
        }
-       
 
        ret = sockaddr_convert_to_un(si, to, tolen, &un_addr);
        if (ret == -1) return -1;