The Windows HANDLE type, despite being a `void *', does not actually
[u/mdw/putty] / windows / winplink.c
CommitLineData
12dc4ec0 1/*
f4ff9455 2 * PLink - a Windows command-line (stdin/stdout) variant of PuTTY.
12dc4ec0 3 */
4
12dc4ec0 5#include <stdio.h>
49bad831 6#include <stdlib.h>
2b0c045b 7#include <assert.h>
12dc4ec0 8#include <stdarg.h>
9
32874aea 10#define PUTTY_DO_GLOBALS /* actually _define_ globals */
12dc4ec0 11#include "putty.h"
a9422f39 12#include "storage.h"
8df7a775 13#include "tree234.h"
12dc4ec0 14
604fab0c 15#define WM_AGENT_CALLBACK (WM_APP + 4)
c44bf5bd 16
c44bf5bd 17struct agent_callback {
18 void (*callback)(void *, void *, int);
19 void *callback_ctx;
20 void *data;
21 int len;
22};
23
32874aea 24void fatalbox(char *p, ...)
25{
12dc4ec0 26 va_list ap;
49bad831 27 fprintf(stderr, "FATAL ERROR: ");
1709795f 28 va_start(ap, p);
29 vfprintf(stderr, p, ap);
30 va_end(ap);
31 fputc('\n', stderr);
1709795f 32 cleanup_exit(1);
33}
34void modalfatalbox(char *p, ...)
35{
36 va_list ap;
37 fprintf(stderr, "FATAL ERROR: ");
12dc4ec0 38 va_start(ap, p);
39 vfprintf(stderr, p, ap);
40 va_end(ap);
41 fputc('\n', stderr);
93b581bd 42 cleanup_exit(1);
12dc4ec0 43}
a8327734 44void connection_fatal(void *frontend, char *p, ...)
32874aea 45{
8d5de777 46 va_list ap;
49bad831 47 fprintf(stderr, "FATAL ERROR: ");
8d5de777 48 va_start(ap, p);
49 vfprintf(stderr, p, ap);
50 va_end(ap);
51 fputc('\n', stderr);
93b581bd 52 cleanup_exit(1);
8d5de777 53}
c0a81592 54void 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}
12dc4ec0 64
0965bee0 65HANDLE inhandle, outhandle, errhandle;
34292b1d 66struct handle *stdin_handle, *stdout_handle, *stderr_handle;
6f34e365 67DWORD orig_console_mode;
34292b1d 68int connopen;
6f34e365 69
8df7a775 70WSAEVENT netevent;
71
6b78788a 72static Backend *back;
73static void *backhandle;
3ea863a3 74static Config cfg;
6b78788a 75
887035a5 76int term_ldisc(Terminal *term, int mode)
32874aea 77{
78 return FALSE;
79}
b9d7bcad 80void ldisc_update(void *frontend, int echo, int edit)
32874aea 81{
0965bee0 82 /* Update stdin read mode to reflect changes in line discipline. */
83 DWORD mode;
84
85 mode = ENABLE_PROCESSED_INPUT;
86 if (echo)
32874aea 87 mode = mode | ENABLE_ECHO_INPUT;
0965bee0 88 else
32874aea 89 mode = mode & ~ENABLE_ECHO_INPUT;
0965bee0 90 if (edit)
32874aea 91 mode = mode | ENABLE_LINE_INPUT;
0965bee0 92 else
32874aea 93 mode = mode & ~ENABLE_LINE_INPUT;
0965bee0 94 SetConsoleMode(inhandle, mode);
95}
96
c6ccd5c2 97char *get_ttymode(void *frontend, const char *mode) { return NULL; }
98
9fab77dc 99int from_backend(void *frontend_handle, int is_stderr,
100 const char *data, int len)
5471d09a 101{
5471d09a 102 if (is_stderr) {
34292b1d 103 handle_write(stderr_handle, data, len);
5471d09a 104 } else {
34292b1d 105 handle_write(stdout_handle, data, len);
5471d09a 106 }
107
34292b1d 108 return handle_backlog(stdout_handle) + handle_backlog(stderr_handle);
5471d09a 109}
110
edd0cb8a 111int from_backend_untrusted(void *frontend_handle, const char *data, int len)
112{
113 /*
114 * No "untrusted" output should get here (the way the code is
115 * currently, it's all diverted by FLAG_STDERR).
116 */
117 assert(!"Unexpected call to from_backend_untrusted()");
118 return 0; /* not reached */
119}
120
121int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
122{
123 int ret;
124 ret = cmdline_get_passwd_input(p, in, inlen);
125 if (ret == -1)
126 ret = console_get_userpass_input(p, in, inlen);
127 return ret;
128}
129
c44bf5bd 130static DWORD main_thread_id;
131
132void agent_schedule_callback(void (*callback)(void *, void *, int),
133 void *callback_ctx, void *data, int len)
134{
135 struct agent_callback *c = snew(struct agent_callback);
136 c->callback = callback;
137 c->callback_ctx = callback_ctx;
138 c->data = data;
139 c->len = len;
140 PostThreadMessage(main_thread_id, WM_AGENT_CALLBACK, 0, (LPARAM)c);
141}
142
d8426c54 143/*
144 * Short description of parameters.
145 */
146static void usage(void)
147{
148 printf("PuTTY Link: command-line connection utility\n");
149 printf("%s\n", ver);
150 printf("Usage: plink [options] [user@]host [command]\n");
e672967c 151 printf(" (\"host\" can also be a PuTTY saved session name)\n");
d8426c54 152 printf("Options:\n");
2285d016 153 printf(" -V print version information and exit\n");
154 printf(" -pgpfp print PGP key fingerprints and exit\n");
d8426c54 155 printf(" -v show verbose messages\n");
e2a197cf 156 printf(" -load sessname Load settings from saved session\n");
157 printf(" -ssh -telnet -rlogin -raw\n");
afd4d0d2 158 printf(" force use of a particular protocol\n");
d8426c54 159 printf(" -P port connect to specified port\n");
e2a197cf 160 printf(" -l user connect with specified username\n");
e2a197cf 161 printf(" -batch disable all interactive prompts\n");
162 printf("The following options only apply to SSH connections:\n");
163 printf(" -pw passw login with specified password\n");
dbe6c525 164 printf(" -D [listen-IP:]listen-port\n");
165 printf(" Dynamic SOCKS-based port forwarding\n");
166 printf(" -L [listen-IP:]listen-port:host:port\n");
167 printf(" Forward local port to remote address\n");
168 printf(" -R [listen-IP:]listen-port:host:port\n");
169 printf(" Forward remote port to local address\n");
e2a197cf 170 printf(" -X -x enable / disable X11 forwarding\n");
171 printf(" -A -a enable / disable agent forwarding\n");
172 printf(" -t -T enable / disable pty allocation\n");
173 printf(" -1 -2 force use of particular protocol version\n");
05581745 174 printf(" -4 -6 force use of IPv4 or IPv6\n");
e2a197cf 175 printf(" -C enable compression\n");
176 printf(" -i key private key file for authentication\n");
e5708bc7 177 printf(" -noagent disable use of Pageant\n");
178 printf(" -agent enable use of Pageant\n");
54018d95 179 printf(" -m file read remote command(s) from file\n");
4d1cdf5d 180 printf(" -s remote command is an SSH subsystem (SSH-2 only)\n");
b72c366d 181 printf(" -N don't start a shell/command (SSH-2 only)\n");
23828b7e 182 printf(" -nc host:port\n");
183 printf(" open tunnel in place of session (SSH-2 only)\n");
dc108ebc 184 exit(1);
185}
186
187static void version(void)
188{
189 printf("plink: %s\n", ver);
d8426c54 190 exit(1);
191}
192
32874aea 193char *do_select(SOCKET skt, int startup)
194{
8df7a775 195 int events;
196 if (startup) {
3ad9d396 197 events = (FD_CONNECT | FD_READ | FD_WRITE |
198 FD_OOB | FD_CLOSE | FD_ACCEPT);
8df7a775 199 } else {
200 events = 0;
201 }
7440fd44 202 if (p_WSAEventSelect(skt, netevent, events) == SOCKET_ERROR) {
203 switch (p_WSAGetLastError()) {
32874aea 204 case WSAENETDOWN:
205 return "Network is down";
206 default:
7440fd44 207 return "WSAEventSelect(): unknown error";
32874aea 208 }
8df7a775 209 }
210 return NULL;
211}
212
34292b1d 213int stdin_gotdata(struct handle *h, void *data, int len)
214{
215 if (len < 0) {
216 /*
217 * Special case: report read error.
218 */
219 fprintf(stderr, "Unable to read from standard input\n");
220 cleanup_exit(0);
221 }
222 noise_ultralight(len);
6226c939 223 if (connopen && back->connected(backhandle)) {
34292b1d 224 if (len > 0) {
225 return back->send(backhandle, data, len);
226 } else {
227 back->special(backhandle, TS_EOF);
228 return 0;
229 }
230 } else
231 return 0;
232}
233
234void stdouterr_sent(struct handle *h, int new_backlog)
235{
236 if (new_backlog < 0) {
237 /*
238 * Special case: report write error.
239 */
240 fprintf(stderr, "Unable to write to standard %s\n",
241 (h == stdout_handle ? "output" : "error"));
242 cleanup_exit(0);
243 }
6226c939 244 if (connopen && back->connected(backhandle)) {
34292b1d 245 back->unthrottle(backhandle, (handle_backlog(stdout_handle) +
246 handle_backlog(stderr_handle)));
247 }
248}
249
32874aea 250int main(int argc, char **argv)
251{
12dc4ec0 252 int sending;
d8426c54 253 int portnumber = -1;
8df7a775 254 SOCKET *sklist;
255 int skcount, sksize;
d8d6c7e5 256 int exitcode;
86256dc6 257 int errors;
4d1cdf5d 258 int use_subsystem = 0;
39934deb 259 long now, next;
12dc4ec0 260
32874aea 261 sklist = NULL;
262 skcount = sksize = 0;
c9bdcd96 263 /*
264 * Initialise port and protocol to sensible defaults. (These
265 * will be overridden by more or less anything.)
266 */
267 default_protocol = PROT_SSH;
268 default_port = 22;
8df7a775 269
67779be7 270 flags = FLAG_STDERR;
12dc4ec0 271 /*
272 * Process the command line.
273 */
a9422f39 274 do_defaults(NULL, &cfg);
18e62ad8 275 loaded_session = FALSE;
e7a7383f 276 default_protocol = cfg.protocol;
277 default_port = cfg.port;
86256dc6 278 errors = 0;
8cb9c947 279 {
32874aea 280 /*
281 * Override the default protocol if PLINK_PROTOCOL is set.
282 */
283 char *p = getenv("PLINK_PROTOCOL");
284 int i;
285 if (p) {
286 for (i = 0; backends[i].backend != NULL; i++) {
287 if (!strcmp(backends[i].name, p)) {
288 default_protocol = cfg.protocol = backends[i].protocol;
289 default_port = cfg.port =
290 backends[i].backend->default_port;
291 break;
292 }
293 }
294 }
8cb9c947 295 }
12dc4ec0 296 while (--argc) {
32874aea 297 char *p = *++argv;
298 if (*p == '-') {
5555d393 299 int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
300 1, &cfg);
c0a81592 301 if (ret == -2) {
302 fprintf(stderr,
303 "plink: option \"%s\" requires an argument\n", p);
86256dc6 304 errors = 1;
c0a81592 305 } else if (ret == 2) {
306 --argc, ++argv;
307 } else if (ret == 1) {
308 continue;
ff2ae367 309 } else if (!strcmp(p, "-batch")) {
c0a81592 310 console_batch_mode = 1;
4d1cdf5d 311 } else if (!strcmp(p, "-s")) {
312 /* Save status to write to cfg later. */
313 use_subsystem = 1;
dc108ebc 314 } else if (!strcmp(p, "-V")) {
315 version();
2285d016 316 } else if (!strcmp(p, "-pgpfp")) {
317 pgp_fingerprints();
318 exit(1);
86256dc6 319 } else {
320 fprintf(stderr, "plink: unknown option \"%s\"\n", p);
321 errors = 1;
32874aea 322 }
12dc4ec0 323 } else if (*p) {
7374c779 324 if (!cfg_launchable(&cfg)) {
32874aea 325 char *q = p;
326 /*
327 * If the hostname starts with "telnet:", set the
328 * protocol to Telnet and process the string as a
329 * Telnet URL.
330 */
331 if (!strncmp(q, "telnet:", 7)) {
332 char c;
333
334 q += 7;
335 if (q[0] == '/' && q[1] == '/')
336 q += 2;
337 cfg.protocol = PROT_TELNET;
338 p = q;
339 while (*p && *p != ':' && *p != '/')
340 p++;
341 c = *p;
342 if (*p)
343 *p++ = '\0';
344 if (c == ':')
345 cfg.port = atoi(p);
346 else
347 cfg.port = -1;
348 strncpy(cfg.host, q, sizeof(cfg.host) - 1);
349 cfg.host[sizeof(cfg.host) - 1] = '\0';
350 } else {
3608528b 351 char *r, *user, *host;
32874aea 352 /*
353 * Before we process the [user@]host string, we
354 * first check for the presence of a protocol
355 * prefix (a protocol name followed by ",").
356 */
357 r = strchr(p, ',');
358 if (r) {
359 int i, j;
360 for (i = 0; backends[i].backend != NULL; i++) {
361 j = strlen(backends[i].name);
362 if (j == r - p &&
363 !memcmp(backends[i].name, p, j)) {
364 default_protocol = cfg.protocol =
365 backends[i].protocol;
366 portnumber =
367 backends[i].backend->default_port;
368 p = r + 1;
369 break;
370 }
371 }
372 }
373
374 /*
3608528b 375 * A nonzero length string followed by an @ is treated
376 * as a username. (We discount an _initial_ @.) The
377 * rest of the string (or the whole string if no @)
378 * is treated as a session name and/or hostname.
32874aea 379 */
380 r = strrchr(p, '@');
381 if (r == p)
382 p++, r = NULL; /* discount initial @ */
3608528b 383 if (r) {
384 *r++ = '\0';
385 user = p, host = r;
386 } else {
387 user = NULL, host = p;
388 }
389
390 /*
391 * Now attempt to load a saved session with the
392 * same name as the hostname.
393 */
394 {
32874aea 395 Config cfg2;
3608528b 396 do_defaults(host, &cfg2);
7374c779 397 if (loaded_session || !cfg_launchable(&cfg2)) {
32874aea 398 /* No settings for this host; use defaults */
18e62ad8 399 /* (or session was already loaded with -load) */
3608528b 400 strncpy(cfg.host, host, sizeof(cfg.host) - 1);
32874aea 401 cfg.host[sizeof(cfg.host) - 1] = '\0';
402 cfg.port = default_port;
403 } else {
404 cfg = cfg2;
32874aea 405 }
3608528b 406 }
407
408 if (user) {
409 /* Patch in specified username. */
410 strncpy(cfg.username, user,
411 sizeof(cfg.username) - 1);
32874aea 412 cfg.username[sizeof(cfg.username) - 1] = '\0';
32874aea 413 }
3608528b 414
32874aea 415 }
416 } else {
385528da 417 char *command;
418 int cmdlen, cmdsize;
419 cmdlen = cmdsize = 0;
420 command = NULL;
421
422 while (argc) {
423 while (*p) {
424 if (cmdlen >= cmdsize) {
425 cmdsize = cmdlen + 512;
3d88e64d 426 command = sresize(command, cmdsize, char);
385528da 427 }
428 command[cmdlen++]=*p++;
429 }
430 if (cmdlen >= cmdsize) {
431 cmdsize = cmdlen + 512;
3d88e64d 432 command = sresize(command, cmdsize, char);
385528da 433 }
434 command[cmdlen++]=' '; /* always add trailing space */
435 if (--argc) p = *++argv;
32874aea 436 }
385528da 437 if (cmdlen) command[--cmdlen]='\0';
438 /* change trailing blank to NUL */
439 cfg.remote_cmd_ptr = command;
440 cfg.remote_cmd_ptr2 = NULL;
32874aea 441 cfg.nopty = TRUE; /* command => no terminal */
385528da 442
32874aea 443 break; /* done with cmdline */
444 }
12dc4ec0 445 }
446 }
447
86256dc6 448 if (errors)
449 return 1;
450
7374c779 451 if (!cfg_launchable(&cfg)) {
32874aea 452 usage();
d8426c54 453 }
d8426c54 454
449925a6 455 /*
456 * Trim leading whitespace off the hostname if it's there.
457 */
458 {
459 int space = strspn(cfg.host, " \t");
460 memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space);
461 }
462
463 /* See if host is of the form user@host */
7374c779 464 if (cfg_launchable(&cfg)) {
5dd103a8 465 char *atsign = strrchr(cfg.host, '@');
449925a6 466 /* Make sure we're not overflowing the user field */
467 if (atsign) {
468 if (atsign - cfg.host < sizeof cfg.username) {
469 strncpy(cfg.username, cfg.host, atsign - cfg.host);
470 cfg.username[atsign - cfg.host] = '\0';
471 }
472 memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1));
473 }
474 }
475
476 /*
c0a81592 477 * Perform command-line overrides on session configuration.
478 */
5555d393 479 cmdline_run_saved(&cfg);
c0a81592 480
481 /*
4d1cdf5d 482 * Apply subsystem status.
483 */
484 if (use_subsystem)
485 cfg.ssh_subsys = TRUE;
486
487 /*
449925a6 488 * Trim a colon suffix off the hostname if it's there.
489 */
490 cfg.host[strcspn(cfg.host, ":")] = '\0';
491
cae0c023 492 /*
493 * Remove any remaining whitespace from the hostname.
494 */
495 {
496 int p1 = 0, p2 = 0;
497 while (cfg.host[p2] != '\0') {
498 if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') {
499 cfg.host[p1] = cfg.host[p2];
500 p1++;
501 }
502 p2++;
503 }
504 cfg.host[p1] = '\0';
505 }
506
feb02b4e 507 if (!cfg.remote_cmd_ptr && !*cfg.remote_cmd && !*cfg.ssh_nc_host)
32874aea 508 flags |= FLAG_INTERACTIVE;
67779be7 509
12dc4ec0 510 /*
511 * Select protocol. This is farmed out into a table in a
512 * separate file to enable an ssh-free variant.
513 */
514 {
32874aea 515 int i;
516 back = NULL;
517 for (i = 0; backends[i].backend != NULL; i++)
518 if (backends[i].protocol == cfg.protocol) {
519 back = backends[i].backend;
520 break;
521 }
522 if (back == NULL) {
523 fprintf(stderr,
524 "Internal fault: Unsupported protocol found\n");
525 return 1;
526 }
12dc4ec0 527 }
528
529 /*
8cb9c947 530 * Select port.
531 */
532 if (portnumber != -1)
32874aea 533 cfg.port = portnumber;
8cb9c947 534
7440fd44 535 sk_init();
536 if (p_WSAEventSelect == NULL) {
537 fprintf(stderr, "Plink requires WinSock 2\n");
12dc4ec0 538 return 1;
539 }
540
1626d169 541 logctx = log_init(NULL, &cfg);
542 console_provide_logctx(logctx);
543
12dc4ec0 544 /*
545 * Start up the connection.
546 */
8df7a775 547 netevent = CreateEvent(NULL, FALSE, FALSE, NULL);
12dc4ec0 548 {
cbe2d68f 549 const char *error;
12dc4ec0 550 char *realhost;
2184a5d9 551 /* nodelay is only useful if stdin is a character device (console) */
552 int nodelay = cfg.tcp_nodelay &&
553 (GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_CHAR);
12dc4ec0 554
86916870 555 error = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port,
79bf227b 556 &realhost, nodelay, cfg.tcp_keepalives);
12dc4ec0 557 if (error) {
558 fprintf(stderr, "Unable to open connection:\n%s", error);
559 return 1;
560 }
a8327734 561 back->provide_logctx(backhandle, logctx);
6e1ebb76 562 sfree(realhost);
12dc4ec0 563 }
8df7a775 564 connopen = 1;
12dc4ec0 565
0965bee0 566 inhandle = GetStdHandle(STD_INPUT_HANDLE);
12dc4ec0 567 outhandle = GetStdHandle(STD_OUTPUT_HANDLE);
fe50e814 568 errhandle = GetStdHandle(STD_ERROR_HANDLE);
34292b1d 569
12dc4ec0 570 /*
12dc4ec0 571 * Turn off ECHO and LINE input modes. We don't care if this
572 * call fails, because we know we aren't necessarily running in
573 * a console.
574 */
f8efb0d5 575 GetConsoleMode(inhandle, &orig_console_mode);
576 SetConsoleMode(inhandle, ENABLE_PROCESSED_INPUT);
577
34292b1d 578 /*
579 * Pass the output handles to the handle-handling subsystem.
580 * (The input one we leave until we're through the
581 * authentication process.)
582 */
bdebd7e9 583 stdout_handle = handle_output_new(outhandle, stdouterr_sent, NULL, 0);
584 stderr_handle = handle_output_new(errhandle, stdouterr_sent, NULL, 0);
34292b1d 585
f8efb0d5 586 main_thread_id = GetCurrentThreadId();
587
12dc4ec0 588 sending = FALSE;
5471d09a 589
39934deb 590 now = GETTICKCOUNT();
591
12dc4ec0 592 while (1) {
34292b1d 593 int nhandles;
594 HANDLE *handles;
32874aea 595 int n;
39934deb 596 DWORD ticks;
32874aea 597
51470298 598 if (!sending && back->sendok(backhandle)) {
bdebd7e9 599 stdin_handle = handle_input_new(inhandle, stdin_gotdata, NULL,
600 0);
32874aea 601 sending = TRUE;
602 }
603
39934deb 604 if (run_timers(now, &next)) {
605 ticks = next - GETTICKCOUNT();
606 if (ticks < 0) ticks = 0; /* just in case */
607 } else {
608 ticks = INFINITE;
609 }
610
34292b1d 611 handles = handle_get_events(&nhandles);
612 handles = sresize(handles, nhandles+1, HANDLE);
613 handles[nhandles] = netevent;
614 n = MsgWaitForMultipleObjects(nhandles+1, handles, FALSE, ticks,
c44bf5bd 615 QS_POSTMESSAGE);
34292b1d 616 if ((unsigned)(n - WAIT_OBJECT_0) < (unsigned)nhandles) {
617 handle_got_event(handles[n - WAIT_OBJECT_0]);
618 } else if (n == WAIT_OBJECT_0 + nhandles) {
32874aea 619 WSANETWORKEVENTS things;
8df7a775 620 SOCKET socket;
d2371c81 621 extern SOCKET first_socket(int *), next_socket(int *);
8df7a775 622 extern int select_result(WPARAM, LPARAM);
32874aea 623 int i, socketstate;
624
625 /*
626 * We must not call select_result() for any socket
627 * until we have finished enumerating within the tree.
628 * This is because select_result() may close the socket
629 * and modify the tree.
630 */
631 /* Count the active sockets. */
632 i = 0;
633 for (socket = first_socket(&socketstate);
634 socket != INVALID_SOCKET;
635 socket = next_socket(&socketstate)) i++;
636
637 /* Expand the buffer if necessary. */
638 if (i > sksize) {
639 sksize = i + 16;
3d88e64d 640 sklist = sresize(sklist, sksize, SOCKET);
32874aea 641 }
642
643 /* Retrieve the sockets into sklist. */
644 skcount = 0;
645 for (socket = first_socket(&socketstate);
646 socket != INVALID_SOCKET;
d2371c81 647 socket = next_socket(&socketstate)) {
32874aea 648 sklist[skcount++] = socket;
649 }
650
651 /* Now we're done enumerating; go through the list. */
652 for (i = 0; i < skcount; i++) {
653 WPARAM wp;
654 socket = sklist[i];
655 wp = (WPARAM) socket;
7440fd44 656 if (!p_WSAEnumNetworkEvents(socket, NULL, &things)) {
64cdd21b 657 static const struct { int bit, mask; } eventtypes[] = {
658 {FD_CONNECT_BIT, FD_CONNECT},
659 {FD_READ_BIT, FD_READ},
660 {FD_CLOSE_BIT, FD_CLOSE},
661 {FD_OOB_BIT, FD_OOB},
662 {FD_WRITE_BIT, FD_WRITE},
663 {FD_ACCEPT_BIT, FD_ACCEPT},
664 };
665 int e;
666
32874aea 667 noise_ultralight(socket);
668 noise_ultralight(things.lNetworkEvents);
d74d141c 669
64cdd21b 670 for (e = 0; e < lenof(eventtypes); e++)
671 if (things.lNetworkEvents & eventtypes[e].mask) {
672 LPARAM lp;
673 int err = things.iErrorCode[eventtypes[e].bit];
674 lp = WSAMAKESELECTREPLY(eventtypes[e].mask, err);
675 connopen &= select_result(wp, lp);
676 }
8df7a775 677 }
678 }
34292b1d 679 } else if (n == WAIT_OBJECT_0 + nhandles + 1) {
c44bf5bd 680 MSG msg;
681 while (PeekMessage(&msg, INVALID_HANDLE_VALUE,
682 WM_AGENT_CALLBACK, WM_AGENT_CALLBACK,
683 PM_REMOVE)) {
684 struct agent_callback *c = (struct agent_callback *)msg.lParam;
685 c->callback(c->callback_ctx, c->data, c->len);
686 sfree(c);
687 }
5471d09a 688 }
39934deb 689
690 if (n == WAIT_TIMEOUT) {
691 now = next;
692 } else {
693 now = GETTICKCOUNT();
694 }
695
34292b1d 696 sfree(handles);
697
698 if (sending)
699 handle_unthrottle(stdin_handle, back->sendbuffer(backhandle));
700
6226c939 701 if ((!connopen || !back->connected(backhandle)) &&
34292b1d 702 handle_backlog(stdout_handle) + handle_backlog(stderr_handle) == 0)
32874aea 703 break; /* we closed the connection */
12dc4ec0 704 }
51470298 705 exitcode = back->exitcode(backhandle);
d8d6c7e5 706 if (exitcode < 0) {
707 fprintf(stderr, "Remote process exit code unavailable\n");
708 exitcode = 1; /* this is an error condition */
709 }
7440fd44 710 cleanup_exit(exitcode);
711 return 0; /* placate compiler warning */
12dc4ec0 712}