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
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.
19 #include "ares_setup.h"
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"
28 #ifdef HAVE_NETINET_IN_H
29 # include <netinet/in.h>
34 #ifdef HAVE_ARPA_INET_H
35 # include <arpa/inet.h>
38 #include "ares_nameser.h"
51 #include "ares_private.h"
57 # include "ares_platform.h"
64 unsigned short port; /* in host order */
65 ares_addrinfo_callback callback;
67 struct ares_addrinfo_hints hints;
68 int sent_family; /* this family is what was is being used */
69 int timeouts; /* number of timeouts we saw for this request */
70 const char *remaining_lookups; /* types of lookup we need to perform ("fb" by
71 default, file and dns respectively) */
72 struct ares_addrinfo *ai; /* store results between lookups */
73 int remaining; /* number of DNS answers waiting for */
74 int next_domain; /* next search domain to try */
75 int nodata_cnt; /* Track nodata responses to possibly override final result */
78 static const struct ares_addrinfo_hints default_hints = {
80 AF_UNSPEC, /* ai_family */
85 static const struct ares_addrinfo_cname empty_addrinfo_cname = {
92 static const struct ares_addrinfo_node empty_addrinfo_node = {
103 static const struct ares_addrinfo empty_addrinfo = {
109 /* forward declarations */
110 static void host_callback(void *arg, int status, int timeouts,
111 unsigned char *abuf, int alen);
112 static int as_is_first(const struct host_query *hquery);
113 static int as_is_only(const struct host_query* hquery);
114 static int next_dns_lookup(struct host_query *hquery);
116 struct ares_addrinfo_cname *ares__malloc_addrinfo_cname()
118 struct ares_addrinfo_cname *cname = ares_malloc(sizeof(struct ares_addrinfo_cname));
122 *cname = empty_addrinfo_cname;
126 struct ares_addrinfo_cname *ares__append_addrinfo_cname(struct ares_addrinfo_cname **head)
128 struct ares_addrinfo_cname *tail = ares__malloc_addrinfo_cname();
129 struct ares_addrinfo_cname *last = *head;
145 void ares__addrinfo_cat_cnames(struct ares_addrinfo_cname **head,
146 struct ares_addrinfo_cname *tail)
148 struct ares_addrinfo_cname *last = *head;
163 struct ares_addrinfo *ares__malloc_addrinfo()
165 struct ares_addrinfo *ai = ares_malloc(sizeof(struct ares_addrinfo));
169 *ai = empty_addrinfo;
173 struct ares_addrinfo_node *ares__malloc_addrinfo_node()
175 struct ares_addrinfo_node *node =
176 ares_malloc(sizeof(struct ares_addrinfo_node));
180 *node = empty_addrinfo_node;
184 /* Allocate new addrinfo and append to the tail. */
185 struct ares_addrinfo_node *ares__append_addrinfo_node(struct ares_addrinfo_node **head)
187 struct ares_addrinfo_node *tail = ares__malloc_addrinfo_node();
188 struct ares_addrinfo_node *last = *head;
195 while (last->ai_next)
197 last = last->ai_next;
200 last->ai_next = tail;
204 void ares__addrinfo_cat_nodes(struct ares_addrinfo_node **head,
205 struct ares_addrinfo_node *tail)
207 struct ares_addrinfo_node *last = *head;
214 while (last->ai_next)
216 last = last->ai_next;
219 last->ai_next = tail;
222 /* Resolve service name into port number given in host byte order.
223 * If not resolved, return 0.
225 static unsigned short lookup_service(const char *service, int flags)
229 #ifdef HAVE_GETSERVBYNAME_R
236 if (flags & ARES_NI_UDP)
238 else if (flags & ARES_NI_SCTP)
240 else if (flags & ARES_NI_DCCP)
244 #ifdef HAVE_GETSERVBYNAME_R
245 memset(&se, 0, sizeof(se));
247 memset(tmpbuf, 0, sizeof(tmpbuf));
248 #if GETSERVBYNAME_R_ARGS == 6
249 if (getservbyname_r(service, proto, &se, (void *)tmpbuf, sizeof(tmpbuf),
251 sep = NULL; /* LCOV_EXCL_LINE: buffer large so this never fails */
252 #elif GETSERVBYNAME_R_ARGS == 5
254 getservbyname_r(service, proto, &se, (void *)tmpbuf, sizeof(tmpbuf));
255 #elif GETSERVBYNAME_R_ARGS == 4
256 if (getservbyname_r(service, proto, &se, (void *)tmpbuf) != 0)
259 /* Lets just hope the OS uses TLS! */
260 sep = getservbyname(service, proto);
263 /* Lets just hope the OS uses TLS! */
264 #if (defined(NETWARE) && !defined(__NOVELL_LIBC__))
265 sep = getservbyname(service, (char *)proto);
267 sep = getservbyname(service, proto);
270 return (sep ? ntohs((unsigned short)sep->s_port) : 0);
275 /* If the name looks like an IP address or an error occured,
276 * fake up a host entry, end the query immediately, and return true.
277 * Otherwise return false.
279 static int fake_addrinfo(const char *name,
281 const struct ares_addrinfo_hints *hints,
282 struct ares_addrinfo *ai,
283 ares_addrinfo_callback callback,
286 struct ares_addrinfo_cname *cname;
287 int status = ARES_SUCCESS;
289 int family = hints->ai_family;
290 if (family == AF_INET || family == AF_INET6 || family == AF_UNSPEC)
292 /* It only looks like an IP address if it's all numbers and dots. */
293 int numdots = 0, valid = 1;
295 for (p = name; *p; p++)
297 if (!ISDIGIT(*p) && *p != '.')
308 /* if we don't have 3 dots, it is illegal
309 * (although inet_pton doesn't think so).
311 if (numdots != 3 || !valid)
315 struct in_addr addr4;
316 result = ares_inet_pton(AF_INET, name, &addr4) < 1 ? 0 : 1;
319 status = ares_append_ai_node(AF_INET, port, 0, &addr4, &ai->nodes);
320 if (status != ARES_SUCCESS)
322 callback(arg, status, 0, NULL);
329 if (family == AF_INET6 || family == AF_UNSPEC)
331 struct ares_in6_addr addr6;
332 result = ares_inet_pton(AF_INET6, name, &addr6) < 1 ? 0 : 1;
335 status = ares_append_ai_node(AF_INET6, port, 0, &addr6, &ai->nodes);
336 if (status != ARES_SUCCESS)
338 callback(arg, status, 0, NULL);
347 if (hints->ai_flags & ARES_AI_CANONNAME)
349 cname = ares__append_addrinfo_cname(&ai->cnames);
352 ares_freeaddrinfo(ai);
353 callback(arg, ARES_ENOMEM, 0, NULL);
357 /* Duplicate the name, to avoid a constness violation. */
358 cname->name = ares_strdup(name);
361 ares_freeaddrinfo(ai);
362 callback(arg, ARES_ENOMEM, 0, NULL);
367 ai->nodes->ai_socktype = hints->ai_socktype;
368 ai->nodes->ai_protocol = hints->ai_protocol;
370 callback(arg, ARES_SUCCESS, 0, ai);
374 static void end_hquery(struct host_query *hquery, int status)
376 struct ares_addrinfo_node sentinel;
377 struct ares_addrinfo_node *next;
378 if (status == ARES_SUCCESS)
380 if (!(hquery->hints.ai_flags & ARES_AI_NOSORT) && hquery->ai->nodes)
382 sentinel.ai_next = hquery->ai->nodes;
383 ares__sortaddrinfo(hquery->channel, &sentinel);
384 hquery->ai->nodes = sentinel.ai_next;
386 next = hquery->ai->nodes;
390 next->ai_socktype = hquery->hints.ai_socktype;
391 next->ai_protocol = hquery->hints.ai_protocol;
392 next = next->ai_next;
397 /* Clean up what we have collected by so far. */
398 ares_freeaddrinfo(hquery->ai);
402 hquery->callback(hquery->arg, status, hquery->timeouts, hquery->ai);
403 ares_free(hquery->name);
407 static int file_lookup(struct host_query *hquery)
412 const char *path_hosts = NULL;
414 if (hquery->hints.ai_flags & ARES_AI_ENVHOSTS)
416 path_hosts = getenv("CARES_HOSTS");
422 char PATH_HOSTS[MAX_PATH];
423 win_platform platform;
425 PATH_HOSTS[0] = '\0';
427 platform = ares__getplatform();
429 if (platform == WIN_NT)
434 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, KEY_READ,
435 &hkeyHosts) == ERROR_SUCCESS)
437 DWORD dwLength = MAX_PATH;
438 RegQueryValueExA(hkeyHosts, DATABASEPATH, NULL, NULL, (LPBYTE)tmp,
440 ExpandEnvironmentStringsA(tmp, PATH_HOSTS, MAX_PATH);
441 RegCloseKey(hkeyHosts);
444 else if (platform == WIN_9X)
445 GetWindowsDirectoryA(PATH_HOSTS, MAX_PATH);
447 return ARES_ENOTFOUND;
449 strcat(PATH_HOSTS, WIN_PATH_HOSTS);
450 path_hosts = PATH_HOSTS;
452 #elif defined(WATT32)
453 const char *PATH_HOSTS = _w32_GetHostsFile();
456 return ARES_ENOTFOUND;
458 path_hosts = PATH_HOSTS;
461 fp = fopen(path_hosts, "r");
469 return ARES_ENOTFOUND;
471 DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n", error,
473 DEBUGF(fprintf(stderr, "Error opening file: %s\n", path_hosts));
477 status = ares__readaddrinfo(fp, hquery->name, hquery->port, &hquery->hints, hquery->ai);
480 /* RFC6761 section 6.3 #3 states that "Name resolution APIs and libraries
481 * SHOULD recognize localhost names as special and SHOULD always return the
482 * IP loopback address for address queries" */
483 if (status == ARES_ENOTFOUND && strcmp(hquery->name, "localhost") == 0)
485 return ares__addrinfo_localhost(hquery->name, hquery->port,
486 &hquery->hints, hquery->ai);
492 static void next_lookup(struct host_query *hquery, int status)
494 switch (*hquery->remaining_lookups)
497 /* RFC6761 section 6.3 #3 says "Name resolution APIs SHOULD NOT send
498 * queries for localhost names to their configured caching DNS
500 if (strcmp(hquery->name, "localhost") != 0)
503 if (next_dns_lookup(hquery))
507 hquery->remaining_lookups++;
508 next_lookup(hquery, status);
512 /* Host file lookup */
513 if (file_lookup(hquery) == ARES_SUCCESS)
515 end_hquery(hquery, ARES_SUCCESS);
518 hquery->remaining_lookups++;
519 next_lookup(hquery, status);
523 end_hquery(hquery, status);
528 static void host_callback(void *arg, int status, int timeouts,
529 unsigned char *abuf, int alen)
531 struct host_query *hquery = (struct host_query*)arg;
532 int addinfostatus = ARES_SUCCESS;
533 hquery->timeouts += timeouts;
536 if (status == ARES_SUCCESS)
538 addinfostatus = ares__parse_into_addrinfo(abuf, alen, 1, hquery->port, hquery->ai);
541 if (!hquery->remaining)
543 if (addinfostatus != ARES_SUCCESS && addinfostatus != ARES_ENODATA)
545 /* error in parsing result e.g. no memory */
546 end_hquery(hquery, addinfostatus);
548 else if (hquery->ai->nodes)
550 /* at least one query ended with ARES_SUCCESS */
551 end_hquery(hquery, ARES_SUCCESS);
553 else if (status == ARES_ENOTFOUND || status == ARES_ENODATA ||
554 addinfostatus == ARES_ENODATA)
556 if (status == ARES_ENODATA || addinfostatus == ARES_ENODATA)
557 hquery->nodata_cnt++;
558 next_lookup(hquery, hquery->nodata_cnt?ARES_ENODATA:status);
560 else if (status == ARES_EDESTRUCTION)
562 /* NOTE: Could also be ARES_EDESTRUCTION. We need to only call this
563 * once all queries (there can be multiple for getaddrinfo) are
565 end_hquery(hquery, status);
569 end_hquery(hquery, status);
573 /* at this point we keep on waiting for the next query to finish */
576 void ares_getaddrinfo(ares_channel channel,
577 const char* name, const char* service,
578 const struct ares_addrinfo_hints* hints,
579 ares_addrinfo_callback callback, void* arg)
581 struct host_query *hquery;
582 unsigned short port = 0;
584 struct ares_addrinfo *ai;
585 char *alias_name = NULL;
590 hints = &default_hints;
593 family = hints->ai_family;
595 /* Right now we only know how to look up Internet addresses
596 and unspec means try both basically. */
597 if (family != AF_INET &&
598 family != AF_INET6 &&
601 callback(arg, ARES_ENOTIMP, 0, NULL);
605 if (ares__is_onion_domain(name))
607 callback(arg, ARES_ENOTFOUND, 0, NULL);
611 /* perform HOSTALIAS resolution (technically this function does some other
612 * things we are going to ignore) */
613 status = ares__single_domain(channel, name, &alias_name);
614 if (status != ARES_SUCCESS) {
615 callback(arg, status, 0, NULL);
624 if (hints->ai_flags & ARES_AI_NUMERICSERV)
626 port = (unsigned short)strtoul(service, NULL, 0);
629 ares_free(alias_name);
630 callback(arg, ARES_ESERVICE, 0, NULL);
636 port = lookup_service(service, 0);
639 port = (unsigned short)strtoul(service, NULL, 0);
642 ares_free(alias_name);
643 callback(arg, ARES_ESERVICE, 0, NULL);
650 ai = ares__malloc_addrinfo();
653 ares_free(alias_name);
654 callback(arg, ARES_ENOMEM, 0, NULL);
658 if (fake_addrinfo(name, port, hints, ai, callback, arg))
660 ares_free(alias_name);
664 /* Allocate and fill in the host query structure. */
665 hquery = ares_malloc(sizeof(struct host_query));
668 ares_free(alias_name);
669 ares_freeaddrinfo(ai);
670 callback(arg, ARES_ENOMEM, 0, NULL);
674 hquery->name = ares_strdup(name);
675 ares_free(alias_name);
679 ares_freeaddrinfo(ai);
680 callback(arg, ARES_ENOMEM, 0, NULL);
685 hquery->channel = channel;
686 hquery->hints = *hints;
687 hquery->sent_family = -1; /* nothing is sent yet */
688 hquery->callback = callback;
690 hquery->remaining_lookups = channel->lookups;
691 hquery->timeouts = 0;
693 hquery->next_domain = -1;
694 hquery->remaining = 0;
695 hquery->nodata_cnt = 0;
697 /* Start performing lookups according to channel->lookups. */
698 next_lookup(hquery, ARES_ECONNREFUSED /* initial error code */);
701 static int next_dns_lookup(struct host_query *hquery)
704 int is_s_allocated = 0;
707 /* if next_domain == -1 and as_is_first is true, try hquery->name */
708 if (hquery->next_domain == -1)
710 if (as_is_first(hquery))
714 hquery->next_domain = 0;
717 /* if as_is_first is false, try hquery->name at last */
718 if (!s && hquery->next_domain == hquery->channel->ndomains) {
719 if (!as_is_first(hquery))
723 hquery->next_domain++;
726 if (!s && hquery->next_domain < hquery->channel->ndomains && !as_is_only(hquery))
728 status = ares__cat_domain(
730 hquery->channel->domains[hquery->next_domain++],
732 if (status == ARES_SUCCESS)
740 switch (hquery->hints.ai_family)
743 hquery->remaining += 1;
744 ares_query(hquery->channel, s, C_IN, T_A, host_callback, hquery);
747 hquery->remaining += 1;
748 ares_query(hquery->channel, s, C_IN, T_AAAA, host_callback, hquery);
751 hquery->remaining += 2;
752 ares_query(hquery->channel, s, C_IN, T_A, host_callback, hquery);
753 ares_query(hquery->channel, s, C_IN, T_AAAA, host_callback, hquery);
765 assert(!hquery->ai->nodes);
770 static int as_is_first(const struct host_query* hquery)
774 size_t nname = hquery->name?strlen(hquery->name):0;
775 for (p = hquery->name; *p; p++)
782 if (nname && hquery->name[nname-1] == '.')
784 /* prevent ARES_EBADNAME for valid FQDN, where ndots < channel->ndots */
787 return ndots >= hquery->channel->ndots;
790 static int as_is_only(const struct host_query* hquery)
792 size_t nname = hquery->name?strlen(hquery->name):0;
793 if (nname && hquery->name[nname-1] == '.')