mirror of https://github.com/getdnsapi/getdns.git
First pass of implementing per query namespace lookup for stub resolver. Also a getdns local namespace lookup is now used for all sync calls.
This commit is contained in:
parent
ea4ee27612
commit
050506341c
173
src/context.c
173
src/context.c
|
@ -52,8 +52,14 @@
|
|||
|
||||
void *plain_mem_funcs_user_arg = MF_PLAIN;
|
||||
|
||||
struct host_name_addr_type {
|
||||
ldns_rdf * host_name;
|
||||
ldns_rr_type addr_type;
|
||||
};
|
||||
|
||||
/* Private functions */
|
||||
getdns_return_t create_default_namespaces(struct getdns_context *context);
|
||||
getdns_return_t create_local_hosts(struct getdns_context *context);
|
||||
static struct getdns_list *create_default_root_servers(void);
|
||||
static getdns_return_t add_ip_str(struct getdns_dict *);
|
||||
static struct getdns_dict *create_ipaddr_dict_from_rdf(struct getdns_context *,
|
||||
|
@ -63,6 +69,7 @@ static struct getdns_list *create_from_ldns_list(struct getdns_context *,
|
|||
static getdns_return_t set_os_defaults(struct getdns_context *);
|
||||
static int transaction_id_cmp(const void *, const void *);
|
||||
static int timeout_cmp(const void *, const void *);
|
||||
static int local_host_cmp(const void *, const void *);
|
||||
static void dispatch_updated(struct getdns_context *, uint16_t);
|
||||
static void cancel_dns_req(getdns_dns_req *);
|
||||
static void cancel_outstanding_requests(struct getdns_context*, int);
|
||||
|
@ -107,6 +114,63 @@ create_default_namespaces(struct getdns_context *context)
|
|||
return GETDNS_RETURN_GOOD;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to get contents from hosts file
|
||||
*/
|
||||
getdns_return_t
|
||||
create_local_hosts(struct getdns_context *context)
|
||||
{
|
||||
|
||||
ldns_rr_list * host_names = ldns_get_rr_list_hosts_frm_file(NULL);
|
||||
if (host_names == NULL)
|
||||
return GETDNS_RETURN_GENERIC_ERROR;
|
||||
|
||||
/*TODO: free up memory on error paths*/
|
||||
//ldns_rr_list_print(stderr, host_names);
|
||||
|
||||
/* We have a 1:1 list of name -> ip address where there is an
|
||||
underlying many to many relationship. Need to create a lookup of
|
||||
(unique name + A/AAAA)-> list of IPV4/IPv6 ip addresses*/
|
||||
for (int i = 0 ; i<ldns_rr_list_rr_count(host_names) ; i++) {
|
||||
|
||||
ldns_rr *rr = ldns_rr_list_rr(host_names, i);
|
||||
ldns_rdf *owner = ldns_rdf_clone(ldns_rr_owner(rr));
|
||||
|
||||
/*Check to see if we already have an entry*/
|
||||
struct host_name_addr_type *lh_key =
|
||||
GETDNS_MALLOC(context->my_mf, struct host_name_addr_type);
|
||||
if (lh_key == NULL)
|
||||
return GETDNS_RETURN_MEMORY_ERROR;
|
||||
lh_key->host_name = owner;
|
||||
lh_key->addr_type = ldns_rr_get_type(rr);
|
||||
ldns_rbnode_t *result_node = ldns_rbtree_search(context->local_hosts, lh_key);
|
||||
if (result_node) {
|
||||
if (!ldns_rr_list_push_rr ((ldns_rr_list *)result_node->data, ldns_rr_clone(rr)))
|
||||
return GETDNS_RETURN_GENERIC_ERROR;
|
||||
}
|
||||
else {
|
||||
ldns_rr_list *address_list = ldns_rr_list_new ();
|
||||
if (!ldns_rr_list_push_rr (address_list, ldns_rr_clone(rr)))
|
||||
return GETDNS_RETURN_GENERIC_ERROR;
|
||||
|
||||
ldns_rbnode_t *node = GETDNS_MALLOC(context->my_mf, ldns_rbnode_t);
|
||||
if (!node) {
|
||||
return GETDNS_RETURN_GENERIC_ERROR;
|
||||
}
|
||||
node->key = lh_key;
|
||||
node->data = address_list;
|
||||
if (!ldns_rbtree_insert(context->local_hosts, node)) {
|
||||
/* free the node */
|
||||
GETDNS_FREE(context->my_mf, node);
|
||||
return GETDNS_RETURN_GENERIC_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return GETDNS_RETURN_GOOD;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Helper to get the default root servers.
|
||||
* TODO: Implement
|
||||
|
@ -385,6 +449,27 @@ timeout_cmp(const void *to1, const void *to2)
|
|||
}
|
||||
}
|
||||
|
||||
static int
|
||||
local_host_cmp(const void *id1, const void *id2)
|
||||
{
|
||||
if (id1 == NULL && id2 == NULL) {
|
||||
return 0;
|
||||
} else if (id1 == NULL && id2 != NULL) {
|
||||
return 1;
|
||||
} else if (id1 != NULL && id2 == NULL) {
|
||||
return -1;
|
||||
} else {
|
||||
const struct host_name_addr_type *hn1 = (const struct host_name_addr_type*) id1;
|
||||
const struct host_name_addr_type *hn2 = (const struct host_name_addr_type*) id2;
|
||||
if ((ldns_rr_type) hn1->addr_type < (ldns_rr_type) hn2->addr_type)
|
||||
return -1;
|
||||
if ((ldns_rr_type) hn1->addr_type > (ldns_rr_type) hn2->addr_type)
|
||||
return 1;
|
||||
return (ldns_rdf_compare((const ldns_rdf *) hn1->host_name,
|
||||
(const ldns_rdf *) hn2->host_name));
|
||||
}
|
||||
}
|
||||
|
||||
static ldns_rbtree_t*
|
||||
create_ldns_rbtree(getdns_context * context,
|
||||
int(*cmpf)(const void *, const void *)) {
|
||||
|
@ -444,6 +529,7 @@ getdns_context_create_with_extended_memory_functions(
|
|||
result->outbound_requests = create_ldns_rbtree(result, transaction_id_cmp);
|
||||
result->timeouts_by_time = create_ldns_rbtree(result, timeout_cmp);
|
||||
result->timeouts_by_id = create_ldns_rbtree(result, transaction_id_cmp);
|
||||
result->local_hosts = create_ldns_rbtree(result, local_host_cmp);
|
||||
|
||||
|
||||
result->resolution_type = GETDNS_RESOLUTION_RECURSING;
|
||||
|
@ -484,7 +570,8 @@ getdns_context_create_with_extended_memory_functions(
|
|||
result->return_dnssec_status = GETDNS_EXTENSION_FALSE;
|
||||
if (!result->outbound_requests ||
|
||||
!result->timeouts_by_id ||
|
||||
!result->timeouts_by_time) {
|
||||
!result->timeouts_by_time ||
|
||||
!result->local_hosts) {
|
||||
getdns_context_destroy(result);
|
||||
return GETDNS_RETURN_MEMORY_ERROR;
|
||||
}
|
||||
|
@ -495,7 +582,12 @@ getdns_context_create_with_extended_memory_functions(
|
|||
return GETDNS_RETURN_GENERIC_ERROR;
|
||||
}
|
||||
/* ldns context is initialised to NULL here and rebuilt later if needed */
|
||||
result->ldns_res = NULL;
|
||||
result->ldns_res = NULL;
|
||||
|
||||
if(create_local_hosts(result) != GETDNS_RETURN_GOOD) {
|
||||
getdns_context_destroy(result);
|
||||
return GETDNS_RETURN_GENERIC_ERROR;
|
||||
}
|
||||
|
||||
*context = result;
|
||||
|
||||
|
@ -591,6 +683,10 @@ getdns_context_destroy(struct getdns_context *context)
|
|||
GETDNS_FREE(context->my_mf, context->timeouts_by_id);
|
||||
if (context->timeouts_by_time)
|
||||
GETDNS_FREE(context->my_mf, context->timeouts_by_time);
|
||||
if (context->local_hosts) {
|
||||
/*TODO: deep free of this tree*/
|
||||
GETDNS_FREE(context->my_mf, context->local_hosts);
|
||||
}
|
||||
|
||||
GETDNS_FREE(context->my_mf, context);
|
||||
} /* getdns_context_destroy */
|
||||
|
@ -2023,4 +2119,77 @@ getdns_context_set_use_threads(getdns_context* context, int use_threads) {
|
|||
return r == 0 ? GETDNS_RETURN_GOOD : GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
|
||||
}
|
||||
|
||||
getdns_return_t
|
||||
getdns_context_local_namespace_resolve(getdns_dns_req* req,
|
||||
struct getdns_context *context)
|
||||
{
|
||||
|
||||
/* NOTE: This only returns GETDNS_RETURN_GOOD if it finds answers for all the
|
||||
netreq that it tries */
|
||||
/*TODO: free memory on error paths*/
|
||||
|
||||
getdns_network_req *netreq = req->first_req;
|
||||
while (netreq) {
|
||||
/*This request may have already been answered by another namespace*/
|
||||
if (netreq->result) {
|
||||
netreq = netreq->next;
|
||||
continue;
|
||||
}
|
||||
if (netreq->request_type != GETDNS_RRTYPE_A && netreq->request_type != GETDNS_RRTYPE_AAAA)
|
||||
return GETDNS_RETURN_GENERIC_ERROR;
|
||||
|
||||
/*Do the lookup*/
|
||||
ldns_rdf *query_name = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME, req->name);
|
||||
struct host_name_addr_type *lh_key =
|
||||
GETDNS_MALLOC(context->my_mf, struct host_name_addr_type);
|
||||
if (lh_key == NULL)
|
||||
return GETDNS_RETURN_MEMORY_ERROR;
|
||||
lh_key->host_name = query_name;
|
||||
lh_key->addr_type = netreq->request_type;
|
||||
ldns_rbnode_t *result_node = ldns_rbtree_search(context->local_hosts, lh_key);
|
||||
if (!result_node) {
|
||||
ldns_rdf_deep_free(query_name);
|
||||
return GETDNS_RETURN_GENERIC_ERROR;
|
||||
}
|
||||
|
||||
/*Fabricate the result packet*/
|
||||
ldns_pkt *answer_pkt;
|
||||
ldns_rr *question_rr;
|
||||
ldns_rr_list *answer_qr;
|
||||
ldns_rr_list *answer_an;
|
||||
ldns_rr_list *answer_ns;
|
||||
ldns_rr_list *answer_ad;
|
||||
|
||||
question_rr = ldns_rr_new_frm_type(netreq->request_type);
|
||||
ldns_rr_set_class(question_rr, netreq->request_class);
|
||||
ldns_rr_set_owner(question_rr, query_name);
|
||||
ldns_rr_set_rd_count (question_rr, (size_t)0);
|
||||
answer_qr = ldns_rr_list_new();
|
||||
if (!ldns_rr_list_push_rr (answer_qr, question_rr)) {
|
||||
ldns_rdf_deep_free(query_name);
|
||||
ldns_rr_free(question_rr);
|
||||
ldns_rr_list_deep_free(answer_qr);
|
||||
return GETDNS_RETURN_GENERIC_ERROR;
|
||||
}
|
||||
|
||||
answer_an = ldns_rr_list_clone((ldns_rr_list *)result_node->data);
|
||||
answer_ns = ldns_rr_list_new();
|
||||
answer_ad = ldns_rr_list_new();
|
||||
|
||||
answer_pkt = ldns_pkt_new();
|
||||
ldns_pkt_set_qr(answer_pkt, 1);
|
||||
ldns_pkt_set_aa(answer_pkt, 1);
|
||||
|
||||
ldns_pkt_push_rr_list(answer_pkt, LDNS_SECTION_QUESTION, answer_qr);
|
||||
ldns_pkt_push_rr_list(answer_pkt, LDNS_SECTION_ANSWER, answer_an);
|
||||
ldns_pkt_push_rr_list(answer_pkt, LDNS_SECTION_AUTHORITY, answer_ns);
|
||||
ldns_pkt_push_rr_list(answer_pkt, LDNS_SECTION_ADDITIONAL, answer_ad);
|
||||
|
||||
netreq->result = answer_pkt;
|
||||
netreq = netreq->next;
|
||||
}
|
||||
return GETDNS_RETURN_GOOD;
|
||||
|
||||
}
|
||||
|
||||
/* context.c */
|
||||
|
|
|
@ -99,6 +99,8 @@ struct getdns_context {
|
|||
/* The underlying contexts that do the real work */
|
||||
struct ub_ctx *unbound_ctx;
|
||||
ldns_resolver *ldns_res;
|
||||
/* A tree to hold local host information*/
|
||||
struct ldns_rbtree_t *local_hosts;
|
||||
int has_ta; /* No DNSSEC without trust anchor */
|
||||
int return_dnssec_status;
|
||||
|
||||
|
@ -183,6 +185,10 @@ getdns_return_t getdns_context_schedule_timeout(struct getdns_context* context,
|
|||
getdns_return_t getdns_context_clear_timeout(struct getdns_context* context,
|
||||
getdns_transaction_t id);
|
||||
|
||||
/* perform name resolution in /etc/hosts */
|
||||
getdns_return_t getdns_context_local_namespace_resolve(getdns_dns_req* req,
|
||||
struct getdns_context *context);
|
||||
|
||||
int filechg_check(struct getdns_context *context, struct filechg *fchg);
|
||||
|
||||
#endif /* _GETDNS_CONTEXT_H_ */
|
||||
|
|
152
src/sync.c
152
src/sync.c
|
@ -56,6 +56,11 @@ static getdns_return_t submit_request_sync_rec(
|
|||
getdns_network_req *netreq = req->first_req;
|
||||
|
||||
while (netreq) {
|
||||
/*This request may have already been answered by another namespace*/
|
||||
if (netreq->result) {
|
||||
netreq = netreq->next;
|
||||
continue;
|
||||
}
|
||||
int r = ub_timed_resolve(req->context->unbound_ctx,
|
||||
req->name,
|
||||
netreq->request_type,
|
||||
|
@ -85,6 +90,11 @@ static getdns_return_t submit_request_sync_stub(
|
|||
struct timeval tv;
|
||||
|
||||
while (netreq) {
|
||||
/*This request may have already been answered by another namespace*/
|
||||
if (netreq->result) {
|
||||
netreq = netreq->next;
|
||||
continue;
|
||||
}
|
||||
qname = ldns_dname_new_frm_str(req->name);
|
||||
qflags = qflags | LDNS_RD;
|
||||
/* TODO: Use timeout properly - create a ldns_timed_resolve function */
|
||||
|
@ -95,6 +105,10 @@ static getdns_return_t submit_request_sync_stub(
|
|||
netreq->result = ldns_resolver_query(
|
||||
req->context->ldns_res, qname, netreq->request_type,
|
||||
netreq->request_class, qflags);
|
||||
/*TODO: The rec unbound case always sends DO=1 and then
|
||||
getdns_apply_network_result sets these values...*/
|
||||
// netreq->secure = ;
|
||||
// netreq->bogus = ;
|
||||
ldns_rdf_deep_free(qname);
|
||||
qname = NULL;
|
||||
|
||||
|
@ -118,6 +132,86 @@ static getdns_return_t submit_request_sync(
|
|||
}
|
||||
}
|
||||
|
||||
getdns_return_t
|
||||
getdns_general_sync_ns(struct getdns_context *context,
|
||||
const char *name,
|
||||
uint16_t request_type,
|
||||
struct getdns_dict *extensions,
|
||||
struct getdns_dict **response,
|
||||
bool usenamespaces)
|
||||
{
|
||||
getdns_dns_req *req;
|
||||
getdns_return_t response_status;
|
||||
uint64_t timeout;
|
||||
|
||||
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
||||
RETURN_IF_NULL(response, GETDNS_RETURN_INVALID_PARAMETER);
|
||||
RETURN_IF_NULL(name, GETDNS_RETURN_INVALID_PARAMETER);
|
||||
|
||||
timeout = context->timeout;
|
||||
response_status = validate_dname(name);
|
||||
if (response_status != GETDNS_RETURN_GOOD)
|
||||
return response_status;
|
||||
|
||||
response_status = validate_extensions(extensions);
|
||||
if (response_status != GETDNS_RETURN_GOOD)
|
||||
return response_status;
|
||||
|
||||
/* Set up the context assuming we won't use the specified namespaces.
|
||||
This is (currently) identical to setting up a pure DNS namespace */
|
||||
response_status = getdns_context_prepare_for_resolution(context, 0);
|
||||
if (response_status != GETDNS_RETURN_GOOD)
|
||||
return response_status;
|
||||
|
||||
/* create the request */
|
||||
req = dns_req_new(context, name, request_type, extensions);
|
||||
if (!req)
|
||||
return GETDNS_RETURN_MEMORY_ERROR;
|
||||
|
||||
/*TODO: Would be tidier to loop over the netreq here trying each namespace
|
||||
rather then trying each namespace...*/
|
||||
|
||||
/* resolve using the appropriate namespace*/
|
||||
if (!usenamespaces) {
|
||||
response_status = submit_request_sync(req, context);
|
||||
} else {
|
||||
for (int i = 0; i < context->namespace_count; i++) {
|
||||
switch (context->namespaces[i]) {
|
||||
case GETDNS_NAMESPACE_LOCALNAMES:
|
||||
response_status = getdns_context_local_namespace_resolve(req, context);
|
||||
break;
|
||||
|
||||
case GETDNS_NAMESPACE_DNS:
|
||||
response_status = submit_request_sync(req, context);
|
||||
break;
|
||||
|
||||
default:
|
||||
response_status = GETDNS_RETURN_BAD_CONTEXT;
|
||||
break;
|
||||
}
|
||||
/* If we have all good responses break out the for loop as we are done,
|
||||
but if we don't then give the next namespace a try*/
|
||||
if (response_status == GETDNS_RETURN_GOOD)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (response_status == GETDNS_RETURN_GOOD) {
|
||||
if (is_extension_set(req->extensions,
|
||||
"dnssec_return_validation_chain"))
|
||||
*response = priv_getdns_get_validation_chain_sync(req, &timeout);
|
||||
else
|
||||
*response = create_getdns_response(req);
|
||||
|
||||
} else if (response_status == GETDNS_RESPSTATUS_ALL_TIMEOUT) {
|
||||
*response = create_getdns_response(req);
|
||||
response_status = GETDNS_RETURN_GOOD;
|
||||
}
|
||||
|
||||
dns_req_free(req);
|
||||
return response_status;
|
||||
}
|
||||
|
||||
getdns_return_t
|
||||
getdns_general_sync(struct getdns_context *context,
|
||||
const char *name,
|
||||
|
@ -125,49 +219,9 @@ getdns_general_sync(struct getdns_context *context,
|
|||
struct getdns_dict *extensions,
|
||||
struct getdns_dict **response)
|
||||
{
|
||||
getdns_dns_req *req;
|
||||
getdns_return_t response_status;
|
||||
uint64_t timeout;
|
||||
|
||||
RETURN_IF_NULL(context, GETDNS_RETURN_INVALID_PARAMETER);
|
||||
RETURN_IF_NULL(response, GETDNS_RETURN_INVALID_PARAMETER);
|
||||
RETURN_IF_NULL(name, GETDNS_RETURN_INVALID_PARAMETER);
|
||||
|
||||
timeout = context->timeout;
|
||||
response_status = validate_dname(name);
|
||||
if (response_status != GETDNS_RETURN_GOOD)
|
||||
return response_status;
|
||||
|
||||
response_status = validate_extensions(extensions);
|
||||
if (response_status != GETDNS_RETURN_GOOD)
|
||||
return response_status;
|
||||
|
||||
/* general, so without dns lookup (no namespaces) */;
|
||||
response_status = getdns_context_prepare_for_resolution(context, 0);
|
||||
if (response_status != GETDNS_RETURN_GOOD)
|
||||
return response_status;
|
||||
|
||||
/* for each netreq we call ub_ctx_resolve */
|
||||
/* request state */
|
||||
req = dns_req_new(context, name, request_type, extensions);
|
||||
if (!req)
|
||||
return GETDNS_RETURN_MEMORY_ERROR;
|
||||
|
||||
response_status = submit_request_sync(req, context);
|
||||
if (response_status == GETDNS_RETURN_GOOD) {
|
||||
if (is_extension_set(req->extensions,
|
||||
"dnssec_return_validation_chain"))
|
||||
*response = priv_getdns_get_validation_chain_sync(req, &timeout);
|
||||
else
|
||||
*response = create_getdns_response(req);
|
||||
|
||||
} else if (response_status == GETDNS_RESPSTATUS_ALL_TIMEOUT) {
|
||||
*response = create_getdns_response(req);
|
||||
response_status = GETDNS_RETURN_GOOD;
|
||||
}
|
||||
|
||||
dns_req_free(req);
|
||||
return response_status;
|
||||
/* general, so without dns lookup (no namespaces) */;
|
||||
return getdns_general_sync_ns(context, name, request_type,
|
||||
extensions, response, false);
|
||||
}
|
||||
|
||||
getdns_return_t
|
||||
|
@ -185,8 +239,8 @@ getdns_address_sync(struct getdns_context *context,
|
|||
GETDNS_STR_EXTENSION_RETURN_BOTH_V4_AND_V6, GETDNS_EXTENSION_TRUE);
|
||||
|
||||
getdns_return_t result =
|
||||
getdns_general_sync(context, name, GETDNS_RRTYPE_A,
|
||||
extensions, response);
|
||||
getdns_general_sync_ns(context, name, GETDNS_RRTYPE_A,
|
||||
extensions, response, true);
|
||||
if (cleanup_extensions) {
|
||||
getdns_dict_destroy(extensions);
|
||||
}
|
||||
|
@ -226,8 +280,8 @@ getdns_hostname_sync(struct getdns_context *context,
|
|||
return GETDNS_RETURN_INVALID_PARAMETER;
|
||||
if ((name = reverse_address(address_data)) == NULL)
|
||||
return GETDNS_RETURN_INVALID_PARAMETER;
|
||||
retval = getdns_general_sync(context, name, req_type, extensions,
|
||||
response);
|
||||
retval = getdns_general_sync_ns(context, name, req_type, extensions,
|
||||
response, true);
|
||||
free(name);
|
||||
return retval;
|
||||
}
|
||||
|
@ -239,8 +293,8 @@ getdns_service_sync(struct getdns_context *context,
|
|||
struct getdns_dict ** response)
|
||||
{
|
||||
|
||||
return getdns_general_sync(context, name, GETDNS_RRTYPE_SRV,
|
||||
extensions, response);
|
||||
return getdns_general_sync_ns(context, name, GETDNS_RRTYPE_SRV,
|
||||
extensions, response, true);
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue