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
25 /* Solaris needs <sys/sockio.h> for SIOCATMARK. */
27 #include <sys/sockio.h>
31 # define X11_UNIX_PATH "/tmp/.X11-unix/X"
35 * We used to typedef struct Socket_tag *Socket.
37 * Since we have made the networking abstraction slightly more
38 * abstract, Socket no longer means a tcp socket (it could mean
39 * an ssl socket). So now we must use Actual_Socket when we know
40 * we are talking about a tcp socket.
42 typedef struct Socket_tag *Actual_Socket;
45 struct socket_function_table *fn;
46 /* the above variable absolutely *must* be the first in this structure */
52 int connected; /* irrelevant for listening sockets */
54 int frozen; /* this causes readability notifications to be ignored */
55 int frozen_readable; /* this means we missed at least one readability
56 * notification while we were frozen */
57 int localhost_only; /* for listening sockets */
60 int oobpending; /* is there OOB data available to read? */
62 int pending_error; /* in case send() returns error */
64 int nodelay, keepalive; /* for connect()-type sockets */
65 int privport, port; /* and again */
68 * We sometimes need pairs of Socket structures to be linked:
69 * if we are listening on the same IPv6 and v4 port, for
70 * example. So here we define `parent' and `child' pointers to
73 Actual_Socket parent, child;
79 * Which address family this address belongs to. AF_INET for
80 * IPv4; AF_INET6 for IPv6; AF_UNSPEC indicates that name
81 * resolution has not been done and a simple host name is held
82 * in this SockAddr structure.
86 struct addrinfo *ais; /* Addresses IPv6 style. */
87 struct addrinfo *ai; /* steps along the linked list */
89 unsigned long *addresses; /* Addresses IPv4 style. */
90 int naddresses, curraddr;
92 char hostname[512]; /* Store an unresolved host name. */
95 static tree234 *sktree;
97 static void uxsel_tell(Actual_Socket s);
99 static int cmpfortree(void *av, void *bv)
101 Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv;
102 int as = a->s, bs = b->s;
114 static int cmpforsearch(void *av, void *bv)
116 Actual_Socket b = (Actual_Socket) bv;
117 int as = *(int *)av, bs = b->s;
127 sktree = newtree234(cmpfortree);
130 void sk_cleanup(void)
136 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
142 SockAddr sk_namelookup(const char *host, char **canonicalname, int address_family)
144 SockAddr ret = snew(struct SockAddr_tag);
146 struct addrinfo hints;
150 struct hostent *h = NULL;
155 /* Clear the structure and default to IPv4. */
156 memset(ret, 0, sizeof(struct SockAddr_tag));
157 ret->family = 0; /* We set this one when we have resolved the host. */
162 hints.ai_flags = AI_CANONNAME;
163 hints.ai_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
164 address_family == ADDRTYPE_IPV6 ? AF_INET6 :
166 hints.ai_socktype = SOCK_STREAM;
167 hints.ai_protocol = 0;
168 hints.ai_addrlen = 0;
169 hints.ai_addr = NULL;
170 hints.ai_canonname = NULL;
171 hints.ai_next = NULL;
172 err = getaddrinfo(host, NULL, &hints, &ret->ais);
175 ret->error = gai_strerror(err);
178 ret->family = ret->ai->ai_family;
180 if (ret->ai->ai_canonname != NULL)
181 strncat(realhost, ret->ai->ai_canonname, sizeof(realhost) - 1);
183 strncat(realhost, host, sizeof(realhost) - 1);
185 if ((a = inet_addr(host)) == (unsigned long)(in_addr_t)(-1)) {
187 * Otherwise use the IPv4-only gethostbyname... (NOTE:
188 * we don't use gethostbyname as a fallback!)
190 if (ret->family == 0) {
191 /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
192 if ( (h = gethostbyname(host)) )
193 ret->family = AF_INET;
195 if (ret->family == 0) {
196 ret->error = (h_errno == HOST_NOT_FOUND ||
197 h_errno == NO_DATA ||
198 h_errno == NO_ADDRESS ? "Host does not exist" :
199 h_errno == TRY_AGAIN ?
200 "Temporary name service failure" :
201 "gethostbyname: unknown error");
204 /* This way we are always sure the h->h_name is valid :) */
205 strncpy(realhost, h->h_name, sizeof(realhost));
206 for (n = 0; h->h_addr_list[n]; n++);
207 ret->addresses = snewn(n, unsigned long);
209 for (n = 0; n < ret->naddresses; n++) {
210 memcpy(&a, h->h_addr_list[n], sizeof(a));
211 ret->addresses[n] = ntohl(a);
215 * This must be a numeric IPv4 address because it caused a
216 * success return from inet_addr.
218 ret->family = AF_INET;
219 strncpy(realhost, host, sizeof(realhost));
220 ret->addresses = snew(unsigned long);
222 ret->addresses[0] = ntohl(a);
226 realhost[lenof(realhost)-1] = '\0';
227 *canonicalname = snewn(1+strlen(realhost), char);
228 strcpy(*canonicalname, realhost);
232 SockAddr sk_nonamelookup(const char *host)
234 SockAddr ret = snew(struct SockAddr_tag);
236 ret->family = AF_UNSPEC;
237 strncpy(ret->hostname, host, lenof(ret->hostname));
238 ret->hostname[lenof(ret->hostname)-1] = '\0';
242 ret->addresses = NULL;
247 static int sk_nextaddr(SockAddr addr)
250 if (addr->ai && addr->ai->ai_next) {
251 addr->ai = addr->ai->ai_next;
252 addr->family = addr->ai->ai_family;
257 if (addr->curraddr+1 < addr->naddresses) {
266 void sk_getaddr(SockAddr addr, char *buf, int buflen)
269 if (addr->family == AF_UNSPEC) {
270 strncpy(buf, addr->hostname, buflen);
271 buf[buflen-1] = '\0';
274 if (getnameinfo(addr->ai->ai_addr, addr->ai->ai_addrlen, buf, buflen,
275 NULL, 0, NI_NUMERICHOST) != 0) {
277 strncat(buf, "<unknown>", buflen - 1);
281 assert(addr->family == AF_INET);
282 a.s_addr = htonl(addr->addresses[addr->curraddr]);
283 strncpy(buf, inet_ntoa(a), buflen);
284 buf[buflen-1] = '\0';
289 int sk_hostname_is_local(char *name)
291 return !strcmp(name, "localhost");
294 #define ipv4_is_loopback(addr) \
295 (((addr).s_addr & htonl(0xff000000)) == htonl(0x7f000000))
297 static int sockaddr_is_loopback(struct sockaddr *sa)
299 struct sockaddr_in *sin;
301 struct sockaddr_in6 *sin6;
304 switch (sa->sa_family) {
306 sin = (struct sockaddr_in *)sa;
307 return ipv4_is_loopback(sin->sin_addr);
310 sin6 = (struct sockaddr_in6 *)sa;
311 return IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr);
320 int sk_address_is_local(SockAddr addr)
323 if (addr->family == AF_UNSPEC)
324 return 0; /* we don't know; assume not */
327 return sockaddr_is_loopback(addr->ai->ai_addr);
330 assert(addr->family == AF_INET);
331 a.s_addr = htonl(addr->addresses[addr->curraddr]);
332 return ipv4_is_loopback(a);
337 int sk_addrtype(SockAddr addr)
339 return (addr->family == AF_INET ? ADDRTYPE_IPV4 :
341 addr->family == AF_INET6 ? ADDRTYPE_IPV6 :
346 void sk_addrcopy(SockAddr addr, char *buf)
350 if (addr->family == AF_INET)
351 memcpy(buf, &((struct sockaddr_in *)addr->ai->ai_addr)->sin_addr,
352 sizeof(struct in_addr));
353 else if (addr->family == AF_INET6)
354 memcpy(buf, &((struct sockaddr_in6 *)addr->ai->ai_addr)->sin6_addr,
355 sizeof(struct in6_addr));
361 assert(addr->family == AF_INET);
362 a.s_addr = htonl(addr->addresses[addr->curraddr]);
363 memcpy(buf, (char*) &a.s_addr, 4);
367 void sk_addr_free(SockAddr addr)
371 if (addr->ais != NULL)
372 freeaddrinfo(addr->ais);
374 sfree(addr->addresses);
379 static Plug sk_tcp_plug(Socket sock, Plug p)
381 Actual_Socket s = (Actual_Socket) sock;
388 static void sk_tcp_flush(Socket s)
391 * We send data to the socket as soon as we can anyway,
392 * so we don't need to do anything here. :-)
396 static void sk_tcp_close(Socket s);
397 static int sk_tcp_write(Socket s, const char *data, int len);
398 static int sk_tcp_write_oob(Socket s, const char *data, int len);
399 static void sk_tcp_set_private_ptr(Socket s, void *ptr);
400 static void *sk_tcp_get_private_ptr(Socket s);
401 static void sk_tcp_set_frozen(Socket s, int is_frozen);
402 static const char *sk_tcp_socket_error(Socket s);
404 static struct socket_function_table tcp_fn_table = {
410 sk_tcp_set_private_ptr,
411 sk_tcp_get_private_ptr,
416 Socket sk_register(OSSocket sockfd, Plug plug)
421 * Create Socket structure.
423 ret = snew(struct Socket_tag);
424 ret->fn = &tcp_fn_table;
427 bufchain_init(&ret->output_data);
428 ret->writable = 1; /* to start with */
429 ret->sending_oob = 0;
431 ret->frozen_readable = 0;
432 ret->localhost_only = 0; /* unused, but best init anyway */
433 ret->pending_error = 0;
434 ret->oobpending = FALSE;
436 ret->parent = ret->child = NULL;
443 ret->error = strerror(errno);
455 static int try_connect(Actual_Socket sock)
459 struct sockaddr_in6 a6;
461 struct sockaddr_in a;
462 struct sockaddr_un au;
463 const struct sockaddr *sa;
469 * Remove the socket from the tree before we overwrite its
470 * internal socket id, because that forms part of the tree's
471 * sorting criterion. We'll add it back before exiting this
472 * function, whether we changed anything or not.
474 del234(sktree, sock);
479 plug_log(sock->plug, 0, sock->addr, sock->port, NULL, 0);
484 assert(sock->addr->family != AF_UNSPEC);
485 s = socket(sock->addr->family, SOCK_STREAM, 0);
495 if (sock->oobinline) {
497 setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));
502 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *) &b, sizeof(b));
505 if (sock->keepalive) {
507 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *) &b, sizeof(b));
511 * Bind to local address.
514 localport = 1023; /* count from 1023 downwards */
516 localport = 0; /* just use port 0 (ie kernel picks) */
518 /* BSD IP stacks need sockaddr_in zeroed before filling in */
519 memset(&a,'\0',sizeof(struct sockaddr_in));
521 memset(&a6,'\0',sizeof(struct sockaddr_in6));
524 /* We don't try to bind to a local address for UNIX domain sockets. (Why
525 * do we bother doing the bind when localport == 0 anyway?) */
526 if(sock->addr->family != AF_UNIX) {
527 /* Loop round trying to bind */
532 if (sock->addr->family == AF_INET6) {
533 /* XXX use getaddrinfo to get a local address? */
534 a6.sin6_family = AF_INET6;
535 a6.sin6_addr = in6addr_any;
536 a6.sin6_port = htons(localport);
537 retcode = bind(s, (struct sockaddr *) &a6, sizeof(a6));
541 assert(sock->addr->family == AF_INET);
542 a.sin_family = AF_INET;
543 a.sin_addr.s_addr = htonl(INADDR_ANY);
544 a.sin_port = htons(localport);
545 retcode = bind(s, (struct sockaddr *) &a, sizeof(a));
552 if (err != EADDRINUSE) /* failed, for a bad reason */
557 break; /* we're only looping once */
560 break; /* we might have got to the end */
568 * Connect to remote address.
570 switch(sock->addr->family) {
573 /* XXX would be better to have got getaddrinfo() to fill in the port. */
574 ((struct sockaddr_in *)sock->addr->ai->ai_addr)->sin_port =
576 sa = (const struct sockaddr *)sock->addr->ai->ai_addr;
577 salen = sock->addr->ai->ai_addrlen;
580 ((struct sockaddr_in *)sock->addr->ai->ai_addr)->sin_port =
582 sa = (const struct sockaddr *)sock->addr->ai->ai_addr;
583 salen = sock->addr->ai->ai_addrlen;
587 a.sin_family = AF_INET;
588 a.sin_addr.s_addr = htonl(sock->addr->addresses[sock->addr->curraddr]);
589 a.sin_port = htons((short) sock->port);
590 sa = (const struct sockaddr *)&a;
595 assert(sock->port == 0); /* to catch confused people */
596 assert(strlen(sock->addr->hostname) < sizeof au.sun_path);
597 memset(&au, 0, sizeof au);
598 au.sun_family = AF_UNIX;
599 strcpy(au.sun_path, sock->addr->hostname);
600 sa = (const struct sockaddr *)&au;
605 assert(0 && "unknown address family");
606 exit(1); /* XXX: GCC doesn't understand assert() on some systems. */
609 fl = fcntl(s, F_GETFL);
611 fcntl(s, F_SETFL, fl | O_NONBLOCK);
613 if ((connect(s, sa, salen)) < 0) {
614 if ( errno != EINPROGRESS ) {
620 * If we _don't_ get EWOULDBLOCK, the connect has completed
621 * and we should set the socket as connected and writable.
632 * No matter what happened, put the socket back in the tree.
634 add234(sktree, sock);
637 plug_log(sock->plug, 1, sock->addr, sock->port, strerror(err), err);
641 Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
642 int nodelay, int keepalive, Plug plug)
648 * Create Socket structure.
650 ret = snew(struct Socket_tag);
651 ret->fn = &tcp_fn_table;
654 bufchain_init(&ret->output_data);
655 ret->connected = 0; /* to start with */
656 ret->writable = 0; /* to start with */
657 ret->sending_oob = 0;
659 ret->frozen_readable = 0;
660 ret->localhost_only = 0; /* unused, but best init anyway */
661 ret->pending_error = 0;
662 ret->parent = ret->child = NULL;
663 ret->oobpending = FALSE;
667 ret->oobinline = oobinline;
668 ret->nodelay = nodelay;
669 ret->keepalive = keepalive;
670 ret->privport = privport;
675 err = try_connect(ret);
676 } while (err && sk_nextaddr(ret->addr));
679 ret->error = strerror(err);
684 Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only, int orig_address_family)
688 struct addrinfo hints, *ai;
690 struct sockaddr_in6 a6;
692 struct sockaddr *addr;
694 struct sockaddr_in a;
701 * Create Socket structure.
703 ret = snew(struct Socket_tag);
704 ret->fn = &tcp_fn_table;
707 bufchain_init(&ret->output_data);
708 ret->writable = 0; /* to start with */
709 ret->sending_oob = 0;
711 ret->frozen_readable = 0;
712 ret->localhost_only = local_host_only;
713 ret->pending_error = 0;
714 ret->parent = ret->child = NULL;
715 ret->oobpending = FALSE;
720 * Translate address_family from platform-independent constants
721 * into local reality.
723 address_family = (orig_address_family == ADDRTYPE_IPV4 ? AF_INET :
725 orig_address_family == ADDRTYPE_IPV6 ? AF_INET6 :
730 /* Let's default to IPv6.
731 * If the stack doesn't support IPv6, we will fall back to IPv4. */
732 if (address_family == AF_UNSPEC) address_family = AF_INET6;
734 /* No other choice, default to IPv4 */
735 if (address_family == AF_UNSPEC) address_family = AF_INET;
741 s = socket(address_family, SOCK_STREAM, 0);
744 /* If the host doesn't support IPv6 try fallback to IPv4. */
745 if (s < 0 && address_family == AF_INET6) {
746 address_family = AF_INET;
747 s = socket(address_family, SOCK_STREAM, 0);
752 ret->error = strerror(errno);
760 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
763 addr = NULL; addrlen = -1; /* placate optimiser */
765 if (srcaddr != NULL) {
767 hints.ai_flags = AI_NUMERICHOST;
768 hints.ai_family = address_family;
769 hints.ai_socktype = SOCK_STREAM;
770 hints.ai_protocol = 0;
771 hints.ai_addrlen = 0;
772 hints.ai_addr = NULL;
773 hints.ai_canonname = NULL;
774 hints.ai_next = NULL;
775 assert(port >= 0 && port <= 99999);
776 sprintf(portstr, "%d", port);
777 retcode = getaddrinfo(srcaddr, portstr, &hints, &ai);
780 addrlen = ai->ai_addrlen;
783 memset(&a,'\0',sizeof(struct sockaddr_in));
784 a.sin_family = AF_INET;
785 a.sin_port = htons(port);
786 a.sin_addr.s_addr = inet_addr(srcaddr);
787 if (a.sin_addr.s_addr != (in_addr_t)(-1)) {
788 /* Override localhost_only with specified listen addr. */
789 ret->localhost_only = ipv4_is_loopback(a.sin_addr);
791 addr = (struct sockaddr *)&a;
799 if (address_family == AF_INET6) {
800 memset(&a6,'\0',sizeof(struct sockaddr_in6));
801 a6.sin6_family = AF_INET6;
802 a6.sin6_port = htons(port);
804 a6.sin6_addr = in6addr_loopback;
806 a6.sin6_addr = in6addr_any;
807 addr = (struct sockaddr *)&a6;
808 addrlen = sizeof(a6);
812 memset(&a,'\0',sizeof(struct sockaddr_in));
813 a.sin_family = AF_INET;
814 a.sin_port = htons(port);
816 a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
818 a.sin_addr.s_addr = htonl(INADDR_ANY);
819 addr = (struct sockaddr *)&a;
824 retcode = bind(s, addr, addrlen);
827 ret->error = strerror(errno);
831 if (listen(s, SOMAXCONN) < 0) {
833 ret->error = strerror(errno);
839 * If we were given ADDRTYPE_UNSPEC, we must also create an
840 * IPv4 listening socket and link it to this one.
842 if (address_family == AF_INET6 && orig_address_family == ADDRTYPE_UNSPEC) {
845 other = (Actual_Socket) sk_newlistener(srcaddr, port, plug,
846 local_host_only, ADDRTYPE_IPV4);
853 /* If we couldn't create a listening socket on IPv4 as well
854 * as IPv6, we must return an error overall. */
857 return (Socket) other;
871 static void sk_tcp_close(Socket sock)
873 Actual_Socket s = (Actual_Socket) sock;
876 sk_tcp_close((Socket)s->child);
882 sk_addr_free(s->addr);
886 void *sk_getxdmdata(void *sock, int *lenp)
888 Actual_Socket s = (Actual_Socket) sock;
890 struct sockaddr_in addr;
892 struct sockaddr_storage addr;
893 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr;
895 struct sockaddr *sa = (struct sockaddr *)&addr;
896 struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
899 static unsigned int unix_addr = 0xFFFFFFFF;
902 * We must check that this socket really _is_ an Actual_Socket.
904 if (s->fn != &tcp_fn_table)
905 return NULL; /* failure */
907 addrlen = sizeof(addr);
908 if (getsockname(s->s, sa, &addrlen) < 0)
910 switch(sa->sa_family) {
913 buf = snewn(*lenp, char);
914 PUT_32BIT_MSB_FIRST(buf, ntohl(sin->sin_addr.s_addr));
915 PUT_16BIT_MSB_FIRST(buf+4, ntohs(sin->sin_port));
920 buf = snewn(*lenp, char);
921 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
922 memcpy(buf, sin6->sin6_addr.s6_addr + 12, 4);
923 PUT_16BIT_MSB_FIRST(buf+4, ntohs(sin6->sin6_port));
925 /* This is stupid, but it's what XLib does. */
931 buf = snewn(*lenp, char);
932 PUT_32BIT_MSB_FIRST(buf, unix_addr--);
933 PUT_16BIT_MSB_FIRST(buf+4, getpid());
946 * The function which tries to send on a socket once it's deemed
949 void try_send(Actual_Socket s)
951 while (s->sending_oob || bufchain_size(&s->output_data) > 0) {
957 if (s->sending_oob) {
958 urgentflag = MSG_OOB;
959 len = s->sending_oob;
963 bufchain_prefix(&s->output_data, &data, &len);
965 nsent = send(s->s, data, len, urgentflag);
966 noise_ultralight(nsent);
968 err = (nsent < 0 ? errno : 0);
969 if (err == EWOULDBLOCK) {
971 * Perfectly normal: we've sent all we can for the moment.
977 * We unfortunately can't just call plug_closing(),
978 * because it's quite likely that we're currently
979 * _in_ a call from the code we'd be calling back
980 * to, so we'd have to make half the SSH code
981 * reentrant. Instead we flag a pending error on
982 * the socket, to be dealt with (by calling
983 * plug_closing()) at some suitable future moment.
985 s->pending_error = err;
989 if (s->sending_oob) {
991 memmove(s->oobdata, s->oobdata+nsent, len-nsent);
992 s->sending_oob = len - nsent;
997 bufchain_consume(&s->output_data, nsent);
1004 static int sk_tcp_write(Socket sock, const char *buf, int len)
1006 Actual_Socket s = (Actual_Socket) sock;
1009 * Add the data to the buffer list on the socket.
1011 bufchain_add(&s->output_data, buf, len);
1014 * Now try sending from the start of the buffer list.
1020 * Update the select() status to correctly reflect whether or
1021 * not we should be selecting for write.
1025 return bufchain_size(&s->output_data);
1028 static int sk_tcp_write_oob(Socket sock, const char *buf, int len)
1030 Actual_Socket s = (Actual_Socket) sock;
1033 * Replace the buffer list on the socket with the data.
1035 bufchain_clear(&s->output_data);
1036 assert(len <= sizeof(s->oobdata));
1037 memcpy(s->oobdata, buf, len);
1038 s->sending_oob = len;
1041 * Now try sending from the start of the buffer list.
1047 * Update the select() status to correctly reflect whether or
1048 * not we should be selecting for write.
1052 return s->sending_oob;
1055 static int net_select_result(int fd, int event)
1058 char buf[20480]; /* nice big buffer for plenty of speed */
1062 /* Find the Socket structure */
1063 s = find234(sktree, &fd, cmpforsearch);
1065 return 1; /* boggle */
1067 noise_ultralight(event);
1070 case 4: /* exceptional */
1071 if (!s->oobinline) {
1073 * On a non-oobinline socket, this indicates that we
1074 * can immediately perform an OOB read and get back OOB
1075 * data, which we will send to the back end with
1076 * type==2 (urgent data).
1078 ret = recv(s->s, buf, sizeof(buf), MSG_OOB);
1079 noise_ultralight(ret);
1081 return plug_closing(s->plug,
1082 ret == 0 ? "Internal networking trouble" :
1083 strerror(errno), errno, 0);
1086 * Receiving actual data on a socket means we can
1087 * stop falling back through the candidate
1088 * addresses to connect to.
1091 sk_addr_free(s->addr);
1094 return plug_receive(s->plug, 2, buf, ret);
1100 * If we reach here, this is an oobinline socket, which
1101 * means we should set s->oobpending and then deal with it
1102 * when we get called for the readability event (which
1103 * should also occur).
1105 s->oobpending = TRUE;
1107 case 1: /* readable; also acceptance */
1110 * On a listening socket, the readability event means a
1111 * connection is ready to be accepted.
1114 struct sockaddr_in ss;
1116 struct sockaddr_storage ss;
1118 socklen_t addrlen = sizeof(ss);
1119 int t; /* socket of connection */
1122 memset(&ss, 0, addrlen);
1123 t = accept(s->s, (struct sockaddr *)&ss, &addrlen);
1128 fl = fcntl(t, F_GETFL);
1130 fcntl(t, F_SETFL, fl | O_NONBLOCK);
1132 if (s->localhost_only &&
1133 !sockaddr_is_loopback((struct sockaddr *)&ss)) {
1134 close(t); /* someone let nonlocal through?! */
1135 } else if (plug_accepting(s->plug, t)) {
1136 close(t); /* denied or error */
1142 * If we reach here, this is not a listening socket, so
1143 * readability really means readability.
1146 /* In the case the socket is still frozen, we don't even bother */
1148 s->frozen_readable = 1;
1153 * We have received data on the socket. For an oobinline
1154 * socket, this might be data _before_ an urgent pointer,
1155 * in which case we send it to the back end with type==1
1156 * (data prior to urgent).
1158 if (s->oobinline && s->oobpending) {
1160 if (ioctl(s->s, SIOCATMARK, &atmark) == 0 && atmark)
1161 s->oobpending = FALSE; /* clear this indicator */
1165 ret = recv(s->s, buf, s->oobpending ? 1 : sizeof(buf), 0);
1166 noise_ultralight(ret);
1168 if (errno == EWOULDBLOCK) {
1174 * An error at this point _might_ be an error reported
1175 * by a non-blocking connect(). So before we return a
1176 * panic status to the user, let's just see whether
1181 plug_log(s->plug, 1, s->addr, s->port, strerror(err), err);
1182 while (s->addr && sk_nextaddr(s->addr)) {
1183 err = try_connect(s);
1187 return plug_closing(s->plug, strerror(err), err, 0);
1188 } else if (0 == ret) {
1189 return plug_closing(s->plug, NULL, 0, 0);
1192 * Receiving actual data on a socket means we can
1193 * stop falling back through the candidate
1194 * addresses to connect to.
1197 sk_addr_free(s->addr);
1200 return plug_receive(s->plug, atmark ? 0 : 1, buf, ret);
1203 case 2: /* writable */
1204 if (!s->connected) {
1206 * select() reports a socket as _writable_ when an
1207 * asynchronous connection is completed.
1209 s->connected = s->writable = 1;
1213 int bufsize_before, bufsize_after;
1215 bufsize_before = s->sending_oob + bufchain_size(&s->output_data);
1217 bufsize_after = s->sending_oob + bufchain_size(&s->output_data);
1218 if (bufsize_after < bufsize_before)
1219 plug_sent(s->plug, bufsize_after);
1228 * Deal with socket errors detected in try_send().
1230 void net_pending_errors(void)
1236 * This might be a fiddly business, because it's just possible
1237 * that handling a pending error on one socket might cause
1238 * others to be closed. (I can't think of any reason this might
1239 * happen in current SSH implementation, but to maintain
1240 * generality of this network layer I'll assume the worst.)
1242 * So what we'll do is search the socket list for _one_ socket
1243 * with a pending error, and then handle it, and then search
1244 * the list again _from the beginning_. Repeat until we make a
1245 * pass with no socket errors present. That way we are
1246 * protected against the socket list changing under our feet.
1250 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
1251 if (s->pending_error) {
1253 * An error has occurred on this socket. Pass it to the
1256 plug_closing(s->plug, strerror(s->pending_error),
1257 s->pending_error, 0);
1265 * Each socket abstraction contains a `void *' private field in
1266 * which the client can keep state.
1268 static void sk_tcp_set_private_ptr(Socket sock, void *ptr)
1270 Actual_Socket s = (Actual_Socket) sock;
1271 s->private_ptr = ptr;
1274 static void *sk_tcp_get_private_ptr(Socket sock)
1276 Actual_Socket s = (Actual_Socket) sock;
1277 return s->private_ptr;
1281 * Special error values are returned from sk_namelookup and sk_new
1282 * if there's a problem. These functions extract an error message,
1283 * or return NULL if there's no problem.
1285 const char *sk_addr_error(SockAddr addr)
1289 static const char *sk_tcp_socket_error(Socket sock)
1291 Actual_Socket s = (Actual_Socket) sock;
1295 static void sk_tcp_set_frozen(Socket sock, int is_frozen)
1297 Actual_Socket s = (Actual_Socket) sock;
1298 if (s->frozen == is_frozen)
1300 s->frozen = is_frozen;
1301 if (!is_frozen && s->frozen_readable) {
1303 recv(s->s, &c, 1, MSG_PEEK);
1305 s->frozen_readable = 0;
1309 static void uxsel_tell(Actual_Socket s)
1313 rwx |= 1; /* read == accept */
1316 rwx |= 2; /* write == connect */
1317 if (s->connected && !s->frozen)
1318 rwx |= 1 | 4; /* read, except */
1319 if (bufchain_size(&s->output_data))
1320 rwx |= 2; /* write */
1322 uxsel_set(s->s, rwx, net_select_result);
1325 int net_service_lookup(char *service)
1328 se = getservbyname(service, NULL);
1330 return ntohs(se->s_port);
1335 SockAddr platform_get_x11_unix_address(const char *display, int displaynum,
1336 char **canonicalname)
1338 SockAddr ret = snew(struct SockAddr_tag);
1341 memset(ret, 0, sizeof *ret);
1342 ret->family = AF_UNIX;
1344 * Mac OS X Leopard uses an innovative X display naming
1345 * convention in which the entire display name is the path to
1346 * the Unix socket, including the trailing :0 which only
1347 * _looks_ like a display number. Heuristically, I think
1348 * detecting this by means of a leading slash ought to be
1351 if (display[0] == '/') {
1352 n = snprintf(ret->hostname, sizeof ret->hostname,
1355 n = snprintf(ret->hostname, sizeof ret->hostname,
1356 "%s%d", X11_UNIX_PATH, displaynum);
1359 ret->error = "snprintf failed";
1360 else if(n >= sizeof ret->hostname)
1361 ret->error = "X11 UNIX name too long";
1363 *canonicalname = dupstr(ret->hostname);
1365 ret->ai = ret->ais = NULL;
1367 ret->addresses = NULL;
1368 ret->curraddr = ret->naddresses = 0;