2013-11-05 10:28:10 -06:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <getdns_core_only.h>
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
getdns_return_t this_ret; /* Holder for all function returns */
|
|
|
|
/* Create the DNS context for this call */
|
2013-12-04 09:46:42 -06:00
|
|
|
struct getdns_context *this_context = NULL;
|
2013-12-04 09:36:28 -06:00
|
|
|
getdns_return_t context_create_return = getdns_context_create(&this_context, 1);
|
2013-11-05 10:28:10 -06:00
|
|
|
if (context_create_return != GETDNS_RETURN_GOOD)
|
|
|
|
{
|
2013-12-11 16:55:35 -06:00
|
|
|
fprintf(stderr, "Trying to create the context failed: %d\n", context_create_return);
|
2013-11-05 10:28:10 -06:00
|
|
|
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)
|
|
|
|
{
|
2013-12-11 16:55:35 -06:00
|
|
|
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);
|
2013-11-05 10:28:10 -06:00
|
|
|
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,
|
2013-11-13 08:05:27 -06:00
|
|
|
this_extensions, &this_response);
|
2013-11-05 10:28:10 -06:00
|
|
|
if (dns_request_return == GETDNS_RETURN_BAD_DOMAIN_NAME)
|
|
|
|
{
|
2013-12-11 16:55:35 -06:00
|
|
|
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);
|
2013-11-05 10:28:10 -06:00
|
|
|
return(GETDNS_RETURN_GENERIC_ERROR);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Be sure the search returned something */
|
2013-12-11 16:55:35 -06:00
|
|
|
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"
|
2013-11-05 10:28:10 -06:00
|
|
|
{
|
2013-12-11 16:55:35 -06:00
|
|
|
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);
|
2013-11-05 10:28:10 -06:00
|
|
|
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
|
2013-12-11 16:55:35 -06:00
|
|
|
size_t num_addresses;
|
|
|
|
this_ret = getdns_list_get_length(just_the_addresses_ptr, &num_addresses); // Ignore any error
|
2013-11-05 10:28:10 -06:00
|
|
|
/* Go through each record */
|
2013-12-11 16:55:35 -06:00
|
|
|
for ( size_t rec_count = 0; rec_count < num_addresses; ++rec_count )
|
2013-11-05 10:28:10 -06:00
|
|
|
{
|
|
|
|
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
|
2013-12-11 16:55:35 -06:00
|
|
|
char *this_address_str = getdns_display_ip_address(this_address_data);
|
|
|
|
printf("The address is %s\n", this_address_str);
|
|
|
|
free(this_address_str);
|
2013-11-05 10:28:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Clean up */
|
2013-11-13 08:11:59 -06:00
|
|
|
getdns_dict_destroy(this_response);
|
2013-12-11 16:55:35 -06:00
|
|
|
getdns_dict_destroy(this_extensions);
|
|
|
|
getdns_context_destroy(this_context);
|
2013-11-05 10:28:10 -06:00
|
|
|
/* Assuming we get here, leave gracefully */
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|