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 |
58 | struct 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 */ |
71 | char oobdata[1]; |
1ad4eb6f |
72 | int sending_oob; |
4b1e8acc |
73 | int oobinline; |
2f75bae1 |
74 | }; |
75 | |
7e78000d |
76 | /* |
77 | * We used to typedef struct Socket_tag *Socket. |
78 | * |
79 | * Since we have made the networking abstraction slightly more |
80 | * abstract, Socket no longer means a tcp socket (it could mean |
81 | * an ssl socket). So now we must use Actual_Socket when we know |
82 | * we are talking about a tcp socket. |
83 | */ |
84 | typedef struct Socket_tag *Actual_Socket; |
85 | |
2f75bae1 |
86 | struct SockAddr_tag { |
87 | char *error; |
c4d8e107 |
88 | /* address family this belongs to, AF_INET for IPv4, AF_INET6 for IPv6. */ |
32874aea |
89 | int family; |
c4d8e107 |
90 | unsigned long address; /* Address IPv4 style. */ |
91 | #ifdef IPV6 |
92 | struct addrinfo *ai; /* Address IPv6 style. */ |
93 | #endif |
2f75bae1 |
94 | }; |
95 | |
2f75bae1 |
96 | static tree234 *sktree; |
97 | |
32874aea |
98 | static int cmpfortree(void *av, void *bv) |
99 | { |
100 | Actual_Socket a = (Actual_Socket) av, b = (Actual_Socket) bv; |
101 | unsigned long as = (unsigned long) a->s, bs = (unsigned long) b->s; |
102 | if (as < bs) |
103 | return -1; |
104 | if (as > bs) |
105 | return +1; |
2f75bae1 |
106 | return 0; |
107 | } |
108 | |
32874aea |
109 | static int cmpforsearch(void *av, void *bv) |
110 | { |
111 | Actual_Socket b = (Actual_Socket) bv; |
112 | unsigned long as = (unsigned long) av, bs = (unsigned long) b->s; |
113 | if (as < bs) |
114 | return -1; |
115 | if (as > bs) |
116 | return +1; |
2f75bae1 |
117 | return 0; |
118 | } |
119 | |
32874aea |
120 | void sk_init(void) |
121 | { |
2f75bae1 |
122 | sktree = newtree234(cmpfortree); |
123 | } |
124 | |
32874aea |
125 | char *winsock_error_string(int error) |
126 | { |
e74fbad9 |
127 | switch (error) { |
32874aea |
128 | case WSAEACCES: |
129 | return "Network error: Permission denied"; |
130 | case WSAEADDRINUSE: |
131 | return "Network error: Address already in use"; |
132 | case WSAEADDRNOTAVAIL: |
133 | return "Network error: Cannot assign requested address"; |
134 | case WSAEAFNOSUPPORT: |
135 | return |
136 | "Network error: Address family not supported by protocol family"; |
137 | case WSAEALREADY: |
138 | return "Network error: Operation already in progress"; |
139 | case WSAECONNABORTED: |
140 | return "Network error: Software caused connection abort"; |
141 | case WSAECONNREFUSED: |
142 | return "Network error: Connection refused"; |
143 | case WSAECONNRESET: |
144 | return "Network error: Connection reset by peer"; |
145 | case WSAEDESTADDRREQ: |
146 | return "Network error: Destination address required"; |
147 | case WSAEFAULT: |
148 | return "Network error: Bad address"; |
149 | case WSAEHOSTDOWN: |
150 | return "Network error: Host is down"; |
151 | case WSAEHOSTUNREACH: |
152 | return "Network error: No route to host"; |
153 | case WSAEINPROGRESS: |
154 | return "Network error: Operation now in progress"; |
155 | case WSAEINTR: |
156 | return "Network error: Interrupted function call"; |
157 | case WSAEINVAL: |
158 | return "Network error: Invalid argument"; |
159 | case WSAEISCONN: |
160 | return "Network error: Socket is already connected"; |
161 | case WSAEMFILE: |
162 | return "Network error: Too many open files"; |
163 | case WSAEMSGSIZE: |
164 | return "Network error: Message too long"; |
165 | case WSAENETDOWN: |
166 | return "Network error: Network is down"; |
167 | case WSAENETRESET: |
168 | return "Network error: Network dropped connection on reset"; |
169 | case WSAENETUNREACH: |
170 | return "Network error: Network is unreachable"; |
171 | case WSAENOBUFS: |
172 | return "Network error: No buffer space available"; |
173 | case WSAENOPROTOOPT: |
174 | return "Network error: Bad protocol option"; |
175 | case WSAENOTCONN: |
176 | return "Network error: Socket is not connected"; |
177 | case WSAENOTSOCK: |
178 | return "Network error: Socket operation on non-socket"; |
179 | case WSAEOPNOTSUPP: |
180 | return "Network error: Operation not supported"; |
181 | case WSAEPFNOSUPPORT: |
182 | return "Network error: Protocol family not supported"; |
183 | case WSAEPROCLIM: |
184 | return "Network error: Too many processes"; |
185 | case WSAEPROTONOSUPPORT: |
186 | return "Network error: Protocol not supported"; |
187 | case WSAEPROTOTYPE: |
188 | return "Network error: Protocol wrong type for socket"; |
189 | case WSAESHUTDOWN: |
190 | return "Network error: Cannot send after socket shutdown"; |
191 | case WSAESOCKTNOSUPPORT: |
192 | return "Network error: Socket type not supported"; |
193 | case WSAETIMEDOUT: |
194 | return "Network error: Connection timed out"; |
195 | case WSAEWOULDBLOCK: |
196 | return "Network error: Resource temporarily unavailable"; |
197 | case WSAEDISCON: |
198 | return "Network error: Graceful shutdown in progress"; |
199 | default: |
200 | return "Unknown network error"; |
e74fbad9 |
201 | } |
202 | } |
203 | |
c4d8e107 |
204 | SockAddr sk_namelookup(char *host, char **canonicalname) |
205 | { |
2f75bae1 |
206 | SockAddr ret = smalloc(sizeof(struct SockAddr_tag)); |
207 | unsigned long a; |
c4d8e107 |
208 | struct hostent *h = NULL; |
6e1ebb76 |
209 | char realhost[8192]; |
2f75bae1 |
210 | |
c4d8e107 |
211 | /* Clear the structure and default to IPv4. */ |
212 | memset(ret, 0, sizeof(struct SockAddr_tag)); |
213 | ret->family = 0; /* We set this one when we have resolved the host. */ |
6e1ebb76 |
214 | *realhost = '\0'; |
c4d8e107 |
215 | |
32874aea |
216 | if ((a = inet_addr(host)) == (unsigned long) INADDR_NONE) { |
c4d8e107 |
217 | #ifdef IPV6 |
218 | |
219 | /* Try to get the getaddrinfo() function from wship6.dll */ |
220 | /* This way one doesn't need to have IPv6 dll's to use PuTTY and |
221 | * it will fallback to IPv4. */ |
32874aea |
222 | typedef int (CALLBACK * FGETADDRINFO) (const char *nodename, |
223 | const char *servname, |
224 | const struct addrinfo * |
225 | hints, |
226 | struct addrinfo ** res); |
c4d8e107 |
227 | FGETADDRINFO fGetAddrInfo = NULL; |
228 | |
229 | HINSTANCE dllWSHIP6 = LoadLibrary("wship6.dll"); |
230 | if (dllWSHIP6) |
32874aea |
231 | fGetAddrInfo = (FGETADDRINFO) GetProcAddress(dllWSHIP6, |
232 | "getaddrinfo"); |
c4d8e107 |
233 | |
234 | /* |
235 | * Use fGetAddrInfo when it's available (which usually also |
236 | * means IPv6 is installed...) |
237 | */ |
32874aea |
238 | if (fGetAddrInfo) { |
c4d8e107 |
239 | /*debug(("Resolving \"%s\" with getaddrinfo() (IPv4+IPv6 capable)...\n", host)); */ |
240 | if (fGetAddrInfo(host, NULL, NULL, &ret->ai) == 0) |
241 | ret->family = ret->ai->ai_family; |
32874aea |
242 | } else |
c4d8e107 |
243 | #endif |
32874aea |
244 | { |
c4d8e107 |
245 | /* |
246 | * Otherwise use the IPv4-only gethostbyname... |
247 | * (NOTE: we don't use gethostbyname as a |
248 | * fallback!) |
249 | */ |
32874aea |
250 | if (ret->family == 0) { |
251 | /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */ |
2d466ffd |
252 | if ( (h = gethostbyname(host)) ) |
32874aea |
253 | ret->family = AF_INET; |
254 | } |
c4d8e107 |
255 | } |
256 | /*debug(("Done resolving...(family is %d) AF_INET = %d, AF_INET6 = %d\n", ret->family, AF_INET, AF_INET6)); */ |
257 | |
32874aea |
258 | if (ret->family == 0) { |
2f75bae1 |
259 | DWORD err = WSAGetLastError(); |
c4d8e107 |
260 | ret->error = (err == WSAENETDOWN ? "Network is down" : |
32874aea |
261 | err == |
262 | WSAHOST_NOT_FOUND ? "Host does not exist" : err |
263 | == WSATRY_AGAIN ? "Host not found" : |
c4d8e107 |
264 | #ifdef IPV6 |
265 | fGetAddrInfo ? "getaddrinfo: unknown error" : |
266 | #endif |
267 | "gethostbyname: unknown error"); |
268 | #ifdef DEBUG |
269 | { |
270 | LPVOID lpMsgBuf; |
32874aea |
271 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
272 | FORMAT_MESSAGE_FROM_SYSTEM | |
273 | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, |
274 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
275 | (LPTSTR) & lpMsgBuf, 0, NULL); |
276 | /*debug(("Error %ld: %s (h=%lx)\n", err, lpMsgBuf, h)); */ |
c4d8e107 |
277 | /* Free the buffer. */ |
278 | LocalFree(lpMsgBuf); |
279 | } |
280 | #endif |
32874aea |
281 | } else { |
c4d8e107 |
282 | ret->error = NULL; |
283 | |
284 | #ifdef IPV6 |
285 | /* If we got an address info use that... */ |
32874aea |
286 | if (ret->ai) { |
287 | typedef int (CALLBACK * FGETNAMEINFO) |
288 | (const struct sockaddr FAR * sa, socklen_t salen, |
289 | char FAR * host, size_t hostlen, char FAR * serv, |
290 | size_t servlen, int flags); |
c4d8e107 |
291 | FGETNAMEINFO fGetNameInfo = NULL; |
292 | |
293 | /* Are we in IPv4 fallback mode? */ |
294 | /* We put the IPv4 address into the a variable so we can further-on use the IPv4 code... */ |
295 | if (ret->family == AF_INET) |
32874aea |
296 | memcpy(&a, |
297 | (char *) &((SOCKADDR_IN *) ret->ai-> |
298 | ai_addr)->sin_addr, sizeof(a)); |
c4d8e107 |
299 | |
300 | /* Now let's find that canonicalname... */ |
32874aea |
301 | if ((dllWSHIP6) |
302 | && (fGetNameInfo = |
303 | (FGETNAMEINFO) GetProcAddress(dllWSHIP6, |
304 | "getnameinfo"))) { |
305 | if (fGetNameInfo |
306 | ((struct sockaddr *) ret->ai->ai_addr, |
307 | ret->family == |
308 | AF_INET ? sizeof(SOCKADDR_IN) : |
6e1ebb76 |
309 | sizeof(SOCKADDR_IN6), realhost, |
310 | sizeof(realhost), NULL, 0, 0) != 0) { |
311 | strncpy(realhost, host, sizeof(realhost)); |
c4d8e107 |
312 | } |
313 | } |
314 | } |
315 | /* We used the IPv4-only gethostbyname()... */ |
316 | else |
c4d8e107 |
317 | #endif |
32874aea |
318 | { |
c4d8e107 |
319 | memcpy(&a, h->h_addr, sizeof(a)); |
320 | /* This way we are always sure the h->h_name is valid :) */ |
6e1ebb76 |
321 | strncpy(realhost, h->h_name, sizeof(realhost)); |
c4d8e107 |
322 | } |
c4d8e107 |
323 | } |
324 | #ifdef IPV6 |
325 | FreeLibrary(dllWSHIP6); |
326 | #endif |
32874aea |
327 | } else { |
87ed061f |
328 | /* |
329 | * This must be a numeric IPv4 address because it caused a |
330 | * success return from inet_addr. |
331 | */ |
32874aea |
332 | ret->family = AF_INET; |
6e1ebb76 |
333 | strncpy(realhost, host, sizeof(realhost)); |
2f75bae1 |
334 | } |
335 | ret->address = ntohl(a); |
6e1ebb76 |
336 | realhost[lenof(realhost)-1] = '\0'; |
337 | *canonicalname = smalloc(1+strlen(realhost)); |
338 | strcpy(*canonicalname, realhost); |
2f75bae1 |
339 | return ret; |
340 | } |
341 | |
3ad9d396 |
342 | void sk_getaddr(SockAddr addr, char *buf, int buflen) |
343 | { |
344 | #ifdef IPV6 |
345 | if (addr->family == AF_INET) { |
346 | #endif |
347 | struct in_addr a; |
348 | a.s_addr = htonl(addr->address); |
349 | strncpy(buf, inet_ntoa(a), buflen); |
350 | #ifdef IPV6 |
351 | } else { |
352 | FIXME; /* I don't know how to get a text form of an IPv6 address. */ |
353 | } |
354 | #endif |
355 | } |
356 | |
32874aea |
357 | void sk_addr_free(SockAddr addr) |
358 | { |
2f75bae1 |
359 | sfree(addr); |
360 | } |
361 | |
32874aea |
362 | static Plug sk_tcp_plug(Socket sock, Plug p) |
363 | { |
7e78000d |
364 | Actual_Socket s = (Actual_Socket) sock; |
365 | Plug ret = s->plug; |
32874aea |
366 | if (p) |
367 | s->plug = p; |
7e78000d |
368 | return ret; |
369 | } |
370 | |
32874aea |
371 | static void sk_tcp_flush(Socket s) |
372 | { |
7e78000d |
373 | /* |
374 | * We send data to the socket as soon as we can anyway, |
375 | * so we don't need to do anything here. :-) |
376 | */ |
377 | } |
378 | |
2d466ffd |
379 | static void sk_tcp_close(Socket s); |
5471d09a |
380 | static int sk_tcp_write(Socket s, char *data, int len); |
381 | static int sk_tcp_write_oob(Socket s, char *data, int len); |
2d466ffd |
382 | static char *sk_tcp_socket_error(Socket s); |
7e78000d |
383 | |
d74d141c |
384 | extern char *do_select(SOCKET skt, int startup); |
385 | |
386 | Socket sk_register(void *sock, Plug plug) |
387 | { |
388 | static struct socket_function_table fn_table = { |
389 | sk_tcp_plug, |
390 | sk_tcp_close, |
391 | sk_tcp_write, |
392 | sk_tcp_write_oob, |
393 | sk_tcp_flush, |
394 | sk_tcp_socket_error |
395 | }; |
396 | |
397 | DWORD err; |
398 | char *errstr; |
399 | Actual_Socket ret; |
400 | |
401 | /* |
402 | * Create Socket structure. |
403 | */ |
404 | ret = smalloc(sizeof(struct Socket_tag)); |
405 | ret->fn = &fn_table; |
406 | ret->error = NULL; |
407 | ret->plug = plug; |
5471d09a |
408 | bufchain_init(&ret->output_data); |
d74d141c |
409 | ret->writable = 1; /* to start with */ |
410 | ret->sending_oob = 0; |
411 | ret->frozen = 1; |
5471d09a |
412 | ret->frozen_readable = 0; |
d74d141c |
413 | |
414 | ret->s = (SOCKET)sock; |
415 | |
416 | if (ret->s == INVALID_SOCKET) { |
417 | err = WSAGetLastError(); |
418 | ret->error = winsock_error_string(err); |
419 | return (Socket) ret; |
420 | } |
421 | |
422 | ret->oobinline = 0; |
423 | |
424 | /* Set up a select mechanism. This could be an AsyncSelect on a |
425 | * window, or an EventSelect on an event object. */ |
426 | errstr = do_select(ret->s, 1); |
427 | if (errstr) { |
428 | ret->error = errstr; |
429 | return (Socket) ret; |
430 | } |
431 | |
432 | add234(sktree, ret); |
433 | |
434 | return (Socket) ret; |
435 | } |
436 | |
4b1e8acc |
437 | Socket sk_new(SockAddr addr, int port, int privport, int oobinline, |
32874aea |
438 | Plug plug) |
7e78000d |
439 | { |
440 | static struct socket_function_table fn_table = { |
441 | sk_tcp_plug, |
442 | sk_tcp_close, |
443 | sk_tcp_write, |
444 | sk_tcp_write_oob, |
445 | sk_tcp_flush, |
446 | sk_tcp_socket_error |
447 | }; |
448 | |
2f75bae1 |
449 | SOCKET s; |
c4d8e107 |
450 | #ifdef IPV6 |
451 | SOCKADDR_IN6 a6; |
452 | #endif |
2f75bae1 |
453 | SOCKADDR_IN a; |
454 | DWORD err; |
455 | char *errstr; |
7e78000d |
456 | Actual_Socket ret; |
c91409da |
457 | short localport; |
2f75bae1 |
458 | |
459 | /* |
460 | * Create Socket structure. |
461 | */ |
462 | ret = smalloc(sizeof(struct Socket_tag)); |
7e78000d |
463 | ret->fn = &fn_table; |
2f75bae1 |
464 | ret->error = NULL; |
7e78000d |
465 | ret->plug = plug; |
5471d09a |
466 | bufchain_init(&ret->output_data); |
3ad9d396 |
467 | ret->connected = 0; /* to start with */ |
468 | ret->writable = 0; /* to start with */ |
33232c8f |
469 | ret->sending_oob = 0; |
d74d141c |
470 | ret->frozen = 0; |
5471d09a |
471 | ret->frozen_readable = 0; |
2f75bae1 |
472 | |
473 | /* |
474 | * Open socket. |
475 | */ |
c4d8e107 |
476 | s = socket(addr->family, SOCK_STREAM, 0); |
2f75bae1 |
477 | ret->s = s; |
478 | |
479 | if (s == INVALID_SOCKET) { |
480 | err = WSAGetLastError(); |
32874aea |
481 | ret->error = winsock_error_string(err); |
7e78000d |
482 | return (Socket) ret; |
2f75bae1 |
483 | } |
4b1e8acc |
484 | |
485 | ret->oobinline = oobinline; |
486 | if (oobinline) { |
1ad4eb6f |
487 | BOOL b = TRUE; |
32874aea |
488 | setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b)); |
1ad4eb6f |
489 | } |
2f75bae1 |
490 | |
491 | /* |
492 | * Bind to local address. |
493 | */ |
c91409da |
494 | if (privport) |
32874aea |
495 | localport = 1023; /* count from 1023 downwards */ |
c4d8e107 |
496 | else |
32874aea |
497 | localport = 0; /* just use port 0 (ie winsock picks) */ |
c91409da |
498 | |
499 | /* Loop round trying to bind */ |
500 | while (1) { |
32874aea |
501 | int retcode; |
c91409da |
502 | |
503 | #ifdef IPV6 |
32874aea |
504 | if (addr->family == AF_INET6) { |
505 | memset(&a6, 0, sizeof(a6)); |
506 | a6.sin6_family = AF_INET6; |
507 | /*a6.sin6_addr = in6addr_any; *//* == 0 */ |
508 | a6.sin6_port = htons(localport); |
509 | } else |
c4d8e107 |
510 | #endif |
32874aea |
511 | { |
512 | a.sin_family = AF_INET; |
513 | a.sin_addr.s_addr = htonl(INADDR_ANY); |
514 | a.sin_port = htons(localport); |
515 | } |
c4d8e107 |
516 | #ifdef IPV6 |
32874aea |
517 | retcode = bind(s, (addr->family == AF_INET6 ? |
518 | (struct sockaddr *) &a6 : |
519 | (struct sockaddr *) &a), |
520 | (addr->family == |
521 | AF_INET6 ? sizeof(a6) : sizeof(a))); |
c4d8e107 |
522 | #else |
32874aea |
523 | retcode = bind(s, (struct sockaddr *) &a, sizeof(a)); |
c4d8e107 |
524 | #endif |
32874aea |
525 | if (retcode != SOCKET_ERROR) { |
526 | err = 0; |
527 | break; /* done */ |
528 | } else { |
529 | err = WSAGetLastError(); |
530 | if (err != WSAEADDRINUSE) /* failed, for a bad reason */ |
531 | break; |
532 | } |
533 | |
534 | if (localport == 0) |
535 | break; /* we're only looping once */ |
536 | localport--; |
537 | if (localport == 0) |
538 | break; /* we might have got to the end */ |
c91409da |
539 | } |
540 | |
32874aea |
541 | if (err) { |
c4d8e107 |
542 | ret->error = winsock_error_string(err); |
7e78000d |
543 | return (Socket) ret; |
2f75bae1 |
544 | } |
545 | |
546 | /* |
547 | * Connect to remote address. |
548 | */ |
c4d8e107 |
549 | #ifdef IPV6 |
32874aea |
550 | if (addr->family == AF_INET6) { |
551 | memset(&a, 0, sizeof(a)); |
c4d8e107 |
552 | a6.sin6_family = AF_INET6; |
32874aea |
553 | a6.sin6_port = htons((short) port); |
554 | a6.sin6_addr = |
555 | ((struct sockaddr_in6 *) addr->ai->ai_addr)->sin6_addr; |
556 | } else |
c4d8e107 |
557 | #endif |
32874aea |
558 | { |
c4d8e107 |
559 | a.sin_family = AF_INET; |
560 | a.sin_addr.s_addr = htonl(addr->address); |
32874aea |
561 | a.sin_port = htons((short) port); |
c4d8e107 |
562 | } |
3ad9d396 |
563 | |
564 | /* Set up a select mechanism. This could be an AsyncSelect on a |
565 | * window, or an EventSelect on an event object. */ |
566 | errstr = do_select(s, 1); |
567 | if (errstr) { |
568 | ret->error = errstr; |
569 | return (Socket) ret; |
570 | } |
571 | |
32874aea |
572 | if (( |
573 | #ifdef IPV6 |
574 | connect(s, ((addr->family == AF_INET6) ? |
575 | (struct sockaddr *) &a6 : (struct sockaddr *) &a), |
576 | (addr->family == AF_INET6) ? sizeof(a6) : sizeof(a)) |
c4d8e107 |
577 | #else |
32874aea |
578 | connect(s, (struct sockaddr *) &a, sizeof(a)) |
c4d8e107 |
579 | #endif |
32874aea |
580 | ) == SOCKET_ERROR) { |
2f75bae1 |
581 | err = WSAGetLastError(); |
3ad9d396 |
582 | /* |
583 | * We expect a potential EWOULDBLOCK here, because the |
584 | * chances are the front end has done a select for |
585 | * FD_CONNECT, so that connect() will complete |
586 | * asynchronously. |
587 | */ |
588 | if ( err != WSAEWOULDBLOCK ) { |
589 | ret->error = winsock_error_string(err); |
590 | return (Socket) ret; |
591 | } |
592 | } else { |
593 | /* |
594 | * If we _don't_ get EWOULDBLOCK, the connect has completed |
595 | * and we should set the socket as writable. |
596 | */ |
597 | ret->writable = 1; |
2f75bae1 |
598 | } |
599 | |
600 | add234(sktree, ret); |
601 | |
7e78000d |
602 | return (Socket) ret; |
2f75bae1 |
603 | } |
604 | |
bcce45ed |
605 | Socket sk_newlistener(int port, Plug plug, int local_host_only) |
d74d141c |
606 | { |
607 | static struct socket_function_table fn_table = { |
608 | sk_tcp_plug, |
609 | sk_tcp_close, |
610 | sk_tcp_write, |
611 | sk_tcp_write_oob, |
612 | sk_tcp_flush, |
613 | sk_tcp_socket_error |
614 | }; |
615 | |
616 | SOCKET s; |
617 | #ifdef IPV6 |
618 | SOCKADDR_IN6 a6; |
619 | #endif |
620 | SOCKADDR_IN a; |
621 | DWORD err; |
622 | char *errstr; |
623 | Actual_Socket ret; |
624 | int retcode; |
625 | int on = 1; |
626 | |
627 | /* |
628 | * Create Socket structure. |
629 | */ |
630 | ret = smalloc(sizeof(struct Socket_tag)); |
631 | ret->fn = &fn_table; |
632 | ret->error = NULL; |
633 | ret->plug = plug; |
5471d09a |
634 | bufchain_init(&ret->output_data); |
d74d141c |
635 | ret->writable = 0; /* to start with */ |
636 | ret->sending_oob = 0; |
637 | ret->frozen = 0; |
5471d09a |
638 | ret->frozen_readable = 0; |
d74d141c |
639 | |
640 | /* |
641 | * Open socket. |
642 | */ |
643 | s = socket(AF_INET, SOCK_STREAM, 0); |
644 | ret->s = s; |
645 | |
646 | if (s == INVALID_SOCKET) { |
647 | err = WSAGetLastError(); |
648 | ret->error = winsock_error_string(err); |
649 | return (Socket) ret; |
650 | } |
651 | |
652 | ret->oobinline = 0; |
653 | |
654 | |
655 | setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on)); |
656 | |
657 | |
658 | #ifdef IPV6 |
659 | if (addr->family == AF_INET6) { |
660 | memset(&a6, 0, sizeof(a6)); |
661 | a6.sin6_family = AF_INET6; |
bcce45ed |
662 | if (local_host_only) |
663 | a6.sin6_addr = in6addr_loopback; |
664 | else |
665 | a6.sin6_addr = in6addr_any; |
d74d141c |
666 | a6.sin6_port = htons(port); |
667 | } else |
668 | #endif |
669 | { |
670 | a.sin_family = AF_INET; |
bcce45ed |
671 | if (local_host_only) |
672 | a.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
673 | else |
674 | a.sin_addr.s_addr = htonl(INADDR_ANY); |
d74d141c |
675 | a.sin_port = htons((short)port); |
676 | } |
677 | #ifdef IPV6 |
678 | retcode = bind(s, (addr->family == AF_INET6 ? |
679 | (struct sockaddr *) &a6 : |
680 | (struct sockaddr *) &a), |
681 | (addr->family == |
682 | AF_INET6 ? sizeof(a6) : sizeof(a))); |
683 | #else |
684 | retcode = bind(s, (struct sockaddr *) &a, sizeof(a)); |
685 | #endif |
686 | if (retcode != SOCKET_ERROR) { |
687 | err = 0; |
688 | } else { |
689 | err = WSAGetLastError(); |
690 | } |
691 | |
692 | if (err) { |
693 | ret->error = winsock_error_string(err); |
694 | return (Socket) ret; |
695 | } |
696 | |
697 | |
698 | if (listen(s, SOMAXCONN) == SOCKET_ERROR) { |
699 | closesocket(s); |
700 | ret->error = winsock_error_string(err); |
701 | return (Socket) ret; |
702 | } |
703 | |
704 | /* Set up a select mechanism. This could be an AsyncSelect on a |
705 | * window, or an EventSelect on an event object. */ |
706 | errstr = do_select(s, 1); |
707 | if (errstr) { |
708 | ret->error = errstr; |
709 | return (Socket) ret; |
710 | } |
711 | |
712 | add234(sktree, ret); |
713 | |
714 | return (Socket) ret; |
715 | } |
716 | |
32874aea |
717 | static void sk_tcp_close(Socket sock) |
718 | { |
9c964e85 |
719 | extern char *do_select(SOCKET skt, int startup); |
7e78000d |
720 | Actual_Socket s = (Actual_Socket) sock; |
9c964e85 |
721 | |
2f75bae1 |
722 | del234(sktree, s); |
723 | do_select(s->s, 0); |
724 | closesocket(s->s); |
dcbde236 |
725 | sfree(s); |
2f75bae1 |
726 | } |
727 | |
2f75bae1 |
728 | /* |
729 | * The function which tries to send on a socket once it's deemed |
730 | * writable. |
731 | */ |
32874aea |
732 | void try_send(Actual_Socket s) |
733 | { |
5471d09a |
734 | while (s->sending_oob || bufchain_size(&s->output_data) > 0) { |
2f75bae1 |
735 | int nsent; |
736 | DWORD err; |
5471d09a |
737 | void *data; |
32874aea |
738 | int len, urgentflag; |
739 | |
740 | if (s->sending_oob) { |
741 | urgentflag = MSG_OOB; |
742 | len = s->sending_oob; |
5471d09a |
743 | data = &s->oobdata; |
32874aea |
744 | } else { |
745 | urgentflag = 0; |
5471d09a |
746 | bufchain_prefix(&s->output_data, &data, &len); |
32874aea |
747 | } |
748 | |
5471d09a |
749 | nsent = send(s->s, data, len, urgentflag); |
32874aea |
750 | noise_ultralight(nsent); |
2f75bae1 |
751 | if (nsent <= 0) { |
752 | err = (nsent < 0 ? WSAGetLastError() : 0); |
e5eb3a1c |
753 | if ((err < WSABASEERR && nsent < 0) || err == WSAEWOULDBLOCK) { |
d40a94b9 |
754 | /* |
755 | * Perfectly normal: we've sent all we can for the moment. |
756 | * |
e5eb3a1c |
757 | * (Some WinSock send() implementations can return |
758 | * <0 but leave no sensible error indication - |
759 | * WSAGetLastError() is called but returns zero or |
760 | * a small number - so we check that case and treat |
761 | * it just like WSAEWOULDBLOCK.) |
d40a94b9 |
762 | */ |
2f75bae1 |
763 | s->writable = FALSE; |
32874aea |
764 | return; |
2f75bae1 |
765 | } else if (nsent == 0 || |
32874aea |
766 | err == WSAECONNABORTED || err == WSAECONNRESET) { |
767 | /* |
768 | * FIXME. This will have to be done better when we |
769 | * start managing multiple sockets (e.g. SSH port |
770 | * forwarding), because if we get CONNRESET while |
771 | * trying to write a particular forwarded socket |
772 | * then it isn't necessarily the end of the world. |
773 | * Ideally I'd like to pass the error code back to |
774 | * somewhere the next select_result() will see it, |
775 | * but that might be hard. Perhaps I should pass it |
776 | * back to be queued in the Windows front end bit. |
777 | */ |
778 | fatalbox(winsock_error_string(err)); |
2f75bae1 |
779 | } else { |
780 | fatalbox(winsock_error_string(err)); |
781 | } |
782 | } else { |
5471d09a |
783 | if (s->sending_oob) { |
784 | if (nsent < len) { |
785 | memmove(s->oobdata, s->oobdata+nsent, len-nsent); |
786 | s->sending_oob = len - nsent; |
787 | } else { |
788 | s->sending_oob = 0; |
789 | } |
790 | } else { |
791 | bufchain_consume(&s->output_data, nsent); |
2f75bae1 |
792 | } |
793 | } |
794 | } |
795 | } |
796 | |
5471d09a |
797 | static int sk_tcp_write(Socket sock, char *buf, int len) |
32874aea |
798 | { |
7e78000d |
799 | Actual_Socket s = (Actual_Socket) sock; |
800 | |
2f75bae1 |
801 | /* |
802 | * Add the data to the buffer list on the socket. |
803 | */ |
5471d09a |
804 | bufchain_add(&s->output_data, buf, len); |
2f75bae1 |
805 | |
806 | /* |
807 | * Now try sending from the start of the buffer list. |
808 | */ |
809 | if (s->writable) |
810 | try_send(s); |
5471d09a |
811 | |
812 | return bufchain_size(&s->output_data); |
2f75bae1 |
813 | } |
814 | |
5471d09a |
815 | static int sk_tcp_write_oob(Socket sock, char *buf, int len) |
32874aea |
816 | { |
7e78000d |
817 | Actual_Socket s = (Actual_Socket) sock; |
818 | |
2f75bae1 |
819 | /* |
820 | * Replace the buffer list on the socket with the data. |
821 | */ |
5471d09a |
822 | bufchain_clear(&s->output_data); |
823 | assert(len <= sizeof(s->oobdata)); |
824 | memcpy(s->oobdata, buf, len); |
2f75bae1 |
825 | s->sending_oob = len; |
826 | |
827 | /* |
828 | * Now try sending from the start of the buffer list. |
829 | */ |
830 | if (s->writable) |
831 | try_send(s); |
5471d09a |
832 | |
833 | return s->sending_oob; |
2f75bae1 |
834 | } |
835 | |
32874aea |
836 | int select_result(WPARAM wParam, LPARAM lParam) |
837 | { |
f9d3f227 |
838 | int ret, open; |
2f75bae1 |
839 | DWORD err; |
b675c612 |
840 | char buf[20480]; /* nice big buffer for plenty of speed */ |
7e78000d |
841 | Actual_Socket s; |
49bad831 |
842 | u_long atmark; |
2f75bae1 |
843 | |
844 | /* wParam is the socket itself */ |
32874aea |
845 | s = find234(sktree, (void *) wParam, cmpforsearch); |
2f75bae1 |
846 | if (!s) |
847 | return 1; /* boggle */ |
848 | |
849 | if ((err = WSAGETSELECTERROR(lParam)) != 0) { |
32874aea |
850 | /* |
851 | * An error has occurred on this socket. Pass it to the |
852 | * plug. |
853 | */ |
854 | return plug_closing(s->plug, winsock_error_string(err), err, 0); |
2f75bae1 |
855 | } |
856 | |
7d6ee6ff |
857 | noise_ultralight(lParam); |
858 | |
2f75bae1 |
859 | switch (WSAGETSELECTEVENT(lParam)) { |
3ad9d396 |
860 | case FD_CONNECT: |
861 | s->connected = s->writable = 1; |
862 | break; |
2f75bae1 |
863 | case FD_READ: |
d74d141c |
864 | /* In the case the socket is still frozen, we don't even bother */ |
5471d09a |
865 | if (s->frozen) { |
866 | s->frozen_readable = 1; |
d74d141c |
867 | break; |
5471d09a |
868 | } |
d74d141c |
869 | |
32874aea |
870 | /* |
871 | * We have received data on the socket. For an oobinline |
872 | * socket, this might be data _before_ an urgent pointer, |
873 | * in which case we send it to the back end with type==1 |
874 | * (data prior to urgent). |
875 | */ |
876 | if (s->oobinline) { |
877 | atmark = 1; |
878 | ioctlsocket(s->s, SIOCATMARK, &atmark); |
879 | /* |
880 | * Avoid checking the return value from ioctlsocket(), |
881 | * on the grounds that some WinSock wrappers don't |
882 | * support it. If it does nothing, we get atmark==1, |
883 | * which is equivalent to `no OOB pending', so the |
884 | * effect will be to non-OOB-ify any OOB data. |
885 | */ |
886 | } else |
887 | atmark = 1; |
4b1e8acc |
888 | |
2f75bae1 |
889 | ret = recv(s->s, buf, sizeof(buf), 0); |
32874aea |
890 | noise_ultralight(ret); |
2f75bae1 |
891 | if (ret < 0) { |
892 | err = WSAGetLastError(); |
893 | if (err == WSAEWOULDBLOCK) { |
894 | break; |
895 | } |
896 | } |
897 | if (ret < 0) { |
32874aea |
898 | return plug_closing(s->plug, winsock_error_string(err), err, |
899 | 0); |
7e78000d |
900 | } else if (0 == ret) { |
32874aea |
901 | return plug_closing(s->plug, NULL, 0, 0); |
2f75bae1 |
902 | } else { |
32874aea |
903 | return plug_receive(s->plug, atmark ? 0 : 1, buf, ret); |
2f75bae1 |
904 | } |
905 | break; |
906 | case FD_OOB: |
32874aea |
907 | /* |
908 | * This will only happen on a non-oobinline socket. It |
909 | * indicates that we can immediately perform an OOB read |
910 | * and get back OOB data, which we will send to the back |
911 | * end with type==2 (urgent data). |
912 | */ |
913 | ret = recv(s->s, buf, sizeof(buf), MSG_OOB); |
914 | noise_ultralight(ret); |
915 | if (ret <= 0) { |
916 | fatalbox(ret == 0 ? "Internal networking trouble" : |
917 | winsock_error_string(WSAGetLastError())); |
918 | } else { |
919 | return plug_receive(s->plug, 2, buf, ret); |
920 | } |
921 | break; |
2f75bae1 |
922 | case FD_WRITE: |
5471d09a |
923 | { |
924 | int bufsize_before, bufsize_after; |
925 | s->writable = 1; |
926 | bufsize_before = s->sending_oob + bufchain_size(&s->output_data); |
927 | try_send(s); |
928 | bufsize_after = s->sending_oob + bufchain_size(&s->output_data); |
929 | if (bufsize_after < bufsize_before) |
930 | plug_sent(s->plug, bufsize_after); |
931 | } |
2f75bae1 |
932 | break; |
933 | case FD_CLOSE: |
f9d3f227 |
934 | /* Signal a close on the socket. First read any outstanding data. */ |
32874aea |
935 | open = 1; |
936 | do { |
937 | ret = recv(s->s, buf, sizeof(buf), 0); |
938 | if (ret < 0) { |
939 | err = WSAGetLastError(); |
940 | if (err == WSAEWOULDBLOCK) |
941 | break; |
942 | return plug_closing(s->plug, winsock_error_string(err), |
943 | err, 0); |
944 | } else { |
945 | if (ret) |
946 | open &= plug_receive(s->plug, 0, buf, ret); |
947 | else |
948 | open &= plug_closing(s->plug, NULL, 0, 0); |
7e78000d |
949 | } |
f9d3f227 |
950 | } while (ret > 0); |
32874aea |
951 | return open; |
d74d141c |
952 | case FD_ACCEPT: |
953 | { |
bcce45ed |
954 | struct sockaddr isa; |
955 | int addrlen = sizeof(struct sockaddr); |
956 | SOCKET t; /* socket of connection */ |
957 | |
958 | memset(&isa, 0, sizeof(struct sockaddr)); |
959 | err = 0; |
960 | t = accept(s->s,&isa,&addrlen); |
961 | if (t == INVALID_SOCKET) |
962 | { |
963 | err = WSAGetLastError(); |
964 | if (err == WSATRY_AGAIN) |
965 | break; |
966 | } |
967 | |
968 | if (plug_accepting(s->plug, (void*)t)) { |
969 | closesocket(t); /* denied or error */ |
970 | } |
d74d141c |
971 | } |
2f75bae1 |
972 | } |
973 | |
974 | return 1; |
975 | } |
976 | |
977 | /* |
978 | * Each socket abstraction contains a `void *' private field in |
979 | * which the client can keep state. |
980 | */ |
32874aea |
981 | void sk_set_private_ptr(Socket sock, void *ptr) |
982 | { |
7e78000d |
983 | Actual_Socket s = (Actual_Socket) sock; |
2f75bae1 |
984 | s->private_ptr = ptr; |
985 | } |
32874aea |
986 | |
987 | void *sk_get_private_ptr(Socket sock) |
988 | { |
7e78000d |
989 | Actual_Socket s = (Actual_Socket) sock; |
2f75bae1 |
990 | return s->private_ptr; |
991 | } |
992 | |
993 | /* |
994 | * Special error values are returned from sk_namelookup and sk_new |
995 | * if there's a problem. These functions extract an error message, |
996 | * or return NULL if there's no problem. |
997 | */ |
32874aea |
998 | char *sk_addr_error(SockAddr addr) |
999 | { |
2f75bae1 |
1000 | return addr->error; |
1001 | } |
32874aea |
1002 | static char *sk_tcp_socket_error(Socket sock) |
1003 | { |
7e78000d |
1004 | Actual_Socket s = (Actual_Socket) sock; |
2f75bae1 |
1005 | return s->error; |
1006 | } |
1007 | |
d74d141c |
1008 | void sk_set_frozen(Socket sock, int is_frozen) |
1009 | { |
1010 | Actual_Socket s = (Actual_Socket) sock; |
5471d09a |
1011 | if (s->frozen == is_frozen) |
1012 | return; |
d74d141c |
1013 | s->frozen = is_frozen; |
5471d09a |
1014 | if (!is_frozen && s->frozen_readable) { |
d74d141c |
1015 | char c; |
1016 | recv(s->s, &c, 1, MSG_PEEK); |
1017 | } |
5471d09a |
1018 | s->frozen_readable = 0; |
d74d141c |
1019 | } |
1020 | |
2f75bae1 |
1021 | /* |
1022 | * For Plink: enumerate all sockets currently active. |
1023 | */ |
32874aea |
1024 | SOCKET first_socket(int *state) |
1025 | { |
d2371c81 |
1026 | Actual_Socket s; |
1027 | *state = 0; |
1028 | s = index234(sktree, (*state)++); |
2f75bae1 |
1029 | return s ? s->s : INVALID_SOCKET; |
1030 | } |
32874aea |
1031 | |
1032 | SOCKET next_socket(int *state) |
1033 | { |
d2371c81 |
1034 | Actual_Socket s = index234(sktree, (*state)++); |
2f75bae1 |
1035 | return s ? s->s : INVALID_SOCKET; |
1036 | } |