]>
diplodocus.org Git - nmh/blob - sbr/client.c
3 * client.c -- connect to a server
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
13 #include <sys/socket.h>
14 #include <netinet/in.h>
16 #include <arpa/inet.h>
21 client (char *server
, char *service
, char *response
, int len_response
,
25 struct addrinfo hints
, *res
, *ai
;
27 if (!server
|| *server
== '\0') {
28 snprintf(response
, len_response
, "Internal error: NULL server name "
29 "passed to client()");
33 memset(&hints
, 0, sizeof(hints
));
35 hints
.ai_flags
= AI_ADDRCONFIG
;
37 hints
.ai_family
= PF_UNSPEC
;
38 hints
.ai_socktype
= SOCK_STREAM
;
41 fprintf(stderr
, "Trying to connect to \"%s\" ...\n", server
);
44 rc
= getaddrinfo(server
, service
, &hints
, &res
);
47 snprintf(response
, len_response
, "Lookup of \"%s\" failed: %s",
48 server
, gai_strerror(rc
));
52 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
54 char address
[NI_MAXHOST
];
55 char port
[NI_MAXSERV
];
57 rc
= getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, address
,
58 sizeof(address
), port
, sizeof port
,
59 NI_NUMERICHOST
| NI_NUMERICSERV
);
61 fprintf(stderr
, "Connecting to %s:%s...\n",
62 rc
? "unknown" : address
, rc
? "--" : port
);
65 sd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
69 fprintf(stderr
, "socket() failed: %s\n", strerror(errno
));
74 if (connect(sd
, ai
->ai_addr
, ai
->ai_addrlen
) == 0) {
82 fprintf(stderr
, "Connection failed: %s\n", strerror(errno
));
89 * Improve error handling a bit. If we were given multiple IP addresses
90 * then return the old "no servers available" error, but point the user
91 * to -snoop (hopefully that's universal). Otherwise report a specific
96 snprintf(response
, len_response
, "no servers available (use -snoop "
99 snprintf(response
, len_response
, "Connection to \"%s\" failed: %s",
100 server
, strerror(errno
));