From 67a7cb179e5bf1caacbb5261cf83aa4955dfaa20 Mon Sep 17 00:00:00 2001
From: Willem Toorop
Date: Fri, 6 Dec 2013 14:41:40 +0100
Subject: [PATCH] [API 0.367 again] getdns_dict_remove_name() added
Paul added getdns_dict_remove_name() and changed the doc for the adders to be more sane.
---
spec/example-all-functions.c | 1 +
spec/example-reverse.c | 105 +++++++++++++++++++++++++++++++++++
spec/getdns_core_only.c | 3 +
spec/getdns_core_only.h | 3 +-
spec/index.html | 25 ++++++---
5 files changed, 127 insertions(+), 10 deletions(-)
create mode 100644 spec/example-reverse.c
diff --git a/spec/example-all-functions.c b/spec/example-all-functions.c
index 1e5d0852..5adab816 100644
--- a/spec/example-all-functions.c
+++ b/spec/example-all-functions.c
@@ -203,6 +203,7 @@ retregular = getdns_dict_set_dict(dictarg, charstararg, dictarg);
retregular = getdns_dict_set_list(dictarg, charstararg, listarg);
retregular = getdns_dict_set_bindata(dictarg, charstararg, bindataarg);
retregular = getdns_dict_set_int(dictarg, charstararg, uint32arg);
+retregular = getdns_dict_remove_name(dictarg, charstararg);
retcharstar = getdns_convert_fqdn_to_dns_name(
charstararg
diff --git a/spec/example-reverse.c b/spec/example-reverse.c
new file mode 100644
index 00000000..795a8bae
--- /dev/null
+++ b/spec/example-reverse.c
@@ -0,0 +1,105 @@
+#include
+#include
+#include
+#include
+#include
+#include
+
+#define UNUSED_PARAM(x) ((void)(x))
+
+/* Set up the callback function, which will also do the processing of the results */
+void this_callbackfn(struct getdns_context *this_context,
+ uint16_t this_callback_type,
+ struct getdns_dict *this_response,
+ 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 */
+ if (this_callback_type == GETDNS_CALLBACK_COMPLETE) /* This is a callback with data */
+ {
+ /* Be sure the search returned something */
+ uint32_t * this_error = NULL;
+ 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"
+ {
+ fprintf(stderr, "The search had no results, and a return value of %d. Exiting.", *this_error);
+ 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"
+ {
+ fprintf(stderr, "Trying to get the answers failed: %d", this_ret);
+ return;
+ }
+ size_t * num_addresses_ptr = NULL;
+ this_ret = getdns_list_get_length(just_the_addresses_ptr, num_addresses_ptr); // Ignore any error
+ /* Go through each record */
+ for ( size_t rec_count = 0; rec_count <= *num_addresses_ptr; ++rec_count )
+ {
+ 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
+ printf("The address is %s", getdns_display_ip_address(this_address_data));
+ }
+ }
+ else if (this_callback_type == GETDNS_CALLBACK_CANCEL)
+ fprintf(stderr, "The callback with ID %"PRIu64" was cancelled. Exiting.", this_transaction_id);
+ else
+ fprintf(stderr, "The callback got a callback_type of %d. Exiting.", this_callback_type);
+}
+
+int main()
+{
+ /* Create the DNS context for this call */
+ struct getdns_context *this_context = NULL;
+ getdns_return_t 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)
+ {
+ fprintf(stderr, "Trying to create the event base failed.");
+ return(GETDNS_RETURN_GENERIC_ERROR);
+ }
+ (void)getdns_extension_set_libevent_base(this_context, this_event_base);
+ /* Set up the getdns call */
+ struct getdns_dict * this_addr_to_look_up = getdns_dict_create();
+ // TODO: check the return value above
+ struct getdns_bindata * this_type = { 4, 0x49507634 };
+ getdns_return_t this_ret = getdns_dict_set_bindata(this_addr_to_look_up, "address_type", this_type);
+ struct getdns_bindata * this_ipv4_addr = { 4, 0x08080808 };
+ 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
+ getdns_transaction_t this_transaction_id = 0;
+
+ /* Make the call */
+ getdns_return_t dns_request_return = getdns_hostname(this_context, this_addr_to_look_up,
+ NULL, this_userarg, &this_transaction_id, this_callbackfn);
+ if (dns_request_return == GETDNS_RETURN_BAD_DOMAIN_NAME)
+ {
+ fprintf(stderr, "A bad IP address was used: %s. Exiting.", this_ipv4_addr);
+ return(GETDNS_RETURN_GENERIC_ERROR);
+ }
+ else
+ {
+ /* Call the event loop */
+ int dispatch_return = event_base_dispatch(this_event_base);
+ UNUSED_PARAM(dispatch_return);
+ // TODO: check the return value above
+ }
+ /* Clean up */
+ getdns_dict_destroy(this_addr_to_look_up);
+ getdns_context_destroy(this_context);
+ /* Assuming we get here, leave gracefully */
+ exit(EXIT_SUCCESS);
+}
diff --git a/spec/getdns_core_only.c b/spec/getdns_core_only.c
index 74a861d7..d6819325 100644
--- a/spec/getdns_core_only.c
+++ b/spec/getdns_core_only.c
@@ -266,6 +266,9 @@ getdns_return_t getdns_dict_set_bindata(struct getdns_dict *this_dict, char *nam
getdns_return_t getdns_dict_set_int(struct getdns_dict *this_dict, char *name, uint32_t child_uint32)
{ UNUSED_PARAM(this_dict); UNUSED_PARAM(name); UNUSED_PARAM(child_uint32); return GETDNS_RETURN_GOOD; }
+getdns_return_t getdns_dict_remove_name(struct getdns_dict *this_dict, char *name)
+{ UNUSED_PARAM(this_dict); UNUSED_PARAM(name); return GETDNS_RETURN_GOOD; }
+
char *
getdns_convert_dns_name_to_fqdn(
char *name_from_dns_response
diff --git a/spec/getdns_core_only.h b/spec/getdns_core_only.h
index 7407c6a2..f16d07de 100644
--- a/spec/getdns_core_only.h
+++ b/spec/getdns_core_only.h
@@ -1,4 +1,4 @@
-/* Created at 2013-12-04-16-47-24*/
+/* Created at 2013-12-06-14-39-56*/
#ifndef GETDNS_H
#define GETDNS_H
@@ -320,6 +320,7 @@ getdns_return_t getdns_dict_set_dict(struct getdns_dict *this_dict, char *name,
getdns_return_t getdns_dict_set_list(struct getdns_dict *this_dict, char *name, struct getdns_list *child_list);
getdns_return_t getdns_dict_set_bindata(struct getdns_dict *this_dict, char *name, struct getdns_bindata *child_bindata);
getdns_return_t getdns_dict_set_int(struct getdns_dict *this_dict, char *name, uint32_t child_uint32);
+getdns_return_t getdns_dict_remove_name(struct getdns_dict *this_dict, char *name);
/* Callback arguments */
typedef void (*getdns_callback_t)(
diff --git a/spec/index.html b/spec/index.html
index cdc552c5..d36b4b07 100644
--- a/spec/index.html
+++ b/spec/index.html
@@ -97,7 +97,7 @@ tr.code { font-family: monospace }
Description of the getdns
API
Paul Hoffman, Editor
-Document version: "getdns November 2013"
+Document version: "getdns December 2013"
This document describes a modern asynchronous DNS API. This new API is intended to be useful to
application developers and operating system distributors as a way of making
@@ -602,13 +602,20 @@ getdns_return_t getdns_dict_set_dict(struct getdns_dict *this_dict, char *name,
getdns_return_t getdns_dict_set_list(struct getdns_dict *this_dict, char *name, struct getdns_list *child_list);
getdns_return_t getdns_dict_set_bindata(struct getdns_dict *this_dict, char *name, struct getdns_bindata *child_bindata);
getdns_return_t getdns_dict_set_int(struct getdns_dict *this_dict, char *name, uint32_t child_uint32);
+getdns_return_t getdns_dict_remove_name(struct getdns_dict *this_dict, char *name);
+
Lists are extended with the getdns_list_set_
calls with the index
set to the
+size of the list (such as 0 for an empty list). Dicts are extended with the getdns_dict_set_
calls
+with the name
set to a name that does not yet exist. Name-value pairs are removed with
+getdns_dict_remove_name()
.
+
These helper setter functions return GETDNS_RETURN_GOOD
if the call is successful.
-The list functions will return GETDNS_RETURN_NO_SUCH_LIST_ITEM
if the index argument is
-out of range; the dict functions will return GETDNS_RETURN_NO_SUCH_DICT_NAME
if the name
-argument doesn't exist in the dict. The functions also return GETDNS_RETURN_WRONG_TYPE_REQUESTED
-if the requested data type doesn't match the contents of the indexed argument or name.
+The functions return GETDNS_RETURN_WRONG_TYPE_REQUESTED
if the requested data type
+doesn't match the contents of the indexed argument or name. The list functions will return
+GETDNS_RETURN_NO_SUCH_LIST_ITEM
if the index argument is higher than the length of the
+list. getdns_dict_remove_name()
will return
+GETDNS_RETURN_NO_SUCH_DICT_NAME
if the name argument doesn't exist in the dict.
@@ -2089,7 +2096,7 @@ getdns_context_set_edns_do_bit(
The value is between 0 and 1; the default
is 0.
-8.9 Context use of custom memory management functions
+8.9 Context Use of Custom Memory Management Functions
getdns_return_t
@@ -2101,7 +2108,7 @@ getdns_context_set_memory_functions(
);
The given memory management functions will be used for creating the response dicts.
The response dicts inherit the custom memory management functions from the context and will deallocate themselves (and their members) with the custom deallocator.
-By default the system malloc, realloc and free are used.
+By default, the system malloc, realloc, and free are used.
getdns_return_t
@@ -2112,8 +2119,8 @@ getdns_context_set_extended_memory_functions(
void *(*realloc)(void *userarg, void *ptr, size_t sz),
void (*free)(void *userarg, void *ptr)
);
-The given extended memory management functions will be used for creating the response dicts.
-The value of userarg
argument will be passed to the custom malloc, realloc
and free
.
+The given extended memory management functions will be used for creating the response dicts.
+The value of userarg
argument will be passed to the custom malloc, realloc
, and free
.
The response dicts inherit the custom memory management functions and the value for userarg
from the context and will deallocate themselves (and their members) with the custom deallocator.