1
0
mirror of https://github.com/samba-team/samba.git synced 2024-12-24 21:34:56 +03:00

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 commit is contained in:
James Peach 2007-12-09 13:28:00 -08:00
parent ffee51586c
commit e1bfdc17c4
4 changed files with 26 additions and 7 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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<num_addrs; i++) {
const struct sockaddr * a =
(const struct sockaddr *)&(addrs[i]);
if (sockets[i] == -1)
continue;
if (connect(sockets[i], (struct sockaddr *)&(addrs[i]),
sizeof(*addrs)) == 0) {
if (sys_connect(sockets[i], a) == 0) {
/* Rather unlikely as we are non-blocking, but it
* might actually happen. */
resulting_index = i;
@ -1681,7 +1682,7 @@ int open_udp_socket(const char *host, int port)
sock_out.sin_port = htons(port);
sock_out.sin_family = PF_INET;
if (connect(res,(struct sockaddr *)&sock_out,sizeof(sock_out))) {
if (sys_connect(res,(struct sockaddr *)&sock_out)) {
close(res);
return -1;
}