]>
Commit | Line | Data |
---|---|---|
f6fdbddf | 1 | /* |
2 | * Copyright 1995, 1996 by David Mazieres <dm@lcs.mit.edu>. | |
3 | * | |
4 | * Modification and redistribution in source and binary forms is | |
5 | * permitted provided that due credit is given to the author and the | |
6 | * OpenBSD project (for instance by leaving this copyright notice | |
7 | * intact). | |
8 | */ | |
9 | ||
10 | #include "includes.h" | |
2671b47f | 11 | RCSID("$OpenBSD: ssh-keyscan.c,v 1.18 2001/03/03 06:53:12 deraadt Exp $"); |
f6fdbddf | 12 | |
b705bea0 | 13 | #if defined(HAVE_SYS_QUEUE_H) && !defined(HAVE_BOGUS_SYS_QUEUE_H) |
f6fdbddf | 14 | #include <sys/queue.h> |
bf5f69f7 | 15 | #else |
bf3db92d | 16 | #include "fake-queue.h" |
bf5f69f7 | 17 | #endif |
f6fdbddf | 18 | #include <errno.h> |
19 | ||
20 | #include <openssl/bn.h> | |
f6fdbddf | 21 | |
22 | #include "xmalloc.h" | |
23 | #include "ssh.h" | |
42f11eb2 | 24 | #include "ssh1.h" |
f6fdbddf | 25 | #include "key.h" |
26 | #include "buffer.h" | |
27 | #include "bufaux.h" | |
42f11eb2 | 28 | #include "log.h" |
2671b47f | 29 | #include "atomicio.h" |
f6fdbddf | 30 | |
31 | static int argno = 1; /* Number of argument currently being parsed */ | |
32 | ||
33 | int family = AF_UNSPEC; /* IPv4, IPv6 or both */ | |
34 | ||
f6fdbddf | 35 | #define MAXMAXFD 256 |
36 | ||
37 | /* The number of seconds after which to give up on a TCP connection */ | |
38 | int timeout = 5; | |
39 | ||
40 | int maxfd; | |
2671b47f | 41 | #define MAXCON (maxfd - 10) |
f6fdbddf | 42 | |
1fe6a48f | 43 | #ifdef HAVE___PROGNAME |
44 | extern char *__progname; | |
45 | #else | |
46 | char *__progname; | |
47 | #endif | |
f6fdbddf | 48 | fd_set read_wait; |
49 | int ncon; | |
50 | ||
51 | /* | |
52 | * Keep a connection structure for each file descriptor. The state | |
53 | * associated with file descriptor n is held in fdcon[n]. | |
54 | */ | |
55 | typedef struct Connection { | |
1e3b8b07 | 56 | u_char c_status; /* State of connection on this file desc. */ |
f6fdbddf | 57 | #define CS_UNUSED 0 /* File descriptor unused */ |
58 | #define CS_CON 1 /* Waiting to connect/read greeting */ | |
59 | #define CS_SIZE 2 /* Waiting to read initial packet size */ | |
60 | #define CS_KEYS 3 /* Waiting to read public key packet */ | |
61 | int c_fd; /* Quick lookup: c->c_fd == c - fdcon */ | |
62 | int c_plen; /* Packet length field for ssh packet */ | |
63 | int c_len; /* Total bytes which must be read. */ | |
64 | int c_off; /* Length of data read so far. */ | |
65 | char *c_namebase; /* Address to free for c_name and c_namelist */ | |
66 | char *c_name; /* Hostname of connection for errors */ | |
67 | char *c_namelist; /* Pointer to other possible addresses */ | |
68 | char *c_output_name; /* Hostname of connection for output */ | |
69 | char *c_data; /* Data read from this fd */ | |
70 | struct timeval c_tv; /* Time at which connection gets aborted */ | |
71 | TAILQ_ENTRY(Connection) c_link; /* List of connections in timeout order. */ | |
72 | } con; | |
73 | ||
74 | TAILQ_HEAD(conlist, Connection) tq; /* Timeout Queue */ | |
75 | con *fdcon; | |
76 | ||
77 | /* | |
78 | * This is just a wrapper around fgets() to make it usable. | |
79 | */ | |
80 | ||
81 | /* Stress-test. Increase this later. */ | |
82 | #define LINEBUF_SIZE 16 | |
83 | ||
84 | typedef struct { | |
85 | char *buf; | |
1e3b8b07 | 86 | u_int size; |
f6fdbddf | 87 | int lineno; |
88 | const char *filename; | |
89 | FILE *stream; | |
90 | void (*errfun) (const char *,...); | |
91 | } Linebuf; | |
92 | ||
778f6940 | 93 | static __inline__ Linebuf * |
f6fdbddf | 94 | Linebuf_alloc(const char *filename, void (*errfun) (const char *,...)) |
95 | { | |
96 | Linebuf *lb; | |
97 | ||
98 | if (!(lb = malloc(sizeof(*lb)))) { | |
99 | if (errfun) | |
100 | (*errfun) ("linebuf (%s): malloc failed\n", lb->filename); | |
101 | return (NULL); | |
102 | } | |
103 | if (filename) { | |
104 | lb->filename = filename; | |
105 | if (!(lb->stream = fopen(filename, "r"))) { | |
b5c334cc | 106 | xfree(lb); |
f6fdbddf | 107 | if (errfun) |
108 | (*errfun) ("%s: %s\n", filename, strerror(errno)); | |
109 | return (NULL); | |
110 | } | |
111 | } else { | |
112 | lb->filename = "(stdin)"; | |
113 | lb->stream = stdin; | |
114 | } | |
115 | ||
116 | if (!(lb->buf = malloc(lb->size = LINEBUF_SIZE))) { | |
117 | if (errfun) | |
118 | (*errfun) ("linebuf (%s): malloc failed\n", lb->filename); | |
b5c334cc | 119 | xfree(lb); |
f6fdbddf | 120 | return (NULL); |
121 | } | |
122 | lb->errfun = errfun; | |
123 | lb->lineno = 0; | |
124 | return (lb); | |
125 | } | |
126 | ||
778f6940 | 127 | static __inline__ void |
f6fdbddf | 128 | Linebuf_free(Linebuf * lb) |
129 | { | |
130 | fclose(lb->stream); | |
b5c334cc | 131 | xfree(lb->buf); |
132 | xfree(lb); | |
f6fdbddf | 133 | } |
134 | ||
778f6940 | 135 | static __inline__ void |
f6fdbddf | 136 | Linebuf_restart(Linebuf * lb) |
137 | { | |
138 | clearerr(lb->stream); | |
139 | rewind(lb->stream); | |
140 | lb->lineno = 0; | |
141 | } | |
142 | ||
778f6940 | 143 | static __inline__ int |
f6fdbddf | 144 | Linebuf_lineno(Linebuf * lb) |
145 | { | |
146 | return (lb->lineno); | |
147 | } | |
148 | ||
778f6940 | 149 | static __inline__ char * |
5be2ec5e | 150 | Linebuf_getline(Linebuf * lb) |
f6fdbddf | 151 | { |
152 | int n = 0; | |
153 | ||
154 | lb->lineno++; | |
155 | for (;;) { | |
156 | /* Read a line */ | |
157 | if (!fgets(&lb->buf[n], lb->size - n, lb->stream)) { | |
158 | if (ferror(lb->stream) && lb->errfun) | |
778f6940 | 159 | (*lb->errfun) ("%s: %s\n", lb->filename, |
160 | strerror(errno)); | |
f6fdbddf | 161 | return (NULL); |
162 | } | |
163 | n = strlen(lb->buf); | |
164 | ||
165 | /* Return it or an error if it fits */ | |
166 | if (n > 0 && lb->buf[n - 1] == '\n') { | |
167 | lb->buf[n - 1] = '\0'; | |
168 | return (lb->buf); | |
169 | } | |
170 | if (n != lb->size - 1) { | |
171 | if (lb->errfun) | |
778f6940 | 172 | (*lb->errfun) ("%s: skipping incomplete last line\n", |
173 | lb->filename); | |
f6fdbddf | 174 | return (NULL); |
175 | } | |
176 | /* Double the buffer if we need more space */ | |
177 | if (!(lb->buf = realloc(lb->buf, (lb->size *= 2)))) { | |
178 | if (lb->errfun) | |
778f6940 | 179 | (*lb->errfun) ("linebuf (%s): realloc failed\n", |
180 | lb->filename); | |
f6fdbddf | 181 | return (NULL); |
182 | } | |
183 | } | |
184 | } | |
185 | ||
186 | static int | |
187 | fdlim_get(int hard) | |
188 | { | |
f318b98b | 189 | #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) |
f6fdbddf | 190 | struct rlimit rlfd; |
778f6940 | 191 | |
f6fdbddf | 192 | if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0) |
193 | return (-1); | |
194 | if ((hard ? rlfd.rlim_max : rlfd.rlim_cur) == RLIM_INFINITY) | |
195 | return 10000; | |
196 | else | |
197 | return hard ? rlfd.rlim_max : rlfd.rlim_cur; | |
b03bd394 | 198 | #elif defined (HAVE_SYSCONF) |
199 | return sysconf (_SC_OPEN_MAX); | |
200 | #else | |
201 | return 10000; | |
202 | #endif | |
f6fdbddf | 203 | } |
204 | ||
205 | static int | |
206 | fdlim_set(int lim) | |
207 | { | |
f318b98b | 208 | #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE) |
f6fdbddf | 209 | struct rlimit rlfd; |
b03bd394 | 210 | #endif |
f6fdbddf | 211 | if (lim <= 0) |
212 | return (-1); | |
f318b98b | 213 | #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_NOFILE) |
f6fdbddf | 214 | if (getrlimit(RLIMIT_NOFILE, &rlfd) < 0) |
215 | return (-1); | |
216 | rlfd.rlim_cur = lim; | |
217 | if (setrlimit(RLIMIT_NOFILE, &rlfd) < 0) | |
218 | return (-1); | |
b03bd394 | 219 | #elif defined (HAVE_SETDTABLESIZE) |
b705bea0 | 220 | setdtablesize(lim); |
b03bd394 | 221 | #endif |
f6fdbddf | 222 | return (0); |
223 | } | |
224 | ||
225 | /* | |
226 | * This is an strsep function that returns a null field for adjacent | |
227 | * separators. This is the same as the 4.4BSD strsep, but different from the | |
228 | * one in the GNU libc. | |
229 | */ | |
778f6940 | 230 | static __inline__ char * |
f6fdbddf | 231 | xstrsep(char **str, const char *delim) |
232 | { | |
233 | char *s, *e; | |
234 | ||
235 | if (!**str) | |
236 | return (NULL); | |
237 | ||
238 | s = *str; | |
239 | e = s + strcspn(s, delim); | |
240 | ||
241 | if (*e != '\0') | |
242 | *e++ = '\0'; | |
243 | *str = e; | |
244 | ||
245 | return (s); | |
246 | } | |
247 | ||
248 | /* | |
249 | * Get the next non-null token (like GNU strsep). Strsep() will return a | |
250 | * null token for two adjacent separators, so we may have to loop. | |
251 | */ | |
252 | char * | |
253 | strnnsep(char **stringp, char *delim) | |
254 | { | |
255 | char *tok; | |
256 | ||
257 | do { | |
258 | tok = xstrsep(stringp, delim); | |
259 | } while (tok && *tok == '\0'); | |
260 | return (tok); | |
261 | } | |
262 | ||
263 | void | |
264 | keyprint(char *host, char *output_name, char *kd, int len) | |
265 | { | |
266 | static Key *rsa; | |
267 | static Buffer msg; | |
268 | ||
269 | if (rsa == NULL) { | |
270 | buffer_init(&msg); | |
271 | rsa = key_new(KEY_RSA1); | |
272 | } | |
273 | buffer_append(&msg, kd, len); | |
274 | buffer_consume(&msg, 8 - (len & 7)); /* padding */ | |
275 | if (buffer_get_char(&msg) != (int) SSH_SMSG_PUBLIC_KEY) { | |
276 | error("%s: invalid packet type", host); | |
277 | buffer_clear(&msg); | |
278 | return; | |
279 | } | |
280 | buffer_consume(&msg, 8); /* cookie */ | |
281 | ||
282 | /* server key */ | |
283 | (void) buffer_get_int(&msg); | |
284 | buffer_get_bignum(&msg, rsa->rsa->e); | |
285 | buffer_get_bignum(&msg, rsa->rsa->n); | |
286 | ||
287 | /* host key */ | |
288 | (void) buffer_get_int(&msg); | |
289 | buffer_get_bignum(&msg, rsa->rsa->e); | |
290 | buffer_get_bignum(&msg, rsa->rsa->n); | |
291 | buffer_clear(&msg); | |
292 | ||
293 | fprintf(stdout, "%s ", output_name ? output_name : host); | |
294 | key_write(rsa, stdout); | |
295 | fputs("\n", stdout); | |
296 | } | |
297 | ||
298 | int | |
299 | tcpconnect(char *host) | |
300 | { | |
301 | struct addrinfo hints, *ai, *aitop; | |
302 | char strport[NI_MAXSERV]; | |
303 | int gaierr, s = -1; | |
304 | ||
b5c334cc | 305 | snprintf(strport, sizeof strport, "%d", SSH_DEFAULT_PORT); |
f6fdbddf | 306 | memset(&hints, 0, sizeof(hints)); |
307 | hints.ai_family = family; | |
308 | hints.ai_socktype = SOCK_STREAM; | |
309 | if ((gaierr = getaddrinfo(host, strport, &hints, &aitop)) != 0) | |
310 | fatal("getaddrinfo %s: %s", host, gai_strerror(gaierr)); | |
311 | for (ai = aitop; ai; ai = ai->ai_next) { | |
312 | s = socket(ai->ai_family, SOCK_STREAM, 0); | |
313 | if (s < 0) { | |
314 | error("socket: %s", strerror(errno)); | |
315 | continue; | |
316 | } | |
eea39c02 | 317 | if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) |
f6fdbddf | 318 | fatal("F_SETFL: %s", strerror(errno)); |
319 | if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0 && | |
320 | errno != EINPROGRESS) | |
321 | error("connect (`%s'): %s", host, strerror(errno)); | |
322 | else | |
323 | break; | |
324 | close(s); | |
325 | s = -1; | |
326 | } | |
327 | freeaddrinfo(aitop); | |
328 | return s; | |
329 | } | |
330 | ||
331 | int | |
332 | conalloc(char *iname, char *oname) | |
333 | { | |
334 | int s; | |
335 | char *namebase, *name, *namelist; | |
336 | ||
337 | namebase = namelist = xstrdup(iname); | |
338 | ||
339 | do { | |
340 | name = xstrsep(&namelist, ","); | |
341 | if (!name) { | |
b5c334cc | 342 | xfree(namebase); |
f6fdbddf | 343 | return (-1); |
344 | } | |
345 | } while ((s = tcpconnect(name)) < 0); | |
346 | ||
347 | if (s >= maxfd) | |
227e8e86 | 348 | fatal("conalloc: fdno %d too high", s); |
f6fdbddf | 349 | if (fdcon[s].c_status) |
227e8e86 | 350 | fatal("conalloc: attempt to reuse fdno %d", s); |
f6fdbddf | 351 | |
352 | fdcon[s].c_fd = s; | |
353 | fdcon[s].c_status = CS_CON; | |
354 | fdcon[s].c_namebase = namebase; | |
355 | fdcon[s].c_name = name; | |
356 | fdcon[s].c_namelist = namelist; | |
357 | fdcon[s].c_output_name = xstrdup(oname); | |
358 | fdcon[s].c_data = (char *) &fdcon[s].c_plen; | |
359 | fdcon[s].c_len = 4; | |
360 | fdcon[s].c_off = 0; | |
361 | gettimeofday(&fdcon[s].c_tv, NULL); | |
362 | fdcon[s].c_tv.tv_sec += timeout; | |
363 | TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); | |
364 | FD_SET(s, &read_wait); | |
365 | ncon++; | |
366 | return (s); | |
367 | } | |
368 | ||
369 | void | |
370 | confree(int s) | |
371 | { | |
f6fdbddf | 372 | if (s >= maxfd || fdcon[s].c_status == CS_UNUSED) |
227e8e86 | 373 | fatal("confree: attempt to free bad fdno %d", s); |
2671b47f | 374 | close(s); |
b5c334cc | 375 | xfree(fdcon[s].c_namebase); |
376 | xfree(fdcon[s].c_output_name); | |
f6fdbddf | 377 | if (fdcon[s].c_status == CS_KEYS) |
b5c334cc | 378 | xfree(fdcon[s].c_data); |
f6fdbddf | 379 | fdcon[s].c_status = CS_UNUSED; |
380 | TAILQ_REMOVE(&tq, &fdcon[s], c_link); | |
381 | FD_CLR(s, &read_wait); | |
382 | ncon--; | |
383 | } | |
384 | ||
385 | void | |
386 | contouch(int s) | |
387 | { | |
388 | TAILQ_REMOVE(&tq, &fdcon[s], c_link); | |
389 | gettimeofday(&fdcon[s].c_tv, NULL); | |
390 | fdcon[s].c_tv.tv_sec += timeout; | |
391 | TAILQ_INSERT_TAIL(&tq, &fdcon[s], c_link); | |
392 | } | |
393 | ||
394 | int | |
395 | conrecycle(int s) | |
396 | { | |
397 | int ret; | |
398 | con *c = &fdcon[s]; | |
399 | char *iname, *oname; | |
400 | ||
401 | iname = xstrdup(c->c_namelist); | |
b5c334cc | 402 | oname = xstrdup(c->c_output_name); |
f6fdbddf | 403 | confree(s); |
404 | ret = conalloc(iname, oname); | |
b5c334cc | 405 | xfree(iname); |
406 | xfree(oname); | |
f6fdbddf | 407 | return (ret); |
408 | } | |
409 | ||
410 | void | |
411 | congreet(int s) | |
412 | { | |
413 | char buf[80]; | |
414 | int n; | |
415 | con *c = &fdcon[s]; | |
416 | ||
417 | n = read(s, buf, sizeof(buf)); | |
418 | if (n < 0) { | |
419 | if (errno != ECONNREFUSED) | |
420 | error("read (%s): %s", c->c_name, strerror(errno)); | |
421 | conrecycle(s); | |
422 | return; | |
423 | } | |
424 | if (buf[n - 1] != '\n') { | |
425 | error("%s: bad greeting", c->c_name); | |
426 | confree(s); | |
427 | return; | |
428 | } | |
429 | buf[n - 1] = '\0'; | |
430 | fprintf(stderr, "# %s %s\n", c->c_name, buf); | |
431 | n = snprintf(buf, sizeof buf, "SSH-1.5-OpenSSH-keyscan\r\n"); | |
2671b47f | 432 | if (atomicio(write, s, buf, n) != n) { |
f6fdbddf | 433 | error("write (%s): %s", c->c_name, strerror(errno)); |
434 | confree(s); | |
435 | return; | |
436 | } | |
437 | c->c_status = CS_SIZE; | |
438 | contouch(s); | |
439 | } | |
440 | ||
441 | void | |
442 | conread(int s) | |
443 | { | |
444 | int n; | |
445 | con *c = &fdcon[s]; | |
446 | ||
447 | if (c->c_status == CS_CON) { | |
448 | congreet(s); | |
449 | return; | |
450 | } | |
451 | n = read(s, c->c_data + c->c_off, c->c_len - c->c_off); | |
452 | if (n < 0) { | |
453 | error("read (%s): %s", c->c_name, strerror(errno)); | |
454 | confree(s); | |
455 | return; | |
456 | } | |
457 | c->c_off += n; | |
458 | ||
459 | if (c->c_off == c->c_len) | |
460 | switch (c->c_status) { | |
461 | case CS_SIZE: | |
462 | c->c_plen = htonl(c->c_plen); | |
463 | c->c_len = c->c_plen + 8 - (c->c_plen & 7); | |
464 | c->c_off = 0; | |
465 | c->c_data = xmalloc(c->c_len); | |
466 | c->c_status = CS_KEYS; | |
467 | break; | |
468 | case CS_KEYS: | |
469 | keyprint(c->c_name, c->c_output_name, c->c_data, c->c_plen); | |
470 | confree(s); | |
471 | return; | |
472 | break; | |
473 | default: | |
227e8e86 | 474 | fatal("conread: invalid status %d", c->c_status); |
f6fdbddf | 475 | break; |
476 | } | |
477 | ||
478 | contouch(s); | |
479 | } | |
480 | ||
481 | void | |
482 | conloop(void) | |
483 | { | |
484 | fd_set r, e; | |
485 | struct timeval seltime, now; | |
486 | int i; | |
487 | con *c; | |
488 | ||
489 | gettimeofday(&now, NULL); | |
490 | c = tq.tqh_first; | |
491 | ||
2671b47f | 492 | if (c && (c->c_tv.tv_sec > now.tv_sec || |
493 | (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec > now.tv_usec))) { | |
f6fdbddf | 494 | seltime = c->c_tv; |
495 | seltime.tv_sec -= now.tv_sec; | |
496 | seltime.tv_usec -= now.tv_usec; | |
5be2ec5e | 497 | if (seltime.tv_usec < 0) { |
f6fdbddf | 498 | seltime.tv_usec += 1000000; |
499 | seltime.tv_sec--; | |
500 | } | |
501 | } else | |
502 | seltime.tv_sec = seltime.tv_usec = 0; | |
503 | ||
504 | r = e = read_wait; | |
fd193ca4 | 505 | while (select(maxfd, &r, NULL, &e, &seltime) == -1 && |
506 | (errno == EAGAIN || errno == EINTR)) | |
507 | ; | |
508 | ||
2671b47f | 509 | for (i = 0; i < maxfd; i++) { |
f6fdbddf | 510 | if (FD_ISSET(i, &e)) { |
511 | error("%s: exception!", fdcon[i].c_name); | |
512 | confree(i); | |
513 | } else if (FD_ISSET(i, &r)) | |
514 | conread(i); | |
2671b47f | 515 | } |
f6fdbddf | 516 | |
517 | c = tq.tqh_first; | |
2671b47f | 518 | while (c && (c->c_tv.tv_sec < now.tv_sec || |
519 | (c->c_tv.tv_sec == now.tv_sec && c->c_tv.tv_usec < now.tv_usec))) { | |
f6fdbddf | 520 | int s = c->c_fd; |
2671b47f | 521 | |
f6fdbddf | 522 | c = c->c_link.tqe_next; |
523 | conrecycle(s); | |
524 | } | |
525 | } | |
526 | ||
527 | char * | |
528 | nexthost(int argc, char **argv) | |
529 | { | |
530 | static Linebuf *lb; | |
531 | ||
532 | for (;;) { | |
533 | if (!lb) { | |
534 | if (argno >= argc) | |
535 | return (NULL); | |
536 | if (argv[argno][0] != '-') | |
537 | return (argv[argno++]); | |
538 | if (!strcmp(argv[argno], "--")) { | |
539 | if (++argno >= argc) | |
540 | return (NULL); | |
541 | return (argv[argno++]); | |
542 | } else if (!strncmp(argv[argno], "-f", 2)) { | |
543 | char *fname; | |
2671b47f | 544 | |
f6fdbddf | 545 | if (argv[argno][2]) |
546 | fname = &argv[argno++][2]; | |
547 | else if (++argno >= argc) { | |
548 | error("missing filename for `-f'"); | |
549 | return (NULL); | |
550 | } else | |
551 | fname = argv[argno++]; | |
552 | if (!strcmp(fname, "-")) | |
553 | fname = NULL; | |
f20255cb | 554 | lb = Linebuf_alloc(fname, error); |
f6fdbddf | 555 | } else |
2671b47f | 556 | error("ignoring invalid/misplaced option `%s'", |
557 | argv[argno++]); | |
f6fdbddf | 558 | } else { |
559 | char *line; | |
2671b47f | 560 | |
5be2ec5e | 561 | line = Linebuf_getline(lb); |
f6fdbddf | 562 | if (line) |
563 | return (line); | |
564 | Linebuf_free(lb); | |
565 | lb = NULL; | |
566 | } | |
567 | } | |
568 | } | |
569 | ||
570 | static void | |
571 | usage(void) | |
572 | { | |
227e8e86 | 573 | fatal("usage: %s [-t timeout] { [--] host | -f file } ...", __progname); |
f6fdbddf | 574 | return; |
575 | } | |
576 | ||
577 | int | |
578 | main(int argc, char **argv) | |
579 | { | |
580 | char *host = NULL; | |
581 | ||
1fe6a48f | 582 | __progname = get_progname(argv[0]); |
f6fdbddf | 583 | TAILQ_INIT(&tq); |
584 | ||
f6fdbddf | 585 | if (argc <= argno) |
586 | usage(); | |
587 | ||
588 | if (argv[1][0] == '-' && argv[1][1] == 't') { | |
589 | argno++; | |
590 | if (argv[1][2]) | |
591 | timeout = atoi(&argv[1][2]); | |
592 | else { | |
593 | if (argno >= argc) | |
594 | usage(); | |
595 | timeout = atoi(argv[argno++]); | |
596 | } | |
597 | if (timeout <= 0) | |
598 | usage(); | |
599 | } | |
600 | if (argc <= argno) | |
601 | usage(); | |
602 | ||
603 | maxfd = fdlim_get(1); | |
604 | if (maxfd < 0) | |
227e8e86 | 605 | fatal("%s: fdlim_get: bad value", __progname); |
f6fdbddf | 606 | if (maxfd > MAXMAXFD) |
607 | maxfd = MAXMAXFD; | |
2671b47f | 608 | if (MAXCON <= 0) |
227e8e86 | 609 | fatal("%s: not enough file descriptors", __progname); |
f6fdbddf | 610 | if (maxfd > fdlim_get(0)) |
611 | fdlim_set(maxfd); | |
612 | fdcon = xmalloc(maxfd * sizeof(con)); | |
5be2ec5e | 613 | memset(fdcon, 0, maxfd * sizeof(con)); |
f6fdbddf | 614 | |
615 | do { | |
2671b47f | 616 | while (ncon < MAXCON) { |
f6fdbddf | 617 | char *name; |
618 | ||
619 | host = nexthost(argc, argv); | |
620 | if (host == NULL) | |
621 | break; | |
622 | name = strnnsep(&host, " \t\n"); | |
623 | conalloc(name, *host ? host : name); | |
624 | } | |
625 | conloop(); | |
626 | } while (host); | |
627 | while (ncon > 0) | |
628 | conloop(); | |
629 | ||
630 | return (0); | |
631 | } |