From 075990d3309c924f21fd06e272faf34f5beaac39 Mon Sep 17 00:00:00 2001 From: Neel Goyal Date: Thu, 15 Aug 2013 18:16:50 -0400 Subject: [PATCH] Use strncmp and add some utils --- src/util-internal.c | 24 +++++++++++++++++++----- src/util-internal.h | 5 +++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/util-internal.c b/src/util-internal.c index 4e0ae0b6..78db5e05 100644 --- a/src/util-internal.c +++ b/src/util-internal.c @@ -32,13 +32,27 @@ getdns_return_t getdns_dict_util_set_string(getdns_dict* dict, char* name, char* value) { - char data[strlen(value) + 1]; - data[strlen(value)] = 0; - memcpy(data, value, strlen(value)); - getdns_bindata type_bin = { strlen(value) + 1, (uint8_t*) data }; + getdns_bindata type_bin = { strlen(value), (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 = malloc(bindata->size + 1); + *result[bindata->size] = 0; + memcpy(*result, bindata->data, bindata->size); + return GETDNS_RETURN_GOOD; +} + getdns_return_t dict_to_sockaddr(getdns_dict* ns, struct sockaddr_storage* output) { struct getdns_bindata *address_type = NULL; struct getdns_bindata *address_data = NULL; @@ -51,7 +65,7 @@ getdns_return_t dict_to_sockaddr(getdns_dict* ns, struct sockaddr_storage* outpu if (!address_type || !address_data) { return GETDNS_RETURN_GENERIC_ERROR; } - if (strcmp((char*) address_type->data, GETDNS_STR_IPV4) == 0) { + if (strncmp(GETDNS_STR_IPV4, (char*) address_type->data, strlen(GETDNS_STR_IPV4)) == 0) { /* data is an in_addr_t */ struct sockaddr_in* addr = (struct sockaddr_in*) output; addr->sin_family = AF_INET; diff --git a/src/util-internal.h b/src/util-internal.h index acf198dc..c080fa2a 100644 --- a/src/util-internal.h +++ b/src/util-internal.h @@ -42,6 +42,11 @@ ldns_pkt *create_new_pkt(getdns_context_t context, getdns_dict *create_getdns_response(ldns_pkt* pkt); /* dict util */ +/* set a string as bindata - non null terminated */ getdns_return_t getdns_dict_util_set_string(getdns_dict* dict, char* name, char* value); +/* get a string which was a non null terminated bindata. *result must + * be freed if the call is successful */ +getdns_return_t getdns_dict_util_get_string(getdns_dict* dict, char* name, + char** result);