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