checkpoint
This commit is contained in:
@@ -46,10 +46,19 @@
|
||||
#include <dns/types.h>
|
||||
#include <dns/result.h>
|
||||
|
||||
#define DNS_REQUESTOPT_TCP 0x00000001U
|
||||
|
||||
ISC_LANG_BEGINDECLS
|
||||
|
||||
typedef struct dns_requestevent {
|
||||
ISC_EVENT_COMMON(struct dns_requestevent);
|
||||
isc_result_t result;
|
||||
dns_request_t *request;
|
||||
} dns_requestevent_t;
|
||||
|
||||
isc_result_t
|
||||
dns_requestmgr_create(isc_mem_t *mctx, isc_timermgr_t *timermgr,
|
||||
isc_socketmgr_t *socketmgr,
|
||||
dns_dispatch_t *dispatchv4, dns_dispatch_t *dispatchv6,
|
||||
dns_requestmgr_t **requestmgrp);
|
||||
/*
|
||||
@@ -61,6 +70,8 @@ dns_requestmgr_create(isc_mem_t *mctx, isc_timermgr_t *timermgr,
|
||||
*
|
||||
* 'timermgr' is a valid timer manager.
|
||||
*
|
||||
* 'socketmgr' is a valid socket manager.
|
||||
*
|
||||
* 'dispatchv4' is a valid dispatcher with an IPv4 UDP socket, or is NULL.
|
||||
*
|
||||
* 'dispatchv6' is a valid dispatcher with an IPv6 UDP socket, or is NULL.
|
||||
@@ -114,14 +125,14 @@ dns_requestmgr_shutdown(dns_requestmgr_t *requestmgr);
|
||||
*
|
||||
* Requires:
|
||||
*
|
||||
* 'res' is a valid requestmgr.
|
||||
* 'requestmgr' is a valid requestmgr.
|
||||
*/
|
||||
|
||||
void
|
||||
isc_requestmgr_attach(dns_requestmgr_t *source, dns_requestmgr_t **targetp);
|
||||
dns_requestmgr_attach(dns_requestmgr_t *source, dns_requestmgr_t **targetp);
|
||||
|
||||
void
|
||||
isc_requestmgr_detach(dns_requestmgr_t **requestmgrp);
|
||||
dns_requestmgr_detach(dns_requestmgr_t **requestmgrp);
|
||||
|
||||
|
||||
isc_result_t
|
||||
|
||||
+311
-140
@@ -33,14 +33,21 @@
|
||||
#include <dns/message.h>
|
||||
#include <dns/request.h>
|
||||
|
||||
#define REQUESTMGR_MAGIC 0x12341234U /* XXX MPA */
|
||||
#define REQUESTMGR_MAGIC 0x5271754dU /* RquM */
|
||||
#define VALID_REQUESTMGR(mgr) ((mgr) != NULL && \
|
||||
(mgr)->magic == REQUESTMGR_MAGIC)
|
||||
|
||||
#define REQUEST_MAGIC 0x12341235U /* XXX MPA */
|
||||
#define REQUEST_MAGIC 0x52717521U /* Rqu! */
|
||||
#define VALID_REQUEST(request) ((request) != NULL && \
|
||||
(request)->magic == REQUEST_MAGIC)
|
||||
|
||||
#if 1
|
||||
#define TRACE(x) printf(x)
|
||||
#else
|
||||
#define TRACE(x)
|
||||
#endif
|
||||
typedef ISC_LIST(dns_request_t) dns_requestlist_t;
|
||||
|
||||
struct dns_requestmgr {
|
||||
isc_int32_t magic;
|
||||
isc_mutex_t lock;
|
||||
@@ -49,58 +56,80 @@ struct dns_requestmgr {
|
||||
/* locked */
|
||||
isc_int32_t references;
|
||||
isc_timermgr_t *timermgr;
|
||||
isc_socketmgr_t *socketmgr;
|
||||
dns_dispatch_t *dispatchv4;
|
||||
dns_dispatch_t *dispatchv6;
|
||||
isc_boolean_t exiting;
|
||||
isc_eventlist_t whenshutdown;
|
||||
dns_requestlist_t requests;
|
||||
};
|
||||
|
||||
struct dns_request {
|
||||
isc_int32_t magic;
|
||||
isc_mem_t *mctx;
|
||||
isc_task_t *task;
|
||||
isc_taskaction_t action;
|
||||
void *action_arg;
|
||||
isc_int32_t flags;
|
||||
ISC_LINK(dns_request_t) link;
|
||||
isc_buffer_t *query;
|
||||
isc_buffer_t *answer;
|
||||
dns_requestevent_t *event;
|
||||
dns_dispatch_t *dispatch;
|
||||
dns_dispentry_t *dispentry;
|
||||
dns_messageid_t id;
|
||||
isc_timer_t *timer;
|
||||
dns_requestmgr_t *requestmgr;
|
||||
};
|
||||
|
||||
#define DNS_REQUEST_F_CONNECTING 0x0001
|
||||
#define DNS_REQUEST_F_CANCELED 0x0002
|
||||
#define DNS_REQUEST_CANCELED(r) \
|
||||
(((r)->flags & DNS_REQUEST_F_CANCELED) != 0)
|
||||
#define DNS_REQUEST_CONNECTING(r) \
|
||||
(((r)->flags & DNS_REQUEST_F_CONNECTING) != 0)
|
||||
|
||||
/***
|
||||
*** Forward
|
||||
***/
|
||||
|
||||
static void destroy(dns_requestmgr_t *requestmgr);
|
||||
static void mgr_destroy(dns_requestmgr_t *requestmgr);
|
||||
static void mgr_shutdown(dns_requestmgr_t *requestmgr);
|
||||
static void send_shutdown_events(dns_requestmgr_t *requestmgr);
|
||||
|
||||
static isc_result_t render(dns_message_t *message, isc_buffer_t **buffer,
|
||||
isc_mem_t *mctx);
|
||||
static void senddone(isc_task_t *task, isc_event_t *event);
|
||||
static void response(isc_task_t *task, isc_event_t *event);
|
||||
static void req_senddone(isc_task_t *task, isc_event_t *event);
|
||||
static void req_response(isc_task_t *task, isc_event_t *event);
|
||||
static void req_timeout(isc_task_t *task, isc_event_t *event);
|
||||
static void req_connected(isc_task_t *task, isc_event_t *event);
|
||||
static void req_sendevent(dns_request_t *request, isc_result_t result);
|
||||
static void req_cancel(dns_request_t *request);
|
||||
static void req_destroy(dns_request_t *request);
|
||||
|
||||
/***
|
||||
*** Public
|
||||
***/
|
||||
|
||||
isc_result_t
|
||||
dns_requestmgr_create(isc_mem_t *mctx, isc_timermgr_t *timermgr,
|
||||
dns_dispatch_t *dispatchv4, dns_dispatch_t *dispatchv6,
|
||||
dns_requestmgr_t **requestmgrp) {
|
||||
dns_requestmgr_create(isc_mem_t *mctx,
|
||||
isc_timermgr_t *timermgr,
|
||||
isc_socketmgr_t *socketmgr,
|
||||
dns_dispatch_t *dispatchv4,
|
||||
dns_dispatch_t *dispatchv6,
|
||||
dns_requestmgr_t **requestmgrp)
|
||||
{
|
||||
dns_requestmgr_t *requestmgr;
|
||||
isc_socket_t *socket;
|
||||
isc_result_t result;
|
||||
|
||||
REQUIRE(requestmgrp != NULL && *requestmgrp == NULL);
|
||||
REQUIRE(timermgr != NULL);
|
||||
REQUIRE(socketmgr != NULL);
|
||||
if (dispatchv4 != NULL) {
|
||||
socket = dns_dispatch_getsocket(dispatchv4);
|
||||
REQUIRE(isc_socket_gettype(socket) == PF_INET);
|
||||
REQUIRE(isc_socket_gettype(socket) == isc_sockettype_udp);
|
||||
}
|
||||
if (dispatchv6 != NULL) {
|
||||
socket = dns_dispatch_getsocket(dispatchv6);
|
||||
REQUIRE(isc_socket_gettype(socket) == PF_INET6);
|
||||
REQUIRE(isc_socket_gettype(socket) == isc_sockettype_udp);
|
||||
}
|
||||
REQUIRE(timermgr != NULL);
|
||||
|
||||
requestmgr = isc_mem_get(mctx, sizeof(*requestmgr));
|
||||
if (requestmgr == NULL)
|
||||
@@ -112,6 +141,7 @@ dns_requestmgr_create(isc_mem_t *mctx, isc_timermgr_t *timermgr,
|
||||
return (result);
|
||||
}
|
||||
requestmgr->timermgr = timermgr;
|
||||
requestmgr->socketmgr = socketmgr;
|
||||
requestmgr->dispatchv4 = NULL;
|
||||
if (dispatchv4 != NULL)
|
||||
dns_dispatch_attach(dispatchv4, &requestmgr->dispatchv4);
|
||||
@@ -121,6 +151,7 @@ dns_requestmgr_create(isc_mem_t *mctx, isc_timermgr_t *timermgr,
|
||||
requestmgr->mctx = mctx;
|
||||
requestmgr->references = 1; /* implict attach */
|
||||
ISC_LIST_INIT(requestmgr->whenshutdown);
|
||||
ISC_LIST_INIT(requestmgr->requests);
|
||||
requestmgr->exiting = ISC_FALSE;
|
||||
requestmgr->magic = REQUESTMGR_MAGIC;
|
||||
*requestmgrp = requestmgr;
|
||||
@@ -164,12 +195,25 @@ dns_requestmgr_shutdown(dns_requestmgr_t *requestmgr) {
|
||||
REQUIRE(VALID_REQUESTMGR(requestmgr));
|
||||
|
||||
LOCK(&requestmgr->lock);
|
||||
mgr_shutdown(requestmgr);
|
||||
UNLOCK(&requestmgr->lock);
|
||||
}
|
||||
|
||||
static void
|
||||
mgr_shutdown(dns_requestmgr_t *requestmgr) {
|
||||
dns_request_t *request;
|
||||
/*
|
||||
* Caller holds lock.
|
||||
*/
|
||||
if (!requestmgr->exiting) {
|
||||
requestmgr->exiting = ISC_TRUE;
|
||||
/* XXXMPA shutdown existing requests */
|
||||
for (request = ISC_LIST_HEAD(requestmgr->requests);
|
||||
request != NULL;
|
||||
request = ISC_LIST_NEXT(request, link)) {
|
||||
dns_request_cancel(request);
|
||||
}
|
||||
send_shutdown_events(requestmgr);
|
||||
}
|
||||
UNLOCK(&requestmgr->lock);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -202,13 +246,14 @@ dns_requestmgr_detach(dns_requestmgr_t **requestmgrp) {
|
||||
INSIST(requestmgr->references > 0);
|
||||
requestmgr->references--;
|
||||
if (requestmgr->references == 0) {
|
||||
INSIST(requestmgr->exiting);
|
||||
need_destroy = ISC_TRUE;
|
||||
INSIST(requestmgr->exiting &&
|
||||
ISC_LIST_HEAD(requestmgr->requests) == NULL);
|
||||
need_destroy = ISC_TRUE;
|
||||
}
|
||||
UNLOCK(&requestmgr->lock);
|
||||
|
||||
if (need_destroy)
|
||||
destroy(requestmgr);
|
||||
mgr_destroy(requestmgr);
|
||||
|
||||
*requestmgrp = NULL;
|
||||
}
|
||||
@@ -233,8 +278,9 @@ send_shutdown_events(dns_requestmgr_t *requestmgr) {
|
||||
}
|
||||
|
||||
static void
|
||||
destroy(dns_requestmgr_t *requestmgr) {
|
||||
mgr_destroy(dns_requestmgr_t *requestmgr) {
|
||||
REQUIRE(requestmgr->references == 0);
|
||||
|
||||
isc_mutex_destroy(&requestmgr->lock);
|
||||
if (requestmgr->dispatchv4 != NULL)
|
||||
dns_dispatch_detach(&requestmgr->dispatchv4);
|
||||
@@ -244,6 +290,17 @@ destroy(dns_requestmgr_t *requestmgr) {
|
||||
isc_mem_put(requestmgr->mctx, requestmgr, sizeof *requestmgr);
|
||||
}
|
||||
|
||||
static inline isc_result_t
|
||||
req_send(dns_request_t *request, isc_task_t *task, isc_sockaddr_t *address) {
|
||||
isc_region_t r;
|
||||
isc_socket_t *socket;
|
||||
|
||||
socket = dns_dispatch_getsocket(request->dispatch);
|
||||
isc_buffer_used(request->query, &r);
|
||||
return (isc_socket_sendto(socket, &r, task, req_senddone,
|
||||
request, address, NULL));
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
dns_request_create(dns_requestmgr_t *requestmgr, dns_message_t *message,
|
||||
isc_sockaddr_t *address, unsigned int options,
|
||||
@@ -252,15 +309,13 @@ dns_request_create(dns_requestmgr_t *requestmgr, dns_message_t *message,
|
||||
dns_request_t **requestp)
|
||||
{
|
||||
dns_request_t *request = NULL;
|
||||
dns_requestevent_t *event = NULL;
|
||||
dns_message_t *answer = NULL;
|
||||
dns_dispatch_t *dispatch = NULL;
|
||||
isc_buffer_t *query = NULL;
|
||||
isc_task_t *tclone = NULL;
|
||||
isc_socket_t *socket = NULL;
|
||||
isc_result_t result;
|
||||
isc_mem_t *mctx;
|
||||
isc_region_t r;
|
||||
isc_interval_t interval;
|
||||
dns_messageid_t id;
|
||||
isc_time_t expires;
|
||||
|
||||
REQUIRE(VALID_REQUESTMGR(requestmgr));
|
||||
REQUIRE(message != NULL);
|
||||
@@ -274,92 +329,146 @@ dns_request_create(dns_requestmgr_t *requestmgr, dns_message_t *message,
|
||||
|
||||
request = isc_mem_get(mctx, sizeof(*request));
|
||||
if (request == NULL) {
|
||||
result = ISC_R_NOMEMORY;
|
||||
goto cleanup;
|
||||
return (ISC_R_NOMEMORY);
|
||||
}
|
||||
/*
|
||||
* Zero structure.
|
||||
*/
|
||||
request->magic = 0;
|
||||
request->mctx = mctx;
|
||||
request->flags = 0;
|
||||
ISC_LINK_INIT(request, link);
|
||||
request->query = NULL;
|
||||
request->answer = NULL;
|
||||
request->event = NULL;
|
||||
request->dispatch = NULL;
|
||||
request->dispentry = NULL;
|
||||
request->timer = NULL;
|
||||
request->requestmgr = NULL;
|
||||
|
||||
event = (dns_requestevent_t *)
|
||||
dns_requestmgr_attach(requestmgr, &request->requestmgr);
|
||||
/*
|
||||
* Create timer now. We will set it below once.
|
||||
*/
|
||||
result = isc_timer_create(requestmgr->timermgr, isc_timertype_inactive,
|
||||
NULL, NULL, task, req_timeout, request,
|
||||
&request->timer);
|
||||
if (result != ISC_R_SUCCESS)
|
||||
goto cleanup;
|
||||
|
||||
request->event = (dns_requestevent_t *)
|
||||
isc_event_allocate(mctx, task, DNS_EVENT_REQUESTDONE,
|
||||
action, arg, sizeof (dns_requestevent_t));
|
||||
if (event == NULL) {
|
||||
if (request->event == NULL) {
|
||||
result = ISC_R_NOMEMORY;
|
||||
goto cleanup;
|
||||
}
|
||||
isc_task_attach(task, &tclone);
|
||||
event->sender = task;
|
||||
event->request = request;
|
||||
event->result = ISC_R_FAILURE;
|
||||
|
||||
result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &answer);
|
||||
if (result == DNS_R_USETCP) {
|
||||
options |= DNS_REQUESTOPT_USETCP;
|
||||
result = ISC_R_SUCCESS;
|
||||
}
|
||||
if (result != ISC_R_SUCCESS)
|
||||
goto cleanup;
|
||||
request->event->sender = task;
|
||||
request->event->request = request;
|
||||
request->event->result = ISC_R_FAILURE;
|
||||
|
||||
if ((options & DNS_REQUESTOPT_USETCP) != 0) {
|
||||
/* result = isc_socket_create(requestmgr->socketmgr,
|
||||
use_tcp:
|
||||
if ((options & DNS_REQUESTOPT_TCP) != 0) {
|
||||
result = isc_socket_create(requestmgr->socketmgr,
|
||||
isc_sockaddr_pf(address),
|
||||
isc_sockettype_tcp, &socket); */
|
||||
isc_sockettype_tcp, &socket);
|
||||
if (result != ISC_R_SUCCESS)
|
||||
goto cleanup;
|
||||
result = dns_dispatch_create(mctx, socket, task,
|
||||
4096, 2, 1, 1, 3, NULL,
|
||||
&dispatch);
|
||||
&request->dispatch);
|
||||
isc_socket_detach(&socket);
|
||||
if (result != ISC_R_SUCCESS)
|
||||
goto cleanup;
|
||||
} else {
|
||||
switch (isc_sockaddr_pf(address)) {
|
||||
case PF_INET:
|
||||
dns_dispatch_attach(requestmgr->dispatchv4, &dispatch);
|
||||
dns_dispatch_attach(requestmgr->dispatchv4,
|
||||
&request->dispatch);
|
||||
break;
|
||||
case PF_INET6:
|
||||
dns_dispatch_attach(requestmgr->dispatchv6, &dispatch);
|
||||
dns_dispatch_attach(requestmgr->dispatchv6,
|
||||
&request->dispatch);
|
||||
break;
|
||||
default:
|
||||
result = DNS_R_NOTIMPLEMENTED;
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
result = dns_dispatch_addresponse(dispatch, address, task,
|
||||
response, request, &request->id,
|
||||
socket = dns_dispatch_getsocket(request->dispatch);
|
||||
INSIST(socket != NULL);
|
||||
result = dns_dispatch_addresponse(request->dispatch, address, task,
|
||||
req_response, request, &id,
|
||||
&request->dispentry);
|
||||
if (result != ISC_R_SUCCESS)
|
||||
goto cleanup;
|
||||
|
||||
message->id = request->id;
|
||||
result = render(message, &query, mctx);
|
||||
if (result != ISC_R_SUCCESS)
|
||||
message->id = id;
|
||||
result = render(message, &request->query, mctx);
|
||||
if (result == DNS_R_USETCP &&
|
||||
(options & DNS_REQUESTOPT_TCP) == 0) {
|
||||
/*
|
||||
* Try again using TCP.
|
||||
*/
|
||||
dns_message_renderreset(message);
|
||||
dns_dispatch_removeresponse(request->dispatch,
|
||||
&request->dispentry, NULL);
|
||||
dns_dispatch_detach(&request->dispatch);
|
||||
socket = NULL;
|
||||
isc_buffer_free(&request->query);
|
||||
options |= DNS_REQUESTOPT_TCP;
|
||||
goto use_tcp;
|
||||
}
|
||||
if (result != ISC_R_SUCCESS && result != DNS_R_USETCP)
|
||||
goto cleanup;
|
||||
|
||||
request->mctx = mctx;
|
||||
request->query = query;
|
||||
request->answer = NULL;
|
||||
request->event = event;
|
||||
|
||||
request->magic = REQUEST_MAGIC;
|
||||
LOCK(&requestmgr->lock);
|
||||
ISC_LIST_APPEND(requestmgr->requests, request, link);
|
||||
UNLOCK(&requestmgr->lock);
|
||||
|
||||
isc_buffer_used(query, &r);
|
||||
result = isc_socket_sendto(socket, &r, task, senddone,
|
||||
request, address, NULL);
|
||||
|
||||
isc_interval_set(&interval, timeout, 0);
|
||||
result = isc_time_nowplusinterval(&expires, &interval);
|
||||
if (result != ISC_R_SUCCESS)
|
||||
goto cleanup;
|
||||
|
||||
result = isc_timer_reset(request->timer, isc_timertype_once,
|
||||
&expires, NULL, ISC_FALSE);
|
||||
if (result != ISC_R_SUCCESS)
|
||||
goto cleanup;
|
||||
|
||||
if ((options & DNS_REQUESTOPT_TCP) != 0) {
|
||||
result = isc_socket_connect(socket, address, task,
|
||||
req_connected, request);
|
||||
if (result != ISC_R_SUCCESS)
|
||||
goto cleanup;
|
||||
request->flags |= DNS_REQUEST_F_CONNECTING;
|
||||
} else {
|
||||
result = req_send(request, task, address);
|
||||
if (result != ISC_R_SUCCESS)
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
*requestp = request;
|
||||
return (ISC_R_SUCCESS);
|
||||
|
||||
cleanup:
|
||||
if (request != NULL)
|
||||
isc_mem_put(mctx, request, sizeof *request);
|
||||
if (event != NULL)
|
||||
isc_event_free((isc_event_t **)&event);
|
||||
if (query != NULL)
|
||||
isc_buffer_free(&query);
|
||||
if (answer != NULL)
|
||||
dns_message_destroy(&answer);
|
||||
if (request->dispentry != NULL)
|
||||
dns_dispatch_removeresponse(request->dispatch,
|
||||
&request->dispentry, NULL);
|
||||
if (request->dispatch != NULL)
|
||||
dns_dispatch_detach(&request->dispatch);
|
||||
if (request->event != NULL)
|
||||
isc_event_free((isc_event_t **)&request->event);
|
||||
if (request->query != NULL)
|
||||
isc_buffer_free(&request->query);
|
||||
if (request->timer != NULL)
|
||||
isc_timer_detach(&request->timer);
|
||||
if (tclone != NULL)
|
||||
isc_task_detach(&tclone);
|
||||
request->magic = 0;
|
||||
isc_mem_put(mctx, request, sizeof *request);
|
||||
return (result);
|
||||
}
|
||||
|
||||
@@ -372,6 +481,8 @@ render(dns_message_t *message, isc_buffer_t **bufferp, isc_mem_t *mctx) {
|
||||
|
||||
REQUIRE(bufferp != NULL && *bufferp == NULL);
|
||||
|
||||
TRACE("render\n");
|
||||
|
||||
/*
|
||||
* Create buffer able to hold largest possible message.
|
||||
*/
|
||||
@@ -426,6 +537,7 @@ render(dns_message_t *message, isc_buffer_t **bufferp, isc_mem_t *mctx) {
|
||||
return (ISC_R_SUCCESS);
|
||||
|
||||
cleanup:
|
||||
dns_message_renderreset(message);
|
||||
if (buf1 != NULL)
|
||||
isc_buffer_free(&buf1);
|
||||
if (buf2 != NULL)
|
||||
@@ -433,51 +545,17 @@ render(dns_message_t *message, isc_buffer_t **bufferp, isc_mem_t *mctx) {
|
||||
return (result);
|
||||
}
|
||||
|
||||
/*
|
||||
* Create and send a request.
|
||||
*
|
||||
* Notes:
|
||||
*
|
||||
* 'message' will be rendered and sent to 'address'. If the
|
||||
* DNS_REQUESTOPT_TCP option is set, TCP will be used. The request
|
||||
* will timeout after 'timeout' seconds.
|
||||
*
|
||||
* When the request completes, successfully, due to a timeout, or
|
||||
* because it was canceled, a completion event will be sent to 'task'.
|
||||
*
|
||||
* Requires:
|
||||
*
|
||||
* 'message' is a valid DNS message.
|
||||
*
|
||||
* 'address' is a valid sockaddr.
|
||||
*
|
||||
* 'timeout' > 0
|
||||
*
|
||||
* 'task' is a valid task.
|
||||
*
|
||||
* requestp != NULL && *requestp == NULL
|
||||
*/
|
||||
|
||||
isc_result_t
|
||||
dns_request_cancel(dns_request_t *request) {
|
||||
UNUSED(request);
|
||||
REQUIRE(VALID_REQUEST(request));
|
||||
|
||||
return (ISC_R_NOTIMPLEMENTED);
|
||||
if (!DNS_REQUEST_CANCELED(request)) {
|
||||
req_cancel(request);
|
||||
req_sendevent(request, ISC_R_CANCELED);
|
||||
}
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
/*
|
||||
* Cancel 'request'.
|
||||
*
|
||||
* Requires:
|
||||
*
|
||||
* 'request' is a valid request.
|
||||
*
|
||||
* Ensures:
|
||||
*
|
||||
* If the completion event for 'request' has not yet been sent, it
|
||||
* will be sent, and the result code will be ISC_R_CANCELED.
|
||||
*/
|
||||
|
||||
isc_result_t
|
||||
dns_request_getresponse(dns_request_t *request, dns_message_t *message) {
|
||||
REQUIRE(VALID_REQUEST(request));
|
||||
@@ -488,69 +566,162 @@ dns_request_getresponse(dns_request_t *request, dns_message_t *message) {
|
||||
|
||||
void
|
||||
dns_request_destroy(dns_request_t **requestp) {
|
||||
UNUSED(requestp);
|
||||
dns_request_t *request;
|
||||
|
||||
REQUIRE(requestp != NULL && VALID_REQUEST(*requestp));
|
||||
request = *requestp;
|
||||
LOCK(&request->requestmgr->lock);
|
||||
ISC_LIST_UNLINK(request->requestmgr->requests, request, link);
|
||||
UNLOCK(&request->requestmgr->lock);
|
||||
if (!DNS_REQUEST_CONNECTING(request))
|
||||
req_destroy(request);
|
||||
*requestp = NULL;
|
||||
}
|
||||
/*
|
||||
* Destroy 'request'.
|
||||
*
|
||||
* Requires:
|
||||
*
|
||||
* 'request' is a valid request for which the caller has received the
|
||||
* completion event.
|
||||
*
|
||||
* Ensures:
|
||||
*
|
||||
* *requestp == NULL
|
||||
*/
|
||||
|
||||
/***
|
||||
*** Private: request.
|
||||
***/
|
||||
|
||||
static void
|
||||
senddone(isc_task_t *task, isc_event_t *event) {
|
||||
req_connected(isc_task_t *task, isc_event_t *event) {
|
||||
isc_socketevent_t *sevent = (isc_socketevent_t *)event;
|
||||
isc_result_t result;
|
||||
dns_request_t *request = event->arg;
|
||||
|
||||
REQUIRE(event->type == ISC_SOCKEVENT_SENDDONE);
|
||||
REQUIRE(DNS_REQUEST_CONNECTING(request));
|
||||
|
||||
TRACE("req_connected\n");
|
||||
|
||||
request->flags &= ~DNS_REQUEST_F_CONNECTING;
|
||||
|
||||
if (DNS_REQUEST_CANCELED(request)) {
|
||||
req_destroy(request);
|
||||
} else {
|
||||
result = sevent->result;
|
||||
if (result == ISC_R_SUCCESS)
|
||||
result = req_send(request, task, NULL);
|
||||
|
||||
if (sevent->result != ISC_R_SUCCESS)
|
||||
dns_request_cancel(request);
|
||||
}
|
||||
isc_event_free(&event);
|
||||
}
|
||||
|
||||
static void
|
||||
req_senddone(isc_task_t *task, isc_event_t *event) {
|
||||
isc_socketevent_t *sevent = (isc_socketevent_t *)event;
|
||||
dns_request_t *request = event->arg;
|
||||
|
||||
REQUIRE(event->type == ISC_SOCKEVENT_SENDDONE);
|
||||
|
||||
TRACE("req_senddone\n");
|
||||
(void)task;
|
||||
|
||||
/*
|
||||
if (sevent->result != ISC_R_SUCCESS)
|
||||
fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
|
||||
*/
|
||||
dns_request_cancel(request);
|
||||
|
||||
isc_event_free(&event);
|
||||
}
|
||||
|
||||
static void
|
||||
response(isc_task_t *task, isc_event_t *event) {
|
||||
req_response(isc_task_t *task, isc_event_t *event) {
|
||||
isc_result_t result;
|
||||
dns_request_t *request = event->arg;
|
||||
dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event;
|
||||
dns_message_t *message;
|
||||
isc_region_t r;
|
||||
|
||||
REQUIRE(VALID_REQUEST(request));
|
||||
REQUIRE(event->type == DNS_EVENT_DISPATCH);
|
||||
|
||||
(void)task;
|
||||
if (devent->result != ISC_R_SUCCESS)
|
||||
UNUSED(task);
|
||||
|
||||
TRACE("req_response\n");
|
||||
|
||||
result = devent->result;
|
||||
if (result != ISC_R_SUCCESS)
|
||||
goto done;
|
||||
|
||||
/*
|
||||
* copy buffer
|
||||
* Copy buffer to request.
|
||||
*/
|
||||
|
||||
isc_buffer_used(&devent->buffer, &r);
|
||||
result = isc_buffer_allocate(request->mctx, &request->answer, r.length,
|
||||
ISC_BUFFERTYPE_BINARY);
|
||||
if (result != ISC_R_SUCCESS)
|
||||
goto done;
|
||||
result = isc_buffer_copyregion(request->answer, &r);
|
||||
if (result != ISC_R_SUCCESS)
|
||||
isc_buffer_free(&request->answer);
|
||||
done:
|
||||
return;
|
||||
/*
|
||||
* Cleanup.
|
||||
*/
|
||||
dns_dispatch_removeresponse(request->dispatch, &request->dispentry,
|
||||
&devent);
|
||||
req_cancel(request);
|
||||
/*
|
||||
* Send completion event.
|
||||
*/
|
||||
req_sendevent(request, result);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
timeout(isc_task_t *task, isc_event_t *event) {
|
||||
isc_result_t result;
|
||||
req_timeout(isc_task_t *task, isc_event_t *event) {
|
||||
dns_request_t *request = event->arg;
|
||||
|
||||
if (event->type == ISC_TIMEREVENT_LIFE) {
|
||||
} else {
|
||||
}
|
||||
|
||||
TRACE("req_timeout\n");
|
||||
UNUSED(task);
|
||||
req_cancel(request);
|
||||
req_sendevent(request, ISC_R_TIMEDOUT);
|
||||
isc_event_free(&event);
|
||||
}
|
||||
|
||||
static void
|
||||
req_sendevent(dns_request_t *request, isc_result_t result) {
|
||||
isc_task_t *task;
|
||||
|
||||
task = request->event->sender;
|
||||
request->event->sender = request;
|
||||
request->event->result = result;
|
||||
isc_task_sendanddetach(&task, (isc_event_t **)&request->event);
|
||||
}
|
||||
|
||||
static void
|
||||
req_destroy(dns_request_t *request) {
|
||||
|
||||
request->magic = 0;
|
||||
if (request->query != NULL)
|
||||
isc_buffer_free(&request->query);
|
||||
if (request->answer != NULL)
|
||||
isc_buffer_free(&request->answer);
|
||||
if (request->event != NULL)
|
||||
isc_event_free((isc_event_t **)&request->event);
|
||||
if (request->dispentry != NULL)
|
||||
dns_dispatch_removeresponse(request->dispatch,
|
||||
&request->dispentry, NULL);
|
||||
if (request->dispatch != NULL)
|
||||
dns_dispatch_detach(&request->dispatch);
|
||||
if (request->timer != NULL)
|
||||
isc_timer_detach(&request->timer);
|
||||
dns_requestmgr_detach(&request->requestmgr);
|
||||
isc_mem_put(request->mctx, request, sizeof(*request));
|
||||
}
|
||||
|
||||
static void
|
||||
req_cancel(dns_request_t *request) {
|
||||
isc_socket_t *socket;
|
||||
|
||||
request->flags |= DNS_REQUEST_F_CANCELED;
|
||||
|
||||
if (request->timer != NULL)
|
||||
isc_timer_detach(&request->timer);
|
||||
if (request->dispentry != NULL)
|
||||
dns_dispatch_removeresponse(request->dispatch,
|
||||
&request->dispentry, NULL);
|
||||
dns_dispatch_detach(&request->dispatch);
|
||||
if (DNS_REQUEST_CONNECTING(request)) {
|
||||
socket = dns_dispatch_getsocket(request->dispatch);
|
||||
isc_socket_cancel(socket, NULL, ISC_SOCKCANCEL_CONNECT);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user