The `socket' function in the backends is only ever checked to see if
[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");
dc108ebc 182 exit(1);
183}
184
185static void version(void)
186{
187 printf("plink: %s\n", ver);
d8426c54 188 exit(1);
189}
190
32874aea 191char *do_select(SOCKET skt, int startup)
192{
8df7a775 193 int events;
194 if (startup) {
3ad9d396 195 events = (FD_CONNECT | FD_READ | FD_WRITE |
196 FD_OOB | FD_CLOSE | FD_ACCEPT);
8df7a775 197 } else {
198 events = 0;
199 }
7440fd44 200 if (p_WSAEventSelect(skt, netevent, events) == SOCKET_ERROR) {
201 switch (p_WSAGetLastError()) {
32874aea 202 case WSAENETDOWN:
203 return "Network is down";
204 default:
7440fd44 205 return "WSAEventSelect(): unknown error";
32874aea 206 }
8df7a775 207 }
208 return NULL;
209}
210
34292b1d 211int stdin_gotdata(struct handle *h, void *data, int len)
212{
213 if (len < 0) {
214 /*
215 * Special case: report read error.
216 */
217 fprintf(stderr, "Unable to read from standard input\n");
218 cleanup_exit(0);
219 }
220 noise_ultralight(len);
6226c939 221 if (connopen && back->connected(backhandle)) {
34292b1d 222 if (len > 0) {
223 return back->send(backhandle, data, len);
224 } else {
225 back->special(backhandle, TS_EOF);
226 return 0;
227 }
228 } else
229 return 0;
230}
231
232void stdouterr_sent(struct handle *h, int new_backlog)
233{
234 if (new_backlog < 0) {
235 /*
236 * Special case: report write error.
237 */
238 fprintf(stderr, "Unable to write to standard %s\n",
239 (h == stdout_handle ? "output" : "error"));
240 cleanup_exit(0);
241 }
6226c939 242 if (connopen && back->connected(backhandle)) {
34292b1d 243 back->unthrottle(backhandle, (handle_backlog(stdout_handle) +
244 handle_backlog(stderr_handle)));
245 }
246}
247
32874aea 248int main(int argc, char **argv)
249{
12dc4ec0 250 int sending;
d8426c54 251 int portnumber = -1;
8df7a775 252 SOCKET *sklist;
253 int skcount, sksize;
d8d6c7e5 254 int exitcode;
86256dc6 255 int errors;
4d1cdf5d 256 int use_subsystem = 0;
39934deb 257 long now, next;
12dc4ec0 258
32874aea 259 sklist = NULL;
260 skcount = sksize = 0;
c9bdcd96 261 /*
262 * Initialise port and protocol to sensible defaults. (These
263 * will be overridden by more or less anything.)
264 */
265 default_protocol = PROT_SSH;
266 default_port = 22;
8df7a775 267
67779be7 268 flags = FLAG_STDERR;
12dc4ec0 269 /*
270 * Process the command line.
271 */
a9422f39 272 do_defaults(NULL, &cfg);
18e62ad8 273 loaded_session = FALSE;
e7a7383f 274 default_protocol = cfg.protocol;
275 default_port = cfg.port;
86256dc6 276 errors = 0;
8cb9c947 277 {
32874aea 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 }
8cb9c947 293 }
12dc4ec0 294 while (--argc) {
32874aea 295 char *p = *++argv;
296 if (*p == '-') {
5555d393 297 int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
298 1, &cfg);
c0a81592 299 if (ret == -2) {
300 fprintf(stderr,
301 "plink: option \"%s\" requires an argument\n", p);
86256dc6 302 errors = 1;
c0a81592 303 } else if (ret == 2) {
304 --argc, ++argv;
305 } else if (ret == 1) {
306 continue;
ff2ae367 307 } else if (!strcmp(p, "-batch")) {
c0a81592 308 console_batch_mode = 1;
4d1cdf5d 309 } else if (!strcmp(p, "-s")) {
310 /* Save status to write to cfg later. */
311 use_subsystem = 1;
dc108ebc 312 } else if (!strcmp(p, "-V")) {
313 version();
2285d016 314 } else if (!strcmp(p, "-pgpfp")) {
315 pgp_fingerprints();
316 exit(1);
86256dc6 317 } else {
318 fprintf(stderr, "plink: unknown option \"%s\"\n", p);
319 errors = 1;
32874aea 320 }
12dc4ec0 321 } else if (*p) {
32874aea 322 if (!*cfg.host) {
323 char *q = p;
324 /*
325 * If the hostname starts with "telnet:", set the
326 * protocol to Telnet and process the string as a
327 * Telnet URL.
328 */
329 if (!strncmp(q, "telnet:", 7)) {
330 char c;
331
332 q += 7;
333 if (q[0] == '/' && q[1] == '/')
334 q += 2;
335 cfg.protocol = PROT_TELNET;
336 p = q;
337 while (*p && *p != ':' && *p != '/')
338 p++;
339 c = *p;
340 if (*p)
341 *p++ = '\0';
342 if (c == ':')
343 cfg.port = atoi(p);
344 else
345 cfg.port = -1;
346 strncpy(cfg.host, q, sizeof(cfg.host) - 1);
347 cfg.host[sizeof(cfg.host) - 1] = '\0';
348 } else {
3608528b 349 char *r, *user, *host;
32874aea 350 /*
351 * Before we process the [user@]host string, we
352 * first check for the presence of a protocol
353 * prefix (a protocol name followed by ",").
354 */
355 r = strchr(p, ',');
356 if (r) {
357 int i, j;
358 for (i = 0; backends[i].backend != NULL; i++) {
359 j = strlen(backends[i].name);
360 if (j == r - p &&
361 !memcmp(backends[i].name, p, j)) {
362 default_protocol = cfg.protocol =
363 backends[i].protocol;
364 portnumber =
365 backends[i].backend->default_port;
366 p = r + 1;
367 break;
368 }
369 }
370 }
371
372 /*
3608528b 373 * A nonzero length string followed by an @ is treated
374 * as a username. (We discount an _initial_ @.) The
375 * rest of the string (or the whole string if no @)
376 * is treated as a session name and/or hostname.
32874aea 377 */
378 r = strrchr(p, '@');
379 if (r == p)
380 p++, r = NULL; /* discount initial @ */
3608528b 381 if (r) {
382 *r++ = '\0';
383 user = p, host = r;
384 } else {
385 user = NULL, host = p;
386 }
387
388 /*
389 * Now attempt to load a saved session with the
390 * same name as the hostname.
391 */
392 {
32874aea 393 Config cfg2;
3608528b 394 do_defaults(host, &cfg2);
18e62ad8 395 if (loaded_session || cfg2.host[0] == '\0') {
32874aea 396 /* No settings for this host; use defaults */
18e62ad8 397 /* (or session was already loaded with -load) */
3608528b 398 strncpy(cfg.host, host, sizeof(cfg.host) - 1);
32874aea 399 cfg.host[sizeof(cfg.host) - 1] = '\0';
400 cfg.port = default_port;
401 } else {
402 cfg = cfg2;
32874aea 403 }
3608528b 404 }
405
406 if (user) {
407 /* Patch in specified username. */
408 strncpy(cfg.username, user,
409 sizeof(cfg.username) - 1);
32874aea 410 cfg.username[sizeof(cfg.username) - 1] = '\0';
32874aea 411 }
3608528b 412
32874aea 413 }
414 } else {
385528da 415 char *command;
416 int cmdlen, cmdsize;
417 cmdlen = cmdsize = 0;
418 command = NULL;
419
420 while (argc) {
421 while (*p) {
422 if (cmdlen >= cmdsize) {
423 cmdsize = cmdlen + 512;
3d88e64d 424 command = sresize(command, cmdsize, char);
385528da 425 }
426 command[cmdlen++]=*p++;
427 }
428 if (cmdlen >= cmdsize) {
429 cmdsize = cmdlen + 512;
3d88e64d 430 command = sresize(command, cmdsize, char);
385528da 431 }
432 command[cmdlen++]=' '; /* always add trailing space */
433 if (--argc) p = *++argv;
32874aea 434 }
385528da 435 if (cmdlen) command[--cmdlen]='\0';
436 /* change trailing blank to NUL */
437 cfg.remote_cmd_ptr = command;
438 cfg.remote_cmd_ptr2 = NULL;
32874aea 439 cfg.nopty = TRUE; /* command => no terminal */
385528da 440
32874aea 441 break; /* done with cmdline */
442 }
12dc4ec0 443 }
444 }
445
86256dc6 446 if (errors)
447 return 1;
448
d8426c54 449 if (!*cfg.host) {
32874aea 450 usage();
d8426c54 451 }
d8426c54 452
449925a6 453 /*
454 * Trim leading whitespace off the hostname if it's there.
455 */
456 {
457 int space = strspn(cfg.host, " \t");
458 memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space);
459 }
460
461 /* See if host is of the form user@host */
462 if (cfg.host[0] != '\0') {
5dd103a8 463 char *atsign = strrchr(cfg.host, '@');
449925a6 464 /* Make sure we're not overflowing the user field */
465 if (atsign) {
466 if (atsign - cfg.host < sizeof cfg.username) {
467 strncpy(cfg.username, cfg.host, atsign - cfg.host);
468 cfg.username[atsign - cfg.host] = '\0';
469 }
470 memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1));
471 }
472 }
473
474 /*
c0a81592 475 * Perform command-line overrides on session configuration.
476 */
5555d393 477 cmdline_run_saved(&cfg);
c0a81592 478
479 /*
4d1cdf5d 480 * Apply subsystem status.
481 */
482 if (use_subsystem)
483 cfg.ssh_subsys = TRUE;
484
485 /*
449925a6 486 * Trim a colon suffix off the hostname if it's there.
487 */
488 cfg.host[strcspn(cfg.host, ":")] = '\0';
489
cae0c023 490 /*
491 * Remove any remaining whitespace from the hostname.
492 */
493 {
494 int p1 = 0, p2 = 0;
495 while (cfg.host[p2] != '\0') {
496 if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') {
497 cfg.host[p1] = cfg.host[p2];
498 p1++;
499 }
500 p2++;
501 }
502 cfg.host[p1] = '\0';
503 }
504
a79e1969 505 if (!cfg.remote_cmd_ptr && !*cfg.remote_cmd)
32874aea 506 flags |= FLAG_INTERACTIVE;
67779be7 507
12dc4ec0 508 /*
509 * Select protocol. This is farmed out into a table in a
510 * separate file to enable an ssh-free variant.
511 */
512 {
32874aea 513 int i;
514 back = NULL;
515 for (i = 0; backends[i].backend != NULL; i++)
516 if (backends[i].protocol == cfg.protocol) {
517 back = backends[i].backend;
518 break;
519 }
520 if (back == NULL) {
521 fprintf(stderr,
522 "Internal fault: Unsupported protocol found\n");
523 return 1;
524 }
12dc4ec0 525 }
526
527 /*
8cb9c947 528 * Select port.
529 */
530 if (portnumber != -1)
32874aea 531 cfg.port = portnumber;
8cb9c947 532
7440fd44 533 sk_init();
534 if (p_WSAEventSelect == NULL) {
535 fprintf(stderr, "Plink requires WinSock 2\n");
12dc4ec0 536 return 1;
537 }
538
539 /*
540 * Start up the connection.
541 */
8df7a775 542 netevent = CreateEvent(NULL, FALSE, FALSE, NULL);
12dc4ec0 543 {
cbe2d68f 544 const char *error;
12dc4ec0 545 char *realhost;
2184a5d9 546 /* nodelay is only useful if stdin is a character device (console) */
547 int nodelay = cfg.tcp_nodelay &&
548 (GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_CHAR);
12dc4ec0 549
86916870 550 error = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port,
79bf227b 551 &realhost, nodelay, cfg.tcp_keepalives);
12dc4ec0 552 if (error) {
553 fprintf(stderr, "Unable to open connection:\n%s", error);
554 return 1;
555 }
c229ef97 556 logctx = log_init(NULL, &cfg);
a8327734 557 back->provide_logctx(backhandle, logctx);
d3fef4a5 558 console_provide_logctx(logctx);
6e1ebb76 559 sfree(realhost);
12dc4ec0 560 }
8df7a775 561 connopen = 1;
12dc4ec0 562
0965bee0 563 inhandle = GetStdHandle(STD_INPUT_HANDLE);
12dc4ec0 564 outhandle = GetStdHandle(STD_OUTPUT_HANDLE);
fe50e814 565 errhandle = GetStdHandle(STD_ERROR_HANDLE);
34292b1d 566
12dc4ec0 567 /*
12dc4ec0 568 * Turn off ECHO and LINE input modes. We don't care if this
569 * call fails, because we know we aren't necessarily running in
570 * a console.
571 */
f8efb0d5 572 GetConsoleMode(inhandle, &orig_console_mode);
573 SetConsoleMode(inhandle, ENABLE_PROCESSED_INPUT);
574
34292b1d 575 /*
576 * Pass the output handles to the handle-handling subsystem.
577 * (The input one we leave until we're through the
578 * authentication process.)
579 */
0e03ceff 580 stdout_handle = handle_output_new(outhandle, stdouterr_sent, NULL);
581 stderr_handle = handle_output_new(errhandle, stdouterr_sent, NULL);
34292b1d 582
f8efb0d5 583 main_thread_id = GetCurrentThreadId();
584
12dc4ec0 585 sending = FALSE;
5471d09a 586
39934deb 587 now = GETTICKCOUNT();
588
12dc4ec0 589 while (1) {
34292b1d 590 int nhandles;
591 HANDLE *handles;
32874aea 592 int n;
39934deb 593 DWORD ticks;
32874aea 594
51470298 595 if (!sending && back->sendok(backhandle)) {
0e03ceff 596 stdin_handle = handle_input_new(inhandle, stdin_gotdata, NULL);
32874aea 597 sending = TRUE;
598 }
599
39934deb 600 if (run_timers(now, &next)) {
601 ticks = next - GETTICKCOUNT();
602 if (ticks < 0) ticks = 0; /* just in case */
603 } else {
604 ticks = INFINITE;
605 }
606
34292b1d 607 handles = handle_get_events(&nhandles);
608 handles = sresize(handles, nhandles+1, HANDLE);
609 handles[nhandles] = netevent;
610 n = MsgWaitForMultipleObjects(nhandles+1, handles, FALSE, ticks,
c44bf5bd 611 QS_POSTMESSAGE);
34292b1d 612 if ((unsigned)(n - WAIT_OBJECT_0) < (unsigned)nhandles) {
613 handle_got_event(handles[n - WAIT_OBJECT_0]);
614 } else if (n == WAIT_OBJECT_0 + nhandles) {
32874aea 615 WSANETWORKEVENTS things;
8df7a775 616 SOCKET socket;
d2371c81 617 extern SOCKET first_socket(int *), next_socket(int *);
8df7a775 618 extern int select_result(WPARAM, LPARAM);
32874aea 619 int i, socketstate;
620
621 /*
622 * We must not call select_result() for any socket
623 * until we have finished enumerating within the tree.
624 * This is because select_result() may close the socket
625 * and modify the tree.
626 */
627 /* Count the active sockets. */
628 i = 0;
629 for (socket = first_socket(&socketstate);
630 socket != INVALID_SOCKET;
631 socket = next_socket(&socketstate)) i++;
632
633 /* Expand the buffer if necessary. */
634 if (i > sksize) {
635 sksize = i + 16;
3d88e64d 636 sklist = sresize(sklist, sksize, SOCKET);
32874aea 637 }
638
639 /* Retrieve the sockets into sklist. */
640 skcount = 0;
641 for (socket = first_socket(&socketstate);
642 socket != INVALID_SOCKET;
d2371c81 643 socket = next_socket(&socketstate)) {
32874aea 644 sklist[skcount++] = socket;
645 }
646
647 /* Now we're done enumerating; go through the list. */
648 for (i = 0; i < skcount; i++) {
649 WPARAM wp;
650 socket = sklist[i];
651 wp = (WPARAM) socket;
7440fd44 652 if (!p_WSAEnumNetworkEvents(socket, NULL, &things)) {
64cdd21b 653 static const struct { int bit, mask; } eventtypes[] = {
654 {FD_CONNECT_BIT, FD_CONNECT},
655 {FD_READ_BIT, FD_READ},
656 {FD_CLOSE_BIT, FD_CLOSE},
657 {FD_OOB_BIT, FD_OOB},
658 {FD_WRITE_BIT, FD_WRITE},
659 {FD_ACCEPT_BIT, FD_ACCEPT},
660 };
661 int e;
662
32874aea 663 noise_ultralight(socket);
664 noise_ultralight(things.lNetworkEvents);
d74d141c 665
64cdd21b 666 for (e = 0; e < lenof(eventtypes); e++)
667 if (things.lNetworkEvents & eventtypes[e].mask) {
668 LPARAM lp;
669 int err = things.iErrorCode[eventtypes[e].bit];
670 lp = WSAMAKESELECTREPLY(eventtypes[e].mask, err);
671 connopen &= select_result(wp, lp);
672 }
8df7a775 673 }
674 }
34292b1d 675 } else if (n == WAIT_OBJECT_0 + nhandles + 1) {
c44bf5bd 676 MSG msg;
677 while (PeekMessage(&msg, INVALID_HANDLE_VALUE,
678 WM_AGENT_CALLBACK, WM_AGENT_CALLBACK,
679 PM_REMOVE)) {
680 struct agent_callback *c = (struct agent_callback *)msg.lParam;
681 c->callback(c->callback_ctx, c->data, c->len);
682 sfree(c);
683 }
5471d09a 684 }
39934deb 685
686 if (n == WAIT_TIMEOUT) {
687 now = next;
688 } else {
689 now = GETTICKCOUNT();
690 }
691
34292b1d 692 sfree(handles);
693
694 if (sending)
695 handle_unthrottle(stdin_handle, back->sendbuffer(backhandle));
696
6226c939 697 if ((!connopen || !back->connected(backhandle)) &&
34292b1d 698 handle_backlog(stdout_handle) + handle_backlog(stderr_handle) == 0)
32874aea 699 break; /* we closed the connection */
12dc4ec0 700 }
51470298 701 exitcode = back->exitcode(backhandle);
d8d6c7e5 702 if (exitcode < 0) {
703 fprintf(stderr, "Remote process exit code unavailable\n");
704 exitcode = 1; /* this is an error condition */
705 }
7440fd44 706 cleanup_exit(exitcode);
707 return 0; /* placate compiler warning */
12dc4ec0 708}