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