From daba3f8b54b04fe8623db3bab90c3aba15d4c379 Mon Sep 17 00:00:00 2001 From: James Peach Date: Sun, 9 Dec 2007 13:28:00 -0800 Subject: Fix connect(2) callers to use correct sockaddr size. Some systems (eg Mac OSX 10.5) require the length passed to match the socket address family. This introduces sys_connect() that does the right thing, and replaces all uses oc connect(2) with sys_connect(). Note that there are some LGPL callers that still call connect(2) directly. (This used to be commit e1bfdc17c49da582cdf907e260301ab1946b2ed3) --- source3/lib/ctdbd_conn.c | 2 +- source3/lib/sock_exec.c | 4 ++-- source3/lib/system.c | 18 ++++++++++++++++++ source3/lib/util_sock.c | 9 +++++---- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/source3/lib/ctdbd_conn.c b/source3/lib/ctdbd_conn.c index 47693ec621..899bbcfcce 100644 --- a/source3/lib/ctdbd_conn.c +++ b/source3/lib/ctdbd_conn.c @@ -135,7 +135,7 @@ static NTSTATUS ctdbd_connect(TALLOC_CTX *mem_ctx, addr.sun_family = AF_UNIX; strncpy(addr.sun_path, sockname, sizeof(addr.sun_path)); - if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { + if (sys_connect(fd, (struct sockaddr *)&addr) == -1) { DEBUG(0, ("connect(%s) failed: %s\n", sockname, strerror(errno))); close(fd); diff --git a/source3/lib/sock_exec.c b/source3/lib/sock_exec.c index 5e3178cba9..203d7e93b3 100644 --- a/source3/lib/sock_exec.c +++ b/source3/lib/sock_exec.c @@ -60,7 +60,7 @@ static int socketpair_tcp(int fd[2]) sock.sin_addr.s_addr = htonl(INADDR_LOOPBACK); - if (connect(fd[1],(struct sockaddr *)&sock,sizeof(sock)) == -1) { + if (sys_connect(fd[1], (struct sockaddr *)&sock) == -1) { if (errno != EINPROGRESS) goto failed; } else { connect_done = 1; @@ -70,7 +70,7 @@ static int socketpair_tcp(int fd[2]) close(listener); if (connect_done == 0) { - if (connect(fd[1],(struct sockaddr *)&sock,sizeof(sock)) != 0 + if (sys_connect(fd[1], (struct sockaddr *)&sock) != 0 && errno != EISCONN) goto failed; } diff --git a/source3/lib/system.c b/source3/lib/system.c index 86f3a8c4b8..eb6dcae6fb 100644 --- a/source3/lib/system.c +++ b/source3/lib/system.c @@ -2472,3 +2472,21 @@ int sys_getnameinfo(const struct sockaddr *psa, } return getnameinfo(psa, salen, host, hostlen, service, servlen, flags); } + +int sys_connect(int fd, const struct sockaddr * addr) +{ + socklen_t salen = -1; + + if (addr->sa_family == AF_INET) { + salen = sizeof(struct sockaddr_in); + } else if (addr->sa_family == AF_UNIX) { + salen = sizeof(struct sockaddr_un); + } +#if defined(HAVE_IPV6) + else if (addr->sa_family == AF_INET6) { + salen = sizeof(struct sockaddr_in6); + } +#endif + + return connect(fd, addr, salen); +} diff --git a/source3/lib/util_sock.c b/source3/lib/util_sock.c index e919cc5a46..8f1bd9e686 100644 --- a/source3/lib/util_sock.c +++ b/source3/lib/util_sock.c @@ -1459,7 +1459,7 @@ int open_socket_out(int type, /* and connect it to the destination */ connect_again: - ret = connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out)); + ret = sys_connect(res, (struct sockaddr *)&sock_out); /* Some systems return EAGAIN when they mean EINPROGRESS */ if (ret < 0 && (errno == EINPROGRESS || errno == EALREADY || @@ -1547,12 +1547,13 @@ bool open_any_socket_out(struct sockaddr_storage *addrs, int num_addrs, good_connect = false; for (i=0; i