2013-07-15 16:42:37 -05:00
|
|
|
/**
|
|
|
|
*
|
2014-01-28 08:30:01 -06:00
|
|
|
* \file context.c
|
|
|
|
* @brief getdns context management functions
|
2013-10-16 17:33:12 -05:00
|
|
|
*
|
2014-02-03 14:39:09 -06:00
|
|
|
* Declarations taken from the getdns API description pseudo implementation.
|
2013-07-15 16:42:37 -05:00
|
|
|
*
|
|
|
|
*/
|
2014-01-28 08:30:01 -06:00
|
|
|
|
|
|
|
/*
|
2014-02-25 07:12:33 -06:00
|
|
|
* Copyright (c) 2013, NLnet Labs, Verisign, Inc.
|
2014-01-28 08:30:01 -06:00
|
|
|
* All rights reserved.
|
2013-10-16 17:33:12 -05:00
|
|
|
*
|
2014-01-28 08:30:01 -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
|
2014-01-28 08:30:01 -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-16 17:33:12 -05:00
|
|
|
*
|
2014-01-28 08:30:01 -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-15 16:42:37 -05:00
|
|
|
*/
|
|
|
|
|
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>
|
2014-02-10 18:59:45 -06:00
|
|
|
#include <sys/stat.h>
|
2014-01-27 16:05:25 -06:00
|
|
|
#include <sys/time.h>
|
2013-11-06 12:32:05 -06:00
|
|
|
#include <unbound.h>
|
2014-03-11 10:43:41 -05:00
|
|
|
#include <assert.h>
|
2013-11-06 12:32:05 -06:00
|
|
|
|
2014-05-19 08:50:34 -05:00
|
|
|
#include "config.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"
|
2014-02-19 09:56:00 -06:00
|
|
|
#include "dnssec.h"
|
2013-11-06 12:32:05 -06:00
|
|
|
|
2013-12-08 15:56:34 -06:00
|
|
|
void *plain_mem_funcs_user_arg = MF_PLAIN;
|
|
|
|
|
2014-09-23 06:36:26 -05:00
|
|
|
struct host_name_addr_type {
|
|
|
|
ldns_rdf * host_name;
|
|
|
|
ldns_rr_type addr_type;
|
|
|
|
};
|
|
|
|
|
2013-11-06 12:32:05 -06:00
|
|
|
/* Private functions */
|
2014-02-10 18:59:45 -06:00
|
|
|
getdns_return_t create_default_namespaces(struct getdns_context *context);
|
2014-09-23 06:36:26 -05:00
|
|
|
getdns_return_t create_local_hosts(struct getdns_context *context);
|
2014-02-20 15:17:41 -06:00
|
|
|
static struct getdns_list *create_default_root_servers(void);
|
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 *);
|
2014-01-27 16:05:25 -06:00
|
|
|
static int timeout_cmp(const void *, const void *);
|
2014-09-23 06:36:26 -05:00
|
|
|
static int local_host_cmp(const void *, const void *);
|
2013-12-06 08:54:06 -06:00
|
|
|
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 *);
|
2014-02-03 16:38:06 -06:00
|
|
|
static void cancel_outstanding_requests(struct getdns_context*, int);
|
2013-07-15 16:42:37 -05:00
|
|
|
|
2014-02-19 13:56:37 -06:00
|
|
|
/* unbound helpers */
|
|
|
|
static getdns_return_t rebuild_ub_ctx(struct getdns_context* context);
|
|
|
|
static void set_ub_string_opt(struct getdns_context *, char *, char *);
|
|
|
|
static void set_ub_number_opt(struct getdns_context *, char *, uint16_t);
|
|
|
|
static getdns_return_t set_ub_dns_transport(struct getdns_context*, getdns_transport_t);
|
|
|
|
static void set_ub_limit_outstanding_queries(struct getdns_context*,
|
|
|
|
uint16_t);
|
|
|
|
static void set_ub_dnssec_allowed_skew(struct getdns_context*, uint32_t);
|
|
|
|
static void set_ub_edns_maximum_udp_payload_size(struct getdns_context*,
|
|
|
|
uint16_t);
|
|
|
|
|
2014-09-12 07:44:53 -05:00
|
|
|
/* ldns helpers */
|
|
|
|
static getdns_return_t set_ldns_dns_transport(struct getdns_context* context,
|
|
|
|
getdns_transport_t value);
|
|
|
|
static void set_ldns_edns_maximum_udp_payload_size(struct getdns_context*,
|
|
|
|
uint16_t);
|
2014-09-13 11:19:25 -05:00
|
|
|
static getdns_return_t set_ldns_nameservers(struct getdns_context*,
|
|
|
|
struct getdns_list * upstreams);
|
2014-02-19 13:56:37 -06:00
|
|
|
|
2013-11-06 12:32:05 -06:00
|
|
|
/* Stuff to make it compile pedantically */
|
2013-12-09 11:55:33 -06:00
|
|
|
#define RETURN_IF_NULL(ptr, code) if(ptr == NULL) return code;
|
2013-07-15 16:42:37 -05:00
|
|
|
|
2013-08-12 16:38:05 -05:00
|
|
|
/**
|
|
|
|
* Helper to get default lookup namespaces.
|
|
|
|
* TODO: Determine from OS
|
|
|
|
*/
|
2014-02-10 18:59:45 -06:00
|
|
|
getdns_return_t
|
2013-12-06 08:54:06 -06:00
|
|
|
create_default_namespaces(struct getdns_context *context)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2014-02-10 18:59:45 -06:00
|
|
|
context->namespaces = GETDNS_XMALLOC(context->my_mf, getdns_namespace_t, 2);
|
|
|
|
if(context->namespaces == NULL)
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
|
|
|
|
context->namespaces[0] = GETDNS_NAMESPACE_LOCALNAMES;
|
|
|
|
context->namespaces[1] = GETDNS_NAMESPACE_DNS;
|
|
|
|
context->namespace_count = 2;
|
|
|
|
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-08-12 16:38:05 -05:00
|
|
|
}
|
|
|
|
|
2014-09-23 06:36:26 -05:00
|
|
|
/**
|
|
|
|
* Helper to get contents from hosts file
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
|
|
|
create_local_hosts(struct getdns_context *context)
|
|
|
|
{
|
|
|
|
|
|
|
|
ldns_rr_list * host_names = ldns_get_rr_list_hosts_frm_file(NULL);
|
|
|
|
if (host_names == NULL)
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
|
|
|
|
/*TODO: free up memory on error paths*/
|
|
|
|
//ldns_rr_list_print(stderr, host_names);
|
|
|
|
|
|
|
|
/* We have a 1:1 list of name -> ip address where there is an
|
|
|
|
underlying many to many relationship. Need to create a lookup of
|
|
|
|
(unique name + A/AAAA)-> list of IPV4/IPv6 ip addresses*/
|
|
|
|
for (int i = 0 ; i<ldns_rr_list_rr_count(host_names) ; i++) {
|
|
|
|
|
|
|
|
ldns_rr *rr = ldns_rr_list_rr(host_names, i);
|
|
|
|
ldns_rdf *owner = ldns_rdf_clone(ldns_rr_owner(rr));
|
|
|
|
|
|
|
|
/*Check to see if we already have an entry*/
|
|
|
|
struct host_name_addr_type *lh_key =
|
|
|
|
GETDNS_MALLOC(context->my_mf, struct host_name_addr_type);
|
|
|
|
if (lh_key == NULL)
|
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
lh_key->host_name = owner;
|
|
|
|
lh_key->addr_type = ldns_rr_get_type(rr);
|
|
|
|
ldns_rbnode_t *result_node = ldns_rbtree_search(context->local_hosts, lh_key);
|
|
|
|
if (result_node) {
|
|
|
|
if (!ldns_rr_list_push_rr ((ldns_rr_list *)result_node->data, ldns_rr_clone(rr)))
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ldns_rr_list *address_list = ldns_rr_list_new ();
|
|
|
|
if (!ldns_rr_list_push_rr (address_list, ldns_rr_clone(rr)))
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
|
|
|
|
ldns_rbnode_t *node = GETDNS_MALLOC(context->my_mf, ldns_rbnode_t);
|
|
|
|
if (!node) {
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
node->key = lh_key;
|
|
|
|
node->data = address_list;
|
|
|
|
if (!ldns_rbtree_insert(context->local_hosts, node)) {
|
|
|
|
/* free the node */
|
|
|
|
GETDNS_FREE(context->my_mf, node);
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
return NULL;
|
2013-08-12 16:38:05 -05:00
|
|
|
}
|
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
#define IP_STR_BUFF_LEN 512
|
|
|
|
|
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
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
struct sockaddr_storage storage;
|
|
|
|
uint32_t port = 0;
|
|
|
|
char buff[IP_STR_BUFF_LEN];
|
|
|
|
memset(buff, 0, IP_STR_BUFF_LEN);
|
|
|
|
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, IP_STR_BUFF_LEN);
|
|
|
|
if (!ipStr) {
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
r = getdns_dict_get_int(ip, GETDNS_STR_PORT, &port);
|
|
|
|
if (r == GETDNS_RETURN_GOOD && port > 0) {
|
|
|
|
size_t addrLen = strlen(ipStr);
|
|
|
|
/* append @ and port */
|
|
|
|
buff[addrLen] = '@';
|
|
|
|
++addrLen;
|
|
|
|
snprintf(buff + addrLen, IP_STR_BUFF_LEN - addrLen, "%d", port);
|
|
|
|
}
|
|
|
|
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, IP_STR_BUFF_LEN);
|
|
|
|
if (!ipStr) {
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
r = getdns_dict_get_int(ip, GETDNS_STR_PORT, &port);
|
|
|
|
if (r == GETDNS_RETURN_GOOD && port > 0) {
|
|
|
|
size_t addrLen = strlen(ipStr);
|
|
|
|
/* append @ and port */
|
|
|
|
buff[addrLen] = '@';
|
|
|
|
++addrLen;
|
|
|
|
snprintf(buff + addrLen, IP_STR_BUFF_LEN - addrLen, "%d", port);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-10 18:59:45 -06:00
|
|
|
/**
|
|
|
|
* check a file for changes since the last check
|
|
|
|
* and refresh the current data if changes are detected
|
2014-05-21 09:21:45 -05:00
|
|
|
* @param context pointer to a previously created context to be used for this call
|
|
|
|
* @param fchg file to check
|
2014-02-10 18:59:45 -06:00
|
|
|
* @returns changes as OR'd list of GETDNS_FCHG_* values
|
|
|
|
* @returns GETDNS_FCHG_NONE if no changes
|
|
|
|
* @returns GETDNS_FCHG_ERRORS if problems (see fchg->errors for details)
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
filechg_check(struct getdns_context *context, struct filechg *fchg)
|
|
|
|
{
|
|
|
|
struct stat *finfo;
|
|
|
|
|
|
|
|
if(fchg == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fchg->errors = GETDNS_FCHG_NOERROR;
|
|
|
|
fchg->changes = GETDNS_FCHG_NOCHANGES;
|
|
|
|
|
|
|
|
finfo = GETDNS_MALLOC(context->my_mf, struct stat);
|
|
|
|
if(finfo == NULL)
|
|
|
|
{
|
|
|
|
fchg->errors = errno;
|
|
|
|
return GETDNS_FCHG_ERRORS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(stat(fchg->fn, finfo) != 0)
|
|
|
|
{
|
|
|
|
GETDNS_FREE(context->my_mf, finfo);
|
|
|
|
fchg->errors = errno;
|
|
|
|
return GETDNS_FCHG_ERRORS;
|
|
|
|
}
|
|
|
|
|
2014-02-19 12:15:27 -06:00
|
|
|
/* we want to consider a file that previously returned error for stat() as a
|
2014-02-10 18:59:45 -06:00
|
|
|
change */
|
|
|
|
|
|
|
|
if(fchg->prevstat == NULL)
|
|
|
|
fchg->changes = GETDNS_FCHG_MTIME | GETDNS_FCHG_CTIME;
|
|
|
|
else
|
|
|
|
{
|
2014-02-11 02:57:19 -06:00
|
|
|
if(fchg->prevstat->st_mtime != finfo->st_mtime)
|
2014-02-10 18:59:45 -06:00
|
|
|
fchg->changes |= GETDNS_FCHG_MTIME;
|
2014-02-11 02:57:19 -06:00
|
|
|
if(fchg->prevstat->st_ctime != finfo->st_ctime)
|
2014-02-10 18:59:45 -06:00
|
|
|
fchg->changes |= GETDNS_FCHG_CTIME;
|
|
|
|
GETDNS_FREE(context->my_mf, fchg->prevstat);
|
|
|
|
}
|
|
|
|
fchg->prevstat = finfo;
|
|
|
|
|
|
|
|
return fchg->changes;
|
|
|
|
} /* filechg */
|
|
|
|
|
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
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
ldns_rdf_type rt = ldns_rdf_get_type(rdf);
|
|
|
|
size_t sz = ldns_rdf_size(rdf);
|
|
|
|
struct getdns_dict *result = getdns_dict_create_with_context(context);
|
|
|
|
/* 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 */
|
|
|
|
struct getdns_bindata data_bin = { sz, ldns_rdf_data(rdf) };
|
|
|
|
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
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
size_t i = 0;
|
|
|
|
size_t idx = 0;
|
|
|
|
struct getdns_list *result = getdns_list_create_with_context(context);
|
|
|
|
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:
|
|
|
|
{
|
|
|
|
struct getdns_dict *ipaddr =
|
|
|
|
create_ipaddr_dict_from_rdf(context, rdf);
|
|
|
|
getdns_list_add_item(result, &idx);
|
|
|
|
getdns_list_set_dict(result, idx, ipaddr);
|
|
|
|
getdns_dict_destroy(ipaddr);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LDNS_RDF_TYPE_DNAME:
|
|
|
|
{
|
|
|
|
struct getdns_bindata item;
|
|
|
|
char *srch = ldns_rdf2str(rdf);
|
|
|
|
item.size = strlen(srch) + 1;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-10 18:59:45 -06:00
|
|
|
/*---------------------------------------- set_os_defaults
|
|
|
|
we use ldns to read the resolv.conf file - the ldns resolver is
|
|
|
|
destroyed once the file is read
|
|
|
|
*/
|
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
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
ldns_resolver *lr = NULL;
|
2014-02-10 18:59:45 -06:00
|
|
|
ldns_rdf **rdf_list;
|
|
|
|
size_t rdf_list_sz;
|
|
|
|
|
|
|
|
if (ldns_resolver_new_frm_file(&lr, NULL) != LDNS_STATUS_OK)
|
2014-02-06 21:40:36 -06:00
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
2014-02-10 18:59:45 -06:00
|
|
|
|
|
|
|
if(context->fchg_resolvconf == NULL)
|
|
|
|
{
|
2014-02-20 15:17:41 -06:00
|
|
|
context->fchg_resolvconf = GETDNS_MALLOC(context->my_mf, struct filechg);
|
2014-02-10 18:59:45 -06:00
|
|
|
if(context->fchg_resolvconf == NULL)
|
2014-02-20 15:17:41 -06:00
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
2014-02-10 18:59:45 -06:00
|
|
|
context->fchg_resolvconf->fn = "/etc/resolv.conf";
|
|
|
|
context->fchg_resolvconf->prevstat = NULL;
|
|
|
|
context->fchg_resolvconf->changes = GETDNS_FCHG_NOCHANGES;
|
|
|
|
context->fchg_resolvconf->errors = GETDNS_FCHG_NOERROR;
|
|
|
|
}
|
|
|
|
filechg_check(context, context->fchg_resolvconf);
|
|
|
|
|
|
|
|
rdf_list = ldns_resolver_nameservers(lr);
|
|
|
|
rdf_list_sz = ldns_resolver_nameserver_count(lr);
|
2014-02-06 21:40:36 -06:00
|
|
|
if (rdf_list_sz > 0) {
|
|
|
|
context->upstream_list =
|
|
|
|
create_from_ldns_list(context, rdf_list, rdf_list_sz);
|
|
|
|
}
|
2014-02-10 18:59:45 -06:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
rdf_list = ldns_resolver_searchlist(lr);
|
|
|
|
rdf_list_sz = ldns_resolver_searchlist_count(lr);
|
|
|
|
if (rdf_list_sz > 0) {
|
|
|
|
context->suffix = create_from_ldns_list(context, rdf_list,
|
|
|
|
rdf_list_sz);
|
|
|
|
}
|
|
|
|
ldns_resolver_deep_free(lr);
|
2014-02-03 14:39:09 -06:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2014-02-03 14:39:09 -06:00
|
|
|
} /* set_os_defaults */
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2014-01-31 13:43:02 -06:00
|
|
|
/* compare of transaction ids in DESCENDING order
|
|
|
|
so that 0 comes last
|
|
|
|
*/
|
2013-11-05 14:03:44 -06:00
|
|
|
static int
|
|
|
|
transaction_id_cmp(const void *id1, const void *id2)
|
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-10 18:59:45 -06:00
|
|
|
static int
|
|
|
|
timeout_cmp(const void *to1, const void *to2)
|
|
|
|
{
|
2014-01-27 16:05:25 -06:00
|
|
|
if (to1 == NULL && to2 == NULL) {
|
|
|
|
return 0;
|
|
|
|
} else if (to1 == NULL && to2 != NULL) {
|
|
|
|
return 1;
|
|
|
|
} else if (to1 != NULL && to2 == NULL) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
2014-01-31 13:43:02 -06:00
|
|
|
const getdns_timeout_data_t* t1 = (const getdns_timeout_data_t*) to1;
|
|
|
|
const getdns_timeout_data_t* t2 = (const getdns_timeout_data_t*) to2;
|
2014-01-27 16:05:25 -06:00
|
|
|
if (t1->timeout_time.tv_sec < t2->timeout_time.tv_sec) {
|
|
|
|
return -1;
|
|
|
|
} else if (t1->timeout_time.tv_sec > t2->timeout_time.tv_sec) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
/* compare usec.. */
|
|
|
|
if (t1->timeout_time.tv_usec < t2->timeout_time.tv_usec) {
|
|
|
|
return -1;
|
|
|
|
} else if (t1->timeout_time.tv_usec > t2->timeout_time.tv_usec) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
2014-01-31 13:43:02 -06:00
|
|
|
return transaction_id_cmp(&t1->transaction_id, &t2->transaction_id);
|
2014-01-27 16:05:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 06:36:26 -05:00
|
|
|
static int
|
|
|
|
local_host_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 {
|
|
|
|
const struct host_name_addr_type *hn1 = (const struct host_name_addr_type*) id1;
|
|
|
|
const struct host_name_addr_type *hn2 = (const struct host_name_addr_type*) id2;
|
|
|
|
if ((ldns_rr_type) hn1->addr_type < (ldns_rr_type) hn2->addr_type)
|
|
|
|
return -1;
|
|
|
|
if ((ldns_rr_type) hn1->addr_type > (ldns_rr_type) hn2->addr_type)
|
|
|
|
return 1;
|
|
|
|
return (ldns_rdf_compare((const ldns_rdf *) hn1->host_name,
|
|
|
|
(const ldns_rdf *) hn2->host_name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 16:05:15 -06:00
|
|
|
static ldns_rbtree_t*
|
|
|
|
create_ldns_rbtree(getdns_context * context,
|
|
|
|
int(*cmpf)(const void *, const void *)) {
|
|
|
|
ldns_rbtree_t* result = GETDNS_MALLOC(context->mf, ldns_rbtree_t);
|
|
|
|
if (!result) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ldns_rbtree_init(result, cmpf);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
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-08 17:52:38 -06:00
|
|
|
getdns_context_create_with_extended_memory_functions(
|
|
|
|
struct getdns_context ** context,
|
2013-11-11 16:10:22 -06:00
|
|
|
int set_from_os,
|
2013-12-08 17:52:38 -06:00
|
|
|
void *userarg,
|
|
|
|
void *(*malloc)(void *userarg, size_t),
|
|
|
|
void *(*realloc)(void *userarg, void *, size_t),
|
|
|
|
void (*free)(void *userarg, void *)
|
2013-11-11 16:10:22 -06:00
|
|
|
)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
struct getdns_context *result = NULL;
|
|
|
|
mf_union mf;
|
2013-08-12 18:47:33 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
if (!context || !malloc || !realloc || !free)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2013-10-15 16:28:23 -05:00
|
|
|
/** default init **/
|
2014-02-06 21:40:36 -06:00
|
|
|
mf.ext.malloc = malloc;
|
|
|
|
result = userarg == MF_PLAIN
|
|
|
|
? (*mf.pln.malloc)( sizeof(struct getdns_context))
|
|
|
|
: (*mf.ext.malloc)(userarg, sizeof(struct getdns_context));
|
|
|
|
if (!result) {
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
2014-03-04 16:00:18 -06:00
|
|
|
result->processing = 0;
|
2014-02-20 15:17:41 -06:00
|
|
|
result->destroying = 0;
|
2014-02-06 21:40:36 -06:00
|
|
|
result->my_mf.mf_arg = userarg;
|
|
|
|
result->my_mf.mf.ext.malloc = malloc;
|
|
|
|
result->my_mf.mf.ext.realloc = realloc;
|
|
|
|
result->my_mf.mf.ext.free = free;
|
2013-08-13 15:10:21 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
result->update_callback = NULL;
|
2013-12-08 15:56:34 -06:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
result->mf.mf_arg = userarg;
|
|
|
|
result->mf.mf.ext.malloc = malloc;
|
|
|
|
result->mf.mf.ext.realloc = realloc;
|
|
|
|
result->mf.mf.ext.free = free;
|
2013-10-15 16:28:23 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
result->resolution_type_set = 0;
|
2013-10-15 16:28:23 -05:00
|
|
|
|
2014-02-20 16:05:15 -06:00
|
|
|
result->outbound_requests = create_ldns_rbtree(result, transaction_id_cmp);
|
|
|
|
result->timeouts_by_time = create_ldns_rbtree(result, timeout_cmp);
|
|
|
|
result->timeouts_by_id = create_ldns_rbtree(result, transaction_id_cmp);
|
2014-09-23 06:36:26 -05:00
|
|
|
result->local_hosts = create_ldns_rbtree(result, local_host_cmp);
|
2014-02-20 16:05:15 -06:00
|
|
|
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-02-09 10:46:12 -06:00
|
|
|
result->resolution_type = GETDNS_RESOLUTION_RECURSING;
|
2014-03-07 04:18:23 -06:00
|
|
|
if(create_default_namespaces(result) != GETDNS_RETURN_GOOD) {
|
|
|
|
getdns_context_destroy(result);
|
2014-02-10 18:59:45 -06:00
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
2014-03-07 04:18:23 -06:00
|
|
|
}
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
result->timeout = 5000;
|
2014-02-09 10:46:12 -06:00
|
|
|
result->follow_redirects = GETDNS_REDIRECTS_FOLLOW;
|
2014-02-06 21:40:36 -06:00
|
|
|
result->dns_root_servers = create_default_root_servers();
|
2014-02-09 10:46:12 -06:00
|
|
|
result->append_name = GETDNS_APPEND_NAME_ALWAYS;
|
2014-02-06 21:40:36 -06:00
|
|
|
result->suffix = NULL;
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
result->dnssec_trust_anchors = NULL;
|
|
|
|
result->upstream_list = NULL;
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
result->edns_extended_rcode = 0;
|
|
|
|
result->edns_version = 0;
|
|
|
|
result->edns_do_bit = 1;
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2014-01-22 18:55:04 -06:00
|
|
|
result->extension = NULL;
|
|
|
|
result->extension_data = NULL;
|
|
|
|
|
2014-02-10 18:59:45 -06:00
|
|
|
result->fchg_resolvconf = NULL;
|
|
|
|
result->fchg_hosts = NULL;
|
2014-02-06 21:40:36 -06:00
|
|
|
if (set_from_os) {
|
|
|
|
if (GETDNS_RETURN_GOOD != set_os_defaults(result)) {
|
|
|
|
getdns_context_destroy(result);
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2014-02-19 13:56:37 -06:00
|
|
|
result->dnssec_allowed_skew = 0;
|
|
|
|
result->edns_maximum_udp_payload_size = 512;
|
|
|
|
result->dns_transport = GETDNS_TRANSPORT_UDP_FIRST_AND_FALL_BACK_TO_TCP;
|
|
|
|
result->limit_outstanding_queries = 0;
|
|
|
|
result->has_ta = priv_getdns_parse_ta_file(NULL, NULL);
|
2014-03-07 04:18:23 -06:00
|
|
|
result->return_dnssec_status = GETDNS_EXTENSION_FALSE;
|
2014-02-20 16:05:15 -06:00
|
|
|
if (!result->outbound_requests ||
|
|
|
|
!result->timeouts_by_id ||
|
2014-09-23 06:36:26 -05:00
|
|
|
!result->timeouts_by_time ||
|
|
|
|
!result->local_hosts) {
|
2014-02-20 16:05:15 -06:00
|
|
|
getdns_context_destroy(result);
|
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
}
|
2014-02-19 13:56:37 -06:00
|
|
|
/* unbound context is initialized here */
|
|
|
|
result->unbound_ctx = NULL;
|
|
|
|
if (GETDNS_RETURN_GOOD != rebuild_ub_ctx(result)) {
|
|
|
|
getdns_context_destroy(result);
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
2014-09-17 13:24:07 -05:00
|
|
|
/* ldns context is initialised to NULL here and rebuilt later if needed */
|
2014-09-23 06:36:26 -05:00
|
|
|
result->ldns_res = NULL;
|
|
|
|
|
|
|
|
if(create_local_hosts(result) != GETDNS_RETURN_GOOD) {
|
|
|
|
getdns_context_destroy(result);
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
2014-02-20 16:05:15 -06:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
*context = result;
|
2013-07-15 16:42:37 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2014-02-10 18:59:45 -06:00
|
|
|
} /* getdns_context_create_with_extended_memory_functions */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
2013-12-08 17:52:38 -06:00
|
|
|
/*
|
|
|
|
* getdns_context_create
|
|
|
|
*
|
|
|
|
* Call this to initialize the context that is used in other getdns calls.
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_create_with_memory_functions(struct getdns_context ** context,
|
|
|
|
int set_from_os,
|
|
|
|
void *(*malloc)(size_t),
|
|
|
|
void *(*realloc)(void *, size_t),
|
|
|
|
void (*free)(void *)
|
|
|
|
)
|
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
mf_union mf;
|
|
|
|
mf.pln.malloc = malloc;
|
|
|
|
mf.pln.realloc = realloc;
|
|
|
|
mf.pln.free = free;
|
|
|
|
return getdns_context_create_with_extended_memory_functions(
|
|
|
|
context, set_from_os, MF_PLAIN,
|
|
|
|
mf.ext.malloc, mf.ext.realloc, mf.ext.free);
|
|
|
|
} /* getdns_context_create */
|
2013-12-08 17:52:38 -06: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
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
return getdns_context_create_with_memory_functions(context,
|
|
|
|
set_from_os, malloc, realloc, free);
|
|
|
|
} /* getdns_context_create */
|
2013-11-11 16:10:22 -06:00
|
|
|
|
|
|
|
|
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
|
|
|
*/
|
2014-03-11 10:43:41 -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
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
if (context == NULL) {
|
2014-03-11 10:43:41 -05:00
|
|
|
return;
|
2014-02-06 21:40:36 -06:00
|
|
|
}
|
2014-03-07 09:42:37 -06:00
|
|
|
// If being destroyed during getdns callback,
|
2014-03-11 10:43:41 -05:00
|
|
|
// fail via assert
|
|
|
|
assert(context->processing == 0);
|
2014-03-05 21:13:37 -06:00
|
|
|
if (context->destroying) {
|
2014-03-11 10:43:41 -05:00
|
|
|
return ;
|
2014-03-05 21:13:37 -06:00
|
|
|
}
|
2014-02-20 15:17:41 -06:00
|
|
|
context->destroying = 1;
|
|
|
|
cancel_outstanding_requests(context, 1);
|
|
|
|
getdns_extension_detach_eventloop(context);
|
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
if (context->namespaces)
|
|
|
|
GETDNS_FREE(context->my_mf, context->namespaces);
|
2014-02-10 18:59:45 -06:00
|
|
|
if(context->fchg_resolvconf)
|
|
|
|
{
|
|
|
|
if(context->fchg_resolvconf->prevstat)
|
|
|
|
GETDNS_FREE(context->my_mf, context->fchg_resolvconf->prevstat);
|
|
|
|
GETDNS_FREE(context->my_mf, context->fchg_resolvconf);
|
|
|
|
}
|
|
|
|
if(context->fchg_hosts)
|
|
|
|
{
|
|
|
|
if(context->fchg_hosts->prevstat)
|
|
|
|
GETDNS_FREE(context->my_mf, context->fchg_hosts->prevstat);
|
|
|
|
GETDNS_FREE(context->my_mf, context->fchg_hosts);
|
|
|
|
}
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2014-02-06 21:40:36 -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
|
|
|
|
2014-09-17 13:24:07 -05:00
|
|
|
/* destroy the contexts */
|
2014-02-19 13:56:37 -06:00
|
|
|
if (context->unbound_ctx)
|
|
|
|
ub_ctx_delete(context->unbound_ctx);
|
2014-09-17 13:24:07 -05:00
|
|
|
if (context->ldns_res)
|
|
|
|
ldns_resolver_deep_free(context->ldns_res);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-02-20 16:05:15 -06:00
|
|
|
if (context->outbound_requests)
|
|
|
|
GETDNS_FREE(context->my_mf, context->outbound_requests);
|
|
|
|
if (context->timeouts_by_id)
|
|
|
|
GETDNS_FREE(context->my_mf, context->timeouts_by_id);
|
|
|
|
if (context->timeouts_by_time)
|
|
|
|
GETDNS_FREE(context->my_mf, context->timeouts_by_time);
|
2014-09-23 06:36:26 -05:00
|
|
|
if (context->local_hosts) {
|
|
|
|
/*TODO: deep free of this tree*/
|
|
|
|
GETDNS_FREE(context->my_mf, context->local_hosts);
|
|
|
|
}
|
2013-08-14 16:19:06 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
GETDNS_FREE(context->my_mf, context);
|
|
|
|
} /* 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,
|
2014-02-09 10:46:12 -06:00
|
|
|
void (*value) (struct getdns_context *context,
|
|
|
|
getdns_context_code_t changed_item))
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-06 21:40:36 -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
|
|
|
{
|
2014-02-19 13:56:37 -06:00
|
|
|
if (ctx->unbound_ctx)
|
|
|
|
ub_ctx_set_option(ctx->unbound_ctx, 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
|
|
|
{
|
2014-02-06 21:40:36 -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
|
|
|
}
|
|
|
|
|
2014-02-19 13:56:37 -06:00
|
|
|
static getdns_return_t
|
|
|
|
rebuild_ub_ctx(struct getdns_context* context) {
|
|
|
|
if (context->unbound_ctx != NULL) {
|
|
|
|
/* cancel all requests and delete */
|
|
|
|
cancel_outstanding_requests(context, 1);
|
|
|
|
ub_ctx_delete(context->unbound_ctx);
|
|
|
|
context->unbound_ctx = NULL;
|
|
|
|
}
|
|
|
|
/* setup */
|
|
|
|
context->unbound_ctx = ub_ctx_create();
|
|
|
|
if (!context->unbound_ctx) {
|
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
2014-02-06 21:40:36 -06:00
|
|
|
}
|
2014-02-19 13:56:37 -06:00
|
|
|
set_ub_dnssec_allowed_skew(context,
|
|
|
|
context->dnssec_allowed_skew);
|
|
|
|
set_ub_edns_maximum_udp_payload_size(context,
|
|
|
|
context->edns_maximum_udp_payload_size);
|
|
|
|
set_ub_dns_transport(context,
|
|
|
|
context->dns_transport);
|
|
|
|
|
|
|
|
/* Set default trust anchor */
|
|
|
|
if (context->has_ta) {
|
|
|
|
(void) ub_ctx_add_ta_file(
|
|
|
|
context->unbound_ctx, TRUST_ANCHOR_FILE);
|
|
|
|
}
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-10-16 13:45:43 -05:00
|
|
|
}
|
|
|
|
|
2014-09-12 07:44:53 -05:00
|
|
|
static getdns_return_t
|
|
|
|
rebuild_ldns_res(struct getdns_context* context) {
|
2014-09-17 13:47:04 -05:00
|
|
|
getdns_return_t result;
|
2014-09-12 07:44:53 -05:00
|
|
|
if (context->ldns_res != NULL) {
|
|
|
|
/* cancel all requests and delete */
|
|
|
|
cancel_outstanding_requests(context, 1);
|
|
|
|
ldns_resolver_deep_free(context->ldns_res);
|
|
|
|
context->ldns_res=NULL;
|
|
|
|
}
|
2014-09-13 11:19:25 -05:00
|
|
|
/*Create LDNS resolver object. */
|
|
|
|
context->ldns_res = ldns_resolver_new();
|
|
|
|
if (context->ldns_res == NULL) {
|
2014-09-12 07:44:53 -05:00
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
}
|
2014-09-13 11:19:25 -05:00
|
|
|
|
2014-09-17 13:24:07 -05:00
|
|
|
/* TODO: ldns doesn't support this option so this will have to be taken
|
|
|
|
account expliticly during the ldns validation
|
|
|
|
* set_ldns_dnssec_allowed_skew();*/
|
|
|
|
|
|
|
|
/* This is all the settings required for stub operation in sync mode.
|
|
|
|
* Will need additional work here when supporting async mode.*/
|
2014-09-16 06:25:18 -05:00
|
|
|
set_ldns_edns_maximum_udp_payload_size(context,
|
2014-09-12 07:44:53 -05:00
|
|
|
context->edns_maximum_udp_payload_size);
|
2014-09-17 13:47:04 -05:00
|
|
|
result = set_ldns_dns_transport(context, context->dns_transport);
|
|
|
|
if (result != GETDNS_RETURN_GOOD)
|
|
|
|
return result;
|
2014-09-12 07:44:53 -05:00
|
|
|
|
2014-09-13 11:19:25 -05:00
|
|
|
/* We need to set up the upstream recursive servers from the context */
|
2014-09-17 13:47:04 -05:00
|
|
|
result = set_ldns_nameservers(context, context->upstream_list);
|
|
|
|
if (result != GETDNS_RETURN_GOOD)
|
|
|
|
return result;
|
|
|
|
|
2014-09-12 07:44:53 -05:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-02-06 21:40:36 -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
|
2014-02-09 10:46:12 -06:00
|
|
|
getdns_context_set_resolution_type(struct getdns_context *context,
|
|
|
|
getdns_resolution_t value)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-09 10:46:12 -06:00
|
|
|
if (value != GETDNS_RESOLUTION_STUB && value != GETDNS_RESOLUTION_RECURSING) {
|
2014-02-06 21:40:36 -06:00
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2014-02-19 13:56:37 -06:00
|
|
|
if (context->resolution_type_set != 0) {
|
|
|
|
/* already setup */
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2014-02-06 21:40:36 -06:00
|
|
|
context->resolution_type = value;
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_RESOLUTION_TYPE);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-02-06 21:40:36 -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,
|
2014-02-09 10:46:12 -06:00
|
|
|
size_t namespace_count, getdns_namespace_t *namespaces)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2014-02-20 16:35:27 -06:00
|
|
|
size_t i;
|
2014-02-10 18:59:45 -06:00
|
|
|
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-06 21:40:36 -06:00
|
|
|
if (namespace_count == 0 || namespaces == NULL) {
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2014-02-19 13:56:37 -06:00
|
|
|
if (context->resolution_type_set != 0) {
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-02-10 18:59:45 -06:00
|
|
|
for(i=0; i<namespace_count; i++)
|
|
|
|
{
|
2014-02-19 12:15:27 -06:00
|
|
|
if( namespaces[i] != GETDNS_NAMESPACE_DNS
|
2014-02-10 18:59:45 -06:00
|
|
|
&& namespaces[i] != GETDNS_NAMESPACE_LOCALNAMES
|
|
|
|
&& namespaces[i] != GETDNS_NAMESPACE_NETBIOS
|
|
|
|
&& namespaces[i] != GETDNS_NAMESPACE_MDNS
|
|
|
|
&& namespaces[i] != GETDNS_NAMESPACE_NIS)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
GETDNS_FREE(context->my_mf, context->namespaces);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2013-08-12 16:38:05 -05:00
|
|
|
/** duplicate **/
|
2014-02-09 10:46:12 -06:00
|
|
|
context->namespaces = GETDNS_XMALLOC(context->my_mf, getdns_namespace_t,
|
2014-02-06 21:40:36 -06:00
|
|
|
namespace_count);
|
|
|
|
memcpy(context->namespaces, namespaces,
|
2014-02-09 10:46:12 -06:00
|
|
|
namespace_count * sizeof(getdns_namespace_t));
|
2014-02-10 18:59:45 -06:00
|
|
|
context->namespace_count = namespace_count;
|
2014-02-06 21:40:36 -06:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_NAMESPACES);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_namespaces */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
2014-02-19 13:56:37 -06:00
|
|
|
static getdns_return_t
|
|
|
|
set_ub_dns_transport(struct getdns_context* context,
|
|
|
|
getdns_transport_t value) {
|
|
|
|
switch (value) {
|
|
|
|
case GETDNS_TRANSPORT_UDP_FIRST_AND_FALL_BACK_TO_TCP:
|
2014-08-10 09:59:03 -05:00
|
|
|
set_ub_string_opt(context, "do-udp:", "yes");
|
|
|
|
set_ub_string_opt(context, "do-tcp:", "yes");
|
2014-02-19 13:56:37 -06:00
|
|
|
break;
|
|
|
|
case GETDNS_TRANSPORT_UDP_ONLY:
|
2014-08-10 09:59:03 -05:00
|
|
|
set_ub_string_opt(context, "do-udp:", "yes");
|
|
|
|
set_ub_string_opt(context, "do-tcp:", "no");
|
2014-02-19 13:56:37 -06:00
|
|
|
break;
|
|
|
|
case GETDNS_TRANSPORT_TCP_ONLY:
|
2014-08-10 09:59:03 -05:00
|
|
|
set_ub_string_opt(context, "do-udp:", "no");
|
|
|
|
set_ub_string_opt(context, "do-tcp:", "yes");
|
2014-02-19 13:56:37 -06:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* TODO GETDNS_CONTEXT_TCP_ONLY_KEEP_CONNECTIONS_OPEN */
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
2014-09-12 07:44:53 -05:00
|
|
|
|
|
|
|
static getdns_return_t
|
|
|
|
set_ldns_dns_transport(struct getdns_context* context,
|
|
|
|
getdns_transport_t value) {
|
|
|
|
switch (value) {
|
|
|
|
case GETDNS_TRANSPORT_UDP_FIRST_AND_FALL_BACK_TO_TCP:
|
2014-09-17 13:47:04 -05:00
|
|
|
/* ldns has fallback configured by default */
|
2014-09-12 07:44:53 -05:00
|
|
|
ldns_resolver_set_usevc(context->ldns_res, 0);
|
|
|
|
break;
|
|
|
|
case GETDNS_TRANSPORT_UDP_ONLY:
|
|
|
|
ldns_resolver_set_usevc(context->ldns_res, 0);
|
|
|
|
ldns_resolver_set_fallback(context->ldns_res, false);
|
|
|
|
break;
|
|
|
|
case GETDNS_TRANSPORT_TCP_ONLY:
|
|
|
|
ldns_resolver_set_usevc(context->ldns_res, 1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* TODO GETDNS_CONTEXT_TCP_ONLY_KEEP_CONNECTIONS_OPEN */
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
2013-07-15 16:42:37 -05:00
|
|
|
/*
|
|
|
|
* getdns_context_set_dns_transport
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2014-02-09 10:46:12 -06:00
|
|
|
getdns_context_set_dns_transport(struct getdns_context *context,
|
|
|
|
getdns_transport_t value)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-19 13:56:37 -06:00
|
|
|
if (set_ub_dns_transport(context, value) != GETDNS_RETURN_GOOD) {
|
2014-02-06 21:40:36 -06:00
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2014-02-19 13:56:37 -06:00
|
|
|
if (value != context->dns_transport) {
|
|
|
|
context->dns_transport = value;
|
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_DNS_TRANSPORT);
|
|
|
|
}
|
2014-02-06 21:40:36 -06:00
|
|
|
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_dns_transport */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
2014-02-19 13:56:37 -06:00
|
|
|
static void
|
|
|
|
set_ub_limit_outstanding_queries(struct getdns_context* context, uint16_t value) {
|
|
|
|
/* num-queries-per-thread */
|
2014-08-19 09:38:59 -05:00
|
|
|
set_ub_number_opt(context, "num-queries-per-thread:", value);
|
2014-02-19 13:56:37 -06:00
|
|
|
}
|
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
|
|
|
{
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-19 13:56:37 -06:00
|
|
|
set_ub_limit_outstanding_queries(context, limit);
|
|
|
|
if (limit != context->limit_outstanding_queries) {
|
|
|
|
context->limit_outstanding_queries = limit;
|
|
|
|
dispatch_updated(context,
|
|
|
|
GETDNS_CONTEXT_CODE_LIMIT_OUTSTANDING_QUERIES);
|
|
|
|
}
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-02-06 21:40:36 -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
|
2014-02-09 10:46:12 -06:00
|
|
|
getdns_context_set_timeout(struct getdns_context *context, uint64_t timeout)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
if (timeout == 0) {
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->timeout = timeout;
|
|
|
|
|
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_TIMEOUT);
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2014-02-06 21:40:36 -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
|
2014-02-09 10:46:12 -06:00
|
|
|
getdns_context_set_follow_redirects(struct getdns_context *context,
|
|
|
|
getdns_redirects_t value)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-06 21:40:36 -06:00
|
|
|
context->follow_redirects = value;
|
2014-02-19 13:56:37 -06:00
|
|
|
if (context->resolution_type_set != 0) {
|
|
|
|
/* already setup */
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_FOLLOW_REDIRECTS);
|
|
|
|
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)
|
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
struct getdns_list *copy = NULL;
|
|
|
|
size_t count = 0;
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-19 13:56:37 -06:00
|
|
|
if (context->resolution_type_set != 0) {
|
|
|
|
/* already setup */
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2014-02-06 21:40:36 -06:00
|
|
|
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) {
|
|
|
|
struct getdns_dict *dict = NULL;
|
|
|
|
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;
|
|
|
|
|
|
|
|
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
|
2014-02-09 10:46:12 -06:00
|
|
|
getdns_context_set_append_name(struct getdns_context *context,
|
|
|
|
getdns_append_name_t value)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-09 10:46:12 -06:00
|
|
|
if (value != GETDNS_APPEND_NAME_ALWAYS &&
|
|
|
|
value != GETDNS_APPEND_NAME_ONLY_TO_SINGLE_LABEL_AFTER_FAILURE &&
|
|
|
|
value != GETDNS_APPEND_NAME_ONLY_TO_MULTIPLE_LABEL_NAME_AFTER_FAILURE
|
|
|
|
&& value != GETDNS_APPEND_NAME_NEVER) {
|
2014-02-06 21:40:36 -06:00
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
context->append_name = value;
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_APPEND_NAME);
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2014-02-06 21:40:36 -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
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
struct getdns_list *copy = NULL;
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-19 13:56:37 -06:00
|
|
|
if (context->resolution_type_set != 0) {
|
|
|
|
/* already setup */
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2014-02-06 21:40:36 -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
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_SUFFIX);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-02-06 21:40:36 -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)
|
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
struct getdns_list *copy = NULL;
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-06 21:40:36 -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;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_DNSSEC_TRUST_ANCHORS);
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_dnssec_trust_anchors */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
2014-02-19 13:56:37 -06:00
|
|
|
static void
|
|
|
|
set_ub_dnssec_allowed_skew(struct getdns_context* context, uint32_t value) {
|
2014-08-19 09:38:59 -05:00
|
|
|
set_ub_number_opt(context, "val-sig-skew-min:", value);
|
|
|
|
set_ub_number_opt(context, "val-sig-skew-max:", value);
|
2014-02-19 13:56:37 -06:00
|
|
|
}
|
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,
|
2014-02-09 10:46:12 -06:00
|
|
|
uint32_t value)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-19 13:56:37 -06:00
|
|
|
set_ub_dnssec_allowed_skew(context, value);
|
|
|
|
if (value != context->dnssec_allowed_skew) {
|
|
|
|
context->dnssec_allowed_skew = value;
|
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_DNSSEC_ALLOWED_SKEW);
|
|
|
|
}
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_dnssec_allowed_skew */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
2014-01-20 09:18:14 -06:00
|
|
|
* getdns_context_set_upstream_recursive_servers
|
2013-07-15 16:42:37 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2014-01-20 09:18:14 -06:00
|
|
|
getdns_context_set_upstream_recursive_servers(struct getdns_context *context,
|
2013-11-05 14:03:44 -06:00
|
|
|
struct getdns_list * upstream_list)
|
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
size_t count = 0;
|
|
|
|
size_t i = 0;
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-18 07:38:46 -06:00
|
|
|
RETURN_IF_NULL(upstream_list, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-06 21:40:36 -06:00
|
|
|
getdns_return_t r = getdns_list_get_length(upstream_list, &count);
|
|
|
|
if (count == 0 || r != GETDNS_RETURN_GOOD) {
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2014-02-19 13:56:37 -06:00
|
|
|
if (context->resolution_type_set != 0) {
|
|
|
|
/* already setup */
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2014-02-06 21:40:36 -06:00
|
|
|
struct getdns_list *copy = NULL;
|
|
|
|
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) {
|
|
|
|
struct getdns_dict *dict = NULL;
|
|
|
|
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;
|
|
|
|
|
|
|
|
dispatch_updated(context,
|
|
|
|
GETDNS_CONTEXT_CODE_UPSTREAM_RECURSIVE_SERVERS);
|
|
|
|
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_upstream_recursive_servers */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
2014-02-19 13:56:37 -06:00
|
|
|
|
|
|
|
static void
|
|
|
|
set_ub_edns_maximum_udp_payload_size(struct getdns_context* context,
|
|
|
|
uint16_t value) {
|
2014-09-12 03:09:08 -05:00
|
|
|
/* edns-buffer-size */
|
|
|
|
set_ub_number_opt(context, "edns-buffer-size:", value);
|
2014-02-19 13:56:37 -06:00
|
|
|
}
|
2014-09-12 07:44:53 -05:00
|
|
|
|
|
|
|
static void
|
|
|
|
set_ldns_edns_maximum_udp_payload_size(struct getdns_context* context,
|
|
|
|
uint16_t value) {
|
|
|
|
/* max-udp-size */
|
|
|
|
ldns_resolver_set_edns_udp_size(context->ldns_res, value);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-06 21:40:36 -06:00
|
|
|
/* check for < 512. uint16_t won't let it go above max) */
|
|
|
|
if (value < 512) {
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2014-02-19 13:56:37 -06:00
|
|
|
set_ub_edns_maximum_udp_payload_size(context, value);
|
|
|
|
if (value != context->edns_maximum_udp_payload_size) {
|
|
|
|
context->edns_maximum_udp_payload_size = value;
|
|
|
|
dispatch_updated(context,
|
|
|
|
GETDNS_CONTEXT_CODE_EDNS_MAXIMUM_UDP_PAYLOAD_SIZE);
|
|
|
|
}
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-02-06 21:40:36 -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
|
|
|
{
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-06 21:40:36 -06:00
|
|
|
context->edns_extended_rcode = value;
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_EXTENDED_RCODE);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-02-06 21:40:36 -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
|
|
|
{
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-06 21:40:36 -06:00
|
|
|
context->edns_version = value;
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_VERSION);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-02-06 21:40:36 -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
|
|
|
{
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-20 15:17:41 -06:00
|
|
|
/* only allow 1 */
|
2014-02-06 21:40:36 -06:00
|
|
|
if (value != 1) {
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
context->edns_do_bit = value;
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_DO_BIT);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} /* getdns_context_set_edns_do_bit */
|
2013-07-15 16:42:37 -05:00
|
|
|
|
|
|
|
/*
|
2013-12-08 17:52:38 -06:00
|
|
|
* getdns_context_set_extended_memory_functions
|
2013-07-15 16:42:37 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2013-12-08 17:52:38 -06:00
|
|
|
getdns_context_set_extended_memory_functions(
|
|
|
|
struct getdns_context *context,
|
|
|
|
void *userarg,
|
|
|
|
void *(*malloc) (void *userarg, size_t),
|
|
|
|
void *(*realloc) (void *userarg, void *, size_t),
|
|
|
|
void (*free) (void *userarg, void *)
|
2013-11-05 14:03:44 -06:00
|
|
|
)
|
2013-07-15 16:42:37 -05:00
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
if (!malloc || !realloc || !free)
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
context->mf.mf_arg = userarg;
|
|
|
|
context->mf.mf.ext.malloc = malloc;
|
|
|
|
context->mf.mf.ext.realloc = realloc;
|
|
|
|
context->mf.mf.ext.free = free;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_MEMORY_FUNCTIONS);
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-12-08 17:52:38 -06:00
|
|
|
} /* getdns_context_set_extended_memory_functions*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* getdns_context_set_memory_functions
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_set_memory_functions(struct getdns_context *context,
|
|
|
|
void *(*malloc) (size_t),
|
|
|
|
void *(*realloc) (void *, size_t),
|
|
|
|
void (*free) (void *)
|
|
|
|
)
|
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
mf_union mf;
|
|
|
|
mf.pln.malloc = malloc;
|
|
|
|
mf.pln.realloc = realloc;
|
|
|
|
mf.pln.free = free;
|
|
|
|
return getdns_context_set_extended_memory_functions(
|
|
|
|
context, MF_PLAIN, mf.ext.malloc, mf.ext.realloc, mf.ext.free);
|
2013-11-11 16:10:22 -06:00
|
|
|
} /* getdns_context_set_memory_functions*/
|
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)
|
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
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->context->unbound_ctx, 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)
|
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
getdns_dns_req *req = NULL;
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
/* delete the node from the tree */
|
|
|
|
ldns_rbnode_t *node = ldns_rbtree_delete(context->outbound_requests,
|
|
|
|
&transaction_id);
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
if (!node) {
|
|
|
|
return GETDNS_RETURN_UNKNOWN_TRANSACTION;
|
|
|
|
}
|
|
|
|
req = (getdns_dns_req *) node->data;
|
|
|
|
/* do the cancel */
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
cancel_dns_req(req);
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
if (fire_callback) {
|
|
|
|
getdns_callback_t cb = NULL;
|
|
|
|
void *user_pointer = NULL;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
cb = req->user_callback;
|
|
|
|
user_pointer = req->user_pointer;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
/* fire callback */
|
|
|
|
cb(context,
|
|
|
|
GETDNS_CALLBACK_CANCEL,
|
|
|
|
NULL, user_pointer, transaction_id);
|
|
|
|
}
|
2014-02-21 17:42:04 -06:00
|
|
|
/* clean up */
|
|
|
|
GETDNS_FREE(context->my_mf, node);
|
|
|
|
dns_req_free(req);
|
2014-02-06 21:40:36 -06:00
|
|
|
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)
|
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-03-05 21:13:37 -06:00
|
|
|
context->processing = 1;
|
2014-02-21 17:42:04 -06:00
|
|
|
getdns_return_t r = getdns_context_cancel_request(context, transaction_id, 1);
|
|
|
|
if (context->extension) {
|
|
|
|
context->extension->request_count_changed(context,
|
|
|
|
context->outbound_requests->count, context->extension_data);
|
|
|
|
}
|
2014-03-05 21:13:37 -06:00
|
|
|
context->processing = 0;
|
2014-02-21 17:42:04 -06:00
|
|
|
return r;
|
2014-02-12 05:37:05 -06:00
|
|
|
} /* getdns_cancel_callback */
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2014-02-12 05:37:05 -06:00
|
|
|
static getdns_return_t
|
|
|
|
ub_setup_stub(struct ub_ctx *ctx, struct getdns_list * upstreams)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2014-02-12 05:37:05 -06:00
|
|
|
size_t i;
|
|
|
|
size_t count;
|
|
|
|
struct getdns_dict *dict;
|
|
|
|
struct getdns_bindata *address_string;
|
|
|
|
getdns_return_t r;
|
2014-02-19 12:15:27 -06:00
|
|
|
|
2014-02-12 05:37:05 -06:00
|
|
|
r = getdns_list_get_length(upstreams, &count);
|
|
|
|
if (r != GETDNS_RETURN_GOOD)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (count == 0)
|
|
|
|
return GETDNS_RETURN_BAD_CONTEXT;
|
|
|
|
|
|
|
|
/* reset forwarding servers */
|
|
|
|
(void) ub_ctx_set_fwd(ctx, NULL);
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
r = getdns_list_get_dict(upstreams, i, &dict);
|
|
|
|
if (r != GETDNS_RETURN_GOOD)
|
|
|
|
break;
|
|
|
|
|
|
|
|
r = getdns_dict_get_bindata(dict, GETDNS_STR_ADDRESS_STRING,
|
|
|
|
&address_string);
|
|
|
|
if (r != GETDNS_RETURN_GOOD)
|
|
|
|
break;
|
|
|
|
|
|
|
|
(void) ub_ctx_set_fwd(ctx, (char *)address_string->data);
|
|
|
|
}
|
|
|
|
/* Allow lookups of:
|
|
|
|
*/
|
|
|
|
/* - localhost */
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "localhost.");
|
|
|
|
|
|
|
|
/* - reverse IPv4 loopback */
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "127.in-addr.arpa.");
|
|
|
|
|
|
|
|
/* - reverse IPv6 loopback */
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0."
|
|
|
|
"0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.");
|
|
|
|
|
|
|
|
/* - reverse RFC1918 local use zones */
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "10.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "16.172.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "17.172.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "18.172.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "19.172.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "20.172.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "21.172.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "22.172.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "23.172.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "24.172.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "25.172.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "26.172.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "27.172.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "28.172.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "29.172.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "30.172.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "31.172.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "168.192.in-addr.arpa.");
|
|
|
|
|
|
|
|
/* - reverse RFC3330 IP4 this, link-local, testnet and broadcast */
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "0.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "254.169.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "2.0.192.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "100.51.198.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "113.0.203.in-addr.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "255.255.255.255.in-addr.arpa.");
|
|
|
|
|
|
|
|
/* - reverse RFC4291 IP6 unspecified */
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0."
|
|
|
|
"0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa.");
|
|
|
|
|
|
|
|
/* - reverse RFC4193 IPv6 Locally Assigned Local Addresses */
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "D.F.ip6.arpa.");
|
|
|
|
|
|
|
|
/* - reverse RFC4291 IPv6 Link Local Addresses */
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "8.E.F.ip6.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "9.E.F.ip6.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "A.E.F.ip6.arpa.");
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "B.E.F.ip6.arpa.");
|
|
|
|
|
|
|
|
/* - reverse IPv6 Example Prefix */
|
|
|
|
(void)ub_ctx_zone_remove(ctx, "8.B.D.0.1.0.0.2.ip6.arpa.");
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-09-13 11:19:25 -05:00
|
|
|
static getdns_return_t
|
|
|
|
set_ldns_nameservers(struct getdns_context *context,
|
|
|
|
struct getdns_list * upstreams)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
size_t count;
|
|
|
|
struct getdns_dict *dict;
|
|
|
|
struct getdns_bindata *address_string;
|
|
|
|
char *address_type = NULL;
|
|
|
|
ldns_rdf* ns_rdf = NULL;
|
|
|
|
getdns_return_t r;
|
|
|
|
|
|
|
|
r = getdns_list_get_length(upstreams, &count);
|
|
|
|
if (r != GETDNS_RETURN_GOOD)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (count == 0 || context->ldns_res == NULL)
|
|
|
|
return GETDNS_RETURN_BAD_CONTEXT;
|
|
|
|
|
|
|
|
/* remove current list of nameservers from resolver */
|
|
|
|
ldns_rdf *pop;
|
|
|
|
while((pop = ldns_resolver_pop_nameserver(context->ldns_res))) {
|
|
|
|
ldns_rdf_deep_free(pop);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
r = getdns_list_get_dict(upstreams, i, &dict);
|
|
|
|
if (r != GETDNS_RETURN_GOOD)
|
|
|
|
break;
|
|
|
|
r = getdns_dict_get_bindata(dict, GETDNS_STR_ADDRESS_STRING,
|
|
|
|
&address_string);
|
|
|
|
if (r != GETDNS_RETURN_GOOD)
|
|
|
|
break;
|
|
|
|
r = getdns_dict_util_get_string(dict, GETDNS_STR_ADDRESS_TYPE,
|
|
|
|
&address_type);
|
|
|
|
if (r != GETDNS_RETURN_GOOD)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* TODO: PROBLEM! The upstream list is implemented such that there is both
|
|
|
|
* an IP address and a port in the bindata for each nameserver. Unbound
|
|
|
|
* can handle this but ldns cannot. ldns has a list of nameservers which
|
|
|
|
* must be A or AAAA records and it has one port setting on the resolver.
|
|
|
|
* TEMP SOLUTION: strip off any port and use the port of the last
|
|
|
|
* nameserver in the list. Wrong, but this will support the test scripts
|
|
|
|
* in the short term which rely on being able to set a port for a single
|
|
|
|
* nameserver. */
|
|
|
|
char *address_char = (char *)address_string->data;
|
|
|
|
char *port_char = NULL;
|
|
|
|
char *at_symbol_position = strchr(address_char, '@');
|
|
|
|
if (at_symbol_position != NULL) {
|
|
|
|
int ip_length = at_symbol_position - address_char;
|
|
|
|
int port_length = strlen(address_char) - ip_length;
|
|
|
|
address_char = (char*) malloc(ip_length + 1);
|
|
|
|
memcpy(address_char, (char *)address_string->data, ip_length);
|
|
|
|
address_char[ip_length] = '\0';
|
|
|
|
port_char = (char*) malloc(port_length);
|
|
|
|
memcpy(port_char, (char *)(address_string->data + ip_length + 1), port_length);
|
|
|
|
port_char[port_length] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp(GETDNS_STR_IPV4, address_type,
|
|
|
|
strlen(GETDNS_STR_IPV4)) == 0) {
|
|
|
|
ns_rdf = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_A,
|
|
|
|
address_char);
|
|
|
|
} else if (strncmp(GETDNS_STR_IPV6, address_type,
|
|
|
|
strlen(GETDNS_STR_IPV6)) == 0) {
|
|
|
|
ns_rdf = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_AAAA,
|
|
|
|
address_char);
|
|
|
|
}
|
|
|
|
if (ns_rdf == NULL)
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
|
|
|
|
ldns_resolver_push_nameserver(context->ldns_res, ns_rdf);
|
|
|
|
ldns_rdf_deep_free(ns_rdf);
|
|
|
|
|
|
|
|
if (at_symbol_position != NULL) {
|
|
|
|
ldns_resolver_set_port(context->ldns_res, atoi(port_char));
|
|
|
|
free(port_char);
|
|
|
|
free(address_char);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
2014-02-12 05:37:05 -06:00
|
|
|
static getdns_return_t
|
|
|
|
priv_getdns_ns_dns_setup(struct getdns_context *context)
|
|
|
|
{
|
|
|
|
assert(context);
|
2014-09-12 07:44:53 -05:00
|
|
|
getdns_return_t r;
|
2014-02-12 05:37:05 -06:00
|
|
|
|
|
|
|
switch (context->resolution_type) {
|
2014-09-12 07:44:53 -05:00
|
|
|
case GETDNS_RESOLUTION_STUB:
|
|
|
|
/* Since we don't know if the resolution will be sync or async at this
|
|
|
|
* point and we only support ldns in sync mode then we must set _both_
|
|
|
|
* contexts up */
|
|
|
|
/* We get away with just setting up ldns here here because sync mode
|
|
|
|
* always hits this method because at the moment all sync calls use DNS
|
|
|
|
* namespace */
|
|
|
|
r = ub_setup_stub(context->unbound_ctx, context->upstream_list);
|
|
|
|
if (r != GETDNS_RETURN_GOOD)
|
|
|
|
return r;
|
|
|
|
return rebuild_ldns_res(context);
|
2014-02-12 05:37:05 -06:00
|
|
|
|
|
|
|
case GETDNS_RESOLUTION_RECURSING:
|
|
|
|
/* TODO: use the root servers via root hints file */
|
|
|
|
(void) ub_ctx_set_fwd(context->unbound_ctx, NULL);
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
return GETDNS_RETURN_BAD_CONTEXT;
|
2013-10-16 13:45:43 -05:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_return_t
|
2014-02-12 05:37:05 -06:00
|
|
|
getdns_context_prepare_for_resolution(struct getdns_context *context,
|
|
|
|
int usenamespaces)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2014-02-10 18:59:45 -06:00
|
|
|
int i;
|
2014-02-12 05:37:05 -06:00
|
|
|
getdns_return_t r;
|
2014-02-10 18:59:45 -06:00
|
|
|
|
2014-02-12 05:37:05 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-20 15:17:41 -06:00
|
|
|
if (context->destroying) {
|
|
|
|
return GETDNS_RETURN_BAD_CONTEXT;
|
|
|
|
}
|
2014-02-12 05:37:05 -06:00
|
|
|
if (context->resolution_type_set == context->resolution_type)
|
|
|
|
/* already set and no config changes
|
2014-02-19 12:15:27 -06:00
|
|
|
* have caused this to be bad.
|
2014-02-12 05:37:05 -06:00
|
|
|
*/
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2014-02-06 21:40:36 -06:00
|
|
|
|
2014-02-10 18:59:45 -06:00
|
|
|
/* TODO: respect namespace order (unbound always uses local first if cfg
|
|
|
|
* the spec calls for us to treat the namespace list as ordered
|
|
|
|
* so we need to respect that order
|
|
|
|
*/
|
|
|
|
|
2014-02-19 13:56:37 -06:00
|
|
|
|
2014-02-12 05:37:05 -06:00
|
|
|
if (! usenamespaces) {
|
|
|
|
r = priv_getdns_ns_dns_setup(context);
|
|
|
|
if (r == GETDNS_RETURN_GOOD)
|
|
|
|
context->resolution_type_set = context->resolution_type;
|
|
|
|
return r;
|
2014-02-10 18:59:45 -06:00
|
|
|
}
|
|
|
|
|
2014-02-12 05:37:05 -06:00
|
|
|
r = GETDNS_RETURN_GOOD;
|
|
|
|
for (i = 0; i < context->namespace_count; i++) {
|
|
|
|
switch (context->namespaces[i]) {
|
|
|
|
case GETDNS_NAMESPACE_LOCALNAMES:
|
2014-09-12 07:44:53 -05:00
|
|
|
/* TODO: Note to self! This must change once we have
|
2014-09-17 13:24:07 -05:00
|
|
|
* proper namespace hanlding or asynch stub mode using ldns.*/
|
2014-02-12 05:37:05 -06:00
|
|
|
(void) ub_ctx_hosts(context->unbound_ctx, NULL);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GETDNS_NAMESPACE_DNS:
|
|
|
|
r = priv_getdns_ns_dns_setup(context);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
r = GETDNS_RETURN_BAD_CONTEXT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (r != GETDNS_RETURN_GOOD)
|
|
|
|
return r; /* try again later (resolution_type_set) */
|
|
|
|
}
|
|
|
|
context->resolution_type_set = context->resolution_type;
|
|
|
|
return r;
|
2014-02-10 18:59:45 -06:00
|
|
|
} /* getdns_context_prepare_for_resolution */
|
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)
|
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
if (!req) {
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
struct getdns_context *context = req->context;
|
|
|
|
ldns_rbnode_t *node = GETDNS_MALLOC(context->my_mf, ldns_rbnode_t);
|
|
|
|
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 */
|
|
|
|
GETDNS_FREE(context->my_mf, node);
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
2014-02-21 17:42:04 -06:00
|
|
|
if (context->extension) {
|
|
|
|
context->extension->request_count_changed(context,
|
|
|
|
context->outbound_requests->count, context->extension_data);
|
|
|
|
}
|
2014-02-06 21:40:36 -06:00
|
|
|
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)
|
|
|
|
{
|
2014-02-06 21:40:36 -06:00
|
|
|
if (!req) {
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
struct getdns_context *context = req->context;
|
|
|
|
ldns_rbnode_t *node = ldns_rbtree_delete(context->outbound_requests,
|
|
|
|
&(req->trans_id));
|
|
|
|
if (node) {
|
|
|
|
GETDNS_FREE(context->my_mf, node);
|
|
|
|
}
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-10-16 17:33:12 -05:00
|
|
|
}
|
|
|
|
|
2014-03-05 21:13:37 -06:00
|
|
|
getdns_return_t
|
|
|
|
getdns_context_request_timed_out(struct getdns_dns_req
|
|
|
|
*req) {
|
|
|
|
getdns_context* context = req->context;
|
|
|
|
getdns_transaction_t trans_id = req->trans_id;
|
|
|
|
getdns_callback_t cb = req->user_callback;
|
|
|
|
void *user_arg = req->user_pointer;
|
|
|
|
|
|
|
|
/* cancel the req - also clears it from outbound and cleans up*/
|
|
|
|
getdns_context_cancel_request(context, trans_id, 0);
|
|
|
|
context->processing = 1;
|
|
|
|
cb(context, GETDNS_CALLBACK_TIMEOUT, NULL, user_arg, trans_id);
|
2014-03-07 09:42:37 -06:00
|
|
|
context->processing = 0;
|
|
|
|
if (context->extension) {
|
|
|
|
context->extension->request_count_changed(context,
|
|
|
|
context->outbound_requests->count, context->extension_data);
|
2014-03-05 21:13:37 -06:00
|
|
|
}
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
2014-01-21 14:31:22 -06:00
|
|
|
|
2013-11-11 16:10:22 -06:00
|
|
|
char *
|
2014-01-14 10:25:23 -06:00
|
|
|
getdns_strdup(const struct mem_funcs *mfs, const char *s)
|
2013-11-11 16:10:22 -06:00
|
|
|
{
|
2014-01-31 13:43:02 -06:00
|
|
|
size_t sz = strlen(s) + 1;
|
|
|
|
char *r = GETDNS_XMALLOC(*mfs, char, sz);
|
|
|
|
if (r)
|
|
|
|
return memcpy(r, s, sz);
|
|
|
|
else
|
|
|
|
return NULL;
|
2013-11-11 16:10:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
struct getdns_bindata *
|
2013-12-08 17:05:18 -06:00
|
|
|
getdns_bindata_copy(struct mem_funcs *mfs,
|
2013-11-11 16:10:22 -06:00
|
|
|
const struct getdns_bindata *src)
|
|
|
|
{
|
2014-01-31 13:43:02 -06:00
|
|
|
struct getdns_bindata *dst;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2014-01-31 13:43:02 -06:00
|
|
|
if (!src)
|
|
|
|
return NULL;
|
2013-12-09 11:55:33 -06:00
|
|
|
|
2014-01-31 13:43:02 -06:00
|
|
|
dst = GETDNS_MALLOC(*mfs, struct getdns_bindata);
|
|
|
|
if (!dst)
|
|
|
|
return NULL;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2014-01-31 13:43:02 -06:00
|
|
|
dst->size = src->size;
|
|
|
|
dst->data = GETDNS_XMALLOC(*mfs, uint8_t, src->size);
|
|
|
|
if (!dst->data) {
|
|
|
|
GETDNS_FREE(*mfs, dst);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
(void) memcpy(dst->data, src->data, src->size);
|
|
|
|
return dst;
|
2013-11-11 16:10:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-12-08 17:05:18 -06:00
|
|
|
getdns_bindata_destroy(struct mem_funcs *mfs,
|
|
|
|
struct getdns_bindata *bindata)
|
2013-11-11 16:10:22 -06:00
|
|
|
{
|
2014-01-31 13:43:02 -06:00
|
|
|
if (!bindata)
|
|
|
|
return;
|
|
|
|
GETDNS_FREE(*mfs, bindata->data);
|
|
|
|
GETDNS_FREE(*mfs, bindata);
|
2013-11-11 16:10:22 -06:00
|
|
|
}
|
2014-01-28 08:30:01 -06:00
|
|
|
|
2014-01-31 13:43:02 -06:00
|
|
|
/* get the fd */
|
|
|
|
int getdns_context_fd(struct getdns_context* context) {
|
|
|
|
RETURN_IF_NULL(context, -1);
|
|
|
|
return ub_fd(context->unbound_ctx);
|
|
|
|
}
|
|
|
|
|
2014-02-21 17:42:04 -06:00
|
|
|
uint32_t
|
2014-01-31 14:48:00 -06:00
|
|
|
getdns_context_get_num_pending_requests(struct getdns_context* context,
|
|
|
|
struct timeval* next_timeout) {
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-02-21 17:42:04 -06:00
|
|
|
uint32_t r = context->outbound_requests->count;
|
2014-01-31 14:48:00 -06:00
|
|
|
if (r > 0) {
|
|
|
|
if (!context->extension && next_timeout) {
|
2014-02-05 09:11:53 -06:00
|
|
|
/* default is 1 second */
|
|
|
|
next_timeout->tv_sec = 1;
|
|
|
|
next_timeout->tv_usec = 0;
|
|
|
|
struct timeval now;
|
|
|
|
if (gettimeofday(&now, NULL) == 0) {
|
|
|
|
/* get the first timeout */
|
|
|
|
ldns_rbnode_t* first = ldns_rbtree_first(context->timeouts_by_time);
|
|
|
|
if (first) {
|
|
|
|
getdns_timeout_data_t* timeout_data = (getdns_timeout_data_t*) first->data;
|
|
|
|
/* subtract next_timeout from now */
|
|
|
|
if (timeout_data->timeout_time.tv_sec > now.tv_sec ||
|
|
|
|
(timeout_data->timeout_time.tv_sec == now.tv_sec &&
|
|
|
|
timeout_data->timeout_time.tv_usec >= now.tv_usec)) {
|
|
|
|
next_timeout->tv_sec = timeout_data->timeout_time.tv_sec - now.tv_sec;
|
2014-02-20 15:17:41 -06:00
|
|
|
if (timeout_data->timeout_time.tv_usec < now.tv_usec) {
|
|
|
|
/* we only enter this condition when timeout_data.tv_sec > now.tv_sec */
|
|
|
|
next_timeout->tv_usec = (timeout_data->timeout_time.tv_usec + 100000) - now.tv_usec;
|
|
|
|
next_timeout->tv_sec--;
|
|
|
|
} else {
|
|
|
|
next_timeout->tv_usec = timeout_data->timeout_time.tv_usec - now.tv_usec;
|
|
|
|
}
|
2014-02-05 09:11:53 -06:00
|
|
|
} else {
|
|
|
|
/* timeout passed already */
|
2014-02-20 15:17:41 -06:00
|
|
|
/* usec already 0 per setting default */
|
2014-02-05 09:11:53 -06:00
|
|
|
next_timeout->tv_sec = 0;
|
|
|
|
}
|
|
|
|
}
|
2014-01-31 14:48:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-01-31 13:43:02 -06:00
|
|
|
/* process async reqs */
|
|
|
|
getdns_return_t getdns_context_process_async(struct getdns_context* context) {
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-03-04 16:00:18 -06:00
|
|
|
context->processing = 1;
|
2014-01-31 13:43:02 -06:00
|
|
|
if (ub_poll(context->unbound_ctx)) {
|
|
|
|
if (ub_process(context->unbound_ctx) != 0) {
|
|
|
|
/* need an async return code? */
|
2014-03-07 09:42:37 -06:00
|
|
|
context->processing = 0;
|
2014-01-31 13:43:02 -06:00
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2014-03-04 16:00:18 -06:00
|
|
|
// reset the processing flag
|
|
|
|
context->processing = 0;
|
2014-01-31 13:43:02 -06:00
|
|
|
if (context->extension != NULL) {
|
|
|
|
/* no need to process timeouts since it is delegated
|
|
|
|
* to the extension */
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
getdns_timeout_data_t key;
|
|
|
|
/* set to 0 so it is the last timeout if we have
|
|
|
|
* two with the same time */
|
|
|
|
key.transaction_id = 0;
|
|
|
|
if (gettimeofday(&key.timeout_time, NULL) != 0) {
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
ldns_rbnode_t* next_timeout = ldns_rbtree_first(context->timeouts_by_time);
|
2014-03-05 21:13:37 -06:00
|
|
|
getdns_return_t r = GETDNS_RETURN_GOOD;
|
|
|
|
while (next_timeout && r == GETDNS_RETURN_GOOD) {
|
2014-01-31 13:43:02 -06:00
|
|
|
getdns_timeout_data_t* timeout_data = (getdns_timeout_data_t*) next_timeout->data;
|
|
|
|
if (timeout_cmp(timeout_data, &key) > 0) {
|
|
|
|
/* no more timeouts need to be fired. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* get the next_timeout */
|
|
|
|
next_timeout = ldns_rbtree_next(next_timeout);
|
|
|
|
/* delete the node */
|
2014-02-20 15:17:41 -06:00
|
|
|
/* timeout data and the timeouts_by_id node are freed in the clear_timeout */
|
2014-01-31 13:43:02 -06:00
|
|
|
ldns_rbnode_t* to_del = ldns_rbtree_delete(context->timeouts_by_time, timeout_data);
|
|
|
|
if (to_del) {
|
|
|
|
/* should always exist .. */
|
|
|
|
GETDNS_FREE(context->my_mf, to_del);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fire the timeout */
|
2014-03-05 21:13:37 -06:00
|
|
|
r = timeout_data->callback(timeout_data->userarg);
|
2014-01-31 13:43:02 -06:00
|
|
|
}
|
|
|
|
|
2014-03-05 21:13:37 -06:00
|
|
|
return r;
|
2013-11-11 16:10:22 -06:00
|
|
|
}
|
2014-01-22 18:55:04 -06:00
|
|
|
|
2014-01-31 14:16:10 -06:00
|
|
|
typedef struct timeout_accumulator {
|
|
|
|
getdns_transaction_t* ids;
|
|
|
|
int idx;
|
|
|
|
} timeout_accumulator;
|
|
|
|
|
|
|
|
static void
|
|
|
|
accumulate_outstanding_transactions(ldns_rbnode_t* node, void* arg) {
|
|
|
|
timeout_accumulator* acc = (timeout_accumulator*) arg;
|
|
|
|
acc->ids[acc->idx] = *((getdns_transaction_t*) node->key);
|
|
|
|
acc->idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-02-03 16:38:06 -06:00
|
|
|
cancel_outstanding_requests(struct getdns_context* context, int fire_callback) {
|
2014-01-31 14:16:10 -06:00
|
|
|
if (context->outbound_requests->count > 0) {
|
|
|
|
timeout_accumulator acc;
|
|
|
|
int i;
|
|
|
|
acc.idx = 0;
|
|
|
|
acc.ids = GETDNS_XMALLOC(context->my_mf, getdns_transaction_t, context->outbound_requests->count);
|
|
|
|
ldns_traverse_postorder(context->outbound_requests, accumulate_outstanding_transactions, &acc);
|
|
|
|
for (i = 0; i < acc.idx; ++i) {
|
2014-02-03 16:38:06 -06:00
|
|
|
getdns_context_cancel_request(context, acc.ids[i], fire_callback);
|
2014-01-31 14:16:10 -06:00
|
|
|
}
|
2014-02-03 18:57:10 -06:00
|
|
|
GETDNS_FREE(context->my_mf, acc.ids);
|
2014-01-31 14:16:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-22 18:55:04 -06:00
|
|
|
getdns_return_t
|
|
|
|
getdns_extension_detach_eventloop(struct getdns_context* context)
|
|
|
|
{
|
2014-03-06 07:57:22 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
getdns_return_t r = GETDNS_RETURN_GOOD;
|
|
|
|
if (context->extension) {
|
|
|
|
/* When called from within a callback, do not execute pending
|
|
|
|
* context destroys.
|
|
|
|
* The (other) callback handler will handle it.
|
2014-03-07 09:42:37 -06:00
|
|
|
*
|
2014-03-06 07:57:22 -06:00
|
|
|
* ( because callbacks occur in cancel_outstanding_requests,
|
|
|
|
* and they may destroy the context )
|
|
|
|
*/
|
2014-03-07 09:42:37 -06:00
|
|
|
context->processing = 1;
|
2014-03-06 07:57:22 -06:00
|
|
|
/* cancel all outstanding requests */
|
|
|
|
cancel_outstanding_requests(context, 1);
|
|
|
|
r = context->extension->cleanup_data(context,
|
|
|
|
context->extension_data);
|
|
|
|
if (r == GETDNS_RETURN_GOOD) {
|
|
|
|
context->extension = NULL;
|
|
|
|
context->extension_data = NULL;
|
|
|
|
}
|
2014-03-07 09:42:37 -06:00
|
|
|
context->processing = 0;
|
2014-03-06 07:57:22 -06:00
|
|
|
}
|
|
|
|
return r;
|
2014-01-22 18:55:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_extension_set_eventloop(struct getdns_context* context,
|
|
|
|
getdns_eventloop_extension* extension, void* extension_data)
|
|
|
|
{
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-01-22 18:55:04 -06:00
|
|
|
RETURN_IF_NULL(extension, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
getdns_return_t r = getdns_extension_detach_eventloop(context);
|
|
|
|
if (r != GETDNS_RETURN_GOOD) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
context->extension = extension;
|
|
|
|
context->extension_data = extension_data;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
2014-01-27 16:05:25 -06:00
|
|
|
getdns_return_t
|
|
|
|
getdns_context_schedule_timeout(struct getdns_context* context,
|
|
|
|
getdns_transaction_t id, uint16_t timeout, getdns_timeout_callback callback,
|
2014-01-31 13:43:02 -06:00
|
|
|
void* userarg) {
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-01-31 13:43:02 -06:00
|
|
|
RETURN_IF_NULL(callback, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
getdns_return_t result;
|
|
|
|
/* create a timeout */
|
|
|
|
getdns_timeout_data_t* timeout_data = GETDNS_MALLOC(context->my_mf, getdns_timeout_data_t);
|
|
|
|
if (!timeout_data) {
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
timeout_data->context = context;
|
|
|
|
timeout_data->transaction_id = id;
|
|
|
|
timeout_data->callback = callback;
|
|
|
|
timeout_data->userarg = userarg;
|
|
|
|
timeout_data->extension_timer = NULL;
|
|
|
|
|
|
|
|
/* insert into transaction tree */
|
|
|
|
ldns_rbnode_t *node = GETDNS_MALLOC(context->my_mf, ldns_rbnode_t);
|
|
|
|
if (!node) {
|
|
|
|
GETDNS_FREE(context->my_mf, timeout_data);
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
node->key = &(timeout_data->transaction_id);
|
|
|
|
node->data = timeout_data;
|
2014-02-03 15:33:50 -06:00
|
|
|
node->left = NULL;
|
|
|
|
node->right = NULL;
|
2014-01-31 13:43:02 -06:00
|
|
|
if (!ldns_rbtree_insert(context->timeouts_by_id, node)) {
|
|
|
|
/* free the node */
|
|
|
|
GETDNS_FREE(context->my_mf, timeout_data);
|
|
|
|
GETDNS_FREE(context->my_mf, node);
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
2014-01-27 16:05:25 -06:00
|
|
|
|
2014-01-31 13:43:02 -06:00
|
|
|
if (context->extension) {
|
|
|
|
result = context->extension->schedule_timeout(context, context->extension_data,
|
|
|
|
timeout, timeout_data, &(timeout_data->extension_timer));
|
|
|
|
} else {
|
|
|
|
result = GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
if (gettimeofday(&timeout_data->timeout_time, NULL) == 0) {
|
2014-02-03 15:33:50 -06:00
|
|
|
/* timeout is in millis */
|
2014-02-05 09:11:53 -06:00
|
|
|
uint16_t num_secs = timeout / 1000;
|
|
|
|
uint16_t num_usecs = (timeout % 1000) * 1000;
|
|
|
|
timeout_data->timeout_time.tv_usec += num_usecs;
|
|
|
|
/* overflow check */
|
|
|
|
if (timeout_data->timeout_time.tv_usec > 1000000) {
|
|
|
|
timeout_data->timeout_time.tv_usec -= 1000000;
|
|
|
|
num_secs++;
|
|
|
|
}
|
2014-02-03 15:33:50 -06:00
|
|
|
timeout_data->timeout_time.tv_sec += num_secs;
|
|
|
|
|
2014-01-31 13:43:02 -06:00
|
|
|
ldns_rbnode_t* id_node = GETDNS_MALLOC(context->my_mf, ldns_rbnode_t);
|
|
|
|
if (id_node) {
|
|
|
|
id_node->key = timeout_data;
|
|
|
|
id_node->data = timeout_data;
|
2014-02-03 15:33:50 -06:00
|
|
|
id_node->left = NULL;
|
|
|
|
id_node->right = NULL;
|
|
|
|
if (!ldns_rbtree_insert(context->timeouts_by_time, id_node)) {
|
2014-01-31 13:43:02 -06:00
|
|
|
GETDNS_FREE(context->my_mf, id_node);
|
|
|
|
} else {
|
|
|
|
result = GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result != GETDNS_RETURN_GOOD) {
|
|
|
|
GETDNS_FREE(context->my_mf, timeout_data);
|
|
|
|
GETDNS_FREE(context->my_mf, node);
|
|
|
|
}
|
2014-02-03 15:33:50 -06:00
|
|
|
return result;
|
2014-01-27 16:05:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_clear_timeout(struct getdns_context* context,
|
|
|
|
getdns_transaction_t id) {
|
2014-02-04 04:25:21 -06:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-01-31 13:43:02 -06:00
|
|
|
/* find the timeout_data by id */
|
|
|
|
ldns_rbnode_t* node = ldns_rbtree_delete(context->timeouts_by_id, &id);
|
|
|
|
if (!node) {
|
|
|
|
return GETDNS_RETURN_UNKNOWN_TRANSACTION;
|
|
|
|
}
|
|
|
|
getdns_timeout_data_t* timeout_data = (getdns_timeout_data_t*) node->data;
|
|
|
|
GETDNS_FREE(context->my_mf, node);
|
|
|
|
if (context->extension) {
|
2014-02-05 23:24:26 -06:00
|
|
|
context->extension->clear_timeout(context, context->extension_data,
|
2014-01-31 13:43:02 -06:00
|
|
|
timeout_data->extension_timer);
|
|
|
|
} else {
|
|
|
|
/* make sure it is removed from the timeout node */
|
|
|
|
ldns_rbnode_t* to_del = ldns_rbtree_delete(context->timeouts_by_time, timeout_data);
|
|
|
|
if (to_del) {
|
|
|
|
GETDNS_FREE(context->my_mf, to_del);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GETDNS_FREE(context->my_mf, timeout_data);
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2014-01-27 16:05:25 -06:00
|
|
|
}
|
|
|
|
|
2014-02-21 17:42:04 -06:00
|
|
|
void*
|
|
|
|
getdns_context_get_extension_data(struct getdns_context* context) {
|
|
|
|
RETURN_IF_NULL(context, NULL);
|
|
|
|
return context->extension_data;
|
|
|
|
}
|
|
|
|
|
2014-02-19 15:22:55 -06:00
|
|
|
static inline getdns_return_t
|
|
|
|
priv_dict_set_list_if_not_null(getdns_dict* dict,
|
|
|
|
const char* name, getdns_list* list) {
|
|
|
|
if (!list) {
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
return getdns_dict_set_list(dict, name, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static getdns_dict*
|
|
|
|
priv_get_context_settings(getdns_context* context) {
|
|
|
|
getdns_return_t r = GETDNS_RETURN_GOOD;
|
|
|
|
getdns_dict* result = getdns_dict_create_with_context(context);
|
|
|
|
if (!result) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* int fields */
|
|
|
|
r = getdns_dict_set_int(result, "dns_transport", context->dns_transport);
|
|
|
|
r |= getdns_dict_set_int(result, "timeout", context->timeout);
|
|
|
|
r |= getdns_dict_set_int(result, "limit_outstanding_queries", context->limit_outstanding_queries);
|
|
|
|
r |= getdns_dict_set_int(result, "dnssec_allowed_skew", context->dnssec_allowed_skew);
|
|
|
|
r |= getdns_dict_set_int(result, "follow_redirects", context->follow_redirects);
|
|
|
|
r |= getdns_dict_set_int(result, "edns_maximum_udp_payload_size", context->edns_maximum_udp_payload_size);
|
|
|
|
r |= getdns_dict_set_int(result, "edns_extended_rcode", context->edns_extended_rcode);
|
|
|
|
r |= getdns_dict_set_int(result, "edns_version", context->edns_version);
|
|
|
|
r |= getdns_dict_set_int(result, "edns_do_bit", context->edns_do_bit);
|
|
|
|
r |= getdns_dict_set_int(result, "append_name", context->append_name);
|
|
|
|
/* list fields */
|
|
|
|
r |= priv_dict_set_list_if_not_null(result, "suffix", context->suffix);
|
|
|
|
r |= priv_dict_set_list_if_not_null(result, "upstream_recursive_servers", context->upstream_list);
|
|
|
|
if (context->namespace_count > 0) {
|
|
|
|
/* create a namespace list */
|
|
|
|
size_t i;
|
|
|
|
getdns_list* namespaces = getdns_list_create_with_context(context);
|
|
|
|
if (namespaces) {
|
|
|
|
for (i = 0; i < context->namespace_count; ++i) {
|
|
|
|
r |= getdns_list_set_int(namespaces, i, context->namespaces[i]);
|
|
|
|
}
|
|
|
|
r |= getdns_dict_set_list(result, "namespaces", namespaces);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (r != GETDNS_RETURN_GOOD) {
|
|
|
|
getdns_dict_destroy(result);
|
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-02-19 12:15:27 -06:00
|
|
|
getdns_dict*
|
|
|
|
getdns_context_get_api_information(getdns_context* context) {
|
|
|
|
getdns_return_t r = GETDNS_RETURN_GOOD;
|
|
|
|
getdns_dict* result = getdns_dict_create_with_context(context);
|
2014-02-19 15:22:55 -06:00
|
|
|
getdns_dict* settings;
|
2014-02-19 12:15:27 -06:00
|
|
|
if (!result) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
r = getdns_dict_util_set_string(result, "version_string", PACKAGE_VERSION);
|
|
|
|
r |= getdns_dict_util_set_string(result, "implementation_string", PACKAGE_URL);
|
2014-09-03 08:42:11 -05:00
|
|
|
r |= getdns_dict_set_int(result, "resolution_type", context->resolution_type);
|
2014-02-19 15:22:55 -06:00
|
|
|
settings = priv_get_context_settings(context);
|
|
|
|
r |= getdns_dict_set_dict(result, "all_context", settings);
|
|
|
|
getdns_dict_destroy(settings);
|
2014-02-19 12:15:27 -06:00
|
|
|
if (r != GETDNS_RETURN_GOOD) {
|
|
|
|
getdns_dict_destroy(result);
|
|
|
|
result = NULL;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-02-20 14:42:10 -06:00
|
|
|
getdns_return_t
|
|
|
|
getdns_context_set_return_dnssec_status(getdns_context* context, int enabled) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-03-05 09:42:36 -06:00
|
|
|
if (enabled != GETDNS_EXTENSION_TRUE &&
|
2014-02-20 14:42:10 -06:00
|
|
|
enabled != GETDNS_EXTENSION_FALSE) {
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
context->return_dnssec_status = enabled;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
2014-01-27 16:05:25 -06:00
|
|
|
|
2014-02-28 18:24:09 -06:00
|
|
|
getdns_return_t
|
|
|
|
getdns_context_set_use_threads(getdns_context* context, int use_threads) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
if (context->resolution_type_set != 0) {
|
|
|
|
/* already setup */
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
|
|
|
int r = 0;
|
|
|
|
if (use_threads)
|
|
|
|
r = ub_ctx_async(context->unbound_ctx, 1);
|
|
|
|
else
|
|
|
|
r = ub_ctx_async(context->unbound_ctx, 0);
|
|
|
|
return r == 0 ? GETDNS_RETURN_GOOD : GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
|
|
|
|
2014-09-23 06:36:26 -05:00
|
|
|
getdns_return_t
|
|
|
|
getdns_context_local_namespace_resolve(getdns_dns_req* req,
|
|
|
|
struct getdns_context *context)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* NOTE: This only returns GETDNS_RETURN_GOOD if it finds answers for all the
|
|
|
|
netreq that it tries */
|
|
|
|
/*TODO: free memory on error paths*/
|
|
|
|
|
|
|
|
getdns_network_req *netreq = req->first_req;
|
|
|
|
while (netreq) {
|
|
|
|
/*This request may have already been answered by another namespace*/
|
|
|
|
if (netreq->result) {
|
|
|
|
netreq = netreq->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (netreq->request_type != GETDNS_RRTYPE_A && netreq->request_type != GETDNS_RRTYPE_AAAA)
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
|
|
|
|
/*Do the lookup*/
|
|
|
|
ldns_rdf *query_name = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME, req->name);
|
|
|
|
struct host_name_addr_type *lh_key =
|
|
|
|
GETDNS_MALLOC(context->my_mf, struct host_name_addr_type);
|
|
|
|
if (lh_key == NULL)
|
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
lh_key->host_name = query_name;
|
|
|
|
lh_key->addr_type = netreq->request_type;
|
|
|
|
ldns_rbnode_t *result_node = ldns_rbtree_search(context->local_hosts, lh_key);
|
|
|
|
if (!result_node) {
|
|
|
|
ldns_rdf_deep_free(query_name);
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*Fabricate the result packet*/
|
|
|
|
ldns_pkt *answer_pkt;
|
|
|
|
ldns_rr *question_rr;
|
|
|
|
ldns_rr_list *answer_qr;
|
|
|
|
ldns_rr_list *answer_an;
|
|
|
|
ldns_rr_list *answer_ns;
|
|
|
|
ldns_rr_list *answer_ad;
|
|
|
|
|
|
|
|
question_rr = ldns_rr_new_frm_type(netreq->request_type);
|
|
|
|
ldns_rr_set_class(question_rr, netreq->request_class);
|
|
|
|
ldns_rr_set_owner(question_rr, query_name);
|
|
|
|
ldns_rr_set_rd_count (question_rr, (size_t)0);
|
|
|
|
answer_qr = ldns_rr_list_new();
|
|
|
|
if (!ldns_rr_list_push_rr (answer_qr, question_rr)) {
|
|
|
|
ldns_rdf_deep_free(query_name);
|
|
|
|
ldns_rr_free(question_rr);
|
|
|
|
ldns_rr_list_deep_free(answer_qr);
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
answer_an = ldns_rr_list_clone((ldns_rr_list *)result_node->data);
|
|
|
|
answer_ns = ldns_rr_list_new();
|
|
|
|
answer_ad = ldns_rr_list_new();
|
|
|
|
|
|
|
|
answer_pkt = ldns_pkt_new();
|
|
|
|
ldns_pkt_set_qr(answer_pkt, 1);
|
|
|
|
ldns_pkt_set_aa(answer_pkt, 1);
|
|
|
|
|
|
|
|
ldns_pkt_push_rr_list(answer_pkt, LDNS_SECTION_QUESTION, answer_qr);
|
|
|
|
ldns_pkt_push_rr_list(answer_pkt, LDNS_SECTION_ANSWER, answer_an);
|
|
|
|
ldns_pkt_push_rr_list(answer_pkt, LDNS_SECTION_AUTHORITY, answer_ns);
|
|
|
|
ldns_pkt_push_rr_list(answer_pkt, LDNS_SECTION_ADDITIONAL, answer_ad);
|
|
|
|
|
|
|
|
netreq->result = answer_pkt;
|
|
|
|
netreq = netreq->next;
|
|
|
|
}
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-01-28 08:30:01 -06:00
|
|
|
/* context.c */
|