2f75bae1 |
1 | /* |
2 | * Windows networking abstraction. |
c4d8e107 |
3 | * |
4 | * Due to this clean abstraction it was possible |
5 | * to easily implement IPv6 support :) |
6 | * |
7 | * IPv6 patch 1 (27 October 2000) Jeroen Massar <jeroen@unfix.org> |
8 | * - Preliminary hacked IPv6 support. |
9 | * - Connecting to IPv6 address (eg fec0:4242:4242:100:2d0:b7ff:fe8f:5d42) works. |
10 | * - Connecting to IPv6 hostname (eg heaven.ipv6.unfix.org) works. |
11 | * - Compiles as either IPv4 or IPv6. |
12 | * |
13 | * IPv6 patch 2 (29 October 2000) Jeroen Massar <jeroen@unfix.org> |
14 | * - When compiled as IPv6 it also allows connecting to IPv4 hosts. |
15 | * - Added some more documentation. |
16 | * |
17 | * IPv6 patch 3 (18 November 2000) Jeroen Massar <jeroen@unfix.org> |
18 | * - It now supports dynamically loading the IPv6 resolver dll's. |
19 | * This way we should be able to distribute one (1) binary |
20 | * which supports both IPv4 and IPv6. |
21 | * - getaddrinfo() and getnameinfo() are loaded dynamicaly if possible. |
22 | * - in6addr_any is defined in this file so we don't need to link to wship6.lib |
23 | * - The patch is now more unified so that we can still |
24 | * remove all IPv6 support by undef'ing IPV6. |
25 | * But where it fallsback to IPv4 it uses the IPv4 code which is already in place... |
26 | * - Canonical name resolving works. |
27 | * |
28 | * IPv6 patch 4 (07 January 2001) Jeroen Massar <jeroen@unfix.org> |
29 | * - patch against CVS of today, will be submitted to the bugs list |
30 | * as a 'cvs diff -u' on Simon's request... |
31 | * |
2f75bae1 |
32 | */ |
33 | |
c4d8e107 |
34 | /* |
35 | * Define IPV6 to have IPv6 on-the-fly-loading support. |
36 | * This means that one doesn't have to have an IPv6 stack to use it. |
37 | * But if an IPv6 stack is found it is used with a fallback to IPv4. |
38 | */ |
39 | /* #define IPV6 1 */ |
40 | |
41 | #ifdef IPV6 |
42 | #include <winsock2.h> |
43 | #include <ws2tcpip.h> |
44 | #include <tpipv6.h> |
45 | #else |
2f75bae1 |
46 | #include <winsock.h> |
c4d8e107 |
47 | #endif |
48 | #include <windows.h> |
2f75bae1 |
49 | #include <stdio.h> |
49bad831 |
50 | #include <stdlib.h> |
2f75bae1 |
51 | |
52 | #include "putty.h" |
53 | #include "network.h" |
54 | #include "tree234.h" |
55 | |
56 | #define BUFFER_GRANULE 512 |
57 | |
58 | struct Socket_tag { |
59 | char *error; |
60 | SOCKET s; |
61 | sk_receiver_t receiver; |
62 | void *private_ptr; |
63 | struct buffer *head, *tail; |
64 | int writable; |
1ad4eb6f |
65 | int sending_oob; |
4b1e8acc |
66 | int oobinline; |
2f75bae1 |
67 | }; |
68 | |
69 | struct SockAddr_tag { |
70 | char *error; |
c4d8e107 |
71 | /* address family this belongs to, AF_INET for IPv4, AF_INET6 for IPv6. */ |
72 | int family; |
73 | unsigned long address; /* Address IPv4 style. */ |
74 | #ifdef IPV6 |
75 | struct addrinfo *ai; /* Address IPv6 style. */ |
76 | #endif |
77 | /* |
78 | * We need to have this lengthy enough to hold *any* hostname |
79 | * (including IPv6 reverse...) |
80 | */ |
81 | char realhost[8192]; |
2f75bae1 |
82 | }; |
83 | |
84 | struct buffer { |
85 | struct buffer *next; |
86 | int buflen, bufpos; |
87 | char buf[BUFFER_GRANULE]; |
88 | }; |
89 | |
90 | static tree234 *sktree; |
91 | |
92 | static int cmpfortree(void *av, void *bv) { |
93 | Socket a = (Socket)av, b = (Socket)bv; |
94 | unsigned long as = (unsigned long)a->s, bs = (unsigned long)b->s; |
95 | if (as < bs) return -1; |
96 | if (as > bs) return +1; |
97 | return 0; |
98 | } |
99 | |
100 | static int cmpforsearch(void *av, void *bv) { |
101 | Socket b = (Socket)bv; |
102 | unsigned long as = (unsigned long)av, bs = (unsigned long)b->s; |
103 | if (as < bs) return -1; |
104 | if (as > bs) return +1; |
105 | return 0; |
106 | } |
107 | |
108 | void sk_init(void) { |
109 | sktree = newtree234(cmpfortree); |
110 | } |
111 | |
e74fbad9 |
112 | char *winsock_error_string(int error) { |
113 | switch (error) { |
114 | case WSAEACCES: return "Network error: Permission denied"; |
115 | case WSAEADDRINUSE: return "Network error: Address already in use"; |
116 | case WSAEADDRNOTAVAIL: return "Network error: Cannot assign requested address"; |
117 | case WSAEAFNOSUPPORT: return "Network error: Address family not supported by protocol family"; |
118 | case WSAEALREADY: return "Network error: Operation already in progress"; |
119 | case WSAECONNABORTED: return "Network error: Software caused connection abort"; |
120 | case WSAECONNREFUSED: return "Network error: Connection refused"; |
121 | case WSAECONNRESET: return "Network error: Connection reset by peer"; |
122 | case WSAEDESTADDRREQ: return "Network error: Destination address required"; |
123 | case WSAEFAULT: return "Network error: Bad address"; |
124 | case WSAEHOSTDOWN: return "Network error: Host is down"; |
125 | case WSAEHOSTUNREACH: return "Network error: No route to host"; |
126 | case WSAEINPROGRESS: return "Network error: Operation now in progress"; |
127 | case WSAEINTR: return "Network error: Interrupted function call"; |
128 | case WSAEINVAL: return "Network error: Invalid argument"; |
129 | case WSAEISCONN: return "Network error: Socket is already connected"; |
130 | case WSAEMFILE: return "Network error: Too many open files"; |
131 | case WSAEMSGSIZE: return "Network error: Message too long"; |
132 | case WSAENETDOWN: return "Network error: Network is down"; |
133 | case WSAENETRESET: return "Network error: Network dropped connection on reset"; |
134 | case WSAENETUNREACH: return "Network error: Network is unreachable"; |
135 | case WSAENOBUFS: return "Network error: No buffer space available"; |
136 | case WSAENOPROTOOPT: return "Network error: Bad protocol option"; |
137 | case WSAENOTCONN: return "Network error: Socket is not connected"; |
138 | case WSAENOTSOCK: return "Network error: Socket operation on non-socket"; |
139 | case WSAEOPNOTSUPP: return "Network error: Operation not supported"; |
140 | case WSAEPFNOSUPPORT: return "Network error: Protocol family not supported"; |
141 | case WSAEPROCLIM: return "Network error: Too many processes"; |
142 | case WSAEPROTONOSUPPORT: return "Network error: Protocol not supported"; |
143 | case WSAEPROTOTYPE: return "Network error: Protocol wrong type for socket"; |
144 | case WSAESHUTDOWN: return "Network error: Cannot send after socket shutdown"; |
145 | case WSAESOCKTNOSUPPORT: return "Network error: Socket type not supported"; |
146 | case WSAETIMEDOUT: return "Network error: Connection timed out"; |
147 | case WSAEWOULDBLOCK: return "Network error: Resource temporarily unavailable"; |
148 | case WSAEDISCON: return "Network error: Graceful shutdown in progress"; |
149 | default: return "Unknown network error"; |
150 | } |
151 | } |
152 | |
c4d8e107 |
153 | SockAddr sk_namelookup(char *host, char **canonicalname) |
154 | { |
2f75bae1 |
155 | SockAddr ret = smalloc(sizeof(struct SockAddr_tag)); |
156 | unsigned long a; |
c4d8e107 |
157 | struct hostent *h = NULL; |
2f75bae1 |
158 | |
c4d8e107 |
159 | /* Clear the structure and default to IPv4. */ |
160 | memset(ret, 0, sizeof(struct SockAddr_tag)); |
161 | ret->family = 0; /* We set this one when we have resolved the host. */ |
162 | *canonicalname = ret->realhost; /* This makes sure we always have a hostname to return. */ |
163 | |
164 | if ( (a = inet_addr(host)) == (unsigned long) INADDR_NONE) |
165 | { |
166 | #ifdef IPV6 |
167 | |
168 | /* Try to get the getaddrinfo() function from wship6.dll */ |
169 | /* This way one doesn't need to have IPv6 dll's to use PuTTY and |
170 | * it will fallback to IPv4. */ |
171 | typedef int (CALLBACK* FGETADDRINFO)(const char *nodename, |
172 | const char *servname, |
173 | const struct addrinfo *hints, |
174 | struct addrinfo **res); |
175 | FGETADDRINFO fGetAddrInfo = NULL; |
176 | |
177 | HINSTANCE dllWSHIP6 = LoadLibrary("wship6.dll"); |
178 | if (dllWSHIP6) |
179 | fGetAddrInfo = (FGETADDRINFO)GetProcAddress(dllWSHIP6, |
180 | "getaddrinfo"); |
181 | |
182 | /* |
183 | * Use fGetAddrInfo when it's available (which usually also |
184 | * means IPv6 is installed...) |
185 | */ |
186 | if (fGetAddrInfo) |
187 | { |
188 | /*debug(("Resolving \"%s\" with getaddrinfo() (IPv4+IPv6 capable)...\n", host)); */ |
189 | if (fGetAddrInfo(host, NULL, NULL, &ret->ai) == 0) |
190 | ret->family = ret->ai->ai_family; |
191 | } |
192 | else |
193 | #endif |
194 | /* |
195 | * Otherwise use the IPv4-only gethostbyname... |
196 | * (NOTE: we don't use gethostbyname as a |
197 | * fallback!) |
198 | */ |
199 | if (ret->family == 0) |
200 | { |
201 | /*debug(("Resolving \"%s\" with gethostbyname() (IPv4 only)...\n", host)); */ |
202 | if (h = gethostbyname(host)) ret->family = AF_INET; |
203 | } |
204 | /*debug(("Done resolving...(family is %d) AF_INET = %d, AF_INET6 = %d\n", ret->family, AF_INET, AF_INET6)); */ |
205 | |
206 | if (ret->family == 0) |
207 | { |
2f75bae1 |
208 | DWORD err = WSAGetLastError(); |
c4d8e107 |
209 | ret->error = (err == WSAENETDOWN ? "Network is down" : |
210 | err == WSAHOST_NOT_FOUND ? "Host does not exist" : |
211 | err == WSATRY_AGAIN ? "Host not found" : |
212 | #ifdef IPV6 |
213 | fGetAddrInfo ? "getaddrinfo: unknown error" : |
214 | #endif |
215 | "gethostbyname: unknown error"); |
216 | #ifdef DEBUG |
217 | { |
218 | LPVOID lpMsgBuf; |
219 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL); |
bc51302a |
220 | /*debug(("Error %ld: %s (h=%lx)\n", err, lpMsgBuf, h));*/ |
c4d8e107 |
221 | /* Free the buffer. */ |
222 | LocalFree(lpMsgBuf); |
223 | } |
224 | #endif |
2f75bae1 |
225 | } |
c4d8e107 |
226 | else |
227 | { |
228 | ret->error = NULL; |
229 | |
230 | #ifdef IPV6 |
231 | /* If we got an address info use that... */ |
232 | if (ret->ai) |
233 | { |
234 | typedef int (CALLBACK* FGETNAMEINFO) |
235 | (const struct sockaddr FAR *sa, socklen_t salen, |
236 | char FAR * host, size_t hostlen, char FAR * serv, |
237 | size_t servlen, int flags); |
238 | FGETNAMEINFO fGetNameInfo = NULL; |
239 | |
240 | /* Are we in IPv4 fallback mode? */ |
241 | /* We put the IPv4 address into the a variable so we can further-on use the IPv4 code... */ |
242 | if (ret->family == AF_INET) |
243 | memcpy(&a, (char *)&((SOCKADDR_IN *)ret->ai->ai_addr)->sin_addr, sizeof(a)); |
244 | |
245 | /* Now let's find that canonicalname... */ |
246 | if ((dllWSHIP6) && (fGetNameInfo = (FGETNAMEINFO)GetProcAddress(dllWSHIP6, "getnameinfo"))) |
247 | { |
248 | if (fGetNameInfo((struct sockaddr *)ret->ai->ai_addr, |
249 | ret->family == AF_INET ? |
250 | sizeof(SOCKADDR_IN) : |
251 | sizeof(SOCKADDR_IN6), ret->realhost, |
252 | sizeof(ret->realhost), NULL, |
253 | 0, 0) != 0) |
254 | { |
255 | strncpy(ret->realhost, host, |
256 | sizeof(ret->realhost)); |
257 | } |
258 | } |
259 | } |
260 | /* We used the IPv4-only gethostbyname()... */ |
261 | else |
262 | { |
263 | #endif |
264 | memcpy(&a, h->h_addr, sizeof(a)); |
265 | /* This way we are always sure the h->h_name is valid :) */ |
266 | strncpy(ret->realhost, h->h_name, sizeof(ret->realhost)); |
267 | #ifdef IPV6 |
268 | } |
269 | #endif |
270 | } |
271 | #ifdef IPV6 |
272 | FreeLibrary(dllWSHIP6); |
273 | #endif |
274 | } |
275 | else |
276 | { |
9d47d0ea |
277 | /* |
278 | * Hack inserted to deal with problems with numeric IPs. |
279 | * FIXME: how will this work in IPv6? |
280 | */ |
281 | ret->family = AF_INET; |
2f75bae1 |
282 | *canonicalname = host; |
283 | } |
284 | ret->address = ntohl(a); |
2f75bae1 |
285 | return ret; |
286 | } |
287 | |
288 | void sk_addr_free(SockAddr addr) { |
289 | sfree(addr); |
290 | } |
291 | |
4b1e8acc |
292 | Socket sk_new(SockAddr addr, int port, int privport, int oobinline, |
293 | sk_receiver_t receiver) { |
2f75bae1 |
294 | SOCKET s; |
c4d8e107 |
295 | #ifdef IPV6 |
296 | SOCKADDR_IN6 a6; |
297 | #endif |
2f75bae1 |
298 | SOCKADDR_IN a; |
299 | DWORD err; |
300 | char *errstr; |
301 | Socket ret; |
302 | extern char *do_select(SOCKET skt, int startup); |
c91409da |
303 | short localport; |
2f75bae1 |
304 | |
305 | /* |
306 | * Create Socket structure. |
307 | */ |
308 | ret = smalloc(sizeof(struct Socket_tag)); |
309 | ret->error = NULL; |
310 | ret->receiver = receiver; |
311 | ret->head = ret->tail = NULL; |
312 | ret->writable = 1; /* to start with */ |
33232c8f |
313 | ret->sending_oob = 0; |
2f75bae1 |
314 | |
315 | /* |
316 | * Open socket. |
317 | */ |
c4d8e107 |
318 | s = socket(addr->family, SOCK_STREAM, 0); |
2f75bae1 |
319 | ret->s = s; |
320 | |
321 | if (s == INVALID_SOCKET) { |
322 | err = WSAGetLastError(); |
e74fbad9 |
323 | ret->error = winsock_error_string(err); |
2f75bae1 |
324 | return ret; |
325 | } |
4b1e8acc |
326 | |
327 | ret->oobinline = oobinline; |
328 | if (oobinline) { |
1ad4eb6f |
329 | BOOL b = TRUE; |
330 | setsockopt (s, SOL_SOCKET, SO_OOBINLINE, (void *)&b, sizeof(b)); |
331 | } |
2f75bae1 |
332 | |
333 | /* |
334 | * Bind to local address. |
335 | */ |
c91409da |
336 | if (privport) |
337 | localport = 1023; /* count from 1023 downwards */ |
c4d8e107 |
338 | else |
c91409da |
339 | localport = 0; /* just use port 0 (ie winsock picks) */ |
340 | |
341 | /* Loop round trying to bind */ |
342 | while (1) { |
343 | int retcode; |
344 | |
345 | #ifdef IPV6 |
346 | if (addr->family == AF_INET6) |
347 | { |
348 | memset(&a6,0,sizeof(a6)); |
349 | a6.sin6_family = AF_INET6; |
350 | /*a6.sin6_addr = in6addr_any;*/ /* == 0 */ |
351 | a6.sin6_port = htons(localport); |
352 | } |
353 | else |
354 | { |
c4d8e107 |
355 | #endif |
c91409da |
356 | a.sin_family = AF_INET; |
357 | a.sin_addr.s_addr = htonl(INADDR_ANY); |
358 | a.sin_port = htons(localport); |
c4d8e107 |
359 | #ifdef IPV6 |
c91409da |
360 | } |
361 | retcode = bind (s, (addr->family == AF_INET6 ? |
362 | (struct sockaddr *)&a6 : |
363 | (struct sockaddr *)&a), |
364 | (addr->family == AF_INET6 ? sizeof(a6) : sizeof(a))); |
c4d8e107 |
365 | #else |
c91409da |
366 | retcode = bind (s, (struct sockaddr *)&a, sizeof(a)); |
c4d8e107 |
367 | #endif |
c91409da |
368 | if (retcode != SOCKET_ERROR) { |
369 | err = 0; |
370 | break; /* done */ |
371 | } else { |
372 | err = WSAGetLastError(); |
373 | if (err != WSAEADDRINUSE) /* failed, for a bad reason */ |
374 | break; |
375 | } |
376 | |
377 | if (localport == 0) |
378 | break; /* we're only looping once */ |
379 | localport--; |
380 | if (localport == 0) |
381 | break; /* we might have got to the end */ |
382 | } |
383 | |
384 | if (err) |
c4d8e107 |
385 | { |
c4d8e107 |
386 | ret->error = winsock_error_string(err); |
2f75bae1 |
387 | return ret; |
388 | } |
389 | |
390 | /* |
391 | * Connect to remote address. |
392 | */ |
c4d8e107 |
393 | #ifdef IPV6 |
394 | if (addr->family == AF_INET6) |
395 | { |
396 | memset(&a,0,sizeof(a)); |
397 | a6.sin6_family = AF_INET6; |
398 | a6.sin6_port = htons((short)port); |
399 | a6.sin6_addr = ((struct sockaddr_in6 *)addr->ai->ai_addr)->sin6_addr; |
400 | } |
401 | else |
402 | { |
403 | #endif |
404 | a.sin_family = AF_INET; |
405 | a.sin_addr.s_addr = htonl(addr->address); |
406 | a.sin_port = htons((short)port); |
407 | #ifdef IPV6 |
408 | } |
409 | if (connect (s, (addr->family == AF_INET6) ? (struct sockaddr *)&a6 : (struct sockaddr *)&a, (addr->family == AF_INET6) ? sizeof(a6) : sizeof(a)) == SOCKET_ERROR) |
410 | #else |
411 | if (connect (s, (struct sockaddr *)&a, sizeof(a)) == SOCKET_ERROR) |
412 | #endif |
413 | { |
2f75bae1 |
414 | err = WSAGetLastError(); |
c4d8e107 |
415 | ret->error = winsock_error_string(err); |
2f75bae1 |
416 | return ret; |
417 | } |
418 | |
419 | /* Set up a select mechanism. This could be an AsyncSelect on a |
420 | * window, or an EventSelect on an event object. */ |
421 | errstr = do_select(s, 1); |
422 | if (errstr) { |
423 | ret->error = errstr; |
424 | return ret; |
425 | } |
426 | |
427 | add234(sktree, ret); |
428 | |
429 | return ret; |
430 | } |
431 | |
432 | void sk_close(Socket s) { |
9c964e85 |
433 | extern char *do_select(SOCKET skt, int startup); |
434 | |
2f75bae1 |
435 | del234(sktree, s); |
436 | do_select(s->s, 0); |
437 | closesocket(s->s); |
dcbde236 |
438 | sfree(s); |
2f75bae1 |
439 | } |
440 | |
2f75bae1 |
441 | /* |
442 | * The function which tries to send on a socket once it's deemed |
443 | * writable. |
444 | */ |
445 | void try_send(Socket s) { |
446 | while (s->head) { |
447 | int nsent; |
448 | DWORD err; |
449 | int len, urgentflag; |
450 | |
451 | if (s->sending_oob) { |
452 | urgentflag = MSG_OOB; |
453 | len = s->sending_oob; |
454 | } else { |
455 | urgentflag = 0; |
456 | len = s->head->buflen - s->head->bufpos; |
457 | } |
458 | |
459 | nsent = send(s->s, s->head->buf + s->head->bufpos, len, urgentflag); |
7d6ee6ff |
460 | noise_ultralight(nsent); |
2f75bae1 |
461 | if (nsent <= 0) { |
462 | err = (nsent < 0 ? WSAGetLastError() : 0); |
463 | if (err == WSAEWOULDBLOCK) { |
464 | /* Perfectly normal: we've sent all we can for the moment. */ |
465 | s->writable = FALSE; |
466 | return; |
467 | } else if (nsent == 0 || |
468 | err == WSAECONNABORTED || |
469 | err == WSAECONNRESET) { |
470 | /* |
471 | * FIXME. This will have to be done better when we |
472 | * start managing multiple sockets (e.g. SSH port |
473 | * forwarding), because if we get CONNRESET while |
474 | * trying to write a particular forwarded socket |
475 | * then it isn't necessarily the end of the world. |
476 | * Ideally I'd like to pass the error code back to |
477 | * somewhere the next select_result() will see it, |
478 | * but that might be hard. Perhaps I should pass it |
479 | * back to be queued in the Windows front end bit. |
480 | */ |
481 | fatalbox(winsock_error_string(err)); |
482 | } else { |
483 | fatalbox(winsock_error_string(err)); |
484 | } |
485 | } else { |
486 | s->head->bufpos += nsent; |
487 | if (s->sending_oob) |
488 | s->sending_oob -= nsent; |
489 | if (s->head->bufpos >= s->head->buflen) { |
490 | struct buffer *tmp = s->head; |
491 | s->head = tmp->next; |
dcbde236 |
492 | sfree(tmp); |
2f75bae1 |
493 | if (!s->head) |
494 | s->tail = NULL; |
495 | } |
496 | } |
497 | } |
498 | } |
499 | |
500 | void sk_write(Socket s, char *buf, int len) { |
501 | /* |
502 | * Add the data to the buffer list on the socket. |
503 | */ |
504 | if (s->tail && s->tail->buflen < BUFFER_GRANULE) { |
505 | int copylen = min(len, BUFFER_GRANULE - s->tail->buflen); |
506 | memcpy(s->tail->buf + s->tail->buflen, buf, copylen); |
507 | buf += copylen; |
508 | len -= copylen; |
509 | s->tail->buflen += copylen; |
510 | } |
511 | while (len > 0) { |
512 | int grainlen = min(len, BUFFER_GRANULE); |
513 | struct buffer *newbuf; |
514 | newbuf = smalloc(sizeof(struct buffer)); |
515 | newbuf->bufpos = 0; |
516 | newbuf->buflen = grainlen; |
517 | memcpy(newbuf->buf, buf, grainlen); |
518 | buf += grainlen; |
519 | len -= grainlen; |
520 | if (s->tail) |
521 | s->tail->next = newbuf; |
522 | else |
523 | s->head = s->tail = newbuf; |
524 | newbuf->next = NULL; |
525 | s->tail = newbuf; |
526 | } |
527 | |
528 | /* |
529 | * Now try sending from the start of the buffer list. |
530 | */ |
531 | if (s->writable) |
532 | try_send(s); |
533 | } |
534 | |
535 | void sk_write_oob(Socket s, char *buf, int len) { |
536 | /* |
537 | * Replace the buffer list on the socket with the data. |
538 | */ |
539 | if (!s->head) { |
540 | s->head = smalloc(sizeof(struct buffer)); |
541 | } else { |
542 | struct buffer *walk = s->head->next; |
543 | while (walk) { |
544 | struct buffer *tmp = walk; |
545 | walk = tmp->next; |
dcbde236 |
546 | sfree(tmp); |
2f75bae1 |
547 | } |
548 | } |
549 | s->head->next = NULL; |
550 | s->tail = s->head; |
551 | s->head->buflen = len; |
552 | memcpy(s->head->buf, buf, len); |
553 | |
554 | /* |
555 | * Set the Urgent marker. |
556 | */ |
557 | s->sending_oob = len; |
558 | |
559 | /* |
560 | * Now try sending from the start of the buffer list. |
561 | */ |
562 | if (s->writable) |
563 | try_send(s); |
564 | } |
565 | |
566 | int select_result(WPARAM wParam, LPARAM lParam) { |
f9d3f227 |
567 | int ret, open; |
2f75bae1 |
568 | DWORD err; |
569 | char buf[BUFFER_GRANULE]; |
570 | Socket s; |
49bad831 |
571 | u_long atmark; |
2f75bae1 |
572 | |
573 | /* wParam is the socket itself */ |
574 | s = find234(sktree, (void *)wParam, cmpforsearch); |
575 | if (!s) |
576 | return 1; /* boggle */ |
577 | |
578 | if ((err = WSAGETSELECTERROR(lParam)) != 0) { |
2c94fd1c |
579 | /* |
580 | * An error has occurred on this socket. Pass it to the |
581 | * receiver function. |
582 | */ |
583 | return s->receiver(s, 3, winsock_error_string(err), err); |
2f75bae1 |
584 | } |
585 | |
7d6ee6ff |
586 | noise_ultralight(lParam); |
587 | |
2f75bae1 |
588 | switch (WSAGETSELECTEVENT(lParam)) { |
589 | case FD_READ: |
4b1e8acc |
590 | /* |
591 | * We have received data on the socket. For an oobinline |
592 | * socket, this might be data _before_ an urgent pointer, |
593 | * in which case we send it to the back end with type==1 |
594 | * (data prior to urgent). |
595 | */ |
596 | if (s->oobinline) { |
597 | atmark = 1; |
598 | ioctlsocket(s->s, SIOCATMARK, &atmark); |
599 | /* |
600 | * Avoid checking the return value from ioctlsocket(), |
601 | * on the grounds that some WinSock wrappers don't |
602 | * support it. If it does nothing, we get atmark==1, |
603 | * which is equivalent to `no OOB pending', so the |
604 | * effect will be to non-OOB-ify any OOB data. |
605 | */ |
606 | } else |
607 | atmark = 1; |
608 | |
2f75bae1 |
609 | ret = recv(s->s, buf, sizeof(buf), 0); |
4b1e8acc |
610 | noise_ultralight(ret); |
2f75bae1 |
611 | if (ret < 0) { |
612 | err = WSAGetLastError(); |
613 | if (err == WSAEWOULDBLOCK) { |
614 | break; |
615 | } |
616 | } |
617 | if (ret < 0) { |
2c94fd1c |
618 | return s->receiver(s, 3, winsock_error_string(err), err); |
2f75bae1 |
619 | } else { |
4b1e8acc |
620 | return s->receiver(s, atmark ? 0 : 1, buf, ret); |
2f75bae1 |
621 | } |
622 | break; |
623 | case FD_OOB: |
4b1e8acc |
624 | /* |
625 | * This will only happen on a non-oobinline socket. It |
626 | * indicates that we can immediately perform an OOB read |
627 | * and get back OOB data, which we will send to the back |
628 | * end with type==2 (urgent data). |
629 | */ |
2f75bae1 |
630 | ret = recv(s->s, buf, sizeof(buf), MSG_OOB); |
7d6ee6ff |
631 | noise_ultralight(ret); |
2f75bae1 |
632 | if (ret <= 0) { |
633 | fatalbox(ret == 0 ? "Internal networking trouble" : |
634 | winsock_error_string(WSAGetLastError())); |
635 | } else { |
1ad4eb6f |
636 | return s->receiver(s, 2, buf, ret); |
2f75bae1 |
637 | } |
638 | break; |
639 | case FD_WRITE: |
640 | s->writable = 1; |
641 | try_send(s); |
642 | break; |
643 | case FD_CLOSE: |
f9d3f227 |
644 | /* Signal a close on the socket. First read any outstanding data. */ |
645 | open = 1; |
646 | do { |
647 | ret = recv(s->s, buf, sizeof(buf), 0); |
648 | if (ret < 0) { |
649 | err = WSAGetLastError(); |
650 | if (err == WSAEWOULDBLOCK) |
651 | break; |
652 | return s->receiver(s, 3, winsock_error_string(err), err); |
653 | } else |
654 | open &= s->receiver(s, 0, buf, ret); |
655 | } while (ret > 0); |
656 | return open; |
2f75bae1 |
657 | } |
658 | |
659 | return 1; |
660 | } |
661 | |
662 | /* |
663 | * Each socket abstraction contains a `void *' private field in |
664 | * which the client can keep state. |
665 | */ |
666 | void sk_set_private_ptr(Socket s, void *ptr) { |
667 | s->private_ptr = ptr; |
668 | } |
669 | void *sk_get_private_ptr(Socket s) { |
670 | return s->private_ptr; |
671 | } |
672 | |
673 | /* |
674 | * Special error values are returned from sk_namelookup and sk_new |
675 | * if there's a problem. These functions extract an error message, |
676 | * or return NULL if there's no problem. |
677 | */ |
678 | char *sk_addr_error(SockAddr addr) { |
679 | return addr->error; |
680 | } |
681 | char *sk_socket_error(Socket s) { |
682 | return s->error; |
683 | } |
684 | |
685 | /* |
686 | * For Plink: enumerate all sockets currently active. |
687 | */ |
688 | SOCKET first_socket(enum234 *e) { |
689 | Socket s = first234(sktree, e); |
690 | return s ? s->s : INVALID_SOCKET; |
691 | } |
692 | SOCKET next_socket(enum234 *e) { |
693 | Socket s = next234(e); |
694 | return s ? s->s : INVALID_SOCKET; |
695 | } |