2 * Windows networking abstraction.
4 * For the IPv6 code in here I am indebted to Jeroen Massar and
12 #define DEFINE_PLUG_METHOD_MACROS
21 #pragma clang diagnostic push
22 #pragma clang diagnostic ignored "-Wmissing-braces"
24 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
25 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
27 #pragma clang diagnostic pop
31 #define ipv4_is_loopback(addr) \
32 ((p_ntohl(addr.s_addr) & 0xFF000000L) == 0x7F000000L)
35 * We used to typedef struct Socket_tag *Socket.
37 * Since we have made the networking abstraction slightly more
38 * abstract, Socket no longer means a tcp socket (it could mean
39 * an ssl socket). So now we must use Actual_Socket when we know
40 * we are talking about a tcp socket.
42 typedef struct Socket_tag *Actual_Socket;
45 * Mutable state that goes with a SockAddr: stores information
46 * about where in the list of candidate IP(v*) addresses we've
49 typedef struct SockAddrStep_tag SockAddrStep;
50 struct SockAddrStep_tag {
52 struct addrinfo *ai; /* steps along addr->ais */
58 const struct socket_function_table *fn;
59 /* the above variable absolutely *must* be the first in this structure */
66 int frozen; /* this causes readability notifications to be ignored */
67 int frozen_readable; /* this means we missed at least one readability
68 * notification while we were frozen */
69 int localhost_only; /* for listening sockets */
72 int oobinline, nodelay, keepalive, privport;
73 enum { EOF_NO, EOF_PENDING, EOF_SENT } outgoingeof;
77 int pending_error; /* in case send() returns error */
79 * We sometimes need pairs of Socket structures to be linked:
80 * if we are listening on the same IPv6 and v4 port, for
81 * example. So here we define `parent' and `child' pointers to
84 Actual_Socket parent, child;
91 int namedpipe; /* indicates that this SockAddr is phony, holding a Windows
92 * named pipe pathname instead of a network address */
94 struct addrinfo *ais; /* Addresses IPv6 style. */
96 unsigned long *addresses; /* Addresses IPv4 style. */
98 char hostname[512]; /* Store an unresolved host name. */
102 * Which address family this address belongs to. AF_INET for IPv4;
103 * AF_INET6 for IPv6; AF_UNSPEC indicates that name resolution has
104 * not been done and a simple host name is held in this SockAddr
108 #define SOCKADDR_FAMILY(addr, step) \
109 (!(addr)->resolved ? AF_UNSPEC : \
110 (step).ai ? (step).ai->ai_family : AF_INET)
112 #define SOCKADDR_FAMILY(addr, step) \
113 (!(addr)->resolved ? AF_UNSPEC : AF_INET)
117 * Start a SockAddrStep structure to step through multiple
121 #define START_STEP(addr, step) \
122 ((step).ai = (addr)->ais, (step).curraddr = 0)
124 #define START_STEP(addr, step) \
125 ((step).curraddr = 0)
128 static tree234 *sktree;
130 static int cmpfortree(void *av, void *bv)
132 Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv;
133 unsigned long as = (unsigned long) a->s, bs = (unsigned long) b->s;
145 static int cmpforsearch(void *av, void *bv)
147 Actual_Socket b = (Actual_Socket) bv;
148 uintptr_t as = (uintptr_t) av, bs = (uintptr_t) b->s;
156 DECL_WINDOWS_FUNCTION(static, int, WSAStartup, (WORD, LPWSADATA));
157 DECL_WINDOWS_FUNCTION(static, int, WSACleanup, (void));
158 DECL_WINDOWS_FUNCTION(static, int, closesocket, (SOCKET));
159 DECL_WINDOWS_FUNCTION(static, u_long, ntohl, (u_long));
160 DECL_WINDOWS_FUNCTION(static, u_long, htonl, (u_long));
161 DECL_WINDOWS_FUNCTION(static, u_short, htons, (u_short));
162 DECL_WINDOWS_FUNCTION(static, u_short, ntohs, (u_short));
163 DECL_WINDOWS_FUNCTION(static, int, gethostname, (char *, int));
164 DECL_WINDOWS_FUNCTION(static, struct hostent FAR *, gethostbyname,
166 DECL_WINDOWS_FUNCTION(static, struct servent FAR *, getservbyname,
167 (const char FAR *, const char FAR *));
168 DECL_WINDOWS_FUNCTION(static, unsigned long, inet_addr, (const char FAR *));
169 DECL_WINDOWS_FUNCTION(static, char FAR *, inet_ntoa, (struct in_addr));
170 DECL_WINDOWS_FUNCTION(static, const char FAR *, inet_ntop,
171 (int, void FAR *, char *, size_t));
172 DECL_WINDOWS_FUNCTION(static, int, connect,
173 (SOCKET, const struct sockaddr FAR *, int));
174 DECL_WINDOWS_FUNCTION(static, int, bind,
175 (SOCKET, const struct sockaddr FAR *, int));
176 DECL_WINDOWS_FUNCTION(static, int, setsockopt,
177 (SOCKET, int, int, const char FAR *, int));
178 DECL_WINDOWS_FUNCTION(static, SOCKET, socket, (int, int, int));
179 DECL_WINDOWS_FUNCTION(static, int, listen, (SOCKET, int));
180 DECL_WINDOWS_FUNCTION(static, int, send, (SOCKET, const char FAR *, int, int));
181 DECL_WINDOWS_FUNCTION(static, int, shutdown, (SOCKET, int));
182 DECL_WINDOWS_FUNCTION(static, int, ioctlsocket,
183 (SOCKET, long, u_long FAR *));
184 DECL_WINDOWS_FUNCTION(static, SOCKET, accept,
185 (SOCKET, struct sockaddr FAR *, int FAR *));
186 DECL_WINDOWS_FUNCTION(static, int, getpeername,
187 (SOCKET, struct sockaddr FAR *, int FAR *));
188 DECL_WINDOWS_FUNCTION(static, int, recv, (SOCKET, char FAR *, int, int));
189 DECL_WINDOWS_FUNCTION(static, int, WSAIoctl,
190 (SOCKET, DWORD, LPVOID, DWORD, LPVOID, DWORD,
191 LPDWORD, LPWSAOVERLAPPED,
192 LPWSAOVERLAPPED_COMPLETION_ROUTINE));
194 DECL_WINDOWS_FUNCTION(static, int, getaddrinfo,
195 (const char *nodename, const char *servname,
196 const struct addrinfo *hints, struct addrinfo **res));
197 DECL_WINDOWS_FUNCTION(static, void, freeaddrinfo, (struct addrinfo *res));
198 DECL_WINDOWS_FUNCTION(static, int, getnameinfo,
199 (const struct sockaddr FAR * sa, socklen_t salen,
200 char FAR * host, size_t hostlen, char FAR * serv,
201 size_t servlen, int flags));
202 DECL_WINDOWS_FUNCTION(static, char *, gai_strerror, (int ecode));
203 DECL_WINDOWS_FUNCTION(static, int, WSAAddressToStringA,
204 (LPSOCKADDR, DWORD, LPWSAPROTOCOL_INFO,
208 static HMODULE winsock_module = NULL;
209 static WSADATA wsadata;
211 static HMODULE winsock2_module = NULL;
212 static HMODULE wship6_module = NULL;
215 int sk_startup(int hi, int lo)
219 winsock_ver = MAKEWORD(hi, lo);
221 if (p_WSAStartup(winsock_ver, &wsadata)) {
225 if (LOBYTE(wsadata.wVersion) != LOBYTE(winsock_ver)) {
229 #ifdef NET_SETUP_DIAGNOSTICS
232 sprintf(buf, "Using WinSock %d.%d", hi, lo);
244 winsock_module = load_system32_dll("ws2_32.dll");
245 if (!winsock_module) {
246 winsock_module = load_system32_dll("wsock32.dll");
249 fatalbox("Unable to load any WinSock library");
252 /* Check if we have getaddrinfo in Winsock */
253 if (GetProcAddress(winsock_module, "getaddrinfo") != NULL) {
254 #ifdef NET_SETUP_DIAGNOSTICS
255 logevent(NULL, "Native WinSock IPv6 support detected");
257 GET_WINDOWS_FUNCTION(winsock_module, getaddrinfo);
258 GET_WINDOWS_FUNCTION(winsock_module, freeaddrinfo);
259 GET_WINDOWS_FUNCTION(winsock_module, getnameinfo);
260 GET_WINDOWS_FUNCTION(winsock_module, gai_strerror);
262 /* Fall back to wship6.dll for Windows 2000 */
263 wship6_module = load_system32_dll("wship6.dll");
265 #ifdef NET_SETUP_DIAGNOSTICS
266 logevent(NULL, "WSH IPv6 support detected");
268 GET_WINDOWS_FUNCTION(wship6_module, getaddrinfo);
269 GET_WINDOWS_FUNCTION(wship6_module, freeaddrinfo);
270 GET_WINDOWS_FUNCTION(wship6_module, getnameinfo);
271 GET_WINDOWS_FUNCTION(wship6_module, gai_strerror);
273 #ifdef NET_SETUP_DIAGNOSTICS
274 logevent(NULL, "No IPv6 support detected");
278 GET_WINDOWS_FUNCTION(winsock2_module, WSAAddressToStringA);
280 #ifdef NET_SETUP_DIAGNOSTICS
281 logevent(NULL, "PuTTY was built without IPv6 support");
285 GET_WINDOWS_FUNCTION(winsock_module, WSAAsyncSelect);
286 GET_WINDOWS_FUNCTION(winsock_module, WSAEventSelect);
287 GET_WINDOWS_FUNCTION(winsock_module, select);
288 GET_WINDOWS_FUNCTION(winsock_module, WSAGetLastError);
289 GET_WINDOWS_FUNCTION(winsock_module, WSAEnumNetworkEvents);
290 GET_WINDOWS_FUNCTION(winsock_module, WSAStartup);
291 GET_WINDOWS_FUNCTION(winsock_module, WSACleanup);
292 GET_WINDOWS_FUNCTION(winsock_module, closesocket);
293 GET_WINDOWS_FUNCTION(winsock_module, ntohl);
294 GET_WINDOWS_FUNCTION(winsock_module, htonl);
295 GET_WINDOWS_FUNCTION(winsock_module, htons);
296 GET_WINDOWS_FUNCTION(winsock_module, ntohs);
297 GET_WINDOWS_FUNCTION(winsock_module, gethostname);
298 GET_WINDOWS_FUNCTION(winsock_module, gethostbyname);
299 GET_WINDOWS_FUNCTION(winsock_module, getservbyname);
300 GET_WINDOWS_FUNCTION(winsock_module, inet_addr);
301 GET_WINDOWS_FUNCTION(winsock_module, inet_ntoa);
302 GET_WINDOWS_FUNCTION(winsock_module, inet_ntop);
303 GET_WINDOWS_FUNCTION(winsock_module, connect);
304 GET_WINDOWS_FUNCTION(winsock_module, bind);
305 GET_WINDOWS_FUNCTION(winsock_module, setsockopt);
306 GET_WINDOWS_FUNCTION(winsock_module, socket);
307 GET_WINDOWS_FUNCTION(winsock_module, listen);
308 GET_WINDOWS_FUNCTION(winsock_module, send);
309 GET_WINDOWS_FUNCTION(winsock_module, shutdown);
310 GET_WINDOWS_FUNCTION(winsock_module, ioctlsocket);
311 GET_WINDOWS_FUNCTION(winsock_module, accept);
312 GET_WINDOWS_FUNCTION(winsock_module, getpeername);
313 GET_WINDOWS_FUNCTION(winsock_module, recv);
314 GET_WINDOWS_FUNCTION(winsock_module, WSAIoctl);
316 /* Try to get the best WinSock version we can get */
317 if (!sk_startup(2,2) &&
320 fatalbox("Unable to initialise WinSock");
323 sktree = newtree234(cmpfortree);
326 void sk_cleanup(void)
332 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
342 FreeLibrary(winsock_module);
345 FreeLibrary(wship6_module);
354 static int errstring_find(void *av, void *bv)
357 struct errstring *b = (struct errstring *)bv;
364 static int errstring_compare(void *av, void *bv)
366 struct errstring *a = (struct errstring *)av;
367 return errstring_find(&a->error, bv);
370 static tree234 *errstrings = NULL;
372 const char *winsock_error_string(int error)
374 const char prefix[] = "Network error: ";
375 struct errstring *es;
378 * Error codes we know about and have historically had reasonably
379 * sensible error messages for.
383 return "Network error: Permission denied";
385 return "Network error: Address already in use";
386 case WSAEADDRNOTAVAIL:
387 return "Network error: Cannot assign requested address";
388 case WSAEAFNOSUPPORT:
390 "Network error: Address family not supported by protocol family";
392 return "Network error: Operation already in progress";
393 case WSAECONNABORTED:
394 return "Network error: Software caused connection abort";
395 case WSAECONNREFUSED:
396 return "Network error: Connection refused";
398 return "Network error: Connection reset by peer";
399 case WSAEDESTADDRREQ:
400 return "Network error: Destination address required";
402 return "Network error: Bad address";
404 return "Network error: Host is down";
405 case WSAEHOSTUNREACH:
406 return "Network error: No route to host";
408 return "Network error: Operation now in progress";
410 return "Network error: Interrupted function call";
412 return "Network error: Invalid argument";
414 return "Network error: Socket is already connected";
416 return "Network error: Too many open files";
418 return "Network error: Message too long";
420 return "Network error: Network is down";
422 return "Network error: Network dropped connection on reset";
424 return "Network error: Network is unreachable";
426 return "Network error: No buffer space available";
428 return "Network error: Bad protocol option";
430 return "Network error: Socket is not connected";
432 return "Network error: Socket operation on non-socket";
434 return "Network error: Operation not supported";
435 case WSAEPFNOSUPPORT:
436 return "Network error: Protocol family not supported";
438 return "Network error: Too many processes";
439 case WSAEPROTONOSUPPORT:
440 return "Network error: Protocol not supported";
442 return "Network error: Protocol wrong type for socket";
444 return "Network error: Cannot send after socket shutdown";
445 case WSAESOCKTNOSUPPORT:
446 return "Network error: Socket type not supported";
448 return "Network error: Connection timed out";
450 return "Network error: Resource temporarily unavailable";
452 return "Network error: Graceful shutdown in progress";
456 * Generic code to handle any other error.
458 * Slightly nasty hack here: we want to return a static string
459 * which the caller will never have to worry about freeing, but on
460 * the other hand if we call FormatMessage to get it then it will
461 * want to either allocate a buffer or write into one we own.
463 * So what we do is to maintain a tree234 of error strings we've
464 * already used. New ones are allocated from the heap, but then
465 * put in this tree and kept forever.
469 errstrings = newtree234(errstring_compare);
471 es = find234(errstrings, &error, errstring_find);
474 int bufsize, bufused;
476 es = snew(struct errstring);
478 /* maximum size for FormatMessage is 64K */
479 bufsize = 65535 + sizeof(prefix);
480 es->text = snewn(bufsize, char);
481 strcpy(es->text, prefix);
482 bufused = strlen(es->text);
483 if (!FormatMessage((FORMAT_MESSAGE_FROM_SYSTEM |
484 FORMAT_MESSAGE_IGNORE_INSERTS), NULL, error,
485 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
486 es->text + bufused, bufsize - bufused, NULL)) {
487 sprintf(es->text + bufused,
488 "Windows error code %d (and FormatMessage returned %u)",
489 error, (unsigned int)GetLastError());
491 int len = strlen(es->text);
492 if (len > 0 && es->text[len-1] == '\n')
493 es->text[len-1] = '\0';
495 es->text = sresize(es->text, strlen(es->text) + 1, char);
496 add234(errstrings, es);
502 SockAddr sk_namelookup(const char *host, char **canonicalname,
505 SockAddr ret = snew(struct SockAddr_tag);
510 /* Default to IPv4. */
511 hint_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
513 address_family == ADDRTYPE_IPV6 ? AF_INET6 :
517 /* Clear the structure and default to IPv4. */
518 memset(ret, 0, sizeof(struct SockAddr_tag));
522 ret->namedpipe = FALSE;
523 ret->addresses = NULL;
524 ret->resolved = FALSE;
528 if ((a = p_inet_addr(host)) == (unsigned long) INADDR_NONE) {
529 struct hostent *h = NULL;
533 * Use getaddrinfo when it's available
536 struct addrinfo hints;
537 #ifdef NET_SETUP_DIAGNOSTICS
538 logevent(NULL, "Using getaddrinfo() for resolving");
540 memset(&hints, 0, sizeof(hints));
541 hints.ai_family = hint_family;
542 hints.ai_flags = AI_CANONNAME;
544 /* strip [] on IPv6 address literals */
545 char *trimmed_host = host_strduptrim(host);
546 err = p_getaddrinfo(trimmed_host, NULL, &hints, &ret->ais);
550 ret->resolved = TRUE;
554 #ifdef NET_SETUP_DIAGNOSTICS
555 logevent(NULL, "Using gethostbyname() for resolving");
558 * Otherwise use the IPv4-only gethostbyname...
559 * (NOTE: we don't use gethostbyname as a fallback!)
561 if ( (h = p_gethostbyname(host)) )
562 ret->resolved = TRUE;
564 err = p_WSAGetLastError();
567 if (!ret->resolved) {
568 ret->error = (err == WSAENETDOWN ? "Network is down" :
569 err == WSAHOST_NOT_FOUND ? "Host does not exist" :
570 err == WSATRY_AGAIN ? "Host not found" :
572 p_getaddrinfo&&p_gai_strerror ? p_gai_strerror(err) :
574 "gethostbyname: unknown error");
579 /* If we got an address info use that... */
581 /* Are we in IPv4 fallback mode? */
582 /* We put the IPv4 address into the a variable so we can further-on use the IPv4 code... */
583 if (ret->ais->ai_family == AF_INET)
585 (char *) &((SOCKADDR_IN *) ret->ais->
586 ai_addr)->sin_addr, sizeof(a));
588 if (ret->ais->ai_canonname)
589 strncpy(realhost, ret->ais->ai_canonname, lenof(realhost));
591 strncpy(realhost, host, lenof(realhost));
593 /* We used the IPv4-only gethostbyname()... */
598 for (n = 0; h->h_addr_list[n]; n++);
599 ret->addresses = snewn(n, unsigned long);
601 for (n = 0; n < ret->naddresses; n++) {
602 memcpy(&a, h->h_addr_list[n], sizeof(a));
603 ret->addresses[n] = p_ntohl(a);
605 memcpy(&a, h->h_addr, sizeof(a));
606 /* This way we are always sure the h->h_name is valid :) */
607 strncpy(realhost, h->h_name, sizeof(realhost));
612 * This must be a numeric IPv4 address because it caused a
613 * success return from inet_addr.
615 ret->addresses = snewn(1, unsigned long);
617 ret->addresses[0] = p_ntohl(a);
618 ret->resolved = TRUE;
619 strncpy(realhost, host, sizeof(realhost));
621 realhost[lenof(realhost)-1] = '\0';
622 *canonicalname = snewn(1+strlen(realhost), char);
623 strcpy(*canonicalname, realhost);
627 SockAddr sk_nonamelookup(const char *host)
629 SockAddr ret = snew(struct SockAddr_tag);
631 ret->resolved = FALSE;
635 ret->namedpipe = FALSE;
636 ret->addresses = NULL;
639 strncpy(ret->hostname, host, lenof(ret->hostname));
640 ret->hostname[lenof(ret->hostname)-1] = '\0';
644 SockAddr sk_namedpipe_addr(const char *pipename)
646 SockAddr ret = snew(struct SockAddr_tag);
648 ret->resolved = FALSE;
652 ret->namedpipe = TRUE;
653 ret->addresses = NULL;
656 strncpy(ret->hostname, pipename, lenof(ret->hostname));
657 ret->hostname[lenof(ret->hostname)-1] = '\0';
661 int sk_nextaddr(SockAddr addr, SockAddrStep *step)
665 if (step->ai->ai_next) {
666 step->ai = step->ai->ai_next;
672 if (step->curraddr+1 < addr->naddresses) {
680 void sk_getaddr(SockAddr addr, char *buf, int buflen)
683 START_STEP(addr, step);
688 if (p_WSAAddressToStringA) {
689 DWORD dwbuflen = buflen;
690 err = p_WSAAddressToStringA(step.ai->ai_addr, step.ai->ai_addrlen,
691 NULL, buf, &dwbuflen);
695 strncpy(buf, addr->hostname, buflen);
697 strncpy(buf, "<unknown>", buflen);
698 buf[buflen-1] = '\0';
702 if (SOCKADDR_FAMILY(addr, step) == AF_INET) {
704 assert(addr->addresses && step.curraddr < addr->naddresses);
705 a.s_addr = p_htonl(addr->addresses[step.curraddr]);
706 strncpy(buf, p_inet_ntoa(a), buflen);
707 buf[buflen-1] = '\0';
709 strncpy(buf, addr->hostname, buflen);
710 buf[buflen-1] = '\0';
715 * This constructs a SockAddr that points at one specific sub-address
716 * of a parent SockAddr. The returned SockAddr does not own all its
717 * own memory: it points into the old one's data structures, so it
718 * MUST NOT be used after the old one is freed, and it MUST NOT be
719 * passed to sk_addr_free. (The latter is why it's returned by value
720 * rather than dynamically allocated - that should clue in anyone
721 * writing a call to it that something is weird about it.)
723 static struct SockAddr_tag sk_extractaddr_tmp(
724 SockAddr addr, const SockAddrStep *step)
726 struct SockAddr_tag toret;
727 toret = *addr; /* structure copy */
731 toret.ais = step->ai;
733 if (SOCKADDR_FAMILY(addr, *step) == AF_INET
738 toret.addresses += step->curraddr;
743 int sk_addr_needs_port(SockAddr addr)
745 return addr->namedpipe ? FALSE : TRUE;
748 int sk_hostname_is_local(const char *name)
750 return !strcmp(name, "localhost") ||
751 !strcmp(name, "::1") ||
752 !strncmp(name, "127.", 4);
755 static INTERFACE_INFO local_interfaces[16];
756 static int n_local_interfaces; /* 0=not yet, -1=failed, >0=number */
758 static int ipv4_is_local_addr(struct in_addr addr)
760 if (ipv4_is_loopback(addr))
761 return 1; /* loopback addresses are local */
762 if (!n_local_interfaces) {
763 SOCKET s = p_socket(AF_INET, SOCK_DGRAM, 0);
767 p_WSAIoctl(s, SIO_GET_INTERFACE_LIST, NULL, 0,
768 local_interfaces, sizeof(local_interfaces),
769 &retbytes, NULL, NULL) == 0)
770 n_local_interfaces = retbytes / sizeof(INTERFACE_INFO);
772 logevent(NULL, "Unable to get list of local IP addresses");
774 if (n_local_interfaces > 0) {
776 for (i = 0; i < n_local_interfaces; i++) {
777 SOCKADDR_IN *address =
778 (SOCKADDR_IN *)&local_interfaces[i].iiAddress;
779 if (address->sin_addr.s_addr == addr.s_addr)
780 return 1; /* this address is local */
783 return 0; /* this address is not local */
786 int sk_address_is_local(SockAddr addr)
790 START_STEP(addr, step);
791 family = SOCKADDR_FAMILY(addr, step);
794 if (family == AF_INET6) {
795 return IN6_IS_ADDR_LOOPBACK(&((const struct sockaddr_in6 *)step.ai->ai_addr)->sin6_addr);
798 if (family == AF_INET) {
801 return ipv4_is_local_addr(((struct sockaddr_in *)step.ai->ai_addr)
807 assert(addr->addresses && step.curraddr < addr->naddresses);
808 a.s_addr = p_htonl(addr->addresses[step.curraddr]);
809 return ipv4_is_local_addr(a);
812 assert(family == AF_UNSPEC);
813 return 0; /* we don't know; assume not */
817 int sk_address_is_special_local(SockAddr addr)
819 return 0; /* no Unix-domain socket analogue here */
822 int sk_addrtype(SockAddr addr)
826 START_STEP(addr, step);
827 family = SOCKADDR_FAMILY(addr, step);
829 return (family == AF_INET ? ADDRTYPE_IPV4 :
831 family == AF_INET6 ? ADDRTYPE_IPV6 :
836 void sk_addrcopy(SockAddr addr, char *buf)
840 START_STEP(addr, step);
841 family = SOCKADDR_FAMILY(addr, step);
843 assert(family != AF_UNSPEC);
846 if (family == AF_INET)
847 memcpy(buf, &((struct sockaddr_in *)step.ai->ai_addr)->sin_addr,
848 sizeof(struct in_addr));
849 else if (family == AF_INET6)
850 memcpy(buf, &((struct sockaddr_in6 *)step.ai->ai_addr)->sin6_addr,
851 sizeof(struct in6_addr));
856 if (family == AF_INET) {
858 assert(addr->addresses && step.curraddr < addr->naddresses);
859 a.s_addr = p_htonl(addr->addresses[step.curraddr]);
860 memcpy(buf, (char*) &a.s_addr, 4);
864 void sk_addr_free(SockAddr addr)
866 if (--addr->refcount > 0)
869 if (addr->ais && p_freeaddrinfo)
870 p_freeaddrinfo(addr->ais);
873 sfree(addr->addresses);
877 SockAddr sk_addr_dup(SockAddr addr)
883 static Plug sk_tcp_plug(Socket sock, Plug p)
885 Actual_Socket s = (Actual_Socket) sock;
892 static void sk_tcp_flush(Socket s)
895 * We send data to the socket as soon as we can anyway,
896 * so we don't need to do anything here. :-)
900 static void sk_tcp_close(Socket s);
901 static int sk_tcp_write(Socket s, const char *data, int len);
902 static int sk_tcp_write_oob(Socket s, const char *data, int len);
903 static void sk_tcp_write_eof(Socket s);
904 static void sk_tcp_set_frozen(Socket s, int is_frozen);
905 static const char *sk_tcp_socket_error(Socket s);
906 static char *sk_tcp_peer_info(Socket s);
908 extern char *do_select(SOCKET skt, int startup);
910 static Socket sk_tcp_accept(accept_ctx_t ctx, Plug plug)
912 static const struct socket_function_table fn_table = {
929 * Create Socket structure.
931 ret = snew(struct Socket_tag);
935 bufchain_init(&ret->output_data);
936 ret->writable = 1; /* to start with */
937 ret->sending_oob = 0;
938 ret->outgoingeof = EOF_NO;
940 ret->frozen_readable = 0;
941 ret->localhost_only = 0; /* unused, but best init anyway */
942 ret->pending_error = 0;
943 ret->parent = ret->child = NULL;
946 ret->s = (SOCKET)ctx.p;
948 if (ret->s == INVALID_SOCKET) {
949 err = p_WSAGetLastError();
950 ret->error = winsock_error_string(err);
956 /* Set up a select mechanism. This could be an AsyncSelect on a
957 * window, or an EventSelect on an event object. */
958 errstr = do_select(ret->s, 1);
969 static DWORD try_connect(Actual_Socket sock)
981 if (sock->s != INVALID_SOCKET) {
982 do_select(sock->s, 0);
983 p_closesocket(sock->s);
987 struct SockAddr_tag thisaddr = sk_extractaddr_tmp(
988 sock->addr, &sock->step);
989 plug_log(sock->plug, 0, &thisaddr, sock->port, NULL, 0);
995 family = SOCKADDR_FAMILY(sock->addr, sock->step);
998 * Remove the socket from the tree before we overwrite its
999 * internal socket id, because that forms part of the tree's
1000 * sorting criterion. We'll add it back before exiting this
1001 * function, whether we changed anything or not.
1003 del234(sktree, sock);
1005 s = p_socket(family, SOCK_STREAM, 0);
1008 if (s == INVALID_SOCKET) {
1009 err = p_WSAGetLastError();
1010 sock->error = winsock_error_string(err);
1014 if (sock->oobinline) {
1016 p_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));
1019 if (sock->nodelay) {
1021 p_setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *) &b, sizeof(b));
1024 if (sock->keepalive) {
1026 p_setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *) &b, sizeof(b));
1030 * Bind to local address.
1033 localport = 1023; /* count from 1023 downwards */
1035 localport = 0; /* just use port 0 (ie winsock picks) */
1037 /* Loop round trying to bind */
1042 if (family == AF_INET6) {
1043 memset(&a6, 0, sizeof(a6));
1044 a6.sin6_family = AF_INET6;
1045 /*a6.sin6_addr = in6addr_any; */ /* == 0 done by memset() */
1046 a6.sin6_port = p_htons(localport);
1050 a.sin_family = AF_INET;
1051 a.sin_addr.s_addr = p_htonl(INADDR_ANY);
1052 a.sin_port = p_htons(localport);
1055 sockcode = p_bind(s, (family == AF_INET6 ?
1056 (struct sockaddr *) &a6 :
1057 (struct sockaddr *) &a),
1058 (family == AF_INET6 ? sizeof(a6) : sizeof(a)));
1060 sockcode = p_bind(s, (struct sockaddr *) &a, sizeof(a));
1062 if (sockcode != SOCKET_ERROR) {
1066 err = p_WSAGetLastError();
1067 if (err != WSAEADDRINUSE) /* failed, for a bad reason */
1072 break; /* we're only looping once */
1075 break; /* we might have got to the end */
1079 sock->error = winsock_error_string(err);
1084 * Connect to remote address.
1087 if (sock->step.ai) {
1088 if (family == AF_INET6) {
1089 a6.sin6_family = AF_INET6;
1090 a6.sin6_port = p_htons((short) sock->port);
1092 ((struct sockaddr_in6 *) sock->step.ai->ai_addr)->sin6_addr;
1093 a6.sin6_flowinfo = ((struct sockaddr_in6 *) sock->step.ai->ai_addr)->sin6_flowinfo;
1094 a6.sin6_scope_id = ((struct sockaddr_in6 *) sock->step.ai->ai_addr)->sin6_scope_id;
1096 a.sin_family = AF_INET;
1098 ((struct sockaddr_in *) sock->step.ai->ai_addr)->sin_addr;
1099 a.sin_port = p_htons((short) sock->port);
1104 assert(sock->addr->addresses && sock->step.curraddr < sock->addr->naddresses);
1105 a.sin_family = AF_INET;
1106 a.sin_addr.s_addr = p_htonl(sock->addr->addresses[sock->step.curraddr]);
1107 a.sin_port = p_htons((short) sock->port);
1110 /* Set up a select mechanism. This could be an AsyncSelect on a
1111 * window, or an EventSelect on an event object. */
1112 errstr = do_select(s, 1);
1114 sock->error = errstr;
1122 ((family == AF_INET6) ? (struct sockaddr *) &a6 :
1123 (struct sockaddr *) &a),
1124 (family == AF_INET6) ? sizeof(a6) : sizeof(a))
1126 p_connect(s, (struct sockaddr *) &a, sizeof(a))
1128 ) == SOCKET_ERROR) {
1129 err = p_WSAGetLastError();
1131 * We expect a potential EWOULDBLOCK here, because the
1132 * chances are the front end has done a select for
1133 * FD_CONNECT, so that connect() will complete
1136 if ( err != WSAEWOULDBLOCK ) {
1137 sock->error = winsock_error_string(err);
1142 * If we _don't_ get EWOULDBLOCK, the connect has completed
1143 * and we should set the socket as writable.
1153 * No matter what happened, put the socket back in the tree.
1155 add234(sktree, sock);
1158 struct SockAddr_tag thisaddr = sk_extractaddr_tmp(
1159 sock->addr, &sock->step);
1160 plug_log(sock->plug, 1, &thisaddr, sock->port, sock->error, err);
1165 Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
1166 int nodelay, int keepalive, Plug plug)
1168 static const struct socket_function_table fn_table = {
1176 sk_tcp_socket_error,
1184 * Create Socket structure.
1186 ret = snew(struct Socket_tag);
1187 ret->fn = &fn_table;
1190 bufchain_init(&ret->output_data);
1191 ret->connected = 0; /* to start with */
1192 ret->writable = 0; /* to start with */
1193 ret->sending_oob = 0;
1194 ret->outgoingeof = EOF_NO;
1196 ret->frozen_readable = 0;
1197 ret->localhost_only = 0; /* unused, but best init anyway */
1198 ret->pending_error = 0;
1199 ret->parent = ret->child = NULL;
1200 ret->oobinline = oobinline;
1201 ret->nodelay = nodelay;
1202 ret->keepalive = keepalive;
1203 ret->privport = privport;
1206 START_STEP(ret->addr, ret->step);
1207 ret->s = INVALID_SOCKET;
1211 err = try_connect(ret);
1212 } while (err && sk_nextaddr(ret->addr, &ret->step));
1214 return (Socket) ret;
1217 Socket sk_newlistener(const char *srcaddr, int port, Plug plug,
1218 int local_host_only, int orig_address_family)
1220 static const struct socket_function_table fn_table = {
1228 sk_tcp_socket_error,
1247 * Create Socket structure.
1249 ret = snew(struct Socket_tag);
1250 ret->fn = &fn_table;
1253 bufchain_init(&ret->output_data);
1254 ret->writable = 0; /* to start with */
1255 ret->sending_oob = 0;
1256 ret->outgoingeof = EOF_NO;
1258 ret->frozen_readable = 0;
1259 ret->localhost_only = local_host_only;
1260 ret->pending_error = 0;
1261 ret->parent = ret->child = NULL;
1265 * Translate address_family from platform-independent constants
1266 * into local reality.
1268 address_family = (orig_address_family == ADDRTYPE_IPV4 ? AF_INET :
1270 orig_address_family == ADDRTYPE_IPV6 ? AF_INET6 :
1275 * Our default, if passed the `don't care' value
1276 * ADDRTYPE_UNSPEC, is to listen on IPv4. If IPv6 is supported,
1277 * we will also set up a second socket listening on IPv6, but
1278 * the v4 one is primary since that ought to work even on
1279 * non-v6-supporting systems.
1281 if (address_family == AF_UNSPEC) address_family = AF_INET;
1286 s = p_socket(address_family, SOCK_STREAM, 0);
1289 if (s == INVALID_SOCKET) {
1290 err = p_WSAGetLastError();
1291 ret->error = winsock_error_string(err);
1292 return (Socket) ret;
1297 p_setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
1300 if (address_family == AF_INET6) {
1301 memset(&a6, 0, sizeof(a6));
1302 a6.sin6_family = AF_INET6;
1303 if (local_host_only)
1304 a6.sin6_addr = in6addr_loopback;
1306 a6.sin6_addr = in6addr_any;
1307 if (srcaddr != NULL && p_getaddrinfo) {
1308 struct addrinfo hints;
1309 struct addrinfo *ai;
1312 memset(&hints, 0, sizeof(hints));
1313 hints.ai_family = AF_INET6;
1316 /* strip [] on IPv6 address literals */
1317 char *trimmed_addr = host_strduptrim(srcaddr);
1318 err = p_getaddrinfo(trimmed_addr, NULL, &hints, &ai);
1319 sfree(trimmed_addr);
1321 if (err == 0 && ai->ai_family == AF_INET6) {
1323 ((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
1326 a6.sin6_port = p_htons(port);
1331 a.sin_family = AF_INET;
1334 * Bind to source address. First try an explicitly
1338 a.sin_addr.s_addr = p_inet_addr(srcaddr);
1339 if (a.sin_addr.s_addr != INADDR_NONE) {
1340 /* Override localhost_only with specified listen addr. */
1341 ret->localhost_only = ipv4_is_loopback(a.sin_addr);
1347 * ... and failing that, go with one of the standard ones.
1350 if (local_host_only)
1351 a.sin_addr.s_addr = p_htonl(INADDR_LOOPBACK);
1353 a.sin_addr.s_addr = p_htonl(INADDR_ANY);
1356 a.sin_port = p_htons((short)port);
1359 retcode = p_bind(s, (address_family == AF_INET6 ?
1360 (struct sockaddr *) &a6 :
1361 (struct sockaddr *) &a),
1363 AF_INET6 ? sizeof(a6) : sizeof(a)));
1365 retcode = p_bind(s, (struct sockaddr *) &a, sizeof(a));
1367 if (retcode != SOCKET_ERROR) {
1370 err = p_WSAGetLastError();
1375 ret->error = winsock_error_string(err);
1376 return (Socket) ret;
1380 if (p_listen(s, SOMAXCONN) == SOCKET_ERROR) {
1382 ret->error = winsock_error_string(p_WSAGetLastError());
1383 return (Socket) ret;
1386 /* Set up a select mechanism. This could be an AsyncSelect on a
1387 * window, or an EventSelect on an event object. */
1388 errstr = do_select(s, 1);
1391 ret->error = errstr;
1392 return (Socket) ret;
1395 add234(sktree, ret);
1399 * If we were given ADDRTYPE_UNSPEC, we must also create an
1400 * IPv6 listening socket and link it to this one.
1402 if (address_family == AF_INET && orig_address_family == ADDRTYPE_UNSPEC) {
1403 Actual_Socket other;
1405 other = (Actual_Socket) sk_newlistener(srcaddr, port, plug,
1406 local_host_only, ADDRTYPE_IPV6);
1409 if (!other->error) {
1410 other->parent = ret;
1419 return (Socket) ret;
1422 static void sk_tcp_close(Socket sock)
1424 extern char *do_select(SOCKET skt, int startup);
1425 Actual_Socket s = (Actual_Socket) sock;
1428 sk_tcp_close((Socket)s->child);
1432 p_closesocket(s->s);
1434 sk_addr_free(s->addr);
1439 * Deal with socket errors detected in try_send().
1441 static void socket_error_callback(void *vs)
1443 Actual_Socket s = (Actual_Socket)vs;
1446 * Just in case other socket work has caused this socket to vanish
1447 * or become somehow non-erroneous before this callback arrived...
1449 if (!find234(sktree, s, NULL) || !s->pending_error)
1453 * An error has occurred on this socket. Pass it to the plug.
1455 plug_closing(s->plug, winsock_error_string(s->pending_error),
1456 s->pending_error, 0);
1460 * The function which tries to send on a socket once it's deemed
1463 void try_send(Actual_Socket s)
1465 while (s->sending_oob || bufchain_size(&s->output_data) > 0) {
1469 int len, urgentflag;
1471 if (s->sending_oob) {
1472 urgentflag = MSG_OOB;
1473 len = s->sending_oob;
1477 bufchain_prefix(&s->output_data, &data, &len);
1479 nsent = p_send(s->s, data, len, urgentflag);
1480 noise_ultralight(nsent);
1482 err = (nsent < 0 ? p_WSAGetLastError() : 0);
1483 if ((err < WSABASEERR && nsent < 0) || err == WSAEWOULDBLOCK) {
1485 * Perfectly normal: we've sent all we can for the moment.
1487 * (Some WinSock send() implementations can return
1488 * <0 but leave no sensible error indication -
1489 * WSAGetLastError() is called but returns zero or
1490 * a small number - so we check that case and treat
1491 * it just like WSAEWOULDBLOCK.)
1493 s->writable = FALSE;
1495 } else if (nsent == 0 ||
1496 err == WSAECONNABORTED || err == WSAECONNRESET) {
1498 * If send() returns CONNABORTED or CONNRESET, we
1499 * unfortunately can't just call plug_closing(),
1500 * because it's quite likely that we're currently
1501 * _in_ a call from the code we'd be calling back
1502 * to, so we'd have to make half the SSH code
1503 * reentrant. Instead we flag a pending error on
1504 * the socket, to be dealt with (by calling
1505 * plug_closing()) at some suitable future moment.
1507 s->pending_error = err;
1508 queue_toplevel_callback(socket_error_callback, s);
1511 /* We're inside the Windows frontend here, so we know
1512 * that the frontend handle is unnecessary. */
1513 logevent(NULL, winsock_error_string(err));
1514 fatalbox("%s", winsock_error_string(err));
1517 if (s->sending_oob) {
1519 memmove(s->oobdata, s->oobdata+nsent, len-nsent);
1520 s->sending_oob = len - nsent;
1525 bufchain_consume(&s->output_data, nsent);
1531 * If we reach here, we've finished sending everything we might
1532 * have needed to send. Send EOF, if we need to.
1534 if (s->outgoingeof == EOF_PENDING) {
1535 p_shutdown(s->s, SD_SEND);
1536 s->outgoingeof = EOF_SENT;
1540 static int sk_tcp_write(Socket sock, const char *buf, int len)
1542 Actual_Socket s = (Actual_Socket) sock;
1544 assert(s->outgoingeof == EOF_NO);
1547 * Add the data to the buffer list on the socket.
1549 bufchain_add(&s->output_data, buf, len);
1552 * Now try sending from the start of the buffer list.
1557 return bufchain_size(&s->output_data);
1560 static int sk_tcp_write_oob(Socket sock, const char *buf, int len)
1562 Actual_Socket s = (Actual_Socket) sock;
1564 assert(s->outgoingeof == EOF_NO);
1567 * Replace the buffer list on the socket with the data.
1569 bufchain_clear(&s->output_data);
1570 assert(len <= sizeof(s->oobdata));
1571 memcpy(s->oobdata, buf, len);
1572 s->sending_oob = len;
1575 * Now try sending from the start of the buffer list.
1580 return s->sending_oob;
1583 static void sk_tcp_write_eof(Socket sock)
1585 Actual_Socket s = (Actual_Socket) sock;
1587 assert(s->outgoingeof == EOF_NO);
1590 * Mark the socket as pending outgoing EOF.
1592 s->outgoingeof = EOF_PENDING;
1595 * Now try sending from the start of the buffer list.
1601 int select_result(WPARAM wParam, LPARAM lParam)
1605 char buf[20480]; /* nice big buffer for plenty of speed */
1609 /* wParam is the socket itself */
1612 return 1; /* boggle */
1614 s = find234(sktree, (void *) wParam, cmpforsearch);
1616 return 1; /* boggle */
1618 if ((err = WSAGETSELECTERROR(lParam)) != 0) {
1620 * An error has occurred on this socket. Pass it to the
1624 struct SockAddr_tag thisaddr = sk_extractaddr_tmp(
1626 plug_log(s->plug, 1, &thisaddr, s->port,
1627 winsock_error_string(err), err);
1628 while (err && s->addr && sk_nextaddr(s->addr, &s->step)) {
1629 err = try_connect(s);
1633 return plug_closing(s->plug, winsock_error_string(err), err, 0);
1638 noise_ultralight(lParam);
1640 switch (WSAGETSELECTEVENT(lParam)) {
1642 s->connected = s->writable = 1;
1644 * Once a socket is connected, we can stop falling
1645 * back through the candidate addresses to connect
1649 sk_addr_free(s->addr);
1654 /* In the case the socket is still frozen, we don't even bother */
1656 s->frozen_readable = 1;
1661 * We have received data on the socket. For an oobinline
1662 * socket, this might be data _before_ an urgent pointer,
1663 * in which case we send it to the back end with type==1
1664 * (data prior to urgent).
1668 p_ioctlsocket(s->s, SIOCATMARK, &atmark);
1670 * Avoid checking the return value from ioctlsocket(),
1671 * on the grounds that some WinSock wrappers don't
1672 * support it. If it does nothing, we get atmark==1,
1673 * which is equivalent to `no OOB pending', so the
1674 * effect will be to non-OOB-ify any OOB data.
1679 ret = p_recv(s->s, buf, sizeof(buf), 0);
1680 noise_ultralight(ret);
1682 err = p_WSAGetLastError();
1683 if (err == WSAEWOULDBLOCK) {
1688 return plug_closing(s->plug, winsock_error_string(err), err,
1690 } else if (0 == ret) {
1691 return plug_closing(s->plug, NULL, 0, 0);
1693 return plug_receive(s->plug, atmark ? 0 : 1, buf, ret);
1698 * This will only happen on a non-oobinline socket. It
1699 * indicates that we can immediately perform an OOB read
1700 * and get back OOB data, which we will send to the back
1701 * end with type==2 (urgent data).
1703 ret = p_recv(s->s, buf, sizeof(buf), MSG_OOB);
1704 noise_ultralight(ret);
1706 const char *str = (ret == 0 ? "Internal networking trouble" :
1707 winsock_error_string(p_WSAGetLastError()));
1708 /* We're inside the Windows frontend here, so we know
1709 * that the frontend handle is unnecessary. */
1710 logevent(NULL, str);
1711 fatalbox("%s", str);
1713 return plug_receive(s->plug, 2, buf, ret);
1718 int bufsize_before, bufsize_after;
1720 bufsize_before = s->sending_oob + bufchain_size(&s->output_data);
1722 bufsize_after = s->sending_oob + bufchain_size(&s->output_data);
1723 if (bufsize_after < bufsize_before)
1724 plug_sent(s->plug, bufsize_after);
1728 /* Signal a close on the socket. First read any outstanding data. */
1731 ret = p_recv(s->s, buf, sizeof(buf), 0);
1733 err = p_WSAGetLastError();
1734 if (err == WSAEWOULDBLOCK)
1736 return plug_closing(s->plug, winsock_error_string(err),
1740 open &= plug_receive(s->plug, 0, buf, ret);
1742 open &= plug_closing(s->plug, NULL, 0, 0);
1749 struct sockaddr_in isa;
1751 struct sockaddr_storage isa;
1753 int addrlen = sizeof(isa);
1754 SOCKET t; /* socket of connection */
1757 memset(&isa, 0, sizeof(isa));
1759 t = p_accept(s->s,(struct sockaddr *)&isa,&addrlen);
1760 if (t == INVALID_SOCKET)
1762 err = p_WSAGetLastError();
1763 if (err == WSATRY_AGAIN)
1770 if (isa.ss_family == AF_INET &&
1771 s->localhost_only &&
1772 !ipv4_is_local_addr(((struct sockaddr_in *)&isa)->sin_addr))
1774 if (s->localhost_only && !ipv4_is_local_addr(isa.sin_addr))
1777 p_closesocket(t); /* dodgy WinSock let nonlocal through */
1778 } else if (plug_accepting(s->plug, sk_tcp_accept, actx)) {
1779 p_closesocket(t); /* denied or error */
1788 * Special error values are returned from sk_namelookup and sk_new
1789 * if there's a problem. These functions extract an error message,
1790 * or return NULL if there's no problem.
1792 const char *sk_addr_error(SockAddr addr)
1796 static const char *sk_tcp_socket_error(Socket sock)
1798 Actual_Socket s = (Actual_Socket) sock;
1802 static char *sk_tcp_peer_info(Socket sock)
1804 Actual_Socket s = (Actual_Socket) sock;
1806 struct sockaddr_in addr;
1808 struct sockaddr_storage addr;
1809 char buf[INET6_ADDRSTRLEN];
1811 int addrlen = sizeof(addr);
1813 if (p_getpeername(s->s, (struct sockaddr *)&addr, &addrlen) < 0)
1816 if (((struct sockaddr *)&addr)->sa_family == AF_INET) {
1819 p_inet_ntoa(((struct sockaddr_in *)&addr)->sin_addr),
1820 (int)p_ntohs(((struct sockaddr_in *)&addr)->sin_port));
1822 } else if (((struct sockaddr *)&addr)->sa_family == AF_INET6) {
1825 p_inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&addr)->sin6_addr,
1827 (int)p_ntohs(((struct sockaddr_in6 *)&addr)->sin6_port));
1834 static void sk_tcp_set_frozen(Socket sock, int is_frozen)
1836 Actual_Socket s = (Actual_Socket) sock;
1837 if (s->frozen == is_frozen)
1839 s->frozen = is_frozen;
1842 if (s->frozen_readable) {
1844 p_recv(s->s, &c, 1, MSG_PEEK);
1847 s->frozen_readable = 0;
1850 void socket_reselect_all(void)
1855 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
1862 * For Plink: enumerate all sockets currently active.
1864 SOCKET first_socket(int *state)
1868 s = index234(sktree, (*state)++);
1869 return s ? s->s : INVALID_SOCKET;
1872 SOCKET next_socket(int *state)
1874 Actual_Socket s = index234(sktree, (*state)++);
1875 return s ? s->s : INVALID_SOCKET;
1878 extern int socket_writable(SOCKET skt)
1880 Actual_Socket s = find234(sktree, (void *)skt, cmpforsearch);
1883 return bufchain_size(&s->output_data) > 0;
1888 int net_service_lookup(char *service)
1891 se = p_getservbyname(service, NULL);
1893 return p_ntohs(se->s_port);
1898 char *get_hostname(void)
1901 char *hostname = NULL;
1904 hostname = sresize(hostname, len, char);
1905 if (p_gethostname(hostname, len) < 0) {
1910 } while (strlen(hostname) >= (size_t)(len-1));
1914 SockAddr platform_get_x11_unix_address(const char *display, int displaynum,
1915 char **canonicalname)
1917 SockAddr ret = snew(struct SockAddr_tag);
1918 memset(ret, 0, sizeof(struct SockAddr_tag));
1919 ret->error = "unix sockets not supported on this platform";