]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - unix/uxnet.c
Log identifying information for the other end of connections.
[PuTTY.git] / unix / uxnet.c
1 /*
2  * Unix networking abstraction.
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <assert.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <unistd.h>
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>
17 #include <netdb.h>
18 #include <sys/un.h>
19 #include <pwd.h>
20 #include <grp.h>
21
22 #define DEFINE_PLUG_METHOD_MACROS
23 #include "putty.h"
24 #include "network.h"
25 #include "tree234.h"
26
27 /* Solaris needs <sys/sockio.h> for SIOCATMARK. */
28 #ifndef SIOCATMARK
29 #include <sys/sockio.h>
30 #endif
31
32 #ifndef X11_UNIX_PATH
33 # define X11_UNIX_PATH "/tmp/.X11-unix/X"
34 #endif
35
36 /* 
37  * Access to sockaddr types without breaking C strict aliasing rules.
38  */
39 union sockaddr_union {
40 #ifdef NO_IPV6
41     struct sockaddr_in storage;
42 #else
43     struct sockaddr_storage storage;
44     struct sockaddr_in6 sin6;
45 #endif
46     struct sockaddr sa;
47     struct sockaddr_in sin;
48     struct sockaddr_un su;
49 };
50
51 /*
52  * We used to typedef struct Socket_tag *Socket.
53  *
54  * Since we have made the networking abstraction slightly more
55  * abstract, Socket no longer means a tcp socket (it could mean
56  * an ssl socket).  So now we must use Actual_Socket when we know
57  * we are talking about a tcp socket.
58  */
59 typedef struct Socket_tag *Actual_Socket;
60
61 /*
62  * Mutable state that goes with a SockAddr: stores information
63  * about where in the list of candidate IP(v*) addresses we've
64  * currently got to.
65  */
66 typedef struct SockAddrStep_tag SockAddrStep;
67 struct SockAddrStep_tag {
68 #ifndef NO_IPV6
69     struct addrinfo *ai;               /* steps along addr->ais */
70 #endif
71     int curraddr;
72 };
73
74 struct Socket_tag {
75     struct socket_function_table *fn;
76     /* the above variable absolutely *must* be the first in this structure */
77     const char *error;
78     int s;
79     Plug plug;
80     bufchain output_data;
81     int connected;                     /* irrelevant for listening sockets */
82     int writable;
83     int frozen; /* this causes readability notifications to be ignored */
84     int localhost_only;                /* for listening sockets */
85     char oobdata[1];
86     int sending_oob;
87     int oobpending;                    /* is there OOB data available to read? */
88     int oobinline;
89     enum { EOF_NO, EOF_PENDING, EOF_SENT } outgoingeof;
90     int incomingeof;
91     int pending_error;                 /* in case send() returns error */
92     int listener;
93     int nodelay, keepalive;            /* for connect()-type sockets */
94     int privport, port;                /* and again */
95     SockAddr addr;
96     SockAddrStep step;
97     /*
98      * We sometimes need pairs of Socket structures to be linked:
99      * if we are listening on the same IPv6 and v4 port, for
100      * example. So here we define `parent' and `child' pointers to
101      * track this link.
102      */
103     Actual_Socket parent, child;
104 };
105
106 struct SockAddr_tag {
107     int refcount;
108     const char *error;
109     enum { UNRESOLVED, UNIX, IP } superfamily;
110 #ifndef NO_IPV6
111     struct addrinfo *ais;              /* Addresses IPv6 style. */
112 #else
113     unsigned long *addresses;          /* Addresses IPv4 style. */
114     int naddresses;
115 #endif
116     char hostname[512];                /* Store an unresolved host name. */
117 };
118
119 /*
120  * Which address family this address belongs to. AF_INET for IPv4;
121  * AF_INET6 for IPv6; AF_UNSPEC indicates that name resolution has
122  * not been done and a simple host name is held in this SockAddr
123  * structure.
124  */
125 #ifndef NO_IPV6
126 #define SOCKADDR_FAMILY(addr, step) \
127     ((addr)->superfamily == UNRESOLVED ? AF_UNSPEC : \
128      (addr)->superfamily == UNIX ? AF_UNIX : \
129      (step).ai ? (step).ai->ai_family : AF_INET)
130 #else
131 /* Here we gratuitously reference 'step' to avoid gcc warnings about
132  * 'set but not used' when compiling -DNO_IPV6 */
133 #define SOCKADDR_FAMILY(addr, step) \
134     ((addr)->superfamily == UNRESOLVED ? AF_UNSPEC : \
135      (addr)->superfamily == UNIX ? AF_UNIX : \
136      (step).curraddr ? AF_INET : AF_INET)
137 #endif
138
139 /*
140  * Start a SockAddrStep structure to step through multiple
141  * addresses.
142  */
143 #ifndef NO_IPV6
144 #define START_STEP(addr, step) \
145     ((step).ai = (addr)->ais, (step).curraddr = 0)
146 #else
147 #define START_STEP(addr, step) \
148     ((step).curraddr = 0)
149 #endif
150
151 static tree234 *sktree;
152
153 static void uxsel_tell(Actual_Socket s);
154
155 static int cmpfortree(void *av, void *bv)
156 {
157     Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv;
158     int as = a->s, bs = b->s;
159     if (as < bs)
160         return -1;
161     if (as > bs)
162         return +1;
163     if (a < b)
164        return -1;
165     if (a > b)
166        return +1;
167     return 0;
168 }
169
170 static int cmpforsearch(void *av, void *bv)
171 {
172     Actual_Socket b = (Actual_Socket) bv;
173     int as = *(int *)av, bs = b->s;
174     if (as < bs)
175         return -1;
176     if (as > bs)
177         return +1;
178     return 0;
179 }
180
181 void sk_init(void)
182 {
183     sktree = newtree234(cmpfortree);
184 }
185
186 void sk_cleanup(void)
187 {
188     Actual_Socket s;
189     int i;
190
191     if (sktree) {
192         for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
193             close(s->s);
194         }
195     }
196 }
197
198 SockAddr sk_namelookup(const char *host, char **canonicalname, int address_family)
199 {
200     SockAddr ret = snew(struct SockAddr_tag);
201 #ifndef NO_IPV6
202     struct addrinfo hints;
203     int err;
204 #else
205     unsigned long a;
206     struct hostent *h = NULL;
207     int n;
208 #endif
209     char realhost[8192];
210
211     /* Clear the structure and default to IPv4. */
212     memset(ret, 0, sizeof(struct SockAddr_tag));
213     ret->superfamily = UNRESOLVED;
214     *realhost = '\0';
215     ret->error = NULL;
216     ret->refcount = 1;
217
218 #ifndef NO_IPV6
219     hints.ai_flags = AI_CANONNAME;
220     hints.ai_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
221                        address_family == ADDRTYPE_IPV6 ? AF_INET6 :
222                        AF_UNSPEC);
223     hints.ai_socktype = SOCK_STREAM;
224     hints.ai_protocol = 0;
225     hints.ai_addrlen = 0;
226     hints.ai_addr = NULL;
227     hints.ai_canonname = NULL;
228     hints.ai_next = NULL;
229     {
230         char *trimmed_host = host_strduptrim(host); /* strip [] on literals */
231         err = getaddrinfo(trimmed_host, NULL, &hints, &ret->ais);
232         sfree(trimmed_host);
233     }
234     if (err != 0) {
235         ret->error = gai_strerror(err);
236         return ret;
237     }
238     ret->superfamily = IP;
239     *realhost = '\0';
240     if (ret->ais->ai_canonname != NULL)
241         strncat(realhost, ret->ais->ai_canonname, sizeof(realhost) - 1);
242     else
243         strncat(realhost, host, sizeof(realhost) - 1);
244 #else
245     if ((a = inet_addr(host)) == (unsigned long)(in_addr_t)(-1)) {
246         /*
247          * Otherwise use the IPv4-only gethostbyname... (NOTE:
248          * we don't use gethostbyname as a fallback!)
249          */
250         if (ret->superfamily == UNRESOLVED) {
251             /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
252             if ( (h = gethostbyname(host)) )
253                 ret->superfamily = IP;
254         }
255         if (ret->superfamily == UNRESOLVED) {
256             ret->error = (h_errno == HOST_NOT_FOUND ||
257                           h_errno == NO_DATA ||
258                           h_errno == NO_ADDRESS ? "Host does not exist" :
259                           h_errno == TRY_AGAIN ?
260                           "Temporary name service failure" :
261                           "gethostbyname: unknown error");
262             return ret;
263         }
264         /* This way we are always sure the h->h_name is valid :) */
265         strncpy(realhost, h->h_name, sizeof(realhost));
266         for (n = 0; h->h_addr_list[n]; n++);
267         ret->addresses = snewn(n, unsigned long);
268         ret->naddresses = n;
269         for (n = 0; n < ret->naddresses; n++) {
270             memcpy(&a, h->h_addr_list[n], sizeof(a));
271             ret->addresses[n] = ntohl(a);
272         }
273     } else {
274         /*
275          * This must be a numeric IPv4 address because it caused a
276          * success return from inet_addr.
277          */
278         ret->superfamily = IP;
279         strncpy(realhost, host, sizeof(realhost));
280         ret->addresses = snew(unsigned long);
281         ret->naddresses = 1;
282         ret->addresses[0] = ntohl(a);
283     }
284 #endif
285     realhost[lenof(realhost)-1] = '\0';
286     *canonicalname = snewn(1+strlen(realhost), char);
287     strcpy(*canonicalname, realhost);
288     return ret;
289 }
290
291 SockAddr sk_nonamelookup(const char *host)
292 {
293     SockAddr ret = snew(struct SockAddr_tag);
294     ret->error = NULL;
295     ret->superfamily = UNRESOLVED;
296     strncpy(ret->hostname, host, lenof(ret->hostname));
297     ret->hostname[lenof(ret->hostname)-1] = '\0';
298 #ifndef NO_IPV6
299     ret->ais = NULL;
300 #else
301     ret->addresses = NULL;
302 #endif
303     ret->refcount = 1;
304     return ret;
305 }
306
307 static int sk_nextaddr(SockAddr addr, SockAddrStep *step)
308 {
309 #ifndef NO_IPV6
310     if (step->ai && step->ai->ai_next) {
311         step->ai = step->ai->ai_next;
312         return TRUE;
313     } else
314         return FALSE;
315 #else
316     if (step->curraddr+1 < addr->naddresses) {
317         step->curraddr++;
318         return TRUE;
319     } else {
320         return FALSE;
321     }
322 #endif    
323 }
324
325 void sk_getaddr(SockAddr addr, char *buf, int buflen)
326 {
327     if (addr->superfamily == UNRESOLVED || addr->superfamily == UNIX) {
328         strncpy(buf, addr->hostname, buflen);
329         buf[buflen-1] = '\0';
330     } else {
331 #ifndef NO_IPV6
332         if (getnameinfo(addr->ais->ai_addr, addr->ais->ai_addrlen, buf, buflen,
333                         NULL, 0, NI_NUMERICHOST) != 0) {
334             buf[0] = '\0';
335             strncat(buf, "<unknown>", buflen - 1);
336         }
337 #else
338         struct in_addr a;
339         SockAddrStep step;
340         START_STEP(addr, step);
341         assert(SOCKADDR_FAMILY(addr, step) == AF_INET);
342         a.s_addr = htonl(addr->addresses[0]);
343         strncpy(buf, inet_ntoa(a), buflen);
344         buf[buflen-1] = '\0';
345 #endif
346     }
347 }
348
349 int sk_addr_needs_port(SockAddr addr)
350 {
351     if (addr->superfamily == UNRESOLVED || addr->superfamily == UNIX) {
352         return FALSE;
353     } else {
354         return TRUE;
355     }
356 }
357
358 int sk_hostname_is_local(const char *name)
359 {
360     return !strcmp(name, "localhost") ||
361            !strcmp(name, "::1") ||
362            !strncmp(name, "127.", 4);
363 }
364
365 #define ipv4_is_loopback(addr) \
366     (((addr).s_addr & htonl(0xff000000)) == htonl(0x7f000000))
367
368 static int sockaddr_is_loopback(struct sockaddr *sa)
369 {
370     union sockaddr_union *u = (union sockaddr_union *)sa;
371     switch (u->sa.sa_family) {
372       case AF_INET:
373         return ipv4_is_loopback(u->sin.sin_addr);
374 #ifndef NO_IPV6
375       case AF_INET6:
376         return IN6_IS_ADDR_LOOPBACK(&u->sin6.sin6_addr);
377 #endif
378       case AF_UNIX:
379         return TRUE;
380       default:
381         return FALSE;
382     }
383 }
384
385 int sk_address_is_local(SockAddr addr)
386 {
387     if (addr->superfamily == UNRESOLVED)
388         return 0;                      /* we don't know; assume not */
389     else if (addr->superfamily == UNIX)
390         return 1;
391     else {
392 #ifndef NO_IPV6
393         return sockaddr_is_loopback(addr->ais->ai_addr);
394 #else
395         struct in_addr a;
396         SockAddrStep step;
397         START_STEP(addr, step);
398         assert(SOCKADDR_FAMILY(addr, step) == AF_INET);
399         a.s_addr = htonl(addr->addresses[0]);
400         return ipv4_is_loopback(a);
401 #endif
402     }
403 }
404
405 int sk_address_is_special_local(SockAddr addr)
406 {
407     return addr->superfamily == UNIX;
408 }
409
410 int sk_addrtype(SockAddr addr)
411 {
412     SockAddrStep step;
413     int family;
414     START_STEP(addr, step);
415     family = SOCKADDR_FAMILY(addr, step);
416
417     return (family == AF_INET ? ADDRTYPE_IPV4 :
418 #ifndef NO_IPV6
419             family == AF_INET6 ? ADDRTYPE_IPV6 :
420 #endif
421             ADDRTYPE_NAME);
422 }
423
424 void sk_addrcopy(SockAddr addr, char *buf)
425 {
426     SockAddrStep step;
427     int family;
428     START_STEP(addr, step);
429     family = SOCKADDR_FAMILY(addr, step);
430
431 #ifndef NO_IPV6
432     if (family == AF_INET)
433         memcpy(buf, &((struct sockaddr_in *)step.ai->ai_addr)->sin_addr,
434                sizeof(struct in_addr));
435     else if (family == AF_INET6)
436         memcpy(buf, &((struct sockaddr_in6 *)step.ai->ai_addr)->sin6_addr,
437                sizeof(struct in6_addr));
438     else
439         assert(FALSE);
440 #else
441     struct in_addr a;
442
443     assert(family == AF_INET);
444     a.s_addr = htonl(addr->addresses[step.curraddr]);
445     memcpy(buf, (char*) &a.s_addr, 4);
446 #endif
447 }
448
449 void sk_addr_free(SockAddr addr)
450 {
451     if (--addr->refcount > 0)
452         return;
453 #ifndef NO_IPV6
454     if (addr->ais != NULL)
455         freeaddrinfo(addr->ais);
456 #else
457     sfree(addr->addresses);
458 #endif
459     sfree(addr);
460 }
461
462 SockAddr sk_addr_dup(SockAddr addr)
463 {
464     addr->refcount++;
465     return addr;
466 }
467
468 static Plug sk_tcp_plug(Socket sock, Plug p)
469 {
470     Actual_Socket s = (Actual_Socket) sock;
471     Plug ret = s->plug;
472     if (p)
473         s->plug = p;
474     return ret;
475 }
476
477 static void sk_tcp_flush(Socket s)
478 {
479     /*
480      * We send data to the socket as soon as we can anyway,
481      * so we don't need to do anything here.  :-)
482      */
483 }
484
485 static void sk_tcp_close(Socket s);
486 static int sk_tcp_write(Socket s, const char *data, int len);
487 static int sk_tcp_write_oob(Socket s, const char *data, int len);
488 static void sk_tcp_write_eof(Socket s);
489 static void sk_tcp_set_frozen(Socket s, int is_frozen);
490 static char *sk_tcp_peer_info(Socket s);
491 static const char *sk_tcp_socket_error(Socket s);
492
493 static struct socket_function_table tcp_fn_table = {
494     sk_tcp_plug,
495     sk_tcp_close,
496     sk_tcp_write,
497     sk_tcp_write_oob,
498     sk_tcp_write_eof,
499     sk_tcp_flush,
500     sk_tcp_set_frozen,
501     sk_tcp_socket_error,
502     sk_tcp_peer_info,
503 };
504
505 static Socket sk_tcp_accept(accept_ctx_t ctx, Plug plug)
506 {
507     int sockfd = ctx.i;
508     Actual_Socket ret;
509
510     /*
511      * Create Socket structure.
512      */
513     ret = snew(struct Socket_tag);
514     ret->fn = &tcp_fn_table;
515     ret->error = NULL;
516     ret->plug = plug;
517     bufchain_init(&ret->output_data);
518     ret->writable = 1;                 /* to start with */
519     ret->sending_oob = 0;
520     ret->frozen = 1;
521     ret->localhost_only = 0;           /* unused, but best init anyway */
522     ret->pending_error = 0;
523     ret->oobpending = FALSE;
524     ret->outgoingeof = EOF_NO;
525     ret->incomingeof = FALSE;
526     ret->listener = 0;
527     ret->parent = ret->child = NULL;
528     ret->addr = NULL;
529     ret->connected = 1;
530
531     ret->s = sockfd;
532
533     if (ret->s < 0) {
534         ret->error = strerror(errno);
535         return (Socket) ret;
536     }
537
538     ret->oobinline = 0;
539
540     uxsel_tell(ret);
541     add234(sktree, ret);
542
543     return (Socket) ret;
544 }
545
546 static int try_connect(Actual_Socket sock)
547 {
548     int s;
549     union sockaddr_union u;
550     const union sockaddr_union *sa;
551     int err = 0;
552     short localport;
553     int salen, family;
554
555     /*
556      * Remove the socket from the tree before we overwrite its
557      * internal socket id, because that forms part of the tree's
558      * sorting criterion. We'll add it back before exiting this
559      * function, whether we changed anything or not.
560      */
561     del234(sktree, sock);
562
563     if (sock->s >= 0)
564         close(sock->s);
565
566     plug_log(sock->plug, 0, sock->addr, sock->port, NULL, 0);
567
568     /*
569      * Open socket.
570      */
571     family = SOCKADDR_FAMILY(sock->addr, sock->step);
572     assert(family != AF_UNSPEC);
573     s = socket(family, SOCK_STREAM, 0);
574     sock->s = s;
575
576     if (s < 0) {
577         err = errno;
578         goto ret;
579     }
580
581     cloexec(s);
582
583     if (sock->oobinline) {
584         int b = TRUE;
585         if (setsockopt(s, SOL_SOCKET, SO_OOBINLINE,
586                        (void *) &b, sizeof(b)) < 0) {
587             err = errno;
588             close(s);
589             goto ret;
590         }
591     }
592
593     if (sock->nodelay) {
594         int b = TRUE;
595         if (setsockopt(s, IPPROTO_TCP, TCP_NODELAY,
596                        (void *) &b, sizeof(b)) < 0) {
597             err = errno;
598             close(s);
599             goto ret;
600         }
601     }
602
603     if (sock->keepalive) {
604         int b = TRUE;
605         if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE,
606                        (void *) &b, sizeof(b)) < 0) {
607             err = errno;
608             close(s);
609             goto ret;
610         }
611     }
612
613     /*
614      * Bind to local address.
615      */
616     if (sock->privport)
617         localport = 1023;              /* count from 1023 downwards */
618     else
619         localport = 0;                 /* just use port 0 (ie kernel picks) */
620
621     /* BSD IP stacks need sockaddr_in zeroed before filling in */
622     memset(&u,'\0',sizeof(u));
623
624     /* We don't try to bind to a local address for UNIX domain sockets.  (Why
625      * do we bother doing the bind when localport == 0 anyway?) */
626     if (family != AF_UNIX) {
627         /* Loop round trying to bind */
628         while (1) {
629             int retcode;
630
631 #ifndef NO_IPV6
632             if (family == AF_INET6) {
633                 /* XXX use getaddrinfo to get a local address? */
634                 u.sin6.sin6_family = AF_INET6;
635                 u.sin6.sin6_addr = in6addr_any;
636                 u.sin6.sin6_port = htons(localport);
637                 retcode = bind(s, &u.sa, sizeof(u.sin6));
638             } else
639 #endif
640             {
641                 assert(family == AF_INET);
642                 u.sin.sin_family = AF_INET;
643                 u.sin.sin_addr.s_addr = htonl(INADDR_ANY);
644                 u.sin.sin_port = htons(localport);
645                 retcode = bind(s, &u.sa, sizeof(u.sin));
646             }
647             if (retcode >= 0) {
648                 err = 0;
649                 break;                 /* done */
650             } else {
651                 err = errno;
652                 if (err != EADDRINUSE) /* failed, for a bad reason */
653                   break;
654             }
655             
656             if (localport == 0)
657               break;                   /* we're only looping once */
658             localport--;
659             if (localport == 0)
660               break;                   /* we might have got to the end */
661         }
662         
663         if (err)
664             goto ret;
665     }
666
667     /*
668      * Connect to remote address.
669      */
670     switch(family) {
671 #ifndef NO_IPV6
672       case AF_INET:
673         /* XXX would be better to have got getaddrinfo() to fill in the port. */
674         ((struct sockaddr_in *)sock->step.ai->ai_addr)->sin_port =
675             htons(sock->port);
676         sa = (const union sockaddr_union *)sock->step.ai->ai_addr;
677         salen = sock->step.ai->ai_addrlen;
678         break;
679       case AF_INET6:
680         ((struct sockaddr_in *)sock->step.ai->ai_addr)->sin_port =
681             htons(sock->port);
682         sa = (const union sockaddr_union *)sock->step.ai->ai_addr;
683         salen = sock->step.ai->ai_addrlen;
684         break;
685 #else
686       case AF_INET:
687         u.sin.sin_family = AF_INET;
688         u.sin.sin_addr.s_addr = htonl(sock->addr->addresses[sock->step.curraddr]);
689         u.sin.sin_port = htons((short) sock->port);
690         sa = &u;
691         salen = sizeof u.sin;
692         break;
693 #endif
694       case AF_UNIX:
695         assert(sock->port == 0);       /* to catch confused people */
696         assert(strlen(sock->addr->hostname) < sizeof u.su.sun_path);
697         u.su.sun_family = AF_UNIX;
698         strcpy(u.su.sun_path, sock->addr->hostname);
699         sa = &u;
700         salen = sizeof u.su;
701         break;
702
703       default:
704         assert(0 && "unknown address family");
705         exit(1); /* XXX: GCC doesn't understand assert() on some systems. */
706     }
707
708     nonblock(s);
709
710     if ((connect(s, &(sa->sa), salen)) < 0) {
711         if ( errno != EINPROGRESS ) {
712             err = errno;
713             goto ret;
714         }
715     } else {
716         /*
717          * If we _don't_ get EWOULDBLOCK, the connect has completed
718          * and we should set the socket as connected and writable.
719          */
720         sock->connected = 1;
721         sock->writable = 1;
722     }
723
724     uxsel_tell(sock);
725
726     ret:
727
728     /*
729      * No matter what happened, put the socket back in the tree.
730      */
731     add234(sktree, sock);
732
733     if (err)
734         plug_log(sock->plug, 1, sock->addr, sock->port, strerror(err), err);
735     return err;
736 }
737
738 Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
739               int nodelay, int keepalive, Plug plug)
740 {
741     Actual_Socket ret;
742     int err;
743
744     /*
745      * Create Socket structure.
746      */
747     ret = snew(struct Socket_tag);
748     ret->fn = &tcp_fn_table;
749     ret->error = NULL;
750     ret->plug = plug;
751     bufchain_init(&ret->output_data);
752     ret->connected = 0;                /* to start with */
753     ret->writable = 0;                 /* to start with */
754     ret->sending_oob = 0;
755     ret->frozen = 0;
756     ret->localhost_only = 0;           /* unused, but best init anyway */
757     ret->pending_error = 0;
758     ret->parent = ret->child = NULL;
759     ret->oobpending = FALSE;
760     ret->outgoingeof = EOF_NO;
761     ret->incomingeof = FALSE;
762     ret->listener = 0;
763     ret->addr = addr;
764     START_STEP(ret->addr, ret->step);
765     ret->s = -1;
766     ret->oobinline = oobinline;
767     ret->nodelay = nodelay;
768     ret->keepalive = keepalive;
769     ret->privport = privport;
770     ret->port = port;
771
772     err = 0;
773     do {
774         err = try_connect(ret);
775     } while (err && sk_nextaddr(ret->addr, &ret->step));
776
777     if (err)
778         ret->error = strerror(err);
779
780     return (Socket) ret;
781 }
782
783 Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only, int orig_address_family)
784 {
785     int s;
786 #ifndef NO_IPV6
787     struct addrinfo hints, *ai = NULL;
788     char portstr[6];
789 #endif
790     union sockaddr_union u;
791     union sockaddr_union *addr;
792     int addrlen;
793     Actual_Socket ret;
794     int retcode;
795     int address_family;
796     int on = 1;
797
798     /*
799      * Create Socket structure.
800      */
801     ret = snew(struct Socket_tag);
802     ret->fn = &tcp_fn_table;
803     ret->error = NULL;
804     ret->plug = plug;
805     bufchain_init(&ret->output_data);
806     ret->writable = 0;                 /* to start with */
807     ret->sending_oob = 0;
808     ret->frozen = 0;
809     ret->localhost_only = local_host_only;
810     ret->pending_error = 0;
811     ret->parent = ret->child = NULL;
812     ret->oobpending = FALSE;
813     ret->outgoingeof = EOF_NO;
814     ret->incomingeof = FALSE;
815     ret->listener = 1;
816     ret->addr = NULL;
817     ret->s = -1;
818
819     /*
820      * Translate address_family from platform-independent constants
821      * into local reality.
822      */
823     address_family = (orig_address_family == ADDRTYPE_IPV4 ? AF_INET :
824 #ifndef NO_IPV6
825                       orig_address_family == ADDRTYPE_IPV6 ? AF_INET6 :
826 #endif
827                       AF_UNSPEC);
828
829 #ifndef NO_IPV6
830     /* Let's default to IPv6.
831      * If the stack doesn't support IPv6, we will fall back to IPv4. */
832     if (address_family == AF_UNSPEC) address_family = AF_INET6;
833 #else
834     /* No other choice, default to IPv4 */
835     if (address_family == AF_UNSPEC)  address_family = AF_INET;
836 #endif
837
838     /*
839      * Open socket.
840      */
841     s = socket(address_family, SOCK_STREAM, 0);
842
843 #ifndef NO_IPV6
844     /* If the host doesn't support IPv6 try fallback to IPv4. */
845     if (s < 0 && address_family == AF_INET6) {
846         address_family = AF_INET;
847         s = socket(address_family, SOCK_STREAM, 0);
848     }
849 #endif
850
851     if (s < 0) {
852         ret->error = strerror(errno);
853         return (Socket) ret;
854     }
855
856     cloexec(s);
857
858     ret->oobinline = 0;
859
860     if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
861                    (const char *)&on, sizeof(on)) < 0) {
862         ret->error = strerror(errno);
863         close(s);
864         return (Socket) ret;
865     }
866
867     retcode = -1;
868     addr = NULL; addrlen = -1;         /* placate optimiser */
869
870     if (srcaddr != NULL) {
871 #ifndef NO_IPV6
872         hints.ai_flags = AI_NUMERICHOST;
873         hints.ai_family = address_family;
874         hints.ai_socktype = SOCK_STREAM;
875         hints.ai_protocol = 0;
876         hints.ai_addrlen = 0;
877         hints.ai_addr = NULL;
878         hints.ai_canonname = NULL;
879         hints.ai_next = NULL;
880         assert(port >= 0 && port <= 99999);
881         sprintf(portstr, "%d", port);
882         {
883             char *trimmed_addr = host_strduptrim(srcaddr);
884             retcode = getaddrinfo(trimmed_addr, portstr, &hints, &ai);
885             sfree(trimmed_addr);
886         }
887         if (retcode == 0) {
888             addr = (union sockaddr_union *)ai->ai_addr;
889             addrlen = ai->ai_addrlen;
890         }
891 #else
892         memset(&u,'\0',sizeof u);
893         u.sin.sin_family = AF_INET;
894         u.sin.sin_port = htons(port);
895         u.sin.sin_addr.s_addr = inet_addr(srcaddr);
896         if (u.sin.sin_addr.s_addr != (in_addr_t)(-1)) {
897             /* Override localhost_only with specified listen addr. */
898             ret->localhost_only = ipv4_is_loopback(u.sin.sin_addr);
899         }
900         addr = &u;
901         addrlen = sizeof(u.sin);
902         retcode = 0;
903 #endif
904     }
905
906     if (retcode != 0) {
907         memset(&u,'\0',sizeof u);
908 #ifndef NO_IPV6
909         if (address_family == AF_INET6) {
910             u.sin6.sin6_family = AF_INET6;
911             u.sin6.sin6_port = htons(port);
912             if (local_host_only)
913                 u.sin6.sin6_addr = in6addr_loopback;
914             else
915                 u.sin6.sin6_addr = in6addr_any;
916             addr = &u;
917             addrlen = sizeof(u.sin6);
918         } else
919 #endif
920         {
921             u.sin.sin_family = AF_INET;
922             u.sin.sin_port = htons(port);
923             if (local_host_only)
924                 u.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
925             else
926                 u.sin.sin_addr.s_addr = htonl(INADDR_ANY);
927             addr = &u;
928             addrlen = sizeof(u.sin);
929         }
930     }
931
932     retcode = bind(s, &addr->sa, addrlen);
933
934 #ifndef NO_IPV6
935     if (ai)
936         freeaddrinfo(ai);
937 #endif
938
939     if (retcode < 0) {
940         close(s);
941         ret->error = strerror(errno);
942         return (Socket) ret;
943     }
944
945     if (listen(s, SOMAXCONN) < 0) {
946         close(s);
947         ret->error = strerror(errno);
948         return (Socket) ret;
949     }
950
951 #ifndef NO_IPV6
952     /*
953      * If we were given ADDRTYPE_UNSPEC, we must also create an
954      * IPv4 listening socket and link it to this one.
955      */
956     if (address_family == AF_INET6 && orig_address_family == ADDRTYPE_UNSPEC) {
957         Actual_Socket other;
958
959         other = (Actual_Socket) sk_newlistener(srcaddr, port, plug,
960                                                local_host_only, ADDRTYPE_IPV4);
961
962         if (other) {
963             if (!other->error) {
964                 other->parent = ret;
965                 ret->child = other;
966             } else {
967                 /* If we couldn't create a listening socket on IPv4 as well
968                  * as IPv6, we must return an error overall. */
969                 close(s);
970                 sfree(ret);
971                 return (Socket) other;
972             }
973         }
974     }
975 #endif
976
977     ret->s = s;
978
979     uxsel_tell(ret);
980     add234(sktree, ret);
981
982     return (Socket) ret;
983 }
984
985 static void sk_tcp_close(Socket sock)
986 {
987     Actual_Socket s = (Actual_Socket) sock;
988
989     if (s->child)
990         sk_tcp_close((Socket)s->child);
991
992     uxsel_del(s->s);
993     del234(sktree, s);
994     close(s->s);
995     if (s->addr)
996         sk_addr_free(s->addr);
997     sfree(s);
998 }
999
1000 void *sk_getxdmdata(void *sock, int *lenp)
1001 {
1002     Actual_Socket s = (Actual_Socket) sock;
1003     union sockaddr_union u;
1004     socklen_t addrlen;
1005     char *buf;
1006     static unsigned int unix_addr = 0xFFFFFFFF;
1007
1008     /*
1009      * We must check that this socket really _is_ an Actual_Socket.
1010      */
1011     if (s->fn != &tcp_fn_table)
1012         return NULL;                   /* failure */
1013
1014     addrlen = sizeof(u);
1015     if (getsockname(s->s, &u.sa, &addrlen) < 0)
1016         return NULL;
1017     switch(u.sa.sa_family) {
1018       case AF_INET:
1019         *lenp = 6;
1020         buf = snewn(*lenp, char);
1021         PUT_32BIT_MSB_FIRST(buf, ntohl(u.sin.sin_addr.s_addr));
1022         PUT_16BIT_MSB_FIRST(buf+4, ntohs(u.sin.sin_port));
1023         break;
1024 #ifndef NO_IPV6
1025     case AF_INET6:
1026         *lenp = 6;
1027         buf = snewn(*lenp, char);
1028         if (IN6_IS_ADDR_V4MAPPED(&u.sin6.sin6_addr)) {
1029             memcpy(buf, u.sin6.sin6_addr.s6_addr + 12, 4);
1030             PUT_16BIT_MSB_FIRST(buf+4, ntohs(u.sin6.sin6_port));
1031         } else
1032             /* This is stupid, but it's what XLib does. */
1033             memset(buf, 0, 6);
1034         break;
1035 #endif
1036       case AF_UNIX:
1037         *lenp = 6;
1038         buf = snewn(*lenp, char);
1039         PUT_32BIT_MSB_FIRST(buf, unix_addr--);
1040         PUT_16BIT_MSB_FIRST(buf+4, getpid());
1041         break;
1042
1043         /* XXX IPV6 */
1044
1045       default:
1046         return NULL;
1047     }
1048
1049     return buf;
1050 }
1051
1052 /*
1053  * Deal with socket errors detected in try_send().
1054  */
1055 static void socket_error_callback(void *vs)
1056 {
1057     Actual_Socket s = (Actual_Socket)vs;
1058
1059     /*
1060      * Just in case other socket work has caused this socket to vanish
1061      * or become somehow non-erroneous before this callback arrived...
1062      */
1063     if (!find234(sktree, s, NULL) || !s->pending_error)
1064         return;
1065
1066     /*
1067      * An error has occurred on this socket. Pass it to the plug.
1068      */
1069     plug_closing(s->plug, strerror(s->pending_error), s->pending_error, 0);
1070 }
1071
1072 /*
1073  * The function which tries to send on a socket once it's deemed
1074  * writable.
1075  */
1076 void try_send(Actual_Socket s)
1077 {
1078     while (s->sending_oob || bufchain_size(&s->output_data) > 0) {
1079         int nsent;
1080         int err;
1081         void *data;
1082         int len, urgentflag;
1083
1084         if (s->sending_oob) {
1085             urgentflag = MSG_OOB;
1086             len = s->sending_oob;
1087             data = &s->oobdata;
1088         } else {
1089             urgentflag = 0;
1090             bufchain_prefix(&s->output_data, &data, &len);
1091         }
1092         nsent = send(s->s, data, len, urgentflag);
1093         noise_ultralight(nsent);
1094         if (nsent <= 0) {
1095             err = (nsent < 0 ? errno : 0);
1096             if (err == EWOULDBLOCK) {
1097                 /*
1098                  * Perfectly normal: we've sent all we can for the moment.
1099                  */
1100                 s->writable = FALSE;
1101                 return;
1102             } else {
1103                 /*
1104                  * We unfortunately can't just call plug_closing(),
1105                  * because it's quite likely that we're currently
1106                  * _in_ a call from the code we'd be calling back
1107                  * to, so we'd have to make half the SSH code
1108                  * reentrant. Instead we flag a pending error on
1109                  * the socket, to be dealt with (by calling
1110                  * plug_closing()) at some suitable future moment.
1111                  */
1112                 s->pending_error = err;
1113                 /*
1114                  * Immediately cease selecting on this socket, so that
1115                  * we don't tight-loop repeatedly trying to do
1116                  * whatever it was that went wrong.
1117                  */
1118                 uxsel_tell(s);
1119                 /*
1120                  * Arrange to be called back from the top level to
1121                  * deal with the error condition on this socket.
1122                  */
1123                 queue_toplevel_callback(socket_error_callback, s);
1124                 return;
1125             }
1126         } else {
1127             if (s->sending_oob) {
1128                 if (nsent < len) {
1129                     memmove(s->oobdata, s->oobdata+nsent, len-nsent);
1130                     s->sending_oob = len - nsent;
1131                 } else {
1132                     s->sending_oob = 0;
1133                 }
1134             } else {
1135                 bufchain_consume(&s->output_data, nsent);
1136             }
1137         }
1138     }
1139
1140     /*
1141      * If we reach here, we've finished sending everything we might
1142      * have needed to send. Send EOF, if we need to.
1143      */
1144     if (s->outgoingeof == EOF_PENDING) {
1145         shutdown(s->s, SHUT_WR);
1146         s->outgoingeof = EOF_SENT;
1147     }
1148
1149     /*
1150      * Also update the select status, because we don't need to select
1151      * for writing any more.
1152      */
1153     uxsel_tell(s);
1154 }
1155
1156 static int sk_tcp_write(Socket sock, const char *buf, int len)
1157 {
1158     Actual_Socket s = (Actual_Socket) sock;
1159
1160     assert(s->outgoingeof == EOF_NO);
1161
1162     /*
1163      * Add the data to the buffer list on the socket.
1164      */
1165     bufchain_add(&s->output_data, buf, len);
1166
1167     /*
1168      * Now try sending from the start of the buffer list.
1169      */
1170     if (s->writable)
1171         try_send(s);
1172
1173     /*
1174      * Update the select() status to correctly reflect whether or
1175      * not we should be selecting for write.
1176      */
1177     uxsel_tell(s);
1178
1179     return bufchain_size(&s->output_data);
1180 }
1181
1182 static int sk_tcp_write_oob(Socket sock, const char *buf, int len)
1183 {
1184     Actual_Socket s = (Actual_Socket) sock;
1185
1186     assert(s->outgoingeof == EOF_NO);
1187
1188     /*
1189      * Replace the buffer list on the socket with the data.
1190      */
1191     bufchain_clear(&s->output_data);
1192     assert(len <= sizeof(s->oobdata));
1193     memcpy(s->oobdata, buf, len);
1194     s->sending_oob = len;
1195
1196     /*
1197      * Now try sending from the start of the buffer list.
1198      */
1199     if (s->writable)
1200         try_send(s);
1201
1202     /*
1203      * Update the select() status to correctly reflect whether or
1204      * not we should be selecting for write.
1205      */
1206     uxsel_tell(s);
1207
1208     return s->sending_oob;
1209 }
1210
1211 static void sk_tcp_write_eof(Socket sock)
1212 {
1213     Actual_Socket s = (Actual_Socket) sock;
1214
1215     assert(s->outgoingeof == EOF_NO);
1216
1217     /*
1218      * Mark the socket as pending outgoing EOF.
1219      */
1220     s->outgoingeof = EOF_PENDING;
1221
1222     /*
1223      * Now try sending from the start of the buffer list.
1224      */
1225     if (s->writable)
1226         try_send(s);
1227
1228     /*
1229      * Update the select() status to correctly reflect whether or
1230      * not we should be selecting for write.
1231      */
1232     uxsel_tell(s);
1233 }
1234
1235 static int net_select_result(int fd, int event)
1236 {
1237     int ret;
1238     char buf[20480];                   /* nice big buffer for plenty of speed */
1239     Actual_Socket s;
1240     u_long atmark;
1241
1242     /* Find the Socket structure */
1243     s = find234(sktree, &fd, cmpforsearch);
1244     if (!s)
1245         return 1;                      /* boggle */
1246
1247     noise_ultralight(event);
1248
1249     switch (event) {
1250       case 4:                          /* exceptional */
1251         if (!s->oobinline) {
1252             /*
1253              * On a non-oobinline socket, this indicates that we
1254              * can immediately perform an OOB read and get back OOB
1255              * data, which we will send to the back end with
1256              * type==2 (urgent data).
1257              */
1258             ret = recv(s->s, buf, sizeof(buf), MSG_OOB);
1259             noise_ultralight(ret);
1260             if (ret <= 0) {
1261                 return plug_closing(s->plug,
1262                                     ret == 0 ? "Internal networking trouble" :
1263                                     strerror(errno), errno, 0);
1264             } else {
1265                 /*
1266                  * Receiving actual data on a socket means we can
1267                  * stop falling back through the candidate
1268                  * addresses to connect to.
1269                  */
1270                 if (s->addr) {
1271                     sk_addr_free(s->addr);
1272                     s->addr = NULL;
1273                 }
1274                 return plug_receive(s->plug, 2, buf, ret);
1275             }
1276             break;
1277         }
1278
1279         /*
1280          * If we reach here, this is an oobinline socket, which
1281          * means we should set s->oobpending and then deal with it
1282          * when we get called for the readability event (which
1283          * should also occur).
1284          */
1285         s->oobpending = TRUE;
1286         break;
1287       case 1:                          /* readable; also acceptance */
1288         if (s->listener) {
1289             /*
1290              * On a listening socket, the readability event means a
1291              * connection is ready to be accepted.
1292              */
1293             union sockaddr_union su;
1294             socklen_t addrlen = sizeof(su);
1295             accept_ctx_t actx;
1296             int t;  /* socket of connection */
1297
1298             memset(&su, 0, addrlen);
1299             t = accept(s->s, &su.sa, &addrlen);
1300             if (t < 0) {
1301                 break;
1302             }
1303
1304             nonblock(t);
1305             actx.i = t;
1306
1307             if ((!s->addr || s->addr->superfamily != UNIX) &&
1308                 s->localhost_only && !sockaddr_is_loopback(&su.sa)) {
1309                 close(t);              /* someone let nonlocal through?! */
1310             } else if (plug_accepting(s->plug, sk_tcp_accept, actx)) {
1311                 close(t);              /* denied or error */
1312             }
1313             break;
1314         }
1315
1316         /*
1317          * If we reach here, this is not a listening socket, so
1318          * readability really means readability.
1319          */
1320
1321         /* In the case the socket is still frozen, we don't even bother */
1322         if (s->frozen)
1323             break;
1324
1325         /*
1326          * We have received data on the socket. For an oobinline
1327          * socket, this might be data _before_ an urgent pointer,
1328          * in which case we send it to the back end with type==1
1329          * (data prior to urgent).
1330          */
1331         if (s->oobinline && s->oobpending) {
1332             atmark = 1;
1333             if (ioctl(s->s, SIOCATMARK, &atmark) == 0 && atmark)
1334                 s->oobpending = FALSE; /* clear this indicator */
1335         } else
1336             atmark = 1;
1337
1338         ret = recv(s->s, buf, s->oobpending ? 1 : sizeof(buf), 0);
1339         noise_ultralight(ret);
1340         if (ret < 0) {
1341             if (errno == EWOULDBLOCK) {
1342                 break;
1343             }
1344         }
1345         if (ret < 0) {
1346             /*
1347              * An error at this point _might_ be an error reported
1348              * by a non-blocking connect(). So before we return a
1349              * panic status to the user, let's just see whether
1350              * that's the case.
1351              */
1352             int err = errno;
1353             if (s->addr) {
1354                 plug_log(s->plug, 1, s->addr, s->port, strerror(err), err);
1355                 while (s->addr && sk_nextaddr(s->addr, &s->step)) {
1356                     err = try_connect(s);
1357                 }
1358             }
1359             if (err != 0)
1360                 return plug_closing(s->plug, strerror(err), err, 0);
1361         } else if (0 == ret) {
1362             s->incomingeof = TRUE;     /* stop trying to read now */
1363             uxsel_tell(s);
1364             return plug_closing(s->plug, NULL, 0, 0);
1365         } else {
1366             /*
1367              * Receiving actual data on a socket means we can
1368              * stop falling back through the candidate
1369              * addresses to connect to.
1370              */
1371             if (s->addr) {
1372                 sk_addr_free(s->addr);
1373                 s->addr = NULL;
1374             }
1375             return plug_receive(s->plug, atmark ? 0 : 1, buf, ret);
1376         }
1377         break;
1378       case 2:                          /* writable */
1379         if (!s->connected) {
1380             /*
1381              * select() reports a socket as _writable_ when an
1382              * asynchronous connection is completed.
1383              */
1384             s->connected = s->writable = 1;
1385             uxsel_tell(s);
1386             break;
1387         } else {
1388             int bufsize_before, bufsize_after;
1389             s->writable = 1;
1390             bufsize_before = s->sending_oob + bufchain_size(&s->output_data);
1391             try_send(s);
1392             bufsize_after = s->sending_oob + bufchain_size(&s->output_data);
1393             if (bufsize_after < bufsize_before)
1394                 plug_sent(s->plug, bufsize_after);
1395         }
1396         break;
1397     }
1398
1399     return 1;
1400 }
1401
1402 /*
1403  * Special error values are returned from sk_namelookup and sk_new
1404  * if there's a problem. These functions extract an error message,
1405  * or return NULL if there's no problem.
1406  */
1407 const char *sk_addr_error(SockAddr addr)
1408 {
1409     return addr->error;
1410 }
1411 static const char *sk_tcp_socket_error(Socket sock)
1412 {
1413     Actual_Socket s = (Actual_Socket) sock;
1414     return s->error;
1415 }
1416
1417 static void sk_tcp_set_frozen(Socket sock, int is_frozen)
1418 {
1419     Actual_Socket s = (Actual_Socket) sock;
1420     if (s->frozen == is_frozen)
1421         return;
1422     s->frozen = is_frozen;
1423     uxsel_tell(s);
1424 }
1425
1426 static char *sk_tcp_peer_info(Socket sock)
1427 {
1428     Actual_Socket s = (Actual_Socket) sock;
1429     struct sockaddr_storage addr;
1430     socklen_t addrlen = sizeof(addr);
1431     char buf[INET6_ADDRSTRLEN];
1432
1433     if (getpeername(s->s, (struct sockaddr *)&addr, &addrlen) < 0)
1434         return NULL;
1435     if (addr.ss_family == AF_INET) {
1436         return dupprintf
1437             ("%s:%d",
1438              inet_ntoa(((struct sockaddr_in *)&addr)->sin_addr),
1439              (int)ntohs(((struct sockaddr_in *)&addr)->sin_port));
1440 #ifndef NO_IPV6
1441     } else if (addr.ss_family == AF_INET6) {
1442         return dupprintf
1443             ("[%s]:%d",
1444              inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&addr)->sin6_addr,
1445                        buf, sizeof(buf)),
1446              (int)ntohs(((struct sockaddr_in6 *)&addr)->sin6_port));
1447 #endif
1448     } else if (addr.ss_family == AF_UNIX) {
1449         /*
1450          * For Unix sockets, the source address is unlikely to be
1451          * helpful. Instead, we try SO_PEERCRED and try to get the
1452          * source pid.
1453          */
1454         int pid, uid, gid;
1455         if (so_peercred(s->s, &pid, &uid, &gid)) {
1456             char uidbuf[64], gidbuf[64];
1457             sprintf(uidbuf, "%d", uid);
1458             sprintf(gidbuf, "%d", gid);
1459             struct passwd *pw = getpwuid(uid);
1460             struct group *gr = getgrgid(gid);
1461             return dupprintf("pid %d (%s:%s)", pid,
1462                              pw ? pw->pw_name : uidbuf,
1463                              gr ? gr->gr_name : gidbuf);
1464         }
1465         return NULL;
1466     } else {
1467         return NULL;
1468     }
1469 }
1470
1471 static void uxsel_tell(Actual_Socket s)
1472 {
1473     int rwx = 0;
1474     if (!s->pending_error) {
1475         if (s->listener) {
1476             rwx |= 1;                  /* read == accept */
1477         } else {
1478             if (!s->connected)
1479                 rwx |= 2;              /* write == connect */
1480             if (s->connected && !s->frozen && !s->incomingeof)
1481                 rwx |= 1 | 4;          /* read, except */
1482             if (bufchain_size(&s->output_data))
1483                 rwx |= 2;              /* write */
1484         }
1485     }
1486     uxsel_set(s->s, rwx, net_select_result);
1487 }
1488
1489 int net_service_lookup(char *service)
1490 {
1491     struct servent *se;
1492     se = getservbyname(service, NULL);
1493     if (se != NULL)
1494         return ntohs(se->s_port);
1495     else
1496         return 0;
1497 }
1498
1499 char *get_hostname(void)
1500 {
1501     int len = 128;
1502     char *hostname = NULL;
1503     do {
1504         len *= 2;
1505         hostname = sresize(hostname, len, char);
1506         if ((gethostname(hostname, len) < 0) &&
1507             (errno != ENAMETOOLONG)) {
1508             sfree(hostname);
1509             hostname = NULL;
1510             break;
1511         }
1512     } while (strlen(hostname) >= len-1);
1513     return hostname;
1514 }
1515
1516 SockAddr platform_get_x11_unix_address(const char *sockpath, int displaynum)
1517 {
1518     SockAddr ret = snew(struct SockAddr_tag);
1519     int n;
1520
1521     memset(ret, 0, sizeof *ret);
1522     ret->superfamily = UNIX;
1523     /*
1524      * In special circumstances (notably Mac OS X Leopard), we'll
1525      * have been passed an explicit Unix socket path.
1526      */
1527     if (sockpath) {
1528         n = snprintf(ret->hostname, sizeof ret->hostname,
1529                      "%s", sockpath);
1530     } else {
1531         n = snprintf(ret->hostname, sizeof ret->hostname,
1532                      "%s%d", X11_UNIX_PATH, displaynum);
1533     }
1534
1535     if (n < 0)
1536         ret->error = "snprintf failed";
1537     else if (n >= sizeof ret->hostname)
1538         ret->error = "X11 UNIX name too long";
1539
1540 #ifndef NO_IPV6
1541     ret->ais = NULL;
1542 #else
1543     ret->addresses = NULL;
1544     ret->naddresses = 0;
1545 #endif
1546     ret->refcount = 1;
1547     return ret;
1548 }
1549
1550 SockAddr unix_sock_addr(const char *path)
1551 {
1552     SockAddr ret = snew(struct SockAddr_tag);
1553     int n;
1554
1555     memset(ret, 0, sizeof *ret);
1556     ret->superfamily = UNIX;
1557     n = snprintf(ret->hostname, sizeof ret->hostname, "%s", path);
1558
1559     if (n < 0)
1560         ret->error = "snprintf failed";
1561     else if (n >= sizeof ret->hostname)
1562         ret->error = "socket pathname too long";
1563
1564 #ifndef NO_IPV6
1565     ret->ais = NULL;
1566 #else
1567     ret->addresses = NULL;
1568     ret->naddresses = 0;
1569 #endif
1570     ret->refcount = 1;
1571     return ret;
1572 }
1573
1574 Socket new_unix_listener(SockAddr listenaddr, Plug plug)
1575 {
1576     int s;
1577     union sockaddr_union u;
1578     union sockaddr_union *addr;
1579     int addrlen;
1580     Actual_Socket ret;
1581     int retcode;
1582
1583     /*
1584      * Create Socket structure.
1585      */
1586     ret = snew(struct Socket_tag);
1587     ret->fn = &tcp_fn_table;
1588     ret->error = NULL;
1589     ret->plug = plug;
1590     bufchain_init(&ret->output_data);
1591     ret->writable = 0;                 /* to start with */
1592     ret->sending_oob = 0;
1593     ret->frozen = 0;
1594     ret->localhost_only = TRUE;
1595     ret->pending_error = 0;
1596     ret->parent = ret->child = NULL;
1597     ret->oobpending = FALSE;
1598     ret->outgoingeof = EOF_NO;
1599     ret->incomingeof = FALSE;
1600     ret->listener = 1;
1601     ret->addr = listenaddr;
1602     ret->s = -1;
1603
1604     assert(listenaddr->superfamily == UNIX);
1605
1606     /*
1607      * Open socket.
1608      */
1609     s = socket(AF_UNIX, SOCK_STREAM, 0);
1610     if (s < 0) {
1611         ret->error = strerror(errno);
1612         return (Socket) ret;
1613     }
1614
1615     cloexec(s);
1616
1617     ret->oobinline = 0;
1618
1619     memset(&u, '\0', sizeof(u));
1620     u.su.sun_family = AF_UNIX;
1621     strncpy(u.su.sun_path, listenaddr->hostname, sizeof(u.su.sun_path)-1);
1622     addr = &u;
1623     addrlen = sizeof(u.su);
1624
1625     if (unlink(u.su.sun_path) < 0 && errno != ENOENT) {
1626         close(s);
1627         ret->error = strerror(errno);
1628         return (Socket) ret;
1629     }
1630
1631     retcode = bind(s, &addr->sa, addrlen);
1632     if (retcode < 0) {
1633         close(s);
1634         ret->error = strerror(errno);
1635         return (Socket) ret;
1636     }
1637
1638     if (listen(s, SOMAXCONN) < 0) {
1639         close(s);
1640         ret->error = strerror(errno);
1641         return (Socket) ret;
1642     }
1643
1644     ret->s = s;
1645
1646     uxsel_tell(ret);
1647     add234(sktree, ret);
1648
1649     return (Socket) ret;
1650 }