dns_cache: refactor dns-cache.c, remove unused code.

This commit is contained in:
Nick Peng
2023-10-31 22:22:31 +08:00
parent f39bdb64b2
commit aca8cf92cb
4 changed files with 44 additions and 291 deletions

View File

@@ -111,11 +111,6 @@ static void _dns_cache_remove(struct dns_cache *dns_cache)
dns_cache_release(dns_cache);
}
enum CACHE_TYPE dns_cache_data_type(struct dns_cache_data *cache_data)
{
return cache_data->head.cache_type;
}
uint32_t dns_cache_get_query_flag(struct dns_cache *dns_cache)
{
return dns_cache->info.query_flag;
@@ -126,83 +121,6 @@ const char *dns_cache_get_dns_group_name(struct dns_cache *dns_cache)
return dns_cache->info.dns_group_name;
}
struct dns_cache_data *dns_cache_new_data_addr(void)
{
struct dns_cache_addr *cache_addr = malloc(sizeof(struct dns_cache_addr));
memset(cache_addr, 0, sizeof(struct dns_cache_addr));
if (cache_addr == NULL) {
return NULL;
}
cache_addr->head.cache_type = CACHE_TYPE_NONE;
cache_addr->head.size = sizeof(struct dns_cache_addr) - sizeof(struct dns_cache_data_head);
cache_addr->head.magic = MAGIC_CACHE_DATA;
atomic_set(&cache_addr->head.ref, 1);
return (struct dns_cache_data *)cache_addr;
}
void dns_cache_set_data_soa(struct dns_cache_data *dns_cache, char *cname, int cname_ttl)
{
if (dns_cache == NULL) {
goto errout;
}
dns_cache->head.is_soa = 1;
if (dns_cache->head.cache_type == CACHE_TYPE_PACKET) {
return;
}
struct dns_cache_addr *cache_addr = (struct dns_cache_addr *)dns_cache;
if (cache_addr == NULL) {
goto errout;
}
memset(cache_addr->addr_data.addr, 0, sizeof(cache_addr->addr_data.addr));
if (cname) {
safe_strncpy(cache_addr->addr_data.cname, cname, DNS_MAX_CNAME_LEN);
cache_addr->addr_data.cname_ttl = cname_ttl;
}
cache_addr->addr_data.soa = 1;
cache_addr->head.cache_type = CACHE_TYPE_ADDR;
cache_addr->head.size = sizeof(struct dns_cache_addr) - sizeof(struct dns_cache_data_head);
errout:
return;
}
void dns_cache_set_data_addr(struct dns_cache_data *dns_cache, char *cname, int cname_ttl, unsigned char *addr,
int addr_len)
{
if (dns_cache == NULL) {
goto errout;
}
struct dns_cache_addr *cache_addr = (struct dns_cache_addr *)dns_cache;
if (cache_addr == NULL) {
goto errout;
}
if (addr_len == DNS_RR_A_LEN) {
memcpy(cache_addr->addr_data.addr, addr, DNS_RR_A_LEN);
} else if (addr_len != DNS_RR_AAAA_LEN) {
memcpy(cache_addr->addr_data.addr, addr, DNS_RR_AAAA_LEN);
} else {
goto errout;
}
if (cname) {
safe_strncpy(cache_addr->addr_data.cname, cname, DNS_MAX_CNAME_LEN);
cache_addr->addr_data.cname_ttl = cname_ttl;
}
cache_addr->head.cache_type = CACHE_TYPE_ADDR;
cache_addr->head.size = sizeof(struct dns_cache_addr) - sizeof(struct dns_cache_data_head);
errout:
return;
}
struct dns_cache_data *dns_cache_new_data_packet(void *packet, size_t packet_len)
{
struct dns_cache_packet *cache_packet = NULL;
@@ -220,7 +138,6 @@ struct dns_cache_data *dns_cache_new_data_packet(void *packet, size_t packet_len
memcpy(cache_packet->data, packet, packet_len);
memset(&cache_packet->head, 0, sizeof(cache_packet->head));
cache_packet->head.cache_type = CACHE_TYPE_PACKET;
cache_packet->head.size = packet_len;
cache_packet->head.magic = MAGIC_CACHE_DATA;
atomic_set(&cache_packet->head.ref, 1);
@@ -254,7 +171,7 @@ static void dns_cache_expired(struct tw_timer_list *timer, void *data, unsigned
dns_timer_mod(&dns_cache->timer, 5);
}
static int _dns_cache_replace(struct dns_cache_key *cache_key, int ttl, int speed, int timeout, int update_time,
static int _dns_cache_replace(struct dns_cache_key *cache_key, int rcode, int ttl, int speed, int timeout, int update_time,
struct dns_cache_data *cache_data)
{
struct dns_cache *dns_cache = NULL;
@@ -267,7 +184,7 @@ static int _dns_cache_replace(struct dns_cache_key *cache_key, int ttl, int spee
/* lookup existing cache */
dns_cache = dns_cache_lookup(cache_key);
if (dns_cache == NULL) {
return dns_cache_insert(cache_key, ttl, speed, timeout, cache_data);
return dns_cache_insert(cache_key, rcode, ttl, speed, timeout, cache_data);
}
if (ttl < DNS_CACHE_TTL_MIN) {
@@ -303,10 +220,10 @@ static int _dns_cache_replace(struct dns_cache_key *cache_key, int ttl, int spee
return 0;
}
int dns_cache_replace(struct dns_cache_key *cache_key, int ttl, int speed, int timeout, int update_time,
int dns_cache_replace(struct dns_cache_key *cache_key, int rcode, int ttl, int speed, int timeout, int update_time,
struct dns_cache_data *cache_data)
{
return _dns_cache_replace(cache_key, ttl, speed, timeout, update_time, cache_data);
return _dns_cache_replace(cache_key, rcode, ttl, speed, timeout, update_time, cache_data);
}
static void _dns_cache_remove_by_domain(struct dns_cache_key *cache_key)
@@ -403,7 +320,7 @@ errout:
return -1;
}
int dns_cache_insert(struct dns_cache_key *cache_key, int ttl, int speed, int timeout,
int dns_cache_insert(struct dns_cache_key *cache_key, int rcode, int ttl, int speed, int timeout,
struct dns_cache_data *cache_data)
{
struct dns_cache_info info;
@@ -432,6 +349,7 @@ int dns_cache_insert(struct dns_cache_key *cache_key, int ttl, int speed, int ti
info.speed = speed;
info.timeout = timeout;
info.is_visited = 1;
info.rcode = rcode;
time(&info.insert_time);
time(&info.replace_time);
@@ -502,60 +420,6 @@ int dns_cache_get_ttl(struct dns_cache *dns_cache)
return ttl;
}
int dns_cache_get_cname_ttl(struct dns_cache *dns_cache)
{
time_t now = 0;
int ttl = 0;
time(&now);
struct dns_cache_addr *cache_addr = (struct dns_cache_addr *)dns_cache_get_data(dns_cache);
if (cache_addr == NULL) {
ttl = 0;
goto out;
}
if (cache_addr->head.cache_type != CACHE_TYPE_ADDR) {
ttl = 0;
goto out;
}
ttl = dns_cache->info.insert_time + cache_addr->addr_data.cname_ttl - now;
if (ttl < 0) {
ttl = 0;
goto out;
}
int addr_ttl = dns_cache_get_ttl(dns_cache);
if (ttl < addr_ttl && ttl < 0) {
return addr_ttl;
}
if (ttl < 0) {
ttl = 0;
goto out;
}
out:
if (cache_addr) {
dns_cache_data_put((struct dns_cache_data *)cache_addr);
}
return ttl;
}
int dns_cache_is_soa(struct dns_cache *dns_cache)
{
if (dns_cache == NULL) {
return 0;
}
if (dns_cache->cache_data->head.is_soa) {
return 1;
}
return 0;
}
struct dns_cache_data *dns_cache_get_data(struct dns_cache *dns_cache)
{
struct dns_cache_data *cache_data;
@@ -928,6 +792,6 @@ void dns_cache_destroy(void)
const char *dns_cache_file_version(void)
{
const char *version = "cache ver 1.2";
const char *version = "cache ver 1.3";
return version;
}