X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/0a032eea07f6d77ac6ea4d5a39c9491c34358058..23816efd3e8e294e8f1c1ce811ac3a1cd15d334e:/sbr/client.c diff --git a/sbr/client.c b/sbr/client.c index 36b2525b..8d390a81 100644 --- a/sbr/client.c +++ b/sbr/client.c @@ -1,14 +1,15 @@ -/* - * client.c -- connect to a server +/* client.c -- connect to a server * * This code is Copyright (c) 2002, by the authors of nmh. See the * COPYRIGHT file in the root directory of the nmh distribution for * complete copyright information. */ -#include -#include -#include +#include "h/mh.h" +#include "client.h" +#include "h/mts.h" +#include "h/utils.h" +#include #include #include #include @@ -29,7 +30,7 @@ client (char *server, char *service, char *response, int len_response, return NOTOK; } - memset(&hints, 0, sizeof(hints)); + ZERO(&hints); #ifdef AI_ADDRCONFIG hints.ai_flags = AI_ADDRCONFIG; #endif @@ -71,7 +72,15 @@ client (char *server, char *service, char *response, int len_response, } if (connect(sd, ai->ai_addr, ai->ai_addrlen) == 0) { + int flags; + freeaddrinfo(res); + + flags = fcntl(sd, F_GETFD, 0); + + if (flags != -1) + fcntl(sd, F_SETFD, flags | FD_CLOEXEC); + return sd; } @@ -93,10 +102,19 @@ client (char *server, char *service, char *response, int len_response, if (res->ai_next) snprintf(response, len_response, "no servers available (use -snoop " - "for details"); - else - snprintf(response, len_response, "Connection to \"%s\" failed: %s", - server, strerror(errno)); + "for details)"); + else { + char port[NI_MAXSERV]; + + if (getnameinfo(res->ai_addr, res->ai_addrlen, NULL, 0, port, + sizeof port, NI_NUMERICSERV)) { + strncpy(port, "unknown", sizeof port); + port[sizeof(port) - 1] = '\0'; + } + + snprintf(response, len_response, "Connection to \"%s:%s\" failed: %s", + server, port, strerror(errno)); + } freeaddrinfo(res);