]>
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.
14 #include <sys/socket.h>
15 #include <netinet/in.h>
18 #ifdef HAVE_ARPA_INET_H
19 # include <arpa/inet.h>
31 /* client's own static version of several nmh subroutines */
32 static char **client_brkstring (char *, char *, char *);
33 static int client_brkany (char, char *);
34 static char **client_copyip (char **, char **, int);
35 static char *client_getcpy (char *);
36 static void client_freelist(char **);
40 client (char *args
, char *service
, char *response
, int len_response
, int debug
)
43 char **ap
, *arguments
[MAXARGS
];
44 struct addrinfo hints
, *res
, *ai
;
47 if (args
!= NULL
&& *args
!= 0) {
48 ap
= client_copyip (client_brkstring (client_getcpy (args
), " ", "\n"),
51 if (servers
!= NULL
&& *servers
!= 0)
52 ap
= client_copyip (client_brkstring (client_getcpy (servers
), " ", "\n"),
55 if (ap
== arguments
) {
56 *ap
++ = client_getcpy ("localhost");
60 memset(&hints
, 0, sizeof(hints
));
62 hints
.ai_flags
= AI_ADDRCONFIG
;
64 hints
.ai_family
= PF_UNSPEC
;
65 hints
.ai_socktype
= SOCK_STREAM
;
67 for (ap
= arguments
; *ap
; ap
++) {
70 fprintf(stderr
, "Trying to connect to \"%s\" ...\n", *ap
);
73 rc
= getaddrinfo(*ap
, service
, &hints
, &res
);
77 fprintf(stderr
, "Lookup of \"%s\" failed: %s\n", *ap
,
83 for (ai
= res
; ai
!= NULL
; ai
= ai
->ai_next
) {
85 char address
[NI_MAXHOST
];
87 rc
= getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, address
,
88 sizeof(address
), NULL
, 0, NI_NUMERICHOST
);
90 fprintf(stderr
, "Connecting to %s...\n",
91 rc
? "unknown" : address
);
94 sd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
98 fprintf(stderr
, "socket() failed: %s\n", strerror(errno
));
102 if (connect(sd
, ai
->ai_addr
, ai
->ai_addrlen
) == 0) {
109 fprintf(stderr
, "Connection failed: %s\n", strerror(errno
));
119 strncpy (response
, "no servers available", len_response
);
125 * Free a list of strings
129 client_freelist(char **list
)
131 while (*list
++ != NULL
)
137 * static copies of three nmh subroutines
140 static char *broken
[MAXARGS
+ 1];
143 client_brkstring (char *strg
, char *brksep
, char *brkterm
)
146 register char c
, *sp
;
150 for (bi
= 0; bi
< MAXARGS
; bi
++) {
151 while (client_brkany (c
= *sp
, brksep
))
153 if (!c
|| client_brkany (c
, brkterm
)) {
160 while ((c
= *++sp
) && !client_brkany (c
, brksep
) && !client_brkany (c
, brkterm
))
170 * returns 1 if chr in strg, 0 otherwise
173 client_brkany (char chr
, char *strg
)
178 for (sp
= strg
; *sp
; sp
++)
186 * copy a string array and return pointer to end
189 client_copyip (char **p
, char **q
, int len_q
)
191 while (*p
&& --len_q
> 0)
201 client_getcpy (char *str
)
206 len
= strlen(str
) + 1;
207 cp
= mh_xmalloc(len
);
209 memcpy (cp
, str
, len
);