From ebb519919ae63d23f5630695f3af61956aeb40c7 Mon Sep 17 00:00:00 2001 From: Willem Toorop Date: Thu, 12 Mar 2015 23:09:04 +0100 Subject: [PATCH] Get rid of ldns usage, and a malloc/free --- src/general.c | 55 ++++++++++++++++++++++++++++++++++++++++++--- src/util-internal.c | 32 -------------------------- src/util-internal.h | 1 - 3 files changed, 52 insertions(+), 36 deletions(-) diff --git a/src/general.c b/src/general.c index 8531b78f..dc81b8f1 100644 --- a/src/general.c +++ b/src/general.c @@ -286,7 +286,7 @@ priv_getdns_hostname_loop(getdns_context *context, getdns_eventloop *loop, struct getdns_bindata *address_data; struct getdns_bindata *address_type; uint16_t req_type; - char *name; + char name[1024]; getdns_return_t retval; if ((retval = @@ -308,11 +308,60 @@ priv_getdns_hostname_loop(getdns_context *context, getdns_eventloop *loop, req_type = GETDNS_RRTYPE_PTR; else return GETDNS_RETURN_INVALID_PARAMETER; - if ((name = reverse_address(address_data)) == NULL) + + switch (address_data->size) { + case 4: + (void)snprintf(name, sizeof(name), + "%hhu.%hhu.%hhu.%hhu.in-addr.arpa.", + ((uint8_t *)address_data->data)[3], + ((uint8_t *)address_data->data)[2], + ((uint8_t *)address_data->data)[1], + ((uint8_t *)address_data->data)[0]); + break; + case 16: + (void)snprintf(name, sizeof(name), + "%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx." + "%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx." + "%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx." + "%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.%hhx.ip6.arpa.", + ((uint8_t *)address_data->data)[15] & 0x0F, + ((uint8_t *)address_data->data)[15] >> 4, + ((uint8_t *)address_data->data)[14] & 0x0F, + ((uint8_t *)address_data->data)[14] >> 4, + ((uint8_t *)address_data->data)[13] & 0x0F, + ((uint8_t *)address_data->data)[13] >> 4, + ((uint8_t *)address_data->data)[12] & 0x0F, + ((uint8_t *)address_data->data)[12] >> 4, + ((uint8_t *)address_data->data)[11] & 0x0F, + ((uint8_t *)address_data->data)[11] >> 4, + ((uint8_t *)address_data->data)[10] & 0x0F, + ((uint8_t *)address_data->data)[10] >> 4, + ((uint8_t *)address_data->data)[9] & 0x0F, + ((uint8_t *)address_data->data)[9] >> 4, + ((uint8_t *)address_data->data)[8] & 0x0F, + ((uint8_t *)address_data->data)[8] >> 4, + ((uint8_t *)address_data->data)[7] & 0x0F, + ((uint8_t *)address_data->data)[7] >> 4, + ((uint8_t *)address_data->data)[6] & 0x0F, + ((uint8_t *)address_data->data)[6] >> 4, + ((uint8_t *)address_data->data)[5] & 0x0F, + ((uint8_t *)address_data->data)[5] >> 4, + ((uint8_t *)address_data->data)[4] & 0x0F, + ((uint8_t *)address_data->data)[4] >> 4, + ((uint8_t *)address_data->data)[3] & 0x0F, + ((uint8_t *)address_data->data)[3] >> 4, + ((uint8_t *)address_data->data)[2] & 0x0F, + ((uint8_t *)address_data->data)[2] >> 4, + ((uint8_t *)address_data->data)[1] & 0x0F, + ((uint8_t *)address_data->data)[1] >> 4, + ((uint8_t *)address_data->data)[0] & 0x0F, + ((uint8_t *)address_data->data)[0] >> 4); + break; + default: return GETDNS_RETURN_INVALID_PARAMETER; + } retval = priv_getdns_general_loop(context, loop, name, req_type, extensions, userarg, transaction_id, callback); - free(name); return retval; } /* getdns_hostname_loop */ diff --git a/src/util-internal.c b/src/util-internal.c index bbbfd4da..98596c62 100644 --- a/src/util-internal.c +++ b/src/util-internal.c @@ -848,38 +848,6 @@ error_free_result: return NULL; } -/** - * reverse an IP address for PTR lookup - * @param address_data IP address to reverse - * @return NULL on allocation failure - * @return reversed string on success, caller must free storage via call to free() - */ -char * -reverse_address(struct getdns_bindata *address_data) -{ - ldns_rdf *addr_rdf; - ldns_rdf *rev_rdf; - char *rev_str; - - if (address_data->size == 4) - addr_rdf = ldns_rdf_new(LDNS_RDF_TYPE_A, 4, address_data->data); - else if (address_data->size == 16) - addr_rdf = ldns_rdf_new(LDNS_RDF_TYPE_AAAA, 16, address_data->data); - else - return NULL; - if (!addr_rdf) - return NULL; - - rev_rdf = ldns_rdf_address_reverse(addr_rdf); - ldns_rdf_free(addr_rdf); - if (!rev_rdf) - return NULL; - - rev_str = ldns_rdf2str(rev_rdf); - ldns_rdf_deep_free(rev_rdf); - return rev_str; -} - static int extformatcmp(const void *a, const void *b) { diff --git a/src/util-internal.h b/src/util-internal.h index 07aa68d2..e469fae8 100644 --- a/src/util-internal.h +++ b/src/util-internal.h @@ -122,7 +122,6 @@ getdns_return_t sockaddr_to_dict(struct getdns_context *context, struct getdns_dns_req; struct getdns_dict *create_getdns_response(struct getdns_dns_req *completed_request); -char *reverse_address(struct getdns_bindata *address_data); getdns_return_t validate_dname(const char* dname);