Configurable TCP_NODELAY option on network connections
[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>
5471d09a 51#include <assert.h>
2f75bae1 52
7e78000d 53#define DEFINE_PLUG_METHOD_MACROS
2f75bae1 54#include "putty.h"
55#include "network.h"
56#include "tree234.h"
57
2f75bae1 58struct Socket_tag {
7e78000d 59 struct socket_function_table *fn;
60 /* the above variable absolutely *must* be the first in this structure */
2f75bae1 61 char *error;
62 SOCKET s;
7e78000d 63 Plug plug;
2f75bae1 64 void *private_ptr;
5471d09a 65 bufchain output_data;
3ad9d396 66 int connected;
2f75bae1 67 int writable;
5471d09a 68 int frozen; /* this causes readability notifications to be ignored */
69 int frozen_readable; /* this means we missed at least one readability
70 * notification while we were frozen */
bc4802a1 71 int localhost_only; /* for listening sockets */
5471d09a 72 char oobdata[1];
1ad4eb6f 73 int sending_oob;
4b1e8acc 74 int oobinline;
7732d38a 75 int pending_error; /* in case send() returns error */
2f75bae1 76};
77
7e78000d 78/*
79 * We used to typedef struct Socket_tag *Socket.
80 *
81 * Since we have made the networking abstraction slightly more
82 * abstract, Socket no longer means a tcp socket (it could mean
83 * an ssl socket). So now we must use Actual_Socket when we know
84 * we are talking about a tcp socket.
85 */
86typedef struct Socket_tag *Actual_Socket;
87
2f75bae1 88struct SockAddr_tag {
89 char *error;
c4d8e107 90 /* address family this belongs to, AF_INET for IPv4, AF_INET6 for IPv6. */
32874aea 91 int family;
c4d8e107 92 unsigned long address; /* Address IPv4 style. */
93#ifdef IPV6
94 struct addrinfo *ai; /* Address IPv6 style. */
95#endif
2f75bae1 96};
97
2f75bae1 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
3ad9d396 344void sk_getaddr(SockAddr addr, char *buf, int buflen)
345{
346#ifdef IPV6
347 if (addr->family == AF_INET) {
348#endif
349 struct in_addr a;
350 a.s_addr = htonl(addr->address);
351 strncpy(buf, inet_ntoa(a), buflen);
352#ifdef IPV6
353 } else {
354 FIXME; /* I don't know how to get a text form of an IPv6 address. */
355 }
356#endif
357}
358
32874aea 359void sk_addr_free(SockAddr addr)
360{
2f75bae1 361 sfree(addr);
362}
363
32874aea 364static Plug sk_tcp_plug(Socket sock, Plug p)
365{
7e78000d 366 Actual_Socket s = (Actual_Socket) sock;
367 Plug ret = s->plug;
32874aea 368 if (p)
369 s->plug = p;
7e78000d 370 return ret;
371}
372
32874aea 373static void sk_tcp_flush(Socket s)
374{
7e78000d 375 /*
376 * We send data to the socket as soon as we can anyway,
377 * so we don't need to do anything here. :-)
378 */
379}
380
2d466ffd 381static void sk_tcp_close(Socket s);
5471d09a 382static int sk_tcp_write(Socket s, char *data, int len);
383static int sk_tcp_write_oob(Socket s, char *data, int len);
2d466ffd 384static char *sk_tcp_socket_error(Socket s);
7e78000d 385
d74d141c 386extern char *do_select(SOCKET skt, int startup);
387
388Socket sk_register(void *sock, Plug plug)
389{
390 static struct socket_function_table fn_table = {
391 sk_tcp_plug,
392 sk_tcp_close,
393 sk_tcp_write,
394 sk_tcp_write_oob,
395 sk_tcp_flush,
396 sk_tcp_socket_error
397 };
398
399 DWORD err;
400 char *errstr;
401 Actual_Socket ret;
402
403 /*
404 * Create Socket structure.
405 */
406 ret = smalloc(sizeof(struct Socket_tag));
407 ret->fn = &fn_table;
408 ret->error = NULL;
409 ret->plug = plug;
5471d09a 410 bufchain_init(&ret->output_data);
d74d141c 411 ret->writable = 1; /* to start with */
412 ret->sending_oob = 0;
413 ret->frozen = 1;
5471d09a 414 ret->frozen_readable = 0;
bc4802a1 415 ret->localhost_only = 0; /* unused, but best init anyway */
7732d38a 416 ret->pending_error = 0;
d74d141c 417
418 ret->s = (SOCKET)sock;
419
420 if (ret->s == INVALID_SOCKET) {
421 err = WSAGetLastError();
422 ret->error = winsock_error_string(err);
423 return (Socket) ret;
424 }
425
426 ret->oobinline = 0;
427
428 /* Set up a select mechanism. This could be an AsyncSelect on a
429 * window, or an EventSelect on an event object. */
430 errstr = do_select(ret->s, 1);
431 if (errstr) {
432 ret->error = errstr;
433 return (Socket) ret;
434 }
435
436 add234(sktree, ret);
437
438 return (Socket) ret;
439}
440
4b1e8acc 441Socket sk_new(SockAddr addr, int port, int privport, int oobinline,
2184a5d9 442 int nodelay, Plug plug)
7e78000d 443{
444 static struct socket_function_table fn_table = {
445 sk_tcp_plug,
446 sk_tcp_close,
447 sk_tcp_write,
448 sk_tcp_write_oob,
449 sk_tcp_flush,
450 sk_tcp_socket_error
451 };
452
2f75bae1 453 SOCKET s;
c4d8e107 454#ifdef IPV6
455 SOCKADDR_IN6 a6;
456#endif
2f75bae1 457 SOCKADDR_IN a;
458 DWORD err;
459 char *errstr;
7e78000d 460 Actual_Socket ret;
c91409da 461 short localport;
2f75bae1 462
463 /*
464 * Create Socket structure.
465 */
466 ret = smalloc(sizeof(struct Socket_tag));
7e78000d 467 ret->fn = &fn_table;
2f75bae1 468 ret->error = NULL;
7e78000d 469 ret->plug = plug;
5471d09a 470 bufchain_init(&ret->output_data);
3ad9d396 471 ret->connected = 0; /* to start with */
472 ret->writable = 0; /* to start with */
33232c8f 473 ret->sending_oob = 0;
d74d141c 474 ret->frozen = 0;
5471d09a 475 ret->frozen_readable = 0;
bc4802a1 476 ret->localhost_only = 0; /* unused, but best init anyway */
7732d38a 477 ret->pending_error = 0;
2f75bae1 478
479 /*
480 * Open socket.
481 */
c4d8e107 482 s = socket(addr->family, SOCK_STREAM, 0);
2f75bae1 483 ret->s = s;
484
485 if (s == INVALID_SOCKET) {
486 err = WSAGetLastError();
32874aea 487 ret->error = winsock_error_string(err);
7e78000d 488 return (Socket) ret;
2f75bae1 489 }
4b1e8acc 490
491 ret->oobinline = oobinline;
492 if (oobinline) {
1ad4eb6f 493 BOOL b = TRUE;
32874aea 494 setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));
1ad4eb6f 495 }
2f75bae1 496
2184a5d9 497 if (nodelay) {
498 BOOL b = TRUE;
499 setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *) &b, sizeof(b));
500 }
501
2f75bae1 502 /*
503 * Bind to local address.
504 */
c91409da 505 if (privport)
32874aea 506 localport = 1023; /* count from 1023 downwards */
c4d8e107 507 else
32874aea 508 localport = 0; /* just use port 0 (ie winsock picks) */
c91409da 509
510 /* Loop round trying to bind */
511 while (1) {
32874aea 512 int retcode;
c91409da 513
514#ifdef IPV6
32874aea 515 if (addr->family == AF_INET6) {
516 memset(&a6, 0, sizeof(a6));
517 a6.sin6_family = AF_INET6;
518/*a6.sin6_addr = in6addr_any; *//* == 0 */
519 a6.sin6_port = htons(localport);
520 } else
c4d8e107 521#endif
32874aea 522 {
523 a.sin_family = AF_INET;
524 a.sin_addr.s_addr = htonl(INADDR_ANY);
525 a.sin_port = htons(localport);
526 }
c4d8e107 527#ifdef IPV6
32874aea 528 retcode = bind(s, (addr->family == AF_INET6 ?
529 (struct sockaddr *) &a6 :
530 (struct sockaddr *) &a),
531 (addr->family ==
532 AF_INET6 ? sizeof(a6) : sizeof(a)));
c4d8e107 533#else
32874aea 534 retcode = bind(s, (struct sockaddr *) &a, sizeof(a));
c4d8e107 535#endif
32874aea 536 if (retcode != SOCKET_ERROR) {
537 err = 0;
538 break; /* done */
539 } else {
540 err = WSAGetLastError();
541 if (err != WSAEADDRINUSE) /* failed, for a bad reason */
542 break;
543 }
544
545 if (localport == 0)
546 break; /* we're only looping once */
547 localport--;
548 if (localport == 0)
549 break; /* we might have got to the end */
c91409da 550 }
551
32874aea 552 if (err) {
c4d8e107 553 ret->error = winsock_error_string(err);
7e78000d 554 return (Socket) ret;
2f75bae1 555 }
556
557 /*
558 * Connect to remote address.
559 */
c4d8e107 560#ifdef IPV6
32874aea 561 if (addr->family == AF_INET6) {
562 memset(&a, 0, sizeof(a));
c4d8e107 563 a6.sin6_family = AF_INET6;
32874aea 564 a6.sin6_port = htons((short) port);
565 a6.sin6_addr =
566 ((struct sockaddr_in6 *) addr->ai->ai_addr)->sin6_addr;
567 } else
c4d8e107 568#endif
32874aea 569 {
c4d8e107 570 a.sin_family = AF_INET;
571 a.sin_addr.s_addr = htonl(addr->address);
32874aea 572 a.sin_port = htons((short) port);
c4d8e107 573 }
3ad9d396 574
575 /* Set up a select mechanism. This could be an AsyncSelect on a
576 * window, or an EventSelect on an event object. */
577 errstr = do_select(s, 1);
578 if (errstr) {
579 ret->error = errstr;
580 return (Socket) ret;
581 }
582
32874aea 583 if ((
584#ifdef IPV6
585 connect(s, ((addr->family == AF_INET6) ?
586 (struct sockaddr *) &a6 : (struct sockaddr *) &a),
587 (addr->family == AF_INET6) ? sizeof(a6) : sizeof(a))
c4d8e107 588#else
32874aea 589 connect(s, (struct sockaddr *) &a, sizeof(a))
c4d8e107 590#endif
32874aea 591 ) == SOCKET_ERROR) {
2f75bae1 592 err = WSAGetLastError();
3ad9d396 593 /*
594 * We expect a potential EWOULDBLOCK here, because the
595 * chances are the front end has done a select for
596 * FD_CONNECT, so that connect() will complete
597 * asynchronously.
598 */
599 if ( err != WSAEWOULDBLOCK ) {
600 ret->error = winsock_error_string(err);
601 return (Socket) ret;
602 }
603 } else {
604 /*
605 * If we _don't_ get EWOULDBLOCK, the connect has completed
606 * and we should set the socket as writable.
607 */
608 ret->writable = 1;
2f75bae1 609 }
610
611 add234(sktree, ret);
612
7e78000d 613 return (Socket) ret;
2f75bae1 614}
615
bcce45ed 616Socket sk_newlistener(int port, Plug plug, int local_host_only)
d74d141c 617{
618 static struct socket_function_table fn_table = {
619 sk_tcp_plug,
620 sk_tcp_close,
621 sk_tcp_write,
622 sk_tcp_write_oob,
623 sk_tcp_flush,
624 sk_tcp_socket_error
625 };
626
627 SOCKET s;
628#ifdef IPV6
629 SOCKADDR_IN6 a6;
630#endif
631 SOCKADDR_IN a;
632 DWORD err;
633 char *errstr;
634 Actual_Socket ret;
635 int retcode;
636 int on = 1;
637
638 /*
639 * Create Socket structure.
640 */
641 ret = smalloc(sizeof(struct Socket_tag));
642 ret->fn = &fn_table;
643 ret->error = NULL;
644 ret->plug = plug;
5471d09a 645 bufchain_init(&ret->output_data);
d74d141c 646 ret->writable = 0; /* to start with */
647 ret->sending_oob = 0;
648 ret->frozen = 0;
5471d09a 649 ret->frozen_readable = 0;
bc4802a1 650 ret->localhost_only = local_host_only;
7732d38a 651 ret->pending_error = 0;
d74d141c 652
653 /*
654 * Open socket.
655 */
656 s = socket(AF_INET, SOCK_STREAM, 0);
657 ret->s = s;
658
659 if (s == INVALID_SOCKET) {
660 err = WSAGetLastError();
661 ret->error = winsock_error_string(err);
662 return (Socket) ret;
663 }
664
665 ret->oobinline = 0;
666
667
668 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
669
670
671#ifdef IPV6
672 if (addr->family == AF_INET6) {
673 memset(&a6, 0, sizeof(a6));
674 a6.sin6_family = AF_INET6;
bcce45ed 675 if (local_host_only)
676 a6.sin6_addr = in6addr_loopback;
677 else
678 a6.sin6_addr = in6addr_any;
d74d141c 679 a6.sin6_port = htons(port);
680 } else
681#endif
682 {
683 a.sin_family = AF_INET;
bcce45ed 684 if (local_host_only)
685 a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
686 else
687 a.sin_addr.s_addr = htonl(INADDR_ANY);
d74d141c 688 a.sin_port = htons((short)port);
689 }
690#ifdef IPV6
691 retcode = bind(s, (addr->family == AF_INET6 ?
692 (struct sockaddr *) &a6 :
693 (struct sockaddr *) &a),
694 (addr->family ==
695 AF_INET6 ? sizeof(a6) : sizeof(a)));
696#else
697 retcode = bind(s, (struct sockaddr *) &a, sizeof(a));
698#endif
699 if (retcode != SOCKET_ERROR) {
700 err = 0;
701 } else {
702 err = WSAGetLastError();
703 }
704
705 if (err) {
706 ret->error = winsock_error_string(err);
707 return (Socket) ret;
708 }
709
710
711 if (listen(s, SOMAXCONN) == SOCKET_ERROR) {
712 closesocket(s);
713 ret->error = winsock_error_string(err);
714 return (Socket) ret;
715 }
716
717 /* Set up a select mechanism. This could be an AsyncSelect on a
718 * window, or an EventSelect on an event object. */
719 errstr = do_select(s, 1);
720 if (errstr) {
721 ret->error = errstr;
722 return (Socket) ret;
723 }
724
725 add234(sktree, ret);
726
727 return (Socket) ret;
728}
729
32874aea 730static void sk_tcp_close(Socket sock)
731{
9c964e85 732 extern char *do_select(SOCKET skt, int startup);
7e78000d 733 Actual_Socket s = (Actual_Socket) sock;
9c964e85 734
2f75bae1 735 del234(sktree, s);
736 do_select(s->s, 0);
737 closesocket(s->s);
dcbde236 738 sfree(s);
2f75bae1 739}
740
2f75bae1 741/*
742 * The function which tries to send on a socket once it's deemed
743 * writable.
744 */
32874aea 745void try_send(Actual_Socket s)
746{
5471d09a 747 while (s->sending_oob || bufchain_size(&s->output_data) > 0) {
2f75bae1 748 int nsent;
749 DWORD err;
5471d09a 750 void *data;
32874aea 751 int len, urgentflag;
752
753 if (s->sending_oob) {
754 urgentflag = MSG_OOB;
755 len = s->sending_oob;
5471d09a 756 data = &s->oobdata;
32874aea 757 } else {
758 urgentflag = 0;
5471d09a 759 bufchain_prefix(&s->output_data, &data, &len);
32874aea 760 }
5471d09a 761 nsent = send(s->s, data, len, urgentflag);
32874aea 762 noise_ultralight(nsent);
2f75bae1 763 if (nsent <= 0) {
764 err = (nsent < 0 ? WSAGetLastError() : 0);
e5eb3a1c 765 if ((err < WSABASEERR && nsent < 0) || err == WSAEWOULDBLOCK) {
d40a94b9 766 /*
767 * Perfectly normal: we've sent all we can for the moment.
768 *
e5eb3a1c 769 * (Some WinSock send() implementations can return
770 * <0 but leave no sensible error indication -
771 * WSAGetLastError() is called but returns zero or
772 * a small number - so we check that case and treat
773 * it just like WSAEWOULDBLOCK.)
d40a94b9 774 */
2f75bae1 775 s->writable = FALSE;
32874aea 776 return;
2f75bae1 777 } else if (nsent == 0 ||
32874aea 778 err == WSAECONNABORTED || err == WSAECONNRESET) {
779 /*
7732d38a 780 * If send() returns CONNABORTED or CONNRESET, we
781 * unfortunately can't just call plug_closing(),
782 * because it's quite likely that we're currently
783 * _in_ a call from the code we'd be calling back
784 * to, so we'd have to make half the SSH code
785 * reentrant. Instead we flag a pending error on
786 * the socket, to be dealt with (by calling
787 * plug_closing()) at some suitable future moment.
32874aea 788 */
7732d38a 789 s->pending_error = err;
790 return;
2f75bae1 791 } else {
792 fatalbox(winsock_error_string(err));
793 }
794 } else {
5471d09a 795 if (s->sending_oob) {
796 if (nsent < len) {
797 memmove(s->oobdata, s->oobdata+nsent, len-nsent);
798 s->sending_oob = len - nsent;
799 } else {
800 s->sending_oob = 0;
801 }
802 } else {
803 bufchain_consume(&s->output_data, nsent);
2f75bae1 804 }
805 }
806 }
807}
808
5471d09a 809static int sk_tcp_write(Socket sock, char *buf, int len)
32874aea 810{
7e78000d 811 Actual_Socket s = (Actual_Socket) sock;
812
2f75bae1 813 /*
814 * Add the data to the buffer list on the socket.
815 */
5471d09a 816 bufchain_add(&s->output_data, buf, len);
2f75bae1 817
818 /*
819 * Now try sending from the start of the buffer list.
820 */
821 if (s->writable)
822 try_send(s);
5471d09a 823
824 return bufchain_size(&s->output_data);
2f75bae1 825}
826
5471d09a 827static int sk_tcp_write_oob(Socket sock, char *buf, int len)
32874aea 828{
7e78000d 829 Actual_Socket s = (Actual_Socket) sock;
830
2f75bae1 831 /*
832 * Replace the buffer list on the socket with the data.
833 */
5471d09a 834 bufchain_clear(&s->output_data);
835 assert(len <= sizeof(s->oobdata));
836 memcpy(s->oobdata, buf, len);
2f75bae1 837 s->sending_oob = len;
838
839 /*
840 * Now try sending from the start of the buffer list.
841 */
842 if (s->writable)
843 try_send(s);
5471d09a 844
845 return s->sending_oob;
2f75bae1 846}
847
32874aea 848int select_result(WPARAM wParam, LPARAM lParam)
849{
f9d3f227 850 int ret, open;
2f75bae1 851 DWORD err;
b675c612 852 char buf[20480]; /* nice big buffer for plenty of speed */
7e78000d 853 Actual_Socket s;
49bad831 854 u_long atmark;
2f75bae1 855
856 /* wParam is the socket itself */
32874aea 857 s = find234(sktree, (void *) wParam, cmpforsearch);
2f75bae1 858 if (!s)
859 return 1; /* boggle */
860
861 if ((err = WSAGETSELECTERROR(lParam)) != 0) {
32874aea 862 /*
863 * An error has occurred on this socket. Pass it to the
864 * plug.
865 */
866 return plug_closing(s->plug, winsock_error_string(err), err, 0);
2f75bae1 867 }
868
7d6ee6ff 869 noise_ultralight(lParam);
870
2f75bae1 871 switch (WSAGETSELECTEVENT(lParam)) {
3ad9d396 872 case FD_CONNECT:
873 s->connected = s->writable = 1;
874 break;
2f75bae1 875 case FD_READ:
d74d141c 876 /* In the case the socket is still frozen, we don't even bother */
5471d09a 877 if (s->frozen) {
878 s->frozen_readable = 1;
d74d141c 879 break;
5471d09a 880 }
d74d141c 881
32874aea 882 /*
883 * We have received data on the socket. For an oobinline
884 * socket, this might be data _before_ an urgent pointer,
885 * in which case we send it to the back end with type==1
886 * (data prior to urgent).
887 */
888 if (s->oobinline) {
889 atmark = 1;
890 ioctlsocket(s->s, SIOCATMARK, &atmark);
891 /*
892 * Avoid checking the return value from ioctlsocket(),
893 * on the grounds that some WinSock wrappers don't
894 * support it. If it does nothing, we get atmark==1,
895 * which is equivalent to `no OOB pending', so the
896 * effect will be to non-OOB-ify any OOB data.
897 */
898 } else
899 atmark = 1;
4b1e8acc 900
2f75bae1 901 ret = recv(s->s, buf, sizeof(buf), 0);
32874aea 902 noise_ultralight(ret);
2f75bae1 903 if (ret < 0) {
904 err = WSAGetLastError();
905 if (err == WSAEWOULDBLOCK) {
906 break;
907 }
908 }
909 if (ret < 0) {
32874aea 910 return plug_closing(s->plug, winsock_error_string(err), err,
911 0);
7e78000d 912 } else if (0 == ret) {
32874aea 913 return plug_closing(s->plug, NULL, 0, 0);
2f75bae1 914 } else {
32874aea 915 return plug_receive(s->plug, atmark ? 0 : 1, buf, ret);
2f75bae1 916 }
917 break;
918 case FD_OOB:
32874aea 919 /*
920 * This will only happen on a non-oobinline socket. It
921 * indicates that we can immediately perform an OOB read
922 * and get back OOB data, which we will send to the back
923 * end with type==2 (urgent data).
924 */
925 ret = recv(s->s, buf, sizeof(buf), MSG_OOB);
926 noise_ultralight(ret);
927 if (ret <= 0) {
928 fatalbox(ret == 0 ? "Internal networking trouble" :
929 winsock_error_string(WSAGetLastError()));
930 } else {
931 return plug_receive(s->plug, 2, buf, ret);
932 }
933 break;
2f75bae1 934 case FD_WRITE:
5471d09a 935 {
936 int bufsize_before, bufsize_after;
937 s->writable = 1;
938 bufsize_before = s->sending_oob + bufchain_size(&s->output_data);
939 try_send(s);
940 bufsize_after = s->sending_oob + bufchain_size(&s->output_data);
941 if (bufsize_after < bufsize_before)
942 plug_sent(s->plug, bufsize_after);
943 }
2f75bae1 944 break;
945 case FD_CLOSE:
f9d3f227 946 /* Signal a close on the socket. First read any outstanding data. */
32874aea 947 open = 1;
948 do {
949 ret = recv(s->s, buf, sizeof(buf), 0);
950 if (ret < 0) {
951 err = WSAGetLastError();
952 if (err == WSAEWOULDBLOCK)
953 break;
954 return plug_closing(s->plug, winsock_error_string(err),
955 err, 0);
956 } else {
957 if (ret)
958 open &= plug_receive(s->plug, 0, buf, ret);
959 else
960 open &= plug_closing(s->plug, NULL, 0, 0);
7e78000d 961 }
f9d3f227 962 } while (ret > 0);
32874aea 963 return open;
d74d141c 964 case FD_ACCEPT:
965 {
bc4802a1 966 struct sockaddr_in isa;
967 int addrlen = sizeof(struct sockaddr_in);
bcce45ed 968 SOCKET t; /* socket of connection */
969
bc4802a1 970 memset(&isa, 0, sizeof(struct sockaddr_in));
bcce45ed 971 err = 0;
972 t = accept(s->s,&isa,&addrlen);
973 if (t == INVALID_SOCKET)
974 {
975 err = WSAGetLastError();
976 if (err == WSATRY_AGAIN)
977 break;
978 }
979
bc4802a1 980 if (s->localhost_only &&
981 ntohl(isa.sin_addr.s_addr) != INADDR_LOOPBACK) {
982 closesocket(t); /* dodgy WinSock let nonlocal through */
983 } else if (plug_accepting(s->plug, (void*)t)) {
bcce45ed 984 closesocket(t); /* denied or error */
985 }
d74d141c 986 }
2f75bae1 987 }
988
989 return 1;
990}
991
992/*
7732d38a 993 * Deal with socket errors detected in try_send().
994 */
995void net_pending_errors(void)
996{
997 int i;
998 Actual_Socket s;
999
1000 /*
1001 * This might be a fiddly business, because it's just possible
1002 * that handling a pending error on one socket might cause
1003 * others to be closed. (I can't think of any reason this might
1004 * happen in current SSH implementation, but to maintain
1005 * generality of this network layer I'll assume the worst.)
1006 *
1007 * So what we'll do is search the socket list for _one_ socket
1008 * with a pending error, and then handle it, and then search
1009 * the list again _from the beginning_. Repeat until we make a
1010 * pass with no socket errors present. That way we are
1011 * protected against the socket list changing under our feet.
1012 */
1013
1014 do {
1015 for (i = 0; (s = index234(sktree, i)) != NULL; i++) {
1016 if (s->pending_error) {
1017 /*
1018 * An error has occurred on this socket. Pass it to the
1019 * plug.
1020 */
1021 plug_closing(s->plug,
1022 winsock_error_string(s->pending_error),
1023 s->pending_error, 0);
1024 break;
1025 }
1026 }
1027 } while (s);
1028}
1029
1030/*
2f75bae1 1031 * Each socket abstraction contains a `void *' private field in
1032 * which the client can keep state.
1033 */
32874aea 1034void sk_set_private_ptr(Socket sock, void *ptr)
1035{
7e78000d 1036 Actual_Socket s = (Actual_Socket) sock;
2f75bae1 1037 s->private_ptr = ptr;
1038}
32874aea 1039
1040void *sk_get_private_ptr(Socket sock)
1041{
7e78000d 1042 Actual_Socket s = (Actual_Socket) sock;
2f75bae1 1043 return s->private_ptr;
1044}
1045
1046/*
1047 * Special error values are returned from sk_namelookup and sk_new
1048 * if there's a problem. These functions extract an error message,
1049 * or return NULL if there's no problem.
1050 */
32874aea 1051char *sk_addr_error(SockAddr addr)
1052{
2f75bae1 1053 return addr->error;
1054}
32874aea 1055static char *sk_tcp_socket_error(Socket sock)
1056{
7e78000d 1057 Actual_Socket s = (Actual_Socket) sock;
2f75bae1 1058 return s->error;
1059}
1060
d74d141c 1061void sk_set_frozen(Socket sock, int is_frozen)
1062{
1063 Actual_Socket s = (Actual_Socket) sock;
5471d09a 1064 if (s->frozen == is_frozen)
1065 return;
d74d141c 1066 s->frozen = is_frozen;
5471d09a 1067 if (!is_frozen && s->frozen_readable) {
d74d141c 1068 char c;
1069 recv(s->s, &c, 1, MSG_PEEK);
1070 }
5471d09a 1071 s->frozen_readable = 0;
d74d141c 1072}
1073
2f75bae1 1074/*
1075 * For Plink: enumerate all sockets currently active.
1076 */
32874aea 1077SOCKET first_socket(int *state)
1078{
d2371c81 1079 Actual_Socket s;
1080 *state = 0;
1081 s = index234(sktree, (*state)++);
2f75bae1 1082 return s ? s->s : INVALID_SOCKET;
1083}
32874aea 1084
1085SOCKET next_socket(int *state)
1086{
d2371c81 1087 Actual_Socket s = index234(sktree, (*state)++);
2f75bae1 1088 return s ? s->s : INVALID_SOCKET;
1089}