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