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 * Mutable state that goes with a SockAddr: stores information
46 * about where in the list of candidate IP(v*) addresses we've
49 typedef struct SockAddrStep_tag SockAddrStep;
50 struct SockAddrStep_tag {
52 struct addrinfo *ai; /* steps along addr->ais */
58 struct socket_function_table *fn;
59 /* the above variable absolutely *must* be the first in this structure */
65 int connected; /* irrelevant for listening sockets */
67 int frozen; /* this causes readability notifications to be ignored */
68 int frozen_readable; /* this means we missed at least one readability
69 * notification while we were frozen */
70 int localhost_only; /* for listening sockets */
73 int oobpending; /* is there OOB data available to read? */
75 int pending_error; /* in case send() returns error */
77 int nodelay, keepalive; /* for connect()-type sockets */
78 int privport, port; /* and again */
82 * We sometimes need pairs of Socket structures to be linked:
83 * if we are listening on the same IPv6 and v4 port, for
84 * example. So here we define `parent' and `child' pointers to
87 Actual_Socket parent, child;
93 enum { UNRESOLVED, UNIX, IP } superfamily;
95 struct addrinfo *ais; /* Addresses IPv6 style. */
97 unsigned long *addresses; /* Addresses IPv4 style. */
100 char hostname[512]; /* Store an unresolved host name. */
104 * Which address family this address belongs to. AF_INET for IPv4;
105 * AF_INET6 for IPv6; AF_UNSPEC indicates that name resolution has
106 * not been done and a simple host name is held in this SockAddr
110 #define SOCKADDR_FAMILY(addr, step) \
111 ((addr)->superfamily == UNRESOLVED ? AF_UNSPEC : \
112 (addr)->superfamily == UNIX ? AF_UNIX : \
113 (step).ai ? (step).ai->ai_family : AF_INET)
115 #define SOCKADDR_FAMILY(addr, step) \
116 ((addr)->superfamily == UNRESOLVED ? AF_UNSPEC : \
117 (addr)->superfamily == UNIX ? AF_UNIX : AF_INET)
121 * Start a SockAddrStep structure to step through multiple
125 #define START_STEP(addr, step) \
126 ((step).ai = (addr)->ais, (step).curraddr = 0)
128 #define START_STEP(addr, step) \
129 ((step).curraddr = 0)
132 static tree234 *sktree;
134 static void uxsel_tell(Actual_Socket s);
136 static int cmpfortree(void *av, void *bv)
138 Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv;
139 int as = a->s, bs = b->s;
151 static int cmpforsearch(void *av, void *bv)
153 Actual_Socket b = (Actual_Socket) bv;
154 int as = *(int *)av, bs = b->s;
164 sktree = newtree234(cmpfortree);
167 void sk_cleanup(void)
173 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
179 SockAddr sk_namelookup(const char *host, char **canonicalname, int address_family)
181 SockAddr ret = snew(struct SockAddr_tag);
183 struct addrinfo hints;
187 struct hostent *h = NULL;
192 /* Clear the structure and default to IPv4. */
193 memset(ret, 0, sizeof(struct SockAddr_tag));
194 ret->superfamily = UNRESOLVED;
200 hints.ai_flags = AI_CANONNAME;
201 hints.ai_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
202 address_family == ADDRTYPE_IPV6 ? AF_INET6 :
204 hints.ai_socktype = SOCK_STREAM;
205 hints.ai_protocol = 0;
206 hints.ai_addrlen = 0;
207 hints.ai_addr = NULL;
208 hints.ai_canonname = NULL;
209 hints.ai_next = NULL;
210 err = getaddrinfo(host, NULL, &hints, &ret->ais);
212 ret->error = gai_strerror(err);
215 ret->superfamily = IP;
217 if (ret->ais->ai_canonname != NULL)
218 strncat(realhost, ret->ais->ai_canonname, sizeof(realhost) - 1);
220 strncat(realhost, host, sizeof(realhost) - 1);
222 if ((a = inet_addr(host)) == (unsigned long)(in_addr_t)(-1)) {
224 * Otherwise use the IPv4-only gethostbyname... (NOTE:
225 * we don't use gethostbyname as a fallback!)
227 if (ret->superfamily == UNRESOLVED) {
228 /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
229 if ( (h = gethostbyname(host)) )
230 ret->superfamily = IP;
232 if (ret->superfamily == UNRESOLVED) {
233 ret->error = (h_errno == HOST_NOT_FOUND ||
234 h_errno == NO_DATA ||
235 h_errno == NO_ADDRESS ? "Host does not exist" :
236 h_errno == TRY_AGAIN ?
237 "Temporary name service failure" :
238 "gethostbyname: unknown error");
241 /* This way we are always sure the h->h_name is valid :) */
242 strncpy(realhost, h->h_name, sizeof(realhost));
243 for (n = 0; h->h_addr_list[n]; n++);
244 ret->addresses = snewn(n, unsigned long);
246 for (n = 0; n < ret->naddresses; n++) {
247 memcpy(&a, h->h_addr_list[n], sizeof(a));
248 ret->addresses[n] = ntohl(a);
252 * This must be a numeric IPv4 address because it caused a
253 * success return from inet_addr.
255 ret->superfamily = IP;
256 strncpy(realhost, host, sizeof(realhost));
257 ret->addresses = snew(unsigned long);
259 ret->addresses[0] = ntohl(a);
262 realhost[lenof(realhost)-1] = '\0';
263 *canonicalname = snewn(1+strlen(realhost), char);
264 strcpy(*canonicalname, realhost);
268 SockAddr sk_nonamelookup(const char *host)
270 SockAddr ret = snew(struct SockAddr_tag);
272 ret->superfamily = UNRESOLVED;
273 strncpy(ret->hostname, host, lenof(ret->hostname));
274 ret->hostname[lenof(ret->hostname)-1] = '\0';
278 ret->addresses = NULL;
284 static int sk_nextaddr(SockAddr addr, SockAddrStep *step)
287 if (step->ai && step->ai->ai_next) {
288 step->ai = step->ai->ai_next;
293 if (step->curraddr+1 < addr->naddresses) {
302 void sk_getaddr(SockAddr addr, char *buf, int buflen)
304 /* XXX not clear what we should return for Unix-domain sockets; let's
305 * hope the question never arises */
306 assert(addr->superfamily != UNIX);
307 if (addr->superfamily == UNRESOLVED) {
308 strncpy(buf, addr->hostname, buflen);
309 buf[buflen-1] = '\0';
312 if (getnameinfo(addr->ais->ai_addr, addr->ais->ai_addrlen, buf, buflen,
313 NULL, 0, NI_NUMERICHOST) != 0) {
315 strncat(buf, "<unknown>", buflen - 1);
320 START_STEP(addr, step);
321 assert(SOCKADDR_FAMILY(addr, step) == AF_INET);
322 a.s_addr = htonl(addr->addresses[0]);
323 strncpy(buf, inet_ntoa(a), buflen);
324 buf[buflen-1] = '\0';
329 int sk_hostname_is_local(char *name)
331 return !strcmp(name, "localhost") ||
332 !strcmp(name, "::1") ||
333 !strncmp(name, "127.", 4);
336 #define ipv4_is_loopback(addr) \
337 (((addr).s_addr & htonl(0xff000000)) == htonl(0x7f000000))
339 static int sockaddr_is_loopback(struct sockaddr *sa)
341 struct sockaddr_in *sin;
343 struct sockaddr_in6 *sin6;
346 switch (sa->sa_family) {
348 sin = (struct sockaddr_in *)sa;
349 return ipv4_is_loopback(sin->sin_addr);
352 sin6 = (struct sockaddr_in6 *)sa;
353 return IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr);
362 int sk_address_is_local(SockAddr addr)
364 if (addr->superfamily == UNRESOLVED)
365 return 0; /* we don't know; assume not */
366 else if (addr->superfamily == UNIX)
370 return sockaddr_is_loopback(addr->ais->ai_addr);
374 START_STEP(addr, step);
375 assert(SOCKADDR_FAMILY(addr, step) == AF_INET);
376 a.s_addr = htonl(addr->addresses[0]);
377 return ipv4_is_loopback(a);
382 int sk_addrtype(SockAddr addr)
386 START_STEP(addr, step);
387 family = SOCKADDR_FAMILY(addr, step);
389 return (family == AF_INET ? ADDRTYPE_IPV4 :
391 family == AF_INET6 ? ADDRTYPE_IPV6 :
396 void sk_addrcopy(SockAddr addr, char *buf)
400 START_STEP(addr, step);
401 family = SOCKADDR_FAMILY(addr, step);
404 if (family == AF_INET)
405 memcpy(buf, &((struct sockaddr_in *)step.ai->ai_addr)->sin_addr,
406 sizeof(struct in_addr));
407 else if (family == AF_INET6)
408 memcpy(buf, &((struct sockaddr_in6 *)step.ai->ai_addr)->sin6_addr,
409 sizeof(struct in6_addr));
415 assert(family == AF_INET);
416 a.s_addr = htonl(addr->addresses[step.curraddr]);
417 memcpy(buf, (char*) &a.s_addr, 4);
421 void sk_addr_free(SockAddr addr)
423 if (--addr->refcount > 0)
426 if (addr->ais != NULL)
427 freeaddrinfo(addr->ais);
429 sfree(addr->addresses);
434 SockAddr sk_addr_dup(SockAddr addr)
440 static Plug sk_tcp_plug(Socket sock, Plug p)
442 Actual_Socket s = (Actual_Socket) sock;
449 static void sk_tcp_flush(Socket s)
452 * We send data to the socket as soon as we can anyway,
453 * so we don't need to do anything here. :-)
457 static void sk_tcp_close(Socket s);
458 static int sk_tcp_write(Socket s, const char *data, int len);
459 static int sk_tcp_write_oob(Socket s, const char *data, int len);
460 static void sk_tcp_set_private_ptr(Socket s, void *ptr);
461 static void *sk_tcp_get_private_ptr(Socket s);
462 static void sk_tcp_set_frozen(Socket s, int is_frozen);
463 static const char *sk_tcp_socket_error(Socket s);
465 static struct socket_function_table tcp_fn_table = {
471 sk_tcp_set_private_ptr,
472 sk_tcp_get_private_ptr,
477 Socket sk_register(OSSocket sockfd, Plug plug)
482 * Create Socket structure.
484 ret = snew(struct Socket_tag);
485 ret->fn = &tcp_fn_table;
488 bufchain_init(&ret->output_data);
489 ret->writable = 1; /* to start with */
490 ret->sending_oob = 0;
492 ret->frozen_readable = 0;
493 ret->localhost_only = 0; /* unused, but best init anyway */
494 ret->pending_error = 0;
495 ret->oobpending = FALSE;
497 ret->parent = ret->child = NULL;
504 ret->error = strerror(errno);
516 static int try_connect(Actual_Socket sock)
520 struct sockaddr_in6 a6;
522 struct sockaddr_in a;
523 struct sockaddr_un au;
524 const struct sockaddr *sa;
527 int fl, salen, family;
530 * Remove the socket from the tree before we overwrite its
531 * internal socket id, because that forms part of the tree's
532 * sorting criterion. We'll add it back before exiting this
533 * function, whether we changed anything or not.
535 del234(sktree, sock);
540 plug_log(sock->plug, 0, sock->addr, sock->port, NULL, 0);
545 family = SOCKADDR_FAMILY(sock->addr, sock->step);
546 assert(family != AF_UNSPEC);
547 s = socket(family, SOCK_STREAM, 0);
557 if (sock->oobinline) {
559 setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));
564 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *) &b, sizeof(b));
567 if (sock->keepalive) {
569 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *) &b, sizeof(b));
573 * Bind to local address.
576 localport = 1023; /* count from 1023 downwards */
578 localport = 0; /* just use port 0 (ie kernel picks) */
580 /* BSD IP stacks need sockaddr_in zeroed before filling in */
581 memset(&a,'\0',sizeof(struct sockaddr_in));
583 memset(&a6,'\0',sizeof(struct sockaddr_in6));
586 /* We don't try to bind to a local address for UNIX domain sockets. (Why
587 * do we bother doing the bind when localport == 0 anyway?) */
588 if (family != AF_UNIX) {
589 /* Loop round trying to bind */
594 if (family == AF_INET6) {
595 /* XXX use getaddrinfo to get a local address? */
596 a6.sin6_family = AF_INET6;
597 a6.sin6_addr = in6addr_any;
598 a6.sin6_port = htons(localport);
599 retcode = bind(s, (struct sockaddr *) &a6, sizeof(a6));
603 assert(family == AF_INET);
604 a.sin_family = AF_INET;
605 a.sin_addr.s_addr = htonl(INADDR_ANY);
606 a.sin_port = htons(localport);
607 retcode = bind(s, (struct sockaddr *) &a, sizeof(a));
614 if (err != EADDRINUSE) /* failed, for a bad reason */
619 break; /* we're only looping once */
622 break; /* we might have got to the end */
630 * Connect to remote address.
635 /* XXX would be better to have got getaddrinfo() to fill in the port. */
636 ((struct sockaddr_in *)sock->step.ai->ai_addr)->sin_port =
638 sa = (const struct sockaddr *)sock->step.ai->ai_addr;
639 salen = sock->step.ai->ai_addrlen;
642 ((struct sockaddr_in *)sock->step.ai->ai_addr)->sin_port =
644 sa = (const struct sockaddr *)sock->step.ai->ai_addr;
645 salen = sock->step.ai->ai_addrlen;
649 a.sin_family = AF_INET;
650 a.sin_addr.s_addr = htonl(sock->addr->addresses[sock->step.curraddr]);
651 a.sin_port = htons((short) sock->port);
652 sa = (const struct sockaddr *)&a;
657 assert(sock->port == 0); /* to catch confused people */
658 assert(strlen(sock->addr->hostname) < sizeof au.sun_path);
659 memset(&au, 0, sizeof au);
660 au.sun_family = AF_UNIX;
661 strcpy(au.sun_path, sock->addr->hostname);
662 sa = (const struct sockaddr *)&au;
667 assert(0 && "unknown address family");
668 exit(1); /* XXX: GCC doesn't understand assert() on some systems. */
671 fl = fcntl(s, F_GETFL);
673 fcntl(s, F_SETFL, fl | O_NONBLOCK);
675 if ((connect(s, sa, salen)) < 0) {
676 if ( errno != EINPROGRESS ) {
682 * If we _don't_ get EWOULDBLOCK, the connect has completed
683 * and we should set the socket as connected and writable.
694 * No matter what happened, put the socket back in the tree.
696 add234(sktree, sock);
699 plug_log(sock->plug, 1, sock->addr, sock->port, strerror(err), err);
703 Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
704 int nodelay, int keepalive, Plug plug)
710 * Create Socket structure.
712 ret = snew(struct Socket_tag);
713 ret->fn = &tcp_fn_table;
716 bufchain_init(&ret->output_data);
717 ret->connected = 0; /* to start with */
718 ret->writable = 0; /* to start with */
719 ret->sending_oob = 0;
721 ret->frozen_readable = 0;
722 ret->localhost_only = 0; /* unused, but best init anyway */
723 ret->pending_error = 0;
724 ret->parent = ret->child = NULL;
725 ret->oobpending = FALSE;
728 START_STEP(ret->addr, ret->step);
730 ret->oobinline = oobinline;
731 ret->nodelay = nodelay;
732 ret->keepalive = keepalive;
733 ret->privport = privport;
738 err = try_connect(ret);
739 } while (err && sk_nextaddr(ret->addr, &ret->step));
742 ret->error = strerror(err);
747 Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only, int orig_address_family)
751 struct addrinfo hints, *ai;
753 struct sockaddr_in6 a6;
755 struct sockaddr *addr;
757 struct sockaddr_in a;
764 * Create Socket structure.
766 ret = snew(struct Socket_tag);
767 ret->fn = &tcp_fn_table;
770 bufchain_init(&ret->output_data);
771 ret->writable = 0; /* to start with */
772 ret->sending_oob = 0;
774 ret->frozen_readable = 0;
775 ret->localhost_only = local_host_only;
776 ret->pending_error = 0;
777 ret->parent = ret->child = NULL;
778 ret->oobpending = FALSE;
783 * Translate address_family from platform-independent constants
784 * into local reality.
786 address_family = (orig_address_family == ADDRTYPE_IPV4 ? AF_INET :
788 orig_address_family == ADDRTYPE_IPV6 ? AF_INET6 :
793 /* Let's default to IPv6.
794 * If the stack doesn't support IPv6, we will fall back to IPv4. */
795 if (address_family == AF_UNSPEC) address_family = AF_INET6;
797 /* No other choice, default to IPv4 */
798 if (address_family == AF_UNSPEC) address_family = AF_INET;
804 s = socket(address_family, SOCK_STREAM, 0);
807 /* If the host doesn't support IPv6 try fallback to IPv4. */
808 if (s < 0 && address_family == AF_INET6) {
809 address_family = AF_INET;
810 s = socket(address_family, SOCK_STREAM, 0);
815 ret->error = strerror(errno);
823 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
826 addr = NULL; addrlen = -1; /* placate optimiser */
828 if (srcaddr != NULL) {
830 hints.ai_flags = AI_NUMERICHOST;
831 hints.ai_family = address_family;
832 hints.ai_socktype = SOCK_STREAM;
833 hints.ai_protocol = 0;
834 hints.ai_addrlen = 0;
835 hints.ai_addr = NULL;
836 hints.ai_canonname = NULL;
837 hints.ai_next = NULL;
838 assert(port >= 0 && port <= 99999);
839 sprintf(portstr, "%d", port);
840 retcode = getaddrinfo(srcaddr, portstr, &hints, &ai);
843 addrlen = ai->ai_addrlen;
846 memset(&a,'\0',sizeof(struct sockaddr_in));
847 a.sin_family = AF_INET;
848 a.sin_port = htons(port);
849 a.sin_addr.s_addr = inet_addr(srcaddr);
850 if (a.sin_addr.s_addr != (in_addr_t)(-1)) {
851 /* Override localhost_only with specified listen addr. */
852 ret->localhost_only = ipv4_is_loopback(a.sin_addr);
854 addr = (struct sockaddr *)&a;
862 if (address_family == AF_INET6) {
863 memset(&a6,'\0',sizeof(struct sockaddr_in6));
864 a6.sin6_family = AF_INET6;
865 a6.sin6_port = htons(port);
867 a6.sin6_addr = in6addr_loopback;
869 a6.sin6_addr = in6addr_any;
870 addr = (struct sockaddr *)&a6;
871 addrlen = sizeof(a6);
875 memset(&a,'\0',sizeof(struct sockaddr_in));
876 a.sin_family = AF_INET;
877 a.sin_port = htons(port);
879 a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
881 a.sin_addr.s_addr = htonl(INADDR_ANY);
882 addr = (struct sockaddr *)&a;
887 retcode = bind(s, addr, addrlen);
890 ret->error = strerror(errno);
894 if (listen(s, SOMAXCONN) < 0) {
896 ret->error = strerror(errno);
902 * If we were given ADDRTYPE_UNSPEC, we must also create an
903 * IPv4 listening socket and link it to this one.
905 if (address_family == AF_INET6 && orig_address_family == ADDRTYPE_UNSPEC) {
908 other = (Actual_Socket) sk_newlistener(srcaddr, port, plug,
909 local_host_only, ADDRTYPE_IPV4);
916 /* If we couldn't create a listening socket on IPv4 as well
917 * as IPv6, we must return an error overall. */
920 return (Socket) other;
934 static void sk_tcp_close(Socket sock)
936 Actual_Socket s = (Actual_Socket) sock;
939 sk_tcp_close((Socket)s->child);
945 sk_addr_free(s->addr);
949 void *sk_getxdmdata(void *sock, int *lenp)
951 Actual_Socket s = (Actual_Socket) sock;
953 struct sockaddr_in addr;
955 struct sockaddr_storage addr;
956 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr;
958 struct sockaddr *sa = (struct sockaddr *)&addr;
959 struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
962 static unsigned int unix_addr = 0xFFFFFFFF;
965 * We must check that this socket really _is_ an Actual_Socket.
967 if (s->fn != &tcp_fn_table)
968 return NULL; /* failure */
970 addrlen = sizeof(addr);
971 if (getsockname(s->s, sa, &addrlen) < 0)
973 switch(sa->sa_family) {
976 buf = snewn(*lenp, char);
977 PUT_32BIT_MSB_FIRST(buf, ntohl(sin->sin_addr.s_addr));
978 PUT_16BIT_MSB_FIRST(buf+4, ntohs(sin->sin_port));
983 buf = snewn(*lenp, char);
984 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
985 memcpy(buf, sin6->sin6_addr.s6_addr + 12, 4);
986 PUT_16BIT_MSB_FIRST(buf+4, ntohs(sin6->sin6_port));
988 /* This is stupid, but it's what XLib does. */
994 buf = snewn(*lenp, char);
995 PUT_32BIT_MSB_FIRST(buf, unix_addr--);
996 PUT_16BIT_MSB_FIRST(buf+4, getpid());
1009 * The function which tries to send on a socket once it's deemed
1012 void try_send(Actual_Socket s)
1014 while (s->sending_oob || bufchain_size(&s->output_data) > 0) {
1018 int len, urgentflag;
1020 if (s->sending_oob) {
1021 urgentflag = MSG_OOB;
1022 len = s->sending_oob;
1026 bufchain_prefix(&s->output_data, &data, &len);
1028 nsent = send(s->s, data, len, urgentflag);
1029 noise_ultralight(nsent);
1031 err = (nsent < 0 ? errno : 0);
1032 if (err == EWOULDBLOCK) {
1034 * Perfectly normal: we've sent all we can for the moment.
1036 s->writable = FALSE;
1040 * We unfortunately can't just call plug_closing(),
1041 * because it's quite likely that we're currently
1042 * _in_ a call from the code we'd be calling back
1043 * to, so we'd have to make half the SSH code
1044 * reentrant. Instead we flag a pending error on
1045 * the socket, to be dealt with (by calling
1046 * plug_closing()) at some suitable future moment.
1048 s->pending_error = err;
1052 if (s->sending_oob) {
1054 memmove(s->oobdata, s->oobdata+nsent, len-nsent);
1055 s->sending_oob = len - nsent;
1060 bufchain_consume(&s->output_data, nsent);
1067 static int sk_tcp_write(Socket sock, const char *buf, int len)
1069 Actual_Socket s = (Actual_Socket) sock;
1072 * Add the data to the buffer list on the socket.
1074 bufchain_add(&s->output_data, buf, len);
1077 * Now try sending from the start of the buffer list.
1083 * Update the select() status to correctly reflect whether or
1084 * not we should be selecting for write.
1088 return bufchain_size(&s->output_data);
1091 static int sk_tcp_write_oob(Socket sock, const char *buf, int len)
1093 Actual_Socket s = (Actual_Socket) sock;
1096 * Replace the buffer list on the socket with the data.
1098 bufchain_clear(&s->output_data);
1099 assert(len <= sizeof(s->oobdata));
1100 memcpy(s->oobdata, buf, len);
1101 s->sending_oob = len;
1104 * Now try sending from the start of the buffer list.
1110 * Update the select() status to correctly reflect whether or
1111 * not we should be selecting for write.
1115 return s->sending_oob;
1118 static int net_select_result(int fd, int event)
1121 char buf[20480]; /* nice big buffer for plenty of speed */
1125 /* Find the Socket structure */
1126 s = find234(sktree, &fd, cmpforsearch);
1128 return 1; /* boggle */
1130 noise_ultralight(event);
1133 case 4: /* exceptional */
1134 if (!s->oobinline) {
1136 * On a non-oobinline socket, this indicates that we
1137 * can immediately perform an OOB read and get back OOB
1138 * data, which we will send to the back end with
1139 * type==2 (urgent data).
1141 ret = recv(s->s, buf, sizeof(buf), MSG_OOB);
1142 noise_ultralight(ret);
1144 return plug_closing(s->plug,
1145 ret == 0 ? "Internal networking trouble" :
1146 strerror(errno), errno, 0);
1149 * Receiving actual data on a socket means we can
1150 * stop falling back through the candidate
1151 * addresses to connect to.
1154 sk_addr_free(s->addr);
1157 return plug_receive(s->plug, 2, buf, ret);
1163 * If we reach here, this is an oobinline socket, which
1164 * means we should set s->oobpending and then deal with it
1165 * when we get called for the readability event (which
1166 * should also occur).
1168 s->oobpending = TRUE;
1170 case 1: /* readable; also acceptance */
1173 * On a listening socket, the readability event means a
1174 * connection is ready to be accepted.
1177 struct sockaddr_in ss;
1179 struct sockaddr_storage ss;
1181 socklen_t addrlen = sizeof(ss);
1182 int t; /* socket of connection */
1185 memset(&ss, 0, addrlen);
1186 t = accept(s->s, (struct sockaddr *)&ss, &addrlen);
1191 fl = fcntl(t, F_GETFL);
1193 fcntl(t, F_SETFL, fl | O_NONBLOCK);
1195 if (s->localhost_only &&
1196 !sockaddr_is_loopback((struct sockaddr *)&ss)) {
1197 close(t); /* someone let nonlocal through?! */
1198 } else if (plug_accepting(s->plug, t)) {
1199 close(t); /* denied or error */
1205 * If we reach here, this is not a listening socket, so
1206 * readability really means readability.
1209 /* In the case the socket is still frozen, we don't even bother */
1211 s->frozen_readable = 1;
1216 * We have received data on the socket. For an oobinline
1217 * socket, this might be data _before_ an urgent pointer,
1218 * in which case we send it to the back end with type==1
1219 * (data prior to urgent).
1221 if (s->oobinline && s->oobpending) {
1223 if (ioctl(s->s, SIOCATMARK, &atmark) == 0 && atmark)
1224 s->oobpending = FALSE; /* clear this indicator */
1228 ret = recv(s->s, buf, s->oobpending ? 1 : sizeof(buf), 0);
1229 noise_ultralight(ret);
1231 if (errno == EWOULDBLOCK) {
1237 * An error at this point _might_ be an error reported
1238 * by a non-blocking connect(). So before we return a
1239 * panic status to the user, let's just see whether
1244 plug_log(s->plug, 1, s->addr, s->port, strerror(err), err);
1245 while (s->addr && sk_nextaddr(s->addr, &s->step)) {
1246 err = try_connect(s);
1250 return plug_closing(s->plug, strerror(err), err, 0);
1251 } else if (0 == ret) {
1252 return plug_closing(s->plug, NULL, 0, 0);
1255 * Receiving actual data on a socket means we can
1256 * stop falling back through the candidate
1257 * addresses to connect to.
1260 sk_addr_free(s->addr);
1263 return plug_receive(s->plug, atmark ? 0 : 1, buf, ret);
1266 case 2: /* writable */
1267 if (!s->connected) {
1269 * select() reports a socket as _writable_ when an
1270 * asynchronous connection is completed.
1272 s->connected = s->writable = 1;
1276 int bufsize_before, bufsize_after;
1278 bufsize_before = s->sending_oob + bufchain_size(&s->output_data);
1280 bufsize_after = s->sending_oob + bufchain_size(&s->output_data);
1281 if (bufsize_after < bufsize_before)
1282 plug_sent(s->plug, bufsize_after);
1291 * Deal with socket errors detected in try_send().
1293 void net_pending_errors(void)
1299 * This might be a fiddly business, because it's just possible
1300 * that handling a pending error on one socket might cause
1301 * others to be closed. (I can't think of any reason this might
1302 * happen in current SSH implementation, but to maintain
1303 * generality of this network layer I'll assume the worst.)
1305 * So what we'll do is search the socket list for _one_ socket
1306 * with a pending error, and then handle it, and then search
1307 * the list again _from the beginning_. Repeat until we make a
1308 * pass with no socket errors present. That way we are
1309 * protected against the socket list changing under our feet.
1313 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
1314 if (s->pending_error) {
1316 * An error has occurred on this socket. Pass it to the
1319 plug_closing(s->plug, strerror(s->pending_error),
1320 s->pending_error, 0);
1328 * Each socket abstraction contains a `void *' private field in
1329 * which the client can keep state.
1331 static void sk_tcp_set_private_ptr(Socket sock, void *ptr)
1333 Actual_Socket s = (Actual_Socket) sock;
1334 s->private_ptr = ptr;
1337 static void *sk_tcp_get_private_ptr(Socket sock)
1339 Actual_Socket s = (Actual_Socket) sock;
1340 return s->private_ptr;
1344 * Special error values are returned from sk_namelookup and sk_new
1345 * if there's a problem. These functions extract an error message,
1346 * or return NULL if there's no problem.
1348 const char *sk_addr_error(SockAddr addr)
1352 static const char *sk_tcp_socket_error(Socket sock)
1354 Actual_Socket s = (Actual_Socket) sock;
1358 static void sk_tcp_set_frozen(Socket sock, int is_frozen)
1360 Actual_Socket s = (Actual_Socket) sock;
1361 if (s->frozen == is_frozen)
1363 s->frozen = is_frozen;
1364 if (!is_frozen && s->frozen_readable) {
1366 recv(s->s, &c, 1, MSG_PEEK);
1368 s->frozen_readable = 0;
1372 static void uxsel_tell(Actual_Socket s)
1376 rwx |= 1; /* read == accept */
1379 rwx |= 2; /* write == connect */
1380 if (s->connected && !s->frozen)
1381 rwx |= 1 | 4; /* read, except */
1382 if (bufchain_size(&s->output_data))
1383 rwx |= 2; /* write */
1385 uxsel_set(s->s, rwx, net_select_result);
1388 int net_service_lookup(char *service)
1391 se = getservbyname(service, NULL);
1393 return ntohs(se->s_port);
1398 SockAddr platform_get_x11_unix_address(const char *sockpath, int displaynum)
1400 SockAddr ret = snew(struct SockAddr_tag);
1403 memset(ret, 0, sizeof *ret);
1404 ret->superfamily = UNIX;
1406 * In special circumstances (notably Mac OS X Leopard), we'll
1407 * have been passed an explicit Unix socket path.
1410 n = snprintf(ret->hostname, sizeof ret->hostname,
1413 n = snprintf(ret->hostname, sizeof ret->hostname,
1414 "%s%d", X11_UNIX_PATH, displaynum);
1418 ret->error = "snprintf failed";
1419 else if (n >= sizeof ret->hostname)
1420 ret->error = "X11 UNIX name too long";
1425 ret->addresses = NULL;
1426 ret->naddresses = 0;