mirror of https://github.com/getdnsapi/getdns.git
cleaned up example, added error code checks
This commit is contained in:
parent
adcee707ac
commit
16e58b28a3
|
@ -3,7 +3,7 @@
|
|||
* \file context.c
|
||||
* @brief getdns context management functions
|
||||
*
|
||||
* Originally taken from the getdns API description pseudo implementation.
|
||||
* Declarations taken from the getdns API description pseudo implementation.
|
||||
*
|
||||
*/
|
||||
|
||||
|
@ -192,6 +192,7 @@ create_from_ldns_list(struct getdns_context *context, ldns_rdf ** ldns_list,
|
|||
return result;
|
||||
}
|
||||
|
||||
/*---------------------------------------- set_os_defaults */
|
||||
static getdns_return_t
|
||||
set_os_defaults(struct getdns_context *context)
|
||||
{
|
||||
|
@ -213,8 +214,9 @@ set_os_defaults(struct getdns_context *context)
|
|||
}
|
||||
/** cleanup **/
|
||||
ldns_resolver_deep_free(lr);
|
||||
|
||||
return GETDNS_RETURN_GOOD;
|
||||
}
|
||||
} /* set_os_defaults */
|
||||
|
||||
static int
|
||||
transaction_id_cmp(const void *id1, const void *id2)
|
||||
|
@ -323,7 +325,7 @@ getdns_context_create_with_extended_memory_functions(
|
|||
GETDNS_CONTEXT_UDP_FIRST_AND_FALL_BACK_TO_TCP);
|
||||
|
||||
return GETDNS_RETURN_GOOD;
|
||||
} /* getdns_context_create */
|
||||
} /* getdns_context_create */
|
||||
|
||||
/*
|
||||
* getdns_context_create
|
||||
|
|
|
@ -1,3 +1,39 @@
|
|||
/**
|
||||
*
|
||||
* \file example-simple-answers.c
|
||||
* @brief example using getdns to resolve a simple query
|
||||
*
|
||||
* Originally taken from the getdns API description pseudo implementation.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2013, Versign, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of the <organization> nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -14,62 +50,118 @@ void this_callbackfn(struct getdns_context *this_context,
|
|||
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 */
|
||||
getdns_return_t this_ret;
|
||||
uint32_t this_error;
|
||||
size_t num_addresses;
|
||||
struct getdns_list *just_the_addresses_ptr;
|
||||
struct getdns_dict *this_address;
|
||||
struct getdns_bindata *this_address_data;
|
||||
size_t rec_count;
|
||||
char *this_address_str;
|
||||
|
||||
UNUSED_PARAM(this_userarg);
|
||||
UNUSED_PARAM(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;
|
||||
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"
|
||||
|
||||
this_ret = getdns_dict_get_int(this_response, "status", &this_error);
|
||||
if (this_ret != GETDNS_RETURN_GOOD)
|
||||
{
|
||||
fprintf(stderr, "The dictionary does not contain \"status\" (this shouldn't have happened). Exiting\n");
|
||||
getdns_dict_destroy(this_response);
|
||||
return;
|
||||
}
|
||||
|
||||
if (this_error != GETDNS_RESPSTATUS_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"
|
||||
if (this_ret != GETDNS_RETURN_GOOD)
|
||||
{
|
||||
fprintf(stderr, "Trying to get the answers failed: %d\n", this_ret);
|
||||
fprintf(stderr, "The dict does not contain \"just_address_answers\" (this shouldn't have happened), and returned %d. Exiting.\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 )
|
||||
|
||||
this_ret = getdns_list_get_length(just_the_addresses_ptr, &num_addresses);
|
||||
if (this_ret != GETDNS_RETURN_GOOD)
|
||||
{
|
||||
struct getdns_dict * this_address;
|
||||
this_ret = getdns_list_get_dict(just_the_addresses_ptr, rec_count, &this_address); // Ignore any error
|
||||
fprintf(stderr, "The address list is invalid (this shouldn't have happened). Exiting\n");
|
||||
getdns_dict_destroy(this_response);
|
||||
return;
|
||||
}
|
||||
|
||||
if (num_addresses == 0)
|
||||
fprintf(stderr, "The address list has 0 records. Exiting\n");
|
||||
|
||||
/* Go through each record */
|
||||
for (rec_count = 0; rec_count < num_addresses; ++rec_count)
|
||||
{
|
||||
this_ret = getdns_list_get_dict(just_the_addresses_ptr, rec_count, &this_address);
|
||||
if(this_ret != GETDNS_RETURN_GOOD)
|
||||
{
|
||||
fprintf(stderr, "Record %d is invalid (this shouldn't have happened). skipping.\n", (int) rec_count);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* 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);
|
||||
this_ret = getdns_dict_get_bindata(this_address, "address_data", &this_address_data);
|
||||
if(this_ret != GETDNS_RETURN_GOOD)
|
||||
{
|
||||
fprintf(stderr, "Record %d does not contain \"address_data\" (this shouldn't happen), skipping\n", (int) rec_count);
|
||||
}
|
||||
else
|
||||
{
|
||||
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()
|
||||
getdns_dict_destroy(this_response);
|
||||
} /* this_callbackfn */
|
||||
|
||||
/*---------------------------------------- main */
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
/* Create the DNS context for this call */
|
||||
char *this_name = "www.example.com";
|
||||
char *this_userarg = "somestring";
|
||||
int dispatch_return;
|
||||
int exitval = EXIT_SUCCESS;
|
||||
struct getdns_context *this_context = NULL;
|
||||
getdns_return_t context_create_return = getdns_context_create(&this_context, 1);
|
||||
struct event_base *this_event_base;
|
||||
getdns_return_t dns_request_return;
|
||||
getdns_transaction_t this_transaction_id;
|
||||
getdns_return_t context_create_return;
|
||||
|
||||
if(argc > 1)
|
||||
this_name = argv[1];
|
||||
|
||||
printf("resolving %s\n", this_name);
|
||||
|
||||
/* Create the DNS context for this call, use OS configs such as resolv.conf */
|
||||
|
||||
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)
|
||||
{
|
||||
|
@ -77,32 +169,37 @@ int main()
|
|||
getdns_context_destroy(this_context);
|
||||
return(GETDNS_RETURN_GENERIC_ERROR);
|
||||
}
|
||||
(void)getdns_extension_set_libevent_base(this_context, this_event_base);
|
||||
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;
|
||||
|
||||
this_transaction_id = 0;
|
||||
|
||||
/* Make the call */
|
||||
getdns_return_t dns_request_return = getdns_address(this_context, this_name,
|
||||
|
||||
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);
|
||||
exitval = GETDNS_RETURN_GENERIC_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Call the event loop */
|
||||
int dispatch_return = event_base_dispatch(this_event_base);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return exitval;
|
||||
} /* main */
|
||||
|
||||
/* example-simple-answers.c */
|
||||
|
|
Loading…
Reference in New Issue