Skip to content

Commit

Permalink
Fix cache leak issue
Browse files Browse the repository at this point in the history
  • Loading branch information
pymumu committed Dec 14, 2019
1 parent 7c51259 commit 63db665
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
29 changes: 16 additions & 13 deletions src/dns_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
struct dns_cache_head {
DECLARE_HASHTABLE(cache_hash, 10);
struct list_head cache_list;
int num;
atomic_t num;
int size;
pthread_mutex_t lock;
};
Expand All @@ -16,7 +16,7 @@ int dns_cache_init(int size)
{
INIT_LIST_HEAD(&dns_cache_head.cache_list);
hash_init(dns_cache_head.cache_hash);
dns_cache_head.num = 0;
atomic_set(&dns_cache_head.num, 0);
dns_cache_head.size = size;

pthread_mutex_init(&dns_cache_head.lock, NULL);
Expand All @@ -38,7 +38,7 @@ static void _dns_cache_delete(struct dns_cache *dns_cache)
{
hash_del(&dns_cache->node);
list_del_init(&dns_cache->list);
dns_cache_head.num--;
atomic_dec(&dns_cache_head.num);
free(dns_cache);
}

Expand All @@ -52,6 +52,9 @@ void dns_cache_get(struct dns_cache *dns_cache)

void dns_cache_release(struct dns_cache *dns_cache)
{
if (dns_cache == NULL) {
return;
}
if (!atomic_dec_and_test(&dns_cache->ref)) {
return;
}
Expand Down Expand Up @@ -92,30 +95,31 @@ int dns_cache_replace(char *domain, char *cname, int cname_ttl, int ttl, dns_typ
dns_cache->del_pending = 0;
dns_cache->speed = speed;
time(&dns_cache->insert_time);
pthread_mutex_unlock(&dns_cache_head.lock);
if (qtype == DNS_T_A) {
if (addr_len != DNS_RR_A_LEN) {
goto errout;
goto errout_unlock;
}
memcpy(dns_cache->addr, addr, DNS_RR_A_LEN);
} else if (qtype == DNS_T_AAAA) {
if (addr_len != DNS_RR_AAAA_LEN) {
goto errout;
goto errout_unlock;
}
memcpy(dns_cache->addr, addr, DNS_RR_AAAA_LEN);
} else {
goto errout;
goto errout_unlock;
}

if (cname) {
strncpy(dns_cache->cname, cname, DNS_MAX_CNAME_LEN);
dns_cache->cname_ttl = cname_ttl;
}
pthread_mutex_unlock(&dns_cache_head.lock);

dns_cache_release(dns_cache);
return 0;

errout:
errout_unlock:
pthread_mutex_unlock(&dns_cache_head.lock);
//errout:
if (dns_cache) {
dns_cache_release(dns_cache);
}
Expand Down Expand Up @@ -154,7 +158,7 @@ int dns_cache_insert(char *domain, char *cname, int cname_ttl, int ttl, dns_type
dns_cache->cname[0] = 0;
dns_cache->qtype = qtype;
dns_cache->ttl = ttl;
dns_cache->hitnum = 2;
atomic_set(&dns_cache->hitnum, 2);
dns_cache->del_pending = 0;
dns_cache->speed = speed;
atomic_set(&dns_cache->ref, 1);
Expand Down Expand Up @@ -183,9 +187,8 @@ int dns_cache_insert(char *domain, char *cname, int cname_ttl, int ttl, dns_type
list_add_tail(&dns_cache->list, &dns_cache_head.cache_list);
INIT_LIST_HEAD(&dns_cache->check_list);

dns_cache_head.num++;
/* Release extra cache, remove oldest cache record */
if (dns_cache_head.num > dns_cache_head.size) {
if (atomic_inc_return(&dns_cache_head.num) > dns_cache_head.size) {
struct dns_cache *del_cache;
del_cache = _dns_cache_first();
if (del_cache) {
Expand Down Expand Up @@ -276,7 +279,7 @@ void dns_cache_update(struct dns_cache *dns_cache)
if (!list_empty(&dns_cache->list)) {
list_del_init(&dns_cache->list);
list_add_tail(&dns_cache->list, &dns_cache_head.cache_list);
dns_cache->hitnum++;
atomic_inc(&dns_cache->hitnum);
}
pthread_mutex_unlock(&dns_cache_head.lock);
}
Expand Down
2 changes: 1 addition & 1 deletion src/dns_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct dns_cache {
unsigned int cname_ttl;
unsigned int ttl;;
int speed;
int hitnum;
atomic_t hitnum;
int del_pending;
time_t insert_time;
dns_type_t qtype;
Expand Down
2 changes: 1 addition & 1 deletion src/dns_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ static int _DNS_client_create_socket_tls(struct dns_server_info *server_info)

if (connect(fd, (struct sockaddr *)&server_info->addr, server_info->ai_addrlen) != 0) {
if (errno != EINPROGRESS) {
tlog(TLOG_ERROR, "connect failed.");
tlog(TLOG_ERROR, "connect %s failed, %s", server_info->ip, strerror(errno));
goto errout;
}
}
Expand Down
20 changes: 15 additions & 5 deletions src/dns_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,7 @@ static int _dns_server_process_address(struct dns_request *request, struct dns_p
static int _dns_server_process_cache(struct dns_request *request, struct dns_packet *packet)
{
struct dns_cache *dns_cache = NULL;
struct dns_cache *dns_cache_A = NULL;

dns_cache = dns_cache_lookup(request->domain, request->qtype);
if (dns_cache == NULL) {
Expand All @@ -1478,10 +1479,12 @@ static int _dns_server_process_cache(struct dns_request *request, struct dns_pac
}

if (dns_conf_dualstack_ip_selection && request->qtype == DNS_T_AAAA) {
struct dns_cache *dns_cache_A = dns_cache_lookup(request->domain, DNS_T_A);
dns_cache_A = dns_cache_lookup(request->domain, DNS_T_A);
if (dns_cache_A && (dns_cache_A->speed > 0)) {
if ((dns_cache_A->speed + (dns_conf_dualstack_ip_selection_threshold * 10)) < dns_cache->speed || dns_cache->speed < 0) {
tlog(TLOG_DEBUG, "Force IPV4 perfered.");
dns_cache_release(dns_cache_A);
dns_cache_release(dns_cache);
return _dns_server_reply_SOA(DNS_RC_NOERROR, request, NULL);
}
}
Expand Down Expand Up @@ -1515,11 +1518,20 @@ static int _dns_server_process_cache(struct dns_request *request, struct dns_pac
dns_cache_update(dns_cache);
dns_cache_release(dns_cache);

if (dns_cache_A) {
dns_cache_release(dns_cache_A);
dns_cache_A = NULL;
}

return 0;
errout:
if (dns_cache) {
dns_cache_release(dns_cache);
}
if (dns_cache_A) {
dns_cache_release(dns_cache_A);
dns_cache_A = NULL;
}
return -1;
}

Expand Down Expand Up @@ -2064,14 +2076,12 @@ static void _dns_server_tcp_ping_check(struct dns_request *request)
static void _dns_server_prefetch_domain(struct dns_cache *dns_cache)
{
/* If there are still hits, continue pre-fetching */
if (dns_cache->hitnum <= 0) {
if (atomic_dec_return(&dns_cache->hitnum) <= 0) {
return;
}

dns_cache->hitnum--;

/* start prefetch domain */
tlog(TLOG_DEBUG, "prefetch by cache %s, qtype %d, ttl %d, hitnum %d", dns_cache->domain, dns_cache->qtype, dns_cache->ttl, dns_cache->hitnum);
tlog(TLOG_DEBUG, "prefetch by cache %s, qtype %d, ttl %d, hitnum %d", dns_cache->domain, dns_cache->qtype, dns_cache->ttl, atomic_read(&dns_cache->hitnum));
if (_dns_server_prefetch_request(dns_cache->domain, dns_cache->qtype) != 0) {
tlog(TLOG_ERROR, "prefetch domain %s, qtype %d, failed.", dns_cache->domain, dns_cache->qtype);
}
Expand Down

0 comments on commit 63db665

Please sign in to comment.