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>
|
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>
|
2014-03-11 10:43:41 -05:00
|
|
|
#include <assert.h>
|
2014-09-30 08:12:48 -05:00
|
|
|
#include <netdb.h>
|
2015-06-12 06:51:36 -05:00
|
|
|
#include <ctype.h>
|
2013-11-06 12:32:05 -06:00
|
|
|
|
2014-05-19 08:50:34 -05:00
|
|
|
#include "config.h"
|
2015-03-12 15:37:03 -05:00
|
|
|
#include "gldns/str2wire.h"
|
2015-07-09 17:05:26 -05:00
|
|
|
#include "gldns/wire2str.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"
|
2014-10-17 17:25:41 -05:00
|
|
|
#include "stub.h"
|
2015-03-12 15:37:03 -05:00
|
|
|
#include "list.h"
|
2013-11-06 12:32:05 -06:00
|
|
|
|
2015-04-29 13:20:25 -05:00
|
|
|
#define GETDNS_PORT_ZERO 0
|
2015-04-30 13:07:49 -05:00
|
|
|
#define GETDNS_PORT_DNS 53
|
|
|
|
#define GETDNS_PORT_DNS_OVER_TLS 1021
|
2015-04-29 13:20:25 -05:00
|
|
|
#define GETDNS_STR_PORT_ZERO "0"
|
2015-04-30 13:07:49 -05:00
|
|
|
#define GETDNS_STR_PORT_DNS "53"
|
|
|
|
#define GETDNS_STR_PORT_DNS_OVER_TLS "1021"
|
2015-04-29 13:20:25 -05:00
|
|
|
|
2015-08-24 07:37:02 -05:00
|
|
|
void *plain_mem_funcs_user_arg = MF_PLAIN;
|
2013-12-08 15:56:34 -06:00
|
|
|
|
2014-10-23 16:00:30 -05:00
|
|
|
typedef struct host_name_addrs {
|
2015-08-19 09:07:01 -05:00
|
|
|
_getdns_rbnode_t node;
|
2015-03-12 15:37:03 -05:00
|
|
|
getdns_list *ipv4addrs;
|
|
|
|
getdns_list *ipv6addrs;
|
|
|
|
uint8_t host_name[];
|
2014-10-23 16:00:30 -05:00
|
|
|
} host_name_addrs;
|
2014-09-23 06:36:26 -05:00
|
|
|
|
2015-06-19 12:28:29 -05:00
|
|
|
static getdns_transport_list_t
|
|
|
|
getdns_upstream_transports[GETDNS_UPSTREAM_TRANSPORTS] = {
|
2015-06-22 12:02:28 -05:00
|
|
|
GETDNS_TRANSPORT_STARTTLS, // Define before TCP to ease fallback
|
2015-06-19 12:28:29 -05:00
|
|
|
GETDNS_TRANSPORT_TCP,
|
|
|
|
GETDNS_TRANSPORT_TLS,
|
|
|
|
};
|
|
|
|
|
2015-04-19 11:16:58 -05:00
|
|
|
static in_port_t
|
2015-06-19 12:28:29 -05:00
|
|
|
getdns_port_array[GETDNS_UPSTREAM_TRANSPORTS] = {
|
2015-04-30 13:07:49 -05:00
|
|
|
GETDNS_PORT_DNS,
|
|
|
|
GETDNS_PORT_DNS,
|
2015-06-22 12:02:28 -05:00
|
|
|
GETDNS_PORT_DNS_OVER_TLS
|
2015-04-19 11:16:58 -05:00
|
|
|
};
|
|
|
|
|
2015-04-27 09:32:57 -05:00
|
|
|
char*
|
|
|
|
getdns_port_str_array[] = {
|
2015-04-30 13:07:49 -05:00
|
|
|
GETDNS_STR_PORT_DNS,
|
|
|
|
GETDNS_STR_PORT_DNS,
|
2015-06-22 12:02:28 -05:00
|
|
|
GETDNS_STR_PORT_DNS_OVER_TLS
|
2015-04-27 09:32:57 -05:00
|
|
|
};
|
2015-04-19 11:16:58 -05:00
|
|
|
|
2013-11-06 12:32:05 -06:00
|
|
|
/* Private functions */
|
2015-08-19 09:15:26 -05:00
|
|
|
static getdns_return_t create_default_namespaces(struct getdns_context *context);
|
|
|
|
static getdns_return_t create_default_dns_transports(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 set_os_defaults(struct getdns_context *);
|
2013-11-06 12:32:05 -06:00
|
|
|
static int transaction_id_cmp(const void *, const void *);
|
2013-12-06 08:54:06 -06:00
|
|
|
static void 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 */
|
2015-08-19 03:47:46 -05:00
|
|
|
#ifdef HAVE_LIBUNBOUND
|
2014-02-19 13:56:37 -06:00
|
|
|
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);
|
2015-06-17 11:18:09 -05:00
|
|
|
static getdns_return_t set_ub_dns_transport(struct getdns_context*);
|
2014-02-19 13:56:37 -06:00
|
|
|
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*,
|
2014-10-23 07:30:23 -05:00
|
|
|
int);
|
2015-08-19 03:47:46 -05:00
|
|
|
#endif
|
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
|
|
|
|
2015-08-19 09:07:01 -05:00
|
|
|
static void destroy_local_host(_getdns_rbnode_t * node, void *arg)
|
2014-09-25 09:25:22 -05:00
|
|
|
{
|
2014-10-23 16:00:30 -05:00
|
|
|
getdns_context *context = (getdns_context *)arg;
|
|
|
|
host_name_addrs *hnas = (host_name_addrs *)node;
|
2015-03-12 15:37:03 -05:00
|
|
|
getdns_list_destroy(hnas->ipv4addrs);
|
|
|
|
getdns_list_destroy(hnas->ipv6addrs);
|
2014-10-23 16:00:30 -05:00
|
|
|
GETDNS_FREE(context->my_mf, hnas);
|
2014-09-25 09:25:22 -05:00
|
|
|
}
|
|
|
|
|
2013-08-12 16:38:05 -05:00
|
|
|
/**
|
|
|
|
* Helper to get default lookup namespaces.
|
|
|
|
* TODO: Determine from OS
|
|
|
|
*/
|
2015-08-19 09:15:26 -05:00
|
|
|
static 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
|
|
|
}
|
|
|
|
|
2015-06-19 12:28:29 -05:00
|
|
|
/**
|
|
|
|
* Helper to get default transports.
|
|
|
|
*/
|
2015-08-19 09:15:26 -05:00
|
|
|
static getdns_return_t
|
2015-06-19 12:28:29 -05:00
|
|
|
create_default_dns_transports(struct getdns_context *context)
|
2015-06-17 11:18:09 -05:00
|
|
|
{
|
2015-06-19 12:28:29 -05:00
|
|
|
context->dns_transports = GETDNS_XMALLOC(context->my_mf, getdns_transport_list_t, 2);
|
|
|
|
if(context->dns_transports == NULL)
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
2015-06-17 11:18:09 -05:00
|
|
|
|
2015-06-19 12:28:29 -05:00
|
|
|
context->dns_transports[0] = GETDNS_TRANSPORT_UDP;
|
|
|
|
context->dns_transports[1] = GETDNS_TRANSPORT_TCP;
|
|
|
|
context->dns_transport_count = 2;
|
|
|
|
context->dns_transport_current = 0;
|
2015-06-17 11:18:09 -05:00
|
|
|
|
2015-06-19 12:28:29 -05:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2015-06-17 11:18:09 -05:00
|
|
|
}
|
|
|
|
|
2015-03-12 15:37:03 -05:00
|
|
|
static inline void canonicalize_dname(uint8_t *dname)
|
|
|
|
{
|
|
|
|
uint8_t *next_label;
|
|
|
|
|
|
|
|
while (*dname) {
|
|
|
|
next_label = dname + *dname + 1;
|
|
|
|
dname += 1;
|
|
|
|
while (dname < next_label) {
|
|
|
|
*dname = (uint8_t)tolower((unsigned char)*dname);
|
|
|
|
dname++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
canonical_dname_compare(register const uint8_t *d1, register const uint8_t *d2)
|
|
|
|
{
|
|
|
|
register uint8_t lab1, lab2;
|
|
|
|
|
|
|
|
assert(d1 && d2);
|
|
|
|
|
|
|
|
lab1 = *d1++;
|
|
|
|
lab2 = *d2++;
|
|
|
|
while (lab1 != 0 || lab2 != 0) {
|
|
|
|
/* compare label length */
|
|
|
|
/* if one dname ends, it has labellength 0 */
|
|
|
|
if (lab1 != lab2) {
|
|
|
|
if (lab1 < lab2)
|
|
|
|
return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
while (lab1--) {
|
|
|
|
/* compare bytes first for speed */
|
|
|
|
if (*d1 != *d2) {
|
|
|
|
if (*d1 < *d2)
|
|
|
|
return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
d1++;
|
|
|
|
d2++;
|
|
|
|
}
|
|
|
|
/* next pair of labels. */
|
|
|
|
lab1 = *d1++;
|
|
|
|
lab2 = *d2++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
local_host_cmp(const void *id1, const void *id2)
|
|
|
|
{
|
|
|
|
return canonical_dname_compare(id1, id2);
|
|
|
|
}
|
|
|
|
|
2014-10-23 16:00:30 -05:00
|
|
|
static void
|
2015-03-12 15:37:03 -05:00
|
|
|
add_local_host(getdns_context *context, getdns_dict *address, const char *str)
|
2014-09-23 06:36:26 -05:00
|
|
|
{
|
2015-03-12 15:37:03 -05:00
|
|
|
uint8_t host_name[256];
|
|
|
|
size_t host_name_len = sizeof(host_name);
|
2014-10-23 16:00:30 -05:00
|
|
|
host_name_addrs *hnas;
|
2015-03-12 15:37:03 -05:00
|
|
|
getdns_bindata *address_type;
|
|
|
|
int hnas_found = 0;
|
|
|
|
getdns_list **addrs;
|
2014-09-25 09:25:22 -05:00
|
|
|
|
2015-03-12 15:37:03 -05:00
|
|
|
if (gldns_str2wire_dname_buf(str, host_name, &host_name_len))
|
2014-10-23 16:00:30 -05:00
|
|
|
return;
|
2014-09-23 06:36:26 -05:00
|
|
|
|
2015-03-12 15:37:03 -05:00
|
|
|
canonicalize_dname(host_name);
|
|
|
|
|
2015-08-19 09:07:01 -05:00
|
|
|
if (!(hnas = (host_name_addrs *)_getdns_rbtree_search(
|
2015-03-12 15:37:03 -05:00
|
|
|
&context->local_hosts, host_name))) {
|
2014-10-23 16:00:30 -05:00
|
|
|
|
2015-03-12 15:37:03 -05:00
|
|
|
if (!(hnas = (host_name_addrs *)GETDNS_XMALLOC(context->mf,
|
|
|
|
uint8_t, sizeof(host_name_addrs) + host_name_len)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
hnas->ipv4addrs = NULL;
|
|
|
|
hnas->ipv6addrs = NULL;
|
|
|
|
(void)memcpy(hnas->host_name, host_name, host_name_len);
|
|
|
|
hnas->node.key = &hnas->host_name;
|
|
|
|
|
|
|
|
} else
|
|
|
|
hnas_found = 1;
|
|
|
|
|
|
|
|
if (getdns_dict_get_bindata(address, "address_type", &address_type) ||
|
|
|
|
|
|
|
|
address_type->size < 4 ||
|
|
|
|
|
|
|
|
!(addrs = address_type->data[3] == '4'? &hnas->ipv4addrs
|
|
|
|
: address_type->data[3] == '6'? &hnas->ipv4addrs : NULL)) {
|
2014-10-23 16:00:30 -05:00
|
|
|
|
2015-03-12 15:37:03 -05:00
|
|
|
if (!hnas_found) GETDNS_FREE(context->mf, hnas);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!*addrs && !(*addrs = getdns_list_create_with_context(context))) {
|
|
|
|
if (!hnas_found) GETDNS_FREE(context->mf, hnas);
|
|
|
|
return;
|
|
|
|
}
|
2015-08-19 09:30:15 -05:00
|
|
|
if (_getdns_list_append_dict(*addrs, address) && !hnas_found) {
|
2015-03-12 15:37:03 -05:00
|
|
|
getdns_list_destroy(*addrs);
|
|
|
|
GETDNS_FREE(context->mf, hnas);
|
|
|
|
|
|
|
|
} else if (!hnas_found)
|
2015-08-19 09:07:01 -05:00
|
|
|
(void)_getdns_rbtree_insert(&context->local_hosts, &hnas->node);
|
2015-03-12 15:37:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static getdns_dict *
|
2015-03-12 16:41:39 -05:00
|
|
|
sockaddr_dict(getdns_context *context, struct sockaddr *sa)
|
2015-03-12 15:37:03 -05:00
|
|
|
{
|
|
|
|
getdns_dict *address = getdns_dict_create_with_context(context);
|
|
|
|
char addrstr[1024], *b;
|
|
|
|
getdns_bindata bindata;
|
2015-03-12 16:41:39 -05:00
|
|
|
uint16_t port;
|
2015-03-12 15:37:03 -05:00
|
|
|
|
|
|
|
if (!address)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
switch (sa->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
if (getdns_dict_util_set_string(address,"address_type","IPv4"))
|
|
|
|
break;
|
|
|
|
|
|
|
|
bindata.size = 4;
|
|
|
|
bindata.data = (void *)&((struct sockaddr_in*)sa)->sin_addr;
|
|
|
|
if ((getdns_dict_set_bindata(address,"address_data",&bindata)))
|
|
|
|
break;
|
|
|
|
|
2015-03-12 16:41:39 -05:00
|
|
|
port = ntohs(((struct sockaddr_in *)sa)->sin_port);
|
2015-04-30 13:07:49 -05:00
|
|
|
if (port != GETDNS_PORT_ZERO && port != GETDNS_PORT_DNS &&
|
2015-03-12 16:41:39 -05:00
|
|
|
getdns_dict_set_int(address, "port", (uint32_t)port))
|
2015-03-12 15:37:03 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
return address;
|
|
|
|
|
|
|
|
case AF_INET6:
|
|
|
|
if (getdns_dict_util_set_string(address,"address_type","IPv6"))
|
|
|
|
break;
|
|
|
|
|
|
|
|
bindata.size = 16;
|
|
|
|
bindata.data = (void *)&((struct sockaddr_in6*)sa)->sin6_addr;
|
|
|
|
if ((getdns_dict_set_bindata(address,"address_data",&bindata)))
|
|
|
|
break;
|
|
|
|
|
2015-03-12 16:41:39 -05:00
|
|
|
port = ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
|
2015-04-30 13:07:49 -05:00
|
|
|
if (port != GETDNS_PORT_DNS && port != GETDNS_PORT_DNS &&
|
2015-03-12 16:41:39 -05:00
|
|
|
getdns_dict_set_int(address, "port", (uint32_t)port))
|
2015-03-12 15:37:03 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Try to get scope_id too */
|
|
|
|
if (getnameinfo(sa, sizeof(struct sockaddr_in6),
|
|
|
|
addrstr, sizeof(addrstr), NULL, 0, NI_NUMERICHOST))
|
|
|
|
break;
|
|
|
|
if ((b = strchr(addrstr, '%')) &&
|
|
|
|
getdns_dict_util_set_string(address, "scope_id", b+1))
|
|
|
|
break;
|
|
|
|
|
|
|
|
return address;
|
|
|
|
default:
|
|
|
|
/* Unknown protocol */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
getdns_dict_destroy(address);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static getdns_dict *
|
2015-03-12 16:41:39 -05:00
|
|
|
str_addr_dict(getdns_context *context, const char *str)
|
2015-03-12 15:37:03 -05:00
|
|
|
{
|
|
|
|
static struct addrinfo hints = { .ai_family = AF_UNSPEC
|
|
|
|
, .ai_flags = AI_NUMERICHOST };
|
|
|
|
struct addrinfo *ai;
|
|
|
|
getdns_dict *address;
|
|
|
|
|
|
|
|
if (getaddrinfo(str, NULL, &hints, &ai))
|
|
|
|
return NULL;
|
2015-05-13 05:39:24 -05:00
|
|
|
if (!ai)
|
|
|
|
return NULL;
|
|
|
|
|
2015-03-12 16:41:39 -05:00
|
|
|
address = sockaddr_dict(context, ai->ai_addr);
|
2015-03-12 15:37:03 -05:00
|
|
|
freeaddrinfo(ai);
|
|
|
|
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
create_local_hosts(getdns_context *context)
|
|
|
|
{
|
|
|
|
/* enough space in buf for longest allowed domain name */
|
|
|
|
char buf[1024];
|
|
|
|
char *pos = buf, prev_c, *start_of_word = NULL;
|
|
|
|
FILE *in;
|
|
|
|
int start_of_line = 1;
|
|
|
|
getdns_dict *address = NULL;
|
|
|
|
|
|
|
|
in = fopen("/etc/hosts", "r");
|
|
|
|
while (fgets(pos, (int)(sizeof(buf) - (pos - buf)), in)) {
|
|
|
|
pos = buf;
|
|
|
|
/* Break out of for to read more */
|
|
|
|
for (;;) {
|
|
|
|
/* Skip whitespace */
|
|
|
|
while (*pos == ' ' || *pos == '\f'
|
|
|
|
|| *pos == '\t' || *pos == '\v')
|
|
|
|
pos++;
|
|
|
|
|
|
|
|
if (*pos == '\0') { /* End of read data */
|
|
|
|
pos = buf;
|
|
|
|
goto read_more;
|
|
|
|
|
|
|
|
} else if (*pos == '#' || *pos == '\r' || *pos == '\n')
|
|
|
|
/* Comments or end of line */
|
|
|
|
break; /* skip to next line */
|
|
|
|
|
|
|
|
assert(*pos && !isspace(*pos));
|
|
|
|
|
|
|
|
start_of_word = pos;
|
|
|
|
|
|
|
|
/* Search for end of word */
|
|
|
|
while (*pos && !isspace(*pos))
|
|
|
|
pos++;
|
|
|
|
|
|
|
|
/* '\0' before whitespace, so either the word did not
|
|
|
|
* fit, or we are at the end of the file.
|
|
|
|
*/
|
|
|
|
if (*pos == '\0') {
|
|
|
|
if (start_of_word == buf) /* word too big */
|
|
|
|
break; /* skip to next line */
|
|
|
|
|
|
|
|
/* Move word to fit in buffer */
|
|
|
|
memmove(buf,start_of_word,pos - start_of_word);
|
|
|
|
pos = buf + (pos - start_of_word);
|
|
|
|
start_of_word = buf;
|
|
|
|
*pos = '\0';
|
|
|
|
goto read_more;
|
2014-10-23 16:00:30 -05:00
|
|
|
}
|
2015-03-12 15:37:03 -05:00
|
|
|
assert(isspace(*pos));
|
|
|
|
prev_c = *pos;
|
|
|
|
*pos = '\0';
|
|
|
|
if (start_of_line) {
|
|
|
|
start_of_line = 0;
|
|
|
|
if (address)
|
|
|
|
getdns_dict_destroy(address);
|
|
|
|
if (!(address =
|
2015-03-12 16:41:39 -05:00
|
|
|
str_addr_dict(context, start_of_word)))
|
2015-03-12 15:37:03 -05:00
|
|
|
/* Unparseable address */
|
|
|
|
break; /* skip to next line */
|
|
|
|
} else
|
|
|
|
add_local_host(context, address, start_of_word);
|
|
|
|
|
|
|
|
start_of_word = NULL;
|
|
|
|
*pos = prev_c;
|
|
|
|
/* process next word in buf */
|
2014-10-23 16:00:30 -05:00
|
|
|
}
|
2015-03-12 15:37:03 -05:00
|
|
|
/* skip to next line */
|
|
|
|
while (*pos != '\r' && *pos != '\n')
|
|
|
|
if (*pos)
|
|
|
|
pos++;
|
|
|
|
else if (!fgets((pos = buf), sizeof(buf), in))
|
|
|
|
break; /* We're done */
|
|
|
|
start_of_line = 1;
|
|
|
|
if (address) {
|
|
|
|
getdns_dict_destroy(address);
|
|
|
|
address = NULL;
|
|
|
|
}
|
|
|
|
pos = buf;
|
|
|
|
read_more: ;
|
|
|
|
}
|
|
|
|
fclose(in);
|
|
|
|
if (address) {
|
|
|
|
/* One last name for this address? */
|
|
|
|
if (start_of_word && !start_of_line)
|
|
|
|
add_local_host(context, address, start_of_word);
|
|
|
|
getdns_dict_destroy(address);
|
2014-10-23 16:00:30 -05:00
|
|
|
}
|
2014-09-23 06:36:26 -05:00
|
|
|
}
|
|
|
|
|
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-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
|
2015-08-19 09:33:19 -05:00
|
|
|
_getdns_filechg_check(struct getdns_context *context, struct filechg *fchg)
|
2014-02-10 18:59:45 -06:00
|
|
|
{
|
|
|
|
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 */
|
|
|
|
|
2014-10-01 16:16:43 -05:00
|
|
|
static getdns_upstreams *
|
|
|
|
upstreams_create(getdns_context *context, size_t size)
|
|
|
|
{
|
|
|
|
getdns_upstreams *r = (void *) GETDNS_XMALLOC(context->mf, char,
|
|
|
|
sizeof(getdns_upstreams) +
|
2014-10-16 07:24:13 -05:00
|
|
|
sizeof(getdns_upstream) * size);
|
2014-10-01 16:16:43 -05:00
|
|
|
r->mf = context->mf;
|
|
|
|
r->referenced = 1;
|
|
|
|
r->count = 0;
|
2014-10-16 07:24:13 -05:00
|
|
|
r->current = 0;
|
2014-10-01 16:16:43 -05:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static getdns_upstreams *
|
|
|
|
upstreams_resize(getdns_upstreams *upstreams, size_t size)
|
|
|
|
{
|
|
|
|
getdns_upstreams *r = (void *) GETDNS_XREALLOC(
|
|
|
|
upstreams->mf, upstreams, char,
|
|
|
|
sizeof(getdns_upstreams) +
|
2014-10-16 07:24:13 -05:00
|
|
|
sizeof(getdns_upstream) * size);
|
2014-10-01 16:16:43 -05:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-04-18 15:17:28 -05:00
|
|
|
void
|
2015-08-19 09:22:38 -05:00
|
|
|
_getdns_upstreams_dereference(getdns_upstreams *upstreams)
|
2014-12-07 13:03:34 -06:00
|
|
|
{
|
2015-07-14 03:44:15 -05:00
|
|
|
getdns_upstream *upstream;
|
2014-12-07 13:03:34 -06:00
|
|
|
|
2015-07-14 04:11:06 -05:00
|
|
|
if (!upstreams || --upstreams->referenced > 0)
|
2015-07-14 03:44:15 -05:00
|
|
|
return;
|
|
|
|
|
|
|
|
for ( upstream = upstreams->upstreams
|
|
|
|
; upstreams->count
|
|
|
|
; upstreams->count--, upstream++ ) {
|
|
|
|
|
2015-07-14 04:11:06 -05:00
|
|
|
if (upstream->loop && ( upstream->event.read_cb
|
|
|
|
|| upstream->event.write_cb
|
|
|
|
|| upstream->event.timeout_cb) ) {
|
2015-07-14 03:44:15 -05:00
|
|
|
|
2015-07-14 04:11:06 -05:00
|
|
|
upstream->event.read_cb = NULL;
|
|
|
|
upstream->event.write_cb = NULL;
|
|
|
|
upstream->event.timeout_cb = NULL;
|
2015-07-14 03:44:15 -05:00
|
|
|
GETDNS_CLEAR_EVENT(upstream->loop, &upstream->event);
|
2015-07-14 04:11:06 -05:00
|
|
|
}
|
2015-07-14 03:44:15 -05:00
|
|
|
if (upstream->tls_obj != NULL) {
|
|
|
|
SSL_shutdown(upstream->tls_obj);
|
|
|
|
SSL_free(upstream->tls_obj);
|
2015-04-18 15:17:28 -05:00
|
|
|
}
|
2015-07-14 03:44:15 -05:00
|
|
|
if (upstream->fd != -1)
|
|
|
|
close(upstream->fd);
|
2015-04-18 15:17:28 -05:00
|
|
|
}
|
2015-07-14 03:44:15 -05:00
|
|
|
GETDNS_FREE(upstreams->mf, upstreams);
|
2014-10-01 16:16:43 -05:00
|
|
|
}
|
|
|
|
|
2015-06-29 03:09:13 -05:00
|
|
|
void
|
2015-08-19 09:22:38 -05:00
|
|
|
_getdns_upstream_shutdown(getdns_upstream *upstream)
|
2015-07-14 04:11:06 -05:00
|
|
|
{
|
2015-06-29 03:09:13 -05:00
|
|
|
/*There is a race condition with a new request being scheduled
|
|
|
|
while this happens so take ownership of the fd asap*/
|
|
|
|
int fd = upstream->fd;
|
|
|
|
upstream->fd = -1;
|
|
|
|
/* If the connection had a problem, but had worked this time,
|
|
|
|
* then allow re-use in the future*/
|
|
|
|
if (upstream->tcp.write_error == 1 &&
|
|
|
|
upstream->responses_received > 0)
|
|
|
|
upstream->tcp.write_error = 0;
|
|
|
|
upstream->writes_done = 0;
|
|
|
|
upstream->responses_received = 0;
|
|
|
|
if (upstream->tls_hs_state != GETDNS_HS_FAILED)
|
|
|
|
upstream->tls_hs_state = GETDNS_HS_NONE;
|
|
|
|
/* Now TLS stuff*/
|
|
|
|
if (upstream->tls_obj != NULL) {
|
|
|
|
SSL_shutdown(upstream->tls_obj);
|
|
|
|
SSL_free(upstream->tls_obj);
|
|
|
|
upstream->tls_obj = NULL;
|
|
|
|
}
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2015-08-14 11:55:43 -05:00
|
|
|
static int
|
|
|
|
tls_is_in_transports_list(getdns_context *context) {
|
|
|
|
for (int i=0; i< context->dns_transport_count;i++) {
|
|
|
|
if (context->dns_transports[i] == GETDNS_TRANSPORT_TLS ||
|
|
|
|
context->dns_transports[i] == GETDNS_TRANSPORT_STARTTLS)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
tls_only_is_in_transports_list(getdns_context *context) {
|
|
|
|
if (context->dns_transport_count != 1)
|
|
|
|
return 0;
|
|
|
|
if (context->dns_transports[0] == GETDNS_TRANSPORT_TLS ||
|
|
|
|
context->dns_transports[0] == GETDNS_TRANSPORT_STARTTLS)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-16 04:28:32 -05:00
|
|
|
static int
|
|
|
|
net_req_query_id_cmp(const void *id1, const void *id2)
|
|
|
|
{
|
2014-10-17 17:25:41 -05:00
|
|
|
return (intptr_t)id1 - (intptr_t)id2;
|
2014-10-16 04:28:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-10-17 17:25:41 -05:00
|
|
|
upstream_init(getdns_upstream *upstream,
|
|
|
|
getdns_upstreams *parent, struct addrinfo *ai)
|
2014-10-16 04:28:32 -05:00
|
|
|
{
|
2014-10-17 17:25:41 -05:00
|
|
|
upstream->upstreams = parent;
|
|
|
|
|
2014-10-16 04:28:32 -05:00
|
|
|
upstream->addr_len = ai->ai_addrlen;
|
|
|
|
(void) memcpy(&upstream->addr, ai->ai_addr, ai->ai_addrlen);
|
2014-10-17 17:25:41 -05:00
|
|
|
|
|
|
|
/* How is this upstream doing? */
|
2015-06-24 12:49:34 -05:00
|
|
|
upstream->writes_done = 0;
|
2015-06-29 03:09:13 -05:00
|
|
|
upstream->responses_received = 0;
|
2014-10-16 04:28:32 -05:00
|
|
|
upstream->to_retry = 2;
|
2014-10-16 07:24:13 -05:00
|
|
|
upstream->back_off = 1;
|
2014-10-17 17:25:41 -05:00
|
|
|
|
|
|
|
/* For sharing a socket to this upstream with TCP */
|
|
|
|
upstream->fd = -1;
|
2014-12-07 13:03:34 -06:00
|
|
|
upstream->tls_obj = NULL;
|
2015-04-29 13:20:25 -05:00
|
|
|
upstream->starttls_req = NULL;
|
2015-06-19 12:28:29 -05:00
|
|
|
upstream->transport = GETDNS_TRANSPORT_TCP;
|
2015-04-19 11:16:58 -05:00
|
|
|
upstream->tls_hs_state = GETDNS_HS_NONE;
|
2015-07-19 06:18:38 -05:00
|
|
|
upstream->tls_auth_name[0] = '\0';
|
2015-06-24 12:49:34 -05:00
|
|
|
upstream->tcp.write_error = 0;
|
2014-10-17 17:25:41 -05:00
|
|
|
upstream->loop = NULL;
|
2014-10-18 07:32:55 -05:00
|
|
|
(void) getdns_eventloop_event_init(
|
|
|
|
&upstream->event, upstream, NULL, NULL, NULL);
|
2014-10-17 17:25:41 -05:00
|
|
|
(void) memset(&upstream->tcp, 0, sizeof(upstream->tcp));
|
|
|
|
|
|
|
|
upstream->write_queue = NULL;
|
2014-10-18 07:32:55 -05:00
|
|
|
upstream->write_queue_last = NULL;
|
2014-10-17 17:25:41 -05:00
|
|
|
|
2015-03-22 10:50:48 -05:00
|
|
|
upstream->has_client_cookie = 0;
|
|
|
|
upstream->has_prev_client_cookie = 0;
|
|
|
|
upstream->has_server_cookie = 0;
|
|
|
|
|
2014-10-17 17:25:41 -05:00
|
|
|
/* Tracking of network requests on this socket */
|
2015-08-19 09:07:01 -05:00
|
|
|
_getdns_rbtree_init(&upstream->netreq_by_query_id,
|
2014-10-16 04:28:32 -05:00
|
|
|
net_req_query_id_cmp);
|
|
|
|
}
|
|
|
|
|
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-09-30 08:12:48 -05:00
|
|
|
FILE *in;
|
|
|
|
char line[1024], domain[1024];
|
|
|
|
char *parse, *token, prev_ch;
|
|
|
|
size_t upstreams_limit = 10, length;
|
|
|
|
struct getdns_bindata bindata;
|
|
|
|
struct addrinfo hints;
|
|
|
|
struct addrinfo *result;
|
2014-10-16 07:24:13 -05:00
|
|
|
getdns_upstream *upstream;
|
2014-09-30 08:12:48 -05:00
|
|
|
int s;
|
|
|
|
|
|
|
|
if(context->fchg_resolvconf == NULL) {
|
|
|
|
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;
|
|
|
|
}
|
2015-08-19 09:33:19 -05:00
|
|
|
_getdns_filechg_check(context, context->fchg_resolvconf);
|
2014-02-10 18:59:45 -06:00
|
|
|
|
2014-09-30 08:12:48 -05:00
|
|
|
context->suffix = getdns_list_create_with_context(context);
|
2014-10-01 16:16:43 -05:00
|
|
|
context->upstreams = upstreams_create(context, upstreams_limit);
|
2014-09-30 08:12:48 -05:00
|
|
|
|
|
|
|
in = fopen(context->fchg_resolvconf->fn, "r");
|
|
|
|
if (!in)
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2014-12-06 22:46:44 -06:00
|
|
|
|
2014-09-30 08:12:48 -05:00
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
|
|
|
|
hints.ai_socktype = 0; /* Datagram socket */
|
|
|
|
hints.ai_flags = AI_NUMERICHOST; /* No reverse name lookups */
|
|
|
|
hints.ai_protocol = 0; /* Any protocol */
|
|
|
|
hints.ai_canonname = NULL;
|
|
|
|
hints.ai_addr = NULL;
|
|
|
|
hints.ai_next = NULL;
|
|
|
|
|
|
|
|
*domain = 0;
|
|
|
|
while (fgets(line, (int)sizeof(line), in)) {
|
|
|
|
line[sizeof(line)-1] = 0;
|
|
|
|
/* parse = line + strspn(line, " \t"); */ /* No leading whitespace */
|
|
|
|
parse = line;
|
|
|
|
|
|
|
|
if (strncmp(parse, "domain", 6) == 0) {
|
|
|
|
parse += 6;
|
|
|
|
parse += strspn(parse, " \t");
|
|
|
|
if (*parse == 0 || *parse == '#') continue;
|
|
|
|
token = parse + strcspn(parse, " \t\r\n");
|
|
|
|
*token = 0;
|
|
|
|
|
|
|
|
(void) strcpy(domain, parse);
|
|
|
|
|
|
|
|
} else if (strncmp(parse, "search", 6) == 0) {
|
|
|
|
parse += 6;
|
|
|
|
do {
|
|
|
|
parse += strspn(parse, " \t");
|
|
|
|
if (*parse == '#' || *parse == '\n') break;
|
|
|
|
token = parse + strcspn(parse, " \t\r\n");
|
|
|
|
prev_ch = *token;
|
|
|
|
*token = 0;
|
|
|
|
|
|
|
|
bindata.data = (uint8_t *)parse;
|
|
|
|
bindata.size = strlen(parse) + 1;
|
|
|
|
(void) getdns_list_get_length(
|
|
|
|
context->suffix, &length);
|
|
|
|
(void) getdns_list_set_bindata(
|
|
|
|
context->suffix, length, &bindata);
|
|
|
|
|
|
|
|
*token = prev_ch;
|
|
|
|
parse = token;
|
|
|
|
} while (*parse);
|
|
|
|
|
|
|
|
} else if (strncmp(parse, "nameserver", 10) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
parse += 10;
|
|
|
|
parse += strspn(parse, " \t");
|
|
|
|
if (*parse == 0 || *parse == '#') continue;
|
|
|
|
token = parse + strcspn(parse, " \t\r\n");
|
|
|
|
*token = 0;
|
|
|
|
|
2015-06-19 12:28:29 -05:00
|
|
|
for (size_t i = 0; i < GETDNS_UPSTREAM_TRANSPORTS; i++) {
|
|
|
|
char *port_str = getdns_port_str_array[i];
|
2015-04-29 13:20:25 -05:00
|
|
|
if ((s = getaddrinfo(parse, port_str, &hints, &result)))
|
2015-04-19 11:16:58 -05:00
|
|
|
continue;
|
2015-05-13 05:39:24 -05:00
|
|
|
if (!result)
|
|
|
|
continue;
|
2015-04-19 11:16:58 -05:00
|
|
|
|
|
|
|
/* Grow array when needed */
|
|
|
|
if (context->upstreams->count == upstreams_limit)
|
|
|
|
context->upstreams = upstreams_resize(
|
|
|
|
context->upstreams, (upstreams_limit *= 2));
|
|
|
|
|
|
|
|
upstream = &context->upstreams->
|
|
|
|
upstreams[context->upstreams->count++];
|
|
|
|
upstream_init(upstream, context->upstreams, result);
|
2015-06-19 12:28:29 -05:00
|
|
|
upstream->transport = getdns_upstream_transports[i];
|
2015-05-13 05:39:24 -05:00
|
|
|
freeaddrinfo(result);
|
2015-04-24 10:29:08 -05:00
|
|
|
}
|
2014-09-30 08:12:48 -05:00
|
|
|
}
|
|
|
|
fclose(in);
|
2014-02-03 14:39:09 -06:00
|
|
|
|
2014-10-23 08:32:35 -05:00
|
|
|
(void) getdns_list_get_length(context->suffix, &length);
|
|
|
|
if (length == 0 && *domain != 0) {
|
|
|
|
bindata.data = (uint8_t *)domain;
|
|
|
|
bindata.size = strlen(domain) + 1;
|
|
|
|
(void) getdns_list_set_bindata(context->suffix, 0, &bindata);
|
|
|
|
}
|
2014-09-30 08:12:48 -05: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
|
|
|
}
|
|
|
|
|
2015-04-02 15:01:30 -05:00
|
|
|
static void
|
|
|
|
NULL_update_callback(
|
|
|
|
getdns_context *context, getdns_context_code_t code, void *userarg)
|
|
|
|
{ (void)context; (void)code; (void)userarg; }
|
|
|
|
|
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-10-08 08:42:33 -05:00
|
|
|
getdns_return_t r;
|
|
|
|
struct getdns_context *result = NULL;
|
|
|
|
mf_union mf;
|
2015-06-30 07:43:52 -05:00
|
|
|
gldns_buffer gbuf;
|
2013-08-12 18:47:33 -05:00
|
|
|
|
2014-10-08 08:42:33 -05:00
|
|
|
if (!context || !malloc || !realloc || !free)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2014-10-08 08:42:33 -05:00
|
|
|
/** default init **/
|
|
|
|
mf.ext.malloc = malloc;
|
|
|
|
result = userarg == MF_PLAIN
|
|
|
|
? (*mf.pln.malloc)( sizeof(struct getdns_context))
|
|
|
|
: (*mf.ext.malloc)(userarg, sizeof(struct getdns_context));
|
2013-08-13 15:10:21 -05:00
|
|
|
|
2014-10-08 08:42:33 -05:00
|
|
|
if (!result)
|
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
2013-12-08 15:56:34 -06:00
|
|
|
|
2014-10-08 08:42:33 -05:00
|
|
|
result->processing = 0;
|
|
|
|
result->destroying = 0;
|
|
|
|
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-10-15 16:28:23 -05:00
|
|
|
|
2015-04-02 07:42:26 -05:00
|
|
|
result->update_callback = NULL;
|
2015-04-02 15:01:30 -05:00
|
|
|
result->update_callback2 = NULL_update_callback;
|
2015-04-02 07:42:26 -05:00
|
|
|
result->update_userarg = NULL;
|
2013-10-15 16:28:23 -05:00
|
|
|
|
2014-10-08 08:42:33 -05:00
|
|
|
result->mf.mf_arg = userarg;
|
|
|
|
result->mf.mf.ext.malloc = malloc;
|
|
|
|
result->mf.mf.ext.realloc = realloc;
|
|
|
|
result->mf.mf.ext.free = free;
|
2014-02-20 16:05:15 -06:00
|
|
|
|
2014-10-08 08:42:33 -05:00
|
|
|
result->resolution_type_set = 0;
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2015-08-19 09:07:01 -05:00
|
|
|
_getdns_rbtree_init(&result->outbound_requests, transaction_id_cmp);
|
|
|
|
_getdns_rbtree_init(&result->local_hosts, local_host_cmp);
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-10-08 08:42:33 -05:00
|
|
|
result->resolution_type = GETDNS_RESOLUTION_RECURSING;
|
|
|
|
if ((r = create_default_namespaces(result)))
|
|
|
|
goto error;
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-10-08 08:42:33 -05:00
|
|
|
result->timeout = 5000;
|
2015-06-18 11:11:11 -05:00
|
|
|
result->idle_timeout = 0;
|
2014-10-08 08:42:33 -05:00
|
|
|
result->follow_redirects = GETDNS_REDIRECTS_FOLLOW;
|
|
|
|
result->dns_root_servers = create_default_root_servers();
|
|
|
|
result->append_name = GETDNS_APPEND_NAME_ALWAYS;
|
|
|
|
result->suffix = NULL;
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2015-06-30 07:43:52 -05:00
|
|
|
gldns_buffer_init_frm_data(&gbuf, result->trust_anchors_spc
|
|
|
|
, sizeof(result->trust_anchors_spc));
|
|
|
|
|
|
|
|
if (!_getdns_parse_ta_file(NULL, &gbuf)) {
|
|
|
|
result->trust_anchors = NULL;
|
|
|
|
result->trust_anchors_len = 0;
|
|
|
|
|
|
|
|
} else if ((result->trust_anchors_len = gldns_buffer_position(&gbuf))
|
|
|
|
> sizeof(result->trust_anchors_spc)) {
|
|
|
|
|
|
|
|
if ((result->trust_anchors = GETDNS_XMALLOC(
|
|
|
|
result->mf, uint8_t, result->trust_anchors_len))) {
|
|
|
|
|
|
|
|
gldns_buffer_init_frm_data(&gbuf
|
|
|
|
, result->trust_anchors
|
|
|
|
, result->trust_anchors_len);
|
|
|
|
if (!_getdns_parse_ta_file(NULL, &gbuf)) {
|
|
|
|
result->trust_anchors = NULL;
|
|
|
|
result->trust_anchors_len = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
result->trust_anchors = result->trust_anchors_spc;
|
|
|
|
|
2014-10-14 18:13:39 -05:00
|
|
|
result->upstreams = NULL;
|
2013-08-12 16:38:05 -05:00
|
|
|
|
2014-10-08 08:42:33 -05:00
|
|
|
result->edns_extended_rcode = 0;
|
|
|
|
result->edns_version = 0;
|
2014-10-28 08:32:29 -05:00
|
|
|
result->edns_do_bit = 0;
|
2014-12-07 13:03:34 -06:00
|
|
|
result-> tls_ctx = NULL;
|
2014-10-08 08:42:33 -05:00
|
|
|
|
|
|
|
result->extension = &result->mini_event.loop;
|
2015-08-19 09:26:39 -05:00
|
|
|
if ((r = _getdns_mini_event_init(result, &result->mini_event)))
|
2014-10-08 08:42:33 -05:00
|
|
|
goto error;
|
2014-01-22 18:55:04 -06:00
|
|
|
|
2014-02-10 18:59:45 -06:00
|
|
|
result->fchg_resolvconf = NULL;
|
2015-06-30 07:43:52 -05:00
|
|
|
result->fchg_hosts = NULL;
|
2014-09-23 06:36:26 -05:00
|
|
|
|
2014-10-08 08:42:33 -05:00
|
|
|
if (set_from_os && (r = set_os_defaults(result)))
|
|
|
|
goto error;
|
2014-02-20 16:05:15 -06:00
|
|
|
|
2014-10-08 08:42:33 -05:00
|
|
|
result->dnssec_allowed_skew = 0;
|
2014-10-23 07:30:23 -05:00
|
|
|
result->edns_maximum_udp_payload_size = -1;
|
2015-06-19 12:28:29 -05:00
|
|
|
if ((r = create_default_dns_transports(result)))
|
|
|
|
goto error;
|
2014-10-08 08:42:33 -05:00
|
|
|
result->limit_outstanding_queries = 0;
|
|
|
|
result->return_dnssec_status = GETDNS_EXTENSION_FALSE;
|
2013-07-15 16:42:37 -05:00
|
|
|
|
2014-10-08 08:42:33 -05:00
|
|
|
/* unbound context is initialized here */
|
2015-04-17 09:50:08 -05:00
|
|
|
/* Unbound needs SSL to be init'ed this early when TLS is used. However we
|
|
|
|
* don't know that till later so we will have to do this every time. */
|
2015-06-26 04:39:44 -05:00
|
|
|
|
2015-04-17 09:50:08 -05:00
|
|
|
SSL_library_init();
|
2015-08-19 03:47:46 -05:00
|
|
|
#ifdef HAVE_LIBUNBOUND
|
2014-10-08 08:42:33 -05:00
|
|
|
result->unbound_ctx = NULL;
|
|
|
|
if ((r = rebuild_ub_ctx(result)))
|
|
|
|
goto error;
|
2015-08-19 03:47:46 -05:00
|
|
|
#endif
|
2014-10-08 08:42:33 -05:00
|
|
|
|
2014-10-23 16:00:30 -05:00
|
|
|
create_local_hosts(result);
|
2014-10-08 08:42:33 -05:00
|
|
|
|
|
|
|
*context = result;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
error:
|
|
|
|
getdns_context_destroy(result);
|
|
|
|
return r;
|
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
|
|
|
{
|
2015-07-14 03:44:15 -05:00
|
|
|
if (context == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* If being destroyed during getdns callback, fail via assert */
|
|
|
|
assert(context->processing == 0);
|
|
|
|
if (context->destroying)
|
|
|
|
return;
|
|
|
|
|
|
|
|
context->destroying = 1;
|
2014-10-23 16:55:52 -05:00
|
|
|
context->processing = 1;
|
|
|
|
/* cancel all outstanding requests */
|
|
|
|
cancel_outstanding_requests(context, 1);
|
2015-07-14 03:44:15 -05:00
|
|
|
|
|
|
|
/* This needs to be done before cleaning the extension, because there
|
|
|
|
* might be an idle_timeout schedules, which will not get unscheduled
|
|
|
|
* with cancel_outstanding_requests.
|
|
|
|
*/
|
2015-08-19 09:22:38 -05:00
|
|
|
_getdns_upstreams_dereference(context->upstreams);
|
2015-07-14 03:44:15 -05:00
|
|
|
|
2015-08-19 03:47:46 -05:00
|
|
|
#ifdef HAVE_LIBUNBOUND
|
2015-07-14 03:44:15 -05:00
|
|
|
if (context->unbound_ctx)
|
|
|
|
ub_ctx_delete(context->unbound_ctx);
|
2015-08-19 03:47:46 -05:00
|
|
|
#endif
|
2015-07-14 03:44:15 -05:00
|
|
|
|
2014-10-23 16:55:52 -05:00
|
|
|
context->processing = 0;
|
|
|
|
context->extension->vmt->cleanup(context->extension);
|
2014-02-20 15:17:41 -06:00
|
|
|
|
2015-07-14 03:44:15 -05:00
|
|
|
if (context->namespaces)
|
|
|
|
GETDNS_FREE(context->my_mf, context->namespaces);
|
|
|
|
|
|
|
|
if (context->dns_transports)
|
|
|
|
GETDNS_FREE(context->my_mf, context->dns_transports);
|
|
|
|
|
|
|
|
if(context->fchg_resolvconf) {
|
2014-02-10 18:59:45 -06:00
|
|
|
if(context->fchg_resolvconf->prevstat)
|
|
|
|
GETDNS_FREE(context->my_mf, context->fchg_resolvconf->prevstat);
|
|
|
|
GETDNS_FREE(context->my_mf, context->fchg_resolvconf);
|
|
|
|
}
|
2015-07-14 03:44:15 -05:00
|
|
|
if(context->fchg_hosts) {
|
2014-02-10 18:59:45 -06:00
|
|
|
if(context->fchg_hosts->prevstat)
|
|
|
|
GETDNS_FREE(context->my_mf, context->fchg_hosts->prevstat);
|
|
|
|
GETDNS_FREE(context->my_mf, context->fchg_hosts);
|
|
|
|
}
|
2015-07-14 03:44:15 -05:00
|
|
|
if (context->tls_ctx)
|
2014-12-07 13:03:34 -06:00
|
|
|
SSL_CTX_free(context->tls_ctx);
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2015-07-14 03:44:15 -05:00
|
|
|
getdns_list_destroy(context->dns_root_servers);
|
|
|
|
getdns_list_destroy(context->suffix);
|
2015-06-30 07:43:52 -05:00
|
|
|
|
2015-07-14 03:44:15 -05:00
|
|
|
if (context->trust_anchors &&
|
2015-06-30 07:43:52 -05:00
|
|
|
context->trust_anchors != context->trust_anchors_spc)
|
2015-07-14 03:44:15 -05:00
|
|
|
GETDNS_FREE(context->mf, context->trust_anchors);
|
2013-08-14 16:19:06 -05:00
|
|
|
|
2015-08-19 09:07:01 -05:00
|
|
|
_getdns_traverse_postorder(&context->local_hosts,
|
2014-10-23 16:00:30 -05:00
|
|
|
destroy_local_host, context);
|
2014-09-30 08:12:48 -05:00
|
|
|
|
2015-07-14 03:44:15 -05:00
|
|
|
GETDNS_FREE(context->my_mf, context);
|
2014-02-06 21:40:36 -06:00
|
|
|
} /* 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
|
|
|
|
2015-04-02 07:42:26 -05:00
|
|
|
getdns_return_t
|
|
|
|
getdns_context_set_update_callback(getdns_context *context, void *userarg,
|
2015-04-02 15:01:30 -05:00
|
|
|
void (*cb)(getdns_context *, getdns_context_code_t, void *))
|
2015-04-02 07:42:26 -05:00
|
|
|
{
|
|
|
|
if (!context) return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
context->update_userarg = userarg;
|
2015-04-02 15:01:30 -05:00
|
|
|
context->update_callback2 = cb ? cb : NULL_update_callback;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_get_update_callback(getdns_context *context, void **userarg,
|
|
|
|
void (**cb)(getdns_context *, getdns_context_code_t, void *))
|
|
|
|
{
|
|
|
|
if (!context || !userarg || !cb)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
*userarg = context->update_userarg;
|
|
|
|
*cb = context->update_callback2;
|
2015-04-02 07:42:26 -05:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
2015-08-19 03:47:46 -05:00
|
|
|
#ifdef HAVE_LIBUNBOUND
|
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-10-08 08:42:33 -05:00
|
|
|
static void
|
2014-10-13 17:14:25 -05:00
|
|
|
getdns_context_request_count_changed(getdns_context *context)
|
2014-10-08 08:42:33 -05:00
|
|
|
{
|
2014-10-14 18:13:39 -05:00
|
|
|
DEBUG_SCHED("getdns_context_request_count_changed(%d)\n",
|
2014-10-23 16:00:30 -05:00
|
|
|
(int) context->outbound_requests.count);
|
|
|
|
if (context->outbound_requests.count) {
|
2014-10-14 18:13:39 -05:00
|
|
|
if (context->ub_event.ev) return;
|
|
|
|
|
|
|
|
DEBUG_SCHED("gc_request_count_changed "
|
|
|
|
"-> ub schedule(el_ev = %p, el_ev->ev = %p)\n",
|
|
|
|
&context->ub_event, context->ub_event.ev);
|
2014-10-10 07:48:52 -05:00
|
|
|
context->extension->vmt->schedule(
|
2014-10-08 08:42:33 -05:00
|
|
|
context->extension, ub_fd(context->unbound_ctx),
|
|
|
|
TIMEOUT_FOREVER, &context->ub_event);
|
2014-10-14 18:13:39 -05:00
|
|
|
}
|
|
|
|
else if (context->ub_event.ev) /* Only test if count == 0! */ {
|
|
|
|
DEBUG_SCHED("gc_request_count_changed "
|
|
|
|
"-> ub clear(el_ev = %p, el_ev->ev = %p)\n",
|
|
|
|
&context->ub_event, context->ub_event.ev);
|
2014-10-13 17:14:25 -05:00
|
|
|
|
2014-10-10 07:48:52 -05:00
|
|
|
context->extension->vmt->clear(
|
2014-10-08 08:42:33 -05:00
|
|
|
context->extension, &context->ub_event);
|
2014-10-14 18:13:39 -05:00
|
|
|
}
|
2014-10-08 08:42:33 -05:00
|
|
|
}
|
|
|
|
|
2014-10-15 05:16:34 -05:00
|
|
|
void
|
2015-08-19 09:22:38 -05:00
|
|
|
_getdns_context_ub_read_cb(void *userarg)
|
2014-10-08 08:42:33 -05:00
|
|
|
{
|
|
|
|
getdns_context *context = (getdns_context *)userarg;
|
|
|
|
|
2014-10-14 18:13:39 -05:00
|
|
|
/* getdns_context_process_async, but without reinvoking an eventloop
|
|
|
|
* (with context->extension->vmt->run*), because we are already
|
|
|
|
* called from a running eventloop.
|
|
|
|
*/
|
|
|
|
context->processing = 1;
|
2014-10-15 05:16:34 -05:00
|
|
|
if (ub_poll(context->unbound_ctx))
|
|
|
|
(void) ub_process(context->unbound_ctx);
|
2014-10-14 18:13:39 -05:00
|
|
|
context->processing = 0;
|
|
|
|
|
|
|
|
/* No need to handle timeouts. They are handled by the extension. */
|
|
|
|
|
2014-10-13 17:14:25 -05:00
|
|
|
getdns_context_request_count_changed(context);
|
2014-10-08 08:42:33 -05:00
|
|
|
}
|
|
|
|
|
2014-02-19 13:56:37 -06:00
|
|
|
static getdns_return_t
|
|
|
|
rebuild_ub_ctx(struct getdns_context* context) {
|
2014-10-06 16:50:39 -05:00
|
|
|
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();
|
2015-07-09 17:05:26 -05:00
|
|
|
context->unbound_ta_set = 0;
|
2014-10-06 16:50:39 -05:00
|
|
|
if (!context->unbound_ctx) {
|
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
}
|
|
|
|
set_ub_dnssec_allowed_skew(context,
|
|
|
|
context->dnssec_allowed_skew);
|
|
|
|
set_ub_edns_maximum_udp_payload_size(context,
|
|
|
|
context->edns_maximum_udp_payload_size);
|
2015-06-17 11:18:09 -05:00
|
|
|
set_ub_dns_transport(context);
|
2014-10-06 16:50:39 -05:00
|
|
|
|
2014-10-14 18:13:39 -05:00
|
|
|
context->ub_event.userarg = context;
|
2015-08-19 09:22:38 -05:00
|
|
|
context->ub_event.read_cb = _getdns_context_ub_read_cb;
|
2014-10-14 18:13:39 -05:00
|
|
|
context->ub_event.write_cb = NULL;
|
2014-10-08 08:42:33 -05:00
|
|
|
context->ub_event.timeout_cb = NULL;
|
2014-10-14 18:13:39 -05:00
|
|
|
context->ub_event.ev = NULL;
|
2014-10-08 08:42:33 -05:00
|
|
|
|
2014-10-06 16:50:39 -05:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-10-16 13:45:43 -05:00
|
|
|
}
|
2015-08-19 03:47:46 -05:00
|
|
|
#else
|
|
|
|
#define set_ub_string_opt(ctx, opt, value) do {} while (0)
|
|
|
|
#define set_ub_number_opt(ctx, opt, value) do {} while (0)
|
|
|
|
#define getdns_context_request_count_changed(context) do {} while (0)
|
|
|
|
#endif
|
2013-10-16 13:45:43 -05:00
|
|
|
|
2013-08-12 16:38:05 -05:00
|
|
|
/**
|
|
|
|
* Helper to dispatch the updated callback
|
|
|
|
*/
|
2013-11-05 14:03:44 -06:00
|
|
|
static void
|
2013-12-06 08:54:06 -06:00
|
|
|
dispatch_updated(struct getdns_context *context, uint16_t item)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2015-04-02 15:01:30 -05:00
|
|
|
if (context->update_callback2 != NULL_update_callback)
|
2015-04-02 07:42:26 -05:00
|
|
|
context->update_callback2(
|
|
|
|
context, item, context->update_userarg);
|
|
|
|
|
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;
|
|
|
|
}
|
2015-07-09 17:05:26 -05:00
|
|
|
#ifndef STUB_NATIVE_DNSSEC
|
2014-02-19 13:56:37 -06:00
|
|
|
if (context->resolution_type_set != 0) {
|
|
|
|
/* already setup */
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2015-07-09 17:05:26 -05:00
|
|
|
#endif
|
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;
|
|
|
|
}
|
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
|
|
|
|
2015-06-17 11:18:09 -05:00
|
|
|
static getdns_return_t
|
2015-07-13 09:39:43 -05:00
|
|
|
getdns_set_base_dns_transports(
|
|
|
|
getdns_context *context, size_t transport_count, getdns_transport_list_t *transports)
|
2015-04-24 10:29:08 -05:00
|
|
|
{
|
2015-06-19 12:28:29 -05:00
|
|
|
size_t i;
|
2015-07-13 09:39:43 -05:00
|
|
|
getdns_transport_list_t *new_transports;
|
2015-04-29 13:20:25 -05:00
|
|
|
|
2015-07-13 09:39:43 -05:00
|
|
|
if (!context || transport_count == 0 || transports == NULL)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
2015-06-17 11:18:09 -05:00
|
|
|
|
2015-08-12 07:24:08 -05:00
|
|
|
/* Check for valid transports and that they are used only once*/
|
|
|
|
int u=0,t=0,l=0,s=0;
|
2015-06-19 12:28:29 -05:00
|
|
|
for(i=0; i<transport_count; i++)
|
|
|
|
{
|
2015-08-12 07:24:08 -05:00
|
|
|
switch (transports[i]) {
|
|
|
|
case GETDNS_TRANSPORT_UDP: u++; break;
|
|
|
|
case GETDNS_TRANSPORT_TCP: t++; break;
|
|
|
|
case GETDNS_TRANSPORT_TLS: l++; break;
|
|
|
|
case GETDNS_TRANSPORT_STARTTLS: s++; break;
|
|
|
|
default: return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
}
|
2015-06-17 11:18:09 -05:00
|
|
|
}
|
2015-08-12 07:24:08 -05:00
|
|
|
if ( u>1 || t>1 || l>1 || s>1)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
2015-07-13 09:39:43 -05:00
|
|
|
|
|
|
|
if (!(new_transports = GETDNS_XMALLOC(context->my_mf,
|
|
|
|
getdns_transport_list_t, transport_count)))
|
2015-06-19 12:28:29 -05:00
|
|
|
|
2015-07-13 09:39:43 -05:00
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
2015-06-19 12:28:29 -05:00
|
|
|
|
2015-07-13 09:39:43 -05:00
|
|
|
if (context->dns_transports)
|
|
|
|
GETDNS_FREE(context->my_mf, context->dns_transports);
|
|
|
|
|
|
|
|
/** duplicate **/
|
|
|
|
context->dns_transports = new_transports;
|
|
|
|
memcpy(context->dns_transports, transports,
|
|
|
|
transport_count * sizeof(getdns_transport_list_t));
|
2015-06-19 12:28:29 -05:00
|
|
|
context->dns_transport_count = transport_count;
|
2015-07-13 09:39:43 -05:00
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_NAMESPACES);
|
2015-06-19 12:28:29 -05:00
|
|
|
|
2015-07-13 09:39:43 -05:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2015-04-17 09:50:08 -05:00
|
|
|
}
|
|
|
|
|
2014-02-19 13:56:37 -06:00
|
|
|
static getdns_return_t
|
2015-06-17 11:18:09 -05:00
|
|
|
set_ub_dns_transport(struct getdns_context* context) {
|
|
|
|
/* These mappings are not exact because Unbound is configured differently,
|
2015-06-25 14:21:00 -05:00
|
|
|
so just map as close as possible. Not all options can be supported.*/
|
2015-06-19 12:28:29 -05:00
|
|
|
switch (context->dns_transports[0]) {
|
|
|
|
case GETDNS_TRANSPORT_UDP:
|
2014-08-10 09:59:03 -05:00
|
|
|
set_ub_string_opt(context, "do-udp:", "yes");
|
2015-07-09 08:40:00 -05:00
|
|
|
if (context->dns_transport_count > 1
|
|
|
|
&& context->dns_transports[1] == GETDNS_TRANSPORT_TCP)
|
2015-06-17 11:18:09 -05:00
|
|
|
set_ub_string_opt(context, "do-tcp:", "yes");
|
|
|
|
else
|
|
|
|
set_ub_string_opt(context, "do-tcp:", "no");
|
2014-02-19 13:56:37 -06:00
|
|
|
break;
|
2015-06-25 14:21:00 -05:00
|
|
|
case GETDNS_TRANSPORT_TCP:
|
|
|
|
set_ub_string_opt(context, "do-udp:", "no");
|
|
|
|
set_ub_string_opt(context, "do-tcp:", "yes");
|
|
|
|
break;
|
2015-06-19 12:28:29 -05:00
|
|
|
case GETDNS_TRANSPORT_TLS:
|
|
|
|
case GETDNS_TRANSPORT_STARTTLS:
|
2015-06-18 11:29:23 -05:00
|
|
|
set_ub_string_opt(context, "do-udp:", "no");
|
|
|
|
set_ub_string_opt(context, "do-tcp:", "yes");
|
2015-06-25 14:21:00 -05:00
|
|
|
/* Find out if there is a fallback available. */
|
|
|
|
int fallback = 0;
|
|
|
|
for (size_t i = 1; i < context->dns_transport_count; i++) {
|
|
|
|
if (context->dns_transports[i] == GETDNS_TRANSPORT_TCP) {
|
|
|
|
fallback = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (context->dns_transports[i] == GETDNS_TRANSPORT_UDP) {
|
|
|
|
set_ub_string_opt(context, "do-udp:", "yes");
|
|
|
|
set_ub_string_opt(context, "do-tcp:", "no");
|
|
|
|
fallback = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (context->dns_transports[0] == GETDNS_TRANSPORT_TLS) {
|
|
|
|
if (fallback == 0)
|
|
|
|
/* Use TLS if it is the only thing.*/
|
|
|
|
set_ub_string_opt(context, "ssl-upstream:", "yes");
|
|
|
|
break;
|
|
|
|
} else if (fallback == 0)
|
|
|
|
/* Can't support STARTTLS with no fallback. This leads to
|
|
|
|
* timeouts with un stub validation.... */
|
|
|
|
set_ub_string_opt(context, "do-tcp:", "no");
|
2015-06-18 11:29:23 -05:00
|
|
|
break;
|
2014-12-07 13:03:34 -06:00
|
|
|
default:
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
2014-02-19 13:56:37 -06:00
|
|
|
}
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
2014-09-12 07:44:53 -05:00
|
|
|
|
2013-07-15 16:42:37 -05:00
|
|
|
/*
|
|
|
|
* getdns_context_set_dns_transport
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
2015-07-13 09:39:43 -05:00
|
|
|
getdns_context_set_dns_transport(
|
|
|
|
getdns_context *context, getdns_transport_t value)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2015-07-13 09:39:43 -05:00
|
|
|
size_t count = 2;
|
|
|
|
getdns_transport_list_t *new_transports;
|
2015-06-17 11:18:09 -05:00
|
|
|
|
2015-07-13 09:39:43 -05:00
|
|
|
if (value == GETDNS_TRANSPORT_UDP_ONLY ||
|
|
|
|
value == GETDNS_TRANSPORT_TCP_ONLY ||
|
|
|
|
value == GETDNS_TRANSPORT_TCP_ONLY_KEEP_CONNECTIONS_OPEN ||
|
|
|
|
value == GETDNS_TRANSPORT_TLS_ONLY_KEEP_CONNECTIONS_OPEN)
|
|
|
|
count = 1;
|
|
|
|
|
|
|
|
if (!context)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if (!(new_transports = GETDNS_XMALLOC(
|
|
|
|
context->my_mf, getdns_transport_list_t, count)))
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
|
|
|
|
if (context->dns_transports)
|
|
|
|
GETDNS_FREE(context->my_mf, context->dns_transports);
|
|
|
|
|
|
|
|
context->dns_transport_count = count;
|
|
|
|
context->dns_transports = new_transports;
|
|
|
|
|
|
|
|
switch (value) {
|
|
|
|
case GETDNS_TRANSPORT_UDP_FIRST_AND_FALL_BACK_TO_TCP:
|
|
|
|
context->dns_transports[0] = GETDNS_TRANSPORT_UDP;
|
|
|
|
context->dns_transports[1] = GETDNS_TRANSPORT_TCP;
|
|
|
|
break;
|
|
|
|
case GETDNS_TRANSPORT_UDP_ONLY:
|
|
|
|
context->dns_transports[0] = GETDNS_TRANSPORT_UDP;
|
|
|
|
break;
|
|
|
|
case GETDNS_TRANSPORT_TCP_ONLY:
|
|
|
|
case GETDNS_TRANSPORT_TCP_ONLY_KEEP_CONNECTIONS_OPEN:
|
|
|
|
context->dns_transports[0] = GETDNS_TRANSPORT_TCP;
|
|
|
|
break;
|
|
|
|
case GETDNS_TRANSPORT_TLS_ONLY_KEEP_CONNECTIONS_OPEN:
|
|
|
|
context->dns_transports[0] = GETDNS_TRANSPORT_TLS;
|
|
|
|
break;
|
|
|
|
case GETDNS_TRANSPORT_TLS_FIRST_AND_FALL_BACK_TO_TCP_KEEP_CONNECTIONS_OPEN:
|
|
|
|
context->dns_transports[0] = GETDNS_TRANSPORT_TLS;
|
|
|
|
context->dns_transports[1] = GETDNS_TRANSPORT_TCP;
|
|
|
|
break;
|
|
|
|
case GETDNS_TRANSPORT_STARTTLS_FIRST_AND_FALL_BACK_TO_TCP_KEEP_CONNECTIONS_OPEN:
|
|
|
|
context->dns_transports[0] = GETDNS_TRANSPORT_STARTTLS;
|
|
|
|
context->dns_transports[1] = GETDNS_TRANSPORT_TCP;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
|
|
|
/* Note that the call below does not have any effect in unbound after the
|
|
|
|
* ctx is finalised so for recursive mode or stub + dnssec only the first
|
|
|
|
* transport specified on the first query is used.
|
|
|
|
* However the method returns success as otherwise the transport could not
|
|
|
|
* be reset for stub mode.
|
|
|
|
* Also, not all transport options supported in libunbound yet */
|
|
|
|
if (set_ub_dns_transport(context) != GETDNS_RETURN_GOOD) {
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_DNS_TRANSPORT);
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2015-06-17 11:18:09 -05:00
|
|
|
}
|
2014-02-06 21:40:36 -06:00
|
|
|
|
2015-06-17 11:18:09 -05:00
|
|
|
/*
|
2015-07-13 09:39:43 -05:00
|
|
|
* getdns_context_set_dns_transport_list
|
2015-06-17 11:18:09 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_set_dns_transport_list(getdns_context *context,
|
|
|
|
size_t transport_count, getdns_transport_list_t *transports)
|
|
|
|
{
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
if (getdns_set_base_dns_transports(context, transport_count, transports) != GETDNS_RETURN_GOOD)
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
/* Note that the call below does not have any effect in unbound after the
|
|
|
|
* ctx is finalised so for recursive mode or stub + dnssec only the first
|
|
|
|
* transport specified on the first query is used.
|
|
|
|
* However the method returns success as otherwise the transport could not
|
|
|
|
* be reset for stub mode.
|
|
|
|
* Also, not all transport options supported in libunbound yet */
|
|
|
|
if (set_ub_dns_transport(context) != GETDNS_RETURN_GOOD) {
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_DNS_TRANSPORT);
|
2014-02-06 21:40:36 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2015-07-13 09:39:43 -05:00
|
|
|
} /* getdns_context_set_dns_transport_list */
|
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
|
|
|
|
2015-06-18 11:11:11 -05:00
|
|
|
/*
|
|
|
|
* getdns_context_set_idle_timeout
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_set_idle_timeout(struct getdns_context *context, uint64_t timeout)
|
|
|
|
{
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
|
|
|
|
if (timeout == 0) {
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
context->idle_timeout = timeout;
|
|
|
|
|
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_IDLE_TIMEOUT);
|
|
|
|
|
|
|
|
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-10-28 08:23:40 -05:00
|
|
|
if (value != GETDNS_REDIRECTS_FOLLOW && value != GETDNS_REDIRECTS_DO_NOT_FOLLOW)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
2014-02-06 21:40:36 -06:00
|
|
|
context->follow_redirects = value;
|
2014-10-28 08:27:30 -05: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) {
|
2015-08-19 09:30:15 -05:00
|
|
|
if (_getdns_list_copy(addresses, ©) != GETDNS_RETURN_GOOD) {
|
2014-02-06 21:40:36 -06:00
|
|
|
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);
|
|
|
|
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) {
|
2015-08-19 09:30:15 -05:00
|
|
|
if (_getdns_list_copy(value, ©) != GETDNS_RETURN_GOOD) {
|
2014-02-06 21:40:36 -06:00
|
|
|
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
|
2015-06-30 07:43:52 -05:00
|
|
|
getdns_context_set_dnssec_trust_anchors(
|
|
|
|
getdns_context *context, getdns_list *value)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2015-06-30 07:43:52 -05:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2015-06-30 07:43:52 -05:00
|
|
|
if (context->trust_anchors &&
|
|
|
|
context->trust_anchors != context->trust_anchors_spc)
|
|
|
|
GETDNS_FREE(context->mf, context->trust_anchors);
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2015-06-30 07:43:52 -05:00
|
|
|
if (value) {
|
|
|
|
context->trust_anchors_len = sizeof(context->trust_anchors_spc);
|
|
|
|
context->trust_anchors = _getdns_list2wire(value,
|
|
|
|
context->trust_anchors_spc, &context->trust_anchors_len,
|
|
|
|
&context->mf);
|
|
|
|
} else {
|
|
|
|
context->trust_anchors = NULL;
|
|
|
|
context->trust_anchors_len = 0;
|
|
|
|
}
|
|
|
|
dispatch_updated(context, GETDNS_CONTEXT_CODE_DNSSEC_TRUST_ANCHORS);
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2014-02-06 21:40:36 -06:00
|
|
|
} /* 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,
|
2014-10-01 16:16:43 -05:00
|
|
|
struct getdns_list *upstream_list)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2014-10-01 16:16:43 -05:00
|
|
|
getdns_return_t r;
|
|
|
|
size_t count = 0;
|
|
|
|
size_t i;
|
2015-05-02 12:08:45 -05:00
|
|
|
//size_t upstreams_limit;
|
2014-10-01 16:16:43 -05:00
|
|
|
getdns_upstreams *upstreams;
|
|
|
|
char addrstr[1024], portstr[1024], *eos;
|
|
|
|
struct addrinfo hints;
|
2014-02-06 21:40:36 -06:00
|
|
|
|
2014-10-01 16:16:43 -05:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(upstream_list, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-12-06 22:46:44 -06:00
|
|
|
|
2014-10-01 16:16:43 -05:00
|
|
|
r = getdns_list_get_length(upstream_list, &count);
|
|
|
|
if (count == 0 || r != GETDNS_RETURN_GOOD) {
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
|
|
|
|
hints.ai_socktype = 0; /* Datagram socket */
|
|
|
|
hints.ai_flags = AI_NUMERICHOST; /* No reverse name lookups */
|
|
|
|
hints.ai_protocol = 0; /* Any protocol */
|
|
|
|
hints.ai_canonname = NULL;
|
|
|
|
hints.ai_addr = NULL;
|
|
|
|
hints.ai_next = NULL;
|
2014-02-06 21:40:36 -06:00
|
|
|
|
2015-04-29 13:20:25 -05:00
|
|
|
upstreams = upstreams_create(context, count*3);
|
2015-05-02 12:08:45 -05:00
|
|
|
//upstreams_limit = count;
|
2014-10-01 16:16:43 -05:00
|
|
|
for (i = 0; i < count; i++) {
|
2015-05-02 12:08:45 -05:00
|
|
|
getdns_dict *dict;
|
|
|
|
getdns_bindata *address_type;
|
|
|
|
getdns_bindata *address_data;
|
2015-07-19 06:18:38 -05:00
|
|
|
getdns_bindata *tls_auth_name;
|
2015-05-02 12:08:45 -05:00
|
|
|
struct sockaddr_storage addr;
|
|
|
|
|
|
|
|
getdns_bindata *scope_id;
|
|
|
|
getdns_upstream *upstream;
|
|
|
|
|
|
|
|
if ((r = getdns_list_get_dict(upstream_list, i, &dict)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if ((r = getdns_dict_get_bindata(
|
|
|
|
dict, "address_type",&address_type)))
|
|
|
|
goto error;
|
|
|
|
if (address_type->size < 4)
|
|
|
|
goto invalid_parameter;
|
|
|
|
if (strncmp((char *)address_type->data, "IPv4", 4) == 0)
|
|
|
|
addr.ss_family = AF_INET;
|
|
|
|
else if (strncmp((char *)address_type->data, "IPv6", 4) == 0)
|
|
|
|
addr.ss_family = AF_INET6;
|
|
|
|
else goto invalid_parameter;
|
|
|
|
|
|
|
|
if ((r = getdns_dict_get_bindata(
|
|
|
|
dict, "address_data", &address_data)))
|
|
|
|
goto error;
|
|
|
|
if ((addr.ss_family == AF_INET &&
|
|
|
|
address_data->size != 4) ||
|
|
|
|
(addr.ss_family == AF_INET6 &&
|
|
|
|
address_data->size != 16))
|
|
|
|
goto invalid_parameter;
|
|
|
|
if (inet_ntop(addr.ss_family, address_data->data,
|
|
|
|
addrstr, 1024) == NULL)
|
|
|
|
goto invalid_parameter;
|
|
|
|
|
|
|
|
if (getdns_dict_get_bindata(dict, "scope_id", &scope_id) ==
|
|
|
|
GETDNS_RETURN_GOOD) {
|
|
|
|
if (strlen(addrstr) + scope_id->size > 1022)
|
|
|
|
goto invalid_parameter;
|
|
|
|
eos = &addrstr[strlen(addrstr)];
|
|
|
|
*eos++ = '%';
|
|
|
|
(void) memcpy(eos, scope_id->data, scope_id->size);
|
|
|
|
eos[scope_id->size] = 0;
|
|
|
|
}
|
|
|
|
|
2015-04-29 13:20:25 -05:00
|
|
|
/* Loop to create upstreams as needed*/
|
2015-06-19 12:28:29 -05:00
|
|
|
for (size_t j = 0; j < GETDNS_UPSTREAM_TRANSPORTS; j++) {
|
2015-04-19 11:16:58 -05:00
|
|
|
uint32_t port;
|
|
|
|
struct addrinfo *ai;
|
2015-06-19 12:28:29 -05:00
|
|
|
port = getdns_port_array[j];
|
2015-04-29 13:20:25 -05:00
|
|
|
if (port == GETDNS_PORT_ZERO)
|
|
|
|
continue;
|
|
|
|
|
2015-06-19 12:28:29 -05:00
|
|
|
if (getdns_upstream_transports[j] != GETDNS_TRANSPORT_TLS)
|
2015-05-02 12:08:45 -05:00
|
|
|
(void) getdns_dict_get_int(dict, "port", &port);
|
2015-05-11 15:01:31 -05:00
|
|
|
else
|
2015-06-17 11:18:09 -05:00
|
|
|
(void) getdns_dict_get_int(dict, "tls_port", &port);
|
2015-04-19 11:16:58 -05:00
|
|
|
(void) snprintf(portstr, 1024, "%d", (int)port);
|
|
|
|
|
|
|
|
if (getaddrinfo(addrstr, portstr, &hints, &ai))
|
|
|
|
goto invalid_parameter;
|
2015-05-13 05:39:24 -05:00
|
|
|
if (!ai)
|
|
|
|
continue;
|
2014-02-06 21:40:36 -06:00
|
|
|
|
2015-04-30 13:07:49 -05:00
|
|
|
/* TODO[TLS]: Should probably check that the upstream doesn't
|
2015-05-02 12:08:45 -05:00
|
|
|
* already exist (in case user has specified TLS port explicitly and
|
|
|
|
* to prevent duplicates) */
|
|
|
|
|
|
|
|
/* TODO[TLS]: Grow array when needed. This causes a crash later....
|
|
|
|
if (upstreams->count == upstreams_limit)
|
|
|
|
upstreams = upstreams_resize(
|
|
|
|
upstreams, (upstreams_limit *= 2)); */
|
|
|
|
|
|
|
|
upstream = &upstreams->upstreams[upstreams->count];
|
|
|
|
upstream->addr.ss_family = addr.ss_family;
|
2015-04-19 11:16:58 -05:00
|
|
|
upstream_init(upstream, upstreams, ai);
|
2015-06-19 12:28:29 -05:00
|
|
|
upstream->transport = getdns_upstream_transports[j];
|
2015-08-15 09:27:58 -05:00
|
|
|
if (getdns_upstream_transports[j] == GETDNS_TRANSPORT_TLS ||
|
|
|
|
getdns_upstream_transports[j] == GETDNS_TRANSPORT_STARTTLS) {
|
2015-07-19 06:18:38 -05:00
|
|
|
if ((r = getdns_dict_get_bindata(
|
|
|
|
dict, "tls_auth_name", &tls_auth_name)) == GETDNS_RETURN_GOOD) {
|
|
|
|
/*TODO: VALIDATE THIS STRING!*/
|
|
|
|
memcpy(upstream->tls_auth_name,
|
|
|
|
(char *)tls_auth_name->data,
|
|
|
|
tls_auth_name->size);
|
|
|
|
upstream->tls_auth_name[tls_auth_name->size] = '\0';
|
|
|
|
}
|
|
|
|
}
|
2015-04-19 11:16:58 -05:00
|
|
|
upstreams->count++;
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
}
|
2014-10-01 16:16:43 -05:00
|
|
|
}
|
2015-08-19 09:22:38 -05:00
|
|
|
_getdns_upstreams_dereference(context->upstreams);
|
2014-10-01 17:16:11 -05:00
|
|
|
context->upstreams = upstreams;
|
2014-10-01 16:16:43 -05:00
|
|
|
dispatch_updated(context,
|
|
|
|
GETDNS_CONTEXT_CODE_UPSTREAM_RECURSIVE_SERVERS);
|
|
|
|
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
|
|
|
|
invalid_parameter:
|
|
|
|
r = GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
error:
|
2015-08-19 09:22:38 -05:00
|
|
|
_getdns_upstreams_dereference(upstreams);
|
2014-10-14 18:13:39 -05:00
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
2014-10-01 16:16:43 -05:00
|
|
|
} /* 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,
|
2014-10-23 07:30:23 -05:00
|
|
|
int value) {
|
2014-09-12 03:09:08 -05:00
|
|
|
/* edns-buffer-size */
|
2014-10-23 07:30:23 -05:00
|
|
|
if (value >= 512 && value <= 65535)
|
|
|
|
set_ub_number_opt(context, "edns-buffer-size:", (uint16_t)value);
|
2014-02-19 13:56:37 -06:00
|
|
|
}
|
2014-09-12 07:44:53 -05:00
|
|
|
|
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-10-27 08:18:31 -05:00
|
|
|
if (!context)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
2013-10-16 17:33:12 -05:00
|
|
|
|
2014-10-27 08:18:31 -05:00
|
|
|
/* check for < 512. uint16_t won't let it go above max) */
|
|
|
|
if (value < 512)
|
|
|
|
value = 512;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2014-02-06 21:40:36 -06:00
|
|
|
} /* 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-10-28 08:36:41 -05:00
|
|
|
if (value != 0 && value != 1) {
|
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->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
|
2014-10-17 17:25:41 -05:00
|
|
|
cancel_dns_req(getdns_dns_req *req)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2015-01-29 05:30:40 -06:00
|
|
|
getdns_network_req *netreq, **netreq_p;
|
2014-12-06 22:46:44 -06:00
|
|
|
|
2015-01-29 05:30:40 -06:00
|
|
|
for (netreq_p = req->netreqs; (netreq = *netreq_p); netreq_p++)
|
2015-08-19 03:47:46 -05:00
|
|
|
#ifdef HAVE_LIBUNBOUND
|
2014-10-07 08:52:41 -05:00
|
|
|
if (netreq->unbound_id != -1) {
|
|
|
|
ub_cancel(req->context->unbound_ctx,
|
|
|
|
netreq->unbound_id);
|
|
|
|
netreq->unbound_id = -1;
|
2014-12-06 22:46:44 -06:00
|
|
|
} else
|
2015-08-19 03:47:46 -05:00
|
|
|
#endif
|
2015-08-19 09:22:38 -05:00
|
|
|
_getdns_cancel_stub_request(netreq);
|
2014-10-17 17:25:41 -05:00
|
|
|
|
2014-10-07 08:52:41 -05:00
|
|
|
req->canceled = 1;
|
2013-10-17 18:45:25 -05:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_return_t
|
2015-08-19 09:30:15 -05:00
|
|
|
_getdns_context_cancel_request(getdns_context *context,
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_transaction_t transaction_id, int fire_callback)
|
|
|
|
{
|
2014-10-23 16:00:30 -05:00
|
|
|
getdns_dns_req *dnsreq;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2014-10-23 16:00:30 -05:00
|
|
|
if (!context)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2014-10-23 16:00:30 -05:00
|
|
|
/* delete the node from the tree */
|
2015-08-19 09:07:01 -05:00
|
|
|
if (!(dnsreq = (getdns_dns_req *)_getdns_rbtree_delete(
|
2014-10-23 16:00:30 -05:00
|
|
|
&context->outbound_requests, &transaction_id)))
|
|
|
|
return GETDNS_RETURN_UNKNOWN_TRANSACTION;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2014-10-23 16:00:30 -05:00
|
|
|
/* do the cancel */
|
|
|
|
cancel_dns_req(dnsreq);
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2014-10-23 16:00:30 -05:00
|
|
|
if (fire_callback)
|
|
|
|
dnsreq->user_callback(context, GETDNS_CALLBACK_CANCEL,
|
|
|
|
NULL, dnsreq->user_pointer, transaction_id);
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2014-10-23 16:00:30 -05:00
|
|
|
/* clean up */
|
2015-08-19 09:33:19 -05:00
|
|
|
_getdns_dns_req_free(dnsreq);
|
2014-10-23 16:00:30 -05:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-10-18 12:55:31 -05:00
|
|
|
}
|
|
|
|
|
2014-10-14 18:13:39 -05:00
|
|
|
/*
|
|
|
|
* getdns_cancel_callback
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
getdns_return_t
|
|
|
|
getdns_cancel_callback(getdns_context *context,
|
|
|
|
getdns_transaction_t transaction_id)
|
|
|
|
{
|
|
|
|
if (!context)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
context->processing = 1;
|
2015-08-19 09:30:15 -05:00
|
|
|
getdns_return_t r = _getdns_context_cancel_request(context, transaction_id, 1);
|
2014-10-14 18:13:39 -05:00
|
|
|
context->processing = 0;
|
|
|
|
getdns_context_request_count_changed(context);
|
|
|
|
return r;
|
|
|
|
} /* getdns_cancel_callback */
|
|
|
|
|
2015-07-09 17:05:26 -05:00
|
|
|
#ifndef STUB_NATIVE_DNSSEC
|
|
|
|
static uint8_t*
|
|
|
|
upstream_addr(getdns_upstream *upstream)
|
|
|
|
{
|
|
|
|
return upstream->addr.ss_family == AF_INET
|
|
|
|
? (void *)&((struct sockaddr_in*)&upstream->addr)->sin_addr
|
|
|
|
: (void *)&((struct sockaddr_in6*)&upstream->addr)->sin6_addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static in_port_t
|
|
|
|
upstream_port(getdns_upstream *upstream)
|
|
|
|
{
|
|
|
|
return ntohs(upstream->addr.ss_family == AF_INET
|
|
|
|
? ((struct sockaddr_in *)&upstream->addr)->sin_port
|
|
|
|
: ((struct sockaddr_in6*)&upstream->addr)->sin6_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t *
|
|
|
|
upstream_scope_id(getdns_upstream *upstream)
|
|
|
|
{
|
|
|
|
return upstream->addr.ss_family == AF_INET ? NULL
|
|
|
|
: (upstream_addr(upstream)[0] == 0xFE &&
|
|
|
|
(upstream_addr(upstream)[1] & 0xC0) == 0x80 ?
|
|
|
|
&((struct sockaddr_in6*)&upstream->addr)->sin6_scope_id : NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
upstream_ntop_buf(getdns_upstream *upstream, char *buf, size_t len)
|
|
|
|
{
|
|
|
|
/* Also possible but prints scope_id by name (nor parsed by unbound)
|
|
|
|
*
|
|
|
|
* getnameinfo((struct sockaddr *)&upstream->addr, upstream->addr_len,
|
|
|
|
* buf, len, NULL, 0, NI_NUMERICHOST)
|
|
|
|
*/
|
|
|
|
(void) inet_ntop(upstream->addr.ss_family, upstream_addr(upstream),
|
|
|
|
buf, len);
|
|
|
|
if (upstream_scope_id(upstream))
|
|
|
|
(void) snprintf(buf + strlen(buf), len - strlen(buf),
|
|
|
|
"%%%d", (int)*upstream_scope_id(upstream));
|
|
|
|
else if (upstream_port(upstream) != GETDNS_PORT_DNS && upstream_port(upstream) != GETDNS_PORT_ZERO)
|
|
|
|
(void) snprintf(buf + strlen(buf), len - strlen(buf),
|
|
|
|
"@%d", (int)upstream_port(upstream));
|
|
|
|
}
|
|
|
|
|
2014-02-12 05:37:05 -06:00
|
|
|
static getdns_return_t
|
2015-04-18 15:17:28 -05:00
|
|
|
ub_setup_stub(struct ub_ctx *ctx, getdns_context *context)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2014-09-30 08:12:48 -05:00
|
|
|
getdns_return_t r = GETDNS_RETURN_GOOD;
|
2014-02-12 05:37:05 -06:00
|
|
|
size_t i;
|
2014-10-16 07:24:13 -05:00
|
|
|
getdns_upstream *upstream;
|
2014-09-30 08:12:48 -05:00
|
|
|
char addr[1024];
|
2015-04-17 09:50:08 -05:00
|
|
|
getdns_upstreams *upstreams = context->upstreams;
|
2015-04-18 15:17:28 -05:00
|
|
|
|
2014-02-12 05:37:05 -06:00
|
|
|
(void) ub_ctx_set_fwd(ctx, NULL);
|
2014-09-30 08:12:48 -05:00
|
|
|
for (i = 0; i < upstreams->count; i++) {
|
|
|
|
upstream = &upstreams->upstreams[i];
|
2015-04-30 08:52:16 -05:00
|
|
|
/*[TLS]: Use only the TLS subset of upstreams when TLS is the only thing
|
|
|
|
* used. All other cases must currently fallback to TCP for libunbound.*/
|
2015-06-19 12:28:29 -05:00
|
|
|
if (context->dns_transports[0] == GETDNS_TRANSPORT_TLS &&
|
|
|
|
context->dns_transport_count ==1 &&
|
|
|
|
upstream->transport != GETDNS_TRANSPORT_TLS)
|
2015-04-27 10:37:16 -05:00
|
|
|
continue;
|
2015-04-24 10:29:08 -05:00
|
|
|
upstream_ntop_buf(upstream, addr, 1024);
|
2014-09-30 08:12:48 -05:00
|
|
|
ub_ctx_set_fwd(ctx, addr);
|
2014-02-12 05:37:05 -06:00
|
|
|
}
|
2014-09-30 08:12:48 -05:00
|
|
|
|
2014-02-12 05:37:05 -06:00
|
|
|
/* 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;
|
|
|
|
}
|
2015-07-09 17:05:26 -05:00
|
|
|
#endif
|
2014-02-12 05:37:05 -06:00
|
|
|
|
|
|
|
static getdns_return_t
|
2015-08-19 09:22:38 -05:00
|
|
|
_getdns_ns_dns_setup(struct getdns_context *context)
|
2014-02-12 05:37:05 -06:00
|
|
|
{
|
2015-08-19 03:47:46 -05:00
|
|
|
#ifdef HAVE_LIBUNBOUND
|
2015-08-19 09:22:38 -05:00
|
|
|
_getdns_rr_iter rr_spc, *rr;
|
2015-07-09 17:05:26 -05:00
|
|
|
char ta_str[8192];
|
2015-08-19 03:47:46 -05:00
|
|
|
#endif
|
2014-02-12 05:37:05 -06:00
|
|
|
assert(context);
|
|
|
|
|
|
|
|
switch (context->resolution_type) {
|
2014-12-06 22:46:44 -06:00
|
|
|
case GETDNS_RESOLUTION_STUB:
|
2014-10-16 07:40:47 -05:00
|
|
|
if (!context->upstreams || !context->upstreams->count)
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
2015-07-09 17:05:26 -05:00
|
|
|
#ifdef STUB_NATIVE_DNSSEC
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
#else
|
2015-04-17 09:50:08 -05:00
|
|
|
return ub_setup_stub(context->unbound_ctx, context);
|
2015-07-09 17:05:26 -05:00
|
|
|
#endif
|
2014-02-12 05:37:05 -06:00
|
|
|
|
|
|
|
case GETDNS_RESOLUTION_RECURSING:
|
2015-08-19 03:47:46 -05:00
|
|
|
#ifdef HAVE_LIBUNBOUND
|
2014-02-12 05:37:05 -06:00
|
|
|
/* TODO: use the root servers via root hints file */
|
|
|
|
(void) ub_ctx_set_fwd(context->unbound_ctx, NULL);
|
2015-07-09 17:05:26 -05:00
|
|
|
if (!context->unbound_ta_set && context->trust_anchors) {
|
2015-08-19 09:22:38 -05:00
|
|
|
for ( rr = _getdns_rr_iter_init( &rr_spc
|
2015-07-09 17:05:26 -05:00
|
|
|
, context->trust_anchors
|
|
|
|
, context->trust_anchors_len)
|
2015-08-19 09:22:38 -05:00
|
|
|
; rr ; rr = _getdns_rr_iter_next(rr) ) {
|
2015-07-09 17:05:26 -05:00
|
|
|
|
|
|
|
(void) gldns_wire2str_rr_buf(rr->pos,
|
|
|
|
rr->nxt - rr->pos, ta_str, sizeof(ta_str));
|
|
|
|
(void) ub_ctx_add_ta(
|
|
|
|
context->unbound_ctx, ta_str);
|
|
|
|
}
|
|
|
|
context->unbound_ta_set = 1;
|
|
|
|
}
|
2014-02-12 05:37:05 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2015-08-19 03:47:46 -05:00
|
|
|
#else
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
#endif
|
2014-02-12 05:37:05 -06:00
|
|
|
}
|
|
|
|
return GETDNS_RETURN_BAD_CONTEXT;
|
2013-10-16 13:45:43 -05:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_return_t
|
2015-08-19 09:30:15 -05:00
|
|
|
_getdns_context_prepare_for_resolution(struct getdns_context *context,
|
2014-02-12 05:37:05 -06:00
|
|
|
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-12-07 13:03:34 -06:00
|
|
|
|
2015-04-16 12:01:17 -05:00
|
|
|
/* Transport can in theory be set per query in stub mode */
|
2014-12-07 13:03:34 -06:00
|
|
|
if (context->resolution_type == GETDNS_RESOLUTION_STUB) {
|
2015-08-14 11:55:43 -05:00
|
|
|
if (tls_is_in_transports_list(context) == 1 &&
|
|
|
|
context->tls_ctx == NULL) {
|
2015-08-15 10:35:30 -05:00
|
|
|
#ifdef HAVE_LIBSSL_102
|
2015-04-24 10:29:08 -05:00
|
|
|
/* Create client context, use TLS v1.2 only for now */
|
|
|
|
context->tls_ctx = SSL_CTX_new(TLSv1_2_client_method());
|
2015-04-17 12:38:13 -05:00
|
|
|
#endif
|
2015-06-17 11:18:09 -05:00
|
|
|
if(context->tls_ctx == NULL)
|
|
|
|
return GETDNS_RETURN_BAD_CONTEXT;
|
2015-07-19 06:18:38 -05:00
|
|
|
SSL_CTX_set_verify(context->tls_ctx, SSL_VERIFY_PEER, NULL);
|
|
|
|
if (!SSL_CTX_set_default_verify_paths(context->tls_ctx))
|
|
|
|
return GETDNS_RETURN_BAD_CONTEXT;
|
2014-12-07 13:03:34 -06:00
|
|
|
}
|
|
|
|
}
|
2015-06-25 14:21:00 -05:00
|
|
|
/* Block use of STARTTLS/TLS ONLY in recursive mode as it won't work */
|
|
|
|
/* Note: If TLS is used in recursive mode this will try TLS on port
|
2015-07-19 06:18:38 -05:00
|
|
|
* 53 so it is blocked here. So is 'STARTTLS only' at the moment. */
|
2015-05-02 12:08:45 -05:00
|
|
|
if (context->resolution_type == GETDNS_RESOLUTION_RECURSING &&
|
2015-08-14 11:55:43 -05:00
|
|
|
tls_only_is_in_transports_list(context) == 1)
|
2015-04-17 12:38:13 -05:00
|
|
|
return GETDNS_RETURN_BAD_CONTEXT;
|
2014-12-07 13:03:34 -06:00
|
|
|
|
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) {
|
2015-08-19 09:22:38 -05:00
|
|
|
r = _getdns_ns_dns_setup(context);
|
2014-02-12 05:37:05 -06:00
|
|
|
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_DNS:
|
2015-08-19 09:22:38 -05:00
|
|
|
r = _getdns_ns_dns_setup(context);
|
2014-02-12 05:37:05 -06:00
|
|
|
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;
|
2015-08-19 09:30:15 -05: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
|
2015-08-19 09:30:15 -05:00
|
|
|
_getdns_context_track_outbound_request(getdns_dns_req *dnsreq)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2014-10-23 16:00:30 -05:00
|
|
|
if (!dnsreq)
|
2014-10-08 08:42:33 -05:00
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
2014-10-23 16:00:30 -05:00
|
|
|
dnsreq->node.key = &(dnsreq->trans_id);
|
2015-08-19 09:07:01 -05:00
|
|
|
if (!_getdns_rbtree_insert(
|
2014-10-23 16:00:30 -05:00
|
|
|
&dnsreq->context->outbound_requests, &dnsreq->node))
|
2014-10-08 08:42:33 -05:00
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
2014-10-23 16:00:30 -05:00
|
|
|
|
|
|
|
getdns_context_request_count_changed(dnsreq->context);
|
2014-10-08 08:42:33 -05:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-10-16 17:33:12 -05:00
|
|
|
}
|
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_return_t
|
2015-08-19 09:30:15 -05:00
|
|
|
_getdns_context_clear_outbound_request(getdns_dns_req *dnsreq)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2014-10-23 16:00:30 -05:00
|
|
|
if (!dnsreq)
|
2014-10-08 08:42:33 -05:00
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
2015-08-19 09:07:01 -05:00
|
|
|
if (!_getdns_rbtree_delete(
|
2014-10-23 16:00:30 -05:00
|
|
|
&dnsreq->context->outbound_requests, &dnsreq->trans_id))
|
2014-10-08 08:42:33 -05:00
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
|
2014-10-23 16:00:30 -05:00
|
|
|
getdns_context_request_count_changed(dnsreq->context);
|
2014-10-08 08:42:33 -05:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-10-16 17:33:12 -05:00
|
|
|
}
|
|
|
|
|
2014-03-05 21:13:37 -06:00
|
|
|
getdns_return_t
|
2015-08-19 09:30:15 -05:00
|
|
|
_getdns_context_request_timed_out(struct getdns_dns_req *req)
|
2014-10-13 17:14:25 -05:00
|
|
|
{
|
2014-10-08 08:42:33 -05:00
|
|
|
/* Don't use req after callback */
|
2014-03-05 21:13:37 -06:00
|
|
|
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;
|
2015-08-19 09:33:19 -05:00
|
|
|
getdns_dict *response = _getdns_create_getdns_response(req);
|
2014-03-05 21:13:37 -06:00
|
|
|
|
|
|
|
/* cancel the req - also clears it from outbound and cleans up*/
|
2015-08-19 09:30:15 -05:00
|
|
|
_getdns_context_cancel_request(context, trans_id, 0);
|
2014-03-05 21:13:37 -06:00
|
|
|
context->processing = 1;
|
2014-10-06 08:11:03 -05:00
|
|
|
cb(context, GETDNS_CALLBACK_TIMEOUT, response, user_arg, trans_id);
|
2014-03-07 09:42:37 -06:00
|
|
|
context->processing = 0;
|
2014-10-13 17:14:25 -05:00
|
|
|
getdns_context_request_count_changed(context);
|
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 *
|
2015-08-19 09:30:15 -05: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 *
|
2015-08-19 09:30:15 -05:00
|
|
|
_getdns_bindata_copy(struct mem_funcs *mfs,
|
2013-11-11 16:10:22 -06:00
|
|
|
const struct getdns_bindata *src)
|
|
|
|
{
|
2014-10-30 09:08:42 -05:00
|
|
|
/* Don't know why, but nodata allows
|
|
|
|
* empty bindatas with the python bindings
|
|
|
|
*/
|
|
|
|
static uint8_t nodata[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
struct getdns_bindata *dst;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2014-10-30 09:08:42 -05:00
|
|
|
if (!src)
|
|
|
|
return NULL;
|
2013-12-09 11:55:33 -06:00
|
|
|
|
2014-10-30 09:08:42 -05:00
|
|
|
if (!(dst = GETDNS_MALLOC(*mfs, struct getdns_bindata)))
|
|
|
|
return NULL;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2014-10-30 09:08:42 -05:00
|
|
|
dst->size = src->size;
|
|
|
|
if ((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);
|
|
|
|
} else {
|
|
|
|
dst->data = nodata;
|
|
|
|
}
|
|
|
|
return dst;
|
2013-11-11 16:10:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-19 09:30:15 -05:00
|
|
|
_getdns_bindata_destroy(struct mem_funcs *mfs,
|
2013-12-08 17:05:18 -06:00
|
|
|
struct getdns_bindata *bindata)
|
2013-11-11 16:10:22 -06:00
|
|
|
{
|
2014-10-30 09:08:42 -05:00
|
|
|
if (!bindata)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (bindata->size) 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-10-13 17:14:25 -05:00
|
|
|
/* TODO: Remove next_timeout argument from getdns_context_get_num_pending_requests
|
|
|
|
*/
|
2015-08-19 09:07:01 -05:00
|
|
|
void _getdns_handle_timeouts(struct _getdns_event_base* base, struct timeval* now,
|
2014-10-13 17:14:25 -05:00
|
|
|
struct timeval* wait);
|
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,
|
2014-10-06 16:04:12 -05:00
|
|
|
struct timeval* next_timeout)
|
|
|
|
{
|
2014-10-14 18:13:39 -05:00
|
|
|
struct timeval dispose;
|
|
|
|
|
2014-10-06 16:04:12 -05:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
|
2014-10-23 16:00:30 -05:00
|
|
|
if (context->outbound_requests.count)
|
2014-10-10 07:48:52 -05:00
|
|
|
context->extension->vmt->run_once(context->extension, 0);
|
2014-10-07 08:52:41 -05:00
|
|
|
|
2014-10-13 17:14:25 -05:00
|
|
|
/* TODO: Remove this when next_timeout is gone */
|
2015-08-19 09:07:01 -05:00
|
|
|
_getdns_handle_timeouts(context->mini_event.base,
|
2014-10-14 18:13:39 -05:00
|
|
|
&context->mini_event.time_tv, next_timeout ? next_timeout : &dispose);
|
2014-10-13 17:14:25 -05:00
|
|
|
|
2014-10-23 16:00:30 -05:00
|
|
|
return context->outbound_requests.count;
|
2014-01-31 14:48:00 -06:00
|
|
|
}
|
|
|
|
|
2014-01-31 13:43:02 -06:00
|
|
|
/* process async reqs */
|
2014-10-10 07:48:52 -05:00
|
|
|
getdns_return_t
|
|
|
|
getdns_context_process_async(struct getdns_context* context)
|
2014-10-06 16:04:12 -05:00
|
|
|
{
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
2014-01-31 13:43:02 -06:00
|
|
|
|
2015-08-19 03:47:46 -05:00
|
|
|
#ifdef HAVE_LIBUNBOUND
|
2014-10-06 16:04:12 -05:00
|
|
|
context->processing = 1;
|
|
|
|
if (ub_poll(context->unbound_ctx) && ub_process(context->unbound_ctx)){
|
|
|
|
/* need an async return code? */
|
|
|
|
context->processing = 0;
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
}
|
|
|
|
context->processing = 0;
|
2015-08-19 03:47:46 -05:00
|
|
|
#endif
|
2014-10-10 07:48:52 -05:00
|
|
|
context->extension->vmt->run_once(context->extension, 0);
|
|
|
|
|
2014-10-06 16:04:12 -05:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-11-11 16:10:22 -06:00
|
|
|
}
|
2014-01-22 18:55:04 -06:00
|
|
|
|
2014-10-10 07:48:52 -05:00
|
|
|
void
|
|
|
|
getdns_context_run(getdns_context *context)
|
|
|
|
{
|
2014-10-14 18:13:39 -05:00
|
|
|
if (getdns_context_get_num_pending_requests(context, NULL) > 0 &&
|
|
|
|
!getdns_context_process_async(context))
|
|
|
|
context->extension->vmt->run(context->extension);
|
2014-10-10 07:48:52 -05:00
|
|
|
}
|
|
|
|
|
2014-01-31 14:16:10 -06:00
|
|
|
typedef struct timeout_accumulator {
|
|
|
|
getdns_transaction_t* ids;
|
|
|
|
int idx;
|
|
|
|
} timeout_accumulator;
|
|
|
|
|
|
|
|
static void
|
2015-08-19 09:07:01 -05:00
|
|
|
accumulate_outstanding_transactions(_getdns_rbnode_t* node, void* arg) {
|
2014-01-31 14:16:10 -06:00
|
|
|
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-10-23 16:00:30 -05:00
|
|
|
if (context->outbound_requests.count > 0) {
|
2014-01-31 14:16:10 -06:00
|
|
|
timeout_accumulator acc;
|
|
|
|
int i;
|
|
|
|
acc.idx = 0;
|
2014-10-23 16:00:30 -05:00
|
|
|
acc.ids = GETDNS_XMALLOC(context->my_mf, getdns_transaction_t, context->outbound_requests.count);
|
2015-08-19 09:07:01 -05:00
|
|
|
_getdns_traverse_postorder(&context->outbound_requests, accumulate_outstanding_transactions, &acc);
|
2014-01-31 14:16:10 -06:00
|
|
|
for (i = 0; i < acc.idx; ++i) {
|
2015-08-19 09:30:15 -05: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
|
2014-10-08 08:42:33 -05:00
|
|
|
getdns_context_detach_eventloop(struct getdns_context* context)
|
2014-01-22 18:55:04 -06:00
|
|
|
{
|
2014-10-06 16:04:12 -05:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
|
|
|
|
/* When called from within a callback, do not execute pending
|
|
|
|
* context destroys.
|
|
|
|
* The (other) callback handler will handle it.
|
|
|
|
*
|
|
|
|
* ( because callbacks occur in cancel_outstanding_requests,
|
|
|
|
* and they may destroy the context )
|
|
|
|
*/
|
|
|
|
context->processing = 1;
|
|
|
|
/* cancel all outstanding requests */
|
|
|
|
cancel_outstanding_requests(context, 1);
|
|
|
|
context->processing = 0;
|
2014-10-10 07:48:52 -05:00
|
|
|
context->extension->vmt->cleanup(context->extension);
|
|
|
|
context->extension = &context->mini_event.loop;
|
2015-08-19 09:26:39 -05:00
|
|
|
return _getdns_mini_event_init(context, &context->mini_event);
|
2014-01-22 18:55:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
2014-10-08 08:42:33 -05:00
|
|
|
getdns_context_set_eventloop(struct getdns_context* context, getdns_eventloop* loop)
|
2014-01-22 18:55:04 -06:00
|
|
|
{
|
2014-10-08 08:42:33 -05:00
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(loop , GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
|
|
|
|
getdns_return_t r = getdns_context_detach_eventloop(context);
|
|
|
|
if (r != GETDNS_RETURN_GOOD)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
context->extension = loop;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2014-01-22 18:55:04 -06:00
|
|
|
}
|
|
|
|
|
2015-07-13 09:22:39 -05:00
|
|
|
static in_port_t
|
|
|
|
upstream_port(getdns_upstream *upstream)
|
|
|
|
{
|
|
|
|
return ntohs(upstream->addr.ss_family == AF_INET
|
|
|
|
? ((struct sockaddr_in *)&upstream->addr)->sin_port
|
|
|
|
: ((struct sockaddr_in6*)&upstream->addr)->sin6_port);
|
|
|
|
}
|
|
|
|
|
2014-02-19 15:22:55 -06:00
|
|
|
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 */
|
2015-06-17 11:18:09 -05:00
|
|
|
r = getdns_dict_set_int(result, "timeout", context->timeout);
|
2014-02-19 15:22:55 -06:00
|
|
|
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);
|
2014-10-23 07:30:23 -05:00
|
|
|
if (context->edns_maximum_udp_payload_size != -1)
|
|
|
|
r |= getdns_dict_set_int(result, "edns_maximum_udp_payload_size",
|
|
|
|
context->edns_maximum_udp_payload_size);
|
2014-02-19 15:22:55 -06:00
|
|
|
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 */
|
2014-10-24 16:20:28 -05:00
|
|
|
if (context->suffix) r |= getdns_dict_set_list(result, "suffix", context->suffix);
|
2014-10-16 07:40:47 -05:00
|
|
|
if (context->upstreams && context->upstreams->count > 0) {
|
2014-10-01 16:16:43 -05:00
|
|
|
size_t i;
|
2014-10-16 07:24:13 -05:00
|
|
|
getdns_upstream *upstream;
|
2014-10-01 16:16:43 -05:00
|
|
|
getdns_list *upstreams =
|
|
|
|
getdns_list_create_with_context(context);
|
|
|
|
|
2015-07-19 02:43:12 -05:00
|
|
|
for (i = 0; i < context->upstreams->count;) {
|
2015-07-13 09:22:39 -05:00
|
|
|
size_t j;
|
2014-10-01 16:16:43 -05:00
|
|
|
getdns_dict *d;
|
|
|
|
upstream = &context->upstreams->upstreams[i];
|
2015-03-12 16:41:39 -05:00
|
|
|
d = sockaddr_dict(context,
|
|
|
|
(struct sockaddr *)&upstream->addr);
|
2015-07-13 09:22:39 -05:00
|
|
|
for ( j = 1, i++
|
|
|
|
; j < GETDNS_UPSTREAM_TRANSPORTS &&
|
|
|
|
i < context->upstreams->count
|
|
|
|
; j++, i++) {
|
|
|
|
|
|
|
|
upstream = &context->upstreams->upstreams[i];
|
|
|
|
if (upstream->transport != GETDNS_TRANSPORT_TLS)
|
|
|
|
continue;
|
|
|
|
if (upstream_port(upstream) != getdns_port_array[j])
|
|
|
|
continue;
|
|
|
|
(void) getdns_dict_set_int(d, "tls_port",
|
|
|
|
(uint32_t) upstream_port(upstream));
|
|
|
|
}
|
2015-08-19 09:30:15 -05:00
|
|
|
r |= _getdns_list_append_dict(upstreams, d);
|
2014-10-01 16:16:43 -05:00
|
|
|
getdns_dict_destroy(d);
|
|
|
|
}
|
|
|
|
r |= getdns_dict_set_list(result, "upstream_recursive_servers",
|
|
|
|
upstreams);
|
|
|
|
getdns_list_destroy(upstreams);
|
|
|
|
}
|
2015-06-19 12:28:29 -05:00
|
|
|
if (context->dns_transport_count > 0) {
|
|
|
|
/* create a namespace list */
|
|
|
|
size_t i;
|
|
|
|
getdns_list* transports = getdns_list_create_with_context(context);
|
|
|
|
if (transports) {
|
|
|
|
for (i = 0; i < context->dns_transport_count; ++i) {
|
|
|
|
r |= getdns_list_set_int(transports, i, context->dns_transports[i]);
|
|
|
|
}
|
|
|
|
r |= getdns_dict_set_list(result, "dns_transport_list", transports);
|
2015-06-17 11:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
2014-02-19 15:22:55 -06:00
|
|
|
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);
|
2015-08-19 03:47:46 -05:00
|
|
|
int r = 0;
|
2014-02-28 18:24:09 -06:00
|
|
|
if (context->resolution_type_set != 0) {
|
|
|
|
/* already setup */
|
|
|
|
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
2015-08-19 03:47:46 -05:00
|
|
|
#ifdef HAVE_LIBUNBOUND
|
2014-02-28 18:24:09 -06:00
|
|
|
if (use_threads)
|
|
|
|
r = ub_ctx_async(context->unbound_ctx, 1);
|
|
|
|
else
|
|
|
|
r = ub_ctx_async(context->unbound_ctx, 0);
|
2015-08-19 03:47:46 -05:00
|
|
|
#else
|
|
|
|
(void)use_threads;
|
|
|
|
#endif
|
2014-02-28 18:24:09 -06:00
|
|
|
return r == 0 ? GETDNS_RETURN_GOOD : GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
|
|
|
}
|
|
|
|
|
2014-12-06 22:46:44 -06:00
|
|
|
getdns_return_t
|
2015-08-19 09:30:15 -05:00
|
|
|
_getdns_context_local_namespace_resolve(
|
2014-10-23 16:00:30 -05:00
|
|
|
getdns_dns_req *dnsreq, getdns_dict **response)
|
|
|
|
{
|
2015-03-12 15:37:03 -05:00
|
|
|
getdns_context *context = dnsreq->context;
|
2014-10-23 16:00:30 -05:00
|
|
|
host_name_addrs *hnas;
|
2015-03-12 15:37:03 -05:00
|
|
|
uint8_t lookup[256];
|
|
|
|
getdns_list empty_list = { 0 };
|
|
|
|
getdns_bindata bindata;
|
|
|
|
getdns_list *jaa;
|
|
|
|
size_t i;
|
|
|
|
getdns_dict *addr;
|
2015-01-29 05:30:40 -06:00
|
|
|
int ipv4 = dnsreq->netreqs[0]->request_type == GETDNS_RRTYPE_A ||
|
|
|
|
(dnsreq->netreqs[1] &&
|
|
|
|
dnsreq->netreqs[1]->request_type == GETDNS_RRTYPE_A);
|
|
|
|
int ipv6 = dnsreq->netreqs[0]->request_type == GETDNS_RRTYPE_AAAA ||
|
|
|
|
(dnsreq->netreqs[1] &&
|
|
|
|
dnsreq->netreqs[1]->request_type == GETDNS_RRTYPE_AAAA);
|
2014-12-06 22:46:44 -06:00
|
|
|
|
2014-10-23 16:00:30 -05:00
|
|
|
if (!ipv4 && !ipv6)
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
2014-09-23 06:36:26 -05:00
|
|
|
|
2014-10-23 16:00:30 -05:00
|
|
|
/*Do the lookup*/
|
2015-06-29 16:32:49 -05:00
|
|
|
(void)memcpy(lookup, dnsreq->name, dnsreq->name_len);
|
2015-03-12 15:37:03 -05:00
|
|
|
canonicalize_dname(lookup);
|
2014-09-23 06:36:26 -05:00
|
|
|
|
2015-03-12 15:37:03 -05:00
|
|
|
if (!(hnas = (host_name_addrs *)
|
2015-08-19 09:07:01 -05:00
|
|
|
_getdns_rbtree_search(&context->local_hosts, lookup)))
|
2015-03-12 15:37:03 -05:00
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
|
|
|
|
if (!hnas->ipv4addrs && (!ipv6 || !hnas->ipv6addrs))
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
|
|
|
|
if (!hnas->ipv6addrs && (!ipv4 || !hnas->ipv4addrs))
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
|
|
|
|
|
|
|
if (!(*response = getdns_dict_create_with_context(context)))
|
2014-10-23 16:00:30 -05:00
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
2015-03-12 15:37:03 -05:00
|
|
|
|
2015-06-29 16:32:49 -05:00
|
|
|
bindata.size = dnsreq->name_len;
|
|
|
|
bindata.data = dnsreq->name;
|
2015-03-12 15:37:03 -05:00
|
|
|
if (getdns_dict_set_bindata(*response, "canonical_name", &bindata))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
empty_list.mf = context->mf;
|
|
|
|
if (getdns_dict_set_list(*response, "replies_full", &empty_list))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (getdns_dict_set_list(*response, "replies_tree", &empty_list))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (getdns_dict_set_int(*response, "status", GETDNS_RESPSTATUS_GOOD))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!ipv4 || !hnas->ipv4addrs) {
|
|
|
|
if (getdns_dict_set_list(*response,
|
|
|
|
"just_address_answers", hnas->ipv6addrs))
|
|
|
|
goto error;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
} else if (!ipv6 || !hnas->ipv6addrs) {
|
|
|
|
if (getdns_dict_set_list(*response,
|
|
|
|
"just_address_answers", hnas->ipv4addrs))
|
|
|
|
goto error;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
if (!(jaa = getdns_list_create_with_context(context)))
|
|
|
|
goto error;
|
|
|
|
for (i = 0; !getdns_list_get_dict(hnas->ipv4addrs, i, &addr); i++)
|
2015-08-19 09:30:15 -05:00
|
|
|
if (_getdns_list_append_dict(jaa, addr))
|
2015-03-12 15:37:03 -05:00
|
|
|
break;
|
|
|
|
for (i = 0; !getdns_list_get_dict(hnas->ipv6addrs, i, &addr); i++)
|
2015-08-19 09:30:15 -05:00
|
|
|
if (_getdns_list_append_dict(jaa, addr))
|
2015-03-12 15:37:03 -05:00
|
|
|
break;
|
|
|
|
if (!getdns_dict_set_list(*response, "just_address_answers", jaa)) {
|
|
|
|
getdns_list_destroy(jaa);
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
getdns_list_destroy(jaa);
|
|
|
|
error:
|
|
|
|
getdns_dict_destroy(*response);
|
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
2014-09-23 06:36:26 -05:00
|
|
|
}
|
|
|
|
|
2014-10-08 18:18:53 -05:00
|
|
|
struct mem_funcs *
|
|
|
|
priv_getdns_context_mf(getdns_context *context)
|
|
|
|
{
|
|
|
|
return &context->mf;
|
|
|
|
}
|
|
|
|
|
2014-12-06 22:46:44 -06:00
|
|
|
/** begin getters **/
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_get_resolution_type(getdns_context *context,
|
|
|
|
getdns_resolution_t* value) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(value, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
*value = context->resolution_type;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_get_namespaces(getdns_context *context,
|
|
|
|
size_t* namespace_count, getdns_namespace_t **namespaces) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(namespace_count, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(namespaces, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
*namespace_count = context->namespace_count;
|
|
|
|
if (!context->namespace_count) {
|
|
|
|
*namespaces = NULL;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
// use normal malloc here so users can do normal free
|
|
|
|
*namespaces = malloc(context->namespace_count * sizeof(getdns_namespace_t));
|
|
|
|
memcpy(*namespaces, context->namespaces,
|
|
|
|
context->namespace_count * sizeof(getdns_namespace_t));
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_get_dns_transport(getdns_context *context,
|
|
|
|
getdns_transport_t* value) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(value, GETDNS_RETURN_INVALID_PARAMETER);
|
2015-06-19 12:28:29 -05:00
|
|
|
int count = context->dns_transport_count;
|
|
|
|
getdns_transport_list_t *transports = context->dns_transports;
|
|
|
|
if (!count)
|
2015-06-18 11:29:23 -05:00
|
|
|
return GETDNS_RETURN_WRONG_TYPE_REQUESTED;
|
|
|
|
|
|
|
|
/* Best effort mapping for backwards compatibility*/
|
2015-06-19 12:28:29 -05:00
|
|
|
if (transports[0] == GETDNS_TRANSPORT_UDP) {
|
2015-06-18 11:29:23 -05:00
|
|
|
if (count == 1)
|
|
|
|
*value = GETDNS_TRANSPORT_UDP_ONLY;
|
2015-06-19 12:28:29 -05:00
|
|
|
else if (count == 2 && transports[1] == GETDNS_TRANSPORT_TCP)
|
2015-06-18 11:29:23 -05:00
|
|
|
*value = GETDNS_TRANSPORT_UDP_FIRST_AND_FALL_BACK_TO_TCP;
|
|
|
|
else
|
|
|
|
return GETDNS_RETURN_WRONG_TYPE_REQUESTED;
|
|
|
|
}
|
2015-06-19 12:28:29 -05:00
|
|
|
if (transports[0] == GETDNS_TRANSPORT_TCP) {
|
2015-06-18 11:29:23 -05:00
|
|
|
if (count == 1)
|
|
|
|
*value = GETDNS_TRANSPORT_TCP_ONLY_KEEP_CONNECTIONS_OPEN;
|
|
|
|
}
|
2015-06-19 12:28:29 -05:00
|
|
|
if (transports[0] == GETDNS_TRANSPORT_TLS) {
|
2015-06-18 11:29:23 -05:00
|
|
|
if (count == 1)
|
|
|
|
*value = GETDNS_TRANSPORT_TLS_ONLY_KEEP_CONNECTIONS_OPEN;
|
2015-06-19 12:28:29 -05:00
|
|
|
else if (count == 2 && transports[1] == GETDNS_TRANSPORT_TCP)
|
2015-06-18 11:29:23 -05:00
|
|
|
*value = GETDNS_TRANSPORT_TLS_FIRST_AND_FALL_BACK_TO_TCP_KEEP_CONNECTIONS_OPEN;
|
|
|
|
else
|
|
|
|
return GETDNS_RETURN_WRONG_TYPE_REQUESTED;
|
|
|
|
}
|
2015-06-19 12:28:29 -05:00
|
|
|
if (transports[0] == GETDNS_TRANSPORT_STARTTLS) {
|
|
|
|
if (count == 2 && transports[1] == GETDNS_TRANSPORT_TCP)
|
2015-06-18 11:29:23 -05:00
|
|
|
*value = GETDNS_TRANSPORT_STARTTLS_FIRST_AND_FALL_BACK_TO_TCP_KEEP_CONNECTIONS_OPEN;
|
|
|
|
else
|
|
|
|
return GETDNS_RETURN_WRONG_TYPE_REQUESTED;
|
|
|
|
}
|
2014-12-06 22:46:44 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
2015-06-17 11:18:09 -05:00
|
|
|
getdns_return_t
|
|
|
|
getdns_context_get_dns_transport_list(getdns_context *context,
|
|
|
|
size_t* transport_count, getdns_transport_list_t **transports) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(transport_count, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(transports, GETDNS_RETURN_INVALID_PARAMETER);
|
2015-06-19 12:28:29 -05:00
|
|
|
*transport_count = context->dns_transport_count;
|
|
|
|
if (!context->dns_transport_count) {
|
2015-06-17 11:18:09 -05:00
|
|
|
*transports = NULL;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
2015-06-19 12:28:29 -05:00
|
|
|
// use normal malloc here so users can do normal free
|
|
|
|
*transports = malloc(context->dns_transport_count * sizeof(getdns_transport_list_t));
|
|
|
|
memcpy(*transports, context->dns_transports,
|
|
|
|
context->dns_transport_count * sizeof(getdns_transport_list_t));
|
2015-06-17 11:18:09 -05:00
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
2014-12-06 22:46:44 -06:00
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_get_limit_outstanding_queries(getdns_context *context,
|
|
|
|
uint16_t* value) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(value, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
*value = context->limit_outstanding_queries;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_get_timeout(getdns_context *context, uint64_t* value) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(value, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
*value = context->timeout;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
2015-06-18 11:11:11 -05:00
|
|
|
getdns_return_t
|
|
|
|
getdns_context_get_idle_timeout(getdns_context *context, uint64_t* value) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(value, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
*value = context->idle_timeout;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
2014-12-06 22:46:44 -06:00
|
|
|
getdns_return_t
|
|
|
|
getdns_context_get_follow_redirects(getdns_context *context,
|
|
|
|
getdns_redirects_t* value) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(value, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
*value = context->follow_redirects;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_get_dns_root_servers(getdns_context *context,
|
|
|
|
getdns_list **value) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(value, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
*value = NULL;
|
|
|
|
if (context->dns_root_servers) {
|
2015-08-19 09:30:15 -05:00
|
|
|
return _getdns_list_copy(context->dns_root_servers, value);
|
2014-12-06 22:46:44 -06:00
|
|
|
}
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_get_append_name(getdns_context *context,
|
|
|
|
getdns_append_name_t* value) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(value, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
*value = context->append_name;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_get_suffix(getdns_context *context, getdns_list **value) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(value, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
*value = NULL;
|
|
|
|
if (context->suffix) {
|
2015-08-19 09:30:15 -05:00
|
|
|
return _getdns_list_copy(context->suffix, value);
|
2014-12-06 22:46:44 -06:00
|
|
|
}
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
2015-06-30 07:43:52 -05:00
|
|
|
getdns_context_get_dnssec_trust_anchors(
|
|
|
|
getdns_context *context, getdns_list **value)
|
|
|
|
{
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(value, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
|
|
|
|
if (context->trust_anchors) {
|
|
|
|
if ((*value = getdns_list_create_with_context(context)))
|
|
|
|
_getdns_wire2list( context->trust_anchors
|
|
|
|
, context->trust_anchors_len
|
|
|
|
, *value);
|
|
|
|
else
|
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
} else
|
|
|
|
*value = NULL;
|
|
|
|
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2014-12-06 22:46:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_get_dnssec_allowed_skew(getdns_context *context,
|
|
|
|
uint32_t* value) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(value, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
*value = context->dnssec_allowed_skew;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_get_upstream_recursive_servers(getdns_context *context,
|
|
|
|
getdns_list **upstream_list) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(upstream_list, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
*upstream_list = NULL;
|
|
|
|
if (context->upstreams && context->upstreams->count > 0) {
|
|
|
|
getdns_return_t r = GETDNS_RETURN_GOOD;
|
|
|
|
size_t i;
|
|
|
|
getdns_upstream *upstream;
|
|
|
|
getdns_list *upstreams = getdns_list_create();
|
2015-07-19 02:43:12 -05:00
|
|
|
for (i = 0; i < context->upstreams->count;) {
|
2015-07-13 09:22:39 -05:00
|
|
|
size_t j;
|
|
|
|
getdns_dict *d;
|
|
|
|
upstream = &context->upstreams->upstreams[i];
|
|
|
|
d = sockaddr_dict(context, (struct sockaddr *)&upstream->addr);
|
|
|
|
for ( j = 1, i++
|
|
|
|
; j < GETDNS_UPSTREAM_TRANSPORTS &&
|
|
|
|
i < context->upstreams->count
|
|
|
|
; j++, i++) {
|
|
|
|
|
|
|
|
upstream = &context->upstreams->upstreams[i];
|
|
|
|
if (upstream->transport != GETDNS_TRANSPORT_TLS)
|
|
|
|
continue;
|
|
|
|
if (upstream_port(upstream) != getdns_port_array[j])
|
|
|
|
continue;
|
|
|
|
(void) getdns_dict_set_int(d, "tls_port",
|
|
|
|
(uint32_t) upstream_port(upstream));
|
|
|
|
}
|
2015-08-19 09:30:15 -05:00
|
|
|
r |= _getdns_list_append_dict(upstreams, d);
|
2015-07-13 09:22:39 -05:00
|
|
|
getdns_dict_destroy(d);
|
2014-12-06 22:46:44 -06:00
|
|
|
}
|
|
|
|
if (r != GETDNS_RETURN_GOOD) {
|
|
|
|
getdns_list_destroy(upstreams);
|
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
}
|
|
|
|
*upstream_list = upstreams;
|
|
|
|
}
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_get_edns_maximum_udp_payload_size(getdns_context *context,
|
|
|
|
uint16_t* value) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(value, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
*value = context->edns_maximum_udp_payload_size;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_get_edns_extended_rcode(getdns_context *context,
|
|
|
|
uint8_t* value) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(value, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
*value = context->edns_extended_rcode;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_get_edns_version(getdns_context *context, uint8_t* value) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(value, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
*value = context->edns_version;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
getdns_context_get_edns_do_bit(getdns_context *context, uint8_t* value) {
|
|
|
|
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
RETURN_IF_NULL(value, GETDNS_RETURN_INVALID_PARAMETER);
|
|
|
|
*value = context->edns_do_bit;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
2014-01-28 08:30:01 -06:00
|
|
|
/* context.c */
|