Simple example rewritten

This commit is contained in:
Willem Toorop 2015-10-06 11:57:18 +02:00
parent 3aca772220
commit 51bfa9c86a
4 changed files with 237 additions and 8 deletions

View File

@ -48,9 +48,9 @@ LDFLAGS=@LDFLAGS@ -L../../src
LDLIBS=../../src/libgetdns.la @LIBS@ LDLIBS=../../src/libgetdns.la @LIBS@
OBJS=example-all-functions.lo example-simple-answers.lo example-tree.lo example-synchronous.lo example-reverse.lo synchronous-json-pointer.lo synchronous-concise.lo OBJS=example-all-functions.lo example-simple-answers.lo example-tree.lo example-synchronous.lo example-reverse.lo synchronous-json-pointer.lo simple-json-pointer.lo
PROGRAMS=example-all-functions example-synchronous example-simple-answers example-tree example-reverse synchronous-json-pointer synchronous-concise PROGRAMS=example-all-functions example-synchronous example-simple-answers example-tree example-reverse synchronous-json-pointer simple-json-pointer
.SUFFIXES: .c .o .a .lo .h .SUFFIXES: .c .o .a .lo .h
@ -77,8 +77,8 @@ example-synchronous: example-synchronous.lo
synchronous-json-pointer: synchronous-json-pointer.lo synchronous-json-pointer: synchronous-json-pointer.lo
$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ synchronous-json-pointer.lo $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ synchronous-json-pointer.lo
synchronous-concise: synchronous-concise.lo simple-json-pointer: simple-json-pointer.lo
$(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ synchronous-concise.lo $(LIBTOOL) --tag=CC --mode=link $(CC) $(CFLAGS) $(LDFLAGS) $(LDLIBS) $(EXTENSION_LIBEVENT_LIB) $(EXTENSION_LIBEVENT_LDFLAGS) $(EXTENSION_LIBEVENT_EXT_LIBS) -o $@ simple-json-pointer.lo
$(EXTENSION_LIBEVENT_LIB): $(EXTENSION_LIBEVENT_LIB):
@echo "***" @echo "***"
@ -166,6 +166,12 @@ example-synchronous.lo example-synchronous.o: $(srcdir)/example-synchronous.c $(
example-tree.lo example-tree.o: $(srcdir)/example-tree.c $(srcdir)/getdns_libevent.h ../../src/config.h \ example-tree.lo example-tree.o: $(srcdir)/example-tree.c $(srcdir)/getdns_libevent.h ../../src/config.h \
../../src/getdns/getdns.h $(srcdir)/../../src/getdns/getdns_ext_libevent.h \ ../../src/getdns/getdns.h $(srcdir)/../../src/getdns/getdns_ext_libevent.h \
$(srcdir)/../../src/getdns/getdns_extra.h $(srcdir)/../../src/getdns/getdns_extra.h
simple-concise.lo simple-concise.o: $(srcdir)/simple-concise.c $(srcdir)/getdns_libevent.h ../../src/config.h \
../../src/getdns/getdns.h $(srcdir)/../../src/getdns/getdns_ext_libevent.h \
$(srcdir)/../../src/getdns/getdns_extra.h
simple-json-pointer.lo simple-json-pointer.o: $(srcdir)/simple-json-pointer.c $(srcdir)/getdns_libevent.h \
../../src/config.h ../../src/getdns/getdns.h \
$(srcdir)/../../src/getdns/getdns_ext_libevent.h $(srcdir)/../../src/getdns/getdns_extra.h
synchronous-concise.lo synchronous-concise.o: $(srcdir)/synchronous-concise.c $(srcdir)/getdns_core_only.h \ synchronous-concise.lo synchronous-concise.o: $(srcdir)/synchronous-concise.c $(srcdir)/getdns_core_only.h \
../../src/getdns/getdns.h ../../src/getdns/getdns.h
synchronous-json-pointer.lo synchronous-json-pointer.o: $(srcdir)/synchronous-json-pointer.c $(srcdir)/getdns_core_only.h \ synchronous-json-pointer.lo synchronous-json-pointer.o: $(srcdir)/synchronous-json-pointer.c $(srcdir)/getdns_core_only.h \

View File

@ -0,0 +1,113 @@
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <getdns_libevent.h>
/* Set up the callback function, which will also do the processing of the results */
void callback(getdns_context *context,
getdns_callback_type_t callback_type,
getdns_dict *response,
void *userarg,
getdns_transaction_t transaction_id)
{
getdns_return_t r; /* Holder for all function returns */
uint32_t status;
getdns_list *just_address_answers;
size_t length, i;
printf( "Callback for query \"%s\" with request ID %"PRIu64".\n"
, (char *)userarg, transaction_id );
switch(callback_type) {
case GETDNS_CALLBACK_CANCEL:
printf("Transaction with ID %"PRIu64" was cancelled.\n", transaction_id);
return;
case GETDNS_CALLBACK_TIMEOUT:
printf("Transaction with ID %"PRIu64" timed out.\n", transaction_id);
return;
case GETDNS_CALLBACK_ERROR:
printf("An error occurred for transaction ID %"PRIu64".\n", transaction_id);
return;
default: break;
}
assert( callback_type == GETDNS_CALLBACK_COMPLETE );
if ((r = getdns_dict_get_int(response, "status", &status)))
fprintf(stderr, "Could not get \"status\" from reponse");
else if (status != GETDNS_RESPSTATUS_GOOD)
fprintf(stderr, "The search had no results, and a return value of %"PRIu32".\n", status);
else if ((r = getdns_dict_get_list(response, "just_address_answers", &just_address_answers)))
fprintf(stderr, "Could not get \"just_address_answers\" from reponse");
else if ((r = getdns_list_get_length(just_address_answers, &length)))
fprintf(stderr, "Could not get just_address_answers\' length");
else for (i = 0; i < length && r == GETDNS_RETURN_GOOD; i++) {
getdns_dict *address;
getdns_bindata *address_data;
char *address_str;
if ((r = getdns_list_get_dict(just_address_answers, i, &address)))
fprintf(stderr, "Could not get address %zu from just_address_answers", i);
else if ((r = getdns_dict_get_bindata(address, "address_data", &address_data)))
fprintf(stderr, "Could not get \"address_data\" from address");
else if (!(address_str = getdns_display_ip_address(address_data))) {
fprintf(stderr, "Could not convert address to string");
r = GETDNS_RETURN_MEMORY_ERROR;
}
else {
printf("The address is %s\n", address_str);
free(address_str);
}
}
if (r) {
assert( r != GETDNS_RETURN_GOOD );
fprintf(stderr, ": %d\n", r);
}
getdns_dict_destroy(response);
}
int main()
{
getdns_return_t r; /* Holder for all function returns */
getdns_context *context = NULL;
struct event_base *event_base = NULL;
getdns_dict *extensions = NULL;
char *query_name = "www.example.com";
/* Could add things here to help identify this call */
char *userarg = query_name;
getdns_transaction_t transaction_id;
if ((r = getdns_context_create(&context, 1)))
fprintf(stderr, "Trying to create the context failed");
else if (!(event_base = event_base_new()))
fprintf(stderr, "Trying to create the event base failed.\n");
else if ((r = getdns_extension_set_libevent_base(context, event_base)))
fprintf(stderr, "Setting the event base failed");
else if ((r = getdns_address( context, "www.example.com", extensions
, userarg, &transaction_id, callback)))
fprintf(stderr, "Error scheduling asynchronous request");
else {
printf("Request with transaction ID %"PRIu64" scheduled.\n", transaction_id);
if (event_base_dispatch(event_base) < 0)
fprintf(stderr, "Error dispatching events\n");
}
/* Clean up */
if (event_base)
event_base_free(event_base);
if (context)
getdns_context_destroy(context);
/* Assuming we get here, leave gracefully */
exit(EXIT_SUCCESS);
}

View File

@ -0,0 +1,107 @@
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <getdns_libevent.h>
/* Set up the callback function, which will also do the processing of the results */
void callback(getdns_context *context,
getdns_callback_type_t callback_type,
getdns_dict *response,
void *userarg,
getdns_transaction_t transaction_id)
{
getdns_return_t r; /* Holder for all function returns */
uint32_t status;
getdns_bindata *address_data;
char *first = NULL, *second = NULL;
printf( "Callback for query \"%s\" with request ID %"PRIu64".\n"
, (char *)userarg, transaction_id );
switch(callback_type) {
case GETDNS_CALLBACK_CANCEL:
printf("Transaction with ID %"PRIu64" was cancelled.\n", transaction_id);
return;
case GETDNS_CALLBACK_TIMEOUT:
printf("Transaction with ID %"PRIu64" timed out.\n", transaction_id);
return;
case GETDNS_CALLBACK_ERROR:
printf("An error occurred for transaction ID %"PRIu64".\n", transaction_id);
return;
default: break;
}
assert( callback_type == GETDNS_CALLBACK_COMPLETE );
if ((r = getdns_dict_get_int(response, "status", &status)))
fprintf(stderr, "Could not get \"status\" from reponse");
else if (status != GETDNS_RESPSTATUS_GOOD)
fprintf(stderr, "The search had no results, and a return value of %"PRIu32".\n", status);
else if ((r = getdns_dict_get_bindata(response, "/just_address_answers/0/address_data", &address_data)))
fprintf(stderr, "Could not get first address");
else if (!(first = getdns_display_ip_address(address_data)))
fprintf(stderr, "Could not convert first address to string\n");
else if ((r = getdns_dict_get_bindata(response, "/just_address_answers/1/address_data", &address_data)))
fprintf(stderr, "Could not get second address");
else if (!(second = getdns_display_ip_address(address_data)))
fprintf(stderr, "Could not convert second address to string\n");
if (first) {
printf("The address is %s\n", first);
free(first);
}
if (second) {
printf("The address is %s\n", second);
free(second);
}
if (r) {
assert( r != GETDNS_RETURN_GOOD );
fprintf(stderr, ": %d\n", r);
}
getdns_dict_destroy(response);
}
int main()
{
getdns_return_t r; /* Holder for all function returns */
getdns_context *context = NULL;
struct event_base *event_base = NULL;
getdns_dict *extensions = NULL;
char *query_name = "www.example.com";
/* Could add things here to help identify this call */
char *userarg = query_name;
getdns_transaction_t transaction_id;
if ((r = getdns_context_create(&context, 1)))
fprintf(stderr, "Trying to create the context failed");
else if (!(event_base = event_base_new()))
fprintf(stderr, "Trying to create the event base failed.\n");
else if ((r = getdns_extension_set_libevent_base(context, event_base)))
fprintf(stderr, "Setting the event base failed");
else if ((r = getdns_address( context, "www.example.com", extensions
, userarg, &transaction_id, callback)))
fprintf(stderr, "Error scheduling asynchronous request");
else {
printf("Request with transaction ID %"PRIu64" scheduled.\n", transaction_id);
if (event_base_dispatch(event_base) < 0)
fprintf(stderr, "Error dispatching events\n");
}
/* Clean up */
if (event_base)
event_base_free(event_base);
if (context)
getdns_context_destroy(context);
/* Assuming we get here, leave gracefully */
exit(EXIT_SUCCESS);
}

View File

@ -1,9 +1,10 @@
#include <stdio.h> #include <stdio.h>
#include <assert.h>
#include <getdns_core_only.h> #include <getdns_core_only.h>
int main() int main()
{ {
getdns_return_t r; getdns_return_t r; /* Holder for all function returns */
getdns_context *context = NULL; getdns_context *context = NULL;
getdns_dict *response = NULL; getdns_dict *response = NULL;
getdns_bindata *address_data; getdns_bindata *address_data;
@ -43,11 +44,13 @@ int main()
if (context) if (context)
getdns_context_destroy(context); getdns_context_destroy(context);
/* Assuming we get here, leave gracefully */
if (r) { if (r) {
assert( r != GETDNS_RETURN_GOOD );
fprintf(stderr, ": %d\n", r); fprintf(stderr, ": %d\n", r);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
else /* Assuming we get here, leave gracefully */
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }