Debian Codegraph @

libhylafax/InetTransport.c++

raw
1 /* $Id$ */
2 /*
3 * Copyright (c) 1995-1996 Sam Leffler
4 * Copyright (c) 1995-1996 Silicon Graphics, Inc.
5 * HylaFAX is a trademark of Silicon Graphics
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26 #include "config.h"
27 #include "FaxClient.h"
28 #include "InetTransport.h"
29 #include "Sys.h"
30 #include "NLS.h"
31
32
33 InetTransport::InetTransport(FaxClient& c) : Transport(c) {}
34 InetTransport::~InetTransport(){}
35
36 bool
37 InetTransport::isA(const char*)
38 {
39 return (true); // XXX are there checks we can make?
40 }
41
42 #if CONFIG_INETTRANSPORT
43 #include "Socket.h"
44
45 #include <sys/types.h>
46 extern "C" {
47 #include <arpa/inet.h>
48 #include <arpa/telnet.h>
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netdb.h>
53 }
54 #include <ctype.h>
55 #include <errno.h>
56
57
58 /*
59 * References for IPv6:
60 * http://people.redhat.com/drepper/userapi-ipv6.html
61 * http://www.ietf.org/rfc/rfc2428.txt
62 */
63
64 bool
65 InetTransport::callServer(fxStr& emsg)
66 {
67
68 fxStr service(FAX_SERVICE);
69 fxStr proto(client.getProtoName());
70
71 int protocol = 0;
72 struct addrinfo hints, *ai;
73
74 if (client.getPort() != -1)
75 service = fxStr::format("%d", client.getPort());
76 else {
77 char* cp;
78 if ((cp = getenv("FAXSERVICE")) && *cp != '\0') {
79 fxStr s(cp);
80 u_int l = s.next(0,'/');
81 service = s.head(l);
82 if (l < s.length())
83 proto = s.tail(s.length()-(l+1));
84 }
85 }
86
87 const char* cproto = proto; // XXX for busted include files
88 struct protoent* pp = getprotobyname(cproto);
89 if (!pp) {
90 client.printWarning(NLS::TEXT("%s: No protocol definition, using default."),
91 cproto);
92 } else
93 protocol = pp->p_proto;
94
95 memset (&hints, '\0', sizeof (hints));
96 hints.ai_flags = AI_CANONNAME;
97 #ifdef AI_NUMERICHOST
98 hints.ai_flags |= AI_NUMERICHOST;
99 #endif
100 #ifdef AI_ADDRCONFIG
101 hints.ai_flags |= AI_ADDRCONFIG;
102 #endif
103 hints.ai_socktype = SOCK_STREAM;
104 hints.ai_protocol = protocol;
105
106 int err = getaddrinfo (client.getHost(), service, &hints, &ai);
107
108 #ifdef AI_NUMERICHOST
109 if (err == EAI_NONAME) {
110 hints.ai_flags &= ~AI_NUMERICHOST;
111 err = getaddrinfo (client.getHost(), service, &hints, &ai);
112 }
113 #endif
114 if (err != 0) {
115 client.printWarning(NLS::TEXT("getaddrinfo failed with %d: %s"), err, gai_strerror(err));
116 return false;
117 }
118
119 for (struct addrinfo *aip = ai; aip != NULL; aip = aip->ai_next)
120 {
121 Socket::Address *addr = (Socket::Address*)aip->ai_addr;
122 char buf[256]; // For inet_ntop use
123 fxAssert(aip->ai_family == Socket::family(*addr), "addrinfo ai_family doesn't match in_addr->ai_info");
124 if (client.getVerbose())
125 client.traceServer(NLS::TEXT("Trying %s [%d] (%s) at port %u..."),
126 (const char*)client.getHost(), Socket::family(*addr),
127 inet_ntop(Socket::family(*addr), Socket::addr(*addr), buf, sizeof(buf)),
128 ntohs(Socket::port(*addr)));
129 int fd = socket (aip->ai_family, aip->ai_socktype, aip->ai_protocol);
130 if (fd != -1 && ( connect(fd, aip->ai_addr, aip->ai_addrlen) == 0))
131 {
132 if (client.getVerbose())
133 client.traceServer(NLS::TEXT("Connected to %s."), aip->ai_canonname);
134 /* Free the addrinfo chain before we leave*/
135 freeaddrinfo(ai);
136 #if defined(IP_TOS) && defined(IPTOS_LOWDELAY)
137 int tos = IPTOS_LOWDELAY;
138 if (Socket::setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
139 client.printWarning(NLS::TEXT("setsockopt(TOS): %s (ignored)"),
140 strerror(errno));
141 #endif
142 #ifdef SO_OOBINLINE
143 int on = 1;
144 if (Socket::setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &on, sizeof (on)) < 0)
145 client.printWarning(NLS::TEXT("setsockopt(OOBLINE): %s (ignored)"),
146 strerror(errno));
147 #endif
148 /*
149 * NB: We dup the descriptor because some systems
150 * that emulate sockets with TLI incorrectly handle
151 * things if we use the same descriptor for both
152 * input and output (sigh).
153 */
154 client.setCtrlFds(fd, dup(fd));
155 return (true);
156 }
157 Sys::close(fd), fd = -1;
158 }
159
160 emsg = fxStr::format(NLS::TEXT("Can not reach service %s at host \"%s\"."),
161 (const char*)service, (const char*) client.getHost());
162 freeaddrinfo(ai);
163 return (false);
164 }
165
166 bool
167 InetTransport::initDataConn(fxStr& emsg)
168 {
169 struct sockaddr data_addr;
170 socklen_t dlen = sizeof (data_addr);
171
172 if (Socket::getsockname(fileno(client.getCtrlFd()), &data_addr, &dlen) < 0) {
173 emsg = fxStr::format("getsockname(ctrl): %s", strerror(errno));
174 return (false);
175 }
176
177 return initDataConnV6(emsg);
178 }
179
180 bool
181 InetTransport::initDataConnV4(fxStr& emsg)
182 {
183 struct sockaddr_in data_addr;
184 socklen_t dlen = sizeof (data_addr);
185 if (client.isPassive()) {
186 if (client.command("PASV") != FaxClient::COMPLETE)
187 return (false);
188 const char *cp = strchr(client.getLastResponse(), '(');
189 if (!cp) return (false);
190 cp++;
191 unsigned int v[6];
192 int n = sscanf(cp, "%u,%u,%u,%u,%u,%u", &v[2],&v[3],&v[4],&v[5],&v[0],&v[1]);
193 if (n != 6) return (false);
194 if (!inet_aton(fxStr::format("%u.%u.%u.%u", v[2],v[3],v[4],v[5]), &data_addr.sin_addr)) {
195 return (false);
196 }
197 data_addr.sin_port = htons((v[0]<<8)+v[1]);
198 data_addr.sin_family = AF_INET;
199 } else {
200 if (Socket::getsockname(fileno(client.getCtrlFd()), &data_addr, &dlen) < 0) {
201 emsg = fxStr::format("getsockname(ctrl): %s", strerror(errno));
202 return (false);
203 }
204 data_addr.sin_port = 0; // let system allocate port
205 }
206 int fd = socket(AF_INET, SOCK_STREAM, 0);
207 if (fd < 0) {
208 emsg = fxStr::format("socket: %s", strerror(errno));
209 return (false);
210 }
211 if (client.isPassive()) {
212 if (Socket::connect(fd, &data_addr, sizeof (data_addr)) >= 0) {
213 if (client.getVerbose())
214 client.traceServer("Connected to %s at port %u.",
215 inet_ntoa(data_addr.sin_addr), ntohs(data_addr.sin_port));
216 } else {
217 emsg = fxStr::format("Can not reach server at %s at port %u (%s).",
218 inet_ntoa(data_addr.sin_addr), ntohs(data_addr.sin_port), strerror(errno));
219 goto bad;
220 }
221 } else {
222 if (Socket::bind(fd, &data_addr, sizeof (data_addr)) < 0) {
223 emsg = fxStr::format("bind: %s", strerror(errno));
224 goto bad;
225 }
226 dlen = sizeof (data_addr);
227 if (Socket::getsockname(fd, &data_addr, &dlen) < 0) {
228 emsg = fxStr::format("getsockname: %s", strerror(errno));
229 goto bad;
230 }
231 if (listen(fd, 1) < 0) {
232 emsg = fxStr::format("listen: %s", strerror(errno));
233 goto bad;
234 }
235 const char* a; a = (const char*) &data_addr.sin_addr; // XXX for __GNUC__
236 const char* p; p = (const char*) &data_addr.sin_port; // XXX for __GNUC__
237 #define UC(b) (((int) b) & 0xff)
238 if (client.command("PORT %u,%u,%u,%u,%u,%u",
239 UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
240 UC(p[0]), UC(p[1])) != FaxClient::COMPLETE)
241 return (false);
242 #undef UC
243 }
244 client.setDataFd(fd);
245 return (true);
246 bad:
247 Sys::close(fd), fd = -1;
248 return (false);
249 }
250
251 bool
252 InetTransport::initDataConnV6(fxStr& emsg)
253 {
254 /*
255 * IPv6 extensions to FTP
256 * http://www.ietf.org/rfc/rfc2428.txt
257 */
258 char buf[1024];
259 Socket::Address data_addr;
260 socklen_t dlen = sizeof(data_addr);
261
262
263 if (client.isPassive()) {
264 if (Socket::getpeername(fileno(client.getCtrlFd()), &data_addr, &dlen) < 0) {
265 emsg = fxStr::format("getsockname(ctrl): %s", strerror(errno));
266 return (false);
267 }
268 int err = client.command("EPSV");
269 if (err == FaxClient::COMPLETE) {
270 u_int s = client.getLastResponse().next(0, '(');
271 u_int e = client.getLastResponse().next(s, ')');
272 if (s++ < e && e < client.getLastResponse().length()) {
273 fxStr p = client.getLastResponse().extract(s, e-s);
274 char c = p[0];
275 if (p[0] == c && p[1] == c && p[2] == c && p[p.length()-1] == c)
276 Socket::port(data_addr) = htons(p.extract(3, p.length()-4));
277 else {
278 client.printWarning(NLS::TEXT("Couldn't parse last response \"%s\""), (const char*)client.getLastResponse());
279 return(false);
280 }
281 } else {
282 client.printWarning(NLS::TEXT("Couldn't parse last response \"%s\""), (const char*)client.getLastResponse());
283 return(false);
284 }
285 } else if (err == FaxClient::ERROR && Socket::family(data_addr) == AF_INET) {
286
287 client.printWarning(NLS::TEXT("EPSV not supported, trying PASV since we're AF_INET\n"));
288 if (client.command("PASV") != FaxClient::COMPLETE)
289 return false;
290 const char *cp = strchr(client.getLastResponse(), '(');
291 if (!cp) {
292 client.printWarning(NLS::TEXT("Couldn't parse last response \"%s\""), (const char*)client.getLastResponse());
293 return (false);
294 }
295 cp++;
296 unsigned int v[6];
297 int n = sscanf(cp, "%u,%u,%u,%u,%u,%u", &v[2],&v[3],&v[4],&v[5],&v[0],&v[1]);
298 if (n != 6) return (false);
299 if (!inet_aton(fxStr::format("%u.%u.%u.%u", v[2],v[3],v[4],v[5]), &data_addr.in.sin_addr)) {
300 return (false);
301 }
302 data_addr.in.sin_port = htons((v[0]<<8)+v[1]);
303 data_addr.in.sin_family = AF_INET;
304 dlen = sizeof(data_addr.in);
305 }
306 } else {
307 if (Socket::getsockname(fileno(client.getCtrlFd()), &data_addr, &dlen) < 0) {
308 emsg = fxStr::format("getsockname(ctrl): %s", strerror(errno));
309 return (false);
310 }
311 Socket::port(data_addr) = 0; // let system allocate port
312 }
313
314 int fd = socket(Socket::family(data_addr), SOCK_STREAM, IPPROTO_TCP);
315 if (fd < 0) {
316 emsg = fxStr::format("socket: %s", strerror(errno));
317 return (false);
318 }
319 if (client.isPassive()) {
320 if (Socket::connect(fd, &data_addr.in, Socket::socklen(data_addr)) >= 0) {
321 if (client.getVerbose())
322 client.traceServer("Connected to %s at port %u.",
323 inet_ntop(Socket::family(data_addr), Socket::addr(data_addr), buf, sizeof(buf)), ntohs(Socket::port(data_addr)));
324 } else {
325 emsg = fxStr::format("Can not reach server at %s at port %u (%s).",
326 inet_ntop(Socket::family(data_addr), Socket::addr(data_addr), buf, sizeof(buf)), ntohs(Socket::port(data_addr)), strerror(errno));
327 goto bad;
328 }
329 } else {
330 if (Socket::bind(fd, &data_addr, dlen) < 0) {
331 emsg = fxStr::format("bind: %s", strerror(errno));
332 goto bad;
333 }
334 dlen = sizeof (data_addr);
335 if (Socket::getsockname(fd, &data_addr, &dlen) < 0) {
336 emsg = fxStr::format("getsockname: %s", strerror(errno));
337 goto bad;
338 }
339 if (listen(fd, 1) < 0) {
340 emsg = fxStr::format("listen: %s", strerror(errno));
341 goto bad;
342 }
343
344 char hostbuf[128];
345 char portbuf[64];
346
347 getnameinfo((struct sockaddr*)&data_addr, dlen,
348 hostbuf, sizeof(hostbuf), portbuf, sizeof(portbuf),
349 NI_NUMERICHOST | NI_NUMERICSERV);
350 int err = client.command("EPRT |%d|%s|%s|",
351 (Socket::family(data_addr) == AF_INET6 ? 2 : 1),
352 hostbuf, portbuf);
353 if (err == FaxClient::ERROR && Socket::family(data_addr) == AF_INET)
354 {
355 if (client.getVerbose())
356 client.printWarning(NLS::TEXT("EPRT not supported, trying PORT"));
357 const char* a; a = (const char*)&data_addr.in.sin_addr; // XXX for __GNUC__
358 const char* p; p = (const char*)&data_addr.in.sin_port; // XXX for __GNUC__
359 #define UC(b) (((int) b) & 0xff)
360 err = client.command("PORT %u,%u,%u,%u,%u,%u",
361 UC(a[0]), UC(a[1]), UC(a[2]), UC(a[3]),
362 UC(p[0]), UC(p[1]));
363 #undef UC
364 }
365 if (err != FaxClient::COMPLETE)
366 return false;
367 }
368
369 client.setDataFd(fd);
370 return (true);
371 bad:
372 Sys::close(fd), fd = -1;
373 return (false);
374 }
375
376
377 bool
378 InetTransport::openDataConn(fxStr& emsg)
379 {
380 if (client.isPassive()) {
381 return (client.getDataFd() > 0);
382 }
383 int s = Socket::accept(client.getDataFd(), NULL, NULL);
384 if (s >= 0) {
385 client.setDataFd(s);
386 #if defined(IP_TOS) && defined(IPTOS_THROUGHPUT)
387 int tos = IPTOS_THROUGHPUT;
388 if (Socket::setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof (tos)) < 0)
389 client.printWarning("setsockopt(IP_TOS): %s", strerror(errno));
390 #endif
391 return (true);
392 } else {
393 emsg = fxStr::format("accept: %s", strerror(errno));
394 return (false);
395 }
396 }
397
398 /*
399 * Send an abort request to terminate an operation.
400 * The initial interrupt is sent as urgent data to
401 * cause the server to process the subsequent ABOR
402 * command immediately. We send IAC in urgent mode
403 * instead of DM because 4.3BSD place the out-of-band
404 * mark in the data stream after the urgent byte
405 * rather than before as done by most contemporary
406 * TCP implementations.
407 */
408 bool
409 InetTransport::abortCmd(fxStr& emsg)
410 {
411 static const u_char msg[] =
412 { IAC, IP, IAC, DM, 'A', 'B', 'O', 'R', '\r', '\n' };
413 int s = fileno(client.getCtrlFd());
414 if (send(s, msg, 3, MSG_OOB) != 3) {
415 emsg = fxStr::format("send(MSG_OOB): %s", strerror(errno));
416 return (false);
417 }
418 if (send(s, msg+3, sizeof (msg)-3, 0) != sizeof (msg)-3) {
419 emsg = fxStr::format("send(<DM>ABOR\\r\\n): %s", strerror(errno));
420 return (false);
421 }
422 return (true);
423 }
424 #else
425 void
426 Transport::notConfigured(fxStr& emsg)
427 {
428 emsg = NLS::TEXT("Sorry, no TCP/IP communication support was configured.");
429 }
430
431 bool InetTransport::callServer(fxStr& emsg)
432 { notConfigured(emsg); return (false); }
433 bool InetTransport::initDataConn(fxStr& emsg)
434 { notConfigured(emsg); return (false); }
435 bool InetTransport::openDataConn(fxStr& emsg)
436 { notConfigured(emsg); return (false); }
437 bool InetTransport::abortDataConn(fxStr& emsg)
438 { notConfigured(emsg); return (false); }
439 #endif