-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpkg_ping.c
More file actions
4441 lines (3656 loc) · 92.8 KB
/
Copy pathpkg_ping.c
File metadata and controls
4441 lines (3656 loc) · 92.8 KB
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
/*
* BSD 2-Clause License
*
* Copyright (c) 2016 - 2026, Luke N Small, thinkitdoitdone@gmail.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 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 HOLDER 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.
*/
/*
* Originally used the following from "Dan Mclaughlin"
* on openbsd-misc mailing list
*
*
* ftp -o - http://www.openbsd.org/ftp.html | \
* sed -n \
* -e 's:</a>$::' \
* -e 's: <strong>\([^<]*\)<.*:\1:p' \
* -e 's:^\( [hfr].*\):\1:p'
*/
/*
* As root:
* cc pkg_ping.c -Wno-extern-initializer -o /usr/local/bin/pkg_ping
*
* If you want bleeding edge performance, you can try:
*
* As root:
cc pkg_ping.c -march=native -mtune=native -flto -static -O3 \
-Wno-extern-initializer -o /usr/local/bin/pkg_ping
* run with: /usr/local/bin/pkg_ping
*
* if there are no other pkg_ping files in your execution path:
* you can run with: pkg_ping
*
* You won't see ANY appreciable performance gain between the
* getaddrinfo(3) and ftp(1) calls which fetch data over the network.
* Everything else happens in likely less than third of a second
* after the first ftp call starts to return its results.
*
* program designed to be viewed with tabs which are 8 characters wide
*/
#include <sys/types.h>
#include <sys/event.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
/*
* OpenBSD seems to enforce a single declaration of this, now.
*/
extern char *malloc_options = "CFGJJU";
/*
* Wipe out environment variables:
* which conceivably could be bad to keep.
*
* We don't need environment variables at all.
* I'm going to disable them
*/
extern char **environ = NULL;
/*
* I figured that I'd clean up some of the magic numbers.
*/
#define PARENT_SOCK 0
#define CHILD_SOCK 1
static int entry_line = __LINE__;
/* GENERATED CODE BEGINS HERE */
static const char *ftp_list[51] = {
"openbsd.mirror.constant.com","plug-mirror.rcac.purdue.edu",
"cloudflare.cdn.openbsd.org","ftp.halifax.rwth-aachen.de",
"ftp.rnl.tecnico.ulisboa.pt","mirrors.ocf.berkeley.edu",
"mirrors.pidginhost.com","openbsd.cs.toronto.edu","*artfiles.org/openbsd",
"mirror.planetunix.net","www.mirrorservice.org","mirror.aarnet.edu.au",
"openbsd.c3sl.ufpr.br","ftp.usa.openbsd.org","ftp2.fr.openbsd.org",
"mirror.leaseweb.com","mirror.telepoint.bg","mirrors.gigenet.com",
"ftp.eu.openbsd.org","ftp.fr.openbsd.org","ftp.lysator.liu.se",
"mirror.freedif.org","mirror.fsmg.org.nz","mirror.ungleich.ch",
"mirrors.aliyun.com","mirrors.dotsrc.org","openbsd.ipacct.com",
"ftp.hostserver.de","mirrors.chroot.ro","mirrors.sonic.net","mirrors.ucr.ac.cr",
"openbsd.as250.net","mirror.group.one","mirror.litnet.lt","mirror.yandex.ru",
"cdn.openbsd.org","ftp.OpenBSD.org","ftp.jaist.ac.jp","mirror.ihost.md",
"mirror.junda.nl","mirror.mephi.ru","mirror.ox.ac.uk","mirrors.mit.edu",
"ftp.icm.edu.pl","mirror.rise.ph","ftp.cc.uoc.gr","ftp.spline.de",
"ftp.nluug.nl","ftp.psnc.pl","ftp.bit.nl","ftp.fau.de"
};
static const int ftp_list_index = 51;
/* Trusted OpenBSD.org subdomain mirrors for generating this section */
static const char *ftp_list_g[7] = {
"cloudflare.cdn.openbsd.org","ftp.usa.openbsd.org","ftp2.fr.openbsd.org",
"ftp.eu.openbsd.org","ftp.fr.openbsd.org","cdn.openbsd.org","ftp.OpenBSD.org"
};
static const int ftp_list_index_g = 7;
/* GENERATED CODE ENDS HERE */
static int exit_line = __LINE__;
typedef struct {
long double diff; // usually the time it took to download
// can also be the string length of the MIRROR
// if generating a MIRROR list
long double speed; // the scraped download speed from ftp(1)
long double diff_rating; // rating of the diff amongst other MIRRORs
long double speed_rating; // rating of speed amongst other MIRRORs
char *label; // indicating the city, country, or location
char *http; // URL of the MIRROR
int diff_rank; // rank of the diff amongst other MIRRORs
int speed_rank; // rank of speed amongst other MIRRORs
} MIRROR;
/*
* strlen("http://") == 7
*/
static int h = 7;
static int array_length = 0;
static MIRROR *array = NULL;
/*
* 1 second for an ftp SIGINT to turn into a SIGKILL
*/
static const struct timespec timeout_kill = { 1, 0 };
static int kq = -1;
static char *current_time = NULL;
static char *diff_string = NULL;
static char *line = NULL;
static char *tag = NULL;
static const size_t dns_socket_len = 1256;
/*
* Called once with atexit. It's the only function called with atexit
* and free_array isn't called elsewhere
*/
static void
free_array(void)
{
MIRROR *ac = array + array_length;
while (array < ac) {
--ac;
(void)free(ac->label);
(void)free(ac->http);
}
array_length = 0;
(void)free(array);
array = NULL;
(void)free(diff_string);
diff_string = NULL;
(void)free(line);
line = NULL;
(void)free(tag);
tag = NULL;
(void)free(current_time);
current_time = NULL;
}
static long double almost_zero = 0.0L;
/*
* print long double which is <1 and >0, without the leading '0'
* eg. 0.25 is printed: .25
* it doesn't get here unless diff <1 and >= 0
*/
static void
sub_one_print(long double diff)
{
int i = 0;
if ((diff < almost_zero) && (diff >= 0.0L)) {
(void)printf("0");
return;
}
if ((diff >= 1.0L) || (diff < 0.0L)) {
errx(1, "Shouldn't ever get here line: %d", __LINE__);
}
i = snprintf(diff_string, 12, "%.9Lf", diff);
if (i != 11) {
if (i < 0) {
err(1, "snprintf, line: %d", __LINE__);
} else {
err(1, "'line': %s, snprintf, line: %d",
diff_string, __LINE__);
}
}
(void)printf("%s", 1 + diff_string);
}
/*
* comparison between two MIRRORs.
* Prioritize MIRRORs which are likely closer to the USA,
* otherwise...don't care.
*/
static int
usa_cmp(const void *a, const void *b)
{
const char *one_label = ((const MIRROR *) a)->label;
const char *two_label = ((const MIRROR *) b)->label;
/*
* prioritize the USA mirrors first
*/
int temp = (int)(strstr(one_label, "USA") != NULL);
if (temp != (int)(strstr(two_label, "USA") != NULL)) {
if (temp) {
return (-1);
}
return 1;
}
if (temp) {
return 0;
}
/*
* prioritize Content Delivery Network "CDN" mirrors next
*/
temp = (int)(strstr(one_label, "CDN") != NULL);
if (temp != (int)(strstr(two_label, "CDN") != NULL)) {
if (temp) {
return (-1);
}
return 1;
}
if (temp) {
return 0;
}
/*
* prioritize Canada mirrors last
*/
temp = (int)(strstr(one_label, "Canada") != NULL);
if (temp != (int)(strstr(two_label, "Canada") != NULL)) {
if (temp) {
return (-1);
}
return 1;
}
return 0;
}
/*
* compare the labels alphabetically by proper decreasing
* hierarchy which are in reverse order between commas.
*
* checks to make sure these procedures are safe, are performed in main
* It can assume all commas in the labels are followed by a space,
* there is no leading comma, and there are no double spaces.
*/
static int
label_cmp_minus_usa(const void *a, const void *b)
{
const char *one_label = ((const MIRROR *) a)->label;
const char *two_label = ((const MIRROR *) b)->label;
int ret = 0;
/*
* strlen(", ") == 2
*/
int rc = 2;
int bc = 2;
/*
* start with the last comma
*/
const char *red = strrchr(one_label, ',');
const char *blu = strrchr(two_label, ',');
if (red == NULL) {
red = one_label;
rc = 0;
}
if (blu == NULL) {
blu = two_label;
bc = 0;
}
ret = strcmp(
red + rc,
blu + bc
);
while ((ret == 0) && rc && bc) {
/*
* search for a comma before the one
* found in the previous iteration
*/
/*
* no leading comma is enforced in main()
*/
for (;;) {
if (one_label == red) {
rc = 0;
break;
}
--red;
if (*red == ',') {
break;
}
}
for (;;) {
if (two_label == blu) {
bc = 0;
break;
}
--blu;
if (*blu == ',') {
break;
}
}
ret = strcmp(
red + rc,
blu + bc
);
}
if (ret == 0) {
/*
* rc and bc are NOT both non-zero here:
* it had to escape the "while ((ret == 0) && rc && bc)"
*
* if (rc || bc):
* Either red or blu has no more comma
* separated entries while remaining, equal.
* The one with fewer commas is preferred first.
*/
if (bc) {
return (-1);
}
if (rc) {
return 1;
}
/*
* exactly equal labels (rc == bc == 0):
* Checking for this condition initially
* with a label strcmp() doesn't
* provide useful information unless
* the labels are exactly equal.
* It isn't worth wasting time testing
* for it initially because of its rarity.
*/
return strcmp(
((const MIRROR *) a)->http + h,
((const MIRROR *) b)->http + h
);
}
return ret;
}
/*
* comparison between two MIRRORs.
* It doesn't test speed.
*
* First determine whether the diff is different, then
* Prioritize MIRRORs which are likely close to the USA, otherwise if zero,
* return the reverse result of label_cmp_minus_usa().
*/
static int
diff_cmp_pure(const void *a, const void *b)
{
int ret = 0;
const long double one_diff = ((const MIRROR *) a)->diff;
const long double two_diff = ((const MIRROR *) b)->diff;
if (one_diff < two_diff) {
return (-1);
}
if (one_diff > two_diff) {
return 1;
}
/*
* Prioritize mirrors near to USA next.
* They most likely didn't succeed past here.
*/
ret = usa_cmp(a, b);
if (ret) {
return ret;
}
/*
* reverse subsort label_cmp_minus_usa
*/
return label_cmp_minus_usa(b, a);
}
/*
* comparison between two MIRRORs.
* First determine whether the speed is different, then if the diff is
* different, then
* Prioritize MIRRORs which are likely close to the USA, otherwise if zero,
* return the reverse result of label_cmp_minus_usa().
*/
static int
diff_cmp(const void *a, const void *b)
{
int ret = 0;
const long double one_speed = ((const MIRROR *) a)->speed;
const long double two_speed = ((const MIRROR *) b)->speed;
const long double one_diff = ((const MIRROR *) a)->diff;
const long double two_diff = ((const MIRROR *) b)->diff;
if (one_speed > two_speed) {
return (-1);
}
if (one_speed < two_speed) {
return 1;
}
if (one_diff < two_diff) {
return (-1);
}
if (one_diff > two_diff) {
return 1;
}
/*
* Prioritize mirrors near to USA next.
* They most likely didn't succeed past here.
*/
ret = usa_cmp(a, b);
if (ret) {
return ret;
}
/*
* reverse subsort label_cmp_minus_usa
*/
return label_cmp_minus_usa(b, a);
}
/*
* at this time, diff values represent the length of their http char*
* stripped of the leading "http://" or "https://" and if it exists,
* stripped of the trailing "/pub/OpenBSD".
*/
static int
diff_cmp_g(const void *a, const void *b)
{
/*
* sort those with greater diff values first
* if no difference, compare http strings
*/
const int diff = (
(const int) ((const MIRROR *) b)->diff
-
(const int) ((const MIRROR *) a)->diff
);
if (!diff) {
return strcmp(
((const MIRROR *) a)->http,
((const MIRROR *) b)->http
);
}
return diff;
}
/*
* diff_cmp_g can be used in the place of this function, but it is
* far more efficient to avoid the many unnecessary strcmp() for mirrors
* which have been turned to diff == 0; to be excised from the output.
* if no difference and both are non-zero diff, compare http strings
*/
static int
diff_cmp_g2(const void *a, const void *b)
{
const int one_len = (const int) ((const MIRROR *) a)->diff;
const int two_len = (const int) ((const MIRROR *) b)->diff;
/*
* If either are an OpenBSD.org mirror...
* (which means a non-zero diff)
*
* Vast majority of the time both will be zero
* if so, dont process further.
*
* Otherwise, process like diff_cmp_g
*/
if (one_len || two_len) {
/*
* sort those with greater len values first
*/
if (one_len == two_len) {
return strcmp(
((const MIRROR *) a)->http,
((const MIRROR *) b)->http
);
}
return (two_len - one_len);
}
return 0;
}
/*
* add a usa_cmp to label_cmp_minus_usa()
*/
static int
label_cmp(const void *a, const void *b)
{
/*
* prioritize mirrors near to USA first
*/
const int ret = usa_cmp(a, b);
if (ret) {
return ret;
}
return label_cmp_minus_usa(a, b);
}
/*
* unify diff and speed ratings into a single comparison
*/
static int
unified_cmp(const void *a, const void *b)
{
const long double one_unified_rating =
(((const MIRROR *) a)->diff_rating + 1.0L)
*
(((const MIRROR *) a)->speed_rating + 1.0L);
const long double two_unified_rating =
(((const MIRROR *) b)->diff_rating + 1.0L)
*
(((const MIRROR *) b)->speed_rating + 1.0L);
if (one_unified_rating > two_unified_rating) {
return (-1);
} else if (one_unified_rating < two_unified_rating) {
return 1;
} else {
return 0;
}
}
static void
manpage(void)
{
(void)printf("[-6 (only return IPv6 compatible mirrors)]\n");
(void)printf(
"[-a (rate by an Average of responsiveness and bandwidth!\n");
(void)printf(" This is the default when -v's");
(void)printf(" and no -V are chosen.)]\n");
(void)printf("[-b (rate by the Bandwidth of the download!)]\n");
(void)printf(
"[-D (Debug mode. Short circuit mirror downloads.\n ");
(void)printf("Show elapsed time since ftplist starts downloading.)]\n");
(void)printf("[-d (don't cache DNS)]\n");
(void)printf(
"[-f (don't automatically write to File if run as root)]\n");
(void)printf("[-G (Generate all current sources of the ftp list)]\n");
(void)printf("[-g (Generate source ftp list: only accessible ones)]\n");
(void)printf("[-h (print this Help message and exit)]\n");
(void)printf(
"[-l (ell) quantity of attempts the program will restart\n");
(void)printf(
" in a Loop for recoverable errors (default 20)]\n");
(void)printf("[-n (search for mirrors with the Next release!)]\n");
(void)printf(
"[-O (if you're running a snapshot, it will Override it and\n");
(void)printf(" search for release mirrors. ");
(void)printf("If you're running a release,\n");
(void)printf(" it will Override it and ");
(void)printf("search for snapshot mirrors.)\n");
(void)printf("[-p (search for mirrors with the Previous release!)]\n");
(void)printf(
"[-r (rate by how quickly it Responds to the download!)]\n");
(void)printf("[-S (converts http mirrors into Secure https mirrors\n");
(void)printf(" http mirrors still preserve file integrity!)]\n");
(void)printf("[-s timeout in Seconds (eg. -s 2.3) (default 10 if -g\n");
(void)printf(" is specified. Otherwise default 5)]\n");
(void)printf("[-U (USA, CDN and Canada mirrors Only.");
(void)printf("\n ");
(void)printf("This will likely be faster if you are in these areas.");
(void)printf("\n ");
(void)printf("The program will absolutely take less runtime.)]");
(void)printf("\n");
(void)printf("[-u (no USA mirrors to comply ");
(void)printf("with USA encryption export laws.)]\n");
(void)printf(
"[-V (no Verbose output. No output except error messages)]\n");
(void)printf(
"[-v (increase Verbosity. It recognizes up to 5 of these)]\n\n");
(void)printf("More information at: ");
(void)printf("https://github.com/lukensmall/pkg_ping\n\n");
}
/*
* I created this DNS caching daemon which when supplied a URL, it will print
* out DNS records, but it also pre-caches any ftp(1) call where ftp(1) issues a
* DNS call using the same mechanism so, fetching it precaches whatever dns
* server you're using, so it's fetched very quickly to let the ftp(1) call
* timings not need to wait for DNS calls to fetch the DNS records across the
* internet in an undetermined time; thereby eliminating the biggest problem in
* determining the raw responsiveness of a server to a request in a single
* request to limit the annoying file downloads on their networks to an
* absolute minimum.
*/
static __attribute__((noreturn)) void
dns_cache_d(const int dns_socket, const int secure,
const int six, const int verbose)
{
int i = 0;
int g = 0;
int c = 0;
struct addrinfo *res0 = NULL;
struct addrinfo *res = NULL;
int ret = 1;
/*
from: /usr/src/include/netdb.h
struct addrinfo {
int ai_flags;
int ai_family;
int ai_socktype;
int ai_protocol;
socklen_t ai_addrlen;
struct sockaddr *ai_addr;
char *ai_canonname;
struct addrinfo *ai_next;
};
*/
const struct addrinfo hints =
{ AI_FQDN, AF_UNSPEC, SOCK_STREAM, 0, 0, NULL, NULL, NULL };
struct sockaddr_in *sa4 = NULL;
uint32_t sui4 = 0;
struct sockaddr_in6 *sa6 = NULL;
u_char *suc6 = NULL;
int max = 0;
int i_temp = 0;
int i_max = 0;
char six_available = '0';
const char *dns_line0 = (secure) ? "https" : "http";
const char *dns_line0_alt = (secure) ? "443" : "80";
const char hexadec[16] = { '0','1','2','3',
'4','5','6','7',
'8','9','a','b',
'c','d','e','f' };
char *dns_line = (char*)calloc(dns_socket_len, sizeof(char));
if (dns_line == NULL) {
(void)printf("calloc\n");
goto dns_exit;
}
if (pledge("stdio dns", NULL) == -1) {
(void)printf("%s ", strerror(errno));
(void)printf("dns_cache_d pledge, line: %d\n", __LINE__);
goto dns_exit;
}
dns_loop:
i = (int)read(dns_socket, dns_line, dns_socket_len);
if (i == 0) {
ret = 0;
goto dns_exit;
}
if (i == dns_socket_len) {
(void)printf("i == dns_socket_len, line: %d\n", __LINE__);
goto dns_exit;
}
if (i == -1) {
(void)printf("%s ", strerror(errno));
(void)printf("read error line: %d\n", __LINE__);
goto dns_exit;
}
dns_line[i] = '\0';
if (verbose >= 4) {
(void)printf("DNS caching: %s\n", dns_line);
}
if (getaddrinfo(dns_line, dns_line0, &hints, &res0)) {
c = getaddrinfo(dns_line, dns_line0_alt, &hints, &res0);
if (c) {
if (verbose >= 4) {
(void)printf("%s\n", gai_strerror((int)c));
}
i = (int)write(dns_socket, "f", sizeof(char));
if (i != sizeof(char)) {
if (i == -1) {
(void)printf("%s ", strerror(errno));
}
(void)printf("write error line: %d\n",
__LINE__);
goto dns_exit;
}
goto dns_loop;
}
}
if ((verbose < 4) && (six == 0)) {
for (res = res0; res; res = res->ai_next) {
if (res->ai_family == AF_INET) {
/*
* compiler complains of potential misalignment
*
* sa4 = (struct sockaddr_in *) res->ai_addr;
*/
(void)memcpy(&sa4, &res->ai_addr,
sizeof(struct sockaddr_in *));
sui4 = sa4->sin_addr.s_addr;
/*
* I have an unbound dns blocklist where I
* force unwanted domains to resolve to
* 0.0.0.0 which translates to sui4 == 0
* This shouldn't impact functionality
* for others.
*/
if (sui4 == 0U) {
continue;
}
break;
}
if (res->ai_family == AF_INET6) {
break;
}
}
if (res == NULL) {
i = (int)write(dns_socket, "u", sizeof(char));
} else {
i = (int)write(dns_socket, "1", sizeof(char));
}
if (i != sizeof(char)) {
if (i == -1) {
(void)printf("%s ", strerror(errno));
}
(void)printf("write error line: %d\n", __LINE__);
freeaddrinfo(res0);
goto dns_exit;
}
freeaddrinfo(res0);
goto dns_loop;
}
six_available = 'u';
for (res = res0; res; res = res->ai_next) {
if (res->ai_family == AF_INET) {
/*
* compiler complains of potential misalignment
*
* sa4 = (struct sockaddr_in *) res->ai_addr;
*/
(void)memcpy(&sa4, &res->ai_addr,
sizeof(struct sockaddr_in *));
sui4 = sa4->sin_addr.s_addr;
/*
* I have an unbound dns blocklist where I
* force unwanted domains to resolve to
* 0.0.0.0 which translates to sui4 == 0
* I don't expect a negative impact
* to functionality for others.
*/
if ((six_available == 'u') && sui4) {
six_available = '0';
}
if (six) {
continue;
}
(void)printf(" %hhu.%hhu.%hhu.%hhu\n",
(uint8_t) sui4,
(uint8_t)(sui4 >> 8),
(uint8_t)(sui4 >> 16),
(uint8_t)(sui4 >> 24));
continue;
}
if (res->ai_family != AF_INET6) {
continue;
}
six_available = '1';
if (verbose < 4) {
break;
}
(void)printf(" ");
/*
* compiler complains of potential misalignment
*
* sa6 = (struct sockaddr_in6 *) res->ai_addr;
*/
(void)memcpy(&sa6, &res->ai_addr,
sizeof(struct sockaddr_in6 *));
suc6 = sa6->sin6_addr.s6_addr;
c = 0;
max = 0;
i_max = -1;
/*
* load largest >1 gap beginning into i_max
* and the length of the gap into max
*/
for (i = 0; i < 16; i += 2) {
if ( suc6[i] || suc6[i + 1] ) {
c = 0;
continue;
}
if (c == 0) {
i_temp = i;
c = 1;
continue;
}
++c;
if (max < c) {
max = c;
i_max = i_temp;
}
}
for (i = 0; i < 16; i += 2) {
if (i) {
(void)printf(":");
}
if (i == i_max) {
if (i == 0) {
(void)printf("::");
} else {
(void)printf(":");
}
i += 2 * max;
if (i >= 16) {
break;
}
}
g = i + 1;
if (suc6[i] / (u_char)16) {
(void)printf("%c%c%c%c",
hexadec[suc6[i] / (u_char)16],
hexadec[suc6[i] % (u_char)16],
hexadec[suc6[g] / (u_char)16],
hexadec[suc6[g] % (u_char)16]);
} else if (suc6[i]) {
/*
* Here: suc6[i] == suc6[i] % 16
*/
(void)printf("%c%c%c",
hexadec[suc6[i] ],
hexadec[suc6[g] / (u_char)16],
hexadec[suc6[g] % (u_char)16]);
} else if (suc6[g] / (u_char)16) {
(void)printf("%c%c",