clang-format

pull/595/head
Brad House 1 year ago
parent 699f469f18
commit 2cc2779fc9
  1. 21
      src/lib/ares__buf.c
  2. 6
      src/lib/ares__close_sockets.c
  3. 151
      src/lib/ares__hosts_file.c
  4. 3
      src/lib/ares__htable_strvp.c
  5. 5
      src/lib/ares__slist.c
  6. 1
      src/lib/ares__socket.c
  7. 3
      src/lib/ares__sortaddrinfo.c
  8. 5
      src/lib/ares_destroy.c
  9. 17
      src/lib/ares_getaddrinfo.c
  10. 9
      src/lib/ares_gethostbyaddr.c
  11. 6
      src/lib/ares_gethostbyname.c
  12. 28
      src/lib/ares_init.c
  13. 22
      src/lib/ares_options.c
  14. 17
      src/lib/ares_private.h
  15. 37
      src/lib/ares_process.c
  16. 61
      src/lib/ares_sysconfig.c
  17. 52
      src/lib/ares_sysconfig_files.c
  18. 121
      src/lib/ares_update_servers.c

@ -45,7 +45,6 @@ struct ares__buf {
* SIZE_MAX if not set. */ * SIZE_MAX if not set. */
}; };
/* Reserved characters for names that need to be escaped */ /* Reserved characters for names that need to be escaped */
static ares_bool_t is_reservedch(int ch) static ares_bool_t is_reservedch(int ch)
{ {
@ -107,7 +106,6 @@ ares_bool_t ares__is_hostnamech(int ch)
return ARES_FALSE; return ARES_FALSE;
} }
ares__buf_t *ares__buf_create(void) ares__buf_t *ares__buf_create(void)
{ {
ares__buf_t *buf = ares_malloc_zero(sizeof(*buf)); ares__buf_t *buf = ares_malloc_zero(sizeof(*buf));
@ -389,8 +387,9 @@ const unsigned char *ares__buf_tag_fetch(const ares__buf_t *buf, size_t *len)
size_t ares__buf_tag_length(const ares__buf_t *buf) size_t ares__buf_tag_length(const ares__buf_t *buf)
{ {
if (buf == NULL || buf->tag_offset == SIZE_MAX) if (buf == NULL || buf->tag_offset == SIZE_MAX) {
return 0; return 0;
}
return buf->offset - buf->tag_offset; return buf->offset - buf->tag_offset;
} }
@ -400,11 +399,13 @@ ares_status_t ares__buf_tag_fetch_bytes(const ares__buf_t *buf,
size_t ptr_len = 0; size_t ptr_len = 0;
const unsigned char *ptr = ares__buf_tag_fetch(buf, &ptr_len); const unsigned char *ptr = ares__buf_tag_fetch(buf, &ptr_len);
if (ptr == NULL || bytes == NULL || len == NULL) if (ptr == NULL || bytes == NULL || len == NULL) {
return ARES_EFORMERR; return ARES_EFORMERR;
}
if (*len < ptr_len) if (*len < ptr_len) {
return ARES_EFORMERR; return ARES_EFORMERR;
}
*len = ptr_len; *len = ptr_len;
@ -421,24 +422,27 @@ ares_status_t ares__buf_tag_fetch_string(const ares__buf_t *buf, char *str,
ares_status_t status; ares_status_t status;
size_t i; size_t i;
if (str == NULL || len == 0) if (str == NULL || len == 0) {
return ARES_EFORMERR; return ARES_EFORMERR;
}
/* Space for NULL terminator */ /* Space for NULL terminator */
out_len = len - 1; out_len = len - 1;
status = ares__buf_tag_fetch_bytes(buf, (unsigned char *)str, &out_len); status = ares__buf_tag_fetch_bytes(buf, (unsigned char *)str, &out_len);
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS) {
return status; return status;
}
/* NULL terminate */ /* NULL terminate */
str[out_len] = 0; str[out_len] = 0;
/* Validate string is printable */ /* Validate string is printable */
for (i = 0; i < out_len; i++) { for (i = 0; i < out_len; i++) {
if (!ares__isprint(str[i])) if (!ares__isprint(str[i])) {
return ARES_EBADSTR; return ARES_EBADSTR;
} }
}
return ARES_SUCCESS; return ARES_SUCCESS;
} }
@ -573,7 +577,6 @@ ares_status_t ares__buf_fetch_bytes_into_buf(ares__buf_t *buf,
return ares__buf_consume(buf, len); return ares__buf_consume(buf, len);
} }
static ares_status_t ares__buf_fetch_dnsname_into_buf(ares__buf_t *buf, static ares_status_t ares__buf_fetch_dnsname_into_buf(ares__buf_t *buf,
ares__buf_t *dest, ares__buf_t *dest,
size_t len, size_t len,

@ -84,8 +84,9 @@ void ares__check_cleanup_conn(ares_channel channel,
{ {
ares_bool_t do_cleanup = ARES_FALSE; ares_bool_t do_cleanup = ARES_FALSE;
if (channel == NULL || conn == NULL) if (channel == NULL || conn == NULL) {
return; return;
}
if (ares__llist_len(conn->queries_to_conn)) { if (ares__llist_len(conn->queries_to_conn)) {
return; return;
@ -102,8 +103,9 @@ void ares__check_cleanup_conn(ares_channel channel,
do_cleanup = ARES_TRUE; do_cleanup = ARES_TRUE;
} }
if (!do_cleanup) if (!do_cleanup) {
return; return;
}
ares__close_connection(conn); ares__close_connection(conn);
} }

@ -44,7 +44,6 @@
#include <time.h> #include <time.h>
#include "ares_platform.h" #include "ares_platform.h"
/* HOSTS FILE PROCESSING OVERVIEW /* HOSTS FILE PROCESSING OVERVIEW
* ============================== * ==============================
* The hosts file on the system contains static entries to be processed locally * The hosts file on the system contains static entries to be processed locally
@ -99,8 +98,8 @@ struct ares_hosts_entry {
ares__llist_t *hosts; ares__llist_t *hosts;
}; };
static ares_status_t ares__read_file_into_buf(const char *filename,
static ares_status_t ares__read_file_into_buf(const char *filename, ares__buf_t *buf) ares__buf_t *buf)
{ {
FILE *fp = NULL; FILE *fp = NULL;
unsigned char *ptr = NULL; unsigned char *ptr = NULL;
@ -108,8 +107,9 @@ static ares_status_t ares__read_file_into_buf(const char *filename, ares__buf_t
size_t ptr_len = 0; size_t ptr_len = 0;
ares_status_t status; ares_status_t status;
if (filename == NULL || buf == NULL) if (filename == NULL || buf == NULL) {
return ARES_EFORMERR; return ARES_EFORMERR;
}
fp = fopen(filename, "rb"); fp = fopen(filename, "rb");
if (fp == NULL) { if (fp == NULL) {
@ -162,8 +162,9 @@ static ares_status_t ares__read_file_into_buf(const char *filename, ares__buf_t
status = ARES_SUCCESS; status = ARES_SUCCESS;
done: done:
if (fp != NULL) if (fp != NULL) {
fclose(fp); fclose(fp);
}
return status; return status;
} }
@ -171,22 +172,22 @@ static ares_bool_t ares__is_hostname(const char *str)
{ {
size_t i; size_t i;
for (i = 0; str[i] != 0; i++) { for (i = 0; str[i] != 0; i++) {
if (!ares__is_hostnamech(str[i])) if (!ares__is_hostnamech(str[i])) {
return ARES_FALSE; return ARES_FALSE;
} }
}
return ARES_TRUE; return ARES_TRUE;
} }
static const void *ares__parse_ipaddr(const char *ipaddr, static const void *ares__parse_ipaddr(const char *ipaddr,
struct ares_addr *addr, struct ares_addr *addr, size_t *out_len)
size_t *out_len)
{ {
const void *ptr = NULL; const void *ptr = NULL;
size_t ptr_len = 0; size_t ptr_len = 0;
if (ipaddr == NULL || addr == NULL || out_len == NULL) if (ipaddr == NULL || addr == NULL || out_len == NULL) {
return NULL; return NULL;
}
*out_len = 0; *out_len = 0;
@ -225,8 +226,9 @@ static ares_bool_t ares__normalize_ipaddr(const char *ipaddr, char *out,
data.family = AF_UNSPEC; data.family = AF_UNSPEC;
addr = ares__parse_ipaddr(ipaddr, &data, &addr_len); addr = ares__parse_ipaddr(ipaddr, &data, &addr_len);
if (addr == NULL) if (addr == NULL) {
return ARES_FALSE; return ARES_FALSE;
}
if (!ares_inet_ntop(data.family, addr, out, (ares_socklen_t)out_len)) { if (!ares_inet_ntop(data.family, addr, out, (ares_socklen_t)out_len)) {
return ARES_FALSE; return ARES_FALSE;
@ -237,15 +239,18 @@ static ares_bool_t ares__normalize_ipaddr(const char *ipaddr, char *out,
static void ares__hosts_entry_destroy(ares_hosts_entry_t *entry) static void ares__hosts_entry_destroy(ares_hosts_entry_t *entry)
{ {
if (entry == NULL) if (entry == NULL) {
return; return;
}
/* Honor reference counting */ /* Honor reference counting */
if (entry->refcnt != 0) if (entry->refcnt != 0) {
entry->refcnt--; entry->refcnt--;
}
if (entry->refcnt > 0) if (entry->refcnt > 0) {
return; return;
}
ares__llist_destroy(entry->hosts); ares__llist_destroy(entry->hosts);
ares__llist_destroy(entry->ips); ares__llist_destroy(entry->ips);
@ -259,8 +264,9 @@ static void ares__hosts_entry_destroy_cb(void *entry)
void ares__hosts_file_destroy(ares_hosts_file_t *hf) void ares__hosts_file_destroy(ares_hosts_file_t *hf)
{ {
if (hf == NULL) if (hf == NULL) {
return; return;
}
ares_free(hf->filename); ares_free(hf->filename);
ares__htable_strvp_destroy(hf->hosthash); ares__htable_strvp_destroy(hf->hosthash);
@ -299,38 +305,40 @@ fail:
return NULL; return NULL;
} }
static ares_bool_t ares__hosts_entry_ipaddr_exists( static ares_bool_t ares__hosts_entry_ipaddr_exists(ares_hosts_entry_t *entry,
ares_hosts_entry_t *entry, const char *ipaddr) const char *ipaddr)
{ {
ares__llist_node_t *node; ares__llist_node_t *node;
for (node = ares__llist_node_first(entry->ips); node != NULL; for (node = ares__llist_node_first(entry->ips); node != NULL;
node = ares__llist_node_next(node)) { node = ares__llist_node_next(node)) {
const char *myaddr = ares__llist_node_val(node); const char *myaddr = ares__llist_node_val(node);
if (strcmp(myaddr, ipaddr) == 0) if (strcmp(myaddr, ipaddr) == 0) {
return ARES_TRUE; return ARES_TRUE;
} }
}
return ARES_FALSE; return ARES_FALSE;
} }
static ares_bool_t ares__hosts_entry_host_exists( static ares_bool_t ares__hosts_entry_host_exists(ares_hosts_entry_t *entry,
ares_hosts_entry_t *entry, const char *host) const char *host)
{ {
ares__llist_node_t *node; ares__llist_node_t *node;
for (node = ares__llist_node_first(entry->ips); node != NULL; for (node = ares__llist_node_first(entry->ips); node != NULL;
node = ares__llist_node_next(node)) { node = ares__llist_node_next(node)) {
const char *myhost = ares__llist_node_val(node); const char *myhost = ares__llist_node_val(node);
if (strcasecmp(myhost, host) == 0) if (strcasecmp(myhost, host) == 0) {
return ARES_TRUE; return ARES_TRUE;
} }
}
return ARES_FALSE; return ARES_FALSE;
} }
static ares_status_t ares__hosts_file_merge_entry( static ares_status_t ares__hosts_file_merge_entry(ares_hosts_entry_t *existing,
ares_hosts_entry_t *existing, ares_hosts_entry_t *entry) ares_hosts_entry_t *entry)
{ {
ares__llist_node_t *node; ares__llist_node_t *node;
@ -373,8 +381,8 @@ typedef enum {
ARES_MATCH_HOST = 2 ARES_MATCH_HOST = 2
} ares_hosts_file_match_t; } ares_hosts_file_match_t;
static ares_hosts_file_match_t ares__hosts_file_match( static ares_hosts_file_match_t
const ares_hosts_file_t *hf, ares_hosts_entry_t *entry, ares__hosts_file_match(const ares_hosts_file_t *hf, ares_hosts_entry_t *entry,
ares_hosts_entry_t **match) ares_hosts_entry_t **match)
{ {
ares__llist_node_t *node; ares__llist_node_t *node;
@ -384,17 +392,19 @@ static ares_hosts_file_match_t ares__hosts_file_match(
node = ares__llist_node_next(node)) { node = ares__llist_node_next(node)) {
const char *ipaddr = ares__llist_node_val(node); const char *ipaddr = ares__llist_node_val(node);
*match = ares__htable_strvp_get_direct(hf->iphash, ipaddr); *match = ares__htable_strvp_get_direct(hf->iphash, ipaddr);
if (*match != NULL) if (*match != NULL) {
return ARES_MATCH_IPADDR; return ARES_MATCH_IPADDR;
} }
}
for (node = ares__llist_node_first(entry->hosts); node != NULL; for (node = ares__llist_node_first(entry->hosts); node != NULL;
node = ares__llist_node_next(node)) { node = ares__llist_node_next(node)) {
const char *host = ares__llist_node_val(node); const char *host = ares__llist_node_val(node);
*match = ares__htable_strvp_get_direct(hf->hosthash, host); *match = ares__htable_strvp_get_direct(hf->hosthash, host);
if (*match != NULL) if (*match != NULL) {
return ARES_MATCH_HOST; return ARES_MATCH_HOST;
} }
}
return ARES_MATCH_NONE; return ARES_MATCH_NONE;
} }
@ -438,8 +448,9 @@ static ares_status_t ares__hosts_file_add(ares_hosts_file_t *hosts,
/* first hostname match wins. If we detect a duplicate hostname for another /* first hostname match wins. If we detect a duplicate hostname for another
* ip it will automatically be added to the same entry */ * ip it will automatically be added to the same entry */
if (ares__htable_strvp_get(hosts->hosthash, val, NULL)) if (ares__htable_strvp_get(hosts->hosthash, val, NULL)) {
continue; continue;
}
if (!ares__htable_strvp_insert(hosts->hosthash, val, entry)) { if (!ares__htable_strvp_insert(hosts->hosthash, val, entry)) {
return ARES_ENOMEM; return ARES_ENOMEM;
@ -453,8 +464,9 @@ static ares_status_t ares__parse_hosts_hostnames(ares__buf_t *buf,
ares_hosts_entry_t *entry) ares_hosts_entry_t *entry)
{ {
entry->hosts = ares__llist_create(ares_free); entry->hosts = ares__llist_create(ares_free);
if (entry->hosts == NULL) if (entry->hosts == NULL) {
return ARES_ENOMEM; return ARES_ENOMEM;
}
/* Parse hostnames and aliases */ /* Parse hostnames and aliases */
while (ares__buf_len(buf)) { while (ares__buf_len(buf)) {
@ -465,8 +477,9 @@ static ares_status_t ares__parse_hosts_hostnames(ares__buf_t *buf,
ares__buf_consume_whitespace(buf, ARES_FALSE); ares__buf_consume_whitespace(buf, ARES_FALSE);
if (ares__buf_len(buf) == 0) if (ares__buf_len(buf) == 0) {
break; break;
}
/* See if it is a comment, if so stop processing */ /* See if it is a comment, if so stop processing */
if (ares__buf_begins_with(buf, &comment, 1)) { if (ares__buf_begins_with(buf, &comment, 1)) {
@ -476,31 +489,36 @@ static ares_status_t ares__parse_hosts_hostnames(ares__buf_t *buf,
ares__buf_tag(buf); ares__buf_tag(buf);
/* Must be at end of line */ /* Must be at end of line */
if (ares__buf_consume_nonwhitespace(buf) == 0) if (ares__buf_consume_nonwhitespace(buf) == 0) {
break; break;
}
status = ares__buf_tag_fetch_string(buf, hostname, sizeof(hostname)); status = ares__buf_tag_fetch_string(buf, hostname, sizeof(hostname));
if (status != ARES_SUCCESS) { if (status != ARES_SUCCESS) {
/* Bad entry, just ignore as long as its not the first. If its the first, /* Bad entry, just ignore as long as its not the first. If its the first,
* it must be valid */ * it must be valid */
if (ares__llist_len(entry->hosts) == 0) if (ares__llist_len(entry->hosts) == 0) {
return ARES_EBADSTR; return ARES_EBADSTR;
}
continue; continue;
} }
/* Validate it is a valid hostname characterset */ /* Validate it is a valid hostname characterset */
if (!ares__is_hostname(hostname)) if (!ares__is_hostname(hostname)) {
continue; continue;
}
/* Don't add a duplicate to the same entry */ /* Don't add a duplicate to the same entry */
if (ares__hosts_entry_host_exists(entry, hostname)) if (ares__hosts_entry_host_exists(entry, hostname)) {
continue; continue;
}
/* Add to list */ /* Add to list */
temp = ares_strdup(hostname); temp = ares_strdup(hostname);
if (temp == NULL) if (temp == NULL) {
return ARES_ENOMEM; return ARES_ENOMEM;
}
if (ares__llist_insert_last(entry->hosts, temp) == NULL) { if (ares__llist_insert_last(entry->hosts, temp) == NULL) {
ares_free(temp); ares_free(temp);
@ -509,13 +527,13 @@ static ares_status_t ares__parse_hosts_hostnames(ares__buf_t *buf,
} }
/* Must have at least 1 entry */ /* Must have at least 1 entry */
if (ares__llist_len(entry->hosts) == 0) if (ares__llist_len(entry->hosts) == 0) {
return ARES_EBADSTR; return ARES_EBADSTR;
}
return ARES_SUCCESS; return ARES_SUCCESS;
} }
static ares_status_t ares__parse_hosts_ipaddr(ares__buf_t *buf, static ares_status_t ares__parse_hosts_ipaddr(ares__buf_t *buf,
ares_hosts_entry_t **entry_out) ares_hosts_entry_t **entry_out)
{ {
@ -566,7 +584,6 @@ static ares_status_t ares__parse_hosts_ipaddr(ares__buf_t *buf,
return ARES_SUCCESS; return ARES_SUCCESS;
} }
static ares_status_t ares__parse_hosts(const char *filename, static ares_status_t ares__parse_hosts(const char *filename,
ares_hosts_file_t **out) ares_hosts_file_t **out)
{ {
@ -584,8 +601,9 @@ static ares_status_t ares__parse_hosts(const char *filename,
} }
status = ares__read_file_into_buf(filename, buf); status = ares__read_file_into_buf(filename, buf);
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS) {
goto done; goto done;
}
hf = ares__hosts_file_create(filename); hf = ares__hosts_file_create(filename);
if (hf == NULL) { if (hf == NULL) {
@ -613,8 +631,9 @@ static ares_status_t ares__parse_hosts(const char *filename,
/* Pull off ip address */ /* Pull off ip address */
status = ares__parse_hosts_ipaddr(buf, &entry); status = ares__parse_hosts_ipaddr(buf, &entry);
if (status == ARES_ENOMEM) if (status == ARES_ENOMEM) {
goto done; goto done;
}
if (status != ARES_SUCCESS) { if (status != ARES_SUCCESS) {
/* Bad line, consume and go onto next */ /* Bad line, consume and go onto next */
ares__buf_consume_line(buf, ARES_TRUE); ares__buf_consume_line(buf, ARES_TRUE);
@ -657,7 +676,6 @@ done:
return status; return status;
} }
static ares_bool_t ares__hosts_expired(const char *filename, static ares_bool_t ares__hosts_expired(const char *filename,
const ares_hosts_file_t *hf) const ares_hosts_file_t *hf)
{ {
@ -677,8 +695,9 @@ static ares_bool_t ares__hosts_expired(const char *filename,
(void)filename; (void)filename;
#endif #endif
if (hf == NULL) if (hf == NULL) {
return ARES_TRUE; return ARES_TRUE;
}
/* Expire every 60s if we can't get a time */ /* Expire every 60s if we can't get a time */
if (mod_ts == 0) { if (mod_ts == 0) {
@ -686,16 +705,17 @@ static ares_bool_t ares__hosts_expired(const char *filename,
} }
/* If filenames are different, its expired */ /* If filenames are different, its expired */
if (strcasecmp(hf->filename, filename) != 0) if (strcasecmp(hf->filename, filename) != 0) {
return ARES_TRUE; return ARES_TRUE;
}
if (hf->ts <= mod_ts) if (hf->ts <= mod_ts) {
return ARES_TRUE; return ARES_TRUE;
}
return ARES_FALSE; return ARES_FALSE;
} }
static ares_status_t ares__hosts_path(ares_channel channel, ares_bool_t use_env, static ares_status_t ares__hosts_path(ares_channel channel, ares_bool_t use_env,
char **path) char **path)
{ {
@ -766,7 +786,6 @@ static ares_status_t ares__hosts_path(ares_channel channel, ares_bool_t use_env,
return ARES_SUCCESS; return ARES_SUCCESS;
} }
static ares_status_t ares__hosts_update(ares_channel channel, static ares_status_t ares__hosts_update(ares_channel channel,
ares_bool_t use_env) ares_bool_t use_env)
{ {
@ -774,8 +793,9 @@ static ares_status_t ares__hosts_update(ares_channel channel,
char *filename = NULL; char *filename = NULL;
status = ares__hosts_path(channel, use_env, &filename); status = ares__hosts_path(channel, use_env, &filename);
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS) {
return status; return status;
}
if (!ares__hosts_expired(filename, channel->hf)) { if (!ares__hosts_expired(filename, channel->hf)) {
ares_free(filename); ares_free(filename);
@ -800,25 +820,28 @@ ares_status_t ares__hosts_search_ipaddr(ares_channel channel,
*entry = NULL; *entry = NULL;
status = ares__hosts_update(channel, use_env); status = ares__hosts_update(channel, use_env);
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS) {
return status; return status;
}
if (channel->hf == NULL) if (channel->hf == NULL) {
return ARES_ENOTFOUND; return ARES_ENOTFOUND;
}
if (!ares__normalize_ipaddr(ipaddr, addr, sizeof(addr))) { if (!ares__normalize_ipaddr(ipaddr, addr, sizeof(addr))) {
return ARES_EBADNAME; return ARES_EBADNAME;
} }
*entry = ares__htable_strvp_get_direct(channel->hf->iphash, addr); *entry = ares__htable_strvp_get_direct(channel->hf->iphash, addr);
if (*entry == NULL) if (*entry == NULL) {
return ARES_ENOTFOUND; return ARES_ENOTFOUND;
}
return ARES_SUCCESS; return ARES_SUCCESS;
} }
ares_status_t ares__hosts_search_host(ares_channel channel, ares_status_t ares__hosts_search_host(ares_channel channel, ares_bool_t use_env,
ares_bool_t use_env, const char *host, const char *host,
const ares_hosts_entry_t **entry) const ares_hosts_entry_t **entry)
{ {
ares_status_t status; ares_status_t status;
@ -826,11 +849,13 @@ ares_status_t ares__hosts_search_host(ares_channel channel,
*entry = NULL; *entry = NULL;
status = ares__hosts_update(channel, use_env); status = ares__hosts_update(channel, use_env);
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS) {
return status; return status;
}
if (channel->hf == NULL) if (channel->hf == NULL) {
return ARES_ENOTFOUND; return ARES_ENOTFOUND;
}
*entry = ares__htable_strvp_get_direct(channel->hf->hosthash, host); *entry = ares__htable_strvp_get_direct(channel->hf->hosthash, host);
if (*entry == NULL) { if (*entry == NULL) {
@ -841,8 +866,7 @@ ares_status_t ares__hosts_search_host(ares_channel channel,
} }
ares_status_t ares__hosts_entry_to_hostent(const ares_hosts_entry_t *entry, ares_status_t ares__hosts_entry_to_hostent(const ares_hosts_entry_t *entry,
int family, int family, struct hostent **hostent)
struct hostent **hostent)
{ {
ares_status_t status; ares_status_t status;
size_t naliases; size_t naliases;
@ -871,8 +895,9 @@ ares_status_t ares__hosts_entry_to_hostent(const ares_hosts_entry_t *entry,
addr.family = family; addr.family = family;
ptr = ares__parse_ipaddr(ipaddr, &addr, &ptr_len); ptr = ares__parse_ipaddr(ipaddr, &addr, &ptr_len);
if (ptr == NULL) if (ptr == NULL) {
continue; continue;
}
/* If family == AF_UNSPEC, then we want to inherit this for future /* If family == AF_UNSPEC, then we want to inherit this for future
* conversions as we can only support a single address class */ * conversions as we can only support a single address class */
@ -917,8 +942,8 @@ ares_status_t ares__hosts_entry_to_hostent(const ares_hosts_entry_t *entry,
/* Copy aliases */ /* Copy aliases */
naliases = ares__llist_len(entry->hosts) - 1; naliases = ares__llist_len(entry->hosts) - 1;
(*hostent)->h_aliases = ares_malloc_zero((naliases + 1) * (*hostent)->h_aliases =
sizeof((*hostent)->h_aliases)); ares_malloc_zero((naliases + 1) * sizeof((*hostent)->h_aliases));
if ((*hostent)->h_aliases == NULL) { if ((*hostent)->h_aliases == NULL) {
status = ARES_ENOMEM; status = ARES_ENOMEM;
goto fail; goto fail;
@ -946,8 +971,9 @@ fail:
return status; return status;
} }
static ares_status_t ares__hosts_ai_append_cnames( static ares_status_t
const ares_hosts_entry_t *entry, struct ares_addrinfo_cname **cnames_out) ares__hosts_ai_append_cnames(const ares_hosts_entry_t *entry,
struct ares_addrinfo_cname **cnames_out)
{ {
struct ares_addrinfo_cname *cname = NULL; struct ares_addrinfo_cname *cname = NULL;
struct ares_addrinfo_cname *cnames = NULL; struct ares_addrinfo_cname *cnames = NULL;
@ -1011,8 +1037,7 @@ done:
} }
ares_status_t ares__hosts_entry_to_addrinfo(const ares_hosts_entry_t *entry, ares_status_t ares__hosts_entry_to_addrinfo(const ares_hosts_entry_t *entry,
const char *name, const char *name, int family,
int family,
unsigned short port, unsigned short port,
ares_bool_t want_cnames, ares_bool_t want_cnames,
struct ares_addrinfo *ai) struct ares_addrinfo *ai)

@ -113,8 +113,7 @@ fail:
} }
ares_bool_t ares__htable_strvp_insert(ares__htable_strvp_t *htable, ares_bool_t ares__htable_strvp_insert(ares__htable_strvp_t *htable,
const char *key, const char *key, void *val)
void *val)
{ {
ares__htable_strvp_bucket_t *bucket = NULL; ares__htable_strvp_bucket_t *bucket = NULL;

@ -304,13 +304,13 @@ void *ares__slist_node_claim(ares__slist_node_t *node)
return val; return val;
} }
void ares__slist_node_reinsert(ares__slist_node_t *node) void ares__slist_node_reinsert(ares__slist_node_t *node)
{ {
ares__slist_t *list; ares__slist_t *list;
if (node == NULL) if (node == NULL) {
return; return;
}
list = node->parent; list = node->parent;
@ -318,7 +318,6 @@ void ares__slist_node_reinsert(ares__slist_node_t *node)
ares__slist_node_push(list, node); ares__slist_node_push(list, node);
} }
ares__slist_node_t *ares__slist_node_find(const ares__slist_t *list, ares__slist_node_t *ares__slist_node_find(const ares__slist_t *list,
const void *val) const void *val)
{ {

@ -450,4 +450,3 @@ void ares_set_socket_functions(ares_channel channel,
channel->sock_funcs = funcs; channel->sock_funcs = funcs;
channel->sock_func_cb_data = data; channel->sock_func_cb_data = data;
} }

@ -70,7 +70,8 @@ struct addrinfo_sort_elem {
#define ARES_IPV6_ADDR_SCOPE_ORGLOCAL 0x08 #define ARES_IPV6_ADDR_SCOPE_ORGLOCAL 0x08
#define ARES_IPV6_ADDR_SCOPE_GLOBAL 0x0e #define ARES_IPV6_ADDR_SCOPE_GLOBAL 0x0e
#define ARES_IN_LOOPBACK(a) ((((long unsigned int)(a)) & 0xff000000) == 0x7f000000) #define ARES_IN_LOOPBACK(a) \
((((long unsigned int)(a)) & 0xff000000) == 0x7f000000)
/* RFC 4193. */ /* RFC 4193. */
#define ARES_IN6_IS_ADDR_ULA(a) (((a)->s6_addr[0] & 0xfe) == 0xfc) #define ARES_IN6_IS_ADDR_ULA(a) (((a)->s6_addr[0] & 0xfe) == 0xfc)

@ -32,7 +32,6 @@
#include "ares.h" #include "ares.h"
#include "ares_private.h" #include "ares_private.h"
void ares_destroy(ares_channel channel) void ares_destroy(ares_channel channel)
{ {
size_t i; size_t i;
@ -93,11 +92,11 @@ void ares_destroy(ares_channel channel)
ares_free(channel); ares_free(channel);
} }
void ares__destroy_server(struct server_state *server) void ares__destroy_server(struct server_state *server)
{ {
if (server == NULL) if (server == NULL) {
return; return;
}
ares__close_sockets(server); ares__close_sockets(server);
ares__llist_destroy(server->connections); ares__llist_destroy(server->connections);

@ -108,8 +108,9 @@ struct ares_addrinfo_cname *
struct ares_addrinfo_cname *tail = ares_malloc_zero(sizeof(*tail)); struct ares_addrinfo_cname *tail = ares_malloc_zero(sizeof(*tail));
struct ares_addrinfo_cname *last = *head; struct ares_addrinfo_cname *last = *head;
if (tail == NULL) if (tail == NULL) {
return NULL; return NULL;
}
if (!last) { if (!last) {
*head = tail; *head = tail;
@ -147,8 +148,9 @@ struct ares_addrinfo_node *
struct ares_addrinfo_node *tail = ares_malloc_zero(sizeof(*tail)); struct ares_addrinfo_node *tail = ares_malloc_zero(sizeof(*tail));
struct ares_addrinfo_node *last = *head; struct ares_addrinfo_node *last = *head;
if (tail == NULL) if (tail == NULL) {
return NULL; return NULL;
}
if (!last) { if (!last) {
*head = tail; *head = tail;
@ -385,7 +387,8 @@ static ares_status_t file_lookup(struct host_query *hquery)
return ARES_ENOTFOUND; return ARES_ENOTFOUND;
} }
status = ares__hosts_search_host(hquery->channel, status = ares__hosts_search_host(
hquery->channel,
(hquery->hints.ai_flags & ARES_AI_ENVHOSTS) ? ARES_TRUE : ARES_FALSE, (hquery->hints.ai_flags & ARES_AI_ENVHOSTS) ? ARES_TRUE : ARES_FALSE,
hquery->name, &entry); hquery->name, &entry);
@ -393,9 +396,8 @@ static ares_status_t file_lookup(struct host_query *hquery)
goto done; goto done;
} }
status = ares__hosts_entry_to_addrinfo(entry, hquery->name, status = ares__hosts_entry_to_addrinfo(
hquery->hints.ai_family, entry, hquery->name, hquery->hints.ai_family, hquery->port,
hquery->port,
(hquery->hints.ai_flags & ARES_AI_CANONNAME) ? ARES_TRUE : ARES_FALSE, (hquery->hints.ai_flags & ARES_AI_CANONNAME) ? ARES_TRUE : ARES_FALSE,
hquery->ai); hquery->ai);
@ -451,7 +453,8 @@ static void next_lookup(struct host_query *hquery, ares_status_t status)
} }
} }
static void terminate_retries(const struct host_query *hquery, unsigned short qid) static void terminate_retries(const struct host_query *hquery,
unsigned short qid)
{ {
unsigned short term_qid = unsigned short term_qid =
(qid == hquery->qid_a) ? hquery->qid_aaaa : hquery->qid_a; (qid == hquery->qid_a) ? hquery->qid_aaaa : hquery->qid_a;

@ -189,20 +189,23 @@ static ares_status_t file_lookup(ares_channel channel,
ptr = &addr->addr.addr6; ptr = &addr->addr.addr6;
} }
if (ptr == NULL) if (ptr == NULL) {
return ARES_ENOTFOUND; return ARES_ENOTFOUND;
}
if (!ares_inet_ntop(addr->family, ptr, ipaddr, sizeof(ipaddr))) { if (!ares_inet_ntop(addr->family, ptr, ipaddr, sizeof(ipaddr))) {
return ARES_ENOTFOUND; return ARES_ENOTFOUND;
} }
status = ares__hosts_search_ipaddr(channel, ARES_FALSE, ipaddr, &entry); status = ares__hosts_search_ipaddr(channel, ARES_FALSE, ipaddr, &entry);
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS) {
return status; return status;
}
status = ares__hosts_entry_to_hostent(entry, addr->family, host); status = ares__hosts_entry_to_hostent(entry, addr->family, host);
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS) {
return status; return status;
}
return ARES_SUCCESS; return ARES_SUCCESS;
} }

@ -230,7 +230,6 @@ static size_t get6_address_index(const struct ares_in6_addr *addr,
return i; return i;
} }
static ares_status_t ares__hostent_localhost(const char *name, int family, static ares_status_t ares__hostent_localhost(const char *name, int family,
struct hostent **host_out) struct hostent **host_out)
{ {
@ -267,7 +266,6 @@ done:
int ares_gethostbyname_file(ares_channel channel, const char *name, int family, int ares_gethostbyname_file(ares_channel channel, const char *name, int family,
struct hostent **host) struct hostent **host)
{ {
const ares_hosts_entry_t *entry; const ares_hosts_entry_t *entry;
ares_status_t status; ares_status_t status;
@ -275,8 +273,9 @@ int ares_gethostbyname_file(ares_channel channel, const char *name, int family,
if (channel == NULL || name == NULL || host == NULL) { if (channel == NULL || name == NULL || host == NULL) {
/* Anything will do, really. This seems fine, and is consistent with /* Anything will do, really. This seems fine, and is consistent with
other error cases. */ other error cases. */
if (host != NULL) if (host != NULL) {
*host = NULL; *host = NULL;
}
return ARES_ENOTFOUND; return ARES_ENOTFOUND;
} }
@ -308,4 +307,3 @@ done:
return (int)status; return (int)status;
} }

@ -103,21 +103,26 @@ static int server_sort_cb(const void *data1, const void *data2)
const struct server_state *s1 = data1; const struct server_state *s1 = data1;
const struct server_state *s2 = data2; const struct server_state *s2 = data2;
if (s1->consec_failures < s2->consec_failures) if (s1->consec_failures < s2->consec_failures) {
return -1; return -1;
if (s1->consec_failures > s2->consec_failures) }
if (s1->consec_failures > s2->consec_failures) {
return 1; return 1;
if (s1->idx < s2->idx) }
if (s1->idx < s2->idx) {
return -1; return -1;
if (s1->idx > s2->idx) }
if (s1->idx > s2->idx) {
return 1; return 1;
}
return 0; return 0;
} }
static void server_destroy_cb(void *data) static void server_destroy_cb(void *data)
{ {
if (data == NULL) if (data == NULL) {
return; return;
}
ares__destroy_server(data); ares__destroy_server(data);
} }
@ -153,15 +158,17 @@ static ares_status_t init_by_defaults(ares_channel channel)
addr.addr.addr4.s_addr = htonl(INADDR_LOOPBACK); addr.addr.addr4.s_addr = htonl(INADDR_LOOPBACK);
rc = ares__sconfig_append(&sconfig, &addr, 0, 0); rc = ares__sconfig_append(&sconfig, &addr, 0, 0);
if (rc != ARES_SUCCESS) if (rc != ARES_SUCCESS) {
return rc; return rc;
}
rc = ares__servers_update(channel, sconfig, ARES_FALSE); rc = ares__servers_update(channel, sconfig, ARES_FALSE);
ares__llist_destroy(sconfig); ares__llist_destroy(sconfig);
if (rc != ARES_SUCCESS) if (rc != ARES_SUCCESS) {
return rc; return rc;
} }
}
#if defined(USE_WINSOCK) #if defined(USE_WINSOCK)
# define toolong(x) (x == -1) && (SOCKERRNO == WSAEFAULT) # define toolong(x) (x == -1) && (SOCKERRNO == WSAEFAULT)
@ -303,8 +310,8 @@ int ares_init_options(ares_channel *channelptr, struct ares_options *options,
} }
/* Initialize Server List */ /* Initialize Server List */
channel->servers = ares__slist_create(channel->rand_state, server_sort_cb, channel->servers =
server_destroy_cb); ares__slist_create(channel->rand_state, server_sort_cb, server_destroy_cb);
if (channel->servers == NULL) { if (channel->servers == NULL) {
status = ARES_ENOMEM; status = ARES_ENOMEM;
goto done; goto done;
@ -449,7 +456,6 @@ int ares_dup(ares_channel *dest, ares_channel src)
return ARES_SUCCESS; /* everything went fine */ return ARES_SUCCESS; /* everything went fine */
} }
void ares_set_local_ip4(ares_channel channel, unsigned int local_ip) void ares_set_local_ip4(ares_channel channel, unsigned int local_ip)
{ {
channel->local_ip4 = local_ip; channel->local_ip4 = local_ip;
@ -469,7 +475,6 @@ void ares_set_local_dev(ares_channel channel, const char *local_dev_name)
channel->local_dev_name[sizeof(channel->local_dev_name) - 1] = 0; channel->local_dev_name[sizeof(channel->local_dev_name) - 1] = 0;
} }
int ares_set_sortlist(ares_channel channel, const char *sortstr) int ares_set_sortlist(ares_channel channel, const char *sortstr)
{ {
size_t nsort = 0; size_t nsort = 0;
@ -493,4 +498,3 @@ int ares_set_sortlist(ares_channel channel, const char *sortstr)
} }
return (int)status; return (int)status;
} }

@ -57,21 +57,22 @@ static struct in_addr *ares_save_opt_servers(ares_channel channel,
int *nservers) int *nservers)
{ {
ares__slist_node_t *snode; ares__slist_node_t *snode;
struct in_addr *out = ares_malloc_zero(ares__slist_len(channel->servers) * struct in_addr *out =
sizeof(*out)); ares_malloc_zero(ares__slist_len(channel->servers) * sizeof(*out));
*nservers = 0; *nservers = 0;
if (out == NULL) if (out == NULL) {
return NULL; return NULL;
}
for (snode = ares__slist_node_first(channel->servers); snode != NULL; for (snode = ares__slist_node_first(channel->servers); snode != NULL;
snode = ares__slist_node_next(snode)) { snode = ares__slist_node_next(snode)) {
const struct server_state *server = ares__slist_node_val(snode); const struct server_state *server = ares__slist_node_val(snode);
if (server->addr.family != AF_INET) if (server->addr.family != AF_INET) {
continue; continue;
}
memcpy(&out[*nservers], &server->addr.addr.addr4, sizeof(*out)); memcpy(&out[*nservers], &server->addr.addr.addr4, sizeof(*out));
(*nservers)++; (*nservers)++;
@ -80,7 +81,6 @@ static struct in_addr *ares_save_opt_servers(ares_channel channel,
return out; return out;
} }
/* Save options from initialized channel */ /* Save options from initialized channel */
int ares_save_options(ares_channel channel, struct ares_options *options, int ares_save_options(ares_channel channel, struct ares_options *options,
int *optmask) int *optmask)
@ -222,7 +222,6 @@ int ares_save_options(ares_channel channel, struct ares_options *options,
return ARES_SUCCESS; return ARES_SUCCESS;
} }
static ares_status_t ares__init_options_servers(ares_channel channel, static ares_status_t ares__init_options_servers(ares_channel channel,
const struct in_addr *servers, const struct in_addr *servers,
size_t nservers) size_t nservers)
@ -231,8 +230,9 @@ static ares_status_t ares__init_options_servers(ares_channel channel,
ares_status_t status; ares_status_t status;
slist = ares_in_addr_to_server_config_llist(servers, nservers); slist = ares_in_addr_to_server_config_llist(servers, nservers);
if (slist == NULL) if (slist == NULL) {
return ARES_ENOMEM; return ARES_ENOMEM;
}
status = ares__servers_update(channel, slist, ARES_TRUE); status = ares__servers_update(channel, slist, ARES_TRUE);
@ -247,8 +247,9 @@ ares_status_t ares__init_by_options(ares_channel channel,
{ {
size_t i; size_t i;
if (channel == NULL) if (channel == NULL) {
return ARES_ENODATA; return ARES_ENODATA;
}
if (options == NULL) { if (options == NULL) {
if (optmask != 0) { if (optmask != 0) {
@ -377,9 +378,10 @@ ares_status_t ares__init_by_options(ares_channel channel,
ares_status_t status; ares_status_t status;
status = ares__init_options_servers(channel, options->servers, status = ares__init_options_servers(channel, options->servers,
(size_t)options->nservers); (size_t)options->nservers);
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS) {
return status; return status;
} }
}
channel->optmask = (unsigned int)optmask; channel->optmask = (unsigned int)optmask;

@ -388,7 +388,6 @@ ares_status_t ares__init_by_options(ares_channel channel,
int optmask); int optmask);
ares_status_t ares__init_by_sysconfig(ares_channel channel); ares_status_t ares__init_by_sysconfig(ares_channel channel);
typedef struct { typedef struct {
ares__llist_t *sconfig; ares__llist_t *sconfig;
struct apattern *sortlist; struct apattern *sortlist;
@ -401,6 +400,7 @@ typedef struct {
ares_bool_t rotate; ares_bool_t rotate;
size_t timeout_ms; size_t timeout_ms;
} ares_sysconfig_t; } ares_sysconfig_t;
ares_status_t ares__init_by_environment(ares_sysconfig_t *sysconfig); ares_status_t ares__init_by_environment(ares_sysconfig_t *sysconfig);
ares_status_t ares__init_sysconfig_files(ares_channel channel, ares_status_t ares__init_sysconfig_files(ares_channel channel,
@ -465,8 +465,7 @@ ares_ssize_t ares__socket_recv(ares_channel channel, ares_socket_t s,
void *data, size_t data_len); void *data, size_t data_len);
void ares__close_socket(ares_channel, ares_socket_t); void ares__close_socket(ares_channel, ares_socket_t);
int ares__connect_socket(ares_channel channel, ares_socket_t sockfd, int ares__connect_socket(ares_channel channel, ares_socket_t sockfd,
const struct sockaddr *addr, const struct sockaddr *addr, ares_socklen_t addrlen);
ares_socklen_t addrlen);
ares_bool_t ares__is_hostnamech(int ch); ares_bool_t ares__is_hostnamech(int ch);
void ares__destroy_server(struct server_state *server); void ares__destroy_server(struct server_state *server);
@ -479,8 +478,9 @@ ares_status_t ares__sconfig_append(ares__llist_t **sconfig,
unsigned short tcp_port); unsigned short tcp_port);
ares_status_t ares__sconfig_append_fromstr(ares__llist_t **sconfig, ares_status_t ares__sconfig_append_fromstr(ares__llist_t **sconfig,
const char *str); const char *str);
ares__llist_t *ares_in_addr_to_server_config_llist( ares__llist_t *
const struct in_addr *servers, size_t nservers); ares_in_addr_to_server_config_llist(const struct in_addr *servers,
size_t nservers);
struct ares_hosts_entry; struct ares_hosts_entry;
typedef struct ares_hosts_entry ares_hosts_entry_t; typedef struct ares_hosts_entry ares_hosts_entry_t;
@ -489,15 +489,14 @@ void ares__hosts_file_destroy(ares_hosts_file_t *hf);
ares_status_t ares__hosts_search_ipaddr(ares_channel channel, ares_status_t ares__hosts_search_ipaddr(ares_channel channel,
ares_bool_t use_env, const char *ipaddr, ares_bool_t use_env, const char *ipaddr,
const ares_hosts_entry_t **entry); const ares_hosts_entry_t **entry);
ares_status_t ares__hosts_search_host(ares_channel channel, ares_status_t ares__hosts_search_host(ares_channel channel, ares_bool_t use_env,
ares_bool_t use_env, const char *host, const char *host,
const ares_hosts_entry_t **entry); const ares_hosts_entry_t **entry);
ares_status_t ares__hosts_entry_to_hostent(const ares_hosts_entry_t *entry, ares_status_t ares__hosts_entry_to_hostent(const ares_hosts_entry_t *entry,
int family, int family,
struct hostent **hostent); struct hostent **hostent);
ares_status_t ares__hosts_entry_to_addrinfo(const ares_hosts_entry_t *entry, ares_status_t ares__hosts_entry_to_addrinfo(const ares_hosts_entry_t *entry,
const char *name, const char *name, int family,
int family,
unsigned short port, unsigned short port,
ares_bool_t want_cnames, ares_bool_t want_cnames,
struct ares_addrinfo *ai); struct ares_addrinfo *ai);

@ -54,8 +54,7 @@ static void read_packets(ares_channel channel, fd_set *read_fds,
ares_socket_t read_fd, struct timeval *now); ares_socket_t read_fd, struct timeval *now);
static void process_timeouts(ares_channel channel, struct timeval *now); static void process_timeouts(ares_channel channel, struct timeval *now);
static ares_status_t process_answer(ares_channel channel, static ares_status_t process_answer(ares_channel channel,
const unsigned char *abuf, const unsigned char *abuf, size_t alen,
size_t alen,
struct server_connection *conn, struct server_connection *conn,
ares_bool_t tcp, struct timeval *now); ares_bool_t tcp, struct timeval *now);
static void handle_conn_error(struct server_connection *conn, static void handle_conn_error(struct server_connection *conn,
@ -70,7 +69,6 @@ static void end_query(ares_channel channel, struct query *query,
ares_status_t status, const unsigned char *abuf, ares_status_t status, const unsigned char *abuf,
size_t alen); size_t alen);
static void server_increment_failures(struct server_state *server) static void server_increment_failures(struct server_state *server)
{ {
ares__slist_node_t *node; ares__slist_node_t *node;
@ -84,14 +82,14 @@ static void server_increment_failures(struct server_state *server)
ares__slist_node_reinsert(node); ares__slist_node_reinsert(node);
} }
static void server_set_good(struct server_state *server) static void server_set_good(struct server_state *server)
{ {
ares__slist_node_t *node; ares__slist_node_t *node;
ares_channel channel = server->channel; ares_channel channel = server->channel;
if (!server->consec_failures) if (!server->consec_failures) {
return; return;
}
node = ares__slist_node_find(channel->servers, server); node = ares__slist_node_find(channel->servers, server);
if (node == NULL) { if (node == NULL) {
@ -103,7 +101,8 @@ static void server_set_good(struct server_state *server)
} }
/* return true if now is exactly check time or later */ /* return true if now is exactly check time or later */
ares_bool_t ares__timedout(const struct timeval *now, const struct timeval *check) ares_bool_t ares__timedout(const struct timeval *now,
const struct timeval *check)
{ {
time_t secs = (now->tv_sec - check->tv_sec); time_t secs = (now->tv_sec - check->tv_sec);
@ -178,13 +177,15 @@ static ares_bool_t try_again(int errnum)
#endif #endif
#ifdef EWOULDBLOCK #ifdef EWOULDBLOCK
if (errnum == EWOULDBLOCK) if (errnum == EWOULDBLOCK) {
return ARES_TRUE; return ARES_TRUE;
}
#endif #endif
#if defined EAGAIN && EAGAIN != EWOULDBLOCK #if defined EAGAIN && EAGAIN != EWOULDBLOCK
if (errnum == EAGAIN) if (errnum == EAGAIN) {
return ARES_TRUE; return ARES_TRUE;
}
#endif #endif
return ARES_FALSE; return ARES_FALSE;
@ -254,7 +255,6 @@ static void write_tcp_data(ares_channel channel, fd_set *write_fds,
} }
} }
/* If any TCP socket selects true for reading, read some data, /* If any TCP socket selects true for reading, read some data,
* allocate a buffer if we finish reading the length word, and process * allocate a buffer if we finish reading the length word, and process
* a packet if we finish reading one. * a packet if we finish reading one.
@ -373,8 +373,8 @@ static ares_socket_t *channel_socket_list(ares_channel channel, size_t *num)
struct server_state *server = ares__slist_node_val(snode); struct server_state *server = ares__slist_node_val(snode);
ares__llist_node_t *node; ares__llist_node_t *node;
for (node = ares__llist_node_first(server->connections); for (node = ares__llist_node_first(server->connections); node != NULL;
node != NULL; node = ares__llist_node_next(node)) { node = ares__llist_node_next(node)) {
const struct server_connection *conn = ares__llist_node_val(node); const struct server_connection *conn = ares__llist_node_val(node);
if (conn->fd == ARES_SOCKET_BAD) { if (conn->fd == ARES_SOCKET_BAD) {
@ -559,8 +559,7 @@ static void process_timeouts(ares_channel channel, struct timeval *now)
* server connection! Return something other than ARES_SUCCESS to cause * server connection! Return something other than ARES_SUCCESS to cause
* the connection to be terminated after this call. */ * the connection to be terminated after this call. */
static ares_status_t process_answer(ares_channel channel, static ares_status_t process_answer(ares_channel channel,
const unsigned char *abuf, const unsigned char *abuf, size_t alen,
size_t alen,
struct server_connection *conn, struct server_connection *conn,
ares_bool_t tcp, struct timeval *now) ares_bool_t tcp, struct timeval *now)
{ {
@ -699,8 +698,7 @@ static void handle_conn_error(struct server_connection *conn,
ares_status_t ares__requeue_query(struct query *query, struct timeval *now) ares_status_t ares__requeue_query(struct query *query, struct timeval *now)
{ {
ares_channel channel = query->channel; ares_channel channel = query->channel;
size_t max_tries = ares__slist_len(channel->servers) * size_t max_tries = ares__slist_len(channel->servers) * channel->tries;
channel->tries;
query->try_count++; query->try_count++;
@ -709,14 +707,14 @@ ares_status_t ares__requeue_query(struct query *query, struct timeval *now)
} }
/* If we are here, all attempts to perform query failed. */ /* If we are here, all attempts to perform query failed. */
if (query->error_status == ARES_SUCCESS) if (query->error_status == ARES_SUCCESS) {
query->error_status = ARES_ETIMEOUT; query->error_status = ARES_ETIMEOUT;
}
end_query(channel, query, query->error_status, NULL, 0); end_query(channel, query, query->error_status, NULL, 0);
return ARES_ETIMEOUT; return ARES_ETIMEOUT;
} }
/* Pick a random server from the list, we first get a random number in the /* Pick a random server from the list, we first get a random number in the
* range of the number of servers, then scan until we find that server in * range of the number of servers, then scan until we find that server in
* the list */ * the list */
@ -735,7 +733,6 @@ static struct server_state *ares__random_server(ares_channel channel)
cnt = 0; cnt = 0;
for (node = ares__slist_node_first(channel->servers); node != NULL; for (node = ares__slist_node_first(channel->servers); node != NULL;
node = ares__slist_node_next(node)) { node = ares__slist_node_next(node)) {
if (cnt == idx) { if (cnt == idx) {
return ares__slist_node_val(node); return ares__slist_node_val(node);
} }
@ -932,8 +929,6 @@ ares_status_t ares__send_query(struct query *query, struct timeval *now)
return ARES_SUCCESS; return ARES_SUCCESS;
} }
static ares_bool_t same_questions(const unsigned char *qbuf, size_t qlen, static ares_bool_t same_questions(const unsigned char *qbuf, size_t qlen,
const ares_dns_record_t *arec) const ares_dns_record_t *arec)
{ {
@ -1061,5 +1056,3 @@ void ares__free_query(struct query *query)
ares_free(query); ares_free(query);
} }

@ -586,7 +586,6 @@ static ares_bool_t get_SuffixList_Windows(char **outptr)
return *outptr != NULL ? ARES_TRUE : ARES_FALSE; return *outptr != NULL ? ARES_TRUE : ARES_FALSE;
} }
static ares_status_t ares__init_sysconfig_windows(ares_sysconfig_t *sysconfig) static ares_status_t ares__init_sysconfig_windows(ares_sysconfig_t *sysconfig)
{ {
char *line = NULL; char *line = NULL;
@ -595,18 +594,20 @@ static ares_status_t ares__init_sysconfig_windows(ares_sysconfig_t *sysconfig)
if (get_DNS_Windows(&line)) { if (get_DNS_Windows(&line)) {
status = ares__sconfig_append_fromstr(&sysconfig->sconfig, line); status = ares__sconfig_append_fromstr(&sysconfig->sconfig, line);
ares_free(line); ares_free(line);
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS) {
goto done; goto done;
} }
}
if (get_SuffixList_Windows(&line)) { if (get_SuffixList_Windows(&line)) {
sysconfig->domains = ares__strsplit(line, ", ", &sysconfig->ndomains); sysconfig->domains = ares__strsplit(line, ", ", &sysconfig->ndomains);
if (sysconfig->domains == NULL) { if (sysconfig->domains == NULL) {
status = ARES_EFILE; status = ARES_EFILE;
} }
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS) {
goto done; goto done;
} }
}
done: done:
return status; return status;
@ -653,13 +654,14 @@ static ares_status_t ares__init_sysconfig_mvs(ares_sysconfig_t *sysconfig)
addr.addr.addr4.s_addr = addr_in->sin_addr.s_addr; addr.addr.addr4.s_addr = addr_in->sin_addr.s_addr;
addr.family = AF_INET; addr.family = AF_INET;
status = ares__sconfig_append(&sysconfig->sconfig, &addr, status =
htons(addr_in->sin_port), ares__sconfig_append(&sysconfig->sconfig, &addr, htons(addr_in->sin_port),
htons(addr_in->sin_port)); htons(addr_in->sin_port));
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS) {
return status; return status;
} }
}
for (i = 0; i < count6; i++) { for (i = 0; i < count6; i++) {
struct sockaddr_in6 *addr_in = &(v6->__stat_nsaddr_list[i]); struct sockaddr_in6 *addr_in = &(v6->__stat_nsaddr_list[i]);
@ -669,13 +671,14 @@ static ares_status_t ares__init_sysconfig_mvs(ares_sysconfig_t *sysconfig)
memcpy(&(addr.addr.addr6), &(addr_in->sin6_addr), memcpy(&(addr.addr.addr6), &(addr_in->sin6_addr),
sizeof(addr_in->sin6_addr)); sizeof(addr_in->sin6_addr));
status = ares__sconfig_append(&sysconfig->sconfig, &addr, status =
htons(addr_in->sin_port), ares__sconfig_append(&sysconfig->sconfig, &addr, htons(addr_in->sin_port),
htons(addr_in->sin_port)); htons(addr_in->sin_port));
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS) {
return status; return status;
} }
}
return ARES_SUCCESS; return ARES_SUCCESS;
} }
@ -735,17 +738,18 @@ static ares_status_t ares__init_sysconfig_watt32(ares_sysconfig_t *sysconfig)
status = ares__sconfig_append(&sysconfig->sconfig, &addr, 0, 0); status = ares__sconfig_append(&sysconfig->sconfig, &addr, 0, 0);
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS) {
return status; return status;
} }
}
return ARES_SUCCESS; return ARES_SUCCESS;
} }
#endif #endif
#if defined(ANDROID) || defined(__ANDROID__) #if defined(ANDROID) || defined(__ANDROID__)
static ares_status_t ares__init_sysconfig_android(ares_sysconfig_t *sysconfig) static ares_status_t
size_t i; ares__init_sysconfig_android(ares_sysconfig_t *sysconfig) size_t i;
char **dns_servers; char **dns_servers;
char *domains; char *domains;
size_t num_servers; size_t num_servers;
@ -831,8 +835,7 @@ static ares_status_t ares__init_sysconfig_libresolv(ares_sysconfig_t *sysconfig)
ares_inet_ntop(family, &addr[i].sin.sin_addr, ipaddr, sizeof(ipaddr)); ares_inet_ntop(family, &addr[i].sin.sin_addr, ipaddr, sizeof(ipaddr));
port = ntohs(addr[i].sin.sin_port); port = ntohs(addr[i].sin.sin_port);
} else if (family == AF_INET6) { } else if (family == AF_INET6) {
ares_inet_ntop(family, &addr[i].sin6.sin6_addr, ipaddr, ares_inet_ntop(family, &addr[i].sin6.sin6_addr, ipaddr, sizeof(ipaddr));
sizeof(ipaddr));
port = ntohs(addr[i].sin6.sin6_port); port = ntohs(addr[i].sin6.sin6_port);
} else { } else {
continue; continue;
@ -887,7 +890,8 @@ static ares_status_t ares__init_sysconfig_libresolv(ares_sysconfig_t *sysconfig)
} }
# ifdef __APPLE__ # ifdef __APPLE__
if (res.retry >= 0) { if (res.retry >= 0) {
sysconfig->timeout_ms /= ((unsigned int)res.retry + 1) * sysconfig->timeout_ms /=
((unsigned int)res.retry + 1) *
(unsigned int)(res.nscount > 0 ? res.nscount : 1); (unsigned int)(res.nscount > 0 ? res.nscount : 1);
} }
# endif # endif
@ -915,26 +919,29 @@ static ares_status_t ares_sysconfig_apply(ares_channel channel,
if (sysconfig->sconfig && !(channel->optmask & ARES_OPT_SERVERS)) { if (sysconfig->sconfig && !(channel->optmask & ARES_OPT_SERVERS)) {
status = ares__servers_update(channel, sysconfig->sconfig, ARES_FALSE); status = ares__servers_update(channel, sysconfig->sconfig, ARES_FALSE);
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS) {
return status; return status;
} }
}
if (sysconfig->domains && !(channel->optmask & ARES_OPT_DOMAINS)) { if (sysconfig->domains && !(channel->optmask & ARES_OPT_DOMAINS)) {
size_t i; size_t i;
ares__strsplit_free(channel->domains, channel->ndomains); ares__strsplit_free(channel->domains, channel->ndomains);
channel->domains = ares_malloc_zero(sizeof(*channel->domains) * channel->domains =
sysconfig->ndomains); ares_malloc_zero(sizeof(*channel->domains) * sysconfig->ndomains);
if (channel->domains == NULL) if (channel->domains == NULL) {
return ARES_ENOMEM; return ARES_ENOMEM;
}
channel->ndomains = sysconfig->ndomains; channel->ndomains = sysconfig->ndomains;
for (i = 0; i < channel->ndomains; i++) { for (i = 0; i < channel->ndomains; i++) {
channel->domains[i] = ares_strdup(sysconfig->domains[i]); channel->domains[i] = ares_strdup(sysconfig->domains[i]);
if (channel->domains[i] == NULL) if (channel->domains[i] == NULL) {
return ARES_ENOMEM; return ARES_ENOMEM;
} }
} }
}
if (sysconfig->lookups && !(channel->optmask & ARES_OPT_LOOKUPS)) { if (sysconfig->lookups && !(channel->optmask & ARES_OPT_LOOKUPS)) {
ares_free(channel->lookups); ares_free(channel->lookups);
@ -946,13 +953,13 @@ static ares_status_t ares_sysconfig_apply(ares_channel channel,
if (sysconfig->sortlist && !(channel->optmask & ARES_OPT_SORTLIST)) { if (sysconfig->sortlist && !(channel->optmask & ARES_OPT_SORTLIST)) {
ares_free(channel->sortlist); ares_free(channel->sortlist);
channel->sortlist = ares_malloc(sizeof(*channel->sortlist) * channel->sortlist =
sysconfig->nsortlist); ares_malloc(sizeof(*channel->sortlist) * sysconfig->nsortlist);
if (channel->sortlist == NULL) { if (channel->sortlist == NULL) {
return ARES_ENOMEM; return ARES_ENOMEM;
} }
memcpy(channel->sortlist, sysconfig->sortlist, sizeof(*channel->sortlist) * memcpy(channel->sortlist, sysconfig->sortlist,
sysconfig->nsortlist); sizeof(*channel->sortlist) * sysconfig->nsortlist);
channel->nsort = sysconfig->nsortlist; channel->nsort = sysconfig->nsortlist;
} }
@ -983,8 +990,9 @@ ares_status_t ares__init_by_sysconfig(ares_channel channel)
memset(&sysconfig, 0, sizeof(sysconfig)); memset(&sysconfig, 0, sizeof(sysconfig));
status = ares__init_by_environment(&sysconfig); status = ares__init_by_environment(&sysconfig);
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS) {
goto done; goto done;
}
#ifdef _WIN32 #ifdef _WIN32
status = ares__init_sysconfig_windows(&sysconfig); status = ares__init_sysconfig_windows(&sysconfig);
@ -1007,8 +1015,9 @@ ares_status_t ares__init_by_sysconfig(ares_channel channel)
} }
status = ares_sysconfig_apply(channel, &sysconfig); status = ares_sysconfig_apply(channel, &sysconfig);
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS) {
goto done; goto done;
}
done: done:
ares_sysconfig_free(&sysconfig); ares_sysconfig_free(&sysconfig);

@ -66,7 +66,6 @@
#include "ares_platform.h" #include "ares_platform.h"
#include "ares_private.h" #include "ares_private.h"
static int ip_addr(const char *ipbuf, ares_ssize_t len, struct in_addr *addr) static int ip_addr(const char *ipbuf, ares_ssize_t len, struct in_addr *addr)
{ {
/* Four octets and three periods yields at most 15 characters. */ /* Four octets and three periods yields at most 15 characters. */
@ -171,7 +170,8 @@ ares_status_t ares__parse_sortlist(struct apattern **sortlist, size_t *nsort,
/* Lets see if it is CIDR */ /* Lets see if it is CIDR */
/* First we'll try IPv6 */ /* First we'll try IPv6 */
if ((bits = ares_inet_net_pton(AF_INET6, ipbufpfx[0] ? ipbufpfx : ipbuf, if ((bits = ares_inet_net_pton(AF_INET6, ipbufpfx[0] ? ipbufpfx : ipbuf,
&pat.addr.addr6, sizeof(pat.addr.addr6))) > 0) { &pat.addr.addr6, sizeof(pat.addr.addr6))) >
0) {
pat.type = PATTERN_CIDR; pat.type = PATTERN_CIDR;
pat.mask.bits = (unsigned short)bits; pat.mask.bits = (unsigned short)bits;
pat.family = AF_INET6; pat.family = AF_INET6;
@ -231,7 +231,6 @@ ares_status_t ares__parse_sortlist(struct apattern **sortlist, size_t *nsort,
return ARES_SUCCESS; return ARES_SUCCESS;
} }
static ares_status_t config_search(ares_sysconfig_t *sysconfig, const char *str) static ares_status_t config_search(ares_sysconfig_t *sysconfig, const char *str)
{ {
if (sysconfig->domains && sysconfig->ndomains > 0) { if (sysconfig->domains && sysconfig->ndomains > 0) {
@ -242,8 +241,9 @@ static ares_status_t config_search(ares_sysconfig_t *sysconfig, const char *str)
} }
sysconfig->domains = ares__strsplit(str, ", ", &sysconfig->ndomains); sysconfig->domains = ares__strsplit(str, ", ", &sysconfig->ndomains);
if (sysconfig->domains == NULL) if (sysconfig->domains == NULL) {
return ARES_ENOMEM; return ARES_ENOMEM;
}
return ARES_SUCCESS; return ARES_SUCCESS;
} }
@ -262,7 +262,6 @@ static ares_status_t config_domain(ares_sysconfig_t *sysconfig, char *str)
return config_search(sysconfig, str); return config_search(sysconfig, str);
} }
static ares_status_t config_lookup(ares_sysconfig_t *sysconfig, const char *str, static ares_status_t config_lookup(ares_sysconfig_t *sysconfig, const char *str,
const char *bindch, const char *altbindch, const char *bindch, const char *altbindch,
const char *filech) const char *filech)
@ -325,8 +324,9 @@ static ares_status_t set_options(ares_sysconfig_t *sysconfig, const char *str)
const char *q; const char *q;
const char *val; const char *val;
if (str == NULL) if (str == NULL) {
return ARES_SUCCESS; return ARES_SUCCESS;
}
p = str; p = str;
while (*p) { while (*p) {
@ -446,7 +446,6 @@ static char *try_config(char *s, const char *opt, char scc)
return p; return p;
} }
ares_status_t ares__init_by_environment(ares_sysconfig_t *sysconfig) ares_status_t ares__init_by_environment(ares_sysconfig_t *sysconfig)
{ {
const char *localdomain; const char *localdomain;
@ -456,8 +455,9 @@ ares_status_t ares__init_by_environment(ares_sysconfig_t *sysconfig)
localdomain = getenv("LOCALDOMAIN"); localdomain = getenv("LOCALDOMAIN");
if (localdomain) { if (localdomain) {
char *temp = ares_strdup(localdomain); char *temp = ares_strdup(localdomain);
if (temp == NULL) if (temp == NULL) {
return ARES_ENOMEM; return ARES_ENOMEM;
}
status = config_domain(sysconfig, temp); status = config_domain(sysconfig, temp);
ares_free(temp); ares_free(temp);
if (status != ARES_SUCCESS) { if (status != ARES_SUCCESS) {
@ -476,7 +476,6 @@ ares_status_t ares__init_by_environment(ares_sysconfig_t *sysconfig)
return ARES_SUCCESS; return ARES_SUCCESS;
} }
ares_status_t ares__init_sysconfig_files(ares_channel channel, ares_status_t ares__init_sysconfig_files(ares_channel channel,
ares_sysconfig_t *sysconfig) ares_sysconfig_t *sysconfig)
{ {
@ -507,7 +506,8 @@ ares_status_t ares__init_sysconfig_files(ares_channel channel,
} else if ((p = try_config(line, "nameserver", ';'))) { } else if ((p = try_config(line, "nameserver", ';'))) {
status = ares__sconfig_append_fromstr(&sysconfig->sconfig, p); status = ares__sconfig_append_fromstr(&sysconfig->sconfig, p);
} else if ((p = try_config(line, "sortlist", ';'))) { } else if ((p = try_config(line, "sortlist", ';'))) {
status = ares__parse_sortlist(&sysconfig->sortlist, &sysconfig->nsortlist, p); status =
ares__parse_sortlist(&sysconfig->sortlist, &sysconfig->nsortlist, p);
} else if ((p = try_config(line, "options", ';'))) { } else if ((p = try_config(line, "options", ';'))) {
status = set_options(sysconfig, p); status = set_options(sysconfig, p);
} else { } else {
@ -520,8 +520,9 @@ ares_status_t ares__init_sysconfig_files(ares_channel channel,
} }
fclose(fp); fclose(fp);
if (status != ARES_EOF) if (status != ARES_EOF) {
goto done; goto done;
}
} else { } else {
error = ERRNO; error = ERRNO;
switch (error) { switch (error) {
@ -541,15 +542,15 @@ ares_status_t ares__init_sysconfig_files(ares_channel channel,
/* Many systems (Solaris, Linux, BSD's) use nsswitch.conf */ /* Many systems (Solaris, Linux, BSD's) use nsswitch.conf */
fp = fopen("/etc/nsswitch.conf", "r"); fp = fopen("/etc/nsswitch.conf", "r");
if (fp) { if (fp) {
while ((status = ares__read_line(fp, &line, &linesize)) == while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS) {
ARES_SUCCESS) {
if ((p = try_config(line, "hosts:", '\0'))) { if ((p = try_config(line, "hosts:", '\0'))) {
(void)config_lookup(sysconfig, p, "dns", "resolve", "files"); (void)config_lookup(sysconfig, p, "dns", "resolve", "files");
} }
} }
fclose(fp); fclose(fp);
if (status != ARES_EOF) if (status != ARES_EOF) {
goto done; goto done;
}
} else { } else {
error = ERRNO; error = ERRNO;
switch (error) { switch (error) {
@ -559,28 +560,27 @@ ares_status_t ares__init_sysconfig_files(ares_channel channel,
default: default:
DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n", error, DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n", error,
strerror(error))); strerror(error)));
DEBUGF(fprintf(stderr, "Error opening file: %s\n", DEBUGF(
"/etc/nsswitch.conf")); fprintf(stderr, "Error opening file: %s\n", "/etc/nsswitch.conf"));
break; break;
} }
/* ignore error, maybe we will get luck in next if clause */ /* ignore error, maybe we will get luck in next if clause */
} }
/* Linux / GNU libc 2.x and possibly others have host.conf */ /* Linux / GNU libc 2.x and possibly others have host.conf */
fp = fopen("/etc/host.conf", "r"); fp = fopen("/etc/host.conf", "r");
if (fp) { if (fp) {
while ((status = ares__read_line(fp, &line, &linesize)) == while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS) {
ARES_SUCCESS) {
if ((p = try_config(line, "order", '\0'))) { if ((p = try_config(line, "order", '\0'))) {
/* ignore errors */ /* ignore errors */
(void)config_lookup(sysconfig, p, "bind", NULL, "hosts"); (void)config_lookup(sysconfig, p, "bind", NULL, "hosts");
} }
} }
fclose(fp); fclose(fp);
if (status != ARES_EOF) if (status != ARES_EOF) {
goto done; goto done;
}
} else { } else {
error = ERRNO; error = ERRNO;
switch (error) { switch (error) {
@ -590,8 +590,7 @@ ares_status_t ares__init_sysconfig_files(ares_channel channel,
default: default:
DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n", error, DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n", error,
strerror(error))); strerror(error)));
DEBUGF( DEBUGF(fprintf(stderr, "Error opening file: %s\n", "/etc/host.conf"));
fprintf(stderr, "Error opening file: %s\n", "/etc/host.conf"));
break; break;
} }
@ -602,16 +601,16 @@ ares_status_t ares__init_sysconfig_files(ares_channel channel,
/* Tru64 uses /etc/svc.conf */ /* Tru64 uses /etc/svc.conf */
fp = fopen("/etc/svc.conf", "r"); fp = fopen("/etc/svc.conf", "r");
if (fp) { if (fp) {
while ((status = ares__read_line(fp, &line, &linesize)) == while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS) {
ARES_SUCCESS) {
if ((p = try_config(line, "hosts=", '\0'))) { if ((p = try_config(line, "hosts=", '\0'))) {
/* ignore errors */ /* ignore errors */
(void)config_lookup(sysconfig, p, "bind", NULL, "local"); (void)config_lookup(sysconfig, p, "bind", NULL, "local");
} }
} }
fclose(fp); fclose(fp);
if (status != ARES_EOF) if (status != ARES_EOF) {
goto done; goto done;
}
} else { } else {
error = ERRNO; error = ERRNO;
switch (error) { switch (error) {
@ -621,8 +620,7 @@ ares_status_t ares__init_sysconfig_files(ares_channel channel,
default: default:
DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n", error, DEBUGF(fprintf(stderr, "fopen() failed with error: %d %s\n", error,
strerror(error))); strerror(error)));
DEBUGF( DEBUGF(fprintf(stderr, "Error opening file: %s\n", "/etc/svc.conf"));
fprintf(stderr, "Error opening file: %s\n", "/etc/svc.conf"));
break; break;
} }
/* ignore error */ /* ignore error */

@ -45,32 +45,32 @@ typedef struct {
static ares_bool_t ares__addr_match(const struct ares_addr *addr1, static ares_bool_t ares__addr_match(const struct ares_addr *addr1,
const struct ares_addr *addr2) const struct ares_addr *addr2)
{ {
if (addr1 == NULL && addr2 == NULL) if (addr1 == NULL && addr2 == NULL) {
return ARES_TRUE; return ARES_TRUE;
}
if (addr1 == NULL || addr2 == NULL) if (addr1 == NULL || addr2 == NULL) {
return ARES_FALSE; return ARES_FALSE;
}
if (addr1->family != addr2->family) if (addr1->family != addr2->family) {
return ARES_FALSE; return ARES_FALSE;
}
if (addr1->family == AF_INET && if (addr1->family == AF_INET && memcmp(&addr1->addr.addr4, &addr2->addr.addr4,
memcmp(&addr1->addr.addr4, &addr2->addr.addr4, sizeof(addr1->addr.addr4)) sizeof(addr1->addr.addr4)) == 0) {
== 0) {
return ARES_TRUE; return ARES_TRUE;
} }
if (addr1->family == AF_INET6 && if (addr1->family == AF_INET6 &&
memcmp(&addr1->addr.addr6._S6_un._S6_u8, &addr2->addr.addr6._S6_un._S6_u8, memcmp(&addr1->addr.addr6._S6_un._S6_u8, &addr2->addr.addr6._S6_un._S6_u8,
sizeof(addr1->addr.addr6._S6_un._S6_u8)) sizeof(addr1->addr.addr6._S6_un._S6_u8)) == 0) {
== 0) {
return ARES_TRUE; return ARES_TRUE;
} }
return ARES_FALSE; return ARES_FALSE;
} }
/* Validate that the ip address matches the subnet (network base and network /* Validate that the ip address matches the subnet (network base and network
* mask) specified. Addresses are specified in standard Network Byte Order as * mask) specified. Addresses are specified in standard Network Byte Order as
* 16 bytes, and the netmask is 0 to 128 (bits). * 16 bytes, and the netmask is 0 to 128 (bits).
@ -122,12 +122,14 @@ static ares_bool_t ares_server_blacklisted(const struct ares_addr *addr)
size_t i; size_t i;
if (addr->family != AF_INET6) if (addr->family != AF_INET6) {
return ARES_FALSE; return ARES_FALSE;
}
/* See if ipaddr matches any of the entries in the blacklist. */ /* See if ipaddr matches any of the entries in the blacklist. */
for (i = 0; i < sizeof(blacklist_v6) / sizeof(*blacklist_v6); i++) { for (i = 0; i < sizeof(blacklist_v6) / sizeof(*blacklist_v6); i++) {
if (ares_ipv6_subnet_matches(blacklist_v6[i].netbase, blacklist_v6[i].netmask, if (ares_ipv6_subnet_matches(blacklist_v6[i].netbase,
blacklist_v6[i].netmask,
(const unsigned char *)&addr->addr.addr6)) { (const unsigned char *)&addr->addr.addr6)) {
return ARES_TRUE; return ARES_TRUE;
} }
@ -229,7 +231,6 @@ static ares_status_t parse_dnsaddrport(const char *str, size_t len,
return ARES_SUCCESS; return ARES_SUCCESS;
} }
ares_status_t ares__sconfig_append(ares__llist_t **sconfig, ares_status_t ares__sconfig_append(ares__llist_t **sconfig,
const struct ares_addr *addr, const struct ares_addr *addr,
unsigned short udp_port, unsigned short udp_port,
@ -238,12 +239,14 @@ ares_status_t ares__sconfig_append(ares__llist_t **sconfig,
ares_sconfig_t *s; ares_sconfig_t *s;
ares_status_t status; ares_status_t status;
if (sconfig == NULL || addr == NULL) if (sconfig == NULL || addr == NULL) {
return ARES_EFORMERR; return ARES_EFORMERR;
}
/* Silently skip blacklisted IPv6 servers. */ /* Silently skip blacklisted IPv6 servers. */
if (ares_server_blacklisted(addr)) if (ares_server_blacklisted(addr)) {
return ARES_SUCCESS; return ARES_SUCCESS;
}
s = ares_malloc_zero(sizeof(*s)); s = ares_malloc_zero(sizeof(*s));
if (s == NULL) { if (s == NULL) {
@ -275,7 +278,6 @@ fail:
return status; return status;
} }
/* Add the IPv4 or IPv6 nameservers in str (separated by commas or spaces) to /* Add the IPv4 or IPv6 nameservers in str (separated by commas or spaces) to
* the servers list, updating servers and nservers as required. * the servers list, updating servers and nservers as required.
* *
@ -337,9 +339,6 @@ ares_status_t ares__sconfig_append_fromstr(ares__llist_t **sconfig,
return ARES_SUCCESS; return ARES_SUCCESS;
} }
static unsigned short ares__sconfig_get_port(ares_channel channel, static unsigned short ares__sconfig_get_port(ares_channel channel,
const ares_sconfig_t *s, const ares_sconfig_t *s,
ares_bool_t is_tcp) ares_bool_t is_tcp)
@ -366,14 +365,17 @@ static ares__slist_node_t *ares__server_find(ares_channel channel,
node = ares__slist_node_next(node)) { node = ares__slist_node_next(node)) {
const struct server_state *server = ares__slist_node_val(node); const struct server_state *server = ares__slist_node_val(node);
if (!ares__addr_match(&server->addr, &s->addr)) if (!ares__addr_match(&server->addr, &s->addr)) {
continue; continue;
}
if (server->tcp_port != ares__sconfig_get_port(channel, s, ARES_TRUE)) if (server->tcp_port != ares__sconfig_get_port(channel, s, ARES_TRUE)) {
continue; continue;
}
if (server->udp_port != ares__sconfig_get_port(channel, s, ARES_FALSE)) if (server->udp_port != ares__sconfig_get_port(channel, s, ARES_FALSE)) {
continue; continue;
}
return node; return node;
} }
@ -389,19 +391,21 @@ static ares_bool_t ares__server_isdup(ares_channel channel,
for (prev = ares__llist_node_prev(s); prev != NULL; for (prev = ares__llist_node_prev(s); prev != NULL;
prev = ares__llist_node_prev(prev)) { prev = ares__llist_node_prev(prev)) {
const ares_sconfig_t *p = ares__llist_node_val(prev); const ares_sconfig_t *p = ares__llist_node_val(prev);
if (!ares__addr_match(&server->addr, &p->addr)) if (!ares__addr_match(&server->addr, &p->addr)) {
continue; continue;
}
if (ares__sconfig_get_port(channel, server, ARES_TRUE) != if (ares__sconfig_get_port(channel, server, ARES_TRUE) !=
ares__sconfig_get_port(channel, p, ARES_TRUE)) ares__sconfig_get_port(channel, p, ARES_TRUE)) {
continue; continue;
}
if (ares__sconfig_get_port(channel, server, ARES_FALSE) != if (ares__sconfig_get_port(channel, server, ARES_FALSE) !=
ares__sconfig_get_port(channel, p, ARES_FALSE)) ares__sconfig_get_port(channel, p, ARES_FALSE)) {
continue; continue;
}
return ARES_TRUE; return ARES_TRUE;
} }
@ -416,8 +420,9 @@ static ares_status_t ares__server_create(ares_channel channel,
ares_status_t status; ares_status_t status;
struct server_state *server = ares_malloc_zero(sizeof(*server)); struct server_state *server = ares_malloc_zero(sizeof(*server));
if (server == NULL) if (server == NULL) {
return ARES_ENOMEM; return ARES_ENOMEM;
}
server->idx = idx; server->idx = idx;
server->channel = channel; server->channel = channel;
@ -474,17 +479,19 @@ static ares_bool_t ares__server_in_newconfig(struct server_state *server,
for (node = ares__llist_node_first(srvlist); node != NULL; for (node = ares__llist_node_first(srvlist); node != NULL;
node = ares__llist_node_next(node)) { node = ares__llist_node_next(node)) {
const ares_sconfig_t *s = ares__llist_node_val(node); const ares_sconfig_t *s = ares__llist_node_val(node);
if (!ares__addr_match(&server->addr, &s->addr)) if (!ares__addr_match(&server->addr, &s->addr)) {
continue; continue;
}
if (server->tcp_port != ares__sconfig_get_port(channel, s, ARES_TRUE)) if (server->tcp_port != ares__sconfig_get_port(channel, s, ARES_TRUE)) {
continue; continue;
}
if (server->udp_port != ares__sconfig_get_port(channel, s, ARES_FALSE)) if (server->udp_port != ares__sconfig_get_port(channel, s, ARES_FALSE)) {
continue; continue;
}
return ARES_TRUE; return ARES_TRUE;
} }
@ -509,7 +516,6 @@ static void ares__servers_remove_stale(ares_channel channel,
} }
} }
static void ares__servers_trim_single(ares_channel channel) static void ares__servers_trim_single(ares_channel channel)
{ {
while (ares__slist_len(channel->servers) > 1) { while (ares__slist_len(channel->servers) > 1) {
@ -517,7 +523,6 @@ static void ares__servers_trim_single(ares_channel channel)
} }
} }
ares_status_t ares__servers_update(ares_channel channel, ares_status_t ares__servers_update(ares_channel channel,
ares__llist_t *server_list, ares__llist_t *server_list,
ares_bool_t user_specified) ares_bool_t user_specified)
@ -534,7 +539,6 @@ ares_status_t ares__servers_update(ares_channel channel,
/* Add new entries */ /* Add new entries */
for (node = ares__llist_node_first(server_list); node != NULL; for (node = ares__llist_node_first(server_list); node != NULL;
node = ares__llist_node_next(node)) { node = ares__llist_node_next(node)) {
const ares_sconfig_t *sconfig = ares__llist_node_val(node); const ares_sconfig_t *sconfig = ares__llist_node_val(node);
ares__slist_node_t *snode; ares__slist_node_t *snode;
@ -555,9 +559,10 @@ ares_status_t ares__servers_update(ares_channel channel,
} }
} else { } else {
status = ares__server_create(channel, sconfig, idx); status = ares__server_create(channel, sconfig, idx);
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS) {
goto done; goto done;
} }
}
idx++; idx++;
} }
@ -580,30 +585,33 @@ done:
return status; return status;
} }
static ares__llist_t *
static ares__llist_t *ares_addr_node_to_server_config_llist( ares_addr_node_to_server_config_llist(const struct ares_addr_node *servers)
const struct ares_addr_node *servers)
{ {
const struct ares_addr_node *node; const struct ares_addr_node *node;
ares__llist_t *s; ares__llist_t *s;
if (servers == NULL) if (servers == NULL) {
return NULL; return NULL;
}
s = ares__llist_create(ares_free); s = ares__llist_create(ares_free);
if (s == NULL) if (s == NULL) {
return NULL; return NULL;
}
for (node = servers; node != NULL; node = node->next) { for (node = servers; node != NULL; node = node->next) {
ares_sconfig_t *sconfig; ares_sconfig_t *sconfig;
/* Invalid entry */ /* Invalid entry */
if (node->family != AF_INET && node->family != AF_INET6) if (node->family != AF_INET && node->family != AF_INET6) {
continue; continue;
}
sconfig = ares_malloc_zero(sizeof(*sconfig)); sconfig = ares_malloc_zero(sizeof(*sconfig));
if (sconfig == NULL) if (sconfig == NULL) {
goto fail; goto fail;
}
sconfig->addr.family = node->family; sconfig->addr.family = node->family;
if (node->family == AF_INET) { if (node->family == AF_INET) {
@ -618,7 +626,6 @@ static ares__llist_t *ares_addr_node_to_server_config_llist(
ares_free(sconfig); ares_free(sconfig);
goto fail; goto fail;
} }
} }
return s; return s;
@ -634,23 +641,27 @@ static ares__llist_t *ares_addr_port_node_to_server_config_llist(
const struct ares_addr_port_node *node; const struct ares_addr_port_node *node;
ares__llist_t *s; ares__llist_t *s;
if (servers == NULL) if (servers == NULL) {
return NULL; return NULL;
}
s = ares__llist_create(ares_free); s = ares__llist_create(ares_free);
if (s == NULL) if (s == NULL) {
return NULL; return NULL;
}
for (node = servers; node != NULL; node = node->next) { for (node = servers; node != NULL; node = node->next) {
ares_sconfig_t *sconfig; ares_sconfig_t *sconfig;
/* Invalid entry */ /* Invalid entry */
if (node->family != AF_INET && node->family != AF_INET6) if (node->family != AF_INET && node->family != AF_INET6) {
continue; continue;
}
sconfig = ares_malloc_zero(sizeof(*sconfig)); sconfig = ares_malloc_zero(sizeof(*sconfig));
if (sconfig == NULL) if (sconfig == NULL) {
goto fail; goto fail;
}
sconfig->addr.family = node->family; sconfig->addr.family = node->family;
if (node->family == AF_INET) { if (node->family == AF_INET) {
@ -677,26 +688,29 @@ fail:
return NULL; return NULL;
} }
ares__llist_t *
ares__llist_t *ares_in_addr_to_server_config_llist( ares_in_addr_to_server_config_llist(const struct in_addr *servers,
const struct in_addr *servers, size_t nservers) size_t nservers)
{ {
size_t i; size_t i;
ares__llist_t *s; ares__llist_t *s;
if (servers == NULL || nservers == 0) if (servers == NULL || nservers == 0) {
return NULL; return NULL;
}
s = ares__llist_create(ares_free); s = ares__llist_create(ares_free);
if (s == NULL) if (s == NULL) {
return NULL; return NULL;
}
for (i = 0; i < nservers; i++) { for (i = 0; i < nservers; i++) {
ares_sconfig_t *sconfig; ares_sconfig_t *sconfig;
sconfig = ares_malloc_zero(sizeof(*sconfig)); sconfig = ares_malloc_zero(sizeof(*sconfig));
if (sconfig == NULL) if (sconfig == NULL) {
goto fail; goto fail;
}
sconfig->addr.family = AF_INET; sconfig->addr.family = AF_INET;
memcpy(&sconfig->addr.addr.addr4, &servers[i], memcpy(&sconfig->addr.addr.addr4, &servers[i],
@ -818,7 +832,6 @@ int ares_get_servers_ports(ares_channel channel,
return (int)status; return (int)status;
} }
int ares_set_servers(ares_channel channel, struct ares_addr_node *servers) int ares_set_servers(ares_channel channel, struct ares_addr_node *servers)
{ {
ares__llist_t *slist; ares__llist_t *slist;
@ -840,7 +853,6 @@ int ares_set_servers(ares_channel channel, struct ares_addr_node *servers)
return (int)status; return (int)status;
} }
int ares_set_servers_ports(ares_channel channel, int ares_set_servers_ports(ares_channel channel,
struct ares_addr_port_node *servers) struct ares_addr_port_node *servers)
{ {
@ -863,7 +875,6 @@ int ares_set_servers_ports(ares_channel channel,
return (int)status; return (int)status;
} }
/* Incomming string format: host[:port][,host[:port]]... */ /* Incomming string format: host[:port][,host[:port]]... */
/* IPv6 addresses with ports require square brackets [fe80::1]:53 */ /* IPv6 addresses with ports require square brackets [fe80::1]:53 */
static ares_status_t set_servers_csv(ares_channel channel, const char *_csv, static ares_status_t set_servers_csv(ares_channel channel, const char *_csv,

Loading…
Cancel
Save