]>
diplodocus.org Git - nmh/blob - sbr/client.c
1 /* client.c -- connect to a server
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
12 #include <sys/socket.h>
13 #include <netinet/in.h>
15 #include <arpa/inet.h>
20 client (char *server
, char *service
, char *response
, int len_response
,
24 struct addrinfo hints
, *res
, *ai
;
26 if (!server
|| *server
== '\0') {
27 snprintf(response
, len_response
, "Internal error: NULL server name "
28 "passed to client()");
34 hints
.ai_flags
= AI_ADDRCONFIG
;
36 hints
.ai_family
= PF_UNSPEC
;
37 hints
.ai_socktype
= SOCK_STREAM
;
40 fprintf(stderr
, "Trying to connect to \"%s\" ...\n", server
);
43 rc
= getaddrinfo(server
, service
, &hints
, &res
);
46 snprintf(response
, len_response
, "Lookup of \"%s\" failed: %s",
47 server
, gai_strerror(rc
));
51 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
53 char address
[NI_MAXHOST
];
54 char port
[NI_MAXSERV
];
56 rc
= getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, address
,
57 sizeof(address
), port
, sizeof port
,
58 NI_NUMERICHOST
| NI_NUMERICSERV
);
60 fprintf(stderr
, "Connecting to %s:%s...\n",
61 rc
? "unknown" : address
, rc
? "--" : port
);
64 sd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
68 fprintf(stderr
, "socket() failed: %s\n", strerror(errno
));
73 if (connect(sd
, ai
->ai_addr
, ai
->ai_addrlen
) == 0) {
81 fprintf(stderr
, "Connection failed: %s\n", strerror(errno
));
88 * Improve error handling a bit. If we were given multiple IP addresses
89 * then return the old "no servers available" error, but point the user
90 * to -snoop (hopefully that's universal). Otherwise report a specific
95 snprintf(response
, len_response
, "no servers available (use -snoop "
98 snprintf(response
, len_response
, "Connection to \"%s\" failed: %s",
99 server
, strerror(errno
));