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