1
2 /* Copyright 1998, 2011, 2013 by the Massachusetts Institute of Technology.
3 * Copyright (C) 2017 - 2018 by Christian Ammer
4 * Copyright (C) 2019 by Andrew Selivanov
5 *
6 * Permission to use, copy, modify, and distribute this
7 * software and its documentation for any purpose and without
8 * fee is hereby granted, provided that the above copyright
9 * notice appear in all copies and that both that copyright
10 * notice and this permission notice appear in supporting
11 * documentation, and that the name of M.I.T. not be used in
12 * advertising or publicity pertaining to distribution of the
13 * software without specific, written prior permission.
14 * M.I.T. makes no representations about the suitability of
15 * this software for any purpose. It is provided "as is"
16 * without express or implied warranty.
17 */
18
19 #include "ares_setup.h"
20
21 #ifdef HAVE_GETSERVBYNAME_R
22 # if !defined(GETSERVBYNAME_R_ARGS) || \
23 (GETSERVBYNAME_R_ARGS < 4) || (GETSERVBYNAME_R_ARGS > 6)
24 # error "you MUST specifiy a valid number of arguments for getservbyname_r"
25 # endif
26 #endif
27
28 #ifdef HAVE_NETINET_IN_H
29 # include <netinet/in.h>
30 #endif
31 #ifdef HAVE_NETDB_H
32 # include <netdb.h>
33 #endif
34 #ifdef HAVE_ARPA_INET_H
35 # include <arpa/inet.h>
36 #endif
37 #ifdef HAVE_ARPA_NAMESER_H
38 # include <arpa/nameser.h>
39 #else
40 # include "nameser.h"
41 #endif
42 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
43 # include <arpa/nameser_compat.h>
44 #endif
45
46 #ifdef HAVE_STRINGS_H
47 #include <strings.h>
48 #endif
49 #include <assert.h>
50
51 #ifdef HAVE_LIMITS_H
52 #include <limits.h>
53 #endif
54
55 #include "ares.h"
56 #include "bitncmp.h"
57 #include "ares_private.h"
58
59 #ifdef WATT32
60 #undef WIN32
61 #endif
62 #ifdef WIN32
63 # include "ares_platform.h"
64 #endif
65
66 struct host_query
67 {
68 ares_channel channel;
69 char *name;
70 unsigned short port; /* in host order */
71 ares_addrinfo_callback callback;
72 void *arg;
73 struct ares_addrinfo_hints hints;
74 int sent_family; /* this family is what was is being used */
75 int timeouts; /* number of timeouts we saw for this request */
76 const char *remaining_lookups; /* types of lookup we need to perform ("fb" by
77 default, file and dns respectively) */
78 struct ares_addrinfo *ai; /* store results between lookups */
79 int remaining; /* number of DNS answers waiting for */
80 int next_domain; /* next search domain to try */
81 };
82
83 static const struct ares_addrinfo_hints default_hints = {
84 0, /* ai_flags */
85 AF_UNSPEC, /* ai_family */
86 0, /* ai_socktype */
87 0, /* ai_protocol */
88 };
89
90 static const struct ares_addrinfo_cname empty_addrinfo_cname = {
91 INT_MAX, /* ttl */
92 NULL, /* alias */
93 NULL, /* name */
94 NULL, /* next */
95 };
96
97 static const struct ares_addrinfo_node empty_addrinfo_node = {
98 0, /* ai_ttl */
99 0, /* ai_flags */
100 0, /* ai_family */
101 0, /* ai_socktype */
102 0, /* ai_protocol */
103 0, /* ai_addrlen */
104 NULL, /* ai_addr */
105 NULL /* ai_next */
106 };
107
108 static const struct ares_addrinfo empty_addrinfo = {
109 NULL, /* cnames */
110 NULL /* nodes */
111 };
112
113 /* forward declarations */
114 static void host_callback(void *arg, int status, int timeouts,
115 unsigned char *abuf, int alen);
116 static int as_is_first(const struct host_query *hquery);
117 static int next_dns_lookup(struct host_query *hquery);
118
ares__malloc_addrinfo_cname()119 struct ares_addrinfo_cname *ares__malloc_addrinfo_cname()
120 {
121 struct ares_addrinfo_cname *cname = ares_malloc(sizeof(struct ares_addrinfo_cname));
122 if (!cname)
123 return NULL;
124
125 *cname = empty_addrinfo_cname;
126 return cname;
127 }
128
ares__append_addrinfo_cname(struct ares_addrinfo_cname ** head)129 struct ares_addrinfo_cname *ares__append_addrinfo_cname(struct ares_addrinfo_cname **head)
130 {
131 struct ares_addrinfo_cname *tail = ares__malloc_addrinfo_cname();
132 struct ares_addrinfo_cname *last = *head;
133 if (!last)
134 {
135 *head = tail;
136 return tail;
137 }
138
139 while (last->next)
140 {
141 last = last->next;
142 }
143
144 last->next = tail;
145 return tail;
146 }
147
ares__addrinfo_cat_cnames(struct ares_addrinfo_cname ** head,struct ares_addrinfo_cname * tail)148 void ares__addrinfo_cat_cnames(struct ares_addrinfo_cname **head,
149 struct ares_addrinfo_cname *tail)
150 {
151 struct ares_addrinfo_cname *last = *head;
152 if (!last)
153 {
154 *head = tail;
155 return;
156 }
157
158 while (last->next)
159 {
160 last = last->next;
161 }
162
163 last->next = tail;
164 }
165
ares__malloc_addrinfo()166 struct ares_addrinfo *ares__malloc_addrinfo()
167 {
168 struct ares_addrinfo *ai = ares_malloc(sizeof(struct ares_addrinfo));
169 if (!ai)
170 return NULL;
171
172 *ai = empty_addrinfo;
173 return ai;
174 }
175
ares__malloc_addrinfo_node()176 struct ares_addrinfo_node *ares__malloc_addrinfo_node()
177 {
178 struct ares_addrinfo_node *node =
179 ares_malloc(sizeof(struct ares_addrinfo_node));
180 if (!node)
181 return NULL;
182
183 *node = empty_addrinfo_node;
184 return node;
185 }
186
187 /* Allocate new addrinfo and append to the tail. */
ares__append_addrinfo_node(struct ares_addrinfo_node ** head)188 struct ares_addrinfo_node *ares__append_addrinfo_node(struct ares_addrinfo_node **head)
189 {
190 struct ares_addrinfo_node *tail = ares__malloc_addrinfo_node();
191 struct ares_addrinfo_node *last = *head;
192 if (!last)
193 {
194 *head = tail;
195 return tail;
196 }
197
198 while (last->ai_next)
199 {
200 last = last->ai_next;
201 }
202
203 last->ai_next = tail;
204 return tail;
205 }
206
ares__addrinfo_cat_nodes(struct ares_addrinfo_node ** head,struct ares_addrinfo_node * tail)207 void ares__addrinfo_cat_nodes(struct ares_addrinfo_node **head,
208 struct ares_addrinfo_node *tail)
209 {
210 struct ares_addrinfo_node *last = *head;
211 if (!last)
212 {
213 *head = tail;
214 return;
215 }
216
217 while (last->ai_next)
218 {
219 last = last->ai_next;
220 }
221
222 last->ai_next = tail;
223 }
224
225 /* Resolve service name into port number given in host byte order.
226 * If not resolved, return 0.
227 */
lookup_service(const char * service,int flags)228 static unsigned short lookup_service(const char *service, int flags)
229 {
230 const char *proto;
231 struct servent *sep;
232 #ifdef HAVE_GETSERVBYNAME_R
233 struct servent se;
234 char tmpbuf[4096];
235 #endif
236
237 if (service)
238 {
239 if (flags & ARES_NI_UDP)
240 proto = "udp";
241 else if (flags & ARES_NI_SCTP)
242 proto = "sctp";
243 else if (flags & ARES_NI_DCCP)
244 proto = "dccp";
245 else
246 proto = "tcp";
247 #ifdef HAVE_GETSERVBYNAME_R
248 memset(&se, 0, sizeof(se));
249 sep = &se;
250 memset(tmpbuf, 0, sizeof(tmpbuf));
251 #if GETSERVBYNAME_R_ARGS == 6
252 if (getservbyname_r(service, proto, &se, (void *)tmpbuf, sizeof(tmpbuf),
253 &sep) != 0)
254 sep = NULL; /* LCOV_EXCL_LINE: buffer large so this never fails */
255 #elif GETSERVBYNAME_R_ARGS == 5
256 sep =
257 getservbyname_r(service, proto, &se, (void *)tmpbuf, sizeof(tmpbuf));
258 #elif GETSERVBYNAME_R_ARGS == 4
259 if (getservbyname_r(service, proto, &se, (void *)tmpbuf) != 0)
260 sep = NULL;
261 #else
262 /* Lets just hope the OS uses TLS! */
263 sep = getservbyname(service, proto);
264 #endif
265 #else
266 /* Lets just hope the OS uses TLS! */
267 #if (defined(NETWARE) && !defined(__NOVELL_LIBC__))
268 sep = getservbyname(service, (char *)proto);
269 #else
270 sep = getservbyname(service, proto);
271 #endif
272 #endif
273 return (sep ? ntohs((unsigned short)sep->s_port) : 0);
274 }
275 return 0;
276 }
277
278 /* If the name looks like an IP address or an error occured,
279 * fake up a host entry, end the query immediately, and return true.
280 * Otherwise return false.
281 */
fake_addrinfo(const char * name,unsigned short port,const struct ares_addrinfo_hints * hints,struct ares_addrinfo * ai,ares_addrinfo_callback callback,void * arg)282 static int fake_addrinfo(const char *name,
283 unsigned short port,
284 const struct ares_addrinfo_hints *hints,
285 struct ares_addrinfo *ai,
286 ares_addrinfo_callback callback,
287 void *arg)
288 {
289 struct ares_addrinfo_cname *cname;
290 struct ares_addrinfo_node *node;
291 ares_sockaddr addr;
292 size_t addrlen;
293 int result = 0;
294 int family = hints->ai_family;
295 if (family == AF_INET || family == AF_INET6 || family == AF_UNSPEC)
296 {
297 /* It only looks like an IP address if it's all numbers and dots. */
298 int numdots = 0, valid = 1;
299 const char *p;
300 for (p = name; *p; p++)
301 {
302 if (!ISDIGIT(*p) && *p != '.')
303 {
304 valid = 0;
305 break;
306 }
307 else if (*p == '.')
308 {
309 numdots++;
310 }
311 }
312
313 memset(&addr, 0, sizeof(addr));
314
315 /* if we don't have 3 dots, it is illegal
316 * (although inet_pton doesn't think so).
317 */
318 if (numdots != 3 || !valid)
319 result = 0;
320 else
321 result =
322 (ares_inet_pton(AF_INET, name, &addr.sa4.sin_addr) < 1 ? 0 : 1);
323
324 if (result)
325 {
326 family = addr.sa.sa_family = AF_INET;
327 addr.sa4.sin_port = htons(port);
328 addrlen = sizeof(addr.sa4);
329 }
330 }
331
332 if (family == AF_INET6 || family == AF_UNSPEC)
333 {
334 result =
335 (ares_inet_pton(AF_INET6, name, &addr.sa6.sin6_addr) < 1 ? 0 : 1);
336 addr.sa6.sin6_family = AF_INET6;
337 addr.sa6.sin6_port = htons(port);
338 addrlen = sizeof(addr.sa6);
339 }
340
341 if (!result)
342 return 0;
343
344 node = ares__malloc_addrinfo_node();
345 if (!node)
346 {
347 ares_freeaddrinfo(ai);
348 callback(arg, ARES_ENOMEM, 0, NULL);
349 return 1;
350 }
351
352 ai->nodes = node;
353
354 node->ai_addr = ares_malloc(addrlen);
355 if (!node->ai_addr)
356 {
357 ares_freeaddrinfo(ai);
358 callback(arg, ARES_ENOMEM, 0, NULL);
359 return 1;
360 }
361
362 node->ai_addrlen = (unsigned int)addrlen;
363 node->ai_family = addr.sa.sa_family;
364 if (addr.sa.sa_family == AF_INET)
365 memcpy(node->ai_addr, &addr.sa4, sizeof(addr.sa4));
366 else
367 memcpy(node->ai_addr, &addr.sa6, sizeof(addr.sa6));
368
369 if (hints->ai_flags & ARES_AI_CANONNAME)
370 {
371 cname = ares__append_addrinfo_cname(&ai->cnames);
372 if (!cname)
373 {
374 ares_freeaddrinfo(ai);
375 callback(arg, ARES_ENOMEM, 0, NULL);
376 return 1;
377 }
378
379 /* Duplicate the name, to avoid a constness violation. */
380 cname->name = ares_strdup(name);
381 if (!cname->name)
382 {
383 ares_freeaddrinfo(ai);
384 callback(arg, ARES_ENOMEM, 0, NULL);
385 return 1;
386 }
387 }
388
389 callback(arg, ARES_SUCCESS, 0, ai);
390 return 1;
391 }
392
end_hquery(struct host_query * hquery,int status)393 static void end_hquery(struct host_query *hquery, int status)
394 {
395 struct ares_addrinfo_node sentinel;
396 struct ares_addrinfo_node *next;
397 if (status == ARES_SUCCESS)
398 {
399 if (!(hquery->hints.ai_flags & ARES_AI_NOSORT))
400 {
401 sentinel.ai_next = hquery->ai->nodes;
402 ares__sortaddrinfo(hquery->channel, &sentinel);
403 hquery->ai->nodes = sentinel.ai_next;
404 }
405 next = hquery->ai->nodes;
406 /* Set port into each address (resolved separately). */
407 while (next)
408 {
409 if (next->ai_family == AF_INET)
410 {
411 (CARES_INADDR_CAST(struct sockaddr_in *, next->ai_addr))->sin_port = htons(hquery->port);
412 }
413 else
414 {
415 (CARES_INADDR_CAST(struct sockaddr_in6 *, next->ai_addr))->sin6_port = htons(hquery->port);
416 }
417 next = next->ai_next;
418 }
419 }
420 else
421 {
422 /* Clean up what we have collected by so far. */
423 ares_freeaddrinfo(hquery->ai);
424 hquery->ai = NULL;
425 }
426
427 hquery->callback(hquery->arg, status, hquery->timeouts, hquery->ai);
428 ares_free(hquery->name);
429 ares_free(hquery);
430 }
431
file_lookup(struct host_query * hquery)432 static int file_lookup(struct host_query *hquery)
433 {
434 FILE *fp;
435 int error;
436 int status;
437 const char *path_hosts = NULL;
438
439 if (hquery->hints.ai_flags & ARES_AI_ENVHOSTS)
440 {
441 path_hosts = getenv("CARES_HOSTS");
442 }
443
444 if (!path_hosts)
445 {
446 #ifdef WIN32
447 char PATH_HOSTS[MAX_PATH];
448 win_platform platform;
449
450 PATH_HOSTS[0] = '\0';
451
452 platform = ares__getplatform();
453
454 if (platform == WIN_NT)
455 {
456 char tmp[MAX_PATH];
457 HKEY hkeyHosts;
458
459 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ,
460 &hkeyHosts) == ERROR_SUCCESS)
461 {
462 DWORD dwLength = MAX_PATH;
463 RegQueryValueExA(hkeyHosts, DATABASEPATH, NULL, NULL, (LPBYTE)tmp,
464 &dwLength);
465 ExpandEnvironmentStringsA(tmp, PATH_HOSTS, MAX_PATH);
466 RegCloseKey(hkeyHosts);
467 }
468 }
469 else if (platform == WIN_9X)
470 GetWindowsDirectoryA(PATH_HOSTS, MAX_PATH);
471 else
472 return ARES_ENOTFOUND;
473
474 strcat(PATH_HOSTS, WIN_PATH_HOSTS);
475 path_hosts = PATH_HOSTS;
476
477 #elif defined(WATT32)
478 const char *PATH_HOSTS = _w32_GetHostsFile();
479
480 if (!PATH_HOSTS)
481 return ARES_ENOTFOUND;
482 #endif
483 path_hosts = PATH_HOSTS;
484 }
485
486 fp = fopen(path_hosts, "r");
487 if (!fp)
488 {
489 error = ERRNO;
490 switch (error)
491 {
492 case ENOENT:
493 case ESRCH:
494 return ARES_ENOTFOUND;
495 default:
496 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n", error,
497 strerror(error)));
498 DEBUGF(fprintf(stderr, "Error opening file: %s\n", path_hosts));
499 return ARES_EFILE;
500 }
501 }
502 status = ares__readaddrinfo(fp, hquery->name, hquery->port, &hquery->hints, hquery->ai);
503 fclose(fp);
504 return status;
505 }
506
next_lookup(struct host_query * hquery,int status)507 static void next_lookup(struct host_query *hquery, int status)
508 {
509 switch (*hquery->remaining_lookups)
510 {
511 case 'b':
512 /* DNS lookup */
513 if (next_dns_lookup(hquery))
514 break;
515 hquery->remaining_lookups++;
516 next_lookup(hquery, status);
517 break;
518
519 case 'f':
520 /* Host file lookup */
521 if (file_lookup(hquery) == ARES_SUCCESS)
522 {
523 end_hquery(hquery, ARES_SUCCESS);
524 break;
525 }
526 hquery->remaining_lookups++;
527 next_lookup(hquery, status);
528 break;
529 default:
530 /* No lookup left */
531 end_hquery(hquery, status);
532 break;
533 }
534 }
535
host_callback(void * arg,int status,int timeouts,unsigned char * abuf,int alen)536 static void host_callback(void *arg, int status, int timeouts,
537 unsigned char *abuf, int alen)
538 {
539 struct host_query *hquery = (struct host_query*)arg;
540 int addinfostatus = ARES_SUCCESS;
541 hquery->timeouts += timeouts;
542 hquery->remaining--;
543
544 if (status == ARES_SUCCESS)
545 {
546 addinfostatus = ares__parse_into_addrinfo(abuf, alen, hquery->ai);
547 }
548 else if (status == ARES_EDESTRUCTION)
549 {
550 end_hquery(hquery, status);
551 return;
552 }
553
554 if (!hquery->remaining)
555 {
556 if (addinfostatus != ARES_SUCCESS)
557 {
558 /* error in parsing result e.g. no memory */
559 end_hquery(hquery, addinfostatus);
560 }
561 else if (hquery->ai->nodes)
562 {
563 /* at least one query ended with ARES_SUCCESS */
564 end_hquery(hquery, ARES_SUCCESS);
565 }
566 else if (status == ARES_ENOTFOUND)
567 {
568 next_lookup(hquery, status);
569 }
570 else
571 {
572 end_hquery(hquery, status);
573 }
574 }
575
576 /* at this point we keep on waiting for the next query to finish */
577 }
578
ares_getaddrinfo(ares_channel channel,const char * name,const char * service,const struct ares_addrinfo_hints * hints,ares_addrinfo_callback callback,void * arg)579 void ares_getaddrinfo(ares_channel channel,
580 const char* name, const char* service,
581 const struct ares_addrinfo_hints* hints,
582 ares_addrinfo_callback callback, void* arg)
583 {
584 struct host_query *hquery;
585 unsigned short port = 0;
586 int family;
587 struct ares_addrinfo *ai;
588
589 if (!hints)
590 {
591 hints = &default_hints;
592 }
593
594 family = hints->ai_family;
595
596 /* Right now we only know how to look up Internet addresses
597 and unspec means try both basically. */
598 if (family != AF_INET &&
599 family != AF_INET6 &&
600 family != AF_UNSPEC)
601 {
602 callback(arg, ARES_ENOTIMP, 0, NULL);
603 return;
604 }
605
606 if (ares__is_onion_domain(name))
607 {
608 callback(arg, ARES_ENOTFOUND, 0, NULL);
609 return;
610 }
611
612 if (service)
613 {
614 if (hints->ai_flags & ARES_AI_NUMERICSERV)
615 {
616 port = (unsigned short)strtoul(service, NULL, 0);
617 if (!port)
618 {
619 callback(arg, ARES_ESERVICE, 0, NULL);
620 return;
621 }
622 }
623 else
624 {
625 port = lookup_service(service, 0);
626 if (!port)
627 {
628 port = (unsigned short)strtoul(service, NULL, 0);
629 if (!port)
630 {
631 callback(arg, ARES_ESERVICE, 0, NULL);
632 return;
633 }
634 }
635 }
636 }
637
638 ai = ares__malloc_addrinfo();
639 if (!ai)
640 {
641 callback(arg, ARES_ENOMEM, 0, NULL);
642 return;
643 }
644
645 if (fake_addrinfo(name, port, hints, ai, callback, arg))
646 {
647 return;
648 }
649
650 /* Allocate and fill in the host query structure. */
651 hquery = ares_malloc(sizeof(struct host_query));
652 if (!hquery)
653 {
654 ares_freeaddrinfo(ai);
655 callback(arg, ARES_ENOMEM, 0, NULL);
656 return;
657 }
658
659 hquery->name = ares_strdup(name);
660 if (!hquery->name)
661 {
662 ares_free(hquery);
663 ares_freeaddrinfo(ai);
664 callback(arg, ARES_ENOMEM, 0, NULL);
665 return;
666 }
667
668 hquery->port = port;
669 hquery->channel = channel;
670 hquery->hints = *hints;
671 hquery->sent_family = -1; /* nothing is sent yet */
672 hquery->callback = callback;
673 hquery->arg = arg;
674 hquery->remaining_lookups = channel->lookups;
675 hquery->timeouts = 0;
676 hquery->ai = ai;
677 hquery->next_domain = -1;
678 hquery->remaining = 0;
679
680 /* Start performing lookups according to channel->lookups. */
681 next_lookup(hquery, ARES_ECONNREFUSED /* initial error code */);
682 }
683
next_dns_lookup(struct host_query * hquery)684 static int next_dns_lookup(struct host_query *hquery)
685 {
686 char *s = NULL;
687 int is_s_allocated = 0;
688 int status;
689
690 /* if next_domain == -1 and as_is_first is true, try hquery->name */
691 if (hquery->next_domain == -1)
692 {
693 if (as_is_first(hquery))
694 {
695 s = hquery->name;
696 }
697 hquery->next_domain = 0;
698 }
699
700 /* if as_is_first is false, try hquery->name at last */
701 if (!s && hquery->next_domain == hquery->channel->ndomains) {
702 if (!as_is_first(hquery))
703 {
704 s = hquery->name;
705 }
706 hquery->next_domain++;
707 }
708
709 if (!s && hquery->next_domain < hquery->channel->ndomains)
710 {
711 status = ares__cat_domain(
712 hquery->name,
713 hquery->channel->domains[hquery->next_domain++],
714 &s);
715 if (status == ARES_SUCCESS)
716 {
717 is_s_allocated = 1;
718 }
719 }
720
721 if (s)
722 {
723 switch (hquery->hints.ai_family)
724 {
725 case AF_INET:
726 hquery->remaining += 1;
727 ares_query(hquery->channel, s, C_IN, T_A, host_callback, hquery);
728 break;
729 case AF_INET6:
730 hquery->remaining += 1;
731 ares_query(hquery->channel, s, C_IN, T_AAAA, host_callback, hquery);
732 break;
733 case AF_UNSPEC:
734 hquery->remaining += 2;
735 ares_query(hquery->channel, s, C_IN, T_A, host_callback, hquery);
736 ares_query(hquery->channel, s, C_IN, T_AAAA, host_callback, hquery);
737 break;
738 default: break;
739 }
740 if (is_s_allocated)
741 {
742 ares_free(s);
743 }
744 return 1;
745 }
746 else
747 {
748 assert(!hquery->ai->nodes);
749 return 0;
750 }
751 }
752
as_is_first(const struct host_query * hquery)753 static int as_is_first(const struct host_query* hquery)
754 {
755 char* p;
756 int ndots = 0;
757 for (p = hquery->name; *p; p++)
758 {
759 if (*p == '.')
760 {
761 ndots++;
762 }
763 }
764 return ndots >= hquery->channel->ndots;
765 }
766