2 * Unix networking abstraction.
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>
20 #define DEFINE_PLUG_METHOD_MACROS
26 # define X11_UNIX_PATH "/tmp/.X11-unix/X"
29 #define ipv4_is_loopback(addr) (inet_netof(addr) == IN_LOOPBACKNET)
32 struct socket_function_table *fn;
33 /* the above variable absolutely *must* be the first in this structure */
41 int frozen; /* this causes readability notifications to be ignored */
42 int frozen_readable; /* this means we missed at least one readability
43 * notification while we were frozen */
44 int localhost_only; /* for listening sockets */
47 int oobpending; /* is there OOB data available to read? */
49 int pending_error; /* in case send() returns error */
51 int nodelay, keepalive; /* for connect()-type sockets */
52 int privport, port; /* and again */
57 * We used to typedef struct Socket_tag *Socket.
59 * Since we have made the networking abstraction slightly more
60 * abstract, Socket no longer means a tcp socket (it could mean
61 * an ssl socket). So now we must use Actual_Socket when we know
62 * we are talking about a tcp socket.
64 typedef struct Socket_tag *Actual_Socket;
69 * Which address family this address belongs to. AF_INET for
70 * IPv4; AF_INET6 for IPv6; AF_UNSPEC indicates that name
71 * resolution has not been done and a simple host name is held
72 * in this SockAddr structure.
76 struct addrinfo *ais; /* Addresses IPv6 style. */
77 struct addrinfo *ai; /* steps along the linked list */
79 unsigned long *addresses; /* Addresses IPv4 style. */
80 int naddresses, curraddr;
82 char hostname[512]; /* Store an unresolved host name. */
85 static tree234 *sktree;
87 static void uxsel_tell(Actual_Socket s);
89 static int cmpfortree(void *av, void *bv)
91 Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv;
92 int as = a->s, bs = b->s;
100 static int cmpforsearch(void *av, void *bv)
102 Actual_Socket b = (Actual_Socket) bv;
103 int as = *(int *)av, bs = b->s;
113 sktree = newtree234(cmpfortree);
116 void sk_cleanup(void)
122 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
128 SockAddr sk_namelookup(const char *host, char **canonicalname, int address_family)
130 SockAddr ret = snew(struct SockAddr_tag);
132 struct addrinfo hints;
136 struct hostent *h = NULL;
141 /* Clear the structure and default to IPv4. */
142 memset(ret, 0, sizeof(struct SockAddr_tag));
143 ret->family = 0; /* We set this one when we have resolved the host. */
148 hints.ai_flags = AI_CANONNAME;
149 hints.ai_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
150 address_family == ADDRTYPE_IPV6 ? AF_INET6 :
152 hints.ai_socktype = SOCK_STREAM;
153 hints.ai_protocol = 0;
154 hints.ai_addrlen = 0;
155 hints.ai_addr = NULL;
156 hints.ai_canonname = NULL;
157 hints.ai_next = NULL;
158 err = getaddrinfo(host, NULL, &hints, &ret->ais);
161 ret->error = gai_strerror(err);
164 ret->family = ret->ai->ai_family;
166 if (ret->ai->ai_canonname != NULL)
167 strncat(realhost, ret->ai->ai_canonname, sizeof(realhost) - 1);
169 strncat(realhost, host, sizeof(realhost) - 1);
171 if ((a = inet_addr(host)) == (unsigned long)(in_addr_t)(-1)) {
173 * Otherwise use the IPv4-only gethostbyname... (NOTE:
174 * we don't use gethostbyname as a fallback!)
176 if (ret->family == 0) {
177 /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
178 if ( (h = gethostbyname(host)) )
179 ret->family = AF_INET;
181 if (ret->family == 0) {
182 ret->error = (h_errno == HOST_NOT_FOUND ||
183 h_errno == NO_DATA ||
184 h_errno == NO_ADDRESS ? "Host does not exist" :
185 h_errno == TRY_AGAIN ?
186 "Temporary name service failure" :
187 "gethostbyname: unknown error");
190 /* This way we are always sure the h->h_name is valid :) */
191 strncpy(realhost, h->h_name, sizeof(realhost));
192 for (n = 0; h->h_addr_list[n]; n++);
193 ret->addresses = snewn(n, unsigned long);
195 for (n = 0; n < ret->naddresses; n++) {
196 memcpy(&a, h->h_addr_list[n], sizeof(a));
197 ret->addresses[n] = ntohl(a);
201 * This must be a numeric IPv4 address because it caused a
202 * success return from inet_addr.
204 ret->family = AF_INET;
205 strncpy(realhost, host, sizeof(realhost));
206 ret->addresses = snew(unsigned long);
208 ret->addresses[0] = ntohl(a);
212 realhost[lenof(realhost)-1] = '\0';
213 *canonicalname = snewn(1+strlen(realhost), char);
214 strcpy(*canonicalname, realhost);
218 SockAddr sk_nonamelookup(const char *host)
220 SockAddr ret = snew(struct SockAddr_tag);
222 ret->family = AF_UNSPEC;
223 strncpy(ret->hostname, host, lenof(ret->hostname));
224 ret->hostname[lenof(ret->hostname)-1] = '\0';
228 ret->addresses = NULL;
233 static int sk_nextaddr(SockAddr addr)
236 if (addr->ai->ai_next) {
237 addr->ai = addr->ai->ai_next;
238 addr->family = addr->ai->ai_family;
243 if (addr->curraddr+1 < addr->naddresses) {
252 void sk_getaddr(SockAddr addr, char *buf, int buflen)
255 if (addr->family == AF_UNSPEC) {
256 strncpy(buf, addr->hostname, buflen);
257 buf[buflen-1] = '\0';
260 if (getnameinfo(addr->ai->ai_addr, addr->ai->ai_addrlen, buf, buflen,
261 NULL, 0, NI_NUMERICHOST) != 0) {
263 strncat(buf, "<unknown>", buflen - 1);
267 assert(addr->family == AF_INET);
268 a.s_addr = htonl(addr->addresses[addr->curraddr]);
269 strncpy(buf, inet_ntoa(a), buflen);
270 buf[buflen-1] = '\0';
275 int sk_hostname_is_local(char *name)
277 return !strcmp(name, "localhost");
280 int sk_address_is_local(SockAddr addr)
283 if (addr->family == AF_UNSPEC)
284 return 0; /* we don't know; assume not */
287 if (addr->family == AF_INET)
288 return ipv4_is_loopback(
289 ((struct sockaddr_in *)addr->ai->ai_addr)->sin_addr);
290 else if (addr->family == AF_INET6)
291 return IN6_IS_ADDR_LOOPBACK(
292 &((struct sockaddr_in6 *)addr->ai->ai_addr)->sin6_addr);
297 assert(addr->family == AF_INET);
298 a.s_addr = htonl(addr->addresses[addr->curraddr]);
299 return ipv4_is_loopback(a);
304 int sk_addrtype(SockAddr addr)
306 return (addr->family == AF_INET ? ADDRTYPE_IPV4 :
308 addr->family == AF_INET6 ? ADDRTYPE_IPV6 :
313 void sk_addrcopy(SockAddr addr, char *buf)
317 if (addr->family == AF_INET)
318 memcpy(buf, &((struct sockaddr_in *)addr->ai->ai_addr)->sin_addr,
319 sizeof(struct in_addr));
320 else if (addr->family == AF_INET6)
321 memcpy(buf, &((struct sockaddr_in6 *)addr->ai->ai_addr)->sin6_addr,
322 sizeof(struct in6_addr));
328 assert(addr->family == AF_INET);
329 a.s_addr = htonl(addr->addresses[addr->curraddr]);
330 memcpy(buf, (char*) &a.s_addr, 4);
334 void sk_addr_free(SockAddr addr)
338 if (addr->ais != NULL)
339 freeaddrinfo(addr->ais);
341 sfree(addr->addresses);
346 static Plug sk_tcp_plug(Socket sock, Plug p)
348 Actual_Socket s = (Actual_Socket) sock;
355 static void sk_tcp_flush(Socket s)
358 * We send data to the socket as soon as we can anyway,
359 * so we don't need to do anything here. :-)
363 static void sk_tcp_close(Socket s);
364 static int sk_tcp_write(Socket s, const char *data, int len);
365 static int sk_tcp_write_oob(Socket s, const char *data, int len);
366 static void sk_tcp_set_private_ptr(Socket s, void *ptr);
367 static void *sk_tcp_get_private_ptr(Socket s);
368 static void sk_tcp_set_frozen(Socket s, int is_frozen);
369 static const char *sk_tcp_socket_error(Socket s);
371 static struct socket_function_table tcp_fn_table = {
377 sk_tcp_set_private_ptr,
378 sk_tcp_get_private_ptr,
383 Socket sk_register(OSSocket sockfd, Plug plug)
388 * Create Socket structure.
390 ret = snew(struct Socket_tag);
391 ret->fn = &tcp_fn_table;
394 bufchain_init(&ret->output_data);
395 ret->writable = 1; /* to start with */
396 ret->sending_oob = 0;
398 ret->frozen_readable = 0;
399 ret->localhost_only = 0; /* unused, but best init anyway */
400 ret->pending_error = 0;
401 ret->oobpending = FALSE;
408 ret->error = strerror(errno);
420 static int try_connect(Actual_Socket sock)
424 struct sockaddr_in6 a6;
426 struct sockaddr_in a;
427 struct sockaddr_un au;
428 const struct sockaddr *sa;
436 plug_log(sock->plug, 0, sock->addr, sock->port, NULL, 0);
441 assert(sock->addr->family != AF_UNSPEC);
442 s = socket(sock->addr->family, SOCK_STREAM, 0);
450 if (sock->oobinline) {
452 setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));
457 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *) &b, sizeof(b));
460 if (sock->keepalive) {
462 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *) &b, sizeof(b));
466 * Bind to local address.
469 localport = 1023; /* count from 1023 downwards */
471 localport = 0; /* just use port 0 (ie kernel picks) */
473 /* BSD IP stacks need sockaddr_in zeroed before filling in */
474 memset(&a,'\0',sizeof(struct sockaddr_in));
476 memset(&a6,'\0',sizeof(struct sockaddr_in6));
479 /* We don't try to bind to a local address for UNIX domain sockets. (Why
480 * do we bother doing the bind when localport == 0 anyway?) */
481 if(sock->addr->family != AF_UNIX) {
482 /* Loop round trying to bind */
487 if (sock->addr->family == AF_INET6) {
488 /* XXX use getaddrinfo to get a local address? */
489 a6.sin6_family = AF_INET6;
490 a6.sin6_addr = in6addr_any;
491 a6.sin6_port = htons(localport);
492 retcode = bind(s, (struct sockaddr *) &a6, sizeof(a6));
496 assert(sock->addr->family == AF_INET);
497 a.sin_family = AF_INET;
498 a.sin_addr.s_addr = htonl(INADDR_ANY);
499 a.sin_port = htons(localport);
500 retcode = bind(s, (struct sockaddr *) &a, sizeof(a));
507 if (err != EADDRINUSE) /* failed, for a bad reason */
512 break; /* we're only looping once */
515 break; /* we might have got to the end */
523 * Connect to remote address.
525 switch(sock->addr->family) {
528 /* XXX would be better to have got getaddrinfo() to fill in the port. */
529 ((struct sockaddr_in *)sock->addr->ai->ai_addr)->sin_port =
531 sa = (const struct sockaddr *)sock->addr->ai->ai_addr;
532 salen = sock->addr->ai->ai_addrlen;
535 ((struct sockaddr_in *)sock->addr->ai->ai_addr)->sin_port =
537 sa = (const struct sockaddr *)sock->addr->ai->ai_addr;
538 salen = sock->addr->ai->ai_addrlen;
542 a.sin_family = AF_INET;
543 a.sin_addr.s_addr = htonl(sock->addr->addresses[sock->addr->curraddr]);
544 a.sin_port = htons((short) sock->port);
545 sa = (const struct sockaddr *)&a;
550 assert(sock->port == 0); /* to catch confused people */
551 assert(strlen(sock->addr->hostname) < sizeof au.sun_path);
552 memset(&au, 0, sizeof au);
553 au.sun_family = AF_UNIX;
554 strcpy(au.sun_path, sock->addr->hostname);
555 sa = (const struct sockaddr *)&au;
560 assert(0 && "unknown address family");
563 fl = fcntl(s, F_GETFL);
565 fcntl(s, F_SETFL, fl | O_NONBLOCK);
567 if ((connect(s, sa, salen)) < 0) {
568 if ( errno != EINPROGRESS ) {
574 * If we _don't_ get EWOULDBLOCK, the connect has completed
575 * and we should set the socket as connected and writable.
582 add234(sktree, sock);
586 plug_log(sock->plug, 1, sock->addr, sock->port, strerror(err), err);
590 Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
591 int nodelay, int keepalive, Plug plug)
597 * Create Socket structure.
599 ret = snew(struct Socket_tag);
600 ret->fn = &tcp_fn_table;
603 bufchain_init(&ret->output_data);
604 ret->connected = 0; /* to start with */
605 ret->writable = 0; /* to start with */
606 ret->sending_oob = 0;
608 ret->frozen_readable = 0;
609 ret->localhost_only = 0; /* unused, but best init anyway */
610 ret->pending_error = 0;
611 ret->oobpending = FALSE;
615 ret->oobinline = oobinline;
616 ret->nodelay = nodelay;
617 ret->keepalive = keepalive;
618 ret->privport = privport;
623 err = try_connect(ret);
624 } while (err && sk_nextaddr(ret->addr));
627 ret->error = strerror(err);
632 Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only, int address_family)
636 struct addrinfo hints, *ai;
638 struct sockaddr_in6 a6;
640 struct sockaddr *addr;
642 struct sockaddr_in a;
648 * Create Socket structure.
650 ret = snew(struct Socket_tag);
651 ret->fn = &tcp_fn_table;
654 bufchain_init(&ret->output_data);
655 ret->writable = 0; /* to start with */
656 ret->sending_oob = 0;
658 ret->frozen_readable = 0;
659 ret->localhost_only = local_host_only;
660 ret->pending_error = 0;
661 ret->oobpending = FALSE;
666 * Translate address_family from platform-independent constants
667 * into local reality.
669 address_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
670 address_family == ADDRTYPE_IPV6 ? AF_INET6 : AF_UNSPEC);
673 /* Let's default to IPv6.
674 * If the stack doesn't support IPv6, we will fall back to IPv4. */
675 if (address_family == AF_UNSPEC) address_family = AF_INET6;
677 /* No other choice, default to IPv4 */
678 if (address_family == AF_UNSPEC) address_family = AF_INET;
684 s = socket(address_family, SOCK_STREAM, 0);
686 /* If the host doesn't support IPv6 try fallback to IPv4. */
687 if (s < 0 && address_family == AF_INET6) {
688 address_family = AF_INET;
689 s = socket(address_family, SOCK_STREAM, 0);
693 ret->error = strerror(errno);
699 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
702 addr = NULL; addrlen = -1; /* placate optimiser */
704 if (srcaddr != NULL) {
706 hints.ai_flags = AI_NUMERICHOST;
707 hints.ai_family = address_family;
708 hints.ai_socktype = SOCK_STREAM;
709 hints.ai_protocol = 0;
710 hints.ai_addrlen = 0;
711 hints.ai_addr = NULL;
712 hints.ai_canonname = NULL;
713 hints.ai_next = NULL;
714 sprintf(portstr, "%d", port);
715 retcode = getaddrinfo(srcaddr, portstr, &hints, &ai);
718 addrlen = ai->ai_addrlen;
721 memset(&a,'\0',sizeof(struct sockaddr_in));
722 a.sin_family = AF_INET;
723 a.sin_port = htons(port);
724 a.sin_addr.s_addr = inet_addr(srcaddr);
725 if (a.sin_addr.s_addr != (in_addr_t)(-1)) {
726 /* Override localhost_only with specified listen addr. */
727 ret->localhost_only = ipv4_is_loopback(a.sin_addr);
730 addr = (struct sockaddr *)a;
738 if (address_family == AF_INET6) {
739 memset(&a6,'\0',sizeof(struct sockaddr_in6));
740 a6.sin6_family = AF_INET6;
741 a6.sin6_port = htons(port);
743 a6.sin6_addr = in6addr_loopback;
745 a6.sin6_addr = in6addr_any;
746 addr = (struct sockaddr *)&a6;
747 addrlen = sizeof(a6);
751 memset(&a,'\0',sizeof(struct sockaddr_in));
752 a.sin_family = AF_INET;
753 a.sin_port = htons(port);
755 a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
757 a.sin_addr.s_addr = htonl(INADDR_ANY);
758 addr = (struct sockaddr *)&a;
763 retcode = bind(s, addr, addrlen);
766 ret->error = strerror(errno);
770 if (listen(s, SOMAXCONN) < 0) {
772 ret->error = strerror(errno);
784 static void sk_tcp_close(Socket sock)
786 Actual_Socket s = (Actual_Socket) sock;
792 sk_addr_free(s->addr);
796 int sk_getxdmdata(void *sock, unsigned long *ip, int *port)
798 Actual_Socket s = (Actual_Socket) sock;
799 struct sockaddr_in addr;
803 * We must check that this socket really _is_ an Actual_Socket.
805 if (s->fn != &tcp_fn_table)
806 return 0; /* failure */
808 addrlen = sizeof(addr);
809 if (getsockname(s->s, (struct sockaddr *)&addr, &addrlen) < 0)
811 switch(addr.sin_family) {
813 *ip = ntohl(addr.sin_addr.s_addr);
814 *port = ntohs(addr.sin_port);
818 * For a Unix socket, we return 0xFFFFFFFF for the IP address and
819 * our current pid for the port. Bizarre, but such is life.
821 *ip = ntohl(0xFFFFFFFF);
835 * The function which tries to send on a socket once it's deemed
838 void try_send(Actual_Socket s)
840 while (s->sending_oob || bufchain_size(&s->output_data) > 0) {
846 if (s->sending_oob) {
847 urgentflag = MSG_OOB;
848 len = s->sending_oob;
852 bufchain_prefix(&s->output_data, &data, &len);
854 nsent = send(s->s, data, len, urgentflag);
855 noise_ultralight(nsent);
857 err = (nsent < 0 ? errno : 0);
858 if (err == EWOULDBLOCK) {
860 * Perfectly normal: we've sent all we can for the moment.
864 } else if (nsent == 0 ||
865 err == ECONNABORTED || err == ECONNRESET) {
867 * If send() returns CONNABORTED or CONNRESET, we
868 * unfortunately can't just call plug_closing(),
869 * because it's quite likely that we're currently
870 * _in_ a call from the code we'd be calling back
871 * to, so we'd have to make half the SSH code
872 * reentrant. Instead we flag a pending error on
873 * the socket, to be dealt with (by calling
874 * plug_closing()) at some suitable future moment.
876 s->pending_error = err;
879 /* We're inside the Unix frontend here, so we know
880 * that the frontend handle is unnecessary. */
881 logevent(NULL, strerror(err));
882 fatalbox("%s", strerror(err));
885 if (s->sending_oob) {
887 memmove(s->oobdata, s->oobdata+nsent, len-nsent);
888 s->sending_oob = len - nsent;
893 bufchain_consume(&s->output_data, nsent);
900 static int sk_tcp_write(Socket sock, const char *buf, int len)
902 Actual_Socket s = (Actual_Socket) sock;
905 * Add the data to the buffer list on the socket.
907 bufchain_add(&s->output_data, buf, len);
910 * Now try sending from the start of the buffer list.
916 * Update the select() status to correctly reflect whether or
917 * not we should be selecting for write.
921 return bufchain_size(&s->output_data);
924 static int sk_tcp_write_oob(Socket sock, const char *buf, int len)
926 Actual_Socket s = (Actual_Socket) sock;
929 * Replace the buffer list on the socket with the data.
931 bufchain_clear(&s->output_data);
932 assert(len <= sizeof(s->oobdata));
933 memcpy(s->oobdata, buf, len);
934 s->sending_oob = len;
937 * Now try sending from the start of the buffer list.
943 * Update the select() status to correctly reflect whether or
944 * not we should be selecting for write.
948 return s->sending_oob;
951 static int sockaddr_is_loopback(struct sockaddr *sa)
953 struct sockaddr_in *sin;
955 struct sockaddr_in6 *sin6;
958 switch (sa->sa_family) {
960 sin = (struct sockaddr_in *)sa;
961 return ipv4_is_loopback(sin->sin_addr);
964 sin6 = (struct sockaddr_in6 *)sa;
965 return IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr);
974 static int net_select_result(int fd, int event)
977 char buf[20480]; /* nice big buffer for plenty of speed */
981 /* Find the Socket structure */
982 s = find234(sktree, &fd, cmpforsearch);
984 return 1; /* boggle */
986 noise_ultralight(event);
989 case 4: /* exceptional */
992 * On a non-oobinline socket, this indicates that we
993 * can immediately perform an OOB read and get back OOB
994 * data, which we will send to the back end with
995 * type==2 (urgent data).
997 ret = recv(s->s, buf, sizeof(buf), MSG_OOB);
998 noise_ultralight(ret);
1000 const char *str = (ret == 0 ? "Internal networking trouble" :
1002 /* We're inside the Unix frontend here, so we know
1003 * that the frontend handle is unnecessary. */
1004 logevent(NULL, str);
1005 fatalbox("%s", str);
1008 * Receiving actual data on a socket means we can
1009 * stop falling back through the candidate
1010 * addresses to connect to.
1013 sk_addr_free(s->addr);
1016 return plug_receive(s->plug, 2, buf, ret);
1022 * If we reach here, this is an oobinline socket, which
1023 * means we should set s->oobpending and then deal with it
1024 * when we get called for the readability event (which
1025 * should also occur).
1027 s->oobpending = TRUE;
1029 case 1: /* readable; also acceptance */
1032 * On a listening socket, the readability event means a
1033 * connection is ready to be accepted.
1036 struct sockaddr_in ss;
1038 struct sockaddr_storage ss;
1040 socklen_t addrlen = sizeof(ss);
1041 int t; /* socket of connection */
1043 memset(&ss, 0, addrlen);
1044 t = accept(s->s, (struct sockaddr *)&ss, &addrlen);
1049 if (s->localhost_only &&
1050 !sockaddr_is_loopback((struct sockaddr *)&ss)) {
1051 close(t); /* someone let nonlocal through?! */
1052 } else if (plug_accepting(s->plug, t)) {
1053 close(t); /* denied or error */
1059 * If we reach here, this is not a listening socket, so
1060 * readability really means readability.
1063 /* In the case the socket is still frozen, we don't even bother */
1065 s->frozen_readable = 1;
1070 * We have received data on the socket. For an oobinline
1071 * socket, this might be data _before_ an urgent pointer,
1072 * in which case we send it to the back end with type==1
1073 * (data prior to urgent).
1075 if (s->oobinline && s->oobpending) {
1077 if (ioctl(s->s, SIOCATMARK, &atmark) == 0 && atmark)
1078 s->oobpending = FALSE; /* clear this indicator */
1082 ret = recv(s->s, buf, s->oobpending ? 1 : sizeof(buf), 0);
1083 noise_ultralight(ret);
1085 if (errno == EWOULDBLOCK) {
1091 * An error at this point _might_ be an error reported
1092 * by a non-blocking connect(). So before we return a
1093 * panic status to the user, let's just see whether
1098 plug_log(s->plug, 1, s->addr, s->port, strerror(err), err);
1099 while (s->addr && sk_nextaddr(s->addr)) {
1100 err = try_connect(s);
1104 return plug_closing(s->plug, strerror(err), err, 0);
1105 } else if (0 == ret) {
1106 return plug_closing(s->plug, NULL, 0, 0);
1109 * Receiving actual data on a socket means we can
1110 * stop falling back through the candidate
1111 * addresses to connect to.
1114 sk_addr_free(s->addr);
1117 return plug_receive(s->plug, atmark ? 0 : 1, buf, ret);
1120 case 2: /* writable */
1121 if (!s->connected) {
1123 * select() reports a socket as _writable_ when an
1124 * asynchronous connection is completed.
1126 s->connected = s->writable = 1;
1130 int bufsize_before, bufsize_after;
1132 bufsize_before = s->sending_oob + bufchain_size(&s->output_data);
1134 bufsize_after = s->sending_oob + bufchain_size(&s->output_data);
1135 if (bufsize_after < bufsize_before)
1136 plug_sent(s->plug, bufsize_after);
1145 * Deal with socket errors detected in try_send().
1147 void net_pending_errors(void)
1153 * This might be a fiddly business, because it's just possible
1154 * that handling a pending error on one socket might cause
1155 * others to be closed. (I can't think of any reason this might
1156 * happen in current SSH implementation, but to maintain
1157 * generality of this network layer I'll assume the worst.)
1159 * So what we'll do is search the socket list for _one_ socket
1160 * with a pending error, and then handle it, and then search
1161 * the list again _from the beginning_. Repeat until we make a
1162 * pass with no socket errors present. That way we are
1163 * protected against the socket list changing under our feet.
1167 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
1168 if (s->pending_error) {
1170 * An error has occurred on this socket. Pass it to the
1173 plug_closing(s->plug, strerror(s->pending_error),
1174 s->pending_error, 0);
1182 * Each socket abstraction contains a `void *' private field in
1183 * which the client can keep state.
1185 static void sk_tcp_set_private_ptr(Socket sock, void *ptr)
1187 Actual_Socket s = (Actual_Socket) sock;
1188 s->private_ptr = ptr;
1191 static void *sk_tcp_get_private_ptr(Socket sock)
1193 Actual_Socket s = (Actual_Socket) sock;
1194 return s->private_ptr;
1198 * Special error values are returned from sk_namelookup and sk_new
1199 * if there's a problem. These functions extract an error message,
1200 * or return NULL if there's no problem.
1202 const char *sk_addr_error(SockAddr addr)
1206 static const char *sk_tcp_socket_error(Socket sock)
1208 Actual_Socket s = (Actual_Socket) sock;
1212 static void sk_tcp_set_frozen(Socket sock, int is_frozen)
1214 Actual_Socket s = (Actual_Socket) sock;
1215 if (s->frozen == is_frozen)
1217 s->frozen = is_frozen;
1218 if (!is_frozen && s->frozen_readable) {
1220 recv(s->s, &c, 1, MSG_PEEK);
1222 s->frozen_readable = 0;
1226 static void uxsel_tell(Actual_Socket s)
1230 rwx |= 2; /* write == connect */
1231 if (s->connected && !s->frozen)
1232 rwx |= 1 | 4; /* read, except */
1233 if (bufchain_size(&s->output_data))
1234 rwx |= 2; /* write */
1236 rwx |= 1; /* read == accept */
1237 uxsel_set(s->s, rwx, net_select_result);
1240 int net_service_lookup(char *service)
1243 se = getservbyname(service, NULL);
1245 return ntohs(se->s_port);
1250 SockAddr platform_get_x11_unix_address(int displaynum, char **canonicalname)
1252 SockAddr ret = snew(struct SockAddr_tag);
1255 memset(ret, 0, sizeof *ret);
1256 ret->family = AF_UNIX;
1257 n = snprintf(ret->hostname, sizeof ret->hostname,
1258 "%s%d", X11_UNIX_PATH, displaynum);
1260 ret->error = "snprintf failed";
1261 else if(n >= sizeof ret->hostname)
1262 ret->error = "X11 UNIX name too long";
1264 *canonicalname = dupstr(ret->hostname);
1268 ret->addresses = NULL;