-
Notifications
You must be signed in to change notification settings - Fork 6
/
strobe.c
1537 lines (1450 loc) · 30.8 KB
/
strobe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Strobe (c) 1995-1997 Julian Assange ([email protected]),
* All rights reserved.
*
* $ cc strobe.c -o strobe
*/
#define VERSION "1.05"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#ifdef _AIX
# include <sys/select.h>
#endif
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#if defined(solaris) || defined(linux) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__GCC__) || defined(__GNUC__)
# define fvoid void
#else
# define fvoid
extern int optind;
extern char *optarg;
#endif
#define bool int
#define FALSE 0
#define TRUE 1
#ifndef INADDR_NONE
# define INADDR_NONE ((unsigned long)-1)
#endif
#define port_t (unsigned short)
/*
* the below should be set via the Makefile, but if not...
*/
#ifndef ETC_SERVICES
# define ETC_SERVICES "/etc/services"
#endif
#ifndef STROBE_SERVICES
# define STROBE_SERVICES "strobe.services"
#endif
#ifndef LIB_STROBE_SERVICES
# define LIB_STROBE_SERVICES "/usr/local/lib/strobe.services"
#endif
int a_timeout = 20;
int a_data_timeout = 30;
char *a_output = NULL;
char *a_services = "strobe.services";
char *a_input = NULL;
/* char *a_prescan = NULL; */
int a_start = 1;
int a_end = 65535;
int a_sock_max = 64;
int a_abort = 0;
int a_bindport = 0;
int a_capture = 1024;
int a_wrap = 79;
int a_lines = 1;
char *a_bindaddr = NULL;
char *a_dircap = NULL;
char *a_views = "all,hosts,networks,ports";
struct in_addr bindaddr;
bool f_linear = 0;
bool f_verbose = 0;
bool f_verbose_stats = 0;
bool f_fast = 0;
bool f_stats = 0;
bool f_quiet = 0;
bool f_delete_dupes = 0;
bool f_minimise = 0;
bool f_dontgetpeername = 0;
bool f_hexdump = 0;
int connects = 0;
int hosts_done = 0;
int attempts_done = 0;
int attempts_outstanding = 0;
struct timeval time_start;
fd_set set_sel_check_r;
fd_set set_sel_check_w;
fd_set set_sel_r;
fd_set set_sel_w;
int host_n;
int Argc;
char **Argv;
FILE *fh_input;
char *capture_buf;
#define HO_ACTIVE 1
#define HO_ABORT 2
#define HO_COMPLETING 4
struct hosts_s
{
char *name;
struct in_addr in_addr;
int port;
int *portlist;
int portlist_n,portlist_alloc;
int portlist_ent;
struct timeval time_used;
struct timeval time_start;
int attempts;
int attempts_done;
int attempts_highest_done;
int connects;
time_t notice_abort;
int status;
};
struct hosts_s ho_initial; /* the null template */
struct hosts_s *hosts;
#define HT_SOCKET 1
#define HT_CONNECTING 2
#define HT_CONNECTED 4
struct htuple_s
{
char *name;
struct in_addr in_addr;
int port;
int sfd;
int status;
struct timeval sock_start;
int timeout;
struct hosts_s *host;
int data_lines;
int data_len;
u_char *data;
int send_pos;
int send_len;
u_char *send;
};
struct htuple_s ht_initial;
struct htuple_s *attempt;
struct port_desc_s
{
int port;
char *name;
char *portname;
struct port_desc_s *next;
struct port_desc_s *next_port;
};
struct port_desc_s **port_descs;
int *portlist = NULL;
int portlist_n = 0;
void display_port_sw (struct htuple_s *h);
char *
Srealloc (ptr, len)
char *ptr;
int len;
{
char *p;
int retries = 10;
while (!(p = ptr? realloc (ptr, len): malloc(len)))
{
if (!--retries)
{
perror("malloc");
exit(1);
}
if (!f_quiet)
fprintf(stderr, "Smalloc: couldn't allocate %d bytes...sleeping\n", len);
sleep (2);
}
return p;
}
char *
Smalloc (len)
int len;
{
return Srealloc (NULL, len);
}
fvoid
sock_block (sfd)
int sfd;
{
int flags;
flags = (~O_NONBLOCK) & fcntl (sfd, F_GETFL);
fcntl (sfd, F_SETFL, flags);
}
fvoid
sock_unblock (sfd)
int sfd;
{
int flags;
flags = O_NONBLOCK | fcntl (sfd, F_GETFL);
fcntl (sfd, F_SETFL, flags);
}
int
timeval_subtract (result, x, y) /* why not floating point? */
struct timeval *result, *x, *y;
{
result->tv_usec = x->tv_usec - y->tv_usec;
result->tv_sec = x->tv_sec - y->tv_sec;
if (result->tv_usec<0)
{
result->tv_usec+=1000000;
result->tv_sec --;
}
/* Return 1 if result is negative. */
return result->tv_sec < 0;
}
fvoid
attempt_clear (h)
struct htuple_s *h;
{
if (h->status & HT_CONNECTED)
display_port_sw (h);
if (h->status & HT_SOCKET)
{
struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);
timeval_subtract(&tv2, &tv1, &(h->sock_start));
h->host->time_used.tv_sec+=tv2.tv_sec;
if ((h->host->time_used.tv_usec+=tv2.tv_usec) >= 1000000)
{
h->host->time_used.tv_usec -= 1000000;
h->host->time_used.tv_sec++;
}
attempts_done++;
h->host->attempts_done++;
if (h->port > h->host->attempts_highest_done)
h->host->attempts_highest_done=h->port;
sock_unblock (h->sfd);
/* shutdown (h->sfd, 2); */
close (h->sfd);
if ( (FD_ISSET(h->sfd, &set_sel_check_r)) || (FD_ISSET(h->sfd, &set_sel_check_w)) )
{
FD_CLR (h->sfd, &set_sel_check_r);
FD_CLR (h->sfd, &set_sel_check_w);
attempts_outstanding--;
}
}
if (h->data)
free(h->data);
*h = ht_initial;
}
fvoid
clear_all ()
{
int n;
for (n = 0; n < a_sock_max; n++)
attempt_clear (&attempt[n]);
}
fvoid
attempt_init ()
{
int n;
for (n = 0; n < a_sock_max; n++)
attempt[n] = ht_initial;
}
fvoid
hosts_init ()
{
int n;
for (n = 0; n < a_sock_max; n++)
hosts[n] = ho_initial;
}
fvoid
fdsets_init ()
{
FD_ZERO(&set_sel_r); /* yes, we have to do this, despite the later */
FD_ZERO(&set_sel_w); /* assisgnments */
FD_ZERO(&set_sel_check_r);
FD_ZERO(&set_sel_check_w);
}
int
sc_connect (h)
struct htuple_s *h;
{
struct sockaddr_in sa_in;
int sopts1 = 1;
struct linger slinger;
if ((h->sfd = socket (PF_INET, SOCK_STREAM, 0)) == -1)
return 0;
memset(&sa_in, 0, sizeof(sa_in));
h->status |= HT_SOCKET;
gettimeofday(&(h->sock_start), NULL);
sock_unblock (h->sfd);
setsockopt (h->sfd, SOL_SOCKET, SO_REUSEADDR, (char *) &sopts1, sizeof (sopts1));
setsockopt (h->sfd, SOL_SOCKET, SO_OOBINLINE, (char *) &sopts1, sizeof (sopts1));
slinger.l_onoff = 0; /* off */
setsockopt (h->sfd, SOL_SOCKET, SO_LINGER, (char *) &slinger, sizeof (slinger));
sa_in.sin_family = AF_INET;
if (a_bindport)
sa_in.sin_port = a_bindport;
if (a_bindaddr)
sa_in.sin_addr = bindaddr;
if (a_bindaddr || a_bindport)
if (bind (h->sfd, (struct sockaddr *)&sa_in, sizeof(sa_in)) == -1)
{
fprintf(stderr, "couldn't bind %s : %d ", a_bindaddr? a_bindaddr: "0.0.0.0", ntohs(a_bindport));
perror("");
if (errno == EACCES)
exit(1);
return 0;
}
sa_in.sin_addr = h->in_addr;
sa_in.sin_port = htons (h->port);
h->host->attempts++;
if (connect (h->sfd, (struct sockaddr *) &sa_in, sizeof (sa_in)) == -1)
{
switch (errno)
{
case EINPROGRESS:
case EWOULDBLOCK:
break;
case ETIMEDOUT:
case ECONNREFUSED:
case EADDRNOTAVAIL:
if (f_verbose)
{
fprintf(stderr, "%s:%d ", h->name, h->port);
perror("");
}
attempt_clear (h);
return 0;
default:
if (!f_quiet)
{
fprintf(stderr, "%s:%d ", h->name, h->port);
perror ("");
}
attempt_clear (h);
return 0;
}
}
h->status |= HT_CONNECTING;
sock_block (h->sfd);
FD_SET(h->sfd, &set_sel_check_r);
FD_SET(h->sfd, &set_sel_check_w);
attempts_outstanding++;
return 1;
}
void
gen_port_simple (h, buf)
struct htuple_s *h;
char *buf;
{
sprintf (buf, "%.128s %5d", h->name, h->port);
}
int
display_port_simple (h, fh)
struct htuple_s *h;
FILE *fh;
{
char buf[256];
gen_port_simple(h, buf);
fputs(buf, fh);
return strlen(buf);
}
void
display_port (h, fh)
struct htuple_s *h;
FILE *fh;
{
int ds_len;
ds_len = display_port_simple(h, fh);
if (f_minimise)
fputc('\n', fh);
else
{
struct port_desc_s *pd;
if ((pd = port_descs[h->port]))
{
fprintf (fh, " %-12s %s\n", pd->portname, pd->name);
while (!f_delete_dupes && (pd=pd->next))
fprintf (fh, "%*s %-12s %s\n", ds_len, "", pd->portname, pd->name);
}
else
fprintf (fh, " %-12s %s\n", "unassigned", "unknown");
}
}
u_char *
conv_char(c, c2)
u_char c;
u_char c2;
{
static u_char b[8];
bzero(b, sizeof b);
if (c=='\\')
{
b[0] = c;
b[1] = c;
} else
if (c>=32 && c<127)
{
b[0] = c;
} else
if (c == '\r')
{
b[0] = '\\';
b[1] = 'r';
} else
if (c == '\n')
{
b[0] = '\\';
b[1] = 'n';
} else
if (c == '\t')
{
b[0] = '\\';
b[1] = 't';
} else
if (c == 8)
{
b[0] = '\\';
b[1] = 'b';
} else
if (c<32 || c >= 127)
{
/* remove possible ambiguity \n or \nn then digit */
if (c<100 && isdigit(c2))
sprintf (b, "\\%03d", (int)c);
else
sprintf (b, "\\%d", (int)c);
} else
{
fprintf (stderr, "internal error in print_char");
}
return b;
}
void
asciidump (h, fh)
struct htuple_s *h;
FILE *fh;
{
int n;
int col = 0;
char buf[256];
int buf_len;
char *p=h->data;
gen_port_simple(h, buf);
buf_len = strlen(buf);
for (n=0; n<h->data_len; n++) {
if (col == 0)
{
if (!a_dircap)
{
if (f_minimise)
{
fputs(buf, fh);
col = buf_len;
}
else
{
fprintf(fh, "%*s->", buf_len+1, "");
}
fputc(' ', fh);
}
}
fputs(conv_char(p[n], (n==h->data_len-1)? '\0': p[n+1]), fh);
col++;
if (a_wrap)
{
if (col >= a_wrap)
{
fputc('\n', fh);
col = 0;
}
else
{
switch (p[n])
{
case '\0':
case '\n':
fputc('\n', fh);
col = 0;
}
}
}
}
if (col !=0)
fputc('\n', fh);
}
void
hexdump(h, fh)
struct htuple_s *h;
FILE *fh;
{
int n;
char buf[256];
int buf_len;
gen_port_simple(h, buf);
buf_len = strlen(buf);
for (n = 0; n < h->data_len; n+=12)
{
int y;
if (!a_dircap)
{
if (f_minimise)
{
fputs(buf, fh);
}
else
{
fprintf(fh, "%*s->", buf_len+1, "");
}
fputc(' ', fh);
}
for (y=0; y<12; y++)
{
if (y+n< h->data_len)
fprintf(fh, "%02X ", h->data[n+y]);
else
fputs(" ", fh);
if (y%4==3 && y!=11)
fputc(' ', fh);
}
fputs(": ", fh);
for (y=0; y<12; y++)
{
if (y+n< h->data_len)
fprintf(fh, "%c", isprint(h->data[n+y])? h->data[n+y]: '.');
else
fputc(' ', fh);
}
fputc('\n', fh);
}
fputc('\n', fh);
}
char *
trslash (s)
char *s;
{
char *p=s;
for (;*s; s++)
if (*s == '/')
*s = ',';
return p;
}
/*
* recursively build directory hierarchy, starting at leaf
*/
bool
blddir (name)
char *name;
{
char *p;
if ((p = strrchr (name, '/')) == NULL)
return FALSE;
*p = '\0';
if (mkdir (name, (mode_t) 0775) == -1)
if (!blddir (name))
{
*p = '/';
return FALSE;
}
if (mkdir (name, (mode_t) 0775) == -1)
{
if (errno != EEXIST)
{
fprintf(stderr, "error building directory hierarchy %s", name);
*p = '/';
return FALSE;
}
}
*p = '/';
return TRUE;
}
bool
makeln (char *from, char *to)
{
if (link(from, to) !=0)
{
blddir(to);
if (link(from, to) !=0)
{
perror(to);
return FALSE;
}
}
return TRUE;
}
void
display_port_sw (h)
struct htuple_s *h;
{
char master[1024];
FILE *fh = stdout;
if (a_dircap)
{
char buf[2048];
char in[64];
char *p;
struct port_desc_s *pd;
sprintf(master, "%.512s/strobe.%d", a_dircap, (int)getpid());
fh = fopen(master, "w");
if (!fh)
{
blddir(master);
fh = fopen(master, "w");
if (!fh)
{
perror(master);
return;
}
}
strcpy(in, inet_ntoa(h->host->in_addr));
for (p=in; *p; p++)
if (*p == '.')
*p = '/';
pd = port_descs[h->port];
if (strstr(a_views, "networks"))
{
if (f_minimise)
sprintf(buf, "%.512s/networks/%s/%s-%.256s/%05d", a_dircap, in, inet_ntoa(h->host->in_addr),
h->host->name, h->port);
else
sprintf(buf, "%.512s/networks/%s/%s-%.256s/%05d-%.200s-%.200s", a_dircap, in, inet_ntoa(h->host->in_addr),
h->host->name, h->port, pd? port_descs[h->port]->portname: "unknown", pd? trslash(port_descs[h->port]->name): "");
makeln(master, buf);
}
if (strstr(a_views, "hosts"))
{
if (f_minimise)
sprintf(buf, "%.512s/hosts/%s-%.256s/%05d", a_dircap, inet_ntoa(h->host->in_addr),
h->host->name, h->port);
else
sprintf(buf, "%.512s/hosts/%s-%.256s/%05d-%.200s-%.200s", a_dircap, inet_ntoa(h->host->in_addr),
h->host->name, h->port, pd? port_descs[h->port]->portname: "unknown", pd? trslash(port_descs[h->port]->name): "");
makeln(master, buf);
}
if (strstr(a_views, "all"))
{
if (f_minimise)
sprintf(buf, "%.512s/all/%.64s-%.200s-%05d", a_dircap, inet_ntoa(h->host->in_addr),
h->host->name, h->port);
else
sprintf(buf, "%.512s/all/%.64s-%.200s-%05d-%.200s-%.200s", a_dircap, inet_ntoa(h->host->in_addr),
h->host->name, h->port, pd? port_descs[h->port]->portname: "unknown", pd? trslash(port_descs[h->port]->name): "");
makeln(master, buf);
}
if (strstr(a_views, "ports"))
{
if (f_minimise)
sprintf(buf, "%.512s/ports/%05d/%s-%.200s", a_dircap, h->port, inet_ntoa(h->host->in_addr),
h->host->name);
else
sprintf(buf, "%.512s/ports/%05d-%.200s-%.200s/%.64s-%.200s", a_dircap, h->port,
pd? port_descs[h->port]->portname: "unknown", pd? trslash(port_descs[h->port]->name): "",
inet_ntoa(h->host->in_addr), h->host->name);
makeln(master, buf);
}
}
if (h->status&HT_CONNECTED)
{
if (!f_minimise && !a_dircap)
display_port(h, fh);
if (h->data_len>0)
{
if (f_hexdump)
hexdump(h, fh);
else
asciidump(h, fh);
}
}
if (a_dircap)
{
fclose(fh);
unlink(master);
}
if (!(h->status&HT_CONNECTED))
display_port(h,fh);
}
void
init_capture_tcp (h)
struct htuple_s *h;
{
h->status&=~HT_CONNECTING;
h->status|=HT_CONNECTED;
FD_CLR (h->sfd, &set_sel_check_w);
}
void
capture_tcp (h)
struct htuple_s *h;
{
int cc;
bool f_drop = 0;
cc = recv(h->sfd, capture_buf, a_capture, 0);
if (cc == 0)
{
attempt_clear (h);
return;
}
if (cc < 0)
{
switch (errno)
{
case EAGAIN:
case EINTR:
break;
default:
attempt_clear (h);
}
return;
}
if (h->data_len+cc>a_capture)
{
cc = a_capture - h->data_len;
f_drop = 1;
}
if (a_lines)
{
int n;
for (n=0; n<cc; n++)
if (capture_buf[n] == '\n' ||
capture_buf[n] == '\0')
h->data_lines++;
}
h->data_len+=cc;
if (h->data)
h->data = Srealloc(h->data, h->data_len);
else
h->data = Smalloc(h->data_len);
memcpy(h->data+h->data_len-cc, capture_buf, cc);
if (f_drop || h->data_lines >= a_lines)
attempt_clear (h);
}
int
gatherer_tcp (h)
struct htuple_s *h;
{
h->host->connects++;
connects++;
if (a_capture)
init_capture_tcp (h);
else
{
display_port_sw (h);
attempt_clear (h);
}
return 1;
}
int
gather (timeout_secs)
int timeout_secs;
{
struct timeval timeout;
struct htuple_s *h;
int n;
int last = -1;
int selected;
time_t tim;
if (!attempts_outstanding) return 1;
set_sel_r=set_sel_check_r;
set_sel_w=set_sel_check_w;
if (timeout_secs)
{
timeout.tv_sec = timeout_secs;
timeout.tv_usec = 0;
}
else
{
timeout.tv_sec = 0;
timeout.tv_usec = 250000; /* 1/4 of a second */
}
selected = select (FD_SETSIZE, &set_sel_r, &set_sel_w, 0, &timeout);
if (selected<0)
perror ("select");
tim = time (NULL);
for ( n = 0 ; n < a_sock_max; n++ )
{
h = &attempt[n];
if (h->status & HT_CONNECTED)
{
if (!FD_ISSET(h->sfd,&set_sel_r))
{
if ( (tim - h->sock_start.tv_sec) >= h->timeout)
{
attempt_clear(h);
}
continue;
}
}
if (selected>0 && h->status & (HT_CONNECTING|HT_CONNECTED))
{
if (FD_ISSET (h->sfd, &set_sel_r) ||
FD_ISSET (h->sfd, &set_sel_w))
{
struct sockaddr_in in;
int len = sizeof (in);
selected--;
/* select() lies occasionaly
*/
if (!f_dontgetpeername) /* but solaris2.3 crashes occasionally ;-| */
{
if (getpeername (h->sfd, (struct sockaddr *) &in, &len) == 0)
{
if ((h->status & HT_CONNECTED) && FD_ISSET (h->sfd, &set_sel_r))
capture_tcp (h);
else
gatherer_tcp (h);
}
else
attempt_clear (h);
}
else
{
if ((h->status & HT_CONNECTED) &&
FD_ISSET (h->sfd, &set_sel_r))
capture_tcp (h);
else
gatherer_tcp (h);
}
}
last = n;
} else
{
if ((h->status & HT_SOCKET) &&
((h->sock_start.tv_sec + h->timeout) < tim))
{
attempt_clear (h);
last = n;
}
}
}
return last;
}
bool
add_attempt (add)
struct htuple_s *add;
{
struct htuple_s *h;
static time_t oldtime;
int ret;
for (;;)
{
int n;
int last;
for (n=0; n < a_sock_max; n++)
{
h = &attempt[n];
if (!h->status)
goto foundfree;
}
last = gather (a_timeout);
oldtime = time(NULL);
if (last != -1) {
h = &attempt[last];
goto foundfree;
}
}
foundfree:
*h = *add;
ret = sc_connect (h);
if (oldtime+1<time(NULL)) {
if (oldtime > 0)
gather (0);
oldtime = time(NULL);
}
return ret;
}
int
scatter (host, timeout)
struct hosts_s *host;
int timeout;
{
static struct htuple_s add;
add = ht_initial;
add.host = host;
add.name = host->name;
add.in_addr = host->in_addr;
add.port = host->port;
add.timeout = timeout;
if (f_verbose)
fprintf (stderr, "attempting port=%d host=%s\n", add.port, add.name);
add_attempt (&add);
return 1;
}
fvoid
wait_end (t)
int t;
{
time_t st;
st = time (NULL);
while ((st + t) > time (NULL))
{
gather (a_timeout);
if (attempts_outstanding<1) break;
}
}
struct in_addr
resolve (name)
char *name;
{
static struct in_addr in;
unsigned long l;
struct hostent *ent;
if ((l = inet_addr (name)) != INADDR_NONE)
{
in.s_addr = l;
return in;
}
if (!(ent = gethostbyname (name)))
{
perror (name);
in.s_addr = INADDR_NONE;
return in;
}
return *(struct in_addr *) ent->h_addr;
}
char *
next_host ()
{
static char lbuf[512];
if (a_input)
{
int n;
reread:
if (!fgets (lbuf, sizeof (lbuf), fh_input))
{
fclose (fh_input);
a_input = NULL;
return next_host();
}
if (strchr("# \t\n\r", lbuf[0])) goto reread;
n = strcspn (lbuf, " \t\n\r");
if (n)
lbuf[n] = '\0';
hosts_done++;
return lbuf;
}
if ( host_n >= Argc )
return NULL;
hosts_done++;
return Argv[host_n++];
}
int
next_port (h)
struct hosts_s *h;
{