introduce ares_bool_t datatype (#570)

c-ares currently uses int for boolean, which can be confusing as there are some functions which return int but use '0' as the success condition. Some internal variable usage is similar. Lets try to identify the boolean use cases and split them out into their own data type of ares_bool_t. Since we're trying to keep C89 compatibility, we can't rely on stdbool.h or the _Bool C99 data type, so we'll define our own.

Also, chose using an enum rather than say unsigned char or int because of the type safety benefits it provides. Compilers should warn if you try to pass, ARES_TRUE on to a ares_status_t enum (or similar) since they are different enums.

Fix By: Brad House (@bradh352)
pull/575/head
Brad House 1 year ago committed by GitHub
parent a070d7835d
commit 75a873c86b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      include/ares.h
  2. 8
      src/lib/ares__buf.c
  3. 30
      src/lib/ares__htable.c
  4. 14
      src/lib/ares__htable.h
  5. 26
      src/lib/ares__htable_asvp.c
  6. 12
      src/lib/ares__htable_asvp.h
  7. 24
      src/lib/ares__htable_stvp.c
  8. 16
      src/lib/ares__htable_stvp.h
  9. 11
      src/lib/ares__parse_into_addrinfo.c
  10. 12
      src/lib/ares__readaddrinfo.c
  11. 4
      src/lib/ares__slist.c
  12. 15
      src/lib/ares__timeval.c
  13. 2
      src/lib/ares_android.c
  14. 36
      src/lib/ares_expand_name.c
  15. 42
      src/lib/ares_getaddrinfo.c
  16. 8
      src/lib/ares_getnameinfo.c
  17. 58
      src/lib/ares_init.c
  18. 27
      src/lib/ares_private.h
  19. 71
      src/lib/ares_process.c
  20. 8
      src/lib/ares_rand.c
  21. 16
      src/lib/ares_search.c
  22. 2
      src/lib/ares_send.c

@ -158,6 +158,11 @@ typedef enum {
} ares_status_t; } ares_status_t;
typedef enum {
ARES_FALSE = 0,
ARES_TRUE = 1
} ares_bool_t;
/* Flag values */ /* Flag values */
#define ARES_FLAG_USEVC (1 << 0) #define ARES_FLAG_USEVC (1 << 0)
#define ARES_FLAG_PRIMARY (1 << 1) #define ARES_FLAG_PRIMARY (1 << 1)

@ -84,15 +84,15 @@ void ares__buf_destroy(ares__buf_t *buf)
} }
static int ares__buf_is_const(const ares__buf_t *buf) static ares_bool_t ares__buf_is_const(const ares__buf_t *buf)
{ {
if (buf == NULL) if (buf == NULL)
return 0; return ARES_FALSE;
if (buf->data != NULL && buf->alloc_buf == NULL) if (buf->data != NULL && buf->alloc_buf == NULL)
return 1; return ARES_TRUE;
return 0; return ARES_FALSE;
} }

@ -160,7 +160,7 @@ static ares__llist_node_t *ares__htable_find(ares__htable_t *htable,
} }
static unsigned int ares__htable_expand(ares__htable_t *htable) static ares_bool_t ares__htable_expand(ares__htable_t *htable)
{ {
ares__llist_t **buckets = NULL; ares__llist_t **buckets = NULL;
unsigned int old_size = htable->size; unsigned int old_size = htable->size;
@ -168,7 +168,7 @@ static unsigned int ares__htable_expand(ares__htable_t *htable)
/* Not a failure, just won't expand */ /* Not a failure, just won't expand */
if (old_size == ARES__HTABLE_MAX_BUCKETS) if (old_size == ARES__HTABLE_MAX_BUCKETS)
return 1; return ARES_TRUE;
htable->size <<= 1; htable->size <<= 1;
@ -207,24 +207,24 @@ static unsigned int ares__htable_expand(ares__htable_t *htable)
/* Swap out buckets */ /* Swap out buckets */
ares__htable_buckets_destroy(htable->buckets, old_size, 0); ares__htable_buckets_destroy(htable->buckets, old_size, 0);
htable->buckets = buckets; htable->buckets = buckets;
return 1; return ARES_TRUE;
fail: fail:
ares__htable_buckets_destroy(buckets, htable->size, 0); ares__htable_buckets_destroy(buckets, htable->size, 0);
htable->size = old_size; htable->size = old_size;
return 0; return ARES_FALSE;
} }
unsigned int ares__htable_insert(ares__htable_t *htable, void *bucket) ares_bool_t ares__htable_insert(ares__htable_t *htable, void *bucket)
{ {
unsigned int idx = 0; unsigned int idx = 0;
ares__llist_node_t *node = NULL; ares__llist_node_t *node = NULL;
const void *key = NULL; const void *key = NULL;
if (htable == NULL || bucket == NULL) if (htable == NULL || bucket == NULL)
return 0; return ARES_FALSE;
key = htable->bucket_key(bucket); key = htable->bucket_key(bucket);
@ -234,14 +234,14 @@ unsigned int ares__htable_insert(ares__htable_t *htable, void *bucket)
node = ares__htable_find(htable, idx, key); node = ares__htable_find(htable, idx, key);
if (node != NULL) { if (node != NULL) {
ares__llist_node_replace(node, bucket); ares__llist_node_replace(node, bucket);
return 1; return ARES_TRUE;
} }
/* Check to see if we should rehash because likelihood of collisions has /* Check to see if we should rehash because likelihood of collisions has
* increased beyond our threshold */ * increased beyond our threshold */
if (htable->num_keys+1 > (htable->size * ARES__HTABLE_EXPAND_PERCENT) / 100) { if (htable->num_keys+1 > (htable->size * ARES__HTABLE_EXPAND_PERCENT) / 100) {
if (!ares__htable_expand(htable)) { if (!ares__htable_expand(htable)) {
return 0; return ARES_FALSE;
} }
/* If we expanded, need to calculate a new index */ /* If we expanded, need to calculate a new index */
idx = HASH_IDX(htable, key); idx = HASH_IDX(htable, key);
@ -251,16 +251,16 @@ unsigned int ares__htable_insert(ares__htable_t *htable, void *bucket)
if (htable->buckets[idx] == NULL) { if (htable->buckets[idx] == NULL) {
htable->buckets[idx] = ares__llist_create(htable->bucket_free); htable->buckets[idx] = ares__llist_create(htable->bucket_free);
if (htable->buckets[idx] == NULL) if (htable->buckets[idx] == NULL)
return 0; return ARES_FALSE;
} }
node = ares__llist_insert_first(htable->buckets[idx], bucket); node = ares__llist_insert_first(htable->buckets[idx], bucket);
if (node == NULL) if (node == NULL)
return 0; return ARES_FALSE;
htable->num_keys++; htable->num_keys++;
return 1; return ARES_TRUE;
} }
@ -277,22 +277,22 @@ void *ares__htable_get(ares__htable_t *htable, const void *key)
} }
unsigned int ares__htable_remove(ares__htable_t *htable, const void *key) ares_bool_t ares__htable_remove(ares__htable_t *htable, const void *key)
{ {
ares__llist_node_t *node; ares__llist_node_t *node;
unsigned int idx; unsigned int idx;
if (htable == NULL || key == NULL) if (htable == NULL || key == NULL)
return 0; return ARES_FALSE;
idx = HASH_IDX(htable, key); idx = HASH_IDX(htable, key);
node = ares__htable_find(htable, idx, key); node = ares__htable_find(htable, idx, key);
if (node == NULL) if (node == NULL)
return 0; return ARES_FALSE;
htable->num_keys--; htable->num_keys--;
ares__llist_node_destroy(node); ares__llist_node_destroy(node);
return 1; return ARES_TRUE;
} }
size_t ares__htable_num_keys(ares__htable_t *htable) size_t ares__htable_num_keys(ares__htable_t *htable)

