Run context's event loop when doing sync requests

This commit is contained in:
Willem Toorop 2016-01-20 11:10:53 +01:00
parent ca36c879a0
commit d50860c089
1 changed files with 46 additions and 130 deletions

View File

@ -33,197 +33,113 @@
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <string.h>
#include "getdns/getdns.h"
#include "config.h" #include "config.h"
#include "context.h" #include "getdns/getdns.h"
#include "general.h" #include "getdns/getdns_extra.h"
#include "types-internal.h"
#include "util-internal.h"
#include "dnssec.h"
#include "stub.h"
#include "gldns/wire2str.h"
typedef struct getdns_sync_loop { typedef struct getdns_sync_data {
_getdns_default_eventloop loop;
#ifdef HAVE_LIBUNBOUND
getdns_eventloop_event ub_event;
#endif
getdns_context *context;
int to_run; int to_run;
getdns_callback_type_t callback_type;
getdns_dict *response; getdns_dict *response;
} getdns_sync_loop; getdns_transaction_t transaction_id;
} getdns_sync_data;
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);
}
static void static void
getdns_sync_cb(getdns_context *context, getdns_callback_type_t callback_type, getdns_sync_cb(getdns_context *context, getdns_callback_type_t callback_type,
getdns_dict *response, void *userarg, getdns_transaction_t transaction_id) 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; data->callback_type = callback_type;
loop->to_run = 0; 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_return_t
getdns_general_sync(getdns_context *context, const char *name, getdns_general_sync(getdns_context *context, const char *name,
uint16_t request_type, getdns_dict *extensions, getdns_dict **response) uint16_t request_type, getdns_dict *extensions, getdns_dict **response)
{ {
getdns_sync_loop loop; getdns_sync_data data;
getdns_return_t r; getdns_return_t r;
if (!context || !name || !response) if (!context || !name || !response)
return GETDNS_RETURN_INVALID_PARAMETER; 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; return r;
if ((r = _getdns_general_loop(context, &loop.loop.loop, name, return _getdns_sync_run(context, &data, response);
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;
} }
getdns_return_t getdns_return_t
getdns_address_sync(getdns_context *context, const char *name, getdns_address_sync(getdns_context *context, const char *name,
getdns_dict *extensions, getdns_dict **response) getdns_dict *extensions, getdns_dict **response)
{ {
getdns_sync_loop loop; getdns_sync_data data;
getdns_return_t r; getdns_return_t r;
if (!context || !name || !response) if (!context || !name || !response)
return GETDNS_RETURN_INVALID_PARAMETER; 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; return r;
if ((r = _getdns_address_loop(context, &loop.loop.loop, name, return _getdns_sync_run(context, &data, response);
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;
} }
getdns_return_t getdns_return_t
getdns_hostname_sync(getdns_context *context, getdns_dict *address, getdns_hostname_sync(getdns_context *context, getdns_dict *address,
getdns_dict *extensions, getdns_dict **response) getdns_dict *extensions, getdns_dict **response)
{ {
getdns_sync_loop loop; getdns_sync_data data;
getdns_return_t r; getdns_return_t r;
if (!context || !address|| !response) if (!context || !address|| !response)
return GETDNS_RETURN_INVALID_PARAMETER; 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; return r;
if ((r = _getdns_hostname_loop(context, &loop.loop.loop, address, return _getdns_sync_run(context, &data, response);
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;
} }
getdns_return_t getdns_return_t
getdns_service_sync(getdns_context *context, const char *name, getdns_service_sync(getdns_context *context, const char *name,
getdns_dict *extensions, getdns_dict **response) getdns_dict *extensions, getdns_dict **response)
{ {
getdns_sync_loop loop; getdns_sync_data data;
getdns_return_t r; getdns_return_t r;
if (!context || !name || !response) if (!context || !name || !response)
return GETDNS_RETURN_INVALID_PARAMETER; 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; return r;
if ((r = _getdns_service_loop(context, &loop.loop.loop, name, return _getdns_sync_run(context, &data, response);
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;
} }
/* getdns_core_sync.c */ /* getdns_core_sync.c */