]> diplodocus.org Git - nmh/blob - sbr/client.c
9704f89c5d888810cffbc4f70ba7d1cc12d63584
[nmh] / sbr / client.c
1
2 /*
3 * client.c -- connect to a server
4 *
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.
8 */
9
10 #include <h/mh.h>
11 #include <h/mts.h>
12 #include <h/utils.h>
13 #include <errno.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <netdb.h>
17 #include <arpa/inet.h>
18
19 #define MAXARGS 1000
20
21 /*
22 * static prototypes
23 */
24
25 /* client's own static version of several nmh subroutines */
26 static char **client_brkstring (char *, char *, char *);
27 static int client_brkany (char, char *);
28 static char **client_copyip (char **, char **, int);
29 static char *client_getcpy (char *);
30 static void client_freelist(char **);
31
32
33 int
34 client (char *args, char *service, char *response, int len_response, int debug)
35 {
36 int sd, rc;
37 char **ap, *arguments[MAXARGS];
38 struct addrinfo hints, *res, *ai;
39
40 ap = arguments;
41 if (args != NULL && *args != 0) {
42 ap = client_copyip (client_brkstring (client_getcpy (args), " ", "\n"),
43 ap, MAXARGS);
44 } else {
45 if (servers != NULL && *servers != 0)
46 ap = client_copyip (client_brkstring (client_getcpy (servers), " ", "\n"),
47 ap, MAXARGS);
48 }
49 if (ap == arguments) {
50 *ap++ = client_getcpy ("localhost");
51 *ap = NULL;
52 }
53
54 memset(&hints, 0, sizeof(hints));
55 #ifdef AI_ADDRCONFIG
56 hints.ai_flags = AI_ADDRCONFIG;
57 #endif
58 hints.ai_family = PF_UNSPEC;
59 hints.ai_socktype = SOCK_STREAM;
60
61 for (ap = arguments; *ap; ap++) {
62
63 if (debug) {
64 fprintf(stderr, "Trying to connect to \"%s\" ...\n", *ap);
65 }
66
67 rc = getaddrinfo(*ap, service, &hints, &res);
68
69 if (rc) {
70 if (debug) {
71 fprintf(stderr, "Lookup of \"%s\" failed: %s\n", *ap,
72 gai_strerror(rc));
73 }
74 continue;
75 }
76
77 for (ai = res; ai != NULL; ai = ai->ai_next) {
78 if (debug) {
79 char address[NI_MAXHOST];
80 char port[NI_MAXSERV];
81
82 rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, address,
83 sizeof(address), port, sizeof port,
84 NI_NUMERICHOST | NI_NUMERICSERV);
85
86 fprintf(stderr, "Connecting to %s:%s...\n",
87 rc ? "unknown" : address,
88 rc ? "--" : port);
89 }
90
91 sd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
92
93 if (sd < 0) {
94 if (debug)
95 fprintf(stderr, "socket() failed: %s\n", strerror(errno));
96 continue;
97 }
98
99 if (connect(sd, ai->ai_addr, ai->ai_addrlen) == 0) {
100 freeaddrinfo(res);
101 client_freelist(arguments);
102 return sd;
103 }
104
105 if (debug) {
106 fprintf(stderr, "Connection failed: %s\n", strerror(errno));
107 }
108
109 close(sd);
110 }
111
112 freeaddrinfo(res);
113 }
114
115 client_freelist(arguments);
116 strncpy (response, "no servers available", len_response);
117 return NOTOK;
118 }
119
120
121 /*
122 * Free a list of strings
123 */
124
125 static void
126 client_freelist(char **list)
127 {
128 free(*list);
129 }
130
131
132 /*
133 * static copies of three nmh subroutines
134 */
135
136 static char *broken[MAXARGS + 1];
137
138 static char **
139 client_brkstring (char *strg, char *brksep, char *brkterm)
140 {
141 register int bi;
142 register char c, *sp;
143
144 sp = strg;
145
146 for (bi = 0; bi < MAXARGS; bi++) {
147 while (client_brkany (c = *sp, brksep))
148 *sp++ = 0;
149 if (!c || client_brkany (c, brkterm)) {
150 *sp = 0;
151 broken[bi] = 0;
152 return broken;
153 }
154
155 broken[bi] = sp;
156 while ((c = *++sp) && !client_brkany (c, brksep) && !client_brkany (c, brkterm))
157 continue;
158 }
159 broken[MAXARGS] = 0;
160
161 return broken;
162 }
163
164
165 /*
166 * returns 1 if chr in strg, 0 otherwise
167 */
168 static int
169 client_brkany (char chr, char *strg)
170 {
171 register char *sp;
172
173 if (strg)
174 for (sp = strg; *sp; sp++)
175 if (chr == *sp)
176 return 1;
177 return 0;
178 }
179
180
181 /*
182 * copy a string array and return pointer to end
183 */
184 static char **
185 client_copyip (char **p, char **q, int len_q)
186 {
187 while (*p && --len_q > 0)
188 *q++ = *p++;
189
190 *q = NULL;
191
192 return q;
193 }
194
195
196 static char *
197 client_getcpy (char *str)
198 {
199 char *cp;
200 size_t len;
201
202 len = strlen(str) + 1;
203 cp = mh_xmalloc(len);
204
205 memcpy (cp, str, len);
206 return cp;
207 }