2013-07-15 16:42:37 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* /brief getdns contect management functions
|
2013-10-16 17:33:12 -05:00
|
|
|
*
|
2013-07-15 16:42:37 -05:00
|
|
|
* This is the meat of the API
|
|
|
|
* 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-16 17:33:12 -05:00
|
|
|
*
|
2013-07-15 16:42:37 -05:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2013-10-16 17:33:12 -05:00
|
|
|
*
|
2013-07-15 16:42:37 -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-11-30 06:53:57 -06:00
|
|
|
#include "config.h"
|
|
|
|
#ifdef HAVE_EVENT2_EVENT_H
|
|
|
|
# include <event2/event.h>
|
|
|
|
#else
|
|
|
|
# include <event.h>
|
|
|
|
#endif
|
2013-11-06 12:32:05 -06:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <ldns/ldns.h>
|
2013-08-12 16:38:05 -05:00
|
|
|
#include <string.h>
|
2013-10-15 16:28:23 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2013-11-06 12:32:05 -06:00
|
|
|
#include <unbound-event.h>
|
|
|
|
#include <unbound.h>
|
|
|
|
|
2013-08-15 09:54:30 -05:00
|
|
|
#include "context.h"
|
2013-10-16 17:33:12 -05:00
|
|
|
#include "types-internal.h"
|
2013-11-06 12:32:05 -06:00
|
|
|
#include "util-internal.h"
|
|
|
|
|
|
|
|
/* Private functions */
|
2013-12-06 08:54:06 -06:00
|
|
|
static uint16_t *create_default_namespaces(struct getdns_context *context);
|
2013-11-06 12:32:05 -06:00
|
|
|
static struct getdns_list *create_default_root_servers();
|
2013-12-06 08:54:06 -06:00
|
|
|
static getdns_return_t add_ip_str(struct getdns_dict *);
|
|
|
|
static struct getdns_dict *create_ipaddr_dict_from_rdf(struct getdns_context *,
|
2013-11-11 16:10:22 -06:00
|
|
|
ldns_rdf *);
|
2013-12-06 08:54:06 -06:00
|
|
|
static struct getdns_list *create_from_ldns_list(struct getdns_context *,
|
2013-11-11 16:10:22 -06:00
|
|
|
ldns_rdf **, size_t);
|
2013-12-06 08:54:06 -06:00
|
|
|
static getdns_return_t set_os_defaults(struct getdns_context *);
|
2013-11-06 12:32:05 -06:00
|
|
|
static int transaction_id_cmp(const void *, const void *);
|
2013-12-06 08:54:06 -06:00
|
|
|
static void set_ub_string_opt(struct getdns_context *, char *, char *);
|
|
|
|
static void set_ub_number_opt(struct getdns_context *, char *, uint16_t);
|
|
|
|
static inline void clear_resolution_type_set_flag(struct getdns_context *, uint16_t);
|
|
|
|
static void dispatch_updated(struct getdns_context *, uint16_t);
|
2013-11-06 12:32:05 -06:00
|
|
|
static void cancel_dns_req(getdns_dns_req *);
|
2013-07-15 16:42:37 -05:00
|
|
|
|
2013-11-06 12:32:05 -06:00
|
|
|
/* Stuff to make it compile pedantically */
|
2013-07-15 16:42:37 -05:00
|
|
|
#define UNUSED_PARAM(x) ((void)(x))
|
|
|
|
|
2013-08-12 16:38:05 -05:00
|
|
|
/**
|
|
|
|
* Helper to get default lookup namespaces.
|
|
|
|
* TODO: Determine from OS
|
|
|
|
*/
|
2013-11-05 14:03:44 -06:00
|
|
|
static uint16_t *
|
2013-12-06 08:54:06 -06:00
|
|
|
create_default_namespaces(struct getdns_context *context)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2013-11-11 16:10:22 -06:00
|
|
|
uint16_t *result = GETDNS_XMALLOC(context, uint16_t, 2);
|
2013-11-05 14:03:44 -06:00
|
|
|
result[0] = GETDNS_CONTEXT_NAMESPACE_LOCALNAMES;
|
|
|
|
result[1] = GETDNS_CONTEXT_NAMESPACE_DNS;
|
|
|
|
return result;
|
2013-08-12 16:38:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to get the default root servers.
|
|
|
|
* TODO: Implement
|
|
|
|
*/
|
2013-11-05 14:03:44 -06:00
|
|
|
static struct getdns_list *
|
|
|
|
create_default_root_servers()
|
|
|
|
{
|
|
|
|
return NULL;
|
2013-08-12 16:38:05 -05:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
static getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
add_ip_str(struct getdns_dict * ip)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
|
|
|
struct sockaddr_storage storage;
|
|
|
|
char buff[256];
|
|
|
|
getdns_return_t r = dict_to_sockaddr(ip, &storage);
|
|
|
|
if (r != GETDNS_RETURN_GOOD) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (storage.ss_family == AF_INET) {
|
|
|
|
struct sockaddr_in *addr = (struct sockaddr_in *) &storage;
|
|
|
|
const char *ipStr =
|
|
|
|
inet_ntop(AF_INET, &(addr->sin_addr), buff, 256);
|
|
|
|
if (!ipStr) {
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
getdns_dict_util_set_string(ip, GETDNS_STR_ADDRESS_STRING,
|
|
|
|
ipStr);
|
|
|
|
} else if (storage.ss_family == AF_INET6) {
|
|
|
|
struct sockaddr_in6 *addr = (struct sockaddr_in6 *) &storage;
|
|
|
|
const char *ipStr =
|
|
|
|
inet_ntop(AF_INET6, &(addr->sin6_addr), buff, 256);
|
|
|
|
if (!ipStr) {
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
getdns_dict_util_set_string(ip, GETDNS_STR_ADDRESS_STRING,
|
|
|
|
ipStr);
|
|
|
|
} else {
|
|
|
|
/* unknown */
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-10-16 13:45:43 -05:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
static struct getdns_dict *
|
2013-12-06 08:54:06 -06:00
|
|
|
create_ipaddr_dict_from_rdf(struct getdns_context *context, ldns_rdf * rdf)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
|
|
|
ldns_rdf_type rt = ldns_rdf_get_type(rdf);
|
|
|
|
size_t sz = ldns_rdf_size(rdf);
|
2013-12-06 08:54:06 -06:00
|
|
|
struct getdns_dict *result = getdns_dict_create_with_context(context);
|
2013-11-05 14:03:44 -06:00
|
|
|
/* set type */
|
|
|
|
if (rt == LDNS_RDF_TYPE_A) {
|
|
|
|
getdns_dict_util_set_string(result, GETDNS_STR_ADDRESS_TYPE,
|
|
|
|
GETDNS_STR_IPV4);
|
|
|
|
} else {
|
|
|
|
getdns_dict_util_set_string(result, GETDNS_STR_ADDRESS_TYPE,
|
|
|
|
GETDNS_STR_IPV6);
|
|
|
|
}
|
|
|
|
/* set data */
|
2013-12-06 08:54:06 -06:00
|
|
|
struct getdns_bindata data_bin = { sz, ldns_rdf_data(rdf) };
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_dict_set_bindata(result, GETDNS_STR_ADDRESS_DATA, &data_bin);
|
|
|
|
add_ip_str(result);
|
|
|
|
return result;
|
2013-08-12 18:47:33 -05:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
static struct getdns_list *
|
2013-12-06 08:54:06 -06:00
|
|
|
create_from_ldns_list(struct getdns_context *context, ldns_rdf ** ldns_list,
|
2013-11-11 16:10:22 -06:00
|
|
|
size_t count)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
|
|
|
size_t idx = 0;
|
2013-11-11 16:10:22 -06:00
|
|
|
struct getdns_list *result = getdns_list_create_with_context(context);
|
2013-11-05 14:03:44 -06:00
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
ldns_rdf *rdf = ldns_list[i];
|
|
|
|
switch (ldns_rdf_get_type(rdf)) {
|
|
|
|
case LDNS_RDF_TYPE_A:
|
|
|
|
case LDNS_RDF_TYPE_AAAA:
|
|
|
|
{
|
2013-12-06 08:54:06 -06:00
|
|
|
struct getdns_dict *ipaddr =
|
2013-11-11 16:10:22 -06:00
|
|
|
create_ipaddr_dict_from_rdf(context, rdf);
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_list_add_item(result, &idx);
|
|
|
|
getdns_list_set_dict(result, idx, ipaddr);
|
|
|
|
getdns_dict_destroy(ipaddr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LDNS_RDF_TYPE_DNAME:
|
|
|
|
{
|
2013-12-06 08:54:06 -06:00
|
|
|
struct getdns_bindata item;
|
2013-11-05 14:03:44 -06:00
|
|
|
char *srch = ldns_rdf2str(rdf);
|
2013-11-11 16:10:22 -06:00
|
|
|
item.size = strlen(srch) + 1;
|
2013-11-05 14:03:44 -06:00
|
|
|
item.data = (uint8_t *) srch;
|
|
|
|
getdns_list_add_item(result, &idx);
|
|
|
|
getdns_list_set_bindata(result, idx, &item);
|
|
|
|
free(srch);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2013-08-12 18:47:33 -05:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
static getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
set_os_defaults(struct getdns_context *context)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
|
|
|
ldns_resolver *lr = NULL;
|
|
|
|
if (ldns_resolver_new_frm_file(&lr, NULL) != LDNS_STATUS_OK) {
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
ldns_rdf **rdf_list = ldns_resolver_nameservers(lr);
|
|
|
|
size_t rdf_list_sz = ldns_resolver_nameserver_count(lr);
|
|
|
|
if (rdf_list_sz > 0) {
|
|
|
|
context->upstream_list =
|
2013-11-11 16:10:22 -06:00
|
|
|
create_from_ldns_list(context, rdf_list, rdf_list_sz);
|
2013-11-05 14:03:44 -06:00
|
|
|
}
|
|
|
|
rdf_list = ldns_resolver_searchlist(lr);
|
|
|
|
rdf_list_sz = ldns_resolver_searchlist_count(lr);
|
|
|
|
if (rdf_list_sz > 0) {
|
2013-11-11 16:10:22 -06:00
|
|
|
context->suffix = create_from_ldns_list(context, rdf_list,
|
|
|
|
rdf_list_sz);
|
2013-11-05 14:03:44 -06:00
|
|
|
}
|
2013-08-12 18:47:33 -05:00
|
|
|
/** cleanup **/
|
2013-11-05 14:03:44 -06:00
|
|
|
ldns_resolver_deep_free(lr);
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-08-12 16:38:05 -05:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
static int
|
|
|
|
transaction_id_cmp(const void *id1, const void *id2)
|
|
|
|
{
|
|
|
|
if (id1 == NULL && id2 == NULL) {
|
|
|
|
return 0;
|
|
|
|
} else if (id1 == NULL && id2 != NULL) {
|
|
|
|
return 1;
|
|
|
|
} else if (id1 != NULL && id2 == NULL) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
getdns_transaction_t t1 =
|
|
|
|
*((const getdns_transaction_t *) id1);
|
|
|
|
getdns_transaction_t t2 =
|
|
|
|
*((const getdns_transaction_t *) id2);
|
|
|
|
if (t1 == t2) {
|
|
|
|
return 0;
|
|
|
|
} else if (t1 < t2) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2013-10-16 17:33:12 -05:00
|
|
|
}
|
|
|
|
|
2013-07-15 16:42:37 -05:00
|
|
|
/*
|
|
|
|
* getdns_context_create
|
|
|
|
*
|
2013-11-06 12:32:05 -06:00
|
|
|
* Call this to initialize the context that is used in other getdns calls.
|
2013-07-15 16:42:37 -05:00
|
|
|
*/
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_create_with_memory_functions(struct getdns_context ** context,
|
2013-11-11 16:10:22 -06:00
|
|
|
int set_from_os,
|
|
|
|
void *(*malloc)(size_t),
|
|
|
|
void *(*realloc)(void *, size_t),
|
|
|
|
void (*free)(void *)
|
|
|
|
)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2013-12-06 08:54:06 -06:00
|
|
|
struct getdns_context *result = NULL;
|
2013-08-12 18:47:33 -05:00
|
|
|
|
2013-11-11 16:10:22 -06:00
|
|
|
if (!context || !malloc || !realloc || !free)
|
2013-11-05 14:03:44 -06:00
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2013-10-15 16:28:23 -05:00
|
|
|
/** default init **/
|
2013-12-06 08:54:06 -06:00
|
|
|
result = (*malloc)(sizeof(struct getdns_context));
|
2013-11-05 14:03:44 -06:00
|
|
|
if (!result) {
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
2013-08-13 15:10:21 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
result->update_callback = NULL;
|
2013-11-11 16:10:22 -06:00
|
|
|
result->malloc = malloc;
|
|
|
|
result->realloc = realloc;
|
|
|
|
result->free = free;
|
2013-10-15 16:28:23 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
result->event_base_sync = event_base_new();
|
|
|
|
result->unbound_sync = ub_ctx_create_event(result->event_base_sync);
|
|
|
|
/* create the async one also so options are kept up to date */
|
|
|
|
result->unbound_async = ub_ctx_create_event(result->event_base_sync);
|
|
|
|
result->event_base_async = NULL;
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
result->resolution_type_set = 0;
|
2013-10-15 16:28:23 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
result->outbound_requests = ldns_rbtree_create(transaction_id_cmp);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
result->resolution_type = GETDNS_CONTEXT_RECURSING;
|
2013-11-11 16:10:22 -06:00
|
|
|
result->namespaces = create_default_namespaces(result);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
result->timeout = 5000;
|
|
|
|
result->follow_redirects = GETDNS_CONTEXT_FOLLOW_REDIRECTS;
|
|
|
|
result->dns_root_servers = create_default_root_servers();
|
|
|
|
result->append_name = GETDNS_CONTEXT_APPEND_NAME_ALWAYS;
|
|
|
|
result->suffix = NULL;
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
result->dnssec_trust_anchors = NULL;
|
|
|
|
result->upstream_list = NULL;
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
result->edns_extended_rcode = 0;
|
|
|
|
result->edns_version = 0;
|
|
|
|
result->edns_do_bit = 0;
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
if (set_from_os) {
|
|
|
|
if (GETDNS_RETURN_GOOD != set_os_defaults(result)) {
|
|
|
|
getdns_context_destroy(result);
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
*context = result;
|
2013-07-15 16:42:37 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
/* other opts */
|
|
|
|
getdns_context_set_dnssec_allowed_skew(result, 0);
|
|
|
|
getdns_context_set_edns_maximum_udp_payload_size(result, 512);
|
|
|
|
getdns_context_set_dns_transport(result,
|
|
|
|
GETDNS_CONTEXT_UDP_FIRST_AND_FALL_BACK_TO_TCP);
|
2013-10-15 16:28:23 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_create */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
2013-11-11 16:10:22 -06:00
|
|
|
/*
|
|
|
|
* getdns_context_create
|
|
|
|
*
|
|
|
|
* Call this to initialize the context that is used in other getdns calls.
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_create(struct getdns_context ** context, int set_from_os)
|
2013-11-11 16:10:22 -06:00
|
|
|
{
|
|
|
|
return getdns_context_create_with_memory_functions(context,
|
|
|
|
set_from_os, malloc, realloc, free);
|
|
|
|
} /* getdns_context_create */
|
|
|
|
|
|
|
|
|
2013-07-15 16:42:37 -05:00
|
|
|
/*
|
|
|
|
* getdns_context_destroy
|
|
|
|
*
|
2013-11-06 12:32:05 -06:00
|
|
|
* Call this to dispose of resources associated with a context once you
|
|
|
|
* are done with it.
|
2013-07-15 16:42:37 -05:00
|
|
|
*/
|
|
|
|
void
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_destroy(struct getdns_context *context)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
if (context == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-11 16:10:22 -06:00
|
|
|
if (context->namespaces)
|
|
|
|
GETDNS_FREE(context, context->namespaces);
|
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_list_destroy(context->dns_root_servers);
|
|
|
|
getdns_list_destroy(context->suffix);
|
|
|
|
getdns_list_destroy(context->dnssec_trust_anchors);
|
|
|
|
getdns_list_destroy(context->upstream_list);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
/* destroy the ub context */
|
|
|
|
ub_ctx_delete(context->unbound_async);
|
|
|
|
ub_ctx_delete(context->unbound_sync);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
event_base_free(context->event_base_sync);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
ldns_rbtree_free(context->outbound_requests);
|
2013-08-14 16:19:06 -05:00
|
|
|
|
2013-11-11 16:10:22 -06:00
|
|
|
GETDNS_FREE(context, context);
|
2013-11-05 14:03:44 -06:00
|
|
|
return;
|
|
|
|
} /* getdns_context_destroy */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_context_set_context_update_callback
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_context_update_callback(struct getdns_context *context,
|
|
|
|
void (*value) (struct getdns_context *context, uint16_t changed_item)
|
2013-11-05 14:03:44 -06:00
|
|
|
)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
context->update_callback = value;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_context_update_callback */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
2013-10-15 16:28:23 -05:00
|
|
|
/*
|
|
|
|
* Helpers to set options on the unbound ctx
|
|
|
|
*/
|
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
static void
|
2013-12-06 08:54:06 -06:00
|
|
|
set_ub_string_opt(struct getdns_context *ctx, char *opt, char *value)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
|
|
|
ub_ctx_set_option(ctx->unbound_sync, opt, value);
|
|
|
|
ub_ctx_set_option(ctx->unbound_async, opt, value);
|
2013-10-15 16:28:23 -05:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
static void
|
2013-12-06 08:54:06 -06:00
|
|
|
set_ub_number_opt(struct getdns_context *ctx, char *opt, uint16_t value)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
|
|
|
char buffer[64];
|
|
|
|
snprintf(buffer, 64, "%hu", value);
|
|
|
|
set_ub_string_opt(ctx, opt, buffer);
|
2013-10-15 16:28:23 -05:00
|
|
|
}
|
|
|
|
|
2013-10-16 13:45:43 -05:00
|
|
|
/*
|
|
|
|
* Clear the resolution type set flag if needed
|
|
|
|
*/
|
2013-11-05 14:03:44 -06:00
|
|
|
static inline void
|
2013-12-06 08:54:06 -06:00
|
|
|
clear_resolution_type_set_flag(struct getdns_context *context, uint16_t type)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
|
|
|
if (context->resolution_type_set == type) {
|
|
|
|
context->resolution_type_set = 0;
|
|
|
|
}
|
2013-10-16 13:45:43 -05:00
|
|
|
}
|
|
|
|
|
2013-07-15 16:42:37 -05:00
|
|
|
/*
|
|
|
|
* getdns_context_set_context_update
|
2013-10-16 17:33:12 -05:00
|
|
|
*
|
2013-07-15 16:42:37 -05:00
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_context_update(struct getdns_context *context, uint16_t value)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
UNUSED_PARAM(context);
|
|
|
|
UNUSED_PARAM(value);
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_context_update */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
2013-08-12 16:38:05 -05:00
|
|
|
/**
|
|
|
|
* Helper to dispatch the updated callback
|
|
|
|
*/
|
2013-11-05 14:03:44 -06:00
|
|
|
static void
|
2013-12-06 08:54:06 -06:00
|
|
|
dispatch_updated(struct getdns_context *context, uint16_t item)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
|
|
|
if (context->update_callback) {
|
|
|
|
context->update_callback(context, item);
|
|
|
|
}
|
2013-08-12 16:38:05 -05:00
|
|
|
}
|
|
|
|
|
2013-07-15 16:42:37 -05:00
|
|
|
/*
|
|
|
|
* getdns_context_set_resolution_type
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_resolution_type(struct getdns_context *context, uint16_t value)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
if (value != GETDNS_CONTEXT_STUB && value != GETDNS_CONTEXT_RECURSING) {
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
context->resolution_type = value;
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_RESOLUTION_TYPE);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_resolution_type */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_context_set_namespaces
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_namespaces(struct getdns_context *context,
|
2013-11-05 14:03:44 -06:00
|
|
|
size_t namespace_count, uint16_t * namespaces)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
if (namespace_count == 0 || namespaces == NULL) {
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-08-12 16:38:05 -05:00
|
|
|
/** clean up old namespaces **/
|
2013-11-11 16:10:22 -06:00
|
|
|
GETDNS_FREE(context, context->namespaces);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-08-12 16:38:05 -05:00
|
|
|
/** duplicate **/
|
2013-11-11 16:10:22 -06:00
|
|
|
context->namespaces = GETDNS_XMALLOC(context, uint16_t,
|
|
|
|
namespace_count);
|
|
|
|
memcpy(context->namespaces, namespaces,
|
|
|
|
namespace_count * sizeof(uint16_t));
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_NAMESPACES);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_namespaces */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_context_set_dns_transport
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_dns_transport(struct getdns_context *context, uint16_t value)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
|
|
|
|
|
|
|
switch (value) {
|
|
|
|
case GETDNS_CONTEXT_UDP_FIRST_AND_FALL_BACK_TO_TCP:
|
|
|
|
set_ub_string_opt(context, "do-udp", "yes");
|
|
|
|
set_ub_string_opt(context, "do-tcp", "yes");
|
|
|
|
break;
|
|
|
|
case GETDNS_CONTEXT_UDP_ONLY:
|
|
|
|
set_ub_string_opt(context, "do-udp", "yes");
|
|
|
|
set_ub_string_opt(context, "do-tcp", "no");
|
|
|
|
break;
|
|
|
|
case GETDNS_CONTEXT_TCP_ONLY:
|
|
|
|
set_ub_string_opt(context, "do-udp", "no");
|
|
|
|
set_ub_string_opt(context, "do-tcp", "yes");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* TODO GETDNS_CONTEXT_TCP_ONLY_KEEP_CONNECTIONS_OPEN */
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_DNS_TRANSPORT);
|
|
|
|
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_dns_transport */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_context_set_limit_outstanding_queries
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_limit_outstanding_queries(struct getdns_context *context,
|
2013-11-05 14:03:44 -06:00
|
|
|
uint16_t limit)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
/* num-queries-per-thread */
|
|
|
|
set_ub_number_opt(context, "num-queries-per-thread", limit);
|
2013-10-15 16:28:23 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
dispatch_updated(context,
|
|
|
|
GETDNS_CONTEXT_CODE_LIMIT_OUTSTANDING_QUERIES);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_limit_outstanding_queries */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_context_set_timeout
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_timeout(struct getdns_context *context, uint16_t timeout)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
context->timeout = timeout;
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_TIMEOUT);
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_timeout */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_context_set_follow_redirects
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_follow_redirects(struct getdns_context *context, uint16_t value)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
context->follow_redirects = value;
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
clear_resolution_type_set_flag(context, GETDNS_CONTEXT_RECURSING);
|
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_FOLLOW_REDIRECTS);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_follow_redirects */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_context_set_dns_root_servers
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_dns_root_servers(struct getdns_context *context,
|
2013-11-05 14:03:44 -06:00
|
|
|
struct getdns_list * addresses)
|
|
|
|
{
|
2013-12-06 08:54:06 -06:00
|
|
|
struct getdns_list *copy = NULL;
|
2013-11-05 14:03:44 -06:00
|
|
|
size_t count = 0;
|
|
|
|
if (addresses != NULL) {
|
|
|
|
if (getdns_list_copy(addresses, ©) != GETDNS_RETURN_GOOD) {
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
|
|
|
addresses = copy;
|
|
|
|
getdns_list_get_length(addresses, &count);
|
|
|
|
if (count == 0) {
|
|
|
|
getdns_list_destroy(addresses);
|
|
|
|
addresses = NULL;
|
|
|
|
} else {
|
|
|
|
size_t i = 0;
|
|
|
|
getdns_return_t r = GETDNS_RETURN_GOOD;
|
|
|
|
/* validate and add ip str */
|
|
|
|
for (i = 0; i < count; ++i) {
|
2013-12-06 08:54:06 -06:00
|
|
|
struct getdns_dict *dict = NULL;
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_list_get_dict(addresses, i, &dict);
|
|
|
|
r = add_ip_str(dict);
|
|
|
|
if (r != GETDNS_RETURN_GOOD) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (r != GETDNS_RETURN_GOOD) {
|
|
|
|
getdns_list_destroy(addresses);
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_list_destroy(context->dns_root_servers);
|
|
|
|
context->dns_root_servers = addresses;
|
|
|
|
|
|
|
|
clear_resolution_type_set_flag(context, GETDNS_CONTEXT_RECURSING);
|
|
|
|
|
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_DNS_ROOT_SERVERS);
|
|
|
|
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_dns_root_servers */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_context_set_append_name
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_append_name(struct getdns_context *context, uint16_t value)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
if (value != GETDNS_CONTEXT_APPEND_NAME_ALWAYS &&
|
|
|
|
value !=
|
|
|
|
GETDNS_CONTEXT_APPEND_NAME_ONLY_TO_SINGLE_LABEL_AFTER_FAILURE
|
|
|
|
&& value !=
|
|
|
|
GETDNS_CONTEXT_APPEND_NAME_ONLY_TO_MULTIPLE_LABEL_NAME_AFTER_FAILURE
|
|
|
|
&& value != GETDNS_CONTEXT_DO_NOT_APPEND_NAMES) {
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
context->append_name = value;
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_APPEND_NAME);
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_append_name */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_context_set_suffix
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_suffix(struct getdns_context *context, struct getdns_list * value)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2013-12-06 08:54:06 -06:00
|
|
|
struct getdns_list *copy = NULL;
|
2013-11-05 14:03:44 -06:00
|
|
|
if (value != NULL) {
|
|
|
|
if (getdns_list_copy(value, ©) != GETDNS_RETURN_GOOD) {
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
|
|
|
value = copy;
|
|
|
|
}
|
|
|
|
getdns_list_destroy(context->suffix);
|
|
|
|
context->suffix = value;
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
clear_resolution_type_set_flag(context, GETDNS_CONTEXT_STUB);
|
2013-10-16 13:45:43 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_SUFFIX);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_suffix */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_context_set_dnssec_trust_anchors
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_dnssec_trust_anchors(struct getdns_context *context,
|
2013-11-05 14:03:44 -06:00
|
|
|
struct getdns_list * value)
|
|
|
|
{
|
2013-12-06 08:54:06 -06:00
|
|
|
struct getdns_list *copy = NULL;
|
2013-11-05 14:03:44 -06:00
|
|
|
if (value != NULL) {
|
|
|
|
if (getdns_list_copy(value, ©) != GETDNS_RETURN_GOOD) {
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
|
|
|
value = copy;
|
|
|
|
}
|
|
|
|
getdns_list_destroy(context->dnssec_trust_anchors);
|
|
|
|
context->dnssec_trust_anchors = value;
|
|
|
|
|
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_DNSSEC_TRUST_ANCHORS);
|
|
|
|
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_dnssec_trust_anchors */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_context_set_dnssec_allowed_skew
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_dnssec_allowed_skew(struct getdns_context *context,
|
2013-11-05 14:03:44 -06:00
|
|
|
uint16_t value)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
set_ub_number_opt(context, "val-sig-skew-min", value);
|
|
|
|
set_ub_number_opt(context, "val-sig-skew-max", value);
|
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_DNSSEC_ALLOWED_SKEW);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_dnssec_allowed_skew */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_context_set_stub_resolution
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_stub_resolution(struct getdns_context *context,
|
2013-11-05 14:03:44 -06:00
|
|
|
struct getdns_list * upstream_list)
|
|
|
|
{
|
|
|
|
size_t count = 0;
|
|
|
|
size_t i = 0;
|
|
|
|
getdns_return_t r = getdns_list_get_length(upstream_list, &count);
|
|
|
|
if (count == 0 || r != GETDNS_RETURN_GOOD) {
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2013-12-06 08:54:06 -06:00
|
|
|
struct getdns_list *copy = NULL;
|
2013-11-05 14:03:44 -06:00
|
|
|
if (getdns_list_copy(upstream_list, ©) != GETDNS_RETURN_GOOD) {
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
|
|
|
upstream_list = copy;
|
|
|
|
/* validate and add ip str */
|
|
|
|
for (i = 0; i < count; ++i) {
|
2013-12-06 08:54:06 -06:00
|
|
|
struct getdns_dict *dict = NULL;
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_list_get_dict(upstream_list, i, &dict);
|
|
|
|
r = add_ip_str(dict);
|
|
|
|
if (r != GETDNS_RETURN_GOOD) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r != GETDNS_RETURN_GOOD) {
|
|
|
|
getdns_list_destroy(upstream_list);
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_list_destroy(context->upstream_list);
|
|
|
|
context->upstream_list = upstream_list;
|
|
|
|
|
|
|
|
clear_resolution_type_set_flag(context, GETDNS_CONTEXT_STUB);
|
|
|
|
|
|
|
|
dispatch_updated(context,
|
|
|
|
GETDNS_CONTEXT_CODE_UPSTREAM_RECURSIVE_SERVERS);
|
|
|
|
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_stub_resolution */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_context_set_edns_maximum_udp_payload_size
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_edns_maximum_udp_payload_size(struct getdns_context *context,
|
2013-11-05 14:03:44 -06:00
|
|
|
uint16_t value)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
/* check for < 512. uint16_t won't let it go above max) */
|
|
|
|
if (value < 512) {
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
/* max-udp-size */
|
|
|
|
set_ub_number_opt(context, "max-udp-size", value);
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
dispatch_updated(context,
|
|
|
|
GETDNS_CONTEXT_CODE_EDNS_MAXIMUM_UDP_PAYLOAD_SIZE);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_edns_maximum_udp_payload_size */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_context_set_edns_extended_rcode
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_edns_extended_rcode(struct getdns_context *context, uint8_t value)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
context->edns_extended_rcode = value;
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_EXTENDED_RCODE);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_edns_extended_rcode */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_context_set_edns_version
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_edns_version(struct getdns_context *context, uint8_t value)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
context->edns_version = value;
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_VERSION);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_edns_version */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_context_set_edns_do_bit
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_edns_do_bit(struct getdns_context *context, uint8_t value)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
/* 0 or 1 */
|
|
|
|
if (value > 1) {
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
context->edns_do_bit = value;
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_DO_BIT);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_edns_do_bit */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
2013-11-11 16:10:22 -06:00
|
|
|
* getdns_context_set_memory_functions
|
2013-07-15 16:42:37 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_set_memory_functions(struct getdns_context *context,
|
2013-11-11 16:10:22 -06:00
|
|
|
void *(*malloc) (size_t),
|
|
|
|
void *(*realloc) (void *, size_t),
|
|
|
|
void (*free) (void *)
|
2013-11-05 14:03:44 -06:00
|
|
|
)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2013-11-11 16:10:22 -06:00
|
|
|
if (!context)
|
|
|
|
return GETDNS_RETURN_BAD_CONTEXT;
|
2013-07-15 16:42:37 -05:00
|
|
|
|
2013-11-11 16:10:22 -06:00
|
|
|
if (!malloc || !realloc || !free)
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
|
|
|
|
context->malloc = malloc;
|
|
|
|
context->realloc = realloc;
|
|
|
|
context->free = free;
|
|
|
|
|
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_MEMORY_FUNCTIONS);
|
|
|
|
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_memory_functions*/
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_extension_set_libevent_base
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_extension_set_libevent_base(struct getdns_context *context,
|
2013-11-05 14:03:44 -06:00
|
|
|
struct event_base * this_event_base)
|
|
|
|
{
|
|
|
|
if (this_event_base) {
|
|
|
|
ub_ctx_set_event(context->unbound_async, this_event_base);
|
|
|
|
context->event_base_async = this_event_base;
|
|
|
|
} else {
|
|
|
|
ub_ctx_set_event(context->unbound_async,
|
|
|
|
context->event_base_sync);
|
|
|
|
context->event_base_async = NULL;
|
|
|
|
}
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_extension_set_libevent_base */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
2013-10-17 18:45:25 -05:00
|
|
|
/* cancel the request */
|
2013-11-05 14:03:44 -06:00
|
|
|
static void
|
|
|
|
cancel_dns_req(getdns_dns_req * req)
|
|
|
|
{
|
|
|
|
getdns_network_req *netreq = req->first_req;
|
|
|
|
while (netreq) {
|
|
|
|
if (netreq->state == NET_REQ_IN_FLIGHT) {
|
|
|
|
/* for ev based ub, this should always prevent
|
|
|
|
* the callback from firing */
|
|
|
|
ub_cancel(req->unbound, netreq->unbound_id);
|
|
|
|
netreq->state = NET_REQ_CANCELED;
|
|
|
|
} else if (netreq->state == NET_REQ_NOT_SENT) {
|
|
|
|
netreq->state = NET_REQ_CANCELED;
|
|
|
|
}
|
|
|
|
netreq = netreq->next;
|
|
|
|
}
|
|
|
|
req->canceled = 1;
|
2013-10-17 18:45:25 -05:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_cancel_request(struct getdns_context *context,
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_transaction_t transaction_id, int fire_callback)
|
|
|
|
{
|
|
|
|
getdns_dns_req *req = NULL;
|
|
|
|
|
|
|
|
/* delete the node from the tree */
|
|
|
|
ldns_rbnode_t *node = ldns_rbtree_delete(context->outbound_requests,
|
|
|
|
&transaction_id);
|
|
|
|
|
|
|
|
if (!node) {
|
|
|
|
return GETDNS_RETURN_UNKNOWN_TRANSACTION;
|
|
|
|
}
|
|
|
|
req = (getdns_dns_req *) node->data;
|
|
|
|
/* do the cancel */
|
|
|
|
|
|
|
|
cancel_dns_req(req);
|
|
|
|
|
|
|
|
if (fire_callback) {
|
|
|
|
getdns_callback_t cb = NULL;
|
|
|
|
void *user_pointer = NULL;
|
|
|
|
|
|
|
|
cb = req->user_callback;
|
|
|
|
user_pointer = req->user_pointer;
|
|
|
|
|
|
|
|
/* clean up */
|
2013-11-11 16:10:22 -06:00
|
|
|
GETDNS_FREE(context, node);
|
2013-11-05 14:03:44 -06:00
|
|
|
dns_req_free(req);
|
|
|
|
|
|
|
|
/* fire callback */
|
|
|
|
cb(context,
|
|
|
|
GETDNS_CALLBACK_CANCEL,
|
|
|
|
NULL, user_pointer, transaction_id);
|
|
|
|
}
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-10-18 12:55:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_cancel_callback
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_cancel_callback(struct getdns_context *context,
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_transaction_t transaction_id)
|
|
|
|
{
|
|
|
|
return getdns_context_cancel_request(context, transaction_id, 1);
|
|
|
|
} /* getdns_cancel_callback */
|
|
|
|
|
|
|
|
static void
|
2013-12-06 08:54:06 -06:00
|
|
|
ub_setup_stub(struct ub_ctx *ctx, struct getdns_list * upstreams, size_t count)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
/* reset forwarding servers */
|
|
|
|
ub_ctx_set_fwd(ctx, NULL);
|
|
|
|
for (i = 0; i < count; ++i) {
|
2013-12-06 08:54:06 -06:00
|
|
|
struct getdns_dict *dict = NULL;
|
2013-11-05 14:03:44 -06:00
|
|
|
char *ip_str = NULL;
|
|
|
|
getdns_list_get_dict(upstreams, i, &dict);
|
|
|
|
getdns_dict_util_get_string(dict, GETDNS_STR_ADDRESS_STRING,
|
|
|
|
&ip_str);
|
|
|
|
ub_ctx_set_fwd(ctx, ip_str);
|
|
|
|
}
|
2013-10-16 13:45:43 -05:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
getdns_context_prepare_for_resolution(struct getdns_context *context)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
|
|
|
if (context->resolution_type_set == context->resolution_type) {
|
|
|
|
/* already set and no config changes have caused this to be
|
|
|
|
* bad.
|
|
|
|
*/
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
if (context->resolution_type == GETDNS_CONTEXT_STUB) {
|
|
|
|
size_t upstream_len = 0;
|
|
|
|
getdns_return_t r =
|
|
|
|
getdns_list_get_length(context->upstream_list,
|
|
|
|
&upstream_len);
|
|
|
|
if (r != GETDNS_RETURN_GOOD || upstream_len == 0) {
|
|
|
|
return GETDNS_RETURN_BAD_CONTEXT;
|
|
|
|
}
|
|
|
|
/* set upstreams */
|
|
|
|
ub_setup_stub(context->unbound_async, context->upstream_list,
|
|
|
|
upstream_len);
|
|
|
|
ub_setup_stub(context->unbound_sync, context->upstream_list,
|
|
|
|
upstream_len);
|
|
|
|
/* use /etc/hosts */
|
|
|
|
ub_ctx_hosts(context->unbound_sync, NULL);
|
|
|
|
ub_ctx_hosts(context->unbound_async, NULL);
|
|
|
|
|
|
|
|
} else if (context->resolution_type == GETDNS_CONTEXT_RECURSING) {
|
|
|
|
/* set recursive */
|
|
|
|
/* TODO: use the root servers via root hints file */
|
|
|
|
ub_ctx_set_fwd(context->unbound_async, NULL);
|
|
|
|
ub_ctx_set_fwd(context->unbound_sync, NULL);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* bogus? */
|
|
|
|
return GETDNS_RETURN_BAD_CONTEXT;
|
|
|
|
}
|
|
|
|
context->resolution_type_set = context->resolution_type;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-10-16 13:45:43 -05:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_return_t
|
|
|
|
getdns_context_track_outbound_request(getdns_dns_req * req)
|
|
|
|
{
|
|
|
|
if (!req) {
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
2013-12-06 08:54:06 -06:00
|
|
|
struct getdns_context *context = req->context;
|
2013-11-11 16:10:22 -06:00
|
|
|
ldns_rbnode_t *node = GETDNS_MALLOC(context, ldns_rbnode_t);
|
2013-11-05 14:03:44 -06:00
|
|
|
if (!node) {
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
node->key = &(req->trans_id);
|
|
|
|
node->data = req;
|
|
|
|
if (!ldns_rbtree_insert(context->outbound_requests, node)) {
|
|
|
|
/* free the node */
|
2013-11-11 16:10:22 -06:00
|
|
|
GETDNS_FREE(context, node);
|
2013-11-05 14:03:44 -06:00
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-10-16 17:33:12 -05:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_return_t
|
|
|
|
getdns_context_clear_outbound_request(getdns_dns_req * req)
|
|
|
|
{
|
|
|
|
if (!req) {
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
2013-12-06 08:54:06 -06:00
|
|
|
struct getdns_context *context = req->context;
|
2013-11-05 14:03:44 -06:00
|
|
|
ldns_rbnode_t *node = ldns_rbtree_delete(context->outbound_requests,
|
|
|
|
&(req->trans_id));
|
|
|
|
if (node) {
|
2013-11-11 16:10:22 -06:00
|
|
|
GETDNS_FREE(context, node);
|
2013-11-05 14:03:44 -06:00
|
|
|
}
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-10-16 17:33:12 -05:00
|
|
|
}
|
|
|
|
|
2013-11-11 16:10:22 -06:00
|
|
|
char *
|
2013-11-12 10:00:19 -06:00
|
|
|
getdns_strdup(void *(*malloc)(size_t), const char *s)
|
2013-11-11 16:10:22 -06:00
|
|
|
{
|
|
|
|
size_t sz = strlen(s) + 1;
|
2013-11-12 10:00:19 -06:00
|
|
|
char *r = (char *)(*malloc)(sizeof(char) * sz);
|
2013-11-11 16:10:22 -06:00
|
|
|
if (r)
|
|
|
|
return memcpy(r, s, sz);
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct getdns_bindata *
|
2013-11-12 10:00:19 -06:00
|
|
|
getdns_bindata_copy(void *(*malloc)(size_t), void (*free)(void *),
|
2013-11-11 16:10:22 -06:00
|
|
|
const struct getdns_bindata *src)
|
|
|
|
{
|
|
|
|
struct getdns_bindata *dst;
|
|
|
|
|
|
|
|
if (!src)
|
|
|
|
return NULL;
|
|
|
|
|
2013-11-12 10:00:19 -06:00
|
|
|
dst = (struct getdns_bindata *)(*malloc)(sizeof(struct getdns_bindata));
|
2013-11-11 16:10:22 -06:00
|
|
|
if (!dst)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
dst->size = src->size;
|
2013-11-12 10:00:19 -06:00
|
|
|
dst->data = (uint8_t *)(*malloc)(sizeof(uint8_t) * src->size);
|
2013-11-11 16:10:22 -06:00
|
|
|
if (!dst->data) {
|
2013-11-12 10:00:19 -06:00
|
|
|
(*free)(dst);
|
2013-11-11 16:10:22 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
(void) memcpy(dst->data, src->data, src->size);
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-12 10:00:19 -06:00
|
|
|
getdns_bindata_destroy(void (*free)(void *), struct getdns_bindata *bindata)
|
2013-11-11 16:10:22 -06:00
|
|
|
{
|
|
|
|
if (!bindata)
|
|
|
|
return;
|
2013-11-12 10:00:19 -06:00
|
|
|
(*free)(bindata->data);
|
|
|
|
(*free)(bindata);
|
2013-11-11 16:10:22 -06:00
|
|
|
}
|
2013-07-15 16:42:37 -05:00
|
|
|
/* getdns_context.c */
|