diff --git a/spec/example-reverse.c b/spec/example-reverse.c index 795a8bae..4389dc42 100644 --- a/spec/example-reverse.c +++ b/spec/example-reverse.c @@ -20,37 +20,57 @@ void this_callbackfn(struct getdns_context *this_context, 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" + 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); + 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" + struct getdns_list *replies_tree; + this_ret = getdns_dict_get_list(this_response, "replies_tree", &replies_tree); // Ignore any error + size_t num_replies; + this_ret = getdns_list_get_length(replies_tree, &num_replies); // Ignore any error + /* Go through each reply */ + for ( size_t reply_count = 0; reply_count < num_replies; ++reply_count) { - 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 + struct getdns_dict * this_reply; + this_ret = getdns_list_get_dict(replies_tree, reply_count, &this_reply); // 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)); + struct getdns_list* reply_answers; + this_ret = getdns_dict_get_list(this_reply, "answer", &reply_answers); // Ignore any error + size_t num_answers; + this_ret = getdns_list_get_length(reply_answers, &num_answers); // Ignore any error + /* Go through each answer */ + for ( size_t answer_count = 0; answer_count < num_answers; ++answer_count) + { + struct getdns_dict * this_rr; + this_ret = getdns_list_get_dict(reply_answers, answer_count, &this_rr); + /* Get the RDATA type */ + uint32_t this_type; + this_ret = getdns_dict_get_int(this_rr, "type", &this_type); // Ignore any error + if (this_type == GETDNS_RRTYPE_PTR) + { + struct getdns_dict *this_rdata; + this_ret = getdns_dict_get_dict(this_rr, "rdata", &this_rdata); // Ignore any error + + struct getdns_bindata * this_dname; + this_ret = getdns_dict_get_bindata(this_rdata, "rdata_raw", &this_dname); + char *this_dname_str = getdns_convert_dns_name_to_fqdn(this_dname->data); + printf("The dname is %s\n", this_dname_str); + free(this_dname_str); + + } + } + } } 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); + getdns_dict_destroy(this_response); } int main() @@ -69,16 +89,18 @@ int main() if (this_event_base == NULL) { fprintf(stderr, "Trying to create the event base failed."); + 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 */ struct getdns_dict * this_addr_to_look_up = getdns_dict_create(); // TODO: check the return value above - struct getdns_bindata * this_type = { 4, 0x49507634 }; - getdns_return_t this_ret = getdns_dict_set_bindata(this_addr_to_look_up, "address_type", this_type); - struct getdns_bindata * this_ipv4_addr = { 4, 0x08080808 }; - this_ret = getdns_dict_set_bindata(this_addr_to_look_up, "address_data", this_ipv4_addr; + 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; @@ -87,7 +109,13 @@ int main() NULL, this_userarg, &this_transaction_id, this_callbackfn); if (dns_request_return == GETDNS_RETURN_BAD_DOMAIN_NAME) { - fprintf(stderr, "A bad IP address was used: %s. Exiting.", this_ipv4_addr); + 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); + getdns_dict_destroy(this_addr_to_look_up); + event_base_free(this_event_base); + getdns_context_destroy(this_context); return(GETDNS_RETURN_GENERIC_ERROR); } else @@ -99,6 +127,7 @@ int main() } /* Clean up */ getdns_dict_destroy(this_addr_to_look_up); + event_base_free(this_event_base); getdns_context_destroy(this_context); /* Assuming we get here, leave gracefully */ exit(EXIT_SUCCESS); diff --git a/spec/example-simple-answers.c b/spec/example-simple-answers.c index 4a4c2b2b..e9d75608 100644 --- a/spec/example-simple-answers.c +++ b/spec/example-simple-answers.c @@ -20,37 +20,42 @@ void this_callbackfn(struct getdns_context *this_context, 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" + 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); + 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", this_ret); + fprintf(stderr, "Trying to get the answers failed: %d\n", this_ret); + getdns_dict_destroy(this_response); return; } - size_t * num_addresses_ptr = NULL; - this_ret = getdns_list_get_length(just_the_addresses_ptr, num_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_ptr; ++rec_count ) + 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 - printf("The address is %s", getdns_display_ip_address(this_address_data)); + 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.", this_transaction_id); + 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.", this_callback_type); + fprintf(stderr, "The callback got a callback_type of %d. Exiting.\n", this_callback_type); + getdns_dict_destroy(this_response); } int main() @@ -68,7 +73,8 @@ int main() this_event_base = event_base_new(); if (this_event_base == NULL) { - fprintf(stderr, "Trying to create the event base failed."); + 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); @@ -82,7 +88,9 @@ int main() 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); + 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 @@ -93,6 +101,7 @@ int main() // 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/spec/example-synchronous.c b/spec/example-synchronous.c index df987bbc..463a7e61 100644 --- a/spec/example-synchronous.c +++ b/spec/example-synchronous.c @@ -13,7 +13,7 @@ int main() 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); + 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 */ @@ -24,7 +24,9 @@ int main() 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); + 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; @@ -34,37 +36,46 @@ int main() this_extensions, &this_response); if (dns_request_return == GETDNS_RETURN_BAD_DOMAIN_NAME) { - fprintf(stderr, "A bad domain name was used: %s. Exiting.", this_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 = 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" + 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); + 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_ptr = NULL; - this_ret = getdns_list_get_length(just_the_addresses_ptr, num_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_ptr; ++rec_count ) + 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 - printf("The address is %s", getdns_display_ip_address(this_address_data)); + 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_context_destroy(this_context); 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/spec/example-tree.c b/spec/example-tree.c index d4960967..dc264dc3 100644 --- a/spec/example-tree.c +++ b/spec/example-tree.c @@ -20,25 +20,27 @@ void this_callbackfn(struct getdns_context *this_context, 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" + 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); + 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); + 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."); + 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_ptr = NULL; - this_ret = getdns_list_get_length(these_answers, num_answers_ptr); + 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_ptr; ++rec_count ) + 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 @@ -46,50 +48,57 @@ void this_callbackfn(struct getdns_context *this_context, 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 = NULL; - this_ret = getdns_list_get_length(this_answer, num_rrs_ptr); - for ( size_t rr_count = 0; rr_count <= *num_rrs_ptr; ++rr_count ) + 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; + 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 = NULL; - this_ret = getdns_dict_get_int(this_rdata, "type", this_type); // Ignore any error + 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) + 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.", + 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; } - printf("The IPv4 address is %s", getdns_display_ip_address(this_a_record)); + 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) + 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.", + 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; } - printf("The IPv6 address is %s", getdns_display_ip_address(this_aaaa_record)); + 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.", this_transaction_id); + 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.", this_callback_type); + fprintf(stderr, "The callback got a callback_type of %d. Exiting.\n", this_callback_type); + getdns_dict_destroy(this_response); } int main() @@ -99,7 +108,7 @@ int main() 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); + 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 */ @@ -107,7 +116,8 @@ int main() this_event_base = event_base_new(); if (this_event_base == NULL) { - fprintf(stderr, "Trying to create the event base failed."); + 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); @@ -121,7 +131,9 @@ int main() 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); + 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 @@ -132,6 +144,7 @@ int main() // 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/spec/getdns-0.371.tgz b/spec/getdns-0.371.tgz new file mode 100644 index 00000000..97c9fd41 Binary files /dev/null and b/spec/getdns-0.371.tgz differ diff --git a/spec/getdns_core_only.h b/spec/getdns_core_only.h index 1da6f724..17d0f893 100644 --- a/spec/getdns_core_only.h +++ b/spec/getdns_core_only.h @@ -1,4 +1,4 @@ -/* Created at 2013-12-06-16-00-32*/ +/* Created at 2013-12-11-23-50-41*/ #ifndef GETDNS_H #define GETDNS_H diff --git a/spec/index.html b/spec/index.html index 7cc6778b..6f67b14c 100644 --- a/spec/index.html +++ b/spec/index.html @@ -1405,37 +1405,42 @@ function.
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" + 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); + 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", this_ret); + fprintf(stderr, "Trying to get the answers failed: %d\n", this_ret); + getdns_dict_destroy(this_response); return; } - size_t * num_addresses_ptr = NULL; - this_ret = getdns_list_get_length(just_the_addresses_ptr, num_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_ptr; ++rec_count ) + 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 - printf("The address is %s", getdns_display_ip_address(this_address_data)); + 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.", this_transaction_id); + 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.", this_callback_type); + fprintf(stderr, "The callback got a callback_type of %d. Exiting.\n", this_callback_type); + getdns_dict_destroy(this_response); } int main() @@ -1453,7 +1458,8 @@ function. this_event_base = event_base_new(); if (this_event_base == NULL) { - fprintf(stderr, "Trying to create the event base failed."); + 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); @@ -1467,7 +1473,9 @@ function. 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); + 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 @@ -1478,6 +1486,7 @@ function. // 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); @@ -1514,25 +1523,27 @@ their TTLs. 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" + 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); + 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); + 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."); + 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_ptr = NULL; - this_ret = getdns_list_get_length(these_answers, num_answers_ptr); + 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_ptr; ++rec_count ) + 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 @@ -1540,50 +1551,57 @@ their TTLs. 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 = NULL; - this_ret = getdns_list_get_length(this_answer, num_rrs_ptr); - for ( size_t rr_count = 0; rr_count <= *num_rrs_ptr; ++rr_count ) + 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; + 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 = NULL; - this_ret = getdns_dict_get_int(this_rdata, "type", this_type); // Ignore any error + 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) + 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.", + 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; } - printf("The IPv4 address is %s", getdns_display_ip_address(this_a_record)); + 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) + 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.", + 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; } - printf("The IPv6 address is %s", getdns_display_ip_address(this_aaaa_record)); + 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.", this_transaction_id); + 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.", this_callback_type); + fprintf(stderr, "The callback got a callback_type of %d. Exiting.\n", this_callback_type); + getdns_dict_destroy(this_response); } int main() @@ -1593,7 +1611,7 @@ their TTLs. 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); + 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 */ @@ -1601,7 +1619,8 @@ their TTLs. this_event_base = event_base_new(); if (this_event_base == NULL) { - fprintf(stderr, "Trying to create the event base failed."); + 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); @@ -1615,7 +1634,9 @@ their TTLs. 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); + 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 @@ -1626,6 +1647,7 @@ their TTLs. // 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); @@ -1686,7 +1708,7 @@ as it is for the synchronous example, it is just done inmain()
.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);
+ 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 */
@@ -1697,7 +1719,9 @@ as it is for the synchronous example, it is just done in main()
.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);
+ 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;
@@ -1707,37 +1731,46 @@ as it is for the synchronous example, it is just done in main()
.this_extensions, &this_response);
if (dns_request_return == GETDNS_RETURN_BAD_DOMAIN_NAME)
{
- fprintf(stderr, "A bad domain name was used: %s. Exiting.", this_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 = 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"
+ 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);
+ 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_ptr = NULL;
- this_ret = getdns_list_get_length(just_the_addresses_ptr, num_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_ptr; ++rec_count )
+ 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
- printf("The address is %s", getdns_display_ip_address(this_address_data));
+ 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_context_destroy(this_context);
getdns_dict_destroy(this_response);
+ getdns_dict_destroy(this_extensions);
+ getdns_context_destroy(this_context);
/* Assuming we get here, leave gracefully */
exit(EXIT_SUCCESS);
}
@@ -2120,7 +2153,7 @@ getdns_context_set_extended_memory_functions(
void (*free)(void *userarg, void *ptr)
);
The given extended memory management functions will be used for creating the response dicts.
-The value of userarg
argument will be passed to the custom malloc
,
realloc
, and free
.
+The value of userarg
argument will be passed to the custom malloc
, realloc
, and free
.
The response dicts inherit the custom memory management functions and the value for userarg
from the context and will deallocate themselves (and their members) with the custom deallocator.
There is a tarball that includes the .h files, +
There is a tarball that includes the .h files, the examples, and so on. The examples all make, even though there is no API implementation, based on a pseudo-implementation in the tarball; see make-examples-PLATFORM.sh. Note that this currently builds fine on the Macintosh and Ubuntu; help is definitely appreciated on making the build process diff --git a/src/getdns/index.html b/src/getdns/index.html index 99c6218c..6f67b14c 100644 --- a/src/getdns/index.html +++ b/src/getdns/index.html @@ -2,6 +2,11 @@