Placate gcc's `-Wall' warnings.
[u/mdw/putty] / winnet.c
CommitLineData
2f75bae1 1/*
2 * Windows networking abstraction.
c4d8e107 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 *
2f75bae1 32 */
33
c4d8e107 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
2f75bae1 46#include <winsock.h>
c4d8e107 47#endif
48#include <windows.h>
2f75bae1 49#include <stdio.h>
49bad831 50#include <stdlib.h>
2f75bae1 51
7e78000d 52#define DEFINE_PLUG_METHOD_MACROS
2f75bae1 53#include "putty.h"
54#include "network.h"
55#include "tree234.h"
56
b675c612 57#define BUFFER_GRANULE 512
2f75bae1 58
59struct Socket_tag {
7e78000d 60 struct socket_function_table *fn;
61 /* the above variable absolutely *must* be the first in this structure */
2f75bae1 62 char *error;
63 SOCKET s;
7e78000d 64 Plug plug;
2f75bae1 65 void *private_ptr;
66 struct buffer *head, *tail;
67 int writable;
1ad4eb6f 68 int sending_oob;
4b1e8acc 69 int oobinline;
2f75bae1 70};
71
7e78000d 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 */
80typedef struct Socket_tag *Actual_Socket;
81
2f75bae1 82struct SockAddr_tag {
83 char *error;
c4d8e107 84 /* address family this belongs to, AF_INET for IPv4, AF_INET6 for IPv6. */
32874aea 85 int family;
c4d8e107 86 unsigned long address; /* Address IPv4 style. */
87#ifdef IPV6
88 struct addrinfo *ai; /* Address IPv6 style. */
89#endif
2f75bae1 90};
91
92struct buffer {
93 struct buffer *next;
94 int buflen, bufpos;
95 char buf[BUFFER_GRANULE];
96};
97
98static tree234 *sktree;
99
32874aea 100static 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;
2f75bae1 108 return 0;
109}
110
32874aea 111static 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;
2f75bae1 119 return 0;
120}
121
32874aea 122void sk_init(void)
123{
2f75bae1 124 sktree = newtree234(cmpfortree);
125}
126
32874aea 127char *winsock_error_string(int error)
128{
e74fbad9 129 switch (error) {
32874aea 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";
e74fbad9 203 }
204}
205
c4d8e107 206SockAddr sk_namelookup(char *host, char **canonicalname)
207{
2f75bae1 208 SockAddr ret = smalloc(sizeof(struct SockAddr_tag));
209 unsigned long a;
c4d8e107 210 struct hostent *h = NULL;
6e1ebb76 211 char realhost[8192];
2f75bae1 212
c4d8e107 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. */
6e1ebb76 216 *realhost = '\0';
c4d8e107 217
32874aea 218 if ((a = inet_addr(host)) == (unsigned long) INADDR_NONE) {
c4d8e107 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. */
32874aea 224 typedef int (CALLBACK * FGETADDRINFO) (const char *nodename,
225 const char *servname,
226 const struct addrinfo *
227 hints,
228 struct addrinfo ** res);
c4d8e107 229 FGETADDRINFO fGetAddrInfo = NULL;
230
231 HINSTANCE dllWSHIP6 = LoadLibrary("wship6.dll");
232 if (dllWSHIP6)
32874aea 233 fGetAddrInfo = (FGETADDRINFO) GetProcAddress(dllWSHIP6,
234 "getaddrinfo");
c4d8e107 235
236 /*
237 * Use fGetAddrInfo when it's available (which usually also
238 * means IPv6 is installed...)
239 */
32874aea 240 if (fGetAddrInfo) {
c4d8e107 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;
32874aea 244 } else
c4d8e107 245#endif
32874aea 246 {
c4d8e107 247 /*
248 * Otherwise use the IPv4-only gethostbyname...
249 * (NOTE: we don't use gethostbyname as a
250 * fallback!)
251 */
32874aea 252 if (ret->family == 0) {
253 /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */
2d466ffd 254 if ( (h = gethostbyname(host)) )
32874aea 255 ret->family = AF_INET;
256 }
c4d8e107 257 }
258 /*debug(("Done resolving...(family is %d) AF_INET = %d, AF_INET6 = %d\n", ret->family, AF_INET, AF_INET6)); */
259
32874aea 260 if (ret->family == 0) {
2f75bae1 261 DWORD err = WSAGetLastError();
c4d8e107 262 ret->error = (err == WSAENETDOWN ? "Network is down" :
32874aea 263 err ==
264 WSAHOST_NOT_FOUND ? "Host does not exist" : err
265 == WSATRY_AGAIN ? "Host not found" :
c4d8e107 266#ifdef IPV6
267 fGetAddrInfo ? "getaddrinfo: unknown error" :
268#endif
269 "gethostbyname: unknown error");
270#ifdef DEBUG
271 {
272 LPVOID lpMsgBuf;
32874aea 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)); */
c4d8e107 279 /* Free the buffer. */
280 LocalFree(lpMsgBuf);
281 }
282#endif
32874aea 283 } else {
c4d8e107 284 ret->error = NULL;
285
286#ifdef IPV6
287 /* If we got an address info use that... */
32874aea 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);
c4d8e107 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)
32874aea 298 memcpy(&a,
299 (char *) &((SOCKADDR_IN *) ret->ai->
300 ai_addr)->sin_addr, sizeof(a));
c4d8e107 301
302 /* Now let's find that canonicalname... */
32874aea 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) :
6e1ebb76 311 sizeof(SOCKADDR_IN6), realhost,
312 sizeof(realhost), NULL, 0, 0) != 0) {
313 strncpy(realhost, host, sizeof(realhost));
c4d8e107 314 }
315 }
316 }
317 /* We used the IPv4-only gethostbyname()... */
318 else
c4d8e107 319#endif
32874aea 320 {
c4d8e107 321 memcpy(&a, h->h_addr, sizeof(a));
322 /* This way we are always sure the h->h_name is valid :) */
6e1ebb76 323 strncpy(realhost, h->h_name, sizeof(realhost));
c4d8e107 324 }
c4d8e107 325 }
326#ifdef IPV6
327 FreeLibrary(dllWSHIP6);
328#endif
32874aea 329 } else {
87ed061f 330 /*
331 * This must be a numeric IPv4 address because it caused a
332 * success return from inet_addr.
333 */
32874aea 334 ret->family = AF_INET;
6e1ebb76 335 strncpy(realhost, host, sizeof(realhost));
2f75bae1 336 }
337 ret->address = ntohl(a);
6e1ebb76 338 realhost[lenof(realhost)-1] = '\0';
339 *canonicalname = smalloc(1+strlen(realhost));
340 strcpy(*canonicalname, realhost);
2f75bae1 341 return ret;
342}
343
32874aea 344void sk_addr_free(SockAddr addr)
345{
2f75bae1 346 sfree(addr);
347}
348
32874aea 349static Plug sk_tcp_plug(Socket sock, Plug p)
350{
7e78000d 351 Actual_Socket s = (Actual_Socket) sock;
352 Plug ret = s->plug;
32874aea 353 if (p)
354 s->plug = p;
7e78000d 355 return ret;
356}
357
32874aea 358static void sk_tcp_flush(Socket s)
359{
7e78000d 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
2d466ffd 366static void sk_tcp_close(Socket s);
367static void sk_tcp_write(Socket s, char *data, int len);
368static void sk_tcp_write_oob(Socket s, char *data, int len);
369static char *sk_tcp_socket_error(Socket s);
7e78000d 370
4b1e8acc 371Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
32874aea 372 Plug plug)
7e78000d 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
2f75bae1 383 SOCKET s;
c4d8e107 384#ifdef IPV6
385 SOCKADDR_IN6 a6;
386#endif
2f75bae1 387 SOCKADDR_IN a;
388 DWORD err;
389 char *errstr;
7e78000d 390 Actual_Socket ret;
2f75bae1 391 extern char *do_select(SOCKET skt, int startup);
c91409da 392 short localport;
2f75bae1 393
394 /*
395 * Create Socket structure.
396 */
397 ret = smalloc(sizeof(struct Socket_tag));
7e78000d 398 ret->fn = &fn_table;
2f75bae1 399 ret->error = NULL;
7e78000d 400 ret->plug = plug;
2f75bae1 401 ret->head = ret->tail = NULL;
402 ret->writable = 1; /* to start with */
33232c8f 403 ret->sending_oob = 0;
2f75bae1 404
405 /*
406 * Open socket.
407 */
c4d8e107 408 s = socket(addr->family, SOCK_STREAM, 0);
2f75bae1 409 ret->s = s;
410
411 if (s == INVALID_SOCKET) {
412 err = WSAGetLastError();
32874aea 413 ret->error = winsock_error_string(err);
7e78000d 414 return (Socket) ret;
2f75bae1 415 }
4b1e8acc 416
417 ret->oobinline = oobinline;
418 if (oobinline) {
1ad4eb6f 419 BOOL b = TRUE;
32874aea 420 setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));
1ad4eb6f 421 }
2f75bae1 422
423 /*
424 * Bind to local address.
425 */
c91409da 426 if (privport)
32874aea 427 localport = 1023; /* count from 1023 downwards */
c4d8e107 428 else
32874aea 429 localport = 0; /* just use port 0 (ie winsock picks) */
c91409da 430
431 /* Loop round trying to bind */
432 while (1) {
32874aea 433 int retcode;
c91409da 434
435#ifdef IPV6
32874aea 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
c4d8e107 442#endif
32874aea 443 {
444 a.sin_family = AF_INET;
445 a.sin_addr.s_addr = htonl(INADDR_ANY);
446 a.sin_port = htons(localport);
447 }
c4d8e107 448#ifdef IPV6
32874aea 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)));
c4d8e107 454#else
32874aea 455 retcode = bind(s, (struct sockaddr *) &a, sizeof(a));
c4d8e107 456#endif
32874aea 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 */
c91409da 471 }
472
32874aea 473 if (err) {
c4d8e107 474 ret->error = winsock_error_string(err);
7e78000d 475 return (Socket) ret;
2f75bae1 476 }
477
478 /*
479 * Connect to remote address.
480 */
c4d8e107 481#ifdef IPV6
32874aea 482 if (addr->family == AF_INET6) {
483 memset(&a, 0, sizeof(a));
c4d8e107 484 a6.sin6_family = AF_INET6;
32874aea 485 a6.sin6_port = htons((short) port);
486 a6.sin6_addr =
487 ((struct sockaddr_in6 *) addr->ai->ai_addr)->sin6_addr;
488 } else
c4d8e107 489#endif
32874aea 490 {
c4d8e107 491 a.sin_family = AF_INET;
492 a.sin_addr.s_addr = htonl(addr->address);
32874aea 493 a.sin_port = htons((short) port);
c4d8e107 494 }
32874aea 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))
c4d8e107 500#else
32874aea 501 connect(s, (struct sockaddr *) &a, sizeof(a))
c4d8e107 502#endif
32874aea 503 ) == SOCKET_ERROR) {
2f75bae1 504 err = WSAGetLastError();
c4d8e107 505 ret->error = winsock_error_string(err);
7e78000d 506 return (Socket) ret;
2f75bae1 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;
7e78000d 514 return (Socket) ret;
2f75bae1 515 }
516
517 add234(sktree, ret);
518
7e78000d 519 return (Socket) ret;
2f75bae1 520}
521
32874aea 522static void sk_tcp_close(Socket sock)
523{
9c964e85 524 extern char *do_select(SOCKET skt, int startup);
7e78000d 525 Actual_Socket s = (Actual_Socket) sock;
9c964e85 526
2f75bae1 527 del234(sktree, s);
528 do_select(s->s, 0);
529 closesocket(s->s);
dcbde236 530 sfree(s);
2f75bae1 531}
532
2f75bae1 533/*
534 * The function which tries to send on a socket once it's deemed
535 * writable.
536 */
32874aea 537void try_send(Actual_Socket s)
538{
2f75bae1 539 while (s->head) {
540 int nsent;
541 DWORD err;
32874aea 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);
2f75bae1 555 if (nsent <= 0) {
556 err = (nsent < 0 ? WSAGetLastError() : 0);
32874aea 557 if ((err == 0 && nsent < 0) || err == WSAEWOULDBLOCK) {
d40a94b9 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 */
2f75bae1 566 s->writable = FALSE;
32874aea 567 return;
2f75bae1 568 } else if (nsent == 0 ||
32874aea 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));
2f75bae1 582 } else {
583 fatalbox(winsock_error_string(err));
584 }
585 } else {
586 s->head->bufpos += nsent;
32874aea 587 if (s->sending_oob)
588 s->sending_oob -= nsent;
2f75bae1 589 if (s->head->bufpos >= s->head->buflen) {
590 struct buffer *tmp = s->head;
591 s->head = tmp->next;
dcbde236 592 sfree(tmp);
2f75bae1 593 if (!s->head)
594 s->tail = NULL;
595 }
596 }
597 }
598}
599
32874aea 600static void sk_tcp_write(Socket sock, char *buf, int len)
601{
7e78000d 602 Actual_Socket s = (Actual_Socket) sock;
603
2f75bae1 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
32874aea 638static void sk_tcp_write_oob(Socket sock, char *buf, int len)
639{
7e78000d 640 Actual_Socket s = (Actual_Socket) sock;
641
2f75bae1 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 {
32874aea 648 struct buffer *walk = s->head->next;
649 while (walk) {
650 struct buffer *tmp = walk;
651 walk = tmp->next;
652 sfree(tmp);
653 }
2f75bae1 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
32874aea 672int select_result(WPARAM wParam, LPARAM lParam)
673{
f9d3f227 674 int ret, open;
2f75bae1 675 DWORD err;
b675c612 676 char buf[20480]; /* nice big buffer for plenty of speed */
7e78000d 677 Actual_Socket s;
49bad831 678 u_long atmark;
2f75bae1 679
680 /* wParam is the socket itself */
32874aea 681 s = find234(sktree, (void *) wParam, cmpforsearch);
2f75bae1 682 if (!s)
683 return 1; /* boggle */
684
685 if ((err = WSAGETSELECTERROR(lParam)) != 0) {
32874aea 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);
2f75bae1 691 }
692
7d6ee6ff 693 noise_ultralight(lParam);
694
2f75bae1 695 switch (WSAGETSELECTEVENT(lParam)) {
696 case FD_READ:
32874aea 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;
4b1e8acc 715
2f75bae1 716 ret = recv(s->s, buf, sizeof(buf), 0);
32874aea 717 noise_ultralight(ret);
2f75bae1 718 if (ret < 0) {
719 err = WSAGetLastError();
720 if (err == WSAEWOULDBLOCK) {
721 break;
722 }
723 }
724 if (ret < 0) {
32874aea 725 return plug_closing(s->plug, winsock_error_string(err), err,
726 0);
7e78000d 727 } else if (0 == ret) {
32874aea 728 return plug_closing(s->plug, NULL, 0, 0);
2f75bae1 729 } else {
32874aea 730 return plug_receive(s->plug, atmark ? 0 : 1, buf, ret);
2f75bae1 731 }
732 break;
733 case FD_OOB:
32874aea 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;
2f75bae1 749 case FD_WRITE:
750 s->writable = 1;
751 try_send(s);
752 break;
753 case FD_CLOSE:
f9d3f227 754 /* Signal a close on the socket. First read any outstanding data. */
32874aea 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);
7e78000d 769 }
f9d3f227 770 } while (ret > 0);
32874aea 771 return open;
2f75bae1 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 */
32874aea 781void sk_set_private_ptr(Socket sock, void *ptr)
782{
7e78000d 783 Actual_Socket s = (Actual_Socket) sock;
2f75bae1 784 s->private_ptr = ptr;
785}
32874aea 786
787void *sk_get_private_ptr(Socket sock)
788{
7e78000d 789 Actual_Socket s = (Actual_Socket) sock;
2f75bae1 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 */
32874aea 798char *sk_addr_error(SockAddr addr)
799{
2f75bae1 800 return addr->error;
801}
32874aea 802static char *sk_tcp_socket_error(Socket sock)
803{
7e78000d 804 Actual_Socket s = (Actual_Socket) sock;
2f75bae1 805 return s->error;
806}
807
808/*
809 * For Plink: enumerate all sockets currently active.
810 */
32874aea 811SOCKET first_socket(int *state)
812{
d2371c81 813 Actual_Socket s;
814 *state = 0;
815 s = index234(sktree, (*state)++);
2f75bae1 816 return s ? s->s : INVALID_SOCKET;
817}
32874aea 818
819SOCKET next_socket(int *state)
820{
d2371c81 821 Actual_Socket s = index234(sktree, (*state)++);
2f75bae1 822 return s ? s->s : INVALID_SOCKET;
823}