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.
This commit is contained in:
Baptiste Assmann 2017-05-03 10:11:44 +02:00 committed by Willy Tarreau
parent d0aa6d2399
commit 81ed1a0516
3 changed files with 33 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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;
}