From 8a8accea42034893645557a286d5603da18685fb Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Wed, 11 Dec 2013 16:21:49 +0100 Subject: [PATCH] Copied examples from spec and fixed everything ;) Well.. fixed the examples so they work and free memory and fixed our code to return correct dict format with "just_address_answers" Now tests are probably broken, and also the example-reverse needs some attention --- src/context.c | 1 + src/dict.c | 6 + src/example/Makefile.in | 21 +- src/example/example-all-functions.c | 339 +++++++++++++++++++++++++++ src/example/example-reverse.c | 109 +++++++++ src/example/example-simple-answers.c | 108 +++++++++ src/example/example-synchronous.c | 81 +++++++ src/example/example-tree.c | 151 ++++++++++++ src/example/example_all_functions.c | 197 ---------------- src/example/example_simple_answers.c | 160 ------------- src/example/example_synchronous.c | 118 ---------- src/example/example_tree.c | 163 ------------- src/example/getdns_core_only.h | 1 + src/example/getdns_libevent.h | 6 + src/test/Makefile.in | 5 +- src/util-internal.c | 48 +++- 16 files changed, 852 insertions(+), 662 deletions(-) create mode 100644 src/example/example-all-functions.c create mode 100644 src/example/example-reverse.c create mode 100644 src/example/example-simple-answers.c create mode 100644 src/example/example-synchronous.c create mode 100644 src/example/example-tree.c delete mode 100644 src/example/example_all_functions.c delete mode 100644 src/example/example_simple_answers.c delete mode 100644 src/example/example_synchronous.c delete mode 100644 src/example/example_tree.c create mode 100644 src/example/getdns_core_only.h create mode 100644 src/example/getdns_libevent.h diff --git a/src/context.c b/src/context.c index 7f91ef8c..d0313bec 100644 --- a/src/context.c +++ b/src/context.c @@ -969,6 +969,7 @@ getdns_return_t getdns_cancel_callback(struct getdns_context *context, getdns_transaction_t transaction_id) { + RETURN_IF_NULL(context, GETDNS_RETURN_BAD_CONTEXT); return getdns_context_cancel_request(context, transaction_id, 1); } /* getdns_cancel_callback */ diff --git a/src/dict.c b/src/dict.c index 96523a73..59789010 100644 --- a/src/dict.c +++ b/src/dict.c @@ -716,4 +716,10 @@ getdns_pretty_print_dict(struct getdns_dict *dict) return ret; } /* getdns_pretty_print_dict */ +getdns_return_t +getdns_dict_remove_name(struct getdns_dict *this_dict, char *name) +{ + return GETDNS_RETURN_GENERIC_ERROR; +} + /* dict.c */ diff --git a/src/example/Makefile.in b/src/example/Makefile.in index ce6837d6..009c39c2 100644 --- a/src/example/Makefile.in +++ b/src/example/Makefile.in @@ -19,7 +19,7 @@ CC=gcc CFLAGS=@CFLAGS@ -Wall -I$(srcdir)/ -I$(srcdir)/../ -I/usr/local/include -std=c99 LDFLAGS=@LDFLAGS@ -L. -L.. -L/usr/local/lib LDLIBS=-lgetdns @LIBS@ -PROGRAMS=example_simple_answers example_tree example_all_functions example_synchronous +PROGRAMS=example-all-functions example-simple-answers example-tree example-synchronous example-reverse .SUFFIXES: .c .o .a .lo .h @@ -30,17 +30,20 @@ default: all all: $(PROGRAMS) -example_simple_answers: example_simple_answers.o - $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ example_simple_answers.o +example-all-functions: example-all-functions.o + $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ example-all-functions.o -example_tree: example_tree.o - $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ example_tree.o +example-simple-answers: example-simple-answers.o + $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ example-simple-answers.o -example_all_functions: example_all_functions.o - $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ example_all_functions.o +example-tree: example-tree.o + $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ example-tree.o -example_synchronous: example_synchronous.o - $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ example_synchronous.o +example-synchronous: example-synchronous.o + $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ example-synchronous.o + +example-reverse: example-reverse.o + $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ example-reverse.o clean: rm -f *.o $(PROGRAMS) diff --git a/src/example/example-all-functions.c b/src/example/example-all-functions.c new file mode 100644 index 00000000..5adab816 --- /dev/null +++ b/src/example/example-all-functions.c @@ -0,0 +1,339 @@ +#include +#include +#include +#include +#include +#include + +#define UNUSED_PARAM(x) ((void)(x)) + +/* The return values */ +getdns_return_t retregular; +char * retcharstar; + +/* The args */ +int boolarg; +char * charstararg; +getdns_callback_t callbackarg; +uint16_t regulararg; +uint16_t *regularptrarg; +getdns_transaction_t txidarg; +getdns_transaction_t * txidptrarg; + +getdns_data_type * datatypeptrarg; +struct getdns_bindata ** bindataptrarg; +struct getdns_dict * dictarg; +struct getdns_bindata * bindataarg; +struct getdns_list * listarg; +struct getdns_dict ** dictptrarg; +struct getdns_list ** listptrarg; + +size_t sizetarg; +size_t * sizetptrarg; +struct getdns_context *contextarg = NULL; +uint8_t uint8arg; +uint16_t uint16arg; +uint32_t uint32arg; +uint8_t * uint8ptrarg; +uint16_t * uint16ptrarg; +uint32_t * uint32ptrarg; +void * arrayarg; +void * userarg; +void * allocfunctionarg(size_t foo) {UNUSED_PARAM(foo); return NULL; } +void * reallocfunctionarg(void* foo, size_t bar) + {UNUSED_PARAM(foo); UNUSED_PARAM(bar); return NULL; } +void deallocfunctionarg(void* foo) {UNUSED_PARAM(foo);} +void * extendedallocfunctionarg(void* userarg, size_t foo) + {UNUSED_PARAM(userarg); UNUSED_PARAM(foo); return NULL; } +void * extendedreallocfunctionarg(void* userarg, void* foo, size_t bar) + {UNUSED_PARAM(userarg); UNUSED_PARAM(foo); UNUSED_PARAM(bar); return NULL; } +void extendeddeallocfunctionarg(void* userarg, void* foo) + {UNUSED_PARAM(userarg); UNUSED_PARAM(foo);} +void setcallbackfunctionarg(struct getdns_context *foo1, uint16_t foo2) + {UNUSED_PARAM(foo1);UNUSED_PARAM(foo2);} + +int main() +{ + +retregular = getdns_general( + contextarg, + charstararg, + uint16arg, + dictarg, + arrayarg, + txidptrarg, + callbackarg +); + +retregular = getdns_address( + contextarg, + charstararg, + dictarg, + arrayarg, + txidptrarg, + callbackarg +); + +retregular = getdns_hostname( + contextarg, + dictarg, + dictarg, + arrayarg, + txidptrarg, + callbackarg +); + +retregular = getdns_service( + contextarg, + charstararg, + dictarg, + arrayarg, + txidptrarg, + callbackarg +); + +retregular = getdns_context_create( + &contextarg, + boolarg +); + +retregular = getdns_context_create_with_memory_functions( + &contextarg, + boolarg, + allocfunctionarg, + reallocfunctionarg, + deallocfunctionarg +); + +retregular = getdns_context_create_with_extended_memory_functions( + &contextarg, + boolarg, + userarg, + extendedallocfunctionarg, + extendedreallocfunctionarg, + extendeddeallocfunctionarg +); + +getdns_context_destroy( + contextarg +); + +retregular = getdns_cancel_callback( + contextarg, + txidarg +); + +retregular = getdns_general_sync( + contextarg, + charstararg, + uint16arg, + dictarg, + &dictarg +); + +retregular = getdns_address_sync( + contextarg, + charstararg, + dictarg, + &dictarg +); + +retregular = getdns_hostname_sync( + contextarg, + dictarg, + dictarg, + &dictarg +); + +retregular = getdns_service_sync( + contextarg, + charstararg, + dictarg, + &dictarg +); + +retregular = getdns_list_get_length(listarg, sizetptrarg); +retregular = getdns_list_get_data_type(listarg, sizetarg, datatypeptrarg); +retregular = getdns_list_get_dict(listarg, sizetarg, dictptrarg); +retregular = getdns_list_get_list(listarg, sizetarg, listptrarg); +retregular = getdns_list_get_bindata(listarg, sizetarg, bindataptrarg); +retregular = getdns_list_get_int(listarg, sizetarg, uint32ptrarg); + +retregular = getdns_dict_get_names(dictarg, listptrarg); +retregular = getdns_dict_get_data_type(dictarg, charstararg, datatypeptrarg); +retregular = getdns_dict_get_dict(dictarg, charstararg, dictptrarg); +retregular = getdns_dict_get_list(dictarg, charstararg, listptrarg); +retregular = getdns_dict_get_bindata(dictarg, charstararg, bindataptrarg); +retregular = getdns_dict_get_int(dictarg, charstararg, uint32ptrarg); + +listarg = getdns_list_create(); +listarg = getdns_list_create_with_context(contextarg); +listarg = getdns_list_create_with_memory_functions( + allocfunctionarg, + reallocfunctionarg, + deallocfunctionarg +); +listarg = getdns_list_create_with_extended_memory_functions( + userarg, + extendedallocfunctionarg, + extendedreallocfunctionarg, + extendeddeallocfunctionarg +); +getdns_list_destroy(listarg); +retregular = getdns_list_set_dict(listarg, sizetarg, dictarg); +retregular = getdns_list_set_list(listarg, sizetarg, listarg); +retregular = getdns_list_set_bindata(listarg, sizetarg, bindataarg); +retregular = getdns_list_set_int(listarg, sizetarg, uint32arg); + +dictarg = getdns_dict_create(); +dictarg = getdns_dict_create_with_context(contextarg); +dictarg = getdns_dict_create_with_memory_functions( + allocfunctionarg, + reallocfunctionarg, + deallocfunctionarg +); +dictarg = getdns_dict_create_with_extended_memory_functions( + userarg, + extendedallocfunctionarg, + extendedreallocfunctionarg, + extendeddeallocfunctionarg +); +getdns_dict_destroy(dictarg); +retregular = getdns_dict_set_dict(dictarg, charstararg, dictarg); +retregular = getdns_dict_set_list(dictarg, charstararg, listarg); +retregular = getdns_dict_set_bindata(dictarg, charstararg, bindataarg); +retregular = getdns_dict_set_int(dictarg, charstararg, uint32arg); +retregular = getdns_dict_remove_name(dictarg, charstararg); + +retcharstar = getdns_convert_fqdn_to_dns_name( + charstararg +); + +retcharstar = getdns_convert_dns_name_to_fqdn( + charstararg +); + +retcharstar = getdns_convert_ulabel_to_alabel( + charstararg +); + +retcharstar = getdns_convert_alabel_to_ulabel( + charstararg +); + +retregular = getdns_validate_dnssec( + bindataarg, + listarg, + listarg +); + +retcharstar = getdns_pretty_print_dict( + dictarg +); + +retcharstar = getdns_display_ip_address( + bindataarg +); + +retregular = getdns_context_set_context_update_callback( + contextarg, + setcallbackfunctionarg +); + +retregular = getdns_context_set_resolution_type( + contextarg, + regulararg +); + +retregular = getdns_context_set_namespaces( + contextarg, + sizetarg, + regularptrarg +); + +retregular = getdns_context_set_dns_transport( + contextarg, + regulararg +); + +retregular = getdns_context_set_limit_outstanding_queries( + contextarg, + uint16arg +); + +retregular = getdns_context_set_timeout( + contextarg, + uint16arg +); + +retregular = getdns_context_set_follow_redirects( + contextarg, + regulararg +); + +retregular = getdns_context_set_dns_root_servers( + contextarg, + listarg +); + +retregular = getdns_context_set_append_name( + contextarg, + regulararg +); + +retregular = getdns_context_set_suffix( + contextarg, + listarg +); + +retregular = getdns_context_set_dnssec_trust_anchors( + contextarg, + listarg +); + +retregular = getdns_context_set_dnssec_allowed_skew( + contextarg, + uint16arg +); + +retregular = getdns_context_set_stub_resolution( + contextarg, + listarg +); + +retregular = getdns_context_set_edns_maximum_udp_payload_size( + contextarg, + uint16arg +); + +retregular = getdns_context_set_edns_extended_rcode( + contextarg, + uint8arg +); + +retregular = getdns_context_set_edns_version( + contextarg, + uint8arg +); + +retregular = getdns_context_set_edns_do_bit( + contextarg, + uint8arg +); + +retregular = getdns_context_set_memory_functions( + contextarg, + allocfunctionarg, + reallocfunctionarg, + deallocfunctionarg +); + +retregular = getdns_context_set_extended_memory_functions( + contextarg, + userarg, + extendedallocfunctionarg, + extendedreallocfunctionarg, + extendeddeallocfunctionarg +); + +return(0); } /* End of main() */ diff --git a/src/example/example-reverse.c b/src/example/example-reverse.c new file mode 100644 index 00000000..c55424f8 --- /dev/null +++ b/src/example/example-reverse.c @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include +#include + +#define UNUSED_PARAM(x) ((void)(x)) + +/* Set up the callback function, which will also do the processing of the results */ +void this_callbackfn(struct getdns_context *this_context, + uint16_t this_callback_type, + struct getdns_dict *this_response, + void *this_userarg, + getdns_transaction_t this_transaction_id) +{ + UNUSED_PARAM(this_userarg); /* Not looking at the userarg for this example */ + UNUSED_PARAM(this_context); /* Not looking at the context for this example */ + getdns_return_t this_ret; /* Holder for all function returns */ + if (this_callback_type == GETDNS_CALLBACK_COMPLETE) /* This is a callback with data */ + { + /* Be sure the search returned something */ + uint32_t * this_error = NULL; + this_ret = getdns_dict_get_int(this_response, "status", this_error); // Ignore any error + if (*this_error != GETDNS_RESPSTATUS_GOOD) // If the search didn't return "good" + { + fprintf(stderr, "The search had no results, and a return value of %d. Exiting.", *this_error); + return; + } + struct getdns_list * just_the_addresses_ptr; + this_ret = getdns_dict_get_list(this_response, "just_address_answers", &just_the_addresses_ptr); + if (this_ret != GETDNS_RETURN_GOOD) // This check is really not needed, but prevents a compiler error under "pedantic" + { + fprintf(stderr, "Trying to get the answers failed: %d", this_ret); + return; + } + size_t * num_addresses_ptr = NULL; + this_ret = getdns_list_get_length(just_the_addresses_ptr, num_addresses_ptr); // Ignore any error + /* Go through each record */ + for ( size_t rec_count = 0; rec_count <= *num_addresses_ptr; ++rec_count ) + { + struct getdns_dict * this_address; + this_ret = getdns_list_get_dict(just_the_addresses_ptr, rec_count, &this_address); // Ignore any error + /* Just print the address */ + struct getdns_bindata * this_address_data; + this_ret = getdns_dict_get_bindata(this_address, "address_data", &this_address_data); // Ignore any error + printf("The address is %s", getdns_display_ip_address(this_address_data)); + } + } + else if (this_callback_type == GETDNS_CALLBACK_CANCEL) + fprintf(stderr, "The callback with ID %"PRIu64" was cancelled. Exiting.", this_transaction_id); + else + fprintf(stderr, "The callback got a callback_type of %d. Exiting.", this_callback_type); +} + +int main() +{ + /* Create the DNS context for this call */ + struct getdns_context *this_context = NULL; + getdns_return_t context_create_return = getdns_context_create(&this_context, 1); + if (context_create_return != GETDNS_RETURN_GOOD) + { + fprintf(stderr, "Trying to create the context failed: %d", context_create_return); + return(GETDNS_RETURN_GENERIC_ERROR); + } + /* 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."); + return(GETDNS_RETURN_GENERIC_ERROR); + } + (void)getdns_extension_set_libevent_base(this_context, this_event_base); + /* Set up the getdns call */ + struct getdns_dict * this_addr_to_look_up = getdns_dict_create(); + // TODO: check the return value above + struct getdns_bindata this_type = { 5, (void *)"IPv4" }; + getdns_return_t this_ret = getdns_dict_set_bindata(this_addr_to_look_up, "address_type", &this_type); + UNUSED_PARAM(this_ret); + struct getdns_bindata this_ipv4_addr = { 4, (void *)"\x08\x08\x08\x08" }; + this_ret = getdns_dict_set_bindata(this_addr_to_look_up, "address_data", &this_ipv4_addr); + char* this_userarg = "somestring"; // Could add things here to help identify this call + getdns_transaction_t this_transaction_id = 0; + + /* Make the call */ + getdns_return_t dns_request_return = getdns_hostname(this_context, this_addr_to_look_up, + NULL, this_userarg, &this_transaction_id, this_callbackfn); + if (dns_request_return == GETDNS_RETURN_BAD_DOMAIN_NAME) + { + char *ip_address_str = getdns_display_ip_address(&this_ipv4_addr); + + fprintf(stderr, "A bad IP address was used: %s. Exiting.\n", ip_address_str); + free(ip_address_str); + return(GETDNS_RETURN_GENERIC_ERROR); + } + else + { + /* Call the event loop */ + int dispatch_return = event_base_dispatch(this_event_base); + UNUSED_PARAM(dispatch_return); + // TODO: check the return value above + } + /* Clean up */ + getdns_dict_destroy(this_addr_to_look_up); + getdns_context_destroy(this_context); + /* Assuming we get here, leave gracefully */ + exit(EXIT_SUCCESS); +} diff --git a/src/example/example-simple-answers.c b/src/example/example-simple-answers.c new file mode 100644 index 00000000..e9d75608 --- /dev/null +++ b/src/example/example-simple-answers.c @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include + +#define UNUSED_PARAM(x) ((void)(x)) + +/* Set up the callback function, which will also do the processing of the results */ +void this_callbackfn(struct getdns_context *this_context, + uint16_t this_callback_type, + struct getdns_dict *this_response, + void *this_userarg, + getdns_transaction_t this_transaction_id) +{ + UNUSED_PARAM(this_userarg); /* Not looking at the userarg for this example */ + UNUSED_PARAM(this_context); /* Not looking at the context for this example */ + getdns_return_t this_ret; /* Holder for all function returns */ + if (this_callback_type == GETDNS_CALLBACK_COMPLETE) /* This is a callback with data */ + { + /* Be sure the search returned something */ + uint32_t this_error; + this_ret = getdns_dict_get_int(this_response, "status", &this_error); // Ignore any error + if (this_error != GETDNS_RESPSTATUS_GOOD) // If the search didn't return "good" + { + fprintf(stderr, "The search had no results, and a return value of %d. Exiting.\n", this_error); + getdns_dict_destroy(this_response); + return; + } + struct getdns_list * just_the_addresses_ptr; + this_ret = getdns_dict_get_list(this_response, "just_address_answers", &just_the_addresses_ptr); + if (this_ret != GETDNS_RETURN_GOOD) // This check is really not needed, but prevents a compiler error under "pedantic" + { + fprintf(stderr, "Trying to get the answers failed: %d\n", this_ret); + getdns_dict_destroy(this_response); + return; + } + size_t num_addresses; + this_ret = getdns_list_get_length(just_the_addresses_ptr, &num_addresses); // Ignore any error + /* Go through each record */ + for ( size_t rec_count = 0; rec_count < num_addresses; ++rec_count ) + { + struct getdns_dict * this_address; + this_ret = getdns_list_get_dict(just_the_addresses_ptr, rec_count, &this_address); // Ignore any error + /* Just print the address */ + struct getdns_bindata * this_address_data; + this_ret = getdns_dict_get_bindata(this_address, "address_data", &this_address_data); // Ignore any error + char *this_address_str = getdns_display_ip_address(this_address_data); + printf("The address is %s\n", this_address_str); + free(this_address_str); + } + } + else if (this_callback_type == GETDNS_CALLBACK_CANCEL) + fprintf(stderr, "The callback with ID %"PRIu64" was cancelled. Exiting.\n", this_transaction_id); + else + fprintf(stderr, "The callback got a callback_type of %d. Exiting.\n", this_callback_type); + getdns_dict_destroy(this_response); +} + +int main() +{ + /* Create the DNS context for this call */ + struct getdns_context *this_context = NULL; + getdns_return_t context_create_return = getdns_context_create(&this_context, 1); + if (context_create_return != GETDNS_RETURN_GOOD) + { + fprintf(stderr, "Trying to create the context failed: %d", context_create_return); + return(GETDNS_RETURN_GENERIC_ERROR); + } + /* 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.\n"); + getdns_context_destroy(this_context); + return(GETDNS_RETURN_GENERIC_ERROR); + } + (void)getdns_extension_set_libevent_base(this_context, this_event_base); + /* Set up the getdns call */ + const char * this_name = "www.example.com"; + char* this_userarg = "somestring"; // Could add things here to help identify this call + getdns_transaction_t this_transaction_id = 0; + + /* Make the call */ + getdns_return_t dns_request_return = getdns_address(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.\n", this_name); + event_base_free(this_event_base); + getdns_context_destroy(this_context); + return(GETDNS_RETURN_GENERIC_ERROR); + } + else + { + /* Call the event loop */ + int dispatch_return = event_base_dispatch(this_event_base); + UNUSED_PARAM(dispatch_return); + // TODO: check the return value above + } + /* Clean up */ + event_base_free(this_event_base); + getdns_context_destroy(this_context); + /* Assuming we get here, leave gracefully */ + exit(EXIT_SUCCESS); +} diff --git a/src/example/example-synchronous.c b/src/example/example-synchronous.c new file mode 100644 index 00000000..463a7e61 --- /dev/null +++ b/src/example/example-synchronous.c @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include + +int main() +{ + getdns_return_t this_ret; /* Holder for all function returns */ + /* Create the DNS context for this call */ + struct getdns_context *this_context = NULL; + getdns_return_t context_create_return = getdns_context_create(&this_context, 1); + if (context_create_return != GETDNS_RETURN_GOOD) + { + fprintf(stderr, "Trying to create the context failed: %d\n", context_create_return); + return(GETDNS_RETURN_GENERIC_ERROR); + } + /* Set up the getdns_sync_request call */ + const char * this_name = "www.example.com"; + uint8_t this_request_type = GETDNS_RRTYPE_A; + /* Get the A and AAAA records */ + struct getdns_dict * this_extensions = getdns_dict_create(); + this_ret = getdns_dict_set_int(this_extensions, "return_both_v4_and_v6", GETDNS_EXTENSION_TRUE); + if (this_ret != GETDNS_RETURN_GOOD) + { + fprintf(stderr, "Trying to set an extension do both IPv4 and IPv6 failed: %d\n", this_ret); + getdns_dict_destroy(this_extensions); + getdns_context_destroy(this_context); + return(GETDNS_RETURN_GENERIC_ERROR); + } + struct getdns_dict * this_response = NULL; + + /* Make the call */ + getdns_return_t dns_request_return = getdns_general_sync(this_context, this_name, this_request_type, + this_extensions, &this_response); + if (dns_request_return == GETDNS_RETURN_BAD_DOMAIN_NAME) + { + fprintf(stderr, "A bad domain name was used: %s. Exiting.\n", this_name); + getdns_dict_destroy(this_response); + getdns_dict_destroy(this_extensions); + getdns_context_destroy(this_context); + return(GETDNS_RETURN_GENERIC_ERROR); + } + else + { + /* Be sure the search returned something */ + uint32_t this_error; + this_ret = getdns_dict_get_int(this_response, "status", &this_error); // Ignore any error + if (this_error != GETDNS_RESPSTATUS_GOOD) // If the search didn't return "good" + { + fprintf(stderr, "The search had no results, and a return value of %d. Exiting.\n", this_error); + getdns_dict_destroy(this_response); + getdns_dict_destroy(this_extensions); + getdns_context_destroy(this_context); + return(GETDNS_RETURN_GENERIC_ERROR); + } + struct getdns_list * just_the_addresses_ptr; + this_ret = getdns_dict_get_list(this_response, "just_address_answers", &just_the_addresses_ptr); // Ignore any error + size_t num_addresses; + this_ret = getdns_list_get_length(just_the_addresses_ptr, &num_addresses); // Ignore any error + /* Go through each record */ + for ( size_t rec_count = 0; rec_count < num_addresses; ++rec_count ) + { + struct getdns_dict * this_address; + this_ret = getdns_list_get_dict(just_the_addresses_ptr, rec_count, &this_address); // Ignore any error + /* Just print the address */ + struct getdns_bindata * this_address_data; + this_ret = getdns_dict_get_bindata(this_address, "address_data", &this_address_data); // Ignore any error + char *this_address_str = getdns_display_ip_address(this_address_data); + printf("The address is %s\n", this_address_str); + free(this_address_str); + } + } + /* Clean up */ + getdns_dict_destroy(this_response); + getdns_dict_destroy(this_extensions); + getdns_context_destroy(this_context); + /* Assuming we get here, leave gracefully */ + exit(EXIT_SUCCESS); +} diff --git a/src/example/example-tree.c b/src/example/example-tree.c new file mode 100644 index 00000000..dc264dc3 --- /dev/null +++ b/src/example/example-tree.c @@ -0,0 +1,151 @@ +#include +#include +#include +#include +#include +#include + +#define UNUSED_PARAM(x) ((void)(x)) + +/* Set up the callback function, which will also do the processing of the results */ +void this_callbackfn(struct getdns_context *this_context, + getdns_return_t this_callback_type, + struct getdns_dict *this_response, + void *this_userarg, + getdns_transaction_t this_transaction_id) +{ + UNUSED_PARAM(this_userarg); /* Not looking at the userarg for this example */ + UNUSED_PARAM(this_context); /* Not looking at the context for this example */ + getdns_return_t this_ret; /* Holder for all function returns */ + if (this_callback_type == GETDNS_CALLBACK_COMPLETE) /* This is a callback with data */ + { + /* Be sure the search returned something */ + uint32_t this_error; + this_ret = getdns_dict_get_int(this_response, "status", &this_error); // Ignore any error + if (this_error != GETDNS_RESPSTATUS_GOOD) // If the search didn't return "good" + { + fprintf(stderr, "The search had no results, and a return value of %d. Exiting.\n", this_error); + getdns_dict_destroy(this_response); + return; + } + /* Find all the answers returned */ + struct getdns_list * these_answers; + this_ret = getdns_dict_get_list(this_response, "replies_tree", &these_answers); + if (this_ret == GETDNS_RETURN_NO_SUCH_DICT_NAME) + { + fprintf(stderr, "Weird: the response had no error, but also no replies_tree. Exiting.\n"); + getdns_dict_destroy(this_response); + return; + } + size_t num_answers; + this_ret = getdns_list_get_length(these_answers, &num_answers); + /* Go through each answer */ + for ( size_t rec_count = 0; rec_count < num_answers; ++rec_count ) + { + struct getdns_dict * this_record; + this_ret = getdns_list_get_dict(these_answers, rec_count, &this_record); // Ignore any error + /* Get the answer section */ + struct getdns_list * this_answer; + this_ret = getdns_dict_get_list(this_record, "answer", &this_answer); // Ignore any error + /* Get each RR in the answer section */ + size_t num_rrs; + this_ret = getdns_list_get_length(this_answer, &num_rrs); + for ( size_t rr_count = 0; rr_count < num_rrs; ++rr_count ) + { + struct getdns_dict *this_rr = NULL; + this_ret = getdns_list_get_dict(this_answer, rr_count, &this_rr); // Ignore any error + /* Get the RDATA */ + struct getdns_dict * this_rdata = NULL; + this_ret = getdns_dict_get_dict(this_rr, "rdata", &this_rdata); // Ignore any error + /* Get the RDATA type */ + uint32_t this_type; + this_ret = getdns_dict_get_int(this_rr, "type", &this_type); // Ignore any error + /* If it is type A or AAAA, print the value */ + if (this_type == GETDNS_RRTYPE_A) + { + struct getdns_bindata * this_a_record = NULL; + this_ret = getdns_dict_get_bindata(this_rdata, "ipv4_address", &this_a_record); + if (this_ret == GETDNS_RETURN_NO_SUCH_DICT_NAME) + { + fprintf(stderr, "Weird: the A record at %d in record at %d had no address. Exiting.\n", + (int) rr_count, (int) rec_count); + getdns_dict_destroy(this_response); + return; + } + char *this_address_str = getdns_display_ip_address(this_a_record); + printf("The IPv4 address is %s\n", this_address_str); + free(this_address_str); + } + else if (this_type == GETDNS_RRTYPE_AAAA) + { + struct getdns_bindata * this_aaaa_record = NULL; + this_ret = getdns_dict_get_bindata(this_rdata, "ipv6_address", &this_aaaa_record); + if (this_ret == GETDNS_RETURN_NO_SUCH_DICT_NAME) + { + fprintf(stderr, "Weird: the AAAA record at %d in record at %d had no address. Exiting.\n", + (int) rr_count, (int) rec_count); + getdns_dict_destroy(this_response); + return; + } + char *this_address_str = getdns_display_ip_address(this_aaaa_record); + printf("The IPv6 address is %s\n", this_address_str); + free(this_address_str); + } + } + } + } + else if (this_callback_type == GETDNS_CALLBACK_CANCEL) + fprintf(stderr, "The callback with ID %"PRIu64" was cancelled. Exiting.\n", this_transaction_id); + else + fprintf(stderr, "The callback got a callback_type of %d. Exiting.\n", this_callback_type); + getdns_dict_destroy(this_response); +} + +int main() +{ + /* Create the DNS context for this call */ + struct getdns_context *this_context = NULL; + getdns_return_t context_create_return = getdns_context_create(&this_context, 1); + if (context_create_return != GETDNS_RETURN_GOOD) + { + fprintf(stderr, "Trying to create the context failed: %d\n", context_create_return); + return(GETDNS_RETURN_GENERIC_ERROR); + } + /* 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.\n"); + getdns_context_destroy(this_context); + return(GETDNS_RETURN_GENERIC_ERROR); + } + (void)getdns_extension_set_libevent_base(this_context, this_event_base); + /* Set up the getdns call */ + const char * this_name = "www.example.com"; + char* this_userarg = "somestring"; // Could add things here to help identify this call + getdns_transaction_t this_transaction_id = 0; + + /* Make the call */ + getdns_return_t dns_request_return = getdns_address(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.\n", this_name); + event_base_free(this_event_base); + getdns_context_destroy(this_context); + return(GETDNS_RETURN_GENERIC_ERROR); + } + else + { + /* Call the event loop */ + int dispatch_return = event_base_dispatch(this_event_base); + UNUSED_PARAM(dispatch_return); + // TODO: check the return value above + } + /* Clean up */ + event_base_free(this_event_base); + getdns_context_destroy(this_context); + /* Assuming we get here, leave gracefully */ + exit(EXIT_SUCCESS); +} diff --git a/src/example/example_all_functions.c b/src/example/example_all_functions.c deleted file mode 100644 index e8719fae..00000000 --- a/src/example/example_all_functions.c +++ /dev/null @@ -1,197 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#define UNUSED_PARAM(x) ((void)(x)) - -/* The return values */ -getdns_return_t retregular; -char *retcharstar; - -/* The args */ -bool boolarg; -char *charstararg; -getdns_callback_t callbackarg; -uint16_t regulararg; -uint16_t *regularptrarg; -getdns_transaction_t txidarg; -getdns_transaction_t *txidptrarg; - -getdns_data_type *datatypeptrarg; -struct getdns_bindata **bindataptrarg; -struct getdns_dict *dictarg; -struct getdns_bindata *bindataarg; -struct getdns_list *listarg; -struct getdns_dict **dictptrarg; -struct getdns_list **listptrarg; - -size_t sizetarg; -size_t *sizetptrarg; -struct getdns_context *contextarg = NULL; -uint8_t uint8arg; -uint16_t uint16arg; -uint32_t uint32arg; -uint8_t *uint8ptrarg; -uint16_t *uint16ptrarg; -uint32_t *uint32ptrarg; -void *arrayarg; -void * -allocfunctionarg(size_t foo) -{ - UNUSED_PARAM(foo); - return NULL; -} - -void * -reallocfunctionarg(void *foo, size_t bar) -{ - UNUSED_PARAM(foo); - UNUSED_PARAM(bar); - return NULL; -} - -void -deallocfunctionarg(void *foo) -{ - UNUSED_PARAM(foo); -} - -void -setcallbackfunctionarg(struct getdns_context *foo1, uint16_t foo2) -{ - UNUSED_PARAM(foo1); - UNUSED_PARAM(foo2); -} - -int -main() -{ - - retregular = getdns_general(contextarg, - charstararg, - uint16arg, dictarg, arrayarg, txidptrarg, callbackarg); - - retregular = getdns_address(contextarg, - charstararg, dictarg, arrayarg, txidptrarg, callbackarg); - - retregular = getdns_hostname(contextarg, - dictarg, dictarg, arrayarg, txidptrarg, callbackarg); - - retregular = getdns_service(contextarg, - charstararg, dictarg, arrayarg, txidptrarg, callbackarg); - - retregular = getdns_context_create(&contextarg, boolarg); - - retregular = getdns_cancel_callback(contextarg, txidarg); - - retregular = getdns_general_sync(contextarg, - charstararg, uint16arg, dictarg, &dictarg); - - retregular = getdns_address_sync(contextarg, - charstararg, dictarg, &dictarg); - - retregular = getdns_hostname_sync(contextarg, - dictarg, dictarg, &dictarg); - - retregular = getdns_service_sync(contextarg, - charstararg, dictarg, &dictarg); - - retregular = getdns_list_get_length(listarg, sizetptrarg); - retregular = - getdns_list_get_data_type(listarg, sizetarg, datatypeptrarg); - retregular = getdns_list_get_dict(listarg, sizetarg, dictptrarg); - retregular = getdns_list_get_list(listarg, sizetarg, listptrarg); - retregular = getdns_list_get_bindata(listarg, sizetarg, bindataptrarg); - retregular = getdns_list_get_int(listarg, sizetarg, uint32ptrarg); - - retregular = getdns_dict_get_names(dictarg, listptrarg); - retregular = - getdns_dict_get_data_type(dictarg, charstararg, datatypeptrarg); - retregular = getdns_dict_get_dict(dictarg, charstararg, dictptrarg); - retregular = getdns_dict_get_list(dictarg, charstararg, listptrarg); - retregular = - getdns_dict_get_bindata(dictarg, charstararg, bindataptrarg); - retregular = getdns_dict_get_int(dictarg, charstararg, uint32ptrarg); - - listarg = getdns_list_create(); - retregular = getdns_list_set_dict(listarg, sizetarg, dictarg); - retregular = getdns_list_set_list(listarg, sizetarg, listarg); - retregular = getdns_list_set_bindata(listarg, sizetarg, bindataarg); - retregular = getdns_list_set_int(listarg, sizetarg, uint32arg); - - dictarg = getdns_dict_create(); - retregular = getdns_dict_set_dict(dictarg, charstararg, dictarg); - retregular = getdns_dict_set_list(dictarg, charstararg, listarg); - retregular = getdns_dict_set_bindata(dictarg, charstararg, bindataarg); - retregular = getdns_dict_set_int(dictarg, charstararg, uint32arg); - retcharstar = getdns_pretty_print_dict(dictarg); - - retcharstar = getdns_convert_fqdn_to_dns_name(charstararg); - - retcharstar = getdns_convert_dns_name_to_fqdn(charstararg); - - retcharstar = getdns_convert_ulabel_to_alabel(charstararg); - - retcharstar = getdns_convert_alabel_to_ulabel(charstararg); - - retregular = getdns_validate_dnssec(bindataarg, listarg, listarg); - - retcharstar = getdns_display_ip_address(bindataarg); - - retregular = getdns_context_set_context_update_callback(contextarg, - setcallbackfunctionarg); - - retregular = getdns_context_set_resolution_type(contextarg, - regulararg); - - retregular = getdns_context_set_namespaces(contextarg, - sizetarg, regularptrarg); - - retregular = getdns_context_set_dns_transport(contextarg, regulararg); - - retregular = getdns_context_set_limit_outstanding_queries(contextarg, - uint16arg); - - retregular = getdns_context_set_timeout(contextarg, uint16arg); - - retregular = getdns_context_set_follow_redirects(contextarg, - regulararg); - - retregular = getdns_context_set_dns_root_servers(contextarg, listarg); - - retregular = getdns_context_set_append_name(contextarg, regulararg); - - retregular = getdns_context_set_suffix(contextarg, listarg); - - retregular = getdns_context_set_dnssec_trust_anchors(contextarg, - listarg); - - retregular = getdns_context_set_dnssec_allowed_skew(contextarg, - uint16arg); - - retregular = getdns_context_set_stub_resolution(contextarg, listarg); - - retregular = - getdns_context_set_edns_maximum_udp_payload_size(contextarg, - uint16arg); - - retregular = getdns_context_set_edns_extended_rcode(contextarg, - uint8arg); - - retregular = getdns_context_set_edns_version(contextarg, uint8arg); - - retregular = getdns_context_set_edns_do_bit(contextarg, uint8arg); - - retregular = getdns_context_set_memory_functions(contextarg, - allocfunctionarg, reallocfunctionarg, deallocfunctionarg); - - getdns_list_destroy(listarg); - getdns_dict_destroy(dictarg); - getdns_context_destroy(contextarg); - - return (0); -} /* End of main() */ diff --git a/src/example/example_simple_answers.c b/src/example/example_simple_answers.c deleted file mode 100644 index d29fd97d..00000000 --- a/src/example/example_simple_answers.c +++ /dev/null @@ -1,160 +0,0 @@ -/** - * /brief demonstrate asynchronous use of the API for fetching DNS data - * - * Originally taken from the getdns API description pseudo implementation. - */ - -/* The MIT License (MIT) - * Copyright (c) 2013 Verisign, Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "../config.h" -#include -#include -#include -#include -#include -#include -#ifdef HAVE_EVENT2_EVENT_H -# include -#else -# include -#endif - -#define UNUSED_PARAM(x) ((void)(x)) - -/* Set up the callback function, which will also do the processing of the results */ -void -this_callbackfn(struct getdns_context *this_context, - uint16_t this_callback_type, - struct getdns_dict *this_response, - void *this_userarg, getdns_transaction_t this_transaction_id) -{ - UNUSED_PARAM(this_userarg); /* Not looking at the userarg for this example */ - UNUSED_PARAM(this_context); /* Not looking at the context for this example */ - getdns_return_t this_ret; /* Holder for all function returns */ - if (this_callback_type == GETDNS_CALLBACK_COMPLETE) { /* This is a callback with data */ - /* Be sure the search returned something */ - uint32_t this_error; - this_ret = getdns_dict_get_int(this_response, "status", &this_error); // Ignore any error - if (this_error != GETDNS_RESPSTATUS_GOOD) // If the search didn't return "good" - { - fprintf(stderr, - "The search had no results, and a return value of %d. Exiting.\n", - this_error); - getdns_dict_destroy(this_response); - return; - } - struct getdns_list *just_the_addresses_ptr; - this_ret = - getdns_dict_get_list(this_response, "just_address_answers", - &just_the_addresses_ptr); - if (this_ret != GETDNS_RETURN_GOOD) // This check is really not needed, but prevents a compiler error under "pedantic" - { - fprintf(stderr, - "Trying to get the answers failed: %d\n", - this_ret); - getdns_dict_destroy(this_response); - return; - } - size_t num_addresses = 0; - this_ret = getdns_list_get_length(just_the_addresses_ptr, &num_addresses); // Ignore any error - /* Go through each record */ - if (num_addresses == 0) { - fprintf(stderr, "There are no addresses.\n"); - getdns_dict_destroy(this_response); - return; - } - for (size_t rec_count = 0; rec_count < num_addresses; - ++rec_count) { - struct getdns_bindata *this_address_data; - char *ipAddr = NULL; - this_ret = getdns_list_get_bindata(just_the_addresses_ptr, rec_count, &this_address_data); // Ignore any error - ipAddr = getdns_display_ip_address(this_address_data); - printf("The address is %s\n", ipAddr); - free(ipAddr); - } - } else if (this_callback_type == GETDNS_CALLBACK_CANCEL) - fprintf(stderr, - "The callback with ID %" PRIu64 - " was cancelled. Exiting.\n", this_transaction_id); - else - fprintf(stderr, - "The callback got a callback_type of %d. Exiting.\n", - this_callback_type); - - /* clean up */ - getdns_dict_destroy(this_response); -} - -int -main() -{ - /* Create the DNS context for this call */ - struct getdns_context *this_context = NULL; - getdns_return_t context_create_return = - getdns_context_create(&this_context, 1); - if (context_create_return != GETDNS_RETURN_GOOD) { - fprintf(stderr, "Trying to create the context failed: %d", - context_create_return); - return (GETDNS_RETURN_GENERIC_ERROR); - } - /* 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.\n"); - return (GETDNS_RETURN_GENERIC_ERROR); - } - (void) getdns_extension_set_libevent_base(this_context, - this_event_base); - /* Set up the getdns call */ - const char *this_name = "www.example.com"; - char *this_userarg = "somestring"; // Could add things here to help identify this call - getdns_transaction_t this_transaction_id = 0; - - // getdns_context_set_resolution_type(this_context, GETDNS_CONTEXT_STUB); - - /* Make the call */ - getdns_return_t dns_request_return = - getdns_address(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.\n", - this_name); - return (GETDNS_RETURN_GENERIC_ERROR); - } else if (dns_request_return != GETDNS_RETURN_GOOD) { - fprintf(stderr, "The context is not setup properly.\n"); - return (GETDNS_RETURN_GENERIC_ERROR); - } else { - /* Call the event loop */ - int dispatch_return = event_base_dispatch(this_event_base); - UNUSED_PARAM(dispatch_return); - // TODO: check the return value above - } - /* Clean up */ - getdns_context_destroy(this_context); - event_base_free(this_event_base); - /* Assuming we get here, leave gracefully */ - exit(EXIT_SUCCESS); -} /* main */ - -/* example-simple-answers.c */ diff --git a/src/example/example_synchronous.c b/src/example/example_synchronous.c deleted file mode 100644 index 87c14bba..00000000 --- a/src/example/example_synchronous.c +++ /dev/null @@ -1,118 +0,0 @@ -/* example-synchronous.c - * - * Originally taken from the getdns API description pseudo implementation. - * - * The MIT License (MIT) - * Copyright (c) 2013 Verisign, Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - */ - -#include -#include -#include -#include -#include -#include - -int -main() -{ - getdns_return_t context_create_return; - struct getdns_list *just_the_addresses_ptr; - size_t num_addresses = 0; - size_t rec_count; - struct getdns_bindata *this_address_data; - struct getdns_context *this_context = NULL; - uint32_t this_error = 0; - struct getdns_dict *this_extensions = NULL; - const char *this_name = "www.example.com"; - uint8_t this_request_type = GETDNS_RRTYPE_A; - struct getdns_dict *this_response = NULL; - getdns_return_t this_ret; - - /* Create the DNS context for this call */ - context_create_return = getdns_context_create(&this_context, 1); - if (context_create_return != GETDNS_RETURN_GOOD) { - fprintf(stderr, "Trying to create the context failed: %d", - context_create_return); - return (GETDNS_RETURN_GENERIC_ERROR); - } - - /* Set up the getdns_sync_request call */ - /* Get the A and AAAA records */ - this_extensions = getdns_dict_create(); - this_ret = - getdns_dict_set_int(this_extensions, "return_both_v4_and_v6", - GETDNS_EXTENSION_TRUE); - if (this_ret != GETDNS_RETURN_GOOD) { - fprintf(stderr, - "Trying to set an extension do both IPv4 and IPv6 failed: %d", - this_ret); - return (GETDNS_RETURN_GENERIC_ERROR); - } - - /* Make the call */ - getdns_return_t dns_request_return = - getdns_general_sync(this_context, this_name, this_request_type, - this_extensions, &this_response); - /* free the extensions */ - getdns_dict_destroy(this_extensions); - 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 { - /* Be sure the search returned something */ - this_ret = getdns_dict_get_int(this_response, "status", &this_error); // Ignore any error - if (this_error != GETDNS_RESPSTATUS_GOOD) // If the search didn't return "good" - { - fprintf(stderr, - "The search had no results, and a return value of %d. Exiting.", - this_error); - return (GETDNS_RETURN_GENERIC_ERROR); - } - this_ret = getdns_dict_get_list(this_response, "just_address_answers", &just_the_addresses_ptr); // Ignore any error - this_ret = getdns_list_get_length(just_the_addresses_ptr, &num_addresses); // Ignore any error - /* Go through each record */ - if (num_addresses > 0) { - for (rec_count = 0; rec_count < num_addresses; - ++rec_count) { - char *display; - this_ret = getdns_list_get_bindata(just_the_addresses_ptr, rec_count, &this_address_data); // Ignore any error - display = - getdns_display_ip_address - (this_address_data); - /* Just print the address */ - printf("The address is %s\n", display); - if (display) { - free(display); - } - } - } - } - - /* Clean up */ - getdns_context_destroy(this_context); - - exit(EXIT_SUCCESS); -} /* main */ - -/* example-synchronous.c */ diff --git a/src/example/example_tree.c b/src/example/example_tree.c deleted file mode 100644 index edadd190..00000000 --- a/src/example/example_tree.c +++ /dev/null @@ -1,163 +0,0 @@ -#include "../config.h" -#include -#include -#include -#include -#include -#include -#ifdef HAVE_EVENT2_EVENT_H -# include -#else -# include -#endif - -#define UNUSED_PARAM(x) ((void)(x)) - -/* Set up the callback function, which will also do the processing of the results */ -void -this_callbackfn(struct getdns_context *this_context, - getdns_return_t this_callback_type, - struct getdns_dict *this_response, - void *this_userarg, getdns_transaction_t this_transaction_id) -{ - UNUSED_PARAM(this_userarg); /* Not looking at the userarg for this example */ - UNUSED_PARAM(this_context); /* Not looking at the context for this example */ - getdns_return_t this_ret; /* Holder for all function returns */ - if (this_callback_type == GETDNS_CALLBACK_COMPLETE) { /* This is a callback with data */ - /* Be sure the search returned something */ - uint32_t this_error; - this_ret = getdns_dict_get_int(this_response, "status", &this_error); // Ignore any error - if (this_error != GETDNS_RESPSTATUS_GOOD) // If the search didn't return "good" - { - fprintf(stderr, - "The search had no results, and a return value of %d. Exiting.", - this_error); - return; - } - /* Find all the answers returned */ - struct getdns_list *these_answers; - this_ret = - getdns_dict_get_list(this_response, "replies_tree", - &these_answers); - if (this_ret == GETDNS_RETURN_NO_SUCH_DICT_NAME) { - fprintf(stderr, - "Weird: the response had no error, but also no replies_tree. Exiting."); - return; - } - size_t num_answers; - this_ret = getdns_list_get_length(these_answers, &num_answers); - /* Go through each answer */ - for (size_t rec_count = 0; rec_count < num_answers; - ++rec_count) { - struct getdns_dict *this_record; - this_ret = getdns_list_get_dict(these_answers, rec_count, &this_record); // Ignore any error - /* Get the answer section */ - struct getdns_list *this_answer; - this_ret = getdns_dict_get_list(this_record, "answer", &this_answer); // Ignore any error - /* Get each RR in the answer section */ - size_t num_rrs_ptr; - this_ret = - getdns_list_get_length(this_answer, &num_rrs_ptr); - for (size_t rr_count = 0; rr_count < num_rrs_ptr; - ++rr_count) { - struct getdns_dict *this_rr = NULL; - this_ret = getdns_list_get_dict(this_answer, rr_count, &this_rr); // Ignore any error - /* Get the RDATA */ - struct getdns_dict *this_rdata = NULL; - this_ret = getdns_dict_get_dict(this_rr, "rdata", &this_rdata); // Ignore any error - /* Get the RDATA type */ - uint32_t this_type; - this_ret = getdns_dict_get_int(this_rr, "type", &this_type); // Ignore any error - /* If it is type A or AAAA, print the value */ - if (this_type == GETDNS_RRTYPE_A) { - struct getdns_bindata *this_a_record = - NULL; - this_ret = - getdns_dict_get_bindata(this_rdata, - "ipv4_address", &this_a_record); - if (this_ret == - GETDNS_RETURN_NO_SUCH_DICT_NAME) { - fprintf(stderr, - "Weird: the A record at %d in record at %d had no address. Exiting.", - (int) rr_count, - (int) rec_count); - return; - } - printf("The IPv4 address is %s\n", - getdns_display_ip_address - (this_a_record)); - } else if (this_type == GETDNS_RRTYPE_AAAA) { - struct getdns_bindata *this_aaaa_record - = NULL; - this_ret = - getdns_dict_get_bindata(this_rdata, - "ipv6_address", &this_aaaa_record); - if (this_ret == - GETDNS_RETURN_NO_SUCH_DICT_NAME) { - fprintf(stderr, - "Weird: the AAAA record at %d in record at %d had no address. Exiting.", - (int) rr_count, - (int) rec_count); - return; - } - printf("The IPv6 address is %s\n", - getdns_display_ip_address - (this_aaaa_record)); - } - } - } - } else if (this_callback_type == GETDNS_CALLBACK_CANCEL) - fprintf(stderr, - "The callback with ID %" PRIu64 " was cancelled. Exiting.", - this_transaction_id); - else - fprintf(stderr, - "The callback got a callback_type of %d. Exiting.", - this_callback_type); -} - -int -main() -{ - /* Create the DNS context for this call */ - struct getdns_context *this_context = NULL; - getdns_return_t context_create_return = - getdns_context_create(&this_context, 1); - if (context_create_return != GETDNS_RETURN_GOOD) { - fprintf(stderr, "Trying to create the context failed: %d", - context_create_return); - return (GETDNS_RETURN_GENERIC_ERROR); - } - /* 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."); - return (GETDNS_RETURN_GENERIC_ERROR); - } - (void) getdns_extension_set_libevent_base(this_context, - this_event_base); - /* Set up the getdns call */ - const char *this_name = "www.example.com"; - char *this_userarg = "somestring"; // Could add things here to help identify this call - getdns_transaction_t this_transaction_id = 0; - - /* Make the call */ - getdns_return_t dns_request_return = - getdns_address(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 { - /* Call the event loop */ - int dispatch_return = event_base_dispatch(this_event_base); - UNUSED_PARAM(dispatch_return); - // TODO: check the return value above - } - /* Clean up */ - getdns_context_destroy(this_context); - /* Assuming we get here, leave gracefully */ - exit(EXIT_SUCCESS); -} diff --git a/src/example/getdns_core_only.h b/src/example/getdns_core_only.h new file mode 100644 index 00000000..537f3a42 --- /dev/null +++ b/src/example/getdns_core_only.h @@ -0,0 +1 @@ +#include diff --git a/src/example/getdns_libevent.h b/src/example/getdns_libevent.h new file mode 100644 index 00000000..8b4a19ac --- /dev/null +++ b/src/example/getdns_libevent.h @@ -0,0 +1,6 @@ +#include +#ifdef HAVE_EVENT2_EVENT_H +#include +#else +#include +#endif diff --git a/src/test/Makefile.in b/src/test/Makefile.in index f5583513..603f93a6 100644 --- a/src/test/Makefile.in +++ b/src/test/Makefile.in @@ -19,7 +19,7 @@ CC=gcc CFLAGS=@CFLAGS@ -Wall -I$(srcdir)/ -I$(srcdir)/../ -I/usr/local/include -std=c99 $(cflags) LDFLAGS=@LDFLAGS@ -L. -L.. -L/usr/local/lib LDLIBS=-lgetdns @LIBS@ -PROGRAMS=tests_dict tests_list tests_stub_async tests_stub_sync tests_list_mu +PROGRAMS=tests_dict tests_list tests_stub_async tests_stub_sync .SUFFIXES: .c .o .a .lo .h @@ -37,8 +37,7 @@ tests_list: tests_list.o testmessages.o $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ tests_list.o testmessages.o tests_list_mu: tests_list_mu.o - $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -lcunit -o $@ - tests_list_mu.o + $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -lcunit -o $@ tests_list_mu.o tests_stub_async: tests_stub_async.o testmessages.o $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ tests_stub_async.o testmessages.o diff --git a/src/util-internal.c b/src/util-internal.c index d6a44943..24992604 100644 --- a/src/util-internal.c +++ b/src/util-internal.c @@ -38,6 +38,7 @@ #include "getdns/getdns.h" #include #include "dict.h" +#include "list.h" #include "util-internal.h" #include "types-internal.h" @@ -47,7 +48,7 @@ * The list has to be in sorted order for bsearch lookup in function * validate_extensions. */ -getdns_extension_format extformats[] = { +static getdns_extension_format extformats[] = { {"add_opt_parameters", t_dict}, {"add_warning_for_bad_dns", t_int}, {"dnssec_return_only_secure", t_int}, @@ -59,6 +60,9 @@ getdns_extension_format extformats[] = { {"specify_class", t_int}, }; +static struct getdns_bindata IPv4_str_bindata = { 5, (void *)"IPv4" }; +static struct getdns_bindata IPv6_str_bindata = { 5, (void *)"IPv6" }; + getdns_return_t getdns_dict_util_set_string(struct getdns_dict * dict, char *name, const char *value) { @@ -336,23 +340,43 @@ create_list_from_rr_list(struct getdns_context *context, ldns_rr_list * rr_list) static getdns_return_t add_only_addresses(struct getdns_list * addrs, ldns_rr_list * rr_list) { - int r = 0; + int r = GETDNS_RETURN_GOOD; size_t i = 0; - for (i = 0; i < ldns_rr_list_rr_count(rr_list); ++i) { + size_t item_idx = 0; + + r = getdns_list_get_length(addrs, &item_idx); + for (i = 0; r == GETDNS_RETURN_GOOD && + i < ldns_rr_list_rr_count(rr_list); ++i) { ldns_rr *rr = ldns_rr_list_rr(rr_list, i); size_t j = 0; size_t rd_count = ldns_rr_rd_count(rr); - for (j = 0; j < rd_count; ++j) { - size_t item_idx = 0; + for (j = 0; r == GETDNS_RETURN_GOOD && j < rd_count; ++j) { ldns_rdf *rdf = ldns_rr_rdf(rr, j); - if (ldns_rdf_get_type(rdf) == LDNS_RDF_TYPE_A || - ldns_rdf_get_type(rdf) == LDNS_RDF_TYPE_AAAA) { - struct getdns_bindata rbin = - { ldns_rdf_size(rdf), ldns_rdf_data(rdf) }; - r |= getdns_list_add_item(addrs, &item_idx); - r |= getdns_list_set_bindata(addrs, item_idx, - &rbin); + if (ldns_rdf_get_type(rdf) != LDNS_RDF_TYPE_A && + ldns_rdf_get_type(rdf) != LDNS_RDF_TYPE_AAAA) { + continue; } + struct getdns_dict *this_address = + getdns_dict_create_with_extended_memory_functions( + addrs->mf.mf_arg, + addrs->mf.mf.ext.malloc, + addrs->mf.mf.ext.realloc, + addrs->mf.mf.ext.free); + if (this_address == NULL) { + r |= GETDNS_RETURN_MEMORY_ERROR; + break; + } + struct getdns_bindata rbin = + { ldns_rdf_size(rdf), ldns_rdf_data(rdf) }; + r |= getdns_dict_set_bindata(this_address, + GETDNS_STR_ADDRESS_TYPE, + ( ldns_rdf_get_type(rdf) == LDNS_RDF_TYPE_A + ? &IPv4_str_bindata : &IPv6_str_bindata)); + r |= getdns_dict_set_bindata(this_address, + GETDNS_STR_ADDRESS_DATA, &rbin); + r |= getdns_list_set_dict(addrs, item_idx++, + this_address); + getdns_dict_destroy(this_address); } } return r;