diff options
author | Andrew Tridgell <tridge@samba.org> | 2005-06-10 12:21:46 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 13:17:55 -0500 |
commit | d543e4a26510da5293b5c884f026f3e40ec869d7 (patch) | |
tree | c58896e5c4ea6919d457ec817e766df351ba1b19 /source4 | |
parent | 70656780add642ae390c5be48255bd99af915445 (diff) | |
download | samba-d543e4a26510da5293b5c884f026f3e40ec869d7.tar.gz samba-d543e4a26510da5293b5c884f026f3e40ec869d7.tar.bz2 samba-d543e4a26510da5293b5c884f026f3e40ec869d7.zip |
r7460: fixed several problems with the socket wrapper code and unbound sockets
This should fix the LOCAL-SOCKET test in the build farm
(This used to be commit 417e967afb457ee505c2302cdc83d7060033b0aa)
Diffstat (limited to 'source4')
-rw-r--r-- | source4/lib/socket_wrapper/socket_wrapper.c | 110 |
1 files changed, 79 insertions, 31 deletions
diff --git a/source4/lib/socket_wrapper/socket_wrapper.c b/source4/lib/socket_wrapper/socket_wrapper.c index 2a26ba1534..882ef462ea 100644 --- a/source4/lib/socket_wrapper/socket_wrapper.c +++ b/source4/lib/socket_wrapper/socket_wrapper.c @@ -22,8 +22,10 @@ #ifdef _SAMBA_BUILD #include "includes.h" #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> @@ -95,8 +97,8 @@ 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) { @@ -107,9 +109,20 @@ 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); + if (prt == 0) { + struct stat st; + /* handle auto-allocation of ephemeral ports */ + prt = 5000; + do { + snprintf(un->sun_path, sizeof(un->sun_path), "%s/sock_ip_%d_%u", + 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/sock_ip_%d_%u", getenv("SOCKET_WRAPPER_DIR"), type, prt); return 0; @@ -126,7 +139,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 +149,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; @@ -235,9 +248,53 @@ int swrap_accept(int s, struct sockaddr *addr, socklen_t *addrlen) child_si->myname = sockaddr_dup(addr, *addrlen); } + 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/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; + } + + 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; @@ -256,6 +313,11 @@ int swrap_connect(int s, const struct sockaddr *serv_addr, socklen_t addrlen) 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; @@ -280,6 +342,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; @@ -289,8 +359,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; } @@ -402,6 +470,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; @@ -412,31 +481,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; |