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