Match examples with current spec [API 0.402]

This commit is contained in:
Willem Toorop 2014-02-21 12:55:34 +01:00
parent 7c30f3710a
commit 6e8ace78ff
5 changed files with 90 additions and 346 deletions

View File

@ -1,29 +1,3 @@
/*
* Copyright (c) 2013, NLNet Labs, 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 <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <inttypes.h> #include <inttypes.h>
@ -40,6 +14,7 @@ char * retcharstar;
/* The args */ /* The args */
int boolarg; int boolarg;
char * charstararg; char * charstararg;
char ** charstarptrarg;
getdns_callback_t callbackarg; getdns_callback_t callbackarg;
uint16_t regulararg; uint16_t regulararg;
uint16_t *regularptrarg; uint16_t *regularptrarg;
@ -52,16 +27,16 @@ getdns_transport_t transportarg;
getdns_append_name_t appendnamearg; getdns_append_name_t appendnamearg;
getdns_data_type * datatypeptrarg; getdns_data_type * datatypeptrarg;
struct getdns_bindata ** bindataptrarg; getdns_bindata ** bindataptrarg;
struct getdns_dict * dictarg; getdns_dict * dictarg;
struct getdns_bindata * bindataarg; getdns_bindata * bindataarg;
struct getdns_list * listarg; getdns_list * listarg;
struct getdns_dict ** dictptrarg; getdns_dict ** dictptrarg;
struct getdns_list ** listptrarg; getdns_list ** listptrarg;
size_t sizetarg; size_t sizetarg;
size_t * sizetptrarg; size_t * sizetptrarg;
struct getdns_context *contextarg = NULL; getdns_context *contextarg = NULL;
uint8_t uint8arg; uint8_t uint8arg;
uint16_t uint16arg; uint16_t uint16arg;
uint32_t uint32arg; uint32_t uint32arg;
@ -80,7 +55,7 @@ void * extendedreallocfunctionarg(void* userarg, void* foo, size_t bar)
{UNUSED_PARAM(userarg); UNUSED_PARAM(foo); UNUSED_PARAM(bar); return NULL; } {UNUSED_PARAM(userarg); UNUSED_PARAM(foo); UNUSED_PARAM(bar); return NULL; }
void extendeddeallocfunctionarg(void* userarg, void* foo) void extendeddeallocfunctionarg(void* userarg, void* foo)
{UNUSED_PARAM(userarg); UNUSED_PARAM(foo);} {UNUSED_PARAM(userarg); UNUSED_PARAM(foo);}
void setcallbackfunctionarg(struct getdns_context *foo1, getdns_context_code_t foo2) void setcallbackfunctionarg(getdns_context *foo1, getdns_context_code_t foo2)
{UNUSED_PARAM(foo1);UNUSED_PARAM(foo2);} {UNUSED_PARAM(foo1);UNUSED_PARAM(foo2);}
int main() int main()
@ -236,12 +211,14 @@ retregular = getdns_dict_set_bindata(dictarg, charstararg, bindataarg);
retregular = getdns_dict_set_int(dictarg, charstararg, uint32arg); retregular = getdns_dict_set_int(dictarg, charstararg, uint32arg);
retregular = getdns_dict_remove_name(dictarg, charstararg); retregular = getdns_dict_remove_name(dictarg, charstararg);
retcharstar = getdns_convert_fqdn_to_dns_name( retregular = getdns_convert_fqdn_to_dns_name(
charstararg charstararg,
bindataptrarg
); );
retcharstar = getdns_convert_dns_name_to_fqdn( retregular = getdns_convert_dns_name_to_fqdn(
charstararg bindataarg,
charstarptrarg
); );
retcharstar = getdns_convert_ulabel_to_alabel( retcharstar = getdns_convert_ulabel_to_alabel(

View File

@ -1,35 +1,3 @@
/*
* Copyright (c) 2013, NLNet Labs, 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 "config.h"
#ifdef HAVE_EVENT2_EVENT_H
# include <event2/event.h>
#else
# include <event.h>
#endif
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
@ -40,9 +8,9 @@
#define UNUSED_PARAM(x) ((void)(x)) #define UNUSED_PARAM(x) ((void)(x))
/* Set up the callback function, which will also do the processing of the results */ /* Set up the callback function, which will also do the processing of the results */
void this_callbackfn(struct getdns_context *this_context, void this_callbackfn(getdns_context *this_context,
getdns_callback_type_t this_callback_type, getdns_callback_type_t this_callback_type,
struct getdns_dict *this_response, getdns_dict *this_response,
void *this_userarg, void *this_userarg,
getdns_transaction_t this_transaction_id) getdns_transaction_t this_transaction_id)
{ {
@ -61,39 +29,37 @@ void this_callbackfn(struct getdns_context *this_context,
getdns_dict_destroy(this_response); getdns_dict_destroy(this_response);
return; return;
} }
struct getdns_list *replies_tree; getdns_list *replies_tree;
this_ret = getdns_dict_get_list(this_response, "replies_tree", &replies_tree); // Ignore any error this_ret = getdns_dict_get_list(this_response, "replies_tree", &replies_tree); // Ignore any error
size_t num_replies; size_t num_replies;
this_ret = getdns_list_get_length(replies_tree, &num_replies); // Ignore any error this_ret = getdns_list_get_length(replies_tree, &num_replies); // Ignore any error
/* Go through each reply */ /* Go through each reply */
for ( size_t reply_count = 0; reply_count < num_replies; ++reply_count) for ( size_t reply_count = 0; reply_count < num_replies; ++reply_count)
{ {
struct getdns_dict * this_reply; getdns_dict * this_reply;
this_ret = getdns_list_get_dict(replies_tree, reply_count, &this_reply); // Ignore any error this_ret = getdns_list_get_dict(replies_tree, reply_count, &this_reply); // Ignore any error
/* Just print the address */ /* Just print the address */
struct getdns_list* reply_answers; getdns_list* reply_answers;
this_ret = getdns_dict_get_list(this_reply, "answer", &reply_answers); // Ignore any error this_ret = getdns_dict_get_list(this_reply, "answer", &reply_answers); // Ignore any error
size_t num_answers; size_t num_answers;
this_ret = getdns_list_get_length(reply_answers, &num_answers); // Ignore any error this_ret = getdns_list_get_length(reply_answers, &num_answers); // Ignore any error
/* Go through each answer */ /* Go through each answer */
for ( size_t answer_count = 0; answer_count < num_answers; ++answer_count) for ( size_t answer_count = 0; answer_count < num_answers; ++answer_count)
{ {
struct getdns_dict * this_rr; getdns_dict * this_rr;
this_ret = getdns_list_get_dict(reply_answers, answer_count, &this_rr); this_ret = getdns_list_get_dict(reply_answers, answer_count, &this_rr);
/* Get the RDATA type */ /* Get the RDATA type */
uint32_t this_type; uint32_t this_type;
this_ret = getdns_dict_get_int(this_rr, "type", &this_type); // Ignore any error this_ret = getdns_dict_get_int(this_rr, "type", &this_type); // Ignore any error
if (this_type == GETDNS_RRTYPE_PTR) if (this_type == GETDNS_RRTYPE_PTR)
{ {
struct getdns_dict *this_rdata; getdns_dict *this_rdata;
this_ret = getdns_dict_get_dict(this_rr, "rdata", &this_rdata); // Ignore any error this_ret = getdns_dict_get_dict(this_rr, "rdata", &this_rdata); // Ignore any error
struct getdns_bindata * this_dname; getdns_bindata * this_dname;
this_ret = getdns_dict_get_bindata(this_rdata, "rdata_raw", &this_dname); this_ret = getdns_dict_get_bindata(this_rdata, "rdata_raw", &this_dname);
char *this_dname_str = getdns_convert_dns_name_to_fqdn((char *)this_dname->data); char *this_dname_str;
printf("The dname is %s\n", this_dname_str); this_ret = getdns_convert_dns_name_to_fqdn(this_dname, &this_dname_str); // Ignore any error
free(this_dname_str);
} }
} }
@ -109,7 +75,7 @@ void this_callbackfn(struct getdns_context *this_context,
int main() int main()
{ {
/* Create the DNS context for this call */ /* Create the DNS context for this call */
struct getdns_context *this_context = NULL; getdns_context *this_context = NULL;
getdns_return_t context_create_return = getdns_context_create(&this_context, 1); getdns_return_t context_create_return = getdns_context_create(&this_context, 1);
if (context_create_return != GETDNS_RETURN_GOOD) if (context_create_return != GETDNS_RETURN_GOOD)
{ {
@ -127,12 +93,12 @@ int main()
} }
(void)getdns_extension_set_libevent_base(this_context, this_event_base); (void)getdns_extension_set_libevent_base(this_context, this_event_base);
/* Set up the getdns call */ /* Set up the getdns call */
struct getdns_dict * this_addr_to_look_up = getdns_dict_create(); getdns_dict * this_addr_to_look_up = getdns_dict_create();
// TODO: check the return value above // TODO: check the return value above
struct getdns_bindata this_type = { 4, (void *)"IPv4" }; getdns_bindata this_type = { 4, (void *)"IPv4" };
getdns_return_t this_ret = getdns_dict_set_bindata(this_addr_to_look_up, "address_type", &this_type); getdns_return_t this_ret = getdns_dict_set_bindata(this_addr_to_look_up, "address_type", &this_type);
UNUSED_PARAM(this_ret); UNUSED_PARAM(this_ret);
struct getdns_bindata this_ipv4_addr = { 4, (void *)"\x08\x08\x08\x08" }; 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); 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 char* this_userarg = "somestring"; // Could add things here to help identify this call
getdns_transaction_t this_transaction_id = 0; getdns_transaction_t this_transaction_id = 0;

View File

@ -1,212 +1,75 @@
/**
*
* \file example-simple-answers.c
* @brief example using getdns to resolve a simple query
*
* Originally taken from the getdns API description pseudo implementation.
* Modified to more clearly demonstrate some of the API features.
*
*/
/*
* Copyright (c) 2013, NLNet Labs, 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 <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <inttypes.h> #include <inttypes.h>
#include <getdns_libevent.h> #include <getdns_libevent.h>
#define UNUSED_PARAM(x) ((void)(x)) #define UNUSED_PARAM(x) ((void)(x))
/* Set up the callback function, which will also do the processing of the results */ /* Set up the callback function, which will also do the processing of the results */
void this_callbackfn(struct getdns_context *this_context, void this_callbackfn(getdns_context *this_context,
uint16_t this_callback_type, getdns_callback_type_t this_callback_type,
struct getdns_dict *this_response, getdns_dict *this_response,
void *this_userarg, void *this_userarg,
getdns_transaction_t this_transaction_id) getdns_transaction_t this_transaction_id)
{ {
getdns_return_t this_ret; UNUSED_PARAM(this_userarg); /* Not looking at the userarg for this example */
uint32_t this_error; UNUSED_PARAM(this_context); /* Not looking at the context for this example */
size_t num_addresses; getdns_return_t this_ret; /* Holder for all function returns */
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 */ if (this_callback_type == GETDNS_CALLBACK_COMPLETE) /* This is a callback with data */
{ {
/* Be sure the search returned something */ /* Be sure the search returned something */
uint32_t this_error;
this_ret = getdns_dict_get_int(this_response, "status", &this_error); this_ret = getdns_dict_get_int(this_response, "status", &this_error); // Ignore any error
if (this_ret != GETDNS_RETURN_GOOD) if (this_error != GETDNS_RESPSTATUS_GOOD) // If the search didn't 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); 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_response);
return; return;
} }
getdns_list * just_the_addresses_ptr;
this_ret = getdns_dict_get_list(this_response, "just_address_answers", &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) if (this_ret != GETDNS_RETURN_GOOD) // This check is really not needed, but prevents a compiler error under "pedantic"
{ {
fprintf(stderr, "The dict does not contain \"just_address_answers\" (this shouldn't have happened), and returned %d. Exiting.\n", this_ret); fprintf(stderr, "Trying to get the answers failed: %d\n", this_ret);
getdns_dict_destroy(this_response); getdns_dict_destroy(this_response);
return; return;
} }
size_t num_addresses;
this_ret = getdns_list_get_length(just_the_addresses_ptr, &num_addresses); this_ret = getdns_list_get_length(just_the_addresses_ptr, &num_addresses); // Ignore any error
if (this_ret != GETDNS_RETURN_GOOD)
{
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 */ /* Go through each record */
for (rec_count = 0; rec_count < num_addresses; ++rec_count) for ( size_t rec_count = 0; rec_count < num_addresses; ++rec_count )
{ {
this_ret = getdns_list_get_dict(just_the_addresses_ptr, rec_count, &this_address); getdns_dict * this_address;
if(this_ret != GETDNS_RETURN_GOOD) this_ret = getdns_list_get_dict(just_the_addresses_ptr, rec_count, &this_address); // Ignore any error
{
fprintf(stderr, "Record %d is invalid (this shouldn't have happened). skipping.\n", (int) rec_count);
continue;
}
/* Just print the address */ /* Just print the address */
this_ret = getdns_dict_get_bindata(this_address, "address_data", &this_address_data); getdns_bindata * this_address_data;
if(this_ret != GETDNS_RETURN_GOOD) 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);
fprintf(stderr, "Record %d does not contain \"address_data\" (this shouldn't happen), skipping\n", (int) rec_count); printf("The address is %s\n", this_address_str);
} free(this_address_str);
else }
{
this_address_str = getdns_display_ip_address(this_address_data);
printf("The address is %s\n", this_address_str);
free(this_address_str);
}
} // for rec_count
} }
else if (this_callback_type == GETDNS_CALLBACK_CANCEL) else if (this_callback_type == GETDNS_CALLBACK_CANCEL)
fprintf(stderr, "The callback with ID %"PRIu64" was cancelled. Exiting.\n", this_transaction_id); fprintf(stderr, "The callback with ID %"PRIu64" was cancelled. Exiting.\n", this_transaction_id);
else else
fprintf(stderr, "The callback got a callback_type of %d. Exiting.\n", this_callback_type); fprintf(stderr, "The callback got a callback_type of %d. Exiting.\n", this_callback_type);
getdns_dict_destroy(this_response); getdns_dict_destroy(this_response);
} /* this_callbackfn */ }
void int main()
usage(void)
{ {
printf( /* Create the DNS context for this call */
"USAGE: example-simple-answers [-s] [hostname]\n" getdns_context *this_context = NULL;
"\n" getdns_return_t context_create_return = getdns_context_create(&this_context, 1);
"-s act as stub resolver (default is to act as a recursive resolver)\n"
"\n"
"The example program demonstrates the simplest use of the getdns API to\n"
"resolve a hostname to an IP address\n"
"\n");
return;
} /* usage */
/*---------------------------------------- main */
int
main(int argc, char *argv[])
{
char *this_name = "www.example.com";
char *this_userarg = "somestring";
char opt;
int stubonly = 0;
int dispatch_return;
int exitval = EXIT_SUCCESS;
struct getdns_context *this_context = NULL;
struct event_base *this_event_base;
getdns_return_t dns_request_return;
getdns_transaction_t this_transaction_id;
getdns_return_t context_create_return;
while((opt = getopt(argc, argv, "?s")) != -1)
{
switch(opt)
{
case 's':
stubonly = 1;
break;
case '?':
default:
usage();
exit(1);
}
}
argc -= optind;
argv += optind;
if(argc > 0)
this_name = argv[0];
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) 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", context_create_return);
return(GETDNS_RETURN_GENERIC_ERROR); return(GETDNS_RETURN_GENERIC_ERROR);
} }
if(stubonly == 1)
{
if(getdns_context_set_resolution_type(this_context, GETDNS_RESOLUTION_STUB)
!= GETDNS_RETURN_GOOD)
{
fprintf(stderr, "Failed to set stub resolver in context (this should never fail). Exiting.\n");
return(GETDNS_RETURN_GENERIC_ERROR);
}
}
/* Create an event base and put it in the context using the unknown function name */ /* Create an event base and put it in the context using the unknown function name */
struct event_base *this_event_base;
this_event_base = event_base_new(); this_event_base = event_base_new();
if (this_event_base == NULL) if (this_event_base == NULL)
{ {
@ -214,36 +77,32 @@ main(int argc, char *argv[])
getdns_context_destroy(this_context); getdns_context_destroy(this_context);
return(GETDNS_RETURN_GENERIC_ERROR); return(GETDNS_RETURN_GENERIC_ERROR);
} }
getdns_extension_set_libevent_base(this_context, this_event_base); (void)getdns_extension_set_libevent_base(this_context, this_event_base);
/* Set up the getdns call */ /* Set up the getdns call */
const char * this_name = "www.example.com";
this_transaction_id = 0; char* this_userarg = "somestring"; // Could add things here to help identify this call
getdns_transaction_t this_transaction_id = 0;
/* Make the call */ /* 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); NULL, this_userarg, &this_transaction_id, this_callbackfn);
if (dns_request_return == GETDNS_RETURN_BAD_DOMAIN_NAME) if (dns_request_return == GETDNS_RETURN_BAD_DOMAIN_NAME)
{ {
fprintf(stderr, "A bad domain name was used: %s. Exiting.\n", this_name); fprintf(stderr, "A bad domain name was used: %s. Exiting.\n", this_name);
exitval = GETDNS_RETURN_GENERIC_ERROR; event_base_free(this_event_base);
getdns_context_destroy(this_context);
return(GETDNS_RETURN_GENERIC_ERROR);
} }
else else
{ {
/* Call the event loop */ /* 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);
if(dispatch_return < 0) // TODO: check the return value above
fprintf(stderr, "event_base_dispatch() failed, returned %d\n", dispatch_return);
} }
/* Clean up */ /* Clean up */
event_base_free(this_event_base); event_base_free(this_event_base);
getdns_context_destroy(this_context); getdns_context_destroy(this_context);
/* Assuming we get here, leave gracefully */
return exitval; exit(EXIT_SUCCESS);
} /* main */ }
/* example-simple-answers.c */

View File

@ -1,29 +1,3 @@
/*
* Copyright (c) 2013, NLNet Labs, 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 <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
@ -35,7 +9,7 @@ int main()
{ {
getdns_return_t this_ret; /* Holder for all function returns */ getdns_return_t this_ret; /* Holder for all function returns */
/* Create the DNS context for this call */ /* Create the DNS context for this call */
struct getdns_context *this_context = NULL; getdns_context *this_context = NULL;
getdns_return_t context_create_return = getdns_context_create(&this_context, 1); getdns_return_t context_create_return = getdns_context_create(&this_context, 1);
if (context_create_return != GETDNS_RETURN_GOOD) if (context_create_return != GETDNS_RETURN_GOOD)
{ {
@ -46,7 +20,7 @@ int main()
const char * this_name = "www.example.com"; const char * this_name = "www.example.com";
uint8_t this_request_type = GETDNS_RRTYPE_A; uint8_t this_request_type = GETDNS_RRTYPE_A;
/* Get the A and AAAA records */ /* Get the A and AAAA records */
struct getdns_dict * this_extensions = getdns_dict_create(); getdns_dict * this_extensions = getdns_dict_create();
this_ret = getdns_dict_set_int(this_extensions, "return_both_v4_and_v6", GETDNS_EXTENSION_TRUE); this_ret = getdns_dict_set_int(this_extensions, "return_both_v4_and_v6", GETDNS_EXTENSION_TRUE);
if (this_ret != GETDNS_RETURN_GOOD) if (this_ret != GETDNS_RETURN_GOOD)
{ {
@ -55,7 +29,7 @@ int main()
getdns_context_destroy(this_context); getdns_context_destroy(this_context);
return(GETDNS_RETURN_GENERIC_ERROR); return(GETDNS_RETURN_GENERIC_ERROR);
} }
struct getdns_dict * this_response = NULL; getdns_dict * this_response = NULL;
/* Make the call */ /* Make the call */
getdns_return_t dns_request_return = getdns_general_sync(this_context, this_name, this_request_type, getdns_return_t dns_request_return = getdns_general_sync(this_context, this_name, this_request_type,
@ -81,17 +55,17 @@ int main()
getdns_context_destroy(this_context); getdns_context_destroy(this_context);
return(GETDNS_RETURN_GENERIC_ERROR); return(GETDNS_RETURN_GENERIC_ERROR);
} }
struct getdns_list * just_the_addresses_ptr; getdns_list * just_the_addresses_ptr;
this_ret = getdns_dict_get_list(this_response, "just_address_answers", &just_the_addresses_ptr); // Ignore any error this_ret = getdns_dict_get_list(this_response, "just_address_answers", &just_the_addresses_ptr); // Ignore any error
size_t num_addresses; size_t num_addresses;
this_ret = getdns_list_get_length(just_the_addresses_ptr, &num_addresses); // Ignore any error this_ret = getdns_list_get_length(just_the_addresses_ptr, &num_addresses); // Ignore any error
/* Go through each record */ /* Go through each record */
for ( size_t rec_count = 0; rec_count < num_addresses; ++rec_count ) for ( size_t rec_count = 0; rec_count < num_addresses; ++rec_count )
{ {
struct getdns_dict * this_address; getdns_dict * this_address;
this_ret = getdns_list_get_dict(just_the_addresses_ptr, rec_count, &this_address); // Ignore any error this_ret = getdns_list_get_dict(just_the_addresses_ptr, rec_count, &this_address); // Ignore any error
/* Just print the address */ /* Just print the address */
struct getdns_bindata * this_address_data; getdns_bindata * this_address_data;
this_ret = getdns_dict_get_bindata(this_address, "address_data", &this_address_data); // Ignore any error 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); char *this_address_str = getdns_display_ip_address(this_address_data);
printf("The address is %s\n", this_address_str); printf("The address is %s\n", this_address_str);

View File

@ -1,35 +1,3 @@
/*
* Copyright (c) 2013, NLNet Labs, 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 "config.h"
#ifdef HAVE_EVENT2_EVENT_H
# include <event2/event.h>
#else
# include <event.h>
#endif
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
@ -40,9 +8,9 @@
#define UNUSED_PARAM(x) ((void)(x)) #define UNUSED_PARAM(x) ((void)(x))
/* Set up the callback function, which will also do the processing of the results */ /* Set up the callback function, which will also do the processing of the results */
void this_callbackfn(struct getdns_context *this_context, void this_callbackfn(getdns_context *this_context,
getdns_callback_type_t this_callback_type, getdns_callback_type_t this_callback_type,
struct getdns_dict *this_response, getdns_dict *this_response,
void *this_userarg, void *this_userarg,
getdns_transaction_t this_transaction_id) getdns_transaction_t this_transaction_id)
{ {
@ -61,7 +29,7 @@ void this_callbackfn(struct getdns_context *this_context,
return; return;
} }
/* Find all the answers returned */ /* Find all the answers returned */
struct getdns_list * these_answers; 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) if (this_ret == GETDNS_RETURN_NO_SUCH_DICT_NAME)
{ {
@ -74,20 +42,20 @@ void this_callbackfn(struct getdns_context *this_context,
/* Go through each answer */ /* Go through each answer */
for ( size_t rec_count = 0; rec_count < num_answers; ++rec_count ) for ( size_t rec_count = 0; rec_count < num_answers; ++rec_count )
{ {
struct getdns_dict * this_record; getdns_dict * this_record;
this_ret = getdns_list_get_dict(these_answers, rec_count, &this_record); // Ignore any error this_ret = getdns_list_get_dict(these_answers, rec_count, &this_record); // Ignore any error
/* Get the answer section */ /* Get the answer section */
struct getdns_list * this_answer; getdns_list * this_answer;
this_ret = getdns_dict_get_list(this_record, "answer", &this_answer); // Ignore any error this_ret = getdns_dict_get_list(this_record, "answer", &this_answer); // Ignore any error
/* Get each RR in the answer section */ /* Get each RR in the answer section */
size_t num_rrs; size_t num_rrs;
this_ret = getdns_list_get_length(this_answer, &num_rrs); this_ret = getdns_list_get_length(this_answer, &num_rrs);
for ( size_t rr_count = 0; rr_count < num_rrs; ++rr_count ) for ( size_t rr_count = 0; rr_count < num_rrs; ++rr_count )
{ {
struct getdns_dict *this_rr = NULL; getdns_dict *this_rr = NULL;
this_ret = getdns_list_get_dict(this_answer, rr_count, &this_rr); // Ignore any error this_ret = getdns_list_get_dict(this_answer, rr_count, &this_rr); // Ignore any error
/* Get the RDATA */ /* Get the RDATA */
struct getdns_dict * this_rdata = NULL; getdns_dict * this_rdata = NULL;
this_ret = getdns_dict_get_dict(this_rr, "rdata", &this_rdata); // Ignore any error this_ret = getdns_dict_get_dict(this_rr, "rdata", &this_rdata); // Ignore any error
/* Get the RDATA type */ /* Get the RDATA type */
uint32_t this_type; uint32_t this_type;
@ -95,7 +63,7 @@ void this_callbackfn(struct getdns_context *this_context,
/* If it is type A or AAAA, print the value */ /* 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; getdns_bindata * this_a_record = NULL;
this_ret = getdns_dict_get_bindata(this_rdata, "ipv4_address", &this_a_record); this_ret = getdns_dict_get_bindata(this_rdata, "ipv4_address", &this_a_record);
if (this_ret == GETDNS_RETURN_NO_SUCH_DICT_NAME) if (this_ret == GETDNS_RETURN_NO_SUCH_DICT_NAME)
{ {
@ -110,7 +78,7 @@ void this_callbackfn(struct getdns_context *this_context,
} }
else if (this_type == GETDNS_RRTYPE_AAAA) else if (this_type == GETDNS_RRTYPE_AAAA)
{ {
struct getdns_bindata * this_aaaa_record = NULL; getdns_bindata * this_aaaa_record = NULL;
this_ret = getdns_dict_get_bindata(this_rdata, "ipv6_address", &this_aaaa_record); this_ret = getdns_dict_get_bindata(this_rdata, "ipv6_address", &this_aaaa_record);
if (this_ret == GETDNS_RETURN_NO_SUCH_DICT_NAME) if (this_ret == GETDNS_RETURN_NO_SUCH_DICT_NAME)
{ {
@ -136,7 +104,7 @@ void this_callbackfn(struct getdns_context *this_context,
int main() int main()
{ {
/* Create the DNS context for this call */ /* Create the DNS context for this call */
struct getdns_context *this_context = NULL; getdns_context *this_context = NULL;
getdns_return_t context_create_return = getdns_context_create(&this_context, 1); getdns_return_t context_create_return = getdns_context_create(&this_context, 1);
if (context_create_return != GETDNS_RETURN_GOOD) if (context_create_return != GETDNS_RETURN_GOOD)
{ {