/*
* client.c -- connect to a server
*
- * $Id$
- *
* 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 <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
-
-#ifdef HAVE_ARPA_INET_H
-# include <arpa/inet.h>
-#endif
-
-#define TRUE 1
-#define FALSE 0
+#include <arpa/inet.h>
#define MAXARGS 1000
static char **client_brkstring (char *, char *, char *);
static int client_brkany (char, char *);
static char **client_copyip (char **, char **, int);
-static char *client_getcpy (char *);
static void client_freelist(char **);
ap = arguments;
if (args != NULL && *args != 0) {
- ap = client_copyip (client_brkstring (client_getcpy (args), " ", "\n"),
+ ap = client_copyip (client_brkstring (mh_xstrdup(args), " ", "\n"),
ap, MAXARGS);
} else {
if (servers != NULL && *servers != 0)
- ap = client_copyip (client_brkstring (client_getcpy (servers), " ", "\n"),
+ ap = client_copyip (client_brkstring (mh_xstrdup(servers), " ", "\n"),
ap, MAXARGS);
}
if (ap == arguments) {
- *ap++ = client_getcpy ("localhost");
+ *ap++ = mh_xstrdup("localhost");
*ap = NULL;
}
memset(&hints, 0, sizeof(hints));
+#ifdef AI_ADDRCONFIG
hints.ai_flags = AI_ADDRCONFIG;
+#endif
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
for (ai = res; ai != NULL; ai = ai->ai_next) {
if (debug) {
char address[NI_MAXHOST];
+ char port[NI_MAXSERV];
rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, address,
- sizeof(address), NULL, 0, NI_NUMERICHOST);
+ sizeof(address), port, sizeof port,
+ NI_NUMERICHOST | NI_NUMERICSERV);
- fprintf(stderr, "Connecting to %s...\n",
- rc ? "unknown" : address);
+ fprintf(stderr, "Connecting to %s:%s...\n",
+ rc ? "unknown" : address,
+ rc ? "--" : port);
}
sd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (connect(sd, ai->ai_addr, ai->ai_addrlen) == 0) {
freeaddrinfo(res);
- client_freelist(ap);
+ client_freelist(arguments);
return sd;
}
freeaddrinfo(res);
}
- client_freelist(ap);
+ client_freelist(arguments);
strncpy (response, "no servers available", len_response);
return NOTOK;
}
static void
client_freelist(char **list)
{
- while (*list++ != NULL)
- free(*list);
+ free(*list);
}
static char **
client_brkstring (char *strg, char *brksep, char *brkterm)
{
- register int bi;
- register char c, *sp;
+ int bi;
+ char c, *sp;
sp = strg;
static int
client_brkany (char chr, char *strg)
{
- register char *sp;
-
+ char *sp;
+
if (strg)
for (sp = strg; *sp; sp++)
if (chr == *sp)
return q;
}
-
-
-static char *
-client_getcpy (char *str)
-{
- char *cp;
- size_t len;
-
- len = strlen(str) + 1;
- cp = mh_xmalloc(len);
-
- memcpy (cp, str, len);
- return cp;
-}
-