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