From 6c04432144b2d842372c252ea30fd0340828f620 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Fri, 20 Aug 2010 01:31:37 +0000 Subject: [PATCH] * When attempting to connect() to the first available of multiple addresses, create a new socket() with the correct address type (IPv6 or IPv4) for the address we're about to try. * Also CLOSE that socket if the connection attempt fails. This also fixes a socket descriptor leak that's been in the previous implementation pretty much forever. --- citadel/clientsocket.c | 14 ++++++++------ citadel/textclient/citadel.c | 3 +-- citadel/utillib/citadel_ipc.c | 16 +++++++++------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/citadel/clientsocket.c b/citadel/clientsocket.c index 0d2da5a5a..a9325a2b3 100644 --- a/citadel/clientsocket.c +++ b/citadel/clientsocket.c @@ -89,24 +89,26 @@ int sock_connect(char *host, char *service) return(-1); } - sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); - if (sock < 0) { - CtdlLogPrintf(CTDL_ERR, "socket() failed: %s\n", strerror(errno)); - return(-1); - } - /* * Try all available addresses until we connect to one or until we run out. */ struct addrinfo *ai; for (ai = res; ai != NULL; ai = ai->ai_next) { /* FIXME display the address to which we are trying to connect */ + + sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); + if (sock < 0) { + CtdlLogPrintf(CTDL_ERR, "socket() failed: %s\n", strerror(errno)); + return(-1); + } + rc = connect(sock, res->ai_addr, res->ai_addrlen); if (rc >= 0) { return(sock); } else { CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno)); + close(sock); } } diff --git a/citadel/textclient/citadel.c b/citadel/textclient/citadel.c index 407b47ae9..30583cfae 100644 --- a/citadel/textclient/citadel.c +++ b/citadel/textclient/citadel.c @@ -1568,8 +1568,7 @@ int main(int argc, char **argv) newprompt("Connect to (return for local server): ", hostbuf, 64); #endif - sln_printf("Attaching to server... \r"); - sln_flush(); + sln_printf("Attaching to server...\n"); ipc = CtdlIPC_new(argc, argv, hostbuf, portbuf); if (!ipc) { screen_delete(); diff --git a/citadel/utillib/citadel_ipc.c b/citadel/utillib/citadel_ipc.c index 785c45199..01e890241 100644 --- a/citadel/utillib/citadel_ipc.c +++ b/citadel/utillib/citadel_ipc.c @@ -2658,24 +2658,26 @@ static int tcp_connectsock(char *host, char *service) return(-1); } - sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); - if (sock < 0) { - // CtdlLogPrintf(CTDL_ERR, "socket() failed: %s\n", strerror(errno)); - return(-1); - } - /* * Try all available addresses until we connect to one or until we run out. */ struct addrinfo *ai; for (ai = res; ai != NULL; ai = ai->ai_next) { /* FIXME display the address to which we are trying to connect */ - rc = connect(sock, res->ai_addr, res->ai_addrlen); +fprintf(stderr, "TRYING...\n"); + + sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); + if (sock < 0) return(-1); + + rc = connect(sock, ai->ai_addr, ai->ai_addrlen); if (rc >= 0) { +fprintf(stderr, "CONNECTED\n"); return(sock); } else { +fprintf(stderr, "FAILED: %s\n", strerror(errno)); // CtdlLogPrintf(CTDL_ERR, "connect() failed: %s\n", strerror(errno)); + close(sock); } } -- 2.39.2