2 * Unix networking abstraction.
\r
11 #include <sys/types.h>
\r
12 #include <sys/socket.h>
\r
13 #include <sys/ioctl.h>
\r
14 #include <arpa/inet.h>
\r
15 #include <netinet/in.h>
\r
16 #include <netinet/tcp.h>
\r
20 #define DEFINE_PLUG_METHOD_MACROS
\r
22 #include "network.h"
\r
23 #include "tree234.h"
\r
25 /* Solaris needs <sys/sockio.h> for SIOCATMARK. */
\r
27 #include <sys/sockio.h>
\r
30 #ifndef X11_UNIX_PATH
\r
31 # define X11_UNIX_PATH "/tmp/.X11-unix/X"
\r
35 * Access to sockaddr types without breaking C strict aliasing rules.
\r
37 union sockaddr_union {
\r
39 struct sockaddr_in storage;
\r
41 struct sockaddr_storage storage;
\r
42 struct sockaddr_in6 sin6;
\r
45 struct sockaddr_in sin;
\r
46 struct sockaddr_un su;
\r
50 * We used to typedef struct Socket_tag *Socket.
\r
52 * Since we have made the networking abstraction slightly more
\r
53 * abstract, Socket no longer means a tcp socket (it could mean
\r
54 * an ssl socket). So now we must use Actual_Socket when we know
\r
55 * we are talking about a tcp socket.
\r
57 typedef struct Socket_tag *Actual_Socket;
\r
60 * Mutable state that goes with a SockAddr: stores information
\r
61 * about where in the list of candidate IP(v*) addresses we've
\r
64 typedef struct SockAddrStep_tag SockAddrStep;
\r
65 struct SockAddrStep_tag {
\r
67 struct addrinfo *ai; /* steps along addr->ais */
\r
73 struct socket_function_table *fn;
\r
74 /* the above variable absolutely *must* be the first in this structure */
\r
79 bufchain output_data;
\r
80 int connected; /* irrelevant for listening sockets */
\r
82 int frozen; /* this causes readability notifications to be ignored */
\r
83 int frozen_readable; /* this means we missed at least one readability
\r
84 * notification while we were frozen */
\r
85 int localhost_only; /* for listening sockets */
\r
88 int oobpending; /* is there OOB data available to read? */
\r
90 int pending_error; /* in case send() returns error */
\r
92 int nodelay, keepalive; /* for connect()-type sockets */
\r
93 int privport, port; /* and again */
\r
97 * We sometimes need pairs of Socket structures to be linked:
\r
98 * if we are listening on the same IPv6 and v4 port, for
\r
99 * example. So here we define `parent' and `child' pointers to
\r
102 Actual_Socket parent, child;
\r
105 struct SockAddr_tag {
\r
108 enum { UNRESOLVED, UNIX, IP } superfamily;
\r
110 struct addrinfo *ais; /* Addresses IPv6 style. */
\r
112 unsigned long *addresses; /* Addresses IPv4 style. */
\r
115 char hostname[512]; /* Store an unresolved host name. */
\r
119 * Which address family this address belongs to. AF_INET for IPv4;
\r
120 * AF_INET6 for IPv6; AF_UNSPEC indicates that name resolution has
\r
121 * not been done and a simple host name is held in this SockAddr
\r
125 #define SOCKADDR_FAMILY(addr, step) \
\r
126 ((addr)->superfamily == UNRESOLVED ? AF_UNSPEC : \
\r
127 (addr)->superfamily == UNIX ? AF_UNIX : \
\r
128 (step).ai ? (step).ai->ai_family : AF_INET)
\r
130 #define SOCKADDR_FAMILY(addr, step) \
\r
131 ((addr)->superfamily == UNRESOLVED ? AF_UNSPEC : \
\r
132 (addr)->superfamily == UNIX ? AF_UNIX : AF_INET)
\r
136 * Start a SockAddrStep structure to step through multiple
\r
140 #define START_STEP(addr, step) \
\r
141 ((step).ai = (addr)->ais, (step).curraddr = 0)
\r
143 #define START_STEP(addr, step) \
\r
144 ((step).curraddr = 0)
\r
147 static tree234 *sktree;
\r
149 static void uxsel_tell(Actual_Socket s);
\r
151 static int cmpfortree(void *av, void *bv)
\r
153 Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv;
\r
154 int as = a->s, bs = b->s;
\r
166 static int cmpforsearch(void *av, void *bv)
\r
168 Actual_Socket b = (Actual_Socket) bv;
\r
169 int as = *(int *)av, bs = b->s;
\r
179 sktree = newtree234(cmpfortree);
\r
182 void sk_cleanup(void)
\r
188 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
\r
194 SockAddr sk_namelookup(const char *host, char **canonicalname, int address_family)
\r
196 SockAddr ret = snew(struct SockAddr_tag);
\r
198 struct addrinfo hints;
\r
202 struct hostent *h = NULL;
\r
205 char realhost[8192];
\r
207 /* Clear the structure and default to IPv4. */
\r
208 memset(ret, 0, sizeof(struct SockAddr_tag));
\r
209 ret->superfamily = UNRESOLVED;
\r
215 hints.ai_flags = AI_CANONNAME;
\r
216 hints.ai_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
\r
217 address_family == ADDRTYPE_IPV6 ? AF_INET6 :
\r
219 hints.ai_socktype = SOCK_STREAM;
\r
220 hints.ai_protocol = 0;
\r
221 hints.ai_addrlen = 0;
\r
222 hints.ai_addr = NULL;
\r
223 hints.ai_canonname = NULL;
\r
224 hints.ai_next = NULL;
\r
225 err = getaddrinfo(host, NULL, &hints, &ret->ais);
\r
227 ret->error = gai_strerror(err);
\r
230 ret->superfamily = IP;
\r
232 if (ret->ais->ai_canonname != NULL)
\r
233 strncat(realhost, ret->ais->ai_canonname, sizeof(realhost) - 1);
\r
235 strncat(realhost, host, sizeof(realhost) - 1);
\r
237 if ((a = inet_addr(host)) == (unsigned long)(in_addr_t)(-1)) {
\r
239 * Otherwise use the IPv4-only gethostbyname... (NOTE:
\r
240 * we don't use gethostbyname as a fallback!)
\r
242 if (ret->superfamily == UNRESOLVED) {
\r
243 /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
\r
244 if ( (h = gethostbyname(host)) )
\r
245 ret->superfamily = IP;
\r
247 if (ret->superfamily == UNRESOLVED) {
\r
248 ret->error = (h_errno == HOST_NOT_FOUND ||
\r
249 h_errno == NO_DATA ||
\r
250 h_errno == NO_ADDRESS ? "Host does not exist" :
\r
251 h_errno == TRY_AGAIN ?
\r
252 "Temporary name service failure" :
\r
253 "gethostbyname: unknown error");
\r
256 /* This way we are always sure the h->h_name is valid :) */
\r
257 strncpy(realhost, h->h_name, sizeof(realhost));
\r
258 for (n = 0; h->h_addr_list[n]; n++);
\r
259 ret->addresses = snewn(n, unsigned long);
\r
260 ret->naddresses = n;
\r
261 for (n = 0; n < ret->naddresses; n++) {
\r
262 memcpy(&a, h->h_addr_list[n], sizeof(a));
\r
263 ret->addresses[n] = ntohl(a);
\r
267 * This must be a numeric IPv4 address because it caused a
\r
268 * success return from inet_addr.
\r
270 ret->superfamily = IP;
\r
271 strncpy(realhost, host, sizeof(realhost));
\r
272 ret->addresses = snew(unsigned long);
\r
273 ret->naddresses = 1;
\r
274 ret->addresses[0] = ntohl(a);
\r
277 realhost[lenof(realhost)-1] = '\0';
\r
278 *canonicalname = snewn(1+strlen(realhost), char);
\r
279 strcpy(*canonicalname, realhost);
\r
283 SockAddr sk_nonamelookup(const char *host)
\r
285 SockAddr ret = snew(struct SockAddr_tag);
\r
287 ret->superfamily = UNRESOLVED;
\r
288 strncpy(ret->hostname, host, lenof(ret->hostname));
\r
289 ret->hostname[lenof(ret->hostname)-1] = '\0';
\r
293 ret->addresses = NULL;
\r
299 static int sk_nextaddr(SockAddr addr, SockAddrStep *step)
\r
302 if (step->ai && step->ai->ai_next) {
\r
303 step->ai = step->ai->ai_next;
\r
308 if (step->curraddr+1 < addr->naddresses) {
\r
317 void sk_getaddr(SockAddr addr, char *buf, int buflen)
\r
319 /* XXX not clear what we should return for Unix-domain sockets; let's
\r
320 * hope the question never arises */
\r
321 assert(addr->superfamily != UNIX);
\r
322 if (addr->superfamily == UNRESOLVED) {
\r
323 strncpy(buf, addr->hostname, buflen);
\r
324 buf[buflen-1] = '\0';
\r
327 if (getnameinfo(addr->ais->ai_addr, addr->ais->ai_addrlen, buf, buflen,
\r
328 NULL, 0, NI_NUMERICHOST) != 0) {
\r
330 strncat(buf, "<unknown>", buflen - 1);
\r
335 START_STEP(addr, step);
\r
336 assert(SOCKADDR_FAMILY(addr, step) == AF_INET);
\r
337 a.s_addr = htonl(addr->addresses[0]);
\r
338 strncpy(buf, inet_ntoa(a), buflen);
\r
339 buf[buflen-1] = '\0';
\r
344 int sk_hostname_is_local(char *name)
\r
346 return !strcmp(name, "localhost") ||
\r
347 !strcmp(name, "::1") ||
\r
348 !strncmp(name, "127.", 4);
\r
351 #define ipv4_is_loopback(addr) \
\r
352 (((addr).s_addr & htonl(0xff000000)) == htonl(0x7f000000))
\r
354 static int sockaddr_is_loopback(struct sockaddr *sa)
\r
356 union sockaddr_union *u = (union sockaddr_union *)sa;
\r
357 switch (u->sa.sa_family) {
\r
359 return ipv4_is_loopback(u->sin.sin_addr);
\r
362 return IN6_IS_ADDR_LOOPBACK(&u->sin6.sin6_addr);
\r
371 int sk_address_is_local(SockAddr addr)
\r
373 if (addr->superfamily == UNRESOLVED)
\r
374 return 0; /* we don't know; assume not */
\r
375 else if (addr->superfamily == UNIX)
\r
379 return sockaddr_is_loopback(addr->ais->ai_addr);
\r
383 START_STEP(addr, step);
\r
384 assert(SOCKADDR_FAMILY(addr, step) == AF_INET);
\r
385 a.s_addr = htonl(addr->addresses[0]);
\r
386 return ipv4_is_loopback(a);
\r
391 int sk_addrtype(SockAddr addr)
\r
395 START_STEP(addr, step);
\r
396 family = SOCKADDR_FAMILY(addr, step);
\r
398 return (family == AF_INET ? ADDRTYPE_IPV4 :
\r
400 family == AF_INET6 ? ADDRTYPE_IPV6 :
\r
405 void sk_addrcopy(SockAddr addr, char *buf)
\r
409 START_STEP(addr, step);
\r
410 family = SOCKADDR_FAMILY(addr, step);
\r
413 if (family == AF_INET)
\r
414 memcpy(buf, &((struct sockaddr_in *)step.ai->ai_addr)->sin_addr,
\r
415 sizeof(struct in_addr));
\r
416 else if (family == AF_INET6)
\r
417 memcpy(buf, &((struct sockaddr_in6 *)step.ai->ai_addr)->sin6_addr,
\r
418 sizeof(struct in6_addr));
\r
424 assert(family == AF_INET);
\r
425 a.s_addr = htonl(addr->addresses[step.curraddr]);
\r
426 memcpy(buf, (char*) &a.s_addr, 4);
\r
430 void sk_addr_free(SockAddr addr)
\r
432 if (--addr->refcount > 0)
\r
435 if (addr->ais != NULL)
\r
436 freeaddrinfo(addr->ais);
\r
438 sfree(addr->addresses);
\r
443 SockAddr sk_addr_dup(SockAddr addr)
\r
449 static Plug sk_tcp_plug(Socket sock, Plug p)
\r
451 Actual_Socket s = (Actual_Socket) sock;
\r
452 Plug ret = s->plug;
\r
458 static void sk_tcp_flush(Socket s)
\r
461 * We send data to the socket as soon as we can anyway,
\r
462 * so we don't need to do anything here. :-)
\r
466 static void sk_tcp_close(Socket s);
\r
467 static int sk_tcp_write(Socket s, const char *data, int len);
\r
468 static int sk_tcp_write_oob(Socket s, const char *data, int len);
\r
469 static void sk_tcp_set_private_ptr(Socket s, void *ptr);
\r
470 static void *sk_tcp_get_private_ptr(Socket s);
\r
471 static void sk_tcp_set_frozen(Socket s, int is_frozen);
\r
472 static const char *sk_tcp_socket_error(Socket s);
\r
474 static struct socket_function_table tcp_fn_table = {
\r
480 sk_tcp_set_private_ptr,
\r
481 sk_tcp_get_private_ptr,
\r
483 sk_tcp_socket_error
\r
486 Socket sk_register(OSSocket sockfd, Plug plug)
\r
491 * Create Socket structure.
\r
493 ret = snew(struct Socket_tag);
\r
494 ret->fn = &tcp_fn_table;
\r
497 bufchain_init(&ret->output_data);
\r
498 ret->writable = 1; /* to start with */
\r
499 ret->sending_oob = 0;
\r
501 ret->frozen_readable = 0;
\r
502 ret->localhost_only = 0; /* unused, but best init anyway */
\r
503 ret->pending_error = 0;
\r
504 ret->oobpending = FALSE;
\r
506 ret->parent = ret->child = NULL;
\r
508 ret->connected = 1;
\r
513 ret->error = strerror(errno);
\r
514 return (Socket) ret;
\r
517 ret->oobinline = 0;
\r
520 add234(sktree, ret);
\r
522 return (Socket) ret;
\r
525 static int try_connect(Actual_Socket sock)
\r
528 union sockaddr_union u;
\r
529 const union sockaddr_union *sa;
\r
532 int fl, salen, family;
\r
535 * Remove the socket from the tree before we overwrite its
\r
536 * internal socket id, because that forms part of the tree's
\r
537 * sorting criterion. We'll add it back before exiting this
\r
538 * function, whether we changed anything or not.
\r
540 del234(sktree, sock);
\r
545 plug_log(sock->plug, 0, sock->addr, sock->port, NULL, 0);
\r
550 family = SOCKADDR_FAMILY(sock->addr, sock->step);
\r
551 assert(family != AF_UNSPEC);
\r
552 s = socket(family, SOCK_STREAM, 0);
\r
562 if (sock->oobinline) {
\r
564 setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));
\r
567 if (sock->nodelay) {
\r
569 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *) &b, sizeof(b));
\r
572 if (sock->keepalive) {
\r
574 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *) &b, sizeof(b));
\r
578 * Bind to local address.
\r
580 if (sock->privport)
\r
581 localport = 1023; /* count from 1023 downwards */
\r
583 localport = 0; /* just use port 0 (ie kernel picks) */
\r
585 /* BSD IP stacks need sockaddr_in zeroed before filling in */
\r
586 memset(&u,'\0',sizeof(u));
\r
588 /* We don't try to bind to a local address for UNIX domain sockets. (Why
\r
589 * do we bother doing the bind when localport == 0 anyway?) */
\r
590 if (family != AF_UNIX) {
\r
591 /* Loop round trying to bind */
\r
596 if (family == AF_INET6) {
\r
597 /* XXX use getaddrinfo to get a local address? */
\r
598 u.sin6.sin6_family = AF_INET6;
\r
599 u.sin6.sin6_addr = in6addr_any;
\r
600 u.sin6.sin6_port = htons(localport);
\r
601 retcode = bind(s, &u.sa, sizeof(u.sin6));
\r
605 assert(family == AF_INET);
\r
606 u.sin.sin_family = AF_INET;
\r
607 u.sin.sin_addr.s_addr = htonl(INADDR_ANY);
\r
608 u.sin.sin_port = htons(localport);
\r
609 retcode = bind(s, &u.sa, sizeof(u.sin));
\r
611 if (retcode >= 0) {
\r
616 if (err != EADDRINUSE) /* failed, for a bad reason */
\r
620 if (localport == 0)
\r
621 break; /* we're only looping once */
\r
623 if (localport == 0)
\r
624 break; /* we might have got to the end */
\r
632 * Connect to remote address.
\r
637 /* XXX would be better to have got getaddrinfo() to fill in the port. */
\r
638 ((struct sockaddr_in *)sock->step.ai->ai_addr)->sin_port =
\r
640 sa = (const union sockaddr_union *)sock->step.ai->ai_addr;
\r
641 salen = sock->step.ai->ai_addrlen;
\r
644 ((struct sockaddr_in *)sock->step.ai->ai_addr)->sin_port =
\r
646 sa = (const union sockaddr_union *)sock->step.ai->ai_addr;
\r
647 salen = sock->step.ai->ai_addrlen;
\r
651 u.sin.sin_family = AF_INET;
\r
652 u.sin.sin_addr.s_addr = htonl(sock->addr->addresses[sock->step.curraddr]);
\r
653 u.sin.sin_port = htons((short) sock->port);
\r
655 salen = sizeof u.sin;
\r
659 assert(sock->port == 0); /* to catch confused people */
\r
660 assert(strlen(sock->addr->hostname) < sizeof u.su.sun_path);
\r
661 u.su.sun_family = AF_UNIX;
\r
662 strcpy(u.su.sun_path, sock->addr->hostname);
\r
664 salen = sizeof u.su;
\r
668 assert(0 && "unknown address family");
\r
669 exit(1); /* XXX: GCC doesn't understand assert() on some systems. */
\r
672 fl = fcntl(s, F_GETFL);
\r
674 fcntl(s, F_SETFL, fl | O_NONBLOCK);
\r
676 if ((connect(s, &(sa->sa), salen)) < 0) {
\r
677 if ( errno != EINPROGRESS ) {
\r
683 * If we _don't_ get EWOULDBLOCK, the connect has completed
\r
684 * and we should set the socket as connected and writable.
\r
686 sock->connected = 1;
\r
687 sock->writable = 1;
\r
695 * No matter what happened, put the socket back in the tree.
\r
697 add234(sktree, sock);
\r
700 plug_log(sock->plug, 1, sock->addr, sock->port, strerror(err), err);
\r
704 Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
\r
705 int nodelay, int keepalive, Plug plug)
\r
711 * Create Socket structure.
\r
713 ret = snew(struct Socket_tag);
\r
714 ret->fn = &tcp_fn_table;
\r
717 bufchain_init(&ret->output_data);
\r
718 ret->connected = 0; /* to start with */
\r
719 ret->writable = 0; /* to start with */
\r
720 ret->sending_oob = 0;
\r
722 ret->frozen_readable = 0;
\r
723 ret->localhost_only = 0; /* unused, but best init anyway */
\r
724 ret->pending_error = 0;
\r
725 ret->parent = ret->child = NULL;
\r
726 ret->oobpending = FALSE;
\r
729 START_STEP(ret->addr, ret->step);
\r
731 ret->oobinline = oobinline;
\r
732 ret->nodelay = nodelay;
\r
733 ret->keepalive = keepalive;
\r
734 ret->privport = privport;
\r
739 err = try_connect(ret);
\r
740 } while (err && sk_nextaddr(ret->addr, &ret->step));
\r
743 ret->error = strerror(err);
\r
745 return (Socket) ret;
\r
748 Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only, int orig_address_family)
\r
752 struct addrinfo hints, *ai;
\r
755 union sockaddr_union u;
\r
756 union sockaddr_union *addr;
\r
760 int address_family;
\r
764 * Create Socket structure.
\r
766 ret = snew(struct Socket_tag);
\r
767 ret->fn = &tcp_fn_table;
\r
770 bufchain_init(&ret->output_data);
\r
771 ret->writable = 0; /* to start with */
\r
772 ret->sending_oob = 0;
\r
774 ret->frozen_readable = 0;
\r
775 ret->localhost_only = local_host_only;
\r
776 ret->pending_error = 0;
\r
777 ret->parent = ret->child = NULL;
\r
778 ret->oobpending = FALSE;
\r
783 * Translate address_family from platform-independent constants
\r
784 * into local reality.
\r
786 address_family = (orig_address_family == ADDRTYPE_IPV4 ? AF_INET :
\r
788 orig_address_family == ADDRTYPE_IPV6 ? AF_INET6 :
\r
793 /* Let's default to IPv6.
\r
794 * If the stack doesn't support IPv6, we will fall back to IPv4. */
\r
795 if (address_family == AF_UNSPEC) address_family = AF_INET6;
\r
797 /* No other choice, default to IPv4 */
\r
798 if (address_family == AF_UNSPEC) address_family = AF_INET;
\r
804 s = socket(address_family, SOCK_STREAM, 0);
\r
807 /* If the host doesn't support IPv6 try fallback to IPv4. */
\r
808 if (s < 0 && address_family == AF_INET6) {
\r
809 address_family = AF_INET;
\r
810 s = socket(address_family, SOCK_STREAM, 0);
\r
815 ret->error = strerror(errno);
\r
816 return (Socket) ret;
\r
821 ret->oobinline = 0;
\r
823 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
\r
826 addr = NULL; addrlen = -1; /* placate optimiser */
\r
828 if (srcaddr != NULL) {
\r
830 hints.ai_flags = AI_NUMERICHOST;
\r
831 hints.ai_family = address_family;
\r
832 hints.ai_socktype = SOCK_STREAM;
\r
833 hints.ai_protocol = 0;
\r
834 hints.ai_addrlen = 0;
\r
835 hints.ai_addr = NULL;
\r
836 hints.ai_canonname = NULL;
\r
837 hints.ai_next = NULL;
\r
838 assert(port >= 0 && port <= 99999);
\r
839 sprintf(portstr, "%d", port);
\r
840 retcode = getaddrinfo(srcaddr, portstr, &hints, &ai);
\r
841 if (retcode == 0) {
\r
842 addr = (union sockaddr_union *)ai->ai_addr;
\r
843 addrlen = ai->ai_addrlen;
\r
846 memset(&u,'\0',sizeof u);
\r
847 u.sin.sin_family = AF_INET;
\r
848 u.sin.sin_port = htons(port);
\r
849 u.sin.sin_addr.s_addr = inet_addr(srcaddr);
\r
850 if (u.sin.sin_addr.s_addr != (in_addr_t)(-1)) {
\r
851 /* Override localhost_only with specified listen addr. */
\r
852 ret->localhost_only = ipv4_is_loopback(u.sin.sin_addr);
\r
855 addrlen = sizeof(u.sin);
\r
860 if (retcode != 0) {
\r
861 memset(&u,'\0',sizeof u);
\r
863 if (address_family == AF_INET6) {
\r
864 u.sin6.sin6_family = AF_INET6;
\r
865 u.sin6.sin6_port = htons(port);
\r
866 if (local_host_only)
\r
867 u.sin6.sin6_addr = in6addr_loopback;
\r
869 u.sin6.sin6_addr = in6addr_any;
\r
871 addrlen = sizeof(u.sin6);
\r
875 u.sin.sin_family = AF_INET;
\r
876 u.sin.sin_port = htons(port);
\r
877 if (local_host_only)
\r
878 u.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
\r
880 u.sin.sin_addr.s_addr = htonl(INADDR_ANY);
\r
882 addrlen = sizeof(u.sin);
\r
886 retcode = bind(s, &addr->sa, addrlen);
\r
889 ret->error = strerror(errno);
\r
890 return (Socket) ret;
\r
893 if (listen(s, SOMAXCONN) < 0) {
\r
895 ret->error = strerror(errno);
\r
896 return (Socket) ret;
\r
901 * If we were given ADDRTYPE_UNSPEC, we must also create an
\r
902 * IPv4 listening socket and link it to this one.
\r
904 if (address_family == AF_INET6 && orig_address_family == ADDRTYPE_UNSPEC) {
\r
905 Actual_Socket other;
\r
907 other = (Actual_Socket) sk_newlistener(srcaddr, port, plug,
\r
908 local_host_only, ADDRTYPE_IPV4);
\r
911 if (!other->error) {
\r
912 other->parent = ret;
\r
913 ret->child = other;
\r
915 /* If we couldn't create a listening socket on IPv4 as well
\r
916 * as IPv6, we must return an error overall. */
\r
919 return (Socket) other;
\r
928 add234(sktree, ret);
\r
930 return (Socket) ret;
\r
933 static void sk_tcp_close(Socket sock)
\r
935 Actual_Socket s = (Actual_Socket) sock;
\r
938 sk_tcp_close((Socket)s->child);
\r
944 sk_addr_free(s->addr);
\r
948 void *sk_getxdmdata(void *sock, int *lenp)
\r
950 Actual_Socket s = (Actual_Socket) sock;
\r
951 union sockaddr_union u;
\r
954 static unsigned int unix_addr = 0xFFFFFFFF;
\r
957 * We must check that this socket really _is_ an Actual_Socket.
\r
959 if (s->fn != &tcp_fn_table)
\r
960 return NULL; /* failure */
\r
962 addrlen = sizeof(u);
\r
963 if (getsockname(s->s, &u.sa, &addrlen) < 0)
\r
965 switch(u.sa.sa_family) {
\r
968 buf = snewn(*lenp, char);
\r
969 PUT_32BIT_MSB_FIRST(buf, ntohl(u.sin.sin_addr.s_addr));
\r
970 PUT_16BIT_MSB_FIRST(buf+4, ntohs(u.sin.sin_port));
\r
975 buf = snewn(*lenp, char);
\r
976 if (IN6_IS_ADDR_V4MAPPED(&u.sin6.sin6_addr)) {
\r
977 memcpy(buf, u.sin6.sin6_addr.s6_addr + 12, 4);
\r
978 PUT_16BIT_MSB_FIRST(buf+4, ntohs(u.sin6.sin6_port));
\r
980 /* This is stupid, but it's what XLib does. */
\r
986 buf = snewn(*lenp, char);
\r
987 PUT_32BIT_MSB_FIRST(buf, unix_addr--);
\r
988 PUT_16BIT_MSB_FIRST(buf+4, getpid());
\r
1001 * The function which tries to send on a socket once it's deemed
\r
1004 void try_send(Actual_Socket s)
\r
1006 while (s->sending_oob || bufchain_size(&s->output_data) > 0) {
\r
1010 int len, urgentflag;
\r
1012 if (s->sending_oob) {
\r
1013 urgentflag = MSG_OOB;
\r
1014 len = s->sending_oob;
\r
1015 data = &s->oobdata;
\r
1018 bufchain_prefix(&s->output_data, &data, &len);
\r
1020 nsent = send(s->s, data, len, urgentflag);
\r
1021 noise_ultralight(nsent);
\r
1023 err = (nsent < 0 ? errno : 0);
\r
1024 if (err == EWOULDBLOCK) {
\r
1026 * Perfectly normal: we've sent all we can for the moment.
\r
1028 s->writable = FALSE;
\r
1032 * We unfortunately can't just call plug_closing(),
\r
1033 * because it's quite likely that we're currently
\r
1034 * _in_ a call from the code we'd be calling back
\r
1035 * to, so we'd have to make half the SSH code
\r
1036 * reentrant. Instead we flag a pending error on
\r
1037 * the socket, to be dealt with (by calling
\r
1038 * plug_closing()) at some suitable future moment.
\r
1040 s->pending_error = err;
\r
1044 if (s->sending_oob) {
\r
1045 if (nsent < len) {
\r
1046 memmove(s->oobdata, s->oobdata+nsent, len-nsent);
\r
1047 s->sending_oob = len - nsent;
\r
1049 s->sending_oob = 0;
\r
1052 bufchain_consume(&s->output_data, nsent);
\r
1059 static int sk_tcp_write(Socket sock, const char *buf, int len)
\r
1061 Actual_Socket s = (Actual_Socket) sock;
\r
1064 * Add the data to the buffer list on the socket.
\r
1066 bufchain_add(&s->output_data, buf, len);
\r
1069 * Now try sending from the start of the buffer list.
\r
1075 * Update the select() status to correctly reflect whether or
\r
1076 * not we should be selecting for write.
\r
1080 return bufchain_size(&s->output_data);
\r
1083 static int sk_tcp_write_oob(Socket sock, const char *buf, int len)
\r
1085 Actual_Socket s = (Actual_Socket) sock;
\r
1088 * Replace the buffer list on the socket with the data.
\r
1090 bufchain_clear(&s->output_data);
\r
1091 assert(len <= sizeof(s->oobdata));
\r
1092 memcpy(s->oobdata, buf, len);
\r
1093 s->sending_oob = len;
\r
1096 * Now try sending from the start of the buffer list.
\r
1102 * Update the select() status to correctly reflect whether or
\r
1103 * not we should be selecting for write.
\r
1107 return s->sending_oob;
\r
1110 static int net_select_result(int fd, int event)
\r
1113 char buf[20480]; /* nice big buffer for plenty of speed */
\r
1117 /* Find the Socket structure */
\r
1118 s = find234(sktree, &fd, cmpforsearch);
\r
1120 return 1; /* boggle */
\r
1122 noise_ultralight(event);
\r
1125 case 4: /* exceptional */
\r
1126 if (!s->oobinline) {
\r
1128 * On a non-oobinline socket, this indicates that we
\r
1129 * can immediately perform an OOB read and get back OOB
\r
1130 * data, which we will send to the back end with
\r
1131 * type==2 (urgent data).
\r
1133 ret = recv(s->s, buf, sizeof(buf), MSG_OOB);
\r
1134 noise_ultralight(ret);
\r
1136 return plug_closing(s->plug,
\r
1137 ret == 0 ? "Internal networking trouble" :
\r
1138 strerror(errno), errno, 0);
\r
1141 * Receiving actual data on a socket means we can
\r
1142 * stop falling back through the candidate
\r
1143 * addresses to connect to.
\r
1146 sk_addr_free(s->addr);
\r
1149 return plug_receive(s->plug, 2, buf, ret);
\r
1155 * If we reach here, this is an oobinline socket, which
\r
1156 * means we should set s->oobpending and then deal with it
\r
1157 * when we get called for the readability event (which
\r
1158 * should also occur).
\r
1160 s->oobpending = TRUE;
\r
1162 case 1: /* readable; also acceptance */
\r
1163 if (s->listener) {
\r
1165 * On a listening socket, the readability event means a
\r
1166 * connection is ready to be accepted.
\r
1168 union sockaddr_union su;
\r
1169 socklen_t addrlen = sizeof(su);
\r
1170 int t; /* socket of connection */
\r
1173 memset(&su, 0, addrlen);
\r
1174 t = accept(s->s, &su.sa, &addrlen);
\r
1179 fl = fcntl(t, F_GETFL);
\r
1181 fcntl(t, F_SETFL, fl | O_NONBLOCK);
\r
1183 if (s->localhost_only &&
\r
1184 !sockaddr_is_loopback(&su.sa)) {
\r
1185 close(t); /* someone let nonlocal through?! */
\r
1186 } else if (plug_accepting(s->plug, t)) {
\r
1187 close(t); /* denied or error */
\r
1193 * If we reach here, this is not a listening socket, so
\r
1194 * readability really means readability.
\r
1197 /* In the case the socket is still frozen, we don't even bother */
\r
1199 s->frozen_readable = 1;
\r
1204 * We have received data on the socket. For an oobinline
\r
1205 * socket, this might be data _before_ an urgent pointer,
\r
1206 * in which case we send it to the back end with type==1
\r
1207 * (data prior to urgent).
\r
1209 if (s->oobinline && s->oobpending) {
\r
1211 if (ioctl(s->s, SIOCATMARK, &atmark) == 0 && atmark)
\r
1212 s->oobpending = FALSE; /* clear this indicator */
\r
1216 ret = recv(s->s, buf, s->oobpending ? 1 : sizeof(buf), 0);
\r
1217 noise_ultralight(ret);
\r
1219 if (errno == EWOULDBLOCK) {
\r
1225 * An error at this point _might_ be an error reported
\r
1226 * by a non-blocking connect(). So before we return a
\r
1227 * panic status to the user, let's just see whether
\r
1228 * that's the case.
\r
1232 plug_log(s->plug, 1, s->addr, s->port, strerror(err), err);
\r
1233 while (s->addr && sk_nextaddr(s->addr, &s->step)) {
\r
1234 err = try_connect(s);
\r
1238 return plug_closing(s->plug, strerror(err), err, 0);
\r
1239 } else if (0 == ret) {
\r
1240 return plug_closing(s->plug, NULL, 0, 0);
\r
1243 * Receiving actual data on a socket means we can
\r
1244 * stop falling back through the candidate
\r
1245 * addresses to connect to.
\r
1248 sk_addr_free(s->addr);
\r
1251 return plug_receive(s->plug, atmark ? 0 : 1, buf, ret);
\r
1254 case 2: /* writable */
\r
1255 if (!s->connected) {
\r
1257 * select() reports a socket as _writable_ when an
\r
1258 * asynchronous connection is completed.
\r
1260 s->connected = s->writable = 1;
\r
1264 int bufsize_before, bufsize_after;
\r
1266 bufsize_before = s->sending_oob + bufchain_size(&s->output_data);
\r
1268 bufsize_after = s->sending_oob + bufchain_size(&s->output_data);
\r
1269 if (bufsize_after < bufsize_before)
\r
1270 plug_sent(s->plug, bufsize_after);
\r
1279 * Deal with socket errors detected in try_send().
\r
1281 void net_pending_errors(void)
\r
1287 * This might be a fiddly business, because it's just possible
\r
1288 * that handling a pending error on one socket might cause
\r
1289 * others to be closed. (I can't think of any reason this might
\r
1290 * happen in current SSH implementation, but to maintain
\r
1291 * generality of this network layer I'll assume the worst.)
\r
1293 * So what we'll do is search the socket list for _one_ socket
\r
1294 * with a pending error, and then handle it, and then search
\r
1295 * the list again _from the beginning_. Repeat until we make a
\r
1296 * pass with no socket errors present. That way we are
\r
1297 * protected against the socket list changing under our feet.
\r
1301 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
\r
1302 if (s->pending_error) {
\r
1304 * An error has occurred on this socket. Pass it to the
\r
1307 plug_closing(s->plug, strerror(s->pending_error),
\r
1308 s->pending_error, 0);
\r
1316 * Each socket abstraction contains a `void *' private field in
\r
1317 * which the client can keep state.
\r
1319 static void sk_tcp_set_private_ptr(Socket sock, void *ptr)
\r
1321 Actual_Socket s = (Actual_Socket) sock;
\r
1322 s->private_ptr = ptr;
\r
1325 static void *sk_tcp_get_private_ptr(Socket sock)
\r
1327 Actual_Socket s = (Actual_Socket) sock;
\r
1328 return s->private_ptr;
\r
1332 * Special error values are returned from sk_namelookup and sk_new
\r
1333 * if there's a problem. These functions extract an error message,
\r
1334 * or return NULL if there's no problem.
\r
1336 const char *sk_addr_error(SockAddr addr)
\r
1338 return addr->error;
\r
1340 static const char *sk_tcp_socket_error(Socket sock)
\r
1342 Actual_Socket s = (Actual_Socket) sock;
\r
1346 static void sk_tcp_set_frozen(Socket sock, int is_frozen)
\r
1348 Actual_Socket s = (Actual_Socket) sock;
\r
1349 if (s->frozen == is_frozen)
\r
1351 s->frozen = is_frozen;
\r
1352 if (!is_frozen && s->frozen_readable) {
\r
1354 recv(s->s, &c, 1, MSG_PEEK);
\r
1356 s->frozen_readable = 0;
\r
1360 static void uxsel_tell(Actual_Socket s)
\r
1363 if (s->listener) {
\r
1364 rwx |= 1; /* read == accept */
\r
1366 if (!s->connected)
\r
1367 rwx |= 2; /* write == connect */
\r
1368 if (s->connected && !s->frozen)
\r
1369 rwx |= 1 | 4; /* read, except */
\r
1370 if (bufchain_size(&s->output_data))
\r
1371 rwx |= 2; /* write */
\r
1373 uxsel_set(s->s, rwx, net_select_result);
\r
1376 int net_service_lookup(char *service)
\r
1378 struct servent *se;
\r
1379 se = getservbyname(service, NULL);
\r
1381 return ntohs(se->s_port);
\r
1386 char *get_hostname(void)
\r
1389 char *hostname = NULL;
\r
1392 hostname = sresize(hostname, len, char);
\r
1393 if ((gethostname(hostname, len) < 0) &&
\r
1394 (errno != ENAMETOOLONG)) {
\r
1399 } while (strlen(hostname) >= len-1);
\r
1403 SockAddr platform_get_x11_unix_address(const char *sockpath, int displaynum)
\r
1405 SockAddr ret = snew(struct SockAddr_tag);
\r
1408 memset(ret, 0, sizeof *ret);
\r
1409 ret->superfamily = UNIX;
\r
1411 * In special circumstances (notably Mac OS X Leopard), we'll
\r
1412 * have been passed an explicit Unix socket path.
\r
1415 n = snprintf(ret->hostname, sizeof ret->hostname,
\r
1418 n = snprintf(ret->hostname, sizeof ret->hostname,
\r
1419 "%s%d", X11_UNIX_PATH, displaynum);
\r
1423 ret->error = "snprintf failed";
\r
1424 else if (n >= sizeof ret->hostname)
\r
1425 ret->error = "X11 UNIX name too long";
\r
1430 ret->addresses = NULL;
\r
1431 ret->naddresses = 0;
\r
1433 ret->refcount = 1;
\r