getdns/src/sync.c

185 lines
5.7 KiB
C
Raw Normal View History

/**
*
* /brief getdns core functions for synchronous use
2013-10-17 18:45:25 -05:00
*
* Originally taken from the getdns API description pseudo implementation.
*
*/
/* The MIT License (MIT)
* Copyright (c) 2013 Verisign, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
2013-10-17 18:45:25 -05:00
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
2013-10-17 18:45:25 -05:00
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2013-08-15 09:16:15 -05:00
#include <getdns/getdns.h>
2013-08-16 15:28:21 -05:00
#include <pthread.h>
2013-10-15 16:28:23 -05:00
#include <event2/event.h>
#include <unbound-event.h>
#include "context.h"
#include "general.h"
#include <string.h>
/* stuff to make it compile pedantically */
#define UNUSED_PARAM(x) ((void)(x))
2013-08-16 15:28:21 -05:00
/* struct used for the request */
typedef struct sync_request_data {
getdns_context_t context;
char* name;
2013-08-16 15:28:21 -05:00
uint16_t request_type;
getdns_dict *extensions;
getdns_return_t response_status;
getdns_dict **response;
} sync_request_data;
static void sync_callback_func(getdns_context_t context,
uint16_t callback_type,
struct getdns_dict *response,
void *userarg,
getdns_transaction_t transaction_id) {
sync_request_data* req_data = userarg;
*(req_data->response) = response;
}
static void * request_thread_start(void *arg) {
struct sync_request_data *req_data = arg;
2013-10-17 18:45:25 -05:00
2013-10-15 16:28:23 -05:00
req_data->response_status = getdns_general_ub(req_data->context->unbound_sync,
2013-10-18 12:55:31 -05:00
req_data->context->event_base_sync,
2013-10-15 16:28:23 -05:00
req_data->context,
req_data->name,
req_data->request_type,
req_data->extensions,
req_data,
NULL,
sync_callback_func);
2013-10-17 18:45:25 -05:00
2013-10-15 16:28:23 -05:00
event_base_dispatch(req_data->context->event_base_sync);
2013-08-16 15:28:21 -05:00
return NULL;
}
getdns_return_t
getdns_general_sync(
getdns_context_t context,
const char *name,
uint16_t request_type,
struct getdns_dict *extensions,
uint32_t *response_length,
2013-08-16 15:28:21 -05:00
struct getdns_dict **response
)
2013-08-16 15:28:21 -05:00
{
/* we will cheat and spawn a thread */
2013-10-15 16:28:23 -05:00
/* set up for sync resolution */
2013-08-16 15:28:21 -05:00
pthread_t thread;
pthread_attr_t attr;
sync_request_data req_data = {
context,
strdup(name),
request_type,
2013-08-16 15:28:21 -05:00
extensions,
GETDNS_RETURN_GOOD,
response
};
2013-10-17 18:45:25 -05:00
2013-08-16 15:28:21 -05:00
/* create the thread */
int ret = pthread_attr_init(&attr);
if (ret != 0) {
free(req_data.name);
2013-08-16 15:28:21 -05:00
return GETDNS_RETURN_GENERIC_ERROR;
}
ret = pthread_create(&thread, &attr, request_thread_start, &req_data);
if (ret != 0) {
pthread_attr_destroy(&attr);
free(req_data.name);
2013-08-16 15:28:21 -05:00
return GETDNS_RETURN_GENERIC_ERROR;
}
/* wait for the thread */
ret = pthread_join(thread, NULL);
/* delete attr */
pthread_attr_destroy(&attr);
if (ret != 0) {
free(req_data.name);
2013-08-16 15:28:21 -05:00
return GETDNS_RETURN_GENERIC_ERROR;
}
free(req_data.name);
2013-08-16 15:28:21 -05:00
return req_data.response_status;
}
getdns_return_t
getdns_address_sync(
getdns_context_t context,
const char *name,
struct getdns_dict *extensions,
uint32_t *response_length,
2013-08-16 15:28:21 -05:00
struct getdns_dict **response
)
2013-08-16 15:28:21 -05:00
{
int cleanup_extensions = 0;
if (!extensions) {
extensions = getdns_dict_create();
cleanup_extensions = 1;
}
getdns_dict_set_int(extensions,
GETDNS_STR_EXTENSION_RETURN_BOTH_V4_AND_V6,
GETDNS_EXTENSION_TRUE);
2013-10-17 18:45:25 -05:00
2013-08-16 15:28:21 -05:00
getdns_return_t result =
getdns_general_sync(context, name, GETDNS_RRTYPE_A,
extensions, response_length, response);
if (cleanup_extensions) {
getdns_dict_destroy(extensions);
}
return result;
}
getdns_return_t
getdns_hostname_sync(
getdns_context_t context,
struct getdns_dict *address,
struct getdns_dict *extensions,
uint32_t *response_length,
2013-08-16 15:28:21 -05:00
struct getdns_dict **response
)
{ UNUSED_PARAM(context); UNUSED_PARAM(address); UNUSED_PARAM(extensions);
UNUSED_PARAM(response_length); UNUSED_PARAM(response); return GETDNS_RETURN_GOOD; }
getdns_return_t
getdns_service_sync(
getdns_context_t context,
const char *name,
struct getdns_dict *extensions,
uint32_t *response_length,
2013-08-16 15:28:21 -05:00
struct getdns_dict **response
)
2013-08-16 15:28:21 -05:00
{
2013-10-17 18:45:25 -05:00
2013-08-16 15:28:21 -05:00
return getdns_general_sync(context, name, GETDNS_RRTYPE_SRV, extensions,
response_length, response);
}
void
getdns_free_sync_request_memory(
struct getdns_dict *response
)
{ UNUSED_PARAM(response); }
/* getdns_core_sync.c */