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 | |
c44bf5bd |
17 | struct agent_callback { |
18 | void (*callback)(void *, void *, int); |
19 | void *callback_ctx; |
20 | void *data; |
21 | int len; |
22 | }; |
23 | |
32874aea |
24 | void fatalbox(char *p, ...) |
25 | { |
12dc4ec0 |
26 | va_list ap; |
49bad831 |
27 | fprintf(stderr, "FATAL ERROR: "); |
1709795f |
28 | va_start(ap, p); |
29 | vfprintf(stderr, p, ap); |
30 | va_end(ap); |
31 | fputc('\n', stderr); |
7bfc5fd0 |
32 | if (logctx) { |
33 | log_free(logctx); |
34 | logctx = NULL; |
35 | } |
1709795f |
36 | cleanup_exit(1); |
37 | } |
38 | void modalfatalbox(char *p, ...) |
39 | { |
40 | va_list ap; |
41 | fprintf(stderr, "FATAL ERROR: "); |
12dc4ec0 |
42 | va_start(ap, p); |
43 | vfprintf(stderr, p, ap); |
44 | va_end(ap); |
45 | fputc('\n', stderr); |
7bfc5fd0 |
46 | if (logctx) { |
47 | log_free(logctx); |
48 | logctx = NULL; |
49 | } |
93b581bd |
50 | cleanup_exit(1); |
12dc4ec0 |
51 | } |
a8327734 |
52 | void connection_fatal(void *frontend, char *p, ...) |
32874aea |
53 | { |
8d5de777 |
54 | va_list ap; |
49bad831 |
55 | fprintf(stderr, "FATAL ERROR: "); |
8d5de777 |
56 | va_start(ap, p); |
57 | vfprintf(stderr, p, ap); |
58 | va_end(ap); |
59 | fputc('\n', stderr); |
7bfc5fd0 |
60 | if (logctx) { |
61 | log_free(logctx); |
62 | logctx = NULL; |
63 | } |
93b581bd |
64 | cleanup_exit(1); |
8d5de777 |
65 | } |
c0a81592 |
66 | void cmdline_error(char *p, ...) |
67 | { |
68 | va_list ap; |
69 | fprintf(stderr, "plink: "); |
70 | va_start(ap, p); |
71 | vfprintf(stderr, p, ap); |
72 | va_end(ap); |
73 | fputc('\n', stderr); |
74 | exit(1); |
75 | } |
12dc4ec0 |
76 | |
0965bee0 |
77 | HANDLE inhandle, outhandle, errhandle; |
34292b1d |
78 | struct handle *stdin_handle, *stdout_handle, *stderr_handle; |
6f34e365 |
79 | DWORD orig_console_mode; |
34292b1d |
80 | int connopen; |
6f34e365 |
81 | |
8df7a775 |
82 | WSAEVENT netevent; |
83 | |
6b78788a |
84 | static Backend *back; |
85 | static void *backhandle; |
3ea863a3 |
86 | static Config cfg; |
6b78788a |
87 | |
887035a5 |
88 | int term_ldisc(Terminal *term, int mode) |
32874aea |
89 | { |
90 | return FALSE; |
91 | } |
b9d7bcad |
92 | void ldisc_update(void *frontend, int echo, int edit) |
32874aea |
93 | { |
0965bee0 |
94 | /* Update stdin read mode to reflect changes in line discipline. */ |
95 | DWORD mode; |
96 | |
97 | mode = ENABLE_PROCESSED_INPUT; |
98 | if (echo) |
32874aea |
99 | mode = mode | ENABLE_ECHO_INPUT; |
0965bee0 |
100 | else |
32874aea |
101 | mode = mode & ~ENABLE_ECHO_INPUT; |
0965bee0 |
102 | if (edit) |
32874aea |
103 | mode = mode | ENABLE_LINE_INPUT; |
0965bee0 |
104 | else |
32874aea |
105 | mode = mode & ~ENABLE_LINE_INPUT; |
0965bee0 |
106 | SetConsoleMode(inhandle, mode); |
107 | } |
108 | |
c6ccd5c2 |
109 | char *get_ttymode(void *frontend, const char *mode) { return NULL; } |
110 | |
9fab77dc |
111 | int from_backend(void *frontend_handle, int is_stderr, |
112 | const char *data, int len) |
5471d09a |
113 | { |
5471d09a |
114 | if (is_stderr) { |
34292b1d |
115 | handle_write(stderr_handle, data, len); |
5471d09a |
116 | } else { |
34292b1d |
117 | handle_write(stdout_handle, data, len); |
5471d09a |
118 | } |
119 | |
34292b1d |
120 | return handle_backlog(stdout_handle) + handle_backlog(stderr_handle); |
5471d09a |
121 | } |
122 | |
edd0cb8a |
123 | int from_backend_untrusted(void *frontend_handle, const char *data, int len) |
124 | { |
125 | /* |
126 | * No "untrusted" output should get here (the way the code is |
127 | * currently, it's all diverted by FLAG_STDERR). |
128 | */ |
129 | assert(!"Unexpected call to from_backend_untrusted()"); |
130 | return 0; /* not reached */ |
131 | } |
132 | |
133 | int get_userpass_input(prompts_t *p, unsigned char *in, int inlen) |
134 | { |
135 | int ret; |
136 | ret = cmdline_get_passwd_input(p, in, inlen); |
137 | if (ret == -1) |
138 | ret = console_get_userpass_input(p, in, inlen); |
139 | return ret; |
140 | } |
141 | |
c44bf5bd |
142 | static DWORD main_thread_id; |
143 | |
144 | void agent_schedule_callback(void (*callback)(void *, void *, int), |
145 | void *callback_ctx, void *data, int len) |
146 | { |
147 | struct agent_callback *c = snew(struct agent_callback); |
148 | c->callback = callback; |
149 | c->callback_ctx = callback_ctx; |
150 | c->data = data; |
151 | c->len = len; |
152 | PostThreadMessage(main_thread_id, WM_AGENT_CALLBACK, 0, (LPARAM)c); |
153 | } |
154 | |
d8426c54 |
155 | /* |
156 | * Short description of parameters. |
157 | */ |
158 | static void usage(void) |
159 | { |
160 | printf("PuTTY Link: command-line connection utility\n"); |
161 | printf("%s\n", ver); |
162 | printf("Usage: plink [options] [user@]host [command]\n"); |
e672967c |
163 | printf(" (\"host\" can also be a PuTTY saved session name)\n"); |
d8426c54 |
164 | printf("Options:\n"); |
2285d016 |
165 | printf(" -V print version information and exit\n"); |
166 | printf(" -pgpfp print PGP key fingerprints and exit\n"); |
d8426c54 |
167 | printf(" -v show verbose messages\n"); |
e2a197cf |
168 | printf(" -load sessname Load settings from saved session\n"); |
f41f07bc |
169 | printf(" -ssh -telnet -rlogin -raw -serial\n"); |
afd4d0d2 |
170 | printf(" force use of a particular protocol\n"); |
d8426c54 |
171 | printf(" -P port connect to specified port\n"); |
e2a197cf |
172 | printf(" -l user connect with specified username\n"); |
e2a197cf |
173 | printf(" -batch disable all interactive prompts\n"); |
174 | printf("The following options only apply to SSH connections:\n"); |
175 | printf(" -pw passw login with specified password\n"); |
dbe6c525 |
176 | printf(" -D [listen-IP:]listen-port\n"); |
177 | printf(" Dynamic SOCKS-based port forwarding\n"); |
178 | printf(" -L [listen-IP:]listen-port:host:port\n"); |
179 | printf(" Forward local port to remote address\n"); |
180 | printf(" -R [listen-IP:]listen-port:host:port\n"); |
181 | printf(" Forward remote port to local address\n"); |
e2a197cf |
182 | printf(" -X -x enable / disable X11 forwarding\n"); |
183 | printf(" -A -a enable / disable agent forwarding\n"); |
184 | printf(" -t -T enable / disable pty allocation\n"); |
185 | printf(" -1 -2 force use of particular protocol version\n"); |
05581745 |
186 | printf(" -4 -6 force use of IPv4 or IPv6\n"); |
e2a197cf |
187 | printf(" -C enable compression\n"); |
188 | printf(" -i key private key file for authentication\n"); |
e5708bc7 |
189 | printf(" -noagent disable use of Pageant\n"); |
190 | printf(" -agent enable use of Pageant\n"); |
54018d95 |
191 | printf(" -m file read remote command(s) from file\n"); |
4d1cdf5d |
192 | printf(" -s remote command is an SSH subsystem (SSH-2 only)\n"); |
b72c366d |
193 | printf(" -N don't start a shell/command (SSH-2 only)\n"); |
23828b7e |
194 | printf(" -nc host:port\n"); |
195 | printf(" open tunnel in place of session (SSH-2 only)\n"); |
9621bbab |
196 | printf(" -sercfg configuration-string (e.g. 19200,8,n,1,X)\n"); |
197 | printf(" Specify the serial configuration (serial only)\n"); |
dc108ebc |
198 | exit(1); |
199 | } |
200 | |
201 | static void version(void) |
202 | { |
203 | printf("plink: %s\n", ver); |
d8426c54 |
204 | exit(1); |
205 | } |
206 | |
32874aea |
207 | char *do_select(SOCKET skt, int startup) |
208 | { |
8df7a775 |
209 | int events; |
210 | if (startup) { |
3ad9d396 |
211 | events = (FD_CONNECT | FD_READ | FD_WRITE | |
212 | FD_OOB | FD_CLOSE | FD_ACCEPT); |
8df7a775 |
213 | } else { |
214 | events = 0; |
215 | } |
7440fd44 |
216 | if (p_WSAEventSelect(skt, netevent, events) == SOCKET_ERROR) { |
217 | switch (p_WSAGetLastError()) { |
32874aea |
218 | case WSAENETDOWN: |
219 | return "Network is down"; |
220 | default: |
7440fd44 |
221 | return "WSAEventSelect(): unknown error"; |
32874aea |
222 | } |
8df7a775 |
223 | } |
224 | return NULL; |
225 | } |
226 | |
34292b1d |
227 | int stdin_gotdata(struct handle *h, void *data, int len) |
228 | { |
229 | if (len < 0) { |
230 | /* |
231 | * Special case: report read error. |
232 | */ |
21ae394f |
233 | char buf[4096]; |
234 | FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, -len, 0, |
235 | buf, lenof(buf), NULL); |
236 | buf[lenof(buf)-1] = '\0'; |
237 | if (buf[strlen(buf)-1] == '\n') |
238 | buf[strlen(buf)-1] = '\0'; |
239 | fprintf(stderr, "Unable to read from standard input: %s\n", buf); |
34292b1d |
240 | cleanup_exit(0); |
241 | } |
242 | noise_ultralight(len); |
6226c939 |
243 | if (connopen && back->connected(backhandle)) { |
34292b1d |
244 | if (len > 0) { |
245 | return back->send(backhandle, data, len); |
246 | } else { |
247 | back->special(backhandle, TS_EOF); |
248 | return 0; |
249 | } |
250 | } else |
251 | return 0; |
252 | } |
253 | |
254 | void stdouterr_sent(struct handle *h, int new_backlog) |
255 | { |
256 | if (new_backlog < 0) { |
257 | /* |
258 | * Special case: report write error. |
259 | */ |
21ae394f |
260 | char buf[4096]; |
261 | FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, -new_backlog, 0, |
262 | buf, lenof(buf), NULL); |
263 | buf[lenof(buf)-1] = '\0'; |
264 | if (buf[strlen(buf)-1] == '\n') |
265 | buf[strlen(buf)-1] = '\0'; |
266 | fprintf(stderr, "Unable to write to standard %s: %s\n", |
267 | (h == stdout_handle ? "output" : "error"), buf); |
34292b1d |
268 | cleanup_exit(0); |
269 | } |
6226c939 |
270 | if (connopen && back->connected(backhandle)) { |
34292b1d |
271 | back->unthrottle(backhandle, (handle_backlog(stdout_handle) + |
272 | handle_backlog(stderr_handle))); |
273 | } |
274 | } |
275 | |
32874aea |
276 | int main(int argc, char **argv) |
277 | { |
12dc4ec0 |
278 | int sending; |
d8426c54 |
279 | int portnumber = -1; |
8df7a775 |
280 | SOCKET *sklist; |
281 | int skcount, sksize; |
d8d6c7e5 |
282 | int exitcode; |
86256dc6 |
283 | int errors; |
f0f51e90 |
284 | int got_host = FALSE; |
4d1cdf5d |
285 | int use_subsystem = 0; |
39934deb |
286 | long now, next; |
12dc4ec0 |
287 | |
32874aea |
288 | sklist = NULL; |
289 | skcount = sksize = 0; |
c9bdcd96 |
290 | /* |
291 | * Initialise port and protocol to sensible defaults. (These |
292 | * will be overridden by more or less anything.) |
293 | */ |
294 | default_protocol = PROT_SSH; |
295 | default_port = 22; |
8df7a775 |
296 | |
67779be7 |
297 | flags = FLAG_STDERR; |
12dc4ec0 |
298 | /* |
299 | * Process the command line. |
300 | */ |
a9422f39 |
301 | do_defaults(NULL, &cfg); |
18e62ad8 |
302 | loaded_session = FALSE; |
e7a7383f |
303 | default_protocol = cfg.protocol; |
304 | default_port = cfg.port; |
86256dc6 |
305 | errors = 0; |
8cb9c947 |
306 | { |
32874aea |
307 | /* |
308 | * Override the default protocol if PLINK_PROTOCOL is set. |
309 | */ |
310 | char *p = getenv("PLINK_PROTOCOL"); |
32874aea |
311 | if (p) { |
9e164d82 |
312 | const Backend *b = backend_from_name(p); |
313 | if (b) { |
314 | default_protocol = cfg.protocol = b->protocol; |
315 | default_port = cfg.port = b->default_port; |
32874aea |
316 | } |
317 | } |
8cb9c947 |
318 | } |
12dc4ec0 |
319 | while (--argc) { |
32874aea |
320 | char *p = *++argv; |
321 | if (*p == '-') { |
5555d393 |
322 | int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL), |
323 | 1, &cfg); |
c0a81592 |
324 | if (ret == -2) { |
325 | fprintf(stderr, |
326 | "plink: option \"%s\" requires an argument\n", p); |
86256dc6 |
327 | errors = 1; |
c0a81592 |
328 | } else if (ret == 2) { |
329 | --argc, ++argv; |
330 | } else if (ret == 1) { |
331 | continue; |
ff2ae367 |
332 | } else if (!strcmp(p, "-batch")) { |
c0a81592 |
333 | console_batch_mode = 1; |
4d1cdf5d |
334 | } else if (!strcmp(p, "-s")) { |
335 | /* Save status to write to cfg later. */ |
336 | use_subsystem = 1; |
dc108ebc |
337 | } else if (!strcmp(p, "-V")) { |
338 | version(); |
2285d016 |
339 | } else if (!strcmp(p, "-pgpfp")) { |
340 | pgp_fingerprints(); |
341 | exit(1); |
86256dc6 |
342 | } else { |
343 | fprintf(stderr, "plink: unknown option \"%s\"\n", p); |
344 | errors = 1; |
32874aea |
345 | } |
12dc4ec0 |
346 | } else if (*p) { |
f0f51e90 |
347 | if (!cfg_launchable(&cfg) || !(got_host || loaded_session)) { |
32874aea |
348 | char *q = p; |
349 | /* |
350 | * If the hostname starts with "telnet:", set the |
351 | * protocol to Telnet and process the string as a |
352 | * Telnet URL. |
353 | */ |
354 | if (!strncmp(q, "telnet:", 7)) { |
355 | char c; |
356 | |
357 | q += 7; |
358 | if (q[0] == '/' && q[1] == '/') |
359 | q += 2; |
360 | cfg.protocol = PROT_TELNET; |
361 | p = q; |
362 | while (*p && *p != ':' && *p != '/') |
363 | p++; |
364 | c = *p; |
365 | if (*p) |
366 | *p++ = '\0'; |
367 | if (c == ':') |
368 | cfg.port = atoi(p); |
369 | else |
370 | cfg.port = -1; |
371 | strncpy(cfg.host, q, sizeof(cfg.host) - 1); |
372 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
f0f51e90 |
373 | got_host = TRUE; |
32874aea |
374 | } else { |
3608528b |
375 | char *r, *user, *host; |
32874aea |
376 | /* |
377 | * Before we process the [user@]host string, we |
378 | * first check for the presence of a protocol |
379 | * prefix (a protocol name followed by ","). |
380 | */ |
381 | r = strchr(p, ','); |
382 | if (r) { |
9e164d82 |
383 | const Backend *b; |
384 | *r = '\0'; |
385 | b = backend_from_name(p); |
386 | if (b) { |
387 | default_protocol = cfg.protocol = b->protocol; |
388 | portnumber = b->default_port; |
32874aea |
389 | } |
9e164d82 |
390 | p = r + 1; |
32874aea |
391 | } |
392 | |
393 | /* |
3608528b |
394 | * A nonzero length string followed by an @ is treated |
395 | * as a username. (We discount an _initial_ @.) The |
396 | * rest of the string (or the whole string if no @) |
397 | * is treated as a session name and/or hostname. |
32874aea |
398 | */ |
399 | r = strrchr(p, '@'); |
400 | if (r == p) |
401 | p++, r = NULL; /* discount initial @ */ |
3608528b |
402 | if (r) { |
403 | *r++ = '\0'; |
404 | user = p, host = r; |
405 | } else { |
406 | user = NULL, host = p; |
407 | } |
408 | |
409 | /* |
410 | * Now attempt to load a saved session with the |
411 | * same name as the hostname. |
412 | */ |
413 | { |
32874aea |
414 | Config cfg2; |
3608528b |
415 | do_defaults(host, &cfg2); |
7374c779 |
416 | if (loaded_session || !cfg_launchable(&cfg2)) { |
32874aea |
417 | /* No settings for this host; use defaults */ |
18e62ad8 |
418 | /* (or session was already loaded with -load) */ |
3608528b |
419 | strncpy(cfg.host, host, sizeof(cfg.host) - 1); |
32874aea |
420 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
421 | cfg.port = default_port; |
f0f51e90 |
422 | got_host = TRUE; |
32874aea |
423 | } else { |
424 | cfg = cfg2; |
f0f51e90 |
425 | loaded_session = TRUE; |
32874aea |
426 | } |
3608528b |
427 | } |
428 | |
429 | if (user) { |
430 | /* Patch in specified username. */ |
431 | strncpy(cfg.username, user, |
432 | sizeof(cfg.username) - 1); |
32874aea |
433 | cfg.username[sizeof(cfg.username) - 1] = '\0'; |
32874aea |
434 | } |
3608528b |
435 | |
32874aea |
436 | } |
437 | } else { |
385528da |
438 | char *command; |
439 | int cmdlen, cmdsize; |
440 | cmdlen = cmdsize = 0; |
441 | command = NULL; |
442 | |
443 | while (argc) { |
444 | while (*p) { |
445 | if (cmdlen >= cmdsize) { |
446 | cmdsize = cmdlen + 512; |
3d88e64d |
447 | command = sresize(command, cmdsize, char); |
385528da |
448 | } |
449 | command[cmdlen++]=*p++; |
450 | } |
451 | if (cmdlen >= cmdsize) { |
452 | cmdsize = cmdlen + 512; |
3d88e64d |
453 | command = sresize(command, cmdsize, char); |
385528da |
454 | } |
455 | command[cmdlen++]=' '; /* always add trailing space */ |
456 | if (--argc) p = *++argv; |
32874aea |
457 | } |
385528da |
458 | if (cmdlen) command[--cmdlen]='\0'; |
459 | /* change trailing blank to NUL */ |
460 | cfg.remote_cmd_ptr = command; |
461 | cfg.remote_cmd_ptr2 = NULL; |
32874aea |
462 | cfg.nopty = TRUE; /* command => no terminal */ |
385528da |
463 | |
32874aea |
464 | break; /* done with cmdline */ |
465 | } |
12dc4ec0 |
466 | } |
467 | } |
468 | |
86256dc6 |
469 | if (errors) |
470 | return 1; |
471 | |
f0f51e90 |
472 | if (!cfg_launchable(&cfg) || !(got_host || loaded_session)) { |
32874aea |
473 | usage(); |
d8426c54 |
474 | } |
d8426c54 |
475 | |
449925a6 |
476 | /* |
477 | * Trim leading whitespace off the hostname if it's there. |
478 | */ |
479 | { |
480 | int space = strspn(cfg.host, " \t"); |
481 | memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space); |
482 | } |
483 | |
484 | /* See if host is of the form user@host */ |
7374c779 |
485 | if (cfg_launchable(&cfg)) { |
5dd103a8 |
486 | char *atsign = strrchr(cfg.host, '@'); |
449925a6 |
487 | /* Make sure we're not overflowing the user field */ |
488 | if (atsign) { |
489 | if (atsign - cfg.host < sizeof cfg.username) { |
490 | strncpy(cfg.username, cfg.host, atsign - cfg.host); |
491 | cfg.username[atsign - cfg.host] = '\0'; |
492 | } |
493 | memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1)); |
494 | } |
495 | } |
496 | |
497 | /* |
c0a81592 |
498 | * Perform command-line overrides on session configuration. |
499 | */ |
5555d393 |
500 | cmdline_run_saved(&cfg); |
c0a81592 |
501 | |
502 | /* |
4d1cdf5d |
503 | * Apply subsystem status. |
504 | */ |
505 | if (use_subsystem) |
506 | cfg.ssh_subsys = TRUE; |
507 | |
508 | /* |
449925a6 |
509 | * Trim a colon suffix off the hostname if it's there. |
510 | */ |
511 | cfg.host[strcspn(cfg.host, ":")] = '\0'; |
512 | |
cae0c023 |
513 | /* |
514 | * Remove any remaining whitespace from the hostname. |
515 | */ |
516 | { |
517 | int p1 = 0, p2 = 0; |
518 | while (cfg.host[p2] != '\0') { |
519 | if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') { |
520 | cfg.host[p1] = cfg.host[p2]; |
521 | p1++; |
522 | } |
523 | p2++; |
524 | } |
525 | cfg.host[p1] = '\0'; |
526 | } |
527 | |
feb02b4e |
528 | if (!cfg.remote_cmd_ptr && !*cfg.remote_cmd && !*cfg.ssh_nc_host) |
32874aea |
529 | flags |= FLAG_INTERACTIVE; |
67779be7 |
530 | |
12dc4ec0 |
531 | /* |
532 | * Select protocol. This is farmed out into a table in a |
533 | * separate file to enable an ssh-free variant. |
534 | */ |
9e164d82 |
535 | back = backend_from_proto(cfg.protocol); |
536 | if (back == NULL) { |
537 | fprintf(stderr, |
538 | "Internal fault: Unsupported protocol found\n"); |
539 | return 1; |
12dc4ec0 |
540 | } |
541 | |
542 | /* |
8cb9c947 |
543 | * Select port. |
544 | */ |
545 | if (portnumber != -1) |
32874aea |
546 | cfg.port = portnumber; |
8cb9c947 |
547 | |
7440fd44 |
548 | sk_init(); |
549 | if (p_WSAEventSelect == NULL) { |
550 | fprintf(stderr, "Plink requires WinSock 2\n"); |
12dc4ec0 |
551 | return 1; |
552 | } |
553 | |
1626d169 |
554 | logctx = log_init(NULL, &cfg); |
555 | console_provide_logctx(logctx); |
556 | |
12dc4ec0 |
557 | /* |
558 | * Start up the connection. |
559 | */ |
8df7a775 |
560 | netevent = CreateEvent(NULL, FALSE, FALSE, NULL); |
12dc4ec0 |
561 | { |
cbe2d68f |
562 | const char *error; |
12dc4ec0 |
563 | char *realhost; |
2184a5d9 |
564 | /* nodelay is only useful if stdin is a character device (console) */ |
565 | int nodelay = cfg.tcp_nodelay && |
566 | (GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_CHAR); |
12dc4ec0 |
567 | |
86916870 |
568 | error = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port, |
79bf227b |
569 | &realhost, nodelay, cfg.tcp_keepalives); |
12dc4ec0 |
570 | if (error) { |
571 | fprintf(stderr, "Unable to open connection:\n%s", error); |
572 | return 1; |
573 | } |
a8327734 |
574 | back->provide_logctx(backhandle, logctx); |
6e1ebb76 |
575 | sfree(realhost); |
12dc4ec0 |
576 | } |
8df7a775 |
577 | connopen = 1; |
12dc4ec0 |
578 | |
0965bee0 |
579 | inhandle = GetStdHandle(STD_INPUT_HANDLE); |
12dc4ec0 |
580 | outhandle = GetStdHandle(STD_OUTPUT_HANDLE); |
fe50e814 |
581 | errhandle = GetStdHandle(STD_ERROR_HANDLE); |
34292b1d |
582 | |
12dc4ec0 |
583 | /* |
12dc4ec0 |
584 | * Turn off ECHO and LINE input modes. We don't care if this |
585 | * call fails, because we know we aren't necessarily running in |
586 | * a console. |
587 | */ |
f8efb0d5 |
588 | GetConsoleMode(inhandle, &orig_console_mode); |
589 | SetConsoleMode(inhandle, ENABLE_PROCESSED_INPUT); |
590 | |
34292b1d |
591 | /* |
592 | * Pass the output handles to the handle-handling subsystem. |
593 | * (The input one we leave until we're through the |
594 | * authentication process.) |
595 | */ |
bdebd7e9 |
596 | stdout_handle = handle_output_new(outhandle, stdouterr_sent, NULL, 0); |
597 | stderr_handle = handle_output_new(errhandle, stdouterr_sent, NULL, 0); |
34292b1d |
598 | |
f8efb0d5 |
599 | main_thread_id = GetCurrentThreadId(); |
600 | |
12dc4ec0 |
601 | sending = FALSE; |
5471d09a |
602 | |
39934deb |
603 | now = GETTICKCOUNT(); |
604 | |
12dc4ec0 |
605 | while (1) { |
34292b1d |
606 | int nhandles; |
607 | HANDLE *handles; |
32874aea |
608 | int n; |
39934deb |
609 | DWORD ticks; |
32874aea |
610 | |
51470298 |
611 | if (!sending && back->sendok(backhandle)) { |
bdebd7e9 |
612 | stdin_handle = handle_input_new(inhandle, stdin_gotdata, NULL, |
613 | 0); |
32874aea |
614 | sending = TRUE; |
615 | } |
616 | |
39934deb |
617 | if (run_timers(now, &next)) { |
618 | ticks = next - GETTICKCOUNT(); |
619 | if (ticks < 0) ticks = 0; /* just in case */ |
620 | } else { |
621 | ticks = INFINITE; |
622 | } |
623 | |
34292b1d |
624 | handles = handle_get_events(&nhandles); |
625 | handles = sresize(handles, nhandles+1, HANDLE); |
626 | handles[nhandles] = netevent; |
627 | n = MsgWaitForMultipleObjects(nhandles+1, handles, FALSE, ticks, |
c44bf5bd |
628 | QS_POSTMESSAGE); |
34292b1d |
629 | if ((unsigned)(n - WAIT_OBJECT_0) < (unsigned)nhandles) { |
630 | handle_got_event(handles[n - WAIT_OBJECT_0]); |
631 | } else if (n == WAIT_OBJECT_0 + nhandles) { |
32874aea |
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; |
3d88e64d |
653 | sklist = sresize(sklist, sksize, SOCKET); |
32874aea |
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; |
7440fd44 |
669 | if (!p_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 | } |
34292b1d |
692 | } else if (n == WAIT_OBJECT_0 + nhandles + 1) { |
c44bf5bd |
693 | MSG msg; |
694 | while (PeekMessage(&msg, INVALID_HANDLE_VALUE, |
695 | WM_AGENT_CALLBACK, WM_AGENT_CALLBACK, |
696 | PM_REMOVE)) { |
697 | struct agent_callback *c = (struct agent_callback *)msg.lParam; |
698 | c->callback(c->callback_ctx, c->data, c->len); |
699 | sfree(c); |
700 | } |
5471d09a |
701 | } |
39934deb |
702 | |
703 | if (n == WAIT_TIMEOUT) { |
704 | now = next; |
705 | } else { |
706 | now = GETTICKCOUNT(); |
707 | } |
708 | |
34292b1d |
709 | sfree(handles); |
710 | |
711 | if (sending) |
712 | handle_unthrottle(stdin_handle, back->sendbuffer(backhandle)); |
713 | |
6226c939 |
714 | if ((!connopen || !back->connected(backhandle)) && |
34292b1d |
715 | handle_backlog(stdout_handle) + handle_backlog(stderr_handle) == 0) |
32874aea |
716 | break; /* we closed the connection */ |
12dc4ec0 |
717 | } |
51470298 |
718 | exitcode = back->exitcode(backhandle); |
d8d6c7e5 |
719 | if (exitcode < 0) { |
720 | fprintf(stderr, "Remote process exit code unavailable\n"); |
721 | exitcode = 1; /* this is an error condition */ |
722 | } |
7440fd44 |
723 | cleanup_exit(exitcode); |
724 | return 0; /* placate compiler warning */ |
12dc4ec0 |
725 | } |