2 * Unix networking abstraction.
11 #include <sys/types.h>
12 #include <sys/socket.h>
13 #include <sys/ioctl.h>
14 #include <arpa/inet.h>
15 #include <netinet/in.h>
16 #include <netinet/tcp.h>
20 #define DEFINE_PLUG_METHOD_MACROS
26 # define X11_UNIX_PATH "/tmp/.X11-unix/X"
29 #define ipv4_is_loopback(addr) (inet_netof(addr) == IN_LOOPBACKNET)
32 struct socket_function_table *fn;
33 /* the above variable absolutely *must* be the first in this structure */
41 int frozen; /* this causes readability notifications to be ignored */
42 int frozen_readable; /* this means we missed at least one readability
43 * notification while we were frozen */
44 int localhost_only; /* for listening sockets */
47 int oobpending; /* is there OOB data available to read? */
49 int pending_error; /* in case send() returns error */
54 * We used to typedef struct Socket_tag *Socket.
56 * Since we have made the networking abstraction slightly more
57 * abstract, Socket no longer means a tcp socket (it could mean
58 * an ssl socket). So now we must use Actual_Socket when we know
59 * we are talking about a tcp socket.
61 typedef struct Socket_tag *Actual_Socket;
66 * Which address family this address belongs to. AF_INET for
67 * IPv4; AF_INET6 for IPv6; AF_UNSPEC indicates that name
68 * resolution has not been done and a simple host name is held
69 * in this SockAddr structure.
73 struct addrinfo *ai; /* Address IPv6 style. */
75 unsigned long address; /* Address IPv4 style. */
77 char hostname[512]; /* Store an unresolved host name. */
80 static tree234 *sktree;
82 static void uxsel_tell(Actual_Socket s);
84 static int cmpfortree(void *av, void *bv)
86 Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv;
87 int as = a->s, bs = b->s;
95 static int cmpforsearch(void *av, void *bv)
97 Actual_Socket b = (Actual_Socket) bv;
98 int as = *(int *)av, bs = b->s;
108 sktree = newtree234(cmpfortree);
111 void sk_cleanup(void)
117 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
123 const char *error_string(int error)
125 return strerror(error);
128 SockAddr sk_namelookup(const char *host, char **canonicalname, int address_family)
130 SockAddr ret = snew(struct SockAddr_tag);
132 struct addrinfo hints;
136 struct hostent *h = NULL;
140 /* Clear the structure and default to IPv4. */
141 memset(ret, 0, sizeof(struct SockAddr_tag));
142 ret->family = 0; /* We set this one when we have resolved the host. */
147 hints.ai_flags = AI_CANONNAME;
148 hints.ai_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
149 address_family == ADDRTYPE_IPV6 ? AF_INET6 :
151 hints.ai_socktype = SOCK_STREAM;
152 hints.ai_protocol = 0;
153 hints.ai_addrlen = 0;
154 hints.ai_addr = NULL;
155 hints.ai_canonname = NULL;
156 hints.ai_next = NULL;
157 err = getaddrinfo(host, NULL, &hints, &ret->ai);
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 memcpy(&a, h->h_addr, sizeof(a));
189 /* This way we are always sure the h->h_name is valid :) */
190 strncpy(realhost, h->h_name, sizeof(realhost));
193 * This must be a numeric IPv4 address because it caused a
194 * success return from inet_addr.
196 ret->family = AF_INET;
197 strncpy(realhost, host, sizeof(realhost));
199 ret->address = ntohl(a);
201 realhost[lenof(realhost)-1] = '\0';
202 *canonicalname = snewn(1+strlen(realhost), char);
203 strcpy(*canonicalname, realhost);
207 SockAddr sk_nonamelookup(const char *host)
209 SockAddr ret = snew(struct SockAddr_tag);
211 ret->family = AF_UNSPEC;
212 strncpy(ret->hostname, host, lenof(ret->hostname));
213 ret->hostname[lenof(ret->hostname)-1] = '\0';
220 void sk_getaddr(SockAddr addr, char *buf, int buflen)
223 if (addr->family == AF_UNSPEC) {
224 strncpy(buf, addr->hostname, buflen);
225 buf[buflen-1] = '\0';
228 if (getnameinfo(addr->ai->ai_addr, addr->ai->ai_addrlen, buf, buflen,
229 NULL, 0, NI_NUMERICHOST) != 0) {
231 strncat(buf, "<unknown>", buflen - 1);
235 assert(addr->family == AF_INET);
236 a.s_addr = htonl(addr->address);
237 strncpy(buf, inet_ntoa(a), buflen);
238 buf[buflen-1] = '\0';
243 int sk_hostname_is_local(char *name)
245 return !strcmp(name, "localhost");
248 int sk_address_is_local(SockAddr addr)
251 if (addr->family == AF_UNSPEC)
252 return 0; /* we don't know; assume not */
255 if (addr->family == AF_INET)
256 return ipv4_is_loopback(
257 ((struct sockaddr_in *)addr->ai->ai_addr)->sin_addr);
258 else if (addr->family == AF_INET6)
259 return IN6_IS_ADDR_LOOPBACK(
260 &((struct sockaddr_in6 *)addr->ai->ai_addr)->sin6_addr);
265 assert(addr->family == AF_INET);
266 a.s_addr = htonl(addr->address);
267 return ipv4_is_loopback(a);
272 int sk_addrtype(SockAddr addr)
274 return (addr->family == AF_INET ? ADDRTYPE_IPV4 :
276 addr->family == AF_INET6 ? ADDRTYPE_IPV6 :
281 void sk_addrcopy(SockAddr addr, char *buf)
285 if (addr->family == AF_INET)
286 memcpy(buf, &((struct sockaddr_in *)addr->ai->ai_addr)->sin_addr,
287 sizeof(struct in_addr));
288 else if (addr->family == AF_INET6)
289 memcpy(buf, &((struct sockaddr_in6 *)addr->ai->ai_addr)->sin6_addr,
290 sizeof(struct in6_addr));
296 assert(addr->family == AF_INET);
297 a.s_addr = htonl(addr->address);
298 memcpy(buf, (char*) &a.s_addr, 4);
302 void sk_addr_free(SockAddr addr)
306 if (addr->ai != NULL)
307 freeaddrinfo(addr->ai);
312 static Plug sk_tcp_plug(Socket sock, Plug p)
314 Actual_Socket s = (Actual_Socket) sock;
321 static void sk_tcp_flush(Socket s)
324 * We send data to the socket as soon as we can anyway,
325 * so we don't need to do anything here. :-)
329 static void sk_tcp_close(Socket s);
330 static int sk_tcp_write(Socket s, const char *data, int len);
331 static int sk_tcp_write_oob(Socket s, const char *data, int len);
332 static void sk_tcp_set_private_ptr(Socket s, void *ptr);
333 static void *sk_tcp_get_private_ptr(Socket s);
334 static void sk_tcp_set_frozen(Socket s, int is_frozen);
335 static const char *sk_tcp_socket_error(Socket s);
337 static struct socket_function_table tcp_fn_table = {
343 sk_tcp_set_private_ptr,
344 sk_tcp_get_private_ptr,
349 Socket sk_register(OSSocket sockfd, Plug plug)
354 * Create Socket structure.
356 ret = snew(struct Socket_tag);
357 ret->fn = &tcp_fn_table;
360 bufchain_init(&ret->output_data);
361 ret->writable = 1; /* to start with */
362 ret->sending_oob = 0;
364 ret->frozen_readable = 0;
365 ret->localhost_only = 0; /* unused, but best init anyway */
366 ret->pending_error = 0;
367 ret->oobpending = FALSE;
373 ret->error = error_string(errno);
385 Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
386 int nodelay, int keepalive, Plug plug)
390 struct sockaddr_in6 a6;
392 struct sockaddr_in a;
393 struct sockaddr_un au;
394 const struct sockaddr *sa;
401 * Create Socket structure.
403 ret = snew(struct Socket_tag);
404 ret->fn = &tcp_fn_table;
407 bufchain_init(&ret->output_data);
408 ret->connected = 0; /* to start with */
409 ret->writable = 0; /* to start with */
410 ret->sending_oob = 0;
412 ret->frozen_readable = 0;
413 ret->localhost_only = 0; /* unused, but best init anyway */
414 ret->pending_error = 0;
415 ret->oobpending = FALSE;
421 assert(addr->family != AF_UNSPEC);
422 s = socket(addr->family, SOCK_STREAM, 0);
426 ret->error = error_string(errno);
430 ret->oobinline = oobinline;
433 setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));
438 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *) &b, sizeof(b));
443 setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *) &b, sizeof(b));
447 * Bind to local address.
450 localport = 1023; /* count from 1023 downwards */
452 localport = 0; /* just use port 0 (ie kernel picks) */
454 /* BSD IP stacks need sockaddr_in zeroed before filling in */
455 memset(&a,'\0',sizeof(struct sockaddr_in));
457 memset(&a6,'\0',sizeof(struct sockaddr_in6));
460 /* We don't try to bind to a local address for UNIX domain sockets. (Why
461 * do we bother doing the bind when localport == 0 anyway?) */
462 if(addr->family != AF_UNIX) {
463 /* Loop round trying to bind */
468 if (addr->family == AF_INET6) {
469 /* XXX use getaddrinfo to get a local address? */
470 a6.sin6_family = AF_INET6;
471 a6.sin6_addr = in6addr_any;
472 a6.sin6_port = htons(localport);
473 retcode = bind(s, (struct sockaddr *) &a6, sizeof(a6));
477 assert(addr->family == AF_INET);
478 a.sin_family = AF_INET;
479 a.sin_addr.s_addr = htonl(INADDR_ANY);
480 a.sin_port = htons(localport);
481 retcode = bind(s, (struct sockaddr *) &a, sizeof(a));
488 if (err != EADDRINUSE) /* failed, for a bad reason */
493 break; /* we're only looping once */
496 break; /* we might have got to the end */
500 ret->error = error_string(err);
506 * Connect to remote address.
508 switch(addr->family) {
511 /* XXX would be better to have got getaddrinfo() to fill in the port. */
512 ((struct sockaddr_in *)addr->ai->ai_addr)->sin_port =
514 sa = (const struct sockaddr *)addr->ai->ai_addr;
515 salen = addr->ai->ai_addrlen;
518 ((struct sockaddr_in *)addr->ai->ai_addr)->sin_port =
520 sa = (const struct sockaddr *)addr->ai->ai_addr;
521 salen = addr->ai->ai_addrlen;
525 a.sin_family = AF_INET;
526 a.sin_addr.s_addr = htonl(addr->address);
527 a.sin_port = htons((short) port);
528 sa = (const struct sockaddr *)&a;
533 assert(port == 0); /* to catch confused people */
534 assert(strlen(addr->hostname) < sizeof au.sun_path);
535 memset(&au, 0, sizeof au);
536 au.sun_family = AF_UNIX;
537 strcpy(au.sun_path, addr->hostname);
538 sa = (const struct sockaddr *)&au;
543 assert(0 && "unknown address family");
546 fl = fcntl(s, F_GETFL);
548 fcntl(s, F_SETFL, fl | O_NONBLOCK);
550 if ((connect(s, sa, salen)) < 0) {
551 if ( errno != EINPROGRESS ) {
552 ret->error = error_string(errno);
557 * If we _don't_ get EWOULDBLOCK, the connect has completed
558 * and we should set the socket as connected and writable.
572 Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only, int address_family)
576 struct addrinfo hints, *ai;
578 struct sockaddr_in6 a6;
580 struct sockaddr *addr;
582 struct sockaddr_in a;
588 * Create Socket structure.
590 ret = snew(struct Socket_tag);
591 ret->fn = &tcp_fn_table;
594 bufchain_init(&ret->output_data);
595 ret->writable = 0; /* to start with */
596 ret->sending_oob = 0;
598 ret->frozen_readable = 0;
599 ret->localhost_only = local_host_only;
600 ret->pending_error = 0;
601 ret->oobpending = FALSE;
605 * Translate address_family from platform-independent constants
606 * into local reality.
608 address_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
609 address_family == ADDRTYPE_IPV6 ? AF_INET6 : AF_UNSPEC);
612 /* Let's default to IPv6.
613 * If the stack doesn't support IPv6, we will fall back to IPv4. */
614 if (address_family == AF_UNSPEC) address_family = AF_INET6;
616 /* No other choice, default to IPv4 */
617 if (address_family == AF_UNSPEC) address_family = AF_INET;
623 s = socket(address_family, SOCK_STREAM, 0);
625 /* If the host doesn't support IPv6 try fallback to IPv4. */
626 if (s < 0 && address_family == AF_INET6) {
627 address_family = AF_INET;
628 s = socket(address_family, SOCK_STREAM, 0);
632 ret->error = error_string(errno);
638 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
641 addr = NULL; addrlen = -1; /* placate optimiser */
643 if (srcaddr != NULL) {
645 hints.ai_flags = AI_NUMERICHOST;
646 hints.ai_family = address_family;
647 hints.ai_socktype = 0;
648 hints.ai_protocol = 0;
649 hints.ai_addrlen = 0;
650 hints.ai_addr = NULL;
651 hints.ai_canonname = NULL;
652 hints.ai_next = NULL;
653 sprintf(portstr, "%d", port);
654 retcode = getaddrinfo(srcaddr, portstr, &hints, &ai);
656 addrlen = ai->ai_addrlen;
658 memset(&a,'\0',sizeof(struct sockaddr_in));
659 a.sin_family = AF_INET;
660 a.sin_port = htons(port);
661 a.sin_addr.s_addr = inet_addr(srcaddr);
662 if (a.sin_addr.s_addr != (in_addr_t)(-1)) {
663 /* Override localhost_only with specified listen addr. */
664 ret->localhost_only = ipv4_is_loopback(a.sin_addr);
667 addr = (struct sockaddr *)a;
675 if (address_family == AF_INET6) {
676 memset(&a6,'\0',sizeof(struct sockaddr_in6));
677 a6.sin6_family = AF_INET6;
678 a6.sin6_port = htons(port);
680 a6.sin6_addr = in6addr_loopback;
682 a6.sin6_addr = in6addr_any;
683 addr = (struct sockaddr *)&a6;
684 addrlen = sizeof(a6);
688 memset(&a,'\0',sizeof(struct sockaddr_in));
689 a.sin_family = AF_INET;
690 a.sin_port = htons(port);
692 a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
694 a.sin_addr.s_addr = htonl(INADDR_ANY);
695 addr = (struct sockaddr *)&a;
700 retcode = bind(s, addr, addrlen);
703 ret->error = error_string(errno);
708 if (listen(s, SOMAXCONN) < 0) {
710 ret->error = error_string(errno);
722 static void sk_tcp_close(Socket sock)
724 Actual_Socket s = (Actual_Socket) sock;
732 int sk_getxdmdata(void *sock, unsigned long *ip, int *port)
734 Actual_Socket s = (Actual_Socket) sock;
735 struct sockaddr_in addr;
739 * We must check that this socket really _is_ an Actual_Socket.
741 if (s->fn != &tcp_fn_table)
742 return 0; /* failure */
744 addrlen = sizeof(addr);
745 if (getsockname(s->s, (struct sockaddr *)&addr, &addrlen) < 0)
747 switch(addr.sin_family) {
749 *ip = ntohl(addr.sin_addr.s_addr);
750 *port = ntohs(addr.sin_port);
754 * For a Unix socket, we return 0xFFFFFFFF for the IP address and
755 * our current pid for the port. Bizarre, but such is life.
757 *ip = ntohl(0xFFFFFFFF);
771 * The function which tries to send on a socket once it's deemed
774 void try_send(Actual_Socket s)
776 while (s->sending_oob || bufchain_size(&s->output_data) > 0) {
782 if (s->sending_oob) {
783 urgentflag = MSG_OOB;
784 len = s->sending_oob;
788 bufchain_prefix(&s->output_data, &data, &len);
790 nsent = send(s->s, data, len, urgentflag);
791 noise_ultralight(nsent);
793 err = (nsent < 0 ? errno : 0);
794 if (err == EWOULDBLOCK) {
796 * Perfectly normal: we've sent all we can for the moment.
800 } else if (nsent == 0 ||
801 err == ECONNABORTED || err == ECONNRESET) {
803 * If send() returns CONNABORTED or CONNRESET, we
804 * unfortunately can't just call plug_closing(),
805 * because it's quite likely that we're currently
806 * _in_ a call from the code we'd be calling back
807 * to, so we'd have to make half the SSH code
808 * reentrant. Instead we flag a pending error on
809 * the socket, to be dealt with (by calling
810 * plug_closing()) at some suitable future moment.
812 s->pending_error = err;
815 /* We're inside the Unix frontend here, so we know
816 * that the frontend handle is unnecessary. */
817 logevent(NULL, error_string(err));
818 fatalbox("%s", error_string(err));
821 if (s->sending_oob) {
823 memmove(s->oobdata, s->oobdata+nsent, len-nsent);
824 s->sending_oob = len - nsent;
829 bufchain_consume(&s->output_data, nsent);
836 static int sk_tcp_write(Socket sock, const char *buf, int len)
838 Actual_Socket s = (Actual_Socket) sock;
841 * Add the data to the buffer list on the socket.
843 bufchain_add(&s->output_data, buf, len);
846 * Now try sending from the start of the buffer list.
852 * Update the select() status to correctly reflect whether or
853 * not we should be selecting for write.
857 return bufchain_size(&s->output_data);
860 static int sk_tcp_write_oob(Socket sock, const char *buf, int len)
862 Actual_Socket s = (Actual_Socket) sock;
865 * Replace the buffer list on the socket with the data.
867 bufchain_clear(&s->output_data);
868 assert(len <= sizeof(s->oobdata));
869 memcpy(s->oobdata, buf, len);
870 s->sending_oob = len;
873 * Now try sending from the start of the buffer list.
879 * Update the select() status to correctly reflect whether or
880 * not we should be selecting for write.
884 return s->sending_oob;
887 static int net_select_result(int fd, int event)
891 char buf[20480]; /* nice big buffer for plenty of speed */
895 /* Find the Socket structure */
896 s = find234(sktree, &fd, cmpforsearch);
898 return 1; /* boggle */
900 noise_ultralight(event);
903 case 4: /* exceptional */
906 * On a non-oobinline socket, this indicates that we
907 * can immediately perform an OOB read and get back OOB
908 * data, which we will send to the back end with
909 * type==2 (urgent data).
911 ret = recv(s->s, buf, sizeof(buf), MSG_OOB);
912 noise_ultralight(ret);
914 const char *str = (ret == 0 ? "Internal networking trouble" :
915 error_string(errno));
916 /* We're inside the Unix frontend here, so we know
917 * that the frontend handle is unnecessary. */
921 return plug_receive(s->plug, 2, buf, ret);
927 * If we reach here, this is an oobinline socket, which
928 * means we should set s->oobpending and then deal with it
929 * when we get called for the readability event (which
930 * should also occur).
932 s->oobpending = TRUE;
934 case 1: /* readable; also acceptance */
937 * On a listening socket, the readability event means a
938 * connection is ready to be accepted.
940 struct sockaddr_in isa;
941 int addrlen = sizeof(struct sockaddr_in);
942 int t; /* socket of connection */
944 memset(&isa, 0, sizeof(struct sockaddr_in));
946 t = accept(s->s,(struct sockaddr *)&isa,(socklen_t *) &addrlen);
951 if (s->localhost_only && !ipv4_is_loopback(isa.sin_addr)) {
952 close(t); /* someone let nonlocal through?! */
953 } else if (plug_accepting(s->plug, t)) {
954 close(t); /* denied or error */
960 * If we reach here, this is not a listening socket, so
961 * readability really means readability.
964 /* In the case the socket is still frozen, we don't even bother */
966 s->frozen_readable = 1;
971 * We have received data on the socket. For an oobinline
972 * socket, this might be data _before_ an urgent pointer,
973 * in which case we send it to the back end with type==1
974 * (data prior to urgent).
976 if (s->oobinline && s->oobpending) {
978 if (ioctl(s->s, SIOCATMARK, &atmark) == 0 && atmark)
979 s->oobpending = FALSE; /* clear this indicator */
983 ret = recv(s->s, buf, s->oobpending ? 1 : sizeof(buf), 0);
984 noise_ultralight(ret);
986 if (errno == EWOULDBLOCK) {
991 return plug_closing(s->plug, error_string(errno), errno, 0);
992 } else if (0 == ret) {
993 return plug_closing(s->plug, NULL, 0, 0);
995 return plug_receive(s->plug, atmark ? 0 : 1, buf, ret);
998 case 2: /* writable */
1001 * select() reports a socket as _writable_ when an
1002 * asynchronous connection is completed.
1004 s->connected = s->writable = 1;
1008 int bufsize_before, bufsize_after;
1010 bufsize_before = s->sending_oob + bufchain_size(&s->output_data);
1012 bufsize_after = s->sending_oob + bufchain_size(&s->output_data);
1013 if (bufsize_after < bufsize_before)
1014 plug_sent(s->plug, bufsize_after);
1023 * Deal with socket errors detected in try_send().
1025 void net_pending_errors(void)
1031 * This might be a fiddly business, because it's just possible
1032 * that handling a pending error on one socket might cause
1033 * others to be closed. (I can't think of any reason this might
1034 * happen in current SSH implementation, but to maintain
1035 * generality of this network layer I'll assume the worst.)
1037 * So what we'll do is search the socket list for _one_ socket
1038 * with a pending error, and then handle it, and then search
1039 * the list again _from the beginning_. Repeat until we make a
1040 * pass with no socket errors present. That way we are
1041 * protected against the socket list changing under our feet.
1045 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
1046 if (s->pending_error) {
1048 * An error has occurred on this socket. Pass it to the
1051 plug_closing(s->plug, error_string(s->pending_error),
1052 s->pending_error, 0);
1060 * Each socket abstraction contains a `void *' private field in
1061 * which the client can keep state.
1063 static void sk_tcp_set_private_ptr(Socket sock, void *ptr)
1065 Actual_Socket s = (Actual_Socket) sock;
1066 s->private_ptr = ptr;
1069 static void *sk_tcp_get_private_ptr(Socket sock)
1071 Actual_Socket s = (Actual_Socket) sock;
1072 return s->private_ptr;
1076 * Special error values are returned from sk_namelookup and sk_new
1077 * if there's a problem. These functions extract an error message,
1078 * or return NULL if there's no problem.
1080 const char *sk_addr_error(SockAddr addr)
1084 static const char *sk_tcp_socket_error(Socket sock)
1086 Actual_Socket s = (Actual_Socket) sock;
1090 static void sk_tcp_set_frozen(Socket sock, int is_frozen)
1092 Actual_Socket s = (Actual_Socket) sock;
1093 if (s->frozen == is_frozen)
1095 s->frozen = is_frozen;
1096 if (!is_frozen && s->frozen_readable) {
1098 recv(s->s, &c, 1, MSG_PEEK);
1100 s->frozen_readable = 0;
1104 static void uxsel_tell(Actual_Socket s)
1108 rwx |= 2; /* write == connect */
1109 if (s->connected && !s->frozen)
1110 rwx |= 1 | 4; /* read, except */
1111 if (bufchain_size(&s->output_data))
1112 rwx |= 2; /* write */
1114 rwx |= 1; /* read == accept */
1115 uxsel_set(s->s, rwx, net_select_result);
1118 int net_service_lookup(char *service)
1121 se = getservbyname(service, NULL);
1123 return ntohs(se->s_port);
1128 SockAddr platform_get_x11_unix_address(int displaynum, char **canonicalname)
1130 SockAddr ret = snew(struct SockAddr_tag);
1133 memset(ret, 0, sizeof *ret);
1134 ret->family = AF_UNIX;
1135 n = snprintf(ret->hostname, sizeof ret->hostname,
1136 "%s%d", X11_UNIX_PATH, displaynum);
1138 ret->error = "snprintf failed";
1139 else if(n >= sizeof ret->hostname)
1140 ret->error = "X11 UNIX name too long";
1142 *canonicalname = dupstr(ret->hostname);