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