From f8380ff862c497d6f64475fbb64276114a52508e Mon Sep 17 00:00:00 2001 From: Neel Goyal Date: Tue, 5 Nov 2013 15:03:44 -0500 Subject: [PATCH] Ran indent with the indent.pro committed --- .indent.pro | 19 + src/context.c | 1231 +++++++++++++------------- src/context.h | 93 +- src/convert.c | 121 +-- src/dict.c | 901 ++++++++++--------- src/dict.h | 26 +- src/example/example_all_functions.c | 348 +++----- src/example/example_simple_answers.c | 106 ++- src/example/example_synchronous.c | 108 +-- src/example/example_tree.c | 171 ++-- src/general.c | 499 ++++++----- src/general.h | 19 +- src/getdns/getdns.h | 377 ++++---- src/getdns/getdns_error.h | 9 +- src/getdns_error.c | 64 +- src/hostname.c | 61 +- src/list.c | 546 ++++++------ src/list.h | 30 +- src/request-internal.c | 227 ++--- src/service.c | 20 +- src/sync.c | 164 ++-- src/test/testmessages.c | 62 +- src/test/tests_dict.c | 686 +++++++------- src/test/tests_list.c | 549 ++++++------ src/test/tests_stub_async.c | 88 +- src/test/tests_stub_sync.c | 79 +- src/types-internal.h | 119 ++- src/util-internal.c | 921 ++++++++++--------- src/util-internal.h | 18 +- src/validate_dnssec.c | 18 +- 30 files changed, 3842 insertions(+), 3838 deletions(-) create mode 100644 .indent.pro diff --git a/.indent.pro b/.indent.pro new file mode 100644 index 00000000..6fc13b65 --- /dev/null +++ b/.indent.pro @@ -0,0 +1,19 @@ +-bap +-br +-ce +-ci4 +-cli0 +-d0 +-di0 +-i8 +-ip0 +-l79 +-nbc +-ncdb +-ndj +-nfc1 +-nlp +-npcs +-psl +-sc +-sob diff --git a/src/context.c b/src/context.c index 19d14fb3..242278d9 100644 --- a/src/context.c +++ b/src/context.c @@ -47,140 +47,164 @@ * Helper to get default lookup namespaces. * TODO: Determine from OS */ -static uint16_t* create_default_namespaces() { - uint16_t *result = malloc(2 * sizeof(uint16_t)); - result[0] = GETDNS_CONTEXT_NAMESPACE_LOCALNAMES; - result[1] = GETDNS_CONTEXT_NAMESPACE_DNS; - return result; +static uint16_t * +create_default_namespaces() +{ + uint16_t *result = malloc(2 * sizeof(uint16_t)); + result[0] = GETDNS_CONTEXT_NAMESPACE_LOCALNAMES; + result[1] = GETDNS_CONTEXT_NAMESPACE_DNS; + return result; } /** * Helper to get the default root servers. * TODO: Implement */ -static struct getdns_list* create_default_root_servers() { - return NULL; +static struct getdns_list * +create_default_root_servers() +{ + return NULL; } -static getdns_return_t add_ip_str(getdns_dict* ip) { - struct sockaddr_storage storage; - char buff[256]; - getdns_return_t r = dict_to_sockaddr(ip, &storage); - if (r != GETDNS_RETURN_GOOD) { - return r; - } - if (storage.ss_family == AF_INET) { - struct sockaddr_in* addr = (struct sockaddr_in*) &storage; - const char* ipStr = inet_ntop(AF_INET, &(addr->sin_addr), buff, 256); - if (!ipStr) { - return GETDNS_RETURN_GENERIC_ERROR; - } - getdns_dict_util_set_string(ip, GETDNS_STR_ADDRESS_STRING, ipStr); - } else if (storage.ss_family == AF_INET6) { - struct sockaddr_in6* addr = (struct sockaddr_in6*) &storage; - const char* ipStr = inet_ntop(AF_INET6, &(addr->sin6_addr), buff, 256); - if (!ipStr) { - return GETDNS_RETURN_GENERIC_ERROR; - } - getdns_dict_util_set_string(ip, GETDNS_STR_ADDRESS_STRING, ipStr); - } else { - /* unknown */ - return GETDNS_RETURN_GENERIC_ERROR; - } - return GETDNS_RETURN_GOOD; +static getdns_return_t +add_ip_str(getdns_dict * ip) +{ + struct sockaddr_storage storage; + char buff[256]; + getdns_return_t r = dict_to_sockaddr(ip, &storage); + if (r != GETDNS_RETURN_GOOD) { + return r; + } + if (storage.ss_family == AF_INET) { + struct sockaddr_in *addr = (struct sockaddr_in *) &storage; + const char *ipStr = + inet_ntop(AF_INET, &(addr->sin_addr), buff, 256); + if (!ipStr) { + return GETDNS_RETURN_GENERIC_ERROR; + } + getdns_dict_util_set_string(ip, GETDNS_STR_ADDRESS_STRING, + ipStr); + } else if (storage.ss_family == AF_INET6) { + struct sockaddr_in6 *addr = (struct sockaddr_in6 *) &storage; + const char *ipStr = + inet_ntop(AF_INET6, &(addr->sin6_addr), buff, 256); + if (!ipStr) { + return GETDNS_RETURN_GENERIC_ERROR; + } + getdns_dict_util_set_string(ip, GETDNS_STR_ADDRESS_STRING, + ipStr); + } else { + /* unknown */ + return GETDNS_RETURN_GENERIC_ERROR; + } + return GETDNS_RETURN_GOOD; } -static struct getdns_dict* create_ipaddr_dict_from_rdf(ldns_rdf* rdf) { - ldns_rdf_type rt = ldns_rdf_get_type(rdf); - size_t sz = ldns_rdf_size(rdf); - getdns_dict *result = getdns_dict_create(); - /* set type */ - if (rt == LDNS_RDF_TYPE_A) { - getdns_dict_util_set_string(result, GETDNS_STR_ADDRESS_TYPE, GETDNS_STR_IPV4); - } else { - getdns_dict_util_set_string(result, GETDNS_STR_ADDRESS_TYPE, GETDNS_STR_IPV6); - } - /* set data */ - getdns_bindata data_bin = { sz, ldns_rdf_data(rdf) }; - getdns_dict_set_bindata(result, GETDNS_STR_ADDRESS_DATA, &data_bin); - add_ip_str(result); - return result; +static struct getdns_dict * +create_ipaddr_dict_from_rdf(ldns_rdf * rdf) +{ + ldns_rdf_type rt = ldns_rdf_get_type(rdf); + size_t sz = ldns_rdf_size(rdf); + getdns_dict *result = getdns_dict_create(); + /* set type */ + if (rt == LDNS_RDF_TYPE_A) { + getdns_dict_util_set_string(result, GETDNS_STR_ADDRESS_TYPE, + GETDNS_STR_IPV4); + } else { + getdns_dict_util_set_string(result, GETDNS_STR_ADDRESS_TYPE, + GETDNS_STR_IPV6); + } + /* set data */ + getdns_bindata data_bin = { sz, ldns_rdf_data(rdf) }; + getdns_dict_set_bindata(result, GETDNS_STR_ADDRESS_DATA, &data_bin); + add_ip_str(result); + return result; } -static struct getdns_list* create_from_ldns_list(ldns_rdf** ldns_list, size_t count) { - size_t i = 0; - size_t idx = 0; - struct getdns_list *result = getdns_list_create(); - for (i = 0; i < count; ++i) { - ldns_rdf* rdf = ldns_list[i]; - switch (ldns_rdf_get_type(rdf)) { - case LDNS_RDF_TYPE_A: - case LDNS_RDF_TYPE_AAAA: - { - getdns_dict *ipaddr = create_ipaddr_dict_from_rdf(rdf); - getdns_list_add_item(result, &idx); - getdns_list_set_dict(result, idx, ipaddr); - getdns_dict_destroy(ipaddr); - } - break; +static struct getdns_list * +create_from_ldns_list(ldns_rdf ** ldns_list, size_t count) +{ + size_t i = 0; + size_t idx = 0; + struct getdns_list *result = getdns_list_create(); + for (i = 0; i < count; ++i) { + ldns_rdf *rdf = ldns_list[i]; + switch (ldns_rdf_get_type(rdf)) { + case LDNS_RDF_TYPE_A: + case LDNS_RDF_TYPE_AAAA: + { + getdns_dict *ipaddr = + create_ipaddr_dict_from_rdf(rdf); + getdns_list_add_item(result, &idx); + getdns_list_set_dict(result, idx, ipaddr); + getdns_dict_destroy(ipaddr); + } + break; - case LDNS_RDF_TYPE_DNAME: - { - getdns_bindata item; - char* srch = ldns_rdf2str(rdf); - item.size = strlen(srch); - item.data = (uint8_t*) srch; - getdns_list_add_item(result, &idx); - getdns_list_set_bindata(result, idx, &item); - free(srch); - } - break; + case LDNS_RDF_TYPE_DNAME: + { + getdns_bindata item; + char *srch = ldns_rdf2str(rdf); + item.size = strlen(srch); + item.data = (uint8_t *) srch; + getdns_list_add_item(result, &idx); + getdns_list_set_bindata(result, idx, &item); + free(srch); + } + break; - default: - break; - } - } - return result; + default: + break; + } + } + return result; } -static getdns_return_t set_os_defaults(getdns_context_t context) { - ldns_resolver *lr = NULL; - if (ldns_resolver_new_frm_file(&lr, NULL) != LDNS_STATUS_OK) { - return GETDNS_RETURN_GENERIC_ERROR; - } - ldns_rdf **rdf_list= ldns_resolver_nameservers(lr); - size_t rdf_list_sz = ldns_resolver_nameserver_count(lr); - if (rdf_list_sz > 0) { - context->upstream_list = create_from_ldns_list(rdf_list, rdf_list_sz); - } - rdf_list = ldns_resolver_searchlist(lr); - rdf_list_sz = ldns_resolver_searchlist_count(lr); - if (rdf_list_sz > 0) { - context->suffix = create_from_ldns_list(rdf_list, rdf_list_sz); - } +static getdns_return_t +set_os_defaults(getdns_context_t context) +{ + ldns_resolver *lr = NULL; + if (ldns_resolver_new_frm_file(&lr, NULL) != LDNS_STATUS_OK) { + return GETDNS_RETURN_GENERIC_ERROR; + } + ldns_rdf **rdf_list = ldns_resolver_nameservers(lr); + size_t rdf_list_sz = ldns_resolver_nameserver_count(lr); + if (rdf_list_sz > 0) { + context->upstream_list = + create_from_ldns_list(rdf_list, rdf_list_sz); + } + rdf_list = ldns_resolver_searchlist(lr); + rdf_list_sz = ldns_resolver_searchlist_count(lr); + if (rdf_list_sz > 0) { + context->suffix = create_from_ldns_list(rdf_list, rdf_list_sz); + } /** cleanup **/ - ldns_resolver_deep_free(lr); - return GETDNS_RETURN_GOOD; + ldns_resolver_deep_free(lr); + return GETDNS_RETURN_GOOD; } -static int transaction_id_cmp(const void* id1, const void* id2) { - if (id1 == NULL && id2 == NULL) { - return 0; - } else if (id1 == NULL && id2 != NULL) { - return 1; - } else if (id1 != NULL && id2 == NULL) { - return -1; - } else { - getdns_transaction_t t1 = *((const getdns_transaction_t*) id1); - getdns_transaction_t t2 = *((const getdns_transaction_t*) id2); - if (t1 == t2) { - return 0; - } else if (t1 < t2) { - return -1; - } else { - return 1; - } - } +static int +transaction_id_cmp(const void *id1, const void *id2) +{ + if (id1 == NULL && id2 == NULL) { + return 0; + } else if (id1 == NULL && id2 != NULL) { + return 1; + } else if (id1 != NULL && id2 == NULL) { + return -1; + } else { + getdns_transaction_t t1 = + *((const getdns_transaction_t *) id1); + getdns_transaction_t t2 = + *((const getdns_transaction_t *) id2); + if (t1 == t2) { + return 0; + } else if (t1 < t2) { + return -1; + } else { + return 1; + } + } } /* @@ -188,71 +212,69 @@ static int transaction_id_cmp(const void* id1, const void* id2) { * * call this to initialize the context that is used in other getdns calls */ -getdns_return_t getdns_context_create( - getdns_context_t *context, - int set_from_os -) +getdns_return_t +getdns_context_create(getdns_context_t * context, int set_from_os) { - getdns_context_t result = NULL; + getdns_context_t result = NULL; - if (context == NULL) { - return GETDNS_RETURN_GENERIC_ERROR; - } + if (context == NULL) { + return GETDNS_RETURN_GENERIC_ERROR; + } /** default init **/ - result = malloc(sizeof(struct getdns_context_t)); - if (!result) { - return GETDNS_RETURN_GENERIC_ERROR; - } + result = malloc(sizeof(struct getdns_context_t)); + if (!result) { + return GETDNS_RETURN_GENERIC_ERROR; + } - result->update_callback = NULL; - result->memory_allocator = malloc; - result->memory_deallocator = free; - result->memory_reallocator = realloc; + result->update_callback = NULL; + result->memory_allocator = malloc; + result->memory_deallocator = free; + result->memory_reallocator = realloc; - result->event_base_sync = event_base_new(); - result->unbound_sync = ub_ctx_create_event(result->event_base_sync); - /* create the async one also so options are kept up to date */ - result->unbound_async = ub_ctx_create_event(result->event_base_sync); - result->event_base_async = NULL; + result->event_base_sync = event_base_new(); + result->unbound_sync = ub_ctx_create_event(result->event_base_sync); + /* create the async one also so options are kept up to date */ + result->unbound_async = ub_ctx_create_event(result->event_base_sync); + result->event_base_async = NULL; - result->resolution_type_set = 0; + result->resolution_type_set = 0; - result->outbound_requests = ldns_rbtree_create(transaction_id_cmp); + result->outbound_requests = ldns_rbtree_create(transaction_id_cmp); - result->resolution_type = GETDNS_CONTEXT_RECURSING; - result->namespaces = create_default_namespaces(); + result->resolution_type = GETDNS_CONTEXT_RECURSING; + result->namespaces = create_default_namespaces(); - result->timeout = 5000; - result->follow_redirects = GETDNS_CONTEXT_FOLLOW_REDIRECTS; - result->dns_root_servers = create_default_root_servers(); - result->append_name = GETDNS_CONTEXT_APPEND_NAME_ALWAYS; - result->suffix = NULL; + result->timeout = 5000; + result->follow_redirects = GETDNS_CONTEXT_FOLLOW_REDIRECTS; + result->dns_root_servers = create_default_root_servers(); + result->append_name = GETDNS_CONTEXT_APPEND_NAME_ALWAYS; + result->suffix = NULL; - result->dnssec_trust_anchors = NULL; - result->upstream_list = NULL; + result->dnssec_trust_anchors = NULL; + result->upstream_list = NULL; - result->edns_extended_rcode = 0; - result->edns_version = 0; - result->edns_do_bit = 0; + result->edns_extended_rcode = 0; + result->edns_version = 0; + result->edns_do_bit = 0; - if (set_from_os) { - if (GETDNS_RETURN_GOOD != set_os_defaults(result)) { - getdns_context_destroy(result); - return GETDNS_RETURN_GENERIC_ERROR; - } - } + if (set_from_os) { + if (GETDNS_RETURN_GOOD != set_os_defaults(result)) { + getdns_context_destroy(result); + return GETDNS_RETURN_GENERIC_ERROR; + } + } - *context = result; + *context = result; - /* other opts */ - getdns_context_set_dnssec_allowed_skew(result, 0); - getdns_context_set_edns_maximum_udp_payload_size(result, 512); - getdns_context_set_dns_transport(result, GETDNS_CONTEXT_UDP_FIRST_AND_FALL_BACK_TO_TCP); + /* other opts */ + getdns_context_set_dnssec_allowed_skew(result, 0); + getdns_context_set_edns_maximum_udp_payload_size(result, 512); + getdns_context_set_dns_transport(result, + GETDNS_CONTEXT_UDP_FIRST_AND_FALL_BACK_TO_TCP); - - return GETDNS_RETURN_GOOD; -} /* getdns_context_create */ + return GETDNS_RETURN_GOOD; +} /* getdns_context_create */ /* * getdns_context_destroy @@ -261,69 +283,72 @@ getdns_return_t getdns_context_create( * are done with it */ void -getdns_context_destroy( - getdns_context_t context -) +getdns_context_destroy(getdns_context_t context) { - if (context == NULL) { - return; - } - if (context->namespaces) { - context->memory_deallocator(context->namespaces); - } - getdns_list_destroy(context->dns_root_servers); - getdns_list_destroy(context->suffix); - getdns_list_destroy(context->dnssec_trust_anchors); - getdns_list_destroy(context->upstream_list); + if (context == NULL) { + return; + } + if (context->namespaces) { + context->memory_deallocator(context->namespaces); + } + getdns_list_destroy(context->dns_root_servers); + getdns_list_destroy(context->suffix); + getdns_list_destroy(context->dnssec_trust_anchors); + getdns_list_destroy(context->upstream_list); - /* destroy the ub context */ - ub_ctx_delete(context->unbound_async); - ub_ctx_delete(context->unbound_sync); + /* destroy the ub context */ + ub_ctx_delete(context->unbound_async); + ub_ctx_delete(context->unbound_sync); - event_base_free(context->event_base_sync); + event_base_free(context->event_base_sync); - ldns_rbtree_free(context->outbound_requests); + ldns_rbtree_free(context->outbound_requests); - free(context); - return; -} /* getdns_context_destroy */ + free(context); + return; +} /* getdns_context_destroy */ /* * getdns_context_set_context_update_callback * */ getdns_return_t -getdns_context_set_context_update_callback( - getdns_context_t context, - void (*value)(getdns_context_t context, uint16_t changed_item) -) +getdns_context_set_context_update_callback(getdns_context_t context, + void (*value) (getdns_context_t context, uint16_t changed_item) + ) { - context->update_callback = value; - return GETDNS_RETURN_GOOD; -} /* getdns_context_set_context_update_callback */ + context->update_callback = value; + return GETDNS_RETURN_GOOD; +} /* getdns_context_set_context_update_callback */ /* * Helpers to set options on the unbound ctx */ -static void set_ub_string_opt(getdns_context_t ctx, char* opt, char* value) { - ub_ctx_set_option(ctx->unbound_sync, opt, value); - ub_ctx_set_option(ctx->unbound_async, opt, value); +static void +set_ub_string_opt(getdns_context_t ctx, char *opt, char *value) +{ + ub_ctx_set_option(ctx->unbound_sync, opt, value); + ub_ctx_set_option(ctx->unbound_async, opt, value); } -static void set_ub_number_opt(getdns_context_t ctx, char* opt, uint16_t value) { - char buffer[64]; - snprintf(buffer, 64, "%hu", value); - set_ub_string_opt(ctx, opt, buffer); +static void +set_ub_number_opt(getdns_context_t ctx, char *opt, uint16_t value) +{ + char buffer[64]; + snprintf(buffer, 64, "%hu", value); + set_ub_string_opt(ctx, opt, buffer); } /* * Clear the resolution type set flag if needed */ -static inline void clear_resolution_type_set_flag(getdns_context_t context, uint16_t type) { - if (context->resolution_type_set == type) { - context->resolution_type_set = 0; - } +static inline void +clear_resolution_type_set_flag(getdns_context_t context, uint16_t type) +{ + if (context->resolution_type_set == type) { + context->resolution_type_set = 0; + } } /* @@ -331,24 +356,22 @@ static inline void clear_resolution_type_set_flag(getdns_context_t context, uint * */ getdns_return_t -getdns_context_set_context_update( - getdns_context_t context, - uint16_t value -) +getdns_context_set_context_update(getdns_context_t context, uint16_t value) { - UNUSED_PARAM(context); - UNUSED_PARAM(value); - return GETDNS_RETURN_GOOD; -} /* getdns_context_set_context_update */ + UNUSED_PARAM(context); + UNUSED_PARAM(value); + return GETDNS_RETURN_GOOD; +} /* getdns_context_set_context_update */ /** * Helper to dispatch the updated callback */ -static void dispatch_updated(getdns_context_t context, - uint16_t item) { - if (context->update_callback) { - context->update_callback(context, item); - } +static void +dispatch_updated(getdns_context_t context, uint16_t item) +{ + if (context->update_callback) { + context->update_callback(context, item); + } } /* @@ -356,525 +379,484 @@ static void dispatch_updated(getdns_context_t context, * */ getdns_return_t -getdns_context_set_resolution_type( - getdns_context_t context, - uint16_t value -) +getdns_context_set_resolution_type(getdns_context_t context, uint16_t value) { - if (value != GETDNS_CONTEXT_STUB && - value != GETDNS_CONTEXT_RECURSING) { - return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; - } + if (value != GETDNS_CONTEXT_STUB && value != GETDNS_CONTEXT_RECURSING) { + return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; + } - context->resolution_type = value; + context->resolution_type = value; - dispatch_updated(context, GETDNS_CONTEXT_CODE_RESOLUTION_TYPE); + dispatch_updated(context, GETDNS_CONTEXT_CODE_RESOLUTION_TYPE); - return GETDNS_RETURN_GOOD; -} /* getdns_context_set_resolution_type */ + return GETDNS_RETURN_GOOD; +} /* getdns_context_set_resolution_type */ /* * getdns_context_set_namespaces * */ getdns_return_t -getdns_context_set_namespaces( - getdns_context_t context, - size_t namespace_count, - uint16_t *namespaces -) +getdns_context_set_namespaces(getdns_context_t context, + size_t namespace_count, uint16_t * namespaces) { - size_t namespaces_size; - if (namespace_count == 0 || namespaces == NULL) { - return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; - } + size_t namespaces_size; + if (namespace_count == 0 || namespaces == NULL) { + return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; + } /** clean up old namespaces **/ - context->memory_deallocator(context->namespaces); + context->memory_deallocator(context->namespaces); /** duplicate **/ - namespaces_size = namespace_count * sizeof(uint16_t); - context->namespaces = context->memory_allocator(namespaces_size); - memcpy(context->namespaces, namespaces, namespaces_size); + namespaces_size = namespace_count * sizeof(uint16_t); + context->namespaces = context->memory_allocator(namespaces_size); + memcpy(context->namespaces, namespaces, namespaces_size); - dispatch_updated(context, GETDNS_CONTEXT_CODE_NAMESPACES); + dispatch_updated(context, GETDNS_CONTEXT_CODE_NAMESPACES); - return GETDNS_RETURN_GOOD; -} /* getdns_context_set_namespaces */ + return GETDNS_RETURN_GOOD; +} /* getdns_context_set_namespaces */ /* * getdns_context_set_dns_transport * */ getdns_return_t -getdns_context_set_dns_transport( - getdns_context_t context, - uint16_t value -) +getdns_context_set_dns_transport(getdns_context_t context, uint16_t value) { - switch (value) { - case GETDNS_CONTEXT_UDP_FIRST_AND_FALL_BACK_TO_TCP: - set_ub_string_opt(context, "do-udp", "yes"); - set_ub_string_opt(context, "do-tcp", "yes"); - break; - case GETDNS_CONTEXT_UDP_ONLY: - set_ub_string_opt(context, "do-udp", "yes"); - set_ub_string_opt(context, "do-tcp", "no"); - break; - case GETDNS_CONTEXT_TCP_ONLY: - set_ub_string_opt(context, "do-udp", "no"); - set_ub_string_opt(context, "do-tcp", "yes"); - break; - default: - /* TODO GETDNS_CONTEXT_TCP_ONLY_KEEP_CONNECTIONS_OPEN */ - return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; - } + switch (value) { + case GETDNS_CONTEXT_UDP_FIRST_AND_FALL_BACK_TO_TCP: + set_ub_string_opt(context, "do-udp", "yes"); + set_ub_string_opt(context, "do-tcp", "yes"); + break; + case GETDNS_CONTEXT_UDP_ONLY: + set_ub_string_opt(context, "do-udp", "yes"); + set_ub_string_opt(context, "do-tcp", "no"); + break; + case GETDNS_CONTEXT_TCP_ONLY: + set_ub_string_opt(context, "do-udp", "no"); + set_ub_string_opt(context, "do-tcp", "yes"); + break; + default: + /* TODO GETDNS_CONTEXT_TCP_ONLY_KEEP_CONNECTIONS_OPEN */ + return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; + } - dispatch_updated(context, GETDNS_CONTEXT_CODE_DNS_TRANSPORT); + dispatch_updated(context, GETDNS_CONTEXT_CODE_DNS_TRANSPORT); - return GETDNS_RETURN_GOOD; -} /* getdns_context_set_dns_transport */ + return GETDNS_RETURN_GOOD; +} /* getdns_context_set_dns_transport */ /* * getdns_context_set_limit_outstanding_queries * */ getdns_return_t -getdns_context_set_limit_outstanding_queries( - getdns_context_t context, - uint16_t limit -) +getdns_context_set_limit_outstanding_queries(getdns_context_t context, + uint16_t limit) { - /* num-queries-per-thread */ - set_ub_number_opt(context, "num-queries-per-thread", limit); + /* num-queries-per-thread */ + set_ub_number_opt(context, "num-queries-per-thread", limit); - dispatch_updated(context, GETDNS_CONTEXT_CODE_LIMIT_OUTSTANDING_QUERIES); + dispatch_updated(context, + GETDNS_CONTEXT_CODE_LIMIT_OUTSTANDING_QUERIES); - return GETDNS_RETURN_GOOD; -} /* getdns_context_set_limit_outstanding_queries */ + return GETDNS_RETURN_GOOD; +} /* getdns_context_set_limit_outstanding_queries */ /* * getdns_context_set_timeout * */ getdns_return_t -getdns_context_set_timeout( - getdns_context_t context, - uint16_t timeout -) +getdns_context_set_timeout(getdns_context_t context, uint16_t timeout) { - context->timeout = timeout; + context->timeout = timeout; - dispatch_updated(context, GETDNS_CONTEXT_CODE_TIMEOUT); + dispatch_updated(context, GETDNS_CONTEXT_CODE_TIMEOUT); - return GETDNS_RETURN_GOOD; -} /* getdns_context_set_timeout */ + return GETDNS_RETURN_GOOD; +} /* getdns_context_set_timeout */ /* * getdns_context_set_follow_redirects * */ getdns_return_t -getdns_context_set_follow_redirects( - getdns_context_t context, - uint16_t value -) +getdns_context_set_follow_redirects(getdns_context_t context, uint16_t value) { - context->follow_redirects = value; + context->follow_redirects = value; - clear_resolution_type_set_flag(context, GETDNS_CONTEXT_RECURSING); - dispatch_updated(context, GETDNS_CONTEXT_CODE_FOLLOW_REDIRECTS); + clear_resolution_type_set_flag(context, GETDNS_CONTEXT_RECURSING); + dispatch_updated(context, GETDNS_CONTEXT_CODE_FOLLOW_REDIRECTS); - return GETDNS_RETURN_GOOD; -} /* getdns_context_set_follow_redirects */ + return GETDNS_RETURN_GOOD; +} /* getdns_context_set_follow_redirects */ /* * getdns_context_set_dns_root_servers * */ getdns_return_t -getdns_context_set_dns_root_servers( - getdns_context_t context, - struct getdns_list *addresses -) +getdns_context_set_dns_root_servers(getdns_context_t context, + struct getdns_list * addresses) { - getdns_list *copy = NULL; - size_t count = 0; - if (addresses != NULL) { - if (getdns_list_copy(addresses, ©) != GETDNS_RETURN_GOOD) { - return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; - } - addresses = copy; - getdns_list_get_length(addresses, &count); - if (count == 0) { - getdns_list_destroy(addresses); - addresses = NULL; - } else { - size_t i = 0; - getdns_return_t r = GETDNS_RETURN_GOOD; - /* validate and add ip str */ - for (i = 0; i < count; ++i) { - getdns_dict* dict = NULL; - getdns_list_get_dict(addresses, i, &dict); - r = add_ip_str(dict); - if (r != GETDNS_RETURN_GOOD) { - break; - } - } - if (r != GETDNS_RETURN_GOOD) { - getdns_list_destroy(addresses); - return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; - } - } - } + getdns_list *copy = NULL; + size_t count = 0; + if (addresses != NULL) { + if (getdns_list_copy(addresses, ©) != GETDNS_RETURN_GOOD) { + return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; + } + addresses = copy; + getdns_list_get_length(addresses, &count); + if (count == 0) { + getdns_list_destroy(addresses); + addresses = NULL; + } else { + size_t i = 0; + getdns_return_t r = GETDNS_RETURN_GOOD; + /* validate and add ip str */ + for (i = 0; i < count; ++i) { + getdns_dict *dict = NULL; + getdns_list_get_dict(addresses, i, &dict); + r = add_ip_str(dict); + if (r != GETDNS_RETURN_GOOD) { + break; + } + } + if (r != GETDNS_RETURN_GOOD) { + getdns_list_destroy(addresses); + return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; + } + } + } - getdns_list_destroy(context->dns_root_servers); - context->dns_root_servers = addresses; + getdns_list_destroy(context->dns_root_servers); + context->dns_root_servers = addresses; - clear_resolution_type_set_flag(context, GETDNS_CONTEXT_RECURSING); + clear_resolution_type_set_flag(context, GETDNS_CONTEXT_RECURSING); - dispatch_updated(context, GETDNS_CONTEXT_CODE_DNS_ROOT_SERVERS); + dispatch_updated(context, GETDNS_CONTEXT_CODE_DNS_ROOT_SERVERS); - return GETDNS_RETURN_GOOD; -} /* getdns_context_set_dns_root_servers */ + return GETDNS_RETURN_GOOD; +} /* getdns_context_set_dns_root_servers */ /* * getdns_context_set_append_name * */ getdns_return_t -getdns_context_set_append_name( - getdns_context_t context, - uint16_t value -) +getdns_context_set_append_name(getdns_context_t context, uint16_t value) { - if (value != GETDNS_CONTEXT_APPEND_NAME_ALWAYS && - value != GETDNS_CONTEXT_APPEND_NAME_ONLY_TO_SINGLE_LABEL_AFTER_FAILURE && - value != GETDNS_CONTEXT_APPEND_NAME_ONLY_TO_MULTIPLE_LABEL_NAME_AFTER_FAILURE && - value != GETDNS_CONTEXT_DO_NOT_APPEND_NAMES) { - return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; - } + if (value != GETDNS_CONTEXT_APPEND_NAME_ALWAYS && + value != + GETDNS_CONTEXT_APPEND_NAME_ONLY_TO_SINGLE_LABEL_AFTER_FAILURE + && value != + GETDNS_CONTEXT_APPEND_NAME_ONLY_TO_MULTIPLE_LABEL_NAME_AFTER_FAILURE + && value != GETDNS_CONTEXT_DO_NOT_APPEND_NAMES) { + return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; + } - context->append_name = value; + context->append_name = value; - dispatch_updated(context, GETDNS_CONTEXT_CODE_APPEND_NAME); + dispatch_updated(context, GETDNS_CONTEXT_CODE_APPEND_NAME); - return GETDNS_RETURN_GOOD; -} /* getdns_context_set_append_name */ + return GETDNS_RETURN_GOOD; +} /* getdns_context_set_append_name */ /* * getdns_context_set_suffix * */ getdns_return_t -getdns_context_set_suffix( - getdns_context_t context, - struct getdns_list *value -) +getdns_context_set_suffix(getdns_context_t context, struct getdns_list * value) { - getdns_list *copy = NULL; - if (value != NULL) { - if (getdns_list_copy(value, ©) != GETDNS_RETURN_GOOD) { - return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; - } - value = copy; - } - getdns_list_destroy(context->suffix); - context->suffix = value; + getdns_list *copy = NULL; + if (value != NULL) { + if (getdns_list_copy(value, ©) != GETDNS_RETURN_GOOD) { + return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; + } + value = copy; + } + getdns_list_destroy(context->suffix); + context->suffix = value; - clear_resolution_type_set_flag(context, GETDNS_CONTEXT_STUB); + clear_resolution_type_set_flag(context, GETDNS_CONTEXT_STUB); - dispatch_updated(context, GETDNS_CONTEXT_CODE_SUFFIX); + dispatch_updated(context, GETDNS_CONTEXT_CODE_SUFFIX); - return GETDNS_RETURN_GOOD; -} /* getdns_context_set_suffix */ + return GETDNS_RETURN_GOOD; +} /* getdns_context_set_suffix */ /* * getdns_context_set_dnssec_trust_anchors * */ getdns_return_t -getdns_context_set_dnssec_trust_anchors( - getdns_context_t context, - struct getdns_list *value -) +getdns_context_set_dnssec_trust_anchors(getdns_context_t context, + struct getdns_list * value) { - getdns_list *copy = NULL; - if (value != NULL) { - if (getdns_list_copy(value, ©) != GETDNS_RETURN_GOOD) { - return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; - } - value = copy; - } - getdns_list_destroy(context->dnssec_trust_anchors); - context->dnssec_trust_anchors = value; + getdns_list *copy = NULL; + if (value != NULL) { + if (getdns_list_copy(value, ©) != GETDNS_RETURN_GOOD) { + return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; + } + value = copy; + } + getdns_list_destroy(context->dnssec_trust_anchors); + context->dnssec_trust_anchors = value; - dispatch_updated(context, GETDNS_CONTEXT_CODE_DNSSEC_TRUST_ANCHORS); + dispatch_updated(context, GETDNS_CONTEXT_CODE_DNSSEC_TRUST_ANCHORS); - return GETDNS_RETURN_GOOD; -} /* getdns_context_set_dnssec_trust_anchors */ + return GETDNS_RETURN_GOOD; +} /* getdns_context_set_dnssec_trust_anchors */ /* * getdns_context_set_dnssec_allowed_skew * */ getdns_return_t -getdns_context_set_dnssec_allowed_skew( - getdns_context_t context, - uint16_t value -) +getdns_context_set_dnssec_allowed_skew(getdns_context_t context, + uint16_t value) { - set_ub_number_opt(context, "val-sig-skew-min", value); - set_ub_number_opt(context, "val-sig-skew-max", value); - dispatch_updated(context, GETDNS_CONTEXT_CODE_DNSSEC_ALLOWED_SKEW); + set_ub_number_opt(context, "val-sig-skew-min", value); + set_ub_number_opt(context, "val-sig-skew-max", value); + dispatch_updated(context, GETDNS_CONTEXT_CODE_DNSSEC_ALLOWED_SKEW); - return GETDNS_RETURN_GOOD; -} /* getdns_context_set_dnssec_allowed_skew */ + return GETDNS_RETURN_GOOD; +} /* getdns_context_set_dnssec_allowed_skew */ /* * getdns_context_set_stub_resolution * */ getdns_return_t -getdns_context_set_stub_resolution( - getdns_context_t context, - struct getdns_list *upstream_list -) +getdns_context_set_stub_resolution(getdns_context_t context, + struct getdns_list * upstream_list) { - size_t count = 0; - size_t i = 0; - getdns_return_t r = getdns_list_get_length(upstream_list, &count); - if (count == 0 || r != GETDNS_RETURN_GOOD) { - return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; - } - getdns_list *copy = NULL; - if (getdns_list_copy(upstream_list, ©) != GETDNS_RETURN_GOOD) { - return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; - } - upstream_list = copy; - /* validate and add ip str */ - for (i = 0; i < count; ++i) { - getdns_dict* dict = NULL; - getdns_list_get_dict(upstream_list, i, &dict); - r = add_ip_str(dict); - if (r != GETDNS_RETURN_GOOD) { - break; - } - } + size_t count = 0; + size_t i = 0; + getdns_return_t r = getdns_list_get_length(upstream_list, &count); + if (count == 0 || r != GETDNS_RETURN_GOOD) { + return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; + } + getdns_list *copy = NULL; + if (getdns_list_copy(upstream_list, ©) != GETDNS_RETURN_GOOD) { + return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; + } + upstream_list = copy; + /* validate and add ip str */ + for (i = 0; i < count; ++i) { + getdns_dict *dict = NULL; + getdns_list_get_dict(upstream_list, i, &dict); + r = add_ip_str(dict); + if (r != GETDNS_RETURN_GOOD) { + break; + } + } - if (r != GETDNS_RETURN_GOOD) { - getdns_list_destroy(upstream_list); - return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; - } + if (r != GETDNS_RETURN_GOOD) { + getdns_list_destroy(upstream_list); + return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; + } - getdns_list_destroy(context->upstream_list); - context->upstream_list = upstream_list; + getdns_list_destroy(context->upstream_list); + context->upstream_list = upstream_list; - clear_resolution_type_set_flag(context, GETDNS_CONTEXT_STUB); + clear_resolution_type_set_flag(context, GETDNS_CONTEXT_STUB); - dispatch_updated(context, GETDNS_CONTEXT_CODE_UPSTREAM_RECURSIVE_SERVERS); + dispatch_updated(context, + GETDNS_CONTEXT_CODE_UPSTREAM_RECURSIVE_SERVERS); - return GETDNS_RETURN_GOOD; -} /* getdns_context_set_stub_resolution */ + return GETDNS_RETURN_GOOD; +} /* getdns_context_set_stub_resolution */ /* * getdns_context_set_edns_maximum_udp_payload_size * */ getdns_return_t -getdns_context_set_edns_maximum_udp_payload_size( - getdns_context_t context, - uint16_t value -) +getdns_context_set_edns_maximum_udp_payload_size(getdns_context_t context, + uint16_t value) { - /* check for < 512. uint16_t won't let it go above max) */ - if (value < 512) { - return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; - } + /* check for < 512. uint16_t won't let it go above max) */ + if (value < 512) { + return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; + } - /* max-udp-size */ - set_ub_number_opt(context, "max-udp-size", value); + /* max-udp-size */ + set_ub_number_opt(context, "max-udp-size", value); - dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_MAXIMUM_UDP_PAYLOAD_SIZE); + dispatch_updated(context, + GETDNS_CONTEXT_CODE_EDNS_MAXIMUM_UDP_PAYLOAD_SIZE); - return GETDNS_RETURN_GOOD; -} /* getdns_context_set_edns_maximum_udp_payload_size */ + return GETDNS_RETURN_GOOD; +} /* getdns_context_set_edns_maximum_udp_payload_size */ /* * getdns_context_set_edns_extended_rcode * */ getdns_return_t -getdns_context_set_edns_extended_rcode( - getdns_context_t context, - uint8_t value -) +getdns_context_set_edns_extended_rcode(getdns_context_t context, uint8_t value) { - context->edns_extended_rcode = value; + context->edns_extended_rcode = value; - dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_EXTENDED_RCODE); + dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_EXTENDED_RCODE); - return GETDNS_RETURN_GOOD; -} /* getdns_context_set_edns_extended_rcode */ + return GETDNS_RETURN_GOOD; +} /* getdns_context_set_edns_extended_rcode */ /* * getdns_context_set_edns_version * */ getdns_return_t -getdns_context_set_edns_version( - getdns_context_t context, - uint8_t value -) +getdns_context_set_edns_version(getdns_context_t context, uint8_t value) { - context->edns_version = value; + context->edns_version = value; - dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_VERSION); + dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_VERSION); - return GETDNS_RETURN_GOOD; -} /* getdns_context_set_edns_version */ + return GETDNS_RETURN_GOOD; +} /* getdns_context_set_edns_version */ /* * getdns_context_set_edns_do_bit * */ getdns_return_t -getdns_context_set_edns_do_bit( - getdns_context_t context, - uint8_t value -) +getdns_context_set_edns_do_bit(getdns_context_t context, uint8_t value) { - /* 0 or 1 */ - if (value > 1) { - return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; - } + /* 0 or 1 */ + if (value > 1) { + return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; + } - context->edns_do_bit = value; + context->edns_do_bit = value; - dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_DO_BIT); + dispatch_updated(context, GETDNS_CONTEXT_CODE_EDNS_DO_BIT); - return GETDNS_RETURN_GOOD; -} /* getdns_context_set_edns_do_bit */ + return GETDNS_RETURN_GOOD; +} /* getdns_context_set_edns_do_bit */ /* * getdns_context_set_memory_allocator * */ getdns_return_t -getdns_context_set_memory_allocator( - getdns_context_t context, - void (*value)(size_t somesize) -) +getdns_context_set_memory_allocator(getdns_context_t context, + void (*value) (size_t somesize) + ) { - UNUSED_PARAM(context); - UNUSED_PARAM(value); - return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; -} /* getdns_context_set_memory_allocator */ + UNUSED_PARAM(context); + UNUSED_PARAM(value); + return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; +} /* getdns_context_set_memory_allocator */ /* * getdns_context_set_memory_deallocator * */ getdns_return_t -getdns_context_set_memory_deallocator( - getdns_context_t context, - void (*value)(void*) -) +getdns_context_set_memory_deallocator(getdns_context_t context, + void (*value) (void *) + ) { - UNUSED_PARAM(context); - UNUSED_PARAM(value); - return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; -} /* getdns_context_set_memory_deallocator */ + UNUSED_PARAM(context); + UNUSED_PARAM(value); + return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; +} /* getdns_context_set_memory_deallocator */ /* * getdns_context_set_memory_reallocator * */ getdns_return_t -getdns_context_set_memory_reallocator( - getdns_context_t context, - void (*value)(void*) -) +getdns_context_set_memory_reallocator(getdns_context_t context, + void (*value) (void *) + ) { - UNUSED_PARAM(context); - UNUSED_PARAM(value); - return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; -} /* getdns_context_set_memory_reallocator */ + UNUSED_PARAM(context); + UNUSED_PARAM(value); + return GETDNS_RETURN_CONTEXT_UPDATE_FAIL; +} /* getdns_context_set_memory_reallocator */ /* * getdns_extension_set_libevent_base * */ getdns_return_t -getdns_extension_set_libevent_base( - getdns_context_t context, - struct event_base *this_event_base -) +getdns_extension_set_libevent_base(getdns_context_t context, + struct event_base * this_event_base) { - if (this_event_base) { - ub_ctx_set_event(context->unbound_async, this_event_base); - context->event_base_async = this_event_base; - } else { - ub_ctx_set_event(context->unbound_async, context->event_base_sync); - context->event_base_async = NULL; - } - return GETDNS_RETURN_GOOD; -} /* getdns_extension_set_libevent_base */ + if (this_event_base) { + ub_ctx_set_event(context->unbound_async, this_event_base); + context->event_base_async = this_event_base; + } else { + ub_ctx_set_event(context->unbound_async, + context->event_base_sync); + context->event_base_async = NULL; + } + return GETDNS_RETURN_GOOD; +} /* getdns_extension_set_libevent_base */ /* cancel the request */ -static void cancel_dns_req(getdns_dns_req* req) { - getdns_network_req* netreq = req->first_req; - while (netreq) { - if (netreq->state == NET_REQ_IN_FLIGHT) { - /* for ev based ub, this should always prevent - * the callback from firing */ - ub_cancel(req->unbound, netreq->unbound_id); - netreq->state = NET_REQ_CANCELED; - } else if (netreq->state == NET_REQ_NOT_SENT) { - netreq->state = NET_REQ_CANCELED; - } - netreq = netreq->next; - } - req->canceled = 1; +static void +cancel_dns_req(getdns_dns_req * req) +{ + getdns_network_req *netreq = req->first_req; + while (netreq) { + if (netreq->state == NET_REQ_IN_FLIGHT) { + /* for ev based ub, this should always prevent + * the callback from firing */ + ub_cancel(req->unbound, netreq->unbound_id); + netreq->state = NET_REQ_CANCELED; + } else if (netreq->state == NET_REQ_NOT_SENT) { + netreq->state = NET_REQ_CANCELED; + } + netreq = netreq->next; + } + req->canceled = 1; } -getdns_return_t getdns_context_cancel_request(getdns_context_t context, - getdns_transaction_t transaction_id, - int fire_callback) { - getdns_dns_req *req = NULL; +getdns_return_t +getdns_context_cancel_request(getdns_context_t context, + getdns_transaction_t transaction_id, int fire_callback) +{ + getdns_dns_req *req = NULL; - /* delete the node from the tree */ - ldns_rbnode_t* node = ldns_rbtree_delete(context->outbound_requests, - &transaction_id); + /* delete the node from the tree */ + ldns_rbnode_t *node = ldns_rbtree_delete(context->outbound_requests, + &transaction_id); - if (!node) { - return GETDNS_RETURN_UNKNOWN_TRANSACTION; - } - req = (getdns_dns_req*) node->data; - /* do the cancel */ + if (!node) { + return GETDNS_RETURN_UNKNOWN_TRANSACTION; + } + req = (getdns_dns_req *) node->data; + /* do the cancel */ - cancel_dns_req(req); + cancel_dns_req(req); - if (fire_callback) { - getdns_callback_t cb = NULL; - void* user_pointer = NULL; + if (fire_callback) { + getdns_callback_t cb = NULL; + void *user_pointer = NULL; - cb = req->user_callback; - user_pointer = req->user_pointer; + cb = req->user_callback; + user_pointer = req->user_pointer; - /* clean up */ - context->memory_deallocator(node); - dns_req_free(req); + /* clean up */ + context->memory_deallocator(node); + dns_req_free(req); - /* fire callback */ - cb(context, - GETDNS_CALLBACK_CANCEL, - NULL, - user_pointer, - transaction_id); - } - return GETDNS_RETURN_GOOD; + /* fire callback */ + cb(context, + GETDNS_CALLBACK_CANCEL, + NULL, user_pointer, transaction_id); + } + return GETDNS_RETURN_GOOD; } /* @@ -882,91 +864,102 @@ getdns_return_t getdns_context_cancel_request(getdns_context_t context, * */ getdns_return_t -getdns_cancel_callback( - getdns_context_t context, - getdns_transaction_t transaction_id -) +getdns_cancel_callback(getdns_context_t context, + getdns_transaction_t transaction_id) { - return getdns_context_cancel_request(context, transaction_id, 1); -} /* getdns_cancel_callback */ + return getdns_context_cancel_request(context, transaction_id, 1); +} /* getdns_cancel_callback */ -static void ub_setup_stub(struct ub_ctx* ctx, getdns_list* upstreams, size_t count) { - size_t i; - /* reset forwarding servers */ - ub_ctx_set_fwd(ctx, NULL); - for (i = 0 ; i < count; ++i) { - getdns_dict* dict = NULL; - char* ip_str = NULL; - getdns_list_get_dict(upstreams, i, &dict); - getdns_dict_util_get_string(dict, GETDNS_STR_ADDRESS_STRING, &ip_str); - ub_ctx_set_fwd(ctx, ip_str); - } +static void +ub_setup_stub(struct ub_ctx *ctx, getdns_list * upstreams, size_t count) +{ + size_t i; + /* reset forwarding servers */ + ub_ctx_set_fwd(ctx, NULL); + for (i = 0; i < count; ++i) { + getdns_dict *dict = NULL; + char *ip_str = NULL; + getdns_list_get_dict(upstreams, i, &dict); + getdns_dict_util_get_string(dict, GETDNS_STR_ADDRESS_STRING, + &ip_str); + ub_ctx_set_fwd(ctx, ip_str); + } } -getdns_return_t getdns_context_prepare_for_resolution(getdns_context_t context) { - if (context->resolution_type_set == context->resolution_type) { - /* already set and no config changes have caused this to be - * bad. - */ - return GETDNS_RETURN_GOOD; - } - if (context->resolution_type == GETDNS_CONTEXT_STUB) { - size_t upstream_len = 0; - getdns_return_t r = getdns_list_get_length(context->upstream_list, &upstream_len); - if (r != GETDNS_RETURN_GOOD || upstream_len == 0) { - return GETDNS_RETURN_BAD_CONTEXT; - } - /* set upstreams */ - ub_setup_stub(context->unbound_async, context->upstream_list, upstream_len); - ub_setup_stub(context->unbound_sync, context->upstream_list, upstream_len); - /* use /etc/hosts */ - ub_ctx_hosts(context->unbound_sync, NULL); - ub_ctx_hosts(context->unbound_async, NULL); +getdns_return_t +getdns_context_prepare_for_resolution(getdns_context_t context) +{ + if (context->resolution_type_set == context->resolution_type) { + /* already set and no config changes have caused this to be + * bad. + */ + return GETDNS_RETURN_GOOD; + } + if (context->resolution_type == GETDNS_CONTEXT_STUB) { + size_t upstream_len = 0; + getdns_return_t r = + getdns_list_get_length(context->upstream_list, + &upstream_len); + if (r != GETDNS_RETURN_GOOD || upstream_len == 0) { + return GETDNS_RETURN_BAD_CONTEXT; + } + /* set upstreams */ + ub_setup_stub(context->unbound_async, context->upstream_list, + upstream_len); + ub_setup_stub(context->unbound_sync, context->upstream_list, + upstream_len); + /* use /etc/hosts */ + ub_ctx_hosts(context->unbound_sync, NULL); + ub_ctx_hosts(context->unbound_async, NULL); - } else if (context->resolution_type == GETDNS_CONTEXT_RECURSING) { - /* set recursive */ - /* TODO: use the root servers via root hints file */ - ub_ctx_set_fwd(context->unbound_async, NULL); - ub_ctx_set_fwd(context->unbound_sync, NULL); + } else if (context->resolution_type == GETDNS_CONTEXT_RECURSING) { + /* set recursive */ + /* TODO: use the root servers via root hints file */ + ub_ctx_set_fwd(context->unbound_async, NULL); + ub_ctx_set_fwd(context->unbound_sync, NULL); - } else { - /* bogus? */ - return GETDNS_RETURN_BAD_CONTEXT; - } - context->resolution_type_set = context->resolution_type; - return GETDNS_RETURN_GOOD; + } else { + /* bogus? */ + return GETDNS_RETURN_BAD_CONTEXT; + } + context->resolution_type_set = context->resolution_type; + return GETDNS_RETURN_GOOD; } -getdns_return_t getdns_context_track_outbound_request(getdns_dns_req* req) { - if (!req) { - return GETDNS_RETURN_GENERIC_ERROR; - } - getdns_context_t context = req->context; - ldns_rbnode_t* node = context->memory_allocator(sizeof(ldns_rbnode_t)); - if (!node) { - return GETDNS_RETURN_GENERIC_ERROR; - } - node->key = &(req->trans_id); - node->data = req; - if (!ldns_rbtree_insert(context->outbound_requests, node)) { - /* free the node */ - context->memory_deallocator(node); - return GETDNS_RETURN_GENERIC_ERROR; - } - return GETDNS_RETURN_GOOD; +getdns_return_t +getdns_context_track_outbound_request(getdns_dns_req * req) +{ + if (!req) { + return GETDNS_RETURN_GENERIC_ERROR; + } + getdns_context_t context = req->context; + ldns_rbnode_t *node = context->memory_allocator(sizeof(ldns_rbnode_t)); + if (!node) { + return GETDNS_RETURN_GENERIC_ERROR; + } + node->key = &(req->trans_id); + node->data = req; + if (!ldns_rbtree_insert(context->outbound_requests, node)) { + /* free the node */ + context->memory_deallocator(node); + return GETDNS_RETURN_GENERIC_ERROR; + } + return GETDNS_RETURN_GOOD; } -getdns_return_t getdns_context_clear_outbound_request(getdns_dns_req* req) { - if (!req) { - return GETDNS_RETURN_GENERIC_ERROR; - } - getdns_context_t context = req->context; - ldns_rbnode_t* node = ldns_rbtree_delete(context->outbound_requests, - &(req->trans_id)); - if (node) { - context->memory_deallocator(node); - } - return GETDNS_RETURN_GOOD; +getdns_return_t +getdns_context_clear_outbound_request(getdns_dns_req * req) +{ + if (!req) { + return GETDNS_RETURN_GENERIC_ERROR; + } + getdns_context_t context = req->context; + ldns_rbnode_t *node = ldns_rbtree_delete(context->outbound_requests, + &(req->trans_id)); + if (node) { + context->memory_deallocator(node); + } + return GETDNS_RETURN_GOOD; } /* getdns_context.c */ diff --git a/src/context.h b/src/context.h index 8c283899..a2ebf112 100644 --- a/src/context.h +++ b/src/context.h @@ -38,69 +38,72 @@ struct ldns_rbtree_t; struct getdns_dns_req; /** function pointer typedefs */ -typedef void (*getdns_update_callback)(getdns_context_t context, uint16_t changed_item); -typedef void* (*getdns_memory_allocator)(size_t size); -typedef void (*getdns_memory_deallocator)(void*); -typedef void* (*getdns_memory_reallocator)(void* ptr, size_t size); +typedef void (*getdns_update_callback) (getdns_context_t context, + uint16_t changed_item); +typedef void *(*getdns_memory_allocator) (size_t size); +typedef void (*getdns_memory_deallocator) (void *); +typedef void *(*getdns_memory_reallocator) (void *ptr, size_t size); -struct getdns_context_t { +struct getdns_context_t +{ - /* Context values */ - uint16_t resolution_type; - uint16_t *namespaces; - uint16_t timeout; - uint16_t follow_redirects; - struct getdns_list *dns_root_servers; - uint16_t append_name; - struct getdns_list *suffix; - struct getdns_list *dnssec_trust_anchors; - struct getdns_list *upstream_list; + /* Context values */ + uint16_t resolution_type; + uint16_t *namespaces; + uint16_t timeout; + uint16_t follow_redirects; + struct getdns_list *dns_root_servers; + uint16_t append_name; + struct getdns_list *suffix; + struct getdns_list *dnssec_trust_anchors; + struct getdns_list *upstream_list; - uint8_t edns_extended_rcode; - uint8_t edns_version; - uint8_t edns_do_bit; + uint8_t edns_extended_rcode; + uint8_t edns_version; + uint8_t edns_do_bit; - getdns_update_callback update_callback; - getdns_memory_allocator memory_allocator; - getdns_memory_deallocator memory_deallocator; - getdns_memory_reallocator memory_reallocator; + getdns_update_callback update_callback; + getdns_memory_allocator memory_allocator; + getdns_memory_deallocator memory_deallocator; + getdns_memory_reallocator memory_reallocator; - /* Event loop for sync requests */ - struct event_base* event_base_sync; - /* Event loop for async requests */ - struct event_base* event_base_async; + /* Event loop for sync requests */ + struct event_base *event_base_sync; + /* Event loop for async requests */ + struct event_base *event_base_async; - /* The underlying unbound contexts that do - the real work */ - struct ub_ctx *unbound_sync; - struct ub_ctx *unbound_async; + /* The underlying unbound contexts that do + * the real work */ + struct ub_ctx *unbound_sync; + struct ub_ctx *unbound_async; - /* which resolution type the contexts are configured for - * 0 means nothing set - */ - uint8_t resolution_type_set; + /* which resolution type the contexts are configured for + * 0 means nothing set + */ + uint8_t resolution_type_set; - /* - * outbound requests -> transaction to getdns_dns_req - */ - struct ldns_rbtree_t* outbound_requests; -} ; + /* + * outbound requests -> transaction to getdns_dns_req + */ + struct ldns_rbtree_t *outbound_requests; +}; /** internal functions **/ /** * Sets up the unbound contexts with stub or recursive behavior * if needed. */ -getdns_return_t getdns_context_prepare_for_resolution(getdns_context_t context); +getdns_return_t getdns_context_prepare_for_resolution(getdns_context_t + context); /* track an outbound request */ -getdns_return_t getdns_context_track_outbound_request(struct getdns_dns_req* req); +getdns_return_t getdns_context_track_outbound_request(struct getdns_dns_req + *req); /* clear the outbound request from being tracked - does not cancel it */ -getdns_return_t getdns_context_clear_outbound_request(struct getdns_dns_req* req); +getdns_return_t getdns_context_clear_outbound_request(struct getdns_dns_req + *req); /* cancel callback internal - flag to indicate if req should be freed and callback fired */ getdns_return_t getdns_context_cancel_request(getdns_context_t context, - getdns_transaction_t transaction_id, - int fire_callback); + getdns_transaction_t transaction_id, int fire_callback); #endif - diff --git a/src/convert.c b/src/convert.c index 9f1d5d9c..7a7fc001 100644 --- a/src/convert.c +++ b/src/convert.c @@ -38,73 +38,76 @@ #define UNUSED_PARAM(x) ((void)(x)) char * -getdns_convert_dns_name_to_fqdn( - char *name_from_dns_response -) -{ UNUSED_PARAM(name_from_dns_response); return NULL; } - -char * -getdns_convert_fqdn_to_dns_name( - char *fqdn_as_string -) -{ UNUSED_PARAM(fqdn_as_string); return NULL; } - -char * -getdns_convert_ulabel_to_alabel( - char *ulabel -) -{ UNUSED_PARAM(ulabel); return NULL; } - -char * -getdns_convert_alabel_to_ulabel( - char *alabel -) -{ UNUSED_PARAM(alabel); return NULL; } - -char * -getdns_display_ip_address( - struct getdns_bindata *bindata_of_ipv4_or_ipv6_address -) +getdns_convert_dns_name_to_fqdn(char *name_from_dns_response) { - char buff[256]; - if (!bindata_of_ipv4_or_ipv6_address || - bindata_of_ipv4_or_ipv6_address->size == 0 || - !bindata_of_ipv4_or_ipv6_address->data) { - return NULL; - } - if (bindata_of_ipv4_or_ipv6_address->size == 4) { - const char* ipStr = inet_ntop(AF_INET, - bindata_of_ipv4_or_ipv6_address->data, - buff, - 256); - if (ipStr) { - return strdup(ipStr); - } - } else if (bindata_of_ipv4_or_ipv6_address->size == 16) { - const char* ipStr = inet_ntop(AF_INET6, - bindata_of_ipv4_or_ipv6_address->data, - buff, - 256); - if (ipStr) { - return strdup(ipStr); - } - } - return NULL; + UNUSED_PARAM(name_from_dns_response); + return NULL; +} + +char * +getdns_convert_fqdn_to_dns_name(char *fqdn_as_string) +{ + UNUSED_PARAM(fqdn_as_string); + return NULL; +} + +char * +getdns_convert_ulabel_to_alabel(char *ulabel) +{ + UNUSED_PARAM(ulabel); + return NULL; +} + +char * +getdns_convert_alabel_to_ulabel(char *alabel) +{ + UNUSED_PARAM(alabel); + return NULL; +} + +char * +getdns_display_ip_address(struct getdns_bindata + *bindata_of_ipv4_or_ipv6_address) +{ + char buff[256]; + if (!bindata_of_ipv4_or_ipv6_address || + bindata_of_ipv4_or_ipv6_address->size == 0 || + !bindata_of_ipv4_or_ipv6_address->data) { + return NULL; + } + if (bindata_of_ipv4_or_ipv6_address->size == 4) { + const char *ipStr = inet_ntop(AF_INET, + bindata_of_ipv4_or_ipv6_address->data, + buff, + 256); + if (ipStr) { + return strdup(ipStr); + } + } else if (bindata_of_ipv4_or_ipv6_address->size == 16) { + const char *ipStr = inet_ntop(AF_INET6, + bindata_of_ipv4_or_ipv6_address->data, + buff, + 256); + if (ipStr) { + return strdup(ipStr); + } + } + return NULL; } getdns_return_t getdns_strerror(getdns_return_t err, char *buf, size_t buflen) { - getdns_return_t retval = GETDNS_RETURN_GOOD; + getdns_return_t retval = GETDNS_RETURN_GOOD; - const char* err_str = getdns_get_errorstr_by_id(err); - if (!err_str) { - return GETDNS_RETURN_GENERIC_ERROR; - } + const char *err_str = getdns_get_errorstr_by_id(err); + if (!err_str) { + return GETDNS_RETURN_GENERIC_ERROR; + } - snprintf(buf, buflen, "%s", err_str); + snprintf(buf, buflen, "%s", err_str); - return retval; -} /* getdns_strerror */ + return retval; +} /* getdns_strerror */ /* getdns_core_only.c */ diff --git a/src/dict.c b/src/dict.c index 9e51d1c4..da029790 100644 --- a/src/dict.c +++ b/src/dict.c @@ -50,183 +50,177 @@ */ struct getdns_dict_item * getdns_dict_find(struct getdns_dict *dict, char *key, bool addifnotfnd) -{ - struct getdns_dict_item *item = NULL; +{ + struct getdns_dict_item *item = NULL; - if(dict != NULL && key != NULL) - { - item = (struct getdns_dict_item *)ldns_rbtree_search(&(dict->root), key); - if(addifnotfnd == true && item == NULL) - { - /* tsearch will add a node automatically for us */ - item = (struct getdns_dict_item *) malloc(sizeof(struct getdns_dict_item)); - item->node.key = strdup(key); - item->dtype = t_invalid; - item->data.n = 0; - ldns_rbtree_insert(&(dict->root), (ldns_rbnode_t *)item); - } - } - return item; -} /* getdns_dict_find */ + if (dict != NULL && key != NULL) { + item = + (struct getdns_dict_item *) ldns_rbtree_search(&(dict-> + root), key); + if (addifnotfnd == true && item == NULL) { + /* tsearch will add a node automatically for us */ + item = + (struct getdns_dict_item *) malloc(sizeof(struct + getdns_dict_item)); + item->node.key = strdup(key); + item->dtype = t_invalid; + item->data.n = 0; + ldns_rbtree_insert(&(dict->root), + (ldns_rbnode_t *) item); + } + } + return item; +} /* getdns_dict_find */ /*---------------------------------------- getdns_dict_get_names */ getdns_return_t -getdns_dict_get_names(struct getdns_dict *dict, struct getdns_list **answer) +getdns_dict_get_names(struct getdns_dict * dict, struct getdns_list ** answer) { - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; - struct getdns_dict_item *item; - size_t index; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; + struct getdns_dict_item *item; + size_t index; - if(dict != NULL && answer != NULL) - { - *answer = getdns_list_create(); + if (dict != NULL && answer != NULL) { + *answer = getdns_list_create(); - LDNS_RBTREE_FOR(item, struct getdns_dict_item *, &(dict->root)) - { - if(getdns_list_add_item(*answer, &index) == GETDNS_RETURN_GOOD) - { - struct getdns_bindata bindata; - bindata.size = strlen(item->node.key); - bindata.data = (void *)item->node.key; - getdns_list_set_bindata(*answer, index, &bindata); - } - } - retval = GETDNS_RETURN_GOOD; - } - return retval; -} /* getdns_dict_get_names */ + LDNS_RBTREE_FOR(item, struct getdns_dict_item *, &(dict->root)) + { + if (getdns_list_add_item(*answer, + &index) == GETDNS_RETURN_GOOD) { + struct getdns_bindata bindata; + bindata.size = strlen(item->node.key); + bindata.data = (void *) item->node.key; + getdns_list_set_bindata(*answer, index, + &bindata); + } + } + retval = GETDNS_RETURN_GOOD; + } + return retval; +} /* getdns_dict_get_names */ /*---------------------------------------- getdns_dict_get_data_type */ getdns_return_t -getdns_dict_get_data_type(struct getdns_dict *dict, char *name, getdns_data_type *answer) -{ - struct getdns_dict_item *item; - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; +getdns_dict_get_data_type(struct getdns_dict * dict, char *name, + getdns_data_type * answer) +{ + struct getdns_dict_item *item; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; - if(dict != NULL && name != NULL && answer != NULL) - { - item = getdns_dict_find(dict, name, false); - if(item != NULL) - { - *answer = item->dtype; - retval = GETDNS_RETURN_GOOD; - } - } + if (dict != NULL && name != NULL && answer != NULL) { + item = getdns_dict_find(dict, name, false); + if (item != NULL) { + *answer = item->dtype; + retval = GETDNS_RETURN_GOOD; + } + } - return retval; -} /* getdns_dict_get_data_type */ + return retval; +} /* getdns_dict_get_data_type */ /*---------------------------------------- getdns_dict_get_dict */ getdns_return_t -getdns_dict_get_dict(struct getdns_dict *dict, char *name, struct getdns_dict **answer) -{ - struct getdns_dict_item *item; - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; +getdns_dict_get_dict(struct getdns_dict * dict, char *name, + struct getdns_dict ** answer) +{ + struct getdns_dict_item *item; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; - if(dict != NULL && name != NULL && answer != NULL) - { - item = getdns_dict_find(dict, name, false); - if(item != NULL) - { - if(item->dtype != t_dict) - retval = GETDNS_RETURN_WRONG_TYPE_REQUESTED; - else - { - *answer = item->data.dict; - retval = GETDNS_RETURN_GOOD; - } - } - } + if (dict != NULL && name != NULL && answer != NULL) { + item = getdns_dict_find(dict, name, false); + if (item != NULL) { + if (item->dtype != t_dict) + retval = GETDNS_RETURN_WRONG_TYPE_REQUESTED; + else { + *answer = item->data.dict; + retval = GETDNS_RETURN_GOOD; + } + } + } - return retval; -} /* getdns_dict_get_dict */ + return retval; +} /* getdns_dict_get_dict */ /*---------------------------------------- getdns_dict_get_list */ getdns_return_t -getdns_dict_get_list(struct getdns_dict *dict, char *name, struct getdns_list **answer) -{ - struct getdns_dict_item *item; - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; +getdns_dict_get_list(struct getdns_dict * dict, char *name, + struct getdns_list ** answer) +{ + struct getdns_dict_item *item; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; - if(dict != NULL && name != NULL && answer != NULL) - { - item = getdns_dict_find(dict, name, false); - if(item != NULL) - { - if(item->dtype != t_list) - retval = GETDNS_RETURN_WRONG_TYPE_REQUESTED; - else - { - *answer = item->data.list; - retval = GETDNS_RETURN_GOOD; - } - } - } + if (dict != NULL && name != NULL && answer != NULL) { + item = getdns_dict_find(dict, name, false); + if (item != NULL) { + if (item->dtype != t_list) + retval = GETDNS_RETURN_WRONG_TYPE_REQUESTED; + else { + *answer = item->data.list; + retval = GETDNS_RETURN_GOOD; + } + } + } - return retval; -} /* getdns_dict_get_list */ + return retval; +} /* getdns_dict_get_list */ /*---------------------------------------- getdns_dict_get_bindata */ getdns_return_t -getdns_dict_get_bindata(struct getdns_dict *dict, char *name, struct getdns_bindata **answer) -{ - struct getdns_dict_item *item; - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; +getdns_dict_get_bindata(struct getdns_dict * dict, char *name, + struct getdns_bindata ** answer) +{ + struct getdns_dict_item *item; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; - if(dict != NULL && name != NULL && answer != NULL) - { - item = getdns_dict_find(dict, name, false); - if(item != NULL) - { - if(item->dtype != t_bindata) - retval = GETDNS_RETURN_WRONG_TYPE_REQUESTED; - else - { - *answer = item->data.bindata; - retval = GETDNS_RETURN_GOOD; - } - } - } + if (dict != NULL && name != NULL && answer != NULL) { + item = getdns_dict_find(dict, name, false); + if (item != NULL) { + if (item->dtype != t_bindata) + retval = GETDNS_RETURN_WRONG_TYPE_REQUESTED; + else { + *answer = item->data.bindata; + retval = GETDNS_RETURN_GOOD; + } + } + } - return retval; -} /* getdns_dict_get_bindata */ + return retval; +} /* getdns_dict_get_bindata */ /*---------------------------------------- getdns_dict_get_int */ getdns_return_t -getdns_dict_get_int(struct getdns_dict *dict, char *name, uint32_t *answer) -{ - struct getdns_dict_item *item; - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; +getdns_dict_get_int(struct getdns_dict * dict, char *name, uint32_t * answer) +{ + struct getdns_dict_item *item; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; - if(dict != NULL && name != NULL && answer != NULL) - { - item = getdns_dict_find(dict, name, false); - if(item != NULL) - { - if(item->dtype != t_int) - retval = GETDNS_RETURN_WRONG_TYPE_REQUESTED; - else - { - *answer = item->data.n; - retval = GETDNS_RETURN_GOOD; - } - } - } + if (dict != NULL && name != NULL && answer != NULL) { + item = getdns_dict_find(dict, name, false); + if (item != NULL) { + if (item->dtype != t_int) + retval = GETDNS_RETURN_WRONG_TYPE_REQUESTED; + else { + *answer = item->data.n; + retval = GETDNS_RETURN_GOOD; + } + } + } - return retval; -} /* getdns_dict_get_int */ + return retval; +} /* getdns_dict_get_int */ /*---------------------------------------- getdns_dict_create */ struct getdns_dict * getdns_dict_create() { - struct getdns_dict *dict; + struct getdns_dict *dict; - dict = (struct getdns_dict *) malloc(sizeof(struct getdns_dict)); - ldns_rbtree_init(&(dict->root), (int (*)(const void *, const void *))strcmp); - return dict; -} /* getdns_dict_create */ + dict = (struct getdns_dict *) malloc(sizeof(struct getdns_dict)); + ldns_rbtree_init(&(dict->root), (int (*)(const void *, + const void *)) strcmp); + return dict; +} /* getdns_dict_create */ /*---------------------------------------- getdns_dict_copy */ /** @@ -238,48 +232,51 @@ getdns_dict_create() * @return NULL on error (out of memory, invalid srcdict) */ getdns_return_t -getdns_dict_copy(struct getdns_dict *srcdict, struct getdns_dict **dstdict) +getdns_dict_copy(struct getdns_dict * srcdict, struct getdns_dict ** dstdict) { - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; - struct getdns_dict_item *item; - char *key; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; + struct getdns_dict_item *item; + char *key; - if(srcdict != NULL && dstdict != NULL) - { - *dstdict = getdns_dict_create(); + if (srcdict != NULL && dstdict != NULL) { + *dstdict = getdns_dict_create(); - LDNS_RBTREE_FOR(item, struct getdns_dict_item *, &(srcdict->root)) - { - key = (char *)item->node.key; - switch(item->dtype) - { - case t_bindata: - getdns_dict_set_bindata(*dstdict, key, item->data.bindata); - break; + LDNS_RBTREE_FOR(item, struct getdns_dict_item *, + &(srcdict->root)) + { + key = (char *) item->node.key; + switch (item->dtype) { + case t_bindata: + getdns_dict_set_bindata(*dstdict, key, + item->data.bindata); + break; - case t_dict: - getdns_dict_set_dict(*dstdict, key, item->data.dict); - break; + case t_dict: + getdns_dict_set_dict(*dstdict, key, + item->data.dict); + break; - case t_int: - getdns_dict_set_int(*dstdict, key, item->data.n); - break; + case t_int: + getdns_dict_set_int(*dstdict, key, + item->data.n); + break; - case t_list: - getdns_dict_set_list(*dstdict, key, item->data.list); - break; + case t_list: + getdns_dict_set_list(*dstdict, key, + item->data.list); + break; - case t_invalid: - default: - // TODO: this is a fault of some kind, for now ignore it - break; - } - } - retval = GETDNS_RETURN_GOOD; - } + case t_invalid: + default: + // TODO: this is a fault of some kind, for now ignore it + break; + } + } + retval = GETDNS_RETURN_GOOD; + } - return retval; -} /* getdns_dict_copy */ + return retval; +} /* getdns_dict_copy */ /*---------------------------------------- getdns_dict_item_free */ /** @@ -288,150 +285,140 @@ getdns_dict_copy(struct getdns_dict *srcdict, struct getdns_dict **dstdict) * @return void */ void -getdns_dict_item_free(ldns_rbnode_t *node, void *arg) +getdns_dict_item_free(ldns_rbnode_t * node, void *arg) { - (void) arg; - struct getdns_dict_item *item = (struct getdns_dict_item *)node; - if(item != NULL) - { - if(item->dtype == t_bindata) - { - if(item->data.bindata->size > 0) - free(item->data.bindata->data); - free(item->data.bindata); - } - else if(item->dtype == t_dict) - { - getdns_dict_destroy(item->data.dict); - } - else if(item->dtype == t_list) - { - getdns_list_destroy(item->data.list); - } + (void) arg; + struct getdns_dict_item *item = (struct getdns_dict_item *) node; + if (item != NULL) { + if (item->dtype == t_bindata) { + if (item->data.bindata->size > 0) + free(item->data.bindata->data); + free(item->data.bindata); + } else if (item->dtype == t_dict) { + getdns_dict_destroy(item->data.dict); + } else if (item->dtype == t_list) { + getdns_list_destroy(item->data.list); + } - if(item->node.key != NULL) - free((char *)item->node.key); - free(item); - } -} /* getdns_dict_item_free */ + if (item->node.key != NULL) + free((char *) item->node.key); + free(item); + } +} /* getdns_dict_item_free */ /*---------------------------------------- getdns_dict_destroy */ void getdns_dict_destroy(struct getdns_dict *dict) { - if(dict != NULL) - { - ldns_traverse_postorder(&(dict->root), getdns_dict_item_free, NULL); - free(dict); - } + if (dict != NULL) { + ldns_traverse_postorder(&(dict->root), getdns_dict_item_free, + NULL); + free(dict); + } - return; -} /* getdns_dict_destroy */ + return; +} /* getdns_dict_destroy */ /*---------------------------------------- getdns_dict_set_dict */ getdns_return_t -getdns_dict_set_dict(struct getdns_dict *dict, char *name, struct getdns_dict *child_dict) -{ - struct getdns_dict_item *item; - struct getdns_dict *newdict; - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; +getdns_dict_set_dict(struct getdns_dict * dict, char *name, + struct getdns_dict * child_dict) +{ + struct getdns_dict_item *item; + struct getdns_dict *newdict; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; - if(dict != NULL && name != NULL) - { - item = getdns_dict_find(dict, name, true); - if(item != NULL) - { - retval = getdns_dict_copy(child_dict, &newdict); - if(retval == GETDNS_RETURN_GOOD) - { - item->dtype = t_dict; - item->data.dict = newdict; - } - else - item->dtype = t_invalid; - } - } + if (dict != NULL && name != NULL) { + item = getdns_dict_find(dict, name, true); + if (item != NULL) { + retval = getdns_dict_copy(child_dict, &newdict); + if (retval == GETDNS_RETURN_GOOD) { + item->dtype = t_dict; + item->data.dict = newdict; + } else + item->dtype = t_invalid; + } + } - return retval; -} /* getdns_dict_set_dict */ + return retval; +} /* getdns_dict_set_dict */ /*---------------------------------------- getdns_dict_set_list */ getdns_return_t -getdns_dict_set_list(struct getdns_dict *dict, char *name, struct getdns_list *child_list) -{ - struct getdns_dict_item *item; - struct getdns_list *newlist; - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; +getdns_dict_set_list(struct getdns_dict * dict, char *name, + struct getdns_list * child_list) +{ + struct getdns_dict_item *item; + struct getdns_list *newlist; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; - if(dict != NULL && name != NULL) - { - item = getdns_dict_find(dict, name, true); - if(item != NULL) - { - retval = getdns_list_copy(child_list, &newlist); - if(retval == GETDNS_RETURN_GOOD) - { - item->dtype = t_list; - item->data.list = newlist; - } - else - item->dtype = t_invalid; - } - } + if (dict != NULL && name != NULL) { + item = getdns_dict_find(dict, name, true); + if (item != NULL) { + retval = getdns_list_copy(child_list, &newlist); + if (retval == GETDNS_RETURN_GOOD) { + item->dtype = t_list; + item->data.list = newlist; + } else + item->dtype = t_invalid; + } + } - return retval; -} /* getdns_dict_set_list */ + return retval; +} /* getdns_dict_set_list */ /*---------------------------------------- getdns_dict_set_bindata */ getdns_return_t -getdns_dict_set_bindata(struct getdns_dict *dict, char *name, struct getdns_bindata *child_bindata) -{ - struct getdns_dict_item *item; - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; +getdns_dict_set_bindata(struct getdns_dict * dict, char *name, + struct getdns_bindata * child_bindata) +{ + struct getdns_dict_item *item; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; - if(dict != NULL && name != NULL && child_bindata != NULL) - { - item = getdns_dict_find(dict, name, true); - if(item != NULL) - { - item->dtype = t_bindata; - item->data.bindata = (struct getdns_bindata *) malloc(sizeof(struct getdns_bindata)); - if(item->data.bindata != NULL) - { - item->data.bindata->data = (void *) malloc(child_bindata->size); - if(item->data.bindata->data != NULL) - { - item->data.bindata->size = child_bindata->size; - memcpy(item->data.bindata->data, child_bindata->data, child_bindata->size); - retval = GETDNS_RETURN_GOOD; - } - } - } - } + if (dict != NULL && name != NULL && child_bindata != NULL) { + item = getdns_dict_find(dict, name, true); + if (item != NULL) { + item->dtype = t_bindata; + item->data.bindata = + (struct getdns_bindata *) malloc(sizeof(struct + getdns_bindata)); + if (item->data.bindata != NULL) { + item->data.bindata->data = + (void *) malloc(child_bindata->size); + if (item->data.bindata->data != NULL) { + item->data.bindata->size = + child_bindata->size; + memcpy(item->data.bindata->data, + child_bindata->data, + child_bindata->size); + retval = GETDNS_RETURN_GOOD; + } + } + } + } - return retval; -} /* getdns_dict_set_bindata */ + return retval; +} /* getdns_dict_set_bindata */ /*---------------------------------------- getdns_dict_set_int */ getdns_return_t -getdns_dict_set_int(struct getdns_dict *dict, char *name, uint32_t child_uint32) -{ - struct getdns_dict_item *item; - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; +getdns_dict_set_int(struct getdns_dict * dict, char *name, + uint32_t child_uint32) +{ + struct getdns_dict_item *item; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_DICT_NAME; - if(dict != NULL && name != NULL) - { - item = getdns_dict_find(dict, name, true); - if(item != NULL) - { - item->dtype = t_int; - item->data.n = child_uint32; - retval = GETDNS_RETURN_GOOD; - } - } + if (dict != NULL && name != NULL) { + item = getdns_dict_find(dict, name, true); + if (item != NULL) { + item->dtype = t_int; + item->data.n = child_uint32; + retval = GETDNS_RETURN_GOOD; + } + } - return retval; -} /* getdns_dict_set_int */ + return retval; +} /* getdns_dict_set_int */ /*---------------------------------------- getdns_pp_dict */ /** @@ -442,10 +429,10 @@ getdns_dict_set_int(struct getdns_dict *dict, char *name, uint32_t child_uint32) static const char * getdns_indent(size_t indent) { - static const char *spaces = " " - " "; - return spaces + 80 - (indent < 80 ? indent : 0); -} /* getdns_indent */ + static const char *spaces = " " + " "; + return spaces + 80 - (indent < 80 ? indent : 0); +} /* getdns_indent */ /*---------------------------------------- getdns_pp_bindata */ /** @@ -457,44 +444,45 @@ getdns_indent(size_t indent) * if an output error is encountered, a negative value */ static int -getdns_pp_bindata(ldns_buffer *buf, size_t indent, struct getdns_bindata *bindata) +getdns_pp_bindata(ldns_buffer * buf, size_t indent, + struct getdns_bindata *bindata) { - size_t i, p = ldns_buffer_position(buf); - uint8_t *dptr; + size_t i, p = ldns_buffer_position(buf); + uint8_t *dptr; - if(ldns_buffer_printf(buf, " size && bindata->data[bindata->size - 1] == 0) - while(i < bindata->size - 1 && isprint(bindata->data[i])) - i++; - - if(i >= bindata->size - 1) { /* all chars were printable */ - if(ldns_buffer_printf(buf, "for \"%s\">", bindata->data) < 0) - return -1; - } else { - if(ldns_buffer_printf(buf, "of 0x") < 0) - return -1; - for(dptr = bindata->data; dptr < bindata->data + bindata->size; dptr++) { - if(dptr - bindata->data >= 16) { - if(ldns_buffer_printf(buf, "...") < 0) - return -1; - break; - } - if(ldns_buffer_printf(buf, "%.2x", *dptr) < 0) - return -1; - } - if(ldns_buffer_printf(buf, ">") < 0) - return -1; - } - return ldns_buffer_position(buf) - p; -} /* getdns_pp_bindata */ + /* Walk through all printable characters */ + i = 0; + if (bindata->size && bindata->data[bindata->size - 1] == 0) + while (i < bindata->size - 1 && isprint(bindata->data[i])) + i++; + if (i >= bindata->size - 1) { /* all chars were printable */ + if (ldns_buffer_printf(buf, "for \"%s\">", bindata->data) < 0) + return -1; + } else { + if (ldns_buffer_printf(buf, "of 0x") < 0) + return -1; + for (dptr = bindata->data; + dptr < bindata->data + bindata->size; dptr++) { + if (dptr - bindata->data >= 16) { + if (ldns_buffer_printf(buf, "...") < 0) + return -1; + break; + } + if (ldns_buffer_printf(buf, "%.2x", *dptr) < 0) + return -1; + } + if (ldns_buffer_printf(buf, ">") < 0) + return -1; + } + return ldns_buffer_position(buf) - p; +} /* getdns_pp_bindata */ static int -getdns_pp_dict(ldns_buffer *buf, size_t indent, struct getdns_dict *dict); +getdns_pp_dict(ldns_buffer * buf, size_t indent, struct getdns_dict *dict); /*---------------------------------------- getdns_pp_list */ /** @@ -506,75 +494,81 @@ getdns_pp_dict(ldns_buffer *buf, size_t indent, struct getdns_dict *dict); * if an output error is encountered, a negative value */ static int -getdns_pp_list(ldns_buffer *buf, size_t indent, struct getdns_list *list) +getdns_pp_list(ldns_buffer * buf, size_t indent, struct getdns_list *list) { - size_t i, length, p = ldns_buffer_position(buf); - getdns_data_type dtype; - struct getdns_dict *dict_item; - struct getdns_list *list_item; - struct getdns_bindata *bindata_item; - uint32_t int_item; + size_t i, length, p = ldns_buffer_position(buf); + getdns_data_type dtype; + struct getdns_dict *dict_item; + struct getdns_list *list_item; + struct getdns_bindata *bindata_item; + uint32_t int_item; - if(list == NULL) - return 0; + if (list == NULL) + return 0; - if(ldns_buffer_printf(buf, "[") < 0) - return -1; + if (ldns_buffer_printf(buf, "[") < 0) + return -1; - if (getdns_list_get_length(list, &length) != GETDNS_RETURN_GOOD) - return -1; + if (getdns_list_get_length(list, &length) != GETDNS_RETURN_GOOD) + return -1; - indent += 2; - for(i = 0; i < length; i++) - { - if(ldns_buffer_printf(buf, "%s\n%s", (i ? "," : ""), - getdns_indent(indent)) < 0) - return -1; + indent += 2; + for (i = 0; i < length; i++) { + if (ldns_buffer_printf(buf, "%s\n%s", (i ? "," : ""), + getdns_indent(indent)) < 0) + return -1; - if(getdns_list_get_data_type(list, i, &dtype) != GETDNS_RETURN_GOOD) - return -1; + if (getdns_list_get_data_type(list, i, + &dtype) != GETDNS_RETURN_GOOD) + return -1; - switch(dtype) { - case t_int : if (getdns_list_get_int(list, i, &int_item) != - GETDNS_RETURN_GOOD) - if(ldns_buffer_printf(buf, " %d", (int)int_item) < 0) - return -1; - break; + switch (dtype) { + case t_int: + if (getdns_list_get_int(list, i, &int_item) != + GETDNS_RETURN_GOOD) + if (ldns_buffer_printf(buf, " %d", + (int) int_item) < 0) + return -1; + break; - case t_bindata: if (getdns_list_get_bindata(list, i, &bindata_item) != - GETDNS_RETURN_GOOD) - return -1; - if (getdns_pp_bindata(buf, indent, bindata_item) < 0) - return -1; - break; + case t_bindata: + if (getdns_list_get_bindata(list, i, &bindata_item) != + GETDNS_RETURN_GOOD) + return -1; + if (getdns_pp_bindata(buf, indent, bindata_item) < 0) + return -1; + break; - case t_list : if (getdns_list_get_list(list, i, &list_item) != - GETDNS_RETURN_GOOD) - return -1; - if (getdns_pp_list(buf, indent, list_item) < 0) - return -1; - break; + case t_list: + if (getdns_list_get_list(list, i, &list_item) != + GETDNS_RETURN_GOOD) + return -1; + if (getdns_pp_list(buf, indent, list_item) < 0) + return -1; + break; - case t_dict : if (getdns_list_get_dict(list, i, &dict_item) != - GETDNS_RETURN_GOOD) - return -1; - if (getdns_pp_dict(buf, indent, dict_item) < 0) - return -1; - break; + case t_dict: + if (getdns_list_get_dict(list, i, &dict_item) != + GETDNS_RETURN_GOOD) + return -1; + if (getdns_pp_dict(buf, indent, dict_item) < 0) + return -1; + break; - case t_invalid: - default : if(ldns_buffer_printf(buf, " ") < 0) - return -1; - } - i++; - } - indent -= 2; - if (ldns_buffer_printf(buf, i ? "\n%s]" : "]", getdns_indent(indent)) < 0) - return -1; - - return ldns_buffer_position(buf) - p; -} /* getdns_pp_list */ + case t_invalid: + default: + if (ldns_buffer_printf(buf, " ") < 0) + return -1; + } + i++; + } + indent -= 2; + if (ldns_buffer_printf(buf, i ? "\n%s]" : "]", + getdns_indent(indent)) < 0) + return -1; + return ldns_buffer_position(buf) - p; +} /* getdns_pp_list */ /*---------------------------------------- getdns_pp_dict */ /** @@ -586,71 +580,76 @@ getdns_pp_list(ldns_buffer *buf, size_t indent, struct getdns_list *list) * if an output error is encountered, a negative value */ static int -getdns_pp_dict(ldns_buffer *buf, size_t indent, struct getdns_dict *dict) +getdns_pp_dict(ldns_buffer * buf, size_t indent, struct getdns_dict *dict) { - size_t i, length, p = ldns_buffer_position(buf); - struct getdns_dict_item *item; + size_t i, length, p = ldns_buffer_position(buf); + struct getdns_dict_item *item; - if(dict == NULL) - return 0; + if (dict == NULL) + return 0; - if(ldns_buffer_printf(buf, "{") < 0) - return -1; + if (ldns_buffer_printf(buf, "{") < 0) + return -1; - i = 0; - indent += 2; - LDNS_RBTREE_FOR(item, struct getdns_dict_item *, &(dict->root)) - { - if(ldns_buffer_printf(buf, "%s\n%s\"%s\":", (i ? "," : "") - , getdns_indent(indent) - , item->node.key) < 0) - return -1; + i = 0; + indent += 2; + LDNS_RBTREE_FOR(item, struct getdns_dict_item *, &(dict->root)) { + if (ldns_buffer_printf(buf, "%s\n%s\"%s\":", (i ? "," : "") + , getdns_indent(indent) + , item->node.key) < 0) + return -1; - switch(item->dtype) { - case t_int : if(ldns_buffer_printf(buf, " %d", item->data.n) < 0) - return -1; - break; + switch (item->dtype) { + case t_int: + if (ldns_buffer_printf(buf, " %d", item->data.n) < 0) + return -1; + break; - case t_bindata: if (getdns_pp_bindata(buf, indent, - item->data.bindata) < 0) - return -1; - break; + case t_bindata: + if (getdns_pp_bindata(buf, indent, + item->data.bindata) < 0) + return -1; + break; - case t_list : /* Don't put empty lists on a new line */ + case t_list: /* Don't put empty lists on a new line */ - if(getdns_list_get_length(item->data.list, - &length) != GETDNS_RETURN_GOOD) - return -1; - if(length == 0) { - if(ldns_buffer_printf(buf, " []") < 0) - return -1; - break; - } - if(ldns_buffer_printf(buf, "\n%s", getdns_indent(indent)) < 0) - return -1; - if(getdns_pp_list(buf, indent, item->data.list) < 0) - return -1; - break; + if (getdns_list_get_length(item->data.list, + &length) != GETDNS_RETURN_GOOD) + return -1; + if (length == 0) { + if (ldns_buffer_printf(buf, " []") < 0) + return -1; + break; + } + if (ldns_buffer_printf(buf, "\n%s", + getdns_indent(indent)) < 0) + return -1; + if (getdns_pp_list(buf, indent, item->data.list) < 0) + return -1; + break; - case t_dict : if(ldns_buffer_printf(buf, "\n%s", getdns_indent(indent)) < 0) - return -1; - if(getdns_pp_dict(buf, indent, item->data.dict) < 0) - return -1; - break; + case t_dict: + if (ldns_buffer_printf(buf, "\n%s", + getdns_indent(indent)) < 0) + return -1; + if (getdns_pp_dict(buf, indent, item->data.dict) < 0) + return -1; + break; - case t_invalid: - default : if(ldns_buffer_printf(buf, " ") < 0) - return -1; - } - i++; - } - indent -= 2; - if (ldns_buffer_printf(buf, i ? "\n%s}" : "}", getdns_indent(indent)) < 0) - return -1; - - return ldns_buffer_position(buf) - p; -} /* getdns_pp_dict */ + case t_invalid: + default: + if (ldns_buffer_printf(buf, " ") < 0) + return -1; + } + i++; + } + indent -= 2; + if (ldns_buffer_printf(buf, i ? "\n%s}" : "}", + getdns_indent(indent)) < 0) + return -1; + return ldns_buffer_position(buf) - p; +} /* getdns_pp_dict */ /*---------------------------------------- getdns_pretty_print_dict */ /** @@ -663,24 +662,24 @@ getdns_pp_dict(ldns_buffer *buf, size_t indent, struct getdns_dict *dict) char * getdns_pretty_print_dict(struct getdns_dict *dict) { - ldns_buffer *buf; - char *ret; + ldns_buffer *buf; + char *ret; - if (!dict) { - return NULL; - } + if (!dict) { + return NULL; + } - buf = ldns_buffer_new(100); - if (! buf) - return NULL; + buf = ldns_buffer_new(100); + if (!buf) + return NULL; - if (getdns_pp_dict(buf, 0, dict) < 0) { - ldns_buffer_free(buf); - return NULL; - } - ret = (char *)ldns_buffer_export(buf); - ldns_buffer_free(buf); - return ret; -} /* getdns_pretty_print_dict */ + if (getdns_pp_dict(buf, 0, dict) < 0) { + ldns_buffer_free(buf); + return NULL; + } + ret = (char *) ldns_buffer_export(buf); + ldns_buffer_free(buf); + return ret; +} /* getdns_pretty_print_dict */ /* dict.c */ diff --git a/src/dict.h b/src/dict.h index 91ca0180..e8c80e1c 100644 --- a/src/dict.h +++ b/src/dict.h @@ -38,20 +38,22 @@ #include #include -union getdns_item { - struct getdns_list *list; - struct getdns_dict *dict; - uint32_t n; - struct getdns_bindata *bindata; +union getdns_item +{ + struct getdns_list *list; + struct getdns_dict *dict; + uint32_t n; + struct getdns_bindata *bindata; }; /** * this structure represents a single item in a dictionary type */ -struct getdns_dict_item { - ldns_rbnode_t node; - getdns_data_type dtype; - union getdns_item data; +struct getdns_dict_item +{ + ldns_rbnode_t node; + getdns_data_type dtype; + union getdns_item data; }; /** @@ -61,11 +63,11 @@ struct getdns_dict_item { * trees in the std library. The internal implementation may change so the * application should stick to the helper functions. */ -struct getdns_dict { - ldns_rbtree_t root; +struct getdns_dict +{ + ldns_rbtree_t root; }; - #endif /* dict.h */ diff --git a/src/example/example_all_functions.c b/src/example/example_all_functions.c index 24367269..8d824a73 100644 --- a/src/example/example_all_functions.c +++ b/src/example/example_all_functions.c @@ -10,285 +10,187 @@ /* The return values */ getdns_return_t retregular; -char * retcharstar; +char *retcharstar; /* The args */ bool boolarg; -char * charstararg; +char *charstararg; getdns_callback_t callbackarg; uint16_t regulararg; uint16_t *regularptrarg; getdns_transaction_t txidarg; -getdns_transaction_t * txidptrarg; +getdns_transaction_t *txidptrarg; -getdns_data_type * datatypeptrarg; -struct getdns_bindata ** bindataptrarg; -struct getdns_dict * dictarg; -struct getdns_bindata * bindataarg; -struct getdns_list * listarg; -struct getdns_dict ** dictptrarg; -struct getdns_list ** listptrarg; +getdns_data_type *datatypeptrarg; +struct getdns_bindata **bindataptrarg; +struct getdns_dict *dictarg; +struct getdns_bindata *bindataarg; +struct getdns_list *listarg; +struct getdns_dict **dictptrarg; +struct getdns_list **listptrarg; size_t sizetarg; -size_t * sizetptrarg; +size_t *sizetptrarg; getdns_context_t contextarg = NULL; uint8_t uint8arg; uint16_t uint16arg; uint32_t uint32arg; -uint8_t * uint8ptrarg; -uint16_t * uint16ptrarg; -uint32_t * uint32ptrarg; -void * arrayarg; -void allocfunctionarg(size_t foo) {UNUSED_PARAM(foo);} -void deallocfunctionarg(void* foo) {UNUSED_PARAM(foo);} -void setcallbackfunctionarg(struct getdns_context_t *foo1, uint16_t foo2) - {UNUSED_PARAM(foo1);UNUSED_PARAM(foo2);} +uint8_t *uint8ptrarg; +uint16_t *uint16ptrarg; +uint32_t *uint32ptrarg; +void *arrayarg; +void +allocfunctionarg(size_t foo) +{ + UNUSED_PARAM(foo); +} -int main() +void +deallocfunctionarg(void *foo) +{ + UNUSED_PARAM(foo); +} + +void +setcallbackfunctionarg(struct getdns_context_t *foo1, uint16_t foo2) +{ + UNUSED_PARAM(foo1); + UNUSED_PARAM(foo2); +} + +int +main() { -retregular = getdns_general( - contextarg, - charstararg, - uint16arg, - dictarg, - arrayarg, - txidptrarg, - callbackarg -); + retregular = getdns_general(contextarg, + charstararg, + uint16arg, dictarg, arrayarg, txidptrarg, callbackarg); -retregular = getdns_address( - contextarg, - charstararg, - dictarg, - arrayarg, - txidptrarg, - callbackarg -); + retregular = getdns_address(contextarg, + charstararg, dictarg, arrayarg, txidptrarg, callbackarg); -retregular = getdns_hostname( - contextarg, - dictarg, - dictarg, - arrayarg, - txidptrarg, - callbackarg -); + retregular = getdns_hostname(contextarg, + dictarg, dictarg, arrayarg, txidptrarg, callbackarg); -retregular = getdns_service( - contextarg, - charstararg, - dictarg, - arrayarg, - txidptrarg, - callbackarg -); + retregular = getdns_service(contextarg, + charstararg, dictarg, arrayarg, txidptrarg, callbackarg); -retregular = getdns_context_create( - &contextarg, - boolarg -); + retregular = getdns_context_create(&contextarg, boolarg); -retregular = getdns_cancel_callback( - contextarg, - txidarg -); + retregular = getdns_cancel_callback(contextarg, txidarg); -retregular = getdns_general_sync( - contextarg, - charstararg, - uint16arg, - dictarg, - uint32ptrarg, - &dictarg -); + retregular = getdns_general_sync(contextarg, + charstararg, uint16arg, dictarg, uint32ptrarg, &dictarg); -retregular = getdns_address_sync( - contextarg, - charstararg, - dictarg, - uint32ptrarg, - &dictarg -); + retregular = getdns_address_sync(contextarg, + charstararg, dictarg, uint32ptrarg, &dictarg); -retregular = getdns_hostname_sync( - contextarg, - dictarg, - dictarg, - uint32ptrarg, - &dictarg -); + retregular = getdns_hostname_sync(contextarg, + dictarg, dictarg, uint32ptrarg, &dictarg); -retregular = getdns_service_sync( - contextarg, - charstararg, - dictarg, - uint32ptrarg, - &dictarg -); + retregular = getdns_service_sync(contextarg, + charstararg, dictarg, uint32ptrarg, &dictarg); -getdns_free_sync_request_memory( - dictarg -); + getdns_free_sync_request_memory(dictarg); -retregular = getdns_list_get_length(listarg, sizetptrarg); -retregular = getdns_list_get_data_type(listarg, sizetarg, datatypeptrarg); -retregular = getdns_list_get_dict(listarg, sizetarg, dictptrarg); -retregular = getdns_list_get_list(listarg, sizetarg, listptrarg); -retregular = getdns_list_get_bindata(listarg, sizetarg, bindataptrarg); -retregular = getdns_list_get_int(listarg, sizetarg, uint32ptrarg); + retregular = getdns_list_get_length(listarg, sizetptrarg); + retregular = + getdns_list_get_data_type(listarg, sizetarg, datatypeptrarg); + retregular = getdns_list_get_dict(listarg, sizetarg, dictptrarg); + retregular = getdns_list_get_list(listarg, sizetarg, listptrarg); + retregular = getdns_list_get_bindata(listarg, sizetarg, bindataptrarg); + retregular = getdns_list_get_int(listarg, sizetarg, uint32ptrarg); -retregular = getdns_dict_get_names(dictarg, listptrarg); -retregular = getdns_dict_get_data_type(dictarg, charstararg, datatypeptrarg); -retregular = getdns_dict_get_dict(dictarg, charstararg, dictptrarg); -retregular = getdns_dict_get_list(dictarg, charstararg, listptrarg); -retregular = getdns_dict_get_bindata(dictarg, charstararg, bindataptrarg); -retregular = getdns_dict_get_int(dictarg, charstararg, uint32ptrarg); + retregular = getdns_dict_get_names(dictarg, listptrarg); + retregular = + getdns_dict_get_data_type(dictarg, charstararg, datatypeptrarg); + retregular = getdns_dict_get_dict(dictarg, charstararg, dictptrarg); + retregular = getdns_dict_get_list(dictarg, charstararg, listptrarg); + retregular = + getdns_dict_get_bindata(dictarg, charstararg, bindataptrarg); + retregular = getdns_dict_get_int(dictarg, charstararg, uint32ptrarg); -listarg = getdns_list_create(); -retregular = getdns_list_set_dict(listarg, sizetarg, dictarg); -retregular = getdns_list_set_list(listarg, sizetarg, listarg); -retregular = getdns_list_set_bindata(listarg, sizetarg, bindataarg); -retregular = getdns_list_set_int(listarg, sizetarg, uint32arg); + listarg = getdns_list_create(); + retregular = getdns_list_set_dict(listarg, sizetarg, dictarg); + retregular = getdns_list_set_list(listarg, sizetarg, listarg); + retregular = getdns_list_set_bindata(listarg, sizetarg, bindataarg); + retregular = getdns_list_set_int(listarg, sizetarg, uint32arg); -dictarg = getdns_dict_create(); -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); -retcharstar = getdns_pretty_print_dict( - dictarg -); + dictarg = getdns_dict_create(); + 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); + retcharstar = getdns_pretty_print_dict(dictarg); -retcharstar = getdns_convert_fqdn_to_dns_name( - charstararg -); + retcharstar = getdns_convert_fqdn_to_dns_name(charstararg); -retcharstar = getdns_convert_dns_name_to_fqdn( - charstararg -); + retcharstar = getdns_convert_dns_name_to_fqdn(charstararg); -retcharstar = getdns_convert_ulabel_to_alabel( - charstararg -); + retcharstar = getdns_convert_ulabel_to_alabel(charstararg); -retcharstar = getdns_convert_alabel_to_ulabel( - charstararg -); + retcharstar = getdns_convert_alabel_to_ulabel(charstararg); -retregular = getdns_validate_dnssec( - bindataarg, - listarg, - listarg -); + retregular = getdns_validate_dnssec(bindataarg, listarg, listarg); -retcharstar = getdns_display_ip_address( - bindataarg -); + retcharstar = getdns_display_ip_address(bindataarg); -retregular = getdns_context_set_context_update_callback( - contextarg, - setcallbackfunctionarg -); + retregular = getdns_context_set_context_update_callback(contextarg, + setcallbackfunctionarg); -retregular = getdns_context_set_resolution_type( - contextarg, - regulararg -); + retregular = getdns_context_set_resolution_type(contextarg, + regulararg); -retregular = getdns_context_set_namespaces( - contextarg, - sizetarg, - regularptrarg -); + retregular = getdns_context_set_namespaces(contextarg, + sizetarg, regularptrarg); -retregular = getdns_context_set_dns_transport( - contextarg, - regulararg -); + retregular = getdns_context_set_dns_transport(contextarg, regulararg); -retregular = getdns_context_set_limit_outstanding_queries( - contextarg, - uint16arg -); + retregular = getdns_context_set_limit_outstanding_queries(contextarg, + uint16arg); -retregular = getdns_context_set_timeout( - contextarg, - uint16arg -); + retregular = getdns_context_set_timeout(contextarg, uint16arg); -retregular = getdns_context_set_follow_redirects( - contextarg, - regulararg -); + retregular = getdns_context_set_follow_redirects(contextarg, + regulararg); -retregular = getdns_context_set_dns_root_servers( - contextarg, - listarg -); + retregular = getdns_context_set_dns_root_servers(contextarg, listarg); -retregular = getdns_context_set_append_name( - contextarg, - regulararg -); + retregular = getdns_context_set_append_name(contextarg, regulararg); -retregular = getdns_context_set_suffix( - contextarg, - listarg -); + retregular = getdns_context_set_suffix(contextarg, listarg); -retregular = getdns_context_set_dnssec_trust_anchors( - contextarg, - listarg -); + retregular = getdns_context_set_dnssec_trust_anchors(contextarg, + listarg); -retregular = getdns_context_set_dnssec_allowed_skew( - contextarg, - uint16arg -); + retregular = getdns_context_set_dnssec_allowed_skew(contextarg, + uint16arg); -retregular = getdns_context_set_stub_resolution( - contextarg, - listarg -); + retregular = getdns_context_set_stub_resolution(contextarg, listarg); -retregular = getdns_context_set_edns_maximum_udp_payload_size( - contextarg, - uint16arg -); + retregular = + getdns_context_set_edns_maximum_udp_payload_size(contextarg, + uint16arg); -retregular = getdns_context_set_edns_extended_rcode( - contextarg, - uint8arg -); + retregular = getdns_context_set_edns_extended_rcode(contextarg, + uint8arg); -retregular = getdns_context_set_edns_version( - contextarg, - uint8arg -); + retregular = getdns_context_set_edns_version(contextarg, uint8arg); -retregular = getdns_context_set_edns_do_bit( - contextarg, - uint8arg -); + retregular = getdns_context_set_edns_do_bit(contextarg, uint8arg); -retregular = getdns_context_set_memory_allocator( - contextarg, - allocfunctionarg -); + retregular = getdns_context_set_memory_allocator(contextarg, + allocfunctionarg); -retregular = getdns_context_set_memory_deallocator( - contextarg, - deallocfunctionarg -); + retregular = getdns_context_set_memory_deallocator(contextarg, + deallocfunctionarg); -retregular = getdns_context_set_memory_reallocator( - contextarg, - deallocfunctionarg -); + retregular = getdns_context_set_memory_reallocator(contextarg, + deallocfunctionarg); -getdns_list_destroy(listarg); -getdns_dict_destroy(dictarg); -getdns_context_destroy(contextarg); + getdns_list_destroy(listarg); + getdns_dict_destroy(dictarg); + getdns_context_destroy(contextarg); -return(0); } /* End of main() */ + return (0); +} /* End of main() */ diff --git a/src/example/example_simple_answers.c b/src/example/example_simple_answers.c index b8840331..8b92a2af 100644 --- a/src/example/example_simple_answers.c +++ b/src/example/example_simple_answers.c @@ -37,56 +37,64 @@ #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_t *this_context, - uint16_t this_callback_type, - struct getdns_dict *this_response, - void *this_userarg, - getdns_transaction_t this_transaction_id) +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) { - 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 */ - { + 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; - 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); // 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.\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); 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" + 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\n", this_ret); + fprintf(stderr, + "Trying to get the answers failed: %d\n", + this_ret); getdns_dict_destroy(this_response); return; } size_t num_addresses = 0; - 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 */ if (num_addresses == 0) { fprintf(stderr, "There are no addresses.\n"); getdns_dict_destroy(this_response); return; } - for ( size_t rec_count = 0; rec_count < num_addresses; ++rec_count ) - { - struct getdns_bindata * this_address_data; - char* ipAddr = NULL; - this_ret = getdns_list_get_bindata(just_the_addresses_ptr, rec_count, &this_address_data); // Ignore any error + for (size_t rec_count = 0; rec_count < num_addresses; + ++rec_count) { + struct getdns_bindata *this_address_data; + char *ipAddr = NULL; + this_ret = getdns_list_get_bindata(just_the_addresses_ptr, rec_count, &this_address_data); // Ignore any error ipAddr = getdns_display_ip_address(this_address_data); printf("The address is %s\n", ipAddr); free(ipAddr); } - } - else if (this_callback_type == GETDNS_CALLBACK_CANCEL) - fprintf(stderr, "The callback with ID %"PRIu64" was cancelled. Exiting.\n", this_transaction_id); + } 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); + fprintf(stderr, + "The callback got a callback_type of %d. Exiting.\n", + this_callback_type); /* clean up */ getdns_dict_destroy(this_response); @@ -97,41 +105,41 @@ main() { /* Create the DNS context for this call */ struct getdns_context_t *this_context = NULL; - 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); + 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); } /* 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) - { + if (this_event_base == NULL) { fprintf(stderr, "Trying to create the event base failed.\n"); - return(GETDNS_RETURN_GENERIC_ERROR); + return (GETDNS_RETURN_GENERIC_ERROR); } - (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 */ - const char * this_name = "www.example.com"; - char* this_userarg = "somestring"; // Could add things here to help identify this 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; // getdns_context_set_resolution_type(this_context, GETDNS_CONTEXT_STUB); /* Make the call */ - 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.\n", this_name); - return(GETDNS_RETURN_GENERIC_ERROR); + 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.\n", + this_name); + return (GETDNS_RETURN_GENERIC_ERROR); } else if (dns_request_return != GETDNS_RETURN_GOOD) { fprintf(stderr, "The context is not setup properly.\n"); - return(GETDNS_RETURN_GENERIC_ERROR); - } - else - { + return (GETDNS_RETURN_GENERIC_ERROR); + } else { /* Call the event loop */ int dispatch_return = event_base_dispatch(this_event_base); UNUSED_PARAM(dispatch_return); @@ -142,6 +150,6 @@ main() event_base_free(this_event_base); /* Assuming we get here, leave gracefully */ exit(EXIT_SUCCESS); -} /* main */ +} /* main */ /* example-simple-answers.c */ diff --git a/src/example/example_synchronous.c b/src/example/example_synchronous.c index a08ef79a..772af527 100644 --- a/src/example/example_synchronous.c +++ b/src/example/example_synchronous.c @@ -35,73 +35,79 @@ int main() { - getdns_return_t context_create_return; - struct getdns_list *just_the_addresses_ptr; - size_t num_addresses = 0; - size_t rec_count; - struct getdns_bindata *this_address_data; - struct getdns_context_t *this_context = NULL; - uint32_t this_error = 0; - struct getdns_dict *this_extensions = NULL; - const char *this_name = "www.example.com"; - uint8_t this_request_type = GETDNS_RRTYPE_A; - struct getdns_dict *this_response = NULL; - uint32_t this_response_length; - getdns_return_t this_ret; + getdns_return_t context_create_return; + struct getdns_list *just_the_addresses_ptr; + size_t num_addresses = 0; + size_t rec_count; + struct getdns_bindata *this_address_data; + struct getdns_context_t *this_context = NULL; + uint32_t this_error = 0; + struct getdns_dict *this_extensions = NULL; + const char *this_name = "www.example.com"; + uint8_t this_request_type = GETDNS_RRTYPE_A; + struct getdns_dict *this_response = NULL; + uint32_t this_response_length; + getdns_return_t this_ret; /* Create the DNS context for this call */ 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); + if (context_create_return != GETDNS_RETURN_GOOD) { + fprintf(stderr, "Trying to create the context failed: %d", + context_create_return); + return (GETDNS_RETURN_GENERIC_ERROR); } /* Set up the getdns_sync_request call */ /* Get the A and AAAA records */ this_extensions = getdns_dict_create(); - this_ret = getdns_dict_set_int(this_extensions, "return_both_v4_and_v6", GETDNS_EXTENSION_TRUE); - if (this_ret != GETDNS_RETURN_GOOD) - { - fprintf(stderr, "Trying to set an extension do both IPv4 and IPv6 failed: %d", this_ret); - return(GETDNS_RETURN_GENERIC_ERROR); + this_ret = + getdns_dict_set_int(this_extensions, "return_both_v4_and_v6", + GETDNS_EXTENSION_TRUE); + if (this_ret != GETDNS_RETURN_GOOD) { + fprintf(stderr, + "Trying to set an extension do both IPv4 and IPv6 failed: %d", + this_ret); + return (GETDNS_RETURN_GENERIC_ERROR); } /* Make the call */ - getdns_return_t dns_request_return = getdns_general_sync(this_context, this_name, this_request_type, - this_extensions, &this_response_length, &this_response); + getdns_return_t dns_request_return = + getdns_general_sync(this_context, this_name, this_request_type, + this_extensions, &this_response_length, &this_response); /* free the extensions */ getdns_dict_destroy(this_extensions); - 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); - } - else - { + 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); + } else { /* Be sure the search returned something */ - 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); // 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(GETDNS_RETURN_GENERIC_ERROR); + fprintf(stderr, + "The search had no results, and a return value of %d. Exiting.", + this_error); + return (GETDNS_RETURN_GENERIC_ERROR); } - this_ret = getdns_dict_get_list(this_response, "just_address_answers", &just_the_addresses_ptr); // Ignore any error - this_ret = getdns_list_get_length(just_the_addresses_ptr, &num_addresses); // Ignore any error + this_ret = getdns_dict_get_list(this_response, "just_address_answers", &just_the_addresses_ptr); // Ignore any error + this_ret = getdns_list_get_length(just_the_addresses_ptr, &num_addresses); // Ignore any error /* Go through each record */ - if (num_addresses > 0) { - for (rec_count = 0; rec_count < num_addresses; ++rec_count ) - { - char * display; - this_ret = getdns_list_get_bindata(just_the_addresses_ptr, rec_count, &this_address_data); // Ignore any error - display = getdns_display_ip_address(this_address_data); - /* Just print the address */ - printf("The address is %s\n", display); - if (display) { - free(display); - } - } - } + if (num_addresses > 0) { + for (rec_count = 0; rec_count < num_addresses; + ++rec_count) { + char *display; + this_ret = getdns_list_get_bindata(just_the_addresses_ptr, rec_count, &this_address_data); // Ignore any error + display = + getdns_display_ip_address + (this_address_data); + /* Just print the address */ + printf("The address is %s\n", display); + if (display) { + free(display); + } + } + } } /* Clean up */ @@ -109,6 +115,6 @@ main() getdns_free_sync_request_memory(this_response); exit(EXIT_SUCCESS); -} /* main */ +} /* main */ /* example-synchronous.c */ diff --git a/src/example/example_tree.c b/src/example/example_tree.c index 6f8abcf0..45e7a231 100644 --- a/src/example/example_tree.c +++ b/src/example/example_tree.c @@ -9,124 +9,143 @@ #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_t *this_context, - getdns_return_t this_callback_type, - struct getdns_dict *this_response, - void *this_userarg, - getdns_transaction_t this_transaction_id) +void +this_callbackfn(struct getdns_context_t *this_context, + getdns_return_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 */ - { + 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; - 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); // 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); + fprintf(stderr, + "The search had no results, and a return value of %d. Exiting.", + this_error); return; } /* Find all the answers returned */ - struct getdns_list * these_answers; - this_ret = getdns_dict_get_list(this_response, "replies_tree", &these_answers); - if (this_ret == GETDNS_RETURN_NO_SUCH_DICT_NAME) - { - fprintf(stderr, "Weird: the response had no error, but also no replies_tree. Exiting."); + struct getdns_list *these_answers; + this_ret = + getdns_dict_get_list(this_response, "replies_tree", + &these_answers); + if (this_ret == GETDNS_RETURN_NO_SUCH_DICT_NAME) { + fprintf(stderr, + "Weird: the response had no error, but also no replies_tree. Exiting."); return; } size_t num_answers; this_ret = getdns_list_get_length(these_answers, &num_answers); /* Go through each answer */ - for ( size_t rec_count = 0; rec_count < num_answers; ++rec_count ) - { - struct getdns_dict * this_record; - this_ret = getdns_list_get_dict(these_answers, rec_count, &this_record); // Ignore any error + for (size_t rec_count = 0; rec_count < num_answers; + ++rec_count) { + struct getdns_dict *this_record; + this_ret = getdns_list_get_dict(these_answers, rec_count, &this_record); // Ignore any error /* Get the answer section */ - struct getdns_list * this_answer; - this_ret = getdns_dict_get_list(this_record, "answer", &this_answer); // Ignore any error + struct getdns_list *this_answer; + this_ret = getdns_dict_get_list(this_record, "answer", &this_answer); // Ignore any error /* Get each RR in the answer section */ size_t num_rrs_ptr; - this_ret = getdns_list_get_length(this_answer, &num_rrs_ptr); - for ( size_t rr_count = 0; rr_count < num_rrs_ptr; ++rr_count ) - { - struct 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_length(this_answer, &num_rrs_ptr); + for (size_t rr_count = 0; rr_count < num_rrs_ptr; + ++rr_count) { + struct getdns_dict *this_rr = NULL; + this_ret = getdns_list_get_dict(this_answer, rr_count, &this_rr); // Ignore any error /* Get the RDATA */ - struct getdns_dict * this_rdata = NULL; - this_ret = getdns_dict_get_dict(this_rr, "rdata", &this_rdata); // Ignore any error + struct getdns_dict *this_rdata = NULL; + this_ret = getdns_dict_get_dict(this_rr, "rdata", &this_rdata); // Ignore any error /* Get the RDATA 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 it is type A or AAAA, print the value */ - if (this_type == GETDNS_RRTYPE_A) - { - struct getdns_bindata * this_a_record = NULL; - this_ret = getdns_dict_get_bindata(this_rdata, "ipv4_address", &this_a_record); - if (this_ret == GETDNS_RETURN_NO_SUCH_DICT_NAME) - { - fprintf(stderr, "Weird: the A record at %d in record at %d had no address. Exiting.", - (int) rr_count, (int) rec_count); + if (this_type == GETDNS_RRTYPE_A) { + struct getdns_bindata *this_a_record = + NULL; + this_ret = + getdns_dict_get_bindata(this_rdata, + "ipv4_address", &this_a_record); + if (this_ret == + GETDNS_RETURN_NO_SUCH_DICT_NAME) { + fprintf(stderr, + "Weird: the A record at %d in record at %d had no address. Exiting.", + (int) rr_count, + (int) rec_count); return; } - printf("The IPv4 address is %s\n", getdns_display_ip_address(this_a_record)); - } - else if (this_type == GETDNS_RRTYPE_AAAA) - { - struct getdns_bindata * this_aaaa_record = NULL; - this_ret = getdns_dict_get_bindata(this_rdata, "ipv6_address", &this_aaaa_record); - if (this_ret == GETDNS_RETURN_NO_SUCH_DICT_NAME) - { - fprintf(stderr, "Weird: the AAAA record at %d in record at %d had no address. Exiting.", - (int) rr_count, (int) rec_count); + printf("The IPv4 address is %s\n", + getdns_display_ip_address + (this_a_record)); + } else if (this_type == GETDNS_RRTYPE_AAAA) { + struct getdns_bindata *this_aaaa_record + = NULL; + this_ret = + getdns_dict_get_bindata(this_rdata, + "ipv6_address", &this_aaaa_record); + if (this_ret == + GETDNS_RETURN_NO_SUCH_DICT_NAME) { + fprintf(stderr, + "Weird: the AAAA record at %d in record at %d had no address. Exiting.", + (int) rr_count, + (int) rec_count); return; } - printf("The IPv6 address is %s\n", getdns_display_ip_address(this_aaaa_record)); + printf("The IPv6 address is %s\n", + getdns_display_ip_address + (this_aaaa_record)); } } } - } - else if (this_callback_type == GETDNS_CALLBACK_CANCEL) - fprintf(stderr, "The callback with ID %"PRIu64" was cancelled. Exiting.", this_transaction_id); + } 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); + fprintf(stderr, + "The callback got a callback_type of %d. Exiting.", + this_callback_type); } -int main() +int +main() { /* Create the DNS context for this call */ struct getdns_context_t *this_context = NULL; - 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); + 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); } /* 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) - { + if (this_event_base == NULL) { fprintf(stderr, "Trying to create the event base failed."); - return(GETDNS_RETURN_GENERIC_ERROR); + return (GETDNS_RETURN_GENERIC_ERROR); } - (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 */ - const char * this_name = "www.example.com"; - char* this_userarg = "somestring"; // Could add things here to help identify this 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; /* Make the call */ - 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); - } - else - { + 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); + } else { /* Call the event loop */ int dispatch_return = event_base_dispatch(this_event_base); UNUSED_PARAM(dispatch_return); diff --git a/src/general.c b/src/general.c index 060974ee..5a7b48de 100644 --- a/src/general.c +++ b/src/general.c @@ -48,316 +48,311 @@ #define UNUSED_PARAM(x) ((void)(x)) /* declarations */ -static void ub_resolve_callback(void* arg, int err, ldns_buffer* result, int sec, char* bogus); +static void ub_resolve_callback(void *arg, int err, ldns_buffer * result, + int sec, char *bogus); static void ub_resolve_timeout(evutil_socket_t fd, short what, void *arg); -static void ub_local_resolve_timeout(evutil_socket_t fd, short what, void *arg); +static void ub_local_resolve_timeout(evutil_socket_t fd, short what, + void *arg); -static void handle_network_request_error(getdns_network_req* netreq, int err); -static void handle_dns_request_complete(getdns_dns_req* dns_req); -static int submit_network_request(getdns_network_req* netreq); +static void handle_network_request_error(getdns_network_req * netreq, int err); +static void handle_dns_request_complete(getdns_dns_req * dns_req); +static int submit_network_request(getdns_network_req * netreq); -typedef struct netreq_cb_data { - getdns_network_req *netreq; - int err; - ldns_buffer* result; - int sec; - char* bogus; +typedef struct netreq_cb_data +{ + getdns_network_req *netreq; + int err; + ldns_buffer *result; + int sec; + char *bogus; } netreq_cb_data; /* cancel, cleanup and send timeout to callback */ -static void ub_resolve_timeout(evutil_socket_t fd, short what, void *arg) { - getdns_dns_req *dns_req = (getdns_dns_req*) arg; - getdns_context_t context = dns_req->context; - getdns_transaction_t trans_id = dns_req->trans_id; - getdns_callback_t cb = dns_req->user_callback; - void* user_arg = dns_req->user_pointer; +static void +ub_resolve_timeout(evutil_socket_t fd, short what, void *arg) +{ + getdns_dns_req *dns_req = (getdns_dns_req *) arg; + getdns_context_t context = dns_req->context; + getdns_transaction_t trans_id = dns_req->trans_id; + getdns_callback_t cb = dns_req->user_callback; + void *user_arg = dns_req->user_pointer; - /* cancel the req - also clears it from outbound */ - getdns_context_cancel_request(context, trans_id, 0); + /* cancel the req - also clears it from outbound */ + getdns_context_cancel_request(context, trans_id, 0); - /* cleanup */ - dns_req_free(dns_req); + /* cleanup */ + dns_req_free(dns_req); - cb(context, - GETDNS_CALLBACK_TIMEOUT, - NULL, - user_arg, - trans_id); + cb(context, GETDNS_CALLBACK_TIMEOUT, NULL, user_arg, trans_id); } -static void ub_local_resolve_timeout(evutil_socket_t fd, short what, void *arg) { - netreq_cb_data* cb_data = (netreq_cb_data*) arg; - - /* cleanup the local timer here since the memory may be - * invalid after calling ub_resolve_callback - */ - getdns_dns_req* dnsreq = cb_data->netreq->owner; - event_free(dnsreq->local_cb_timer); - dnsreq->local_cb_timer = NULL; +static void +ub_local_resolve_timeout(evutil_socket_t fd, short what, void *arg) +{ + netreq_cb_data *cb_data = (netreq_cb_data *) arg; - /* just call ub_resolve_callback */ - ub_resolve_callback(cb_data->netreq, cb_data->err, cb_data->result, cb_data->sec, cb_data->bogus); + /* cleanup the local timer here since the memory may be + * invalid after calling ub_resolve_callback + */ + getdns_dns_req *dnsreq = cb_data->netreq->owner; + event_free(dnsreq->local_cb_timer); + dnsreq->local_cb_timer = NULL; - /* cleanup the state */ - ldns_buffer_free(cb_data->result); - if (cb_data->bogus) { - free(cb_data->bogus); - } - free(cb_data); + /* just call ub_resolve_callback */ + ub_resolve_callback(cb_data->netreq, cb_data->err, cb_data->result, + cb_data->sec, cb_data->bogus); + + /* cleanup the state */ + ldns_buffer_free(cb_data->result); + if (cb_data->bogus) { + free(cb_data->bogus); + } + free(cb_data); } /* cleanup and send an error to the user callback */ -static void handle_network_request_error(getdns_network_req* netreq, int err) { - getdns_dns_req *dns_req = netreq->owner; - getdns_context_t context = dns_req->context; - getdns_transaction_t trans_id = dns_req->trans_id; - getdns_callback_t cb = dns_req->user_callback; - void* user_arg = dns_req->user_pointer; +static void +handle_network_request_error(getdns_network_req * netreq, int err) +{ + getdns_dns_req *dns_req = netreq->owner; + getdns_context_t context = dns_req->context; + getdns_transaction_t trans_id = dns_req->trans_id; + getdns_callback_t cb = dns_req->user_callback; + void *user_arg = dns_req->user_pointer; - /* clean up */ - getdns_context_clear_outbound_request(dns_req); - dns_req_free(dns_req); + /* clean up */ + getdns_context_clear_outbound_request(dns_req); + dns_req_free(dns_req); - cb(context, - GETDNS_CALLBACK_ERROR, - NULL, - user_arg, - trans_id); + cb(context, GETDNS_CALLBACK_ERROR, NULL, user_arg, trans_id); } /* cleanup and send the response to the user callback */ -static void handle_dns_request_complete(getdns_dns_req* dns_req) { - getdns_dict* response = create_getdns_response(dns_req); +static void +handle_dns_request_complete(getdns_dns_req * dns_req) +{ + getdns_dict *response = create_getdns_response(dns_req); - getdns_context_t context = dns_req->context; - getdns_transaction_t trans_id = dns_req->trans_id; - getdns_callback_t cb = dns_req->user_callback; - void* user_arg = dns_req->user_pointer; + getdns_context_t context = dns_req->context; + getdns_transaction_t trans_id = dns_req->trans_id; + getdns_callback_t cb = dns_req->user_callback; + void *user_arg = dns_req->user_pointer; - /* clean up the request */ - getdns_context_clear_outbound_request(dns_req); - dns_req_free(dns_req); - if (response) { - cb(context, - GETDNS_CALLBACK_COMPLETE, - response, - user_arg, - trans_id); - } else { - cb(context, - GETDNS_CALLBACK_ERROR, - NULL, - user_arg, - trans_id); - } + /* clean up the request */ + getdns_context_clear_outbound_request(dns_req); + dns_req_free(dns_req); + if (response) { + cb(context, + GETDNS_CALLBACK_COMPLETE, response, user_arg, trans_id); + } else { + cb(context, GETDNS_CALLBACK_ERROR, NULL, user_arg, trans_id); + } } -static int submit_network_request(getdns_network_req* netreq) { - getdns_dns_req *dns_req = netreq->owner; - int r = ub_resolve_event(dns_req->unbound, - dns_req->name, - netreq->request_type, - netreq->request_class, - netreq, - ub_resolve_callback, - &(netreq->unbound_id)); - netreq->state = NET_REQ_IN_FLIGHT; - return r; +static int +submit_network_request(getdns_network_req * netreq) +{ + getdns_dns_req *dns_req = netreq->owner; + int r = ub_resolve_event(dns_req->unbound, + dns_req->name, + netreq->request_type, + netreq->request_class, + netreq, + ub_resolve_callback, + &(netreq->unbound_id)); + netreq->state = NET_REQ_IN_FLIGHT; + return r; } +static void +ub_resolve_callback(void *arg, int err, ldns_buffer * result, int sec, + char *bogus) +{ + getdns_network_req *netreq = (getdns_network_req *) arg; + /* if netreq->state == NET_REQ_NOT_SENT here, that implies + * that ub called us back immediately - probably from a local file. + * This most likely means that getdns_general has not returned + */ + if (netreq->state == NET_REQ_NOT_SENT) { + /* just do a very short timer since this was called immediately. + * we can make this less hacky, but it gets interesting when multiple + * netreqs need to be issued and some resolve immediately vs. not. + */ + struct timeval tv; + getdns_dns_req *dnsreq = netreq->owner; + netreq_cb_data *cb_data = + (netreq_cb_data *) malloc(sizeof(netreq_cb_data)); - -static void ub_resolve_callback(void* arg, int err, ldns_buffer* result, int sec, char* bogus) { - getdns_network_req* netreq = (getdns_network_req*) arg; - /* if netreq->state == NET_REQ_NOT_SENT here, that implies - * that ub called us back immediately - probably from a local file. - * This most likely means that getdns_general has not returned - */ - if (netreq->state == NET_REQ_NOT_SENT) { - /* just do a very short timer since this was called immediately. - * we can make this less hacky, but it gets interesting when multiple - * netreqs need to be issued and some resolve immediately vs. not. - */ - struct timeval tv; - getdns_dns_req* dnsreq = netreq->owner; - netreq_cb_data* cb_data = (netreq_cb_data*) malloc(sizeof(netreq_cb_data)); - - cb_data->netreq = netreq; - cb_data->err = err; - cb_data->sec = sec; - cb_data->result = NULL; - cb_data->bogus = NULL; /* unused but here in case we need it */ - if (result) { - cb_data->result = ldns_buffer_new(ldns_buffer_limit(result)); - if (!cb_data->result) { - cb_data->err = GETDNS_RETURN_GENERIC_ERROR; - } else { - /* copy */ - ldns_buffer_copy(cb_data->result, result); - } - } - /* schedule the timeout */ - dnsreq->local_cb_timer = evtimer_new(dnsreq->ev_base, ub_local_resolve_timeout, cb_data); - tv.tv_sec = 0; - /* half ms */ - tv.tv_usec = 500; - evtimer_add(dnsreq->local_cb_timer, &tv); - return; - } - netreq->state = NET_REQ_FINISHED; - if (err) { - handle_network_request_error(netreq, err); - } else { - /* parse */ - ldns_status r = ldns_buffer2pkt_wire(&(netreq->result), result); - if (r != LDNS_STATUS_OK) { - handle_network_request_error(netreq, r); - } else { - /* is this the last request */ - if (!netreq->next) { - /* finished */ - handle_dns_request_complete(netreq->owner); - } else { - /* not finished - update to next request and ship it */ - getdns_dns_req* dns_req = netreq->owner; - dns_req->current_req = netreq->next; - submit_network_request(netreq->next); - } - } - } + cb_data->netreq = netreq; + cb_data->err = err; + cb_data->sec = sec; + cb_data->result = NULL; + cb_data->bogus = NULL; /* unused but here in case we need it */ + if (result) { + cb_data->result = + ldns_buffer_new(ldns_buffer_limit(result)); + if (!cb_data->result) { + cb_data->err = GETDNS_RETURN_GENERIC_ERROR; + } else { + /* copy */ + ldns_buffer_copy(cb_data->result, result); + } + } + /* schedule the timeout */ + dnsreq->local_cb_timer = + evtimer_new(dnsreq->ev_base, ub_local_resolve_timeout, + cb_data); + tv.tv_sec = 0; + /* half ms */ + tv.tv_usec = 500; + evtimer_add(dnsreq->local_cb_timer, &tv); + return; + } + netreq->state = NET_REQ_FINISHED; + if (err) { + handle_network_request_error(netreq, err); + } else { + /* parse */ + ldns_status r = + ldns_buffer2pkt_wire(&(netreq->result), result); + if (r != LDNS_STATUS_OK) { + handle_network_request_error(netreq, r); + } else { + /* is this the last request */ + if (!netreq->next) { + /* finished */ + handle_dns_request_complete(netreq->owner); + } else { + /* not finished - update to next request and ship it */ + getdns_dns_req *dns_req = netreq->owner; + dns_req->current_req = netreq->next; + submit_network_request(netreq->next); + } + } + } } getdns_return_t -getdns_general_ub(struct ub_ctx* unbound, - struct event_base* ev_base, - getdns_context_t context, - const char *name, - uint16_t request_type, - struct getdns_dict *extensions, - void *userarg, - getdns_transaction_t *transaction_id, - getdns_callback_t callbackfn) +getdns_general_ub(struct ub_ctx *unbound, + struct event_base *ev_base, + getdns_context_t context, + const char *name, + uint16_t request_type, + struct getdns_dict *extensions, + void *userarg, + getdns_transaction_t * transaction_id, getdns_callback_t callbackfn) { - /* timeout */ - struct timeval tv; - getdns_return_t gr; - int r; + /* timeout */ + struct timeval tv; + getdns_return_t gr; + int r; - if (!name) { - return GETDNS_RETURN_GENERIC_ERROR; - } + if (!name) { + return GETDNS_RETURN_GENERIC_ERROR; + } - gr = getdns_context_prepare_for_resolution(context); - if (gr != GETDNS_RETURN_GOOD) { - return GETDNS_RETURN_BAD_CONTEXT; - } + gr = getdns_context_prepare_for_resolution(context); + if (gr != GETDNS_RETURN_GOOD) { + return GETDNS_RETURN_BAD_CONTEXT; + } - /* request state */ - getdns_dns_req* req = dns_req_new(context, - unbound, - name, - request_type, - extensions); - if (!req) { - return GETDNS_RETURN_GENERIC_ERROR; - } + /* request state */ + getdns_dns_req *req = dns_req_new(context, + unbound, + name, + request_type, + extensions); + if (!req) { + return GETDNS_RETURN_GENERIC_ERROR; + } - req->user_pointer = userarg; - req->user_callback = callbackfn; + req->user_pointer = userarg; + req->user_callback = callbackfn; - if (transaction_id) { - *transaction_id = req->trans_id; - } + if (transaction_id) { + *transaction_id = req->trans_id; + } - getdns_context_track_outbound_request(req); + getdns_context_track_outbound_request(req); - /* assign a timeout */ - req->ev_base = ev_base; - req->timeout = evtimer_new(ev_base, ub_resolve_timeout, req); - tv.tv_sec = context->timeout / 1000; - tv.tv_usec = (context->timeout % 1000) * 1000; - evtimer_add(req->timeout, &tv); + /* assign a timeout */ + req->ev_base = ev_base; + req->timeout = evtimer_new(ev_base, ub_resolve_timeout, req); + tv.tv_sec = context->timeout / 1000; + tv.tv_usec = (context->timeout % 1000) * 1000; + evtimer_add(req->timeout, &tv); - /* issue the first network req */ + /* issue the first network req */ - r = submit_network_request(req->first_req); + r = submit_network_request(req->first_req); - if (r != 0) { - /* clean up the request */ - getdns_context_clear_outbound_request(req); - dns_req_free(req); - return GETDNS_RETURN_GENERIC_ERROR; - } - return GETDNS_RETURN_GOOD; -} /* getdns_general_ub */ + if (r != 0) { + /* clean up the request */ + getdns_context_clear_outbound_request(req); + dns_req_free(req); + return GETDNS_RETURN_GENERIC_ERROR; + } + return GETDNS_RETURN_GOOD; +} /* getdns_general_ub */ /** * getdns_general */ - getdns_return_t - getdns_general(getdns_context_t context, - const char *name, - uint16_t request_type, - struct getdns_dict *extensions, - void *userarg, - getdns_transaction_t *transaction_id, - getdns_callback_t callback) +getdns_return_t +getdns_general(getdns_context_t context, + const char *name, + uint16_t request_type, + struct getdns_dict * extensions, + void *userarg, + getdns_transaction_t * transaction_id, getdns_callback_t callback) { - int extcheck = GETDNS_RETURN_GOOD; + int extcheck = GETDNS_RETURN_GOOD; - if (!context || !context->event_base_async || - callback == NULL) { - /* Can't do async without an event loop - * or callback - */ - return GETDNS_RETURN_BAD_CONTEXT; - } + if (!context || !context->event_base_async || callback == NULL) { + /* Can't do async without an event loop + * or callback + */ + return GETDNS_RETURN_BAD_CONTEXT; + } - extcheck = validate_extensions(extensions); - if(extcheck != GETDNS_RETURN_GOOD) - return extcheck; + extcheck = validate_extensions(extensions); + if (extcheck != GETDNS_RETURN_GOOD) + return extcheck; - return getdns_general_ub(context->unbound_async, - context->event_base_async, - context, - name, - request_type, - extensions, - userarg, - transaction_id, - callback); + return getdns_general_ub(context->unbound_async, + context->event_base_async, + context, + name, request_type, extensions, userarg, transaction_id, callback); -} /* getdns_general */ +} /* getdns_general */ /* * getdns_address * */ - getdns_return_t - getdns_address(getdns_context_t context, - const char *name, - struct getdns_dict *extensions, - void *userarg, - getdns_transaction_t *transaction_id, - getdns_callback_t callback) { - int cleanup_extensions = 0; - if (!extensions) { - extensions = getdns_dict_create(); - cleanup_extensions = 1; - } - getdns_dict_set_int(extensions, - GETDNS_STR_EXTENSION_RETURN_BOTH_V4_AND_V6, - GETDNS_EXTENSION_TRUE); +getdns_return_t +getdns_address(getdns_context_t context, + const char *name, + struct getdns_dict * extensions, + void *userarg, + getdns_transaction_t * transaction_id, getdns_callback_t callback) +{ + int cleanup_extensions = 0; + if (!extensions) { + extensions = getdns_dict_create(); + cleanup_extensions = 1; + } + getdns_dict_set_int(extensions, + GETDNS_STR_EXTENSION_RETURN_BOTH_V4_AND_V6, GETDNS_EXTENSION_TRUE); - getdns_return_t result = - getdns_general(context, name, GETDNS_RRTYPE_A, - extensions, userarg, transaction_id, - callback); - if (cleanup_extensions) { - getdns_dict_destroy(extensions); - } - return result; + getdns_return_t result = getdns_general(context, name, GETDNS_RRTYPE_A, + extensions, userarg, transaction_id, + callback); + if (cleanup_extensions) { + getdns_dict_destroy(extensions); + } + return result; } /* getdns_general.c */ diff --git a/src/general.h b/src/general.h index 7b4c98e8..31743771 100644 --- a/src/general.h +++ b/src/general.h @@ -44,16 +44,13 @@ struct ub_ctx; struct event_base; getdns_return_t -getdns_general_ub( - struct ub_ctx* unbound, - struct event_base* ev_base, - getdns_context_t context, - const char *name, - uint16_t request_type, - struct getdns_dict *extensions, - void *userarg, - getdns_transaction_t *transaction_id, - getdns_callback_t callbackfn -); +getdns_general_ub(struct ub_ctx *unbound, + struct event_base *ev_base, + getdns_context_t context, + const char *name, + uint16_t request_type, + struct getdns_dict *extensions, + void *userarg, + getdns_transaction_t * transaction_id, getdns_callback_t callbackfn); #endif diff --git a/src/getdns/getdns.h b/src/getdns/getdns.h index 727741cb..ed8ebeff 100644 --- a/src/getdns/getdns.h +++ b/src/getdns/getdns.h @@ -74,7 +74,6 @@ struct event_base; #define GETDNS_RETURN_DNSSEC_WITH_STUB_DISALLOWED 309 #define GETDNS_RETURN_DNSSEC_WITH_STUB_DISALLOWED_TEXT A query was made with a context that is using stub resolution and a DNSSEC extension specified. - /** @} */ @@ -370,17 +369,19 @@ struct event_base; */ typedef struct getdns_context_t *getdns_context_t; -typedef uint16_t getdns_return_t; -typedef uint64_t getdns_transaction_t; +typedef uint16_t getdns_return_t; +typedef uint64_t getdns_transaction_t; /** * used to check data types within complex types (dict, list) */ -typedef enum getdns_data_type { - t_dict, t_list, t_int, t_bindata, t_invalid +typedef enum getdns_data_type +{ + t_dict, t_list, t_int, t_bindata, t_invalid } getdns_data_type; -typedef struct getdns_bindata { - size_t size; - uint8_t *data; +typedef struct getdns_bindata +{ + size_t size; + uint8_t *data; } getdns_bindata; /** @@ -399,7 +400,6 @@ typedef struct getdns_dict getdns_dict; */ getdns_return_t getdns_strerror(getdns_return_t err, char *buf, size_t buflen); - /** * getdns list data type * Use helper functions getdns_list_* to manipulate and iterate lists @@ -414,7 +414,8 @@ typedef struct getdns_list getdns_list; * @return GETDNS_RETURN_GOOD on success * @return GETDNS_RETURN_NO_SUCH_LIST_ITEM if list is not valid or params are NULL */ -getdns_return_t getdns_list_get_length(struct getdns_list *list, size_t *answer); +getdns_return_t getdns_list_get_length(struct getdns_list *list, + size_t * answer); /** * private function (API users should not be calling this), this uses library * routines to make a copy of the list - would be faster to make the copy directly @@ -426,7 +427,8 @@ getdns_return_t getdns_list_get_length(struct getdns_list *list, size_t *answer) * @return GETDNS_RETURN_NO_SUCH_LIST_ITEM if list is invalid * @return GETDNS_RETURN_GENERIC_ERROR if out of memory */ -getdns_return_t getdns_list_copy(struct getdns_list *srclist, struct getdns_list **dstlist); +getdns_return_t getdns_list_copy(struct getdns_list *srclist, + struct getdns_list **dstlist); /** * get the enumerated data type of the indexed list item * @param list the list from which to fetch the data type @@ -435,7 +437,8 @@ getdns_return_t getdns_list_copy(struct getdns_list *srclist, struct getdns_list * @return GETDNS_RETURN_GOOD on success * @return GETDNS_RETURN_NO_SUCH_LIST_ITEM if the index is out of range or the list is NULL */ -getdns_return_t getdns_list_get_data_type(struct getdns_list *list, size_t index, getdns_data_type *answer); +getdns_return_t getdns_list_get_data_type(struct getdns_list *list, + size_t index, getdns_data_type * answer); /** * retrieve the dictionary value of the specified list item, the caller must not free * storage associated with the return value. When the list is destroyed this @@ -447,7 +450,8 @@ getdns_return_t getdns_list_get_data_type(struct getdns_list *list, size_t index * @return GETDNS_RETURN_NO_SUCH_LIST_ITEM if the index is out of range or the list is NULL * @return GETDNS_RETURN_WRONG_TYPE_REQUESTED if the data type does not match the contents of the indexed item */ -getdns_return_t getdns_list_get_dict(struct getdns_list *list, size_t index, struct getdns_dict **answer); +getdns_return_t getdns_list_get_dict(struct getdns_list *list, size_t index, + struct getdns_dict **answer); /** * retrieve the list value of the specified list item, the caller must not free @@ -460,7 +464,8 @@ getdns_return_t getdns_list_get_dict(struct getdns_list *list, size_t index, str * @return GETDNS_RETURN_NO_SUCH_LIST_ITEM if the index is out of range or the list is NULL * @return GETDNS_RETURN_WRONG_TYPE_REQUESTED if the data type does not match the contents of the indexed item */ -getdns_return_t getdns_list_get_list(struct getdns_list *list, size_t index, struct getdns_list **answer); +getdns_return_t getdns_list_get_list(struct getdns_list *list, size_t index, + struct getdns_list **answer); /** * retrieve the binary data value of the specified list item, the caller must not * free storage associated with the return value. When the list is destroyed any @@ -472,7 +477,8 @@ getdns_return_t getdns_list_get_list(struct getdns_list *list, size_t index, str * @return GETDNS_RETURN_NO_SUCH_LIST_ITEM if the index is out of range or the list is NULL * @return GETDNS_RETURN_WRONG_TYPE_REQUESTED if the data type does not match the contents of the indexed item */ -getdns_return_t getdns_list_get_bindata(struct getdns_list *list, size_t index, struct getdns_bindata **answer); +getdns_return_t getdns_list_get_bindata(struct getdns_list *list, size_t index, + struct getdns_bindata **answer); /** * retrieve the integer value of the specified list item * @param list the list from which to fetch the item @@ -482,7 +488,8 @@ getdns_return_t getdns_list_get_bindata(struct getdns_list *list, size_t index, * @return GETDNS_RETURN_NO_SUCH_LIST_ITEM if the index is out of range or the list is NULL * @return GETDNS_RETURN_WRONG_TYPE_REQUESTED if the data type does not match the contents of the indexed item */ -getdns_return_t getdns_list_get_int(struct getdns_list *list, size_t index, uint32_t *answer); +getdns_return_t getdns_list_get_int(struct getdns_list *list, size_t index, + uint32_t * answer); /** * fetch a list of names from the dictionary, this list must be freed by the caller @@ -492,7 +499,8 @@ getdns_return_t getdns_list_get_int(struct getdns_list *list, size_t index, uint * @return GETDNS_RETURN_GOOD on success * @return GETDNS_RETURN_NO_SUCH_DICT_NAME if dict is invalid or empty */ -getdns_return_t getdns_dict_get_names(struct getdns_dict *dict, struct getdns_list **answer); +getdns_return_t getdns_dict_get_names(struct getdns_dict *dict, + struct getdns_list **answer); /** * fetch the data type for the data associated with the specified name * @param this_dict dictionary from which to fetch the data type @@ -501,7 +509,8 @@ getdns_return_t getdns_dict_get_names(struct getdns_dict *dict, struct getdns_li * @return GETDNS_RETURN_GOOD on success * @return GETDNS_RETURN_NO_SUCH_DICT_NAME if dict is invalid or name does not exist */ -getdns_return_t getdns_dict_get_data_type(struct getdns_dict *this_dict, char *name, getdns_data_type *answer); +getdns_return_t getdns_dict_get_data_type(struct getdns_dict *this_dict, + char *name, getdns_data_type * answer); /** * fetch the dictionary associated with the specified name, the dictionary should * not be free()'d by the caller, it will be freed when the parent dictionary is @@ -512,7 +521,8 @@ getdns_return_t getdns_dict_get_data_type(struct getdns_dict *this_dict, char *n * @return GETDNS_RETURN_GOOD on success * @return GETDNS_RETURN_NO_SUCH_DICT_NAME if dict is invalid or name does not exist */ -getdns_return_t getdns_dict_get_dict(struct getdns_dict *this_dict, char *name, struct getdns_dict **answer); +getdns_return_t getdns_dict_get_dict(struct getdns_dict *this_dict, char *name, + struct getdns_dict **answer); /** * fetch the list associated with the specified name * the list should not be free()'d by the caller, when the dictionary is destroyed @@ -523,7 +533,8 @@ getdns_return_t getdns_dict_get_dict(struct getdns_dict *this_dict, char *name, * @return GETDNS_RETURN_GOOD on success * @return GETDNS_RETURN_NO_SUCH_DICT_NAME if dict is invalid or name does not exist */ -getdns_return_t getdns_dict_get_list(struct getdns_dict *this_dict, char *name, struct getdns_list **answer); +getdns_return_t getdns_dict_get_list(struct getdns_dict *this_dict, char *name, + struct getdns_list **answer); /** * fetch the bindata associated with the specified name, the bindata should not be * free()'d by the caller @@ -533,7 +544,8 @@ getdns_return_t getdns_dict_get_list(struct getdns_dict *this_dict, char *name, * @return GETDNS_RETURN_GOOD on success * @return GETDNS_RETURN_NO_SUCH_DICT_NAME if dict is invalid or name does not exist */ -getdns_return_t getdns_dict_get_bindata(struct getdns_dict *this_dict, char *name, struct getdns_bindata **answer); +getdns_return_t getdns_dict_get_bindata(struct getdns_dict *this_dict, + char *name, struct getdns_bindata **answer); /** * fetch the integer value associated with the specified name * @param this_dict dictionary from which to fetch the integer @@ -542,13 +554,14 @@ getdns_return_t getdns_dict_get_bindata(struct getdns_dict *this_dict, char *nam * @return GETDNS_RETURN_GOOD on success * @return GETDNS_RETURN_NO_SUCH_DICT_NAME if dict is invalid or name does not exist */ -getdns_return_t getdns_dict_get_int(struct getdns_dict *this_dict, char *name, uint32_t *answer); +getdns_return_t getdns_dict_get_int(struct getdns_dict *this_dict, char *name, + uint32_t * answer); /** * create a new list with no items * @return pointer to an allocated list, NULL if insufficient memory */ -struct getdns_list * getdns_list_create(); +struct getdns_list *getdns_list_create(); /** * free memory allocated to the list (also frees all children of the list) * note that lists and bindata retrieved from the list via the getdns_list_get_* @@ -566,8 +579,9 @@ void getdns_list_destroy(struct getdns_list *list); * @return GETDNS_RETURN_GOOD on success * @return GETDNS_RETURN_GENERAL_ERROR if out of memory */ -getdns_return_t getdns_list_add_item(struct getdns_list *list, size_t *index); -getdns_return_t getdns_list_set_dict(struct getdns_list *list, size_t index, struct getdns_dict *child_dict); +getdns_return_t getdns_list_add_item(struct getdns_list *list, size_t * index); +getdns_return_t getdns_list_set_dict(struct getdns_list *list, size_t index, + struct getdns_dict *child_dict); /** * assign the child_list to an item in a parent list, the parent list copies * the child list and will free the copy when the list is destroyed @@ -577,7 +591,8 @@ getdns_return_t getdns_list_set_dict(struct getdns_list *list, size_t index, str * @return GETDNS_RETURN_GOOD on success * @return GETDNS_RETURN_NO_SUCH_LIST_ITEM if index is out of range, or list is NULL */ -getdns_return_t getdns_list_set_list(struct getdns_list *list, size_t index, struct getdns_list *child_list); +getdns_return_t getdns_list_set_list(struct getdns_list *list, size_t index, + struct getdns_list *child_list); /** * assign the child_bindata to an item in a parent list, the parent list copies * the child data and will free the copy when the list is destroyed @@ -587,13 +602,15 @@ getdns_return_t getdns_list_set_list(struct getdns_list *list, size_t index, str * @return GETDNS_RETURN_GOOD on success * @return GETDNS_RETURN_NO_SUCH_LIST_ITEM if index is out of range, or list is NULL */ -getdns_return_t getdns_list_set_bindata(struct getdns_list *list, size_t index, struct getdns_bindata *child_bindata); +getdns_return_t getdns_list_set_bindata(struct getdns_list *list, size_t index, + struct getdns_bindata *child_bindata); /** * set the integer value of the indexed item (zero based index) * @return GETDNS_RETURN_GOOD on success * @return GETDNS_RETURN_NO_SUCH_LIST_ITEM if index is out of range, or list is NULL */ -getdns_return_t getdns_list_set_int(struct getdns_list *list, size_t index, uint32_t child_uint32); +getdns_return_t getdns_list_set_int(struct getdns_list *list, size_t index, + uint32_t child_uint32); /** * create a new dictionary with no items @@ -619,7 +636,8 @@ getdns_dict_copy(struct getdns_dict *srcdict, struct getdns_dict **dstdict); */ void getdns_dict_destroy(struct getdns_dict *dict); -getdns_return_t getdns_dict_set_dict(struct getdns_dict *dict, char *name, struct getdns_dict *child_dict); +getdns_return_t getdns_dict_set_dict(struct getdns_dict *dict, char *name, + struct getdns_dict *child_dict); /** * create a new entry in the dictionary, or replace the value of an existing entry * this routine makes a copy of the child_list @@ -628,7 +646,8 @@ getdns_return_t getdns_dict_set_dict(struct getdns_dict *dict, char *name, struc * @param child_list value to assign to the node identified by name * @return GETDNS_RETURN_GOOD on success */ -getdns_return_t getdns_dict_set_list(struct getdns_dict *dict, char *name, struct getdns_list *child_list); +getdns_return_t getdns_dict_set_list(struct getdns_dict *dict, char *name, + struct getdns_list *child_list); /** * create a new entry in the dictionary, or replace the value of an existing entry * this routine makes a copy of the child_bindata @@ -637,7 +656,8 @@ getdns_return_t getdns_dict_set_list(struct getdns_dict *dict, char *name, struc * @param child_bindata value to assign to the node identified by name * @return GETDNS_RETURN_GOOD on success */ -getdns_return_t getdns_dict_set_bindata(struct getdns_dict *dict, char *name, struct getdns_bindata *child_bindata); +getdns_return_t getdns_dict_set_bindata(struct getdns_dict *dict, char *name, + struct getdns_bindata *child_bindata); /** * create a new entry in the dictionary, or replace the value of an existing entry * @param dict dictionary in which to add or change the value @@ -645,71 +665,51 @@ getdns_return_t getdns_dict_set_bindata(struct getdns_dict *dict, char *name, st * @param child_uint32 value to assign to the node identified by name * @return GETDNS_RETURN_GOOD on success */ -getdns_return_t getdns_dict_set_int(struct getdns_dict *dict, char *name, uint32_t child_uint32); +getdns_return_t getdns_dict_set_int(struct getdns_dict *dict, char *name, + uint32_t child_uint32); /* Callback arguments */ -typedef void (*getdns_callback_t)( - getdns_context_t context, - uint16_t callback_type, - struct getdns_dict *response, - void *userarg, - getdns_transaction_t transaction_id); +typedef void (*getdns_callback_t) (getdns_context_t context, + uint16_t callback_type, + struct getdns_dict * response, + void *userarg, getdns_transaction_t transaction_id); /* Function definitions */ getdns_return_t -getdns_general( - getdns_context_t context, - const char *name, - uint16_t request_type, - struct getdns_dict *extensions, - void *userarg, - getdns_transaction_t *transaction_id, - getdns_callback_t callbackfn -); +getdns_general(getdns_context_t context, + const char *name, + uint16_t request_type, + struct getdns_dict *extensions, + void *userarg, + getdns_transaction_t * transaction_id, getdns_callback_t callbackfn); getdns_return_t -getdns_address( - getdns_context_t context, - const char *name, - struct getdns_dict *extensions, - void *userarg, - getdns_transaction_t *transaction_id, - getdns_callback_t callbackfn -); +getdns_address(getdns_context_t context, + const char *name, + struct getdns_dict *extensions, + void *userarg, + getdns_transaction_t * transaction_id, getdns_callback_t callbackfn); getdns_return_t -getdns_hostname( - getdns_context_t context, - struct getdns_dict *address, - struct getdns_dict *extensions, - void *userarg, - getdns_transaction_t *transaction_id, - getdns_callback_t callbackfn -); +getdns_hostname(getdns_context_t context, + struct getdns_dict *address, + struct getdns_dict *extensions, + void *userarg, + getdns_transaction_t * transaction_id, getdns_callback_t callbackfn); getdns_return_t -getdns_service( - getdns_context_t context, - const char *name, - struct getdns_dict *extensions, - void *userarg, - getdns_transaction_t *transaction_id, - getdns_callback_t callbackfn -); +getdns_service(getdns_context_t context, + const char *name, + struct getdns_dict *extensions, + void *userarg, + getdns_transaction_t * transaction_id, getdns_callback_t callbackfn); -getdns_return_t getdns_context_create( - getdns_context_t *context, - int set_from_os -); +getdns_return_t getdns_context_create(getdns_context_t * context, + int set_from_os); -void -getdns_context_destroy( - getdns_context_t context -); +void getdns_context_destroy(getdns_context_t context); getdns_return_t -getdns_cancel_callback( - getdns_context_t context, - getdns_transaction_t transaction_id -); +getdns_cancel_callback(getdns_context_t context, + getdns_transaction_t transaction_id); /** * \defgroup syncfuns Synchronous API functions that do not use callbacks @@ -731,14 +731,11 @@ getdns_cancel_callback( * @return GETDNS_RETURN_GOOD on success */ getdns_return_t -getdns_general_sync( - getdns_context_t context, - const char *name, - uint16_t request_type, - struct getdns_dict *extensions, - uint32_t *response_length, - struct getdns_dict **response -); +getdns_general_sync(getdns_context_t context, + const char *name, + uint16_t request_type, + struct getdns_dict *extensions, + uint32_t * response_length, struct getdns_dict **response); /** * retrieve address assigned to a DNS name @@ -751,13 +748,10 @@ getdns_general_sync( */ getdns_return_t -getdns_address_sync( - getdns_context_t context, - const char *name, - struct getdns_dict *extensions, - uint32_t *response_length, - struct getdns_dict **response -); +getdns_address_sync(getdns_context_t context, + const char *name, + struct getdns_dict *extensions, + uint32_t * response_length, struct getdns_dict **response); /** * retrieve hostname assigned to an IP address @@ -769,13 +763,10 @@ getdns_address_sync( * @return GETDNS_RETURN_GOOD on success */ getdns_return_t -getdns_hostname_sync( - getdns_context_t context, - struct getdns_dict *address, - struct getdns_dict *extensions, - uint32_t *response_length, - struct getdns_dict **response -); +getdns_hostname_sync(getdns_context_t context, + struct getdns_dict *address, + struct getdns_dict *extensions, + uint32_t * response_length, struct getdns_dict **response); /** * retrieve a service assigned to a DNS name @@ -787,48 +778,28 @@ getdns_hostname_sync( * @return GETDNS_RETURN_GOOD on success */ getdns_return_t -getdns_service_sync( - getdns_context_t context, - const char *name, - struct getdns_dict *extensions, - uint32_t *response_length, - struct getdns_dict **response -); +getdns_service_sync(getdns_context_t context, + const char *name, + struct getdns_dict *extensions, + uint32_t * response_length, struct getdns_dict **response); -void -getdns_free_sync_request_memory( - struct getdns_dict *response -); +void getdns_free_sync_request_memory(struct getdns_dict *response); /** @} */ -char * -getdns_convert_dns_name_to_fqdn( - char *name_from_dns_response -); +char *getdns_convert_dns_name_to_fqdn(char *name_from_dns_response); -char * -getdns_convert_fqdn_to_dns_name( - char *fqdn_as_string -); +char *getdns_convert_fqdn_to_dns_name(char *fqdn_as_string); -char * -getdns_convert_ulabel_to_alabel( - char *ulabel -); +char *getdns_convert_ulabel_to_alabel(char *ulabel); -char * -getdns_convert_alabel_to_ulabel( - char *alabel -); +char *getdns_convert_alabel_to_ulabel(char *alabel); getdns_return_t -getdns_validate_dnssec( - struct getdns_bindata *record_to_validate, - struct getdns_list *bundle_of_support_records, - struct getdns_list *trust_anchor_rdatas -); +getdns_validate_dnssec(struct getdns_bindata *record_to_validate, + struct getdns_list *bundle_of_support_records, + struct getdns_list *trust_anchor_rdatas); /** * creates a string that describes the dictionary in a human readable form @@ -837,141 +808,91 @@ getdns_validate_dnssec( * @param dict dictionary to pretty print * @return character array (caller must free this) containing pretty string */ -char * -getdns_pretty_print_dict(struct getdns_dict *dict); +char *getdns_pretty_print_dict(struct getdns_dict *dict); -char * -getdns_display_ip_address( - struct getdns_bindata *bindata_of_ipv4_or_ipv6_address -); +char *getdns_display_ip_address(struct getdns_bindata + *bindata_of_ipv4_or_ipv6_address); getdns_return_t -getdns_context_set_context_update_callback( - getdns_context_t context, - void (*value)(getdns_context_t context, uint16_t changed_item) -); +getdns_context_set_context_update_callback(getdns_context_t context, + void (*value) (getdns_context_t context, uint16_t changed_item) + ); getdns_return_t -getdns_context_set_resolution_type( - getdns_context_t context, - uint16_t value -); +getdns_context_set_resolution_type(getdns_context_t context, uint16_t value); getdns_return_t -getdns_context_set_namespaces( - getdns_context_t context, - size_t namespace_count, - uint16_t *namespaces -); +getdns_context_set_namespaces(getdns_context_t context, + size_t namespace_count, uint16_t * namespaces); getdns_return_t -getdns_context_set_dns_transport( - getdns_context_t context, - uint16_t value -); +getdns_context_set_dns_transport(getdns_context_t context, uint16_t value); getdns_return_t -getdns_context_set_limit_outstanding_queries( - getdns_context_t context, - uint16_t limit -); +getdns_context_set_limit_outstanding_queries(getdns_context_t context, + uint16_t limit); getdns_return_t -getdns_context_set_timeout( - getdns_context_t context, - uint16_t timeout -); +getdns_context_set_timeout(getdns_context_t context, uint16_t timeout); getdns_return_t -getdns_context_set_follow_redirects( - getdns_context_t context, - uint16_t value -); +getdns_context_set_follow_redirects(getdns_context_t context, uint16_t value); getdns_return_t -getdns_context_set_dns_root_servers( - getdns_context_t context, - struct getdns_list *addresses -); +getdns_context_set_dns_root_servers(getdns_context_t context, + struct getdns_list *addresses); getdns_return_t -getdns_context_set_append_name( - getdns_context_t context, - uint16_t value -); +getdns_context_set_append_name(getdns_context_t context, uint16_t value); getdns_return_t -getdns_context_set_suffix( - getdns_context_t context, - struct getdns_list *value -); +getdns_context_set_suffix(getdns_context_t context, struct getdns_list *value); getdns_return_t -getdns_context_set_dnssec_trust_anchors( - getdns_context_t context, - struct getdns_list *value -); +getdns_context_set_dnssec_trust_anchors(getdns_context_t context, + struct getdns_list *value); getdns_return_t -getdns_context_set_dnssec_allowed_skew( - getdns_context_t context, - uint16_t value -); +getdns_context_set_dnssec_allowed_skew(getdns_context_t context, + uint16_t value); getdns_return_t -getdns_context_set_stub_resolution( - getdns_context_t context, - struct getdns_list *upstream_list -); +getdns_context_set_stub_resolution(getdns_context_t context, + struct getdns_list *upstream_list); getdns_return_t -getdns_context_set_edns_maximum_udp_payload_size( - getdns_context_t context, - uint16_t value -); +getdns_context_set_edns_maximum_udp_payload_size(getdns_context_t context, + uint16_t value); getdns_return_t -getdns_context_set_edns_extended_rcode( - getdns_context_t context, - uint8_t value -); +getdns_context_set_edns_extended_rcode(getdns_context_t context, + uint8_t value); getdns_return_t -getdns_context_set_edns_version( - getdns_context_t context, - uint8_t value -); +getdns_context_set_edns_version(getdns_context_t context, uint8_t value); getdns_return_t -getdns_context_set_edns_do_bit( - getdns_context_t context, - uint8_t value -); +getdns_context_set_edns_do_bit(getdns_context_t context, uint8_t value); getdns_return_t -getdns_context_set_memory_allocator( - getdns_context_t context, - void (*value)(size_t somesize) -); +getdns_context_set_memory_allocator(getdns_context_t context, + void (*value) (size_t somesize) + ); getdns_return_t -getdns_context_set_memory_deallocator( - getdns_context_t context, - void (*value)(void*) -); +getdns_context_set_memory_deallocator(getdns_context_t context, + void (*value) (void *) + ); getdns_return_t -getdns_context_set_memory_reallocator( - getdns_context_t context, - void (*value)(void*) -); +getdns_context_set_memory_reallocator(getdns_context_t context, + void (*value) (void *) + ); /* Extension - refactor to abstract async evt loop */ /* For libevent, which we are using for these examples */ getdns_return_t -getdns_extension_set_libevent_base( - getdns_context_t context, - struct event_base *this_event_base -); +getdns_extension_set_libevent_base(getdns_context_t context, + struct event_base *this_event_base); #endif /* GETDNS_H */ diff --git a/src/getdns/getdns_error.h b/src/getdns/getdns_error.h index 311e3f69..614a4e27 100644 --- a/src/getdns/getdns_error.h +++ b/src/getdns/getdns_error.h @@ -32,11 +32,10 @@ #include - - -struct getdns_struct_lookup_table { /* may or may not want to move this into */ - int id; /* getdns.h if it's more generally useful */ - const char *name; +struct getdns_struct_lookup_table +{ /* may or may not want to move this into */ + int id; /* getdns.h if it's more generally useful */ + const char *name; }; typedef struct getdns_struct_lookup_table getdns_lookup_table; diff --git a/src/getdns_error.c b/src/getdns_error.c index bc7832d7..486d1e4d 100644 --- a/src/getdns_error.c +++ b/src/getdns_error.c @@ -26,27 +26,44 @@ * THE SOFTWARE. */ - #include #include getdns_lookup_table getdns_error_str[] = { - { GETDNS_RETURN_GOOD, "Good" }, - { GETDNS_RETURN_GENERIC_ERROR, "Generic error" }, - { GETDNS_RETURN_BAD_DOMAIN_NAME, "Badly-formed domain name" }, - { GETDNS_RETURN_BAD_CONTEXT, "Bad value for a context type" }, - { GETDNS_RETURN_CONTEXT_UPDATE_FAIL, "Did not update the context" }, - { GETDNS_RETURN_UNKNOWN_TRANSACTION, "An attempt was made to cancel a callback with a transaction_id that is not recognized" }, - { GETDNS_RETURN_NO_SUCH_LIST_ITEM, "A helper function for lists had an index argument that was too high" }, - { GETDNS_RETURN_NO_SUCH_DICT_NAME, "A helper function for dicts had a name argument that for a name that is not in the dict" }, - { GETDNS_RETURN_WRONG_TYPE_REQUESTED, "A helper function was supposed to return a certain type for an item, but the wrong type was given" }, - { GETDNS_RETURN_NO_SUCH_EXTENSION, "A name in the extensions dict is not a valid extension" }, - { GETDNS_RETURN_EXTENSION_MISFORMAT, "One or more of the extensions is has a bad format" }, - { GETDNS_RETURN_DNSSEC_WITH_STUB_DISALLOWED, "A query was made with a context that is using stub resolution and a DNSSEC extension specified" }, - { 0, "" } + {GETDNS_RETURN_GOOD, "Good"} + , + {GETDNS_RETURN_GENERIC_ERROR, "Generic error"} + , + {GETDNS_RETURN_BAD_DOMAIN_NAME, "Badly-formed domain name"} + , + {GETDNS_RETURN_BAD_CONTEXT, "Bad value for a context type"} + , + {GETDNS_RETURN_CONTEXT_UPDATE_FAIL, "Did not update the context"} + , + {GETDNS_RETURN_UNKNOWN_TRANSACTION, + "An attempt was made to cancel a callback with a transaction_id that is not recognized"} + , + {GETDNS_RETURN_NO_SUCH_LIST_ITEM, + "A helper function for lists had an index argument that was too high"} + , + {GETDNS_RETURN_NO_SUCH_DICT_NAME, + "A helper function for dicts had a name argument that for a name that is not in the dict"} + , + {GETDNS_RETURN_WRONG_TYPE_REQUESTED, + "A helper function was supposed to return a certain type for an item, but the wrong type was given"} + , + {GETDNS_RETURN_NO_SUCH_EXTENSION, + "A name in the extensions dict is not a valid extension"} + , + {GETDNS_RETURN_EXTENSION_MISFORMAT, + "One or more of the extensions is has a bad format"} + , + {GETDNS_RETURN_DNSSEC_WITH_STUB_DISALLOWED, + "A query was made with a context that is using stub resolution and a DNSSEC extension specified"} + , + {0, ""} }; - /*---------------------------------------- getdns_get_errorstr_by_id() */ /** * return error string from getdns return @@ -55,17 +72,16 @@ getdns_lookup_table getdns_error_str[] = { * @return string containing error message */ - const char * getdns_get_errorstr_by_id(uint16_t err) { - getdns_lookup_table *lt; + getdns_lookup_table *lt; - lt = getdns_error_str; - while (lt->name != 0) { - if (lt->id == err) - return lt->name; - lt++; - } - return 0; + lt = getdns_error_str; + while (lt->name != 0) { + if (lt->id == err) + return lt->name; + lt++; + } + return 0; } diff --git a/src/hostname.c b/src/hostname.c index dbdfe62d..6640b6ae 100644 --- a/src/hostname.c +++ b/src/hostname.c @@ -34,7 +34,6 @@ #include "util-internal.h" #include - /* stuff to make it compile pedantically */ #define UNUSED_PARAM(x) ((void)(x)) @@ -43,37 +42,39 @@ * */ getdns_return_t -getdns_hostname( - getdns_context_t context, - struct getdns_dict *address, - struct getdns_dict *extensions, - void *userarg, - getdns_transaction_t *transaction_id, - getdns_callback_t callback -) +getdns_hostname(getdns_context_t context, + struct getdns_dict * address, + struct getdns_dict * extensions, + void *userarg, + getdns_transaction_t * transaction_id, getdns_callback_t callback) { - struct getdns_bindata *address_data; - struct getdns_bindata *address_type; - uint16_t req_type; - char *name; - getdns_return_t retval; + struct getdns_bindata *address_data; + struct getdns_bindata *address_type; + uint16_t req_type; + char *name; + getdns_return_t retval; + if ((retval = + getdns_dict_get_bindata(address, "address_data", + &address_data)) != GETDNS_RETURN_GOOD) + return retval; + if ((retval = + getdns_dict_get_bindata(address, "address_type", + &address_type)) != GETDNS_RETURN_GOOD) + return retval; + if ((strncmp(GETDNS_STR_IPV4, (char *) address_type->data, + strlen(GETDNS_STR_IPV4)) == 0) + || (strncmp(GETDNS_STR_IPV6, (char *) address_type->data, + strlen(GETDNS_STR_IPV6)) == 0)) + req_type = GETDNS_RRTYPE_PTR; + else + return GETDNS_RETURN_WRONG_TYPE_REQUESTED; + if ((name = reverse_address((char *) address_data->data)) == 0) + return GETDNS_RETURN_GENERIC_ERROR; + return getdns_general(context, name, req_type, extensions, + userarg, transaction_id, callback); - if ((retval = getdns_dict_get_bindata(address, "address_data", &address_data)) != GETDNS_RETURN_GOOD) - return retval; - if ((retval = getdns_dict_get_bindata(address, "address_type", &address_type)) != GETDNS_RETURN_GOOD) - return retval; - if ((strncmp(GETDNS_STR_IPV4, (char *)address_type->data, strlen(GETDNS_STR_IPV4)) == 0) || - (strncmp(GETDNS_STR_IPV6, (char *)address_type->data, strlen(GETDNS_STR_IPV6)) == 0)) - req_type = GETDNS_RRTYPE_PTR; - else - return GETDNS_RETURN_WRONG_TYPE_REQUESTED; - if ((name = reverse_address((char *)address_data->data)) == 0) - return GETDNS_RETURN_GENERIC_ERROR; - return getdns_general(context, name, req_type, extensions, - userarg, transaction_id, callback); - - return GETDNS_RETURN_GOOD; -} /* getdns_hostname */ + return GETDNS_RETURN_GOOD; +} /* getdns_hostname */ /* getdns_hostname.c */ diff --git a/src/list.c b/src/list.c index 73036143..176f8ade 100644 --- a/src/list.c +++ b/src/list.c @@ -39,111 +39,106 @@ /*---------------------------------------- getdns_list_get_length */ getdns_return_t -getdns_list_get_length(struct getdns_list *list, size_t *answer) +getdns_list_get_length(struct getdns_list * list, size_t * answer) { - int retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; + int retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; - if(list != NULL && answer != NULL) - { - retval = GETDNS_RETURN_GOOD; - *answer = list->numinuse; - } + if (list != NULL && answer != NULL) { + retval = GETDNS_RETURN_GOOD; + *answer = list->numinuse; + } - return retval; -} /* getdns_list_get_length */ + return retval; +} /* getdns_list_get_length */ /*---------------------------------------- getdns_list_get_data_type */ -getdns_return_t -getdns_list_get_data_type(struct getdns_list *list, size_t index, getdns_data_type *answer) +getdns_return_t +getdns_list_get_data_type(struct getdns_list * list, size_t index, + getdns_data_type * answer) { - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; - if(list != NULL && index < list->numinuse) - { - *answer = list->items[index].dtype; - retval = GETDNS_RETURN_GOOD; - } - return retval; -} /* getdns_list_get_data_type */ + if (list != NULL && index < list->numinuse) { + *answer = list->items[index].dtype; + retval = GETDNS_RETURN_GOOD; + } + return retval; +} /* getdns_list_get_data_type */ /*---------------------------------------- getdns_list_get_dict */ getdns_return_t -getdns_list_get_dict(struct getdns_list *list, size_t index, struct getdns_dict **answer) +getdns_list_get_dict(struct getdns_list * list, size_t index, + struct getdns_dict ** answer) { - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; - if(list != NULL && index < list->numinuse) - { - if(list->items[index].dtype != t_dict) - retval = GETDNS_RETURN_WRONG_TYPE_REQUESTED; - else - { - *answer = list->items[index].data.dict; - retval = GETDNS_RETURN_GOOD; - } - } + if (list != NULL && index < list->numinuse) { + if (list->items[index].dtype != t_dict) + retval = GETDNS_RETURN_WRONG_TYPE_REQUESTED; + else { + *answer = list->items[index].data.dict; + retval = GETDNS_RETURN_GOOD; + } + } - return retval; -} /* getdns_list_get_dict */ + return retval; +} /* getdns_list_get_dict */ /*---------------------------------------- getdns_list_get_list */ getdns_return_t -getdns_list_get_list(struct getdns_list *list, size_t index, struct getdns_list **answer) +getdns_list_get_list(struct getdns_list * list, size_t index, + struct getdns_list ** answer) { - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; - if(list != NULL && index < list->numinuse) - { - if(list->items[index].dtype != t_list) - retval = GETDNS_RETURN_WRONG_TYPE_REQUESTED; - else - { - *answer = list->items[index].data.list; - retval = GETDNS_RETURN_GOOD; - } - } + if (list != NULL && index < list->numinuse) { + if (list->items[index].dtype != t_list) + retval = GETDNS_RETURN_WRONG_TYPE_REQUESTED; + else { + *answer = list->items[index].data.list; + retval = GETDNS_RETURN_GOOD; + } + } - return retval; -} /* getdns_list_get_list */ + return retval; +} /* getdns_list_get_list */ /*---------------------------------------- getdns_list_get_bindata */ -getdns_return_t getdns_list_get_bindata(struct getdns_list *list, size_t index, struct getdns_bindata **answer) +getdns_return_t +getdns_list_get_bindata(struct getdns_list * list, size_t index, + struct getdns_bindata ** answer) { - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; - if(list != NULL && index < list->numinuse) - { - if(list->items[index].dtype != t_bindata) - retval = GETDNS_RETURN_WRONG_TYPE_REQUESTED; - else - { - *answer = list->items[index].data.bindata; - retval = GETDNS_RETURN_GOOD; - } - } + if (list != NULL && index < list->numinuse) { + if (list->items[index].dtype != t_bindata) + retval = GETDNS_RETURN_WRONG_TYPE_REQUESTED; + else { + *answer = list->items[index].data.bindata; + retval = GETDNS_RETURN_GOOD; + } + } - return retval; -} /* getdns_list_get_bindata */ + return retval; +} /* getdns_list_get_bindata */ /*---------------------------------------- getdns_list_get_int */ getdns_return_t -getdns_list_get_int(struct getdns_list *list, size_t index, uint32_t *answer) +getdns_list_get_int(struct getdns_list * list, size_t index, uint32_t * answer) { - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; - if(list != NULL && index < list->numinuse) - { - if(list->items[index].dtype != t_int) - retval = GETDNS_RETURN_WRONG_TYPE_REQUESTED; - else - { - *answer = list->items[index].data.n; - retval = GETDNS_RETURN_GOOD; - } - } + if (list != NULL && index < list->numinuse) { + if (list->items[index].dtype != t_int) + retval = GETDNS_RETURN_WRONG_TYPE_REQUESTED; + else { + *answer = list->items[index].data.n; + retval = GETDNS_RETURN_GOOD; + } + } - return retval; -} /* getdns_list_get_int */ + return retval; +} /* getdns_list_get_int */ /*---------------------------------------- getdns_list_realloc */ /** @@ -154,252 +149,267 @@ getdns_list_get_int(struct getdns_list *list, size_t index, uint32_t *answer) * @return GETDNS_RETURN_GOOD on success, GETDNS_RETURN_GENERIC_ERROR if out of memory */ getdns_return_t -getdns_list_realloc(struct getdns_list *list) +getdns_list_realloc(struct getdns_list * list) { - getdns_return_t retval = GETDNS_RETURN_GENERIC_ERROR; - int i; - struct getdns_list_item *newlist; + getdns_return_t retval = GETDNS_RETURN_GENERIC_ERROR; + int i; + struct getdns_list_item *newlist; - if(list != NULL) - { - newlist = (struct getdns_list_item *) realloc(list->items - , (list->numalloc + GETDNS_LIST_BLOCKSZ) * sizeof(struct getdns_list_item)); - if(newlist != NULL) - { - list->items = newlist; - for(i=list->numalloc; inumalloc + GETDNS_LIST_BLOCKSZ; i++) - { - list->items[i].inuse = false; - list->items[i].dtype = t_invalid; - } - list->numalloc += GETDNS_LIST_BLOCKSZ; - retval = GETDNS_RETURN_GOOD; - } - } + if (list != NULL) { + newlist = + (struct getdns_list_item *) realloc(list->items, + (list->numalloc + + GETDNS_LIST_BLOCKSZ) * + sizeof(struct getdns_list_item)); + if (newlist != NULL) { + list->items = newlist; + for (i = list->numalloc; + i < list->numalloc + GETDNS_LIST_BLOCKSZ; i++) { + list->items[i].inuse = false; + list->items[i].dtype = t_invalid; + } + list->numalloc += GETDNS_LIST_BLOCKSZ; + retval = GETDNS_RETURN_GOOD; + } + } - return retval; -} /* getdns_list_realloc */ + return retval; +} /* getdns_list_realloc */ /*---------------------------------------- getdns_list_copy */ getdns_return_t -getdns_list_copy(struct getdns_list *srclist, struct getdns_list **dstlist) +getdns_list_copy(struct getdns_list * srclist, struct getdns_list ** dstlist) { - int i; - size_t index; - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; + int i; + size_t index; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; - if(srclist != NULL && dstlist != NULL) - { - *dstlist = getdns_list_create(); - if(*dstlist != NULL) - { - retval = GETDNS_RETURN_GOOD; - for(i=0; inuminuse; i++) - { - if(getdns_list_add_item(*dstlist, &index) == GETDNS_RETURN_GOOD) - { - (*dstlist)->items[index].inuse = true; - (*dstlist)->items[index].dtype = srclist->items[i].dtype; + if (srclist != NULL && dstlist != NULL) { + *dstlist = getdns_list_create(); + if (*dstlist != NULL) { + retval = GETDNS_RETURN_GOOD; + for (i = 0; i < srclist->numinuse; i++) { + if (getdns_list_add_item(*dstlist, + &index) == GETDNS_RETURN_GOOD) { + (*dstlist)->items[index].inuse = true; + (*dstlist)->items[index].dtype = + srclist->items[i].dtype; - if(srclist->items[i].dtype == t_int) - (*dstlist)->items[index].data.n = srclist->items[i].data.n; - else if(srclist->items[i].dtype == t_list) - retval = getdns_list_copy(srclist->items[index].data.list - , &((*dstlist)->items[i].data.list)); - else if(srclist->items[i].dtype == t_bindata) - { - (*dstlist)->items[i].data.bindata = (struct getdns_bindata *) - malloc(sizeof(getdns_bindata)); - (*dstlist)->items[i].data.bindata->size = srclist->items[i].data.bindata->size; - (*dstlist)->items[i].data.bindata->data = (uint8_t *) - malloc(srclist->items[i].data.bindata->size); - if((*dstlist)->items[i].data.bindata->data != NULL) - memcpy((*dstlist)->items[i].data.bindata->data, - srclist->items[i].data.bindata->data - , srclist->items[i].data.bindata->size); - else - retval = GETDNS_RETURN_GENERIC_ERROR; - } - else if (srclist->items[i].dtype == t_dict) - { - retval = getdns_dict_copy(srclist->items[index].data.dict, - &((*dstlist)->items[i].data.dict)); - } - } - else { - retval = GETDNS_RETURN_GENERIC_ERROR; - getdns_list_destroy(*dstlist); - *dstlist = NULL; - } + if (srclist->items[i].dtype == t_int) + (*dstlist)->items[index].data. + n = + srclist->items[i].data.n; + else if (srclist->items[i].dtype == + t_list) + retval = + getdns_list_copy(srclist-> + items[index].data.list, + &((*dstlist)->items[i]. + data.list)); + else if (srclist->items[i].dtype == + t_bindata) { + (*dstlist)->items[i].data. + bindata = + (struct getdns_bindata *) + malloc(sizeof + (getdns_bindata)); + (*dstlist)->items[i].data. + bindata->size = + srclist->items[i].data. + bindata->size; + (*dstlist)->items[i].data. + bindata->data = (uint8_t *) + malloc(srclist->items[i]. + data.bindata->size); + if ((*dstlist)->items[i].data. + bindata->data != NULL) + memcpy((*dstlist)-> + items[i].data. + bindata->data, + srclist->items[i]. + data.bindata->data, + srclist->items[i]. + data.bindata-> + size); + else + retval = + GETDNS_RETURN_GENERIC_ERROR; + } else if (srclist->items[i].dtype == + t_dict) { + retval = + getdns_dict_copy(srclist-> + items[index].data.dict, + &((*dstlist)->items[i]. + data.dict)); + } + } else { + retval = GETDNS_RETURN_GENERIC_ERROR; + getdns_list_destroy(*dstlist); + *dstlist = NULL; + } - if(retval != GETDNS_RETURN_GOOD) - break; - } - } - else - retval = GETDNS_RETURN_GENERIC_ERROR; - } + if (retval != GETDNS_RETURN_GOOD) + break; + } + } else + retval = GETDNS_RETURN_GENERIC_ERROR; + } - return retval; -} /* getdns_list_copy */ + return retval; +} /* getdns_list_copy */ /*---------------------------------------- getdns_list_create */ struct getdns_list * getdns_list_create() { - struct getdns_list *list = NULL; + struct getdns_list *list = NULL; - list = (struct getdns_list *) malloc(sizeof(struct getdns_list)); - if(list != NULL) - { - list->numalloc = 0; - list->numinuse = 0; - list->items = NULL; + list = (struct getdns_list *) malloc(sizeof(struct getdns_list)); + if (list != NULL) { + list->numalloc = 0; + list->numinuse = 0; + list->items = NULL; - getdns_list_realloc(list); - } + getdns_list_realloc(list); + } - return list; -} /* getdns_list_create */ + return list; +} /* getdns_list_create */ /*---------------------------------------- getdns_list_destroy */ void getdns_list_destroy(struct getdns_list *list) { - int i; + int i; - if(list != NULL) - { - if(list->items != NULL) - { - for(i=0; inuminuse; i++) - { - if(list->items[i].dtype == t_list) - { - if(list->items[i].dtype == t_list) - getdns_list_destroy(list->items[i].data.list); - } - else if(list->items[i].dtype == t_bindata) - { - if(list->items[i].data.bindata->size > 0) - free(list->items[i].data.bindata->data); - free(list->items[i].data.bindata); - } - else if(list->items[i].dtype == t_dict) - { - getdns_dict_destroy(list->items[i].data.dict); - } - } - free(list->items); - } - free(list); - } -} /* getdns_list_destroy */ + if (list != NULL) { + if (list->items != NULL) { + for (i = 0; i < list->numinuse; i++) { + if (list->items[i].dtype == t_list) { + if (list->items[i].dtype == t_list) + getdns_list_destroy(list-> + items[i].data.list); + } else if (list->items[i].dtype == t_bindata) { + if (list->items[i].data.bindata->size > + 0) + free(list->items[i].data. + bindata->data); + free(list->items[i].data.bindata); + } else if (list->items[i].dtype == t_dict) { + getdns_dict_destroy(list->items[i]. + data.dict); + } + } + free(list->items); + } + free(list); + } +} /* getdns_list_destroy */ /*---------------------------------------- getdns_list_add_item */ getdns_return_t -getdns_list_add_item(struct getdns_list *list, size_t *index) +getdns_list_add_item(struct getdns_list *list, size_t * index) { - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; - if(list != NULL && index != NULL) - { - if(list->numalloc == list->numinuse) - retval = getdns_list_realloc(list); - else - retval = GETDNS_RETURN_GOOD; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; + if (list != NULL && index != NULL) { + if (list->numalloc == list->numinuse) + retval = getdns_list_realloc(list); + else + retval = GETDNS_RETURN_GOOD; - if(retval == GETDNS_RETURN_GOOD) - { - *index = list->numinuse; - list->items[*index].inuse = true; - list->numinuse++; - } - } - return retval; -} /* getdns_list_add_item */ + if (retval == GETDNS_RETURN_GOOD) { + *index = list->numinuse; + list->items[*index].inuse = true; + list->numinuse++; + } + } + return retval; +} /* getdns_list_add_item */ /*---------------------------------------- getdns_list_set_dict */ getdns_return_t -getdns_list_set_dict(struct getdns_list *list, size_t index, struct getdns_dict *child_dict) +getdns_list_set_dict(struct getdns_list * list, size_t index, + struct getdns_dict * child_dict) { - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; - if(list != NULL && child_dict != NULL) - { - if(list->numinuse > index) - { - list->items[index].dtype = t_dict; - retval = getdns_dict_copy(child_dict, &(list->items[index].data.dict)); - } - } + if (list != NULL && child_dict != NULL) { + if (list->numinuse > index) { + list->items[index].dtype = t_dict; + retval = + getdns_dict_copy(child_dict, + &(list->items[index].data.dict)); + } + } - return retval; -} /* getdns_list_set_dict */ + return retval; +} /* getdns_list_set_dict */ /*---------------------------------------- getdns_set_list */ getdns_return_t -getdns_list_set_list(struct getdns_list *list, size_t index, struct getdns_list *child_list) +getdns_list_set_list(struct getdns_list * list, size_t index, + struct getdns_list * child_list) { - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; - if(list != NULL && child_list != NULL) - { - if(list->numinuse > index) - { - list->items[index].dtype = t_list; - retval = getdns_list_copy(child_list, &(list->items[index].data.list)); - } - } + if (list != NULL && child_list != NULL) { + if (list->numinuse > index) { + list->items[index].dtype = t_list; + retval = + getdns_list_copy(child_list, + &(list->items[index].data.list)); + } + } - return retval; -} /* getdns_set_list */ + return retval; +} /* getdns_set_list */ /*---------------------------------------- getdns_list_set_bindata */ getdns_return_t -getdns_list_set_bindata(struct getdns_list *list, size_t index, struct getdns_bindata *child_bindata) +getdns_list_set_bindata(struct getdns_list * list, size_t index, + struct getdns_bindata * child_bindata) { - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; - if(list != NULL && child_bindata != NULL) - { - if(list->numinuse > index) - { - list->items[index].dtype = t_bindata; - list->items[index].data.bindata = (struct getdns_bindata *) - malloc(sizeof(struct getdns_bindata)); - if(list->items[index].data.bindata != NULL) - { - list->items[index].data.bindata->size = child_bindata->size; - list->items[index].data.bindata->data = (uint8_t *) malloc(child_bindata->size - * sizeof(uint8_t)); - memcpy(list->items[index].data.bindata->data, child_bindata->data, child_bindata->size); - retval = GETDNS_RETURN_GOOD; - } - else - retval = GETDNS_RETURN_GENERIC_ERROR; - } - } + if (list != NULL && child_bindata != NULL) { + if (list->numinuse > index) { + list->items[index].dtype = t_bindata; + list->items[index].data.bindata = + (struct getdns_bindata *) + malloc(sizeof(struct getdns_bindata)); + if (list->items[index].data.bindata != NULL) { + list->items[index].data.bindata->size = + child_bindata->size; + list->items[index].data.bindata->data = + (uint8_t *) malloc(child_bindata->size * + sizeof(uint8_t)); + memcpy(list->items[index].data.bindata->data, + child_bindata->data, child_bindata->size); + retval = GETDNS_RETURN_GOOD; + } else + retval = GETDNS_RETURN_GENERIC_ERROR; + } + } - return retval; -} /* getdns_list_set_bindata */ + return retval; +} /* getdns_list_set_bindata */ /*---------------------------------------- getdns_list_set_int */ getdns_return_t -getdns_list_set_int(struct getdns_list *list, size_t index, uint32_t child_uint32) +getdns_list_set_int(struct getdns_list * list, size_t index, + uint32_t child_uint32) { - getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; + getdns_return_t retval = GETDNS_RETURN_NO_SUCH_LIST_ITEM; - if(list != NULL) - { - if(list->numinuse > index) - { - list->items[index].dtype = t_int; - list->items[index].data.n = child_uint32; - retval = GETDNS_RETURN_GOOD; - } - } + if (list != NULL) { + if (list->numinuse > index) { + list->items[index].dtype = t_int; + list->items[index].data.n = child_uint32; + retval = GETDNS_RETURN_GOOD; + } + } - return retval; -} /* getdns_list_set_int */ + return retval; +} /* getdns_list_set_int */ /* getdns_list.c */ diff --git a/src/list.h b/src/list.h index 2fb1b100..2da5b541 100644 --- a/src/list.h +++ b/src/list.h @@ -37,15 +37,17 @@ /** * this structure represents a single item in a list */ -struct getdns_list_item { - int inuse; - getdns_data_type dtype; - union { - getdns_list *list; - getdns_dict *dict; - int n; - getdns_bindata *bindata; - } data; +struct getdns_list_item +{ + int inuse; + getdns_data_type dtype; + union + { + getdns_list *list; + getdns_dict *dict; + int n; + getdns_bindata *bindata; + } data; }; /** @@ -57,11 +59,11 @@ struct getdns_list_item { * The use cases do not justify working too hard at shrinking the structures. * Indexes are 0 based. */ -struct getdns_list { - int numalloc; - int numinuse; - struct getdns_list_item *items; +struct getdns_list +{ + int numalloc; + int numinuse; + struct getdns_list_item *items; }; #endif - diff --git a/src/request-internal.c b/src/request-internal.c index 2893b2d5..f0d93062 100644 --- a/src/request-internal.c +++ b/src/request-internal.c @@ -42,144 +42,147 @@ #define gd_malloc(sz) context->memory_allocator(sz) #define gd_free(ptr) context->memory_deallocator(ptr) -void network_req_free(getdns_network_req* net_req) { - if (!net_req) { - return; - } - getdns_context_t context = net_req->owner->context; - if (net_req->result) { - ldns_pkt_free(net_req->result); - } - gd_free(net_req); +void +network_req_free(getdns_network_req * net_req) +{ + if (!net_req) { + return; + } + getdns_context_t context = net_req->owner->context; + if (net_req->result) { + ldns_pkt_free(net_req->result); + } + gd_free(net_req); } -getdns_network_req* network_req_new(getdns_dns_req* owner, - uint16_t request_type, - uint16_t request_class, - struct getdns_dict* extensions) { +getdns_network_req * +network_req_new(getdns_dns_req * owner, + uint16_t request_type, + uint16_t request_class, struct getdns_dict *extensions) +{ - getdns_context_t context = owner->context; - getdns_network_req* net_req = gd_malloc(sizeof(getdns_network_req)); - if (!net_req) { - return NULL; - } - net_req->result = NULL; - net_req->next = NULL; + getdns_context_t context = owner->context; + getdns_network_req *net_req = gd_malloc(sizeof(getdns_network_req)); + if (!net_req) { + return NULL; + } + net_req->result = NULL; + net_req->next = NULL; - net_req->request_type = request_type; - net_req->request_class = request_class; - net_req->unbound_id = -1; - net_req->state = NET_REQ_NOT_SENT; - net_req->owner = owner; + net_req->request_type = request_type; + net_req->request_class = request_class; + net_req->unbound_id = -1; + net_req->state = NET_REQ_NOT_SENT; + net_req->owner = owner; - /* TODO: records and other extensions */ + /* TODO: records and other extensions */ - return net_req; + return net_req; } -void dns_req_free(getdns_dns_req* req) { - if (!req) { - return; - } - getdns_network_req *net_req = NULL; - getdns_context_t context = req->context; +void +dns_req_free(getdns_dns_req * req) +{ + if (!req) { + return; + } + getdns_network_req *net_req = NULL; + getdns_context_t context = req->context; - /* free extensions */ - getdns_dict_destroy(req->extensions); + /* free extensions */ + getdns_dict_destroy(req->extensions); - /* free network requests */ - net_req = req->first_req; - while (net_req) { - getdns_network_req *next = net_req->next; - network_req_free(net_req); - net_req = next; - } + /* free network requests */ + net_req = req->first_req; + while (net_req) { + getdns_network_req *next = net_req->next; + network_req_free(net_req); + net_req = next; + } - /* cleanup timeout */ - if (req->timeout) { - event_del(req->timeout); - event_free(req->timeout); - } + /* cleanup timeout */ + if (req->timeout) { + event_del(req->timeout); + event_free(req->timeout); + } - if (req->local_cb_timer) { - event_del(req->local_cb_timer); - event_free(req->local_cb_timer); - } + if (req->local_cb_timer) { + event_del(req->local_cb_timer); + event_free(req->local_cb_timer); + } - /* free strduped name */ - free(req->name); + /* free strduped name */ + free(req->name); - gd_free(req); + gd_free(req); } /* create a new dns req to be submitted */ -getdns_dns_req* +getdns_dns_req * dns_req_new(getdns_context_t context, - struct ub_ctx* unbound, - const char* name, - uint16_t request_type, - struct getdns_dict *extensions) { + struct ub_ctx *unbound, + const char *name, uint16_t request_type, struct getdns_dict *extensions) +{ - getdns_dns_req *result = NULL; - getdns_network_req *req = NULL; - getdns_return_t r; - uint32_t both = GETDNS_EXTENSION_FALSE; + getdns_dns_req *result = NULL; + getdns_network_req *req = NULL; + getdns_return_t r; + uint32_t both = GETDNS_EXTENSION_FALSE; - result = gd_malloc(sizeof(getdns_dns_req)); - if (result == NULL) { - return NULL; - } + result = gd_malloc(sizeof(getdns_dns_req)); + if (result == NULL) { + return NULL; + } - result->name = strdup(name); - result->context = context; - result->unbound = unbound; - result->canceled = 0; - result->current_req = NULL; - result->first_req = NULL; - result->trans_id = ldns_get_random(); - result->timeout = NULL; - result->local_cb_timer = NULL; - result->ev_base = NULL; + result->name = strdup(name); + result->context = context; + result->unbound = unbound; + result->canceled = 0; + result->current_req = NULL; + result->first_req = NULL; + result->trans_id = ldns_get_random(); + result->timeout = NULL; + result->local_cb_timer = NULL; + result->ev_base = NULL; - getdns_dict_copy(extensions, &result->extensions); + getdns_dict_copy(extensions, &result->extensions); - /* will be set by caller */ - result->user_pointer = NULL; - result->user_callback = NULL; + /* will be set by caller */ + result->user_pointer = NULL; + result->user_callback = NULL; - /* create the requests */ - req = network_req_new(result, - request_type, - LDNS_RR_CLASS_IN, - extensions); - if (!req) { - dns_req_free(result); - return NULL; - } + /* create the requests */ + req = network_req_new(result, + request_type, LDNS_RR_CLASS_IN, extensions); + if (!req) { + dns_req_free(result); + return NULL; + } - result->current_req = req; - result->first_req = req; + result->current_req = req; + result->first_req = req; - /* tack on A or AAAA if needed */ - r = getdns_dict_get_int(extensions, - GETDNS_STR_EXTENSION_RETURN_BOTH_V4_AND_V6, - &both); - if (r == GETDNS_RETURN_GOOD && - both == GETDNS_EXTENSION_TRUE && - (request_type == GETDNS_RRTYPE_A || request_type == GETDNS_RRTYPE_AAAA)) { + /* tack on A or AAAA if needed */ + r = getdns_dict_get_int(extensions, + GETDNS_STR_EXTENSION_RETURN_BOTH_V4_AND_V6, &both); + if (r == GETDNS_RETURN_GOOD && + both == GETDNS_EXTENSION_TRUE && + (request_type == GETDNS_RRTYPE_A + || request_type == GETDNS_RRTYPE_AAAA)) { - uint16_t next_req_type = (request_type == GETDNS_RRTYPE_A) ? GETDNS_RRTYPE_AAAA : GETDNS_RRTYPE_A; - getdns_network_req* next_req = - network_req_new(result, - next_req_type, - LDNS_RR_CLASS_IN, - extensions); - if (!next_req) { - dns_req_free(result); - return NULL; - } - req->next = next_req; - } + uint16_t next_req_type = + (request_type == + GETDNS_RRTYPE_A) ? GETDNS_RRTYPE_AAAA : GETDNS_RRTYPE_A; + getdns_network_req *next_req = network_req_new(result, + next_req_type, + LDNS_RR_CLASS_IN, + extensions); + if (!next_req) { + dns_req_free(result); + return NULL; + } + req->next = next_req; + } - return result; + return result; } diff --git a/src/service.c b/src/service.c index 7af23ccf..73cd8b12 100644 --- a/src/service.c +++ b/src/service.c @@ -38,18 +38,14 @@ * */ getdns_return_t -getdns_service( - getdns_context_t context, - const char *name, - struct getdns_dict *extensions, - void *userarg, - getdns_transaction_t *transaction_id, - getdns_callback_t callback -) +getdns_service(getdns_context_t context, + const char *name, + struct getdns_dict * extensions, + void *userarg, + getdns_transaction_t * transaction_id, getdns_callback_t callback) { - return getdns_general(context, name, GETDNS_RRTYPE_SRV, - extensions, userarg, transaction_id, - callback); -} /* getdns_service */ + return getdns_general(context, name, GETDNS_RRTYPE_SRV, + extensions, userarg, transaction_id, callback); +} /* getdns_service */ /* getdns_core_only.c */ diff --git a/src/sync.c b/src/sync.c index e3f75621..955a57ed 100644 --- a/src/sync.c +++ b/src/sync.c @@ -44,119 +44,109 @@ /* stuff to make it compile pedantically */ #define UNUSED_PARAM(x) ((void)(x)) -static void sync_callback_func(getdns_context_t context, - uint16_t callback_type, - struct getdns_dict *response, - void *userarg, - getdns_transaction_t transaction_id) { +static void +sync_callback_func(getdns_context_t context, + uint16_t callback_type, + struct getdns_dict *response, + void *userarg, getdns_transaction_t transaction_id) +{ - *((getdns_dict **)userarg) = response; + *((getdns_dict **) userarg) = response; } getdns_return_t -getdns_general_sync( - getdns_context_t context, - const char *name, - uint16_t request_type, - struct getdns_dict *extensions, - uint32_t *response_length, - struct getdns_dict **response -) +getdns_general_sync(getdns_context_t context, + const char *name, + uint16_t request_type, + struct getdns_dict *extensions, + uint32_t * response_length, struct getdns_dict **response) { - getdns_return_t response_status; + getdns_return_t response_status; - response_status = validate_extensions(extensions); - if (response_status == GETDNS_RETURN_GOOD) { - response_status = getdns_general_ub(context->unbound_sync, - context->event_base_sync, - context, name, request_type, - extensions, (void *)response, - NULL, sync_callback_func); + response_status = validate_extensions(extensions); + if (response_status == GETDNS_RETURN_GOOD) { + response_status = getdns_general_ub(context->unbound_sync, + context->event_base_sync, + context, name, request_type, + extensions, (void *) response, NULL, sync_callback_func); - event_base_dispatch(context->event_base_sync); - } - return response_status; + event_base_dispatch(context->event_base_sync); + } + return response_status; } getdns_return_t -getdns_address_sync( - getdns_context_t context, - const char *name, - struct getdns_dict *extensions, - uint32_t *response_length, - struct getdns_dict **response -) +getdns_address_sync(getdns_context_t context, + const char *name, + struct getdns_dict * extensions, + uint32_t * response_length, struct getdns_dict ** response) { - int cleanup_extensions = 0; - if (!extensions) { - extensions = getdns_dict_create(); - cleanup_extensions = 1; - } - getdns_dict_set_int(extensions, - GETDNS_STR_EXTENSION_RETURN_BOTH_V4_AND_V6, - GETDNS_EXTENSION_TRUE); + int cleanup_extensions = 0; + if (!extensions) { + extensions = getdns_dict_create(); + cleanup_extensions = 1; + } + getdns_dict_set_int(extensions, + GETDNS_STR_EXTENSION_RETURN_BOTH_V4_AND_V6, GETDNS_EXTENSION_TRUE); - getdns_return_t result = - getdns_general_sync(context, name, GETDNS_RRTYPE_A, - extensions, response_length, response); - if (cleanup_extensions) { - getdns_dict_destroy(extensions); - } - return result; + getdns_return_t result = + getdns_general_sync(context, name, GETDNS_RRTYPE_A, + extensions, response_length, response); + if (cleanup_extensions) { + getdns_dict_destroy(extensions); + } + return result; } getdns_return_t -getdns_hostname_sync( - getdns_context_t context, - struct getdns_dict *address, - struct getdns_dict *extensions, - uint32_t *response_length, - struct getdns_dict **response -) +getdns_hostname_sync(getdns_context_t context, + struct getdns_dict * address, + struct getdns_dict * extensions, + uint32_t * response_length, struct getdns_dict ** response) { - struct getdns_bindata *address_data; - struct getdns_bindata *address_type; - uint16_t req_type; - char *name; - getdns_return_t retval; + struct getdns_bindata *address_data; + struct getdns_bindata *address_type; + uint16_t req_type; + char *name; + getdns_return_t retval; - - if ((retval = getdns_dict_get_bindata(address, "address_data", &address_data)) != GETDNS_RETURN_GOOD) - return retval; - if ((retval = getdns_dict_get_bindata(address, "address_type", &address_type)) != GETDNS_RETURN_GOOD) - return retval; - if ((strncmp(GETDNS_STR_IPV4, (char *)address_type->data, strlen(GETDNS_STR_IPV4)) == 0) || - (strncmp(GETDNS_STR_IPV6, (char *)address_type->data, strlen(GETDNS_STR_IPV6)) == 0)) - req_type = GETDNS_RRTYPE_PTR; - else - return GETDNS_RETURN_WRONG_TYPE_REQUESTED; - if ((name = reverse_address((char *)address_data)) == 0) - return GETDNS_RETURN_GENERIC_ERROR; - return getdns_general_sync(context, name, req_type, extensions, - response_length, response); + if ((retval = + getdns_dict_get_bindata(address, "address_data", + &address_data)) != GETDNS_RETURN_GOOD) + return retval; + if ((retval = + getdns_dict_get_bindata(address, "address_type", + &address_type)) != GETDNS_RETURN_GOOD) + return retval; + if ((strncmp(GETDNS_STR_IPV4, (char *) address_type->data, + strlen(GETDNS_STR_IPV4)) == 0) + || (strncmp(GETDNS_STR_IPV6, (char *) address_type->data, + strlen(GETDNS_STR_IPV6)) == 0)) + req_type = GETDNS_RRTYPE_PTR; + else + return GETDNS_RETURN_WRONG_TYPE_REQUESTED; + if ((name = reverse_address((char *) address_data)) == 0) + return GETDNS_RETURN_GENERIC_ERROR; + return getdns_general_sync(context, name, req_type, extensions, + response_length, response); } - getdns_return_t -getdns_service_sync( - getdns_context_t context, - const char *name, - struct getdns_dict *extensions, - uint32_t *response_length, - struct getdns_dict **response -) +getdns_service_sync(getdns_context_t context, + const char *name, + struct getdns_dict * extensions, + uint32_t * response_length, struct getdns_dict ** response) { - return getdns_general_sync(context, name, GETDNS_RRTYPE_SRV, extensions, - response_length, response); + return getdns_general_sync(context, name, GETDNS_RRTYPE_SRV, + extensions, response_length, response); } void -getdns_free_sync_request_memory( - struct getdns_dict *response -) +getdns_free_sync_request_memory(struct getdns_dict *response) { - getdns_dict_destroy(response); + getdns_dict_destroy(response); } + /* getdns_core_sync.c */ diff --git a/src/test/testmessages.c b/src/test/testmessages.c index 820884ad..fb26918f 100644 --- a/src/test/testmessages.c +++ b/src/test/testmessages.c @@ -31,57 +31,57 @@ #include "testmessages.h" static char *testprog = NULL; -static char **cases = NULL; -static int ncases = 0; +static char **cases = NULL; +static int ncases = 0; void tstmsg_prog_begin(char *prognm) { - if(testprog != NULL) - { tstmsg_prog_end(); - free(testprog); - } - testprog = strdup(prognm); - printf("TESTPROG %s START\n", testprog); -} /* tstmsg_prog_begin */ + if (testprog != NULL) { + tstmsg_prog_end(); + free(testprog); + } + testprog = strdup(prognm); + printf("TESTPROG %s START\n", testprog); +} /* tstmsg_prog_begin */ void tstmsg_prog_end() { - printf("TESTPROG %s END\n", testprog); - free(testprog); -} /* tstmsg_prog_end */ + printf("TESTPROG %s END\n", testprog); + free(testprog); +} /* tstmsg_prog_end */ void tstmsg_case_begin(char *casenm) { - ncases++; - cases = (char **) realloc(cases, sizeof(char *) * ncases); - cases[ncases-1] = strdup(casenm); + ncases++; + cases = (char **) realloc(cases, sizeof(char *) * ncases); + cases[ncases - 1] = strdup(casenm); - printf("TESTCASE %s:%s BEGIN\n", testprog, cases[ncases-1]); -} /* tstmsg_case_begin */ + printf("TESTCASE %s:%s BEGIN\n", testprog, cases[ncases - 1]); +} /* tstmsg_case_begin */ void tstmsg_case_end(void) { - if(ncases > 0) - { - printf("TESTCASE %s:%s END\n", testprog, cases[ncases-1]); - ncases--; - free(cases[ncases]); - if (ncases) { - cases = (char **) realloc(cases, sizeof(char *) * ncases); - } else { - cases = NULL; - } - } -} /* tstmsg_case_end */ + if (ncases > 0) { + printf("TESTCASE %s:%s END\n", testprog, cases[ncases - 1]); + ncases--; + free(cases[ncases]); + if (ncases) { + cases = + (char **) realloc(cases, sizeof(char *) * ncases); + } else { + cases = NULL; + } + } +} /* tstmsg_case_end */ void tstmsg_case_msg(char *msg) { - printf(" %s:%s: %s\n", testprog, cases[ncases-1], msg); -} /* tstmsg_case_msg */ + printf(" %s:%s: %s\n", testprog, cases[ncases - 1], msg); +} /* tstmsg_case_msg */ /* testmessages.c */ diff --git a/src/test/tests_dict.c b/src/test/tests_dict.c index 10ff2d2c..58a0133b 100644 --- a/src/test/tests_dict.c +++ b/src/test/tests_dict.c @@ -41,71 +41,81 @@ void tst_bindatasetget(void) { - char msg[TSTMSGBUF]; - char key[20]; - getdns_return_t retval; - struct getdns_dict *dict = NULL; - struct getdns_bindata *ans_bdata; - struct getdns_bindata *bindata; + char msg[TSTMSGBUF]; + char key[20]; + getdns_return_t retval; + struct getdns_dict *dict = NULL; + struct getdns_bindata *ans_bdata; + struct getdns_bindata *bindata; - tstmsg_case_begin("tst_bindatasetget"); + tstmsg_case_begin("tst_bindatasetget"); - dict = getdns_dict_create(); + dict = getdns_dict_create(); - /* test int get function against empty dict and with bogus params */ + /* test int get function against empty dict and with bogus params */ - strcpy(key, "foo"); + strcpy(key, "foo"); - tstmsg_case_msg("getdns_dict_get_bindata() empty dict"); - retval = getdns_dict_get_bindata(NULL, key, &ans_bdata); - sprintf(msg, "line %d: getdns_dict_get_bindata(NULL, key, &ans_bdata),retval = %d", __LINE__, retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_dict_get_bindata() empty dict"); + retval = getdns_dict_get_bindata(NULL, key, &ans_bdata); + sprintf(msg, + "line %d: getdns_dict_get_bindata(NULL, key, &ans_bdata),retval = %d", + __LINE__, retval); + tstmsg_case_msg(msg); - retval = getdns_dict_get_bindata(dict, key, NULL); - sprintf(msg, "line %d: getdns_dict_get_bindata(dict, key, NULL),retval = %d", __LINE__, retval); - tstmsg_case_msg(msg); + retval = getdns_dict_get_bindata(dict, key, NULL); + sprintf(msg, + "line %d: getdns_dict_get_bindata(dict, key, NULL),retval = %d", + __LINE__, retval); + tstmsg_case_msg(msg); - tstmsg_case_msg("getdns_dict_get_bindata(dict, NULL, &ans_bindata)"); - retval = getdns_dict_get_bindata(dict, NULL, &ans_bdata); - sprintf(msg, "line %d: getdns_dict_get_bindata,retval = %d", __LINE__, retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_dict_get_bindata(dict, NULL, &ans_bindata)"); + retval = getdns_dict_get_bindata(dict, NULL, &ans_bdata); + sprintf(msg, "line %d: getdns_dict_get_bindata,retval = %d", __LINE__, + retval); + tstmsg_case_msg(msg); - tstmsg_case_msg("getdns_dict_get_bindata(dict, key, &ans_bdata)"); - retval = getdns_dict_get_bindata(dict, key, &ans_bdata); - sprintf(msg, "line %d: getdns_list_get_bindata,retval = %d", __LINE__, retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_dict_get_bindata(dict, key, &ans_bdata)"); + retval = getdns_dict_get_bindata(dict, key, &ans_bdata); + sprintf(msg, "line %d: getdns_list_get_bindata,retval = %d", __LINE__, + retval); + tstmsg_case_msg(msg); - getdns_dict_destroy(dict); + getdns_dict_destroy(dict); - /* TODO: test getdns_dict_set functions with bogus params */ + /* TODO: test getdns_dict_set functions with bogus params */ - /* test set and get legitimate use case */ + /* test set and get legitimate use case */ - dict = getdns_dict_create(); + dict = getdns_dict_create(); - strcpy(key, "foo"); - bindata = (struct getdns_bindata *) malloc(sizeof(struct getdns_bindata)); - bindata->size = strlen("foobar") + 1; - bindata->data = (void *) strdup("foobar"); + strcpy(key, "foo"); + bindata = + (struct getdns_bindata *) malloc(sizeof(struct getdns_bindata)); + bindata->size = strlen("foobar") + 1; + bindata->data = (void *) strdup("foobar"); - tstmsg_case_msg("getdns_dict_set_bindata(dict, key, bindata)"); - retval = getdns_dict_set_bindata(dict, key, bindata); - sprintf(msg, "line %d: getdns_dict_set_bindata,retval=%d,key=%s", __LINE__, retval, key); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_dict_set_bindata(dict, key, bindata)"); + retval = getdns_dict_set_bindata(dict, key, bindata); + sprintf(msg, "line %d: getdns_dict_set_bindata,retval=%d,key=%s", + __LINE__, retval, key); + tstmsg_case_msg(msg); - tstmsg_case_msg("getdns_dict_get_bindata(dict, key, &ans_bdata)"); - retval = getdns_dict_get_bindata(dict, key, &ans_bdata); - sprintf(msg, "line %d: getdns_dict_get_bindata,retval=%d,key=%s,data=%s", __LINE__, retval, key, ans_bdata->data); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_dict_get_bindata(dict, key, &ans_bdata)"); + retval = getdns_dict_get_bindata(dict, key, &ans_bdata); + sprintf(msg, + "line %d: getdns_dict_get_bindata,retval=%d,key=%s,data=%s", + __LINE__, retval, key, ans_bdata->data); + tstmsg_case_msg(msg); - getdns_dict_destroy(dict); - free(bindata->data); - free(bindata); + getdns_dict_destroy(dict); + free(bindata->data); + free(bindata); - tstmsg_case_end(); + tstmsg_case_end(); - return; -} /* tst_bindatasetget */ + return; +} /* tst_bindatasetget */ /*---------------------------------------- tst_dictsetget */ /** @@ -114,75 +124,83 @@ tst_bindatasetget(void) void tst_dictsetget(void) { - char msg[TSTMSGBUF]; - char key[20]; - uint32_t int1; - uint32_t int2; - getdns_return_t retval; - struct getdns_dict *newdict; - struct getdns_dict *ansdict; - struct getdns_dict *dict = NULL; + char msg[TSTMSGBUF]; + char key[20]; + uint32_t int1; + uint32_t int2; + getdns_return_t retval; + struct getdns_dict *newdict; + struct getdns_dict *ansdict; + struct getdns_dict *dict = NULL; - tstmsg_case_begin("tst_dictsetget"); + tstmsg_case_begin("tst_dictsetget"); - dict = getdns_dict_create(); + dict = getdns_dict_create(); - /* test get function against empty list and with bogus params */ + /* test get function against empty list and with bogus params */ - strcpy(key, "foo"); + strcpy(key, "foo"); - tstmsg_case_msg("getdns_dict_get_dict() empty dict"); - retval = getdns_dict_get_dict(NULL, key, &ansdict); - sprintf(msg, "line %d: getdns_dict_get_dict(NULL, key, &ansdict),retval = %d", __LINE__, retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_dict_get_dict() empty dict"); + retval = getdns_dict_get_dict(NULL, key, &ansdict); + sprintf(msg, + "line %d: getdns_dict_get_dict(NULL, key, &ansdict),retval = %d", + __LINE__, retval); + tstmsg_case_msg(msg); - retval = getdns_dict_get_dict(dict, key, NULL); - sprintf(msg, "line %d: getdns_dict_get_dict(dict, key, NULL),retval = %d", __LINE__, retval); - tstmsg_case_msg(msg); + retval = getdns_dict_get_dict(dict, key, NULL); + sprintf(msg, + "line %d: getdns_dict_get_dict(dict, key, NULL),retval = %d", + __LINE__, retval); + tstmsg_case_msg(msg); - tstmsg_case_msg("getdns_dict_get_dict(dict, NULL, &ansdict)"); - retval = getdns_dict_get_dict(dict, NULL, &ansdict); - sprintf(msg, "line %d: getdns_dict_get_dict,retval = %d", __LINE__, retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_dict_get_dict(dict, NULL, &ansdict)"); + retval = getdns_dict_get_dict(dict, NULL, &ansdict); + sprintf(msg, "line %d: getdns_dict_get_dict,retval = %d", __LINE__, + retval); + tstmsg_case_msg(msg); - tstmsg_case_msg("getdns_dict_get_dict(dict, key, &ansdict)"); - retval = getdns_dict_get_dict(dict, key, &ansdict); - sprintf(msg, "line %d: getdns_list_get_dict,retval = %d", __LINE__, retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_dict_get_dict(dict, key, &ansdict)"); + retval = getdns_dict_get_dict(dict, key, &ansdict); + sprintf(msg, "line %d: getdns_list_get_dict,retval = %d", __LINE__, + retval); + tstmsg_case_msg(msg); - getdns_dict_destroy(dict); + getdns_dict_destroy(dict); - /* TODO: test getdns_dict_set functions with bogus params */ + /* TODO: test getdns_dict_set functions with bogus params */ - /* test set and get legitimate use case */ + /* test set and get legitimate use case */ - dict = getdns_dict_create(); + dict = getdns_dict_create(); - strcpy(key, "foo"); - newdict = getdns_dict_create(); - getdns_dict_set_int(newdict, "foo", 42); - getdns_dict_set_int(newdict, "bar", 52); + strcpy(key, "foo"); + newdict = getdns_dict_create(); + getdns_dict_set_int(newdict, "foo", 42); + getdns_dict_set_int(newdict, "bar", 52); - tstmsg_case_msg("getdns_dict_set_dict(dict, key, newdict)"); - retval = getdns_dict_set_dict(dict, key, newdict); - sprintf(msg, "line %d: getdns_dict_set_dict,retval=%d,key=%s", __LINE__, retval, key); - tstmsg_case_msg(msg); - getdns_dict_destroy(newdict); + tstmsg_case_msg("getdns_dict_set_dict(dict, key, newdict)"); + retval = getdns_dict_set_dict(dict, key, newdict); + sprintf(msg, "line %d: getdns_dict_set_dict,retval=%d,key=%s", + __LINE__, retval, key); + tstmsg_case_msg(msg); + getdns_dict_destroy(newdict); - tstmsg_case_msg("getdns_dict_get_dict(dict, key, &ansdict)"); - retval = getdns_dict_get_dict(dict, key, &ansdict); - getdns_dict_get_int(ansdict, "foo", &int1); - getdns_dict_get_int(ansdict, "bar", &int2); - sprintf(msg, "line %d: getdns_dict_get_dict,retval=%d,key=%s,int1=%d,int2=%d" - , __LINE__, retval, key, int1, int2); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_dict_get_dict(dict, key, &ansdict)"); + retval = getdns_dict_get_dict(dict, key, &ansdict); + getdns_dict_get_int(ansdict, "foo", &int1); + getdns_dict_get_int(ansdict, "bar", &int2); + sprintf(msg, + "line %d: getdns_dict_get_dict,retval=%d,key=%s,int1=%d,int2=%d", + __LINE__, retval, key, int1, int2); + tstmsg_case_msg(msg); - getdns_dict_destroy(dict); + getdns_dict_destroy(dict); - tstmsg_case_end(); + tstmsg_case_end(); - return; -} /* tst_dictsetget */ + return; +} /* tst_dictsetget */ /*---------------------------------------- tst_getnames */ /** @@ -191,84 +209,83 @@ tst_dictsetget(void) void tst_getnames(void) { - size_t index; - size_t llen; - uint32_t ansint; - int i; - getdns_return_t result; - getdns_data_type dtype; - struct getdns_dict *dict = NULL; - struct getdns_list *list = NULL; + size_t index; + size_t llen; + uint32_t ansint; + int i; + getdns_return_t result; + getdns_data_type dtype; + struct getdns_dict *dict = NULL; + struct getdns_list *list = NULL; - tstmsg_case_begin("tst_getnames"); + tstmsg_case_begin("tst_getnames"); - dict = getdns_dict_create(); + dict = getdns_dict_create(); - /* degenerative use cases */ + /* degenerative use cases */ - tstmsg_case_msg("getdns_dict_get_names(NULL, &list)"); - getdns_dict_get_names(NULL, &list); - getdns_list_destroy(list); + tstmsg_case_msg("getdns_dict_get_names(NULL, &list)"); + getdns_dict_get_names(NULL, &list); + getdns_list_destroy(list); - tstmsg_case_msg("getdns_dict_get_names(dict, NULL)"); - getdns_dict_get_names(dict, NULL); + tstmsg_case_msg("getdns_dict_get_names(dict, NULL)"); + getdns_dict_get_names(dict, NULL); - tstmsg_case_msg("getdns_dict_get_names(dict, &list), empty dictionary"); - getdns_dict_get_names(dict, &list); - getdns_list_destroy(list); - - /* legit use case, add items out of order to exercise tree */ - /* TODO: add elements of type dict, bindata, list to the dict */ + tstmsg_case_msg + ("getdns_dict_get_names(dict, &list), empty dictionary"); + getdns_dict_get_names(dict, &list); + getdns_list_destroy(list); - i = 0; - getdns_dict_set_int(dict, "foo", i++); - getdns_dict_set_int(dict, "bar", i++); - getdns_dict_set_int(dict, "quz", i++); - getdns_dict_set_int(dict, "alpha", i++); + /* legit use case, add items out of order to exercise tree */ + /* TODO: add elements of type dict, bindata, list to the dict */ - getdns_dict_get_names(dict, &list); + i = 0; + getdns_dict_set_int(dict, "foo", i++); + getdns_dict_set_int(dict, "bar", i++); + getdns_dict_set_int(dict, "quz", i++); + getdns_dict_set_int(dict, "alpha", i++); - result = getdns_list_get_length(list, &llen); - if(llen != i) - { - tstmsg_case_msg("getdns_list_get_length returned unreasonable length, exiting"); - return; - } + getdns_dict_get_names(dict, &list); - for(index=0; indexsize = strlen("foobar") + 1; - new_bindata->data = (uint8_t *) strdup("foobar"); - new_bindata->data[strlen("foobar")] = '\0'; + new_bindata = + (struct getdns_bindata *) malloc(sizeof(struct getdns_bindata)); + new_bindata->size = strlen("foobar") + 1; + new_bindata->data = (uint8_t *) strdup("foobar"); + new_bindata->data[strlen("foobar")] = '\0'; - getdns_list_add_item(list, &index); - getdns_list_set_bindata(list, index, new_bindata); - retval = getdns_list_get_bindata(list, index, &ans_bindata); - sprintf(msg, "getdns_list_set/get_bindata,retval = %d, bindata->data = %d,%s" - , retval, (int) ans_bindata->size, (char *) ans_bindata->data); - tstmsg_case_msg(msg); + getdns_list_add_item(list, &index); + getdns_list_set_bindata(list, index, new_bindata); + retval = getdns_list_get_bindata(list, index, &ans_bindata); + sprintf(msg, + "getdns_list_set/get_bindata,retval = %d, bindata->data = %d,%s", + retval, (int) ans_bindata->size, (char *) ans_bindata->data); + tstmsg_case_msg(msg); - getdns_list_destroy(list); + getdns_list_destroy(list); - tstmsg_case_end(); + tstmsg_case_end(); - return; -} /* tst_bindatasetget */ + return; +} /* tst_bindatasetget */ /*---------------------------------------- tst_dictsetget */ /** @@ -123,74 +129,78 @@ tst_bindatasetget(void) void tst_dictsetget(void) { - char msg[TSTMSGBUF]; - size_t index = 0; - uint32_t ans_int; - getdns_return_t retval; - struct getdns_list *list = NULL; - struct getdns_dict *dict = NULL; - struct getdns_dict *ansdict = NULL; + char msg[TSTMSGBUF]; + size_t index = 0; + uint32_t ans_int; + getdns_return_t retval; + struct getdns_list *list = NULL; + struct getdns_dict *dict = NULL; + struct getdns_dict *ansdict = NULL; - tstmsg_case_begin("tst_dictsetget"); + tstmsg_case_begin("tst_dictsetget"); - list = getdns_list_create(); - dict = getdns_dict_create(); + list = getdns_list_create(); + dict = getdns_dict_create(); - /* test dict get function against empty list and with bogus params */ + /* test dict get function against empty list and with bogus params */ - tstmsg_case_msg("getdns_list_get_dict() empty list"); - retval = getdns_list_get_dict(NULL, index, &dict); - sprintf(msg, "getdns_list_get_dict(NULL, index, &dict),retval = %d", retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_list_get_dict() empty list"); + retval = getdns_list_get_dict(NULL, index, &dict); + sprintf(msg, "getdns_list_get_dict(NULL, index, &dict),retval = %d", + retval); + tstmsg_case_msg(msg); - retval = getdns_list_get_dict(list, index, NULL); - sprintf(msg, "getdns_list_get_dict(list, index, NULL),retval = %d", retval); - tstmsg_case_msg(msg); + retval = getdns_list_get_dict(list, index, NULL); + sprintf(msg, "getdns_list_get_dict(list, index, NULL),retval = %d", + retval); + tstmsg_case_msg(msg); - tstmsg_case_msg("getdns_list_get_dict(list, 0, &dict)"); - retval = getdns_list_get_dict(list, 0, &dict); - sprintf(msg, "getdns_list_get_dict,retval = %d", retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_list_get_dict(list, 0, &dict)"); + retval = getdns_list_get_dict(list, 0, &dict); + sprintf(msg, "getdns_list_get_dict,retval = %d", retval); + tstmsg_case_msg(msg); - tstmsg_case_msg("getdns_list_get_dict(list, 1, &dict)"); - retval = getdns_list_get_dict(list, 1, &dict); - sprintf(msg, "getdns_list_get_dict,retval = %d", retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_list_get_dict(list, 1, &dict)"); + retval = getdns_list_get_dict(list, 1, &dict); + sprintf(msg, "getdns_list_get_dict,retval = %d", retval); + tstmsg_case_msg(msg); - /* test int set function against empty list with bogus params */ + /* test int set function against empty list with bogus params */ - tstmsg_case_msg("getdns_list_set_dict() empty list"); - retval = getdns_list_set_dict(NULL, index, dict); - sprintf(msg, "getdns_list_set_dict(NULL, index, dict),retval = %d", retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_list_set_dict() empty list"); + retval = getdns_list_set_dict(NULL, index, dict); + sprintf(msg, "getdns_list_set_dict(NULL, index, dict),retval = %d", + retval); + tstmsg_case_msg(msg); - tstmsg_case_msg("getdns_list_set_dict(list, 0, dict)"); - retval = getdns_list_set_dict(list, 0, dict); - sprintf(msg, "getdns_list_set_dict,retval = %d", retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_list_set_dict(list, 0, dict)"); + retval = getdns_list_set_dict(list, 0, dict); + sprintf(msg, "getdns_list_set_dict,retval = %d", retval); + tstmsg_case_msg(msg); - tstmsg_case_msg("getdns_list_set_dict(list, 1, dict)"); - retval = getdns_list_set_dict(list, 1, dict); - sprintf(msg, "getdns_list_set_dict,retval = %d", retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_list_set_dict(list, 1, dict)"); + retval = getdns_list_set_dict(list, 1, dict); + sprintf(msg, "getdns_list_set_dict,retval = %d", retval); + tstmsg_case_msg(msg); - /* test set and get legitimate use case */ + /* test set and get legitimate use case */ - getdns_dict_set_int(dict, "foo", 42); - getdns_list_add_item(list, &index); - getdns_list_set_dict(list, index, dict); - retval = getdns_list_get_dict(list, index, &ansdict); - getdns_dict_get_int(ansdict, "foo", &ans_int); - sprintf(msg, "getdns_list_set/get_dict,retval=%d, ans=%d", retval, ans_int); - tstmsg_case_msg(msg); + getdns_dict_set_int(dict, "foo", 42); + getdns_list_add_item(list, &index); + getdns_list_set_dict(list, index, dict); + retval = getdns_list_get_dict(list, index, &ansdict); + getdns_dict_get_int(ansdict, "foo", &ans_int); + sprintf(msg, "getdns_list_set/get_dict,retval=%d, ans=%d", retval, + ans_int); + tstmsg_case_msg(msg); - getdns_dict_destroy(dict); - getdns_list_destroy(list); + getdns_dict_destroy(dict); + getdns_list_destroy(list); - tstmsg_case_end(); + tstmsg_case_end(); - return; -} /* tst_dictsetget */ + return; +} /* tst_dictsetget */ /*---------------------------------------- tst_listsetget */ /** @@ -199,75 +209,80 @@ tst_dictsetget(void) void tst_listsetget(void) { - char msg[TSTMSGBUF]; - size_t index = 0; - getdns_return_t retval; - uint32_t ans_int; - struct getdns_list *list = NULL; - struct getdns_list *new_list = NULL; - struct getdns_list *ans_list = NULL; + char msg[TSTMSGBUF]; + size_t index = 0; + getdns_return_t retval; + uint32_t ans_int; + struct getdns_list *list = NULL; + struct getdns_list *new_list = NULL; + struct getdns_list *ans_list = NULL; - tstmsg_case_begin("tst_listsetget"); + tstmsg_case_begin("tst_listsetget"); - list = getdns_list_create(); + list = getdns_list_create(); - /* test get function against empty list and with bogus params */ + /* test get function against empty list and with bogus params */ - tstmsg_case_msg("getdns_list_get_list() empty list"); - retval = getdns_list_get_list(NULL, index, &ans_list); - sprintf(msg, "getdns_list_get_list(NULL, index, &ans_list),retval = %d", retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_list_get_list() empty list"); + retval = getdns_list_get_list(NULL, index, &ans_list); + sprintf(msg, + "getdns_list_get_list(NULL, index, &ans_list),retval = %d", + retval); + tstmsg_case_msg(msg); - retval = getdns_list_get_list(list, index, NULL); - sprintf(msg, "getdns_list_get_list(list, index, NULL),retval = %d", retval); - tstmsg_case_msg(msg); + retval = getdns_list_get_list(list, index, NULL); + sprintf(msg, "getdns_list_get_list(list, index, NULL),retval = %d", + retval); + tstmsg_case_msg(msg); - tstmsg_case_msg("getdns_list_get_list(list, 0, &ans_list)"); - retval = getdns_list_get_list(list, 0, &ans_list); - sprintf(msg, "getdns_list_get_list,retval = %d", retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_list_get_list(list, 0, &ans_list)"); + retval = getdns_list_get_list(list, 0, &ans_list); + sprintf(msg, "getdns_list_get_list,retval = %d", retval); + tstmsg_case_msg(msg); - tstmsg_case_msg("getdns_list_get_list(list, 1, &ans_list)"); - retval = getdns_list_get_list(list, 1, &ans_list); - sprintf(msg, "getdns_list_get_list,retval = %d", retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_list_get_list(list, 1, &ans_list)"); + retval = getdns_list_get_list(list, 1, &ans_list); + sprintf(msg, "getdns_list_get_list,retval = %d", retval); + tstmsg_case_msg(msg); - /* test set function against empty list with bogus params */ + /* test set function against empty list with bogus params */ - tstmsg_case_msg("getdns_list_set_list() empty list"); - retval = getdns_list_set_list(NULL, index, NULL); - sprintf(msg, "getdns_list_set_list(NULL, index, ans_list),retval = %d", retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_list_set_list() empty list"); + retval = getdns_list_set_list(NULL, index, NULL); + sprintf(msg, "getdns_list_set_list(NULL, index, ans_list),retval = %d", + retval); + tstmsg_case_msg(msg); - tstmsg_case_msg("getdns_list_set_list(list, 0, ans_list)"); - retval = getdns_list_set_list(list, 0, NULL); - sprintf(msg, "getdns_list_set_list,retval = %d", retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_list_set_list(list, 0, ans_list)"); + retval = getdns_list_set_list(list, 0, NULL); + sprintf(msg, "getdns_list_set_list,retval = %d", retval); + tstmsg_case_msg(msg); - tstmsg_case_msg("getdns_list_set_list(list, 1, ans_list)"); - retval = getdns_list_set_list(list, 1, NULL); - sprintf(msg, "getdns_list_set_list,retval = %d", retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_list_set_list(list, 1, ans_list)"); + retval = getdns_list_set_list(list, 1, NULL); + sprintf(msg, "getdns_list_set_list,retval = %d", retval); + tstmsg_case_msg(msg); - /* test set and get legitimate use case */ + /* test set and get legitimate use case */ - new_list = getdns_list_create(); - getdns_list_add_item(new_list, &index); - getdns_list_set_int(new_list, index, 42); + new_list = getdns_list_create(); + getdns_list_add_item(new_list, &index); + getdns_list_set_int(new_list, index, 42); - getdns_list_add_item(list, &index); - getdns_list_set_list(list, index, new_list); - retval = getdns_list_get_list(list, index, &ans_list); - getdns_list_get_int(ans_list, 0, &ans_int); - sprintf(msg, "getdns_list_set/get_list,retval = %d, ans[0] = %d", retval, ans_int); - tstmsg_case_msg(msg); + getdns_list_add_item(list, &index); + getdns_list_set_list(list, index, new_list); + retval = getdns_list_get_list(list, index, &ans_list); + getdns_list_get_int(ans_list, 0, &ans_int); + sprintf(msg, "getdns_list_set/get_list,retval = %d, ans[0] = %d", + retval, ans_int); + tstmsg_case_msg(msg); - getdns_list_destroy(list); + getdns_list_destroy(list); - tstmsg_case_end(); + tstmsg_case_end(); - return; -} /* tst_listsetget */ + return; +} /* tst_listsetget */ /*---------------------------------------- tst_intsetget */ /** @@ -276,68 +291,72 @@ tst_listsetget(void) void tst_intsetget(void) { - char msg[TSTMSGBUF]; - size_t index = 0; - uint32_t ans_int; - getdns_return_t retval; - struct getdns_list *list = NULL; + char msg[TSTMSGBUF]; + size_t index = 0; + uint32_t ans_int; + getdns_return_t retval; + struct getdns_list *list = NULL; - tstmsg_case_begin("tst_intsetget"); + tstmsg_case_begin("tst_intsetget"); - list = getdns_list_create(); + list = getdns_list_create(); - /* test int get function against empty list and with bogus params */ + /* test int get function against empty list and with bogus params */ - tstmsg_case_msg("getdns_list_get_int() empty list"); - retval = getdns_list_get_int(NULL, index, &ans_int); - sprintf(msg, "getdns_list_get_int(NULL, index, &ans_int),retval = %d", retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_list_get_int() empty list"); + retval = getdns_list_get_int(NULL, index, &ans_int); + sprintf(msg, "getdns_list_get_int(NULL, index, &ans_int),retval = %d", + retval); + tstmsg_case_msg(msg); - retval = getdns_list_get_int(list, index, NULL); - sprintf(msg, "getdns_list_get_int(list, index, NULL),retval = %d", retval); - tstmsg_case_msg(msg); + retval = getdns_list_get_int(list, index, NULL); + sprintf(msg, "getdns_list_get_int(list, index, NULL),retval = %d", + retval); + tstmsg_case_msg(msg); - tstmsg_case_msg("getdns_list_get_int(list, 0, &ans_int)"); - retval = getdns_list_get_int(list, 0, &ans_int); - sprintf(msg, "getdns_list_get_int,retval = %d", retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_list_get_int(list, 0, &ans_int)"); + retval = getdns_list_get_int(list, 0, &ans_int); + sprintf(msg, "getdns_list_get_int,retval = %d", retval); + tstmsg_case_msg(msg); - tstmsg_case_msg("getdns_list_get_int(list, 1, &ans_int)"); - retval = getdns_list_get_int(list, 1, &ans_int); - sprintf(msg, "getdns_list_get_int,retval = %d", retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_list_get_int(list, 1, &ans_int)"); + retval = getdns_list_get_int(list, 1, &ans_int); + sprintf(msg, "getdns_list_get_int,retval = %d", retval); + tstmsg_case_msg(msg); - /* test int set function against empty list with bogus params */ + /* test int set function against empty list with bogus params */ - tstmsg_case_msg("getdns_list_set_int() empty list"); - retval = getdns_list_set_int(NULL, index, ans_int); - sprintf(msg, "getdns_list_set_int(NULL, index, ans_int),retval = %d", retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_list_set_int() empty list"); + retval = getdns_list_set_int(NULL, index, ans_int); + sprintf(msg, "getdns_list_set_int(NULL, index, ans_int),retval = %d", + retval); + tstmsg_case_msg(msg); - tstmsg_case_msg("getdns_list_set_int(list, 0, ans_int)"); - retval = getdns_list_set_int(list, 0, ans_int); - sprintf(msg, "getdns_list_set_int,retval = %d", retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_list_set_int(list, 0, ans_int)"); + retval = getdns_list_set_int(list, 0, ans_int); + sprintf(msg, "getdns_list_set_int,retval = %d", retval); + tstmsg_case_msg(msg); - tstmsg_case_msg("getdns_list_set_int(list, 1, ans_int)"); - retval = getdns_list_set_int(list, 1, ans_int); - sprintf(msg, "getdns_list_set_int,retval = %d", retval); - tstmsg_case_msg(msg); + tstmsg_case_msg("getdns_list_set_int(list, 1, ans_int)"); + retval = getdns_list_set_int(list, 1, ans_int); + sprintf(msg, "getdns_list_set_int,retval = %d", retval); + tstmsg_case_msg(msg); - /* test set and get legitimate use case */ + /* test set and get legitimate use case */ - getdns_list_add_item(list, &index); - getdns_list_set_int(list, index, 42); - retval = getdns_list_get_int(list, index, &ans_int); - sprintf(msg, "getdns_list_set/get_int,retval = %d, ans = %d", retval, ans_int); - tstmsg_case_msg(msg); + getdns_list_add_item(list, &index); + getdns_list_set_int(list, index, 42); + retval = getdns_list_get_int(list, index, &ans_int); + sprintf(msg, "getdns_list_set/get_int,retval = %d, ans = %d", retval, + ans_int); + tstmsg_case_msg(msg); - getdns_list_destroy(list); + getdns_list_destroy(list); - tstmsg_case_end(); + tstmsg_case_end(); - return; -} /* tst_intsetget */ + return; +} /* tst_intsetget */ /*---------------------------------------- tst_create */ /** @@ -346,75 +365,71 @@ tst_intsetget(void) void tst_create(void) { - char msg[TSTMSGBUF]; - size_t index; - int i; - getdns_return_t retval; - struct getdns_list *list = NULL; + char msg[TSTMSGBUF]; + size_t index; + int i; + getdns_return_t retval; + struct getdns_list *list = NULL; - /* make sure we can do a simple create/destroy first */ + /* make sure we can do a simple create/destroy first */ - tstmsg_case_begin("tst_create"); + tstmsg_case_begin("tst_create"); - tstmsg_case_msg("getdns_list_create"); - list = getdns_list_create(); + tstmsg_case_msg("getdns_list_create"); + list = getdns_list_create(); - if(list != NULL) - { - tstmsg_case_msg("getdns_list_destroy(list)"); - getdns_list_destroy(list); - } + if (list != NULL) { + tstmsg_case_msg("getdns_list_destroy(list)"); + getdns_list_destroy(list); + } - tstmsg_case_msg("getdns_list_destroy(NULL)"); - getdns_list_destroy(NULL); + tstmsg_case_msg("getdns_list_destroy(NULL)"); + getdns_list_destroy(NULL); - /* add items until we force it to allocate more storage */ + /* add items until we force it to allocate more storage */ - tstmsg_case_msg("getdns_add_item(list) past block size"); - list = getdns_list_create(); - for(i=0; i /* Set up the callback function, which will also do the processing of the results */ -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) +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) { - if (this_callback_type == GETDNS_CALLBACK_COMPLETE) /* This is a callback with data */ - { - char* res = getdns_pretty_print_dict(this_response); - fprintf(stdout, "%s", res); - getdns_dict_destroy(this_response); + if (this_callback_type == GETDNS_CALLBACK_COMPLETE) { /* This is a callback with data */ + char *res = getdns_pretty_print_dict(this_response); + fprintf(stdout, "%s", res); + getdns_dict_destroy(this_response); - } - else if (this_callback_type == GETDNS_CALLBACK_CANCEL) - fprintf(stderr, "The callback with ID %lld was cancelled. Exiting.", this_transaction_id); + } else if (this_callback_type == GETDNS_CALLBACK_CANCEL) + fprintf(stderr, + "The callback with ID %lld was cancelled. Exiting.", + this_transaction_id); else - fprintf(stderr, "The callback got a callback_type of %d. Exiting.", this_callback_type); + fprintf(stderr, + "The callback got a callback_type of %d. Exiting.", + this_callback_type); } int @@ -58,46 +60,47 @@ main() { /* Create the DNS context for this call */ struct getdns_context_t *this_context = NULL; - 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); + 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); } - 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 */ + 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 */ struct event_base *this_event_base; this_event_base = event_base_new(); - if (this_event_base == NULL) - { + if (this_event_base == NULL) { fprintf(stderr, "Trying to create the event base failed."); - return(GETDNS_RETURN_GENERIC_ERROR); + return (GETDNS_RETURN_GENERIC_ERROR); } - (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 */ - const char * this_name = "www.google.com"; - char* this_userarg = "somestring"; // Could add things here to help identify this call + const char *this_name = "www.google.com"; + 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_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); + 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); } // 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) -// { -// fprintf(stderr, "A bad domain name was used: %s. Exiting.", this_name); -// return(GETDNS_RETURN_GENERIC_ERROR); -// } - else - { +// { +// fprintf(stderr, "A bad domain name was used: %s. Exiting.", this_name); +// return(GETDNS_RETURN_GENERIC_ERROR); +// } + else { /* Call the event loop */ event_base_dispatch(this_event_base); // TODO: check the return value above @@ -106,7 +109,6 @@ main() getdns_context_destroy(this_context); /* Assuming we get here, leave gracefully */ exit(EXIT_SUCCESS); -} /* main */ +} /* main */ /* example-simple-answers.c */ - diff --git a/src/test/tests_stub_sync.c b/src/test/tests_stub_sync.c index 48359251..e0cdca22 100644 --- a/src/test/tests_stub_sync.c +++ b/src/test/tests_stub_sync.c @@ -32,50 +32,57 @@ #include "testmessages.h" #include -static void print_response(getdns_dict* response) { - char *dict_str = getdns_pretty_print_dict(response); - if (dict_str) { - fprintf(stdout, "The packet %s\n", dict_str); - free(dict_str); - } +static void +print_response(getdns_dict * response) +{ + char *dict_str = getdns_pretty_print_dict(response); + if (dict_str) { + fprintf(stdout, "The packet %s\n", dict_str); + free(dict_str); + } } -int main() +int +main() { /* Create the DNS context for this call */ struct getdns_context_t *this_context = NULL; - 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); + 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); } - getdns_context_set_resolution_type(this_context, GETDNS_CONTEXT_STUB); - - getdns_dict* response = NULL; - uint32_t responseLen = 0; - getdns_return_t ret = getdns_address_sync(this_context, "www.google.com", NULL, &responseLen, &response); - - if (ret != GETDNS_RETURN_GOOD || response == NULL) { - fprintf(stderr, "Address sync returned error.\n"); - exit(EXIT_FAILURE); - } - print_response(response); - getdns_dict_destroy(response); - - ret = getdns_service_sync(this_context, "www.google.com", NULL, &responseLen, &response); - if (ret != GETDNS_RETURN_GOOD || response == NULL) { - fprintf(stderr, "Service sync returned error.\n"); - exit(EXIT_FAILURE); - } - print_response(response); - getdns_dict_destroy(response); - - /* Clean up */ + getdns_context_set_resolution_type(this_context, GETDNS_CONTEXT_STUB); + + getdns_dict *response = NULL; + uint32_t responseLen = 0; + getdns_return_t ret = + getdns_address_sync(this_context, "www.google.com", NULL, + &responseLen, &response); + + if (ret != GETDNS_RETURN_GOOD || response == NULL) { + fprintf(stderr, "Address sync returned error.\n"); + exit(EXIT_FAILURE); + } + print_response(response); + getdns_dict_destroy(response); + + ret = + getdns_service_sync(this_context, "www.google.com", NULL, + &responseLen, &response); + if (ret != GETDNS_RETURN_GOOD || response == NULL) { + fprintf(stderr, "Service sync returned error.\n"); + exit(EXIT_FAILURE); + } + print_response(response); + getdns_dict_destroy(response); + + /* Clean up */ getdns_context_destroy(this_context); /* Assuming we get here, leave gracefully */ exit(EXIT_SUCCESS); -} /* main */ +} /* main */ /* example-simple-answers.c */ - diff --git a/src/types-internal.h b/src/types-internal.h index 38513bc8..0c04fe8b 100644 --- a/src/types-internal.h +++ b/src/types-internal.h @@ -46,110 +46,109 @@ struct ub_ctx; struct event; struct event_base; -typedef enum network_req_state_enum { - NET_REQ_NOT_SENT, - NET_REQ_IN_FLIGHT, - NET_REQ_FINISHED, - NET_REQ_CANCELED +typedef enum network_req_state_enum +{ + NET_REQ_NOT_SENT, + NET_REQ_IN_FLIGHT, + NET_REQ_FINISHED, + NET_REQ_CANCELED } network_req_state; /** * structure used by validate_extensions() to check extension formats */ -typedef struct getdns_extension_format { - char *extstring; - getdns_data_type exttype; +typedef struct getdns_extension_format +{ + char *extstring; + getdns_data_type exttype; } getdns_extension_format; /** * Request data for unbound **/ -typedef struct getdns_network_req { +typedef struct getdns_network_req +{ /* the async_id from unbound */ - int unbound_id; - /* state var */ - network_req_state state; - /* owner request (contains name) */ - struct getdns_dns_req* owner; + int unbound_id; + /* state var */ + network_req_state state; + /* owner request (contains name) */ + struct getdns_dns_req *owner; - /* request type */ - uint16_t request_type; + /* request type */ + uint16_t request_type; - /* request class */ - uint16_t request_class; + /* request class */ + uint16_t request_class; - /* result */ - ldns_pkt* result; + /* result */ + ldns_pkt *result; - /* next request to issue after this one */ - struct getdns_network_req* next; + /* next request to issue after this one */ + struct getdns_network_req *next; } getdns_network_req; /** * dns request - manages a number of network requests and * the initial data passed to getdns_general */ -typedef struct getdns_dns_req { +typedef struct getdns_dns_req +{ - /* name */ - char *name; + /* name */ + char *name; - /* canceled flag */ - int canceled; + /* canceled flag */ + int canceled; - /* current network request */ + /* current network request */ struct getdns_network_req *current_req; - /* first request in list */ - struct getdns_network_req *first_req; + /* first request in list */ + struct getdns_network_req *first_req; - /* request timeout event */ - struct event* timeout; + /* request timeout event */ + struct event *timeout; - /* local callback timer */ - struct event* local_cb_timer; + /* local callback timer */ + struct event *local_cb_timer; - /* event base this req is scheduled on */ - struct event_base* ev_base; + /* event base this req is scheduled on */ + struct event_base *ev_base; - /* context that owns the request */ - getdns_context_t context; + /* context that owns the request */ + getdns_context_t context; - /* ub_ctx issuing the request */ - struct ub_ctx* unbound; + /* ub_ctx issuing the request */ + struct ub_ctx *unbound; - /* request extensions */ - getdns_dict *extensions; + /* request extensions */ + getdns_dict *extensions; - /* callback data */ - getdns_callback_t user_callback; - void *user_pointer; + /* callback data */ + getdns_callback_t user_callback; + void *user_pointer; - /* the transaction id */ - getdns_transaction_t trans_id; + /* the transaction id */ + getdns_transaction_t trans_id; } getdns_dns_req; /* utility methods */ /* network request utilities */ -void network_req_free(getdns_network_req* net_req); - -getdns_network_req* network_req_new(getdns_dns_req* owner, - uint16_t request_type, - uint16_t request_class, - struct getdns_dict* extensions); +void network_req_free(getdns_network_req * net_req); +getdns_network_req *network_req_new(getdns_dns_req * owner, + uint16_t request_type, + uint16_t request_class, struct getdns_dict *extensions); /* dns request utils */ -getdns_dns_req* dns_req_new(getdns_context_t context, - struct ub_ctx* unbound, - const char* name, - uint16_t request_type, - struct getdns_dict *extensions); +getdns_dns_req *dns_req_new(getdns_context_t context, + struct ub_ctx *unbound, + const char *name, uint16_t request_type, struct getdns_dict *extensions); - -void dns_req_free(getdns_dns_req* req); +void dns_req_free(getdns_dns_req * req); #endif diff --git a/src/util-internal.c b/src/util-internal.c index d264c99a..1d12147f 100644 --- a/src/util-internal.c +++ b/src/util-internal.c @@ -34,7 +34,7 @@ * (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 "getdns/getdns.h" #include #include "dict.h" @@ -48,448 +48,506 @@ * validate_extensions. */ getdns_extension_format extformats[] = { - {"add_opt_parameters", t_dict}, - {"add_warning_for_bad_dns", t_int}, - {"dnssec_return_only_secure", t_int}, - {"dnssec_return_status", t_int}, - {"dnssec_return_supporting_responses", t_int}, - {"return_api_information", t_int}, - {"return_both_v4_and_v6", t_int}, - {"return_call_debugging", t_int}, - {"specify_class", t_int}, + {"add_opt_parameters", t_dict}, + {"add_warning_for_bad_dns", t_int}, + {"dnssec_return_only_secure", t_int}, + {"dnssec_return_status", t_int}, + {"dnssec_return_supporting_responses", t_int}, + {"return_api_information", t_int}, + {"return_both_v4_and_v6", t_int}, + {"return_call_debugging", t_int}, + {"specify_class", t_int}, }; -getdns_return_t getdns_dict_util_set_string(getdns_dict* dict, char* name, - const char* value) { - /* account for the null term */ - if (value == NULL) { - return GETDNS_RETURN_WRONG_TYPE_REQUESTED; - } - getdns_bindata type_bin = { strlen(value) + 1, (uint8_t*) value }; - return getdns_dict_set_bindata(dict, name, &type_bin); +getdns_return_t +getdns_dict_util_set_string(getdns_dict * dict, char *name, const char *value) +{ + /* account for the null term */ + if (value == NULL) { + return GETDNS_RETURN_WRONG_TYPE_REQUESTED; + } + getdns_bindata type_bin = { strlen(value) + 1, (uint8_t *) value }; + return getdns_dict_set_bindata(dict, name, &type_bin); } -getdns_return_t getdns_dict_util_get_string(getdns_dict* dict, char* name, - char** result) { - struct getdns_bindata *bindata = NULL; - if (!result) { - return GETDNS_RETURN_GENERIC_ERROR; - } - *result = NULL; - getdns_dict_get_bindata(dict, name, &bindata); - if (!bindata) { - return GETDNS_RETURN_GENERIC_ERROR; - } - *result = (char*) bindata->data; - return GETDNS_RETURN_GOOD; +getdns_return_t +getdns_dict_util_get_string(getdns_dict * dict, char *name, char **result) +{ + struct getdns_bindata *bindata = NULL; + if (!result) { + return GETDNS_RETURN_GENERIC_ERROR; + } + *result = NULL; + getdns_dict_get_bindata(dict, name, &bindata); + if (!bindata) { + return GETDNS_RETURN_GENERIC_ERROR; + } + *result = (char *) bindata->data; + return GETDNS_RETURN_GOOD; } -getdns_return_t dict_to_sockaddr(getdns_dict* ns, struct sockaddr_storage* output) { - char* address_type = NULL; - struct getdns_bindata *address_data = NULL; - uint32_t port = 53; - memset(output, 0, sizeof(struct sockaddr_storage)); - output->ss_family = AF_UNSPEC; +getdns_return_t +dict_to_sockaddr(getdns_dict * ns, struct sockaddr_storage * output) +{ + char *address_type = NULL; + struct getdns_bindata *address_data = NULL; + uint32_t port = 53; + memset(output, 0, sizeof(struct sockaddr_storage)); + output->ss_family = AF_UNSPEC; - uint32_t prt = 0; - if (getdns_dict_get_int(ns, GETDNS_STR_PORT, &prt) == GETDNS_RETURN_GOOD) { - port = prt; - } + uint32_t prt = 0; + if (getdns_dict_get_int(ns, GETDNS_STR_PORT, + &prt) == GETDNS_RETURN_GOOD) { + port = prt; + } - getdns_dict_util_get_string(ns, GETDNS_STR_ADDRESS_TYPE, &address_type); - getdns_dict_get_bindata(ns, GETDNS_STR_ADDRESS_DATA, &address_data); - if (!address_type || !address_data) { - return GETDNS_RETURN_GENERIC_ERROR; - } - if (strncmp(GETDNS_STR_IPV4, address_type, strlen(GETDNS_STR_IPV4)) == 0) { - /* data is an in_addr_t */ - struct sockaddr_in* addr = (struct sockaddr_in*) output; - addr->sin_family = AF_INET; - addr->sin_port = htons((uint16_t)port); - memcpy(&(addr->sin_addr), address_data->data, address_data->size); - } else { - /* data is a v6 addr in host order */ - struct sockaddr_in6* addr = (struct sockaddr_in6*) output; - addr->sin6_family = AF_INET6; - addr->sin6_port = htons((uint16_t)port); - memcpy(&(addr->sin6_addr), address_data->data, address_data->size); - } - return GETDNS_RETURN_GOOD; + getdns_dict_util_get_string(ns, GETDNS_STR_ADDRESS_TYPE, + &address_type); + getdns_dict_get_bindata(ns, GETDNS_STR_ADDRESS_DATA, &address_data); + if (!address_type || !address_data) { + return GETDNS_RETURN_GENERIC_ERROR; + } + if (strncmp(GETDNS_STR_IPV4, address_type, + strlen(GETDNS_STR_IPV4)) == 0) { + /* data is an in_addr_t */ + struct sockaddr_in *addr = (struct sockaddr_in *) output; + addr->sin_family = AF_INET; + addr->sin_port = htons((uint16_t) port); + memcpy(&(addr->sin_addr), address_data->data, + address_data->size); + } else { + /* data is a v6 addr in host order */ + struct sockaddr_in6 *addr = (struct sockaddr_in6 *) output; + addr->sin6_family = AF_INET6; + addr->sin6_port = htons((uint16_t) port); + memcpy(&(addr->sin6_addr), address_data->data, + address_data->size); + } + return GETDNS_RETURN_GOOD; } -getdns_return_t sockaddr_to_dict(struct sockaddr_storage* address, getdns_dict** output) { - if (!output || !address) { - return GETDNS_RETURN_GENERIC_ERROR; - } - getdns_bindata addr_data; - *output = NULL; - getdns_dict* result = getdns_dict_create(); - if (address->ss_family == AF_INET) { - struct sockaddr_in* addr = (struct sockaddr_in*) address; - getdns_dict_util_set_string(result, GETDNS_STR_ADDRESS_TYPE, GETDNS_STR_IPV4); - addr_data.size = sizeof(addr->sin_addr); - addr_data.data = (uint8_t*) &(addr->sin_addr); - getdns_dict_set_bindata(result, GETDNS_STR_ADDRESS_DATA, &addr_data); - } else if (address->ss_family == AF_INET6) { - struct sockaddr_in6* addr = (struct sockaddr_in6*) address; - getdns_dict_util_set_string(result, GETDNS_STR_ADDRESS_TYPE, GETDNS_STR_IPV6); - addr_data.size = sizeof(addr->sin6_addr); - addr_data.data = (uint8_t*) &(addr->sin6_addr); - getdns_dict_set_bindata(result, GETDNS_STR_ADDRESS_DATA, &addr_data); - } else { - // invalid - getdns_dict_destroy(result); - return GETDNS_RETURN_GENERIC_ERROR; - } - *output = result; - return GETDNS_RETURN_GOOD; +getdns_return_t +sockaddr_to_dict(struct sockaddr_storage * address, getdns_dict ** output) +{ + if (!output || !address) { + return GETDNS_RETURN_GENERIC_ERROR; + } + getdns_bindata addr_data; + *output = NULL; + getdns_dict *result = getdns_dict_create(); + if (address->ss_family == AF_INET) { + struct sockaddr_in *addr = (struct sockaddr_in *) address; + getdns_dict_util_set_string(result, GETDNS_STR_ADDRESS_TYPE, + GETDNS_STR_IPV4); + addr_data.size = sizeof(addr->sin_addr); + addr_data.data = (uint8_t *) & (addr->sin_addr); + getdns_dict_set_bindata(result, GETDNS_STR_ADDRESS_DATA, + &addr_data); + } else if (address->ss_family == AF_INET6) { + struct sockaddr_in6 *addr = (struct sockaddr_in6 *) address; + getdns_dict_util_set_string(result, GETDNS_STR_ADDRESS_TYPE, + GETDNS_STR_IPV6); + addr_data.size = sizeof(addr->sin6_addr); + addr_data.data = (uint8_t *) & (addr->sin6_addr); + getdns_dict_set_bindata(result, GETDNS_STR_ADDRESS_DATA, + &addr_data); + } else { + // invalid + getdns_dict_destroy(result); + return GETDNS_RETURN_GENERIC_ERROR; + } + *output = result; + return GETDNS_RETURN_GOOD; } /* result must be freed */ -static char* convert_rdf_to_str(ldns_rdf* rdf) { - if (ldns_rdf_get_type(rdf) == LDNS_RDF_TYPE_DNAME) { - ldns_dname2canonical(rdf); - } - return ldns_rdf2str(rdf); +static char * +convert_rdf_to_str(ldns_rdf * rdf) +{ + if (ldns_rdf_get_type(rdf) == LDNS_RDF_TYPE_DNAME) { + ldns_dname2canonical(rdf); + } + return ldns_rdf2str(rdf); } /* create the header dict */ -static getdns_dict *create_reply_header_dict(ldns_pkt* reply) { - /* { "id": 23456, "qr": 1, "opcode": 0, ... }, */ - int r = 0; - getdns_dict* result = getdns_dict_create(); - if (!result) { - return NULL; - } - /* cheat since we know GETDNS_RETURN_GOOD == 0 */ - r |= getdns_dict_set_int(result, GETDNS_STR_KEY_ID, ldns_pkt_id(reply)); - r |= getdns_dict_set_int(result, GETDNS_STR_KEY_QR, ldns_pkt_qr(reply)); - r |= getdns_dict_set_int(result, GETDNS_STR_KEY_OPC, (int) ldns_pkt_get_opcode(reply)); +static getdns_dict * +create_reply_header_dict(ldns_pkt * reply) +{ + /* { "id": 23456, "qr": 1, "opcode": 0, ... }, */ + int r = 0; + getdns_dict *result = getdns_dict_create(); + if (!result) { + return NULL; + } + /* cheat since we know GETDNS_RETURN_GOOD == 0 */ + r |= getdns_dict_set_int(result, GETDNS_STR_KEY_ID, + ldns_pkt_id(reply)); + r |= getdns_dict_set_int(result, GETDNS_STR_KEY_QR, + ldns_pkt_qr(reply)); + r |= getdns_dict_set_int(result, GETDNS_STR_KEY_OPC, + (int) ldns_pkt_get_opcode(reply)); - if (r != 0) { - getdns_dict_destroy(result); - result = NULL; - } - return result; + if (r != 0) { + getdns_dict_destroy(result); + result = NULL; + } + return result; } -static getdns_dict *create_reply_question_dict(ldns_pkt* reply) { - /* { "qname": , "qtype": 1, "qclass": 1 } */ - int r = 0; - ldns_rr *question = NULL; - char* qname; - getdns_dict* result = getdns_dict_create(); - if (!result) { - return NULL; - } - question = ldns_rr_list_rr(ldns_pkt_question(reply), 0); - r |= getdns_dict_set_int(result, GETDNS_STR_KEY_QTYPE, (int) ldns_rr_get_type(question)); - r |= getdns_dict_set_int(result, GETDNS_STR_KEY_QCLASS, (int) ldns_rr_get_class(question)); - qname = convert_rdf_to_str(ldns_rr_owner(question)); - if (qname) { - r |= getdns_dict_util_set_string(result, GETDNS_STR_KEY_QNAME, qname); - free(qname); - } else { - r = 1; - } - if (r != 0) { - getdns_dict_destroy(result); - result = NULL; - } - return result; +static getdns_dict * +create_reply_question_dict(ldns_pkt * reply) +{ + /* { "qname": , "qtype": 1, "qclass": 1 } */ + int r = 0; + ldns_rr *question = NULL; + char *qname; + getdns_dict *result = getdns_dict_create(); + if (!result) { + return NULL; + } + question = ldns_rr_list_rr(ldns_pkt_question(reply), 0); + r |= getdns_dict_set_int(result, GETDNS_STR_KEY_QTYPE, + (int) ldns_rr_get_type(question)); + r |= getdns_dict_set_int(result, GETDNS_STR_KEY_QCLASS, + (int) ldns_rr_get_class(question)); + qname = convert_rdf_to_str(ldns_rr_owner(question)); + if (qname) { + r |= getdns_dict_util_set_string(result, GETDNS_STR_KEY_QNAME, + qname); + free(qname); + } else { + r = 1; + } + if (r != 0) { + getdns_dict_destroy(result); + result = NULL; + } + return result; } -static getdns_dict *create_dict_from_rdf(ldns_rdf* rdf) { - /* - create a dict w/ rdata_raw and special fields if needed - i.e. - { - "ipv4_address": - "rdata_raw": - } - */ - int r = 0; - getdns_bindata rbin = { ldns_rdf_size(rdf), ldns_rdf_data(rdf) }; - getdns_dict* result = getdns_dict_create(); - r |= getdns_dict_set_bindata(result, GETDNS_STR_KEY_RDATA_RAW, &rbin); - if (ldns_rdf_get_type(rdf) == LDNS_RDF_TYPE_AAAA) { - r |= getdns_dict_set_bindata(result, GETDNS_STR_KEY_V6_ADDR, &rbin); - } else if (ldns_rdf_get_type(rdf) == LDNS_RDF_TYPE_A) { - r |= getdns_dict_set_bindata(result, GETDNS_STR_KEY_V4_ADDR, &rbin); - } - if (r != 0) { - getdns_dict_destroy(result); - result = NULL; - } - return result; +static getdns_dict * +create_dict_from_rdf(ldns_rdf * rdf) +{ + /* + * create a dict w/ rdata_raw and special fields if needed + * i.e. + * { + * "ipv4_address": + * "rdata_raw": + * } + */ + int r = 0; + getdns_bindata rbin = { ldns_rdf_size(rdf), ldns_rdf_data(rdf) }; + getdns_dict *result = getdns_dict_create(); + r |= getdns_dict_set_bindata(result, GETDNS_STR_KEY_RDATA_RAW, &rbin); + if (ldns_rdf_get_type(rdf) == LDNS_RDF_TYPE_AAAA) { + r |= getdns_dict_set_bindata(result, GETDNS_STR_KEY_V6_ADDR, + &rbin); + } else if (ldns_rdf_get_type(rdf) == LDNS_RDF_TYPE_A) { + r |= getdns_dict_set_bindata(result, GETDNS_STR_KEY_V4_ADDR, + &rbin); + } + if (r != 0) { + getdns_dict_destroy(result); + result = NULL; + } + return result; } -static getdns_dict *create_dict_from_rr(ldns_rr* rr) { - /* - { - "name": , - "type": 1, - "class": 1, - "ttl": 33000, - "rdata": - { - "ipv4_address": - "rdata_raw": - } - } - */ - int r = 0; - char * name = NULL; - getdns_dict *result = getdns_dict_create(); - size_t rd_count = ldns_rr_rd_count(rr); - ldns_rdf* owner = ldns_rr_owner(rr); - r |= getdns_dict_set_int(result, GETDNS_STR_KEY_TYPE, (int) ldns_rr_get_type(rr)); - r |= getdns_dict_set_int(result, GETDNS_STR_KEY_CLASS, (int) ldns_rr_get_class(rr)); - r |= getdns_dict_set_int(result, GETDNS_STR_KEY_TTL, ldns_rr_ttl(rr)); - if (owner) { - name = convert_rdf_to_str(owner); - if (name) { - r |= getdns_dict_util_set_string(result, GETDNS_STR_KEY_NAME, name); - free(name); - } else { - r = 1; - } - } - /* create rdatas */ - if (rd_count >= 1) { - getdns_dict* rdata = create_dict_from_rdf(ldns_rr_rdf(rr, 0)); - r |= getdns_dict_set_dict(result, GETDNS_STR_KEY_RDATA, rdata); - getdns_dict_destroy(rdata); - } - /* TODO - if more than one, is rdata a list? */ +static getdns_dict * +create_dict_from_rr(ldns_rr * rr) +{ + /* + * { + * "name": , + * "type": 1, + * "class": 1, + * "ttl": 33000, + * "rdata": + * { + * "ipv4_address": + * "rdata_raw": + * } + * } + */ + int r = 0; + char *name = NULL; + getdns_dict *result = getdns_dict_create(); + size_t rd_count = ldns_rr_rd_count(rr); + ldns_rdf *owner = ldns_rr_owner(rr); + r |= getdns_dict_set_int(result, GETDNS_STR_KEY_TYPE, + (int) ldns_rr_get_type(rr)); + r |= getdns_dict_set_int(result, GETDNS_STR_KEY_CLASS, + (int) ldns_rr_get_class(rr)); + r |= getdns_dict_set_int(result, GETDNS_STR_KEY_TTL, ldns_rr_ttl(rr)); + if (owner) { + name = convert_rdf_to_str(owner); + if (name) { + r |= getdns_dict_util_set_string(result, + GETDNS_STR_KEY_NAME, name); + free(name); + } else { + r = 1; + } + } + /* create rdatas */ + if (rd_count >= 1) { + getdns_dict *rdata = create_dict_from_rdf(ldns_rr_rdf(rr, 0)); + r |= getdns_dict_set_dict(result, GETDNS_STR_KEY_RDATA, rdata); + getdns_dict_destroy(rdata); + } + /* TODO - if more than one, is rdata a list? */ - if (r != 0) { - getdns_dict_destroy(result); - result = NULL; - } - return result; + if (r != 0) { + getdns_dict_destroy(result); + result = NULL; + } + return result; } /* helper to convert an rr_list to getdns_list. returns a list of objects where each object is a result from create_dict_from_rr */ -static getdns_list *create_list_from_rr_list(ldns_rr_list* rr_list) { - size_t i = 0; - size_t idx = 0; - int r = 0; - getdns_list *result = getdns_list_create(); - for (i = 0; i < ldns_rr_list_rr_count(rr_list); ++i) { - ldns_rr *rr = ldns_rr_list_rr(rr_list, i); - getdns_dict* rrdict = create_dict_from_rr(rr); - r |= getdns_list_add_item(result, &idx); - r |= getdns_list_set_dict(result, idx, rrdict); - getdns_dict_destroy(rrdict); - } - if (r != 0) { - getdns_list_destroy(result); - result = NULL; - } - return result; +static getdns_list * +create_list_from_rr_list(ldns_rr_list * rr_list) +{ + size_t i = 0; + size_t idx = 0; + int r = 0; + getdns_list *result = getdns_list_create(); + for (i = 0; i < ldns_rr_list_rr_count(rr_list); ++i) { + ldns_rr *rr = ldns_rr_list_rr(rr_list, i); + getdns_dict *rrdict = create_dict_from_rr(rr); + r |= getdns_list_add_item(result, &idx); + r |= getdns_list_set_dict(result, idx, rrdict); + getdns_dict_destroy(rrdict); + } + if (r != 0) { + getdns_list_destroy(result); + result = NULL; + } + return result; } /* helper to add the ipv4 or ipv6 bin data to the list of addrs */ -static getdns_return_t add_only_addresses(getdns_list* addrs, ldns_rr_list* rr_list) { - int r = 0; - size_t i = 0; - for (i = 0; i < ldns_rr_list_rr_count(rr_list); ++i) { - ldns_rr *rr = ldns_rr_list_rr(rr_list, i); - size_t j = 0; - size_t rd_count = ldns_rr_rd_count(rr); - for (j = 0; j < rd_count; ++j) { - size_t item_idx = 0; - ldns_rdf* rdf = ldns_rr_rdf(rr, j); - if (ldns_rdf_get_type(rdf) == LDNS_RDF_TYPE_A || - ldns_rdf_get_type(rdf) == LDNS_RDF_TYPE_AAAA) { - getdns_bindata rbin = { ldns_rdf_size(rdf), ldns_rdf_data(rdf) }; - r |= getdns_list_add_item(addrs, &item_idx); - r |= getdns_list_set_bindata(addrs, item_idx, &rbin); - } - } - } - return r; +static getdns_return_t +add_only_addresses(getdns_list * addrs, ldns_rr_list * rr_list) +{ + int r = 0; + size_t i = 0; + for (i = 0; i < ldns_rr_list_rr_count(rr_list); ++i) { + ldns_rr *rr = ldns_rr_list_rr(rr_list, i); + size_t j = 0; + size_t rd_count = ldns_rr_rd_count(rr); + for (j = 0; j < rd_count; ++j) { + size_t item_idx = 0; + ldns_rdf *rdf = ldns_rr_rdf(rr, j); + if (ldns_rdf_get_type(rdf) == LDNS_RDF_TYPE_A || + ldns_rdf_get_type(rdf) == LDNS_RDF_TYPE_AAAA) { + getdns_bindata rbin = + { ldns_rdf_size(rdf), ldns_rdf_data(rdf) }; + r |= getdns_list_add_item(addrs, &item_idx); + r |= getdns_list_set_bindata(addrs, item_idx, + &rbin); + } + } + } + return r; } -static getdns_dict *create_reply_dict(getdns_network_req* req, - getdns_list *just_addrs) { - /* turn a packet into this glorious structure +static getdns_dict * +create_reply_dict(getdns_network_req * req, getdns_list * just_addrs) +{ + /* turn a packet into this glorious structure + * + * { # This is the first reply + * "header": { "id": 23456, "qr": 1, "opcode": 0, ... }, + * "question": { "qname": , "qtype": 1, "qclass": 1 }, + * "answer": + * [ + * { + * "name": , + * "type": 1, + * "class": 1, + * "ttl": 33000, + * "rdata": + * { + * "ipv4_address": + * "rdata_raw": + * } + * } + * ], + * "authority": + * [ + * { + * "name": , + * "type": 1, + * "class": 1, + * "ttl": 600, + * "rdata": + * { + * "ipv4_address": + * "rdata_raw": + * } + * } + * ] + * "additional": [], + * "canonical_name": , + * "answer_type": GETDNS_NAMETYPE_DNS + * } + * + */ + int r = 0; + ldns_pkt *reply = req->result; + ldns_rr_list *rr_list = NULL; + ldns_rr *question = NULL; + getdns_dict *subdict = NULL; + getdns_list *sublist = NULL; + char *name = NULL; - { # This is the first reply - "header": { "id": 23456, "qr": 1, "opcode": 0, ... }, - "question": { "qname": , "qtype": 1, "qclass": 1 }, - "answer": - [ - { - "name": , - "type": 1, - "class": 1, - "ttl": 33000, - "rdata": - { - "ipv4_address": - "rdata_raw": - } - } - ], - "authority": - [ - { - "name": , - "type": 1, - "class": 1, - "ttl": 600, - "rdata": - { - "ipv4_address": - "rdata_raw": - } - } - ] - "additional": [], - "canonical_name": , - "answer_type": GETDNS_NAMETYPE_DNS - } + getdns_dict *result = getdns_dict_create(); + if (!result) { + return NULL; + } + /* header */ + subdict = create_reply_header_dict(reply); + r |= getdns_dict_set_dict(result, GETDNS_STR_KEY_HEADER, subdict); + getdns_dict_destroy(subdict); - */ - int r = 0; - ldns_pkt *reply = req->result; - ldns_rr_list *rr_list = NULL; - ldns_rr* question = NULL; - getdns_dict *subdict = NULL; - getdns_list *sublist = NULL; - char* name = NULL; + /* question */ + subdict = create_reply_question_dict(reply); + r |= getdns_dict_set_dict(result, GETDNS_STR_KEY_QUESTION, subdict); + getdns_dict_destroy(subdict); - getdns_dict *result = getdns_dict_create(); - if (!result) { - return NULL; - } - /* header */ - subdict = create_reply_header_dict(reply); - r |= getdns_dict_set_dict(result, GETDNS_STR_KEY_HEADER, subdict); - getdns_dict_destroy(subdict); + /* answers */ + rr_list = ldns_pkt_answer(reply); + sublist = create_list_from_rr_list(rr_list); + r |= getdns_dict_set_list(result, GETDNS_STR_KEY_ANSWER, sublist); + getdns_list_destroy(sublist); + if ((req->request_type == GETDNS_RRTYPE_A || + req->request_type == GETDNS_RRTYPE_AAAA) && + just_addrs != NULL) { + /* add to just addrs */ + r |= add_only_addresses(just_addrs, rr_list); + } - /* question */ - subdict = create_reply_question_dict(reply); - r |= getdns_dict_set_dict(result, GETDNS_STR_KEY_QUESTION, subdict); - getdns_dict_destroy(subdict); + /* authority */ + rr_list = ldns_pkt_authority(reply); + sublist = create_list_from_rr_list(rr_list); + r |= getdns_dict_set_list(result, GETDNS_STR_KEY_AUTHORITY, sublist); + getdns_list_destroy(sublist); - /* answers */ - rr_list = ldns_pkt_answer(reply); - sublist = create_list_from_rr_list(rr_list); - r |= getdns_dict_set_list(result, GETDNS_STR_KEY_ANSWER, sublist); - getdns_list_destroy(sublist); - if ((req->request_type == GETDNS_RRTYPE_A || - req->request_type == GETDNS_RRTYPE_AAAA) && - just_addrs != NULL) { - /* add to just addrs */ - r |= add_only_addresses(just_addrs, rr_list); - } + /* additional */ + rr_list = ldns_pkt_additional(reply); + sublist = create_list_from_rr_list(rr_list); + r |= getdns_dict_set_list(result, GETDNS_STR_KEY_ADDITIONAL, sublist); + getdns_list_destroy(sublist); - /* authority */ - rr_list = ldns_pkt_authority(reply); - sublist = create_list_from_rr_list(rr_list); - r |= getdns_dict_set_list(result, GETDNS_STR_KEY_AUTHORITY, sublist); - getdns_list_destroy(sublist); - - /* additional */ - rr_list = ldns_pkt_additional(reply); - sublist = create_list_from_rr_list(rr_list); - r |= getdns_dict_set_list(result, GETDNS_STR_KEY_ADDITIONAL, sublist); - getdns_list_destroy(sublist); - - /* other stuff */ - r |= getdns_dict_set_int(result, GETDNS_STR_KEY_ANSWER_TYPE, GETDNS_NAMETYPE_DNS); - question = ldns_rr_list_rr(ldns_pkt_question(reply), 0); - name = convert_rdf_to_str(ldns_rr_owner(question)); - if (name) { - r |= getdns_dict_util_set_string(result, GETDNS_STR_KEY_CANONICAL_NM, name); - free(name); - } else { - r |= 1; - } - if (r != 0) { - getdns_dict_destroy(result); - result = NULL; - } - return result; + /* other stuff */ + r |= getdns_dict_set_int(result, GETDNS_STR_KEY_ANSWER_TYPE, + GETDNS_NAMETYPE_DNS); + question = ldns_rr_list_rr(ldns_pkt_question(reply), 0); + name = convert_rdf_to_str(ldns_rr_owner(question)); + if (name) { + r |= getdns_dict_util_set_string(result, + GETDNS_STR_KEY_CANONICAL_NM, name); + free(name); + } else { + r |= 1; + } + if (r != 0) { + getdns_dict_destroy(result); + result = NULL; + } + return result; } -static char* get_canonical_name(const char* name) { - ldns_rdf* rdf = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME, name); - if (!rdf) { - return NULL; - } - char* result = convert_rdf_to_str(rdf); - ldns_rdf_deep_free(rdf); - return result; +static char * +get_canonical_name(const char *name) +{ + ldns_rdf *rdf = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME, name); + if (!rdf) { + return NULL; + } + char *result = convert_rdf_to_str(rdf); + ldns_rdf_deep_free(rdf); + return result; } -getdns_dict *create_getdns_response(struct getdns_dns_req* completed_request) { - getdns_dict* result = getdns_dict_create(); - getdns_list* replies_full = getdns_list_create(); - getdns_list* just_addrs = NULL; - getdns_list* replies_tree = getdns_list_create(); - getdns_network_req *netreq = completed_request->first_req; - char* canonical_name = NULL; +getdns_dict * +create_getdns_response(struct getdns_dns_req * completed_request) +{ + getdns_dict *result = getdns_dict_create(); + getdns_list *replies_full = getdns_list_create(); + getdns_list *just_addrs = NULL; + getdns_list *replies_tree = getdns_list_create(); + getdns_network_req *netreq = completed_request->first_req; + char *canonical_name = NULL; - int r = 0; + int r = 0; - if (completed_request->first_req->request_class == GETDNS_RRTYPE_A || - completed_request->first_req->request_class == GETDNS_RRTYPE_AAAA) { - just_addrs = getdns_list_create(); - } + if (completed_request->first_req->request_class == GETDNS_RRTYPE_A || + completed_request->first_req->request_class == + GETDNS_RRTYPE_AAAA) { + just_addrs = getdns_list_create(); + } - r |= getdns_dict_set_int(result, GETDNS_STR_KEY_STATUS, GETDNS_RESPSTATUS_GOOD); - canonical_name = get_canonical_name(completed_request->name); - r |= getdns_dict_util_set_string(result, GETDNS_STR_KEY_CANONICAL_NM, canonical_name); - free(canonical_name); - r |= getdns_dict_set_int(result, GETDNS_STR_KEY_ANSWER_TYPE, GETDNS_NAMETYPE_DNS); + r |= getdns_dict_set_int(result, GETDNS_STR_KEY_STATUS, + GETDNS_RESPSTATUS_GOOD); + canonical_name = get_canonical_name(completed_request->name); + r |= getdns_dict_util_set_string(result, GETDNS_STR_KEY_CANONICAL_NM, + canonical_name); + free(canonical_name); + r |= getdns_dict_set_int(result, GETDNS_STR_KEY_ANSWER_TYPE, + GETDNS_NAMETYPE_DNS); - while (netreq) { - getdns_bindata full_data; - full_data.data = NULL; - full_data.size = 0; - ldns_pkt *pkt = netreq->result; - ldns_status s = ldns_pkt2wire(&(full_data.data), pkt, &(full_data.size)); - size_t idx = 0; - /* reply tree */ - getdns_dict *reply = create_reply_dict(netreq, just_addrs); - r |= getdns_list_add_item(replies_tree, &idx); - r |= getdns_list_set_dict(replies_tree, idx, reply); - getdns_dict_destroy(reply); - /* buffer */ - if (s == LDNS_STATUS_OK) { - r |= getdns_list_add_item(replies_full, &idx); - r |= getdns_list_set_bindata(replies_full, idx, &full_data); - free(full_data.data); - } else { - r = 1; - break; - } - netreq = netreq->next; - } + while (netreq) { + getdns_bindata full_data; + full_data.data = NULL; + full_data.size = 0; + ldns_pkt *pkt = netreq->result; + ldns_status s = + ldns_pkt2wire(&(full_data.data), pkt, &(full_data.size)); + size_t idx = 0; + /* reply tree */ + getdns_dict *reply = create_reply_dict(netreq, just_addrs); + r |= getdns_list_add_item(replies_tree, &idx); + r |= getdns_list_set_dict(replies_tree, idx, reply); + getdns_dict_destroy(reply); + /* buffer */ + if (s == LDNS_STATUS_OK) { + r |= getdns_list_add_item(replies_full, &idx); + r |= getdns_list_set_bindata(replies_full, idx, + &full_data); + free(full_data.data); + } else { + r = 1; + break; + } + netreq = netreq->next; + } - r |= getdns_dict_set_list(result, GETDNS_STR_KEY_REPLIES_TREE, replies_tree); - r |= getdns_dict_set_list(result, GETDNS_STR_KEY_REPLIES_FULL, replies_full); - if (just_addrs) { - r |= getdns_dict_set_list(result, GETDNS_STR_KEY_JUST_ADDRS, just_addrs); - } - - /* cleanup */ - getdns_list_destroy(replies_tree); - getdns_list_destroy(replies_full); - getdns_list_destroy(just_addrs); + r |= getdns_dict_set_list(result, GETDNS_STR_KEY_REPLIES_TREE, + replies_tree); + r |= getdns_dict_set_list(result, GETDNS_STR_KEY_REPLIES_FULL, + replies_full); + if (just_addrs) { + r |= getdns_dict_set_list(result, GETDNS_STR_KEY_JUST_ADDRS, + just_addrs); + } - if (r != 0) { - getdns_dict_destroy(result); - result = NULL; - } + /* cleanup */ + getdns_list_destroy(replies_tree); + getdns_list_destroy(replies_full); + getdns_list_destroy(just_addrs); - return result; + if (r != 0) { + getdns_dict_destroy(result); + result = NULL; + } + + return result; } /** @@ -501,55 +559,58 @@ getdns_dict *create_getdns_response(struct getdns_dns_req* completed_request) { char * reverse_address(char *addr_str) { - ldns_rdf *addr_rdf; - ldns_rdf *rev_rdf; - char *rev_str; + ldns_rdf *addr_rdf; + ldns_rdf *rev_rdf; + char *rev_str; - addr_rdf = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_A, addr_str); - if (! addr_rdf) { - addr_rdf = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_AAAA, addr_str); - if (! addr_rdf) - return NULL; - } + addr_rdf = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_A, addr_str); + if (!addr_rdf) { + addr_rdf = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_AAAA, addr_str); + if (!addr_rdf) + return NULL; + } - rev_rdf = ldns_rdf_address_reverse(addr_rdf); - ldns_rdf_deep_free(addr_rdf); - if (! rev_rdf) - return NULL; + rev_rdf = ldns_rdf_address_reverse(addr_rdf); + ldns_rdf_deep_free(addr_rdf); + if (!rev_rdf) + return NULL; - rev_str = ldns_rdf2str(rev_rdf); - ldns_rdf_deep_free(rev_rdf); - return rev_str; -} + rev_str = ldns_rdf2str(rev_rdf); + ldns_rdf_deep_free(rev_rdf); + return rev_str; +} -static int extformatcmp(const void *a, const void *b) +static int +extformatcmp(const void *a, const void *b) { - return strcmp(((getdns_extension_format *)a)->extstring, - ((getdns_extension_format *)b)->extstring); + return strcmp(((getdns_extension_format *) a)->extstring, + ((getdns_extension_format *) b)->extstring); } /*---------------------------------------- validate_extensions */ getdns_return_t -validate_extensions(getdns_dict *extensions) +validate_extensions(getdns_dict * extensions) { - struct getdns_dict_item *item; - getdns_extension_format *extformat; + struct getdns_dict_item *item; + getdns_extension_format *extformat; - if(extensions) - LDNS_RBTREE_FOR(item, struct getdns_dict_item *, &(extensions->root)) { - getdns_extension_format key; - key.extstring = (char *)item->node.key; - extformat = bsearch(&key, extformats, - sizeof(extformats) / - sizeof(getdns_extension_format), - sizeof(getdns_extension_format), extformatcmp); - if (! extformat) - return GETDNS_RETURN_NO_SUCH_EXTENSION; + if (extensions) + LDNS_RBTREE_FOR(item, struct getdns_dict_item *, + &(extensions->root)) + { + getdns_extension_format key; + key.extstring = (char *) item->node.key; + extformat = bsearch(&key, extformats, + sizeof(extformats) / + sizeof(getdns_extension_format), + sizeof(getdns_extension_format), extformatcmp); + if (!extformat) + return GETDNS_RETURN_NO_SUCH_EXTENSION; - if (item->dtype != extformat->exttype) - return GETDNS_RETURN_EXTENSION_MISFORMAT; - } - return GETDNS_RETURN_GOOD; -} /* validate_extensions */ + if (item->dtype != extformat->exttype) + return GETDNS_RETURN_EXTENSION_MISFORMAT; + } + return GETDNS_RETURN_GOOD; +} /* validate_extensions */ /* util-internal.c */ diff --git a/src/util-internal.h b/src/util-internal.h index ef0f89f9..5fac211f 100644 --- a/src/util-internal.h +++ b/src/util-internal.h @@ -44,19 +44,21 @@ struct getdns_dns_req; /* convert an ip address dict to a sock storage */ -getdns_return_t dict_to_sockaddr(getdns_dict* ns, struct sockaddr_storage* output); -getdns_return_t sockaddr_to_dict(struct sockaddr_storage* sockaddr, getdns_dict** output); +getdns_return_t dict_to_sockaddr(getdns_dict * ns, + struct sockaddr_storage *output); +getdns_return_t sockaddr_to_dict(struct sockaddr_storage *sockaddr, + getdns_dict ** output); -getdns_dict *create_getdns_response(struct getdns_dns_req* completed_request); +getdns_dict *create_getdns_response(struct getdns_dns_req *completed_request); /* dict util */ /* set a string as bindata */ -getdns_return_t getdns_dict_util_set_string(getdns_dict* dict, char* name, - const char* value); +getdns_return_t getdns_dict_util_set_string(getdns_dict * dict, char *name, + const char *value); /* get a string from a dict. result is valid as long as dict is valid */ -getdns_return_t getdns_dict_util_get_string(getdns_dict* dict, char* name, - char** result); +getdns_return_t getdns_dict_util_get_string(getdns_dict * dict, char *name, + char **result); char *reverse_address(char *addr_str); /** @@ -67,6 +69,6 @@ char *reverse_address(char *addr_str); * @return GETDNS_RETURN_NO_SUCH_EXTENSION A name in the extensions dict is not a valid extension. * @return GETDNS_RETURN_EXTENSION_MISFORMAT One or more of the extensions has a bad format. */ -getdns_return_t validate_extensions(getdns_dict *extensions); +getdns_return_t validate_extensions(getdns_dict * extensions); /* util-internal.h */ diff --git a/src/validate_dnssec.c b/src/validate_dnssec.c index a6886502..2704599f 100644 --- a/src/validate_dnssec.c +++ b/src/validate_dnssec.c @@ -38,16 +38,14 @@ * */ getdns_return_t -getdns_validate_dnssec( - struct getdns_bindata *record_to_validate, - struct getdns_list *bundle_of_support_records, - struct getdns_list *trust_anchor_rdatas -) +getdns_validate_dnssec(struct getdns_bindata * record_to_validate, + struct getdns_list * bundle_of_support_records, + struct getdns_list * trust_anchor_rdatas) { - UNUSED_PARAM(record_to_validate); - UNUSED_PARAM(bundle_of_support_records); - UNUSED_PARAM(trust_anchor_rdatas); - return GETDNS_RETURN_GOOD; -} /* getdns_validate_dnssec */ + UNUSED_PARAM(record_to_validate); + UNUSED_PARAM(bundle_of_support_records); + UNUSED_PARAM(trust_anchor_rdatas); + return GETDNS_RETURN_GOOD; +} /* getdns_validate_dnssec */ /* getdns_validate_dnssec.c */