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