diff --git a/src/dict.c b/src/dict.c index e3332f52..b42d07ba 100644 --- a/src/dict.c +++ b/src/dict.c @@ -85,12 +85,9 @@ getdns_dict_find_and_add(struct getdns_dict *dict, const char *key) /*---------------------------------------- getdns_dict_get_names */ getdns_return_t -getdns_dict_get_names(const struct getdns_dict * dict, - struct getdns_list ** answer) +getdns_dict_get_names(const getdns_dict *dict, getdns_list **answer) { struct getdns_dict_item *item; - size_t index; - struct getdns_bindata bindata; if (!dict || !answer) return GETDNS_RETURN_INVALID_PARAMETER; @@ -103,11 +100,7 @@ getdns_dict_get_names(const struct getdns_dict * dict, RBTREE_FOR(item, struct getdns_dict_item *, (getdns_rbtree_t *)&(dict->root)) { - if (getdns_list_add_item(*answer, &index) != GETDNS_RETURN_GOOD) - continue; - bindata.size = strlen(item->node.key) + 1; - bindata.data = (void *) item->node.key; - getdns_list_set_bindata(*answer, index, &bindata); + getdns_list_append_string(*answer, item->node.key); } return GETDNS_RETURN_GOOD; } /* getdns_dict_get_names */ @@ -465,6 +458,35 @@ getdns_dict_set_bindata(struct getdns_dict * dict, const char *name, return GETDNS_RETURN_GOOD; } /* getdns_dict_set_bindata */ +/*---------------------------------------- getdns_dict_set_bindata */ +getdns_return_t +getdns_dict_util_set_string(getdns_dict *dict, char *name, const char *value) +{ + struct getdns_dict_item *item; + getdns_bindata *newbindata; + + if (!dict || !name || !value) + return GETDNS_RETURN_INVALID_PARAMETER; + + if (!(newbindata = GETDNS_MALLOC(dict->mf, getdns_bindata))) + return GETDNS_RETURN_MEMORY_ERROR; + + newbindata->size = strlen(value); + if (!(newbindata->data = (void *)getdns_strdup(&dict->mf, value))) + goto error_free_bindata; + + if ((item = getdns_dict_find_and_add(dict, name))) { + + item->dtype = t_bindata; + item->data.bindata = newbindata; + return GETDNS_RETURN_GOOD; + } + GETDNS_FREE(dict->mf, newbindata->data); +error_free_bindata: + GETDNS_FREE(dict->mf, newbindata); + return GETDNS_RETURN_MEMORY_ERROR; +} /* getdns_dict_util_set_dict */ + /*---------------------------------------- getdns_dict_set_int */ getdns_return_t getdns_dict_set_int(struct getdns_dict * dict, const char *name, diff --git a/src/list.c b/src/list.c index 0da051dd..896df4ad 100644 --- a/src/list.c +++ b/src/list.c @@ -456,6 +456,43 @@ getdns_list_set_bindata(struct getdns_list * list, size_t index, return GETDNS_RETURN_GOOD; } /* getdns_list_set_bindata */ +/*----------------------------------------- getdns_list_set_string */ +static getdns_return_t +getdns_list_set_string(getdns_list *list, size_t index, const char *value) +{ + getdns_bindata *newbindata; + getdns_return_t retval; + + if (!list || !value) + return GETDNS_RETURN_INVALID_PARAMETER; + + if (index > list->numinuse) + return GETDNS_RETURN_NO_SUCH_LIST_ITEM; + + if (!(newbindata = GETDNS_MALLOC(list->mf, getdns_bindata))) + return GETDNS_RETURN_MEMORY_ERROR; + + newbindata->size = strlen(value); + if (!(newbindata->data = (void *)getdns_strdup(&list->mf, value))) { + GETDNS_FREE(list->mf, newbindata); + return GETDNS_RETURN_MEMORY_ERROR; + } + + if (index == list->numinuse) { + retval = getdns_list_add_item(list, &index); + if (retval != GETDNS_RETURN_GOOD) { + GETDNS_FREE(list->mf, newbindata->data); + GETDNS_FREE(list->mf, newbindata); + return retval; + } + } else + getdns_list_destroy_item(list, index); + + list->items[index].dtype = t_bindata; + list->items[index].data.bindata = newbindata; + return GETDNS_RETURN_GOOD; +} /* getdns_list_set_string */ + /*---------------------------------------- getdns_list_set_int */ getdns_return_t getdns_list_set_int(struct getdns_list * list, size_t index, @@ -500,6 +537,12 @@ getdns_list_append_bindata(getdns_list *list, const getdns_bindata *child_bindat return getdns_list_set_bindata(list, list->numinuse, child_bindata); } getdns_return_t +getdns_list_append_string(getdns_list *list, const char *value) +{ + if (!list) return GETDNS_RETURN_INVALID_PARAMETER; + return getdns_list_set_string(list, list->numinuse, value); +} +getdns_return_t getdns_list_append_int(getdns_list *list, uint32_t child_int) { if (!list) return GETDNS_RETURN_INVALID_PARAMETER; diff --git a/src/util-internal.c b/src/util-internal.c index 0cf964d5..ffd63941 100644 --- a/src/util-internal.c +++ b/src/util-internal.c @@ -73,19 +73,6 @@ static getdns_extension_format extformats[] = { {"specify_class", t_int}, }; -static struct getdns_bindata IPv4_str_bindata = { 5, (void *)"IPv4" }; -static struct getdns_bindata IPv6_str_bindata = { 5, (void *)"IPv6" }; - -getdns_return_t -getdns_dict_util_set_string(struct getdns_dict * dict, char *name, const char *value) -{ - /* account for the null term */ - if (value == NULL) { - return GETDNS_RETURN_WRONG_TYPE_REQUESTED; - } - struct 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(struct getdns_dict * dict, char *name, char **result) @@ -606,9 +593,8 @@ priv_getdns_create_reply_dict(getdns_context *context, getdns_network_req *req, bindata.data = rdf_iter->pos; if (!set_dict(&rr_dict, getdns_dict_create_with_context(context)) || - getdns_dict_set_bindata(rr_dict, "address_type", - rr_type == GETDNS_RRTYPE_A ? - &IPv4_str_bindata : &IPv6_str_bindata) || + getdns_dict_util_set_string(rr_dict, "address_type", + rr_type == GETDNS_RRTYPE_A ? "IPv4" : "IPv6" ) || getdns_dict_set_bindata(rr_dict,"address_data",&bindata) || diff --git a/src/util-internal.h b/src/util-internal.h index 9236ac0b..bdb2a73d 100644 --- a/src/util-internal.h +++ b/src/util-internal.h @@ -76,6 +76,9 @@ getdns_return_t getdns_list_append_list(getdns_list *list, const getdns_list *child_list); getdns_return_t getdns_list_append_bindata(getdns_list *list, const getdns_bindata *child_bindata); +getdns_return_t getdns_list_append_string(getdns_list *list, + const char *value); + getdns_return_t getdns_list_append_int(getdns_list *list, uint32_t child_uint32);