3c828ad87c8b5bf94264f7682a930a56d3beb76f
[u/mdw/putty] / winnet.c
1 /*
2 * Windows networking abstraction.
3 *
4 * Due to this clean abstraction it was possible
5 * to easily implement IPv6 support :)
6 *
7 * IPv6 patch 1 (27 October 2000) Jeroen Massar <jeroen@unfix.org>
8 * - Preliminary hacked IPv6 support.
9 * - Connecting to IPv6 address (eg fec0:4242:4242:100:2d0:b7ff:fe8f:5d42) works.
10 * - Connecting to IPv6 hostname (eg heaven.ipv6.unfix.org) works.
11 * - Compiles as either IPv4 or IPv6.
12 *
13 * IPv6 patch 2 (29 October 2000) Jeroen Massar <jeroen@unfix.org>
14 * - When compiled as IPv6 it also allows connecting to IPv4 hosts.
15 * - Added some more documentation.
16 *
17 * IPv6 patch 3 (18 November 2000) Jeroen Massar <jeroen@unfix.org>
18 * - It now supports dynamically loading the IPv6 resolver dll's.
19 * This way we should be able to distribute one (1) binary
20 * which supports both IPv4 and IPv6.
21 * - getaddrinfo() and getnameinfo() are loaded dynamicaly if possible.
22 * - in6addr_any is defined in this file so we don't need to link to wship6.lib
23 * - The patch is now more unified so that we can still
24 * remove all IPv6 support by undef'ing IPV6.
25 * But where it fallsback to IPv4 it uses the IPv4 code which is already in place...
26 * - Canonical name resolving works.
27 *
28 * IPv6 patch 4 (07 January 2001) Jeroen Massar <jeroen@unfix.org>
29 * - patch against CVS of today, will be submitted to the bugs list
30 * as a 'cvs diff -u' on Simon's request...
31 *
32 */
33
34 /*
35 * Define IPV6 to have IPv6 on-the-fly-loading support.
36 * This means that one doesn't have to have an IPv6 stack to use it.
37 * But if an IPv6 stack is found it is used with a fallback to IPv4.
38 */
39 /* #define IPV6 1 */
40
41 #ifdef IPV6
42 #include <winsock2.h>
43 #include <ws2tcpip.h>
44 #include <tpipv6.h>
45 #else
46 #include <winsock.h>
47 #endif
48 #include <windows.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51
52 #define DEFINE_PLUG_METHOD_MACROS
53 #include "putty.h"
54 #include "network.h"
55 #include "tree234.h"
56
57 #define BUFFER_GRANULE 512
58
59 struct Socket_tag {
60 struct socket_function_table *fn;
61 /* the above variable absolutely *must* be the first in this structure */
62 char *error;
63 SOCKET s;
64 Plug plug;
65 void *private_ptr;
66 struct buffer *head, *tail;
67 int writable;
68 int sending_oob;
69 int oobinline;
70 };
71
72 /*
73 * We used to typedef struct Socket_tag *Socket.
74 *
75 * Since we have made the networking abstraction slightly more
76 * abstract, Socket no longer means a tcp socket (it could mean
77 * an ssl socket). So now we must use Actual_Socket when we know
78 * we are talking about a tcp socket.
79 */
80 typedef struct Socket_tag *Actual_Socket;
81
82 struct SockAddr_tag {
83 char *error;
84 /* address family this belongs to, AF_INET for IPv4, AF_INET6 for IPv6. */
85 int family;
86 unsigned long address; /* Address IPv4 style. */
87 #ifdef IPV6
88 struct addrinfo *ai; /* Address IPv6 style. */
89 #endif
90 };
91
92 struct buffer {
93 struct buffer *next;
94 int buflen, bufpos;
95 char buf[BUFFER_GRANULE];
96 };
97
98 static tree234 *sktree;
99
100 static int cmpfortree(void *av, void *bv)
101 {
102 Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv;
103 unsigned long as = (unsigned long) a->s, bs = (unsigned long) b->s;
104 if (as < bs)
105 return -1;
106 if (as > bs)
107 return +1;
108 return 0;
109 }
110
111 static int cmpforsearch(void *av, void *bv)
112 {
113 Actual_Socket b = (Actual_Socket) bv;
114 unsigned long as = (unsigned long) av, bs = (unsigned long) b->s;
115 if (as < bs)
116 return -1;
117 if (as > bs)
118 return +1;
119 return 0;
120 }
121
122 void sk_init(void)
123 {
124 sktree = newtree234(cmpfortree);
125 }
126
127 char *winsock_error_string(int error)
128 {
129 switch (error) {
130 case WSAEACCES:
131 return "Network error: Permission denied";
132 case WSAEADDRINUSE:
133 return "Network error: Address already in use";
134 case WSAEADDRNOTAVAIL:
135 return "Network error: Cannot assign requested address";
136 case WSAEAFNOSUPPORT:
137 return
138 "Network error: Address family not supported by protocol family";
139 case WSAEALREADY:
140 return "Network error: Operation already in progress";
141 case WSAECONNABORTED:
142 return "Network error: Software caused connection abort";
143 case WSAECONNREFUSED:
144 return "Network error: Connection refused";
145 case WSAECONNRESET:
146 return "Network error: Connection reset by peer";
147 case WSAEDESTADDRREQ:
148 return "Network error: Destination address required";
149 case WSAEFAULT:
150 return "Network error: Bad address";
151 case WSAEHOSTDOWN:
152 return "Network error: Host is down";
153 case WSAEHOSTUNREACH:
154 return "Network error: No route to host";
155 case WSAEINPROGRESS:
156 return "Network error: Operation now in progress";
157 case WSAEINTR:
158 return "Network error: Interrupted function call";
159 case WSAEINVAL:
160 return "Network error: Invalid argument";
161 case WSAEISCONN:
162 return "Network error: Socket is already connected";
163 case WSAEMFILE:
164 return "Network error: Too many open files";
165 case WSAEMSGSIZE:
166 return "Network error: Message too long";
167 case WSAENETDOWN:
168 return "Network error: Network is down";
169 case WSAENETRESET:
170 return "Network error: Network dropped connection on reset";
171 case WSAENETUNREACH:
172 return "Network error: Network is unreachable";
173 case WSAENOBUFS:
174 return "Network error: No buffer space available";
175 case WSAENOPROTOOPT:
176 return "Network error: Bad protocol option";
177 case WSAENOTCONN:
178 return "Network error: Socket is not connected";
179 case WSAENOTSOCK:
180 return "Network error: Socket operation on non-socket";
181 case WSAEOPNOTSUPP:
182 return "Network error: Operation not supported";
183 case WSAEPFNOSUPPORT:
184 return "Network error: Protocol family not supported";
185 case WSAEPROCLIM:
186 return "Network error: Too many processes";
187 case WSAEPROTONOSUPPORT:
188 return "Network error: Protocol not supported";
189 case WSAEPROTOTYPE:
190 return "Network error: Protocol wrong type for socket";
191 case WSAESHUTDOWN:
192 return "Network error: Cannot send after socket shutdown";
193 case WSAESOCKTNOSUPPORT:
194 return "Network error: Socket type not supported";
195 case WSAETIMEDOUT:
196 return "Network error: Connection timed out";
197 case WSAEWOULDBLOCK:
198 return "Network error: Resource temporarily unavailable";
199 case WSAEDISCON:
200 return "Network error: Graceful shutdown in progress";
201 default:
202 return "Unknown network error";
203 }
204 }
205
206 SockAddr sk_namelookup(char *host, char **canonicalname)
207 {
208 SockAddr ret = smalloc(sizeof(struct SockAddr_tag));
209 unsigned long a;
210 struct hostent *h = NULL;
211 char realhost[8192];
212
213 /* Clear the structure and default to IPv4. */
214 memset(ret, 0, sizeof(struct SockAddr_tag));
215 ret->family = 0; /* We set this one when we have resolved the host. */
216 *realhost = '\0';
217
218 if ((a = inet_addr(host)) == (unsigned long) INADDR_NONE) {
219 #ifdef IPV6
220
221 /* Try to get the getaddrinfo() function from wship6.dll */
222 /* This way one doesn't need to have IPv6 dll's to use PuTTY and
223 * it will fallback to IPv4. */
224 typedef int (CALLBACK * FGETADDRINFO) (const char *nodename,
225 const char *servname,
226 const struct addrinfo *
227 hints,
228 struct addrinfo ** res);
229 FGETADDRINFO fGetAddrInfo = NULL;
230
231 HINSTANCE dllWSHIP6 = LoadLibrary("wship6.dll");
232 if (dllWSHIP6)
233 fGetAddrInfo = (FGETADDRINFO) GetProcAddress(dllWSHIP6,
234 "getaddrinfo");
235
236 /*
237 * Use fGetAddrInfo when it's available (which usually also
238 * means IPv6 is installed...)
239 */
240 if (fGetAddrInfo) {
241 /*debug(("Resolving \"%s\" with getaddrinfo() (IPv4+IPv6 capable)...\n", host)); */
242 if (fGetAddrInfo(host, NULL, NULL, &ret->ai) == 0)
243 ret->family = ret->ai->ai_family;
244 } else
245 #endif
246 {
247 /*
248 * Otherwise use the IPv4-only gethostbyname...
249 * (NOTE: we don't use gethostbyname as a
250 * fallback!)
251 */
252 if (ret->family == 0) {
253 /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
254 if (h = gethostbyname(host))
255 ret->family = AF_INET;
256 }
257 }
258 /*debug(("Done resolving...(family is %d) AF_INET = %d, AF_INET6 = %d\n", ret->family, AF_INET, AF_INET6)); */
259
260 if (ret->family == 0) {
261 DWORD err = WSAGetLastError();
262 ret->error = (err == WSAENETDOWN ? "Network is down" :
263 err ==
264 WSAHOST_NOT_FOUND ? "Host does not exist" : err
265 == WSATRY_AGAIN ? "Host not found" :
266 #ifdef IPV6
267 fGetAddrInfo ? "getaddrinfo: unknown error" :
268 #endif
269 "gethostbyname: unknown error");
270 #ifdef DEBUG
271 {
272 LPVOID lpMsgBuf;
273 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
274 FORMAT_MESSAGE_FROM_SYSTEM |
275 FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err,
276 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
277 (LPTSTR) & lpMsgBuf, 0, NULL);
278 /*debug(("Error %ld: %s (h=%lx)\n", err, lpMsgBuf, h)); */
279 /* Free the buffer. */
280 LocalFree(lpMsgBuf);
281 }
282 #endif
283 } else {
284 ret->error = NULL;
285
286 #ifdef IPV6
287 /* If we got an address info use that... */
288 if (ret->ai) {
289 typedef int (CALLBACK * FGETNAMEINFO)
290 (const struct sockaddr FAR * sa, socklen_t salen,
291 char FAR * host, size_t hostlen, char FAR * serv,
292 size_t servlen, int flags);
293 FGETNAMEINFO fGetNameInfo = NULL;
294
295 /* Are we in IPv4 fallback mode? */
296 /* We put the IPv4 address into the a variable so we can further-on use the IPv4 code... */
297 if (ret->family == AF_INET)
298 memcpy(&a,
299 (char *) &((SOCKADDR_IN *) ret->ai->
300 ai_addr)->sin_addr, sizeof(a));
301
302 /* Now let's find that canonicalname... */
303 if ((dllWSHIP6)
304 && (fGetNameInfo =
305 (FGETNAMEINFO) GetProcAddress(dllWSHIP6,
306 "getnameinfo"))) {
307 if (fGetNameInfo
308 ((struct sockaddr *) ret->ai->ai_addr,
309 ret->family ==
310 AF_INET ? sizeof(SOCKADDR_IN) :
311 sizeof(SOCKADDR_IN6), realhost,
312 sizeof(realhost), NULL, 0, 0) != 0) {
313 strncpy(realhost, host, sizeof(realhost));
314 }
315 }
316 }
317 /* We used the IPv4-only gethostbyname()... */
318 else
319 #endif
320 {
321 memcpy(&a, h->h_addr, sizeof(a));
322 /* This way we are always sure the h->h_name is valid :) */
323 strncpy(realhost, h->h_name, sizeof(realhost));
324 }
325 }
326 #ifdef IPV6
327 FreeLibrary(dllWSHIP6);
328 #endif
329 } else {
330 /*
331 * This must be a numeric IPv4 address because it caused a
332 * success return from inet_addr.
333 */
334 ret->family = AF_INET;
335 strncpy(realhost, host, sizeof(realhost));
336 }
337 ret->address = ntohl(a);
338 realhost[lenof(realhost)-1] = '\0';
339 *canonicalname = smalloc(1+strlen(realhost));
340 strcpy(*canonicalname, realhost);
341 return ret;
342 }
343
344 void sk_addr_free(SockAddr addr)
345 {
346 sfree(addr);
347 }
348
349 static Plug sk_tcp_plug(Socket sock, Plug p)
350 {
351 Actual_Socket s = (Actual_Socket) sock;
352 Plug ret = s->plug;
353 if (p)
354 s->plug = p;
355 return ret;
356 }
357
358 static void sk_tcp_flush(Socket s)
359 {
360 /*
361 * We send data to the socket as soon as we can anyway,
362 * so we don't need to do anything here. :-)
363 */
364 }
365
366 void sk_tcp_close(Socket s);
367 void sk_tcp_write(Socket s, char *data, int len);
368 void sk_tcp_write_oob(Socket s, char *data, int len);
369 char *sk_tcp_socket_error(Socket s);
370
371 Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
372 Plug plug)
373 {
374 static struct socket_function_table fn_table = {
375 sk_tcp_plug,
376 sk_tcp_close,
377 sk_tcp_write,
378 sk_tcp_write_oob,
379 sk_tcp_flush,
380 sk_tcp_socket_error
381 };
382
383 SOCKET s;
384 #ifdef IPV6
385 SOCKADDR_IN6 a6;
386 #endif
387 SOCKADDR_IN a;
388 DWORD err;
389 char *errstr;
390 Actual_Socket ret;
391 extern char *do_select(SOCKET skt, int startup);
392 short localport;
393
394 /*
395 * Create Socket structure.
396 */
397 ret = smalloc(sizeof(struct Socket_tag));
398 ret->fn = &fn_table;
399 ret->error = NULL;
400 ret->plug = plug;
401 ret->head = ret->tail = NULL;
402 ret->writable = 1; /* to start with */
403 ret->sending_oob = 0;
404
405 /*
406 * Open socket.
407 */
408 s = socket(addr->family, SOCK_STREAM, 0);
409 ret->s = s;
410
411 if (s == INVALID_SOCKET) {
412 err = WSAGetLastError();
413 ret->error = winsock_error_string(err);
414 return (Socket) ret;
415 }
416
417 ret->oobinline = oobinline;
418 if (oobinline) {
419 BOOL b = TRUE;
420 setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));
421 }
422
423 /*
424 * Bind to local address.
425 */
426 if (privport)
427 localport = 1023; /* count from 1023 downwards */
428 else
429 localport = 0; /* just use port 0 (ie winsock picks) */
430
431 /* Loop round trying to bind */
432 while (1) {
433 int retcode;
434
435 #ifdef IPV6
436 if (addr->family == AF_INET6) {
437 memset(&a6, 0, sizeof(a6));
438 a6.sin6_family = AF_INET6;
439 /*a6.sin6_addr = in6addr_any; *//* == 0 */
440 a6.sin6_port = htons(localport);
441 } else
442 #endif
443 {
444 a.sin_family = AF_INET;
445 a.sin_addr.s_addr = htonl(INADDR_ANY);
446 a.sin_port = htons(localport);
447 }
448 #ifdef IPV6
449 retcode = bind(s, (addr->family == AF_INET6 ?
450 (struct sockaddr *) &a6 :
451 (struct sockaddr *) &a),
452 (addr->family ==
453 AF_INET6 ? sizeof(a6) : sizeof(a)));
454 #else
455 retcode = bind(s, (struct sockaddr *) &a, sizeof(a));
456 #endif
457 if (retcode != SOCKET_ERROR) {
458 err = 0;
459 break; /* done */
460 } else {
461 err = WSAGetLastError();
462 if (err != WSAEADDRINUSE) /* failed, for a bad reason */
463 break;
464 }
465
466 if (localport == 0)
467 break; /* we're only looping once */
468 localport--;
469 if (localport == 0)
470 break; /* we might have got to the end */
471 }
472
473 if (err) {
474 ret->error = winsock_error_string(err);
475 return (Socket) ret;
476 }
477
478 /*
479 * Connect to remote address.
480 */
481 #ifdef IPV6
482 if (addr->family == AF_INET6) {
483 memset(&a, 0, sizeof(a));
484 a6.sin6_family = AF_INET6;
485 a6.sin6_port = htons((short) port);
486 a6.sin6_addr =
487 ((struct sockaddr_in6 *) addr->ai->ai_addr)->sin6_addr;
488 } else
489 #endif
490 {
491 a.sin_family = AF_INET;
492 a.sin_addr.s_addr = htonl(addr->address);
493 a.sin_port = htons((short) port);
494 }
495 if ((
496 #ifdef IPV6
497 connect(s, ((addr->family == AF_INET6) ?
498 (struct sockaddr *) &a6 : (struct sockaddr *) &a),
499 (addr->family == AF_INET6) ? sizeof(a6) : sizeof(a))
500 #else
501 connect(s, (struct sockaddr *) &a, sizeof(a))
502 #endif
503 ) == SOCKET_ERROR) {
504 err = WSAGetLastError();
505 ret->error = winsock_error_string(err);
506 return (Socket) ret;
507 }
508
509 /* Set up a select mechanism. This could be an AsyncSelect on a
510 * window, or an EventSelect on an event object. */
511 errstr = do_select(s, 1);
512 if (errstr) {
513 ret->error = errstr;
514 return (Socket) ret;
515 }
516
517 add234(sktree, ret);
518
519 return (Socket) ret;
520 }
521
522 static void sk_tcp_close(Socket sock)
523 {
524 extern char *do_select(SOCKET skt, int startup);
525 Actual_Socket s = (Actual_Socket) sock;
526
527 del234(sktree, s);
528 do_select(s->s, 0);
529 closesocket(s->s);
530 sfree(s);
531 }
532
533 /*
534 * The function which tries to send on a socket once it's deemed
535 * writable.
536 */
537 void try_send(Actual_Socket s)
538 {
539 while (s->head) {
540 int nsent;
541 DWORD err;
542 int len, urgentflag;
543
544 if (s->sending_oob) {
545 urgentflag = MSG_OOB;
546 len = s->sending_oob;
547 } else {
548 urgentflag = 0;
549 len = s->head->buflen - s->head->bufpos;
550 }
551
552 nsent =
553 send(s->s, s->head->buf + s->head->bufpos, len, urgentflag);
554 noise_ultralight(nsent);
555 if (nsent <= 0) {
556 err = (nsent < 0 ? WSAGetLastError() : 0);
557 if ((err == 0 && nsent < 0) || err == WSAEWOULDBLOCK) {
558 /*
559 * Perfectly normal: we've sent all we can for the moment.
560 *
561 * (Apparently some WinSocks can return <0 but
562 * leave no error indication - WSAGetLastError() is
563 * called but returns zero - so we check that case
564 * and treat it just like WSAEWOULDBLOCK.)
565 */
566 s->writable = FALSE;
567 return;
568 } else if (nsent == 0 ||
569 err == WSAECONNABORTED || err == WSAECONNRESET) {
570 /*
571 * FIXME. This will have to be done better when we
572 * start managing multiple sockets (e.g. SSH port
573 * forwarding), because if we get CONNRESET while
574 * trying to write a particular forwarded socket
575 * then it isn't necessarily the end of the world.
576 * Ideally I'd like to pass the error code back to
577 * somewhere the next select_result() will see it,
578 * but that might be hard. Perhaps I should pass it
579 * back to be queued in the Windows front end bit.
580 */
581 fatalbox(winsock_error_string(err));
582 } else {
583 fatalbox(winsock_error_string(err));
584 }
585 } else {
586 s->head->bufpos += nsent;
587 if (s->sending_oob)
588 s->sending_oob -= nsent;
589 if (s->head->bufpos >= s->head->buflen) {
590 struct buffer *tmp = s->head;
591 s->head = tmp->next;
592 sfree(tmp);
593 if (!s->head)
594 s->tail = NULL;
595 }
596 }
597 }
598 }
599
600 static void sk_tcp_write(Socket sock, char *buf, int len)
601 {
602 Actual_Socket s = (Actual_Socket) sock;
603
604 /*
605 * Add the data to the buffer list on the socket.
606 */
607 if (s->tail && s->tail->buflen < BUFFER_GRANULE) {
608 int copylen = min(len, BUFFER_GRANULE - s->tail->buflen);
609 memcpy(s->tail->buf + s->tail->buflen, buf, copylen);
610 buf += copylen;
611 len -= copylen;
612 s->tail->buflen += copylen;
613 }
614 while (len > 0) {
615 int grainlen = min(len, BUFFER_GRANULE);
616 struct buffer *newbuf;
617 newbuf = smalloc(sizeof(struct buffer));
618 newbuf->bufpos = 0;
619 newbuf->buflen = grainlen;
620 memcpy(newbuf->buf, buf, grainlen);
621 buf += grainlen;
622 len -= grainlen;
623 if (s->tail)
624 s->tail->next = newbuf;
625 else
626 s->head = s->tail = newbuf;
627 newbuf->next = NULL;
628 s->tail = newbuf;
629 }
630
631 /*
632 * Now try sending from the start of the buffer list.
633 */
634 if (s->writable)
635 try_send(s);
636 }
637
638 static void sk_tcp_write_oob(Socket sock, char *buf, int len)
639 {
640 Actual_Socket s = (Actual_Socket) sock;
641
642 /*
643 * Replace the buffer list on the socket with the data.
644 */
645 if (!s->head) {
646 s->head = smalloc(sizeof(struct buffer));
647 } else {
648 struct buffer *walk = s->head->next;
649 while (walk) {
650 struct buffer *tmp = walk;
651 walk = tmp->next;
652 sfree(tmp);
653 }
654 }
655 s->head->next = NULL;
656 s->tail = s->head;
657 s->head->buflen = len;
658 memcpy(s->head->buf, buf, len);
659
660 /*
661 * Set the Urgent marker.
662 */
663 s->sending_oob = len;
664
665 /*
666 * Now try sending from the start of the buffer list.
667 */
668 if (s->writable)
669 try_send(s);
670 }
671
672 int select_result(WPARAM wParam, LPARAM lParam)
673 {
674 int ret, open;
675 DWORD err;
676 char buf[20480]; /* nice big buffer for plenty of speed */
677 Actual_Socket s;
678 u_long atmark;
679
680 /* wParam is the socket itself */
681 s = find234(sktree, (void *) wParam, cmpforsearch);
682 if (!s)
683 return 1; /* boggle */
684
685 if ((err = WSAGETSELECTERROR(lParam)) != 0) {
686 /*
687 * An error has occurred on this socket. Pass it to the
688 * plug.
689 */
690 return plug_closing(s->plug, winsock_error_string(err), err, 0);
691 }
692
693 noise_ultralight(lParam);
694
695 switch (WSAGETSELECTEVENT(lParam)) {
696 case FD_READ:
697 /*
698 * We have received data on the socket. For an oobinline
699 * socket, this might be data _before_ an urgent pointer,
700 * in which case we send it to the back end with type==1
701 * (data prior to urgent).
702 */
703 if (s->oobinline) {
704 atmark = 1;
705 ioctlsocket(s->s, SIOCATMARK, &atmark);
706 /*
707 * Avoid checking the return value from ioctlsocket(),
708 * on the grounds that some WinSock wrappers don't
709 * support it. If it does nothing, we get atmark==1,
710 * which is equivalent to `no OOB pending', so the
711 * effect will be to non-OOB-ify any OOB data.
712 */
713 } else
714 atmark = 1;
715
716 ret = recv(s->s, buf, sizeof(buf), 0);
717 noise_ultralight(ret);
718 if (ret < 0) {
719 err = WSAGetLastError();
720 if (err == WSAEWOULDBLOCK) {
721 break;
722 }
723 }
724 if (ret < 0) {
725 return plug_closing(s->plug, winsock_error_string(err), err,
726 0);
727 } else if (0 == ret) {
728 return plug_closing(s->plug, NULL, 0, 0);
729 } else {
730 return plug_receive(s->plug, atmark ? 0 : 1, buf, ret);
731 }
732 break;
733 case FD_OOB:
734 /*
735 * This will only happen on a non-oobinline socket. It
736 * indicates that we can immediately perform an OOB read
737 * and get back OOB data, which we will send to the back
738 * end with type==2 (urgent data).
739 */
740 ret = recv(s->s, buf, sizeof(buf), MSG_OOB);
741 noise_ultralight(ret);
742 if (ret <= 0) {
743 fatalbox(ret == 0 ? "Internal networking trouble" :
744 winsock_error_string(WSAGetLastError()));
745 } else {
746 return plug_receive(s->plug, 2, buf, ret);
747 }
748 break;
749 case FD_WRITE:
750 s->writable = 1;
751 try_send(s);
752 break;
753 case FD_CLOSE:
754 /* Signal a close on the socket. First read any outstanding data. */
755 open = 1;
756 do {
757 ret = recv(s->s, buf, sizeof(buf), 0);
758 if (ret < 0) {
759 err = WSAGetLastError();
760 if (err == WSAEWOULDBLOCK)
761 break;
762 return plug_closing(s->plug, winsock_error_string(err),
763 err, 0);
764 } else {
765 if (ret)
766 open &= plug_receive(s->plug, 0, buf, ret);
767 else
768 open &= plug_closing(s->plug, NULL, 0, 0);
769 }
770 } while (ret > 0);
771 return open;
772 }
773
774 return 1;
775 }
776
777 /*
778 * Each socket abstraction contains a `void *' private field in
779 * which the client can keep state.
780 */
781 void sk_set_private_ptr(Socket sock, void *ptr)
782 {
783 Actual_Socket s = (Actual_Socket) sock;
784 s->private_ptr = ptr;
785 }
786
787 void *sk_get_private_ptr(Socket sock)
788 {
789 Actual_Socket s = (Actual_Socket) sock;
790 return s->private_ptr;
791 }
792
793 /*
794 * Special error values are returned from sk_namelookup and sk_new
795 * if there's a problem. These functions extract an error message,
796 * or return NULL if there's no problem.
797 */
798 char *sk_addr_error(SockAddr addr)
799 {
800 return addr->error;
801 }
802 static char *sk_tcp_socket_error(Socket sock)
803 {
804 Actual_Socket s = (Actual_Socket) sock;
805 return s->error;
806 }
807
808 /*
809 * For Plink: enumerate all sockets currently active.
810 */
811 SOCKET first_socket(int *state)
812 {
813 Actual_Socket s;
814 *state = 0;
815 s = index234(sktree, (*state)++);
816 return s ? s->s : INVALID_SOCKET;
817 }
818
819 SOCKET next_socket(int *state)
820 {
821 Actual_Socket s = index234(sktree, (*state)++);
822 return s ? s->s : INVALID_SOCKET;
823 }