getdns/src/sync.c

191 lines
6.0 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.
*
*/
/*
* Copyright (c) 2013, Versign, Inc.
* All rights reserved.
2013-12-09 11:55:33 -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.
* * Neither the name of the <organization> nor the
* 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
*
* 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.
*/
#include "config.h"
2013-08-15 09:16:15 -05:00
#include <getdns/getdns.h>
2014-01-21 14:31:22 -06:00
#include <unbound.h>
2013-10-15 16:28:23 -05:00
#include "context.h"
#include "general.h"
#include "types-internal.h"
2013-10-31 14:08:23 -05:00
#include "util-internal.h"
#include <string.h>
/* stuff to make it compile pedantically */
#define UNUSED_PARAM(x) ((void)(x))
2013-12-09 11:55:33 -06:00
#define RETURN_IF_NULL(ptr, code) if(ptr == NULL) return code;
2014-01-21 14:31:22 -06:00
getdns_return_t submit_request_sync(getdns_dns_req* req) {
struct ub_result* ub_res = NULL;
getdns_return_t gr = GETDNS_RETURN_GOOD;
getdns_network_req *netreq = req->first_req;
while (netreq) {
int r = ub_resolve(req->context->unbound_ctx,
req->name,
netreq->request_type,
netreq->request_class,
&ub_res);
if (r != 0) {
return GETDNS_RETURN_GENERIC_ERROR;
}
gr = getdns_apply_network_result(netreq, ub_res);
ub_resolve_free(ub_res);
ub_res = NULL;
if (gr != GETDNS_RETURN_GOOD) {
return gr;
}
netreq = netreq->next;
}
return gr;
2013-08-16 15:28:21 -05:00
}
getdns_return_t
2013-12-06 08:54:06 -06:00
getdns_general_sync(struct getdns_context *context,
const char *name,
uint16_t request_type,
struct getdns_dict *extensions,
struct getdns_dict **response)
2013-08-16 15:28:21 -05:00
{
getdns_return_t response_status;
2013-12-09 11:55:33 -06:00
RETURN_IF_NULL(context, GETDNS_RETURN_BAD_CONTEXT);
RETURN_IF_NULL(response, GETDNS_RETURN_INVALID_PARAMETER);
2014-01-27 18:32:54 -06:00
RETURN_IF_NULL(name, GETDNS_RETURN_INVALID_PARAMETER);
response_status = validate_dname(name);
if (response_status != GETDNS_RETURN_GOOD) {
return response_status;
}
response_status = validate_extensions(extensions);
if (response_status == GETDNS_RETURN_GOOD) {
2014-01-21 14:31:22 -06:00
/* for each netreq we call ub_ctx_resolve */
/* request state */
getdns_dns_req *req = dns_req_new(context,
name,
request_type,
extensions);
if (!req) {
return GETDNS_RETURN_GENERIC_ERROR;
}
response_status = submit_request_sync(req);
if (response_status != GETDNS_RETURN_GOOD) {
dns_req_free(req);
return response_status;
}
*response = create_getdns_response(req);
dns_req_free(req);
}
return response_status;
2013-08-16 15:28:21 -05:00
}
getdns_return_t
2013-12-06 08:54:06 -06:00
getdns_address_sync(struct getdns_context *context,
const char *name,
struct getdns_dict * extensions,
struct getdns_dict ** response)
2013-08-16 15:28:21 -05:00
{
int cleanup_extensions = 0;
if (!extensions) {
extensions = getdns_dict_create_with_context(context);
cleanup_extensions = 1;
}
getdns_dict_set_int(extensions,
GETDNS_STR_EXTENSION_RETURN_BOTH_V4_AND_V6, GETDNS_EXTENSION_TRUE);
getdns_return_t result =
getdns_general_sync(context, name, GETDNS_RRTYPE_A,
extensions, response);
if (cleanup_extensions) {
getdns_dict_destroy(extensions);
}
return result;
2013-08-16 15:28:21 -05:00
}
getdns_return_t
2013-12-06 08:54:06 -06:00
getdns_hostname_sync(struct getdns_context *context,
struct getdns_dict * address,
struct getdns_dict * extensions,
struct getdns_dict ** response)
{
struct getdns_bindata *address_data;
struct getdns_bindata *address_type;
uint16_t req_type;
char *name;
getdns_return_t retval;
if ((retval =
getdns_dict_get_bindata(address, "address_data",
&address_data)) != GETDNS_RETURN_GOOD)
return retval;
if ((retval =
getdns_dict_get_bindata(address, "address_type",
&address_type)) != GETDNS_RETURN_GOOD)
return retval;
if ((strncmp(GETDNS_STR_IPV4, (char *) address_type->data,
( strlen(GETDNS_STR_IPV4) < address_type->size
? strlen(GETDNS_STR_IPV4) : address_type->size )) == 0
&& address_data->size == 4)
|| (strncmp(GETDNS_STR_IPV6, (char *) address_type->data,
( strlen(GETDNS_STR_IPV6) < address_type->size
? strlen(GETDNS_STR_IPV6) : address_type->size )) == 0
&& address_data->size == 16))
req_type = GETDNS_RRTYPE_PTR;
else
return GETDNS_RETURN_WRONG_TYPE_REQUESTED;
if ((name = reverse_address(address_data)) == NULL)
return GETDNS_RETURN_GENERIC_ERROR;
retval = getdns_general_sync(context, name, req_type, extensions,
response);
free(name);
return retval;
}
getdns_return_t
2013-12-06 08:54:06 -06:00
getdns_service_sync(struct getdns_context *context,
const char *name,
struct getdns_dict * extensions,
struct getdns_dict ** response)
2013-08-16 15:28:21 -05:00
{
2013-10-17 18:45:25 -05:00
return getdns_general_sync(context, name, GETDNS_RRTYPE_SRV,
extensions, response);
2013-08-16 15:28:21 -05:00
}
void
getdns_free_sync_request_memory(struct getdns_dict *response)
2013-11-04 11:51:13 -06:00
{
getdns_dict_destroy(response);
2013-11-04 11:51:13 -06:00
}
/* getdns_core_sync.c */