Integrate unfix.org's IPv6 patches up to level 10, with rather a lot
[u/mdw/putty] / windows / winnet.c
1 /*
2 * Windows networking abstraction.
3 *
4 * For the IPv6 code in here I am indebted to Jeroen Massar and
5 * unfix.org.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <assert.h>
11
12 #define DEFINE_PLUG_METHOD_MACROS
13 #include "putty.h"
14 #include "network.h"
15 #include "tree234.h"
16
17 #include <ws2tcpip.h>
18
19 #ifndef NO_IPV6
20 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
21 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
22 #endif
23
24 #define ipv4_is_loopback(addr) \
25 ((p_ntohl(addr.s_addr) & 0xFF000000L) == 0x7F000000L)
26
27 struct Socket_tag {
28 const struct socket_function_table *fn;
29 /* the above variable absolutely *must* be the first in this structure */
30 char *error;
31 SOCKET s;
32 Plug plug;
33 void *private_ptr;
34 bufchain output_data;
35 int connected;
36 int writable;
37 int frozen; /* this causes readability notifications to be ignored */
38 int frozen_readable; /* this means we missed at least one readability
39 * notification while we were frozen */
40 int localhost_only; /* for listening sockets */
41 char oobdata[1];
42 int sending_oob;
43 int oobinline;
44 int pending_error; /* in case send() returns error */
45 };
46
47 /*
48 * We used to typedef struct Socket_tag *Socket.
49 *
50 * Since we have made the networking abstraction slightly more
51 * abstract, Socket no longer means a tcp socket (it could mean
52 * an ssl socket). So now we must use Actual_Socket when we know
53 * we are talking about a tcp socket.
54 */
55 typedef struct Socket_tag *Actual_Socket;
56
57 struct SockAddr_tag {
58 char *error;
59 /*
60 * Which address family this address belongs to. AF_INET for
61 * IPv4; AF_INET6 for IPv6; AF_UNSPEC indicates that name
62 * resolution has not been done and a simple host name is held
63 * in this SockAddr structure.
64 * The hostname field is also used when the hostname has both
65 * an IPv6 and IPv4 address and the IPv6 connection attempt
66 * fails. We then try the IPv4 address.
67 * This 'family' should become an option in the GUI and
68 * on the commandline for selecting a default protocol.
69 */
70 int family;
71 unsigned long address; /* Address IPv4 style. */
72 #ifndef NO_IPV6
73 struct addrinfo *ai; /* Address AF-independent (IPv4+IPv6) style. */
74 #endif
75 char hostname[512]; /* Store an unresolved host name. */
76 };
77
78 static tree234 *sktree;
79
80 static int cmpfortree(void *av, void *bv)
81 {
82 Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv;
83 unsigned long as = (unsigned long) a->s, bs = (unsigned long) b->s;
84 if (as < bs)
85 return -1;
86 if (as > bs)
87 return +1;
88 return 0;
89 }
90
91 static int cmpforsearch(void *av, void *bv)
92 {
93 Actual_Socket b = (Actual_Socket) bv;
94 unsigned long as = (unsigned long) av, bs = (unsigned long) b->s;
95 if (as < bs)
96 return -1;
97 if (as > bs)
98 return +1;
99 return 0;
100 }
101
102 #define NOTHING
103 #define DECL_WINSOCK_FUNCTION(linkage, rettype, name, params) \
104 typedef rettype (WINAPI *t_##name) params; \
105 linkage t_##name p_##name
106 #define GET_WINSOCK_FUNCTION(name) \
107 p_##name = (t_##name) GetProcAddress(winsock_module, #name)
108
109 DECL_WINSOCK_FUNCTION(NOTHING, int, WSAAsyncSelect,
110 (SOCKET, HWND, u_int, long));
111 DECL_WINSOCK_FUNCTION(NOTHING, int, WSAEventSelect, (SOCKET, WSAEVENT, long));
112 DECL_WINSOCK_FUNCTION(NOTHING, int, select,
113 (int, fd_set FAR *, fd_set FAR *,
114 fd_set FAR *, const struct timeval FAR *));
115 DECL_WINSOCK_FUNCTION(NOTHING, int, WSAGetLastError, (void));
116 DECL_WINSOCK_FUNCTION(NOTHING, int, WSAEnumNetworkEvents,
117 (SOCKET, WSAEVENT, LPWSANETWORKEVENTS));
118 DECL_WINSOCK_FUNCTION(static, int, WSAStartup, (WORD, LPWSADATA));
119 DECL_WINSOCK_FUNCTION(static, int, WSACleanup, (void));
120 DECL_WINSOCK_FUNCTION(static, int, closesocket, (SOCKET));
121 DECL_WINSOCK_FUNCTION(static, u_long, ntohl, (u_long));
122 DECL_WINSOCK_FUNCTION(static, u_long, htonl, (u_long));
123 DECL_WINSOCK_FUNCTION(static, u_short, htons, (u_short));
124 DECL_WINSOCK_FUNCTION(static, u_short, ntohs, (u_short));
125 DECL_WINSOCK_FUNCTION(static, struct hostent FAR *, gethostbyname,
126 (const char FAR *));
127 DECL_WINSOCK_FUNCTION(static, struct servent FAR *, getservbyname,
128 (const char FAR *, const char FAR *));
129 DECL_WINSOCK_FUNCTION(static, unsigned long, inet_addr, (const char FAR *));
130 DECL_WINSOCK_FUNCTION(static, char FAR *, inet_ntoa, (struct in_addr));
131 DECL_WINSOCK_FUNCTION(static, int, connect,
132 (SOCKET, const struct sockaddr FAR *, int));
133 DECL_WINSOCK_FUNCTION(static, int, bind,
134 (SOCKET, const struct sockaddr FAR *, int));
135 DECL_WINSOCK_FUNCTION(static, int, setsockopt,
136 (SOCKET, int, int, const char FAR *, int));
137 DECL_WINSOCK_FUNCTION(static, SOCKET, socket, (int, int, int));
138 DECL_WINSOCK_FUNCTION(static, int, listen, (SOCKET, int));
139 DECL_WINSOCK_FUNCTION(static, int, send, (SOCKET, const char FAR *, int, int));
140 DECL_WINSOCK_FUNCTION(static, int, ioctlsocket,
141 (SOCKET, long, u_long FAR *));
142 DECL_WINSOCK_FUNCTION(static, SOCKET, accept,
143 (SOCKET, struct sockaddr FAR *, int FAR *));
144 DECL_WINSOCK_FUNCTION(static, int, recv, (SOCKET, char FAR *, int, int));
145 DECL_WINSOCK_FUNCTION(static, int, WSAIoctl,
146 (SOCKET, DWORD, LPVOID, DWORD, LPVOID, DWORD,
147 LPDWORD, LPWSAOVERLAPPED,
148 LPWSAOVERLAPPED_COMPLETION_ROUTINE));
149
150 static HMODULE winsock_module;
151
152 void sk_init(void)
153 {
154 WORD winsock_ver;
155 WSADATA wsadata;
156
157 winsock_ver = MAKEWORD(2, 0);
158 winsock_module = LoadLibrary("WS2_32.DLL");
159 if (!winsock_module) {
160 winsock_module = LoadLibrary("WSOCK32.DLL");
161 winsock_ver = MAKEWORD(1, 1);
162 }
163 if (!winsock_module)
164 fatalbox("Unable to load any WinSock library");
165
166 GET_WINSOCK_FUNCTION(WSAAsyncSelect);
167 GET_WINSOCK_FUNCTION(WSAEventSelect);
168 GET_WINSOCK_FUNCTION(select);
169 GET_WINSOCK_FUNCTION(WSAGetLastError);
170 GET_WINSOCK_FUNCTION(WSAEnumNetworkEvents);
171 GET_WINSOCK_FUNCTION(WSAStartup);
172 GET_WINSOCK_FUNCTION(WSACleanup);
173 GET_WINSOCK_FUNCTION(closesocket);
174 GET_WINSOCK_FUNCTION(ntohl);
175 GET_WINSOCK_FUNCTION(htonl);
176 GET_WINSOCK_FUNCTION(htons);
177 GET_WINSOCK_FUNCTION(ntohs);
178 GET_WINSOCK_FUNCTION(gethostbyname);
179 GET_WINSOCK_FUNCTION(getservbyname);
180 GET_WINSOCK_FUNCTION(inet_addr);
181 GET_WINSOCK_FUNCTION(inet_ntoa);
182 GET_WINSOCK_FUNCTION(connect);
183 GET_WINSOCK_FUNCTION(bind);
184 GET_WINSOCK_FUNCTION(setsockopt);
185 GET_WINSOCK_FUNCTION(socket);
186 GET_WINSOCK_FUNCTION(listen);
187 GET_WINSOCK_FUNCTION(send);
188 GET_WINSOCK_FUNCTION(ioctlsocket);
189 GET_WINSOCK_FUNCTION(accept);
190 GET_WINSOCK_FUNCTION(recv);
191 GET_WINSOCK_FUNCTION(WSAIoctl);
192
193 if (p_WSAStartup(winsock_ver, &wsadata)) {
194 fatalbox("Unable to initialise WinSock");
195 }
196 if (LOBYTE(wsadata.wVersion) != LOBYTE(winsock_ver)) {
197 p_WSACleanup();
198 fatalbox("WinSock version is incompatible with %d.%d",
199 LOBYTE(winsock_ver), HIBYTE(winsock_ver));
200 }
201
202 sktree = newtree234(cmpfortree);
203 }
204
205 void sk_cleanup(void)
206 {
207 Actual_Socket s;
208 int i;
209
210 if (sktree) {
211 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
212 p_closesocket(s->s);
213 }
214 freetree234(sktree);
215 sktree = NULL;
216 }
217
218 p_WSACleanup();
219 if (winsock_module)
220 FreeLibrary(winsock_module);
221 }
222
223 char *winsock_error_string(int error)
224 {
225 switch (error) {
226 case WSAEACCES:
227 return "Network error: Permission denied";
228 case WSAEADDRINUSE:
229 return "Network error: Address already in use";
230 case WSAEADDRNOTAVAIL:
231 return "Network error: Cannot assign requested address";
232 case WSAEAFNOSUPPORT:
233 return
234 "Network error: Address family not supported by protocol family";
235 case WSAEALREADY:
236 return "Network error: Operation already in progress";
237 case WSAECONNABORTED:
238 return "Network error: Software caused connection abort";
239 case WSAECONNREFUSED:
240 return "Network error: Connection refused";
241 case WSAECONNRESET:
242 return "Network error: Connection reset by peer";
243 case WSAEDESTADDRREQ:
244 return "Network error: Destination address required";
245 case WSAEFAULT:
246 return "Network error: Bad address";
247 case WSAEHOSTDOWN:
248 return "Network error: Host is down";
249 case WSAEHOSTUNREACH:
250 return "Network error: No route to host";
251 case WSAEINPROGRESS:
252 return "Network error: Operation now in progress";
253 case WSAEINTR:
254 return "Network error: Interrupted function call";
255 case WSAEINVAL:
256 return "Network error: Invalid argument";
257 case WSAEISCONN:
258 return "Network error: Socket is already connected";
259 case WSAEMFILE:
260 return "Network error: Too many open files";
261 case WSAEMSGSIZE:
262 return "Network error: Message too long";
263 case WSAENETDOWN:
264 return "Network error: Network is down";
265 case WSAENETRESET:
266 return "Network error: Network dropped connection on reset";
267 case WSAENETUNREACH:
268 return "Network error: Network is unreachable";
269 case WSAENOBUFS:
270 return "Network error: No buffer space available";
271 case WSAENOPROTOOPT:
272 return "Network error: Bad protocol option";
273 case WSAENOTCONN:
274 return "Network error: Socket is not connected";
275 case WSAENOTSOCK:
276 return "Network error: Socket operation on non-socket";
277 case WSAEOPNOTSUPP:
278 return "Network error: Operation not supported";
279 case WSAEPFNOSUPPORT:
280 return "Network error: Protocol family not supported";
281 case WSAEPROCLIM:
282 return "Network error: Too many processes";
283 case WSAEPROTONOSUPPORT:
284 return "Network error: Protocol not supported";
285 case WSAEPROTOTYPE:
286 return "Network error: Protocol wrong type for socket";
287 case WSAESHUTDOWN:
288 return "Network error: Cannot send after socket shutdown";
289 case WSAESOCKTNOSUPPORT:
290 return "Network error: Socket type not supported";
291 case WSAETIMEDOUT:
292 return "Network error: Connection timed out";
293 case WSAEWOULDBLOCK:
294 return "Network error: Resource temporarily unavailable";
295 case WSAEDISCON:
296 return "Network error: Graceful shutdown in progress";
297 default:
298 return "Unknown network error";
299 }
300 }
301
302 SockAddr sk_namelookup(const char *host, char **canonicalname,
303 int address_family)
304 {
305 SockAddr ret = snew(struct SockAddr_tag);
306 unsigned long a;
307 struct hostent *h = NULL;
308 char realhost[8192];
309
310 /* Clear the structure and default to IPv4. */
311 memset(ret, 0, sizeof(struct SockAddr_tag));
312 ret->family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
313 #ifndef NO_IPV6
314 address_family == ADDRTYPE_IPV6 ? AF_INET6 :
315 #endif
316 AF_UNSPEC);
317 *realhost = '\0';
318
319 if ((a = p_inet_addr(host)) == (unsigned long) INADDR_NONE) {
320 #ifndef NO_IPV6
321
322 /* Try to get the getaddrinfo() function from wship6.dll */
323 /* This way one doesn't need to have IPv6 dll's to use PuTTY and
324 * it will fallback to IPv4. */
325 typedef int (CALLBACK * FGETADDRINFO) (const char *nodename,
326 const char *servname,
327 const struct addrinfo *
328 hints,
329 struct addrinfo ** res);
330 FGETADDRINFO fGetAddrInfo = NULL;
331
332 HINSTANCE dllWSHIP6 = LoadLibrary("wship6.dll");
333 if (dllWSHIP6)
334 fGetAddrInfo = (FGETADDRINFO) GetProcAddress(dllWSHIP6,
335 "getaddrinfo");
336
337 /*
338 * Use fGetAddrInfo when it's available
339 */
340 if (fGetAddrInfo) {
341 struct addrinfo hints;
342 memset(&hints, 0, sizeof(hints));
343 hints.ai_family = ret->family;
344 if (fGetAddrInfo(host, NULL, &hints, &ret->ai) == 0)
345 ret->family = ret->ai->ai_family;
346 } else
347 #endif
348 {
349 /*
350 * Otherwise use the IPv4-only gethostbyname...
351 * (NOTE: we don't use gethostbyname as a fallback!)
352 */
353 if (ret->family == 0) {
354 if ( (h = p_gethostbyname(host)) )
355 ret->family = AF_INET;
356 }
357 }
358
359 if (ret->family == AF_UNSPEC) {
360 DWORD err = p_WSAGetLastError();
361 ret->error = (err == WSAENETDOWN ? "Network is down" :
362 err ==
363 WSAHOST_NOT_FOUND ? "Host does not exist" : err
364 == WSATRY_AGAIN ? "Host not found" :
365 #ifndef NO_IPV6
366 fGetAddrInfo ? "getaddrinfo: unknown error" :
367 #endif
368 "gethostbyname: unknown error");
369 } else {
370 ret->error = NULL;
371
372 #ifndef NO_IPV6
373 /* If we got an address info use that... */
374 if (ret->ai) {
375 typedef int (CALLBACK * FGETNAMEINFO)
376 (const struct sockaddr FAR * sa, socklen_t salen,
377 char FAR * host, size_t hostlen, char FAR * serv,
378 size_t servlen, int flags);
379 FGETNAMEINFO fGetNameInfo = NULL;
380
381 /* Are we in IPv4 fallback mode? */
382 /* We put the IPv4 address into the a variable so we can further-on use the IPv4 code... */
383 if (ret->family == AF_INET)
384 memcpy(&a,
385 (char *) &((SOCKADDR_IN *) ret->ai->
386 ai_addr)->sin_addr, sizeof(a));
387
388 /* Now let's find that canonicalname... */
389 if ((dllWSHIP6)
390 && (fGetNameInfo =
391 (FGETNAMEINFO) GetProcAddress(dllWSHIP6,
392 "getnameinfo"))) {
393 if (fGetNameInfo
394 ((struct sockaddr *) ret->ai->ai_addr,
395 ret->family ==
396 AF_INET ? sizeof(SOCKADDR_IN) :
397 sizeof(SOCKADDR_IN6), realhost,
398 sizeof(realhost), NULL, 0, 0) != 0) {
399 strncpy(realhost, host, sizeof(realhost));
400 }
401 }
402 }
403 /* We used the IPv4-only gethostbyname()... */
404 else
405 #endif
406 {
407 memcpy(&a, h->h_addr, sizeof(a));
408 /* This way we are always sure the h->h_name is valid :) */
409 strncpy(realhost, h->h_name, sizeof(realhost));
410 }
411 }
412 #ifndef NO_IPV6
413 FreeLibrary(dllWSHIP6);
414 #endif
415 } else {
416 /*
417 * This must be a numeric IPv4 address because it caused a
418 * success return from inet_addr.
419 */
420 ret->family = AF_INET;
421 strncpy(realhost, host, sizeof(realhost));
422 }
423 ret->address = p_ntohl(a);
424 realhost[lenof(realhost)-1] = '\0';
425 *canonicalname = snewn(1+strlen(realhost), char);
426 strcpy(*canonicalname, realhost);
427 return ret;
428 }
429
430 SockAddr sk_nonamelookup(const char *host)
431 {
432 SockAddr ret = snew(struct SockAddr_tag);
433 ret->error = NULL;
434 ret->family = AF_UNSPEC;
435 strncpy(ret->hostname, host, lenof(ret->hostname));
436 ret->hostname[lenof(ret->hostname)-1] = '\0';
437 return ret;
438 }
439
440 void sk_getaddr(SockAddr addr, char *buf, int buflen)
441 {
442 #ifndef NO_IPV6
443 if (addr->family == AF_INET6) {
444 /* Try to get the WSAAddressToStringA() function from wship6.dll */
445 /* This way one doesn't need to have IPv6 dll's to use PuTTY and
446 * it will fallback to IPv4. */
447 typedef int (CALLBACK * FADDRTOSTR) (LPSOCKADDR lpsaAddress,
448 DWORD dwAddressLength,
449 LPWSAPROTOCOL_INFO lpProtocolInfo,
450 OUT LPTSTR lpszAddressString,
451 IN OUT LPDWORD lpdwAddressStringLength
452 );
453 FADDRTOSTR fAddrToStr = NULL;
454
455 HINSTANCE dllWS2 = LoadLibrary("ws2_32.dll");
456 if (dllWS2) {
457 fAddrToStr = (FADDRTOSTR)GetProcAddress(dllWS2,
458 "WSAAddressToStringA");
459 if (fAddrToStr) {
460 fAddrToStr(addr->ai->ai_addr, addr->ai->ai_addrlen,
461 NULL, buf, &buflen);
462 }
463 else strncpy(buf, "IPv6", buflen);
464 FreeLibrary(dllWS2);
465 }
466 } else
467 #endif
468 if (addr->family == AF_INET) {
469 struct in_addr a;
470 a.s_addr = p_htonl(addr->address);
471 strncpy(buf, p_inet_ntoa(a), buflen);
472 buf[buflen-1] = '\0';
473 } else {
474 strncpy(buf, addr->hostname, buflen);
475 buf[buflen-1] = '\0';
476 }
477 }
478
479 int sk_hostname_is_local(char *name)
480 {
481 return !strcmp(name, "localhost");
482 }
483
484 static INTERFACE_INFO local_interfaces[16];
485 static int n_local_interfaces; /* 0=not yet, -1=failed, >0=number */
486
487 static int ipv4_is_local_addr(struct in_addr addr)
488 {
489 if (ipv4_is_loopback(addr))
490 return 1; /* loopback addresses are local */
491 if (!n_local_interfaces) {
492 SOCKET s = p_socket(AF_INET, SOCK_DGRAM, 0);
493 DWORD retbytes;
494
495 if (p_WSAIoctl &&
496 p_WSAIoctl(s, SIO_GET_INTERFACE_LIST, NULL, 0,
497 local_interfaces, sizeof(local_interfaces),
498 &retbytes, NULL, NULL) == 0)
499 n_local_interfaces = retbytes / sizeof(INTERFACE_INFO);
500 else
501 logevent(NULL, "Unable to get list of local IP addresses");
502 }
503 if (n_local_interfaces > 0) {
504 int i;
505 for (i = 0; i < n_local_interfaces; i++) {
506 SOCKADDR_IN *address =
507 (SOCKADDR_IN *)&local_interfaces[i].iiAddress;
508 if (address->sin_addr.s_addr == addr.s_addr)
509 return 1; /* this address is local */
510 }
511 }
512 return 0; /* this address is not local */
513 }
514
515 int sk_address_is_local(SockAddr addr)
516 {
517 #ifndef NO_IPV6
518 if (addr->family == AF_INET6) {
519 return IN6_IS_ADDR_LOOPBACK((const struct in6_addr *)addr->ai->ai_addr);
520 } else
521 #endif
522 if (addr->family == AF_INET) {
523 struct in_addr a;
524 a.s_addr = p_htonl(addr->address);
525 return ipv4_is_local_addr(a);
526 } else {
527 assert(addr->family == AF_UNSPEC);
528 return 0; /* we don't know; assume not */
529 }
530 }
531
532 int sk_addrtype(SockAddr addr)
533 {
534 return (addr->family == AF_INET ? ADDRTYPE_IPV4 :
535 #ifndef NO_IPV6
536 addr->family == AF_INET6 ? ADDRTYPE_IPV6 :
537 #endif
538 ADDRTYPE_NAME);
539 }
540
541 void sk_addrcopy(SockAddr addr, char *buf)
542 {
543 assert(addr->family != AF_UNSPEC);
544 #ifndef NO_IPV6
545 if (addr->family == AF_INET6) {
546 memcpy(buf, (char*) addr->ai, 16);
547 } else
548 #endif
549 if (addr->family == AF_INET) {
550 struct in_addr a;
551 a.s_addr = p_htonl(addr->address);
552 memcpy(buf, (char*) &a.s_addr, 4);
553 }
554 }
555
556 void sk_addr_free(SockAddr addr)
557 {
558 sfree(addr);
559 }
560
561 static Plug sk_tcp_plug(Socket sock, Plug p)
562 {
563 Actual_Socket s = (Actual_Socket) sock;
564 Plug ret = s->plug;
565 if (p)
566 s->plug = p;
567 return ret;
568 }
569
570 static void sk_tcp_flush(Socket s)
571 {
572 /*
573 * We send data to the socket as soon as we can anyway,
574 * so we don't need to do anything here. :-)
575 */
576 }
577
578 static void sk_tcp_close(Socket s);
579 static int sk_tcp_write(Socket s, const char *data, int len);
580 static int sk_tcp_write_oob(Socket s, const char *data, int len);
581 static void sk_tcp_set_private_ptr(Socket s, void *ptr);
582 static void *sk_tcp_get_private_ptr(Socket s);
583 static void sk_tcp_set_frozen(Socket s, int is_frozen);
584 static const char *sk_tcp_socket_error(Socket s);
585
586 extern char *do_select(SOCKET skt, int startup);
587
588 Socket sk_register(void *sock, Plug plug)
589 {
590 static const struct socket_function_table fn_table = {
591 sk_tcp_plug,
592 sk_tcp_close,
593 sk_tcp_write,
594 sk_tcp_write_oob,
595 sk_tcp_flush,
596 sk_tcp_set_private_ptr,
597 sk_tcp_get_private_ptr,
598 sk_tcp_set_frozen,
599 sk_tcp_socket_error
600 };
601
602 DWORD err;
603 char *errstr;
604 Actual_Socket ret;
605
606 /*
607 * Create Socket structure.
608 */
609 ret = snew(struct Socket_tag);
610 ret->fn = &fn_table;
611 ret->error = NULL;
612 ret->plug = plug;
613 bufchain_init(&ret->output_data);
614 ret->writable = 1; /* to start with */
615 ret->sending_oob = 0;
616 ret->frozen = 1;
617 ret->frozen_readable = 0;
618 ret->localhost_only = 0; /* unused, but best init anyway */
619 ret->pending_error = 0;
620
621 ret->s = (SOCKET)sock;
622
623 if (ret->s == INVALID_SOCKET) {
624 err = p_WSAGetLastError();
625 ret->error = winsock_error_string(err);
626 return (Socket) ret;
627 }
628
629 ret->oobinline = 0;
630
631 /* Set up a select mechanism. This could be an AsyncSelect on a
632 * window, or an EventSelect on an event object. */
633 errstr = do_select(ret->s, 1);
634 if (errstr) {
635 ret->error = errstr;
636 return (Socket) ret;
637 }
638
639 add234(sktree, ret);
640
641 return (Socket) ret;
642 }
643
644 Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
645 int nodelay, int keepalive, Plug plug)
646 {
647 static const struct socket_function_table fn_table = {
648 sk_tcp_plug,
649 sk_tcp_close,
650 sk_tcp_write,
651 sk_tcp_write_oob,
652 sk_tcp_flush,
653 sk_tcp_set_private_ptr,
654 sk_tcp_get_private_ptr,
655 sk_tcp_set_frozen,
656 sk_tcp_socket_error
657 };
658
659 SOCKET s;
660 #ifndef NO_IPV6
661 SOCKADDR_IN6 a6;
662 #endif
663 SOCKADDR_IN a;
664 DWORD err;
665 char *errstr;
666 Actual_Socket ret;
667 short localport;
668
669 /*
670 * Create Socket structure.
671 */
672 ret = snew(struct Socket_tag);
673 ret->fn = &fn_table;
674 ret->error = NULL;
675 ret->plug = plug;
676 bufchain_init(&ret->output_data);
677 ret->connected = 0; /* to start with */
678 ret->writable = 0; /* to start with */
679 ret->sending_oob = 0;
680 ret->frozen = 0;
681 ret->frozen_readable = 0;
682 ret->localhost_only = 0; /* unused, but best init anyway */
683 ret->pending_error = 0;
684
685 /*
686 * Open socket.
687 */
688 #ifndef NO_IPV6
689 /* Let's default to IPv6, this shouldn't hurt anybody
690 * If the stack supports IPv6 it will also allow IPv4 connections. */
691 if (addr->family == AF_UNSPEC) addr->family = AF_INET6;
692 #else
693 /* No other choice, default to IPv4 */
694 if (addr->family == AF_UNSPEC) addr->family = AF_INET;
695 #endif
696 s = p_socket(addr->family, SOCK_STREAM, 0);
697 ret->s = s;
698
699 if (s == INVALID_SOCKET) {
700 err = p_WSAGetLastError();
701 ret->error = winsock_error_string(err);
702 return (Socket) ret;
703 }
704
705 ret->oobinline = oobinline;
706 if (oobinline) {
707 BOOL b = TRUE;
708 p_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));
709 }
710
711 if (nodelay) {
712 BOOL b = TRUE;
713 p_setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *) &b, sizeof(b));
714 }
715
716 if (keepalive) {
717 BOOL b = TRUE;
718 p_setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *) &b, sizeof(b));
719 }
720
721 /*
722 * Bind to local address.
723 */
724 if (privport)
725 localport = 1023; /* count from 1023 downwards */
726 else
727 localport = 0; /* just use port 0 (ie winsock picks) */
728
729 /* Loop round trying to bind */
730 while (1) {
731 int retcode;
732
733 #ifndef NO_IPV6
734 if (addr->family == AF_INET6) {
735 memset(&a6, 0, sizeof(a6));
736 a6.sin6_family = AF_INET6;
737 /*a6.sin6_addr = in6addr_any; */ /* == 0 done by memset() */
738 a6.sin6_port = p_htons(localport);
739 } else
740 #endif
741 {
742 a.sin_family = AF_INET;
743 a.sin_addr.s_addr = p_htonl(INADDR_ANY);
744 a.sin_port = p_htons(localport);
745 }
746 #ifndef NO_IPV6
747 retcode = p_bind(s, (addr->family == AF_INET6 ?
748 (struct sockaddr *) &a6 :
749 (struct sockaddr *) &a),
750 (addr->family ==
751 AF_INET6 ? sizeof(a6) : sizeof(a)));
752 #else
753 retcode = p_bind(s, (struct sockaddr *) &a, sizeof(a));
754 #endif
755 if (retcode != SOCKET_ERROR) {
756 err = 0;
757 break; /* done */
758 } else {
759 err = p_WSAGetLastError();
760 if (err != WSAEADDRINUSE) /* failed, for a bad reason */
761 break;
762 }
763
764 if (localport == 0)
765 break; /* we're only looping once */
766 localport--;
767 if (localport == 0)
768 break; /* we might have got to the end */
769 }
770
771 if (err) {
772 ret->error = winsock_error_string(err);
773 return (Socket) ret;
774 }
775
776 /*
777 * Connect to remote address.
778 */
779 #ifndef NO_IPV6
780 if (addr->family == AF_INET6) {
781 memset(&a, 0, sizeof(a));
782 a6.sin6_family = AF_INET6;
783 a6.sin6_port = p_htons((short) port);
784 a6.sin6_addr =
785 ((struct sockaddr_in6 *) addr->ai->ai_addr)->sin6_addr;
786 } else
787 #endif
788 {
789 a.sin_family = AF_INET;
790 a.sin_addr.s_addr = p_htonl(addr->address);
791 a.sin_port = p_htons((short) port);
792 }
793
794 /* Set up a select mechanism. This could be an AsyncSelect on a
795 * window, or an EventSelect on an event object. */
796 errstr = do_select(s, 1);
797 if (errstr) {
798 ret->error = errstr;
799 return (Socket) ret;
800 }
801
802 if ((
803 #ifndef NO_IPV6
804 p_connect(s, ((addr->family == AF_INET6) ?
805 (struct sockaddr *) &a6 : (struct sockaddr *) &a),
806 (addr->family == AF_INET6) ? sizeof(a6) : sizeof(a))
807 #else
808 p_connect(s, (struct sockaddr *) &a, sizeof(a))
809 #endif
810 ) == SOCKET_ERROR) {
811 err = p_WSAGetLastError();
812 /*
813 * We expect a potential EWOULDBLOCK here, because the
814 * chances are the front end has done a select for
815 * FD_CONNECT, so that connect() will complete
816 * asynchronously.
817 */
818 if ( err != WSAEWOULDBLOCK ) {
819 ret->error = winsock_error_string(err);
820 return (Socket) ret;
821 }
822 } else {
823 /*
824 * If we _don't_ get EWOULDBLOCK, the connect has completed
825 * and we should set the socket as writable.
826 */
827 ret->writable = 1;
828 }
829
830 add234(sktree, ret);
831
832 /* We're done with 'addr' now. */
833 sk_addr_free(addr);
834
835 return (Socket) ret;
836 }
837
838 Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only,
839 int address_family)
840 {
841 static const struct socket_function_table fn_table = {
842 sk_tcp_plug,
843 sk_tcp_close,
844 sk_tcp_write,
845 sk_tcp_write_oob,
846 sk_tcp_flush,
847 sk_tcp_set_private_ptr,
848 sk_tcp_get_private_ptr,
849 sk_tcp_set_frozen,
850 sk_tcp_socket_error
851 };
852
853 SOCKET s;
854 #ifndef NO_IPV6
855 SOCKADDR_IN6 a6;
856 #endif
857 SOCKADDR_IN a;
858
859 DWORD err;
860 char *errstr;
861 Actual_Socket ret;
862 int retcode;
863 int on = 1;
864
865 /*
866 * Create Socket structure.
867 */
868 ret = snew(struct Socket_tag);
869 ret->fn = &fn_table;
870 ret->error = NULL;
871 ret->plug = plug;
872 bufchain_init(&ret->output_data);
873 ret->writable = 0; /* to start with */
874 ret->sending_oob = 0;
875 ret->frozen = 0;
876 ret->frozen_readable = 0;
877 ret->localhost_only = local_host_only;
878 ret->pending_error = 0;
879
880 /*
881 * Translate address_family from platform-independent constants
882 * into local reality.
883 */
884 address_family = (address_family == ADDRTYPE_IPV4 ? AF_INET :
885 #ifndef NO_IPV6
886 address_family == ADDRTYPE_IPV6 ? AF_INET6 :
887 #endif
888 AF_UNSPEC);
889
890 #ifndef NO_IPV6
891 /* Let's default to IPv6, this shouldn't hurt anybody
892 * If the stack supports IPv6 it will also allow IPv4 connections. */
893 if (address_family == AF_UNSPEC) address_family = AF_INET6;
894 #else
895 /* No other choice, default to IPv4 */
896 if (address_family == AF_UNSPEC) address_family = AF_INET;
897 #endif
898
899 /*
900 * Open socket.
901 */
902 s = p_socket(address_family, SOCK_STREAM, 0);
903 ret->s = s;
904
905 if (s == INVALID_SOCKET) {
906 err = p_WSAGetLastError();
907 ret->error = winsock_error_string(err);
908 return (Socket) ret;
909 }
910
911 ret->oobinline = 0;
912
913 p_setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
914
915 #ifndef NO_IPV6
916 if (address_family == AF_INET6) {
917 memset(&a6, 0, sizeof(a6));
918 a6.sin6_family = AF_INET6;
919 /* FIXME: srcaddr is ignored for IPv6, because I (SGT) don't
920 * know how to do it. :-)
921 * (jeroen:) saddr is specified as an address.. eg 2001:db8::1
922 * Thus we need either a parser that understands [2001:db8::1]:80
923 * style addresses and/or enhance this to understand hostnames too. */
924 if (local_host_only)
925 a6.sin6_addr = in6addr_loopback;
926 else
927 a6.sin6_addr = in6addr_any;
928 a6.sin6_port = p_htons(port);
929 } else
930 #endif
931 {
932 int got_addr = 0;
933 a.sin_family = AF_INET;
934
935 /*
936 * Bind to source address. First try an explicitly
937 * specified one...
938 */
939 if (srcaddr) {
940 a.sin_addr.s_addr = p_inet_addr(srcaddr);
941 if (a.sin_addr.s_addr != INADDR_NONE) {
942 /* Override localhost_only with specified listen addr. */
943 ret->localhost_only = ipv4_is_loopback(a.sin_addr);
944 got_addr = 1;
945 }
946 }
947
948 /*
949 * ... and failing that, go with one of the standard ones.
950 */
951 if (!got_addr) {
952 if (local_host_only)
953 a.sin_addr.s_addr = p_htonl(INADDR_LOOPBACK);
954 else
955 a.sin_addr.s_addr = p_htonl(INADDR_ANY);
956 }
957
958 a.sin_port = p_htons((short)port);
959 }
960 #ifndef NO_IPV6
961 retcode = p_bind(s, (address_family == AF_INET6 ?
962 (struct sockaddr *) &a6 :
963 (struct sockaddr *) &a),
964 (address_family ==
965 AF_INET6 ? sizeof(a6) : sizeof(a)));
966 #else
967 retcode = p_bind(s, (struct sockaddr *) &a, sizeof(a));
968 #endif
969 if (retcode != SOCKET_ERROR) {
970 err = 0;
971 } else {
972 err = p_WSAGetLastError();
973 }
974
975 if (err) {
976 ret->error = winsock_error_string(err);
977 return (Socket) ret;
978 }
979
980
981 if (p_listen(s, SOMAXCONN) == SOCKET_ERROR) {
982 p_closesocket(s);
983 ret->error = winsock_error_string(err);
984 return (Socket) ret;
985 }
986
987 /* Set up a select mechanism. This could be an AsyncSelect on a
988 * window, or an EventSelect on an event object. */
989 errstr = do_select(s, 1);
990 if (errstr) {
991 ret->error = errstr;
992 return (Socket) ret;
993 }
994
995 add234(sktree, ret);
996
997 return (Socket) ret;
998 }
999
1000 static void sk_tcp_close(Socket sock)
1001 {
1002 extern char *do_select(SOCKET skt, int startup);
1003 Actual_Socket s = (Actual_Socket) sock;
1004
1005 del234(sktree, s);
1006 do_select(s->s, 0);
1007 p_closesocket(s->s);
1008 sfree(s);
1009 }
1010
1011 /*
1012 * The function which tries to send on a socket once it's deemed
1013 * writable.
1014 */
1015 void try_send(Actual_Socket s)
1016 {
1017 while (s->sending_oob || bufchain_size(&s->output_data) > 0) {
1018 int nsent;
1019 DWORD err;
1020 void *data;
1021 int len, urgentflag;
1022
1023 if (s->sending_oob) {
1024 urgentflag = MSG_OOB;
1025 len = s->sending_oob;
1026 data = &s->oobdata;
1027 } else {
1028 urgentflag = 0;
1029 bufchain_prefix(&s->output_data, &data, &len);
1030 }
1031 nsent = p_send(s->s, data, len, urgentflag);
1032 noise_ultralight(nsent);
1033 if (nsent <= 0) {
1034 err = (nsent < 0 ? p_WSAGetLastError() : 0);
1035 if ((err < WSABASEERR && nsent < 0) || err == WSAEWOULDBLOCK) {
1036 /*
1037 * Perfectly normal: we've sent all we can for the moment.
1038 *
1039 * (Some WinSock send() implementations can return
1040 * <0 but leave no sensible error indication -
1041 * WSAGetLastError() is called but returns zero or
1042 * a small number - so we check that case and treat
1043 * it just like WSAEWOULDBLOCK.)
1044 */
1045 s->writable = FALSE;
1046 return;
1047 } else if (nsent == 0 ||
1048 err == WSAECONNABORTED || err == WSAECONNRESET) {
1049 /*
1050 * If send() returns CONNABORTED or CONNRESET, we
1051 * unfortunately can't just call plug_closing(),
1052 * because it's quite likely that we're currently
1053 * _in_ a call from the code we'd be calling back
1054 * to, so we'd have to make half the SSH code
1055 * reentrant. Instead we flag a pending error on
1056 * the socket, to be dealt with (by calling
1057 * plug_closing()) at some suitable future moment.
1058 */
1059 s->pending_error = err;
1060 return;
1061 } else {
1062 /* We're inside the Windows frontend here, so we know
1063 * that the frontend handle is unnecessary. */
1064 logevent(NULL, winsock_error_string(err));
1065 fatalbox("%s", winsock_error_string(err));
1066 }
1067 } else {
1068 if (s->sending_oob) {
1069 if (nsent < len) {
1070 memmove(s->oobdata, s->oobdata+nsent, len-nsent);
1071 s->sending_oob = len - nsent;
1072 } else {
1073 s->sending_oob = 0;
1074 }
1075 } else {
1076 bufchain_consume(&s->output_data, nsent);
1077 }
1078 }
1079 }
1080 }
1081
1082 static int sk_tcp_write(Socket sock, const char *buf, int len)
1083 {
1084 Actual_Socket s = (Actual_Socket) sock;
1085
1086 /*
1087 * Add the data to the buffer list on the socket.
1088 */
1089 bufchain_add(&s->output_data, buf, len);
1090
1091 /*
1092 * Now try sending from the start of the buffer list.
1093 */
1094 if (s->writable)
1095 try_send(s);
1096
1097 return bufchain_size(&s->output_data);
1098 }
1099
1100 static int sk_tcp_write_oob(Socket sock, const char *buf, int len)
1101 {
1102 Actual_Socket s = (Actual_Socket) sock;
1103
1104 /*
1105 * Replace the buffer list on the socket with the data.
1106 */
1107 bufchain_clear(&s->output_data);
1108 assert(len <= sizeof(s->oobdata));
1109 memcpy(s->oobdata, buf, len);
1110 s->sending_oob = len;
1111
1112 /*
1113 * Now try sending from the start of the buffer list.
1114 */
1115 if (s->writable)
1116 try_send(s);
1117
1118 return s->sending_oob;
1119 }
1120
1121 int select_result(WPARAM wParam, LPARAM lParam)
1122 {
1123 int ret, open;
1124 DWORD err;
1125 char buf[20480]; /* nice big buffer for plenty of speed */
1126 Actual_Socket s;
1127 u_long atmark;
1128
1129 /* wParam is the socket itself */
1130
1131 if (wParam == 0)
1132 return 1; /* boggle */
1133
1134 s = find234(sktree, (void *) wParam, cmpforsearch);
1135 if (!s)
1136 return 1; /* boggle */
1137
1138 if ((err = WSAGETSELECTERROR(lParam)) != 0) {
1139 /*
1140 * An error has occurred on this socket. Pass it to the
1141 * plug.
1142 */
1143 return plug_closing(s->plug, winsock_error_string(err), err, 0);
1144 }
1145
1146 noise_ultralight(lParam);
1147
1148 switch (WSAGETSELECTEVENT(lParam)) {
1149 case FD_CONNECT:
1150 s->connected = s->writable = 1;
1151 break;
1152 case FD_READ:
1153 /* In the case the socket is still frozen, we don't even bother */
1154 if (s->frozen) {
1155 s->frozen_readable = 1;
1156 break;
1157 }
1158
1159 /*
1160 * We have received data on the socket. For an oobinline
1161 * socket, this might be data _before_ an urgent pointer,
1162 * in which case we send it to the back end with type==1
1163 * (data prior to urgent).
1164 */
1165 if (s->oobinline) {
1166 atmark = 1;
1167 p_ioctlsocket(s->s, SIOCATMARK, &atmark);
1168 /*
1169 * Avoid checking the return value from ioctlsocket(),
1170 * on the grounds that some WinSock wrappers don't
1171 * support it. If it does nothing, we get atmark==1,
1172 * which is equivalent to `no OOB pending', so the
1173 * effect will be to non-OOB-ify any OOB data.
1174 */
1175 } else
1176 atmark = 1;
1177
1178 ret = p_recv(s->s, buf, sizeof(buf), 0);
1179 noise_ultralight(ret);
1180 if (ret < 0) {
1181 err = p_WSAGetLastError();
1182 if (err == WSAEWOULDBLOCK) {
1183 break;
1184 }
1185 }
1186 if (ret < 0) {
1187 return plug_closing(s->plug, winsock_error_string(err), err,
1188 0);
1189 } else if (0 == ret) {
1190 return plug_closing(s->plug, NULL, 0, 0);
1191 } else {
1192 return plug_receive(s->plug, atmark ? 0 : 1, buf, ret);
1193 }
1194 break;
1195 case FD_OOB:
1196 /*
1197 * This will only happen on a non-oobinline socket. It
1198 * indicates that we can immediately perform an OOB read
1199 * and get back OOB data, which we will send to the back
1200 * end with type==2 (urgent data).
1201 */
1202 ret = p_recv(s->s, buf, sizeof(buf), MSG_OOB);
1203 noise_ultralight(ret);
1204 if (ret <= 0) {
1205 char *str = (ret == 0 ? "Internal networking trouble" :
1206 winsock_error_string(p_WSAGetLastError()));
1207 /* We're inside the Windows frontend here, so we know
1208 * that the frontend handle is unnecessary. */
1209 logevent(NULL, str);
1210 fatalbox("%s", str);
1211 } else {
1212 return plug_receive(s->plug, 2, buf, ret);
1213 }
1214 break;
1215 case FD_WRITE:
1216 {
1217 int bufsize_before, bufsize_after;
1218 s->writable = 1;
1219 bufsize_before = s->sending_oob + bufchain_size(&s->output_data);
1220 try_send(s);
1221 bufsize_after = s->sending_oob + bufchain_size(&s->output_data);
1222 if (bufsize_after < bufsize_before)
1223 plug_sent(s->plug, bufsize_after);
1224 }
1225 break;
1226 case FD_CLOSE:
1227 /* Signal a close on the socket. First read any outstanding data. */
1228 open = 1;
1229 do {
1230 ret = p_recv(s->s, buf, sizeof(buf), 0);
1231 if (ret < 0) {
1232 err = p_WSAGetLastError();
1233 if (err == WSAEWOULDBLOCK)
1234 break;
1235 return plug_closing(s->plug, winsock_error_string(err),
1236 err, 0);
1237 } else {
1238 if (ret)
1239 open &= plug_receive(s->plug, 0, buf, ret);
1240 else
1241 open &= plug_closing(s->plug, NULL, 0, 0);
1242 }
1243 } while (ret > 0);
1244 return open;
1245 case FD_ACCEPT:
1246 {
1247 #ifdef NO_IPV6
1248 struct sockaddr_in isa;
1249 #else
1250 struct sockaddr_storage isa;
1251 #endif
1252 int addrlen = sizeof(isa);
1253 SOCKET t; /* socket of connection */
1254
1255 memset(&isa, 0, sizeof(isa));
1256 err = 0;
1257 t = p_accept(s->s,(struct sockaddr *)&isa,&addrlen);
1258 if (t == INVALID_SOCKET)
1259 {
1260 err = p_WSAGetLastError();
1261 if (err == WSATRY_AGAIN)
1262 break;
1263 }
1264 #ifndef NO_IPV6
1265 if (isa.ss_family == AF_INET &&
1266 s->localhost_only &&
1267 !ipv4_is_local_addr(((struct sockaddr_in *)&isa)->sin_addr)) {
1268 #else
1269 if (s->localhost_only && !ipv4_is_local_addr(isa.sin_addr)) {
1270 #endif
1271 p_closesocket(t); /* dodgy WinSock let nonlocal through */
1272 } else if (plug_accepting(s->plug, (void*)t)) {
1273 p_closesocket(t); /* denied or error */
1274 }
1275 }
1276 }
1277
1278 return 1;
1279 }
1280
1281 /*
1282 * Deal with socket errors detected in try_send().
1283 */
1284 void net_pending_errors(void)
1285 {
1286 int i;
1287 Actual_Socket s;
1288
1289 /*
1290 * This might be a fiddly business, because it's just possible
1291 * that handling a pending error on one socket might cause
1292 * others to be closed. (I can't think of any reason this might
1293 * happen in current SSH implementation, but to maintain
1294 * generality of this network layer I'll assume the worst.)
1295 *
1296 * So what we'll do is search the socket list for _one_ socket
1297 * with a pending error, and then handle it, and then search
1298 * the list again _from the beginning_. Repeat until we make a
1299 * pass with no socket errors present. That way we are
1300 * protected against the socket list changing under our feet.
1301 */
1302
1303 do {
1304 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
1305 if (s->pending_error) {
1306 /*
1307 * An error has occurred on this socket. Pass it to the
1308 * plug.
1309 */
1310 plug_closing(s->plug,
1311 winsock_error_string(s->pending_error),
1312 s->pending_error, 0);
1313 break;
1314 }
1315 }
1316 } while (s);
1317 }
1318
1319 /*
1320 * Each socket abstraction contains a `void *' private field in
1321 * which the client can keep state.
1322 */
1323 static void sk_tcp_set_private_ptr(Socket sock, void *ptr)
1324 {
1325 Actual_Socket s = (Actual_Socket) sock;
1326 s->private_ptr = ptr;
1327 }
1328
1329 static void *sk_tcp_get_private_ptr(Socket sock)
1330 {
1331 Actual_Socket s = (Actual_Socket) sock;
1332 return s->private_ptr;
1333 }
1334
1335 /*
1336 * Special error values are returned from sk_namelookup and sk_new
1337 * if there's a problem. These functions extract an error message,
1338 * or return NULL if there's no problem.
1339 */
1340 const char *sk_addr_error(SockAddr addr)
1341 {
1342 return addr->error;
1343 }
1344 static const char *sk_tcp_socket_error(Socket sock)
1345 {
1346 Actual_Socket s = (Actual_Socket) sock;
1347 return s->error;
1348 }
1349
1350 static void sk_tcp_set_frozen(Socket sock, int is_frozen)
1351 {
1352 Actual_Socket s = (Actual_Socket) sock;
1353 if (s->frozen == is_frozen)
1354 return;
1355 s->frozen = is_frozen;
1356 if (!is_frozen && s->frozen_readable) {
1357 char c;
1358 p_recv(s->s, &c, 1, MSG_PEEK);
1359 }
1360 s->frozen_readable = 0;
1361 }
1362
1363 /*
1364 * For Plink: enumerate all sockets currently active.
1365 */
1366 SOCKET first_socket(int *state)
1367 {
1368 Actual_Socket s;
1369 *state = 0;
1370 s = index234(sktree, (*state)++);
1371 return s ? s->s : INVALID_SOCKET;
1372 }
1373
1374 SOCKET next_socket(int *state)
1375 {
1376 Actual_Socket s = index234(sktree, (*state)++);
1377 return s ? s->s : INVALID_SOCKET;
1378 }
1379
1380 extern int socket_writable(SOCKET skt)
1381 {
1382 Actual_Socket s = find234(sktree, (void *)skt, cmpforsearch);
1383
1384 if (s)
1385 return bufchain_size(&s->output_data) > 0;
1386 else
1387 return 0;
1388 }
1389
1390 int net_service_lookup(char *service)
1391 {
1392 struct servent *se;
1393 se = p_getservbyname(service, NULL);
1394 if (se != NULL)
1395 return p_ntohs(se->s_port);
1396 else
1397 return 0;
1398 }
1399
1400 SockAddr platform_get_x11_unix_address(int displaynum, char **canonicalname)
1401 {
1402 SockAddr ret = snew(struct SockAddr_tag);
1403 memset(ret, 0, sizeof(struct SockAddr_tag));
1404 ret->error = "unix sockets not supported on this platform";
1405 return ret;
1406 }