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