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