| 1 | /* |
| 2 | * PLink - a command-line (stdin/stdout) variant of PuTTY. |
| 3 | */ |
| 4 | |
| 5 | #ifndef AUTO_WINSOCK |
| 6 | #include <winsock2.h> |
| 7 | #endif |
| 8 | #include <windows.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <assert.h> |
| 12 | #include <stdarg.h> |
| 13 | |
| 14 | #define PUTTY_DO_GLOBALS /* actually _define_ globals */ |
| 15 | #include "putty.h" |
| 16 | #include "storage.h" |
| 17 | #include "tree234.h" |
| 18 | |
| 19 | #define MAX_STDIN_BACKLOG 4096 |
| 20 | |
| 21 | void fatalbox(char *p, ...) |
| 22 | { |
| 23 | va_list ap; |
| 24 | fprintf(stderr, "FATAL ERROR: "); |
| 25 | va_start(ap, p); |
| 26 | vfprintf(stderr, p, ap); |
| 27 | va_end(ap); |
| 28 | fputc('\n', stderr); |
| 29 | WSACleanup(); |
| 30 | cleanup_exit(1); |
| 31 | } |
| 32 | void modalfatalbox(char *p, ...) |
| 33 | { |
| 34 | va_list ap; |
| 35 | fprintf(stderr, "FATAL ERROR: "); |
| 36 | va_start(ap, p); |
| 37 | vfprintf(stderr, p, ap); |
| 38 | va_end(ap); |
| 39 | fputc('\n', stderr); |
| 40 | WSACleanup(); |
| 41 | cleanup_exit(1); |
| 42 | } |
| 43 | void connection_fatal(void *frontend, char *p, ...) |
| 44 | { |
| 45 | va_list ap; |
| 46 | fprintf(stderr, "FATAL ERROR: "); |
| 47 | va_start(ap, p); |
| 48 | vfprintf(stderr, p, ap); |
| 49 | va_end(ap); |
| 50 | fputc('\n', stderr); |
| 51 | WSACleanup(); |
| 52 | cleanup_exit(1); |
| 53 | } |
| 54 | void cmdline_error(char *p, ...) |
| 55 | { |
| 56 | va_list ap; |
| 57 | fprintf(stderr, "plink: "); |
| 58 | va_start(ap, p); |
| 59 | vfprintf(stderr, p, ap); |
| 60 | va_end(ap); |
| 61 | fputc('\n', stderr); |
| 62 | exit(1); |
| 63 | } |
| 64 | |
| 65 | HANDLE inhandle, outhandle, errhandle; |
| 66 | DWORD orig_console_mode; |
| 67 | |
| 68 | WSAEVENT netevent; |
| 69 | |
| 70 | static Backend *back; |
| 71 | static void *backhandle; |
| 72 | |
| 73 | int term_ldisc(Terminal *term, int mode) |
| 74 | { |
| 75 | return FALSE; |
| 76 | } |
| 77 | void ldisc_update(void *frontend, int echo, int edit) |
| 78 | { |
| 79 | /* Update stdin read mode to reflect changes in line discipline. */ |
| 80 | DWORD mode; |
| 81 | |
| 82 | mode = ENABLE_PROCESSED_INPUT; |
| 83 | if (echo) |
| 84 | mode = mode | ENABLE_ECHO_INPUT; |
| 85 | else |
| 86 | mode = mode & ~ENABLE_ECHO_INPUT; |
| 87 | if (edit) |
| 88 | mode = mode | ENABLE_LINE_INPUT; |
| 89 | else |
| 90 | mode = mode & ~ENABLE_LINE_INPUT; |
| 91 | SetConsoleMode(inhandle, mode); |
| 92 | } |
| 93 | |
| 94 | struct input_data { |
| 95 | DWORD len; |
| 96 | char buffer[4096]; |
| 97 | HANDLE event, eventback; |
| 98 | }; |
| 99 | |
| 100 | static DWORD WINAPI stdin_read_thread(void *param) |
| 101 | { |
| 102 | struct input_data *idata = (struct input_data *) param; |
| 103 | HANDLE inhandle; |
| 104 | |
| 105 | inhandle = GetStdHandle(STD_INPUT_HANDLE); |
| 106 | |
| 107 | while (ReadFile(inhandle, idata->buffer, sizeof(idata->buffer), |
| 108 | &idata->len, NULL) && idata->len > 0) { |
| 109 | SetEvent(idata->event); |
| 110 | WaitForSingleObject(idata->eventback, INFINITE); |
| 111 | } |
| 112 | |
| 113 | idata->len = 0; |
| 114 | SetEvent(idata->event); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | struct output_data { |
| 120 | DWORD len, lenwritten; |
| 121 | int writeret; |
| 122 | char *buffer; |
| 123 | int is_stderr, done; |
| 124 | HANDLE event, eventback; |
| 125 | int busy; |
| 126 | }; |
| 127 | |
| 128 | static DWORD WINAPI stdout_write_thread(void *param) |
| 129 | { |
| 130 | struct output_data *odata = (struct output_data *) param; |
| 131 | HANDLE outhandle, errhandle; |
| 132 | |
| 133 | outhandle = GetStdHandle(STD_OUTPUT_HANDLE); |
| 134 | errhandle = GetStdHandle(STD_ERROR_HANDLE); |
| 135 | |
| 136 | while (1) { |
| 137 | WaitForSingleObject(odata->eventback, INFINITE); |
| 138 | if (odata->done) |
| 139 | break; |
| 140 | odata->writeret = |
| 141 | WriteFile(odata->is_stderr ? errhandle : outhandle, |
| 142 | odata->buffer, odata->len, &odata->lenwritten, NULL); |
| 143 | SetEvent(odata->event); |
| 144 | } |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | bufchain stdout_data, stderr_data; |
| 150 | struct output_data odata, edata; |
| 151 | |
| 152 | void try_output(int is_stderr) |
| 153 | { |
| 154 | struct output_data *data = (is_stderr ? &edata : &odata); |
| 155 | void *senddata; |
| 156 | int sendlen; |
| 157 | |
| 158 | if (!data->busy) { |
| 159 | bufchain_prefix(is_stderr ? &stderr_data : &stdout_data, |
| 160 | &senddata, &sendlen); |
| 161 | data->buffer = senddata; |
| 162 | data->len = sendlen; |
| 163 | SetEvent(data->eventback); |
| 164 | data->busy = 1; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | int from_backend(void *frontend_handle, int is_stderr, char *data, int len) |
| 169 | { |
| 170 | int osize, esize; |
| 171 | |
| 172 | assert(len > 0); |
| 173 | |
| 174 | if (is_stderr) { |
| 175 | bufchain_add(&stderr_data, data, len); |
| 176 | try_output(1); |
| 177 | } else { |
| 178 | bufchain_add(&stdout_data, data, len); |
| 179 | try_output(0); |
| 180 | } |
| 181 | |
| 182 | osize = bufchain_size(&stdout_data); |
| 183 | esize = bufchain_size(&stderr_data); |
| 184 | |
| 185 | return osize + esize; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Short description of parameters. |
| 190 | */ |
| 191 | static void usage(void) |
| 192 | { |
| 193 | printf("PuTTY Link: command-line connection utility\n"); |
| 194 | printf("%s\n", ver); |
| 195 | printf("Usage: plink [options] [user@]host [command]\n"); |
| 196 | printf(" (\"host\" can also be a PuTTY saved session name)\n"); |
| 197 | printf("Options:\n"); |
| 198 | printf(" -v show verbose messages\n"); |
| 199 | printf(" -load sessname Load settings from saved session\n"); |
| 200 | printf(" -ssh -telnet -rlogin -raw\n"); |
| 201 | printf(" force use of a particular protocol (default SSH)\n"); |
| 202 | printf(" -P port connect to specified port\n"); |
| 203 | printf(" -l user connect with specified username\n"); |
| 204 | printf(" -m file read remote command(s) from file\n"); |
| 205 | printf(" -batch disable all interactive prompts\n"); |
| 206 | printf("The following options only apply to SSH connections:\n"); |
| 207 | printf(" -pw passw login with specified password\n"); |
| 208 | printf(" -L listen-port:host:port Forward local port to " |
| 209 | "remote address\n"); |
| 210 | printf(" -R listen-port:host:port Forward remote port to" |
| 211 | " local address\n"); |
| 212 | printf(" -X -x enable / disable X11 forwarding\n"); |
| 213 | printf(" -A -a enable / disable agent forwarding\n"); |
| 214 | printf(" -t -T enable / disable pty allocation\n"); |
| 215 | printf(" -1 -2 force use of particular protocol version\n"); |
| 216 | printf(" -C enable compression\n"); |
| 217 | printf(" -i key private key file for authentication\n"); |
| 218 | exit(1); |
| 219 | } |
| 220 | |
| 221 | char *do_select(SOCKET skt, int startup) |
| 222 | { |
| 223 | int events; |
| 224 | if (startup) { |
| 225 | events = (FD_CONNECT | FD_READ | FD_WRITE | |
| 226 | FD_OOB | FD_CLOSE | FD_ACCEPT); |
| 227 | } else { |
| 228 | events = 0; |
| 229 | } |
| 230 | if (WSAEventSelect(skt, netevent, events) == SOCKET_ERROR) { |
| 231 | switch (WSAGetLastError()) { |
| 232 | case WSAENETDOWN: |
| 233 | return "Network is down"; |
| 234 | default: |
| 235 | return "WSAAsyncSelect(): unknown error"; |
| 236 | } |
| 237 | } |
| 238 | return NULL; |
| 239 | } |
| 240 | |
| 241 | int main(int argc, char **argv) |
| 242 | { |
| 243 | WSADATA wsadata; |
| 244 | WORD winsock_ver; |
| 245 | WSAEVENT stdinevent, stdoutevent, stderrevent; |
| 246 | HANDLE handles[4]; |
| 247 | DWORD in_threadid, out_threadid, err_threadid; |
| 248 | struct input_data idata; |
| 249 | int reading; |
| 250 | int sending; |
| 251 | int portnumber = -1; |
| 252 | SOCKET *sklist; |
| 253 | int skcount, sksize; |
| 254 | int connopen; |
| 255 | int exitcode; |
| 256 | int errors; |
| 257 | |
| 258 | ssh_get_line = console_get_line; |
| 259 | |
| 260 | sklist = NULL; |
| 261 | skcount = sksize = 0; |
| 262 | /* |
| 263 | * Initialise port and protocol to sensible defaults. (These |
| 264 | * will be overridden by more or less anything.) |
| 265 | */ |
| 266 | default_protocol = PROT_SSH; |
| 267 | default_port = 22; |
| 268 | |
| 269 | flags = FLAG_STDERR; |
| 270 | /* |
| 271 | * Process the command line. |
| 272 | */ |
| 273 | do_defaults(NULL, &cfg); |
| 274 | default_protocol = cfg.protocol; |
| 275 | default_port = cfg.port; |
| 276 | errors = 0; |
| 277 | { |
| 278 | /* |
| 279 | * Override the default protocol if PLINK_PROTOCOL is set. |
| 280 | */ |
| 281 | char *p = getenv("PLINK_PROTOCOL"); |
| 282 | int i; |
| 283 | if (p) { |
| 284 | for (i = 0; backends[i].backend != NULL; i++) { |
| 285 | if (!strcmp(backends[i].name, p)) { |
| 286 | default_protocol = cfg.protocol = backends[i].protocol; |
| 287 | default_port = cfg.port = |
| 288 | backends[i].backend->default_port; |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | while (--argc) { |
| 295 | char *p = *++argv; |
| 296 | if (*p == '-') { |
| 297 | int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL), 1); |
| 298 | if (ret == -2) { |
| 299 | fprintf(stderr, |
| 300 | "plink: option \"%s\" requires an argument\n", p); |
| 301 | errors = 1; |
| 302 | } else if (ret == 2) { |
| 303 | --argc, ++argv; |
| 304 | } else if (ret == 1) { |
| 305 | continue; |
| 306 | } else if (!strcmp(p, "-batch")) { |
| 307 | console_batch_mode = 1; |
| 308 | } else { |
| 309 | fprintf(stderr, "plink: unknown option \"%s\"\n", p); |
| 310 | errors = 1; |
| 311 | } |
| 312 | } else if (*p) { |
| 313 | if (!*cfg.host) { |
| 314 | char *q = p; |
| 315 | /* |
| 316 | * If the hostname starts with "telnet:", set the |
| 317 | * protocol to Telnet and process the string as a |
| 318 | * Telnet URL. |
| 319 | */ |
| 320 | if (!strncmp(q, "telnet:", 7)) { |
| 321 | char c; |
| 322 | |
| 323 | q += 7; |
| 324 | if (q[0] == '/' && q[1] == '/') |
| 325 | q += 2; |
| 326 | cfg.protocol = PROT_TELNET; |
| 327 | p = q; |
| 328 | while (*p && *p != ':' && *p != '/') |
| 329 | p++; |
| 330 | c = *p; |
| 331 | if (*p) |
| 332 | *p++ = '\0'; |
| 333 | if (c == ':') |
| 334 | cfg.port = atoi(p); |
| 335 | else |
| 336 | cfg.port = -1; |
| 337 | strncpy(cfg.host, q, sizeof(cfg.host) - 1); |
| 338 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
| 339 | } else { |
| 340 | char *r; |
| 341 | /* |
| 342 | * Before we process the [user@]host string, we |
| 343 | * first check for the presence of a protocol |
| 344 | * prefix (a protocol name followed by ","). |
| 345 | */ |
| 346 | r = strchr(p, ','); |
| 347 | if (r) { |
| 348 | int i, j; |
| 349 | for (i = 0; backends[i].backend != NULL; i++) { |
| 350 | j = strlen(backends[i].name); |
| 351 | if (j == r - p && |
| 352 | !memcmp(backends[i].name, p, j)) { |
| 353 | default_protocol = cfg.protocol = |
| 354 | backends[i].protocol; |
| 355 | portnumber = |
| 356 | backends[i].backend->default_port; |
| 357 | p = r + 1; |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Three cases. Either (a) there's a nonzero |
| 365 | * length string followed by an @, in which |
| 366 | * case that's user and the remainder is host. |
| 367 | * Or (b) there's only one string, not counting |
| 368 | * a potential initial @, and it exists in the |
| 369 | * saved-sessions database. Or (c) only one |
| 370 | * string and it _doesn't_ exist in the |
| 371 | * database. |
| 372 | */ |
| 373 | r = strrchr(p, '@'); |
| 374 | if (r == p) |
| 375 | p++, r = NULL; /* discount initial @ */ |
| 376 | if (r == NULL) { |
| 377 | /* |
| 378 | * One string. |
| 379 | */ |
| 380 | Config cfg2; |
| 381 | do_defaults(p, &cfg2); |
| 382 | if (cfg2.host[0] == '\0') { |
| 383 | /* No settings for this host; use defaults */ |
| 384 | strncpy(cfg.host, p, sizeof(cfg.host) - 1); |
| 385 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
| 386 | cfg.port = default_port; |
| 387 | } else { |
| 388 | cfg = cfg2; |
| 389 | cfg.remote_cmd_ptr = cfg.remote_cmd; |
| 390 | } |
| 391 | } else { |
| 392 | *r++ = '\0'; |
| 393 | strncpy(cfg.username, p, sizeof(cfg.username) - 1); |
| 394 | cfg.username[sizeof(cfg.username) - 1] = '\0'; |
| 395 | strncpy(cfg.host, r, sizeof(cfg.host) - 1); |
| 396 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
| 397 | cfg.port = default_port; |
| 398 | } |
| 399 | } |
| 400 | } else { |
| 401 | char *command; |
| 402 | int cmdlen, cmdsize; |
| 403 | cmdlen = cmdsize = 0; |
| 404 | command = NULL; |
| 405 | |
| 406 | while (argc) { |
| 407 | while (*p) { |
| 408 | if (cmdlen >= cmdsize) { |
| 409 | cmdsize = cmdlen + 512; |
| 410 | command = srealloc(command, cmdsize); |
| 411 | } |
| 412 | command[cmdlen++]=*p++; |
| 413 | } |
| 414 | if (cmdlen >= cmdsize) { |
| 415 | cmdsize = cmdlen + 512; |
| 416 | command = srealloc(command, cmdsize); |
| 417 | } |
| 418 | command[cmdlen++]=' '; /* always add trailing space */ |
| 419 | if (--argc) p = *++argv; |
| 420 | } |
| 421 | if (cmdlen) command[--cmdlen]='\0'; |
| 422 | /* change trailing blank to NUL */ |
| 423 | cfg.remote_cmd_ptr = command; |
| 424 | cfg.remote_cmd_ptr2 = NULL; |
| 425 | cfg.nopty = TRUE; /* command => no terminal */ |
| 426 | |
| 427 | break; /* done with cmdline */ |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | if (errors) |
| 433 | return 1; |
| 434 | |
| 435 | if (!*cfg.host) { |
| 436 | usage(); |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Trim leading whitespace off the hostname if it's there. |
| 441 | */ |
| 442 | { |
| 443 | int space = strspn(cfg.host, " \t"); |
| 444 | memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space); |
| 445 | } |
| 446 | |
| 447 | /* See if host is of the form user@host */ |
| 448 | if (cfg.host[0] != '\0') { |
| 449 | char *atsign = strchr(cfg.host, '@'); |
| 450 | /* Make sure we're not overflowing the user field */ |
| 451 | if (atsign) { |
| 452 | if (atsign - cfg.host < sizeof cfg.username) { |
| 453 | strncpy(cfg.username, cfg.host, atsign - cfg.host); |
| 454 | cfg.username[atsign - cfg.host] = '\0'; |
| 455 | } |
| 456 | memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1)); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Perform command-line overrides on session configuration. |
| 462 | */ |
| 463 | cmdline_run_saved(); |
| 464 | |
| 465 | /* |
| 466 | * Trim a colon suffix off the hostname if it's there. |
| 467 | */ |
| 468 | cfg.host[strcspn(cfg.host, ":")] = '\0'; |
| 469 | |
| 470 | /* |
| 471 | * Remove any remaining whitespace from the hostname. |
| 472 | */ |
| 473 | { |
| 474 | int p1 = 0, p2 = 0; |
| 475 | while (cfg.host[p2] != '\0') { |
| 476 | if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') { |
| 477 | cfg.host[p1] = cfg.host[p2]; |
| 478 | p1++; |
| 479 | } |
| 480 | p2++; |
| 481 | } |
| 482 | cfg.host[p1] = '\0'; |
| 483 | } |
| 484 | |
| 485 | if (!*cfg.remote_cmd_ptr) |
| 486 | flags |= FLAG_INTERACTIVE; |
| 487 | |
| 488 | /* |
| 489 | * Select protocol. This is farmed out into a table in a |
| 490 | * separate file to enable an ssh-free variant. |
| 491 | */ |
| 492 | { |
| 493 | int i; |
| 494 | back = NULL; |
| 495 | for (i = 0; backends[i].backend != NULL; i++) |
| 496 | if (backends[i].protocol == cfg.protocol) { |
| 497 | back = backends[i].backend; |
| 498 | break; |
| 499 | } |
| 500 | if (back == NULL) { |
| 501 | fprintf(stderr, |
| 502 | "Internal fault: Unsupported protocol found\n"); |
| 503 | return 1; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | * Select port. |
| 509 | */ |
| 510 | if (portnumber != -1) |
| 511 | cfg.port = portnumber; |
| 512 | |
| 513 | /* |
| 514 | * Initialise WinSock. |
| 515 | */ |
| 516 | winsock_ver = MAKEWORD(2, 0); |
| 517 | if (WSAStartup(winsock_ver, &wsadata)) { |
| 518 | MessageBox(NULL, "Unable to initialise WinSock", "WinSock Error", |
| 519 | MB_OK | MB_ICONEXCLAMATION); |
| 520 | return 1; |
| 521 | } |
| 522 | if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 0) { |
| 523 | MessageBox(NULL, "WinSock version is incompatible with 2.0", |
| 524 | "WinSock Error", MB_OK | MB_ICONEXCLAMATION); |
| 525 | WSACleanup(); |
| 526 | return 1; |
| 527 | } |
| 528 | sk_init(); |
| 529 | |
| 530 | /* |
| 531 | * Start up the connection. |
| 532 | */ |
| 533 | netevent = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 534 | { |
| 535 | char *error; |
| 536 | char *realhost; |
| 537 | /* nodelay is only useful if stdin is a character device (console) */ |
| 538 | int nodelay = cfg.tcp_nodelay && |
| 539 | (GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_CHAR); |
| 540 | |
| 541 | error = back->init(NULL, &backhandle, cfg.host, cfg.port, |
| 542 | &realhost, nodelay); |
| 543 | if (error) { |
| 544 | fprintf(stderr, "Unable to open connection:\n%s", error); |
| 545 | return 1; |
| 546 | } |
| 547 | logctx = log_init(NULL); |
| 548 | back->provide_logctx(backhandle, logctx); |
| 549 | sfree(realhost); |
| 550 | } |
| 551 | connopen = 1; |
| 552 | |
| 553 | stdinevent = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 554 | stdoutevent = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 555 | stderrevent = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 556 | |
| 557 | inhandle = GetStdHandle(STD_INPUT_HANDLE); |
| 558 | outhandle = GetStdHandle(STD_OUTPUT_HANDLE); |
| 559 | errhandle = GetStdHandle(STD_ERROR_HANDLE); |
| 560 | GetConsoleMode(inhandle, &orig_console_mode); |
| 561 | SetConsoleMode(inhandle, ENABLE_PROCESSED_INPUT); |
| 562 | |
| 563 | /* |
| 564 | * Turn off ECHO and LINE input modes. We don't care if this |
| 565 | * call fails, because we know we aren't necessarily running in |
| 566 | * a console. |
| 567 | */ |
| 568 | handles[0] = netevent; |
| 569 | handles[1] = stdinevent; |
| 570 | handles[2] = stdoutevent; |
| 571 | handles[3] = stderrevent; |
| 572 | sending = FALSE; |
| 573 | |
| 574 | /* |
| 575 | * Create spare threads to write to stdout and stderr, so we |
| 576 | * can arrange asynchronous writes. |
| 577 | */ |
| 578 | odata.event = stdoutevent; |
| 579 | odata.eventback = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 580 | odata.is_stderr = 0; |
| 581 | odata.busy = odata.done = 0; |
| 582 | if (!CreateThread(NULL, 0, stdout_write_thread, |
| 583 | &odata, 0, &out_threadid)) { |
| 584 | fprintf(stderr, "Unable to create output thread\n"); |
| 585 | cleanup_exit(1); |
| 586 | } |
| 587 | edata.event = stderrevent; |
| 588 | edata.eventback = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 589 | edata.is_stderr = 1; |
| 590 | edata.busy = edata.done = 0; |
| 591 | if (!CreateThread(NULL, 0, stdout_write_thread, |
| 592 | &edata, 0, &err_threadid)) { |
| 593 | fprintf(stderr, "Unable to create error output thread\n"); |
| 594 | cleanup_exit(1); |
| 595 | } |
| 596 | |
| 597 | while (1) { |
| 598 | int n; |
| 599 | |
| 600 | if (!sending && back->sendok(backhandle)) { |
| 601 | /* |
| 602 | * Create a separate thread to read from stdin. This is |
| 603 | * a total pain, but I can't find another way to do it: |
| 604 | * |
| 605 | * - an overlapped ReadFile or ReadFileEx just doesn't |
| 606 | * happen; we get failure from ReadFileEx, and |
| 607 | * ReadFile blocks despite being given an OVERLAPPED |
| 608 | * structure. Perhaps we can't do overlapped reads |
| 609 | * on consoles. WHY THE HELL NOT? |
| 610 | * |
| 611 | * - WaitForMultipleObjects(netevent, console) doesn't |
| 612 | * work, because it signals the console when |
| 613 | * _anything_ happens, including mouse motions and |
| 614 | * other things that don't cause data to be readable |
| 615 | * - so we're back to ReadFile blocking. |
| 616 | */ |
| 617 | idata.event = stdinevent; |
| 618 | idata.eventback = CreateEvent(NULL, FALSE, FALSE, NULL); |
| 619 | if (!CreateThread(NULL, 0, stdin_read_thread, |
| 620 | &idata, 0, &in_threadid)) { |
| 621 | fprintf(stderr, "Unable to create input thread\n"); |
| 622 | cleanup_exit(1); |
| 623 | } |
| 624 | sending = TRUE; |
| 625 | } |
| 626 | |
| 627 | n = WaitForMultipleObjects(4, handles, FALSE, INFINITE); |
| 628 | if (n == 0) { |
| 629 | WSANETWORKEVENTS things; |
| 630 | SOCKET socket; |
| 631 | extern SOCKET first_socket(int *), next_socket(int *); |
| 632 | extern int select_result(WPARAM, LPARAM); |
| 633 | int i, socketstate; |
| 634 | |
| 635 | /* |
| 636 | * We must not call select_result() for any socket |
| 637 | * until we have finished enumerating within the tree. |
| 638 | * This is because select_result() may close the socket |
| 639 | * and modify the tree. |
| 640 | */ |
| 641 | /* Count the active sockets. */ |
| 642 | i = 0; |
| 643 | for (socket = first_socket(&socketstate); |
| 644 | socket != INVALID_SOCKET; |
| 645 | socket = next_socket(&socketstate)) i++; |
| 646 | |
| 647 | /* Expand the buffer if necessary. */ |
| 648 | if (i > sksize) { |
| 649 | sksize = i + 16; |
| 650 | sklist = srealloc(sklist, sksize * sizeof(*sklist)); |
| 651 | } |
| 652 | |
| 653 | /* Retrieve the sockets into sklist. */ |
| 654 | skcount = 0; |
| 655 | for (socket = first_socket(&socketstate); |
| 656 | socket != INVALID_SOCKET; |
| 657 | socket = next_socket(&socketstate)) { |
| 658 | sklist[skcount++] = socket; |
| 659 | } |
| 660 | |
| 661 | /* Now we're done enumerating; go through the list. */ |
| 662 | for (i = 0; i < skcount; i++) { |
| 663 | WPARAM wp; |
| 664 | socket = sklist[i]; |
| 665 | wp = (WPARAM) socket; |
| 666 | if (!WSAEnumNetworkEvents(socket, NULL, &things)) { |
| 667 | static const struct { int bit, mask; } eventtypes[] = { |
| 668 | {FD_CONNECT_BIT, FD_CONNECT}, |
| 669 | {FD_READ_BIT, FD_READ}, |
| 670 | {FD_CLOSE_BIT, FD_CLOSE}, |
| 671 | {FD_OOB_BIT, FD_OOB}, |
| 672 | {FD_WRITE_BIT, FD_WRITE}, |
| 673 | {FD_ACCEPT_BIT, FD_ACCEPT}, |
| 674 | }; |
| 675 | int e; |
| 676 | |
| 677 | noise_ultralight(socket); |
| 678 | noise_ultralight(things.lNetworkEvents); |
| 679 | |
| 680 | for (e = 0; e < lenof(eventtypes); e++) |
| 681 | if (things.lNetworkEvents & eventtypes[e].mask) { |
| 682 | LPARAM lp; |
| 683 | int err = things.iErrorCode[eventtypes[e].bit]; |
| 684 | lp = WSAMAKESELECTREPLY(eventtypes[e].mask, err); |
| 685 | connopen &= select_result(wp, lp); |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | } else if (n == 1) { |
| 690 | reading = 0; |
| 691 | noise_ultralight(idata.len); |
| 692 | if (connopen && back->socket(backhandle) != NULL) { |
| 693 | if (idata.len > 0) { |
| 694 | back->send(backhandle, idata.buffer, idata.len); |
| 695 | } else { |
| 696 | back->special(backhandle, TS_EOF); |
| 697 | } |
| 698 | } |
| 699 | } else if (n == 2) { |
| 700 | odata.busy = 0; |
| 701 | if (!odata.writeret) { |
| 702 | fprintf(stderr, "Unable to write to standard output\n"); |
| 703 | cleanup_exit(0); |
| 704 | } |
| 705 | bufchain_consume(&stdout_data, odata.lenwritten); |
| 706 | if (bufchain_size(&stdout_data) > 0) |
| 707 | try_output(0); |
| 708 | if (connopen && back->socket(backhandle) != NULL) { |
| 709 | back->unthrottle(backhandle, bufchain_size(&stdout_data) + |
| 710 | bufchain_size(&stderr_data)); |
| 711 | } |
| 712 | } else if (n == 3) { |
| 713 | edata.busy = 0; |
| 714 | if (!edata.writeret) { |
| 715 | fprintf(stderr, "Unable to write to standard output\n"); |
| 716 | cleanup_exit(0); |
| 717 | } |
| 718 | bufchain_consume(&stderr_data, edata.lenwritten); |
| 719 | if (bufchain_size(&stderr_data) > 0) |
| 720 | try_output(1); |
| 721 | if (connopen && back->socket(backhandle) != NULL) { |
| 722 | back->unthrottle(backhandle, bufchain_size(&stdout_data) + |
| 723 | bufchain_size(&stderr_data)); |
| 724 | } |
| 725 | } |
| 726 | if (!reading && back->sendbuffer(backhandle) < MAX_STDIN_BACKLOG) { |
| 727 | SetEvent(idata.eventback); |
| 728 | reading = 1; |
| 729 | } |
| 730 | if ((!connopen || back->socket(backhandle) == NULL) && |
| 731 | bufchain_size(&stdout_data) == 0 && |
| 732 | bufchain_size(&stderr_data) == 0) |
| 733 | break; /* we closed the connection */ |
| 734 | } |
| 735 | WSACleanup(); |
| 736 | exitcode = back->exitcode(backhandle); |
| 737 | if (exitcode < 0) { |
| 738 | fprintf(stderr, "Remote process exit code unavailable\n"); |
| 739 | exitcode = 1; /* this is an error condition */ |
| 740 | } |
| 741 | return exitcode; |
| 742 | } |