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"); |
2285d016 |
213 | printf(" -V print version information and exit\n"); |
214 | printf(" -pgpfp print PGP key fingerprints and exit\n"); |
d8426c54 |
215 | printf(" -v show verbose messages\n"); |
e2a197cf |
216 | printf(" -load sessname Load settings from saved session\n"); |
217 | printf(" -ssh -telnet -rlogin -raw\n"); |
afd4d0d2 |
218 | printf(" force use of a particular protocol\n"); |
d8426c54 |
219 | printf(" -P port connect to specified port\n"); |
e2a197cf |
220 | printf(" -l user connect with specified username\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"); |
05581745 |
234 | printf(" -4 -6 force use of IPv4 or IPv6\n"); |
e2a197cf |
235 | printf(" -C enable compression\n"); |
236 | printf(" -i key private key file for authentication\n"); |
54018d95 |
237 | printf(" -m file read remote command(s) from file\n"); |
4d1cdf5d |
238 | printf(" -s remote command is an SSH subsystem (SSH-2 only)\n"); |
b72c366d |
239 | printf(" -N don't start a shell/command (SSH-2 only)\n"); |
dc108ebc |
240 | exit(1); |
241 | } |
242 | |
243 | static void version(void) |
244 | { |
245 | printf("plink: %s\n", ver); |
d8426c54 |
246 | exit(1); |
247 | } |
248 | |
32874aea |
249 | char *do_select(SOCKET skt, int startup) |
250 | { |
8df7a775 |
251 | int events; |
252 | if (startup) { |
3ad9d396 |
253 | events = (FD_CONNECT | FD_READ | FD_WRITE | |
254 | FD_OOB | FD_CLOSE | FD_ACCEPT); |
8df7a775 |
255 | } else { |
256 | events = 0; |
257 | } |
7440fd44 |
258 | if (p_WSAEventSelect(skt, netevent, events) == SOCKET_ERROR) { |
259 | switch (p_WSAGetLastError()) { |
32874aea |
260 | case WSAENETDOWN: |
261 | return "Network is down"; |
262 | default: |
7440fd44 |
263 | return "WSAEventSelect(): unknown error"; |
32874aea |
264 | } |
8df7a775 |
265 | } |
266 | return NULL; |
267 | } |
268 | |
32874aea |
269 | int main(int argc, char **argv) |
270 | { |
5471d09a |
271 | WSAEVENT stdinevent, stdoutevent, stderrevent; |
272 | HANDLE handles[4]; |
273 | DWORD in_threadid, out_threadid, err_threadid; |
12dc4ec0 |
274 | struct input_data idata; |
5fc71a3d |
275 | int reading = FALSE; |
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; |
39934deb |
284 | long now, next; |
12dc4ec0 |
285 | |
ff2ae367 |
286 | ssh_get_line = console_get_line; |
67779be7 |
287 | |
32874aea |
288 | sklist = NULL; |
289 | skcount = sksize = 0; |
c9bdcd96 |
290 | /* |
291 | * Initialise port and protocol to sensible defaults. (These |
292 | * will be overridden by more or less anything.) |
293 | */ |
294 | default_protocol = PROT_SSH; |
295 | default_port = 22; |
8df7a775 |
296 | |
67779be7 |
297 | flags = FLAG_STDERR; |
12dc4ec0 |
298 | /* |
299 | * Process the command line. |
300 | */ |
a9422f39 |
301 | do_defaults(NULL, &cfg); |
18e62ad8 |
302 | loaded_session = FALSE; |
e7a7383f |
303 | default_protocol = cfg.protocol; |
304 | default_port = cfg.port; |
86256dc6 |
305 | errors = 0; |
8cb9c947 |
306 | { |
32874aea |
307 | /* |
308 | * Override the default protocol if PLINK_PROTOCOL is set. |
309 | */ |
310 | char *p = getenv("PLINK_PROTOCOL"); |
311 | int i; |
312 | if (p) { |
313 | for (i = 0; backends[i].backend != NULL; i++) { |
314 | if (!strcmp(backends[i].name, p)) { |
315 | default_protocol = cfg.protocol = backends[i].protocol; |
316 | default_port = cfg.port = |
317 | backends[i].backend->default_port; |
318 | break; |
319 | } |
320 | } |
321 | } |
8cb9c947 |
322 | } |
12dc4ec0 |
323 | while (--argc) { |
32874aea |
324 | char *p = *++argv; |
325 | if (*p == '-') { |
5555d393 |
326 | int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL), |
327 | 1, &cfg); |
c0a81592 |
328 | if (ret == -2) { |
329 | fprintf(stderr, |
330 | "plink: option \"%s\" requires an argument\n", p); |
86256dc6 |
331 | errors = 1; |
c0a81592 |
332 | } else if (ret == 2) { |
333 | --argc, ++argv; |
334 | } else if (ret == 1) { |
335 | continue; |
ff2ae367 |
336 | } else if (!strcmp(p, "-batch")) { |
c0a81592 |
337 | console_batch_mode = 1; |
4d1cdf5d |
338 | } else if (!strcmp(p, "-s")) { |
339 | /* Save status to write to cfg later. */ |
340 | use_subsystem = 1; |
dc108ebc |
341 | } else if (!strcmp(p, "-V")) { |
342 | version(); |
2285d016 |
343 | } else if (!strcmp(p, "-pgpfp")) { |
344 | pgp_fingerprints(); |
345 | exit(1); |
86256dc6 |
346 | } else { |
347 | fprintf(stderr, "plink: unknown option \"%s\"\n", p); |
348 | errors = 1; |
32874aea |
349 | } |
12dc4ec0 |
350 | } else if (*p) { |
32874aea |
351 | if (!*cfg.host) { |
352 | char *q = p; |
353 | /* |
354 | * If the hostname starts with "telnet:", set the |
355 | * protocol to Telnet and process the string as a |
356 | * Telnet URL. |
357 | */ |
358 | if (!strncmp(q, "telnet:", 7)) { |
359 | char c; |
360 | |
361 | q += 7; |
362 | if (q[0] == '/' && q[1] == '/') |
363 | q += 2; |
364 | cfg.protocol = PROT_TELNET; |
365 | p = q; |
366 | while (*p && *p != ':' && *p != '/') |
367 | p++; |
368 | c = *p; |
369 | if (*p) |
370 | *p++ = '\0'; |
371 | if (c == ':') |
372 | cfg.port = atoi(p); |
373 | else |
374 | cfg.port = -1; |
375 | strncpy(cfg.host, q, sizeof(cfg.host) - 1); |
376 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
377 | } else { |
3608528b |
378 | char *r, *user, *host; |
32874aea |
379 | /* |
380 | * Before we process the [user@]host string, we |
381 | * first check for the presence of a protocol |
382 | * prefix (a protocol name followed by ","). |
383 | */ |
384 | r = strchr(p, ','); |
385 | if (r) { |
386 | int i, j; |
387 | for (i = 0; backends[i].backend != NULL; i++) { |
388 | j = strlen(backends[i].name); |
389 | if (j == r - p && |
390 | !memcmp(backends[i].name, p, j)) { |
391 | default_protocol = cfg.protocol = |
392 | backends[i].protocol; |
393 | portnumber = |
394 | backends[i].backend->default_port; |
395 | p = r + 1; |
396 | break; |
397 | } |
398 | } |
399 | } |
400 | |
401 | /* |
3608528b |
402 | * A nonzero length string followed by an @ is treated |
403 | * as a username. (We discount an _initial_ @.) The |
404 | * rest of the string (or the whole string if no @) |
405 | * is treated as a session name and/or hostname. |
32874aea |
406 | */ |
407 | r = strrchr(p, '@'); |
408 | if (r == p) |
409 | p++, r = NULL; /* discount initial @ */ |
3608528b |
410 | if (r) { |
411 | *r++ = '\0'; |
412 | user = p, host = r; |
413 | } else { |
414 | user = NULL, host = p; |
415 | } |
416 | |
417 | /* |
418 | * Now attempt to load a saved session with the |
419 | * same name as the hostname. |
420 | */ |
421 | { |
32874aea |
422 | Config cfg2; |
3608528b |
423 | do_defaults(host, &cfg2); |
18e62ad8 |
424 | if (loaded_session || cfg2.host[0] == '\0') { |
32874aea |
425 | /* No settings for this host; use defaults */ |
18e62ad8 |
426 | /* (or session was already loaded with -load) */ |
3608528b |
427 | strncpy(cfg.host, host, sizeof(cfg.host) - 1); |
32874aea |
428 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
429 | cfg.port = default_port; |
430 | } else { |
431 | cfg = cfg2; |
32874aea |
432 | } |
3608528b |
433 | } |
434 | |
435 | if (user) { |
436 | /* Patch in specified username. */ |
437 | strncpy(cfg.username, user, |
438 | sizeof(cfg.username) - 1); |
32874aea |
439 | cfg.username[sizeof(cfg.username) - 1] = '\0'; |
32874aea |
440 | } |
3608528b |
441 | |
32874aea |
442 | } |
443 | } else { |
385528da |
444 | char *command; |
445 | int cmdlen, cmdsize; |
446 | cmdlen = cmdsize = 0; |
447 | command = NULL; |
448 | |
449 | while (argc) { |
450 | while (*p) { |
451 | if (cmdlen >= cmdsize) { |
452 | cmdsize = cmdlen + 512; |
3d88e64d |
453 | command = sresize(command, cmdsize, char); |
385528da |
454 | } |
455 | command[cmdlen++]=*p++; |
456 | } |
457 | if (cmdlen >= cmdsize) { |
458 | cmdsize = cmdlen + 512; |
3d88e64d |
459 | command = sresize(command, cmdsize, char); |
385528da |
460 | } |
461 | command[cmdlen++]=' '; /* always add trailing space */ |
462 | if (--argc) p = *++argv; |
32874aea |
463 | } |
385528da |
464 | if (cmdlen) command[--cmdlen]='\0'; |
465 | /* change trailing blank to NUL */ |
466 | cfg.remote_cmd_ptr = command; |
467 | cfg.remote_cmd_ptr2 = NULL; |
32874aea |
468 | cfg.nopty = TRUE; /* command => no terminal */ |
385528da |
469 | |
32874aea |
470 | break; /* done with cmdline */ |
471 | } |
12dc4ec0 |
472 | } |
473 | } |
474 | |
86256dc6 |
475 | if (errors) |
476 | return 1; |
477 | |
d8426c54 |
478 | if (!*cfg.host) { |
32874aea |
479 | usage(); |
d8426c54 |
480 | } |
d8426c54 |
481 | |
449925a6 |
482 | /* |
483 | * Trim leading whitespace off the hostname if it's there. |
484 | */ |
485 | { |
486 | int space = strspn(cfg.host, " \t"); |
487 | memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space); |
488 | } |
489 | |
490 | /* See if host is of the form user@host */ |
491 | if (cfg.host[0] != '\0') { |
5dd103a8 |
492 | char *atsign = strrchr(cfg.host, '@'); |
449925a6 |
493 | /* Make sure we're not overflowing the user field */ |
494 | if (atsign) { |
495 | if (atsign - cfg.host < sizeof cfg.username) { |
496 | strncpy(cfg.username, cfg.host, atsign - cfg.host); |
497 | cfg.username[atsign - cfg.host] = '\0'; |
498 | } |
499 | memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1)); |
500 | } |
501 | } |
502 | |
503 | /* |
c0a81592 |
504 | * Perform command-line overrides on session configuration. |
505 | */ |
5555d393 |
506 | cmdline_run_saved(&cfg); |
c0a81592 |
507 | |
508 | /* |
4d1cdf5d |
509 | * Apply subsystem status. |
510 | */ |
511 | if (use_subsystem) |
512 | cfg.ssh_subsys = TRUE; |
513 | |
514 | /* |
449925a6 |
515 | * Trim a colon suffix off the hostname if it's there. |
516 | */ |
517 | cfg.host[strcspn(cfg.host, ":")] = '\0'; |
518 | |
cae0c023 |
519 | /* |
520 | * Remove any remaining whitespace from the hostname. |
521 | */ |
522 | { |
523 | int p1 = 0, p2 = 0; |
524 | while (cfg.host[p2] != '\0') { |
525 | if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') { |
526 | cfg.host[p1] = cfg.host[p2]; |
527 | p1++; |
528 | } |
529 | p2++; |
530 | } |
531 | cfg.host[p1] = '\0'; |
532 | } |
533 | |
a79e1969 |
534 | if (!cfg.remote_cmd_ptr && !*cfg.remote_cmd) |
32874aea |
535 | flags |= FLAG_INTERACTIVE; |
67779be7 |
536 | |
12dc4ec0 |
537 | /* |
538 | * Select protocol. This is farmed out into a table in a |
539 | * separate file to enable an ssh-free variant. |
540 | */ |
541 | { |
32874aea |
542 | int i; |
543 | back = NULL; |
544 | for (i = 0; backends[i].backend != NULL; i++) |
545 | if (backends[i].protocol == cfg.protocol) { |
546 | back = backends[i].backend; |
547 | break; |
548 | } |
549 | if (back == NULL) { |
550 | fprintf(stderr, |
551 | "Internal fault: Unsupported protocol found\n"); |
552 | return 1; |
553 | } |
12dc4ec0 |
554 | } |
555 | |
556 | /* |
8cb9c947 |
557 | * Select port. |
558 | */ |
559 | if (portnumber != -1) |
32874aea |
560 | cfg.port = portnumber; |
8cb9c947 |
561 | |
7440fd44 |
562 | sk_init(); |
563 | if (p_WSAEventSelect == NULL) { |
564 | fprintf(stderr, "Plink requires WinSock 2\n"); |
12dc4ec0 |
565 | return 1; |
566 | } |
567 | |
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 | } |
c229ef97 |
585 | logctx = log_init(NULL, &cfg); |
a8327734 |
586 | back->provide_logctx(backhandle, logctx); |
d3fef4a5 |
587 | console_provide_logctx(logctx); |
6e1ebb76 |
588 | sfree(realhost); |
12dc4ec0 |
589 | } |
8df7a775 |
590 | connopen = 1; |
12dc4ec0 |
591 | |
12dc4ec0 |
592 | stdinevent = CreateEvent(NULL, FALSE, FALSE, NULL); |
5471d09a |
593 | stdoutevent = CreateEvent(NULL, FALSE, FALSE, NULL); |
594 | stderrevent = CreateEvent(NULL, FALSE, FALSE, NULL); |
12dc4ec0 |
595 | |
0965bee0 |
596 | inhandle = GetStdHandle(STD_INPUT_HANDLE); |
12dc4ec0 |
597 | outhandle = GetStdHandle(STD_OUTPUT_HANDLE); |
fe50e814 |
598 | errhandle = GetStdHandle(STD_ERROR_HANDLE); |
0965bee0 |
599 | GetConsoleMode(inhandle, &orig_console_mode); |
600 | SetConsoleMode(inhandle, ENABLE_PROCESSED_INPUT); |
12dc4ec0 |
601 | |
c44bf5bd |
602 | main_thread_id = GetCurrentThreadId(); |
603 | |
12dc4ec0 |
604 | /* |
12dc4ec0 |
605 | * Turn off ECHO and LINE input modes. We don't care if this |
606 | * call fails, because we know we aren't necessarily running in |
607 | * a console. |
608 | */ |
12dc4ec0 |
609 | handles[0] = netevent; |
610 | handles[1] = stdinevent; |
5471d09a |
611 | handles[2] = stdoutevent; |
612 | handles[3] = stderrevent; |
12dc4ec0 |
613 | sending = FALSE; |
5471d09a |
614 | |
615 | /* |
616 | * Create spare threads to write to stdout and stderr, so we |
617 | * can arrange asynchronous writes. |
618 | */ |
619 | odata.event = stdoutevent; |
620 | odata.eventback = CreateEvent(NULL, FALSE, FALSE, NULL); |
621 | odata.is_stderr = 0; |
622 | odata.busy = odata.done = 0; |
623 | if (!CreateThread(NULL, 0, stdout_write_thread, |
624 | &odata, 0, &out_threadid)) { |
625 | fprintf(stderr, "Unable to create output thread\n"); |
93b581bd |
626 | cleanup_exit(1); |
5471d09a |
627 | } |
628 | edata.event = stderrevent; |
629 | edata.eventback = CreateEvent(NULL, FALSE, FALSE, NULL); |
630 | edata.is_stderr = 1; |
631 | edata.busy = edata.done = 0; |
632 | if (!CreateThread(NULL, 0, stdout_write_thread, |
633 | &edata, 0, &err_threadid)) { |
634 | fprintf(stderr, "Unable to create error output thread\n"); |
93b581bd |
635 | cleanup_exit(1); |
5471d09a |
636 | } |
637 | |
39934deb |
638 | now = GETTICKCOUNT(); |
639 | |
12dc4ec0 |
640 | while (1) { |
32874aea |
641 | int n; |
39934deb |
642 | DWORD ticks; |
32874aea |
643 | |
51470298 |
644 | if (!sending && back->sendok(backhandle)) { |
32874aea |
645 | /* |
646 | * Create a separate thread to read from stdin. This is |
647 | * a total pain, but I can't find another way to do it: |
648 | * |
649 | * - an overlapped ReadFile or ReadFileEx just doesn't |
650 | * happen; we get failure from ReadFileEx, and |
651 | * ReadFile blocks despite being given an OVERLAPPED |
652 | * structure. Perhaps we can't do overlapped reads |
653 | * on consoles. WHY THE HELL NOT? |
654 | * |
655 | * - WaitForMultipleObjects(netevent, console) doesn't |
656 | * work, because it signals the console when |
657 | * _anything_ happens, including mouse motions and |
658 | * other things that don't cause data to be readable |
659 | * - so we're back to ReadFile blocking. |
660 | */ |
661 | idata.event = stdinevent; |
662 | idata.eventback = CreateEvent(NULL, FALSE, FALSE, NULL); |
663 | if (!CreateThread(NULL, 0, stdin_read_thread, |
5471d09a |
664 | &idata, 0, &in_threadid)) { |
665 | fprintf(stderr, "Unable to create input thread\n"); |
93b581bd |
666 | cleanup_exit(1); |
32874aea |
667 | } |
668 | sending = TRUE; |
5fc71a3d |
669 | reading = TRUE; |
32874aea |
670 | } |
671 | |
39934deb |
672 | if (run_timers(now, &next)) { |
673 | ticks = next - GETTICKCOUNT(); |
674 | if (ticks < 0) ticks = 0; /* just in case */ |
675 | } else { |
676 | ticks = INFINITE; |
677 | } |
678 | |
679 | n = MsgWaitForMultipleObjects(4, handles, FALSE, ticks, |
c44bf5bd |
680 | QS_POSTMESSAGE); |
39934deb |
681 | if (n == WAIT_OBJECT_0 + 0) { |
32874aea |
682 | WSANETWORKEVENTS things; |
8df7a775 |
683 | SOCKET socket; |
d2371c81 |
684 | extern SOCKET first_socket(int *), next_socket(int *); |
8df7a775 |
685 | extern int select_result(WPARAM, LPARAM); |
32874aea |
686 | int i, socketstate; |
687 | |
688 | /* |
689 | * We must not call select_result() for any socket |
690 | * until we have finished enumerating within the tree. |
691 | * This is because select_result() may close the socket |
692 | * and modify the tree. |
693 | */ |
694 | /* Count the active sockets. */ |
695 | i = 0; |
696 | for (socket = first_socket(&socketstate); |
697 | socket != INVALID_SOCKET; |
698 | socket = next_socket(&socketstate)) i++; |
699 | |
700 | /* Expand the buffer if necessary. */ |
701 | if (i > sksize) { |
702 | sksize = i + 16; |
3d88e64d |
703 | sklist = sresize(sklist, sksize, SOCKET); |
32874aea |
704 | } |
705 | |
706 | /* Retrieve the sockets into sklist. */ |
707 | skcount = 0; |
708 | for (socket = first_socket(&socketstate); |
709 | socket != INVALID_SOCKET; |
d2371c81 |
710 | socket = next_socket(&socketstate)) { |
32874aea |
711 | sklist[skcount++] = socket; |
712 | } |
713 | |
714 | /* Now we're done enumerating; go through the list. */ |
715 | for (i = 0; i < skcount; i++) { |
716 | WPARAM wp; |
717 | socket = sklist[i]; |
718 | wp = (WPARAM) socket; |
7440fd44 |
719 | if (!p_WSAEnumNetworkEvents(socket, NULL, &things)) { |
64cdd21b |
720 | static const struct { int bit, mask; } eventtypes[] = { |
721 | {FD_CONNECT_BIT, FD_CONNECT}, |
722 | {FD_READ_BIT, FD_READ}, |
723 | {FD_CLOSE_BIT, FD_CLOSE}, |
724 | {FD_OOB_BIT, FD_OOB}, |
725 | {FD_WRITE_BIT, FD_WRITE}, |
726 | {FD_ACCEPT_BIT, FD_ACCEPT}, |
727 | }; |
728 | int e; |
729 | |
32874aea |
730 | noise_ultralight(socket); |
731 | noise_ultralight(things.lNetworkEvents); |
d74d141c |
732 | |
64cdd21b |
733 | for (e = 0; e < lenof(eventtypes); e++) |
734 | if (things.lNetworkEvents & eventtypes[e].mask) { |
735 | LPARAM lp; |
736 | int err = things.iErrorCode[eventtypes[e].bit]; |
737 | lp = WSAMAKESELECTREPLY(eventtypes[e].mask, err); |
738 | connopen &= select_result(wp, lp); |
739 | } |
8df7a775 |
740 | } |
741 | } |
39934deb |
742 | } else if (n == WAIT_OBJECT_0 + 1) { |
5471d09a |
743 | reading = 0; |
32874aea |
744 | noise_ultralight(idata.len); |
51470298 |
745 | if (connopen && back->socket(backhandle) != NULL) { |
42856df4 |
746 | if (idata.len > 0) { |
51470298 |
747 | back->send(backhandle, idata.buffer, idata.len); |
42856df4 |
748 | } else { |
51470298 |
749 | back->special(backhandle, TS_EOF); |
42856df4 |
750 | } |
32874aea |
751 | } |
39934deb |
752 | } else if (n == WAIT_OBJECT_0 + 2) { |
5471d09a |
753 | odata.busy = 0; |
754 | if (!odata.writeret) { |
755 | fprintf(stderr, "Unable to write to standard output\n"); |
93b581bd |
756 | cleanup_exit(0); |
5471d09a |
757 | } |
758 | bufchain_consume(&stdout_data, odata.lenwritten); |
759 | if (bufchain_size(&stdout_data) > 0) |
760 | try_output(0); |
51470298 |
761 | if (connopen && back->socket(backhandle) != NULL) { |
762 | back->unthrottle(backhandle, bufchain_size(&stdout_data) + |
42856df4 |
763 | bufchain_size(&stderr_data)); |
764 | } |
39934deb |
765 | } else if (n == WAIT_OBJECT_0 + 3) { |
5471d09a |
766 | edata.busy = 0; |
767 | if (!edata.writeret) { |
768 | fprintf(stderr, "Unable to write to standard output\n"); |
93b581bd |
769 | cleanup_exit(0); |
5471d09a |
770 | } |
771 | bufchain_consume(&stderr_data, edata.lenwritten); |
772 | if (bufchain_size(&stderr_data) > 0) |
773 | try_output(1); |
51470298 |
774 | if (connopen && back->socket(backhandle) != NULL) { |
775 | back->unthrottle(backhandle, bufchain_size(&stdout_data) + |
42856df4 |
776 | bufchain_size(&stderr_data)); |
777 | } |
39934deb |
778 | } else if (n == WAIT_OBJECT_0 + 4) { |
c44bf5bd |
779 | MSG msg; |
780 | while (PeekMessage(&msg, INVALID_HANDLE_VALUE, |
781 | WM_AGENT_CALLBACK, WM_AGENT_CALLBACK, |
782 | PM_REMOVE)) { |
783 | struct agent_callback *c = (struct agent_callback *)msg.lParam; |
784 | c->callback(c->callback_ctx, c->data, c->len); |
785 | sfree(c); |
786 | } |
5471d09a |
787 | } |
39934deb |
788 | |
789 | if (n == WAIT_TIMEOUT) { |
790 | now = next; |
791 | } else { |
792 | now = GETTICKCOUNT(); |
793 | } |
794 | |
51470298 |
795 | if (!reading && back->sendbuffer(backhandle) < MAX_STDIN_BACKLOG) { |
32874aea |
796 | SetEvent(idata.eventback); |
5471d09a |
797 | reading = 1; |
32874aea |
798 | } |
51470298 |
799 | if ((!connopen || back->socket(backhandle) == NULL) && |
42856df4 |
800 | bufchain_size(&stdout_data) == 0 && |
801 | bufchain_size(&stderr_data) == 0) |
32874aea |
802 | break; /* we closed the connection */ |
12dc4ec0 |
803 | } |
51470298 |
804 | exitcode = back->exitcode(backhandle); |
d8d6c7e5 |
805 | if (exitcode < 0) { |
806 | fprintf(stderr, "Remote process exit code unavailable\n"); |
807 | exitcode = 1; /* this is an error condition */ |
808 | } |
7440fd44 |
809 | cleanup_exit(exitcode); |
810 | return 0; /* placate compiler warning */ |
12dc4ec0 |
811 | } |