From 15271d0438b80c96a57b43dd33b377920a41cd95 Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Thu, 21 Apr 2016 15:16:38 +0200 Subject: [PATCH] Account for callbacks fired during scheduling --- src/dnssec.c | 2 +- src/general.c | 64 ++++++++++++++++++++++++++++++++---------- src/general.h | 4 ++- src/request-internal.c | 4 ++- src/types-internal.h | 6 ++++ 5 files changed, 62 insertions(+), 18 deletions(-) diff --git a/src/dnssec.c b/src/dnssec.c index 9bb95471..3e26f7b7 100644 --- a/src/dnssec.c +++ b/src/dnssec.c @@ -3181,7 +3181,7 @@ static void check_chain_complete(chain_head *chain) && !dnsreq->avoid_dnssec_roadblocks && dnsreq->netreqs[0]->dnssec_status == GETDNS_DNSSEC_BOGUS) { - getdns_return_t r = GETDNS_RETURN_GOOD; + int r = GETDNS_RETURN_GOOD; getdns_network_req **netreq_p, *netreq; dnsreq->avoid_dnssec_roadblocks = 1; diff --git a/src/general.c b/src/general.c index 4de98853..687fca2c 100644 --- a/src/general.c +++ b/src/general.c @@ -161,7 +161,7 @@ void _getdns_check_dns_req_complete(getdns_dns_req *dns_req) { getdns_network_req **netreq_p, *netreq; - int results_found = 0; + int results_found = 0, r; for (netreq_p = dns_req->netreqs; (netreq = *netreq_p); netreq_p++) if (netreq->state != NET_REQ_FINISHED && @@ -198,8 +198,11 @@ _getdns_check_dns_req_complete(getdns_dns_req *dns_req) ; (netreq = *netreq_p) ; netreq_p++ ) { _getdns_netreq_reinit(netreq); - if (_getdns_submit_netreq(netreq)) + if ((r = _getdns_submit_netreq(netreq))) { + if (r == DNS_REQ_FINISHED) + return; netreq->state = NET_REQ_FINISHED; + } } _getdns_check_dns_req_complete(dns_req); return; @@ -233,8 +236,11 @@ _getdns_check_dns_req_complete(getdns_dns_req *dns_req) ; (netreq = *netreq_p) ; netreq_p++ ) { _getdns_netreq_reinit(netreq); - if (_getdns_submit_netreq(netreq)) + if ((r = _getdns_submit_netreq(netreq))) { + if (r == DNS_REQ_FINISHED) + return; netreq->state = NET_REQ_FINISHED; + } } _getdns_check_dns_req_complete(dns_req); return; @@ -312,13 +318,16 @@ ub_resolve_callback(void* arg, int err, struct ub_result* ub_res) #endif -getdns_return_t +int _getdns_submit_netreq(getdns_network_req *netreq) { getdns_return_t r; getdns_dns_req *dns_req = netreq->owner; char name[1024]; - + int dnsreq_freed = 0; +#ifdef HAVE_LIBUNBOUND + int ub_resolve_r; +#endif #ifdef STUB_NATIVE_DNSSEC # ifdef DNSSEC_ROADBLOCK_AVOIDANCE @@ -351,24 +360,34 @@ _getdns_submit_netreq(getdns_network_req *netreq) dns_req->name_len, name, sizeof(name)); #ifdef HAVE_LIBUNBOUND + dns_req->freed = &dnsreq_freed; #ifdef HAVE_UNBOUND_EVENT_API if (_getdns_ub_loop_enabled(&dns_req->context->ub_loop)) - return ub_resolve_event(dns_req->context->unbound_ctx, + ub_resolve_r = ub_resolve_event(dns_req->context->unbound_ctx, name, netreq->request_type, netreq->owner->request_class, netreq, ub_resolve_event_callback, &(netreq->unbound_id)) ? GETDNS_RETURN_GENERIC_ERROR : GETDNS_RETURN_GOOD; else #endif - return ub_resolve_async(dns_req->context->unbound_ctx, + ub_resolve_r = ub_resolve_async(dns_req->context->unbound_ctx, name, netreq->request_type, netreq->owner->request_class, netreq, ub_resolve_callback, &(netreq->unbound_id)) ? GETDNS_RETURN_GENERIC_ERROR : GETDNS_RETURN_GOOD; + if (dnsreq_freed) + return DNS_REQ_FINISHED; + dns_req->freed = NULL; + return ub_resolve_r ? GETDNS_RETURN_GENERIC_ERROR : GETDNS_RETURN_GOOD; #else return GETDNS_RETURN_NOT_IMPLEMENTED; #endif } /* Submit with stub resolver */ - return _getdns_submit_stub_request(netreq); + dns_req->freed = &dnsreq_freed; + r = _getdns_submit_stub_request(netreq); + if (dnsreq_freed) + return DNS_REQ_FINISHED; + dns_req->freed = NULL; + return r; } @@ -456,7 +475,7 @@ getdns_general_ns(getdns_context *context, getdns_eventloop *loop, void *userarg, getdns_network_req **return_netreq_p, getdns_callback_t callbackfn, internal_cb_t internal_cb, int usenamespaces) { - getdns_return_t r = GETDNS_RETURN_GOOD; + int r = GETDNS_RETURN_GOOD; getdns_network_req *netreq, **netreq_p; getdns_dns_req *req; getdns_dict *localnames_response; @@ -495,8 +514,16 @@ getdns_general_ns(getdns_context *context, getdns_eventloop *loop, /* issue all network requests */ for ( netreq_p = req->netreqs ; !r && (netreq = *netreq_p) - ; netreq_p++) - r = _getdns_submit_netreq(netreq); + ; netreq_p++) { + if ((r = _getdns_submit_netreq(netreq))) { + if (r == DNS_REQ_FINISHED) { + if (return_netreq_p) + *return_netreq_p = NULL; + return GETDNS_RETURN_GOOD; + } + netreq->state = NET_REQ_FINISHED; + } + } else for (i = 0; i < context->namespace_count; i++) { if (context->namespaces[i] == GETDNS_NAMESPACE_LOCALNAMES) { @@ -517,14 +544,21 @@ getdns_general_ns(getdns_context *context, getdns_eventloop *loop, r = GETDNS_RETURN_GOOD; for ( netreq_p = req->netreqs ; !r && (netreq = *netreq_p) - ; netreq_p++) - r = _getdns_submit_netreq(netreq); + ; netreq_p++) { + if ((r = _getdns_submit_netreq(netreq))) { + if (r == DNS_REQ_FINISHED) { + if (return_netreq_p) + *return_netreq_p = NULL; + return GETDNS_RETURN_GOOD; + } + netreq->state = NET_REQ_FINISHED; + } + } break; } else r = GETDNS_RETURN_BAD_CONTEXT; } - - if (r != 0) { + if (r > 0) { /* i.e. r != GETDNS_RETURN_GOOD && r != DNS_REQ_FINISHED */ /* clean up the request */ _getdns_context_clear_outbound_request(req); _getdns_dns_req_free(req); diff --git a/src/general.h b/src/general.h index e9a4529e..29b52f47 100644 --- a/src/general.h +++ b/src/general.h @@ -42,9 +42,11 @@ /* private inner helper used by sync and async */ +#define DNS_REQ_FINISHED -1 + void _getdns_call_user_callback(getdns_dns_req *, getdns_dict *); void _getdns_check_dns_req_complete(getdns_dns_req *dns_req); -getdns_return_t _getdns_submit_netreq(getdns_network_req *netreq); +int _getdns_submit_netreq(getdns_network_req *netreq); getdns_return_t diff --git a/src/request-internal.c b/src/request-internal.c index f619429c..b209a971 100644 --- a/src/request-internal.c +++ b/src/request-internal.c @@ -640,7 +640,8 @@ _getdns_dns_req_free(getdns_dns_req * req) req->loop->vmt->clear(req->loop, &req->timeout); req->timeout.timeout_cb = NULL; } - + if (req->freed) + *req->freed = 1; GETDNS_FREE(req->my_mf, req); } @@ -901,6 +902,7 @@ _getdns_dns_req_new(getdns_context *context, getdns_eventloop *loop, result->upstreams->referenced++; result->finished_next = NULL; + result->freed = NULL; network_req_init(result->netreqs[0], result, request_type, dnssec_extension_set, with_opt, diff --git a/src/types-internal.h b/src/types-internal.h index 9e9a4d34..b70b59f6 100644 --- a/src/types-internal.h +++ b/src/types-internal.h @@ -304,6 +304,12 @@ typedef struct getdns_dns_req { int dnssec_ok_checking_disabled; int is_sync_request; + /* Integer pointed to by pointer will be set to 1 (if set), + * before the request is freed. + * To be used by _getdns_submit_netreq only! + */ + int *freed; + /* internally scheduled request */ internal_cb_t internal_cb;