More conventional function prototypes for servers

This commit is contained in:
Willem Toorop 2016-11-02 13:40:02 +01:00
parent bc9ebd5ae2
commit 4bf93de12b
4 changed files with 50 additions and 27 deletions

View File

@ -1487,7 +1487,7 @@ getdns_context_destroy(struct getdns_context *context)
cancel_outstanding_requests(context, 1); cancel_outstanding_requests(context, 1);
/* Destroy listening addresses */ /* Destroy listening addresses */
(void) getdns_context_set_listen_addresses(context, NULL, NULL); (void) getdns_context_set_listen_addresses(context, NULL, NULL, NULL);
/* This needs to be done before cleaning the extension, because there /* This needs to be done before cleaning the extension, because there
* might be an idle_timeout schedules, which will not get unscheduled * might be an idle_timeout schedules, which will not get unscheduled

View File

@ -1030,9 +1030,11 @@ getdns_context_config(getdns_context *context, const getdns_dict *config_dict);
* The user defined request handler that will be called on incoming requests. * The user defined request handler that will be called on incoming requests.
*/ */
typedef void (*getdns_request_handler_t)( typedef void (*getdns_request_handler_t)(
getdns_context *context, getdns_context *context,
getdns_dict *request, getdns_callback_type_t callback_type,
getdns_transaction_t request_id getdns_dict *request,
void *userarg,
getdns_transaction_t request_id
); );
/** /**
@ -1041,6 +1043,11 @@ typedef void (*getdns_request_handler_t)(
* *
* @param context The context managing the eventloop that needs to be run to * @param context The context managing the eventloop that needs to be run to
* start serving. * start serving.
* @param listen_addresses A list of address dicts or bindatas that will be
* listened on for DNS requests. Both UDP and TCP
* transports will be used.
* @param userarg A user defined argument that will be passed to the handler
* untouched.
* @param handler The user defined request handler that will be called with the * @param handler The user defined request handler that will be called with the
* request received in reply dict format. To reply to this request * request received in reply dict format. To reply to this request
* the function has to construct a response (or modify the request) * the function has to construct a response (or modify the request)
@ -1050,9 +1057,6 @@ typedef void (*getdns_request_handler_t)(
* not answered by the function, by not calling getdns_reply() this * not answered by the function, by not calling getdns_reply() this
* will cause a memory leak. The user most use getdns_reply() * will cause a memory leak. The user most use getdns_reply()
* with NULL as the response to not answer/cancel a request. * with NULL as the response to not answer/cancel a request.
* @param listen_addresses A list of address dicts or bindatas that will be
* listened on for DNS requests. Both UDP and TCP
* transports will be used.
* @return GETDNS_RETURN_GOOD on success or an error code on failure. * @return GETDNS_RETURN_GOOD on success or an error code on failure.
* On failure, the current set of listening addresses is left in place. * On failure, the current set of listening addresses is left in place.
* Also, if there is overlap in listening_addresses between the active set * Also, if there is overlap in listening_addresses between the active set
@ -1061,8 +1065,9 @@ typedef void (*getdns_request_handler_t)(
* DNS transactions will remain. * DNS transactions will remain.
*/ */
getdns_return_t getdns_return_t
getdns_context_set_listen_addresses(getdns_context *context, getdns_context_set_listen_addresses(
getdns_request_handler_t handler, const getdns_list *listen_addresses); getdns_context *context, const getdns_list *listen_addresses,
void *userarg, getdns_request_handler_t handler);
/** /**
* Answer the request associated with a request_id that is received by a * Answer the request associated with a request_id that is received by a
@ -1070,11 +1075,11 @@ getdns_context_set_listen_addresses(getdns_context *context,
* *
* @param context The context managing the eventloop that needs to be run to * @param context The context managing the eventloop that needs to be run to
* listen for and answer requests. * listen for and answer requests.
* @param request_id The identifier that links this response with the
* received request.
* @param reply The answer in getdns reply dict or response dict format. * @param reply The answer in getdns reply dict or response dict format.
* When NULL is given as reply, the request is not answered * When NULL is given as reply, the request is not answered
* but all associated state is deleted. * but all associated state is deleted.
* @param request_id The identifier that links this response with the
* received request.
* @return GETDNS_RETURN_GOOD on success or an error code on failure. * @return GETDNS_RETURN_GOOD on success or an error code on failure.
* On fatal failure (no retry strategy possible) the user still needs to * On fatal failure (no retry strategy possible) the user still needs to
* cancel the request by recalling getdns_reply() but with NULL as response, * cancel the request by recalling getdns_reply() but with NULL as response,
@ -1082,7 +1087,7 @@ getdns_context_set_listen_addresses(getdns_context *context,
*/ */
getdns_return_t getdns_return_t
getdns_reply(getdns_context *context, getdns_reply(getdns_context *context,
getdns_transaction_t request_id, getdns_dict *reply); getdns_dict *reply, getdns_transaction_t request_id);
/** @} /** @}

View File

@ -65,6 +65,7 @@ struct listener {
*/ */
struct listen_set { struct listen_set {
getdns_context *context; getdns_context *context;
void *userarg;
getdns_request_handler_t handler; getdns_request_handler_t handler;
_getdns_rbtree_t connections_set; _getdns_rbtree_t connections_set;
@ -231,7 +232,7 @@ _getdns_cancel_reply(getdns_context *context, connection *conn)
getdns_return_t getdns_return_t
getdns_reply( getdns_reply(
getdns_context *context, getdns_transaction_t request_id, getdns_dict *reply) getdns_context *context, getdns_dict *reply, getdns_transaction_t request_id)
{ {
/* TODO: Check request_id at context->outbound_requests */ /* TODO: Check request_id at context->outbound_requests */
connection *conn = (connection *)(intptr_t)request_id; connection *conn = (connection *)(intptr_t)request_id;
@ -400,9 +401,14 @@ static void tcp_read_cb(void *userarg)
else { else {
conn->to_answer++; conn->to_answer++;
/* TODO: wish list item:
* (void) getdns_dict_set_int64(
* request_dict, "request_id", intptr_t)conn);
*/
/* Call request handler */ /* Call request handler */
conn->super.l->set->handler( conn->super.l->set->handler(
conn->super.l->set->context, request_dict, (intptr_t)conn); conn->super.l->set->context, GETDNS_CALLBACK_COMPLETE,
request_dict, conn->super.l->set->userarg, (intptr_t)conn);
conn->read_pos = conn->read_buf; conn->read_pos = conn->read_buf;
conn->to_read = 2; conn->to_read = 2;
@ -618,8 +624,14 @@ static void udp_read_cb(void *userarg)
conn->prev_next = &l->connections; conn->prev_next = &l->connections;
l->connections = conn; l->connections = conn;
/* TODO: wish list item:
* (void) getdns_dict_set_int64(
* request_dict, "request_id", (intptr_t)conn);
*/
/* Call request handler */ /* Call request handler */
l->set->handler(l->set->context, request_dict, (intptr_t)conn); l->set->handler(l->set->context, GETDNS_CALLBACK_COMPLETE,
request_dict, l->set->userarg, (intptr_t)conn);
return; return;
} }
GETDNS_FREE(*mf, conn); GETDNS_FREE(*mf, conn);
@ -765,9 +777,9 @@ ptr_cmp(const void *a, const void *b)
return a == b ? 0 : (a < b ? -1 : 1); return a == b ? 0 : (a < b ? -1 : 1);
} }
getdns_return_t getdns_context_set_listen_addresses(getdns_context *context, getdns_return_t getdns_context_set_listen_addresses(
getdns_request_handler_t request_handler, getdns_context *context, const getdns_list *listen_addresses,
const getdns_list *listen_addresses) void *userarg, getdns_request_handler_t request_handler)
{ {
static const getdns_transport_list_t listen_transports[] static const getdns_transport_list_t listen_transports[]
= { GETDNS_TRANSPORT_UDP, GETDNS_TRANSPORT_TCP }; = { GETDNS_TRANSPORT_UDP, GETDNS_TRANSPORT_TCP };
@ -830,6 +842,7 @@ getdns_return_t getdns_context_set_listen_addresses(getdns_context *context,
new_set->context = context; new_set->context = context;
new_set->handler = request_handler; new_set->handler = request_handler;
new_set->userarg = userarg;
new_set->count = new_set_count * n_transports; new_set->count = new_set_count * n_transports;
(void) memset(new_set->items, 0, (void) memset(new_set->items, 0,
sizeof(listener) * new_set_count * n_transports); sizeof(listener) * new_set_count * n_transports);

View File

@ -1182,7 +1182,8 @@ getdns_return_t do_the_call(void)
getdns_eventloop *loop = NULL; getdns_eventloop *loop = NULL;
FILE *fp; FILE *fp;
static void incoming_request_handler(getdns_context *context, static void incoming_request_handler(getdns_context *context,
getdns_dict *request, getdns_transaction_t request_id); getdns_callback_type_t callback_type, getdns_dict *request,
void *userarg, getdns_transaction_t request_id);
void read_line_cb(void *userarg) void read_line_cb(void *userarg)
@ -1199,7 +1200,7 @@ void read_line_cb(void *userarg)
loop->vmt->clear(loop, read_line_ev); loop->vmt->clear(loop, read_line_ev);
if (listen_count) if (listen_count)
(void) getdns_context_set_listen_addresses( (void) getdns_context_set_listen_addresses(
context, NULL, NULL); context, NULL, NULL, NULL);
return; return;
} }
if (query_file) if (query_file)
@ -1229,7 +1230,7 @@ void read_line_cb(void *userarg)
r = parse_args(linec, linev); r = parse_args(linec, linev);
if (!r && touched_listen_list) { if (!r && touched_listen_list) {
r = getdns_context_set_listen_addresses( r = getdns_context_set_listen_addresses(
context, incoming_request_handler, listen_list); context, listen_list, NULL, incoming_request_handler);
} }
if ((r || (r = do_the_call())) && if ((r || (r = do_the_call())) &&
(r != CONTINUE && r != CONTINUE_ERROR)) (r != CONTINUE && r != CONTINUE_ERROR))
@ -1406,11 +1407,11 @@ static void request_cb(
else if (n == 0) else if (n == 0)
SERVFAIL("Recursion not available", 0, msg, &response); SERVFAIL("Recursion not available", 0, msg, &response);
if ((r = getdns_reply(context, msg->request_id, response))) { if ((r = getdns_reply(context, response, msg->request_id))) {
fprintf(stderr, "Could not reply: %s\n", fprintf(stderr, "Could not reply: %s\n",
getdns_get_errorstr_by_id(r)); getdns_get_errorstr_by_id(r));
/* Cancel reply */ /* Cancel reply */
(void) getdns_reply(context, msg->request_id, NULL); (void) getdns_reply(context, NULL, msg->request_id);
} }
if (msg) { if (msg) {
getdns_dict_destroy(msg->request); getdns_dict_destroy(msg->request);
@ -1421,7 +1422,8 @@ static void request_cb(
} }
static void incoming_request_handler(getdns_context *context, static void incoming_request_handler(getdns_context *context,
getdns_dict *request, getdns_transaction_t request_id) getdns_callback_type_t callback_type, getdns_dict *request,
void *userarg, getdns_transaction_t request_id)
{ {
getdns_bindata *qname; getdns_bindata *qname;
char *qname_str = NULL; char *qname_str = NULL;
@ -1440,6 +1442,9 @@ static void incoming_request_handler(getdns_context *context,
getdns_dict *rr; getdns_dict *rr;
uint32_t rr_type; uint32_t rr_type;
(void)callback_type;
(void)userarg;
if (!query_extensions_spc && if (!query_extensions_spc &&
!(query_extensions_spc = getdns_dict_create())) !(query_extensions_spc = getdns_dict_create()))
fprintf(stderr, "Could not create query extensions space\n"); fprintf(stderr, "Could not create query extensions space\n");
@ -1567,11 +1572,11 @@ error:
free(request_str); free(request_str);
} while(0); } while(0);
#endif #endif
if ((r = getdns_reply(context, request_id, response))) { if ((r = getdns_reply(context, response, request_id))) {
fprintf(stderr, "Could not reply: %s\n", fprintf(stderr, "Could not reply: %s\n",
getdns_get_errorstr_by_id(r)); getdns_get_errorstr_by_id(r));
/* Cancel reply */ /* Cancel reply */
getdns_reply(context, request_id, NULL); getdns_reply(context, NULL, request_id);
} }
if (msg) { if (msg) {
if (msg->request) if (msg->request)
@ -1648,7 +1653,7 @@ main(int argc, char **argv)
assert(loop); assert(loop);
} }
if (listen_count && (r = getdns_context_set_listen_addresses( if (listen_count && (r = getdns_context_set_listen_addresses(
context, incoming_request_handler, listen_list))) { context, listen_list, NULL, incoming_request_handler))) {
perror("error: Could not bind on given addresses"); perror("error: Could not bind on given addresses");
goto done_destroy_context; goto done_destroy_context;
} }