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