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