]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - plink.c
2802febc7fa30e4e1423f9f5fe2f21d4092c7a9f
[PuTTY.git] / plink.c
1 /*
2  * PLink - a command-line (stdin/stdout) variant of PuTTY.
3  */
4
5 #ifndef AUTO_WINSOCK
6 #include <winsock2.h>
7 #endif
8 #include <windows.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stdarg.h>
12
13 #define PUTTY_DO_GLOBALS               /* actually _define_ globals */
14 #include "putty.h"
15 #include "storage.h"
16 #include "tree234.h"
17
18 #define MAX_STDIN_BACKLOG 4096
19
20 void fatalbox(char *p, ...)
21 {
22     va_list ap;
23     fprintf(stderr, "FATAL ERROR: ");
24     va_start(ap, p);
25     vfprintf(stderr, p, ap);
26     va_end(ap);
27     fputc('\n', stderr);
28     WSACleanup();
29     exit(1);
30 }
31 void connection_fatal(char *p, ...)
32 {
33     va_list ap;
34     fprintf(stderr, "FATAL ERROR: ");
35     va_start(ap, p);
36     vfprintf(stderr, p, ap);
37     va_end(ap);
38     fputc('\n', stderr);
39     WSACleanup();
40     exit(1);
41 }
42
43 static char *password = NULL;
44
45 void logevent(char *string)
46 {
47 }
48
49 void verify_ssh_host_key(char *host, int port, char *keytype,
50                          char *keystr, char *fingerprint)
51 {
52     int ret;
53     HANDLE hin;
54     DWORD savemode, i;
55
56     static const char absentmsg[] =
57         "The server's host key is not cached in the registry. You\n"
58         "have no guarantee that the server is the computer you\n"
59         "think it is.\n"
60         "The server's key fingerprint is:\n"
61         "%s\n"
62         "If you trust this host, enter \"y\" to add the key to\n"
63         "PuTTY's cache and carry on connecting.\n"
64         "If you want to carry on connecting just once, without\n"
65         "adding the key to the cache, enter \"n\".\n"
66         "If you do not trust this host, press Return to abandon the\n"
67         "connection.\n"
68         "Store key in cache? (y/n) ";
69
70     static const char wrongmsg[] =
71         "WARNING - POTENTIAL SECURITY BREACH!\n"
72         "The server's host key does not match the one PuTTY has\n"
73         "cached in the registry. This means that either the\n"
74         "server administrator has changed the host key, or you\n"
75         "have actually connected to another computer pretending\n"
76         "to be the server.\n"
77         "The new key fingerprint is:\n"
78         "%s\n"
79         "If you were expecting this change and trust the new key,\n"
80         "enter \"y\" to update PuTTY's cache and continue connecting.\n"
81         "If you want to carry on connecting but without updating\n"
82         "the cache, enter \"n\".\n"
83         "If you want to abandon the connection completely, press\n"
84         "Return to cancel. Pressing Return is the ONLY guaranteed\n"
85         "safe choice.\n"
86         "Update cached key? (y/n, Return cancels connection) ";
87
88     static const char abandoned[] = "Connection abandoned.\n";
89
90     char line[32];
91
92     /*
93      * Verify the key against the registry.
94      */
95     ret = verify_host_key(host, port, keytype, keystr);
96
97     if (ret == 0)                      /* success - key matched OK */
98         return;
99
100     if (ret == 2) {                    /* key was different */
101         fprintf(stderr, wrongmsg, fingerprint);
102         fflush(stderr);
103     }
104     if (ret == 1) {                    /* key was absent */
105         fprintf(stderr, absentmsg, fingerprint);
106         fflush(stderr);
107     }
108
109     hin = GetStdHandle(STD_INPUT_HANDLE);
110     GetConsoleMode(hin, &savemode);
111     SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
112                          ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
113     ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
114     SetConsoleMode(hin, savemode);
115
116     if (line[0] != '\0' && line[0] != '\r' && line[0] != '\n') {
117         if (line[0] == 'y' || line[0] == 'Y')
118             store_host_key(host, port, keytype, keystr);
119     } else {
120         fprintf(stderr, abandoned);
121         exit(0);
122     }
123 }
124
125 /*
126  * Ask whether the selected cipher is acceptable (since it was
127  * below the configured 'warn' threshold).
128  * cs: 0 = both ways, 1 = client->server, 2 = server->client
129  */
130 void askcipher(char *ciphername, int cs)
131 {
132     HANDLE hin;
133     DWORD savemode, i;
134
135     static const char msg[] =
136         "The first %scipher supported by the server is\n"
137         "%s, which is below the configured warning threshold.\n"
138         "Continue with connection? (y/n) ";
139     static const char abandoned[] = "Connection abandoned.\n";
140
141     char line[32];
142
143     fprintf(stderr, msg,
144             (cs == 0) ? "" :
145             (cs == 1) ? "client-to-server " :
146                         "server-to-client ",
147             ciphername);
148     fflush(stderr);
149
150     hin = GetStdHandle(STD_INPUT_HANDLE);
151     GetConsoleMode(hin, &savemode);
152     SetConsoleMode(hin, (savemode | ENABLE_ECHO_INPUT |
153                          ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT));
154     ReadFile(hin, line, sizeof(line) - 1, &i, NULL);
155     SetConsoleMode(hin, savemode);
156
157     if (line[0] == 'y' || line[0] == 'Y') {
158         return;
159     } else {
160         fprintf(stderr, abandoned);
161         exit(0);
162     }
163 }
164
165 HANDLE inhandle, outhandle, errhandle;
166 DWORD orig_console_mode;
167
168 WSAEVENT netevent;
169
170 int term_ldisc(int mode)
171 {
172     return FALSE;
173 }
174 void ldisc_update(int echo, int edit)
175 {
176     /* Update stdin read mode to reflect changes in line discipline. */
177     DWORD mode;
178
179     mode = ENABLE_PROCESSED_INPUT;
180     if (echo)
181         mode = mode | ENABLE_ECHO_INPUT;
182     else
183         mode = mode & ~ENABLE_ECHO_INPUT;
184     if (edit)
185         mode = mode | ENABLE_LINE_INPUT;
186     else
187         mode = mode & ~ENABLE_LINE_INPUT;
188     SetConsoleMode(inhandle, mode);
189 }
190
191 static int get_line(const char *prompt, char *str, int maxlen, int is_pw)
192 {
193     HANDLE hin, hout;
194     DWORD savemode, newmode, i;
195
196     if (is_pw && password) {
197         static int tried_once = 0;
198
199         if (tried_once) {
200             return 0;
201         } else {
202             strncpy(str, password, maxlen);
203             str[maxlen - 1] = '\0';
204             tried_once = 1;
205             return 1;
206         }
207     }
208
209     hin = GetStdHandle(STD_INPUT_HANDLE);
210     hout = GetStdHandle(STD_OUTPUT_HANDLE);
211     if (hin == INVALID_HANDLE_VALUE || hout == INVALID_HANDLE_VALUE) {
212         fprintf(stderr, "Cannot get standard input/output handles");
213         return 0;
214     }
215
216     GetConsoleMode(hin, &savemode);
217     newmode = savemode | ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT;
218     if (is_pw)
219         newmode &= ~ENABLE_ECHO_INPUT;
220     else
221         newmode |= ENABLE_ECHO_INPUT;
222     SetConsoleMode(hin, newmode);
223
224     WriteFile(hout, prompt, strlen(prompt), &i, NULL);
225     ReadFile(hin, str, maxlen - 1, &i, NULL);
226
227     SetConsoleMode(hin, savemode);
228
229     if ((int) i > maxlen)
230         i = maxlen - 1;
231     else
232         i = i - 2;
233     str[i] = '\0';
234
235     if (is_pw)
236         WriteFile(hout, "\r\n", 2, &i, NULL);
237
238     return 1;
239 }
240
241 struct input_data {
242     DWORD len;
243     char buffer[4096];
244     HANDLE event, eventback;
245 };
246
247 static DWORD WINAPI stdin_read_thread(void *param)
248 {
249     struct input_data *idata = (struct input_data *) param;
250     HANDLE inhandle;
251
252     inhandle = GetStdHandle(STD_INPUT_HANDLE);
253
254     while (ReadFile(inhandle, idata->buffer, sizeof(idata->buffer),
255                     &idata->len, NULL) && idata->len > 0) {
256         SetEvent(idata->event);
257         WaitForSingleObject(idata->eventback, INFINITE);
258     }
259
260     idata->len = 0;
261     SetEvent(idata->event);
262
263     return 0;
264 }
265
266 struct output_data {
267     DWORD len, lenwritten;
268     int writeret;
269     char *buffer;
270     int is_stderr, done;
271     HANDLE event, eventback;
272     int busy;
273 };
274
275 static DWORD WINAPI stdout_write_thread(void *param)
276 {
277     struct output_data *odata = (struct output_data *) param;
278     HANDLE outhandle, errhandle;
279
280     outhandle = GetStdHandle(STD_OUTPUT_HANDLE);
281     errhandle = GetStdHandle(STD_ERROR_HANDLE);
282
283     while (1) {
284         WaitForSingleObject(odata->eventback, INFINITE);
285         if (odata->done)
286             break;
287         odata->writeret =
288             WriteFile(odata->is_stderr ? errhandle : outhandle,
289                       odata->buffer, odata->len, &odata->lenwritten, NULL);
290         SetEvent(odata->event);
291     }
292
293     return 0;
294 }
295
296 bufchain stdout_data, stderr_data;
297 struct output_data odata, edata;
298
299 void try_output(int is_stderr)
300 {
301     struct output_data *data = (is_stderr ? &edata : &odata);
302     void *senddata;
303     int sendlen;
304
305     if (!data->busy) {
306         bufchain_prefix(is_stderr ? &stderr_data : &stdout_data,
307                         &senddata, &sendlen);
308         data->buffer = senddata;
309         data->len = sendlen;
310         SetEvent(data->eventback);
311         data->busy = 1;
312     }
313 }
314
315 int from_backend(int is_stderr, char *data, int len)
316 {
317     int pos;
318     DWORD ret;
319     HANDLE h = (is_stderr ? errhandle : outhandle);
320     void *writedata;
321     int writelen;
322     int osize, esize;
323
324     if (is_stderr) {
325         bufchain_add(&stderr_data, data, len);
326         try_output(1);
327     } else {
328         bufchain_add(&stdout_data, data, len);
329         try_output(0);
330     }
331
332     osize = bufchain_size(&stdout_data);
333     esize = bufchain_size(&stderr_data);
334
335     return osize + esize;
336 }
337
338 /*
339  *  Short description of parameters.
340  */
341 static void usage(void)
342 {
343     printf("PuTTY Link: command-line connection utility\n");
344     printf("%s\n", ver);
345     printf("Usage: plink [options] [user@]host [command]\n");
346     printf("Options:\n");
347     printf("  -v        show verbose messages\n");
348     printf("  -ssh      force use of ssh protocol\n");
349     printf("  -P port   connect to specified port\n");
350     printf("  -pw passw login with specified password\n");
351     printf("  -m file   read remote command(s) from file\n");
352     exit(1);
353 }
354
355 char *do_select(SOCKET skt, int startup)
356 {
357     int events;
358     if (startup) {
359         events = FD_READ | FD_WRITE | FD_OOB | FD_CLOSE | FD_ACCEPT;
360     } else {
361         events = 0;
362     }
363     if (WSAEventSelect(skt, netevent, events) == SOCKET_ERROR) {
364         switch (WSAGetLastError()) {
365           case WSAENETDOWN:
366             return "Network is down";
367           default:
368             return "WSAAsyncSelect(): unknown error";
369         }
370     }
371     return NULL;
372 }
373
374 int main(int argc, char **argv)
375 {
376     WSADATA wsadata;
377     WORD winsock_ver;
378     WSAEVENT stdinevent, stdoutevent, stderrevent;
379     HANDLE handles[4];
380     DWORD in_threadid, out_threadid, err_threadid;
381     struct input_data idata;
382     int reading;
383     int sending;
384     int portnumber = -1;
385     SOCKET *sklist;
386     int skcount, sksize;
387     int connopen;
388
389     ssh_get_line = get_line;
390
391     sklist = NULL;
392     skcount = sksize = 0;
393     /*
394      * Initialise port and protocol to sensible defaults. (These
395      * will be overridden by more or less anything.)
396      */
397     default_protocol = PROT_SSH;
398     default_port = 22;
399
400     flags = FLAG_STDERR;
401     /*
402      * Process the command line.
403      */
404     do_defaults(NULL, &cfg);
405     default_protocol = cfg.protocol;
406     default_port = cfg.port;
407     {
408         /*
409          * Override the default protocol if PLINK_PROTOCOL is set.
410          */
411         char *p = getenv("PLINK_PROTOCOL");
412         int i;
413         if (p) {
414             for (i = 0; backends[i].backend != NULL; i++) {
415                 if (!strcmp(backends[i].name, p)) {
416                     default_protocol = cfg.protocol = backends[i].protocol;
417                     default_port = cfg.port =
418                         backends[i].backend->default_port;
419                     break;
420                 }
421             }
422         }
423     }
424     while (--argc) {
425         char *p = *++argv;
426         if (*p == '-') {
427             if (!strcmp(p, "-ssh")) {
428                 default_protocol = cfg.protocol = PROT_SSH;
429                 default_port = cfg.port = 22;
430             } else if (!strcmp(p, "-telnet")) {
431                 default_protocol = cfg.protocol = PROT_TELNET;
432                 default_port = cfg.port = 23;
433             } else if (!strcmp(p, "-raw")) {
434                 default_protocol = cfg.protocol = PROT_RAW;
435             } else if (!strcmp(p, "-v")) {
436                 flags |= FLAG_VERBOSE;
437             } else if (!strcmp(p, "-log")) {
438                 logfile = "putty.log";
439             } else if (!strcmp(p, "-pw") && argc > 1) {
440                 --argc, password = *++argv;
441             } else if (!strcmp(p, "-l") && argc > 1) {
442                 char *username;
443                 --argc, username = *++argv;
444                 strncpy(cfg.username, username, sizeof(cfg.username));
445                 cfg.username[sizeof(cfg.username) - 1] = '\0';
446             } else if (!strcmp(p, "-m") && argc > 1) {
447                 char *filename, *command;
448                 int cmdlen, cmdsize;
449                 FILE *fp;
450                 int c, d;
451
452                 --argc, filename = *++argv;
453
454                 cmdlen = cmdsize = 0;
455                 command = NULL;
456                 fp = fopen(filename, "r");
457                 if (!fp) {
458                     fprintf(stderr, "plink: unable to open command "
459                             "file \"%s\"\n", filename);
460                     return 1;
461                 }
462                 do {
463                     c = fgetc(fp);
464                     d = c;
465                     if (c == EOF)
466                         d = 0;
467                     if (cmdlen >= cmdsize) {
468                         cmdsize = cmdlen + 512;
469                         command = srealloc(command, cmdsize);
470                     }
471                     command[cmdlen++] = d;
472                 } while (c != EOF);
473                 cfg.remote_cmd_ptr = command;
474                 cfg.nopty = TRUE;      /* command => no terminal */
475             } else if (!strcmp(p, "-P") && argc > 1) {
476                 --argc, portnumber = atoi(*++argv);
477             }
478         } else if (*p) {
479             if (!*cfg.host) {
480                 char *q = p;
481                 /*
482                  * If the hostname starts with "telnet:", set the
483                  * protocol to Telnet and process the string as a
484                  * Telnet URL.
485                  */
486                 if (!strncmp(q, "telnet:", 7)) {
487                     char c;
488
489                     q += 7;
490                     if (q[0] == '/' && q[1] == '/')
491                         q += 2;
492                     cfg.protocol = PROT_TELNET;
493                     p = q;
494                     while (*p && *p != ':' && *p != '/')
495                         p++;
496                     c = *p;
497                     if (*p)
498                         *p++ = '\0';
499                     if (c == ':')
500                         cfg.port = atoi(p);
501                     else
502                         cfg.port = -1;
503                     strncpy(cfg.host, q, sizeof(cfg.host) - 1);
504                     cfg.host[sizeof(cfg.host) - 1] = '\0';
505                 } else {
506                     char *r;
507                     /*
508                      * Before we process the [user@]host string, we
509                      * first check for the presence of a protocol
510                      * prefix (a protocol name followed by ",").
511                      */
512                     r = strchr(p, ',');
513                     if (r) {
514                         int i, j;
515                         for (i = 0; backends[i].backend != NULL; i++) {
516                             j = strlen(backends[i].name);
517                             if (j == r - p &&
518                                 !memcmp(backends[i].name, p, j)) {
519                                 default_protocol = cfg.protocol =
520                                     backends[i].protocol;
521                                 portnumber =
522                                     backends[i].backend->default_port;
523                                 p = r + 1;
524                                 break;
525                             }
526                         }
527                     }
528
529                     /*
530                      * Three cases. Either (a) there's a nonzero
531                      * length string followed by an @, in which
532                      * case that's user and the remainder is host.
533                      * Or (b) there's only one string, not counting
534                      * a potential initial @, and it exists in the
535                      * saved-sessions database. Or (c) only one
536                      * string and it _doesn't_ exist in the
537                      * database.
538                      */
539                     r = strrchr(p, '@');
540                     if (r == p)
541                         p++, r = NULL; /* discount initial @ */
542                     if (r == NULL) {
543                         /*
544                          * One string.
545                          */
546                         Config cfg2;
547                         do_defaults(p, &cfg2);
548                         if (cfg2.host[0] == '\0') {
549                             /* No settings for this host; use defaults */
550                             strncpy(cfg.host, p, sizeof(cfg.host) - 1);
551                             cfg.host[sizeof(cfg.host) - 1] = '\0';
552                             cfg.port = default_port;
553                         } else {
554                             cfg = cfg2;
555                             cfg.remote_cmd_ptr = cfg.remote_cmd;
556                         }
557                     } else {
558                         *r++ = '\0';
559                         strncpy(cfg.username, p, sizeof(cfg.username) - 1);
560                         cfg.username[sizeof(cfg.username) - 1] = '\0';
561                         strncpy(cfg.host, r, sizeof(cfg.host) - 1);
562                         cfg.host[sizeof(cfg.host) - 1] = '\0';
563                         cfg.port = default_port;
564                     }
565                 }
566             } else {
567                 int len = sizeof(cfg.remote_cmd) - 1;
568                 char *cp = cfg.remote_cmd;
569                 int len2;
570
571                 strncpy(cp, p, len);
572                 cp[len] = '\0';
573                 len2 = strlen(cp);
574                 len -= len2;
575                 cp += len2;
576                 while (--argc) {
577                     if (len > 0)
578                         len--, *cp++ = ' ';
579                     strncpy(cp, *++argv, len);
580                     cp[len] = '\0';
581                     len2 = strlen(cp);
582                     len -= len2;
583                     cp += len2;
584                 }
585                 cfg.nopty = TRUE;      /* command => no terminal */
586                 break;                 /* done with cmdline */
587             }
588         }
589     }
590
591     if (!*cfg.host) {
592         usage();
593     }
594
595     if (!*cfg.remote_cmd_ptr)
596         flags |= FLAG_INTERACTIVE;
597
598     /*
599      * Select protocol. This is farmed out into a table in a
600      * separate file to enable an ssh-free variant.
601      */
602     {
603         int i;
604         back = NULL;
605         for (i = 0; backends[i].backend != NULL; i++)
606             if (backends[i].protocol == cfg.protocol) {
607                 back = backends[i].backend;
608                 break;
609             }
610         if (back == NULL) {
611             fprintf(stderr,
612                     "Internal fault: Unsupported protocol found\n");
613             return 1;
614         }
615     }
616
617     /*
618      * Select port.
619      */
620     if (portnumber != -1)
621         cfg.port = portnumber;
622
623     /*
624      * Initialise WinSock.
625      */
626     winsock_ver = MAKEWORD(2, 0);
627     if (WSAStartup(winsock_ver, &wsadata)) {
628         MessageBox(NULL, "Unable to initialise WinSock", "WinSock Error",
629                    MB_OK | MB_ICONEXCLAMATION);
630         return 1;
631     }
632     if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 0) {
633         MessageBox(NULL, "WinSock version is incompatible with 2.0",
634                    "WinSock Error", MB_OK | MB_ICONEXCLAMATION);
635         WSACleanup();
636         return 1;
637     }
638     sk_init();
639
640     /*
641      * Start up the connection.
642      */
643     netevent = CreateEvent(NULL, FALSE, FALSE, NULL);
644     {
645         char *error;
646         char *realhost;
647
648         error = back->init(cfg.host, cfg.port, &realhost);
649         if (error) {
650             fprintf(stderr, "Unable to open connection:\n%s", error);
651             return 1;
652         }
653         sfree(realhost);
654     }
655     connopen = 1;
656
657     stdinevent = CreateEvent(NULL, FALSE, FALSE, NULL);
658     stdoutevent = CreateEvent(NULL, FALSE, FALSE, NULL);
659     stderrevent = CreateEvent(NULL, FALSE, FALSE, NULL);
660
661     inhandle = GetStdHandle(STD_INPUT_HANDLE);
662     outhandle = GetStdHandle(STD_OUTPUT_HANDLE);
663     errhandle = GetStdHandle(STD_ERROR_HANDLE);
664     GetConsoleMode(inhandle, &orig_console_mode);
665     SetConsoleMode(inhandle, ENABLE_PROCESSED_INPUT);
666
667     /*
668      * Turn off ECHO and LINE input modes. We don't care if this
669      * call fails, because we know we aren't necessarily running in
670      * a console.
671      */
672     handles[0] = netevent;
673     handles[1] = stdinevent;
674     handles[2] = stdoutevent;
675     handles[3] = stderrevent;
676     sending = FALSE;
677
678     /*
679      * Create spare threads to write to stdout and stderr, so we
680      * can arrange asynchronous writes.
681      */
682     odata.event = stdoutevent;
683     odata.eventback = CreateEvent(NULL, FALSE, FALSE, NULL);
684     odata.is_stderr = 0;
685     odata.busy = odata.done = 0;
686     if (!CreateThread(NULL, 0, stdout_write_thread,
687                       &odata, 0, &out_threadid)) {
688         fprintf(stderr, "Unable to create output thread\n");
689         exit(1);
690     }
691     edata.event = stderrevent;
692     edata.eventback = CreateEvent(NULL, FALSE, FALSE, NULL);
693     edata.is_stderr = 1;
694     edata.busy = edata.done = 0;
695     if (!CreateThread(NULL, 0, stdout_write_thread,
696                       &edata, 0, &err_threadid)) {
697         fprintf(stderr, "Unable to create error output thread\n");
698         exit(1);
699     }
700
701     while (1) {
702         int n;
703
704         if (!sending && back->sendok()) {
705             /*
706              * Create a separate thread to read from stdin. This is
707              * a total pain, but I can't find another way to do it:
708              *
709              *  - an overlapped ReadFile or ReadFileEx just doesn't
710              *    happen; we get failure from ReadFileEx, and
711              *    ReadFile blocks despite being given an OVERLAPPED
712              *    structure. Perhaps we can't do overlapped reads
713              *    on consoles. WHY THE HELL NOT?
714              * 
715              *  - WaitForMultipleObjects(netevent, console) doesn't
716              *    work, because it signals the console when
717              *    _anything_ happens, including mouse motions and
718              *    other things that don't cause data to be readable
719              *    - so we're back to ReadFile blocking.
720              */
721             idata.event = stdinevent;
722             idata.eventback = CreateEvent(NULL, FALSE, FALSE, NULL);
723             if (!CreateThread(NULL, 0, stdin_read_thread,
724                               &idata, 0, &in_threadid)) {
725                 fprintf(stderr, "Unable to create input thread\n");
726                 exit(1);
727             }
728             sending = TRUE;
729         }
730
731         n = WaitForMultipleObjects(4, handles, FALSE, INFINITE);
732         if (n == 0) {
733             WSANETWORKEVENTS things;
734             SOCKET socket;
735             extern SOCKET first_socket(int *), next_socket(int *);
736             extern int select_result(WPARAM, LPARAM);
737             int i, socketstate;
738
739             /*
740              * We must not call select_result() for any socket
741              * until we have finished enumerating within the tree.
742              * This is because select_result() may close the socket
743              * and modify the tree.
744              */
745             /* Count the active sockets. */
746             i = 0;
747             for (socket = first_socket(&socketstate);
748                  socket != INVALID_SOCKET;
749                  socket = next_socket(&socketstate)) i++;
750
751             /* Expand the buffer if necessary. */
752             if (i > sksize) {
753                 sksize = i + 16;
754                 sklist = srealloc(sklist, sksize * sizeof(*sklist));
755             }
756
757             /* Retrieve the sockets into sklist. */
758             skcount = 0;
759             for (socket = first_socket(&socketstate);
760                  socket != INVALID_SOCKET;
761                  socket = next_socket(&socketstate)) {
762                 sklist[skcount++] = socket;
763             }
764
765             /* Now we're done enumerating; go through the list. */
766             for (i = 0; i < skcount; i++) {
767                 WPARAM wp;
768                 socket = sklist[i];
769                 wp = (WPARAM) socket;
770                 if (!WSAEnumNetworkEvents(socket, NULL, &things)) {
771                     noise_ultralight(socket);
772                     noise_ultralight(things.lNetworkEvents);
773                     if (things.lNetworkEvents & FD_READ)
774                         connopen &= select_result(wp, (LPARAM) FD_READ);
775                     if (things.lNetworkEvents & FD_CLOSE)
776                         connopen &= select_result(wp, (LPARAM) FD_CLOSE);
777                     if (things.lNetworkEvents & FD_OOB)
778                         connopen &= select_result(wp, (LPARAM) FD_OOB);
779                     if (things.lNetworkEvents & FD_WRITE)
780                         connopen &= select_result(wp, (LPARAM) FD_WRITE);
781                     if (things.lNetworkEvents & FD_ACCEPT)
782                         connopen &= select_result(wp, (LPARAM) FD_ACCEPT);
783
784                 }
785             }
786         } else if (n == 1) {
787             reading = 0;
788             noise_ultralight(idata.len);
789             if (idata.len > 0) {
790                 back->send(idata.buffer, idata.len);
791             } else {
792                 back->special(TS_EOF);
793             }
794         } else if (n == 2) {
795             odata.busy = 0;
796             if (!odata.writeret) {
797                 fprintf(stderr, "Unable to write to standard output\n");
798                 exit(0);
799             }
800             bufchain_consume(&stdout_data, odata.lenwritten);
801             if (bufchain_size(&stdout_data) > 0)
802                 try_output(0);
803             back->unthrottle(bufchain_size(&stdout_data) +
804                              bufchain_size(&stderr_data));
805         } else if (n == 3) {
806             edata.busy = 0;
807             if (!edata.writeret) {
808                 fprintf(stderr, "Unable to write to standard output\n");
809                 exit(0);
810             }
811             bufchain_consume(&stderr_data, edata.lenwritten);
812             if (bufchain_size(&stderr_data) > 0)
813                 try_output(1);
814             back->unthrottle(bufchain_size(&stdout_data) +
815                              bufchain_size(&stderr_data));
816         }
817         if (!reading && back->sendbuffer() < MAX_STDIN_BACKLOG) {
818             SetEvent(idata.eventback);
819             reading = 1;
820         }
821         if (!connopen || back->socket() == NULL)
822             break;                     /* we closed the connection */
823     }
824     WSACleanup();
825     return 0;
826 }