Remove unwanted underscore.
[sgt/putty] / unix / uxnet.c
CommitLineData
c5e438ec 1/*
2 * Unix networking abstraction.
3 */
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <assert.h>
8#include <errno.h>
9#include <fcntl.h>
10#include <unistd.h>
11#include <sys/types.h>
12#include <sys/socket.h>
13#include <sys/ioctl.h>
14#include <arpa/inet.h>
15#include <netinet/in.h>
16#include <netinet/tcp.h>
17#include <netdb.h>
fc0f17db 18#include <sys/un.h>
c5e438ec 19
20#define DEFINE_PLUG_METHOD_MACROS
21#include "putty.h"
22#include "network.h"
23#include "tree234.h"
24
fc0f17db 25#ifndef X11_UNIX_PATH
26# define X11_UNIX_PATH "/tmp/.X11-unix/X"
27#endif
28
6ee9b735 29#define ipv4_is_loopback(addr) (inet_netof(addr) == IN_LOOPBACKNET)
30
c5e438ec 31struct Socket_tag {
32 struct socket_function_table *fn;
33 /* the above variable absolutely *must* be the first in this structure */
cbe2d68f 34 const char *error;
c5e438ec 35 int s;
36 Plug plug;
37 void *private_ptr;
38 bufchain output_data;
39 int connected;
40 int writable;
41 int frozen; /* this causes readability notifications to be ignored */
42 int frozen_readable; /* this means we missed at least one readability
43 * notification while we were frozen */
44 int localhost_only; /* for listening sockets */
45 char oobdata[1];
46 int sending_oob;
47 int oobpending; /* is there OOB data available to read? */
48 int oobinline;
49 int pending_error; /* in case send() returns error */
50 int listener;
51};
52
53/*
54 * We used to typedef struct Socket_tag *Socket.
55 *
56 * Since we have made the networking abstraction slightly more
57 * abstract, Socket no longer means a tcp socket (it could mean
58 * an ssl socket). So now we must use Actual_Socket when we know
59 * we are talking about a tcp socket.
60 */
61typedef struct Socket_tag *Actual_Socket;
62
63struct SockAddr_tag {
cbe2d68f 64 const char *error;
b7a189f3 65 /*
66 * Which address family this address belongs to. AF_INET for
67 * IPv4; AF_INET6 for IPv6; AF_UNSPEC indicates that name
68 * resolution has not been done and a simple host name is held
69 * in this SockAddr structure.
70 */
c5e438ec 71 int family;
05581745 72#ifndef NO_IPV6
c5e438ec 73 struct addrinfo *ai; /* Address IPv6 style. */
792c5eb5 74#else
75 unsigned long address; /* Address IPv4 style. */
c5e438ec 76#endif
b7a189f3 77 char hostname[512]; /* Store an unresolved host name. */
c5e438ec 78};
79
80static tree234 *sktree;
81
0ff9ea38 82static void uxsel_tell(Actual_Socket s);
83
c5e438ec 84static int cmpfortree(void *av, void *bv)
85{
86 Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv;
87 int as = a->s, bs = b->s;
88 if (as < bs)
89 return -1;
90 if (as > bs)
91 return +1;
92 return 0;
93}
94
95static int cmpforsearch(void *av, void *bv)
96{
97 Actual_Socket b = (Actual_Socket) bv;
f7f9fb5c 98 int as = *(int *)av, bs = b->s;
c5e438ec 99 if (as < bs)
100 return -1;
101 if (as > bs)
102 return +1;
103 return 0;
104}
105
106void sk_init(void)
107{
108 sktree = newtree234(cmpfortree);
109}
110
111void sk_cleanup(void)
112{
113 Actual_Socket s;
114 int i;
115
116 if (sktree) {
117 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
118 close(s->s);
119 }
120 }
121}
122
cbe2d68f 123const char *error_string(int error)
c5e438ec 124{
e266735f 125 return strerror(error);
c5e438ec 126}
127
05581745 128SockAddr sk_namelookup(const char *host, char **canonicalname, int address_family)
c5e438ec 129{
3d88e64d 130 SockAddr ret = snew(struct SockAddr_tag);
05581745 131#ifndef NO_IPV6
792c5eb5 132 struct addrinfo hints;
133 int err;
134#else
c5e438ec 135 unsigned long a;
136 struct hostent *h = NULL;
792c5eb5 137#endif
c5e438ec 138 char realhost[8192];
139
140 /* Clear the structure and default to IPv4. */
141 memset(ret, 0, sizeof(struct SockAddr_tag));
142 ret->family = 0; /* We set this one when we have resolved the host. */
143 *realhost = '\0';
144 ret->error = NULL;
145
05581745 146#ifndef NO_IPV6
792c5eb5 147 hints.ai_flags = AI_CANONNAME;
05581745 148 hints.ai_family = address_family;
792c5eb5 149 hints.ai_socktype = 0;
150 hints.ai_protocol = 0;
151 hints.ai_addrlen = 0;
152 hints.ai_addr = NULL;
153 hints.ai_canonname = NULL;
154 hints.ai_next = NULL;
72b364a8 155 err = getaddrinfo(host, NULL, &hints, &ret->ai);
792c5eb5 156 if (err != 0) {
157 ret->error = gai_strerror(err);
158 return ret;
159 }
160 ret->family = ret->ai->ai_family;
161 *realhost = '\0';
162 if (ret->ai->ai_canonname != NULL)
163 strncat(realhost, ret->ai->ai_canonname, sizeof(realhost) - 1);
164 else
165 strncat(realhost, host, sizeof(realhost) - 1);
166#else
167 if ((a = inet_addr(host)) == (unsigned long) INADDR_NONE) {
168 /*
169 * Otherwise use the IPv4-only gethostbyname... (NOTE:
170 * we don't use gethostbyname as a fallback!)
171 */
172 if (ret->family == 0) {
c5e438ec 173 /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
174 if ( (h = gethostbyname(host)) )
792c5eb5 175 ret->family = AF_INET;
176 }
177 if (ret->family == 0) {
c5e438ec 178 ret->error = (h_errno == HOST_NOT_FOUND ||
792c5eb5 179 h_errno == NO_DATA ||
180 h_errno == NO_ADDRESS ? "Host does not exist" :
181 h_errno == TRY_AGAIN ?
182 "Temporary name service failure" :
183 "gethostbyname: unknown error");
8c43874c 184 return ret;
c5e438ec 185 }
792c5eb5 186 memcpy(&a, h->h_addr, sizeof(a));
187 /* This way we are always sure the h->h_name is valid :) */
188 strncpy(realhost, h->h_name, sizeof(realhost));
c5e438ec 189 } else {
190 /*
191 * This must be a numeric IPv4 address because it caused a
192 * success return from inet_addr.
193 */
194 ret->family = AF_INET;
195 strncpy(realhost, host, sizeof(realhost));
196 }
197 ret->address = ntohl(a);
792c5eb5 198#endif
c5e438ec 199 realhost[lenof(realhost)-1] = '\0';
3d88e64d 200 *canonicalname = snewn(1+strlen(realhost), char);
c5e438ec 201 strcpy(*canonicalname, realhost);
202 return ret;
203}
204
e8fa8f62 205SockAddr sk_nonamelookup(const char *host)
b7a189f3 206{
3d88e64d 207 SockAddr ret = snew(struct SockAddr_tag);
ab0873ab 208 ret->error = NULL;
b7a189f3 209 ret->family = AF_UNSPEC;
210 strncpy(ret->hostname, host, lenof(ret->hostname));
211 ret->hostname[lenof(ret->hostname)-1] = '\0';
e14d27a1 212#ifndef NO_IPV6
213 ret->ai = NULL;
214#endif
b7a189f3 215 return ret;
216}
217
c5e438ec 218void sk_getaddr(SockAddr addr, char *buf, int buflen)
219{
792c5eb5 220
221 if (addr->family == AF_UNSPEC) {
222 strncpy(buf, addr->hostname, buflen);
223 buf[buflen-1] = '\0';
224 } else {
05581745 225#ifndef NO_IPV6
792c5eb5 226 if (getnameinfo(addr->ai->ai_addr, addr->ai->ai_addrlen, buf, buflen,
227 NULL, 0, NI_NUMERICHOST) != 0) {
228 buf[0] = '\0';
229 strncat(buf, "<unknown>", buflen - 1);
230 }
231#else
c5e438ec 232 struct in_addr a;
792c5eb5 233 assert(addr->family == AF_INET);
c5e438ec 234 a.s_addr = htonl(addr->address);
235 strncpy(buf, inet_ntoa(a), buflen);
b7a189f3 236 buf[buflen-1] = '\0';
792c5eb5 237#endif
c5e438ec 238 }
c5e438ec 239}
240
b804e1e5 241int sk_hostname_is_local(char *name)
242{
243 return !strcmp(name, "localhost");
244}
245
246int sk_address_is_local(SockAddr addr)
247{
792c5eb5 248
249 if (addr->family == AF_UNSPEC)
250 return 0; /* we don't know; assume not */
251 else {
05581745 252#ifndef NO_IPV6
792c5eb5 253 if (addr->family == AF_INET)
254 return ipv4_is_loopback(
255 ((struct sockaddr_in *)addr->ai->ai_addr)->sin_addr);
256 else if (addr->family == AF_INET6)
257 return IN6_IS_ADDR_LOOPBACK(
258 &((struct sockaddr_in6 *)addr->ai->ai_addr)->sin6_addr);
259 else
260 return 0;
261#else
b804e1e5 262 struct in_addr a;
792c5eb5 263 assert(addr->family == AF_INET);
b804e1e5 264 a.s_addr = htonl(addr->address);
265 return ipv4_is_loopback(a);
792c5eb5 266#endif
b804e1e5 267 }
b804e1e5 268}
269
c5e438ec 270int sk_addrtype(SockAddr addr)
271{
b7a189f3 272 return (addr->family == AF_INET ? ADDRTYPE_IPV4 :
05581745 273#ifndef NO_IPV6
b7a189f3 274 addr->family == AF_INET6 ? ADDRTYPE_IPV6 :
275#endif
276 ADDRTYPE_NAME);
c5e438ec 277}
278
279void sk_addrcopy(SockAddr addr, char *buf)
280{
792c5eb5 281
05581745 282#ifndef NO_IPV6
792c5eb5 283 if (addr->family == AF_INET)
284 memcpy(buf, &((struct sockaddr_in *)addr->ai->ai_addr)->sin_addr,
285 sizeof(struct in_addr));
286 else if (addr->family == AF_INET6)
287 memcpy(buf, &((struct sockaddr_in6 *)addr->ai->ai_addr)->sin6_addr,
288 sizeof(struct in6_addr));
289 else
290 assert(FALSE);
291#else
292 struct in_addr a;
293
294 assert(addr->family == AF_INET);
295 a.s_addr = htonl(addr->address);
296 memcpy(buf, (char*) &a.s_addr, 4);
c5e438ec 297#endif
c5e438ec 298}
299
300void sk_addr_free(SockAddr addr)
301{
792c5eb5 302
05581745 303#ifndef NO_IPV6
792c5eb5 304 if (addr->ai != NULL)
305 freeaddrinfo(addr->ai);
306#endif
c5e438ec 307 sfree(addr);
308}
309
310static Plug sk_tcp_plug(Socket sock, Plug p)
311{
312 Actual_Socket s = (Actual_Socket) sock;
313 Plug ret = s->plug;
314 if (p)
315 s->plug = p;
316 return ret;
317}
318
319static void sk_tcp_flush(Socket s)
320{
321 /*
322 * We send data to the socket as soon as we can anyway,
323 * so we don't need to do anything here. :-)
324 */
325}
326
327static void sk_tcp_close(Socket s);
e0e7dff8 328static int sk_tcp_write(Socket s, const char *data, int len);
329static int sk_tcp_write_oob(Socket s, const char *data, int len);
c5e438ec 330static void sk_tcp_set_private_ptr(Socket s, void *ptr);
331static void *sk_tcp_get_private_ptr(Socket s);
332static void sk_tcp_set_frozen(Socket s, int is_frozen);
cbe2d68f 333static const char *sk_tcp_socket_error(Socket s);
c5e438ec 334
2f92b717 335static struct socket_function_table tcp_fn_table = {
336 sk_tcp_plug,
337 sk_tcp_close,
338 sk_tcp_write,
339 sk_tcp_write_oob,
340 sk_tcp_flush,
341 sk_tcp_set_private_ptr,
342 sk_tcp_get_private_ptr,
343 sk_tcp_set_frozen,
344 sk_tcp_socket_error
345};
346
f7f9fb5c 347Socket sk_register(OSSocket sockfd, Plug plug)
c5e438ec 348{
c5e438ec 349 Actual_Socket ret;
350
351 /*
352 * Create Socket structure.
353 */
3d88e64d 354 ret = snew(struct Socket_tag);
2f92b717 355 ret->fn = &tcp_fn_table;
c5e438ec 356 ret->error = NULL;
357 ret->plug = plug;
358 bufchain_init(&ret->output_data);
359 ret->writable = 1; /* to start with */
360 ret->sending_oob = 0;
361 ret->frozen = 1;
362 ret->frozen_readable = 0;
363 ret->localhost_only = 0; /* unused, but best init anyway */
364 ret->pending_error = 0;
365 ret->oobpending = FALSE;
366 ret->listener = 0;
367
f7f9fb5c 368 ret->s = sockfd;
c5e438ec 369
370 if (ret->s < 0) {
371 ret->error = error_string(errno);
372 return (Socket) ret;
373 }
374
375 ret->oobinline = 0;
376
0ff9ea38 377 uxsel_tell(ret);
c5e438ec 378 add234(sktree, ret);
379
380 return (Socket) ret;
381}
382
383Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
79bf227b 384 int nodelay, int keepalive, Plug plug)
c5e438ec 385{
c5e438ec 386 int s;
05581745 387#ifndef NO_IPV6
c5e438ec 388 struct sockaddr_in6 a6;
389#endif
390 struct sockaddr_in a;
fc0f17db 391 struct sockaddr_un au;
392 const struct sockaddr *sa;
c5e438ec 393 int err;
394 Actual_Socket ret;
395 short localport;
fc0f17db 396 int fl, salen;
c5e438ec 397
398 /*
399 * Create Socket structure.
400 */
3d88e64d 401 ret = snew(struct Socket_tag);
2f92b717 402 ret->fn = &tcp_fn_table;
c5e438ec 403 ret->error = NULL;
404 ret->plug = plug;
405 bufchain_init(&ret->output_data);
406 ret->connected = 0; /* to start with */
407 ret->writable = 0; /* to start with */
408 ret->sending_oob = 0;
409 ret->frozen = 0;
410 ret->frozen_readable = 0;
411 ret->localhost_only = 0; /* unused, but best init anyway */
412 ret->pending_error = 0;
413 ret->oobpending = FALSE;
414 ret->listener = 0;
415
416 /*
417 * Open socket.
418 */
b7a189f3 419 assert(addr->family != AF_UNSPEC);
c5e438ec 420 s = socket(addr->family, SOCK_STREAM, 0);
421 ret->s = s;
422
423 if (s < 0) {
424 ret->error = error_string(errno);
425 return (Socket) ret;
426 }
427
428 ret->oobinline = oobinline;
429 if (oobinline) {
430 int b = TRUE;
431 setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));
432 }
433
434 if (nodelay) {
435 int b = TRUE;
436 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *) &b, sizeof(b));
437 }
438
79bf227b 439 if (keepalive) {
440 int b = TRUE;
441 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *) &b, sizeof(b));
442 }
443
c5e438ec 444 /*
445 * Bind to local address.
446 */
447 if (privport)
448 localport = 1023; /* count from 1023 downwards */
449 else
450 localport = 0; /* just use port 0 (ie kernel picks) */
451
d2c29274 452 /* BSD IP stacks need sockaddr_in zeroed before filling in */
453 memset(&a,'\0',sizeof(struct sockaddr_in));
05581745 454#ifndef NO_IPV6
d2c29274 455 memset(&a6,'\0',sizeof(struct sockaddr_in6));
456#endif
fc0f17db 457
458 /* We don't try to bind to a local address for UNIX domain sockets. (Why
459 * do we bother doing the bind when localport == 0 anyway?) */
460 if(addr->family != AF_UNIX) {
461 /* Loop round trying to bind */
462 while (1) {
463 int retcode;
c5e438ec 464
05581745 465#ifndef NO_IPV6
fc0f17db 466 if (addr->family == AF_INET6) {
467 /* XXX use getaddrinfo to get a local address? */
468 a6.sin6_family = AF_INET6;
469 a6.sin6_addr = in6addr_any;
470 a6.sin6_port = htons(localport);
471 retcode = bind(s, (struct sockaddr *) &a6, sizeof(a6));
472 } else
c5e438ec 473#endif
fc0f17db 474 {
475 assert(addr->family == AF_INET);
476 a.sin_family = AF_INET;
477 a.sin_addr.s_addr = htonl(INADDR_ANY);
478 a.sin_port = htons(localport);
479 retcode = bind(s, (struct sockaddr *) &a, sizeof(a));
480 }
481 if (retcode >= 0) {
482 err = 0;
483 break; /* done */
484 } else {
485 err = errno;
486 if (err != EADDRINUSE) /* failed, for a bad reason */
487 break;
488 }
489
490 if (localport == 0)
491 break; /* we're only looping once */
492 localport--;
493 if (localport == 0)
494 break; /* we might have got to the end */
c5e438ec 495 }
fc0f17db 496
497 if (err) {
498 ret->error = error_string(err);
499 return (Socket) ret;
c5e438ec 500 }
c5e438ec 501 }
502
503 /*
504 * Connect to remote address.
505 */
fc0f17db 506 switch(addr->family) {
05581745 507#ifndef NO_IPV6
fc0f17db 508 case AF_INET:
509 /* XXX would be better to have got getaddrinfo() to fill in the port. */
792c5eb5 510 ((struct sockaddr_in *)addr->ai->ai_addr)->sin_port =
511 htons(port);
fc0f17db 512 sa = (const struct sockaddr *)addr->ai->ai_addr;
513 salen = addr->ai->ai_addrlen;
514 break;
515 case AF_INET6:
792c5eb5 516 ((struct sockaddr_in *)addr->ai->ai_addr)->sin_port =
517 htons(port);
fc0f17db 518 sa = (const struct sockaddr *)addr->ai->ai_addr;
519 salen = addr->ai->ai_addrlen;
520 break;
792c5eb5 521#else
fc0f17db 522 case AF_INET:
523 a.sin_family = AF_INET;
524 a.sin_addr.s_addr = htonl(addr->address);
525 a.sin_port = htons((short) port);
526 sa = (const struct sockaddr *)&a;
527 salen = sizeof a;
528 break;
792c5eb5 529#endif
fc0f17db 530 case AF_UNIX:
531 assert(port == 0); /* to catch confused people */
532 assert(strlen(addr->hostname) < sizeof au.sun_path);
533 memset(&au, 0, sizeof au);
534 au.sun_family = AF_UNIX;
535 strcpy(au.sun_path, addr->hostname);
536 sa = (const struct sockaddr *)&au;
537 salen = sizeof au;
538 break;
539
540 default:
541 assert(0 && "unknown address family");
542 }
4b70e70e 543
544 fl = fcntl(s, F_GETFL);
545 if (fl != -1)
546 fcntl(s, F_SETFL, fl | O_NONBLOCK);
051dd789 547
fc0f17db 548 if ((connect(s, sa, salen)) < 0) {
051dd789 549 if ( errno != EINPROGRESS ) {
c5e438ec 550 ret->error = error_string(errno);
551 return (Socket) ret;
552 }
553 } else {
554 /*
555 * If we _don't_ get EWOULDBLOCK, the connect has completed
556 * and we should set the socket as connected and writable.
557 */
558 ret->connected = 1;
559 ret->writable = 1;
560 }
561
0ff9ea38 562 uxsel_tell(ret);
c5e438ec 563 add234(sktree, ret);
564
f85e6f6e 565 sk_addr_free(addr);
566
c5e438ec 567 return (Socket) ret;
568}
569
05581745 570Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only, int address_family)
c5e438ec 571{
c5e438ec 572 int s;
05581745 573#ifndef NO_IPV6
792c5eb5 574 struct addrinfo hints, *ai;
575 char portstr[6];
576#endif
c5e438ec 577 struct sockaddr_in a;
578 int err;
579 Actual_Socket ret;
580 int retcode;
581 int on = 1;
582
583 /*
584 * Create Socket structure.
585 */
3d88e64d 586 ret = snew(struct Socket_tag);
2f92b717 587 ret->fn = &tcp_fn_table;
c5e438ec 588 ret->error = NULL;
589 ret->plug = plug;
590 bufchain_init(&ret->output_data);
591 ret->writable = 0; /* to start with */
592 ret->sending_oob = 0;
593 ret->frozen = 0;
594 ret->frozen_readable = 0;
595 ret->localhost_only = local_host_only;
596 ret->pending_error = 0;
597 ret->oobpending = FALSE;
598 ret->listener = 1;
599
600 /*
05581745 601 * Translate address_family from platform-independent constants
602 * into local reality.
603 */
604 address_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
605 address_family == ADDRTYPE_IPV6 ? AF_INET6 : AF_UNSPEC);
606
607#ifndef NO_IPV6
608 /* Let's default to IPv6.
609 * If the stack doesn't support IPv6, we will fall back to IPv4. */
610 if (address_family == AF_UNSPEC) address_family = AF_INET6;
611#else
612 /* No other choice, default to IPv4 */
613 if (address_family == AF_UNSPEC) address_family = AF_INET;
614#endif
615
616 /*
c5e438ec 617 * Open socket.
618 */
05581745 619 s = socket(address_family, SOCK_STREAM, 0);
620
621 /* If the host doesn't support IPv6 try fallback to IPv4. */
622 if (s < 0 && address_family == AF_INET6) {
623 address_family = AF_INET;
624 s = socket(address_family, SOCK_STREAM, 0);
625 }
c5e438ec 626 ret->s = s;
627
628 if (s < 0) {
629 ret->error = error_string(errno);
630 return (Socket) ret;
631 }
632
633 ret->oobinline = 0;
634
635 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
636
d2c29274 637 /* BSD IP stacks need sockaddr_in zeroed before filling in */
638 memset(&a,'\0',sizeof(struct sockaddr_in));
05581745 639#ifndef NO_IPV6
d2c29274 640#if 0
641 memset(&a6,'\0',sizeof(struct sockaddr_in6));
642#endif
792c5eb5 643 hints.ai_flags = AI_NUMERICHOST;
05581745 644 hints.ai_family = address_family;
792c5eb5 645 hints.ai_socktype = 0;
646 hints.ai_protocol = 0;
647 hints.ai_addrlen = 0;
648 hints.ai_addr = NULL;
649 hints.ai_canonname = NULL;
650 hints.ai_next = NULL;
651 sprintf(portstr, "%d", port);
652 if (srcaddr != NULL && getaddrinfo(srcaddr, portstr, &hints, &ai) == 0)
653 retcode = bind(s, ai->ai_addr, ai->ai_addrlen);
654 else
655#if 0
656 {
657 /*
658 * FIXME: Need two listening sockets, in principle, one for v4
659 * and one for v6
660 */
c5e438ec 661 if (local_host_only)
662 a6.sin6_addr = in6addr_loopback;
663 else
664 a6.sin6_addr = in6addr_any;
665 a6.sin6_port = htons(port);
666 } else
667#endif
792c5eb5 668#endif
c5e438ec 669 {
6ee9b735 670 int got_addr = 0;
c5e438ec 671 a.sin_family = AF_INET;
6ee9b735 672
673 /*
674 * Bind to source address. First try an explicitly
675 * specified one...
676 */
677 if (srcaddr) {
678 a.sin_addr.s_addr = inet_addr(srcaddr);
679 if (a.sin_addr.s_addr != INADDR_NONE) {
680 /* Override localhost_only with specified listen addr. */
681 ret->localhost_only = ipv4_is_loopback(a.sin_addr);
682 got_addr = 1;
683 }
684 }
685
686 /*
687 * ... and failing that, go with one of the standard ones.
688 */
689 if (!got_addr) {
690 if (local_host_only)
691 a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
692 else
693 a.sin_addr.s_addr = htonl(INADDR_ANY);
694 }
695
c5e438ec 696 a.sin_port = htons((short)port);
792c5eb5 697 retcode = bind(s, (struct sockaddr *) &a, sizeof(a));
c5e438ec 698 }
792c5eb5 699
c5e438ec 700 if (retcode >= 0) {
701 err = 0;
702 } else {
703 err = errno;
704 }
705
706 if (err) {
707 ret->error = error_string(err);
708 return (Socket) ret;
709 }
710
711
712 if (listen(s, SOMAXCONN) < 0) {
713 close(s);
714 ret->error = error_string(errno);
715 return (Socket) ret;
716 }
717
0ff9ea38 718 uxsel_tell(ret);
c5e438ec 719 add234(sktree, ret);
720
721 return (Socket) ret;
722}
723
724static void sk_tcp_close(Socket sock)
725{
726 Actual_Socket s = (Actual_Socket) sock;
727
0ff9ea38 728 uxsel_del(s->s);
c5e438ec 729 del234(sktree, s);
730 close(s->s);
731 sfree(s);
732}
733
2f92b717 734int sk_getxdmdata(void *sock, unsigned long *ip, int *port)
735{
736 Actual_Socket s = (Actual_Socket) sock;
737 struct sockaddr_in addr;
738 socklen_t addrlen;
739
740 /*
741 * We must check that this socket really _is_ an Actual_Socket.
742 */
743 if (s->fn != &tcp_fn_table)
744 return 0; /* failure */
745
2f92b717 746 addrlen = sizeof(addr);
fc0f17db 747 if (getsockname(s->s, (struct sockaddr *)&addr, &addrlen) < 0)
2f92b717 748 return 0;
fc0f17db 749 switch(addr.sin_family) {
750 case AF_INET:
751 *ip = ntohl(addr.sin_addr.s_addr);
752 *port = ntohs(addr.sin_port);
753 break;
754 case AF_UNIX:
755 /*
756 * For a Unix socket, we return 0xFFFFFFFF for the IP address and
757 * our current pid for the port. Bizarre, but such is life.
758 */
759 *ip = ntohl(0xFFFFFFFF);
760 *port = getpid();
761 break;
2f92b717 762
fc0f17db 763 /* XXX IPV6 */
764
765 default:
766 return 0;
767 }
2f92b717 768
769 return 1;
770}
771
c5e438ec 772/*
773 * The function which tries to send on a socket once it's deemed
774 * writable.
775 */
776void try_send(Actual_Socket s)
777{
778 while (s->sending_oob || bufchain_size(&s->output_data) > 0) {
779 int nsent;
780 int err;
781 void *data;
782 int len, urgentflag;
783
784 if (s->sending_oob) {
785 urgentflag = MSG_OOB;
786 len = s->sending_oob;
787 data = &s->oobdata;
788 } else {
789 urgentflag = 0;
790 bufchain_prefix(&s->output_data, &data, &len);
791 }
792 nsent = send(s->s, data, len, urgentflag);
793 noise_ultralight(nsent);
794 if (nsent <= 0) {
795 err = (nsent < 0 ? errno : 0);
796 if (err == EWOULDBLOCK) {
797 /*
798 * Perfectly normal: we've sent all we can for the moment.
799 */
800 s->writable = FALSE;
801 return;
802 } else if (nsent == 0 ||
803 err == ECONNABORTED || err == ECONNRESET) {
804 /*
805 * If send() returns CONNABORTED or CONNRESET, we
806 * unfortunately can't just call plug_closing(),
807 * because it's quite likely that we're currently
808 * _in_ a call from the code we'd be calling back
809 * to, so we'd have to make half the SSH code
810 * reentrant. Instead we flag a pending error on
811 * the socket, to be dealt with (by calling
812 * plug_closing()) at some suitable future moment.
813 */
814 s->pending_error = err;
815 return;
816 } else {
817 /* We're inside the Unix frontend here, so we know
818 * that the frontend handle is unnecessary. */
819 logevent(NULL, error_string(err));
820 fatalbox("%s", error_string(err));
821 }
822 } else {
823 if (s->sending_oob) {
824 if (nsent < len) {
825 memmove(s->oobdata, s->oobdata+nsent, len-nsent);
826 s->sending_oob = len - nsent;
827 } else {
828 s->sending_oob = 0;
829 }
830 } else {
831 bufchain_consume(&s->output_data, nsent);
832 }
833 }
834 }
0ff9ea38 835 uxsel_tell(s);
c5e438ec 836}
837
e0e7dff8 838static int sk_tcp_write(Socket sock, const char *buf, int len)
c5e438ec 839{
840 Actual_Socket s = (Actual_Socket) sock;
841
842 /*
843 * Add the data to the buffer list on the socket.
844 */
845 bufchain_add(&s->output_data, buf, len);
846
847 /*
848 * Now try sending from the start of the buffer list.
849 */
850 if (s->writable)
851 try_send(s);
852
c2a71b0d 853 /*
854 * Update the select() status to correctly reflect whether or
855 * not we should be selecting for write.
856 */
857 uxsel_tell(s);
858
c5e438ec 859 return bufchain_size(&s->output_data);
860}
861
e0e7dff8 862static int sk_tcp_write_oob(Socket sock, const char *buf, int len)
c5e438ec 863{
864 Actual_Socket s = (Actual_Socket) sock;
865
866 /*
867 * Replace the buffer list on the socket with the data.
868 */
869 bufchain_clear(&s->output_data);
870 assert(len <= sizeof(s->oobdata));
871 memcpy(s->oobdata, buf, len);
872 s->sending_oob = len;
873
874 /*
875 * Now try sending from the start of the buffer list.
876 */
877 if (s->writable)
878 try_send(s);
879
c2a71b0d 880 /*
881 * Update the select() status to correctly reflect whether or
882 * not we should be selecting for write.
883 */
884 uxsel_tell(s);
885
c5e438ec 886 return s->sending_oob;
887}
888
0ff9ea38 889static int net_select_result(int fd, int event)
c5e438ec 890{
891 int ret;
892 int err;
893 char buf[20480]; /* nice big buffer for plenty of speed */
894 Actual_Socket s;
895 u_long atmark;
896
897 /* Find the Socket structure */
f7f9fb5c 898 s = find234(sktree, &fd, cmpforsearch);
c5e438ec 899 if (!s)
900 return 1; /* boggle */
901
902 noise_ultralight(event);
903
904 switch (event) {
c5e438ec 905 case 4: /* exceptional */
906 if (!s->oobinline) {
907 /*
908 * On a non-oobinline socket, this indicates that we
909 * can immediately perform an OOB read and get back OOB
910 * data, which we will send to the back end with
911 * type==2 (urgent data).
912 */
913 ret = recv(s->s, buf, sizeof(buf), MSG_OOB);
914 noise_ultralight(ret);
915 if (ret <= 0) {
cbe2d68f 916 const char *str = (ret == 0 ? "Internal networking trouble" :
917 error_string(errno));
c5e438ec 918 /* We're inside the Unix frontend here, so we know
919 * that the frontend handle is unnecessary. */
920 logevent(NULL, str);
921 fatalbox("%s", str);
922 } else {
923 return plug_receive(s->plug, 2, buf, ret);
924 }
925 break;
926 }
927
928 /*
929 * If we reach here, this is an oobinline socket, which
56e5b2db 930 * means we should set s->oobpending and then deal with it
931 * when we get called for the readability event (which
932 * should also occur).
c5e438ec 933 */
934 s->oobpending = TRUE;
56e5b2db 935 break;
c5e438ec 936 case 1: /* readable; also acceptance */
937 if (s->listener) {
938 /*
939 * On a listening socket, the readability event means a
940 * connection is ready to be accepted.
941 */
942 struct sockaddr_in isa;
943 int addrlen = sizeof(struct sockaddr_in);
944 int t; /* socket of connection */
945
946 memset(&isa, 0, sizeof(struct sockaddr_in));
947 err = 0;
776792d7 948 t = accept(s->s,(struct sockaddr *)&isa,(socklen_t *) &addrlen);
c5e438ec 949 if (t < 0) {
950 break;
951 }
952
6ee9b735 953 if (s->localhost_only && !ipv4_is_loopback(isa.sin_addr)) {
c5e438ec 954 close(t); /* someone let nonlocal through?! */
f7f9fb5c 955 } else if (plug_accepting(s->plug, t)) {
c5e438ec 956 close(t); /* denied or error */
957 }
958 break;
959 }
960
961 /*
962 * If we reach here, this is not a listening socket, so
963 * readability really means readability.
964 */
965
966 /* In the case the socket is still frozen, we don't even bother */
967 if (s->frozen) {
968 s->frozen_readable = 1;
969 break;
970 }
971
972 /*
973 * We have received data on the socket. For an oobinline
974 * socket, this might be data _before_ an urgent pointer,
975 * in which case we send it to the back end with type==1
976 * (data prior to urgent).
977 */
978 if (s->oobinline && s->oobpending) {
979 atmark = 1;
980 if (ioctl(s->s, SIOCATMARK, &atmark) == 0 && atmark)
981 s->oobpending = FALSE; /* clear this indicator */
982 } else
983 atmark = 1;
984
56e5b2db 985 ret = recv(s->s, buf, s->oobpending ? 1 : sizeof(buf), 0);
c5e438ec 986 noise_ultralight(ret);
987 if (ret < 0) {
988 if (errno == EWOULDBLOCK) {
989 break;
990 }
991 }
992 if (ret < 0) {
993 return plug_closing(s->plug, error_string(errno), errno, 0);
994 } else if (0 == ret) {
995 return plug_closing(s->plug, NULL, 0, 0);
996 } else {
997 return plug_receive(s->plug, atmark ? 0 : 1, buf, ret);
998 }
999 break;
1000 case 2: /* writable */
051dd789 1001 if (!s->connected) {
1002 /*
1003 * select() reports a socket as _writable_ when an
1004 * asynchronous connection is completed.
1005 */
1006 s->connected = s->writable = 1;
0ff9ea38 1007 uxsel_tell(s);
051dd789 1008 break;
1009 } else {
c5e438ec 1010 int bufsize_before, bufsize_after;
1011 s->writable = 1;
1012 bufsize_before = s->sending_oob + bufchain_size(&s->output_data);
1013 try_send(s);
1014 bufsize_after = s->sending_oob + bufchain_size(&s->output_data);
1015 if (bufsize_after < bufsize_before)
1016 plug_sent(s->plug, bufsize_after);
1017 }
1018 break;
1019 }
1020
1021 return 1;
1022}
1023
1024/*
1025 * Deal with socket errors detected in try_send().
1026 */
1027void net_pending_errors(void)
1028{
1029 int i;
1030 Actual_Socket s;
1031
1032 /*
1033 * This might be a fiddly business, because it's just possible
1034 * that handling a pending error on one socket might cause
1035 * others to be closed. (I can't think of any reason this might
1036 * happen in current SSH implementation, but to maintain
1037 * generality of this network layer I'll assume the worst.)
1038 *
1039 * So what we'll do is search the socket list for _one_ socket
1040 * with a pending error, and then handle it, and then search
1041 * the list again _from the beginning_. Repeat until we make a
1042 * pass with no socket errors present. That way we are
1043 * protected against the socket list changing under our feet.
1044 */
1045
1046 do {
1047 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
1048 if (s->pending_error) {
1049 /*
1050 * An error has occurred on this socket. Pass it to the
1051 * plug.
1052 */
1053 plug_closing(s->plug, error_string(s->pending_error),
1054 s->pending_error, 0);
1055 break;
1056 }
1057 }
1058 } while (s);
1059}
1060
1061/*
1062 * Each socket abstraction contains a `void *' private field in
1063 * which the client can keep state.
1064 */
1065static void sk_tcp_set_private_ptr(Socket sock, void *ptr)
1066{
1067 Actual_Socket s = (Actual_Socket) sock;
1068 s->private_ptr = ptr;
1069}
1070
1071static void *sk_tcp_get_private_ptr(Socket sock)
1072{
1073 Actual_Socket s = (Actual_Socket) sock;
1074 return s->private_ptr;
1075}
1076
1077/*
1078 * Special error values are returned from sk_namelookup and sk_new
1079 * if there's a problem. These functions extract an error message,
1080 * or return NULL if there's no problem.
1081 */
cbe2d68f 1082const char *sk_addr_error(SockAddr addr)
c5e438ec 1083{
1084 return addr->error;
1085}
cbe2d68f 1086static const char *sk_tcp_socket_error(Socket sock)
c5e438ec 1087{
1088 Actual_Socket s = (Actual_Socket) sock;
1089 return s->error;
1090}
1091
1092static void sk_tcp_set_frozen(Socket sock, int is_frozen)
1093{
1094 Actual_Socket s = (Actual_Socket) sock;
1095 if (s->frozen == is_frozen)
1096 return;
1097 s->frozen = is_frozen;
1098 if (!is_frozen && s->frozen_readable) {
1099 char c;
1100 recv(s->s, &c, 1, MSG_PEEK);
1101 }
1102 s->frozen_readable = 0;
0ff9ea38 1103 uxsel_tell(s);
c5e438ec 1104}
1105
0ff9ea38 1106static void uxsel_tell(Actual_Socket s)
c5e438ec 1107{
0ff9ea38 1108 int rwx = 0;
051dd789 1109 if (!s->connected)
0ff9ea38 1110 rwx |= 2; /* write == connect */
c5e438ec 1111 if (s->connected && !s->frozen)
0ff9ea38 1112 rwx |= 1 | 4; /* read, except */
c5e438ec 1113 if (bufchain_size(&s->output_data))
0ff9ea38 1114 rwx |= 2; /* write */
c5e438ec 1115 if (s->listener)
0ff9ea38 1116 rwx |= 1; /* read == accept */
1117 uxsel_set(s->s, rwx, net_select_result);
c5e438ec 1118}
1119
1120int net_service_lookup(char *service)
1121{
1122 struct servent *se;
1123 se = getservbyname(service, NULL);
1124 if (se != NULL)
1125 return ntohs(se->s_port);
1126 else
1127 return 0;
1128}
fc0f17db 1129
1130SockAddr platform_get_x11_unix_address(int displaynum, char **canonicalname)
1131{
1132 SockAddr ret = snew(struct SockAddr_tag);
1133 int n;
1134
1135 memset(ret, 0, sizeof *ret);
1136 ret->family = AF_UNIX;
1137 n = snprintf(ret->hostname, sizeof ret->hostname,
1138 "%s%d", X11_UNIX_PATH, displaynum);
1139 if(n < 0)
1140 ret->error = "snprintf failed";
1141 else if(n >= sizeof ret->hostname)
1142 ret->error = "X11 UNIX name too long";
1143 else
1144 *canonicalname = dupstr(ret->hostname);
1145 return ret;
1146}