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