forked from ketkulka/httping
/
main.c
1365 lines (1202 loc) · 41.6 KB
/
main.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
/* The GPL applies to this program.
In addition, as a special exception, the copyright holders give
permission to link the code of portions of this program with the
OpenSSL library under certain conditions as described in each
individual source file, and distribute linked combinations
including the two.
You must obey the GNU General Public License in all respects
for all of the code used other than OpenSSL. If you modify
file(s) with this exception, you may extend this exception to your
version of the file(s), but you are not obligated to do so. If you
do not wish to do so, delete this exception statement from your
version. If you delete this exception statement from all source
files in the program, then also delete it here.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#ifndef NO_SSL
#include <openssl/ssl.h>
#include "mssl.h"
#endif
#include <arpa/inet.h>
#include "gen.h"
#include "http.h"
#include "io.h"
#include "str.h"
#include "mem.h"
#include "tcp.h"
#include "res.h"
#include "utils.h"
#include "error.h"
#include "hostparam.h"
#define BUF_SIZE 4096
static volatile int stop = 0;
int quiet = 0;
char machine_readable = 0;
char nagios_mode = 0;
char last_error[ERROR_BUFFER_SIZE];
void version(void)
{
fprintf(stderr, "HTTPing v" VERSION ", (C) 2003-2012 folkert@vanheusden.com\n");
#ifndef NO_SSL
fprintf(stderr, "SSL support included\n");
#endif
}
void usage(void)
{
fprintf(stderr, "-p portnr portnumber (e.g. 80)\n");
fprintf(stderr, "-x host:port hostname+portnumber of proxyserver\n");
fprintf(stderr, "-c count how many times to connect\n");
fprintf(stderr, "-i interval delay between each connect, can be only smaller than 1 if user is root\n");
fprintf(stderr, "-t timeout timeout (default: 30s)\n");
fprintf(stderr, "-Z ask any proxies on the way not to cache the requests\n");
fprintf(stderr, "-Q use a persistent connection. adds a 'C' to the output if httping had to reconnect\n");
fprintf(stderr, "-6 use IPv6\n");
fprintf(stderr, "-s show statuscodes\n");
fprintf(stderr, "-S split time in connect-time and processing time\n");
fprintf(stderr, "-G do a GET request instead of HEAD (read the\n");
fprintf(stderr, " contents of the page as well)\n");
fprintf(stderr, "-b show transfer speed in KB/s (use with -G)\n");
fprintf(stderr, "-B like -b but use compression if available\n");
fprintf(stderr, "-L x limit the amount of data transferred (for -b)\n");
fprintf(stderr, " to 'x' (in bytes)\n");
fprintf(stderr, "-X show the number of KB transferred (for -b)\n");
#ifndef NO_SSL
fprintf(stderr, "-l connect using SSL\n");
fprintf(stderr, "-z show fingerprint (SSL)\n");
#endif
fprintf(stderr, "-f flood connect (no delays)\n");
fprintf(stderr, "-a audible ping\n");
fprintf(stderr, "-m give machine parseable output (see\n");
fprintf(stderr, " also -o and -e)\n");
fprintf(stderr, "-o rc,rc,... what http results codes indicate 'ok'\n");
fprintf(stderr, " coma seperated WITHOUT spaces inbetween\n");
fprintf(stderr, " default is 200, use with -e\n");
fprintf(stderr, "-e str string to display when http result code\n");
fprintf(stderr, " doesn't match\n");
fprintf(stderr, "-I str use 'str' for the UserAgent header\n");
fprintf(stderr, "-R str use 'str' for the Referer header\n");
fprintf(stderr, "-r resolve hostname only once (usefull when\n");
fprintf(stderr, " pinging roundrobin DNS: also takes the first\n");
fprintf(stderr, " DNS lookup out of the loop so that the first\n");
fprintf(stderr, " measurement is also correct)\n");
fprintf(stderr, "-n warn,crit Nagios-mode: return 1 when avg. response time\n");
fprintf(stderr, " >= warn, 2 if >= crit, otherwhise return 0\n");
fprintf(stderr, "-N x Nagios mode 2: return 0 when all fine, 'x'\n");
fprintf(stderr, " when anything failes\n");
fprintf(stderr, "-y ip[:port] bind to ip-address (and thus interface) [/port]\n");
fprintf(stderr, "-q quiet, only returncode\n");
fprintf(stderr, "-A Activate Basic authentication\n");
fprintf(stderr, "-U Username needed for authentication\n");
fprintf(stderr, "-P Password needed for authentication\n");
fprintf(stderr, "-C cookie=value Add a cookie to the request\n");
fprintf(stderr, "-V show the version\n\n");
fprintf(stderr, "\n");
fprintf(stderr, "--port -p\n");
fprintf(stderr, "--host-port -x\n");
fprintf(stderr, "--count -c\n");
fprintf(stderr, "--interval -i\n");
fprintf(stderr, "--timeout -t\n");
fprintf(stderr, "--ipv6 - 6\n");
fprintf(stderr, "--show-statusodes -s\n");
fprintf(stderr, "--split-time -S\n");
fprintf(stderr, "--get-request -G\n");
fprintf(stderr, "--show-transfer-speed -b\n");
fprintf(stderr, "--show-xfer-speed-compressed -B\n");
fprintf(stderr, "--data-limit -L\n");
fprintf(stderr, "--show-kb -X\n");
#ifndef NO_SSL
fprintf(stderr, "--use-ssl -l\n");
fprintf(stderr, "--show-fingerprint -z\n");
#endif
fprintf(stderr, "--flood -f\n");
fprintf(stderr, "--audible-ping -a\n");
fprintf(stderr, "--parseable-output -m\n");
fprintf(stderr, "--ok-result-codes -o\n");
fprintf(stderr, "--result-string -e\n");
fprintf(stderr, "--user-agent -I\n");
fprintf(stderr, "--referer -S\n");
fprintf(stderr, "--resolve-once -r\n");
fprintf(stderr, "--nagios-mode-1 -n\n");
fprintf(stderr, "--nagios-mode-2 -n\n");
fprintf(stderr, "--bind-to -y\n");
fprintf(stderr, "--quiet -q\n");
fprintf(stderr, "--basic-auth -A\n");
fprintf(stderr, "--username -U\n");
fprintf(stderr, "--password -P\n");
fprintf(stderr, "--cookie -C\n");
fprintf(stderr, "--persistent-connections -Q\n");
fprintf(stderr, "--no-cache -Z\n");
fprintf(stderr, "--tcp-fast-open -F\n");
fprintf(stderr, "--version -V\n");
fprintf(stderr, "--help -h\n");
}
void emit_error()
{
if (!quiet && !machine_readable && !nagios_mode)
{
printf("%s", last_error);
}
if (!nagios_mode)
last_error[0] = 0x00;
fflush(NULL);
}
void handler(int sig)
{
fprintf(stderr, "Got signal %d\n", sig);
stop = 1;
}
/* Base64 encoding start */
const char *alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
void encode_tryptique(char source[3], char result[4])
/* Encode 3 char in B64, result give 4 Char */
{
int tryptique, i;
tryptique = source[0];
tryptique *= 256;
tryptique += source[1];
tryptique *= 256;
tryptique += source[2];
for (i=0; i<4; i++)
{
result[3-i] = alphabet[tryptique%64];
tryptique /= 64;
}
}
int enc_b64(char *source, size_t source_lenght, char *target)
{
/* Divide string /3 and encode trio */
while (source_lenght >= 3) {
encode_tryptique(source, target);
source_lenght -= 3;
source += 3;
target += 4;
}
/* Add padding to the rest */
if (source_lenght > 0) {
char pad[3];
memset(pad, 0, sizeof(pad));
memcpy(pad, source, source_lenght);
encode_tryptique(pad, target);
target[3] = '=';
if (source_lenght == 1) target[2] = '=';
target += 4;
}
target[0] = 0;
return 1;
}
/* Base64 encoding END */
int main(int argc, char *argv[])
{
int n_hosts = 0;
int goto_loop;
char *proxy = NULL, *proxyhost = NULL;
int proxyport = 8080;
int portnr = 80;
int overflow;
char *get = NULL;
int c = 0;
int count = -1, curncount = 0;
double wait = 1.0;
int audible = 0;
int ok = 0;
int timeout=30;
char show_statuscodes = 0;
char use_ssl = 0;
char *ok_str = "200";
char *err_str = "-1";
char *useragent = NULL;
char *referer = NULL;
char *pwd = NULL;
char *usr = NULL;
char *cookie = NULL;
char resolve_once = 0;
char auth_mode = 0;
int req_sent = 0;
double nagios_warn=0.0, nagios_crit=0.0;
int nagios_exit_code = 2;
int get_instead_of_head = 0;
int page_size = sysconf(_SC_PAGESIZE);
char show_Bps = 0, ask_compression = 0;
int Bps_limit = -1;
char show_bytes_xfer = 0, show_fp = 0;
struct sockaddr_in *bind_to = NULL;
struct sockaddr_in bind_to_4;
struct sockaddr_in6 bind_to_6;
host_param *hp, *hp_tmp, *hp_nag = NULL;
ping_buffer* pb;
char bind_to_valid = 0;
char split = 0, use_ipv6 = 0;
char persistent_connections = 0;
char no_cache = 0;
int tfo = 0;
int index;
int body_no_len = 0;
fd_set rd, wr;
int type_err;
char* fp;
double ms;
char *scdummy;
static struct option long_options[] =
{
{"port", 1, NULL, 'p' },
{"host-port", 1, NULL, 'x' },
{"count", 1, NULL, 'c' },
{"persistent-connections", 0, NULL, 'Q' },
{"interval", 1, NULL, 'i' },
{"timeout", 1, NULL, 't' },
{"ipv6", 0, NULL, '6' },
{"show-statusodes", 0, NULL, 's' },
{"split-time", 0, NULL, 'S' },
{"get-request", 0, NULL, 'G' },
{"show-transfer-speed", 0, NULL, 'b' },
{"show-xfer-speed-compressed", 0, NULL, 'B' },
{"data-limit", 1, NULL, 'L' },
{"show-kb", 0, NULL, 'X' },
{"no-cache", 0, NULL, 'Z' },
#ifndef NO_SSL
{"use-ssl", 0, NULL, 'l' },
{"show-fingerprint", 0, NULL, 'z' },
#endif
{"flood", 0, NULL, 'f' },
{"audible-ping", 0, NULL, 'a' },
{"parseable-output", 0, NULL, 'm' },
{"ok-result-codes", 1, NULL, 'o' },
{"result-string", 1, NULL, 'e' },
{"user-agent", 1, NULL, 'I' },
{"referer", 1, NULL, 'S' },
{"resolve-once",0, NULL, 'r' },
{"nagios-mode-1", 1, NULL, 'n' },
{"nagios-mode-2", 1, NULL, 'n' },
{"bind-to", 1, NULL, 'y' },
{"quiet", 0, NULL, 'q' },
{"basic-auth", 0, NULL, 'A' },
{"username", 1, NULL, 'U' },
{"password", 1, NULL, 'P' },
{"cookie", 1, NULL, 'C' },
{"version", 0, NULL, 'V' },
{"help", 0, NULL, 'h' },
{NULL, 0, NULL, 0 }
};
signal(SIGPIPE, SIG_IGN);
if (page_size == -1)
page_size = 4096;
while((c = getopt_long(argc, argv, "ZQ6Sy:XL:bBp:c:i:Gx:t:o:e:falqsmV?I:R:rn:N:z:AP:U:C:F", long_options, NULL)) != -1)
{
switch(c)
{
case 'Z':
no_cache = 1;
break;
case '6':
use_ipv6 = 1;
break;
case 'S':
split = 1;
break;
case 'Q':
persistent_connections = 1;
break;
case 'y':
{
char *dummy = strchr(optarg, ':');
bind_to_valid = 1;
if (dummy)
{
bind_to = (struct sockaddr_in *)&bind_to_6;
memset(&bind_to_6, 0x00, sizeof(bind_to_6));
bind_to_6.sin6_family = AF_INET6;
if (inet_pton(AF_INET6, optarg, &(bind_to_6.sin6_addr)) != 1)
{
error_exit("cannot convert ip address '%s' (for -y)\n", optarg);
}
}
else
{
bind_to = (struct sockaddr_in *)&bind_to_4;
memset(&bind_to_4, 0x00, sizeof(bind_to_4));
bind_to_4.sin_family = AF_INET;
if (inet_pton(AF_INET, optarg, &(bind_to_4.sin_addr)) != 1)
{
error_exit("cannot convert ip address '%s' (for -y)\n", optarg);
}
}
}
break;
case 'z':
show_fp = 1;
break;
case 'X':
show_bytes_xfer = 1;
break;
case 'L':
Bps_limit = atoi(optarg);
break;
case 'B':
show_Bps = 1;
ask_compression = 1;
break;
case 'b':
show_Bps = 1;
break;
case 'e':
err_str = optarg;
break;
case 'o':
ok_str = optarg;
break;
case 'x':
proxy = optarg;
break;
case 'r':
resolve_once = 1;
break;
case 'p':
portnr = atoi(optarg);
break;
case 'c':
count = atoi(optarg);
break;
case 'i':
wait = atof(optarg);
if (wait < 1.0 && getuid() != 0)
{
fprintf(stderr, "Only root can use intervals smaller than 1\n");
wait = 1.0;
}
break;
case 't':
timeout = atoi(optarg);
break;
case 'I':
useragent = optarg;
break;
case 'R':
referer = optarg;
break;
case 'a':
audible = 1;
break;
case 'f':
wait = 0;
break;
case 'G':
get_instead_of_head = 1;
break;
#ifndef NO_SSL
case 'l':
use_ssl = 1;
break;
#endif
case 'm':
machine_readable = 1;
break;
case 'q':
quiet = 1;
break;
case 's':
show_statuscodes = 1;
break;
case 'V':
version();
return 0;
case 'n':
{
char *dummy = strchr(optarg, ',');
if (nagios_mode) error_exit("-n and -N are mutual exclusive\n");
nagios_mode = 1;
if (!dummy)
error_exit("-n: missing parameter\n");
nagios_warn = atof(optarg);
nagios_crit = atof(dummy + 1);
} break;
case 'N':
if (nagios_mode) error_exit("-n and -N are mutual exclusive\n");
nagios_mode = 2;
nagios_exit_code = atoi(optarg);
break;
case 'A':
auth_mode = 1;
break;
case 'P':
pwd = optarg;
break;
case 'U':
usr = optarg;
break;
case 'C':
cookie = optarg;
break;
case 'F':
#ifdef TCP_TFO
tfo = 1;
#else
printf("Warning: No TCP TFO Supported.. Disabling..\n");
#endif
break;
case '?':
default:
version();
usage();
return 1;
}
}
#ifndef NO_SSL
if (use_ssl && portnr == 80)
portnr = 443;
#endif
/* Multihost hosts */
if (!(n_hosts = argc - optind))
{
usage();
error_exit("No hostname/getrequest given\n");
}
hp = (host_param*) calloc(sizeof(host_param), n_hosts);
while (optind < argc)
{
int i = n_hosts - (argc - optind);
char *slash, *colon;
char *getcopy = argv[optind];
hp_tmp = &hp[i];
if (strncasecmp(getcopy, "http://", 7) == 0)
{
getcopy += 7;
}
else if (strncasecmp(getcopy, "https://", 8) == 0)
{
getcopy += 8;
hp_tmp->use_ssl = 1;
}
if (use_ssl)
hp_tmp->use_ssl = 1;
slash = strchr(getcopy, '/');
if (slash)
*slash = 0x00;
if (!use_ipv6)
{
colon = strchr(getcopy, ':');
if (colon)
{
*colon = 0x00;
hp_tmp->portnr = atoi(colon + 1); /* per host port number */
}
else
{
hp_tmp->portnr = portnr; /* global port number */
}
}
hp_tmp->name = strdup(getcopy);
#ifndef NO_SSL
if (hp_tmp->use_ssl && hp_tmp->portnr == 80)
hp_tmp->portnr = 443;
#endif
optind++;
}
last_error[0] = 0x00;
if (!get_instead_of_head && show_Bps)
error_exit("-b/-B can only be used when also using -G\n");
for(index = 0; index < n_hosts; index++)
{
hp_tmp = &hp[index];
pb = &hp_tmp->ph.pb;
if(tfo && hp_tmp->use_ssl)
{
printf("TCP Fast open and SSL not supported together\n");
hp_tmp->fatal = 1;
continue;
}
hp_set_start_values(hp_tmp);
#ifndef NO_SSL
if (hp_tmp->use_ssl)
{
get = mymalloc(8 /* http:// */ + strlen(hp_tmp->name) + 1 /* colon */ + 5 /* portnr */ + 1 /* / */ + 1 /* 0x00 */, "get");
sprintf(get, "https://%s:%d/", hp_tmp->name, hp_tmp->portnr);
}
else
{
#endif
get = mymalloc(7 /* http:// */ + strlen(hp_tmp->name) + 1 /* colon */ + 5 /* portnr */ + 1 /* / */ + 1 /* 0x00 */, "get");
sprintf(get, "http://%s:%d/", hp_tmp->name, hp_tmp->portnr);
#ifndef NO_SSL
}
#endif
if (proxy)
{
char *dummy = strchr(proxy, ':');
proxyhost = proxy;
if (dummy)
{
*dummy=0x00;
proxyport = atoi(dummy + 1);
}
if (!quiet && !nagios_mode)
fprintf(stderr, "Using proxyserver: %s:%d\n", proxyhost, proxyport);
}
#ifndef NO_SSL
if (hp_tmp->use_ssl)
{
hp_tmp->client_ctx = initialize_ctx();
if (!hp_tmp->client_ctx)
{
snprintf(last_error, ERROR_BUFFER_SIZE, "problem creating SSL context\n");
hp_tmp->fatal = 1;
continue;
}
}
#endif
if (ph_init(&hp_tmp->ph, BUF_SIZE, strlen(get) + 8192) < 0)
error_exit("\nSystem error\n");
if (proxyhost)
pb_write_request(pb, 1, "%s %s HTTP/1.%c\r\n", get_instead_of_head?"GET":"HEAD", get, persistent_connections?'1':'0');
else
{
char *dummy = get, *slash;
if (strncasecmp(dummy, "http://", 7) == 0)
dummy += 7;
else if (strncasecmp(dummy, "https://", 7) == 0)
dummy += 8;
slash = strchr(dummy, '/');
if (slash)
pb_write_request(pb, 1, "%s %s HTTP/1.%c\r\n", get_instead_of_head?"GET":"HEAD", slash, persistent_connections?'1':'0');
else
pb_write_request(pb, 1, "%s / HTTP/1.%c\r\n", get_instead_of_head?"GET":"HEAD", persistent_connections?'1':'0');
}
if (useragent)
pb_write_request(pb, 1, "User-Agent: %s\r\n", useragent);
else
pb_write_request(pb, 1, "User-Agent: HTTPing v" VERSION "\r\n");
pb_write_request(pb, 1, "Host: %s\r\n", hp_tmp->name);
if (referer)
pb_write_request(pb, 1, "Referer: %s\r\n", referer);
if (ask_compression)
pb_write_request(pb, 1, "Accept-Encoding: gzip,deflate\r\n");
if (no_cache)
{
pb_write_request(pb, 1, "Pragma: no-cache\r\n");
pb_write_request(pb, 1, "Cache-Control: no-cache\r\n");
}
/* Basic Authentification */
if (auth_mode) {
char auth_string[255];
char b64_auth_string[255];
if (usr == NULL)
error_exit("Basic Authnetication (-A) can only be used with a username and/or password (-U -P) ");
sprintf(auth_string,"%s:%s",usr,pwd);
enc_b64(auth_string, strlen(auth_string), b64_auth_string);
pb_write_request(pb, 1, "Authorization: Basic %s\r\n", b64_auth_string);
}
/* Cookie Insertion */
if (cookie) {
pb_write_request(pb, 1, "Cookie: %s;\r\n", cookie);
}
if (persistent_connections)
pb_write_request(pb, 1, "Connection: keep-alive\r\n");
pb_write_request(pb, 1, "\r\n");
if (!quiet && !machine_readable && !nagios_mode )
printf("PING %s:%d (%s):\n", hp_tmp->name, hp_tmp->portnr, get);
if (get != NULL)
{
free(get);
get = NULL;
}
} /* end for(index) */
signal(SIGINT, handler);
signal(SIGTERM, handler);
timeout *= 1000; /* change to ms */
/* struct sockaddr_in6 addr; */
struct addrinfo *ai = NULL;
int port, alive;
char* host;
struct addrinfo* ai_use;
double started_at = get_ts();
struct timeval to;
int bl_index = 0, bl_found = 0, bl_state_init;
alive = 0;
while((curncount < count || count == -1) && stop == 0)
{
double dafter_connect = 0.0;
int rc, ret;
double time;
char is_compressed = 0;
goto_loop = 0;
FD_ZERO(&rd);
FD_ZERO(&wr);
to.tv_sec = wait + 1;
to.tv_usec = 0;
time = get_ts();
for(index = 0; index < n_hosts; index++)
{
hp_tmp = &hp[index];
if (hp_tmp->ph.state == 0 && !hp_tmp->fatal && hp_tmp->wait <= time)
{
hp_tmp->sc = NULL;
alive++;
host = proxyhost ? proxyhost : hp_tmp->name;
port = proxyhost ? proxyport : hp_tmp->portnr;
persistent_loop:
if (hp_tmp->ph.fd == -1 && (!resolve_once || (resolve_once == 1 && hp_tmp->have_resolved == 0)))
{
memset(&hp_tmp->addr, 0x00, sizeof(hp_tmp->addr));
if (ai)
{
freeaddrinfo(ai);
ai = NULL;
}
if (resolve_host(host, &ai, use_ipv6, port) == -1)
{
hp_tmp->err++;
emit_error();
hp_tmp->have_resolved = 1;
continue;
}
ai_use = select_resolved_host(ai, use_ipv6);
get_addr(ai_use, &hp_tmp->addr);
}
req_sent = 0;
if ((persistent_connections && hp_tmp->ph.fd < 0) || (!persistent_connections))
{
hp_tmp->dstart = get_ts();
hp_tmp->ph.fd = connect_to((struct sockaddr *)(bind_to_valid?bind_to:NULL), ai, timeout, tfo, &hp_tmp->ph.pb, &req_sent);
}
if (hp_tmp->ph.fd < 0)
{
emit_error();
hp_tmp->ph.fd = -1;
continue;
}
if (hp_tmp->ph.fd >= 0)
{
/* set socket to low latency */
if (set_tcp_low_latency(hp_tmp->ph.fd) == -1)
{
close(hp_tmp->ph.fd);
hp_tmp->ph.fd = -1;
continue;
}
/* set fd blocking */
if (set_fd_blocking(hp_tmp->ph.fd) == -1)
{
close(hp_tmp->ph.fd);
hp_tmp->ph.fd = -1;
continue;
}
#ifndef NO_SSL
if (hp_tmp->use_ssl && hp_tmp->ssl_h == NULL)
{
BIO *s_bio = NULL;
rc = connect_ssl(hp_tmp->ph.fd, hp_tmp->client_ctx, &hp_tmp->ssl_h, &s_bio, timeout);
if (rc != 0)
{
close(hp_tmp->ph.fd);
hp_tmp->ph.fd = rc;
if (persistent_connections)
{
if (++hp_tmp->persistent_tries < 2)
{
close(hp_tmp->ph.fd);
hp_tmp->ph.fd = -1;
hp_tmp->persistent_did_reconnect = 1;
goto persistent_loop;
}
}
}
}
#endif
}
if (hp_tmp->ph.fd < 0)
{
if (hp_tmp->ph.fd == -2)
snprintf(last_error, ERROR_BUFFER_SIZE, "timeout connecting to host\n");
emit_error();
hp_tmp->ph.fd = -1;
continue;
}
if (split)
dafter_connect = get_ts();
hp_tmp->ph.state = (req_sent) ? 2 : 1;
}
//states
if (hp_tmp->ph.state == 0)//request connection again
{
if (hp_tmp->ph.fd != -1) {
FD_CLR(hp_tmp->ph.fd, &rd);
FD_CLR(hp_tmp->ph.fd, &wr);
}
}
else if (hp_tmp->ph.state == 1)//ready to write request
FD_SET(hp_tmp->ph.fd, &wr);
else if (hp_tmp->ph.state == 2)//ready to read Header
FD_SET(hp_tmp->ph.fd, &rd);
else if (hp_tmp->ph.state == 3)//ready to read Body
FD_SET(hp_tmp->ph.fd, &rd);
if (goto_loop)
{
goto_loop = 0;
break;
}
}//end for
/* to prevent CPU high usage */
if (!alive && curncount != count && !stop)
{
usleep((useconds_t)(wait * 1000000.0));
continue; // in order to avoid select fail
}
if ((ret = select(hp_max_fd(hp, n_hosts) + 1 , &rd, &wr, NULL, &to)) <= 0)
{
if (stop)
break;
if (ret == 0)
{
bl_index = bl_found = 0;
bl_state_init = 0;
for (;bl_index < n_hosts; bl_index++)
{
body_no_len = 0;
if (hp[bl_index].ph.state == 0)
bl_state_init = 1;
for_body_no_len:
if (hp[bl_index].ph.state == 3)
{
bl_found = 1;
body_no_len = 1;
hp_tmp = &hp[bl_index];
goto body_no_len;
}
}
body_no_len = 0;
if (!bl_found && !bl_state_init)
error_exit("\nNo more host available\n");
continue;
}
else
error_exit("\nSystem error (select)\n");
}
for (index = 0; index < n_hosts; index++)
{
hp_tmp = &hp[index];
/* BSD select overflow fix */
if (hp_tmp->ph.fd == -1)
continue;
/* state 1: send request*/
if (FD_ISSET(hp_tmp->ph.fd, &wr) && hp_tmp->ph.state == 1)
{
#ifndef NO_SSL
if (hp_tmp->use_ssl)
rc = ph_send_ssl(hp_tmp->ssl_h, &hp_tmp->ph);
else
#endif
{
if (!req_sent)
{
hp_tmp->dstart = get_ts();
rc = ph_send(&hp_tmp->ph);
}
else
rc = 1;
}
if (rc < 0) //errors
{
if (persistent_connections)
{
if (++hp_tmp->persistent_tries < 2)
{
close(hp_tmp->ph.fd);
hp_tmp->persistent_did_reconnect = 1;
hp_tmp->ph.fd = -1;
hp_tmp->ph.state = 0;
goto_loop = 1;
goto persistent_loop;
}
}
emit_error();
close(hp_tmp->ph.fd);
hp_tmp->ph.fd = -1;
hp_tmp->ph.state = 0;
hp_tmp->err++;
continue;
}
else if (rc == 1)
hp_tmp->ph.state = 2;
}
/* state 2: Header read */
else if (FD_ISSET(hp_tmp->ph.fd, &rd) && hp_tmp->ph.state == 2)
{
if (hp_tmp->ph.state == 2)
{
hp_tmp->rep_len = overflow = 0;
#ifndef NO_SSL
if (hp_tmp->ssl_h)
rc = ph_recv_ssl_HTTP_header(&hp_tmp->ph, hp_tmp->ssl_h, &hp_tmp->header, &hp_tmp->header_len, &overflow);
else
#endif
rc = ph_recv_HTTP_header(&hp_tmp->ph, &hp_tmp->header, &hp_tmp->header_len, &overflow);
if (rc < 0)
{
if (persistent_connections)
{
if (++hp_tmp->persistent_tries < 2)
{
close(hp_tmp->ph.fd);
hp_tmp->ph.state = 0;
hp_tmp->ph.fd = -1;
hp_tmp->persistent_did_reconnect = 1;
goto_loop = 1;
goto persistent_loop;
}
}
if (rc == -1)
snprintf(last_error, ERROR_BUFFER_SIZE, "error receiving reply from host\n");
emit_error();
close(hp_tmp->ph.fd);
hp_tmp->ph.fd = -1;
hp_tmp->ph.state = 0;
hp_tmp->err++;
continue;
}
if (rc == 0)
continue;
if ((show_statuscodes || machine_readable) && hp_tmp->header != NULL)
{
/* statuscode is in first line behind
* 'HTTP/1.x'
*/
char *dummy = strchr(hp_tmp->header, ' ');
if (dummy)
{
if (hp_tmp->sc != NULL)
free(hp_tmp->sc);
hp_tmp->sc = strdup(dummy + 1);
/* lines are normally terminated with a
* CR/LF
*/