-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhappy.c
1384 lines (1197 loc) · 34.8 KB
/
happy.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
/*
* happy.c --
*
* Copyright (c) 2013, Juergen Schoenwaelder, Jacobs University Bremen
* Copyright (c) 2014, Vaibhav Bajpai, Jacobs University Bremen
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and
* documentation are those of the authors and should not be
* interpreted as representing official policies, either expressed or
* implied, of the Leone Project or Jacobs University Bremen.
*/
#define _POSIX_C_SOURCE 2
#define _DEFAULT_SOURCE
#define _DARWIN_C_SOURCE
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <assert.h>
#include <time.h>
#include <ctype.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
static const char *progname = "happy";
#ifndef NI_MAXHOST
#define NI_MAXHOST 1025
#endif
#ifndef NI_MAXSERV
#define NI_MAXSERV 32
#endif
#define EP_STATE_NEW 0x00
#define EP_STATE_CONNECTING 0x01
#define EP_STATE_CONNECTED 0x02
#define EP_STATE_TIMEDOUT 0x04
#define EP_STATE_FAILED 0x08
typedef struct endpoint {
int family;
int socktype;
int protocol;
struct sockaddr_storage addr;
socklen_t addrlen;
char *canonname;
char *reversename;
int socket;
struct timeval tvs;
int state;
unsigned int sum;
unsigned int tot;
unsigned int idx;
unsigned int cnt;
int *values;
unsigned int send;
unsigned int rcvd;
} endpoint_t;
typedef struct target {
char *host;
char *port;
int num_endpoints;
endpoint_t *endpoints;
struct target *next;
} target_t;
static target_t *targets = NULL;
static int dmode = 0;
static int pmode = 0;
static int cmode = 0;
static int smode = 0;
static int skmode = 0;
static int nqueries = 3;
static int timeout = 2000; /* in ms */
static unsigned int delay = 25; /* in ms */
static int pump_timeout = 2000; /* in ms */
static int target_valid(target_t *tp) {
return (tp && tp->host && tp->port);
}
static int endpoint_valid(endpoint_t *ep) {
return (ep && ep->addrlen);
}
/*
* A calloc() that exits if we run out of memory.
*/
static void*
xcalloc(size_t nmemb, size_t size)
{
void *p = calloc(nmemb, size);
if (!p) {
fprintf(stderr, "%s: memory allocation failure\n", progname);
exit(EXIT_FAILURE);
}
return p;
}
/*
* A realloc() that exits if we run out of memory.
*/
static void*
xrealloc(void *ptr, size_t size)
{
void *p = realloc(ptr, size);
if (!p) {
fprintf(stderr, "%s: memory allocation failure\n", progname);
exit(EXIT_FAILURE);
}
return p;
}
/*
* Trim trailing and leading white space from a string. Note, this
* version modifies the original string.
*/
static char *
trim(char * s)
{
char * p = s;
int l = strlen(p);
while(isspace(p[l-1]) && l) p[--l] = 0;
while(*p && isspace(*p) && l) ++p, --l;
return p;
}
/*
* If the file stream is associated with a regular file, lock the file
* in order coordinate writes to a common file from multiple happy
* instances. This is useful if, for example, multiple happy instances
* try to append results to a common file.
*/
static void
lock(FILE *f)
{
int fd;
struct stat buf;
static struct flock lock;
assert(f);
lock.l_type = F_WRLCK;
lock.l_start = 0;
lock.l_whence = SEEK_END;
lock.l_len = 0;
lock.l_pid = getpid();
fd = fileno(f);
if ((fstat(fd, &buf) == 0) && S_ISREG(buf.st_mode)) {
if (fcntl(fd, F_SETLKW, &lock) == -1) {
fprintf(stderr, "%s: fcntl: %s (ignored)\n",
progname, strerror(errno));
}
}
}
/*
* If the file stream is associated with a regular file, unlock the
* file (which presumably has previously been locked).
*/
static void
unlock(FILE *f)
{
int fd;
struct stat buf;
static struct flock lock;
assert(f);
lock.l_type = F_UNLCK;
lock.l_start = 0;
lock.l_whence = SEEK_END;
lock.l_len = 0;
lock.l_pid = getpid();
fd = fileno(f);
if ((fstat(fd, &buf) == 0) && S_ISREG(buf.st_mode)) {
if (fcntl(fd, F_SETLKW, &lock) == -1) {
fprintf(stderr, "%s: fcntl: %s (ignored)\n",
progname, strerror(errno));
}
}
}
/*
* Append a new target to our list of targets. We keep track of
* the last target added so that we do not have to search for the
* end of the list.
*/
static void
append(target_t *target)
{
static target_t *last_target = NULL;
if (target) {
if (! targets) {
targets = target;
} else {
last_target->next = target;
}
last_target = target;
}
}
/*
* Handler to parse DNS response messages; particularly CNAME
* answers. This is used because getaddrinfo(...) and AI_CANONNAME
* cannot be used with the glibc version on SamKnows probes. The
* version returns PTR entries when asked for a CNAME. Therefore
* explicit CNAME query request needs to be made and a handler
* function is needed to parse CNAME responses.
*/
static char*
parse_cname_response(const u_char* const answer, const int answerlen)
{
/* initialize data structure to store the parsed response */
ns_msg handle;
if (
ns_initparse(
answer, /* answer buffer */
answerlen, /* true answer length */
&handle /* data structure filled in by ns_initparse */
) < 0
) {
herror("ns_initparse(...)");
exit(EXIT_FAILURE);
}
/* Ideally one would iterate over each resource record returned by
* ns_msg_count(...). However, this is a specific parse function for CNAME
* records and because a RR cannot have any other records along with a
* CNAME [RFC 1912], there is no need to iterate in a loop, but only once,
* therefore rrnum is set to 0 */
ns_rr rr; int rrnum = 0;
/* parse the answer section of the resource record */
if (
ns_parserr(
&handle, /* data structure filled by ns_initparse */
ns_s_an, /* resource record answer section */
rrnum, /* resource record index in this section */
&rr /* data structure filled in by ns_parseerr */
) < 0
) {
/* continue to the next resource records if this cannot be parsed */
if (errno != ENODEV) {
herror("ns_parserr(...)");
exit(EXIT_FAILURE);
}
}
char* dst = NULL;
switch (ns_rr_type(rr)) {
/* type: CNAME record */
case ns_t_cname:
dst = (char *) xcalloc (1, NI_MAXHOST);
/* Uncompress the DNS string */
if (
ns_name_uncompress(
ns_msg_base(handle) /* Message Start */
, ns_msg_end(handle) /* Message End */
, ns_rr_rdata(rr) /* Message Position */
, dst /* Result */
, NI_MAXHOST /* Result Size */
) < 0
) {
(void) fprintf(stderr, "ns_name_uncompress failed\n");
free(dst); dst = NULL;
exit(EXIT_FAILURE);
}
break;
/* ignore all the other types */
default:
break;
}
return dst;
}
/*
* Resolve the host and port name and if successful establish a new
* target and create the vector of endpoints we are going to probe
* subsequently.
*/
static target_t*
expand(const char *host, const char *port)
{
struct addrinfo hints, *ai_list, *ai;
char* canonname = NULL;
int n;
target_t *tp;
endpoint_t *ep;
assert(host && port);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if (dmode) {
/* In order to get the CNAME, one can set the AI_CANONNAME
* flag in hints.ai_flags. However it appears glibc tends to
* return PTR entries when this flag is turned on (see:
* http://marc.info/?l=glibc-alpha&m=109239199429843&w=2). The
* newer versions of glibc have been patched, however the
* version running on SamKnows probes still has this
* behavior. As a workaround, we use BIND functions to
* explicitly send a CNAME query and parse the DNS response
* ourselves. The answer string is then plugged back into the
* result structures used below.
*/
/* hints.ai_flags |= AI_CANONNAME; */
/* list to keep a chain of CNAME strings */
short dstset_num = 0;
char** dstset = NULL;
const char* query_name = host;
while (1) {
/* send a DNS query for CNAME record of the input service name */
u_char answer[NS_PACKETSZ];
int answerlen = res_search (
query_name /* domain name */
, ns_c_in /* class type, see: arpa/nameserv.h */
, ns_t_cname /* rr type, see: arpa/nameserv.h */
, (u_char *) answer /* answer buffer */
, NS_PACKETSZ /* answer buffer length */
);
if (answerlen == -1) {
break;
} else {
/* parse the received response */
canonname = parse_cname_response (
answer /* received response */
, answerlen /* true response len */
);
}
/* list to keep a chain of CNAME strings */
if (canonname != NULL) {
if (dstset_num == 0) {
dstset = xcalloc(dstset_num + 1, sizeof(char*));
} else {
dstset = xrealloc(dstset, (dstset_num+1) * sizeof(char*));
}
dstset[dstset_num] = canonname;
dstset_num += 1;
query_name = canonname;
}
}
/* create a string representation of CNAME chains */
canonname = NULL;
for (int i = 0; i < dstset_num; i++){
char* dangler = canonname;
char* dst = dstset[i];
if (i == 0) {
asprintf(&canonname, "%s", dst);
free(dst);
continue;
}
if (i == 1) {
asprintf(&canonname, "%s >", dangler);
free(dangler); dangler = canonname;
}
if ((i + 1) == dstset_num) {
asprintf(&canonname, "%s %s",dangler,dst);
} else {
asprintf(&canonname, "%s %s >", dangler, dst);
}
if (dst != NULL) { free(dst); dst = NULL; }
if (dangler !=NULL) { free(dangler); dangler = NULL; }
}
free(dstset); dstset = NULL;
}
tp = xcalloc(1, sizeof(target_t));
tp->host = strdup(host);
tp->port = strdup(port);
n = getaddrinfo(host, port, &hints, &ai_list);
if (n != 0) {
fprintf(stderr, "%s: getaddrinfo: %s (skipping %s port %s)\n",
progname, gai_strerror(n), host, port);
return tp;
}
for (ai = ai_list, tp->num_endpoints = 0;
ai; ai = ai->ai_next, tp->num_endpoints++) ;
tp->endpoints = xcalloc(1 + tp->num_endpoints, sizeof(endpoint_t));
for (ai = ai_list, ep = tp->endpoints; ai; ai = ai->ai_next, ep++) {
ep->family = ai->ai_family;
ep->socktype = ai->ai_socktype;
ep->protocol = ai->ai_protocol;
memcpy(&ep->addr, ai->ai_addr, ai->ai_addrlen);
ep->addrlen = ai->ai_addrlen;
ep->values = xcalloc(nqueries, sizeof(unsigned int));
if (dmode) {
char revname[NI_MAXHOST];
int n;
revname[0] = 0;
if (canonname != NULL) {
ep->canonname = strdup(canonname);
} else {
ep->canonname = strdup(host);
}
n = getnameinfo(ai->ai_addr, ai->ai_addrlen,
revname, sizeof(revname), NULL, 0,
NI_NAMEREQD);
if (n && n != EAI_NONAME) {
fprintf(stderr, "%s: getnameinfo: %s\n",
progname, gai_strerror(n));
} else {
if (strlen(revname)) {
ep->reversename = strdup(revname);
}
}
}
}
freeaddrinfo(ai_list);
free(canonname); canonname = NULL;
return tp;
}
/*
* Generate the file descriptor set for all sockets with a pending
* asynchronous connect(). If the struct timeval argument is a valid
* pointer, leave the smallest timestamp of a socket with a pending
* asynchronous connect in the struct timeval.
*/
static int
generate_fdset(target_t *targets, fd_set *fdset, struct timeval *to)
{
int max;
target_t *tp;
endpoint_t *ep;
if (to) {
timerclear(to);
}
FD_ZERO(fdset);
for (tp = targets, max = -1; target_valid(tp); tp = tp->next) {
for (ep = tp->endpoints; endpoint_valid(ep); ep++) {
if (ep->state == EP_STATE_CONNECTING) {
FD_SET(ep->socket, fdset);
if (ep->socket > max) {
max = ep->socket;
}
if (to) {
if (! timerisset(to) || timercmp(&ep->tvs, to, <)) {
*to = ep->tvs;
}
}
}
}
}
return max;
}
/*
* Go through all endpoints and check which ones have timed out, for
* which ones the asynchronous connect() has finished and update the
* stats accordingly.
*/
static void
update(target_t *targets, fd_set *fdset)
{
struct timeval tv, td;
int soerror;
socklen_t soerrorlen = sizeof(soerror);
target_t *tp;
endpoint_t *ep;
unsigned int us;
assert(targets && fdset);
(void) gettimeofday(&tv, NULL);
for (tp = targets; target_valid(tp); tp = tp->next) {
for (ep = tp->endpoints; endpoint_valid(ep); ep++) {
/* calculate time since we started the connect */
timersub(&tv, &ep->tvs, &td);
us = td.tv_sec*1000000 + td.tv_usec;
if (ep->state == EP_STATE_CONNECTING && us >= timeout * 1000) {
ep->values[ep->idx] = -us;
ep->idx++;
ep->cnt++;
(void) close(ep->socket);
ep->socket = 0;
ep->state = EP_STATE_TIMEDOUT;
continue;
}
if (ep->state == EP_STATE_CONNECTING
&& FD_ISSET(ep->socket, fdset)) {
if (-1 == getsockopt(ep->socket, SOL_SOCKET, SO_ERROR,
&soerror, &soerrorlen)) {
fprintf(stderr, "%s: getsockopt: %s\n",
progname, strerror(errno));
exit(EXIT_FAILURE);
}
if (! soerror) {
ep->values[ep->idx] = us;
ep->sum += us;
ep->tot++;
ep->cnt++;
ep->idx++;
} else {
ep->values[ep->idx] = -us;
ep->cnt++;
ep->idx++;
}
if (! pmode) {
(void) close(ep->socket);
ep->socket = 0;
}
ep->state = EP_STATE_CONNECTED;
}
}
}
}
/*
* For all endpoints, create a socket and start a non-blocking
* connect(). In order to avoid creating bursts of TCP SYN packets,
* we take a short nap before each connect() call. Note that
* asynchronous connect()s might actually succeed during the
* short nap.
*/
static void
prepare(target_t *targets)
{
int rc, flags;
fd_set fdset;
target_t *tp;
endpoint_t *ep;
struct timeval dts, dtn, dtd, dd;
assert(targets);
dd.tv_sec = delay / 1000;
dd.tv_usec = (delay % 1000) * 1000;
for (tp = targets; target_valid(tp); tp = tp->next) {
for (ep = tp->endpoints; endpoint_valid(ep); ep++) {
if (delay) {
int max;
struct timeval to;
(void) gettimeofday(&dts, NULL);
while (1) {
max = generate_fdset(targets, &fdset, NULL);
(void) gettimeofday(&dtn, NULL);
timersub(&dtn, &dts, &dtd);
if (timercmp(&dd, &dtd, <)) {
break;
}
timeradd(&dts, &dd, &to);
timersub(&to, &dtn, &to);
rc = select(1 + max, NULL, &fdset, NULL, &to);
if (rc == -1) {
fprintf(stderr, "%s: select failed: %s\n",
progname, strerror(errno));
exit(EXIT_FAILURE);
}
update(targets, &fdset);
}
}
ep->socket = socket(ep->family, ep->socktype, ep->protocol);
if (ep->socket < 0) {
switch (errno) {
case EAFNOSUPPORT:
case EPROTONOSUPPORT:
continue;
default:
fprintf(stderr, "%s: socket: %s (skipping %s port %s)\n",
progname, strerror(errno), tp->host, tp->port);
ep->socket = 0;
ep->state = EP_STATE_FAILED;
continue;
}
}
flags = fcntl(ep->socket, F_GETFL, 0);
if (fcntl(ep->socket, F_SETFL, flags | O_NONBLOCK) == -1) {
fprintf(stderr, "%s: fcntl: %s (skipping %s port %s)\n",
progname, strerror(errno), tp->host, tp->port);
(void) close(ep->socket);
ep->socket = 0;
ep->state = EP_STATE_FAILED;
continue;
}
if (connect(ep->socket,
(struct sockaddr *) &ep->addr,
ep->addrlen) == -1) {
if (errno != EINPROGRESS) {
fprintf(stderr, "%s: connect: %s (skipping %s port %s)\n",
progname, strerror(errno), tp->host, tp->port);
(void) close(ep->socket);
ep->socket = 0;
ep->state = EP_STATE_FAILED;
continue;
}
}
ep->state = EP_STATE_CONNECTING;
(void) gettimeofday(&ep->tvs, NULL);
}
}
}
/*
* Wait in a select() loop for any pending connect() requests to
* complete. If the connect() was successful, collect basic timing
* statistics.
*/
static void
collect(target_t *targets)
{
int rc, max;
fd_set fdset;
struct timeval to, ts, tn;
assert(targets);
while (1) {
if (timeout) {
to.tv_sec = timeout / 1000;
to.tv_usec = (timeout % 1000) * 1000;
}
max = generate_fdset(targets, &fdset, timeout ? &ts : NULL);
if (max == -1) {
break;
}
if (timeout) {
(void) gettimeofday(&tn, NULL);
timeradd(&ts, &to, &to);
timersub(&to, &tn, &to);
}
rc = select(1 + max, NULL, &fdset, NULL, timeout ? &to : NULL);
if (rc == -1) {
fprintf(stderr, "%s: select failed: %s\n",
progname, strerror(errno));
exit(EXIT_FAILURE);
}
update(targets, &fdset);
}
}
/*
* Sort the results for each target. This is in particular useful for
* interactive usage.
*/
static int
cmp(const void *a, const void *b)
{
endpoint_t *pa = (endpoint_t *) a;
endpoint_t *pb = (endpoint_t *) b;
if (! pa->tot || ! pb->tot) {
return 0;
}
if (pa->sum/pa->tot < pb->sum/pb->tot) {
return -1;
} else if (pa->sum/pa->tot > pb->sum/pb->tot) {
return 1;
}
return 0;
}
static void
sort(target_t *targets)
{
target_t *tp;
endpoint_t *ep;
assert(targets);
for (tp = targets; target_valid(tp); tp = tp->next) {
if (tp->endpoints) {
qsort(tp->endpoints, tp->num_endpoints, sizeof(*ep), cmp);
}
}
}
/*
* Report the results. For each endpoint of a target, we show the time
* measured to establish a connection. This default format is intended
* primarily for human readers.
*/
static void
report(target_t *targets)
{
int i, n, len;
char host[NI_MAXHOST];
char serv[NI_MAXSERV];
target_t *tp;
endpoint_t *ep;
assert(targets);
for (tp = targets; target_valid(tp); tp = tp->next) {
printf("%s%s:%s\n",
(tp != targets) ? "\n" : "", tp->host, tp->port);
for (ep = tp->endpoints; endpoint_valid(ep); ep++) {
n = getnameinfo((struct sockaddr *) &ep->addr,
ep->addrlen,
host, sizeof(host), serv, sizeof(serv),
NI_NUMERICHOST | NI_NUMERICSERV);
if (n) {
fprintf(stderr, "%s: getnameinfo: %s\n",
progname, gai_strerror(n));
continue;
}
printf(" %s%n", host, &len);
printf("%*s", (42-len), "");
for (i = 0; i < ep->idx; i++) {
if (ep->values[i] >= 0) {
printf(" %4u.%03u",
ep->values[i] / 1000,
ep->values[i] % 1000);
} else {
printf(" * ");
}
}
printf("\n");
}
}
}
/*
* Report the pump results. For each endpoint of a target, we show the
* bytes/seconds send and received.
*/
static void
report_pump(target_t *targets)
{
int n, len;
char host[NI_MAXHOST];
char serv[NI_MAXSERV];
target_t *tp;
endpoint_t *ep;
assert(targets);
for (tp = targets; target_valid(tp); tp = tp->next) {
printf("%s%s:%s\n",
(tp != targets) ? "\n" : "", tp->host, tp->port);
for (ep = tp->endpoints; endpoint_valid(ep); ep++) {
n = getnameinfo((struct sockaddr *) &ep->addr,
ep->addrlen,
host, sizeof(host), serv, sizeof(serv),
NI_NUMERICHOST | NI_NUMERICSERV);
if (n) {
fprintf(stderr, "%s: getnameinfo: %s\n",
progname, gai_strerror(n));
continue;
}
printf(" %s%n", host, &len);
printf("%*s", (42-len), "");
printf(" %4u.%03u [sent]",
ep->send / pump_timeout * 1000 / 1000,
ep->send / pump_timeout * 1000 % 1000);
printf(" %4u.%03u [rcvd]",
ep->rcvd / pump_timeout * 1000 / 1000,
ep->rcvd / pump_timeout * 1000 % 1000);
printf("\n");
}
}
}
/*
* Report the dns results. For each endpoint of a target, we show the
* canonical name and the reverse name.
*/
static void
report_dns(target_t *targets)
{
int n, len;
char host[NI_MAXHOST];
char serv[NI_MAXSERV];
target_t *tp;
endpoint_t *ep;
assert(targets);
for (tp = targets; target_valid(tp); tp = tp->next) {
printf("%s%s:%s\n",
(tp != targets) ? "\n" : "", tp->host, tp->port);
for (ep = tp->endpoints; endpoint_valid(ep); ep++) {
n = getnameinfo((struct sockaddr *) &ep->addr,
ep->addrlen,
host, sizeof(host), serv, sizeof(serv),
NI_NUMERICHOST | NI_NUMERICSERV);
if (n) {
fprintf(stderr, "%s: getnameinfo: %s\n",
progname, gai_strerror(n));
continue;
}
printf(" %s > %s%n", ep->canonname, host, &len);
if (ep->reversename) {
printf(" > %s", ep->reversename);
}
printf("\n");
}
}
}
/*
* Report the results. This function produces a more compact semicolon
* separated output format intended for consumption by other programs.
*/
static void
report_sk(target_t *targets)
{
int i, n;
char host[NI_MAXHOST];
char serv[NI_MAXSERV];
target_t *tp;
endpoint_t *ep;
time_t now;
assert(targets);
now = time(NULL);
for (tp = targets; target_valid(tp); tp = tp->next) {
if (! tp->endpoints) {
printf("HAPPY.0.4;%lu;%s;%s;%s\n",
now, "FAIL", tp->host, tp->port);
}
for (ep = tp->endpoints; endpoint_valid(ep); ep++) {
n = getnameinfo((struct sockaddr *) &ep->addr,
ep->addrlen,
host, sizeof(host), serv, sizeof(serv),
NI_NUMERICHOST | NI_NUMERICSERV);
if (n) {
fprintf(stderr, "%s: getnameinfo: %s\n",
progname, gai_strerror(n));
continue;
}
printf("HAPPY.0.4;%lu;%s;%s;%s;%s",
now, ep->cnt ? "OK" : "FAIL", tp->host, tp->port, host);
for (i = 0; i < ep->idx; i++) {
printf(";%d", ep->values[i]);
}
printf("\n");
}
}
}
/*
* Report the pump results. This function produces a more compact
* semicolon separated output format intended for consumption by other
* programs.
*/
static void
report_pump_sk(target_t *targets)
{
int n;
char host[NI_MAXHOST];
char serv[NI_MAXSERV];
target_t *tp;
endpoint_t *ep;
time_t now;
assert(targets);
now = time(NULL);
for (tp = targets; target_valid(tp); tp = tp->next) {
if (! tp->endpoints) {
printf("PUMP.0.4;%lu;%s;%s;%s\n",
now, "FAIL", tp->host, tp->port);
}
for (ep = tp->endpoints; endpoint_valid(ep); ep++) {
n = getnameinfo((struct sockaddr *) &ep->addr,
ep->addrlen,
host, sizeof(host), serv, sizeof(serv),
NI_NUMERICHOST | NI_NUMERICSERV);
if (n) {
fprintf(stderr, "%s: getnameinfo: %s\n",
progname, gai_strerror(n));
continue;
}
printf("PUMP.0.4;%lu;%s;%s;%s;%s",
now, ep->cnt ? "OK" : "FAIL", tp->host, tp->port, host);
printf(";%u.%03u",