diff options
author | Alexander Smirnov <alex@ydb.tech> | 2024-10-19 17:59:18 +0000 |
---|---|---|
committer | Alexander Smirnov <alex@ydb.tech> | 2024-10-19 17:59:18 +0000 |
commit | ceddbfe68f6ec7949a4062716c8f9840a59c6888 (patch) | |
tree | abfecadbb9c1e5aea40701dd20d902cb7bccd962 /contrib/libs/c-ares/src/lib/ares_qcache.c | |
parent | 07f2e60d02d95eab14a86a4b9469db1af7795001 (diff) | |
parent | d920c750e476fa2dc80c45f990d9456b1afeadd1 (diff) | |
download | ydb-ceddbfe68f6ec7949a4062716c8f9840a59c6888.tar.gz |
Merge branch 'rightlib' into mergelibs-241019-1758
Diffstat (limited to 'contrib/libs/c-ares/src/lib/ares_qcache.c')
-rw-r--r-- | contrib/libs/c-ares/src/lib/ares_qcache.c | 139 |
1 files changed, 69 insertions, 70 deletions
diff --git a/contrib/libs/c-ares/src/lib/ares_qcache.c b/contrib/libs/c-ares/src/lib/ares_qcache.c index 9725212fde..97c0a9137d 100644 --- a/contrib/libs/c-ares/src/lib/ares_qcache.c +++ b/contrib/libs/c-ares/src/lib/ares_qcache.c @@ -25,10 +25,10 @@ */ #include "ares_private.h" -struct ares__qcache { - ares__htable_strvp_t *cache; - ares__slist_t *expire; - unsigned int max_ttl; +struct ares_qcache { + ares_htable_strvp_t *cache; + ares_slist_t *expire; + unsigned int max_ttl; }; typedef struct { @@ -36,11 +36,11 @@ typedef struct { ares_dns_record_t *dnsrec; time_t expire_ts; time_t insert_ts; -} ares__qcache_entry_t; +} ares_qcache_entry_t; -static char *ares__qcache_calc_key(const ares_dns_record_t *dnsrec) +static char *ares_qcache_calc_key(const ares_dns_record_t *dnsrec) { - ares__buf_t *buf = ares__buf_create(); + ares_buf_t *buf = ares_buf_create(); size_t i; ares_status_t status; ares_dns_flags_t flags; @@ -51,13 +51,13 @@ static char *ares__qcache_calc_key(const ares_dns_record_t *dnsrec) /* Format is OPCODE|FLAGS[|QTYPE1|QCLASS1|QNAME1]... */ - status = ares__buf_append_str( + status = ares_buf_append_str( buf, ares_dns_opcode_tostr(ares_dns_record_get_opcode(dnsrec))); if (status != ARES_SUCCESS) { goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ } - status = ares__buf_append_byte(buf, '|'); + status = ares_buf_append_byte(buf, '|'); if (status != ARES_SUCCESS) { goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ } @@ -65,13 +65,13 @@ static char *ares__qcache_calc_key(const ares_dns_record_t *dnsrec) flags = ares_dns_record_get_flags(dnsrec); /* Only care about RD and CD */ if (flags & ARES_FLAG_RD) { - status = ares__buf_append_str(buf, "rd"); + status = ares_buf_append_str(buf, "rd"); if (status != ARES_SUCCESS) { goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ } } if (flags & ARES_FLAG_CD) { - status = ares__buf_append_str(buf, "cd"); + status = ares_buf_append_str(buf, "cd"); if (status != ARES_SUCCESS) { goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ } @@ -88,27 +88,27 @@ static char *ares__qcache_calc_key(const ares_dns_record_t *dnsrec) goto fail; /* LCOV_EXCL_LINE: DefensiveCoding */ } - status = ares__buf_append_byte(buf, '|'); + status = ares_buf_append_byte(buf, '|'); if (status != ARES_SUCCESS) { goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ } - status = ares__buf_append_str(buf, ares_dns_rec_type_tostr(qtype)); + status = ares_buf_append_str(buf, ares_dns_rec_type_tostr(qtype)); if (status != ARES_SUCCESS) { goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ } - status = ares__buf_append_byte(buf, '|'); + status = ares_buf_append_byte(buf, '|'); if (status != ARES_SUCCESS) { goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ } - status = ares__buf_append_str(buf, ares_dns_class_tostr(qclass)); + status = ares_buf_append_str(buf, ares_dns_class_tostr(qclass)); if (status != ARES_SUCCESS) { goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ } - status = ares__buf_append_byte(buf, '|'); + status = ares_buf_append_byte(buf, '|'); if (status != ARES_SUCCESS) { goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ } @@ -122,64 +122,63 @@ static char *ares__qcache_calc_key(const ares_dns_record_t *dnsrec) } if (name_len > 0) { - status = ares__buf_append(buf, (const unsigned char *)name, name_len); + status = ares_buf_append(buf, (const unsigned char *)name, name_len); if (status != ARES_SUCCESS) { goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ } } } - return ares__buf_finish_str(buf, NULL); + return ares_buf_finish_str(buf, NULL); /* LCOV_EXCL_START: OutOfMemory */ fail: - ares__buf_destroy(buf); + ares_buf_destroy(buf); return NULL; /* LCOV_EXCL_STOP */ } -static void ares__qcache_expire(ares__qcache_t *cache, - const ares_timeval_t *now) +static void ares_qcache_expire(ares_qcache_t *cache, const ares_timeval_t *now) { - ares__slist_node_t *node; + ares_slist_node_t *node; if (cache == NULL) { return; } - while ((node = ares__slist_node_first(cache->expire)) != NULL) { - const ares__qcache_entry_t *entry = ares__slist_node_val(node); + while ((node = ares_slist_node_first(cache->expire)) != NULL) { + const ares_qcache_entry_t *entry = ares_slist_node_val(node); /* If now is NULL, we're flushing everything, so don't break */ if (now != NULL && entry->expire_ts > now->sec) { break; } - ares__htable_strvp_remove(cache->cache, entry->key); - ares__slist_node_destroy(node); + ares_htable_strvp_remove(cache->cache, entry->key); + ares_slist_node_destroy(node); } } -void ares__qcache_flush(ares__qcache_t *cache) +void ares_qcache_flush(ares_qcache_t *cache) { - ares__qcache_expire(cache, NULL /* flush all */); + ares_qcache_expire(cache, NULL /* flush all */); } -void ares__qcache_destroy(ares__qcache_t *cache) +void ares_qcache_destroy(ares_qcache_t *cache) { if (cache == NULL) { return; } - ares__htable_strvp_destroy(cache->cache); - ares__slist_destroy(cache->expire); + ares_htable_strvp_destroy(cache->cache); + ares_slist_destroy(cache->expire); ares_free(cache); } -static int ares__qcache_entry_sort_cb(const void *arg1, const void *arg2) +static int ares_qcache_entry_sort_cb(const void *arg1, const void *arg2) { - const ares__qcache_entry_t *entry1 = arg1; - const ares__qcache_entry_t *entry2 = arg2; + const ares_qcache_entry_t *entry1 = arg1; + const ares_qcache_entry_t *entry2 = arg2; if (entry1->expire_ts > entry2->expire_ts) { return 1; @@ -192,9 +191,9 @@ static int ares__qcache_entry_sort_cb(const void *arg1, const void *arg2) return 0; } -static void ares__qcache_entry_destroy_cb(void *arg) +static void ares_qcache_entry_destroy_cb(void *arg) { - ares__qcache_entry_t *entry = arg; + ares_qcache_entry_t *entry = arg; if (entry == NULL) { return; /* LCOV_EXCL_LINE: DefensiveCoding */ } @@ -204,12 +203,12 @@ static void ares__qcache_entry_destroy_cb(void *arg) ares_free(entry); } -ares_status_t ares__qcache_create(ares_rand_state *rand_state, - unsigned int max_ttl, - ares__qcache_t **cache_out) +ares_status_t ares_qcache_create(ares_rand_state *rand_state, + unsigned int max_ttl, + ares_qcache_t **cache_out) { - ares_status_t status = ARES_SUCCESS; - ares__qcache_t *cache; + ares_status_t status = ARES_SUCCESS; + ares_qcache_t *cache; cache = ares_malloc_zero(sizeof(*cache)); if (cache == NULL) { @@ -217,14 +216,14 @@ ares_status_t ares__qcache_create(ares_rand_state *rand_state, goto done; /* LCOV_EXCL_LINE: OutOfMemory */ } - cache->cache = ares__htable_strvp_create(NULL); + cache->cache = ares_htable_strvp_create(NULL); if (cache->cache == NULL) { status = ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */ goto done; /* LCOV_EXCL_LINE: OutOfMemory */ } - cache->expire = ares__slist_create(rand_state, ares__qcache_entry_sort_cb, - ares__qcache_entry_destroy_cb); + cache->expire = ares_slist_create(rand_state, ares_qcache_entry_sort_cb, + ares_qcache_entry_destroy_cb); if (cache->expire == NULL) { status = ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */ goto done; /* LCOV_EXCL_LINE: OutOfMemory */ @@ -235,7 +234,7 @@ ares_status_t ares__qcache_create(ares_rand_state *rand_state, done: if (status != ARES_SUCCESS) { *cache_out = NULL; - ares__qcache_destroy(cache); + ares_qcache_destroy(cache); return status; } @@ -243,7 +242,7 @@ done: return status; } -static unsigned int ares__qcache_calc_minttl(ares_dns_record_t *dnsrec) +static unsigned int ares_qcache_calc_minttl(ares_dns_record_t *dnsrec) { unsigned int minttl = 0xFFFFFFFF; size_t sect; @@ -272,7 +271,7 @@ static unsigned int ares__qcache_calc_minttl(ares_dns_record_t *dnsrec) return minttl; } -static unsigned int ares__qcache_soa_minimum(ares_dns_record_t *dnsrec) +static unsigned int ares_qcache_soa_minimum(ares_dns_record_t *dnsrec) { size_t i; @@ -302,15 +301,15 @@ static unsigned int ares__qcache_soa_minimum(ares_dns_record_t *dnsrec) } /* On success, takes ownership of dnsrec */ -static ares_status_t ares__qcache_insert(ares__qcache_t *qcache, - ares_dns_record_t *qresp, - const ares_dns_record_t *qreq, - const ares_timeval_t *now) +static ares_status_t ares_qcache_insert_int(ares_qcache_t *qcache, + ares_dns_record_t *qresp, + const ares_dns_record_t *qreq, + const ares_timeval_t *now) { - ares__qcache_entry_t *entry; - unsigned int ttl; - ares_dns_rcode_t rcode = ares_dns_record_get_rcode(qresp); - ares_dns_flags_t flags = ares_dns_record_get_flags(qresp); + ares_qcache_entry_t *entry; + unsigned int ttl; + ares_dns_rcode_t rcode = ares_dns_record_get_rcode(qresp); + ares_dns_flags_t flags = ares_dns_record_get_flags(qresp); if (qcache == NULL || qresp == NULL) { return ARES_EFORMERR; @@ -328,9 +327,9 @@ static ares_status_t ares__qcache_insert(ares__qcache_t *qcache, /* Look at SOA for NXDOMAIN for minimum */ if (rcode == ARES_RCODE_NXDOMAIN) { - ttl = ares__qcache_soa_minimum(qresp); + ttl = ares_qcache_soa_minimum(qresp); } else { - ttl = ares__qcache_calc_minttl(qresp); + ttl = ares_qcache_calc_minttl(qresp); } if (ttl > qcache->max_ttl) { @@ -355,16 +354,16 @@ static ares_status_t ares__qcache_insert(ares__qcache_t *qcache, * request had, so we have to re-parse the request in order to generate the * key for caching, but we'll only do this once we know for sure we really * want to cache it */ - entry->key = ares__qcache_calc_key(qreq); + entry->key = ares_qcache_calc_key(qreq); if (entry->key == NULL) { goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ } - if (!ares__htable_strvp_insert(qcache->cache, entry->key, entry)) { + if (!ares_htable_strvp_insert(qcache->cache, entry->key, entry)) { goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ } - if (ares__slist_insert(qcache->expire, entry) == NULL) { + if (ares_slist_insert(qcache->expire, entry) == NULL) { goto fail; /* LCOV_EXCL_LINE: OutOfMemory */ } @@ -373,7 +372,7 @@ static ares_status_t ares__qcache_insert(ares__qcache_t *qcache, /* LCOV_EXCL_START: OutOfMemory */ fail: if (entry != NULL && entry->key != NULL) { - ares__htable_strvp_remove(qcache->cache, entry->key); + ares_htable_strvp_remove(qcache->cache, entry->key); ares_free(entry->key); ares_free(entry); } @@ -386,9 +385,9 @@ ares_status_t ares_qcache_fetch(ares_channel_t *channel, const ares_dns_record_t *dnsrec, const ares_dns_record_t **dnsrec_resp) { - char *key = NULL; - ares__qcache_entry_t *entry; - ares_status_t status = ARES_SUCCESS; + char *key = NULL; + ares_qcache_entry_t *entry; + ares_status_t status = ARES_SUCCESS; if (channel == NULL || dnsrec == NULL || dnsrec_resp == NULL) { return ARES_EFORMERR; @@ -398,22 +397,22 @@ ares_status_t ares_qcache_fetch(ares_channel_t *channel, return ARES_ENOTFOUND; } - ares__qcache_expire(channel->qcache, now); + ares_qcache_expire(channel->qcache, now); - key = ares__qcache_calc_key(dnsrec); + key = ares_qcache_calc_key(dnsrec); if (key == NULL) { status = ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */ goto done; /* LCOV_EXCL_LINE: OutOfMemory */ } - entry = ares__htable_strvp_get_direct(channel->qcache->cache, key); + entry = ares_htable_strvp_get_direct(channel->qcache->cache, key); if (entry == NULL) { status = ARES_ENOTFOUND; goto done; } - ares_dns_record_write_ttl_decrement( - entry->dnsrec, (unsigned int)(now->sec - entry->insert_ts)); + ares_dns_record_ttl_decrement(entry->dnsrec, + (unsigned int)(now->sec - entry->insert_ts)); *dnsrec_resp = entry->dnsrec; @@ -427,5 +426,5 @@ ares_status_t ares_qcache_insert(ares_channel_t *channel, const ares_query_t *query, ares_dns_record_t *dnsrec) { - return ares__qcache_insert(channel->qcache, dnsrec, query->query, now); + return ares_qcache_insert_int(channel->qcache, dnsrec, query->query, now); } |