Colin's const-fixing Patch Of Death. Seems to build fine on Windows
[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 const 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 const 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 const 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 const 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 int fl;
387
388 /*
389 * Create Socket structure.
390 */
391 ret = snew(struct Socket_tag);
392 ret->fn = &tcp_fn_table;
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 */
409 assert(addr->family != AF_UNSPEC);
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) {
443 /* XXX use getaddrinfo to get a local address? */
444 a6.sin6_family = AF_INET6;
445 a6.sin6_addr = in6addr_any;
446 a6.sin6_port = htons(localport);
447 retcode = bind(s, (struct sockaddr *) &a6, sizeof(a6));
448 } else
449 #endif
450 {
451 assert(addr->family == AF_INET);
452 a.sin_family = AF_INET;
453 a.sin_addr.s_addr = htonl(INADDR_ANY);
454 a.sin_port = htons(localport);
455 retcode = bind(s, (struct sockaddr *) &a, sizeof(a));
456 }
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
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);
490 }
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
496
497 fl = fcntl(s, F_GETFL);
498 if (fl != -1)
499 fcntl(s, F_SETFL, fl | O_NONBLOCK);
500
501 if ((
502 #ifdef IPV6
503 connect(s, addr->ai->ai_addr, addr->ai->ai_addrlen)
504 #else
505 connect(s, (struct sockaddr *) &a, sizeof(a))
506 #endif
507 ) < 0) {
508 if ( errno != EINPROGRESS ) {
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
521 uxsel_tell(ret);
522 add234(sktree, ret);
523
524 return (Socket) ret;
525 }
526
527 Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only)
528 {
529 int s;
530 #ifdef IPV6
531 #if 0
532 struct sockaddr_in6 a6;
533 #endif
534 struct addrinfo hints, *ai;
535 char portstr[6];
536 #endif
537 struct sockaddr_in a;
538 int err;
539 Actual_Socket ret;
540 int retcode;
541 int on = 1;
542
543 /*
544 * Create Socket structure.
545 */
546 ret = snew(struct Socket_tag);
547 ret->fn = &tcp_fn_table;
548 ret->error = NULL;
549 ret->plug = plug;
550 bufchain_init(&ret->output_data);
551 ret->writable = 0; /* to start with */
552 ret->sending_oob = 0;
553 ret->frozen = 0;
554 ret->frozen_readable = 0;
555 ret->localhost_only = local_host_only;
556 ret->pending_error = 0;
557 ret->oobpending = FALSE;
558 ret->listener = 1;
559
560 /*
561 * Open socket.
562 */
563 s = socket(AF_INET, SOCK_STREAM, 0);
564 ret->s = s;
565
566 if (s < 0) {
567 ret->error = error_string(errno);
568 return (Socket) ret;
569 }
570
571 ret->oobinline = 0;
572
573 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
574
575 #ifdef IPV6
576 hints.ai_flags = AI_NUMERICHOST;
577 hints.ai_family = AF_UNSPEC;
578 hints.ai_socktype = 0;
579 hints.ai_protocol = 0;
580 hints.ai_addrlen = 0;
581 hints.ai_addr = NULL;
582 hints.ai_canonname = NULL;
583 hints.ai_next = NULL;
584 sprintf(portstr, "%d", port);
585 if (srcaddr != NULL && getaddrinfo(srcaddr, portstr, &hints, &ai) == 0)
586 retcode = bind(s, ai->ai_addr, ai->ai_addrlen);
587 else
588 #if 0
589 {
590 /*
591 * FIXME: Need two listening sockets, in principle, one for v4
592 * and one for v6
593 */
594 if (local_host_only)
595 a6.sin6_addr = in6addr_loopback;
596 else
597 a6.sin6_addr = in6addr_any;
598 a6.sin6_port = htons(port);
599 } else
600 #endif
601 #endif
602 {
603 int got_addr = 0;
604 a.sin_family = AF_INET;
605
606 /*
607 * Bind to source address. First try an explicitly
608 * specified one...
609 */
610 if (srcaddr) {
611 a.sin_addr.s_addr = inet_addr(srcaddr);
612 if (a.sin_addr.s_addr != INADDR_NONE) {
613 /* Override localhost_only with specified listen addr. */
614 ret->localhost_only = ipv4_is_loopback(a.sin_addr);
615 got_addr = 1;
616 }
617 }
618
619 /*
620 * ... and failing that, go with one of the standard ones.
621 */
622 if (!got_addr) {
623 if (local_host_only)
624 a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
625 else
626 a.sin_addr.s_addr = htonl(INADDR_ANY);
627 }
628
629 a.sin_port = htons((short)port);
630 retcode = bind(s, (struct sockaddr *) &a, sizeof(a));
631 }
632
633 if (retcode >= 0) {
634 err = 0;
635 } else {
636 err = errno;
637 }
638
639 if (err) {
640 ret->error = error_string(err);
641 return (Socket) ret;
642 }
643
644
645 if (listen(s, SOMAXCONN) < 0) {
646 close(s);
647 ret->error = error_string(errno);
648 return (Socket) ret;
649 }
650
651 uxsel_tell(ret);
652 add234(sktree, ret);
653
654 return (Socket) ret;
655 }
656
657 static void sk_tcp_close(Socket sock)
658 {
659 Actual_Socket s = (Actual_Socket) sock;
660
661 uxsel_del(s->s);
662 del234(sktree, s);
663 close(s->s);
664 sfree(s);
665 }
666
667 int sk_getxdmdata(void *sock, unsigned long *ip, int *port)
668 {
669 Actual_Socket s = (Actual_Socket) sock;
670 struct sockaddr_in addr;
671 socklen_t addrlen;
672
673 /*
674 * We must check that this socket really _is_ an Actual_Socket.
675 */
676 if (s->fn != &tcp_fn_table)
677 return 0; /* failure */
678
679 /*
680 * If we ever implement connecting to a local X server through
681 * a Unix socket, we return 0xFFFFFFFF for the IP address and
682 * our current pid for the port. Bizarre, but such is life.
683 */
684
685 addrlen = sizeof(addr);
686 if (getsockname(s->s, (struct sockaddr *)&addr, &addrlen) < 0 ||
687 addr.sin_family != AF_INET)
688 return 0;
689
690 *ip = ntohl(addr.sin_addr.s_addr);
691 *port = ntohs(addr.sin_port);
692
693 return 1;
694 }
695
696 /*
697 * The function which tries to send on a socket once it's deemed
698 * writable.
699 */
700 void try_send(Actual_Socket s)
701 {
702 while (s->sending_oob || bufchain_size(&s->output_data) > 0) {
703 int nsent;
704 int err;
705 void *data;
706 int len, urgentflag;
707
708 if (s->sending_oob) {
709 urgentflag = MSG_OOB;
710 len = s->sending_oob;
711 data = &s->oobdata;
712 } else {
713 urgentflag = 0;
714 bufchain_prefix(&s->output_data, &data, &len);
715 }
716 nsent = send(s->s, data, len, urgentflag);
717 noise_ultralight(nsent);
718 if (nsent <= 0) {
719 err = (nsent < 0 ? errno : 0);
720 if (err == EWOULDBLOCK) {
721 /*
722 * Perfectly normal: we've sent all we can for the moment.
723 */
724 s->writable = FALSE;
725 return;
726 } else if (nsent == 0 ||
727 err == ECONNABORTED || err == ECONNRESET) {
728 /*
729 * If send() returns CONNABORTED or CONNRESET, we
730 * unfortunately can't just call plug_closing(),
731 * because it's quite likely that we're currently
732 * _in_ a call from the code we'd be calling back
733 * to, so we'd have to make half the SSH code
734 * reentrant. Instead we flag a pending error on
735 * the socket, to be dealt with (by calling
736 * plug_closing()) at some suitable future moment.
737 */
738 s->pending_error = err;
739 return;
740 } else {
741 /* We're inside the Unix frontend here, so we know
742 * that the frontend handle is unnecessary. */
743 logevent(NULL, error_string(err));
744 fatalbox("%s", error_string(err));
745 }
746 } else {
747 if (s->sending_oob) {
748 if (nsent < len) {
749 memmove(s->oobdata, s->oobdata+nsent, len-nsent);
750 s->sending_oob = len - nsent;
751 } else {
752 s->sending_oob = 0;
753 }
754 } else {
755 bufchain_consume(&s->output_data, nsent);
756 }
757 }
758 }
759 uxsel_tell(s);
760 }
761
762 static int sk_tcp_write(Socket sock, const char *buf, int len)
763 {
764 Actual_Socket s = (Actual_Socket) sock;
765
766 /*
767 * Add the data to the buffer list on the socket.
768 */
769 bufchain_add(&s->output_data, buf, len);
770
771 /*
772 * Now try sending from the start of the buffer list.
773 */
774 if (s->writable)
775 try_send(s);
776
777 /*
778 * Update the select() status to correctly reflect whether or
779 * not we should be selecting for write.
780 */
781 uxsel_tell(s);
782
783 return bufchain_size(&s->output_data);
784 }
785
786 static int sk_tcp_write_oob(Socket sock, const char *buf, int len)
787 {
788 Actual_Socket s = (Actual_Socket) sock;
789
790 /*
791 * Replace the buffer list on the socket with the data.
792 */
793 bufchain_clear(&s->output_data);
794 assert(len <= sizeof(s->oobdata));
795 memcpy(s->oobdata, buf, len);
796 s->sending_oob = len;
797
798 /*
799 * Now try sending from the start of the buffer list.
800 */
801 if (s->writable)
802 try_send(s);
803
804 /*
805 * Update the select() status to correctly reflect whether or
806 * not we should be selecting for write.
807 */
808 uxsel_tell(s);
809
810 return s->sending_oob;
811 }
812
813 static int net_select_result(int fd, int event)
814 {
815 int ret;
816 int err;
817 char buf[20480]; /* nice big buffer for plenty of speed */
818 Actual_Socket s;
819 u_long atmark;
820
821 /* Find the Socket structure */
822 s = find234(sktree, (void *) fd, cmpforsearch);
823 if (!s)
824 return 1; /* boggle */
825
826 noise_ultralight(event);
827
828 switch (event) {
829 case 4: /* exceptional */
830 if (!s->oobinline) {
831 /*
832 * On a non-oobinline socket, this indicates that we
833 * can immediately perform an OOB read and get back OOB
834 * data, which we will send to the back end with
835 * type==2 (urgent data).
836 */
837 ret = recv(s->s, buf, sizeof(buf), MSG_OOB);
838 noise_ultralight(ret);
839 if (ret <= 0) {
840 const char *str = (ret == 0 ? "Internal networking trouble" :
841 error_string(errno));
842 /* We're inside the Unix frontend here, so we know
843 * that the frontend handle is unnecessary. */
844 logevent(NULL, str);
845 fatalbox("%s", str);
846 } else {
847 return plug_receive(s->plug, 2, buf, ret);
848 }
849 break;
850 }
851
852 /*
853 * If we reach here, this is an oobinline socket, which
854 * means we should set s->oobpending and then deal with it
855 * when we get called for the readability event (which
856 * should also occur).
857 */
858 s->oobpending = TRUE;
859 break;
860 case 1: /* readable; also acceptance */
861 if (s->listener) {
862 /*
863 * On a listening socket, the readability event means a
864 * connection is ready to be accepted.
865 */
866 struct sockaddr_in isa;
867 int addrlen = sizeof(struct sockaddr_in);
868 int t; /* socket of connection */
869
870 memset(&isa, 0, sizeof(struct sockaddr_in));
871 err = 0;
872 t = accept(s->s,(struct sockaddr *)&isa,&addrlen);
873 if (t < 0) {
874 break;
875 }
876
877 if (s->localhost_only && !ipv4_is_loopback(isa.sin_addr)) {
878 close(t); /* someone let nonlocal through?! */
879 } else if (plug_accepting(s->plug, (void*)t)) {
880 close(t); /* denied or error */
881 }
882 break;
883 }
884
885 /*
886 * If we reach here, this is not a listening socket, so
887 * readability really means readability.
888 */
889
890 /* In the case the socket is still frozen, we don't even bother */
891 if (s->frozen) {
892 s->frozen_readable = 1;
893 break;
894 }
895
896 /*
897 * We have received data on the socket. For an oobinline
898 * socket, this might be data _before_ an urgent pointer,
899 * in which case we send it to the back end with type==1
900 * (data prior to urgent).
901 */
902 if (s->oobinline && s->oobpending) {
903 atmark = 1;
904 if (ioctl(s->s, SIOCATMARK, &atmark) == 0 && atmark)
905 s->oobpending = FALSE; /* clear this indicator */
906 } else
907 atmark = 1;
908
909 ret = recv(s->s, buf, s->oobpending ? 1 : sizeof(buf), 0);
910 noise_ultralight(ret);
911 if (ret < 0) {
912 if (errno == EWOULDBLOCK) {
913 break;
914 }
915 }
916 if (ret < 0) {
917 return plug_closing(s->plug, error_string(errno), errno, 0);
918 } else if (0 == ret) {
919 return plug_closing(s->plug, NULL, 0, 0);
920 } else {
921 return plug_receive(s->plug, atmark ? 0 : 1, buf, ret);
922 }
923 break;
924 case 2: /* writable */
925 if (!s->connected) {
926 /*
927 * select() reports a socket as _writable_ when an
928 * asynchronous connection is completed.
929 */
930 s->connected = s->writable = 1;
931 uxsel_tell(s);
932 break;
933 } else {
934 int bufsize_before, bufsize_after;
935 s->writable = 1;
936 bufsize_before = s->sending_oob + bufchain_size(&s->output_data);
937 try_send(s);
938 bufsize_after = s->sending_oob + bufchain_size(&s->output_data);
939 if (bufsize_after < bufsize_before)
940 plug_sent(s->plug, bufsize_after);
941 }
942 break;
943 }
944
945 return 1;
946 }
947
948 /*
949 * Deal with socket errors detected in try_send().
950 */
951 void net_pending_errors(void)
952 {
953 int i;
954 Actual_Socket s;
955
956 /*
957 * This might be a fiddly business, because it's just possible
958 * that handling a pending error on one socket might cause
959 * others to be closed. (I can't think of any reason this might
960 * happen in current SSH implementation, but to maintain
961 * generality of this network layer I'll assume the worst.)
962 *
963 * So what we'll do is search the socket list for _one_ socket
964 * with a pending error, and then handle it, and then search
965 * the list again _from the beginning_. Repeat until we make a
966 * pass with no socket errors present. That way we are
967 * protected against the socket list changing under our feet.
968 */
969
970 do {
971 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
972 if (s->pending_error) {
973 /*
974 * An error has occurred on this socket. Pass it to the
975 * plug.
976 */
977 plug_closing(s->plug, error_string(s->pending_error),
978 s->pending_error, 0);
979 break;
980 }
981 }
982 } while (s);
983 }
984
985 /*
986 * Each socket abstraction contains a `void *' private field in
987 * which the client can keep state.
988 */
989 static void sk_tcp_set_private_ptr(Socket sock, void *ptr)
990 {
991 Actual_Socket s = (Actual_Socket) sock;
992 s->private_ptr = ptr;
993 }
994
995 static void *sk_tcp_get_private_ptr(Socket sock)
996 {
997 Actual_Socket s = (Actual_Socket) sock;
998 return s->private_ptr;
999 }
1000
1001 /*
1002 * Special error values are returned from sk_namelookup and sk_new
1003 * if there's a problem. These functions extract an error message,
1004 * or return NULL if there's no problem.
1005 */
1006 const char *sk_addr_error(SockAddr addr)
1007 {
1008 return addr->error;
1009 }
1010 static const char *sk_tcp_socket_error(Socket sock)
1011 {
1012 Actual_Socket s = (Actual_Socket) sock;
1013 return s->error;
1014 }
1015
1016 static void sk_tcp_set_frozen(Socket sock, int is_frozen)
1017 {
1018 Actual_Socket s = (Actual_Socket) sock;
1019 if (s->frozen == is_frozen)
1020 return;
1021 s->frozen = is_frozen;
1022 if (!is_frozen && s->frozen_readable) {
1023 char c;
1024 recv(s->s, &c, 1, MSG_PEEK);
1025 }
1026 s->frozen_readable = 0;
1027 uxsel_tell(s);
1028 }
1029
1030 static void uxsel_tell(Actual_Socket s)
1031 {
1032 int rwx = 0;
1033 if (!s->connected)
1034 rwx |= 2; /* write == connect */
1035 if (s->connected && !s->frozen)
1036 rwx |= 1 | 4; /* read, except */
1037 if (bufchain_size(&s->output_data))
1038 rwx |= 2; /* write */
1039 if (s->listener)
1040 rwx |= 1; /* read == accept */
1041 uxsel_set(s->s, rwx, net_select_result);
1042 }
1043
1044 int net_service_lookup(char *service)
1045 {
1046 struct servent *se;
1047 se = getservbyname(service, NULL);
1048 if (se != NULL)
1049 return ntohs(se->s_port);
1050 else
1051 return 0;
1052 }