mirror of https://github.com/getdnsapi/getdns.git
Adding A and AAAA handling for get_address
This commit is contained in:
parent
c53c00ee2b
commit
da8dad5913
|
@ -31,7 +31,7 @@ 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 $?
|
||||||
|
|
||||||
|
|
|
@ -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>
|
||||||
|
@ -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,9 +214,12 @@ 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();
|
||||||
|
|
||||||
|
@ -258,6 +282,7 @@ getdns_context_destroy(
|
||||||
|
|
||||||
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;
|
||||||
|
@ -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 */
|
||||||
|
@ -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 */
|
||||||
|
|
|
@ -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);
|
||||||
|
@ -72,11 +74,16 @@ struct getdns_context_t {
|
||||||
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
|
||||||
|
|
||||||
|
|
172
src/general.c
172
src/general.c
|
@ -63,100 +63,142 @@
|
||||||
#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,
|
|
||||||
NULL,
|
|
||||||
req->userarg,
|
|
||||||
req->transaction_id);
|
|
||||||
} else {
|
|
||||||
getdns_dict* response = create_getdns_response(pkt);
|
|
||||||
ldns_pkt_free(pkt);
|
|
||||||
req->callback(req->context,
|
|
||||||
GETDNS_CALLBACK_COMPLETE,
|
GETDNS_CALLBACK_COMPLETE,
|
||||||
response,
|
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 {
|
||||||
|
/* parse */
|
||||||
|
ldns_status r = ldns_buffer2pkt_wire(&(netreq->result), result);
|
||||||
|
if (r != LDNS_STATUS_OK) {
|
||||||
|
handle_network_request_error(netreq, r);
|
||||||
|
} else {
|
||||||
|
/* is this the last request */
|
||||||
|
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 */
|
||||||
|
getdns_context_clear_outbound_request(req);
|
||||||
|
dns_req_free(req);
|
||||||
return GETDNS_RETURN_GENERIC_ERROR;
|
return GETDNS_RETURN_GENERIC_ERROR;
|
||||||
}
|
}
|
||||||
return GETDNS_RETURN_GOOD;
|
return GETDNS_RETURN_GOOD;
|
||||||
|
@ -166,16 +208,13 @@ 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) {
|
||||||
|
@ -202,15 +241,12 @@ getdns_general(
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
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();
|
||||||
|
|
|
@ -39,36 +39,33 @@ 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;
|
|
||||||
|
|
||||||
/* create ldns packet */
|
net_req->request_type = request_type;
|
||||||
pkt = create_new_pkt(context, name, request_type, extensions);
|
net_req->request_class = request_class;
|
||||||
if (!pkt) {
|
net_req->unbound_id = -1;
|
||||||
/* free up the req */
|
net_req->state = NET_REQ_NOT_SENT;
|
||||||
network_req_free(net_req);
|
net_req->owner = owner;
|
||||||
return NULL;
|
|
||||||
}
|
/* TODO: records and other extensions */
|
||||||
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,
|
||||||
|
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) {
|
|
||||||
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,
|
|
||||||
|
/* will be set by caller */
|
||||||
|
result->user_pointer = NULL;
|
||||||
|
result->user_callback = NULL;
|
||||||
|
|
||||||
|
/* create the requests */
|
||||||
|
req = network_req_new(result,
|
||||||
|
request_type,
|
||||||
|
LDNS_RR_CLASS_IN,
|
||||||
extensions);
|
extensions);
|
||||||
if (!net_req) {
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
result->current_req = net_req;
|
/* tack on A or AAAA if needed */
|
||||||
net_req->owner = result;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 */
|
||||||
|
|
|
@ -36,63 +36,61 @@
|
||||||
|
|
||||||
/* 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 */
|
* Request data for unbound
|
||||||
|
**/
|
||||||
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
|
|
||||||
*/
|
|
||||||
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) */
|
||||||
|
|
||||||
/* not owned */
|
|
||||||
getdns_nameserver *ns; /* the server which we sent to */
|
|
||||||
|
|
||||||
unsigned transmit_me :1; /* needs to be transmitted */
|
|
||||||
|
|
||||||
getdns_context_t context;
|
|
||||||
|
|
||||||
struct getdns_dns_req* owner;
|
struct getdns_dns_req* owner;
|
||||||
|
|
||||||
|
/* request type */
|
||||||
|
uint16_t request_type;
|
||||||
|
|
||||||
|
/* request class */
|
||||||
|
uint16_t request_class;
|
||||||
|
|
||||||
|
/* 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;
|
||||||
|
@ -100,12 +98,6 @@ typedef struct getdns_dns_req {
|
||||||
|
|
||||||
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
|
||||||
|
|
|
@ -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) {
|
||||||
|
@ -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 = {
|
||||||
|
|
|
@ -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 */
|
||||||
|
|
Loading…
Reference in New Issue