2013-07-02 16:00:20 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* /brief getdns core functions for synchronous use
|
2013-10-17 18:45:25 -05:00
|
|
|
*
|
2013-07-02 16:00:20 -05:00
|
|
|
* Originally taken from the getdns API description pseudo implementation.
|
|
|
|
*
|
|
|
|
*/
|
2013-11-04 17:37:54 -06:00
|
|
|
|
|
|
|
/*
|
2014-02-25 07:12:33 -06:00
|
|
|
* Copyright (c) 2013, NLnet Labs, Verisign, Inc.
|
2013-11-04 17:37:54 -06:00
|
|
|
* All rights reserved.
|
2013-12-09 11:55:33 -06:00
|
|
|
*
|
2013-11-04 17:37:54 -06:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2014-02-25 07:23:19 -06:00
|
|
|
* * Neither the names of the copyright holders nor the
|
2013-11-04 17:37:54 -06:00
|
|
|
* names of its contributors may be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
2013-10-17 18:45:25 -05:00
|
|
|
*
|
2013-11-04 17:37:54 -06:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2013-07-02 16:00:20 -05:00
|
|
|
*/
|
|
|
|
|
2016-03-04 05:08:46 -06:00
|
|
|
#include <string.h>
|
2016-01-20 04:10:53 -06:00
|
|
|
#include "getdns/getdns.h"
|
2016-03-04 05:08:46 -06:00
|
|
|
#include "config.h"
|
|
|
|
#include "context.h"
|
|
|
|
#include "general.h"
|
|
|
|
#include "types-internal.h"
|
|
|
|
#include "util-internal.h"
|
|
|
|
#include "dnssec.h"
|
|
|
|
|
|
|
|
#include "stub.h"
|
|
|
|
#include "gldns/wire2str.h"
|
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
typedef struct getdns_sync_data {
|
2016-03-04 05:08:46 -06:00
|
|
|
#ifdef HAVE_LIBUNBOUND
|
|
|
|
getdns_eventloop_event ub_event;
|
|
|
|
#endif
|
|
|
|
getdns_context *context;
|
|
|
|
int to_run;
|
|
|
|
getdns_dict *response;
|
2016-03-09 04:16:19 -06:00
|
|
|
} getdns_sync_data;
|
2013-08-16 15:28:21 -05:00
|
|
|
|
2016-03-04 05:08:46 -06:00
|
|
|
static getdns_return_t
|
2016-03-09 04:16:19 -06:00
|
|
|
getdns_sync_data_init(getdns_context *context, getdns_sync_data *data)
|
2016-03-04 05:08:46 -06:00
|
|
|
{
|
2016-03-24 08:02:18 -05:00
|
|
|
#ifdef HAVE_LIBUNBOUND
|
2016-03-09 04:16:19 -06:00
|
|
|
getdns_eventloop *ext = &context->sync_eventloop.loop;
|
2016-03-24 08:02:18 -05:00
|
|
|
#endif
|
2016-03-04 05:08:46 -06:00
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
data->context = context;
|
|
|
|
data->to_run = 1;
|
|
|
|
data->response = NULL;
|
2016-03-04 05:08:46 -06:00
|
|
|
|
2016-03-07 09:35:43 -06:00
|
|
|
#ifdef HAVE_LIBUNBOUND
|
|
|
|
# ifndef USE_WINSOCK
|
2016-03-09 04:16:19 -06:00
|
|
|
data->ub_event.userarg = data->context;
|
|
|
|
data->ub_event.read_cb = _getdns_context_ub_read_cb;
|
|
|
|
data->ub_event.write_cb = NULL;
|
|
|
|
data->ub_event.timeout_cb = NULL;
|
|
|
|
data->ub_event.ev = NULL;
|
2016-03-07 09:35:43 -06:00
|
|
|
# endif
|
2016-03-04 05:08:46 -06:00
|
|
|
# ifdef HAVE_UNBOUND_EVENT_API
|
2016-03-07 09:35:43 -06:00
|
|
|
if (_getdns_ub_loop_enabled(&context->ub_loop)) {
|
|
|
|
context->ub_loop.extension = ext;
|
2017-09-21 04:03:38 -05:00
|
|
|
} else
|
2016-03-04 05:08:46 -06:00
|
|
|
# endif
|
2016-03-07 09:35:43 -06:00
|
|
|
# ifndef USE_WINSOCK
|
2016-03-04 05:08:46 -06:00
|
|
|
return ext->vmt->schedule(ext, ub_fd(context->unbound_ctx),
|
2016-03-09 04:16:19 -06:00
|
|
|
TIMEOUT_FOREVER, &data->ub_event);
|
2016-03-07 09:35:43 -06:00
|
|
|
# else
|
|
|
|
/* No sync full recursion requests on windows without
|
|
|
|
* UNBOUND_EVENT_API because ub_fd() doesn't work on windows.
|
|
|
|
*/
|
2017-09-21 04:03:38 -05:00
|
|
|
# ifdef HAVE_UNBOUND_EVENT_API
|
|
|
|
{ ; } /* pass */
|
|
|
|
# endif
|
2016-03-07 09:35:43 -06:00
|
|
|
# endif
|
2016-03-04 05:08:46 -06:00
|
|
|
#endif
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
2014-09-16 08:43:20 -05:00
|
|
|
|
2014-10-15 05:16:34 -05:00
|
|
|
static void
|
2016-03-09 04:16:19 -06:00
|
|
|
getdns_sync_data_cleanup(getdns_sync_data *data)
|
2014-09-12 07:44:53 -05:00
|
|
|
{
|
2016-04-11 07:49:44 -05:00
|
|
|
size_t i;
|
|
|
|
getdns_context *ctxt = data->context;
|
|
|
|
getdns_upstream *upstream;
|
|
|
|
|
2016-03-04 05:08:46 -06:00
|
|
|
#if defined(HAVE_LIBUNBOUND) && !defined(USE_WINSOCK)
|
|
|
|
# ifdef HAVE_UNBOUND_EVENT_API
|
2016-03-09 04:16:19 -06:00
|
|
|
if (_getdns_ub_loop_enabled(&data->context->ub_loop)) {
|
|
|
|
data->context->ub_loop.extension = data->context->extension;
|
2016-03-04 05:08:46 -06:00
|
|
|
} else
|
|
|
|
# endif
|
2016-03-09 04:16:19 -06:00
|
|
|
data->context->sync_eventloop.loop.vmt->clear(
|
|
|
|
&data->context->sync_eventloop.loop, &data->ub_event);
|
2016-03-04 05:08:46 -06:00
|
|
|
#endif
|
2016-04-11 07:49:44 -05:00
|
|
|
/* If statefull upstream have events scheduled against the sync loop,
|
|
|
|
* reschedule against the async loop.
|
|
|
|
*/
|
2017-11-23 05:26:40 -06:00
|
|
|
if (!ctxt->upstreams)
|
|
|
|
; /* pass */
|
|
|
|
|
|
|
|
else for (i = 0; i < ctxt->upstreams->count; i++) {
|
2016-04-11 07:49:44 -05:00
|
|
|
upstream = &ctxt->upstreams->upstreams[i];
|
|
|
|
if (upstream->loop != &data->context->sync_eventloop.loop)
|
|
|
|
continue;
|
2017-10-17 09:58:01 -05:00
|
|
|
GETDNS_CLEAR_EVENT(upstream->loop, &upstream->event);
|
|
|
|
if (upstream->event.timeout_cb &&
|
|
|
|
( upstream->conn_state != GETDNS_CONN_OPEN
|
|
|
|
|| upstream->keepalive_timeout == 0)) {
|
|
|
|
(*upstream->event.timeout_cb)(upstream->event.userarg);
|
|
|
|
upstream->event.timeout_cb = NULL;
|
2016-04-11 07:49:44 -05:00
|
|
|
}
|
|
|
|
upstream->loop = data->context->extension;
|
|
|
|
upstream->is_sync_loop = 0;
|
2017-03-26 10:16:25 -05:00
|
|
|
|
2017-10-17 09:58:01 -05:00
|
|
|
if ( upstream->event.read_cb || upstream->event.write_cb
|
|
|
|
|| upstream->event.timeout_cb) {
|
|
|
|
GETDNS_SCHEDULE_EVENT(upstream->loop,
|
|
|
|
( upstream->event.read_cb
|
|
|
|
|| upstream->event.write_cb ? upstream->fd : -1),
|
|
|
|
( upstream->event.timeout_cb
|
|
|
|
? upstream->keepalive_timeout : TIMEOUT_FOREVER ),
|
|
|
|
&upstream->event);
|
2017-03-26 10:16:25 -05:00
|
|
|
}
|
2016-04-11 07:49:44 -05:00
|
|
|
}
|
2016-03-04 05:08:46 -06:00
|
|
|
}
|
2016-01-20 04:10:53 -06:00
|
|
|
|
2016-03-04 05:08:46 -06:00
|
|
|
static void
|
2016-03-09 04:16:19 -06:00
|
|
|
getdns_sync_loop_run(getdns_sync_data *data)
|
2016-03-04 05:08:46 -06:00
|
|
|
{
|
2016-03-09 04:16:19 -06:00
|
|
|
while (data->to_run)
|
|
|
|
data->context->sync_eventloop.loop.vmt->run_once(
|
|
|
|
&data->context->sync_eventloop.loop, 1);
|
2014-09-12 07:44:53 -05:00
|
|
|
}
|
2014-09-16 08:43:20 -05:00
|
|
|
|
2016-03-04 05:08:46 -06:00
|
|
|
static void
|
|
|
|
getdns_sync_cb(getdns_context *context, getdns_callback_type_t callback_type,
|
|
|
|
getdns_dict *response, void *userarg, getdns_transaction_t transaction_id)
|
2014-09-23 06:36:26 -05:00
|
|
|
{
|
2016-03-09 04:16:19 -06:00
|
|
|
getdns_sync_data *data = (getdns_sync_data *)userarg;
|
2016-12-08 07:05:58 -06:00
|
|
|
(void)context; (void)callback_type; (void)transaction_id;
|
2014-10-15 05:16:34 -05:00
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
assert(data);
|
2014-10-15 05:16:34 -05:00
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
data->response = response;
|
|
|
|
data->to_run = 0;
|
2014-09-23 06:36:26 -05:00
|
|
|
}
|
|
|
|
|
2013-07-02 16:00:20 -05:00
|
|
|
getdns_return_t
|
2014-10-15 05:16:34 -05:00
|
|
|
getdns_general_sync(getdns_context *context, const char *name,
|
|
|
|
uint16_t request_type, getdns_dict *extensions, getdns_dict **response)
|
2013-08-16 15:28:21 -05:00
|
|
|
{
|
2016-03-09 04:16:19 -06:00
|
|
|
getdns_sync_data data;
|
2014-10-15 05:16:34 -05:00
|
|
|
getdns_return_t r;
|
|
|
|
|
|
|
|
if (!context || !name || !response)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
if ((r = getdns_sync_data_init(context, &data)))
|
2014-10-15 05:16:34 -05:00
|
|
|
return r;
|
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
if ((r = _getdns_general_loop(context, &context->sync_eventloop.loop,
|
|
|
|
name, request_type, extensions, &data, NULL, getdns_sync_cb, NULL))) {
|
2016-03-04 05:08:46 -06:00
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
getdns_sync_data_cleanup(&data);
|
2016-03-04 05:08:46 -06:00
|
|
|
return r;
|
|
|
|
}
|
2016-03-09 04:16:19 -06:00
|
|
|
getdns_sync_loop_run(&data);
|
2016-03-17 10:49:05 -05:00
|
|
|
getdns_sync_data_cleanup(&data);
|
2016-03-04 05:08:46 -06:00
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
return (*response = data.response) ?
|
2016-03-04 05:08:46 -06:00
|
|
|
GETDNS_RETURN_GOOD : GETDNS_RETURN_GENERIC_ERROR;
|
2013-08-16 15:28:21 -05:00
|
|
|
}
|
2013-07-02 16:00:20 -05:00
|
|
|
|
|
|
|
getdns_return_t
|
2014-10-15 05:16:34 -05:00
|
|
|
getdns_address_sync(getdns_context *context, const char *name,
|
|
|
|
getdns_dict *extensions, getdns_dict **response)
|
2013-08-16 15:28:21 -05:00
|
|
|
{
|
2016-03-09 04:16:19 -06:00
|
|
|
getdns_sync_data data;
|
2014-10-15 05:16:34 -05:00
|
|
|
getdns_return_t r;
|
|
|
|
|
|
|
|
if (!context || !name || !response)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
if ((r = getdns_sync_data_init(context, &data)))
|
2014-10-15 05:16:34 -05:00
|
|
|
return r;
|
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
if ((r = _getdns_address_loop(context, &context->sync_eventloop.loop,
|
|
|
|
name, extensions, &data, NULL, getdns_sync_cb))) {
|
2016-03-04 05:08:46 -06:00
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
getdns_sync_data_cleanup(&data);
|
2016-03-04 05:08:46 -06:00
|
|
|
return r;
|
|
|
|
}
|
2016-03-09 04:16:19 -06:00
|
|
|
getdns_sync_loop_run(&data);
|
2016-03-17 10:49:05 -05:00
|
|
|
getdns_sync_data_cleanup(&data);
|
2016-03-04 05:08:46 -06:00
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
return (*response = data.response) ?
|
2016-03-04 05:08:46 -06:00
|
|
|
GETDNS_RETURN_GOOD : GETDNS_RETURN_GENERIC_ERROR;
|
2013-08-16 15:28:21 -05:00
|
|
|
}
|
2013-07-02 16:00:20 -05:00
|
|
|
|
|
|
|
getdns_return_t
|
2014-10-15 05:16:34 -05:00
|
|
|
getdns_hostname_sync(getdns_context *context, getdns_dict *address,
|
|
|
|
getdns_dict *extensions, getdns_dict **response)
|
2013-10-30 15:16:20 -05:00
|
|
|
{
|
2016-03-09 04:16:19 -06:00
|
|
|
getdns_sync_data data;
|
2014-10-15 05:16:34 -05:00
|
|
|
getdns_return_t r;
|
|
|
|
|
2016-03-04 05:08:46 -06:00
|
|
|
if (!context || !address || !response)
|
2014-02-03 20:34:11 -06:00
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
2014-10-15 05:16:34 -05:00
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
if ((r = getdns_sync_data_init(context, &data)))
|
2014-10-15 05:16:34 -05:00
|
|
|
return r;
|
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
if ((r = _getdns_hostname_loop(context, &context->sync_eventloop.loop,
|
|
|
|
address, extensions, &data, NULL, getdns_sync_cb))) {
|
2016-03-04 05:08:46 -06:00
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
getdns_sync_data_cleanup(&data);
|
2016-03-04 05:08:46 -06:00
|
|
|
return r;
|
|
|
|
}
|
2016-03-09 04:16:19 -06:00
|
|
|
getdns_sync_loop_run(&data);
|
2016-03-17 10:49:05 -05:00
|
|
|
getdns_sync_data_cleanup(&data);
|
2016-03-04 05:08:46 -06:00
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
return (*response = data.response) ?
|
2016-03-04 05:08:46 -06:00
|
|
|
GETDNS_RETURN_GOOD : GETDNS_RETURN_GENERIC_ERROR;
|
2013-10-30 15:16:20 -05:00
|
|
|
}
|
|
|
|
|
2013-07-02 16:00:20 -05:00
|
|
|
getdns_return_t
|
2014-10-15 05:16:34 -05:00
|
|
|
getdns_service_sync(getdns_context *context, const char *name,
|
|
|
|
getdns_dict *extensions, getdns_dict **response)
|
2013-08-16 15:28:21 -05:00
|
|
|
{
|
2016-03-09 04:16:19 -06:00
|
|
|
getdns_sync_data data;
|
2014-10-15 05:16:34 -05:00
|
|
|
getdns_return_t r;
|
2013-10-17 18:45:25 -05:00
|
|
|
|
2014-10-15 05:16:34 -05:00
|
|
|
if (!context || !name || !response)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
if ((r = getdns_sync_data_init(context, &data)))
|
2014-10-15 05:16:34 -05:00
|
|
|
return r;
|
2013-08-16 15:28:21 -05:00
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
if ((r = _getdns_service_loop(context, &context->sync_eventloop.loop,
|
|
|
|
name, extensions, &data, NULL, getdns_sync_cb))) {
|
2016-03-04 05:08:46 -06:00
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
getdns_sync_data_cleanup(&data);
|
2016-03-04 05:08:46 -06:00
|
|
|
return r;
|
|
|
|
}
|
2016-03-09 04:16:19 -06:00
|
|
|
getdns_sync_loop_run(&data);
|
2016-03-17 10:49:05 -05:00
|
|
|
getdns_sync_data_cleanup(&data);
|
2016-03-04 05:08:46 -06:00
|
|
|
|
2016-03-09 04:16:19 -06:00
|
|
|
return (*response = data.response) ?
|
2016-03-04 05:08:46 -06:00
|
|
|
GETDNS_RETURN_GOOD : GETDNS_RETURN_GENERIC_ERROR;
|
2013-08-16 15:28:21 -05:00
|
|
|
}
|
2013-07-02 16:00:20 -05:00
|
|
|
|
|
|
|
/* getdns_core_sync.c */
|