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