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