MINOR: dns: give ability to dns_init_resolvers() to close a socket when requested
The function dns_init_resolvers() is used to initialize socket used to
send DNS queries.
This patch gives the function the ability to close a socket before
re-opening it.
[wt: this needs to be backported to 1.7 for next fix]
diff --git a/include/proto/dns.h b/include/proto/dns.h
index c62834f..c7cd356 100644
--- a/include/proto/dns.h
+++ b/include/proto/dns.h
@@ -30,7 +30,7 @@
int dns_hostname_validation(const char *string, char **err);
int dns_build_query(int query_id, int query_type, char *hostname_dn, int hostname_dn_len, char *buf, int bufsize);
struct task *dns_process_resolve(struct task *t);
-int dns_init_resolvers(void);
+int dns_init_resolvers(int close_socket);
uint16_t dns_rnd16(void);
int dns_validate_dns_response(unsigned char *resp, unsigned char *bufend, struct dns_response_packet *dns_p);
int dns_get_ip_from_response(struct dns_response_packet *dns_p,
diff --git a/src/dns.c b/src/dns.c
index 2a333f1..5542b17 100644
--- a/src/dns.c
+++ b/src/dns.c
@@ -919,11 +919,13 @@
* parses resolvers sections and initializes:
* - task (time events) for each resolvers section
* - the datagram layer (network IO events) for each nameserver
+ * It takes one argument:
+ * - close_first takes 2 values: 0 or 1. If 1, the connection is closed first.
* returns:
* 0 in case of error
* 1 when no error
*/
-int dns_init_resolvers(void)
+int dns_init_resolvers(int close_socket)
{
struct dns_resolvers *curr_resolvers;
struct dns_nameserver *curnameserver;
@@ -961,7 +963,19 @@
curr_resolvers->t = t;
list_for_each_entry(curnameserver, &curr_resolvers->nameserver_list, list) {
- if ((dgram = calloc(1, sizeof(*dgram))) == NULL) {
+ dgram = NULL;
+
+ if (close_socket == 1) {
+ if (curnameserver->dgram) {
+ close(curnameserver->dgram->t.sock.fd);
+ memset(curnameserver->dgram, '\0', sizeof(*dgram));
+ dgram = curnameserver->dgram;
+ }
+ }
+
+ /* allocate memory only if it has not already been allocated
+ * by a previous call to this function */
+ if (!dgram && (dgram = calloc(1, sizeof(*dgram))) == NULL) {
Alert("Starting [%s/%s] nameserver: out of memory.\n", curr_resolvers->id,
curnameserver->id);
return 0;
diff --git a/src/haproxy.c b/src/haproxy.c
index 4ad370d..6a8b5c2 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -1176,7 +1176,7 @@
exit(1);
/* initialize structures for name resolution */
- if (!dns_init_resolvers())
+ if (!dns_init_resolvers(0))
exit(1);
free(err_msg);