diff --git a/include/proto/dns.h b/include/proto/dns.h index c7cd3564b..8c3ef8c8a 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 cb0a9a95d..dcbf14394 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 17f84f3c8..d26c11876 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; }