12dc4ec0 |
1 | /* |
f4ff9455 |
2 | * PLink - a Windows command-line (stdin/stdout) variant of PuTTY. |
12dc4ec0 |
3 | */ |
4 | |
12dc4ec0 |
5 | #include <stdio.h> |
49bad831 |
6 | #include <stdlib.h> |
2b0c045b |
7 | #include <assert.h> |
12dc4ec0 |
8 | #include <stdarg.h> |
9 | |
32874aea |
10 | #define PUTTY_DO_GLOBALS /* actually _define_ globals */ |
12dc4ec0 |
11 | #include "putty.h" |
a9422f39 |
12 | #include "storage.h" |
8df7a775 |
13 | #include "tree234.h" |
12dc4ec0 |
14 | |
604fab0c |
15 | #define WM_AGENT_CALLBACK (WM_APP + 4) |
c44bf5bd |
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 | |
c6ccd5c2 |
97 | char *get_ttymode(void *frontend, const char *mode) { return NULL; } |
98 | |
5471d09a |
99 | struct input_data { |
100 | DWORD len; |
101 | char buffer[4096]; |
102 | HANDLE event, eventback; |
103 | }; |
104 | |
32874aea |
105 | static DWORD WINAPI stdin_read_thread(void *param) |
106 | { |
107 | struct input_data *idata = (struct input_data *) param; |
12dc4ec0 |
108 | HANDLE inhandle; |
109 | |
110 | inhandle = GetStdHandle(STD_INPUT_HANDLE); |
111 | |
112 | while (ReadFile(inhandle, idata->buffer, sizeof(idata->buffer), |
32874aea |
113 | &idata->len, NULL) && idata->len > 0) { |
114 | SetEvent(idata->event); |
115 | WaitForSingleObject(idata->eventback, INFINITE); |
12dc4ec0 |
116 | } |
117 | |
118 | idata->len = 0; |
119 | SetEvent(idata->event); |
120 | |
121 | return 0; |
122 | } |
123 | |
5471d09a |
124 | struct output_data { |
125 | DWORD len, lenwritten; |
126 | int writeret; |
127 | char *buffer; |
128 | int is_stderr, done; |
129 | HANDLE event, eventback; |
130 | int busy; |
131 | }; |
132 | |
133 | static DWORD WINAPI stdout_write_thread(void *param) |
134 | { |
135 | struct output_data *odata = (struct output_data *) param; |
136 | HANDLE outhandle, errhandle; |
137 | |
138 | outhandle = GetStdHandle(STD_OUTPUT_HANDLE); |
139 | errhandle = GetStdHandle(STD_ERROR_HANDLE); |
140 | |
141 | while (1) { |
142 | WaitForSingleObject(odata->eventback, INFINITE); |
143 | if (odata->done) |
144 | break; |
145 | odata->writeret = |
146 | WriteFile(odata->is_stderr ? errhandle : outhandle, |
147 | odata->buffer, odata->len, &odata->lenwritten, NULL); |
148 | SetEvent(odata->event); |
149 | } |
150 | |
151 | return 0; |
152 | } |
153 | |
154 | bufchain stdout_data, stderr_data; |
155 | struct output_data odata, edata; |
156 | |
157 | void try_output(int is_stderr) |
158 | { |
159 | struct output_data *data = (is_stderr ? &edata : &odata); |
160 | void *senddata; |
161 | int sendlen; |
162 | |
163 | if (!data->busy) { |
164 | bufchain_prefix(is_stderr ? &stderr_data : &stdout_data, |
165 | &senddata, &sendlen); |
166 | data->buffer = senddata; |
167 | data->len = sendlen; |
168 | SetEvent(data->eventback); |
169 | data->busy = 1; |
170 | } |
171 | } |
172 | |
9fab77dc |
173 | int from_backend(void *frontend_handle, int is_stderr, |
174 | const char *data, int len) |
5471d09a |
175 | { |
5471d09a |
176 | int osize, esize; |
177 | |
178 | if (is_stderr) { |
179 | bufchain_add(&stderr_data, data, len); |
180 | try_output(1); |
181 | } else { |
182 | bufchain_add(&stdout_data, data, len); |
183 | try_output(0); |
184 | } |
185 | |
186 | osize = bufchain_size(&stdout_data); |
187 | esize = bufchain_size(&stderr_data); |
188 | |
189 | return osize + esize; |
190 | } |
191 | |
edd0cb8a |
192 | int from_backend_untrusted(void *frontend_handle, const char *data, int len) |
193 | { |
194 | /* |
195 | * No "untrusted" output should get here (the way the code is |
196 | * currently, it's all diverted by FLAG_STDERR). |
197 | */ |
198 | assert(!"Unexpected call to from_backend_untrusted()"); |
199 | return 0; /* not reached */ |
200 | } |
201 | |
202 | int get_userpass_input(prompts_t *p, unsigned char *in, int inlen) |
203 | { |
204 | int ret; |
205 | ret = cmdline_get_passwd_input(p, in, inlen); |
206 | if (ret == -1) |
207 | ret = console_get_userpass_input(p, in, inlen); |
208 | return ret; |
209 | } |
210 | |
c44bf5bd |
211 | static DWORD main_thread_id; |
212 | |
213 | void agent_schedule_callback(void (*callback)(void *, void *, int), |
214 | void *callback_ctx, void *data, int len) |
215 | { |
216 | struct agent_callback *c = snew(struct agent_callback); |
217 | c->callback = callback; |
218 | c->callback_ctx = callback_ctx; |
219 | c->data = data; |
220 | c->len = len; |
221 | PostThreadMessage(main_thread_id, WM_AGENT_CALLBACK, 0, (LPARAM)c); |
222 | } |
223 | |
d8426c54 |
224 | /* |
225 | * Short description of parameters. |
226 | */ |
227 | static void usage(void) |
228 | { |
229 | printf("PuTTY Link: command-line connection utility\n"); |
230 | printf("%s\n", ver); |
231 | printf("Usage: plink [options] [user@]host [command]\n"); |
e672967c |
232 | printf(" (\"host\" can also be a PuTTY saved session name)\n"); |
d8426c54 |
233 | printf("Options:\n"); |
2285d016 |
234 | printf(" -V print version information and exit\n"); |
235 | printf(" -pgpfp print PGP key fingerprints and exit\n"); |
d8426c54 |
236 | printf(" -v show verbose messages\n"); |
e2a197cf |
237 | printf(" -load sessname Load settings from saved session\n"); |
238 | printf(" -ssh -telnet -rlogin -raw\n"); |
afd4d0d2 |
239 | printf(" force use of a particular protocol\n"); |
d8426c54 |
240 | printf(" -P port connect to specified port\n"); |
e2a197cf |
241 | printf(" -l user connect with specified username\n"); |
e2a197cf |
242 | printf(" -batch disable all interactive prompts\n"); |
243 | printf("The following options only apply to SSH connections:\n"); |
244 | printf(" -pw passw login with specified password\n"); |
dbe6c525 |
245 | printf(" -D [listen-IP:]listen-port\n"); |
246 | printf(" Dynamic SOCKS-based port forwarding\n"); |
247 | printf(" -L [listen-IP:]listen-port:host:port\n"); |
248 | printf(" Forward local port to remote address\n"); |
249 | printf(" -R [listen-IP:]listen-port:host:port\n"); |
250 | printf(" Forward remote port to local address\n"); |
e2a197cf |
251 | printf(" -X -x enable / disable X11 forwarding\n"); |
252 | printf(" -A -a enable / disable agent forwarding\n"); |
253 | printf(" -t -T enable / disable pty allocation\n"); |
254 | printf(" -1 -2 force use of particular protocol version\n"); |
05581745 |
255 | printf(" -4 -6 force use of IPv4 or IPv6\n"); |
e2a197cf |
256 | printf(" -C enable compression\n"); |
257 | printf(" -i key private key file for authentication\n"); |
e5708bc7 |
258 | printf(" -noagent disable use of Pageant\n"); |
259 | printf(" -agent enable use of Pageant\n"); |
54018d95 |
260 | printf(" -m file read remote command(s) from file\n"); |
4d1cdf5d |
261 | printf(" -s remote command is an SSH subsystem (SSH-2 only)\n"); |
b72c366d |
262 | printf(" -N don't start a shell/command (SSH-2 only)\n"); |
dc108ebc |
263 | exit(1); |
264 | } |
265 | |
266 | static void version(void) |
267 | { |
268 | printf("plink: %s\n", ver); |
d8426c54 |
269 | exit(1); |
270 | } |
271 | |
32874aea |
272 | char *do_select(SOCKET skt, int startup) |
273 | { |
8df7a775 |
274 | int events; |
275 | if (startup) { |
3ad9d396 |
276 | events = (FD_CONNECT | FD_READ | FD_WRITE | |
277 | FD_OOB | FD_CLOSE | FD_ACCEPT); |
8df7a775 |
278 | } else { |
279 | events = 0; |
280 | } |
7440fd44 |
281 | if (p_WSAEventSelect(skt, netevent, events) == SOCKET_ERROR) { |
282 | switch (p_WSAGetLastError()) { |
32874aea |
283 | case WSAENETDOWN: |
284 | return "Network is down"; |
285 | default: |
7440fd44 |
286 | return "WSAEventSelect(): unknown error"; |
32874aea |
287 | } |
8df7a775 |
288 | } |
289 | return NULL; |
290 | } |
291 | |
32874aea |
292 | int main(int argc, char **argv) |
293 | { |
5471d09a |
294 | WSAEVENT stdinevent, stdoutevent, stderrevent; |
295 | HANDLE handles[4]; |
296 | DWORD in_threadid, out_threadid, err_threadid; |
12dc4ec0 |
297 | struct input_data idata; |
5fc71a3d |
298 | int reading = FALSE; |
12dc4ec0 |
299 | int sending; |
d8426c54 |
300 | int portnumber = -1; |
8df7a775 |
301 | SOCKET *sklist; |
302 | int skcount, sksize; |
303 | int connopen; |
d8d6c7e5 |
304 | int exitcode; |
86256dc6 |
305 | int errors; |
4d1cdf5d |
306 | int use_subsystem = 0; |
39934deb |
307 | long now, next; |
12dc4ec0 |
308 | |
32874aea |
309 | sklist = NULL; |
310 | skcount = sksize = 0; |
c9bdcd96 |
311 | /* |
312 | * Initialise port and protocol to sensible defaults. (These |
313 | * will be overridden by more or less anything.) |
314 | */ |
315 | default_protocol = PROT_SSH; |
316 | default_port = 22; |
8df7a775 |
317 | |
67779be7 |
318 | flags = FLAG_STDERR; |
12dc4ec0 |
319 | /* |
320 | * Process the command line. |
321 | */ |
a9422f39 |
322 | do_defaults(NULL, &cfg); |
18e62ad8 |
323 | loaded_session = FALSE; |
e7a7383f |
324 | default_protocol = cfg.protocol; |
325 | default_port = cfg.port; |
86256dc6 |
326 | errors = 0; |
8cb9c947 |
327 | { |
32874aea |
328 | /* |
329 | * Override the default protocol if PLINK_PROTOCOL is set. |
330 | */ |
331 | char *p = getenv("PLINK_PROTOCOL"); |
332 | int i; |
333 | if (p) { |
334 | for (i = 0; backends[i].backend != NULL; i++) { |
335 | if (!strcmp(backends[i].name, p)) { |
336 | default_protocol = cfg.protocol = backends[i].protocol; |
337 | default_port = cfg.port = |
338 | backends[i].backend->default_port; |
339 | break; |
340 | } |
341 | } |
342 | } |
8cb9c947 |
343 | } |
12dc4ec0 |
344 | while (--argc) { |
32874aea |
345 | char *p = *++argv; |
346 | if (*p == '-') { |
5555d393 |
347 | int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL), |
348 | 1, &cfg); |
c0a81592 |
349 | if (ret == -2) { |
350 | fprintf(stderr, |
351 | "plink: option \"%s\" requires an argument\n", p); |
86256dc6 |
352 | errors = 1; |
c0a81592 |
353 | } else if (ret == 2) { |
354 | --argc, ++argv; |
355 | } else if (ret == 1) { |
356 | continue; |
ff2ae367 |
357 | } else if (!strcmp(p, "-batch")) { |
c0a81592 |
358 | console_batch_mode = 1; |
4d1cdf5d |
359 | } else if (!strcmp(p, "-s")) { |
360 | /* Save status to write to cfg later. */ |
361 | use_subsystem = 1; |
dc108ebc |
362 | } else if (!strcmp(p, "-V")) { |
363 | version(); |
2285d016 |
364 | } else if (!strcmp(p, "-pgpfp")) { |
365 | pgp_fingerprints(); |
366 | exit(1); |
86256dc6 |
367 | } else { |
368 | fprintf(stderr, "plink: unknown option \"%s\"\n", p); |
369 | errors = 1; |
32874aea |
370 | } |
12dc4ec0 |
371 | } else if (*p) { |
32874aea |
372 | if (!*cfg.host) { |
373 | char *q = p; |
374 | /* |
375 | * If the hostname starts with "telnet:", set the |
376 | * protocol to Telnet and process the string as a |
377 | * Telnet URL. |
378 | */ |
379 | if (!strncmp(q, "telnet:", 7)) { |
380 | char c; |
381 | |
382 | q += 7; |
383 | if (q[0] == '/' && q[1] == '/') |
384 | q += 2; |
385 | cfg.protocol = PROT_TELNET; |
386 | p = q; |
387 | while (*p && *p != ':' && *p != '/') |
388 | p++; |
389 | c = *p; |
390 | if (*p) |
391 | *p++ = '\0'; |
392 | if (c == ':') |
393 | cfg.port = atoi(p); |
394 | else |
395 | cfg.port = -1; |
396 | strncpy(cfg.host, q, sizeof(cfg.host) - 1); |
397 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
398 | } else { |
3608528b |
399 | char *r, *user, *host; |
32874aea |
400 | /* |
401 | * Before we process the [user@]host string, we |
402 | * first check for the presence of a protocol |
403 | * prefix (a protocol name followed by ","). |
404 | */ |
405 | r = strchr(p, ','); |
406 | if (r) { |
407 | int i, j; |
408 | for (i = 0; backends[i].backend != NULL; i++) { |
409 | j = strlen(backends[i].name); |
410 | if (j == r - p && |
411 | !memcmp(backends[i].name, p, j)) { |
412 | default_protocol = cfg.protocol = |
413 | backends[i].protocol; |
414 | portnumber = |
415 | backends[i].backend->default_port; |
416 | p = r + 1; |
417 | break; |
418 | } |
419 | } |
420 | } |
421 | |
422 | /* |
3608528b |
423 | * A nonzero length string followed by an @ is treated |
424 | * as a username. (We discount an _initial_ @.) The |
425 | * rest of the string (or the whole string if no @) |
426 | * is treated as a session name and/or hostname. |
32874aea |
427 | */ |
428 | r = strrchr(p, '@'); |
429 | if (r == p) |
430 | p++, r = NULL; /* discount initial @ */ |
3608528b |
431 | if (r) { |
432 | *r++ = '\0'; |
433 | user = p, host = r; |
434 | } else { |
435 | user = NULL, host = p; |
436 | } |
437 | |
438 | /* |
439 | * Now attempt to load a saved session with the |
440 | * same name as the hostname. |
441 | */ |
442 | { |
32874aea |
443 | Config cfg2; |
3608528b |
444 | do_defaults(host, &cfg2); |
18e62ad8 |
445 | if (loaded_session || cfg2.host[0] == '\0') { |
32874aea |
446 | /* No settings for this host; use defaults */ |
18e62ad8 |
447 | /* (or session was already loaded with -load) */ |
3608528b |
448 | strncpy(cfg.host, host, sizeof(cfg.host) - 1); |
32874aea |
449 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
450 | cfg.port = default_port; |
451 | } else { |
452 | cfg = cfg2; |
32874aea |
453 | } |
3608528b |
454 | } |
455 | |
456 | if (user) { |
457 | /* Patch in specified username. */ |
458 | strncpy(cfg.username, user, |
459 | sizeof(cfg.username) - 1); |
32874aea |
460 | cfg.username[sizeof(cfg.username) - 1] = '\0'; |
32874aea |
461 | } |
3608528b |
462 | |
32874aea |
463 | } |
464 | } else { |
385528da |
465 | char *command; |
466 | int cmdlen, cmdsize; |
467 | cmdlen = cmdsize = 0; |
468 | command = NULL; |
469 | |
470 | while (argc) { |
471 | while (*p) { |
472 | if (cmdlen >= cmdsize) { |
473 | cmdsize = cmdlen + 512; |
3d88e64d |
474 | command = sresize(command, cmdsize, char); |
385528da |
475 | } |
476 | command[cmdlen++]=*p++; |
477 | } |
478 | if (cmdlen >= cmdsize) { |
479 | cmdsize = cmdlen + 512; |
3d88e64d |
480 | command = sresize(command, cmdsize, char); |
385528da |
481 | } |
482 | command[cmdlen++]=' '; /* always add trailing space */ |
483 | if (--argc) p = *++argv; |
32874aea |
484 | } |
385528da |
485 | if (cmdlen) command[--cmdlen]='\0'; |
486 | /* change trailing blank to NUL */ |
487 | cfg.remote_cmd_ptr = command; |
488 | cfg.remote_cmd_ptr2 = NULL; |
32874aea |
489 | cfg.nopty = TRUE; /* command => no terminal */ |
385528da |
490 | |
32874aea |
491 | break; /* done with cmdline */ |
492 | } |
12dc4ec0 |
493 | } |
494 | } |
495 | |
86256dc6 |
496 | if (errors) |
497 | return 1; |
498 | |
d8426c54 |
499 | if (!*cfg.host) { |
32874aea |
500 | usage(); |
d8426c54 |
501 | } |
d8426c54 |
502 | |
449925a6 |
503 | /* |
504 | * Trim leading whitespace off the hostname if it's there. |
505 | */ |
506 | { |
507 | int space = strspn(cfg.host, " \t"); |
508 | memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space); |
509 | } |
510 | |
511 | /* See if host is of the form user@host */ |
512 | if (cfg.host[0] != '\0') { |
5dd103a8 |
513 | char *atsign = strrchr(cfg.host, '@'); |
449925a6 |
514 | /* Make sure we're not overflowing the user field */ |
515 | if (atsign) { |
516 | if (atsign - cfg.host < sizeof cfg.username) { |
517 | strncpy(cfg.username, cfg.host, atsign - cfg.host); |
518 | cfg.username[atsign - cfg.host] = '\0'; |
519 | } |
520 | memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1)); |
521 | } |
522 | } |
523 | |
524 | /* |
c0a81592 |
525 | * Perform command-line overrides on session configuration. |
526 | */ |
5555d393 |
527 | cmdline_run_saved(&cfg); |
c0a81592 |
528 | |
529 | /* |
4d1cdf5d |
530 | * Apply subsystem status. |
531 | */ |
532 | if (use_subsystem) |
533 | cfg.ssh_subsys = TRUE; |
534 | |
535 | /* |
449925a6 |
536 | * Trim a colon suffix off the hostname if it's there. |
537 | */ |
538 | cfg.host[strcspn(cfg.host, ":")] = '\0'; |
539 | |
cae0c023 |
540 | /* |
541 | * Remove any remaining whitespace from the hostname. |
542 | */ |
543 | { |
544 | int p1 = 0, p2 = 0; |
545 | while (cfg.host[p2] != '\0') { |
546 | if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') { |
547 | cfg.host[p1] = cfg.host[p2]; |
548 | p1++; |
549 | } |
550 | p2++; |
551 | } |
552 | cfg.host[p1] = '\0'; |
553 | } |
554 | |
a79e1969 |
555 | if (!cfg.remote_cmd_ptr && !*cfg.remote_cmd) |
32874aea |
556 | flags |= FLAG_INTERACTIVE; |
67779be7 |
557 | |
12dc4ec0 |
558 | /* |
559 | * Select protocol. This is farmed out into a table in a |
560 | * separate file to enable an ssh-free variant. |
561 | */ |
562 | { |
32874aea |
563 | int i; |
564 | back = NULL; |
565 | for (i = 0; backends[i].backend != NULL; i++) |
566 | if (backends[i].protocol == cfg.protocol) { |
567 | back = backends[i].backend; |
568 | break; |
569 | } |
570 | if (back == NULL) { |
571 | fprintf(stderr, |
572 | "Internal fault: Unsupported protocol found\n"); |
573 | return 1; |
574 | } |
12dc4ec0 |
575 | } |
576 | |
577 | /* |
8cb9c947 |
578 | * Select port. |
579 | */ |
580 | if (portnumber != -1) |
32874aea |
581 | cfg.port = portnumber; |
8cb9c947 |
582 | |
7440fd44 |
583 | sk_init(); |
584 | if (p_WSAEventSelect == NULL) { |
585 | fprintf(stderr, "Plink requires WinSock 2\n"); |
12dc4ec0 |
586 | return 1; |
587 | } |
588 | |
589 | /* |
590 | * Start up the connection. |
591 | */ |
8df7a775 |
592 | netevent = CreateEvent(NULL, FALSE, FALSE, NULL); |
12dc4ec0 |
593 | { |
cbe2d68f |
594 | const char *error; |
12dc4ec0 |
595 | char *realhost; |
2184a5d9 |
596 | /* nodelay is only useful if stdin is a character device (console) */ |
597 | int nodelay = cfg.tcp_nodelay && |
598 | (GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_CHAR); |
12dc4ec0 |
599 | |
86916870 |
600 | error = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port, |
79bf227b |
601 | &realhost, nodelay, cfg.tcp_keepalives); |
12dc4ec0 |
602 | if (error) { |
603 | fprintf(stderr, "Unable to open connection:\n%s", error); |
604 | return 1; |
605 | } |
c229ef97 |
606 | logctx = log_init(NULL, &cfg); |
a8327734 |
607 | back->provide_logctx(backhandle, logctx); |
d3fef4a5 |
608 | console_provide_logctx(logctx); |
6e1ebb76 |
609 | sfree(realhost); |
12dc4ec0 |
610 | } |
8df7a775 |
611 | connopen = 1; |
12dc4ec0 |
612 | |
12dc4ec0 |
613 | stdinevent = CreateEvent(NULL, FALSE, FALSE, NULL); |
5471d09a |
614 | stdoutevent = CreateEvent(NULL, FALSE, FALSE, NULL); |
615 | stderrevent = CreateEvent(NULL, FALSE, FALSE, NULL); |
12dc4ec0 |
616 | |
0965bee0 |
617 | inhandle = GetStdHandle(STD_INPUT_HANDLE); |
12dc4ec0 |
618 | outhandle = GetStdHandle(STD_OUTPUT_HANDLE); |
fe50e814 |
619 | errhandle = GetStdHandle(STD_ERROR_HANDLE); |
12dc4ec0 |
620 | /* |
12dc4ec0 |
621 | * Turn off ECHO and LINE input modes. We don't care if this |
622 | * call fails, because we know we aren't necessarily running in |
623 | * a console. |
624 | */ |
f8efb0d5 |
625 | GetConsoleMode(inhandle, &orig_console_mode); |
626 | SetConsoleMode(inhandle, ENABLE_PROCESSED_INPUT); |
627 | |
628 | main_thread_id = GetCurrentThreadId(); |
629 | |
12dc4ec0 |
630 | handles[0] = netevent; |
631 | handles[1] = stdinevent; |
5471d09a |
632 | handles[2] = stdoutevent; |
633 | handles[3] = stderrevent; |
12dc4ec0 |
634 | sending = FALSE; |
5471d09a |
635 | |
636 | /* |
637 | * Create spare threads to write to stdout and stderr, so we |
638 | * can arrange asynchronous writes. |
639 | */ |
640 | odata.event = stdoutevent; |
641 | odata.eventback = CreateEvent(NULL, FALSE, FALSE, NULL); |
642 | odata.is_stderr = 0; |
643 | odata.busy = odata.done = 0; |
644 | if (!CreateThread(NULL, 0, stdout_write_thread, |
645 | &odata, 0, &out_threadid)) { |
646 | fprintf(stderr, "Unable to create output thread\n"); |
93b581bd |
647 | cleanup_exit(1); |
5471d09a |
648 | } |
649 | edata.event = stderrevent; |
650 | edata.eventback = CreateEvent(NULL, FALSE, FALSE, NULL); |
651 | edata.is_stderr = 1; |
652 | edata.busy = edata.done = 0; |
653 | if (!CreateThread(NULL, 0, stdout_write_thread, |
654 | &edata, 0, &err_threadid)) { |
655 | fprintf(stderr, "Unable to create error output thread\n"); |
93b581bd |
656 | cleanup_exit(1); |
5471d09a |
657 | } |
658 | |
39934deb |
659 | now = GETTICKCOUNT(); |
660 | |
12dc4ec0 |
661 | while (1) { |
32874aea |
662 | int n; |
39934deb |
663 | DWORD ticks; |
32874aea |
664 | |
51470298 |
665 | if (!sending && back->sendok(backhandle)) { |
32874aea |
666 | /* |
667 | * Create a separate thread to read from stdin. This is |
668 | * a total pain, but I can't find another way to do it: |
669 | * |
670 | * - an overlapped ReadFile or ReadFileEx just doesn't |
671 | * happen; we get failure from ReadFileEx, and |
672 | * ReadFile blocks despite being given an OVERLAPPED |
673 | * structure. Perhaps we can't do overlapped reads |
674 | * on consoles. WHY THE HELL NOT? |
675 | * |
676 | * - WaitForMultipleObjects(netevent, console) doesn't |
677 | * work, because it signals the console when |
678 | * _anything_ happens, including mouse motions and |
679 | * other things that don't cause data to be readable |
680 | * - so we're back to ReadFile blocking. |
681 | */ |
682 | idata.event = stdinevent; |
683 | idata.eventback = CreateEvent(NULL, FALSE, FALSE, NULL); |
684 | if (!CreateThread(NULL, 0, stdin_read_thread, |
5471d09a |
685 | &idata, 0, &in_threadid)) { |
686 | fprintf(stderr, "Unable to create input thread\n"); |
93b581bd |
687 | cleanup_exit(1); |
32874aea |
688 | } |
689 | sending = TRUE; |
5fc71a3d |
690 | reading = TRUE; |
32874aea |
691 | } |
692 | |
39934deb |
693 | if (run_timers(now, &next)) { |
694 | ticks = next - GETTICKCOUNT(); |
695 | if (ticks < 0) ticks = 0; /* just in case */ |
696 | } else { |
697 | ticks = INFINITE; |
698 | } |
699 | |
700 | n = MsgWaitForMultipleObjects(4, handles, FALSE, ticks, |
c44bf5bd |
701 | QS_POSTMESSAGE); |
39934deb |
702 | if (n == WAIT_OBJECT_0 + 0) { |
32874aea |
703 | WSANETWORKEVENTS things; |
8df7a775 |
704 | SOCKET socket; |
d2371c81 |
705 | extern SOCKET first_socket(int *), next_socket(int *); |
8df7a775 |
706 | extern int select_result(WPARAM, LPARAM); |
32874aea |
707 | int i, socketstate; |
708 | |
709 | /* |
710 | * We must not call select_result() for any socket |
711 | * until we have finished enumerating within the tree. |
712 | * This is because select_result() may close the socket |
713 | * and modify the tree. |
714 | */ |
715 | /* Count the active sockets. */ |
716 | i = 0; |
717 | for (socket = first_socket(&socketstate); |
718 | socket != INVALID_SOCKET; |
719 | socket = next_socket(&socketstate)) i++; |
720 | |
721 | /* Expand the buffer if necessary. */ |
722 | if (i > sksize) { |
723 | sksize = i + 16; |
3d88e64d |
724 | sklist = sresize(sklist, sksize, SOCKET); |
32874aea |
725 | } |
726 | |
727 | /* Retrieve the sockets into sklist. */ |
728 | skcount = 0; |
729 | for (socket = first_socket(&socketstate); |
730 | socket != INVALID_SOCKET; |
d2371c81 |
731 | socket = next_socket(&socketstate)) { |
32874aea |
732 | sklist[skcount++] = socket; |
733 | } |
734 | |
735 | /* Now we're done enumerating; go through the list. */ |
736 | for (i = 0; i < skcount; i++) { |
737 | WPARAM wp; |
738 | socket = sklist[i]; |
739 | wp = (WPARAM) socket; |
7440fd44 |
740 | if (!p_WSAEnumNetworkEvents(socket, NULL, &things)) { |
64cdd21b |
741 | static const struct { int bit, mask; } eventtypes[] = { |
742 | {FD_CONNECT_BIT, FD_CONNECT}, |
743 | {FD_READ_BIT, FD_READ}, |
744 | {FD_CLOSE_BIT, FD_CLOSE}, |
745 | {FD_OOB_BIT, FD_OOB}, |
746 | {FD_WRITE_BIT, FD_WRITE}, |
747 | {FD_ACCEPT_BIT, FD_ACCEPT}, |
748 | }; |
749 | int e; |
750 | |
32874aea |
751 | noise_ultralight(socket); |
752 | noise_ultralight(things.lNetworkEvents); |
d74d141c |
753 | |
64cdd21b |
754 | for (e = 0; e < lenof(eventtypes); e++) |
755 | if (things.lNetworkEvents & eventtypes[e].mask) { |
756 | LPARAM lp; |
757 | int err = things.iErrorCode[eventtypes[e].bit]; |
758 | lp = WSAMAKESELECTREPLY(eventtypes[e].mask, err); |
759 | connopen &= select_result(wp, lp); |
760 | } |
8df7a775 |
761 | } |
762 | } |
39934deb |
763 | } else if (n == WAIT_OBJECT_0 + 1) { |
5471d09a |
764 | reading = 0; |
32874aea |
765 | noise_ultralight(idata.len); |
51470298 |
766 | if (connopen && back->socket(backhandle) != NULL) { |
42856df4 |
767 | if (idata.len > 0) { |
51470298 |
768 | back->send(backhandle, idata.buffer, idata.len); |
42856df4 |
769 | } else { |
51470298 |
770 | back->special(backhandle, TS_EOF); |
42856df4 |
771 | } |
32874aea |
772 | } |
39934deb |
773 | } else if (n == WAIT_OBJECT_0 + 2) { |
5471d09a |
774 | odata.busy = 0; |
775 | if (!odata.writeret) { |
776 | fprintf(stderr, "Unable to write to standard output\n"); |
93b581bd |
777 | cleanup_exit(0); |
5471d09a |
778 | } |
779 | bufchain_consume(&stdout_data, odata.lenwritten); |
780 | if (bufchain_size(&stdout_data) > 0) |
781 | try_output(0); |
51470298 |
782 | if (connopen && back->socket(backhandle) != NULL) { |
783 | back->unthrottle(backhandle, bufchain_size(&stdout_data) + |
42856df4 |
784 | bufchain_size(&stderr_data)); |
785 | } |
39934deb |
786 | } else if (n == WAIT_OBJECT_0 + 3) { |
5471d09a |
787 | edata.busy = 0; |
788 | if (!edata.writeret) { |
789 | fprintf(stderr, "Unable to write to standard output\n"); |
93b581bd |
790 | cleanup_exit(0); |
5471d09a |
791 | } |
792 | bufchain_consume(&stderr_data, edata.lenwritten); |
793 | if (bufchain_size(&stderr_data) > 0) |
794 | try_output(1); |
51470298 |
795 | if (connopen && back->socket(backhandle) != NULL) { |
796 | back->unthrottle(backhandle, bufchain_size(&stdout_data) + |
42856df4 |
797 | bufchain_size(&stderr_data)); |
798 | } |
39934deb |
799 | } else if (n == WAIT_OBJECT_0 + 4) { |
c44bf5bd |
800 | MSG msg; |
801 | while (PeekMessage(&msg, INVALID_HANDLE_VALUE, |
802 | WM_AGENT_CALLBACK, WM_AGENT_CALLBACK, |
803 | PM_REMOVE)) { |
804 | struct agent_callback *c = (struct agent_callback *)msg.lParam; |
805 | c->callback(c->callback_ctx, c->data, c->len); |
806 | sfree(c); |
807 | } |
5471d09a |
808 | } |
39934deb |
809 | |
810 | if (n == WAIT_TIMEOUT) { |
811 | now = next; |
812 | } else { |
813 | now = GETTICKCOUNT(); |
814 | } |
815 | |
51470298 |
816 | if (!reading && back->sendbuffer(backhandle) < MAX_STDIN_BACKLOG) { |
32874aea |
817 | SetEvent(idata.eventback); |
5471d09a |
818 | reading = 1; |
32874aea |
819 | } |
51470298 |
820 | if ((!connopen || back->socket(backhandle) == NULL) && |
42856df4 |
821 | bufchain_size(&stdout_data) == 0 && |
822 | bufchain_size(&stderr_data) == 0) |
32874aea |
823 | break; /* we closed the connection */ |
12dc4ec0 |
824 | } |
51470298 |
825 | exitcode = back->exitcode(backhandle); |
d8d6c7e5 |
826 | if (exitcode < 0) { |
827 | fprintf(stderr, "Remote process exit code unavailable\n"); |
828 | exitcode = 1; /* this is an error condition */ |
829 | } |
7440fd44 |
830 | cleanup_exit(exitcode); |
831 | return 0; /* placate compiler warning */ |
12dc4ec0 |
832 | } |