From 81ed1a0516820c31272059eabdcc41565d2c714e Mon Sep 17 00:00:00 2001 From: Baptiste Assmann Date: Wed, 3 May 2017 10:11:44 +0200 Subject: [PATCH] MINOR: dns: functions to manage memory for a DNS resolution structure A couple of new functions to allocate and free memory for a DNS resolution structure. Main purpose is to to make the code related to DNS more consistent. They allocate or free memory for the structure itself. Later, if needed, they should also allocate / free the buffers, etc, used by this structure. They don't set/unset any parameters, this is the role of the caller. This patch also implement calls to these function eveywhere it is required. --- include/proto/dns.h | 2 ++ src/dns.c | 27 +++++++++++++++++++++++++++ src/server.c | 7 ++++--- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/include/proto/dns.h b/include/proto/dns.h index c7cd3564b2..8c3ef8c8ab 100644 --- a/include/proto/dns.h +++ b/include/proto/dns.h @@ -45,5 +45,7 @@ void dns_update_resolvers_timeout(struct dns_resolvers *resolvers); void dns_reset_resolution(struct dns_resolution *resolution); int dns_check_resolution_queue(struct dns_resolvers *resolvers); unsigned short dns_response_get_query_id(unsigned char *resp); +struct dns_resolution *dns_alloc_resolution(void); +void dns_free_resolution(struct dns_resolution *resolution); #endif // _PROTO_DNS_H diff --git a/src/dns.c b/src/dns.c index cb0a9a95d5..dcbf143940 100644 --- a/src/dns.c +++ b/src/dns.c @@ -1340,6 +1340,33 @@ static int cli_parse_stat_resolvers(char **args, struct appctx *appctx, void *pr return 0; } +/* This function allocates memory for a DNS resolution structure. + * It's up to the caller to set the parameters + * Returns a pointer to the structure resolution or NULL if memory could + * not be allocated. + */ +struct dns_resolution *dns_alloc_resolution(void) +{ + struct dns_resolution *resolution = NULL; + + resolution = calloc(1, sizeof(*resolution)); + + if (!resolution) { + free(resolution); + return NULL; + } + + return resolution; +} + +/* This function free the memory allocated to a DNS resolution */ +void dns_free_resolution(struct dns_resolution *resolution) +{ + free(resolution); + + return; +} + /* This function dumps counters from all resolvers section and associated name * servers. It returns 0 if the output buffer is full and it needs to be called * again, otherwise non-zero. It may limit itself to the resolver pointed to by diff --git a/src/server.c b/src/server.c index 17f84f3c82..d26c118764 100644 --- a/src/server.c +++ b/src/server.c @@ -1667,7 +1667,8 @@ static int srv_alloc_dns_resolution(struct server *srv, const char *hostname) free(srv->hostname); srv->hostname = strdup(hostname); - dst_dns_rslt = calloc(1, sizeof *dst_dns_rslt); + dst_dns_rslt = dns_alloc_resolution(); + hostname_dn_len = dns_str_to_dn_label_len(hostname); hostname_dn = calloc(hostname_dn_len + 1, sizeof(char)); @@ -1714,7 +1715,7 @@ static int srv_alloc_dns_resolution(struct server *srv, const char *hostname) free(srv->hostname); srv->hostname = NULL; free(hostname_dn); - free(dst_dns_rslt); + dns_free_resolution(dst_dns_rslt); return -1; } @@ -1724,7 +1725,7 @@ static void srv_free_dns_resolution(struct server *srv) return; free(srv->resolution->hostname_dn); - free(srv->resolution); + dns_free_resolution(srv->resolution); srv->resolution = NULL; }