Adding A and AAAA handling for get_address

This commit is contained in:
Neel Goyal 2013-10-16 18:33:12 -04:00
parent c53c00ee2b
commit da8dad5913
10 changed files with 412 additions and 305 deletions

View File

@ -27,11 +27,11 @@ LDFLAGS=-levent_core -lldns -lunbound
default: all default: all
all: libgetdns all: libgetdns
cd test && $(MAKE) $@ cd test && $(MAKE) $@
cd example && $(MAKE) $@ cd example && $(MAKE) $@
libgetdns: sync.o context.o list.o dict.o convert.o general.o hostname.o service.o \ libgetdns: sync.o context.o list.o dict.o convert.o general.o hostname.o service.o request-internal.o \
validate_dnssec.o util-internal.o getdns_error.o validate_dnssec.o util-internal.o getdns_error.o
$(CC) $(CFLAGS) $(LDFLAGS) -shared -o libgetdns.so $? $(CC) $(CFLAGS) $(LDFLAGS) -shared -o libgetdns.so $?

View File

@ -1,7 +1,7 @@
/** /**
* *
* /brief getdns contect management functions * /brief getdns contect management functions
* *
* This is the meat of the API * This is the meat of the API
* Originally taken from the getdns API description pseudo implementation. * Originally taken from the getdns API description pseudo implementation.
* *
@ -15,10 +15,10 @@
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@ -33,6 +33,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "context.h" #include "context.h"
#include "util-internal.h" #include "util-internal.h"
#include "types-internal.h"
#include <ldns/ldns.h> #include <ldns/ldns.h>
#include <event2/event.h> #include <event2/event.h>
#include <unbound.h> #include <unbound.h>
@ -122,7 +123,7 @@ static struct getdns_list* create_from_ldns_list(ldns_rdf** ldns_list, size_t co
getdns_dict_destroy(ipaddr); getdns_dict_destroy(ipaddr);
} }
break; break;
case LDNS_RDF_TYPE_DNAME: case LDNS_RDF_TYPE_DNAME:
{ {
getdns_bindata item; getdns_bindata item;
@ -134,7 +135,7 @@ static struct getdns_list* create_from_ldns_list(ldns_rdf** ldns_list, size_t co
free(srch); free(srch);
} }
break; break;
default: default:
break; break;
} }
@ -162,6 +163,26 @@ static getdns_return_t set_os_defaults(getdns_context_t context) {
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} }
static int transaction_id_cmp(const void* id1, const void* id2) {
if (id1 == NULL && id2 == NULL) {
return 0;
} else if (id1 == NULL && id2 != NULL) {
return 1;
} else if (id1 != NULL && id2 == NULL) {
return -1;
} else {
getdns_transaction_t t1 = *((const getdns_transaction_t*) id1);
getdns_transaction_t t2 = *((const getdns_transaction_t*) id2);
if (t1 == t2) {
return 0;
} else if (t1 < t2) {
return -1;
} else {
return 1;
}
}
}
/* /*
* getdns_context_create * getdns_context_create
* *
@ -193,21 +214,24 @@ getdns_return_t getdns_context_create(
result->unbound_sync = ub_ctx_create_event(result->event_base_sync); result->unbound_sync = ub_ctx_create_event(result->event_base_sync);
/* create the async one also so options are kept up to date */ /* create the async one also so options are kept up to date */
result->unbound_async = ub_ctx_create_event(result->event_base_sync); result->unbound_async = ub_ctx_create_event(result->event_base_sync);
result->async_set = 0; result->async_set = 0;
result->resolution_type_set = 0; result->resolution_type_set = 0;
result->outbound_requests = ldns_rbtree_create(transaction_id_cmp);
result->resolution_type = GETDNS_CONTEXT_RECURSING; result->resolution_type = GETDNS_CONTEXT_RECURSING;
result->namespaces = create_default_namespaces(); result->namespaces = create_default_namespaces();
result->timeout = 5000; result->timeout = 5000;
result->follow_redirects = GETDNS_CONTEXT_FOLLOW_REDIRECTS; result->follow_redirects = GETDNS_CONTEXT_FOLLOW_REDIRECTS;
result->dns_root_servers = create_default_root_servers(); result->dns_root_servers = create_default_root_servers();
result->append_name = GETDNS_CONTEXT_APPEND_NAME_ALWAYS; result->append_name = GETDNS_CONTEXT_APPEND_NAME_ALWAYS;
result->suffix = NULL; result->suffix = NULL;
result->dnssec_trust_anchors = NULL; result->dnssec_trust_anchors = NULL;
result->upstream_list = NULL; result->upstream_list = NULL;
result->edns_extended_rcode = 0; result->edns_extended_rcode = 0;
result->edns_version = 0; result->edns_version = 0;
result->edns_do_bit = 0; result->edns_do_bit = 0;
@ -251,13 +275,14 @@ getdns_context_destroy(
getdns_list_destroy(context->suffix); getdns_list_destroy(context->suffix);
getdns_list_destroy(context->dnssec_trust_anchors); getdns_list_destroy(context->dnssec_trust_anchors);
getdns_list_destroy(context->upstream_list); getdns_list_destroy(context->upstream_list);
/* destroy the ub context */ /* destroy the ub context */
ub_ctx_delete(context->unbound_async); ub_ctx_delete(context->unbound_async);
ub_ctx_delete(context->unbound_sync); ub_ctx_delete(context->unbound_sync);
event_base_free(context->event_base_sync); event_base_free(context->event_base_sync);
ldns_rbtree_free(context->outbound_requests);
free(context); free(context);
return; return;
@ -282,7 +307,7 @@ getdns_context_set_context_update_callback(
*/ */
static void set_ub_string_opt(getdns_context_t ctx, char* opt, char* value) { static void set_ub_string_opt(getdns_context_t ctx, char* opt, char* value) {
ub_ctx_set_option(ctx->unbound_sync, opt, value); ub_ctx_set_option(ctx->unbound_sync, opt, value);
ub_ctx_set_option(ctx->unbound_async, opt, value); ub_ctx_set_option(ctx->unbound_async, opt, value);
} }
@ -303,7 +328,7 @@ static inline void clear_resolution_type_set_flag(getdns_context_t context, uint
/* /*
* getdns_context_set_context_update * getdns_context_set_context_update
* *
*/ */
getdns_return_t getdns_return_t
getdns_context_set_context_update( getdns_context_set_context_update(
@ -336,15 +361,15 @@ getdns_context_set_resolution_type(
uint16_t value uint16_t value
) )
{ {
if (value != GETDNS_CONTEXT_STUB && if (value != GETDNS_CONTEXT_STUB &&
value != GETDNS_CONTEXT_RECURSING) { value != GETDNS_CONTEXT_RECURSING) {
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
} }
context->resolution_type = value; context->resolution_type = value;
dispatch_updated(context, GETDNS_CONTEXT_CODE_RESOLUTION_TYPE); dispatch_updated(context, GETDNS_CONTEXT_CODE_RESOLUTION_TYPE);
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} /* getdns_context_set_resolution_type */ } /* getdns_context_set_resolution_type */
@ -363,17 +388,17 @@ getdns_context_set_namespaces(
if (namespace_count == 0 || namespaces == NULL) { if (namespace_count == 0 || namespaces == NULL) {
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
} }
/** clean up old namespaces **/ /** clean up old namespaces **/
context->memory_deallocator(context->namespaces); context->memory_deallocator(context->namespaces);
/** duplicate **/ /** duplicate **/
namespaces_size = namespace_count * sizeof(uint16_t); namespaces_size = namespace_count * sizeof(uint16_t);
context->namespaces = context->memory_allocator(namespaces_size); context->namespaces = context->memory_allocator(namespaces_size);
memcpy(context->namespaces, namespaces, namespaces_size); memcpy(context->namespaces, namespaces, namespaces_size);
dispatch_updated(context, GETDNS_CONTEXT_CODE_NAMESPACES); dispatch_updated(context, GETDNS_CONTEXT_CODE_NAMESPACES);
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} /* getdns_context_set_namespaces */ } /* getdns_context_set_namespaces */
@ -425,7 +450,7 @@ getdns_context_set_limit_outstanding_queries(
set_ub_number_opt(context, "num-queries-per-thread", limit); set_ub_number_opt(context, "num-queries-per-thread", limit);
dispatch_updated(context, GETDNS_CONTEXT_CODE_LIMIT_OUTSTANDING_QUERIES); dispatch_updated(context, GETDNS_CONTEXT_CODE_LIMIT_OUTSTANDING_QUERIES);
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} /* getdns_context_set_limit_outstanding_queries */ } /* getdns_context_set_limit_outstanding_queries */
@ -460,7 +485,7 @@ getdns_context_set_follow_redirects(
clear_resolution_type_set_flag(context, GETDNS_CONTEXT_RECURSING); clear_resolution_type_set_flag(context, GETDNS_CONTEXT_RECURSING);
dispatch_updated(context, GETDNS_CONTEXT_CODE_FOLLOW_REDIRECTS); dispatch_updated(context, GETDNS_CONTEXT_CODE_FOLLOW_REDIRECTS);
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} /* getdns_context_set_follow_redirects */ } /* getdns_context_set_follow_redirects */
@ -491,7 +516,7 @@ getdns_context_set_dns_root_servers(
/* validate and add ip str */ /* validate and add ip str */
for (i = 0; i < count; ++i) { for (i = 0; i < count; ++i) {
getdns_dict* dict = NULL; getdns_dict* dict = NULL;
getdns_list_get_dict(addresses, i, &dict); getdns_list_get_dict(addresses, i, &dict);
r = add_ip_str(dict); r = add_ip_str(dict);
if (r != GETDNS_RETURN_GOOD) { if (r != GETDNS_RETURN_GOOD) {
break; break;
@ -561,7 +586,7 @@ getdns_context_set_suffix(
clear_resolution_type_set_flag(context, GETDNS_CONTEXT_STUB); clear_resolution_type_set_flag(context, GETDNS_CONTEXT_STUB);
dispatch_updated(context, GETDNS_CONTEXT_CODE_SUFFIX); dispatch_updated(context, GETDNS_CONTEXT_CODE_SUFFIX);
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} /* getdns_context_set_suffix */ } /* getdns_context_set_suffix */
@ -586,7 +611,7 @@ getdns_context_set_dnssec_trust_anchors(
context->dnssec_trust_anchors = value; context->dnssec_trust_anchors = value;
dispatch_updated(context, GETDNS_CONTEXT_CODE_DNSSEC_TRUST_ANCHORS); dispatch_updated(context, GETDNS_CONTEXT_CODE_DNSSEC_TRUST_ANCHORS);
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} /* getdns_context_set_dnssec_trust_anchors */ } /* getdns_context_set_dnssec_trust_anchors */
@ -603,7 +628,7 @@ getdns_context_set_dnssec_allowed_skew(
set_ub_number_opt(context, "val-sig-skew-min", value); set_ub_number_opt(context, "val-sig-skew-min", value);
set_ub_number_opt(context, "val-sig-skew-max", value); set_ub_number_opt(context, "val-sig-skew-max", value);
dispatch_updated(context, GETDNS_CONTEXT_CODE_DNSSEC_ALLOWED_SKEW); dispatch_updated(context, GETDNS_CONTEXT_CODE_DNSSEC_ALLOWED_SKEW);
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} /* getdns_context_set_dnssec_allowed_skew */ } /* getdns_context_set_dnssec_allowed_skew */
@ -626,12 +651,12 @@ getdns_context_set_stub_resolution(
getdns_list *copy = NULL; getdns_list *copy = NULL;
if (getdns_list_copy(upstream_list, &copy) != GETDNS_RETURN_GOOD) { if (getdns_list_copy(upstream_list, &copy) != GETDNS_RETURN_GOOD) {
return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; return GETDNS_RETURN_CONTEXT_UPDATE_FAIL;
} }
upstream_list = copy; upstream_list = copy;
/* validate and add ip str */ /* validate and add ip str */
for (i = 0; i < count; ++i) { for (i = 0; i < count; ++i) {
getdns_dict* dict = NULL; getdns_dict* dict = NULL;
getdns_list_get_dict(upstream_list, i, &dict); getdns_list_get_dict(upstream_list, i, &dict);
r = add_ip_str(dict); r = add_ip_str(dict);
if (r != GETDNS_RETURN_GOOD) { if (r != GETDNS_RETURN_GOOD) {
break; break;
@ -649,7 +674,7 @@ getdns_context_set_stub_resolution(
clear_resolution_type_set_flag(context, GETDNS_CONTEXT_STUB); clear_resolution_type_set_flag(context, GETDNS_CONTEXT_STUB);
dispatch_updated(context, GETDNS_CONTEXT_CODE_UPSTREAM_RECURSIVE_SERVERS); dispatch_updated(context, GETDNS_CONTEXT_CODE_UPSTREAM_RECURSIVE_SERVERS);
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} /* getdns_context_set_stub_resolution */ } /* getdns_context_set_stub_resolution */
@ -672,7 +697,7 @@ getdns_context_set_edns_maximum_udp_payload_size(
set_ub_number_opt(context, "max-udp-size", value); set_ub_number_opt(context, "max-udp-size", value);
dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_MAXIMUM_UDP_PAYLOAD_SIZE); dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_MAXIMUM_UDP_PAYLOAD_SIZE);
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} /* getdns_context_set_edns_maximum_udp_payload_size */ } /* getdns_context_set_edns_maximum_udp_payload_size */
@ -689,7 +714,7 @@ getdns_context_set_edns_extended_rcode(
context->edns_extended_rcode = value; context->edns_extended_rcode = value;
dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_EXTENDED_RCODE); dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_EXTENDED_RCODE);
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} /* getdns_context_set_edns_extended_rcode */ } /* getdns_context_set_edns_extended_rcode */
@ -706,7 +731,7 @@ getdns_context_set_edns_version(
context->edns_version = value; context->edns_version = value;
dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_VERSION); dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_VERSION);
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} /* getdns_context_set_edns_version */ } /* getdns_context_set_edns_version */
@ -728,7 +753,7 @@ getdns_context_set_edns_do_bit(
context->edns_do_bit = value; context->edns_do_bit = value;
dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_DO_BIT); dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_DO_BIT);
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} /* getdns_context_set_edns_do_bit */ } /* getdns_context_set_edns_do_bit */
@ -791,8 +816,8 @@ getdns_extension_set_libevent_base(
ub_ctx_set_event(context->unbound_async, this_event_base); ub_ctx_set_event(context->unbound_async, this_event_base);
context->async_set = 1; context->async_set = 1;
} else { } else {
context->async_set = 0;
ub_ctx_set_event(context->unbound_async, context->event_base_sync); ub_ctx_set_event(context->unbound_async, context->event_base_sync);
context->async_set = 0;
} }
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} /* getdns_extension_set_libevent_base */ } /* getdns_extension_set_libevent_base */
@ -814,7 +839,7 @@ getdns_cancel_callback(
static void ub_setup_stub(struct ub_ctx* ctx, getdns_list* upstreams, size_t count) { static void ub_setup_stub(struct ub_ctx* ctx, getdns_list* upstreams, size_t count) {
size_t i; size_t i;
/* reset forwarding servers */ /* reset forwarding servers */
ub_ctx_set_fwd(ctx, NULL); ub_ctx_set_fwd(ctx, NULL);
for (i = 0 ; i < count; ++i) { for (i = 0 ; i < count; ++i) {
getdns_dict* dict = NULL; getdns_dict* dict = NULL;
@ -856,4 +881,36 @@ getdns_return_t getdns_context_prepare_for_resolution(getdns_context_t context)
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} }
getdns_return_t getdns_context_track_outbound_request(getdns_dns_req* req) {
if (!req) {
return GETDNS_RETURN_GENERIC_ERROR;
}
getdns_context_t context = req->context;
ldns_rbnode_t* node = context->memory_allocator(sizeof(ldns_rbnode_t));
if (!node) {
return GETDNS_RETURN_GENERIC_ERROR;
}
node->key = &(req->trans_id);
node->data = req;
if (!ldns_rbtree_insert(context->outbound_requests, node)) {
/* free the node */
context->memory_deallocator(node);
return GETDNS_RETURN_GENERIC_ERROR;
}
return GETDNS_RETURN_GOOD;
}
getdns_return_t getdns_context_clear_outbound_request(getdns_dns_req* req) {
if (!req) {
return GETDNS_RETURN_GENERIC_ERROR;
}
getdns_context_t context = req->context;
ldns_rbnode_t* node = ldns_rbtree_delete(context->outbound_requests,
&(req->trans_id));
if (node) {
context->memory_deallocator(node);
}
return GETDNS_RETURN_GOOD;
}
/* getdns_context.c */ /* getdns_context.c */

View File

@ -34,6 +34,8 @@
struct event_base; struct event_base;
struct ub_ctx; struct ub_ctx;
struct ldns_rbtree_t;
struct getdns_dns_req;
/** function pointer typedefs */ /** function pointer typedefs */
typedef void (*getdns_update_callback)(getdns_context_t context, uint16_t changed_item); typedef void (*getdns_update_callback)(getdns_context_t context, uint16_t changed_item);
@ -53,11 +55,11 @@ struct getdns_context_t {
struct getdns_list *suffix; struct getdns_list *suffix;
struct getdns_list *dnssec_trust_anchors; struct getdns_list *dnssec_trust_anchors;
struct getdns_list *upstream_list; struct getdns_list *upstream_list;
uint8_t edns_extended_rcode; uint8_t edns_extended_rcode;
uint8_t edns_version; uint8_t edns_version;
uint8_t edns_do_bit; uint8_t edns_do_bit;
getdns_update_callback update_callback; getdns_update_callback update_callback;
getdns_memory_allocator memory_allocator; getdns_memory_allocator memory_allocator;
getdns_memory_deallocator memory_deallocator; getdns_memory_deallocator memory_deallocator;
@ -65,18 +67,23 @@ struct getdns_context_t {
/* Event loop for sync requests */ /* Event loop for sync requests */
struct event_base* event_base_sync; struct event_base* event_base_sync;
/* The underlying unbound contexts that do /* The underlying unbound contexts that do
the real work */ the real work */
struct ub_ctx *unbound_sync; struct ub_ctx *unbound_sync;
struct ub_ctx *unbound_async; struct ub_ctx *unbound_async;
/* whether an async event base was set */ /* whether an async event base was set */
uint8_t async_set; uint8_t async_set;
/* which resolution type the contexts are configured for
/* which resolution type the contexts are configured for
* 0 means nothing set * 0 means nothing set
*/ */
uint8_t resolution_type_set; uint8_t resolution_type_set;
/*
* outbound requests -> transaction to getdns_dns_req
*/
struct ldns_rbtree_t* outbound_requests;
} ; } ;
/** internal functions **/ /** internal functions **/
@ -86,5 +93,10 @@ struct getdns_context_t {
*/ */
getdns_return_t getdns_context_prepare_for_resolution(getdns_context_t context); getdns_return_t getdns_context_prepare_for_resolution(getdns_context_t context);
/* track an outbound request */
getdns_return_t getdns_context_track_outbound_request(struct getdns_dns_req* req);
/* clear the outbound request from being tracked - does not cancel it */
getdns_return_t getdns_context_clear_outbound_request(struct getdns_dns_req* req);
#endif #endif

View File

@ -1,7 +1,7 @@
/** /**
* *
* /brief getdns core functions * /brief getdns core functions
* *
* This is the meat of the API * This is the meat of the API
* Originally taken from the getdns API description pseudo implementation. * Originally taken from the getdns API description pseudo implementation.
* *
@ -15,10 +15,10 @@
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@ -63,101 +63,143 @@
#include <unbound-event.h> #include <unbound-event.h>
#include <ldns/ldns.h> #include <ldns/ldns.h>
#include "context.h" #include "context.h"
#include "types-internal.h"
#include "util-internal.h" #include "util-internal.h"
/* stuff to make it compile pedantically */ /* stuff to make it compile pedantically */
#define UNUSED_PARAM(x) ((void)(x)) #define UNUSED_PARAM(x) ((void)(x))
typedef struct getdns_ub_req { /* declarations */
struct ub_ctx *unbound; static void ub_resolve_callback(void* arg, int err, ldns_buffer* result, int sec, char* bogus);
getdns_context_t context; static void handle_network_request_error(getdns_network_req* netreq, int err);
char* name; static void handle_dns_request_complete(getdns_dns_req* dns_req);
void* userarg; static int submit_network_request(getdns_network_req* netreq);
getdns_callback_t callback;
getdns_transaction_t transaction_id;
} getdns_ub_req;
static void getdns_ub_req_free(getdns_ub_req* req) { /* cleanup and send an error to the user callback */
free(req->name); static void handle_network_request_error(getdns_network_req* netreq, int err) {
free(req); getdns_dns_req *dns_req = netreq->owner;
getdns_context_t context = dns_req->context;
getdns_transaction_t trans_id = dns_req->trans_id;
getdns_callback_t cb = dns_req->user_callback;
void* user_arg = dns_req->user_pointer;
/* clean up */
getdns_context_clear_outbound_request(dns_req);
dns_req_free(dns_req);
cb(context,
GETDNS_CALLBACK_ERROR,
NULL,
user_arg,
trans_id);
} }
void ub_resolve_callback(void* arg, int err, ldns_buffer* result, int sec, char* bogus) { /* cleanup and send the response to the user callback */
getdns_ub_req* req = (getdns_ub_req*) arg; static void handle_dns_request_complete(getdns_dns_req* dns_req) {
ldns_pkt* pkt = NULL; getdns_dict* response = create_getdns_response(dns_req);
if (err) {
req->callback(req->context, getdns_context_t context = dns_req->context;
GETDNS_CALLBACK_ERROR, getdns_transaction_t trans_id = dns_req->trans_id;
NULL, getdns_callback_t cb = dns_req->user_callback;
req->userarg, void* user_arg = dns_req->user_pointer;
req->transaction_id);
} else { /* clean up the request */
/* parse */ getdns_context_clear_outbound_request(dns_req);
ldns_status r = ldns_buffer2pkt_wire(&pkt, result); dns_req_free(dns_req);
if (r != LDNS_STATUS_OK) {
req->callback(req->context, cb(context,
GETDNS_CALLBACK_ERROR, GETDNS_CALLBACK_COMPLETE,
NULL, response,
req->userarg, user_arg,
req->transaction_id); trans_id);
}
static int submit_network_request(getdns_network_req* netreq) {
getdns_dns_req *dns_req = netreq->owner;
netreq->state = NET_REQ_IN_FLIGHT;
return ub_resolve_event(dns_req->unbound,
dns_req->name,
netreq->request_type,
netreq->request_class,
netreq,
ub_resolve_callback,
&(netreq->unbound_id));
}
static void ub_resolve_callback(void* arg, int err, ldns_buffer* result, int sec, char* bogus) {
getdns_network_req* netreq = (getdns_network_req*) arg;
netreq->state = NET_REQ_FINISHED;
if (err) {
handle_network_request_error(netreq, err);
} else { } else {
getdns_dict* response = create_getdns_response(pkt); /* parse */
ldns_pkt_free(pkt); ldns_status r = ldns_buffer2pkt_wire(&(netreq->result), result);
req->callback(req->context, if (r != LDNS_STATUS_OK) {
GETDNS_CALLBACK_COMPLETE, handle_network_request_error(netreq, r);
response, } else {
req->userarg, /* is this the last request */
req->transaction_id); if (!netreq->next) {
/* finished */
handle_dns_request_complete(netreq->owner);
} else {
/* not finished - update to next request and ship it */
getdns_dns_req* dns_req = netreq->owner;
dns_req->current_req = netreq->next;
submit_network_request(netreq->next);
}
}
} }
}
/* cleanup */
getdns_ub_req_free(req);
} }
getdns_return_t getdns_return_t
getdns_general_ub( getdns_general_ub(struct ub_ctx* unbound,
struct ub_ctx* unbound, getdns_context_t context,
getdns_context_t context, const char *name,
const char *name, uint16_t request_type,
uint16_t request_type, struct getdns_dict *extensions,
struct getdns_dict *extensions, void *userarg,
void *userarg, getdns_transaction_t *transaction_id,
getdns_transaction_t *transaction_id, getdns_callback_t callbackfn) {
getdns_callback_t callbackfn
) {
getdns_return_t gr;
int r; int r;
int async_id = 0;
gr = getdns_context_prepare_for_resolution(context);
if (gr != GETDNS_RETURN_GOOD) {
return GETDNS_RETURN_BAD_CONTEXT;
}
/* request state */ /* request state */
getdns_ub_req* req = (getdns_ub_req*) malloc(sizeof(getdns_ub_req)); getdns_dns_req* req = dns_req_new(context,
req->unbound = unbound; unbound,
req->context = context; name,
req->name = strdup(name); request_type,
req->userarg = userarg; extensions);
req->callback = callbackfn; if (!req) {
return GETDNS_RETURN_GENERIC_ERROR;
}
getdns_context_prepare_for_resolution(context); req->user_pointer = userarg;
req->user_callback = callbackfn;
/* TODO: /* TODO:
setup root or stub
handle immediate callback handle immediate callback
A + AAAA
*/ */
r = ub_resolve_event(unbound, req->name, request_type,
LDNS_RR_CLASS_IN, req, ub_resolve_callback,
&async_id);
if (transaction_id) { if (transaction_id) {
*transaction_id = async_id; *transaction_id = req->trans_id;
} }
req->transaction_id = async_id;
getdns_context_track_outbound_request(req);
/* issue the first network req */
r = submit_network_request(req->first_req);
if (r != 0) { if (r != 0) {
getdns_ub_req_free(req); /* clean up the request */
return GETDNS_RETURN_GENERIC_ERROR; getdns_context_clear_outbound_request(req);
dns_req_free(req);
return GETDNS_RETURN_GENERIC_ERROR;
} }
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} }
@ -165,26 +207,23 @@ getdns_general_ub(
/* /*
* getdns_general * getdns_general
*/ */
getdns_return_t getdns_return_t
getdns_general( getdns_general(getdns_context_t context,
getdns_context_t context, const char *name,
const char *name, uint16_t request_type,
uint16_t request_type, struct getdns_dict *extensions,
struct getdns_dict *extensions, void *userarg,
void *userarg, getdns_transaction_t *transaction_id,
getdns_transaction_t *transaction_id, getdns_callback_t callback) {
getdns_callback_t callback
)
{
if (!context || context->async_set == 0 || if (!context || context->async_set == 0 ||
callback == NULL) { callback == NULL) {
/* Can't do async without an event loop /* Can't do async without an event loop
* or callback * or callback
*/ */
return GETDNS_RETURN_BAD_CONTEXT; return GETDNS_RETURN_BAD_CONTEXT;
} }
return getdns_general_ub(context->unbound_async, return getdns_general_ub(context->unbound_async,
context, context,
name, name,
@ -201,16 +240,13 @@ getdns_general(
* getdns_address * getdns_address
* *
*/ */
getdns_return_t getdns_return_t
getdns_address( getdns_address(getdns_context_t context,
getdns_context_t context, const char *name,
const char *name, struct getdns_dict *extensions,
struct getdns_dict *extensions, void *userarg,
void *userarg, getdns_transaction_t *transaction_id,
getdns_transaction_t *transaction_id, getdns_callback_t callback) {
getdns_callback_t callback
)
{
int cleanup_extensions = 0; int cleanup_extensions = 0;
if (!extensions) { if (!extensions) {
extensions = getdns_dict_create(); extensions = getdns_dict_create();
@ -220,7 +256,7 @@ getdns_address(
GETDNS_STR_EXTENSION_RETURN_BOTH_V4_AND_V6, GETDNS_STR_EXTENSION_RETURN_BOTH_V4_AND_V6,
GETDNS_EXTENSION_TRUE); GETDNS_EXTENSION_TRUE);
getdns_return_t result = getdns_return_t result =
getdns_general(context, name, GETDNS_RRTYPE_A, getdns_general(context, name, GETDNS_RRTYPE_A,
extensions, userarg, transaction_id, extensions, userarg, transaction_id,
callback); callback);
@ -228,6 +264,6 @@ getdns_address(
getdns_dict_destroy(extensions); getdns_dict_destroy(extensions);
} }
return result; return result;
} }
/* getdns_general.c */ /* getdns_general.c */

View File

@ -14,10 +14,10 @@
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@ -202,7 +202,7 @@ struct event_base;
*/ */
/** /**
* \defgroup respstatus Status Codes for Responses * \defgroup respstatus Status Codes for Responses
* @{ * @{
*/ */
#define GETDNS_RESPSTATUS_GOOD 900 #define GETDNS_RESPSTATUS_GOOD 900
@ -217,7 +217,7 @@ struct event_base;
*/ */
/** /**
* \defgroup extvals Values Associated With Extensions * \defgroup extvals Values Associated With Extensions
* @{ * @{
*/ */
#define GETDNS_EXTENSION_TRUE 1 #define GETDNS_EXTENSION_TRUE 1
@ -532,8 +532,8 @@ getdns_return_t getdns_dict_get_dict(struct getdns_dict *this_dict, char *name,
*/ */
getdns_return_t getdns_dict_get_list(struct getdns_dict *this_dict, char *name, struct getdns_list **answer); getdns_return_t getdns_dict_get_list(struct getdns_dict *this_dict, char *name, struct getdns_list **answer);
/** /**
* fetch the bindata associated with the specified name, the bindata should not be * fetch the bindata associated with the specified name, the bindata should not be
* free()'d by the caller * free()'d by the caller
* @param this_dict dictionary from which to fetch the bindata * @param this_dict dictionary from which to fetch the bindata
* @param name a name/key value to look up in the dictionary * @param name a name/key value to look up in the dictionary
* @param **answer a copy of the bindata will be stored at this address * @param **answer a copy of the bindata will be stored at this address
@ -559,7 +559,7 @@ struct getdns_list * getdns_list_create();
/** /**
* free memory allocated to the list (also frees all children of the list) * free memory allocated to the list (also frees all children of the list)
* note that lists and bindata retrieved from the list via the getdns_list_get_* * note that lists and bindata retrieved from the list via the getdns_list_get_*
* helper functions will be destroyed as well - if you fetched them previously * helper functions will be destroyed as well - if you fetched them previously
* you MUST copy those instances BEFORE you destroy the list else * you MUST copy those instances BEFORE you destroy the list else
* unpleasant things will happen at run-time * unpleasant things will happen at run-time
*/ */
@ -576,9 +576,9 @@ void getdns_list_destroy(struct getdns_list *list);
getdns_return_t getdns_list_add_item(struct getdns_list *list, size_t *index); getdns_return_t getdns_list_add_item(struct getdns_list *list, size_t *index);
getdns_return_t getdns_list_set_dict(struct getdns_list *list, size_t index, struct getdns_dict *child_dict); getdns_return_t getdns_list_set_dict(struct getdns_list *list, size_t index, struct getdns_dict *child_dict);
/** /**
* assign the child_list to an item in a parent list, the parent list copies * assign the child_list to an item in a parent list, the parent list copies
* the child list and will free the copy when the list is destroyed * the child list and will free the copy when the list is destroyed
* @param list list containing the item to which child_list is to be assigned * @param list list containing the item to which child_list is to be assigned
* @param index index of the item within list to which child_list is to be assigned * @param index index of the item within list to which child_list is to be assigned
* @param *child_list list to assign to the item * @param *child_list list to assign to the item
* @return GETDNS_RETURN_GOOD on success * @return GETDNS_RETURN_GOOD on success
@ -586,9 +586,9 @@ getdns_return_t getdns_list_set_dict(struct getdns_list *list, size_t index, str
*/ */
getdns_return_t getdns_list_set_list(struct getdns_list *list, size_t index, struct getdns_list *child_list); getdns_return_t getdns_list_set_list(struct getdns_list *list, size_t index, struct getdns_list *child_list);
/** /**
* assign the child_bindata to an item in a parent list, the parent list copies * assign the child_bindata to an item in a parent list, the parent list copies
* the child data and will free the copy when the list is destroyed * the child data and will free the copy when the list is destroyed
* @param list list contiaining the item to which child_list is to be assigned * @param list list contiaining the item to which child_list is to be assigned
* @param index index of the item within list to which child_list is to be assigned * @param index index of the item within list to which child_list is to be assigned
* @param *child_bindata data to assign to the item * @param *child_bindata data to assign to the item
* @return GETDNS_RETURN_GOOD on success * @return GETDNS_RETURN_GOOD on success
@ -613,7 +613,7 @@ struct getdns_dict *getdns_dict_create();
* for freeing storage allocated to returned value * for freeing storage allocated to returned value
* NOTE: not thread safe - this needs to be fixed to be thread safe * NOTE: not thread safe - this needs to be fixed to be thread safe
* @param srcdict the dictionary structure to copy * @param srcdict the dictionary structure to copy
* @param dstdict pointer to the location to write pointer to new dictionary * @param dstdict pointer to the location to write pointer to new dictionary
* @return GETDNS_RETURN_GOOD on success * @return GETDNS_RETURN_GOOD on success
*/ */
getdns_return_t getdns_return_t
@ -721,8 +721,8 @@ getdns_cancel_callback(
/** /**
* \defgroup syncfuns Synchronous API functions that do not use callbacks * \defgroup syncfuns Synchronous API functions that do not use callbacks
* These functions do not use callbacks, when the application calls one of these * These functions do not use callbacks, when the application calls one of these
* functions the library retrieves all of the data before returning. Return * functions the library retrieves all of the data before returning. Return
* values are exactly the same as if you had used a callback with the * values are exactly the same as if you had used a callback with the
* asynchronous functions. * asynchronous functions.
* @{ * @{
*/ */

View File

@ -39,37 +39,34 @@ void network_req_free(getdns_network_req* net_req) {
if (!net_req) { if (!net_req) {
return; return;
} }
getdns_context_t context = net_req->context; getdns_context_t context = net_req->owner->context;
if (net_req->pkt) { if (net_req->result) {
ldns_pkt_free(net_req->pkt); ldns_pkt_free(net_req->result);
} }
gd_free(net_req); gd_free(net_req);
} }
getdns_network_req* network_req_new(getdns_context_t context, getdns_network_req* network_req_new(getdns_dns_req* owner,
const char* name,
uint16_t request_type, uint16_t request_type,
uint16_t request_class,
struct getdns_dict* extensions) { struct getdns_dict* extensions) {
getdns_network_req *net_req = NULL;
ldns_pkt *pkt = NULL; getdns_context_t context = owner->context;
net_req = gd_malloc(sizeof(getdns_network_req)); getdns_network_req* net_req = gd_malloc(sizeof(getdns_network_req));
if (!net_req) { if (!net_req) {
return NULL; return NULL;
} }
net_req->ns = NULL; net_req->result = NULL;
net_req->pkt = NULL; net_req->next = NULL;
net_req->context = context;
net_req->request_type = request_type; net_req->request_type = request_type;
net_req->request_class = request_class;
/* create ldns packet */ net_req->unbound_id = -1;
pkt = create_new_pkt(context, name, request_type, extensions); net_req->state = NET_REQ_NOT_SENT;
if (!pkt) { net_req->owner = owner;
/* free up the req */
network_req_free(net_req); /* TODO: records and other extensions */
return NULL;
}
net_req->pkt = pkt;
return net_req; return net_req;
} }
@ -77,43 +74,86 @@ void dns_req_free(getdns_dns_req* req) {
if (!req) { if (!req) {
return; return;
} }
getdns_network_req *net_req = NULL;
getdns_context_t context = req->context; getdns_context_t context = req->context;
network_req_free(req->current_req);
/* free network requests */
net_req = req->first_req;
while (net_req) {
getdns_network_req *next = net_req->next;
network_req_free(net_req);
net_req = next;
}
/* free strduped name */
free(req->name);
gd_free(req); gd_free(req);
} }
/* create a new dns req to be submitted */ /* create a new dns req to be submitted */
getdns_dns_req* dns_req_new(getdns_context_t context, getdns_dns_req* dns_req_new(getdns_context_t context,
const char* name, struct ub_ctx* unbound,
uint16_t request_type, const char* name,
struct getdns_dict *extensions, uint16_t request_type,
getdns_transaction_t *transaction_id) { struct getdns_dict *extensions) {
getdns_dns_req *result = NULL; getdns_dns_req *result = NULL;
getdns_network_req *net_req = NULL; getdns_network_req *req = NULL;
getdns_return_t r;
uint32_t both = GETDNS_EXTENSION_FALSE;
result = gd_malloc(sizeof(getdns_dns_req)); result = gd_malloc(sizeof(getdns_dns_req));
if (result == NULL) { if (result == NULL) {
return NULL; return NULL;
} }
result->name = strdup(name);
result->context = context; result->context = context;
result->unbound = unbound;
result->current_req = NULL; result->current_req = NULL;
result->pending_cb = 0; result->first_req = NULL;
result->resolver_type = context->resolution_type; result->trans_id = ldns_get_random();
/* create the initial network request */ getdns_dict_copy(extensions, &result->extensions);
net_req = network_req_new(context, name, request_type,
extensions); /* will be set by caller */
if (!net_req) { result->user_pointer = NULL;
result->user_callback = NULL;
/* create the requests */
req = network_req_new(result,
request_type,
LDNS_RR_CLASS_IN,
extensions);
if (!req) {
dns_req_free(result); dns_req_free(result);
result = NULL; return NULL;
} }
result->trans_id = ldns_pkt_id(net_req->pkt); result->current_req = req;
if (transaction_id) { result->first_req = req;
*transaction_id = result->trans_id;
/* tack on A or AAAA if needed */
r = getdns_dict_get_int(extensions,
GETDNS_STR_EXTENSION_RETURN_BOTH_V4_AND_V6,
&both);
if (r == GETDNS_RETURN_GOOD &&
both == GETDNS_EXTENSION_TRUE &&
(request_type == GETDNS_RRTYPE_A || request_type == GETDNS_RRTYPE_AAAA)) {
uint16_t next_req_type = (request_type == GETDNS_RRTYPE_A) ? GETDNS_RRTYPE_AAAA : GETDNS_RRTYPE_A;
getdns_network_req* next_req =
network_req_new(result,
next_req_type,
LDNS_RR_CLASS_IN,
extensions);
if (!next_req) {
dns_req_free(result);
return NULL;
}
req->next = next_req;
} }
result->current_req = net_req;
net_req->owner = result;
return result; return result;
} }

View File

@ -91,13 +91,13 @@ main()
fprintf(stderr, "A bad domain name was used: %s. Exiting.", this_name); fprintf(stderr, "A bad domain name was used: %s. Exiting.", this_name);
return(GETDNS_RETURN_GENERIC_ERROR); return(GETDNS_RETURN_GENERIC_ERROR);
} }
dns_request_return = getdns_service(this_context, this_name, NULL, this_userarg, &this_transaction_id, // dns_request_return = getdns_service(this_context, this_name, NULL, this_userarg, &this_transaction_id,
this_callbackfn); // this_callbackfn);
if (dns_request_return == GETDNS_RETURN_BAD_DOMAIN_NAME) // if (dns_request_return == GETDNS_RETURN_BAD_DOMAIN_NAME)
{ // {
fprintf(stderr, "A bad domain name was used: %s. Exiting.", this_name); // fprintf(stderr, "A bad domain name was used: %s. Exiting.", this_name);
return(GETDNS_RETURN_GENERIC_ERROR); // return(GETDNS_RETURN_GENERIC_ERROR);
} // }
else else
{ {
/* Call the event loop */ /* Call the event loop */

View File

@ -36,76 +36,68 @@
/* declarations */ /* declarations */
struct getdns_dns_req; struct getdns_dns_req;
struct getdns_nameserver;
struct getdns_network_req; struct getdns_network_req;
struct ub_ctx;
typedef struct getdns_nameserver { typedef enum network_req_state_enum {
evutil_socket_t socket; /* a connected UDP socket */ NET_REQ_NOT_SENT,
struct sockaddr_storage address; NET_REQ_IN_FLIGHT,
ev_socklen_t addrlen; NET_REQ_FINISHED
} network_req_state;
int failed_times; /* number of times which we have given this server a chance */
int timedout; /* number of times in a row a request has timed out */
struct event* event;
/* when we next probe this server. */
/* Valid if state == 0 */
/* Outstanding probe request for this nameserver, if any */
struct getdns_dns_req *probe_request;
char state; /* zero if we think that this server is down */
char choked; /* true if we have an EAGAIN from this server's socket */
char write_waiting; /* true if we are waiting for EV_WRITE events */
/* getdns context */
getdns_context_t context;
/* Number of currently inflight requests: used
* to track when we should add/del the event. */
int requests_inflight;
} getdns_nameserver;
/* network request - state for a single network request and referenced /**
* by the the outbound_req * Request data for unbound
*/ **/
typedef struct getdns_network_req { typedef struct getdns_network_req {
ldns_pkt *pkt; /* the dns packet data */ /* the async_id from unbound */
uint16_t request_type; /* query type */ int unbound_id;
/* state var */
int reissue_count; network_req_state state;
int tx_count; /* the number of times that this packet has been sent */ /* owner request (contains name) */
struct getdns_dns_req* owner;
/* not owned */
getdns_nameserver *ns; /* the server which we sent to */ /* request type */
uint16_t request_type;
unsigned transmit_me :1; /* needs to be transmitted */
/* request class */
getdns_context_t context; uint16_t request_class;
struct getdns_dns_req *owner; /* result */
ldns_pkt* result;
/* next request to issue after this one */
struct getdns_network_req* next;
} getdns_network_req; } getdns_network_req;
/* outbound request - manages recursion and stub reqs */ /* dns request - manages a number of network requests and
* the initial data passed to getdns_general
*/
typedef struct getdns_dns_req { typedef struct getdns_dns_req {
/* name */
char *name;
/* current network request */
struct getdns_network_req *current_req; struct getdns_network_req *current_req;
/* first request in list */
struct getdns_network_req *first_req;
/* context that owns the request */
getdns_context_t context; getdns_context_t context;
uint16_t resolver_type; /* ub_ctx issuing the request */
struct ub_ctx* unbound;
/* request extensions */
getdns_dict *extensions;
/* callback data */ /* callback data */
getdns_callback_t user_callback; getdns_callback_t user_callback;
void *user_pointer; void *user_pointer;
getdns_transaction_t trans_id; /* the transaction id */ getdns_transaction_t trans_id; /* the transaction id */
int pending_cb; /* Waiting for its callback to be invoked; not
* owned by event base any more. */
/* search not supported.. yet */
} getdns_dns_req; } getdns_dns_req;
/* utility methods */ /* utility methods */
@ -113,29 +105,23 @@ typedef struct getdns_dns_req {
/* network request utilities */ /* network request utilities */
void network_req_free(getdns_network_req* net_req); void network_req_free(getdns_network_req* net_req);
getdns_network_req* network_req_new(getdns_context_t context, getdns_network_req* network_req_new(getdns_dns_req* owner,
const char* name,
uint16_t request_type, uint16_t request_type,
uint16_t request_class,
struct getdns_dict* extensions); struct getdns_dict* extensions);
/* dns request utils */ /* dns request utils */
getdns_dns_req* dns_req_new(getdns_context_t context, getdns_dns_req* dns_req_new(getdns_context_t context,
struct ub_ctx* unbound,
const char* name, const char* name,
uint16_t request_type, uint16_t request_type,
struct getdns_dict *extensions, struct getdns_dict *extensions);
getdns_transaction_t *transaction_id);
void dns_req_free(getdns_dns_req* req); void dns_req_free(getdns_dns_req* req);
/* nameserver utils */ /* cancel the request */
getdns_nameserver* nameserver_new_from_ip_dict(getdns_context_t context, getdns_return_t dns_req_cancel(getdns_dns_req* req);
getdns_dict* ip_dict);
void nameserver_free(getdns_nameserver* nameserver);
getdns_dict* nameserver_to_dict(getdns_nameserver* nameserver);
#endif #endif

View File

@ -29,6 +29,7 @@
*/ */
#include "util-internal.h" #include "util-internal.h"
#include "types-internal.h"
getdns_return_t getdns_dict_util_set_string(getdns_dict* dict, char* name, getdns_return_t getdns_dict_util_set_string(getdns_dict* dict, char* name,
const char* value) { const char* value) {
@ -58,12 +59,12 @@ getdns_return_t dict_to_sockaddr(getdns_dict* ns, struct sockaddr_storage* outpu
uint32_t port = 53; uint32_t port = 53;
memset(output, 0, sizeof(struct sockaddr_storage)); memset(output, 0, sizeof(struct sockaddr_storage));
output->ss_family = AF_UNSPEC; output->ss_family = AF_UNSPEC;
uint32_t prt = 0; uint32_t prt = 0;
if (getdns_dict_get_int(ns, GETDNS_STR_PORT, &prt) == GETDNS_RETURN_GOOD) { if (getdns_dict_get_int(ns, GETDNS_STR_PORT, &prt) == GETDNS_RETURN_GOOD) {
port = prt; port = prt;
} }
getdns_dict_util_get_string(ns, GETDNS_STR_ADDRESS_TYPE, &address_type); getdns_dict_util_get_string(ns, GETDNS_STR_ADDRESS_TYPE, &address_type);
getdns_dict_get_bindata(ns, GETDNS_STR_ADDRESS_DATA, &address_data); getdns_dict_get_bindata(ns, GETDNS_STR_ADDRESS_DATA, &address_data);
if (!address_type || !address_data) { if (!address_type || !address_data) {
@ -113,28 +114,8 @@ getdns_return_t sockaddr_to_dict(struct sockaddr_storage* address, getdns_dict**
return GETDNS_RETURN_GOOD; return GETDNS_RETURN_GOOD;
} }
/* TODO: flags */ getdns_dict *create_getdns_response(struct getdns_dns_req* completed_request) {
ldns_pkt *create_new_pkt(getdns_context_t context, ldns_pkt* pkt = completed_request->first_req->result;
const char* name,
uint16_t request_type,
struct getdns_dict* extensions) {
ldns_pkt *pkt = NULL;
ldns_rr_type type = (ldns_rr_type) request_type;
uint16_t flags = 0;
if (context->resolution_type == GETDNS_CONTEXT_STUB) {
flags |= LDNS_RD;
}
ldns_pkt_query_new_frm_str(&pkt, name,
type,
LDNS_RR_CLASS_IN, flags);
if (pkt) {
/* id */
ldns_pkt_set_id(pkt, ldns_get_random());
}
return pkt;
}
getdns_dict *create_getdns_response(ldns_pkt* pkt) {
char* data = ldns_pkt2str(pkt); char* data = ldns_pkt2str(pkt);
getdns_dict* result = getdns_dict_create(); getdns_dict* result = getdns_dict_create();
getdns_bindata bindata = { getdns_bindata bindata = {

View File

@ -35,18 +35,13 @@
#include <ldns/ldns.h> #include <ldns/ldns.h>
#include "context.h" #include "context.h"
struct getdns_dns_req;
/* convert an ip address dict to a sock storage */ /* convert an ip address dict to a sock storage */
getdns_return_t dict_to_sockaddr(getdns_dict* ns, struct sockaddr_storage* output); getdns_return_t dict_to_sockaddr(getdns_dict* ns, struct sockaddr_storage* output);
getdns_return_t sockaddr_to_dict(struct sockaddr_storage* sockaddr, getdns_dict** output); getdns_return_t sockaddr_to_dict(struct sockaddr_storage* sockaddr, getdns_dict** output);
/* create a dns packet for the given request type and extensions */ getdns_dict *create_getdns_response(struct getdns_dns_req* completed_request);
ldns_pkt *create_new_pkt(getdns_context_t context,
const char* name,
uint16_t request_type,
struct getdns_dict* extensions);
getdns_dict *create_getdns_response(ldns_pkt* pkt);
/* dict util */ /* dict util */
/* set a string as bindata */ /* set a string as bindata */