BUG/MEDIUM: resolvers: Don't release resolution from a requester callbacks

Another way to say it: "Safely unlink requester from a requester callbacks".

Requester callbacks must never try to unlink a requester from a resolution, for
the current requester or another one. First, these callback functions are called
in a loop on a request list, not necessarily safe. Thus unlink resolution at
this place, may be unsafe. And it is useless to try to make these loops safe
because, all this stuff is placed in a loop on a resolution list. Unlink a
requester may lead to release a resolution if it is the last requester.

However, the unkink is necessary because we cannot reset the server state
(hostname and IP) with some pending DNS resolution on it. So, to workaround
this issue, we introduce the "safe" unlink. It is only performed from a
requester callback. In this case, the unlink function never releases the
resolution, it only reset it if necessary. And when a resolution is found
with an empty requester list, it is released.

This patch depends on the following commits :

 * MINOR: resolvers: Purge answer items when a SRV resolution triggers an error
 * MINOR: resolvers: Use a function to remove answers attached to a resolution
 * MINOR: resolvers: Directly call srvrq_update_srv_state() when possible
 * MINOR: resolvers: Add function to change the srv status based on SRV resolution

All the series must be backported as far as 2.2. It fixes a regression
introduced by the commit b4badf720 ("BUG/MINOR: resolvers: new callback to
properly handle SRV record errors").

don't release resolution from requester cb

(cherry picked from commit 0efc0993ec5210c8cf5a98eb9c0220014e030fd6)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit 119ec5279df7241da25f2ffc1e43de4075717f35)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit f721fd209cb7f7126a548bf5301f4dd008952184)
[cf: Must be backported as far as 2.0 because of recent changes on resolvers]
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit a18ebf4621cae2acb1fb8d61fb631f905bbb3941)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
diff --git a/src/dns.c b/src/dns.c
index f040668..18224d5 100644
--- a/src/dns.c
+++ b/src/dns.c
@@ -519,7 +519,7 @@
 					if (srv->srvrq == srvrq && srv->svc_port == item->port &&
 					    item->data_len == srv->hostname_dn_len &&
 					    !dns_hostname_cmp(srv->hostname_dn, item->target, item->data_len)) {
-						dns_unlink_resolution(srv->dns_requester);
+						dns_unlink_resolution(srv->dns_requester, 0);
 						srvrq_update_srv_status(srv, 1);
 						free(srv->hostname);
 						free(srv->hostname_dn);
@@ -1489,8 +1489,9 @@
 
 /* Removes a requester from a DNS resoltion. It takes takes care of all the
  * consequences. It also cleans up some parameters from the requester.
+ * if <safe> is set to 1, the corresponding resolution is not released.
  */
-void dns_unlink_resolution(struct dns_requester *requester)
+void dns_unlink_resolution(struct dns_requester *requester, int safe)
 {
 	struct dns_resolution *res;
 	struct dns_requester  *req;
@@ -1508,6 +1509,15 @@
 	if (!LIST_ISEMPTY(&res->requesters))
 		req = LIST_NEXT(&res->requesters, struct dns_requester *, list);
 	else {
+		if (safe) {
+			/* Don't release it yet. */
+			dns_reset_resolution(res);
+			res->hostname_dn = NULL;
+			res->hostname_dn_len = 0;
+			dns_purge_resolution_answer_records(res);
+			return;
+		}
+
 		dns_free_resolution(res);
 		return;
 	}
@@ -1808,6 +1818,11 @@
 
 	/* Handle all expired resolutions from the active list */
 	list_for_each_entry_safe(res, resback, &resolvers->resolutions.curr, list) {
+		if (LIST_ISEMPTY(&res->requesters)) {
+			dns_free_resolution(res);
+			continue;
+		}
+
 		/* When we find the first resolution in the future, then we can
 		 * stop here */
 		exp = tick_add(res->last_query, resolvers->timeout.retry);
@@ -1856,6 +1871,11 @@
 
 	/* Handle all resolutions in the wait list */
 	list_for_each_entry_safe(res, resback, &resolvers->resolutions.wait, list) {
+		if (LIST_ISEMPTY(&res->requesters)) {
+			dns_free_resolution(res);
+			continue;
+		}
+
 		exp = tick_add(res->last_resolution, dns_resolution_timeout(res));
 		if (tick_isset(res->last_resolution) && !tick_is_expired(exp, now_ms))
 			continue;
@@ -2285,7 +2305,7 @@
 	s->dns_ctx.hostname_dn = NULL;
 	s->dns_ctx.hostname_dn_len = 0;
 	if (s->dns_ctx.dns_requester) {
-		dns_unlink_resolution(s->dns_ctx.dns_requester);
+		dns_unlink_resolution(s->dns_ctx.dns_requester, 0);
 		pool_free(dns_requester_pool, s->dns_ctx.dns_requester);
 		s->dns_ctx.dns_requester = NULL;
 	}