-
-/*
- * 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 <h/mh.h>
-#include <h/mts.h>
-#include <h/utils.h>
+#include "h/mh.h"
+#include "client.h"
+#include "h/mts.h"
+#include "h/utils.h"
+#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
return NOTOK;
}
- memset(&hints, 0, sizeof(hints));
+ ZERO(&hints);
#ifdef AI_ADDRCONFIG
hints.ai_flags = AI_ADDRCONFIG;
#endif
}
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;
}
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);