Cleanup programming style in tests_dnssec

This commit is contained in:
Willem Toorop 2014-02-10 17:00:08 +01:00
parent b9bc90a986
commit 2a6f73ed3c
1 changed files with 55 additions and 60 deletions

View File

@ -129,7 +129,7 @@ getdns_return_t create_root_trustanchor_list(struct getdns_list **tas)
/* Set up the callback function, which will also do the processing of the results */ /* Set up the callback function, which will also do the processing of the results */
void void
this_callbackfn(struct getdns_context *context, callbackfn(struct getdns_context *context,
getdns_callback_type_t callback_type, getdns_callback_type_t callback_type,
struct getdns_dict *response, void *userarg, struct getdns_dict *response, void *userarg,
getdns_transaction_t transaction_id) getdns_transaction_t transaction_id)
@ -222,72 +222,67 @@ this_callbackfn(struct getdns_context *context,
int int
main(int argc, char** argv) main(int argc, char** argv)
{ {
/* Create the DNS context for this call */ struct getdns_context *context = NULL;
struct getdns_context *this_context = NULL; getdns_return_t r = getdns_context_create(&context, 1);
getdns_return_t context_create_return = if (r != GETDNS_RETURN_GOOD) {
getdns_context_create(&this_context, 1); fprintf(stderr, "Create context failed: %d", r);
if (context_create_return != GETDNS_RETURN_GOOD) { return r;
fprintf(stderr, "Trying to create the context failed: %d",
context_create_return);
return (GETDNS_RETURN_GENERIC_ERROR);
} }
getdns_context_set_timeout(this_context, 5000); r = getdns_context_set_timeout(context, 5000);
struct getdns_dict * this_extensions = getdns_dict_create(); if (r != GETDNS_RETURN_GOOD) {
getdns_return_t this_ret = getdns_dict_set_int(this_extensions, fprintf(stderr, "Set timeout failed: %d", r);
"dnssec_return_validation_chain", GETDNS_EXTENSION_TRUE); goto done_destroy_context;
if (this_ret != GETDNS_RETURN_GOOD) { }
fprintf(stderr, "Setting extension " struct getdns_dict *extensions = getdns_dict_create();
"\"dnssec_return_validation_chain\" failed: %d\n", this_ret); if (! extensions) {
getdns_dict_destroy(this_extensions); fprintf(stderr, "Could not create extensions dict\n");
getdns_context_destroy(this_context); r = GETDNS_RETURN_MEMORY_ERROR;
return (GETDNS_RETURN_GENERIC_ERROR); goto done_destroy_context;
}
r = getdns_dict_set_int(extensions, "dnssec_return_validation_chain",
GETDNS_EXTENSION_TRUE);
if (r != GETDNS_RETURN_GOOD) {
fprintf(stderr, "Could not set extension "
"\"dnssec_return_validation_chain\": %d\n", r);
goto done_destroy_extensions;
} }
/* Create an event base and put it in the context using the unknown function name */ /* Create an event base and put it in the context */
struct event_base *this_event_base; struct event_base *event_base = NULL;
this_event_base = event_base_new(); event_base = event_base_new();
if (this_event_base == NULL) { if (event_base == NULL) {
fprintf(stderr, "Trying to create the event base failed."); fprintf(stderr, "Trying to create the event base failed.");
getdns_dict_destroy(this_extensions); r = GETDNS_RETURN_GENERIC_ERROR;
getdns_context_destroy(this_context); goto done_destroy_extensions;
return (GETDNS_RETURN_GENERIC_ERROR);
} }
(void) getdns_extension_set_libevent_base(this_context, (void) getdns_extension_set_libevent_base(context, event_base);
this_event_base);
/* Set up the getdns call */ /* Set up the getdns call */
const char *this_name = argc > 1 ? argv[1] : "www.example.com"; const char *name = argc > 1 ? argv[1] : "www.example.com";
getdns_transaction_t this_transaction_id = 0; getdns_transaction_t transaction_id = 0;
/* Make the call */ /* Make the call */
getdns_return_t dns_request_return = getdns_address( r = getdns_address(context, name, extensions, event_base,
this_context, this_name, this_extensions, &transaction_id, callbackfn);
this_event_base, &this_transaction_id, this_callbackfn); if (r == GETDNS_RETURN_BAD_DOMAIN_NAME) {
if (dns_request_return == GETDNS_RETURN_BAD_DOMAIN_NAME) { fprintf(stderr, "Bad domain name: %s.", name);
fprintf(stderr, "A bad domain name was used: %s. Exiting.", goto done_destroy_extensions;
this_name);
getdns_dict_destroy(this_extensions);
getdns_context_destroy(this_context);
event_base_free(this_event_base);
return (GETDNS_RETURN_GENERIC_ERROR);
} }
else { /* Call the event loop */
/* Call the event loop */ event_base_dispatch(event_base);
event_base_dispatch(this_event_base);
/*
do event_base_loop(this_event_base, EVLOOP_ONCE);
while (0 < getdns_context_get_num_pending_requests(
this_context, NULL));
*/
}
/* Clean up */
getdns_dict_destroy(this_extensions);
getdns_context_destroy(this_context);
/* we have to destroy the event base after the context, because
* the context has to re-register its sockets from the eventbase,
* who has to communicate this to the system event-mechanism. */
event_base_free(this_event_base);
/* Assuming we get here, leave gracefully */
exit(EXIT_SUCCESS);
} /* main */
/* example-simple-answers.c */ /* Clean up */
done_destroy_extensions:
getdns_dict_destroy(extensions);
done_destroy_context:
getdns_context_destroy(context);
/* Event base must be destroyed after the context, because
* the context has to re-register its sockets from the eventbase,
* who has to communicate this to the system event-mechanism.
*/
if (event_base)
event_base_free(event_base);
return r;
}