mirror of https://github.com/getdnsapi/getdns.git
Convert test programs to not use libevent.
This commit is contained in:
parent
b4eb5d1ea5
commit
8e144d5648
|
@ -76,7 +76,7 @@ tests_list: tests_list.o testmessages.o
|
||||||
$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ tests_list.o testmessages.o
|
$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ tests_list.o testmessages.o
|
||||||
|
|
||||||
tests_stub_async: tests_stub_async.o testmessages.o
|
tests_stub_async: tests_stub_async.o testmessages.o
|
||||||
$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) -lgetdns_ext_event $(LDLIBS) -o $@ tests_stub_async.o testmessages.o
|
$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ tests_stub_async.o testmessages.o
|
||||||
|
|
||||||
tests_stub_sync: tests_stub_sync.o
|
tests_stub_sync: tests_stub_sync.o
|
||||||
$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ tests_stub_sync.o
|
$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ tests_stub_sync.o
|
||||||
|
@ -97,7 +97,7 @@ check_getdns_ev: check_getdns.o check_getdns_common.o check_getdns_context_set_t
|
||||||
$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) -lpthread -lgetdns_ext_ev $(EXTENSION_LIBEV_LDFLAGS) $(EXTENSION_LIBEV_EXT_LIBS) $(LDLIBS) -o $@ check_getdns.o check_getdns_common.o check_getdns_context_set_timeout.o check_getdns_libev.o
|
$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) -lpthread -lgetdns_ext_ev $(EXTENSION_LIBEV_LDFLAGS) $(EXTENSION_LIBEV_EXT_LIBS) $(LDLIBS) -o $@ check_getdns.o check_getdns_common.o check_getdns_context_set_timeout.o check_getdns_libev.o
|
||||||
|
|
||||||
tests_dnssec: tests_dnssec.o testmessages.o
|
tests_dnssec: tests_dnssec.o testmessages.o
|
||||||
$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) -lgetdns_ext_event $(LDLIBS) -o $@ tests_dnssec.o testmessages.o
|
$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ tests_dnssec.o testmessages.o
|
||||||
|
|
||||||
|
|
||||||
test: all
|
test: all
|
||||||
|
|
|
@ -34,24 +34,19 @@
|
||||||
|
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#ifdef HAVE_EVENT2_EVENT_H
|
|
||||||
# include <event2/event.h>
|
|
||||||
#else
|
|
||||||
# include <event.h>
|
|
||||||
#endif
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "testmessages.h"
|
#include "testmessages.h"
|
||||||
#include <getdns/getdns.h>
|
#include <getdns/getdns.h>
|
||||||
#include <getdns/getdns_ext_libevent.h>
|
#include <getdns/getdns_extra.h>
|
||||||
|
|
||||||
/* 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
|
||||||
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)
|
||||||
{
|
{
|
||||||
struct getdns_list *validation_chain;
|
struct getdns_list *validation_chain;
|
||||||
struct getdns_list *trust_anchors;
|
struct getdns_list *trust_anchors;
|
||||||
|
@ -65,77 +60,76 @@ callbackfn(struct getdns_context *context,
|
||||||
do {
|
do {
|
||||||
if (callback_type == GETDNS_CALLBACK_CANCEL) {
|
if (callback_type == GETDNS_CALLBACK_CANCEL) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"The callback with ID %llu was cancelled.\n",
|
"The callback with ID %llu was cancelled.\n",
|
||||||
(long long unsigned int)transaction_id);
|
(long long unsigned int)transaction_id);
|
||||||
break;
|
break;
|
||||||
} else if (callback_type != GETDNS_CALLBACK_COMPLETE) {
|
} else if (callback_type != GETDNS_CALLBACK_COMPLETE) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"The callback got a callback_type of %d.\n",
|
"The callback got a callback_type of %d.\n",
|
||||||
callback_type);
|
callback_type);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
r = getdns_dict_get_list(response,
|
r = getdns_dict_get_list(response,
|
||||||
"validation_chain", &validation_chain);
|
"validation_chain", &validation_chain);
|
||||||
if (r != GETDNS_RETURN_GOOD) {
|
if (r != GETDNS_RETURN_GOOD) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Could not get \"validation_chain\" from response:"
|
"Could not get \"validation_chain\" from response:"
|
||||||
" %d\n", r);
|
" %d\n", r);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
r = getdns_dict_get_list(response, "replies_tree", &replies_tree);
|
r = getdns_dict_get_list(response, "replies_tree", &replies_tree);
|
||||||
if (r != GETDNS_RETURN_GOOD) {
|
if (r != GETDNS_RETURN_GOOD) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Could not get \"replies_tree\" from response:"
|
"Could not get \"replies_tree\" from response:"
|
||||||
" %d\n", r);
|
" %d\n", r);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
r = getdns_list_get_length(replies_tree, &replies_tree_length);
|
r = getdns_list_get_length(replies_tree, &replies_tree_length);
|
||||||
if (r != GETDNS_RETURN_GOOD) {
|
if (r != GETDNS_RETURN_GOOD) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Could not get length of the replies_tree:"
|
"Could not get length of the replies_tree:"
|
||||||
" %d\n", r);
|
" %d\n", r);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
trust_anchors = getdns_root_trust_anchor(NULL);
|
trust_anchors = getdns_root_trust_anchor(NULL);
|
||||||
if (! trust_anchors) {
|
if (! trust_anchors) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"No root trust anchor present:"
|
"No root trust anchor present:"
|
||||||
" %d\n", r);
|
" %d\n", r);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
for (i = 0; i < replies_tree_length; i++) {
|
for (i = 0; i < replies_tree_length; i++) {
|
||||||
r = getdns_list_get_dict(replies_tree, i, &reply);
|
r = getdns_list_get_dict(replies_tree, i, &reply);
|
||||||
if (r != GETDNS_RETURN_GOOD) {
|
if (r != GETDNS_RETURN_GOOD) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Could not get \"reply\" from replies_tree:"
|
"Could not get \"reply\" from replies_tree:"
|
||||||
" %d\n", r);
|
" %d\n", r);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
r = getdns_dict_get_list(reply, "answer", &answer);
|
r = getdns_dict_get_list(reply, "answer", &answer);
|
||||||
if (r != GETDNS_RETURN_GOOD) {
|
if (r != GETDNS_RETURN_GOOD) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Could not get \"answer\" from reply:"
|
"Could not get \"answer\" from reply:"
|
||||||
" %d\n", r);
|
" %d\n", r);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
r = getdns_list_get_length(answer, &answer_length);
|
r = getdns_list_get_length(answer, &answer_length);
|
||||||
if (r != GETDNS_RETURN_GOOD) {
|
if (r != GETDNS_RETURN_GOOD) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Could not get length of answer list:"
|
"Could not get length of answer list:"
|
||||||
" %d\n", r);
|
" %d\n", r);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (answer_length == 0)
|
if (answer_length == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
r = getdns_validate_dnssec(answer,
|
r = getdns_validate_dnssec(answer,
|
||||||
validation_chain, trust_anchors);
|
validation_chain, trust_anchors);
|
||||||
printf("getdns_validate_dnssec returned: %d\n", r);
|
printf("getdns_validate_dnssec returned: %d\n", r);
|
||||||
}
|
}
|
||||||
getdns_list_destroy(trust_anchors);
|
getdns_list_destroy(trust_anchors);
|
||||||
} while (0);
|
} while (0);
|
||||||
getdns_dict_destroy(response);
|
getdns_dict_destroy(response);
|
||||||
(void) event_base_loopexit((struct event_base *)userarg, NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -144,9 +138,9 @@ main(int argc, char** argv)
|
||||||
const char *name = argc > 1 ? argv[1] : "www.example.com";
|
const char *name = argc > 1 ? argv[1] : "www.example.com";
|
||||||
struct getdns_context *context;
|
struct getdns_context *context;
|
||||||
struct getdns_dict *extensions;
|
struct getdns_dict *extensions;
|
||||||
struct event_base *event_base = NULL;
|
|
||||||
getdns_transaction_t transaction_id = 0;
|
getdns_transaction_t transaction_id = 0;
|
||||||
getdns_return_t r;
|
getdns_return_t r;
|
||||||
|
struct timeval tv;
|
||||||
|
|
||||||
r = getdns_context_create(&context, 1);
|
r = getdns_context_create(&context, 1);
|
||||||
if (r != GETDNS_RETURN_GOOD) {
|
if (r != GETDNS_RETURN_GOOD) {
|
||||||
|
@ -168,28 +162,29 @@ main(int argc, char** argv)
|
||||||
GETDNS_EXTENSION_TRUE);
|
GETDNS_EXTENSION_TRUE);
|
||||||
if (r != GETDNS_RETURN_GOOD) {
|
if (r != GETDNS_RETURN_GOOD) {
|
||||||
fprintf(stderr, "Could not set extension "
|
fprintf(stderr, "Could not set extension "
|
||||||
"\"dnssec_return_validation_chain\": %d\n", r);
|
"\"dnssec_return_validation_chain\": %d\n", r);
|
||||||
goto done_destroy_extensions;
|
goto done_destroy_extensions;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Create an event base and put it in the context */
|
|
||||||
event_base = event_base_new();
|
|
||||||
if (event_base == NULL) {
|
|
||||||
fprintf(stderr, "Trying to create the event base failed.");
|
|
||||||
r = GETDNS_RETURN_GENERIC_ERROR;
|
|
||||||
goto done_destroy_extensions;
|
|
||||||
}
|
|
||||||
(void) getdns_extension_set_libevent_base(context, event_base);
|
|
||||||
|
|
||||||
/* Make the call */
|
/* Make the call */
|
||||||
r = getdns_address(context, name, extensions, event_base,
|
r = getdns_address(context, name, extensions, NULL,
|
||||||
&transaction_id, callbackfn);
|
&transaction_id, callbackfn);
|
||||||
if (r == GETDNS_RETURN_BAD_DOMAIN_NAME) {
|
if (r == GETDNS_RETURN_BAD_DOMAIN_NAME) {
|
||||||
fprintf(stderr, "Bad domain name: %s.", name);
|
fprintf(stderr, "Bad domain name: %s.", name);
|
||||||
goto done_destroy_extensions;
|
goto done_destroy_extensions;
|
||||||
}
|
}
|
||||||
/* Call the event loop */
|
/* Call the event loop */
|
||||||
event_base_dispatch(event_base);
|
while (getdns_context_get_num_pending_requests(context, &tv) > 0) {
|
||||||
|
int fd = getdns_context_fd(context);
|
||||||
|
fd_set read_fds;
|
||||||
|
FD_ZERO(&read_fds);
|
||||||
|
FD_SET(fd, &read_fds);
|
||||||
|
select(fd + 1, &read_fds, NULL, NULL, &tv);
|
||||||
|
if (getdns_context_process_async(context) != GETDNS_RETURN_GOOD) {
|
||||||
|
// context destroyed
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Clean up */
|
/* Clean up */
|
||||||
done_destroy_extensions:
|
done_destroy_extensions:
|
||||||
|
@ -197,12 +192,5 @@ done_destroy_extensions:
|
||||||
done_destroy_context:
|
done_destroy_context:
|
||||||
getdns_context_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;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,25 +33,20 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#ifdef HAVE_EVENT2_EVENT_H
|
|
||||||
# include <event2/event.h>
|
|
||||||
#else
|
|
||||||
# include <event.h>
|
|
||||||
#endif
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "testmessages.h"
|
#include "testmessages.h"
|
||||||
#include <getdns/getdns.h>
|
#include <getdns/getdns.h>
|
||||||
#include <getdns/getdns_ext_libevent.h>
|
#include <getdns/getdns_extra.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
|
||||||
/* 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 *this_context,
|
this_callbackfn(struct getdns_context *this_context,
|
||||||
getdns_callback_type_t this_callback_type,
|
getdns_callback_type_t this_callback_type,
|
||||||
struct getdns_dict *this_response,
|
struct getdns_dict *this_response,
|
||||||
void *this_userarg, getdns_transaction_t this_transaction_id)
|
void *this_userarg, getdns_transaction_t this_transaction_id)
|
||||||
{
|
{
|
||||||
if (this_callback_type == GETDNS_CALLBACK_COMPLETE) { /* This is a callback with data */
|
if (this_callback_type == GETDNS_CALLBACK_COMPLETE) { /* This is a callback with data */
|
||||||
char *res = getdns_pretty_print_dict(this_response);
|
char *res = getdns_pretty_print_dict(this_response);
|
||||||
|
@ -60,12 +55,12 @@ this_callbackfn(struct getdns_context *this_context,
|
||||||
|
|
||||||
} else if (this_callback_type == GETDNS_CALLBACK_CANCEL)
|
} else if (this_callback_type == GETDNS_CALLBACK_CANCEL)
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"The callback with ID %llu was cancelled. Exiting.",
|
"The callback with ID %llu was cancelled. Exiting.",
|
||||||
(unsigned long long)this_transaction_id);
|
(unsigned long long)this_transaction_id);
|
||||||
else
|
else
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"The callback got a callback_type of %d. Exiting.",
|
"The callback got a callback_type of %d. Exiting.",
|
||||||
this_callback_type);
|
this_callback_type);
|
||||||
getdns_dict_destroy(this_response);
|
getdns_dict_destroy(this_response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,29 +70,16 @@ main(int argc, char** argv)
|
||||||
/* Create the DNS context for this call */
|
/* Create the DNS context for this call */
|
||||||
struct getdns_context *this_context = NULL;
|
struct getdns_context *this_context = NULL;
|
||||||
getdns_return_t context_create_return =
|
getdns_return_t context_create_return =
|
||||||
getdns_context_create(&this_context, 1);
|
getdns_context_create(&this_context, 1);
|
||||||
if (context_create_return != GETDNS_RETURN_GOOD) {
|
if (context_create_return != GETDNS_RETURN_GOOD) {
|
||||||
fprintf(stderr, "Trying to create the context failed: %d",
|
fprintf(stderr, "Trying to create the context failed: %d",
|
||||||
context_create_return);
|
context_create_return);
|
||||||
return (GETDNS_RETURN_GENERIC_ERROR);
|
return (GETDNS_RETURN_GENERIC_ERROR);
|
||||||
}
|
}
|
||||||
getdns_context_set_resolution_type(this_context, GETDNS_RESOLUTION_STUB);
|
getdns_context_set_resolution_type(this_context, GETDNS_RESOLUTION_STUB);
|
||||||
|
|
||||||
getdns_context_set_timeout(this_context, 5000);
|
getdns_context_set_timeout(this_context, 5000);
|
||||||
/* 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 using the unknown function name */
|
||||||
struct event_base *this_event_base;
|
|
||||||
this_event_base = event_base_new();
|
|
||||||
if (this_event_base == NULL) {
|
|
||||||
fprintf(stderr, "Trying to create the event base failed.");
|
|
||||||
getdns_context_destroy(this_context);
|
|
||||||
return (GETDNS_RETURN_GENERIC_ERROR);
|
|
||||||
}
|
|
||||||
if (getdns_extension_set_libevent_base(this_context,
|
|
||||||
this_event_base) != GETDNS_RETURN_GOOD) {
|
|
||||||
fprintf(stderr, "Setting event base failed.");
|
|
||||||
getdns_context_destroy(this_context);
|
|
||||||
return (GETDNS_RETURN_GENERIC_ERROR);
|
|
||||||
}
|
|
||||||
/* Set up the getdns call */
|
/* Set up the getdns call */
|
||||||
const char *this_name = argc > 1 ? argv[1] : "getdnsapi.net";
|
const char *this_name = argc > 1 ? argv[1] : "getdnsapi.net";
|
||||||
char *this_userarg = "somestring"; // Could add things here to help identify this call
|
char *this_userarg = "somestring"; // Could add things here to help identify this call
|
||||||
|
@ -105,35 +87,31 @@ main(int argc, char** argv)
|
||||||
|
|
||||||
/* Make the call */
|
/* Make the call */
|
||||||
getdns_return_t dns_request_return =
|
getdns_return_t dns_request_return =
|
||||||
getdns_general(this_context, this_name, GETDNS_RRTYPE_A,
|
getdns_general(this_context, this_name, GETDNS_RRTYPE_A,
|
||||||
NULL, this_userarg, &this_transaction_id, this_callbackfn);
|
NULL, this_userarg, &this_transaction_id, 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.",
|
fprintf(stderr, "A bad domain name was used: %s. Exiting.",
|
||||||
this_name);
|
this_name);
|
||||||
getdns_context_destroy(this_context);
|
getdns_context_destroy(this_context);
|
||||||
event_base_free(this_event_base);
|
|
||||||
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,
|
|
||||||
// this_callbackfn);
|
|
||||||
// if (dns_request_return == GETDNS_RETURN_BAD_DOMAIN_NAME)
|
|
||||||
// {
|
|
||||||
// fprintf(stderr, "A bad domain name was used: %s. Exiting.", this_name);
|
|
||||||
// return(GETDNS_RETURN_GENERIC_ERROR);
|
|
||||||
// }
|
|
||||||
else {
|
else {
|
||||||
/* Call the event loop */
|
/* Call the event loop */
|
||||||
event_base_loop(this_event_base, EVLOOP_ONCE);
|
struct timeval tv;
|
||||||
while (getdns_context_get_num_pending_requests(this_context, NULL) > 0) {
|
while (getdns_context_get_num_pending_requests(this_context, &tv) > 0) {
|
||||||
event_base_loop(this_event_base, EVLOOP_ONCE);
|
int fd = getdns_context_fd(this_context);
|
||||||
}
|
fd_set read_fds;
|
||||||
// TODO: check the return value above
|
FD_ZERO(&read_fds);
|
||||||
|
FD_SET(fd, &read_fds);
|
||||||
|
select(fd + 1, &read_fds, NULL, NULL, &tv);
|
||||||
|
if (getdns_context_process_async(this_context) != GETDNS_RETURN_GOOD) {
|
||||||
|
// context destroyed
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/* Clean up */
|
/* Clean up */
|
||||||
getdns_context_destroy(this_context);
|
getdns_context_destroy(this_context);
|
||||||
/* the event base can only be free'd after the context has removed
|
|
||||||
* all of its events from it */
|
|
||||||
event_base_free(this_event_base);
|
|
||||||
/* Assuming we get here, leave gracefully */
|
/* Assuming we get here, leave gracefully */
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
} /* main */
|
} /* main */
|
||||||
|
|
Loading…
Reference in New Issue