-
Notifications
You must be signed in to change notification settings - Fork 0
/
si_getaddrinfo.c
1690 lines (1430 loc) · 38.4 KB
/
si_getaddrinfo.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
/*
* Copyright (c) 2008-2011 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include <netdb.h>
#include <sys/types.h>
#include <ctype.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#if 0 // where is this header?
#include <network/sa_compare.h>
#endif
#include <arpa/inet.h>
#include <ifaddrs.h>
#include <string.h>
#include <sys/param.h>
#if NOTIFY_SUPPORT
#include <notify.h>
#include <notify_keys.h>
#endif
#include <pthread.h>
#include <TargetConditionals.h>
#include "netdb_async.h"
#include "si_module.h"
#define WRAPFN(f) __wrap_##f
#define REALFN(f) __real_##f
#define SOCK_UNSPEC 0
#define IPPROTO_UNSPEC 0
#define IPV6_ADDR_LEN 16
#define IPV4_ADDR_LEN 4
#define WANT_NOTHING 0
#define WANT_A4_ONLY 1
#define WANT_A6_ONLY 2
#define WANT_A6_PLUS_MAPPED_A4 3
#define WANT_A6_OR_MAPPED_A4_IF_NO_A6 4
#define V6TO4_PREFIX_16 0x2002
static int net_config_token = -1;
static uint32_t net_v4_count = 0;
static uint32_t net_v6_count = 0; // includes 6to4 addresses
static pthread_mutex_t net_config_mutex = PTHREAD_MUTEX_INITIALIZER;
// Libc SPI
int _inet_aton_check(const char *cp, struct in_addr *addr, int strict);
typedef struct {
struct hostent host;
int alias_count;
int addr_count;
uint64_t ttl;
} build_hostent_t;
__private_extern__ int
si_inet_config(uint32_t *inet4, uint32_t *inet6)
{
int status, checkit;
struct ifaddrs *ifa, *ifap;
pthread_mutex_lock(&net_config_mutex);
checkit = 1;
#if NOTIFY_SUPPORT
if (net_config_token < 0)
{
status = notify_register_check(kNotifySCNetworkChange, &net_config_token);
if (status != 0) net_config_token = -1;
}
if (net_config_token >= 0)
{
status = notify_check(net_config_token, &checkit);
if (status != 0) checkit = 1;
}
#endif
status = 0;
if (checkit != 0)
{
if (getifaddrs(&ifa) < 0)
{
status = -1;
}
else
{
net_v4_count = 0;
net_v6_count = 0;
for (ifap = ifa; ifap != NULL; ifap = ifap->ifa_next)
{
if (ifap->ifa_addr == NULL) continue;
if ((ifap->ifa_flags & IFF_UP) == 0) continue;
if (ifap->ifa_addr->sa_family == AF_INET)
{
net_v4_count++;
}
else if (ifap->ifa_addr->sa_family == AF_INET6)
{
net_v6_count++;
}
}
}
freeifaddrs(ifa);
}
if (inet4 != NULL) *inet4 = net_v4_count;
if (inet6 != NULL) *inet6 = net_v6_count;
pthread_mutex_unlock(&net_config_mutex);
return status;
}
void
WRAPFN(freeaddrinfo)(struct addrinfo *a)
{
struct addrinfo *next;
while (a != NULL)
{
next = a->ai_next;
if (a->ai_addr != NULL) free(a->ai_addr);
if (a->ai_canonname != NULL) free(a->ai_canonname);
free(a);
a = next;
}
}
const char *
gai_strerror(int32_t err)
{
switch (err)
{
case EAI_ADDRFAMILY: return "Address family for nodename not supported";
case EAI_AGAIN: return "Temporary failure in name resolution";
case EAI_BADFLAGS: return "Invalid value for ai_flags";
case EAI_FAIL: return "Non-recoverable failure in name resolution";
case EAI_FAMILY: return "ai_family not supported";
case EAI_MEMORY: return "Memory allocation failure";
case EAI_NODATA: return "No address associated with nodename";
case EAI_NONAME: return "nodename nor servname provided, or not known";
case EAI_SERVICE: return "servname not supported for ai_socktype";
case EAI_SOCKTYPE: return "ai_socktype not supported";
case EAI_SYSTEM: return "System error";
case EAI_BADHINTS: return "Bad hints";
case EAI_PROTOCOL: return "ai_protocol not supported";
case EAI_OVERFLOW: return "argument buffer overflow";
}
return "Unknown error";
}
/*
* getnameinfo
*
* We handle some "trival" cases locally. If the caller passes
* NI_NUMERICHOST (only), then this call turns into a getservbyport
* to get the service name + inet_pton() to create a host string.
* If the caller passes NI_NUMERICSERV (only), then we zero out the port
* number, complete the getnameinfo, and use printf() to create a service
* string. If the caller specifies both NI_NUMERICHOST and NI_NUMERICSERV,
* we inet_ntop() and printf() and return the results.
*/
si_item_t *
si_nameinfo(si_mod_t *si, const struct sockaddr *sa, int flags, const char *interface, uint32_t *err)
{
si_item_t *out = NULL;
const struct sockaddr *lookup_sa;
struct sockaddr_in s4;
struct in_addr a4;
struct in6_addr a6;
const uint32_t unused = 0;
void *addr = NULL;
char *host = NULL;
char *serv = NULL;
uint32_t ifnum = 0;
uint16_t port = 0;
int do_host_lookup = ((flags & NI_NUMERICHOST) == 0);
int do_serv_lookup = ((flags & NI_NUMERICSERV) == 0);
/* check input */
if ((si == NULL) || (sa == NULL))
{
if (err != NULL) *err = SI_STATUS_EAI_FAIL;
return NULL;
}
if (err != NULL) *err = SI_STATUS_NO_ERROR;
lookup_sa = sa;
if (sa->sa_family == AF_INET)
{
struct sockaddr_in *s4 = (struct sockaddr_in *)sa;
memcpy(&a4, &s4->sin_addr, sizeof(a4));
port = s4->sin_port;
addr = &a4;
}
else if (sa->sa_family == AF_INET6)
{
struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)sa;
memcpy(&a6, &s6->sin6_addr, sizeof(a6));
port = s6->sin6_port;
/* Look for scope id in IPv6 Link Local, Multicast Node Local, and Multicast Link Local */
if (IN6_IS_ADDR_LINKLOCAL(&s6->sin6_addr) || IN6_IS_ADDR_MC_NODELOCAL(&s6->sin6_addr) || IN6_IS_ADDR_MC_LINKLOCAL(&s6->sin6_addr))
{
ifnum = ntohs(a6.__u6_addr.__u6_addr16[1]);
if (ifnum == 0)
{
ifnum = s6->sin6_scope_id;
a6.__u6_addr.__u6_addr16[1] = htons(ifnum);
}
if ((ifnum != s6->sin6_scope_id) && (s6->sin6_scope_id != 0))
{
if (err != NULL) *err = SI_STATUS_EAI_FAIL;
return NULL;
}
}
/* v4 mapped and compat addresses are converted to plain v4 */
if (IN6_IS_ADDR_V4MAPPED(&s6->sin6_addr) || IN6_IS_ADDR_V4COMPAT(&s6->sin6_addr))
{
memcpy(&a4, &s6->sin6_addr.s6_addr[12], sizeof(a4));
addr = &a4;
memset(&s4, 0, sizeof(s4));
s4.sin_len = sizeof(s4);
s4.sin_family = AF_INET;
s4.sin_port = port;
memcpy(&s4.sin_addr, &a4, sizeof(s4.sin_addr));
lookup_sa = (const struct sockaddr *)&s4;
}
else
{
addr = &a6;
}
}
else
{
if (err != NULL) *err = SI_STATUS_EAI_FAMILY;
return NULL;
}
if (do_host_lookup == 1)
{
si_item_t *item = si_host_byaddr(si, addr, lookup_sa->sa_family, interface, NULL);
if (item != NULL)
{
struct hostent *h;
h = (struct hostent *)((uintptr_t)item + sizeof(si_item_t));
host = strdup(h->h_name);
si_item_release(item);
if (host == NULL)
{
if (err != NULL) *err = SI_STATUS_EAI_MEMORY;
return NULL;
}
}
}
if ((do_serv_lookup == 1) && (port != 0))
{
si_item_t *item = si_service_byport(si, port, NULL);
if (item != NULL)
{
struct servent *s;
s = (struct servent *)((uintptr_t)item + sizeof(si_item_t));
serv = strdup(s->s_name);
si_item_release(item);
if (serv == NULL)
{
free(host);
if (err != NULL) *err = SI_STATUS_EAI_MEMORY;
return NULL;
}
}
}
/*
* Return numeric host name for NI_NUMERICHOST or if lookup failed, but not
* if NI_NAMEREQD is specified (so that we later fail with EAI_NONAME).
*/
if ((host == NULL) && ((flags & NI_NAMEREQD) == 0))
{
char tmp[INET6_ADDRSTRLEN + 1 + IF_NAMESIZE + 1];
tmp[0] = '\0';
if (sa->sa_family == AF_INET)
{
char buf[INET_ADDRSTRLEN];
if (inet_ntop(AF_INET, &a4, buf, sizeof(buf)) != 0)
{
host = strdup(buf);
}
}
else if (sa->sa_family == AF_INET6)
{
char buf[INET6_ADDRSTRLEN];
/* zero the embedded scope ID */
if (ifnum != 0)
{
a6.__u6_addr.__u6_addr16[1] = 0;
}
if (inet_ntop(AF_INET6, &a6, buf, sizeof(buf)) != 0)
{
if (ifnum != 0)
{
char ifname[IF_NAMESIZE];
if (if_indextoname(ifnum, ifname) != NULL)
{
asprintf(&host, "%s%%%s", buf, ifname);
}
else
{
/* ENXIO */
if (err != NULL) *err = SI_STATUS_EAI_FAIL;
return NULL;
}
}
else
{
host = strdup(buf);
}
}
}
}
/* Return numeric service name for NI_NUMERICSERV or if lookup failed. */
if (serv == NULL)
{
asprintf(&serv, "%hu", ntohs(port));
}
if ((host == NULL) || (serv == NULL))
{
if (err != NULL)
{
if ((flags & NI_NAMEREQD) != 0)
{
*err = SI_STATUS_EAI_NONAME;
}
else
{
*err = SI_STATUS_EAI_MEMORY;
}
}
}
else
{
out = (si_item_t *)LI_ils_create("L4444ss", (unsigned long)si, CATEGORY_NAMEINFO, 1, unused, unused, host, serv);
}
free(host);
free(serv);
return out;
}
static int
_gai_numericserv(const char *serv, uint16_t *port)
{
int numeric;
char *endptr;
long num;
numeric = 0;
if (serv == NULL)
{
if (port) *port = 0;
numeric = 1;
}
else
{
num = strtol(serv, &endptr, 10);
if ((serv[0] != '\0') && (*endptr == '\0') && (num >= 0) && (num <= UINT16_MAX))
{
numeric = 1;
if (port != NULL) *port = (uint16_t)num;
}
}
return numeric;
}
int
_gai_serv_to_port(const char *serv, uint32_t proto, uint16_t *port)
{
si_item_t *item;
struct servent *s;
const char *protoname = NULL;
if (_gai_numericserv(serv, port)) return 0;
if (proto == IPPROTO_UDP) protoname = "udp";
if (proto == IPPROTO_TCP) protoname = "tcp";
item = si_service_byname(si_search(), serv, protoname);
if (item == NULL) return -1;
s = (struct servent *)((uintptr_t)item + sizeof(si_item_t));
if (port) *port = ntohs(s->s_port);
si_item_release(item);
return 0;
}
si_item_t *
si_addrinfo_v4(si_mod_t *si, int32_t flags, int32_t sock, int32_t proto, uint16_t port, struct in_addr *addr, uint16_t iface, const char *cname)
{
socket_data_t sockdata;
struct sockaddr_in *sa;
int32_t len, v32;
uint32_t unused;
unused = 0;
len = sizeof(struct sockaddr_in);
memset(&sockdata, 0, sizeof(socket_data_t));
sa = (struct sockaddr_in *)&sockdata;
sa->sin_len = len;
sa->sin_family = AF_INET;
sa->sin_port = htons(port);
memcpy(&sa->sin_addr, addr, sizeof(sa->sin_addr));
/* Kludge: Jam the interface number into sin_zero (4 bytes). */
v32 = iface;
memmove(sa->sin_zero, &v32, sizeof(uint32_t));
return (si_item_t *)LI_ils_create("L444444444Ss", (unsigned long)si, CATEGORY_ADDRINFO, 1, unused, unused, flags, AF_INET, sock, proto, len, sockdata, cname);
}
si_item_t *
si_addrinfo_v4_mapped(si_mod_t *si, int32_t flags, int32_t sock, int32_t proto, uint16_t port, struct in_addr *addr, uint16_t iface, const char *cname)
{
socket_data_t sockdata;
struct sockaddr_in6 *sa;
int32_t len;
uint32_t unused;
unused = 0;
len = sizeof(struct sockaddr_in6);
memset(&sockdata, 0, sizeof(socket_data_t));
sa = (struct sockaddr_in6 *)&sockdata;
#ifndef ANDROID
sa->sin6_len = len;
#endif
sa->sin6_family = AF_INET6;
sa->sin6_port = htons(port);
memset(&(sa->sin6_addr.__u6_addr.__u6_addr8[10]), 0xff, 2);
memcpy(&(sa->sin6_addr.__u6_addr.__u6_addr8[12]), addr, sizeof(struct in_addr));
/* sin6_scope_id is in host byte order */
sa->sin6_scope_id = iface;
return (si_item_t *)LI_ils_create("L444444444Ss", (unsigned long)si, CATEGORY_ADDRINFO, 1, unused, unused, flags, AF_INET6, sock, proto, len, sockdata, cname);
}
si_item_t *
si_addrinfo_v6(si_mod_t *si, int32_t flags, int32_t sock, int32_t proto, uint16_t port, struct in6_addr *addr, uint16_t iface, const char *cname)
{
socket_data_t sockdata;
struct sockaddr_in6 *sa;
int32_t len;
uint32_t unused;
unused = 0;
len = sizeof(struct sockaddr_in6);
memset(&sockdata, 0, sizeof(socket_data_t));
sa = (struct sockaddr_in6 *)&sockdata;
#ifndef ANDROID
sa->sin6_len = len;
#endif
sa->sin6_family = AF_INET6;
sa->sin6_port = htons(port);
memcpy(&sa->sin6_addr, addr, sizeof(sa->sin6_addr));
/* sin6_scope_id is in host byte order */
sa->sin6_scope_id = iface;
if (IN6_IS_ADDR_LINKLOCAL(&sa->sin6_addr))
{
/* check for embedded scopeid */
uint16_t esid = ntohs(sa->sin6_addr.__u6_addr.__u6_addr16[1]);
if (esid != 0)
{
sa->sin6_addr.__u6_addr.__u6_addr16[1] = 0;
if (iface == 0) sa->sin6_scope_id = esid;
}
}
return (si_item_t *)LI_ils_create("L444444444Ss", (unsigned long)si, CATEGORY_ADDRINFO, 1, unused, unused, flags, AF_INET6, sock, proto, len, sockdata, cname);
}
si_list_t *
si_addrinfo_list(si_mod_t *si, uint32_t flags, int socktype, int proto, struct in_addr *a4, struct in6_addr *a6, int port, int scopeid, const char *cname4, const char *cname6)
{
int do_map = 0;
si_item_t *item = NULL;
si_list_t *out4 = NULL, *out6 = NULL;
if ((flags & AI_V4MAPPED) && ((flags & AI_ALL) || (a6 == NULL))) do_map = 1;
if (a6 != NULL)
{
if ((proto == IPPROTO_UNSPEC) || (proto == IPPROTO_UDP))
{
item = si_addrinfo_v6(si, 0, SOCK_DGRAM, IPPROTO_UDP, port, a6, scopeid, cname6);
out6 = si_list_add(out6, item);
si_item_release(item);
}
if ((proto == IPPROTO_UNSPEC) || (proto == IPPROTO_TCP))
{
item = si_addrinfo_v6(si, 0, SOCK_STREAM, IPPROTO_TCP, port, a6, scopeid, cname6);
out6 = si_list_add(out6, item);
si_item_release(item);
}
if (proto == IPPROTO_ICMPV6)
{
item = si_addrinfo_v6(si, 0, SOCK_RAW, IPPROTO_ICMPV6, port, a6, scopeid, cname6);
out6 = si_list_add(out6, item);
si_item_release(item);
}
}
if (a4 != NULL)
{
if ((proto == IPPROTO_UNSPEC) || (proto == IPPROTO_UDP))
{
if (do_map == 0)
{
item = si_addrinfo_v4(si, 0, SOCK_DGRAM, IPPROTO_UDP, port, a4, 0, cname4);
out4 = si_list_add(out4, item);
}
else
{
item = si_addrinfo_v4_mapped(si, 0, SOCK_DGRAM, IPPROTO_UDP, port, a4, 0, cname4);
out6 = si_list_add(out6, item);
}
si_item_release(item);
}
if ((proto == IPPROTO_UNSPEC) || (proto == IPPROTO_TCP))
{
if (do_map == 0)
{
item = si_addrinfo_v4(si, 0, SOCK_STREAM, IPPROTO_TCP, port, a4, 0, cname4);
out4 = si_list_add(out4, item);
}
else
{
item = si_addrinfo_v4_mapped(si, 0, SOCK_STREAM, IPPROTO_TCP, port, a4, 0, cname4);
out6 = si_list_add(out6, item);
}
si_item_release(item);
}
if (proto == IPPROTO_ICMP)
{
if (do_map == 0)
{
item = si_addrinfo_v4(si, 0, SOCK_RAW, IPPROTO_ICMP, port, a4, 0, cname4);
out4 = si_list_add(out4, item);
}
else
{
item = si_addrinfo_v4_mapped(si, 0, SOCK_RAW, IPPROTO_ICMP, port, a4, 0, cname4);
out6 = si_list_add(out6, item);
}
si_item_release(item);
}
}
out6 = si_list_concat(out6, out4);
si_list_release(out4);
return out6;
}
/*
* _gai_numerichost
* Determines whether the given host name is a numeric IPv4 or IPv6 address,
* based on the address family input value. If the input addres family is
* unspecified, a more specific value will be provided on output if possible.
* Returns 1 if host name is numeric or 0 if not, or -1 on error.
*/
static int
_gai_numerichost(const char* nodename, uint32_t *family, int flags, struct in_addr *a4, struct in6_addr *a6, int *scope)
{
int numerichost, passive;
numerichost = 0;
if (nodename == NULL)
{
/* return loopback or passive addresses */
passive = (flags & AI_PASSIVE);
if (((*family == AF_UNSPEC) || (*family == AF_INET)) || ((*family == AF_INET6) && (flags & AI_V4MAPPED) && (flags & AI_ALL)))
{
if (passive) a4->s_addr = 0;
else a4->s_addr = htonl(INADDR_LOOPBACK);
}
if ((*family == AF_UNSPEC) || (*family == AF_INET6))
{
memset(a6, 0, sizeof(*a6));
if (!passive) a6->__u6_addr.__u6_addr32[3] = htonl(1);
}
numerichost = 1;
}
else
{
/*
* numeric IPv4 host valid for AF_UNSPEC and AF_INET
* also valid for AF_INET6 with AI_V4MAPPED
*/
numerichost = inet_pton(AF_INET, nodename, a4);
if (numerichost == 0)
{
/* inet_pton doesn't allow "a", "a.b", or "a.b.c" forms, so we re-check */
numerichost = _inet_aton_check(nodename, a4, 1);
}
if (numerichost == 1)
{
if (*family == AF_UNSPEC)
{
*family = AF_INET;
}
else if (*family == AF_INET6)
{
if (flags & AI_V4MAPPED)
{
memset(a6, 0, sizeof(struct in6_addr));
memset(&(a6->__u6_addr.__u6_addr8[10]), 0xff, 2);
memcpy(&(a6->__u6_addr.__u6_addr8[12]), a4, sizeof(struct in_addr));
}
else
{
numerichost = -1;
}
}
return numerichost;
}
/* numeric IPv6 host valid for AF_UNSPEC and AF_INET6 */
numerichost = inet_pton(AF_INET6, nodename, a6);
if (numerichost == 1)
{
/* check for scope/zone id */
char *p = strrchr(nodename, SCOPE_DELIMITER);
if (p != NULL)
{
int i, d;
char *x;
p++;
d = 1;
for (x = p; (*x != '\0') && (d == 1); x++)
{
i = *x;
d = isdigit(i);
}
if (d == 1) *scope = atoi(p);
else *scope = if_nametoindex(p);
}
if (*family == AF_UNSPEC) *family = AF_INET6;
else if (*family == AF_INET) numerichost = -1;
return numerichost;
}
}
return numerichost;
}
/* si_addrinfo_list_from_hostent
* Returns an addrinfo list from IPv4 and IPv6 hostent entries
*/
si_list_t *
si_addrinfo_list_from_hostent(si_mod_t *si, uint32_t flags, uint32_t socktype, uint32_t proto, uint16_t port, uint16_t scope, const struct hostent *h4, const struct hostent *h6)
{
int i;
si_list_t *out = NULL;
si_list_t *list;
if ((h6 != NULL) && (h6->h_addr_list != NULL))
{
for (i = 0; h6->h_addr_list[i] != NULL; i++)
{
struct in6_addr a6;
memcpy(&a6, h6->h_addr_list[i], h6->h_length);
list = si_addrinfo_list(si, flags, socktype, proto, NULL, &a6, port, scope, NULL, h6->h_name);
out = si_list_concat(out, list);
si_list_release(list);
}
}
if ((h4 != NULL) && (h4->h_addr_list != NULL))
{
for (i = 0; h4->h_addr_list[i] != NULL; i++)
{
struct in_addr a4;
memcpy(&a4, h4->h_addr_list[i], h4->h_length);
list = si_addrinfo_list(si, flags, socktype, proto, &a4, NULL, port, 0, h4->h_name, NULL);
out = si_list_concat(out, list);
si_list_release(list);
}
}
return out;
}
int
_gai_addr_sort(const void *a, const void *b)
{
si_item_t **item_a, **item_b;
si_addrinfo_t *p, *q;
struct sockaddr *sp, *sq;
item_a = (si_item_t **)a;
item_b = (si_item_t **)b;
p = (si_addrinfo_t *)((uintptr_t)*item_a + sizeof(si_item_t));
q = (si_addrinfo_t *)((uintptr_t)*item_b + sizeof(si_item_t));
sp = (struct sockaddr *)p->ai_addr.x;
sq = (struct sockaddr *)q->ai_addr.x;
/*
* sa_dst_compare(A,B) returns -1 if A is less desirable than B,
* 0 if they are equally desirable, and 1 if A is more desirable.
* qsort() expects the inverse, so we swap sp and sq.
*/
#ifdef APPORTABLE
#warning TODO: FIXME
// this is the worst idea I have written down in code in many months...
if (sq == sp)
{
return 0;
}
else if ((uintptr_t)sq < (uintptr_t)sp)
{
return -1;
}
else
{
return 1;
}
#else
return sa_dst_compare(sq, sp, 0);
#endif
}
static si_list_t *
_gai_sort_list(si_list_t *in, uint32_t flags)
{
si_list_t *out;
int filter_mapped;
uint32_t i;
uint32_t v4mapped_count = 0;
uint32_t v6_count = 0;
si_addrinfo_t *a;
if (in == NULL) return NULL;
for (i = 0; i < in->count; i++)
{
a = (si_addrinfo_t *)((uintptr_t)in->entry[i] + sizeof(si_item_t));
if (a->ai_family == AF_INET6)
{
struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)a->ai_addr.x;
if (IN6_IS_ADDR_V4MAPPED(&(s6->sin6_addr))) v4mapped_count++;
else v6_count++;
}
}
filter_mapped = 1;
if ((flags & AI_V4MAPPED) && ((v6_count == 0) || (flags & AI_ALL))) filter_mapped = 0;
out = in;
if ((filter_mapped == 1) && (v4mapped_count > 0))
{
i = in->count - v4mapped_count;
if (i == 0) return NULL;
out = (si_list_t *)calloc(1, sizeof(si_list_t));
if (out == NULL) return in;
out->count = i;
out->refcount = in->refcount;
out->entry = (si_item_t **)calloc(out->count, sizeof(si_item_t *));
if (out->entry == NULL)
{
free(out);
return in;
}
out->curr = 0;
for (i = 0; i < in->count; i++)
{
a = (si_addrinfo_t *)((uintptr_t)in->entry[i] + sizeof(si_item_t));
if (a->ai_family == AF_INET6)
{
struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)a->ai_addr.x;
if (IN6_IS_ADDR_V4MAPPED(&(s6->sin6_addr)))
{
si_item_release(in->entry[i]);
continue;
}
}
out->entry[out->curr++] = in->entry[i];
}
out->curr = 0;
free(in->entry);
free(in);
}
qsort(&out->entry[0], out->count, sizeof(si_item_t *), _gai_addr_sort);
return out;
}
/* _gai_simple
* Simple lookup via gethostbyname2(3) mechanism.
*/
si_list_t *
_gai_simple(si_mod_t *si, const void *nodeptr, const void *servptr, uint32_t family, uint32_t socktype, uint32_t proto, uint32_t flags, const char *interface, uint32_t *err)
{
si_item_t *h4_item = NULL, *h6_item = NULL;
struct hostent *h4 = NULL, *h6 = NULL;
si_list_t *out = NULL;
uint16_t port = 0;
if ((flags & AI_NUMERICSERV) != 0 && servptr != NULL)
{
port = *(uint16_t*)servptr;
}
else if (servptr != NULL)
{
if (_gai_serv_to_port(servptr, proto, &port) != 0)
{
if (err) *err = SI_STATUS_EAI_NONAME;
return NULL;
}
}
if ((flags & AI_NUMERICHOST) != 0)
{
if (family == AF_INET)
{
h4_item = si_host_byaddr(si, nodeptr, AF_INET, interface, NULL);
}
else if (family == AF_INET6)
{
h6_item = si_host_byaddr(si, nodeptr, AF_INET6, interface, NULL);
}
}
else
{
if ((family == AF_INET) || (family == AF_UNSPEC))
{
h4_item = si_host_byname(si, nodeptr, AF_INET, interface, NULL);
}
if ((family == AF_INET6) || (family == AF_UNSPEC))
{
h6_item = si_host_byname(si, nodeptr, AF_INET6, interface, NULL);
}
}
if (h4_item != NULL)
{
h4 = (struct hostent *)((uintptr_t)h4_item + sizeof(si_item_t));
}
if (h6_item != NULL)
{
h6 = (struct hostent *)((uintptr_t)h6_item + sizeof(si_item_t));
}
out = si_addrinfo_list_from_hostent(si, flags, socktype, proto, port, 0, h4, h6);
si_item_release(h4_item);
si_item_release(h6_item);
return _gai_sort_list(out, flags);
}
si_list_t *
si_srv_byname(si_mod_t *si, const char *qname, const char *interface, uint32_t *err)
{
if (si == NULL) return 0;
if (si->vtable->sim_srv_byname == NULL) return 0;
return si->vtable->sim_srv_byname(si, qname, interface, err);
}
int
si_wants_addrinfo(si_mod_t *si)
{
if (si == NULL) return 0;
if (si->vtable->sim_addrinfo == NULL) return 0;
if (si->vtable->sim_wants_addrinfo == NULL) return 0;
return si->vtable->sim_wants_addrinfo(si);
}
static si_list_t *
_gai_srv(si_mod_t *si, const char *node, const char *serv, uint32_t family, uint32_t socktype, uint32_t proto, uint32_t flags, const char *interface, uint32_t *err)
{
int i;
char *qname;
si_srv_t *srv;
si_item_t *item;
si_list_t *list = NULL;
si_list_t *result = NULL;
/* Minimum SRV priority is zero. Start below that. */
int lastprio = -1;
int currprio;
if (node == NULL || serv == NULL) return NULL;
asprintf(&qname, "%s.%s", serv, node);
list = si_srv_byname(si, qname, interface, err);
free(qname);
/* Iterate the SRV records starting at lowest priority and attempt to
* lookup the target host name. Returns the first successful lookup.
* It's an O(n^2) algorithm but data sets are small (less than 100) and
* sorting overhead is dwarfed by network I/O for each element.
*/
while (list != NULL && result == NULL)
{
/* Find the next lowest priority level. */
/* Maximum SRV priority is UINT16_MAX. Start above that. */
currprio = INT_MAX;
for (i = 0; i < list->count; ++i)
{
item = list->entry[i];
srv = (si_srv_t *)((uintptr_t)item + sizeof(si_item_t));
if (srv->priority > lastprio && srv->priority < currprio)
{
currprio = srv->priority;
}
}
if (currprio == INT_MAX)
{
/* All priorities have been evaluated. Done. */