-
Notifications
You must be signed in to change notification settings - Fork 0
/
getaddrinfo.cpp
1856 lines (1618 loc) · 60.2 KB
/
getaddrinfo.cpp
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
/* $NetBSD: getaddrinfo.c,v 1.82 2006/03/25 12:09:40 rpaulo Exp $ */
/* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* 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.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT 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 PROJECT 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.
*/
#define LOG_TAG "resolv"
#include "getaddrinfo.h"
#include <arpa/inet.h>
#include <arpa/nameser.h>
#include <assert.h>
#include <ctype.h>
#include <fcntl.h>
#include <net/if.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
#include <chrono>
#include <future>
#include <android-base/logging.h>
#include <android-base/parseint.h>
#include "Experiments.h"
#include "netd_resolv/resolv.h"
#include "res_comp.h"
#include "res_debug.h"
#include "resolv_cache.h"
#include "resolv_private.h"
#define ANY 0
using android::net::Experiments;
using android::net::NetworkDnsEventReported;
const char in_addrany[] = {0, 0, 0, 0};
const char in_loopback[] = {127, 0, 0, 1};
const char in6_addrany[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
const char in6_loopback[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
const struct afd {
int a_af;
int a_addrlen;
int a_socklen;
int a_off;
const char* a_addrany;
const char* a_loopback;
int a_scoped;
} afdl[] = {
{PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6),
offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1},
{PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in),
offsetof(struct sockaddr_in, sin_addr), in_addrany, in_loopback, 0},
{0, 0, 0, 0, NULL, NULL, 0},
};
struct Explore {
int e_af;
int e_socktype;
int e_protocol;
int e_wild;
#define WILD_AF(ex) ((ex).e_wild & 0x01)
#define WILD_SOCKTYPE(ex) ((ex).e_wild & 0x02)
#define WILD_PROTOCOL(ex) ((ex).e_wild & 0x04)
};
const Explore explore_options[] = {
{PF_INET6, SOCK_DGRAM, IPPROTO_UDP, 0x07},
{PF_INET6, SOCK_STREAM, IPPROTO_TCP, 0x07},
{PF_INET6, SOCK_RAW, ANY, 0x05},
{PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0x07},
{PF_INET, SOCK_STREAM, IPPROTO_TCP, 0x07},
{PF_INET, SOCK_RAW, ANY, 0x05},
{PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, 0x07},
{PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0x07},
{PF_UNSPEC, SOCK_RAW, ANY, 0x05},
};
#define PTON_MAX 16
struct res_target {
struct res_target* next;
const char* name; // domain name
int qclass, qtype; // class and type of query
std::vector<uint8_t> answer = std::vector<uint8_t>(MAXPACKET, 0); // buffer to put answer
int n = 0; // result length
};
static int explore_fqdn(const struct addrinfo*, const char*, const char*, struct addrinfo**,
const struct android_net_context*, NetworkDnsEventReported* event);
static int explore_null(const struct addrinfo*, const char*, struct addrinfo**);
static int explore_numeric(const struct addrinfo*, const char*, const char*, struct addrinfo**,
const char*);
static int explore_numeric_scope(const struct addrinfo*, const char*, const char*,
struct addrinfo**);
static int get_canonname(const struct addrinfo*, struct addrinfo*, const char*);
static struct addrinfo* get_ai(const struct addrinfo*, const struct afd*, const char*);
static int get_portmatch(const struct addrinfo*, const char*);
static int get_port(const struct addrinfo*, const char*, int);
static const struct afd* find_afd(int);
static int ip6_str2scopeid(const char*, struct sockaddr_in6*, uint32_t*);
static struct addrinfo* getanswer(const std::vector<uint8_t>&, int, const char*, int,
const struct addrinfo*, int* herrno);
static int dns_getaddrinfo(const char* name, const addrinfo* pai,
const android_net_context* netcontext, addrinfo** rv,
NetworkDnsEventReported* event);
static void _sethtent(FILE**);
static void _endhtent(FILE**);
static struct addrinfo* _gethtent(FILE**, const char*, const struct addrinfo*);
static struct addrinfo* getCustomHosts(const size_t netid, const char*, const struct addrinfo*);
static bool files_getaddrinfo(const size_t netid, const char* name, const addrinfo* pai,
addrinfo** res);
static int _find_src_addr(const struct sockaddr*, struct sockaddr*, unsigned, uid_t,
bool allow_v6_linklocal);
static int res_searchN(const char* name, res_target* target, ResState* res, int* herrno);
static int res_querydomainN(const char* name, const char* domain, res_target* target, ResState* res,
int* herrno);
const char* const ai_errlist[] = {
"Success",
"Address family for hostname not supported", /* EAI_ADDRFAMILY */
"Temporary failure in name resolution", /* EAI_AGAIN */
"Invalid value for ai_flags", /* EAI_BADFLAGS */
"Non-recoverable failure in name resolution", /* EAI_FAIL */
"ai_family not supported", /* EAI_FAMILY */
"Memory allocation failure", /* EAI_MEMORY */
"No address associated with hostname", /* EAI_NODATA */
"hostname nor servname provided, or not known", /* EAI_NONAME */
"servname not supported for ai_socktype", /* EAI_SERVICE */
"ai_socktype not supported", /* EAI_SOCKTYPE */
"System error returned in errno", /* EAI_SYSTEM */
"Invalid value for hints", /* EAI_BADHINTS */
"Resolved protocol is unknown", /* EAI_PROTOCOL */
"Argument buffer overflow", /* EAI_OVERFLOW */
"Unknown error", /* EAI_MAX */
};
/* XXX macros that make external reference is BAD. */
#define GET_AI(ai, afd, addr) \
do { \
/* external reference: pai, error, and label free */ \
(ai) = get_ai(pai, (afd), (addr)); \
if ((ai) == NULL) { \
error = EAI_MEMORY; \
goto free; \
} \
} while (0)
#define GET_PORT(ai, serv) \
do { \
/* external reference: error and label free */ \
error = get_port((ai), (serv), 0); \
if (error != 0) goto free; \
} while (0)
#define MATCH_FAMILY(x, y, w) \
((x) == (y) || ((w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC)))
#define MATCH(x, y, w) ((x) == (y) || ((w) && ((x) == ANY || (y) == ANY)))
const char* gai_strerror(int ecode) {
if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX;
return ai_errlist[ecode];
}
void freeaddrinfo(struct addrinfo* ai) {
while (ai) {
struct addrinfo* next = ai->ai_next;
if (ai->ai_canonname) free(ai->ai_canonname);
// Also frees ai->ai_addr which points to extra space beyond addrinfo
free(ai);
ai = next;
}
}
/*
* The following functions determine whether IPv4 or IPv6 connectivity is
* available in order to implement AI_ADDRCONFIG.
*
* Strictly speaking, AI_ADDRCONFIG should not look at whether connectivity is
* available, but whether addresses of the specified family are "configured
* on the local system". However, bionic doesn't currently support getifaddrs,
* so checking for connectivity is the next best thing.
*/
static int have_ipv6(unsigned mark, uid_t uid, bool mdns) {
static const struct sockaddr_in6 sin6_test = {
.sin6_family = AF_INET6,
.sin6_addr.s6_addr = {// 2000::
0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
sockaddr_union addr = {.sin6 = sin6_test};
sockaddr sa;
return _find_src_addr(&addr.sa, &sa, mark, uid, /*allow_v6_linklocal=*/mdns) == 1;
}
static int have_ipv4(unsigned mark, uid_t uid) {
static const struct sockaddr_in sin_test = {
.sin_family = AF_INET,
.sin_addr.s_addr = __constant_htonl(0x08080808L) // 8.8.8.8
};
sockaddr_union addr = {.sin = sin_test};
sockaddr sa;
return _find_src_addr(&addr.sa, &sa, mark, uid, /*(don't care) allow_v6_linklocal=*/false) == 1;
}
// Internal version of getaddrinfo(), but limited to AI_NUMERICHOST.
// NOTE: also called by resolv_set_nameservers().
int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
addrinfo** result) {
hints.ai_flags = AI_NUMERICHOST;
const android_net_context netcontext = {
.app_netid = NETID_UNSET,
.app_mark = MARK_UNSET,
.dns_netid = NETID_UNSET,
.dns_mark = MARK_UNSET,
.uid = NET_CONTEXT_INVALID_UID,
.pid = NET_CONTEXT_INVALID_PID,
};
NetworkDnsEventReported event;
return android_getaddrinfofornetcontext(hostname, servname, &hints, &netcontext, result,
&event);
}
namespace {
int validateHints(const addrinfo* _Nonnull hints) {
if (!hints) return EAI_BADHINTS;
// error check for hints
if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) {
return EAI_BADHINTS;
}
if (hints->ai_flags & ~AI_MASK) {
return EAI_BADFLAGS;
}
if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET ||
hints->ai_family == PF_INET6)) {
return EAI_FAMILY;
}
// Socket types which are not in explore_options.
switch (hints->ai_socktype) {
case SOCK_RAW:
case SOCK_DGRAM:
case SOCK_STREAM:
case ANY:
break;
default:
return EAI_SOCKTYPE;
}
if (hints->ai_socktype == ANY || hints->ai_protocol == ANY) return 0;
// if both socktype/protocol are specified, check if they are meaningful combination.
for (const Explore& ex : explore_options) {
if (hints->ai_family != ex.e_af) continue;
if (ex.e_socktype == ANY) continue;
if (ex.e_protocol == ANY) continue;
if (hints->ai_socktype == ex.e_socktype && hints->ai_protocol != ex.e_protocol) {
return EAI_BADHINTS;
}
}
return 0;
}
} // namespace
int android_getaddrinfofornetcontext(const char* hostname, const char* servname,
const addrinfo* hints, const android_net_context* netcontext,
addrinfo** res, NetworkDnsEventReported* event) {
// hostname is allowed to be nullptr
// servname is allowed to be nullptr
// hints is allowed to be nullptr
assert(res != nullptr);
assert(netcontext != nullptr);
assert(event != nullptr);
addrinfo sentinel = {};
addrinfo* cur = &sentinel;
int error = 0;
do {
if (hostname == nullptr && servname == nullptr) {
error = EAI_NONAME;
break;
}
if (hints && (error = validateHints(hints))) break;
addrinfo ai = hints ? *hints : addrinfo{};
// Check for special cases:
// (1) numeric servname is disallowed if socktype/protocol are left unspecified.
// (2) servname is disallowed for raw and other inet{,6} sockets.
if (MATCH_FAMILY(ai.ai_family, PF_INET, 1) || MATCH_FAMILY(ai.ai_family, PF_INET6, 1)) {
addrinfo tmp = ai;
if (tmp.ai_family == PF_UNSPEC) {
tmp.ai_family = PF_INET6;
}
error = get_portmatch(&tmp, servname);
if (error) break;
}
// NULL hostname, or numeric hostname
for (const Explore& ex : explore_options) {
/* PF_UNSPEC entries are prepared for DNS queries only */
if (ex.e_af == PF_UNSPEC) continue;
if (!MATCH_FAMILY(ai.ai_family, ex.e_af, WILD_AF(ex))) continue;
if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) continue;
if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) continue;
addrinfo tmp = ai;
if (tmp.ai_family == PF_UNSPEC) tmp.ai_family = ex.e_af;
if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype;
if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol;
LOG(DEBUG) << __func__ << ": explore_numeric: ai_family=" << tmp.ai_family
<< " ai_socktype=" << tmp.ai_socktype << " ai_protocol=" << tmp.ai_protocol;
if (hostname == nullptr)
error = explore_null(&tmp, servname, &cur->ai_next);
else
error = explore_numeric_scope(&tmp, hostname, servname, &cur->ai_next);
if (error) break;
while (cur->ai_next) cur = cur->ai_next;
}
if (error) break;
// If numeric representation of AF1 can be interpreted as FQDN
// representation of AF2, we need to think again about the code below.
if (sentinel.ai_next) break;
if (hostname == nullptr) {
error = EAI_NODATA;
break;
}
if (ai.ai_flags & AI_NUMERICHOST) {
error = EAI_NONAME;
break;
}
return resolv_getaddrinfo(hostname, servname, hints, netcontext, res, event);
} while (0);
if (error) {
freeaddrinfo(sentinel.ai_next);
*res = nullptr;
} else {
*res = sentinel.ai_next;
}
return error;
}
int resolv_getaddrinfo(const char* _Nonnull hostname, const char* servname, const addrinfo* hints,
const android_net_context* _Nonnull netcontext, addrinfo** _Nonnull res,
NetworkDnsEventReported* _Nonnull event) {
if (hostname == nullptr && servname == nullptr) return EAI_NONAME;
if (hostname == nullptr) return EAI_NODATA;
// servname is allowed to be nullptr
// hints is allowed to be nullptr
assert(res != nullptr);
assert(netcontext != nullptr);
assert(event != nullptr);
int error = EAI_FAIL;
if (hints && (error = validateHints(hints))) {
*res = nullptr;
return error;
}
addrinfo ai = hints ? *hints : addrinfo{};
addrinfo sentinel = {};
addrinfo* cur = &sentinel;
// hostname as alphanumeric name.
// We would like to prefer AF_INET6 over AF_INET, so we'll make a outer loop by AFs.
for (const Explore& ex : explore_options) {
// Require exact match for family field
if (ai.ai_family != ex.e_af) continue;
if (!MATCH(ai.ai_socktype, ex.e_socktype, WILD_SOCKTYPE(ex))) continue;
if (!MATCH(ai.ai_protocol, ex.e_protocol, WILD_PROTOCOL(ex))) continue;
addrinfo tmp = ai;
if (tmp.ai_socktype == ANY && ex.e_socktype != ANY) tmp.ai_socktype = ex.e_socktype;
if (tmp.ai_protocol == ANY && ex.e_protocol != ANY) tmp.ai_protocol = ex.e_protocol;
LOG(DEBUG) << __func__ << ": explore_fqdn(): ai_family=" << tmp.ai_family
<< " ai_socktype=" << tmp.ai_socktype << " ai_protocol=" << tmp.ai_protocol;
error = explore_fqdn(&tmp, hostname, servname, &cur->ai_next, netcontext, event);
while (cur->ai_next) cur = cur->ai_next;
}
// Propagate the last error from explore_fqdn(), but only when *all* attempts failed.
if ((*res = sentinel.ai_next)) return 0;
// TODO: consider removing freeaddrinfo.
freeaddrinfo(sentinel.ai_next);
*res = nullptr;
return (error == 0) ? EAI_FAIL : error;
}
// FQDN hostname, DNS lookup
static int explore_fqdn(const addrinfo* pai, const char* hostname, const char* servname,
addrinfo** res, const android_net_context* netcontext,
NetworkDnsEventReported* event) {
assert(pai != nullptr);
// hostname may be nullptr
// servname may be nullptr
assert(res != nullptr);
addrinfo* result = nullptr;
int error = 0;
// If the servname does not match socktype/protocol, return error code.
if ((error = get_portmatch(pai, servname))) return error;
if (!files_getaddrinfo(netcontext->dns_netid, hostname, pai, &result)) {
error = dns_getaddrinfo(hostname, pai, netcontext, &result, event);
}
if (error) {
freeaddrinfo(result);
return error;
}
for (addrinfo* cur = result; cur; cur = cur->ai_next) {
// canonname should be filled already
if ((error = get_port(cur, servname, 0))) {
freeaddrinfo(result);
return error;
}
}
*res = result;
return 0;
}
/*
* hostname == NULL.
* passive socket -> anyaddr (0.0.0.0 or ::)
* non-passive socket -> localhost (127.0.0.1 or ::1)
*/
static int explore_null(const struct addrinfo* pai, const char* servname, struct addrinfo** res) {
int s;
const struct afd* afd;
struct addrinfo* cur;
struct addrinfo sentinel;
int error;
LOG(DEBUG) << __func__;
assert(pai != NULL);
/* servname may be NULL */
assert(res != NULL);
*res = NULL;
sentinel.ai_next = NULL;
cur = &sentinel;
/*
* filter out AFs that are not supported by the kernel
* XXX errno?
*/
s = socket(pai->ai_family, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (s < 0) {
if (errno != EMFILE) return 0;
} else
close(s);
/*
* if the servname does not match socktype/protocol, ignore it.
*/
if (get_portmatch(pai, servname) != 0) return 0;
afd = find_afd(pai->ai_family);
if (afd == NULL) return 0;
if (pai->ai_flags & AI_PASSIVE) {
GET_AI(cur->ai_next, afd, afd->a_addrany);
GET_PORT(cur->ai_next, servname);
} else {
GET_AI(cur->ai_next, afd, afd->a_loopback);
GET_PORT(cur->ai_next, servname);
}
cur = cur->ai_next;
*res = sentinel.ai_next;
return 0;
free:
freeaddrinfo(sentinel.ai_next);
return error;
}
/*
* numeric hostname
*/
static int explore_numeric(const struct addrinfo* pai, const char* hostname, const char* servname,
struct addrinfo** res, const char* canonname) {
const struct afd* afd;
struct addrinfo* cur;
struct addrinfo sentinel;
int error;
char pton[PTON_MAX];
assert(pai != NULL);
/* hostname may be NULL */
/* servname may be NULL */
assert(res != NULL);
*res = NULL;
sentinel.ai_next = NULL;
cur = &sentinel;
/*
* if the servname does not match socktype/protocol, ignore it.
*/
if (get_portmatch(pai, servname) != 0) return 0;
afd = find_afd(pai->ai_family);
if (afd == NULL) return 0;
if (inet_pton(afd->a_af, hostname, pton) == 1) {
if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) {
GET_AI(cur->ai_next, afd, pton);
GET_PORT(cur->ai_next, servname);
if ((pai->ai_flags & AI_CANONNAME)) {
/*
* Set the numeric address itself as
* the canonical name, based on a
* clarification in rfc2553bis-03.
*/
error = get_canonname(pai, cur->ai_next, canonname);
if (error != 0) {
freeaddrinfo(sentinel.ai_next);
return error;
}
}
while (cur->ai_next) cur = cur->ai_next;
} else
return EAI_FAMILY;
}
*res = sentinel.ai_next;
return 0;
free:
freeaddrinfo(sentinel.ai_next);
return error;
}
/*
* numeric hostname with scope
*/
static int explore_numeric_scope(const struct addrinfo* pai, const char* hostname,
const char* servname, struct addrinfo** res) {
const struct afd* afd;
struct addrinfo* cur;
int error;
const char *cp, *scope, *addr;
struct sockaddr_in6* sin6;
LOG(DEBUG) << __func__;
assert(pai != NULL);
/* hostname may be NULL */
/* servname may be NULL */
assert(res != NULL);
/*
* if the servname does not match socktype/protocol, ignore it.
*/
if (get_portmatch(pai, servname) != 0) return 0;
afd = find_afd(pai->ai_family);
if (afd == NULL) return 0;
if (!afd->a_scoped) return explore_numeric(pai, hostname, servname, res, hostname);
cp = strchr(hostname, SCOPE_DELIMITER);
if (cp == NULL) return explore_numeric(pai, hostname, servname, res, hostname);
/*
* Handle special case of <scoped_address><delimiter><scope id>
*/
char* hostname2 = strdup(hostname);
if (hostname2 == NULL) return EAI_MEMORY;
/* terminate at the delimiter */
hostname2[cp - hostname] = '\0';
addr = hostname2;
scope = cp + 1;
error = explore_numeric(pai, addr, servname, res, hostname);
if (error == 0) {
uint32_t scopeid;
for (cur = *res; cur; cur = cur->ai_next) {
if (cur->ai_family != AF_INET6) continue;
sin6 = (struct sockaddr_in6*) (void*) cur->ai_addr;
if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) {
free(hostname2);
return (EAI_NODATA); /* XXX: is return OK? */
}
sin6->sin6_scope_id = scopeid;
}
}
free(hostname2);
return error;
}
static int get_canonname(const struct addrinfo* pai, struct addrinfo* ai, const char* str) {
assert(pai != NULL);
assert(ai != NULL);
assert(str != NULL);
if ((pai->ai_flags & AI_CANONNAME) != 0) {
ai->ai_canonname = strdup(str);
if (ai->ai_canonname == NULL) return EAI_MEMORY;
}
return 0;
}
static struct addrinfo* get_ai(const struct addrinfo* pai, const struct afd* afd,
const char* addr) {
char* p;
struct addrinfo* ai;
assert(pai != NULL);
assert(afd != NULL);
assert(addr != NULL);
ai = (struct addrinfo*) calloc(1, sizeof(struct addrinfo) + sizeof(sockaddr_union));
if (ai == NULL) return NULL;
memcpy(ai, pai, sizeof(struct addrinfo));
ai->ai_addr = (struct sockaddr*) (void*) (ai + 1);
ai->ai_addrlen = afd->a_socklen;
ai->ai_addr->sa_family = ai->ai_family = afd->a_af;
p = (char*) (void*) (ai->ai_addr);
memcpy(p + afd->a_off, addr, (size_t) afd->a_addrlen);
return ai;
}
static int get_portmatch(const struct addrinfo* ai, const char* servname) {
assert(ai != NULL);
/* servname may be NULL */
return get_port(ai, servname, 1);
}
static int get_port(const struct addrinfo* ai, const char* servname, int matchonly) {
const char* proto;
struct servent* sp;
uint port;
int allownumeric;
assert(ai != NULL);
/* servname may be NULL */
if (servname == NULL) return 0;
switch (ai->ai_family) {
case AF_INET:
case AF_INET6:
break;
default:
return 0;
}
switch (ai->ai_socktype) {
case SOCK_RAW:
return EAI_SERVICE;
case SOCK_DGRAM:
case SOCK_STREAM:
case ANY:
allownumeric = 1;
break;
default:
return EAI_SOCKTYPE;
}
if (android::base::ParseUint(servname, &port)) {
if (!allownumeric) return EAI_SERVICE;
if (port > 65535) return EAI_SERVICE;
port = htons(port);
} else {
if (ai->ai_flags & AI_NUMERICSERV) return EAI_NONAME;
switch (ai->ai_socktype) {
case SOCK_DGRAM:
proto = "udp";
break;
case SOCK_STREAM:
proto = "tcp";
break;
default:
proto = NULL;
break;
}
if ((sp = getservbyname(servname, proto)) == NULL) return EAI_SERVICE;
port = sp->s_port;
}
if (!matchonly) {
switch (ai->ai_family) {
case AF_INET:
((struct sockaddr_in*) (void*) ai->ai_addr)->sin_port = port;
break;
case AF_INET6:
((struct sockaddr_in6*) (void*) ai->ai_addr)->sin6_port = port;
break;
}
}
return 0;
}
static const struct afd* find_afd(int af) {
const struct afd* afd;
if (af == PF_UNSPEC) return NULL;
for (afd = afdl; afd->a_af; afd++) {
if (afd->a_af == af) return afd;
}
return NULL;
}
// Convert a string to a scope identifier.
static int ip6_str2scopeid(const char* scope, struct sockaddr_in6* sin6, uint32_t* scopeid) {
struct in6_addr* a6;
assert(scope != NULL);
assert(sin6 != NULL);
assert(scopeid != NULL);
a6 = &sin6->sin6_addr;
/* empty scopeid portion is invalid */
if (*scope == '\0') return -1;
if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) {
/*
* We currently assume a one-to-one mapping between links
* and interfaces, so we simply use interface indices for
* like-local scopes.
*/
*scopeid = if_nametoindex(scope);
if (*scopeid != 0) return 0;
}
/* try to convert to a numeric id as a last resort*/
if (!android::base::ParseUint(scope, scopeid)) return -1;
return 0;
}
/* code duplicate with gethnamaddr.c */
#define BOUNDED_INCR(x) \
do { \
BOUNDS_CHECK(cp, x); \
cp += (x); \
} while (0)
#define BOUNDS_CHECK(ptr, count) \
do { \
if (eom - (ptr) < (count)) { \
*herrno = NO_RECOVERY; \
return NULL; \
} \
} while (0)
static struct addrinfo* getanswer(const std::vector<uint8_t>& answer, int anslen, const char* qname,
int qtype, const struct addrinfo* pai, int* herrno) {
struct addrinfo sentinel = {};
struct addrinfo *cur;
struct addrinfo ai;
const struct afd* afd;
char* canonname;
const HEADER* hp;
const uint8_t* cp;
int n;
const uint8_t* eom;
char *bp, *ep;
int type, ancount, qdcount;
int haveanswer, had_error;
char tbuf[MAXDNAME];
char hostbuf[8 * 1024];
assert(qname != NULL);
assert(pai != NULL);
cur = &sentinel;
canonname = NULL;
eom = answer.data() + anslen;
bool (*name_ok)(const char* dn);
switch (qtype) {
case T_A:
case T_AAAA:
case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/
name_ok = res_hnok;
break;
default:
return NULL; /* XXX should be abort(); */
}
/*
* find first satisfactory answer
*/
hp = reinterpret_cast<const HEADER*>(answer.data());
ancount = ntohs(hp->ancount);
qdcount = ntohs(hp->qdcount);
bp = hostbuf;
ep = hostbuf + sizeof hostbuf;
cp = answer.data();
BOUNDED_INCR(HFIXEDSZ);
if (qdcount != 1) {
*herrno = NO_RECOVERY;
return (NULL);
}
n = dn_expand(answer.data(), eom, cp, bp, ep - bp);
if ((n < 0) || !(*name_ok)(bp)) {
*herrno = NO_RECOVERY;
return (NULL);
}
BOUNDED_INCR(n + QFIXEDSZ);
if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) {
/* res_send() has already verified that the query name is the
* same as the one we sent; this just gets the expanded name
* (i.e., with the succeeding search-domain tacked on).
*/
n = strlen(bp) + 1; /* for the \0 */
if (n >= MAXHOSTNAMELEN) {
*herrno = NO_RECOVERY;
return (NULL);
}
canonname = bp;
bp += n;
/* The qname can be abbreviated, but h_name is now absolute. */
qname = canonname;
}
haveanswer = 0;
had_error = 0;
while (ancount-- > 0 && cp < eom && !had_error) {
n = dn_expand(answer.data(), eom, cp, bp, ep - bp);
if ((n < 0) || !(*name_ok)(bp)) {
had_error++;
continue;
}
cp += n; /* name */
BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ);
type = ntohs(*reinterpret_cast<const uint16_t*>(cp));
cp += INT16SZ; /* type */
int cl = ntohs(*reinterpret_cast<const uint16_t*>(cp));
cp += INT16SZ + INT32SZ; /* class, TTL */
n = ntohs(*reinterpret_cast<const uint16_t*>(cp));
cp += INT16SZ; /* len */
BOUNDS_CHECK(cp, n);
if (cl != C_IN) {
/* XXX - debug? syslog? */
cp += n;
continue; /* XXX - had_error++ ? */
}
if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && type == T_CNAME) {
n = dn_expand(answer.data(), eom, cp, tbuf, sizeof tbuf);
if ((n < 0) || !(*name_ok)(tbuf)) {
had_error++;
continue;
}
cp += n;
/* Get canonical name. */
n = strlen(tbuf) + 1; /* for the \0 */
if (n > ep - bp || n >= MAXHOSTNAMELEN) {
had_error++;
continue;
}
strlcpy(bp, tbuf, (size_t)(ep - bp));
canonname = bp;
bp += n;
continue;
}
if (qtype == T_ANY) {
if (!(type == T_A || type == T_AAAA)) {
cp += n;
continue;
}
} else if (type != qtype) {
if (type != T_KEY && type != T_SIG)
LOG(DEBUG) << __func__ << ": asked for \"" << qname << " " << p_class(C_IN) << " "
<< p_type(qtype) << "\", got type \"" << p_type(type) << "\"";
cp += n;
continue; /* XXX - had_error++ ? */
}
switch (type) {
case T_A:
case T_AAAA:
if (strcasecmp(canonname, bp) != 0) {
LOG(DEBUG) << __func__ << ": asked for \"" << canonname << "\", got \"" << bp
<< "\"";
cp += n;
continue; /* XXX - had_error++ ? */
}
if (type == T_A && n != INADDRSZ) {
cp += n;
continue;
}
if (type == T_AAAA && n != IN6ADDRSZ) {
cp += n;
continue;
}
if (type == T_AAAA) {
struct in6_addr in6;
memcpy(&in6, cp, IN6ADDRSZ);
if (IN6_IS_ADDR_V4MAPPED(&in6)) {
cp += n;
continue;
}
}
if (!haveanswer) {
int nn;
canonname = bp;
nn = strlen(bp) + 1; /* for the \0 */
bp += nn;
}
/* don't overwrite pai */
ai = *pai;
ai.ai_family = (type == T_A) ? AF_INET : AF_INET6;
afd = find_afd(ai.ai_family);
if (afd == NULL) {
cp += n;
continue;
}
cur->ai_next = get_ai(&ai, afd, (const char*) cp);
if (cur->ai_next == NULL) had_error++;
while (cur && cur->ai_next) cur = cur->ai_next;
cp += n;
break;
default:
abort();
}
if (!had_error) haveanswer++;
}
if (haveanswer) {
if (!canonname)
(void) get_canonname(pai, sentinel.ai_next, qname);
else
(void) get_canonname(pai, sentinel.ai_next, canonname);
*herrno = NETDB_SUCCESS;
return sentinel.ai_next;
}
*herrno = NO_RECOVERY;
return NULL;
}