From d50860c0899ad0faa942380c23b3352cae071aef Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Wed, 20 Jan 2016 11:10:53 +0100 Subject: [PATCH] Run context's event loop when doing sync requests --- src/sync.c | 176 ++++++++++++++--------------------------------------- 1 file changed, 46 insertions(+), 130 deletions(-) diff --git a/src/sync.c b/src/sync.c index dffff203..17fd8d28 100644 --- a/src/sync.c +++ b/src/sync.c @@ -33,197 +33,113 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include -#include "getdns/getdns.h" #include "config.h" -#include "context.h" -#include "general.h" -#include "types-internal.h" -#include "util-internal.h" -#include "dnssec.h" +#include "getdns/getdns.h" +#include "getdns/getdns_extra.h" -#include "stub.h" -#include "gldns/wire2str.h" -typedef struct getdns_sync_loop { - _getdns_default_eventloop loop; -#ifdef HAVE_LIBUNBOUND - getdns_eventloop_event ub_event; -#endif - getdns_context *context; - int to_run; - getdns_dict *response; -} getdns_sync_loop; - -static getdns_return_t -getdns_sync_loop_init(getdns_context *context, getdns_sync_loop *loop) -{ -#ifdef HAVE_LIBUNBOUND - getdns_eventloop *ext = &loop->loop.loop; -#endif - - loop->response = NULL; - loop->to_run = 1; - loop->context = context; - - _getdns_default_eventloop_init(&loop->loop); - -#if defined(HAVE_LIBUNBOUND) && !defined(USE_WINSOCK) - loop->ub_event.userarg = loop->context; - loop->ub_event.read_cb = _getdns_context_ub_read_cb; - loop->ub_event.write_cb = NULL; - loop->ub_event.timeout_cb = NULL; - loop->ub_event.ev = NULL; - -# ifdef HAVE_UNBOUND_EVENT_API - if (context->unbound_event_api) { - (void) ub_ctx_set_event(context->unbound_ctx, (void *)ext); - } else -# endif - return ext->vmt->schedule(ext, ub_fd(context->unbound_ctx), - TIMEOUT_FOREVER, &loop->ub_event); -#endif - return GETDNS_RETURN_GOOD; -} - -static void -getdns_sync_loop_cleanup(getdns_sync_loop *loop) -{ - getdns_eventloop *ext = &loop->loop.loop; - -#if defined(HAVE_LIBUNBOUND) && !defined(USE_WINSOCK) -# ifdef HAVE_UNBOUND_EVENT_API - if (loop->context->unbound_event_api) { - (void) ub_ctx_set_event(loop->context->unbound_ctx, - (void *)loop->context->extension); - } else -# endif - ext->vmt->clear(ext, &loop->ub_event); -#endif - ext->vmt->cleanup(ext); -} - -static void -getdns_sync_loop_run(getdns_sync_loop *loop) -{ - getdns_eventloop *ext = &loop->loop.loop; - - while (loop->to_run) - ext->vmt->run_once(ext, 1); - - getdns_sync_loop_cleanup(loop); -} +typedef struct getdns_sync_data { + int to_run; + getdns_callback_type_t callback_type; + getdns_dict *response; + getdns_transaction_t transaction_id; +} getdns_sync_data; static void getdns_sync_cb(getdns_context *context, getdns_callback_type_t callback_type, getdns_dict *response, void *userarg, getdns_transaction_t transaction_id) { - getdns_sync_loop *loop = (getdns_sync_loop *)userarg; + getdns_sync_data *data = (getdns_sync_data *)userarg; - assert(loop); + assert(data); + assert(data->transaction_id == transaction_id); - loop->response = response; - loop->to_run = 0; + data->callback_type = callback_type; + data->response = response; + data->to_run = 0; +} + +static getdns_return_t +_getdns_sync_run( + getdns_context *context, getdns_sync_data *data, getdns_dict **response) +{ + data->to_run = 1; + data->callback_type = (getdns_callback_type_t)0; + data->response = NULL; + + while (data->to_run) + getdns_context_process_async(context); + + return (*response = data->response) ? + GETDNS_RETURN_GOOD : GETDNS_RETURN_GENERIC_ERROR; } getdns_return_t getdns_general_sync(getdns_context *context, const char *name, uint16_t request_type, getdns_dict *extensions, getdns_dict **response) { - getdns_sync_loop loop; + getdns_sync_data data; getdns_return_t r; if (!context || !name || !response) return GETDNS_RETURN_INVALID_PARAMETER; - if ((r = getdns_sync_loop_init(context, &loop))) + if ((r = getdns_general(context, name, request_type, + extensions, &data, &data.transaction_id, getdns_sync_cb))) return r; - if ((r = _getdns_general_loop(context, &loop.loop.loop, name, - request_type, extensions, &loop, NULL, getdns_sync_cb, NULL))) { - - getdns_sync_loop_cleanup(&loop); - return r; - } - getdns_sync_loop_run(&loop); - - return (*response = loop.response) ? - GETDNS_RETURN_GOOD : GETDNS_RETURN_GENERIC_ERROR; + return _getdns_sync_run(context, &data, response); } getdns_return_t getdns_address_sync(getdns_context *context, const char *name, getdns_dict *extensions, getdns_dict **response) { - getdns_sync_loop loop; + getdns_sync_data data; getdns_return_t r; if (!context || !name || !response) return GETDNS_RETURN_INVALID_PARAMETER; - if ((r = getdns_sync_loop_init(context, &loop))) + if ((r = getdns_address(context, name, + extensions, &data, &data.transaction_id, getdns_sync_cb))) return r; - if ((r = _getdns_address_loop(context, &loop.loop.loop, name, - extensions, &loop, NULL, getdns_sync_cb))) { - - getdns_sync_loop_cleanup(&loop); - return r; - } - getdns_sync_loop_run(&loop); - - return (*response = loop.response) ? - GETDNS_RETURN_GOOD : GETDNS_RETURN_GENERIC_ERROR; + return _getdns_sync_run(context, &data, response); } getdns_return_t getdns_hostname_sync(getdns_context *context, getdns_dict *address, getdns_dict *extensions, getdns_dict **response) { - getdns_sync_loop loop; + getdns_sync_data data; getdns_return_t r; - if (!context || !address || !response) + if (!context || !address|| !response) return GETDNS_RETURN_INVALID_PARAMETER; - if ((r = getdns_sync_loop_init(context, &loop))) + if ((r = getdns_hostname(context, address, + extensions, &data, &data.transaction_id, getdns_sync_cb))) return r; - if ((r = _getdns_hostname_loop(context, &loop.loop.loop, address, - extensions, &loop, NULL, getdns_sync_cb))) { - - getdns_sync_loop_cleanup(&loop); - return r; - } - getdns_sync_loop_run(&loop); - - return (*response = loop.response) ? - GETDNS_RETURN_GOOD : GETDNS_RETURN_GENERIC_ERROR; + return _getdns_sync_run(context, &data, response); } getdns_return_t getdns_service_sync(getdns_context *context, const char *name, getdns_dict *extensions, getdns_dict **response) { - getdns_sync_loop loop; + getdns_sync_data data; getdns_return_t r; if (!context || !name || !response) return GETDNS_RETURN_INVALID_PARAMETER; - if ((r = getdns_sync_loop_init(context, &loop))) + if ((r = getdns_service(context, name, + extensions, &data, &data.transaction_id, getdns_sync_cb))) return r; - if ((r = _getdns_service_loop(context, &loop.loop.loop, name, - extensions, &loop, NULL, getdns_sync_cb))) { - - getdns_sync_loop_cleanup(&loop); - return r; - } - getdns_sync_loop_run(&loop); - - return (*response = loop.response) ? - GETDNS_RETURN_GOOD : GETDNS_RETURN_GENERIC_ERROR; + return _getdns_sync_run(context, &data, response); } /* getdns_core_sync.c */