Use strncmp and add some utils

This commit is contained in:
Neel Goyal 2013-08-15 18:16:50 -04:00
parent ff83b43081
commit 075990d330
2 changed files with 24 additions and 5 deletions

View File

@ -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;

View File

@ -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);