diff options
author | Volker Lendecke <vlendec@samba.org> | 2005-07-03 10:20:03 +0000 |
---|---|---|
committer | Gerald (Jerry) Carter <jerry@samba.org> | 2007-10-10 10:58:10 -0500 |
commit | d10c664b6151d3296d2fde5dc9833ea90e7ea0e8 (patch) | |
tree | f3cb4c16f8307221b32a6422d903ba2d6ca9d5fb | |
parent | 22ea1952c8d74a1a54af5e507d124c1e30da067f (diff) | |
download | samba-d10c664b6151d3296d2fde5dc9833ea90e7ea0e8.tar.gz samba-d10c664b6151d3296d2fde5dc9833ea90e7ea0e8.tar.bz2 samba-d10c664b6151d3296d2fde5dc9833ea90e7ea0e8.zip |
r8092: Attempt to fix the socket wrapper stuff for Samba 3.
Volker
(This used to be commit 53c22c036588238d0d732f7dbf7329921f593191)
-rw-r--r-- | source3/include/socket_wrapper.h | 22 | ||||
-rw-r--r-- | source3/lib/socket_wrapper.c | 146 |
2 files changed, 108 insertions, 60 deletions
diff --git a/source3/include/socket_wrapper.h b/source3/include/socket_wrapper.h index 6da5a2980c..d8815a2575 100644 --- a/source3/include/socket_wrapper.h +++ b/source3/include/socket_wrapper.h @@ -32,17 +32,17 @@ ssize_t swrap_sendto(int s, const void *buf, size_t len, int flags, const str int swrap_close(int); #ifdef SOCKET_WRAPPER_REPLACE -#define accept swrap_accept -#define connect swrap_connect -#define bind swrap_bind -#define getpeername swrap_getpeername -#define getsockname swrap_getsockname -#define getsockopt swrap_getsockopt -#define setsockopt swrap_setsockopt -#define recvfrom swrap_recvfrom -#define sendto swrap_sendto -#define socket swrap_socket -#define close swrap_close +#define accept(s,addr,addrlen) swrap_accept(s,addr,addrlen) +#define connect(s,serv_addr,addrlen) swrap_connect(s,serv_addr,addrlen) +#define bind(s,myaddr,addrlen) swrap_bind(s,myaddr,addrlen) +#define getpeername(s,name,addrlen) swrap_getpeername(s,name,addrlen) +#define getsockname(s,name,addrlen) swrap_getsockname(s,name,addrlen) +#define getsockopt(s,level,optname,optval,optlen) swrap_getsockopt(s,level,optname,optval,optlen) +#define setsockopt(s,level,optname,optval,optlen) swrap_setsockopt(s,level,optname,optval,optlen) +#define recvfrom(s,buf,len,flags,from,fromlen) swrap_recvfrom(s,buf,len,flags,from,fromlen) +#define sendto(s,buf,len,flags,to,tolen) swrap_sendto(s,buf,len,flags,to,tolen) +#define socket(domain,type,protocol) swrap_socket(domain,type,protocol) +#define close(s) swrap_close(s) #endif #endif /* __SOCKET_WRAPPER_H__ */ diff --git a/source3/lib/socket_wrapper.c b/source3/lib/socket_wrapper.c index cb3911c358..bb4a4e0a67 100644 --- a/source3/lib/socket_wrapper.c +++ b/source3/lib/socket_wrapper.c @@ -19,21 +19,20 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#ifdef _SAMBA_BUILD_ #include "includes.h" -#include "system/network.h" -#else -#include <sys/types.h> -#include <sys/socket.h> -#include <errno.h> -#include <sys/un.h> -#include <netinet/in.h> -#include <netinet/tcp.h> -#include <stdlib.h> -#include <unistd.h> -#include <string.h> -#include <stdio.h> -#include "dlinklist.h" + +#ifdef SOCKET_WRAPPER_REPLACE +#undef accept +#undef connect +#undef bind +#undef getpeername +#undef getsockname +#undef getsockopt +#undef setsockopt +#undef recvfrom +#undef sendto +#undef socket +#undef close #endif /* LD_PRELOAD doesn't work yet, so REWRITE_CALLS is all we support @@ -54,6 +53,10 @@ #define real_close close #endif +#undef malloc +#undef calloc +#undef strdup + static struct sockaddr *sockaddr_dup(const void *data, socklen_t len) { struct sockaddr *ret = (struct sockaddr *)malloc(len); @@ -95,8 +98,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 +110,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 +140,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 +150,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; @@ -229,15 +243,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/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; |