2013-08-16 14:11:46 -05:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* unit tests for getdns_dict helper routines, these should be used to
|
|
|
|
* perform regression tests, output must be unchanged from canonical output
|
|
|
|
* stored with the sources
|
|
|
|
*/
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2013-11-30 06:53:57 -06:00
|
|
|
#include "config.h"
|
|
|
|
#ifdef HAVE_EVENT2_EVENT_H
|
|
|
|
# include <event2/event.h>
|
|
|
|
#else
|
|
|
|
# include <event.h>
|
|
|
|
#endif
|
2013-08-16 14:11:46 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "testmessages.h"
|
|
|
|
#include <getdns/getdns.h>
|
|
|
|
|
|
|
|
/* Set up the callback function, which will also do the processing of the results */
|
2013-11-05 14:03:44 -06:00
|
|
|
void
|
|
|
|
this_callbackfn(struct getdns_context_t *this_context,
|
|
|
|
uint16_t this_callback_type,
|
|
|
|
struct getdns_dict *this_response,
|
|
|
|
void *this_userarg, getdns_transaction_t this_transaction_id)
|
2013-08-16 14:11:46 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
if (this_callback_type == GETDNS_CALLBACK_COMPLETE) { /* This is a callback with data */
|
|
|
|
char *res = getdns_pretty_print_dict(this_response);
|
2013-11-29 09:24:39 -06:00
|
|
|
fprintf(stdout, "%s\n", res);
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_dict_destroy(this_response);
|
2013-11-08 17:13:49 -06:00
|
|
|
free(res);
|
2013-10-17 18:45:25 -05:00
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
} else if (this_callback_type == GETDNS_CALLBACK_CANCEL)
|
|
|
|
fprintf(stderr,
|
2013-11-12 10:00:19 -06:00
|
|
|
"The callback with ID %llu was cancelled. Exiting.",
|
|
|
|
(unsigned long long)this_transaction_id);
|
2013-08-16 14:11:46 -05:00
|
|
|
else
|
2013-11-05 14:03:44 -06:00
|
|
|
fprintf(stderr,
|
|
|
|
"The callback got a callback_type of %d. Exiting.",
|
|
|
|
this_callback_type);
|
2013-08-16 14:11:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-12-03 03:37:23 -06:00
|
|
|
main(int argc, char** argv)
|
2013-08-16 14:11:46 -05:00
|
|
|
{
|
|
|
|
/* Create the DNS context for this call */
|
|
|
|
struct getdns_context_t *this_context = NULL;
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_return_t context_create_return =
|
|
|
|
getdns_context_create(&this_context, true);
|
|
|
|
if (context_create_return != GETDNS_RETURN_GOOD) {
|
|
|
|
fprintf(stderr, "Trying to create the context failed: %d",
|
|
|
|
context_create_return);
|
|
|
|
return (GETDNS_RETURN_GENERIC_ERROR);
|
2013-08-16 14:11:46 -05:00
|
|
|
}
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_context_set_resolution_type(this_context, GETDNS_CONTEXT_STUB);
|
|
|
|
|
|
|
|
getdns_context_set_timeout(this_context, 5000);
|
|
|
|
/* Create an event base and put it in the context using the unknown function name */
|
2013-08-16 14:11:46 -05:00
|
|
|
struct event_base *this_event_base;
|
|
|
|
this_event_base = event_base_new();
|
2013-11-05 14:03:44 -06:00
|
|
|
if (this_event_base == NULL) {
|
2013-08-16 14:11:46 -05:00
|
|
|
fprintf(stderr, "Trying to create the event base failed.");
|
2013-11-05 14:03:44 -06:00
|
|
|
return (GETDNS_RETURN_GENERIC_ERROR);
|
2013-08-16 14:11:46 -05:00
|
|
|
}
|
2013-11-05 14:03:44 -06:00
|
|
|
(void) getdns_extension_set_libevent_base(this_context,
|
|
|
|
this_event_base);
|
2013-08-16 14:11:46 -05:00
|
|
|
/* Set up the getdns call */
|
2013-12-03 03:37:23 -06:00
|
|
|
const char *this_name = argc > 1 ? argv[1] : "www.google.com";
|
2013-11-05 14:03:44 -06:00
|
|
|
char *this_userarg = "somestring"; // Could add things here to help identify this call
|
2013-08-16 14:11:46 -05:00
|
|
|
getdns_transaction_t this_transaction_id = 0;
|
2013-10-17 18:45:25 -05:00
|
|
|
|
2013-08-16 14:11:46 -05:00
|
|
|
/* Make the call */
|
2013-11-05 14:03:44 -06:00
|
|
|
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);
|
2013-08-16 14:11:46 -05:00
|
|
|
}
|
2013-10-16 17:33:12 -05:00
|
|
|
// dns_request_return = getdns_service(this_context, this_name, NULL, this_userarg, &this_transaction_id,
|
|
|
|
// this_callbackfn);
|
|
|
|
// if (dns_request_return == GETDNS_RETURN_BAD_DOMAIN_NAME)
|
2013-11-05 14:03:44 -06:00
|
|
|
// {
|
|
|
|
// fprintf(stderr, "A bad domain name was used: %s. Exiting.", this_name);
|
|
|
|
// return(GETDNS_RETURN_GENERIC_ERROR);
|
|
|
|
// }
|
|
|
|
else {
|
2013-08-16 14:11:46 -05:00
|
|
|
/* Call the event loop */
|
|
|
|
event_base_dispatch(this_event_base);
|
|
|
|
// TODO: check the return value above
|
|
|
|
}
|
|
|
|
/* Clean up */
|
|
|
|
getdns_context_destroy(this_context);
|
|
|
|
/* Assuming we get here, leave gracefully */
|
|
|
|
exit(EXIT_SUCCESS);
|
2013-11-05 14:03:44 -06:00
|
|
|
} /* main */
|
2013-08-16 14:11:46 -05:00
|
|
|
|
|
|
|
/* example-simple-answers.c */
|