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