2 * Windows networking abstraction.
4 * For the IPv6 code in here I am indebted to Jeroen Massar and
8 #include <winsock2.h> /* need to put this first, for winelib builds */
14 #define DEFINE_PLUG_METHOD_MACROS
15 #define NEED_DECLARATION_OF_SELECT /* in order to initialise it */
25 #pragma clang diagnostic push
26 #pragma clang diagnostic ignored "-Wmissing-braces"
28 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
29 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
31 #pragma clang diagnostic pop
35 #define ipv4_is_loopback(addr) \
36 ((p_ntohl(addr.s_addr) & 0xFF000000L) == 0x7F000000L)
39 * We used to typedef struct Socket_tag *Socket.
41 * Since we have made the networking abstraction slightly more
42 * abstract, Socket no longer means a tcp socket (it could mean
43 * an ssl socket). So now we must use Actual_Socket when we know
44 * we are talking about a tcp socket.
46 typedef struct Socket_tag *Actual_Socket;
49 * Mutable state that goes with a SockAddr: stores information
50 * about where in the list of candidate IP(v*) addresses we've
53 typedef struct SockAddrStep_tag SockAddrStep;
54 struct SockAddrStep_tag {
56 struct addrinfo *ai; /* steps along addr->ais */
62 const struct socket_function_table *fn;
63 /* the above variable absolutely *must* be the first in this structure */
70 int frozen; /* this causes readability notifications to be ignored */
71 int frozen_readable; /* this means we missed at least one readability
72 * notification while we were frozen */
73 int localhost_only; /* for listening sockets */
76 int oobinline, nodelay, keepalive, privport;
77 enum { EOF_NO, EOF_PENDING, EOF_SENT } outgoingeof;
81 int pending_error; /* in case send() returns error */
83 * We sometimes need pairs of Socket structures to be linked:
84 * if we are listening on the same IPv6 and v4 port, for
85 * example. So here we define `parent' and `child' pointers to
88 Actual_Socket parent, child;
95 int namedpipe; /* indicates that this SockAddr is phony, holding a Windows
96 * named pipe pathname instead of a network address */
98 struct addrinfo *ais; /* Addresses IPv6 style. */
100 unsigned long *addresses; /* Addresses IPv4 style. */
102 char hostname[512]; /* Store an unresolved host name. */
106 * Which address family this address belongs to. AF_INET for IPv4;
107 * AF_INET6 for IPv6; AF_UNSPEC indicates that name resolution has
108 * not been done and a simple host name is held in this SockAddr
112 #define SOCKADDR_FAMILY(addr, step) \
113 (!(addr)->resolved ? AF_UNSPEC : \
114 (step).ai ? (step).ai->ai_family : AF_INET)
116 #define SOCKADDR_FAMILY(addr, step) \
117 (!(addr)->resolved ? AF_UNSPEC : AF_INET)
121 * Start a SockAddrStep structure to step through multiple
125 #define START_STEP(addr, step) \
126 ((step).ai = (addr)->ais, (step).curraddr = 0)
128 #define START_STEP(addr, step) \
129 ((step).curraddr = 0)
132 static tree234 *sktree;
134 static int cmpfortree(void *av, void *bv)
136 Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv;
137 unsigned long as = (unsigned long) a->s, bs = (unsigned long) b->s;
149 static int cmpforsearch(void *av, void *bv)
151 Actual_Socket b = (Actual_Socket) bv;
152 uintptr_t as = (uintptr_t) av, bs = (uintptr_t) b->s;
160 DECL_WINDOWS_FUNCTION(static, int, WSAStartup, (WORD, LPWSADATA));
161 DECL_WINDOWS_FUNCTION(static, int, WSACleanup, (void));
162 DECL_WINDOWS_FUNCTION(static, int, closesocket, (SOCKET));
163 DECL_WINDOWS_FUNCTION(static, u_long, ntohl, (u_long));
164 DECL_WINDOWS_FUNCTION(static, u_long, htonl, (u_long));
165 DECL_WINDOWS_FUNCTION(static, u_short, htons, (u_short));
166 DECL_WINDOWS_FUNCTION(static, u_short, ntohs, (u_short));
167 DECL_WINDOWS_FUNCTION(static, int, gethostname, (char *, int));
168 DECL_WINDOWS_FUNCTION(static, struct hostent FAR *, gethostbyname,
170 DECL_WINDOWS_FUNCTION(static, struct servent FAR *, getservbyname,
171 (const char FAR *, const char FAR *));
172 DECL_WINDOWS_FUNCTION(static, unsigned long, inet_addr, (const char FAR *));
173 DECL_WINDOWS_FUNCTION(static, char FAR *, inet_ntoa, (struct in_addr));
174 DECL_WINDOWS_FUNCTION(static, const char FAR *, inet_ntop,
175 (int, void FAR *, char *, size_t));
176 DECL_WINDOWS_FUNCTION(static, int, connect,
177 (SOCKET, const struct sockaddr FAR *, int));
178 DECL_WINDOWS_FUNCTION(static, int, bind,
179 (SOCKET, const struct sockaddr FAR *, int));
180 DECL_WINDOWS_FUNCTION(static, int, setsockopt,
181 (SOCKET, int, int, const char FAR *, int));
182 DECL_WINDOWS_FUNCTION(static, SOCKET, socket, (int, int, int));
183 DECL_WINDOWS_FUNCTION(static, int, listen, (SOCKET, int));
184 DECL_WINDOWS_FUNCTION(static, int, send, (SOCKET, const char FAR *, int, int));
185 DECL_WINDOWS_FUNCTION(static, int, shutdown, (SOCKET, int));
186 DECL_WINDOWS_FUNCTION(static, int, ioctlsocket,
187 (SOCKET, long, u_long FAR *));
188 DECL_WINDOWS_FUNCTION(static, SOCKET, accept,
189 (SOCKET, struct sockaddr FAR *, int FAR *));
190 DECL_WINDOWS_FUNCTION(static, int, getpeername,
191 (SOCKET, struct sockaddr FAR *, int FAR *));
192 DECL_WINDOWS_FUNCTION(static, int, recv, (SOCKET, char FAR *, int, int));
193 DECL_WINDOWS_FUNCTION(static, int, WSAIoctl,
194 (SOCKET, DWORD, LPVOID, DWORD, LPVOID, DWORD,
195 LPDWORD, LPWSAOVERLAPPED,
196 LPWSAOVERLAPPED_COMPLETION_ROUTINE));
198 DECL_WINDOWS_FUNCTION(static, int, getaddrinfo,
199 (const char *nodename, const char *servname,
200 const struct addrinfo *hints, struct addrinfo **res));
201 DECL_WINDOWS_FUNCTION(static, void, freeaddrinfo, (struct addrinfo *res));
202 DECL_WINDOWS_FUNCTION(static, int, getnameinfo,
203 (const struct sockaddr FAR * sa, socklen_t salen,
204 char FAR * host, size_t hostlen, char FAR * serv,
205 size_t servlen, int flags));
206 DECL_WINDOWS_FUNCTION(static, char *, gai_strerror, (int ecode));
207 DECL_WINDOWS_FUNCTION(static, int, WSAAddressToStringA,
208 (LPSOCKADDR, DWORD, LPWSAPROTOCOL_INFO,
212 static HMODULE winsock_module = NULL;
213 static WSADATA wsadata;
215 static HMODULE winsock2_module = NULL;
216 static HMODULE wship6_module = NULL;
219 int sk_startup(int hi, int lo)
223 winsock_ver = MAKEWORD(hi, lo);
225 if (p_WSAStartup(winsock_ver, &wsadata)) {
229 if (LOBYTE(wsadata.wVersion) != LOBYTE(winsock_ver)) {
233 #ifdef NET_SETUP_DIAGNOSTICS
236 sprintf(buf, "Using WinSock %d.%d", hi, lo);
243 /* Actually define this function pointer, which won't have been
244 * defined alongside all the others by PUTTY_DO_GLOBALS because of the
245 * annoying winelib header-ordering issue. (See comment in winstuff.h.) */
246 DECL_WINDOWS_FUNCTION(/* empty */, int, select,
247 (int, fd_set FAR *, fd_set FAR *,
248 fd_set FAR *, const struct timeval FAR *));
255 winsock_module = load_system32_dll("ws2_32.dll");
256 if (!winsock_module) {
257 winsock_module = load_system32_dll("wsock32.dll");
260 fatalbox("Unable to load any WinSock library");
263 /* Check if we have getaddrinfo in Winsock */
264 if (GetProcAddress(winsock_module, "getaddrinfo") != NULL) {
265 #ifdef NET_SETUP_DIAGNOSTICS
266 logevent(NULL, "Native WinSock IPv6 support detected");
268 GET_WINDOWS_FUNCTION(winsock_module, getaddrinfo);
269 GET_WINDOWS_FUNCTION(winsock_module, freeaddrinfo);
270 GET_WINDOWS_FUNCTION(winsock_module, getnameinfo);
271 GET_WINDOWS_FUNCTION(winsock_module, gai_strerror);
273 /* Fall back to wship6.dll for Windows 2000 */
274 wship6_module = load_system32_dll("wship6.dll");
276 #ifdef NET_SETUP_DIAGNOSTICS
277 logevent(NULL, "WSH IPv6 support detected");
279 GET_WINDOWS_FUNCTION(wship6_module, getaddrinfo);
280 GET_WINDOWS_FUNCTION(wship6_module, freeaddrinfo);
281 GET_WINDOWS_FUNCTION(wship6_module, getnameinfo);
282 GET_WINDOWS_FUNCTION(wship6_module, gai_strerror);
284 #ifdef NET_SETUP_DIAGNOSTICS
285 logevent(NULL, "No IPv6 support detected");
289 GET_WINDOWS_FUNCTION(winsock2_module, WSAAddressToStringA);
291 #ifdef NET_SETUP_DIAGNOSTICS
292 logevent(NULL, "PuTTY was built without IPv6 support");
296 GET_WINDOWS_FUNCTION(winsock_module, WSAAsyncSelect);
297 GET_WINDOWS_FUNCTION(winsock_module, WSAEventSelect);
298 GET_WINDOWS_FUNCTION(winsock_module, select);
299 GET_WINDOWS_FUNCTION(winsock_module, WSAGetLastError);
300 GET_WINDOWS_FUNCTION(winsock_module, WSAEnumNetworkEvents);
301 GET_WINDOWS_FUNCTION(winsock_module, WSAStartup);
302 GET_WINDOWS_FUNCTION(winsock_module, WSACleanup);
303 GET_WINDOWS_FUNCTION(winsock_module, closesocket);
304 GET_WINDOWS_FUNCTION(winsock_module, ntohl);
305 GET_WINDOWS_FUNCTION(winsock_module, htonl);
306 GET_WINDOWS_FUNCTION(winsock_module, htons);
307 GET_WINDOWS_FUNCTION(winsock_module, ntohs);
308 GET_WINDOWS_FUNCTION(winsock_module, gethostname);
309 GET_WINDOWS_FUNCTION(winsock_module, gethostbyname);
310 GET_WINDOWS_FUNCTION(winsock_module, getservbyname);
311 GET_WINDOWS_FUNCTION(winsock_module, inet_addr);
312 GET_WINDOWS_FUNCTION(winsock_module, inet_ntoa);
313 GET_WINDOWS_FUNCTION(winsock_module, inet_ntop);
314 GET_WINDOWS_FUNCTION(winsock_module, connect);
315 GET_WINDOWS_FUNCTION(winsock_module, bind);
316 GET_WINDOWS_FUNCTION(winsock_module, setsockopt);
317 GET_WINDOWS_FUNCTION(winsock_module, socket);
318 GET_WINDOWS_FUNCTION(winsock_module, listen);
319 GET_WINDOWS_FUNCTION(winsock_module, send);
320 GET_WINDOWS_FUNCTION(winsock_module, shutdown);
321 GET_WINDOWS_FUNCTION(winsock_module, ioctlsocket);
322 GET_WINDOWS_FUNCTION(winsock_module, accept);
323 GET_WINDOWS_FUNCTION(winsock_module, getpeername);
324 GET_WINDOWS_FUNCTION(winsock_module, recv);
325 GET_WINDOWS_FUNCTION(winsock_module, WSAIoctl);
327 /* Try to get the best WinSock version we can get */
328 if (!sk_startup(2,2) &&
331 fatalbox("Unable to initialise WinSock");
334 sktree = newtree234(cmpfortree);
337 void sk_cleanup(void)
343 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
353 FreeLibrary(winsock_module);
356 FreeLibrary(wship6_module);
365 static int errstring_find(void *av, void *bv)
368 struct errstring *b = (struct errstring *)bv;
375 static int errstring_compare(void *av, void *bv)
377 struct errstring *a = (struct errstring *)av;
378 return errstring_find(&a->error, bv);
381 static tree234 *errstrings = NULL;
383 const char *winsock_error_string(int error)
385 const char prefix[] = "Network error: ";
386 struct errstring *es;
389 * Error codes we know about and have historically had reasonably
390 * sensible error messages for.
394 return "Network error: Permission denied";
396 return "Network error: Address already in use";
397 case WSAEADDRNOTAVAIL:
398 return "Network error: Cannot assign requested address";
399 case WSAEAFNOSUPPORT:
401 "Network error: Address family not supported by protocol family";
403 return "Network error: Operation already in progress";
404 case WSAECONNABORTED:
405 return "Network error: Software caused connection abort";
406 case WSAECONNREFUSED:
407 return "Network error: Connection refused";
409 return "Network error: Connection reset by peer";
410 case WSAEDESTADDRREQ:
411 return "Network error: Destination address required";
413 return "Network error: Bad address";
415 return "Network error: Host is down";
416 case WSAEHOSTUNREACH:
417 return "Network error: No route to host";
419 return "Network error: Operation now in progress";
421 return "Network error: Interrupted function call";
423 return "Network error: Invalid argument";
425 return "Network error: Socket is already connected";
427 return "Network error: Too many open files";
429 return "Network error: Message too long";
431 return "Network error: Network is down";
433 return "Network error: Network dropped connection on reset";
435 return "Network error: Network is unreachable";
437 return "Network error: No buffer space available";
439 return "Network error: Bad protocol option";
441 return "Network error: Socket is not connected";
443 return "Network error: Socket operation on non-socket";
445 return "Network error: Operation not supported";
446 case WSAEPFNOSUPPORT:
447 return "Network error: Protocol family not supported";
449 return "Network error: Too many processes";
450 case WSAEPROTONOSUPPORT:
451 return "Network error: Protocol not supported";
453 return "Network error: Protocol wrong type for socket";
455 return "Network error: Cannot send after socket shutdown";
456 case WSAESOCKTNOSUPPORT:
457 return "Network error: Socket type not supported";
459 return "Network error: Connection timed out";
461 return "Network error: Resource temporarily unavailable";
463 return "Network error: Graceful shutdown in progress";
467 * Generic code to handle any other error.
469 * Slightly nasty hack here: we want to return a static string
470 * which the caller will never have to worry about freeing, but on
471 * the other hand if we call FormatMessage to get it then it will
472 * want to either allocate a buffer or write into one we own.
474 * So what we do is to maintain a tree234 of error strings we've
475 * already used. New ones are allocated from the heap, but then
476 * put in this tree and kept forever.
480 errstrings = newtree234(errstring_compare);
482 es = find234(errstrings, &error, errstring_find);
485 int bufsize, bufused;
487 es = snew(struct errstring);
489 /* maximum size for FormatMessage is 64K */
490 bufsize = 65535 + sizeof(prefix);
491 es->text = snewn(bufsize, char);
492 strcpy(es->text, prefix);
493 bufused = strlen(es->text);
494 if (!FormatMessage((FORMAT_MESSAGE_FROM_SYSTEM |
495 FORMAT_MESSAGE_IGNORE_INSERTS), NULL, error,
496 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
497 es->text + bufused, bufsize - bufused, NULL)) {
498 sprintf(es->text + bufused,
499 "Windows error code %d (and FormatMessage returned %u)",
500 error, (unsigned int)GetLastError());
502 int len = strlen(es->text);
503 if (len > 0 && es->text[len-1] == '\n')
504 es->text[len-1] = '\0';
506 es->text = sresize(es->text, strlen(es->text) + 1, char);
507 add234(errstrings, es);
513 SockAddr sk_namelookup(const char *host, char **canonicalname,
516 SockAddr ret = snew(struct SockAddr_tag);
521 /* Default to IPv4. */
522 hint_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
524 address_family == ADDRTYPE_IPV6 ? AF_INET6 :
528 /* Clear the structure and default to IPv4. */
529 memset(ret, 0, sizeof(struct SockAddr_tag));
533 ret->namedpipe = FALSE;
534 ret->addresses = NULL;
535 ret->resolved = FALSE;
539 if ((a = p_inet_addr(host)) == (unsigned long) INADDR_NONE) {
540 struct hostent *h = NULL;
544 * Use getaddrinfo when it's available
547 struct addrinfo hints;
548 #ifdef NET_SETUP_DIAGNOSTICS
549 logevent(NULL, "Using getaddrinfo() for resolving");
551 memset(&hints, 0, sizeof(hints));
552 hints.ai_family = hint_family;
553 hints.ai_flags = AI_CANONNAME;
555 /* strip [] on IPv6 address literals */
556 char *trimmed_host = host_strduptrim(host);
557 err = p_getaddrinfo(trimmed_host, NULL, &hints, &ret->ais);
561 ret->resolved = TRUE;
565 #ifdef NET_SETUP_DIAGNOSTICS
566 logevent(NULL, "Using gethostbyname() for resolving");
569 * Otherwise use the IPv4-only gethostbyname...
570 * (NOTE: we don't use gethostbyname as a fallback!)
572 if ( (h = p_gethostbyname(host)) )
573 ret->resolved = TRUE;
575 err = p_WSAGetLastError();
578 if (!ret->resolved) {
579 ret->error = (err == WSAENETDOWN ? "Network is down" :
580 err == WSAHOST_NOT_FOUND ? "Host does not exist" :
581 err == WSATRY_AGAIN ? "Host not found" :
583 p_getaddrinfo&&p_gai_strerror ? p_gai_strerror(err) :
585 "gethostbyname: unknown error");
590 /* If we got an address info use that... */
592 /* Are we in IPv4 fallback mode? */
593 /* We put the IPv4 address into the a variable so we can further-on use the IPv4 code... */
594 if (ret->ais->ai_family == AF_INET)
596 (char *) &((SOCKADDR_IN *) ret->ais->
597 ai_addr)->sin_addr, sizeof(a));
599 if (ret->ais->ai_canonname)
600 strncpy(realhost, ret->ais->ai_canonname, lenof(realhost));
602 strncpy(realhost, host, lenof(realhost));
604 /* We used the IPv4-only gethostbyname()... */
609 for (n = 0; h->h_addr_list[n]; n++);
610 ret->addresses = snewn(n, unsigned long);
612 for (n = 0; n < ret->naddresses; n++) {
613 memcpy(&a, h->h_addr_list[n], sizeof(a));
614 ret->addresses[n] = p_ntohl(a);
616 memcpy(&a, h->h_addr, sizeof(a));
617 /* This way we are always sure the h->h_name is valid :) */
618 strncpy(realhost, h->h_name, sizeof(realhost));
623 * This must be a numeric IPv4 address because it caused a
624 * success return from inet_addr.
626 ret->addresses = snewn(1, unsigned long);
628 ret->addresses[0] = p_ntohl(a);
629 ret->resolved = TRUE;
630 strncpy(realhost, host, sizeof(realhost));
632 realhost[lenof(realhost)-1] = '\0';
633 *canonicalname = snewn(1+strlen(realhost), char);
634 strcpy(*canonicalname, realhost);
638 SockAddr sk_nonamelookup(const char *host)
640 SockAddr ret = snew(struct SockAddr_tag);
642 ret->resolved = FALSE;
646 ret->namedpipe = FALSE;
647 ret->addresses = NULL;
650 strncpy(ret->hostname, host, lenof(ret->hostname));
651 ret->hostname[lenof(ret->hostname)-1] = '\0';
655 SockAddr sk_namedpipe_addr(const char *pipename)
657 SockAddr ret = snew(struct SockAddr_tag);
659 ret->resolved = FALSE;
663 ret->namedpipe = TRUE;
664 ret->addresses = NULL;
667 strncpy(ret->hostname, pipename, lenof(ret->hostname));
668 ret->hostname[lenof(ret->hostname)-1] = '\0';
672 int sk_nextaddr(SockAddr addr, SockAddrStep *step)
676 if (step->ai->ai_next) {
677 step->ai = step->ai->ai_next;
683 if (step->curraddr+1 < addr->naddresses) {
691 void sk_getaddr(SockAddr addr, char *buf, int buflen)
694 START_STEP(addr, step);
699 if (p_WSAAddressToStringA) {
700 DWORD dwbuflen = buflen;
701 err = p_WSAAddressToStringA(step.ai->ai_addr, step.ai->ai_addrlen,
702 NULL, buf, &dwbuflen);
706 strncpy(buf, addr->hostname, buflen);
708 strncpy(buf, "<unknown>", buflen);
709 buf[buflen-1] = '\0';
713 if (SOCKADDR_FAMILY(addr, step) == AF_INET) {
715 assert(addr->addresses && step.curraddr < addr->naddresses);
716 a.s_addr = p_htonl(addr->addresses[step.curraddr]);
717 strncpy(buf, p_inet_ntoa(a), buflen);
718 buf[buflen-1] = '\0';
720 strncpy(buf, addr->hostname, buflen);
721 buf[buflen-1] = '\0';
726 * This constructs a SockAddr that points at one specific sub-address
727 * of a parent SockAddr. The returned SockAddr does not own all its
728 * own memory: it points into the old one's data structures, so it
729 * MUST NOT be used after the old one is freed, and it MUST NOT be
730 * passed to sk_addr_free. (The latter is why it's returned by value
731 * rather than dynamically allocated - that should clue in anyone
732 * writing a call to it that something is weird about it.)
734 static struct SockAddr_tag sk_extractaddr_tmp(
735 SockAddr addr, const SockAddrStep *step)
737 struct SockAddr_tag toret;
738 toret = *addr; /* structure copy */
742 toret.ais = step->ai;
744 if (SOCKADDR_FAMILY(addr, *step) == AF_INET
749 toret.addresses += step->curraddr;
754 int sk_addr_needs_port(SockAddr addr)
756 return addr->namedpipe ? FALSE : TRUE;
759 int sk_hostname_is_local(const char *name)
761 return !strcmp(name, "localhost") ||
762 !strcmp(name, "::1") ||
763 !strncmp(name, "127.", 4);
766 static INTERFACE_INFO local_interfaces[16];
767 static int n_local_interfaces; /* 0=not yet, -1=failed, >0=number */
769 static int ipv4_is_local_addr(struct in_addr addr)
771 if (ipv4_is_loopback(addr))
772 return 1; /* loopback addresses are local */
773 if (!n_local_interfaces) {
774 SOCKET s = p_socket(AF_INET, SOCK_DGRAM, 0);
778 p_WSAIoctl(s, SIO_GET_INTERFACE_LIST, NULL, 0,
779 local_interfaces, sizeof(local_interfaces),
780 &retbytes, NULL, NULL) == 0)
781 n_local_interfaces = retbytes / sizeof(INTERFACE_INFO);
783 logevent(NULL, "Unable to get list of local IP addresses");
785 if (n_local_interfaces > 0) {
787 for (i = 0; i < n_local_interfaces; i++) {
788 SOCKADDR_IN *address =
789 (SOCKADDR_IN *)&local_interfaces[i].iiAddress;
790 if (address->sin_addr.s_addr == addr.s_addr)
791 return 1; /* this address is local */
794 return 0; /* this address is not local */
797 int sk_address_is_local(SockAddr addr)
801 START_STEP(addr, step);
802 family = SOCKADDR_FAMILY(addr, step);
805 if (family == AF_INET6) {
806 return IN6_IS_ADDR_LOOPBACK(&((const struct sockaddr_in6 *)step.ai->ai_addr)->sin6_addr);
809 if (family == AF_INET) {
812 return ipv4_is_local_addr(((struct sockaddr_in *)step.ai->ai_addr)
818 assert(addr->addresses && step.curraddr < addr->naddresses);
819 a.s_addr = p_htonl(addr->addresses[step.curraddr]);
820 return ipv4_is_local_addr(a);
823 assert(family == AF_UNSPEC);
824 return 0; /* we don't know; assume not */
828 int sk_address_is_special_local(SockAddr addr)
830 return 0; /* no Unix-domain socket analogue here */
833 int sk_addrtype(SockAddr addr)
837 START_STEP(addr, step);
838 family = SOCKADDR_FAMILY(addr, step);
840 return (family == AF_INET ? ADDRTYPE_IPV4 :
842 family == AF_INET6 ? ADDRTYPE_IPV6 :
847 void sk_addrcopy(SockAddr addr, char *buf)
851 START_STEP(addr, step);
852 family = SOCKADDR_FAMILY(addr, step);
854 assert(family != AF_UNSPEC);
857 if (family == AF_INET)
858 memcpy(buf, &((struct sockaddr_in *)step.ai->ai_addr)->sin_addr,
859 sizeof(struct in_addr));
860 else if (family == AF_INET6)
861 memcpy(buf, &((struct sockaddr_in6 *)step.ai->ai_addr)->sin6_addr,
862 sizeof(struct in6_addr));
867 if (family == AF_INET) {
869 assert(addr->addresses && step.curraddr < addr->naddresses);
870 a.s_addr = p_htonl(addr->addresses[step.curraddr]);
871 memcpy(buf, (char*) &a.s_addr, 4);
875 void sk_addr_free(SockAddr addr)
877 if (--addr->refcount > 0)
880 if (addr->ais && p_freeaddrinfo)
881 p_freeaddrinfo(addr->ais);
884 sfree(addr->addresses);
888 SockAddr sk_addr_dup(SockAddr addr)
894 static Plug sk_tcp_plug(Socket sock, Plug p)
896 Actual_Socket s = (Actual_Socket) sock;
903 static void sk_tcp_flush(Socket s)
906 * We send data to the socket as soon as we can anyway,
907 * so we don't need to do anything here. :-)
911 static void sk_tcp_close(Socket s);
912 static int sk_tcp_write(Socket s, const char *data, int len);
913 static int sk_tcp_write_oob(Socket s, const char *data, int len);
914 static void sk_tcp_write_eof(Socket s);
915 static void sk_tcp_set_frozen(Socket s, int is_frozen);
916 static const char *sk_tcp_socket_error(Socket s);
917 static char *sk_tcp_peer_info(Socket s);
919 extern char *do_select(SOCKET skt, int startup);
921 static Socket sk_tcp_accept(accept_ctx_t ctx, Plug plug)
923 static const struct socket_function_table fn_table = {
940 * Create Socket structure.
942 ret = snew(struct Socket_tag);
946 bufchain_init(&ret->output_data);
947 ret->writable = 1; /* to start with */
948 ret->sending_oob = 0;
949 ret->outgoingeof = EOF_NO;
951 ret->frozen_readable = 0;
952 ret->localhost_only = 0; /* unused, but best init anyway */
953 ret->pending_error = 0;
954 ret->parent = ret->child = NULL;
957 ret->s = (SOCKET)ctx.p;
959 if (ret->s == INVALID_SOCKET) {
960 err = p_WSAGetLastError();
961 ret->error = winsock_error_string(err);
967 /* Set up a select mechanism. This could be an AsyncSelect on a
968 * window, or an EventSelect on an event object. */
969 errstr = do_select(ret->s, 1);
980 static DWORD try_connect(Actual_Socket sock)
992 if (sock->s != INVALID_SOCKET) {
993 do_select(sock->s, 0);
994 p_closesocket(sock->s);
998 struct SockAddr_tag thisaddr = sk_extractaddr_tmp(
999 sock->addr, &sock->step);
1000 plug_log(sock->plug, 0, &thisaddr, sock->port, NULL, 0);
1006 family = SOCKADDR_FAMILY(sock->addr, sock->step);
1009 * Remove the socket from the tree before we overwrite its
1010 * internal socket id, because that forms part of the tree's
1011 * sorting criterion. We'll add it back before exiting this
1012 * function, whether we changed anything or not.
1014 del234(sktree, sock);
1016 s = p_socket(family, SOCK_STREAM, 0);
1019 if (s == INVALID_SOCKET) {
1020 err = p_WSAGetLastError();
1021 sock->error = winsock_error_string(err);
1025 if (sock->oobinline) {
1027 p_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));
1030 if (sock->nodelay) {
1032 p_setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *) &b, sizeof(b));
1035 if (sock->keepalive) {
1037 p_setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *) &b, sizeof(b));
1041 * Bind to local address.
1044 localport = 1023; /* count from 1023 downwards */
1046 localport = 0; /* just use port 0 (ie winsock picks) */
1048 /* Loop round trying to bind */
1053 if (family == AF_INET6) {
1054 memset(&a6, 0, sizeof(a6));
1055 a6.sin6_family = AF_INET6;
1056 /*a6.sin6_addr = in6addr_any; */ /* == 0 done by memset() */
1057 a6.sin6_port = p_htons(localport);
1061 a.sin_family = AF_INET;
1062 a.sin_addr.s_addr = p_htonl(INADDR_ANY);
1063 a.sin_port = p_htons(localport);
1066 sockcode = p_bind(s, (family == AF_INET6 ?
1067 (struct sockaddr *) &a6 :
1068 (struct sockaddr *) &a),
1069 (family == AF_INET6 ? sizeof(a6) : sizeof(a)));
1071 sockcode = p_bind(s, (struct sockaddr *) &a, sizeof(a));
1073 if (sockcode != SOCKET_ERROR) {
1077 err = p_WSAGetLastError();
1078 if (err != WSAEADDRINUSE) /* failed, for a bad reason */
1083 break; /* we're only looping once */
1086 break; /* we might have got to the end */
1090 sock->error = winsock_error_string(err);
1095 * Connect to remote address.
1098 if (sock->step.ai) {
1099 if (family == AF_INET6) {
1100 a6.sin6_family = AF_INET6;
1101 a6.sin6_port = p_htons((short) sock->port);
1103 ((struct sockaddr_in6 *) sock->step.ai->ai_addr)->sin6_addr;
1104 a6.sin6_flowinfo = ((struct sockaddr_in6 *) sock->step.ai->ai_addr)->sin6_flowinfo;
1105 a6.sin6_scope_id = ((struct sockaddr_in6 *) sock->step.ai->ai_addr)->sin6_scope_id;
1107 a.sin_family = AF_INET;
1109 ((struct sockaddr_in *) sock->step.ai->ai_addr)->sin_addr;
1110 a.sin_port = p_htons((short) sock->port);
1115 assert(sock->addr->addresses && sock->step.curraddr < sock->addr->naddresses);
1116 a.sin_family = AF_INET;
1117 a.sin_addr.s_addr = p_htonl(sock->addr->addresses[sock->step.curraddr]);
1118 a.sin_port = p_htons((short) sock->port);
1121 /* Set up a select mechanism. This could be an AsyncSelect on a
1122 * window, or an EventSelect on an event object. */
1123 errstr = do_select(s, 1);
1125 sock->error = errstr;
1133 ((family == AF_INET6) ? (struct sockaddr *) &a6 :
1134 (struct sockaddr *) &a),
1135 (family == AF_INET6) ? sizeof(a6) : sizeof(a))
1137 p_connect(s, (struct sockaddr *) &a, sizeof(a))
1139 ) == SOCKET_ERROR) {
1140 err = p_WSAGetLastError();
1142 * We expect a potential EWOULDBLOCK here, because the
1143 * chances are the front end has done a select for
1144 * FD_CONNECT, so that connect() will complete
1147 if ( err != WSAEWOULDBLOCK ) {
1148 sock->error = winsock_error_string(err);
1153 * If we _don't_ get EWOULDBLOCK, the connect has completed
1154 * and we should set the socket as writable.
1164 * No matter what happened, put the socket back in the tree.
1166 add234(sktree, sock);
1169 struct SockAddr_tag thisaddr = sk_extractaddr_tmp(
1170 sock->addr, &sock->step);
1171 plug_log(sock->plug, 1, &thisaddr, sock->port, sock->error, err);
1176 Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
1177 int nodelay, int keepalive, Plug plug)
1179 static const struct socket_function_table fn_table = {
1187 sk_tcp_socket_error,
1195 * Create Socket structure.
1197 ret = snew(struct Socket_tag);
1198 ret->fn = &fn_table;
1201 bufchain_init(&ret->output_data);
1202 ret->connected = 0; /* to start with */
1203 ret->writable = 0; /* to start with */
1204 ret->sending_oob = 0;
1205 ret->outgoingeof = EOF_NO;
1207 ret->frozen_readable = 0;
1208 ret->localhost_only = 0; /* unused, but best init anyway */
1209 ret->pending_error = 0;
1210 ret->parent = ret->child = NULL;
1211 ret->oobinline = oobinline;
1212 ret->nodelay = nodelay;
1213 ret->keepalive = keepalive;
1214 ret->privport = privport;
1217 START_STEP(ret->addr, ret->step);
1218 ret->s = INVALID_SOCKET;
1222 err = try_connect(ret);
1223 } while (err && sk_nextaddr(ret->addr, &ret->step));
1225 return (Socket) ret;
1228 Socket sk_newlistener(const char *srcaddr, int port, Plug plug,
1229 int local_host_only, int orig_address_family)
1231 static const struct socket_function_table fn_table = {
1239 sk_tcp_socket_error,
1258 * Create Socket structure.
1260 ret = snew(struct Socket_tag);
1261 ret->fn = &fn_table;
1264 bufchain_init(&ret->output_data);
1265 ret->writable = 0; /* to start with */
1266 ret->sending_oob = 0;
1267 ret->outgoingeof = EOF_NO;
1269 ret->frozen_readable = 0;
1270 ret->localhost_only = local_host_only;
1271 ret->pending_error = 0;
1272 ret->parent = ret->child = NULL;
1276 * Translate address_family from platform-independent constants
1277 * into local reality.
1279 address_family = (orig_address_family == ADDRTYPE_IPV4 ? AF_INET :
1281 orig_address_family == ADDRTYPE_IPV6 ? AF_INET6 :
1286 * Our default, if passed the `don't care' value
1287 * ADDRTYPE_UNSPEC, is to listen on IPv4. If IPv6 is supported,
1288 * we will also set up a second socket listening on IPv6, but
1289 * the v4 one is primary since that ought to work even on
1290 * non-v6-supporting systems.
1292 if (address_family == AF_UNSPEC) address_family = AF_INET;
1297 s = p_socket(address_family, SOCK_STREAM, 0);
1300 if (s == INVALID_SOCKET) {
1301 err = p_WSAGetLastError();
1302 ret->error = winsock_error_string(err);
1303 return (Socket) ret;
1308 p_setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
1311 if (address_family == AF_INET6) {
1312 memset(&a6, 0, sizeof(a6));
1313 a6.sin6_family = AF_INET6;
1314 if (local_host_only)
1315 a6.sin6_addr = in6addr_loopback;
1317 a6.sin6_addr = in6addr_any;
1318 if (srcaddr != NULL && p_getaddrinfo) {
1319 struct addrinfo hints;
1320 struct addrinfo *ai;
1323 memset(&hints, 0, sizeof(hints));
1324 hints.ai_family = AF_INET6;
1327 /* strip [] on IPv6 address literals */
1328 char *trimmed_addr = host_strduptrim(srcaddr);
1329 err = p_getaddrinfo(trimmed_addr, NULL, &hints, &ai);
1330 sfree(trimmed_addr);
1332 if (err == 0 && ai->ai_family == AF_INET6) {
1334 ((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
1337 a6.sin6_port = p_htons(port);
1342 a.sin_family = AF_INET;
1345 * Bind to source address. First try an explicitly
1349 a.sin_addr.s_addr = p_inet_addr(srcaddr);
1350 if (a.sin_addr.s_addr != INADDR_NONE) {
1351 /* Override localhost_only with specified listen addr. */
1352 ret->localhost_only = ipv4_is_loopback(a.sin_addr);
1358 * ... and failing that, go with one of the standard ones.
1361 if (local_host_only)
1362 a.sin_addr.s_addr = p_htonl(INADDR_LOOPBACK);
1364 a.sin_addr.s_addr = p_htonl(INADDR_ANY);
1367 a.sin_port = p_htons((short)port);
1370 retcode = p_bind(s, (address_family == AF_INET6 ?
1371 (struct sockaddr *) &a6 :
1372 (struct sockaddr *) &a),
1374 AF_INET6 ? sizeof(a6) : sizeof(a)));
1376 retcode = p_bind(s, (struct sockaddr *) &a, sizeof(a));
1378 if (retcode != SOCKET_ERROR) {
1381 err = p_WSAGetLastError();
1386 ret->error = winsock_error_string(err);
1387 return (Socket) ret;
1391 if (p_listen(s, SOMAXCONN) == SOCKET_ERROR) {
1393 ret->error = winsock_error_string(p_WSAGetLastError());
1394 return (Socket) ret;
1397 /* Set up a select mechanism. This could be an AsyncSelect on a
1398 * window, or an EventSelect on an event object. */
1399 errstr = do_select(s, 1);
1402 ret->error = errstr;
1403 return (Socket) ret;
1406 add234(sktree, ret);
1410 * If we were given ADDRTYPE_UNSPEC, we must also create an
1411 * IPv6 listening socket and link it to this one.
1413 if (address_family == AF_INET && orig_address_family == ADDRTYPE_UNSPEC) {
1414 Actual_Socket other;
1416 other = (Actual_Socket) sk_newlistener(srcaddr, port, plug,
1417 local_host_only, ADDRTYPE_IPV6);
1420 if (!other->error) {
1421 other->parent = ret;
1430 return (Socket) ret;
1433 static void sk_tcp_close(Socket sock)
1435 extern char *do_select(SOCKET skt, int startup);
1436 Actual_Socket s = (Actual_Socket) sock;
1439 sk_tcp_close((Socket)s->child);
1443 p_closesocket(s->s);
1445 sk_addr_free(s->addr);
1450 * Deal with socket errors detected in try_send().
1452 static void socket_error_callback(void *vs)
1454 Actual_Socket s = (Actual_Socket)vs;
1457 * Just in case other socket work has caused this socket to vanish
1458 * or become somehow non-erroneous before this callback arrived...
1460 if (!find234(sktree, s, NULL) || !s->pending_error)
1464 * An error has occurred on this socket. Pass it to the plug.
1466 plug_closing(s->plug, winsock_error_string(s->pending_error),
1467 s->pending_error, 0);
1471 * The function which tries to send on a socket once it's deemed
1474 void try_send(Actual_Socket s)
1476 while (s->sending_oob || bufchain_size(&s->output_data) > 0) {
1480 int len, urgentflag;
1482 if (s->sending_oob) {
1483 urgentflag = MSG_OOB;
1484 len = s->sending_oob;
1488 bufchain_prefix(&s->output_data, &data, &len);
1490 nsent = p_send(s->s, data, len, urgentflag);
1491 noise_ultralight(nsent);
1493 err = (nsent < 0 ? p_WSAGetLastError() : 0);
1494 if ((err < WSABASEERR && nsent < 0) || err == WSAEWOULDBLOCK) {
1496 * Perfectly normal: we've sent all we can for the moment.
1498 * (Some WinSock send() implementations can return
1499 * <0 but leave no sensible error indication -
1500 * WSAGetLastError() is called but returns zero or
1501 * a small number - so we check that case and treat
1502 * it just like WSAEWOULDBLOCK.)
1504 s->writable = FALSE;
1506 } else if (nsent == 0 ||
1507 err == WSAECONNABORTED || err == WSAECONNRESET) {
1509 * If send() returns CONNABORTED or CONNRESET, we
1510 * unfortunately can't just call plug_closing(),
1511 * because it's quite likely that we're currently
1512 * _in_ a call from the code we'd be calling back
1513 * to, so we'd have to make half the SSH code
1514 * reentrant. Instead we flag a pending error on
1515 * the socket, to be dealt with (by calling
1516 * plug_closing()) at some suitable future moment.
1518 s->pending_error = err;
1519 queue_toplevel_callback(socket_error_callback, s);
1522 /* We're inside the Windows frontend here, so we know
1523 * that the frontend handle is unnecessary. */
1524 logevent(NULL, winsock_error_string(err));
1525 fatalbox("%s", winsock_error_string(err));
1528 if (s->sending_oob) {
1530 memmove(s->oobdata, s->oobdata+nsent, len-nsent);
1531 s->sending_oob = len - nsent;
1536 bufchain_consume(&s->output_data, nsent);
1542 * If we reach here, we've finished sending everything we might
1543 * have needed to send. Send EOF, if we need to.
1545 if (s->outgoingeof == EOF_PENDING) {
1546 p_shutdown(s->s, SD_SEND);
1547 s->outgoingeof = EOF_SENT;
1551 static int sk_tcp_write(Socket sock, const char *buf, int len)
1553 Actual_Socket s = (Actual_Socket) sock;
1555 assert(s->outgoingeof == EOF_NO);
1558 * Add the data to the buffer list on the socket.
1560 bufchain_add(&s->output_data, buf, len);
1563 * Now try sending from the start of the buffer list.
1568 return bufchain_size(&s->output_data);
1571 static int sk_tcp_write_oob(Socket sock, const char *buf, int len)
1573 Actual_Socket s = (Actual_Socket) sock;
1575 assert(s->outgoingeof == EOF_NO);
1578 * Replace the buffer list on the socket with the data.
1580 bufchain_clear(&s->output_data);
1581 assert(len <= sizeof(s->oobdata));
1582 memcpy(s->oobdata, buf, len);
1583 s->sending_oob = len;
1586 * Now try sending from the start of the buffer list.
1591 return s->sending_oob;
1594 static void sk_tcp_write_eof(Socket sock)
1596 Actual_Socket s = (Actual_Socket) sock;
1598 assert(s->outgoingeof == EOF_NO);
1601 * Mark the socket as pending outgoing EOF.
1603 s->outgoingeof = EOF_PENDING;
1606 * Now try sending from the start of the buffer list.
1612 int select_result(WPARAM wParam, LPARAM lParam)
1616 char buf[20480]; /* nice big buffer for plenty of speed */
1620 /* wParam is the socket itself */
1623 return 1; /* boggle */
1625 s = find234(sktree, (void *) wParam, cmpforsearch);
1627 return 1; /* boggle */
1629 if ((err = WSAGETSELECTERROR(lParam)) != 0) {
1631 * An error has occurred on this socket. Pass it to the
1635 struct SockAddr_tag thisaddr = sk_extractaddr_tmp(
1637 plug_log(s->plug, 1, &thisaddr, s->port,
1638 winsock_error_string(err), err);
1639 while (err && s->addr && sk_nextaddr(s->addr, &s->step)) {
1640 err = try_connect(s);
1644 return plug_closing(s->plug, winsock_error_string(err), err, 0);
1649 noise_ultralight(lParam);
1651 switch (WSAGETSELECTEVENT(lParam)) {
1653 s->connected = s->writable = 1;
1655 * Once a socket is connected, we can stop falling
1656 * back through the candidate addresses to connect
1660 sk_addr_free(s->addr);
1665 /* In the case the socket is still frozen, we don't even bother */
1667 s->frozen_readable = 1;
1672 * We have received data on the socket. For an oobinline
1673 * socket, this might be data _before_ an urgent pointer,
1674 * in which case we send it to the back end with type==1
1675 * (data prior to urgent).
1679 p_ioctlsocket(s->s, SIOCATMARK, &atmark);
1681 * Avoid checking the return value from ioctlsocket(),
1682 * on the grounds that some WinSock wrappers don't
1683 * support it. If it does nothing, we get atmark==1,
1684 * which is equivalent to `no OOB pending', so the
1685 * effect will be to non-OOB-ify any OOB data.
1690 ret = p_recv(s->s, buf, sizeof(buf), 0);
1691 noise_ultralight(ret);
1693 err = p_WSAGetLastError();
1694 if (err == WSAEWOULDBLOCK) {
1699 return plug_closing(s->plug, winsock_error_string(err), err,
1701 } else if (0 == ret) {
1702 return plug_closing(s->plug, NULL, 0, 0);
1704 return plug_receive(s->plug, atmark ? 0 : 1, buf, ret);
1709 * This will only happen on a non-oobinline socket. It
1710 * indicates that we can immediately perform an OOB read
1711 * and get back OOB data, which we will send to the back
1712 * end with type==2 (urgent data).
1714 ret = p_recv(s->s, buf, sizeof(buf), MSG_OOB);
1715 noise_ultralight(ret);
1717 const char *str = (ret == 0 ? "Internal networking trouble" :
1718 winsock_error_string(p_WSAGetLastError()));
1719 /* We're inside the Windows frontend here, so we know
1720 * that the frontend handle is unnecessary. */
1721 logevent(NULL, str);
1722 fatalbox("%s", str);
1724 return plug_receive(s->plug, 2, buf, ret);
1729 int bufsize_before, bufsize_after;
1731 bufsize_before = s->sending_oob + bufchain_size(&s->output_data);
1733 bufsize_after = s->sending_oob + bufchain_size(&s->output_data);
1734 if (bufsize_after < bufsize_before)
1735 plug_sent(s->plug, bufsize_after);
1739 /* Signal a close on the socket. First read any outstanding data. */
1742 ret = p_recv(s->s, buf, sizeof(buf), 0);
1744 err = p_WSAGetLastError();
1745 if (err == WSAEWOULDBLOCK)
1747 return plug_closing(s->plug, winsock_error_string(err),
1751 open &= plug_receive(s->plug, 0, buf, ret);
1753 open &= plug_closing(s->plug, NULL, 0, 0);
1760 struct sockaddr_in isa;
1762 struct sockaddr_storage isa;
1764 int addrlen = sizeof(isa);
1765 SOCKET t; /* socket of connection */
1768 memset(&isa, 0, sizeof(isa));
1770 t = p_accept(s->s,(struct sockaddr *)&isa,&addrlen);
1771 if (t == INVALID_SOCKET)
1773 err = p_WSAGetLastError();
1774 if (err == WSATRY_AGAIN)
1781 if (isa.ss_family == AF_INET &&
1782 s->localhost_only &&
1783 !ipv4_is_local_addr(((struct sockaddr_in *)&isa)->sin_addr))
1785 if (s->localhost_only && !ipv4_is_local_addr(isa.sin_addr))
1788 p_closesocket(t); /* dodgy WinSock let nonlocal through */
1789 } else if (plug_accepting(s->plug, sk_tcp_accept, actx)) {
1790 p_closesocket(t); /* denied or error */
1799 * Special error values are returned from sk_namelookup and sk_new
1800 * if there's a problem. These functions extract an error message,
1801 * or return NULL if there's no problem.
1803 const char *sk_addr_error(SockAddr addr)
1807 static const char *sk_tcp_socket_error(Socket sock)
1809 Actual_Socket s = (Actual_Socket) sock;
1813 static char *sk_tcp_peer_info(Socket sock)
1815 Actual_Socket s = (Actual_Socket) sock;
1817 struct sockaddr_in addr;
1819 struct sockaddr_storage addr;
1820 char buf[INET6_ADDRSTRLEN];
1822 int addrlen = sizeof(addr);
1824 if (p_getpeername(s->s, (struct sockaddr *)&addr, &addrlen) < 0)
1827 if (((struct sockaddr *)&addr)->sa_family == AF_INET) {
1830 p_inet_ntoa(((struct sockaddr_in *)&addr)->sin_addr),
1831 (int)p_ntohs(((struct sockaddr_in *)&addr)->sin_port));
1833 } else if (((struct sockaddr *)&addr)->sa_family == AF_INET6) {
1836 p_inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&addr)->sin6_addr,
1838 (int)p_ntohs(((struct sockaddr_in6 *)&addr)->sin6_port));
1845 static void sk_tcp_set_frozen(Socket sock, int is_frozen)
1847 Actual_Socket s = (Actual_Socket) sock;
1848 if (s->frozen == is_frozen)
1850 s->frozen = is_frozen;
1853 if (s->frozen_readable) {
1855 p_recv(s->s, &c, 1, MSG_PEEK);
1858 s->frozen_readable = 0;
1861 void socket_reselect_all(void)
1866 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
1873 * For Plink: enumerate all sockets currently active.
1875 SOCKET first_socket(int *state)
1879 s = index234(sktree, (*state)++);
1880 return s ? s->s : INVALID_SOCKET;
1883 SOCKET next_socket(int *state)
1885 Actual_Socket s = index234(sktree, (*state)++);
1886 return s ? s->s : INVALID_SOCKET;
1889 extern int socket_writable(SOCKET skt)
1891 Actual_Socket s = find234(sktree, (void *)skt, cmpforsearch);
1894 return bufchain_size(&s->output_data) > 0;
1899 int net_service_lookup(char *service)
1902 se = p_getservbyname(service, NULL);
1904 return p_ntohs(se->s_port);
1909 char *get_hostname(void)
1912 char *hostname = NULL;
1915 hostname = sresize(hostname, len, char);
1916 if (p_gethostname(hostname, len) < 0) {
1921 } while (strlen(hostname) >= (size_t)(len-1));
1925 SockAddr platform_get_x11_unix_address(const char *display, int displaynum,
1926 char **canonicalname)
1928 SockAddr ret = snew(struct SockAddr_tag);
1929 memset(ret, 0, sizeof(struct SockAddr_tag));
1930 ret->error = "unix sockets not supported on this platform";