@ -78,10 +78,10 @@ typedef const void *(*ares__htable_bucket_key_t)(const void *bucket);
* *
* \param[in] key1 first key * \param[in] key1 first key
* \param[in] key2 second key * \param[in] key2 second key
* \return 1 if equal, 0 if not * \return ARES_TRUE if equal, ARES_FALSE if not
*/ */
typedef unsigned int (*ares__htable_key_eq_t)(const void *key1, typedef ares_bool_t (*ares__htable_key_eq_t)(const void *key1,
const void *key2); const void *key2);
/*! Destroy the initialized hashtable /*! Destroy the initialized hashtable
@ -115,9 +115,9 @@ size_t ares__htable_num_keys(ares__htable_t *htable);
* \param[in] htable Initialized hashtable. * \param[in] htable Initialized hashtable.
* \param[in] bucket User-provided bucket to insert. Takes "ownership". Not * \param[in] bucket User-provided bucket to insert. Takes "ownership". Not
* allowed to be NULL. * allowed to be NULL.
* \return 1 on success, 0 if out of memory * \return ARES_TRUE on success, ARES_FALSE if out of memory
*/ */
unsigned int ares__htable_insert(ares__htable_t *htable, void *bucket); ares_bool_t ares__htable_insert(ares__htable_t *htable, void *bucket);
/*! Retrieve bucket from hashtable based on key. /*! Retrieve bucket from hashtable based on key.
* *
@ -131,9 +131,9 @@ void *ares__htable_get(ares__htable_t *htable, const void *key);
* *
* \param[in] htable Initialized hashtable * \param[in] htable Initialized hashtable
* \param[in] key Pointer to key to use for comparison * \param[in] key Pointer to key to use for comparison
* \return 1 if found, 0 if not found * \return ARES_TRUE if found, ARES_FALSE if not found
*/ */
unsigned int ares__htable_remove(ares__htable_t *htable, const void *key); ares_bool_t ares__htable_remove(ares__htable_t *htable, const void *key);
/*! FNV1a hash algorithm. Can be used as underlying primitive for building /*! FNV1a hash algorithm. Can be used as underlying primitive for building
* a wrapper hashtable. * a wrapper hashtable.

@ -79,15 +79,15 @@ static void bucket_free(void *bucket)
} }
static unsigned int key_eq(const void *key1, const void *key2) static ares_bool_t key_eq(const void *key1, const void *key2)
{ {
const ares_socket_t *k1 = key1; const ares_socket_t *k1 = key1;
const ares_socket_t *k2 = key2; const ares_socket_t *k2 = key2;
if (*k1 == *k2) if (*k1 == *k2)
return 1; return ARES_TRUE;
return 0; return ARES_FALSE;
} }
@ -118,8 +118,8 @@ fail:
} }
unsigned int ares__htable_asvp_insert(ares__htable_asvp_t *htable, ares_bool_t ares__htable_asvp_insert(ares__htable_asvp_t *htable,
ares_socket_t key, void *val) ares_socket_t key, void *val)
{ {
ares__htable_asvp_bucket_t *bucket = NULL; ares__htable_asvp_bucket_t *bucket = NULL;
@ -137,17 +137,17 @@ unsigned int ares__htable_asvp_insert(ares__htable_asvp_t *htable,
if (!ares__htable_insert(htable->hash, bucket)) if (!ares__htable_insert(htable->hash, bucket))
goto fail; goto fail;
return 1; return ARES_TRUE;
fail: fail:
if (bucket) { if (bucket) {
ares_free(bucket); ares_free(bucket);
} }
return 0; return ARES_FALSE;
} }
unsigned int ares__htable_asvp_get(ares__htable_asvp_t *htable, ares_bool_t ares__htable_asvp_get(ares__htable_asvp_t *htable,
ares_socket_t key, void **val) ares_socket_t key, void **val)
{ {
ares__htable_asvp_bucket_t *bucket = NULL; ares__htable_asvp_bucket_t *bucket = NULL;
@ -156,15 +156,15 @@ unsigned int ares__htable_asvp_get(ares__htable_asvp_t *htable,
*val = NULL; *val = NULL;
if (htable == NULL) if (htable == NULL)
return 0; return ARES_FALSE;
bucket = ares__htable_get(htable->hash, &key); bucket = ares__htable_get(htable->hash, &key);
if (bucket == NULL) if (bucket == NULL)
return 0; return ARES_FALSE;
if (val) if (val)
*val = bucket->val; *val = bucket->val;
return 1; return ARES_TRUE;
} }
@ -177,11 +177,11 @@ void *ares__htable_asvp_get_direct(ares__htable_asvp_t *htable,
} }
unsigned int ares__htable_asvp_remove(ares__htable_asvp_t *htable, ares_bool_t ares__htable_asvp_remove(ares__htable_asvp_t *htable,
ares_socket_t key) ares_socket_t key)
{ {
if (htable == NULL) if (htable == NULL)
return 0; return ARES_FALSE;
return ares__htable_remove(htable->hash, &key); return ares__htable_remove(htable->hash, &key);
} }

@ -73,9 +73,9 @@ ares__htable_asvp_t *ares__htable_asvp_create(
* \param[in] htable Initialized hash table * \param[in] htable Initialized hash table
* \param[in] key key to associate with value * \param[in] key key to associate with value
* \param[in] val value to store (takes ownership). May be NULL. * \param[in] val value to store (takes ownership). May be NULL.
* \return 1 on success, 0 on out of memory or misuse * \return ARES_TRUE on success, ARES_FALSE on out of memory or misuse
*/ */
unsigned int ares__htable_asvp_insert(ares__htable_asvp_t *htable, ares_bool_t ares__htable_asvp_insert(ares__htable_asvp_t *htable,
ares_socket_t key, void *val); ares_socket_t key, void *val);
/*! Retrieve value from hashtable based on key /*! Retrieve value from hashtable based on key
@ -83,9 +83,9 @@ unsigned int ares__htable_asvp_insert(ares__htable_asvp_t *htable,
* \param[in] htable Initialized hash table * \param[in] htable Initialized hash table
* \param[in] key key to use to search * \param[in] key key to use to search
* \param[out] val Optional. Pointer to store value. * \param[out] val Optional. Pointer to store value.
* \return 1 on success, 0 on failure * \return ARES_TRUE on success, ARES_FALSE on failure
*/ */
unsigned int ares__htable_asvp_get(ares__htable_asvp_t *htable, ares_bool_t ares__htable_asvp_get(ares__htable_asvp_t *htable,
ares_socket_t key, void **val); ares_socket_t key, void **val);
/*! Retrieve value from hashtable directly as return value. Caveat to this /*! Retrieve value from hashtable directly as return value. Caveat to this
@ -103,9 +103,9 @@ void *ares__htable_asvp_get_direct(ares__htable_asvp_t *htable,
* *
* \param[in] htable Initialized hash table * \param[in] htable Initialized hash table
* \param[in] key key to use to search * \param[in] key key to use to search
* \return 1 if found, 0 if not * \return ARES_TRUE if found, ARES_FALSE if not found
*/ */
unsigned int ares__htable_asvp_remove(ares__htable_asvp_t *htable, ares_bool_t ares__htable_asvp_remove(ares__htable_asvp_t *htable,
ares_socket_t key); ares_socket_t key);
/*! Retrieve the number of keys stored in the hash table /*! Retrieve the number of keys stored in the hash table

@ -79,15 +79,15 @@ static void bucket_free(void *bucket)
} }
static unsigned int key_eq(const void *key1, const void *key2) static ares_bool_t key_eq(const void *key1, const void *key2)
{ {
const size_t *k1 = key1; const size_t *k1 = key1;
const size_t *k2 = key2; const size_t *k2 = key2;
if (*k1 == *k2) if (*k1 == *k2)
return 1; return ARES_TRUE;
return 0; return ARES_FALSE;
} }
@ -118,7 +118,7 @@ fail:
} }
unsigned int ares__htable_stvp_insert(ares__htable_stvp_t *htable, size_t key, ares_bool_t ares__htable_stvp_insert(ares__htable_stvp_t *htable, size_t key,
void *val) void *val)
{ {
ares__htable_stvp_bucket_t *bucket = NULL; ares__htable_stvp_bucket_t *bucket = NULL;
@ -137,17 +137,17 @@ unsigned int ares__htable_stvp_insert(ares__htable_stvp_t *htable, size_t key,
if (!ares__htable_insert(htable->hash, bucket)) if (!ares__htable_insert(htable->hash, bucket))
goto fail; goto fail;
return 1; return ARES_TRUE;
fail: fail:
if (bucket) { if (bucket) {
ares_free(bucket); ares_free(bucket);
} }
return 0; return ARES_FALSE;
} }
unsigned int ares__htable_stvp_get(ares__htable_stvp_t *htable, size_t key, ares_bool_t ares__htable_stvp_get(ares__htable_stvp_t *htable, size_t key,
void **val) void **val)
{ {
ares__htable_stvp_bucket_t *bucket = NULL; ares__htable_stvp_bucket_t *bucket = NULL;
@ -156,15 +156,15 @@ unsigned int ares__htable_stvp_get(ares__htable_stvp_t *htable, size_t key,
*val = NULL; *val = NULL;
if (htable == NULL) if (htable == NULL)
return 0; return ARES_FALSE;
bucket = ares__htable_get(htable->hash, &key); bucket = ares__htable_get(htable->hash, &key);
if (bucket == NULL) if (bucket == NULL)
return 0; return ARES_FALSE;
if (val) if (val)
*val = bucket->val; *val = bucket->val;
return 1; return ARES_TRUE;
} }
@ -176,10 +176,10 @@ void *ares__htable_stvp_get_direct(ares__htable_stvp_t *htable, size_t key)
} }
unsigned int ares__htable_stvp_remove(ares__htable_stvp_t *htable, size_t key) ares_bool_t ares__htable_stvp_remove(ares__htable_stvp_t *htable, size_t key)
{ {
if (htable == NULL) if (htable == NULL)
return 0; return ARES_FALSE;
return ares__htable_remove(htable->hash, &key); return ares__htable_remove(htable->hash, &key);
} }

@ -70,20 +70,20 @@ ares__htable_stvp_t *ares__htable_stvp_create(
* \param[in] htable Initialized hash table * \param[in] htable Initialized hash table
* \param[in] key key to associate with value * \param[in] key key to associate with value
* \param[in] val value to store (takes ownership). May be NULL. * \param[in] val value to store (takes ownership). May be NULL.
* \return 1 on success, 0 on out of memory or misuse * \return ARES_TRUE on success, ARES_FALSE on failure or out of memory
*/ */
unsigned int ares__htable_stvp_insert(ares__htable_stvp_t *htable, size_t key, ares_bool_t ares__htable_stvp_insert(ares__htable_stvp_t *htable, size_t key,
void *val); void *val);
/*! Retrieve value from hashtable based on key /*! Retrieve value from hashtable based on key
* *
* \param[in] htable Initialized hash table * \param[in] htable Initialized hash table
* \param[in] key key to use to search * \param[in] key key to use to search
* \param[out] val Optional. Pointer to store value. * \param[out] val Optional. Pointer to store value.
* \return 1 on success, 0 on failure * \return ARES_TRUE on success, ARES_FALSE on failure
*/ */
unsigned int ares__htable_stvp_get(ares__htable_stvp_t *htable, size_t key, ares_bool_t ares__htable_stvp_get(ares__htable_stvp_t *htable, size_t key,
void **val); void **val);
/*! Retrieve value from hashtable directly as return value. Caveat to this /*! Retrieve value from hashtable directly as return value. Caveat to this
* function over ares__htable_stvp_get() is that if a NULL value is stored * function over ares__htable_stvp_get() is that if a NULL value is stored
@ -99,9 +99,9 @@ void *ares__htable_stvp_get_direct(ares__htable_stvp_t *htable, size_t key);
* *
* \param[in] htable Initialized hash table * \param[in] htable Initialized hash table
* \param[in] key key to use to search * \param[in] key key to use to search
* \return 1 if found, 0 if not * \return ARES_TRUE if found, ARES_FALSE if not
*/ */
unsigned int ares__htable_stvp_remove(ares__htable_stvp_t *htable, size_t key); ares_bool_t ares__htable_stvp_remove(ares__htable_stvp_t *htable, size_t key);
/*! Retrieve the number of keys stored in the hash table /*! Retrieve the number of keys stored in the hash table
* *

@ -50,14 +50,15 @@
#include "ares_private.h" #include "ares_private.h"
ares_status_t ares__parse_into_addrinfo(const unsigned char *abuf, ares_status_t ares__parse_into_addrinfo(const unsigned char *abuf,
int alen, int cname_only_is_enodata, int alen,
ares_bool_t cname_only_is_enodata,
unsigned short port, unsigned short port,
struct ares_addrinfo *ai) struct ares_addrinfo *ai)
{ {
unsigned int qdcount, ancount; unsigned int qdcount, ancount;
ares_status_t status; ares_status_t status;
int i, rr_type, rr_class, rr_len, rr_ttl; int i, rr_type, rr_class, rr_len, rr_ttl;
int got_a = 0, got_aaaa = 0, got_cname = 0; ares_bool_t got_a = ARES_FALSE, got_aaaa = ARES_FALSE, got_cname = ARES_FALSE;
long len; long len;
const unsigned char *aptr; const unsigned char *aptr;
char *question_hostname = NULL; char *question_hostname = NULL;
@ -123,7 +124,7 @@ ares_status_t ares__parse_into_addrinfo(const unsigned char *abuf,
&& rr_len == sizeof(struct in_addr) && rr_len == sizeof(struct in_addr)
&& strcasecmp(rr_name, hostname) == 0) && strcasecmp(rr_name, hostname) == 0)
{ {
got_a = 1; got_a = ARES_TRUE;
if (aptr + sizeof(struct in_addr) > abuf + alen) if (aptr + sizeof(struct in_addr) > abuf + alen)
{ /* LCOV_EXCL_START: already checked above */ { /* LCOV_EXCL_START: already checked above */
status = ARES_EBADRESP; status = ARES_EBADRESP;
@ -138,7 +139,7 @@ ares_status_t ares__parse_into_addrinfo(const unsigned char *abuf,
&& rr_len == sizeof(struct ares_in6_addr) && rr_len == sizeof(struct ares_in6_addr)
&& strcasecmp(rr_name, hostname) == 0) && strcasecmp(rr_name, hostname) == 0)
{ {
got_aaaa = 1; got_aaaa = ARES_TRUE;
if (aptr + sizeof(struct ares_in6_addr) > abuf + alen) if (aptr + sizeof(struct ares_in6_addr) > abuf + alen)
{ /* LCOV_EXCL_START: already checked above */ { /* LCOV_EXCL_START: already checked above */
status = ARES_EBADRESP; status = ARES_EBADRESP;
@ -152,7 +153,7 @@ ares_status_t ares__parse_into_addrinfo(const unsigned char *abuf,
if (rr_class == C_IN && rr_type == T_CNAME) if (rr_class == C_IN && rr_type == T_CNAME)
{ {
got_cname = 1; got_cname = ARES_TRUE;
status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data, status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
&len, 1); &len, 1);
if (status != ARES_SUCCESS) if (status != ARES_SUCCESS)

@ -57,8 +57,8 @@ ares_status_t ares__readaddrinfo(FILE *fp,
size_t linesize; size_t linesize;
struct ares_addrinfo_cname *cname = NULL, *cnames = NULL; struct ares_addrinfo_cname *cname = NULL, *cnames = NULL;
struct ares_addrinfo_node *nodes = NULL; struct ares_addrinfo_node *nodes = NULL;
int match_with_alias, match_with_canonical; ares_bool_t match_with_alias, match_with_canonical;
int want_cname = hints->ai_flags & ARES_AI_CANONNAME; ares_bool_t want_cname = (hints->ai_flags & ARES_AI_CANONNAME)?ARES_TRUE:ARES_FALSE;
/* Validate family */ /* Validate family */
switch (hints->ai_family) { switch (hints->ai_family) {
@ -79,8 +79,8 @@ ares_status_t ares__readaddrinfo(FILE *fp,
while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS) while ((status = ares__read_line(fp, &line, &linesize)) == ARES_SUCCESS)
{ {
match_with_alias = 0; match_with_alias = ARES_FALSE;
match_with_canonical = 0; match_with_canonical = ARES_FALSE;
alias_count = 0; alias_count = 0;
/* Trim line comment. */ /* Trim line comment. */
p = line; p = line;
@ -147,7 +147,7 @@ ares_status_t ares__readaddrinfo(FILE *fp,
/* Find out if host name matches with canonical host name. */ /* Find out if host name matches with canonical host name. */
if (strcasecmp(txthost, name) == 0) if (strcasecmp(txthost, name) == 0)
{ {
match_with_canonical = 1; match_with_canonical = ARES_TRUE;
} }
/* Find out if host name matches with one of the aliases. */ /* Find out if host name matches with one of the aliases. */
@ -162,7 +162,7 @@ ares_status_t ares__readaddrinfo(FILE *fp,
*p = '\0'; *p = '\0';
if (strcasecmp(txtalias, name) == 0) if (strcasecmp(txtalias, name) == 0)
{ {
match_with_alias = 1; match_with_alias = ARES_TRUE;
if (!want_cname) if (!want_cname)
break; break;
} }

@ -90,7 +90,7 @@ ares__slist_t *ares__slist_create(ares_rand_state *rand_state,
} }
static unsigned int ares__slist_coin_flip(ares__slist_t *list) static ares_bool_t ares__slist_coin_flip(ares__slist_t *list)
{ {
size_t total_bits = sizeof(list->rand_data) * 8; size_t total_bits = sizeof(list->rand_data) * 8;
size_t bit; size_t bit;
@ -108,7 +108,7 @@ static unsigned int ares__slist_coin_flip(ares__slist_t *list)
bit = total_bits - list->rand_bits; bit = total_bits - list->rand_bits;
list->rand_bits--; list->rand_bits--;
return (list->rand_data[bit / 8] & (1 << (bit % 8)))?1:0; return (list->rand_data[bit / 8] & (1 << (bit % 8)))?ARES_TRUE:ARES_FALSE;
} }

@ -106,18 +106,3 @@ struct timeval ares__tvnow(void)
} }
#endif #endif
#if 0 /* Not used */
/*
* Make sure that the first argument is the more recent time, as otherwise
* we'll get a weird negative time-diff back...
*
* Returns: the time difference in number of milliseconds.
*/
long ares__tvdiff(struct timeval newer, struct timeval older)
{
return (newer.tv_sec-older.tv_sec)*1000+
(newer.tv_usec-older.tv_usec)/1000;
}
#endif

@ -96,7 +96,7 @@ int ares_library_init_android(jobject connectivity_manager)
JNIEnv *env = NULL; JNIEnv *env = NULL;
int need_detatch = 0; int need_detatch = 0;
int res; int res;
int ret = ARES_ENOTINITIALIZED; ares_status_t ret = ARES_ENOTINITIALIZED;
jclass obj_cls = NULL; jclass obj_cls = NULL;
if (android_jvm == NULL) if (android_jvm == NULL)

@ -41,10 +41,10 @@
#define MAX_INDIRS 50 #define MAX_INDIRS 50
static int name_length(const unsigned char *encoded, const unsigned char *abuf, static int name_length(const unsigned char *encoded, const unsigned char *abuf,
int alen, int is_hostname); int alen, ares_bool_t is_hostname);
/* Reserved characters for names that need to be escaped */ /* Reserved characters for names that need to be escaped */
static int is_reservedch(int ch) static ares_bool_t is_reservedch(int ch)
{ {
switch (ch) { switch (ch) {
case '"': case '"':
@ -55,19 +55,19 @@ static int is_reservedch(int ch)
case ')': case ')':
case '@': case '@':
case '$': case '$':
return 1; return ARES_TRUE;
default: default:
break; break;
} }
return 0; return ARES_FALSE;
} }
static int ares__isprint(int ch) static ares_bool_t ares__isprint(int ch)
{ {
if (ch >= 0x20 && ch <= 0x7E) if (ch >= 0x20 && ch <= 0x7E)
return 1; return ARES_TRUE;
return 0; return ARES_FALSE;
} }
/* Character set allowed by hostnames. This is to include the normal /* Character set allowed by hostnames. This is to include the normal
@ -82,21 +82,21 @@ static int ares__isprint(int ch)
* reported when this validation is not performed. Security is more * reported when this validation is not performed. Security is more
* important than edge-case compatibility (which is probably invalid * important than edge-case compatibility (which is probably invalid
* anyhow). */ * anyhow). */
static int is_hostnamech(int ch) static ares_bool_t is_hostnamech(int ch)
{ {
/* [A-Za-z0-9-*._/] /* [A-Za-z0-9-*._/]
* Don't use isalnum() as it is locale-specific * Don't use isalnum() as it is locale-specific
*/ */
if (ch >= 'A' && ch <= 'Z') if (ch >= 'A' && ch <= 'Z')
return 1; return ARES_TRUE;
if (ch >= 'a' && ch <= 'z') if (ch >= 'a' && ch <= 'z')
return 1; return ARES_TRUE;
if (ch >= '0' && ch <= '9') if (ch >= '0' && ch <= '9')
return 1; return ARES_TRUE;
if (ch == '-' || ch == '.' || ch == '_' || ch == '/' || ch == '*') if (ch == '-' || ch == '.' || ch == '_' || ch == '/' || ch == '*')
return 1; return ARES_TRUE;
return 0; return ARES_FALSE;
} }
/* Expand an RFC1035-encoded domain name given by encoded. The /* Expand an RFC1035-encoded domain name given by encoded. The
@ -129,14 +129,14 @@ static int is_hostnamech(int ch)
ares_status_t ares__expand_name_validated(const unsigned char *encoded, ares_status_t ares__expand_name_validated(const unsigned char *encoded,
const unsigned char *abuf, const unsigned char *abuf,
int alen, char **s, long *enclen, int alen, char **s, long *enclen,
int is_hostname) ares_bool_t is_hostname)
{ {
int len, indir = 0; int len, indir = 0;
char *q; char *q;
const unsigned char *p; const unsigned char *p;
union { union {
ares_ssize_t sig; ares_ssize_t sig;
size_t uns; size_t uns;
} nlen; } nlen;
nlen.sig = name_length(encoded, abuf, alen, is_hostname); nlen.sig = name_length(encoded, abuf, alen, is_hostname);
@ -225,14 +225,14 @@ ares_status_t ares__expand_name_validated(const unsigned char *encoded,
int ares_expand_name(const unsigned char *encoded, const unsigned char *abuf, int ares_expand_name(const unsigned char *encoded, const unsigned char *abuf,
int alen, char **s, long *enclen) int alen, char **s, long *enclen)
{ {
return ares__expand_name_validated(encoded, abuf, alen, s, enclen, 0); return ares__expand_name_validated(encoded, abuf, alen, s, enclen, ARES_FALSE);
} }
/* Return the length of the expansion of an encoded domain name, or /* Return the length of the expansion of an encoded domain name, or
* -1 if the encoding is invalid. * -1 if the encoding is invalid.
*/ */
static int name_length(const unsigned char *encoded, const unsigned char *abuf, static int name_length(const unsigned char *encoded, const unsigned char *abuf,
int alen, int is_hostname) int alen, ares_bool_t is_hostname)
{ {
int n = 0, offset, indir = 0, top; int n = 0, offset, indir = 0, top;
@ -313,7 +313,7 @@ static int name_length(const unsigned char *encoded, const unsigned char *abuf,
ares_status_t ares__expand_name_for_response(const unsigned char *encoded, ares_status_t ares__expand_name_for_response(const unsigned char *encoded,
const unsigned char *abuf, const unsigned char *abuf,
int alen, char **s, long *enclen, int alen, char **s, long *enclen,
int is_hostname) ares_bool_t is_hostname)
{ {
ares_status_t status = ares__expand_name_validated(encoded, abuf, alen, s, ares_status_t status = ares__expand_name_validated(encoded, abuf, alen, s,
enclen, is_hostname); enclen, is_hostname);

@ -123,9 +123,9 @@ static const struct ares_addrinfo empty_addrinfo = {
/* forward declarations */ /* forward declarations */
static void host_callback(void *arg, int status, int timeouts, static void host_callback(void *arg, int status, int timeouts,
unsigned char *abuf, int alen); unsigned char *abuf, int alen);
static int as_is_first(const struct host_query *hquery); static ares_bool_t as_is_first(const struct host_query *hquery);
static int as_is_only(const struct host_query* hquery); static ares_bool_t as_is_only(const struct host_query* hquery);
static int next_dns_lookup(struct host_query *hquery); static ares_bool_t next_dns_lookup(struct host_query *hquery);
static struct ares_addrinfo_cname *ares__malloc_addrinfo_cname(void) static struct ares_addrinfo_cname *ares__malloc_addrinfo_cname(void)
{ {
@ -419,25 +419,25 @@ static void end_hquery(struct host_query *hquery, ares_status_t status)
ares_free(hquery); ares_free(hquery);
} }
static int is_localhost(const char *name) static ares_bool_t is_localhost(const char *name)
{ {
/* RFC6761 6.3 says : The domain "localhost." and any names falling within ".localhost." */ /* RFC6761 6.3 says : The domain "localhost." and any names falling within ".localhost." */
size_t len; size_t len;
if (name == NULL) if (name == NULL)
return 0; return ARES_FALSE;
if (strcmp(name, "localhost") == 0) if (strcmp(name, "localhost") == 0)
return 1; return ARES_TRUE;
len = strlen(name); len = strlen(name);
if (len < 10 /* strlen(".localhost") */) if (len < 10 /* strlen(".localhost") */)
return 0; return ARES_FALSE;
if (strcmp(name + (len - 10 /* strlen(".localhost") */), ".localhost") == 0) if (strcmp(name + (len - 10 /* strlen(".localhost") */), ".localhost") == 0)
return 1; return ARES_TRUE;
return 0; return ARES_FALSE;
} }
static ares_status_t file_lookup(struct host_query *hquery) static ares_status_t file_lookup(struct host_query *hquery)
@ -593,7 +593,7 @@ static void terminate_retries(struct host_query *hquery, unsigned short qid)
if (query == NULL) if (query == NULL)
return; return;
query->no_retries = 1; query->no_retries = ARES_TRUE;
} }
@ -772,10 +772,10 @@ void ares_getaddrinfo(ares_channel channel,
next_lookup(hquery, ARES_ECONNREFUSED /* initial error code */); next_lookup(hquery, ARES_ECONNREFUSED /* initial error code */);
} }
static int next_dns_lookup(struct host_query *hquery) static ares_bool_t next_dns_lookup(struct host_query *hquery)
{ {
char *s = NULL; char *s = NULL;
int is_s_allocated = 0; ares_bool_t is_s_allocated = ARES_FALSE;
ares_status_t status; ares_status_t status;
/* if next_domain == -1 and as_is_first is true, try hquery->name */ /* if next_domain == -1 and as_is_first is true, try hquery->name */
@ -805,7 +805,7 @@ static int next_dns_lookup(struct host_query *hquery)
&s); &s);
if (status == ARES_SUCCESS) if (status == ARES_SUCCESS)
{ {
is_s_allocated = 1; is_s_allocated = ARES_TRUE;
} }
} }
@ -838,16 +838,16 @@ static int next_dns_lookup(struct host_query *hquery)
{ {
ares_free(s); ares_free(s);
} }
return 1; return ARES_TRUE;
} }
else else
{ {
assert(!hquery->ai->nodes); assert(!hquery->ai->nodes);
return 0; return ARES_FALSE;
} }
} }
static int as_is_first(const struct host_query* hquery) static ares_bool_t as_is_first(const struct host_query* hquery)
{ {
char* p; char* p;
int ndots = 0; int ndots = 0;
@ -862,16 +862,16 @@ static int as_is_first(const struct host_query* hquery)
if (nname && hquery->name[nname-1] == '.') if (nname && hquery->name[nname-1] == '.')
{ {
/* prevent ARES_EBADNAME for valid FQDN, where ndots < channel->ndots */ /* prevent ARES_EBADNAME for valid FQDN, where ndots < channel->ndots */
return 1; return ARES_TRUE;
} }
return ndots >= hquery->channel->ndots; return ndots >= hquery->channel->ndots?ARES_TRUE:ARES_FALSE;
} }
static int as_is_only(const struct host_query* hquery) static ares_bool_t as_is_only(const struct host_query* hquery)
{ {
size_t nname = hquery->name?strlen(hquery->name):0; size_t nname = hquery->name?strlen(hquery->name):0;
if (nname && hquery->name[nname-1] == '.') if (nname && hquery->name[nname-1] == '.')
return 1; return ARES_TRUE;
return 0; return ARES_FALSE;
} }

@ -445,13 +445,13 @@ STATIC_TESTABLE char *ares_striendstr(const char *s1, const char *s2)
return (char *)c1_begin; return (char *)c1_begin;
} }
int ares__is_onion_domain(const char *name) ares_bool_t ares__is_onion_domain(const char *name)
{ {
if (ares_striendstr(name, ".onion")) if (ares_striendstr(name, ".onion"))
return 1; return ARES_TRUE;
if (ares_striendstr(name, ".onion.")) if (ares_striendstr(name, ".onion."))
return 1; return ARES_TRUE;
return 0; return ARES_FALSE;
} }

@ -89,8 +89,8 @@ static const char *try_option(const char *p, const char *q, const char *opt);
static ares_status_t config_sortlist(struct apattern **sortlist, int *nsort, static ares_status_t config_sortlist(struct apattern **sortlist, int *nsort,
const char *str); const char *str);
static int sortlist_alloc(struct apattern **sortlist, int *nsort, static ares_bool_t sortlist_alloc(struct apattern **sortlist, int *nsort,
struct apattern *pat); struct apattern *pat);
static int ip_addr(const char *s, ares_ssize_t len, struct in_addr *addr); static int ip_addr(const char *s, ares_ssize_t len, struct in_addr *addr);
static void natural_mask(struct apattern *pat); static void natural_mask(struct apattern *pat);
#if !defined(WIN32) && !defined(WATT32) && \ #if !defined(WIN32) && !defined(WATT32) && \
@ -639,7 +639,7 @@ static ares_status_t init_by_environment(ares_channel channel)
* *
* Supported on Windows NT 3.5 and newer. * Supported on Windows NT 3.5 and newer.
*/ */
static int get_REG_SZ(HKEY hKey, const char *leafKeyName, char **outptr) static ares_bool_t get_REG_SZ(HKEY hKey, const char *leafKeyName, char **outptr)
{ {
DWORD size = 0; DWORD size = 0;
int res; int res;
@ -649,13 +649,13 @@ static int get_REG_SZ(HKEY hKey, const char *leafKeyName, char **outptr)
/* Find out size of string stored in registry */ /* Find out size of string stored in registry */
res = RegQueryValueExA(hKey, leafKeyName, 0, NULL, NULL, &size); res = RegQueryValueExA(hKey, leafKeyName, 0, NULL, NULL, &size);
if ((res != ERROR_SUCCESS && res != ERROR_MORE_DATA) || !size) if ((res != ERROR_SUCCESS && res != ERROR_MORE_DATA) || !size)
return 0; return ARES_FALSE;
/* Allocate buffer of indicated size plus one given that string /* Allocate buffer of indicated size plus one given that string
might have been stored without null termination */ might have been stored without null termination */
*outptr = ares_malloc(size+1); *outptr = ares_malloc(size+1);
if (!*outptr) if (!*outptr)
return 0; return ARES_FALSE;
/* Get the value for real */ /* Get the value for real */
res = RegQueryValueExA(hKey, leafKeyName, 0, NULL, res = RegQueryValueExA(hKey, leafKeyName, 0, NULL,
@ -664,13 +664,13 @@ static int get_REG_SZ(HKEY hKey, const char *leafKeyName, char **outptr)
{ {
ares_free(*outptr); ares_free(*outptr);
*outptr = NULL; *outptr = NULL;
return 0; return ARES_FALSE;
} }
/* Null terminate buffer allways */ /* Null terminate buffer allways */
*(*outptr + size) = '\0'; *(*outptr + size) = '\0';
return 1; return ARES_TRUE;
} }
static void commanjoin(char** dst, const char* const src, const size_t len) static void commanjoin(char** dst, const char* const src, const size_t len)
@ -837,7 +837,7 @@ static ULONG getBestRouteMetric(IF_LUID * const luid, /* Can't be const :( */
*/ */
#define IPAA_INITIAL_BUF_SZ 15 * 1024 #define IPAA_INITIAL_BUF_SZ 15 * 1024
#define IPAA_MAX_TRIES 3 #define IPAA_MAX_TRIES 3
static int get_DNS_Windows(char **outptr) static ares_bool_t get_DNS_Windows(char **outptr)
{ {
IP_ADAPTER_DNS_SERVER_ADDRESS *ipaDNSAddr; IP_ADAPTER_DNS_SERVER_ADDRESS *ipaDNSAddr;
IP_ADAPTER_ADDRESSES *ipaa, *newipaa, *ipaaEntry; IP_ADAPTER_ADDRESSES *ipaa, *newipaa, *ipaaEntry;
@ -864,7 +864,7 @@ static int get_DNS_Windows(char **outptr)
ipaa = ares_malloc(Bufsz); ipaa = ares_malloc(Bufsz);
if (!ipaa) if (!ipaa)
return 0; return ARES_FALSE;
/* Start with enough room for a few DNS server addresses and we'll grow it /* Start with enough room for a few DNS server addresses and we'll grow it
* as we encounter more. * as we encounter more.
@ -874,7 +874,7 @@ static int get_DNS_Windows(char **outptr)
if(addresses == NULL) { if(addresses == NULL) {
/* We need room for at least some addresses to function. */ /* We need room for at least some addresses to function. */
ares_free(ipaa); ares_free(ipaa);
return 0; return ARES_FALSE;
} }
/* Usually this call suceeds with initial buffer size */ /* Usually this call suceeds with initial buffer size */
@ -1028,10 +1028,10 @@ done:
ares_free(ipaa); ares_free(ipaa);
if (!*outptr) { if (!*outptr) {
return 0; return ARES_FALSE;
} }
return 1; return ARES_TRUE;
} }
/* /*
@ -1047,7 +1047,7 @@ done:
* *
* Implementation supports Windows Server 2003 and newer * Implementation supports Windows Server 2003 and newer
*/ */
static int get_SuffixList_Windows(char **outptr) static ares_bool_t get_SuffixList_Windows(char **outptr)
{ {
HKEY hKey, hKeyEnum; HKEY hKey, hKeyEnum;
char keyName[256]; char keyName[256];
@ -1058,7 +1058,7 @@ static int get_SuffixList_Windows(char **outptr)
*outptr = NULL; *outptr = NULL;
if (ares__getplatform() != WIN_NT) if (ares__getplatform() != WIN_NT)
return 0; return ARES_FALSE;
/* 1. Global DNS Suffix Search List */ /* 1. Global DNS Suffix Search List */
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0, if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, WIN_NS_NT_KEY, 0,
@ -1138,7 +1138,7 @@ static int get_SuffixList_Windows(char **outptr)
RegCloseKey(hKey); RegCloseKey(hKey);
} }
return *outptr != NULL; return *outptr != NULL?ARES_TRUE:ARES_FALSE;
} }
#endif #endif
@ -1791,7 +1791,7 @@ static ares_status_t config_lookup(ares_channel channel, const char *str,
{ {
char lookups[3], *l; char lookups[3], *l;
const char *vqualifier p; const char *vqualifier p;
int found; ares_bool_t found;
if (altbindch == NULL) if (altbindch == NULL)
altbindch = bindch; altbindch = bindch;
@ -1802,13 +1802,13 @@ static ares_status_t config_lookup(ares_channel channel, const char *str,
*/ */
l = lookups; l = lookups;
p = str; p = str;
found = 0; found = ARES_FALSE;
while (*p) while (*p)
{ {
if ((*p == *bindch || *p == *altbindch || *p == *filech) && l < lookups + 2) { if ((*p == *bindch || *p == *altbindch || *p == *filech) && l < lookups + 2) {
if (*p == *bindch || *p == *altbindch) *l++ = 'b'; if (*p == *bindch || *p == *altbindch) *l++ = 'b';
else *l++ = 'f'; else *l++ = 'f';
found = 1; found = ARES_TRUE;
} }
while (*p && !ISSPACE(*p) && (*p != ',')) while (*p && !ISSPACE(*p) && (*p != ','))
p++; p++;
@ -1828,7 +1828,7 @@ static ares_status_t config_lookup(ares_channel channel, const char *str,
* 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).
*/ */
static int ares_ipv6_subnet_matches(const unsigned char netbase[16], static ares_bool_t ares_ipv6_subnet_matches(const unsigned char netbase[16],
unsigned char netmask, unsigned char netmask,
const unsigned char ipaddr[16]) const unsigned char ipaddr[16])
{ {
@ -1837,7 +1837,7 @@ static int ares_ipv6_subnet_matches(const unsigned char netbase[16],
/* Misuse */ /* Misuse */
if (netmask > 128) if (netmask > 128)
return 0; return ARES_FALSE;
/* Quickly set whole bytes */ /* Quickly set whole bytes */
memset(mask, 0xFF, netmask / 8); memset(mask, 0xFF, netmask / 8);
@ -1849,14 +1849,14 @@ static int ares_ipv6_subnet_matches(const unsigned char netbase[16],
for (i=0; i<16; i++) { for (i=0; i<16; i++) {
if ((netbase[i] & mask[i]) != (ipaddr[i] & mask[i])) if ((netbase[i] & mask[i]) != (ipaddr[i] & mask[i]))
return 0; return ARES_FALSE;
} }
return 1; return ARES_TRUE;
} }
/* Return true iff the IPv6 ipaddr is blacklisted. */ /* Return true iff the IPv6 ipaddr is blacklisted. */
static int ares_ipv6_server_blacklisted(const unsigned char ipaddr[16]) static ares_bool_t ares_ipv6_server_blacklisted(const unsigned char ipaddr[16])
{ {
/* A list of blacklisted IPv6 subnets. */ /* A list of blacklisted IPv6 subnets. */
const struct { const struct {
@ -1881,9 +1881,9 @@ static int ares_ipv6_server_blacklisted(const unsigned char ipaddr[16])
for (i = 0; i < sizeof(blacklist) / sizeof(blacklist[0]); ++i) { for (i = 0; i < sizeof(blacklist) / sizeof(blacklist[0]); ++i) {
if (ares_ipv6_subnet_matches( if (ares_ipv6_subnet_matches(
blacklist[i].netbase, blacklist[i].netmask, ipaddr)) blacklist[i].netbase, blacklist[i].netmask, ipaddr))
return 1; return ARES_TRUE;
} }
return 0; return ARES_FALSE;
} }
/* Parse address and port in these formats, either ipv4 or ipv6 addresses /* Parse address and port in these formats, either ipv4 or ipv6 addresses
@ -2317,17 +2317,17 @@ static void natural_mask(struct apattern *pat)
pat->mask.addr4.s_addr = htonl(IN_CLASSC_NET); pat->mask.addr4.s_addr = htonl(IN_CLASSC_NET);
} }
static int sortlist_alloc(struct apattern **sortlist, int *nsort, static ares_bool_t sortlist_alloc(struct apattern **sortlist, int *nsort,
struct apattern *pat) struct apattern *pat)
{ {
struct apattern *newsort; struct apattern *newsort;
newsort = ares_realloc(*sortlist, (*nsort + 1) * sizeof(struct apattern)); newsort = ares_realloc(*sortlist, (*nsort + 1) * sizeof(struct apattern));
if (!newsort) if (!newsort)
return 0; return ARES_FALSE;
newsort[*nsort] = *pat; newsort[*nsort] = *pat;
*sortlist = newsort; *sortlist = newsort;
(*nsort)++; (*nsort)++;
return 1; return ARES_TRUE;
} }

@ -163,7 +163,7 @@ struct server_state;
struct server_connection { struct server_connection {
struct server_state *server; struct server_state *server;
ares_socket_t fd; ares_socket_t fd;
int is_tcp; ares_bool_t is_tcp;
/* total number of queries run on this connection since it was established */ /* total number of queries run on this connection since it was established */
size_t total_queries; size_t total_queries;
/* list of outstanding queries to this connection */ /* list of outstanding queries to this connection */
@ -226,16 +226,16 @@ struct query {
int try_count; /* Number of times we tried this query already. */ int try_count; /* Number of times we tried this query already. */
int server; /* Server this query has last been sent to. */ int server; /* Server this query has last been sent to. */
struct query_server_info *server_info; /* per-server state */ struct query_server_info *server_info; /* per-server state */
int using_tcp; ares_bool_t using_tcp;
ares_status_t error_status; ares_status_t error_status;
int timeouts; /* number of timeouts we saw for this request */ int timeouts; /* number of timeouts we saw for this request */
int no_retries; /* do not perform any additional retries, this is set when ares_bool_t no_retries; /* do not perform any additional retries, this is set when
* a query is to be canceled */ * a query is to be canceled */
}; };
/* Per-server state for a query */ /* Per-server state for a query */
struct query_server_info { struct query_server_info {
int skip_server; /* should we skip server, due to errors, etc? */ ares_bool_t skip_server; /* should we skip server, due to errors, etc? */
int tcp_connection_generation; /* into which TCP connection did we send? */ int tcp_connection_generation; /* into which TCP connection did we send? */
}; };
@ -336,7 +336,7 @@ struct ares_channeldata {
}; };
/* Does the domain end in ".onion" or ".onion."? Case-insensitive. */ /* Does the domain end in ".onion" or ".onion."? Case-insensitive. */
int ares__is_onion_domain(const char *name); ares_bool_t ares__is_onion_domain(const char *name);
/* Memory management functions */ /* Memory management functions */
extern void *(*ares_malloc)(size_t size); extern void *(*ares_malloc)(size_t size);
@ -344,8 +344,8 @@ extern void *(*ares_realloc)(void *ptr, size_t size);
extern void (*ares_free)(void *ptr); extern void (*ares_free)(void *ptr);
/* return true if now is exactly check time or later */ /* return true if now is exactly check time or later */
int ares__timedout(struct timeval *now, ares_bool_t ares__timedout(struct timeval *now,
struct timeval *check); struct timeval *check);
/* Returns one of the normal ares status codes like ARES_SUCCESS */ /* Returns one of the normal ares status codes like ARES_SUCCESS */
ares_status_t ares__send_query(ares_channel channel, struct query *query, ares_status_t ares__send_query(ares_channel channel, struct query *query,
@ -377,11 +377,11 @@ struct timeval ares__tvnow(void);
ares_status_t ares__expand_name_validated(const unsigned char *encoded, ares_status_t ares__expand_name_validated(const unsigned char *encoded,
const unsigned char *abuf, const unsigned char *abuf,
int alen, char **s, long *enclen, int alen, char **s, long *enclen,
int is_hostname); ares_bool_t is_hostname);
ares_status_t ares__expand_name_for_response(const unsigned char *encoded, ares_status_t ares__expand_name_for_response(const unsigned char *encoded,
const unsigned char *abuf, const unsigned char *abuf,
int alen, char **s, long *enclen, int alen, char **s, long *enclen,
int is_hostname); ares_bool_t is_hostname);
ares_status_t ares__init_servers_state(ares_channel channel); ares_status_t ares__init_servers_state(ares_channel channel);
void ares__destroy_servers_state(ares_channel channel); void ares__destroy_servers_state(ares_channel channel);
ares_status_t ares__single_domain(ares_channel channel, const char *name, ares_status_t ares__single_domain(ares_channel channel, const char *name,
@ -412,7 +412,8 @@ void ares__addrinfo_cat_cnames(struct ares_addrinfo_cname **head,
struct ares_addrinfo_cname *tail); struct ares_addrinfo_cname *tail);
ares_status_t ares__parse_into_addrinfo(const unsigned char *abuf, ares_status_t ares__parse_into_addrinfo(const unsigned char *abuf,
int alen, int cname_only_is_enodata, int alen,
ares_bool_t cname_only_is_enodata,
unsigned short port, unsigned short port,
struct ares_addrinfo *ai); struct ares_addrinfo *ai);
@ -427,10 +428,6 @@ ares_status_t ares__addrinfo_localhost(const char *name, unsigned short port,
const struct ares_addrinfo_hints *hints, const struct ares_addrinfo_hints *hints,
struct ares_addrinfo *ai); struct ares_addrinfo *ai);
#if 0 /* Not used */
long ares__tvdiff(struct timeval t1, struct timeval t2);
#endif
ares_socket_t ares__open_socket(ares_channel channel, ares_socket_t ares__open_socket(ares_channel channel,
int af, int type, int protocol); int af, int type, int protocol);
void ares__close_socket(ares_channel, ares_socket_t); void ares__close_socket(ares_channel, ares_socket_t);

@ -65,7 +65,7 @@
#include "ares_private.h" #include "ares_private.h"
static int try_again(int errnum); static ares_bool_t try_again(int errnum);
static void write_tcp_data(ares_channel channel, fd_set *write_fds, static void write_tcp_data(ares_channel channel, fd_set *write_fds,
ares_socket_t write_fd, struct timeval *now); ares_socket_t write_fd, struct timeval *now);
static void read_packets(ares_channel channel, fd_set *read_fds, static void read_packets(ares_channel channel, fd_set *read_fds,
@ -80,29 +80,29 @@ static void skip_server(ares_channel channel, struct query *query,
static ares_status_t next_server(ares_channel channel, struct query *query, static ares_status_t next_server(ares_channel channel, struct query *query,
struct timeval *now); struct timeval *now);
static ares_status_t open_socket(ares_channel channel, static ares_status_t open_socket(ares_channel channel,
struct server_state *server, int is_tcp); struct server_state *server,
static int same_questions(const unsigned char *qbuf, int qlen, ares_bool_t is_tcp);
const unsigned char *abuf, int alen); static ares_bool_t same_questions(const unsigned char *qbuf, int qlen,
static int same_address(struct sockaddr *sa, struct ares_addr *aa); const unsigned char *abuf, int alen);
static ares_bool_t same_address(struct sockaddr *sa, struct ares_addr *aa);
static int has_opt_rr(const unsigned char *abuf, int alen); static int has_opt_rr(const unsigned char *abuf, int alen);
static void end_query(ares_channel channel, struct query *query, int status, static void end_query(ares_channel channel, struct query *query,
const unsigned char *abuf, int alen); ares_status_t status, const unsigned char *abuf, int alen);
static ares_ssize_t ares__socket_write(ares_channel channel, ares_socket_t s, static ares_ssize_t ares__socket_write(ares_channel channel, ares_socket_t s,
const void * data, size_t len); const void * data, size_t len);
/* return true if now is exactly check time or later */ /* return true if now is exactly check time or later */
int ares__timedout(struct timeval *now, ares_bool_t ares__timedout(struct timeval *now, struct timeval *check)
struct timeval *check)
{ {
long secs = (now->tv_sec - check->tv_sec); long secs = (now->tv_sec - check->tv_sec);
if(secs > 0) if(secs > 0)
return 1; /* yes, timed out */ return ARES_TRUE; /* yes, timed out */
if(secs < 0) if(secs < 0)
return 0; /* nope, not timed out */ return ARES_FALSE; /* nope, not timed out */
/* if the full seconds were identical, check the sub second parts */ /* if the full seconds were identical, check the sub second parts */
return (now->tv_usec - check->tv_usec >= 0); return (now->tv_usec - check->tv_usec) >= 0?ARES_TRUE:ARES_FALSE;
} }
/* add the specific number of milliseconds to the time in the first argument */ /* add the specific number of milliseconds to the time in the first argument */
@ -158,7 +158,7 @@ void ares_process_fd(ares_channel channel,
* http://devrsrc1.external.hp.com/STKS/cgi-bin/man2html? * http://devrsrc1.external.hp.com/STKS/cgi-bin/man2html?
* manpage=/usr/share/man/man2.Z/send.2 * manpage=/usr/share/man/man2.Z/send.2
*/ */
static int try_again(int errnum) static ares_bool_t try_again(int errnum)
{ {
#if !defined EWOULDBLOCK && !defined EAGAIN #if !defined EWOULDBLOCK && !defined EAGAIN
#error "Neither EWOULDBLOCK nor EAGAIN defined" #error "Neither EWOULDBLOCK nor EAGAIN defined"
@ -167,14 +167,14 @@ static int try_again(int errnum)
{ {
#ifdef EWOULDBLOCK #ifdef EWOULDBLOCK
case EWOULDBLOCK: case EWOULDBLOCK:
return 1; return ARES_TRUE;
#endif #endif
#if defined EAGAIN && EAGAIN != EWOULDBLOCK #if defined EAGAIN && EAGAIN != EWOULDBLOCK
case EAGAIN: case EAGAIN:
return 1; return ARES_TRUE;
#endif #endif
} }
return 0; return ARES_FALSE;
} }
@ -634,7 +634,7 @@ static void process_answer(ares_channel channel, const unsigned char *abuf,
{ {
if (!query->using_tcp) if (!query->using_tcp)
{ {
query->using_tcp = 1; query->using_tcp = ARES_TRUE;
ares__send_query(channel, query, now); ares__send_query(channel, query, now);
} }
ares__check_cleanup_conn(channel, fd); ares__check_cleanup_conn(channel, fd);
@ -721,7 +721,7 @@ static void skip_server(ares_channel channel, struct query *query,
*/ */
if (channel->nservers > 1) if (channel->nservers > 1)
{ {
query->server_info[server->idx].skip_server = 1; query->server_info[server->idx].skip_server = ARES_TRUE;
} }
} }
@ -1052,7 +1052,8 @@ static int configure_socket(ares_socket_t s, int family, ares_channel channel)
} }
static ares_status_t open_socket(ares_channel channel, static ares_status_t open_socket(ares_channel channel,
struct server_state *server, int is_tcp) struct server_state *server,
ares_bool_t is_tcp)
{ {
ares_socket_t s; ares_socket_t s;
int opt; int opt;
@ -1206,8 +1207,8 @@ static ares_status_t open_socket(ares_channel channel,
} }
static int same_questions(const unsigned char *qbuf, int qlen, static ares_bool_t same_questions(const unsigned char *qbuf, int qlen,
const unsigned char *abuf, int alen) const unsigned char *abuf, int alen)
{ {
struct { struct {
const unsigned char *p; const unsigned char *p;
@ -1220,13 +1221,13 @@ static int same_questions(const unsigned char *qbuf, int qlen,
int i, j; int i, j;
if (qlen < HFIXEDSZ || alen < HFIXEDSZ) if (qlen < HFIXEDSZ || alen < HFIXEDSZ)
return 0; return ARES_FALSE;
/* Extract qdcount from the request and reply buffers and compare them. */ /* Extract qdcount from the request and reply buffers and compare them. */
q.qdcount = DNS_HEADER_QDCOUNT(qbuf); q.qdcount = DNS_HEADER_QDCOUNT(qbuf);
a.qdcount = DNS_HEADER_QDCOUNT(abuf); a.qdcount = DNS_HEADER_QDCOUNT(abuf);
if (q.qdcount != a.qdcount) if (q.qdcount != a.qdcount)
return 0; return ARES_FALSE;
/* For each question in qbuf, find it in abuf. */ /* For each question in qbuf, find it in abuf. */
q.p = qbuf + HFIXEDSZ; q.p = qbuf + HFIXEDSZ;
@ -1235,12 +1236,12 @@ static int same_questions(const unsigned char *qbuf, int qlen,
/* Decode the question in the query. */ /* Decode the question in the query. */
if (ares_expand_name(q.p, qbuf, qlen, &q.name, &q.namelen) if (ares_expand_name(q.p, qbuf, qlen, &q.name, &q.namelen)
!= ARES_SUCCESS) != ARES_SUCCESS)
return 0; return ARES_FALSE;
q.p += q.namelen; q.p += q.namelen;
if (q.p + QFIXEDSZ > qbuf + qlen) if (q.p + QFIXEDSZ > qbuf + qlen)
{ {
ares_free(q.name); ares_free(q.name);
return 0; return ARES_FALSE;
} }
q.type = DNS_QUESTION_TYPE(q.p); q.type = DNS_QUESTION_TYPE(q.p);
q.dnsclass = DNS_QUESTION_CLASS(q.p); q.dnsclass = DNS_QUESTION_CLASS(q.p);
@ -1255,14 +1256,14 @@ static int same_questions(const unsigned char *qbuf, int qlen,
!= ARES_SUCCESS) != ARES_SUCCESS)
{ {
ares_free(q.name); ares_free(q.name);
return 0; return ARES_FALSE;
} }
a.p += a.namelen; a.p += a.namelen;
if (a.p + QFIXEDSZ > abuf + alen) if (a.p + QFIXEDSZ > abuf + alen)
{ {
ares_free(q.name); ares_free(q.name);
ares_free(a.name); ares_free(a.name);
return 0; return ARES_FALSE;
} }
a.type = DNS_QUESTION_TYPE(a.p); a.type = DNS_QUESTION_TYPE(a.p);
a.dnsclass = DNS_QUESTION_CLASS(a.p); a.dnsclass = DNS_QUESTION_CLASS(a.p);
@ -1280,12 +1281,12 @@ static int same_questions(const unsigned char *qbuf, int qlen,
ares_free(q.name); ares_free(q.name);
if (j == a.qdcount) if (j == a.qdcount)
return 0; return ARES_FALSE;
} }
return 1; return ARES_TRUE;
} }
static int same_address(struct sockaddr *sa, struct ares_addr *aa) static ares_bool_t same_address(struct sockaddr *sa, struct ares_addr *aa)
{ {
void *addr1; void *addr1;
void *addr2; void *addr2;
@ -1298,19 +1299,19 @@ static int same_address(struct sockaddr *sa, struct ares_addr *aa)
addr1 = &aa->addrV4; addr1 = &aa->addrV4;
addr2 = &(CARES_INADDR_CAST(struct sockaddr_in *, sa))->sin_addr; addr2 = &(CARES_INADDR_CAST(struct sockaddr_in *, sa))->sin_addr;
if (memcmp(addr1, addr2, sizeof(aa->addrV4)) == 0) if (memcmp(addr1, addr2, sizeof(aa->addrV4)) == 0)
return 1; /* match */ return ARES_TRUE; /* match */
break; break;
case AF_INET6: case AF_INET6:
addr1 = &aa->addrV6; addr1 = &aa->addrV6;
addr2 = &(CARES_INADDR_CAST(struct sockaddr_in6 *, sa))->sin6_addr; addr2 = &(CARES_INADDR_CAST(struct sockaddr_in6 *, sa))->sin6_addr;
if (memcmp(addr1, addr2, sizeof(aa->addrV6)) == 0) if (memcmp(addr1, addr2, sizeof(aa->addrV6)) == 0)
return 1; /* match */ return ARES_TRUE; /* match */
break; break;
default: default:
break; /* LCOV_EXCL_LINE */ break; /* LCOV_EXCL_LINE */
} }
} }
return 0; /* different */ return ARES_FALSE; /* different */
} }
/* search for an OPT RR in the response */ /* search for an OPT RR in the response */
@ -1404,8 +1405,8 @@ static void ares_detach_query(struct query *query)
query->node_all_queries = NULL; query->node_all_queries = NULL;
} }
static void end_query(ares_channel channel, struct query *query, int status, static void end_query(ares_channel channel, struct query *query,
const unsigned char *abuf, int alen) ares_status_t status, const unsigned char *abuf, int alen)
{ {
(void)channel; (void)channel;

@ -186,19 +186,19 @@ BOOLEAN WINAPI SystemFunction036(PVOID RandomBuffer, ULONG RandomBufferLength);
#endif #endif
static int ares__init_rand_engine(ares_rand_state *state) static ares_bool_t ares__init_rand_engine(ares_rand_state *state)
{ {
memset(state, 0, sizeof(*state)); memset(state, 0, sizeof(*state));
#if defined(HAVE_ARC4RANDOM_BUF) || defined(HAVE_GETRANDOM) || defined(_WIN32) #if defined(HAVE_ARC4RANDOM_BUF) || defined(HAVE_GETRANDOM) || defined(_WIN32)
state->type = ARES_RAND_OS; state->type = ARES_RAND_OS;
return 1; return ARES_TRUE;
#elif defined(CARES_RANDOM_FILE) #elif defined(CARES_RANDOM_FILE)
state->type = ARES_RAND_FILE; state->type = ARES_RAND_FILE;
state->state.rand_file = fopen(CARES_RANDOM_FILE, "rb"); state->state.rand_file = fopen(CARES_RANDOM_FILE, "rb");
if (state->state.rand_file) { if (state->state.rand_file) {
setvbuf(state->state.rand_file, NULL, _IONBF, 0); setvbuf(state->state.rand_file, NULL, _IONBF, 0);
return 1; return ARES_TRUE;
} }
/* Fall-Thru on failure to RC4 */ /* Fall-Thru on failure to RC4 */
#endif #endif
@ -208,7 +208,7 @@ static int ares__init_rand_engine(ares_rand_state *state)
ares_rc4_init(&state->state.rc4); ares_rc4_init(&state->state.rc4);
/* Currently cannot fail */ /* Currently cannot fail */
return 1; return ARES_TRUE;
#endif #endif
} }

@ -45,9 +45,9 @@ struct search_query {
int status_as_is; /* error status from trying as-is */ int status_as_is; /* error status from trying as-is */
int next_domain; /* next search domain to try */ int next_domain; /* next search domain to try */
int trying_as_is; /* current query is for name as-is */ ares_bool_t trying_as_is; /* current query is for name as-is */
int timeouts; /* number of timeouts we saw for this request */ int timeouts; /* number of timeouts we saw for this request */
int ever_got_nodata; /* did we ever get ARES_ENODATA along the way? */ ares_bool_t ever_got_nodata; /* did we ever get ARES_ENODATA along the way? */
}; };
static void search_callback(void *arg, int status, int timeouts, static void search_callback(void *arg, int status, int timeouts,
@ -110,7 +110,7 @@ void ares_search(ares_channel channel, const char *name, int dnsclass,
squery->callback = callback; squery->callback = callback;
squery->arg = arg; squery->arg = arg;
squery->timeouts = 0; squery->timeouts = 0;
squery->ever_got_nodata = 0; squery->ever_got_nodata = ARES_FALSE;
/* Count the number of dots in name. */ /* Count the number of dots in name. */
ndots = 0; ndots = 0;
@ -128,14 +128,14 @@ void ares_search(ares_channel channel, const char *name, int dnsclass,
{ {
/* Try the name as-is first. */ /* Try the name as-is first. */
squery->next_domain = 0; squery->next_domain = 0;
squery->trying_as_is = 1; squery->trying_as_is = ARES_TRUE;
ares_query(channel, name, dnsclass, type, search_callback, squery); ares_query(channel, name, dnsclass, type, search_callback, squery);
} }
else else
{ {
/* Try the name as-is last; start with the first search domain. */ /* Try the name as-is last; start with the first search domain. */
squery->next_domain = 1; squery->next_domain = 1;
squery->trying_as_is = 0; squery->trying_as_is = ARES_FALSE;
status = ares__cat_domain(name, channel->domains[0], &s); status = ares__cat_domain(name, channel->domains[0], &s);
if (status == ARES_SUCCESS) if (status == ARES_SUCCESS)
{ {
@ -179,7 +179,7 @@ static void search_callback(void *arg, int status, int timeouts,
* returned ARES_ENOTFOUND. * returned ARES_ENOTFOUND.
*/ */
if (status == ARES_ENODATA) if (status == ARES_ENODATA)
squery->ever_got_nodata = 1; squery->ever_got_nodata = ARES_TRUE;
if (squery->next_domain < channel->ndomains) if (squery->next_domain < channel->ndomains)
{ {
@ -190,7 +190,7 @@ static void search_callback(void *arg, int status, int timeouts,
end_squery(squery, status, NULL, 0); end_squery(squery, status, NULL, 0);
else else
{ {
squery->trying_as_is = 0; squery->trying_as_is = ARES_FALSE;
squery->next_domain++; squery->next_domain++;
ares_query(channel, s, squery->dnsclass, squery->type, ares_query(channel, s, squery->dnsclass, squery->type,
search_callback, squery); search_callback, squery);
@ -200,7 +200,7 @@ static void search_callback(void *arg, int status, int timeouts,
else if (squery->status_as_is == -1) else if (squery->status_as_is == -1)
{ {
/* Try the name as-is at the end. */ /* Try the name as-is at the end. */
squery->trying_as_is = 1; squery->trying_as_is = ARES_TRUE;
ares_query(channel, squery->name, squery->dnsclass, squery->type, ares_query(channel, squery->name, squery->dnsclass, squery->type,
search_callback, squery); search_callback, squery);
} }

@ -111,7 +111,7 @@ ares_status_t ares_send_ex(ares_channel channel, const unsigned char *qbuf,
for (i = 0; i < channel->nservers; i++) for (i = 0; i < channel->nservers; i++)
{ {
query->server_info[i].skip_server = 0; query->server_info[i].skip_server = ARES_FALSE;
query->server_info[i].tcp_connection_generation = 0; query->server_info[i].tcp_connection_generation = 0;
} }

Loading…
Cancel
Save