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"
30 struct socket_function_table *fn;
31 /* the above variable absolutely *must* be the first in this structure */
39 int frozen; /* this causes readability notifications to be ignored */
40 int frozen_readable; /* this means we missed at least one readability
41 * notification while we were frozen */
42 int localhost_only; /* for listening sockets */
45 int oobpending; /* is there OOB data available to read? */
47 int pending_error; /* in case send() returns error */
49 int nodelay, keepalive; /* for connect()-type sockets */
50 int privport, port; /* and again */
55 * We used to typedef struct Socket_tag *Socket.
57 * Since we have made the networking abstraction slightly more
58 * abstract, Socket no longer means a tcp socket (it could mean
59 * an ssl socket). So now we must use Actual_Socket when we know
60 * we are talking about a tcp socket.
62 typedef struct Socket_tag *Actual_Socket;
67 * Which address family this address belongs to. AF_INET for
68 * IPv4; AF_INET6 for IPv6; AF_UNSPEC indicates that name
69 * resolution has not been done and a simple host name is held
70 * in this SockAddr structure.
74 struct addrinfo *ais; /* Addresses IPv6 style. */
75 struct addrinfo *ai; /* steps along the linked list */
77 unsigned long *addresses; /* Addresses IPv4 style. */
78 int naddresses, curraddr;
80 char hostname[512]; /* Store an unresolved host name. */
83 static tree234 *sktree;
85 static void uxsel_tell(Actual_Socket s);
87 static int cmpfortree(void *av, void *bv)
89 Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv;
90 int as = a->s, bs = b->s;
98 static int cmpforsearch(void *av, void *bv)
100 Actual_Socket b = (Actual_Socket) bv;
101 int as = *(int *)av, bs = b->s;
111 sktree = newtree234(cmpfortree);
114 void sk_cleanup(void)
120 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
126 SockAddr sk_namelookup(const char *host, char **canonicalname, int address_family)
128 SockAddr ret = snew(struct SockAddr_tag);
130 struct addrinfo hints;
134 struct hostent *h = NULL;
139 /* Clear the structure and default to IPv4. */
140 memset(ret, 0, sizeof(struct SockAddr_tag));
141 ret->family = 0; /* We set this one when we have resolved the host. */
146 hints.ai_flags = AI_CANONNAME;
147 hints.ai_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
148 address_family == ADDRTYPE_IPV6 ? AF_INET6 :
150 hints.ai_socktype = SOCK_STREAM;
151 hints.ai_protocol = 0;
152 hints.ai_addrlen = 0;
153 hints.ai_addr = NULL;
154 hints.ai_canonname = NULL;
155 hints.ai_next = NULL;
156 err = getaddrinfo(host, NULL, &hints, &ret->ais);
159 ret->error = gai_strerror(err);
162 ret->family = ret->ai->ai_family;
164 if (ret->ai->ai_canonname != NULL)
165 strncat(realhost, ret->ai->ai_canonname, sizeof(realhost) - 1);
167 strncat(realhost, host, sizeof(realhost) - 1);
169 if ((a = inet_addr(host)) == (unsigned long)(in_addr_t)(-1)) {
171 * Otherwise use the IPv4-only gethostbyname... (NOTE:
172 * we don't use gethostbyname as a fallback!)
174 if (ret->family == 0) {
175 /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
176 if ( (h = gethostbyname(host)) )
177 ret->family = AF_INET;
179 if (ret->family == 0) {
180 ret->error = (h_errno == HOST_NOT_FOUND ||
181 h_errno == NO_DATA ||
182 h_errno == NO_ADDRESS ? "Host does not exist" :
183 h_errno == TRY_AGAIN ?
184 "Temporary name service failure" :
185 "gethostbyname: unknown error");
188 /* This way we are always sure the h->h_name is valid :) */
189 strncpy(realhost, h->h_name, sizeof(realhost));
190 for (n = 0; h->h_addr_list[n]; n++);
191 ret->addresses = snewn(n, unsigned long);
193 for (n = 0; n < ret->naddresses; n++) {
194 memcpy(&a, h->h_addr_list[n], sizeof(a));
195 ret->addresses[n] = ntohl(a);
199 * This must be a numeric IPv4 address because it caused a
200 * success return from inet_addr.
202 ret->family = AF_INET;
203 strncpy(realhost, host, sizeof(realhost));
204 ret->addresses = snew(unsigned long);
206 ret->addresses[0] = ntohl(a);
210 realhost[lenof(realhost)-1] = '\0';
211 *canonicalname = snewn(1+strlen(realhost), char);
212 strcpy(*canonicalname, realhost);
216 SockAddr sk_nonamelookup(const char *host)
218 SockAddr ret = snew(struct SockAddr_tag);
220 ret->family = AF_UNSPEC;
221 strncpy(ret->hostname, host, lenof(ret->hostname));
222 ret->hostname[lenof(ret->hostname)-1] = '\0';
226 ret->addresses = NULL;
231 static int sk_nextaddr(SockAddr addr)
234 if (addr->ai->ai_next) {
235 addr->ai = addr->ai->ai_next;
236 addr->family = addr->ai->ai_family;
241 if (addr->curraddr+1 < addr->naddresses) {
250 void sk_getaddr(SockAddr addr, char *buf, int buflen)
253 if (addr->family == AF_UNSPEC) {
254 strncpy(buf, addr->hostname, buflen);
255 buf[buflen-1] = '\0';
258 if (getnameinfo(addr->ai->ai_addr, addr->ai->ai_addrlen, buf, buflen,
259 NULL, 0, NI_NUMERICHOST) != 0) {
261 strncat(buf, "<unknown>", buflen - 1);
265 assert(addr->family == AF_INET);
266 a.s_addr = htonl(addr->addresses[addr->curraddr]);
267 strncpy(buf, inet_ntoa(a), buflen);
268 buf[buflen-1] = '\0';
273 int sk_hostname_is_local(char *name)
275 return !strcmp(name, "localhost");
278 #define ipv4_is_loopback(addr) \
279 (((addr).s_addr & htonl(0xff000000)) == htonl(0x7f000000))
281 static int sockaddr_is_loopback(struct sockaddr *sa)
283 struct sockaddr_in *sin;
285 struct sockaddr_in6 *sin6;
288 switch (sa->sa_family) {
290 sin = (struct sockaddr_in *)sa;
291 return ipv4_is_loopback(sin->sin_addr);
294 sin6 = (struct sockaddr_in6 *)sa;
295 return IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr);
304 int sk_address_is_local(SockAddr addr)
307 if (addr->family == AF_UNSPEC)
308 return 0; /* we don't know; assume not */
311 return sockaddr_is_loopback(addr->ai->ai_addr);
314 assert(addr->family == AF_INET);
315 a.s_addr = htonl(addr->addresses[addr->curraddr]);
316 return ipv4_is_loopback(a);
321 int sk_addrtype(SockAddr addr)
323 return (addr->family == AF_INET ? ADDRTYPE_IPV4 :
325 addr->family == AF_INET6 ? ADDRTYPE_IPV6 :
330 void sk_addrcopy(SockAddr addr, char *buf)
334 if (addr->family == AF_INET)
335 memcpy(buf, &((struct sockaddr_in *)addr->ai->ai_addr)->sin_addr,
336 sizeof(struct in_addr));
337 else if (addr->family == AF_INET6)
338 memcpy(buf, &((struct sockaddr_in6 *)addr->ai->ai_addr)->sin6_addr,
339 sizeof(struct in6_addr));
345 assert(addr->family == AF_INET);
346 a.s_addr = htonl(addr->addresses[addr->curraddr]);
347 memcpy(buf, (char*) &a.s_addr, 4);
351 void sk_addr_free(SockAddr addr)
355 if (addr->ais != NULL)
356 freeaddrinfo(addr->ais);
358 sfree(addr->addresses);
363 static Plug sk_tcp_plug(Socket sock, Plug p)
365 Actual_Socket s = (Actual_Socket) sock;
372 static void sk_tcp_flush(Socket s)
375 * We send data to the socket as soon as we can anyway,
376 * so we don't need to do anything here. :-)
380 static void sk_tcp_close(Socket s);
381 static int sk_tcp_write(Socket s, const char *data, int len);
382 static int sk_tcp_write_oob(Socket s, const char *data, int len);
383 static void sk_tcp_set_private_ptr(Socket s, void *ptr);
384 static void *sk_tcp_get_private_ptr(Socket s);
385 static void sk_tcp_set_frozen(Socket s, int is_frozen);
386 static const char *sk_tcp_socket_error(Socket s);
388 static struct socket_function_table tcp_fn_table = {
394 sk_tcp_set_private_ptr,
395 sk_tcp_get_private_ptr,
400 Socket sk_register(OSSocket sockfd, Plug plug)
405 * Create Socket structure.
407 ret = snew(struct Socket_tag);
408 ret->fn = &tcp_fn_table;
411 bufchain_init(&ret->output_data);
412 ret->writable = 1; /* to start with */
413 ret->sending_oob = 0;
415 ret->frozen_readable = 0;
416 ret->localhost_only = 0; /* unused, but best init anyway */
417 ret->pending_error = 0;
418 ret->oobpending = FALSE;
426 ret->error = strerror(errno);
438 static int try_connect(Actual_Socket sock)
442 struct sockaddr_in6 a6;
444 struct sockaddr_in a;
445 struct sockaddr_un au;
446 const struct sockaddr *sa;
454 plug_log(sock->plug, 0, sock->addr, sock->port, NULL, 0);
459 assert(sock->addr->family != AF_UNSPEC);
460 s = socket(sock->addr->family, SOCK_STREAM, 0);
468 if (sock->oobinline) {
470 setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));
475 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *) &b, sizeof(b));
478 if (sock->keepalive) {
480 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *) &b, sizeof(b));
484 * Bind to local address.
487 localport = 1023; /* count from 1023 downwards */
489 localport = 0; /* just use port 0 (ie kernel picks) */
491 /* BSD IP stacks need sockaddr_in zeroed before filling in */
492 memset(&a,'\0',sizeof(struct sockaddr_in));
494 memset(&a6,'\0',sizeof(struct sockaddr_in6));
497 /* We don't try to bind to a local address for UNIX domain sockets. (Why
498 * do we bother doing the bind when localport == 0 anyway?) */
499 if(sock->addr->family != AF_UNIX) {
500 /* Loop round trying to bind */
505 if (sock->addr->family == AF_INET6) {
506 /* XXX use getaddrinfo to get a local address? */
507 a6.sin6_family = AF_INET6;
508 a6.sin6_addr = in6addr_any;
509 a6.sin6_port = htons(localport);
510 retcode = bind(s, (struct sockaddr *) &a6, sizeof(a6));
514 assert(sock->addr->family == AF_INET);
515 a.sin_family = AF_INET;
516 a.sin_addr.s_addr = htonl(INADDR_ANY);
517 a.sin_port = htons(localport);
518 retcode = bind(s, (struct sockaddr *) &a, sizeof(a));
525 if (err != EADDRINUSE) /* failed, for a bad reason */
530 break; /* we're only looping once */
533 break; /* we might have got to the end */
541 * Connect to remote address.
543 switch(sock->addr->family) {
546 /* XXX would be better to have got getaddrinfo() to fill in the port. */
547 ((struct sockaddr_in *)sock->addr->ai->ai_addr)->sin_port =
549 sa = (const struct sockaddr *)sock->addr->ai->ai_addr;
550 salen = sock->addr->ai->ai_addrlen;
553 ((struct sockaddr_in *)sock->addr->ai->ai_addr)->sin_port =
555 sa = (const struct sockaddr *)sock->addr->ai->ai_addr;
556 salen = sock->addr->ai->ai_addrlen;
560 a.sin_family = AF_INET;
561 a.sin_addr.s_addr = htonl(sock->addr->addresses[sock->addr->curraddr]);
562 a.sin_port = htons((short) sock->port);
563 sa = (const struct sockaddr *)&a;
568 assert(sock->port == 0); /* to catch confused people */
569 assert(strlen(sock->addr->hostname) < sizeof au.sun_path);
570 memset(&au, 0, sizeof au);
571 au.sun_family = AF_UNIX;
572 strcpy(au.sun_path, sock->addr->hostname);
573 sa = (const struct sockaddr *)&au;
578 assert(0 && "unknown address family");
581 fl = fcntl(s, F_GETFL);
583 fcntl(s, F_SETFL, fl | O_NONBLOCK);
585 if ((connect(s, sa, salen)) < 0) {
586 if ( errno != EINPROGRESS ) {
592 * If we _don't_ get EWOULDBLOCK, the connect has completed
593 * and we should set the socket as connected and writable.
600 add234(sktree, sock);
604 plug_log(sock->plug, 1, sock->addr, sock->port, strerror(err), err);
608 Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
609 int nodelay, int keepalive, Plug plug)
615 * Create Socket structure.
617 ret = snew(struct Socket_tag);
618 ret->fn = &tcp_fn_table;
621 bufchain_init(&ret->output_data);
622 ret->connected = 0; /* to start with */
623 ret->writable = 0; /* to start with */
624 ret->sending_oob = 0;
626 ret->frozen_readable = 0;
627 ret->localhost_only = 0; /* unused, but best init anyway */
628 ret->pending_error = 0;
629 ret->oobpending = FALSE;
633 ret->oobinline = oobinline;
634 ret->nodelay = nodelay;
635 ret->keepalive = keepalive;
636 ret->privport = privport;
641 err = try_connect(ret);
642 } while (err && sk_nextaddr(ret->addr));
645 ret->error = strerror(err);
650 Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only, int address_family)
654 struct addrinfo hints, *ai;
656 struct sockaddr_in6 a6;
658 struct sockaddr *addr;
660 struct sockaddr_in a;
666 * Create Socket structure.
668 ret = snew(struct Socket_tag);
669 ret->fn = &tcp_fn_table;
672 bufchain_init(&ret->output_data);
673 ret->writable = 0; /* to start with */
674 ret->sending_oob = 0;
676 ret->frozen_readable = 0;
677 ret->localhost_only = local_host_only;
678 ret->pending_error = 0;
679 ret->oobpending = FALSE;
685 * Translate address_family from platform-independent constants
686 * into local reality.
688 address_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
689 address_family == ADDRTYPE_IPV6 ? AF_INET6 : AF_UNSPEC);
692 /* Let's default to IPv6.
693 * If the stack doesn't support IPv6, we will fall back to IPv4. */
694 if (address_family == AF_UNSPEC) address_family = AF_INET6;
696 /* No other choice, default to IPv4 */
697 if (address_family == AF_UNSPEC) address_family = AF_INET;
703 s = socket(address_family, SOCK_STREAM, 0);
705 /* If the host doesn't support IPv6 try fallback to IPv4. */
706 if (s < 0 && address_family == AF_INET6) {
707 address_family = AF_INET;
708 s = socket(address_family, SOCK_STREAM, 0);
712 ret->error = strerror(errno);
718 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
721 addr = NULL; addrlen = -1; /* placate optimiser */
723 if (srcaddr != NULL) {
725 hints.ai_flags = AI_NUMERICHOST;
726 hints.ai_family = address_family;
727 hints.ai_socktype = SOCK_STREAM;
728 hints.ai_protocol = 0;
729 hints.ai_addrlen = 0;
730 hints.ai_addr = NULL;
731 hints.ai_canonname = NULL;
732 hints.ai_next = NULL;
733 assert(port >= 0 && port <= 99999);
734 sprintf(portstr, "%d", port);
735 retcode = getaddrinfo(srcaddr, portstr, &hints, &ai);
738 addrlen = ai->ai_addrlen;
741 memset(&a,'\0',sizeof(struct sockaddr_in));
742 a.sin_family = AF_INET;
743 a.sin_port = htons(port);
744 a.sin_addr.s_addr = inet_addr(srcaddr);
745 if (a.sin_addr.s_addr != (in_addr_t)(-1)) {
746 /* Override localhost_only with specified listen addr. */
747 ret->localhost_only = ipv4_is_loopback(a.sin_addr);
750 addr = (struct sockaddr *)a;
758 if (address_family == AF_INET6) {
759 memset(&a6,'\0',sizeof(struct sockaddr_in6));
760 a6.sin6_family = AF_INET6;
761 a6.sin6_port = htons(port);
763 a6.sin6_addr = in6addr_loopback;
765 a6.sin6_addr = in6addr_any;
766 addr = (struct sockaddr *)&a6;
767 addrlen = sizeof(a6);
771 memset(&a,'\0',sizeof(struct sockaddr_in));
772 a.sin_family = AF_INET;
773 a.sin_port = htons(port);
775 a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
777 a.sin_addr.s_addr = htonl(INADDR_ANY);
778 addr = (struct sockaddr *)&a;
783 retcode = bind(s, addr, addrlen);
786 ret->error = strerror(errno);
790 if (listen(s, SOMAXCONN) < 0) {
792 ret->error = strerror(errno);
804 static void sk_tcp_close(Socket sock)
806 Actual_Socket s = (Actual_Socket) sock;
812 sk_addr_free(s->addr);
816 #define PUT_32BIT_MSB_FIRST(cp, value) ( \
817 (cp)[0] = (char)((value) >> 24), \
818 (cp)[1] = (char)((value) >> 16), \
819 (cp)[2] = (char)((value) >> 8), \
820 (cp)[3] = (char)(value) )
822 #define PUT_16BIT_MSB_FIRST(cp, value) ( \
823 (cp)[0] = (char)((value) >> 8), \
824 (cp)[1] = (char)(value) )
826 void *sk_getxdmdata(void *sock, int *lenp)
828 Actual_Socket s = (Actual_Socket) sock;
830 struct sockaddr_in addr;
832 struct sockaddr_storage addr;
833 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr;
835 struct sockaddr *sa = (struct sockaddr *)&addr;
836 struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
839 static unsigned int unix_addr = 0xFFFFFFFF;
842 * We must check that this socket really _is_ an Actual_Socket.
844 if (s->fn != &tcp_fn_table)
845 return NULL; /* failure */
847 addrlen = sizeof(addr);
848 if (getsockname(s->s, sa, &addrlen) < 0)
850 switch(sa->sa_family) {
853 buf = snewn(*lenp, char);
854 PUT_32BIT_MSB_FIRST(buf, ntohl(sin->sin_addr.s_addr));
855 PUT_16BIT_MSB_FIRST(buf+4, ntohs(sin->sin_port));
860 buf = snewn(*lenp, char);
861 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
862 memcpy(buf, sin6->sin6_addr.s6_addr + 12, 4);
863 PUT_16BIT_MSB_FIRST(buf+4, ntohs(sin6->sin6_port));
865 /* This is stupid, but it's what XLib does. */
871 buf = snewn(*lenp, char);
872 PUT_32BIT_MSB_FIRST(buf, unix_addr--);
873 PUT_16BIT_MSB_FIRST(buf+4, getpid());
886 * The function which tries to send on a socket once it's deemed
889 void try_send(Actual_Socket s)
891 while (s->sending_oob || bufchain_size(&s->output_data) > 0) {
897 if (s->sending_oob) {
898 urgentflag = MSG_OOB;
899 len = s->sending_oob;
903 bufchain_prefix(&s->output_data, &data, &len);
905 nsent = send(s->s, data, len, urgentflag);
906 noise_ultralight(nsent);
908 err = (nsent < 0 ? errno : 0);
909 if (err == EWOULDBLOCK) {
911 * Perfectly normal: we've sent all we can for the moment.
915 } else if (nsent == 0 ||
916 err == ECONNABORTED || err == ECONNRESET) {
918 * If send() returns CONNABORTED or CONNRESET, we
919 * unfortunately can't just call plug_closing(),
920 * because it's quite likely that we're currently
921 * _in_ a call from the code we'd be calling back
922 * to, so we'd have to make half the SSH code
923 * reentrant. Instead we flag a pending error on
924 * the socket, to be dealt with (by calling
925 * plug_closing()) at some suitable future moment.
927 s->pending_error = err;
930 /* We're inside the Unix frontend here, so we know
931 * that the frontend handle is unnecessary. */
932 logevent(NULL, strerror(err));
933 fatalbox("%s", strerror(err));
936 if (s->sending_oob) {
938 memmove(s->oobdata, s->oobdata+nsent, len-nsent);
939 s->sending_oob = len - nsent;
944 bufchain_consume(&s->output_data, nsent);
951 static int sk_tcp_write(Socket sock, const char *buf, int len)
953 Actual_Socket s = (Actual_Socket) sock;
956 * Add the data to the buffer list on the socket.
958 bufchain_add(&s->output_data, buf, len);
961 * Now try sending from the start of the buffer list.
967 * Update the select() status to correctly reflect whether or
968 * not we should be selecting for write.
972 return bufchain_size(&s->output_data);
975 static int sk_tcp_write_oob(Socket sock, const char *buf, int len)
977 Actual_Socket s = (Actual_Socket) sock;
980 * Replace the buffer list on the socket with the data.
982 bufchain_clear(&s->output_data);
983 assert(len <= sizeof(s->oobdata));
984 memcpy(s->oobdata, buf, len);
985 s->sending_oob = len;
988 * Now try sending from the start of the buffer list.
994 * Update the select() status to correctly reflect whether or
995 * not we should be selecting for write.
999 return s->sending_oob;
1002 static int net_select_result(int fd, int event)
1005 char buf[20480]; /* nice big buffer for plenty of speed */
1009 /* Find the Socket structure */
1010 s = find234(sktree, &fd, cmpforsearch);
1012 return 1; /* boggle */
1014 noise_ultralight(event);
1017 case 4: /* exceptional */
1018 if (!s->oobinline) {
1020 * On a non-oobinline socket, this indicates that we
1021 * can immediately perform an OOB read and get back OOB
1022 * data, which we will send to the back end with
1023 * type==2 (urgent data).
1025 ret = recv(s->s, buf, sizeof(buf), MSG_OOB);
1026 noise_ultralight(ret);
1028 const char *str = (ret == 0 ? "Internal networking trouble" :
1030 /* We're inside the Unix frontend here, so we know
1031 * that the frontend handle is unnecessary. */
1032 logevent(NULL, str);
1033 fatalbox("%s", str);
1036 * Receiving actual data on a socket means we can
1037 * stop falling back through the candidate
1038 * addresses to connect to.
1041 sk_addr_free(s->addr);
1044 return plug_receive(s->plug, 2, buf, ret);
1050 * If we reach here, this is an oobinline socket, which
1051 * means we should set s->oobpending and then deal with it
1052 * when we get called for the readability event (which
1053 * should also occur).
1055 s->oobpending = TRUE;
1057 case 1: /* readable; also acceptance */
1060 * On a listening socket, the readability event means a
1061 * connection is ready to be accepted.
1064 struct sockaddr_in ss;
1066 struct sockaddr_storage ss;
1068 socklen_t addrlen = sizeof(ss);
1069 int t; /* socket of connection */
1071 memset(&ss, 0, addrlen);
1072 t = accept(s->s, (struct sockaddr *)&ss, &addrlen);
1077 if (s->localhost_only &&
1078 !sockaddr_is_loopback((struct sockaddr *)&ss)) {
1079 close(t); /* someone let nonlocal through?! */
1080 } else if (plug_accepting(s->plug, t)) {
1081 close(t); /* denied or error */
1087 * If we reach here, this is not a listening socket, so
1088 * readability really means readability.
1091 /* In the case the socket is still frozen, we don't even bother */
1093 s->frozen_readable = 1;
1098 * We have received data on the socket. For an oobinline
1099 * socket, this might be data _before_ an urgent pointer,
1100 * in which case we send it to the back end with type==1
1101 * (data prior to urgent).
1103 if (s->oobinline && s->oobpending) {
1105 if (ioctl(s->s, SIOCATMARK, &atmark) == 0 && atmark)
1106 s->oobpending = FALSE; /* clear this indicator */
1110 ret = recv(s->s, buf, s->oobpending ? 1 : sizeof(buf), 0);
1111 noise_ultralight(ret);
1113 if (errno == EWOULDBLOCK) {
1119 * An error at this point _might_ be an error reported
1120 * by a non-blocking connect(). So before we return a
1121 * panic status to the user, let's just see whether
1126 plug_log(s->plug, 1, s->addr, s->port, strerror(err), err);
1127 while (s->addr && sk_nextaddr(s->addr)) {
1128 err = try_connect(s);
1132 return plug_closing(s->plug, strerror(err), err, 0);
1133 } else if (0 == ret) {
1134 return plug_closing(s->plug, NULL, 0, 0);
1137 * Receiving actual data on a socket means we can
1138 * stop falling back through the candidate
1139 * addresses to connect to.
1142 sk_addr_free(s->addr);
1145 return plug_receive(s->plug, atmark ? 0 : 1, buf, ret);
1148 case 2: /* writable */
1149 if (!s->connected) {
1151 * select() reports a socket as _writable_ when an
1152 * asynchronous connection is completed.
1154 s->connected = s->writable = 1;
1158 int bufsize_before, bufsize_after;
1160 bufsize_before = s->sending_oob + bufchain_size(&s->output_data);
1162 bufsize_after = s->sending_oob + bufchain_size(&s->output_data);
1163 if (bufsize_after < bufsize_before)
1164 plug_sent(s->plug, bufsize_after);
1173 * Deal with socket errors detected in try_send().
1175 void net_pending_errors(void)
1181 * This might be a fiddly business, because it's just possible
1182 * that handling a pending error on one socket might cause
1183 * others to be closed. (I can't think of any reason this might
1184 * happen in current SSH implementation, but to maintain
1185 * generality of this network layer I'll assume the worst.)
1187 * So what we'll do is search the socket list for _one_ socket
1188 * with a pending error, and then handle it, and then search
1189 * the list again _from the beginning_. Repeat until we make a
1190 * pass with no socket errors present. That way we are
1191 * protected against the socket list changing under our feet.
1195 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
1196 if (s->pending_error) {
1198 * An error has occurred on this socket. Pass it to the
1201 plug_closing(s->plug, strerror(s->pending_error),
1202 s->pending_error, 0);
1210 * Each socket abstraction contains a `void *' private field in
1211 * which the client can keep state.
1213 static void sk_tcp_set_private_ptr(Socket sock, void *ptr)
1215 Actual_Socket s = (Actual_Socket) sock;
1216 s->private_ptr = ptr;
1219 static void *sk_tcp_get_private_ptr(Socket sock)
1221 Actual_Socket s = (Actual_Socket) sock;
1222 return s->private_ptr;
1226 * Special error values are returned from sk_namelookup and sk_new
1227 * if there's a problem. These functions extract an error message,
1228 * or return NULL if there's no problem.
1230 const char *sk_addr_error(SockAddr addr)
1234 static const char *sk_tcp_socket_error(Socket sock)
1236 Actual_Socket s = (Actual_Socket) sock;
1240 static void sk_tcp_set_frozen(Socket sock, int is_frozen)
1242 Actual_Socket s = (Actual_Socket) sock;
1243 if (s->frozen == is_frozen)
1245 s->frozen = is_frozen;
1246 if (!is_frozen && s->frozen_readable) {
1248 recv(s->s, &c, 1, MSG_PEEK);
1250 s->frozen_readable = 0;
1254 static void uxsel_tell(Actual_Socket s)
1258 rwx |= 2; /* write == connect */
1259 if (s->connected && !s->frozen)
1260 rwx |= 1 | 4; /* read, except */
1261 if (bufchain_size(&s->output_data))
1262 rwx |= 2; /* write */
1264 rwx |= 1; /* read == accept */
1265 uxsel_set(s->s, rwx, net_select_result);
1268 int net_service_lookup(char *service)
1271 se = getservbyname(service, NULL);
1273 return ntohs(se->s_port);
1278 SockAddr platform_get_x11_unix_address(int displaynum, char **canonicalname)
1280 SockAddr ret = snew(struct SockAddr_tag);
1283 memset(ret, 0, sizeof *ret);
1284 ret->family = AF_UNIX;
1285 n = snprintf(ret->hostname, sizeof ret->hostname,
1286 "%s%d", X11_UNIX_PATH, displaynum);
1288 ret->error = "snprintf failed";
1289 else if(n >= sizeof ret->hostname)
1290 ret->error = "X11 UNIX name too long";
1292 *canonicalname = dupstr(ret->hostname);
1296 ret->addresses = NULL;