getdns/src/dict.c

1231 lines
33 KiB
C
Raw Normal View History

2013-07-15 17:43:30 -05:00
/**
*
2014-02-20 16:35:27 -06:00
* getdns dict management functions, note that the internal storage is
* accomplished via an _getdns_rbtree_t
*
* Interfaces originally taken from the getdns API description pseudo implementation.
2013-07-15 17:43:30 -05:00
*
*/
/*
2014-02-25 07:12:33 -06:00
* Copyright (c) 2013, NLnet Labs, Verisign, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the names of the copyright holders nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Verisign, Inc. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2013-07-15 17:43:30 -05:00
*/
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
2013-11-12 10:00:19 -06:00
#include "types-internal.h"
2013-12-03 09:09:26 -06:00
#include "util-internal.h"
#include "dict.h"
#include "list.h"
#include "rr-dict.h"
#include "const-info.h"
2015-03-02 04:38:46 -06:00
#include "gldns/gbuffer.h"
2015-03-20 13:55:17 -05:00
#include "gldns/wire2str.h"
2013-07-15 17:43:30 -05:00
2015-09-30 09:05:19 -05:00
static char *_json_ptr_first(const struct mem_funcs *mf,
const char *jptr, char *first, size_t first_sz)
2015-09-30 09:05:19 -05:00
{
const char *next_ref, *k;
char *j;
2015-09-30 09:05:19 -05:00
if (*jptr != '/')
return (char *)jptr;
jptr++;
if (!(next_ref = strchr(jptr, '/')))
next_ref = strchr(jptr, '\0');
2015-09-30 09:05:19 -05:00
if (next_ref - jptr > first_sz)
first = GETDNS_XMALLOC(*mf, char, next_ref - jptr + 1);
for (j = first, k = jptr; k < next_ref; j++, k++)
2015-09-30 09:05:19 -05:00
*j = k[0] == '~' && k[1] == '1' ? (k++, '/') : *k;
*j = '\0';
for (j = first, k = first; *k; j++, k++)
2015-09-30 09:05:19 -05:00
*j = k[0] == '~' && k[1] == '0' ? (k++, '~') : *k;
*j = '\0';
return first;
}
static struct getdns_dict_item *
_find_dict_item(const getdns_dict *dict, const char *jptr)
{
char first_spc[1024], *first;
struct getdns_dict_item *d;
first = _json_ptr_first(&dict->mf, jptr,
first_spc, sizeof(first_spc));
2015-09-30 09:05:19 -05:00
d = (struct getdns_dict_item *) _getdns_rbtree_search(
(_getdns_rbtree_t *)&(dict->root), first);
2015-09-30 09:05:19 -05:00
if (first && first != jptr && first != first_spc)
GETDNS_FREE(dict->mf, first);
2015-09-30 09:05:19 -05:00
return d;
}
static struct getdns_dict_item *
_delete_dict_item(const getdns_dict *dict, const char *jptr)
{
char first_spc[1024], *first;
struct getdns_dict_item *d;
first = _json_ptr_first(&dict->mf, jptr,
first_spc, sizeof(first_spc));
d = (struct getdns_dict_item *) _getdns_rbtree_delete(
(_getdns_rbtree_t *)&(dict->root), first);
if (first && first != jptr && first != first_spc)
GETDNS_FREE(dict->mf, first);
return d;
}
static char *
_json_ptr_keydup(const struct mem_funcs *mf, const char *jptr)
{
char *first = _json_ptr_first(mf, jptr, NULL, 0);
if (first == jptr || first == jptr + 1) {
2015-10-06 02:46:59 -05:00
size_t sz = strlen(jptr) + 1;
if ((first = GETDNS_XMALLOC(*mf, char, sz)))
memcpy(first, jptr, sz);
}
return first;
}
getdns_return_t
_getdns_dict_find(const getdns_dict *dict, const char *key, getdns_item **item)
{
const char *next;
struct getdns_dict_item *d;
if (!(d = _find_dict_item(dict, key)))
return GETDNS_RETURN_NO_SUCH_DICT_NAME;
if (*key != '/' || !(next = strchr(key + 1, '/'))) {
*item = &d->i;
return GETDNS_RETURN_GOOD;
} else switch (d->i.dtype) { /* Key was nested reference */
case t_dict: return _getdns_dict_find(d->i.data.dict, next, item);
case t_list: return _getdns_list_find(d->i.data.list, next, item);
default : /* Trying to dereference a non list or dict */
return GETDNS_RETURN_WRONG_TYPE_REQUESTED;
}
}
2015-10-02 08:26:05 -05:00
getdns_return_t
_getdns_dict_find_and_add(
getdns_dict *dict, const char *key, getdns_item **item)
{
2015-10-02 08:26:05 -05:00
const char *next;
2015-09-30 09:05:19 -05:00
struct getdns_dict_item *d;
2015-10-02 08:26:05 -05:00
if (!(d = _find_dict_item(dict, key))) {
2014-02-20 16:35:27 -06:00
/* add a node */
2015-09-30 09:05:19 -05:00
d = GETDNS_MALLOC(dict->mf, struct getdns_dict_item);
d->node.key = _json_ptr_keydup(&dict->mf, key);
2015-09-30 09:05:19 -05:00
_getdns_rbtree_insert(&(dict->root), (_getdns_rbnode_t *) d);
2015-10-02 08:26:05 -05:00
if (*key != '/' || !(next = strchr(key + 1, '/'))) {
(void) memset(&d->i.data, 0, sizeof(d->i.data));
*item = &d->i;
return GETDNS_RETURN_GOOD;
}
if ((next[1] == '0' || next[1] == '-') &&
(next[2] == '/' || next[2] == '\0')) {
d->i.data.list =_getdns_list_create_with_mf(&dict->mf);
return _getdns_list_find_and_add(
d->i.data.list, next, item);
}
d->i.data.dict = _getdns_dict_create_with_mf(&dict->mf);
return _getdns_dict_find_and_add(
d->i.data.dict, next, item);
}
if (*key != '/' || !(next = strchr(key + 1, '/'))) {
*item = &d->i;
return GETDNS_RETURN_GOOD;
} else switch (d->i.dtype) { /* Key was nested reference */
2015-10-02 08:26:05 -05:00
case t_dict: return _getdns_dict_find_and_add(
d->i.data.dict, next, item);
case t_list: return _getdns_list_find_and_add(
d->i.data.list, next, item);
default : /* Trying to dereference a non list or dict */
return GETDNS_RETURN_WRONG_TYPE_REQUESTED;
}
} /* getdns_dict_find_and_add */
/*---------------------------------------- getdns_dict_get_names
*/
getdns_return_t
2015-07-13 08:41:40 -05:00
getdns_dict_get_names(const getdns_dict *dict, getdns_list **answer)
{
struct getdns_dict_item *item;
if (!dict || !answer)
return GETDNS_RETURN_INVALID_PARAMETER;
2013-12-08 17:52:38 -06:00
*answer = getdns_list_create_with_extended_memory_functions(
dict->mf.mf_arg, dict->mf.mf.ext.malloc,
dict->mf.mf.ext.realloc, dict->mf.mf.ext.free);
if (!*answer)
return GETDNS_RETURN_NO_SUCH_DICT_NAME;
RBTREE_FOR(item, struct getdns_dict_item *,
(_getdns_rbtree_t *)&(dict->root)) {
_getdns_list_append_string(*answer, item->node.key);
}
return GETDNS_RETURN_GOOD;
} /* getdns_dict_get_names */
2013-07-15 17:43:30 -05:00
/*---------------------------------------- getdns_dict_get_data_type */
getdns_return_t
2015-09-30 09:05:19 -05:00
getdns_dict_get_data_type(
const getdns_dict *dict, const char *name, getdns_data_type *answer)
{
getdns_return_t r;
2015-09-30 09:05:19 -05:00
getdns_item *item;
if (!dict || !name || !answer)
2014-01-10 09:45:45 -06:00
return GETDNS_RETURN_INVALID_PARAMETER;
if ((r = _getdns_dict_find(dict, name, &item)))
return r;
*answer = item->dtype;
return GETDNS_RETURN_GOOD;
} /* getdns_dict_get_data_type */
/*---------------------------------------- getdns_dict_get_dict */
getdns_return_t
2015-09-30 09:05:19 -05:00
getdns_dict_get_dict(
const getdns_dict *dict, const char *name, getdns_dict **answer)
{
getdns_return_t r;
2015-09-30 09:05:19 -05:00
getdns_item *item;
if (!dict || !name || !answer)
return GETDNS_RETURN_INVALID_PARAMETER;
if ((r = _getdns_dict_find(dict, name, &item)))
return r;
2014-01-10 09:45:45 -06:00
if (item->dtype != t_dict)
2014-01-10 09:45:45 -06:00
return GETDNS_RETURN_WRONG_TYPE_REQUESTED;
*answer = item->data.dict;
return GETDNS_RETURN_GOOD;
} /* getdns_dict_get_dict */
/*---------------------------------------- getdns_dict_get_list */
getdns_return_t
2015-09-30 09:05:19 -05:00
getdns_dict_get_list(
const getdns_dict *dict, const char *name, getdns_list **answer)
{
getdns_return_t r;
2015-09-30 09:05:19 -05:00
getdns_item *item;
if (!dict || !name || !answer)
return GETDNS_RETURN_INVALID_PARAMETER;
if ((r = _getdns_dict_find(dict, name, &item)))
return r;
2014-01-10 09:45:45 -06:00
if (item->dtype != t_list)
return GETDNS_RETURN_WRONG_TYPE_REQUESTED;
*answer = item->data.list;
return GETDNS_RETURN_GOOD;
} /* getdns_dict_get_list */
/*---------------------------------------- getdns_dict_get_bindata */
getdns_return_t
2015-09-30 09:05:19 -05:00
getdns_dict_get_bindata(
const getdns_dict *dict, const char *name, getdns_bindata **answer)
{
getdns_return_t r;
2015-09-30 09:05:19 -05:00
getdns_item *item;
if (!dict || !name || !answer)
return GETDNS_RETURN_INVALID_PARAMETER;
if ((r = _getdns_dict_find(dict, name, &item)))
return GETDNS_RETURN_NO_SUCH_DICT_NAME;
2014-01-10 09:45:45 -06:00
if (item->dtype != t_bindata)
return GETDNS_RETURN_WRONG_TYPE_REQUESTED;
*answer = item->data.bindata;
return GETDNS_RETURN_GOOD;
} /* getdns_dict_get_bindata */
2013-07-15 17:43:30 -05:00
/*---------------------------------------- getdns_dict_get_int */
getdns_return_t
2015-09-30 09:05:19 -05:00
getdns_dict_get_int(
const getdns_dict *dict, const char *name, uint32_t *answer)
{
getdns_return_t r;
2015-09-30 09:05:19 -05:00
getdns_item *item;
if (!dict || !name || !answer)
return GETDNS_RETURN_INVALID_PARAMETER;
if ((r = _getdns_dict_find(dict, name, &item)))
return r;
2014-01-10 09:45:45 -06:00
if (item->dtype != t_int)
return GETDNS_RETURN_WRONG_TYPE_REQUESTED;
*answer = item->data.n;
return GETDNS_RETURN_GOOD;
} /* getdns_dict_get_int */
2013-07-15 17:43:30 -05:00
struct getdns_dict *
2013-12-08 17:52:38 -06:00
getdns_dict_create_with_extended_memory_functions(
void *userarg,
void *(*malloc)(void *userarg, size_t),
void *(*realloc)(void *userarg, void *, size_t),
void (*free)(void *userarg, void *))
{
struct getdns_dict *dict;
2013-12-08 17:52:38 -06:00
mf_union mf;
2013-07-15 17:43:30 -05:00
2013-11-12 10:00:19 -06:00
if (!malloc || !realloc || !free)
return NULL;
2013-12-08 17:52:38 -06:00
mf.ext.malloc = malloc;
dict = userarg == MF_PLAIN
? (struct getdns_dict*)(*mf.pln.malloc)(
sizeof(struct getdns_dict))
: (struct getdns_dict*)(*mf.ext.malloc)(userarg,
sizeof(struct getdns_dict));
if (!dict)
return NULL;
2013-12-08 17:52:38 -06:00
dict->mf.mf_arg = userarg;
dict->mf.mf.ext.malloc = malloc;
dict->mf.mf.ext.realloc = realloc;
dict->mf.mf.ext.free = free;
_getdns_rbtree_init(&(dict->root),
(int (*)(const void *, const void *)) strcmp);
return dict;
2013-11-12 10:00:19 -06:00
}
2013-12-08 17:52:38 -06:00
struct getdns_dict *
getdns_dict_create_with_memory_functions(void *(*malloc)(size_t),
void *(*realloc)(void *, size_t), void (*free)(void *))
2013-12-08 17:52:38 -06:00
{
mf_union mf;
mf.pln.malloc = malloc;
mf.pln.realloc = realloc;
mf.pln.free = free;
return getdns_dict_create_with_extended_memory_functions(
MF_PLAIN, mf.ext.malloc, mf.ext.realloc, mf.ext.free);
2013-12-08 17:52:38 -06:00
}
2013-11-12 10:00:19 -06:00
/*-------------------------- getdns_dict_create_with_context */
struct getdns_dict *
2013-12-06 08:54:06 -06:00
getdns_dict_create_with_context(struct getdns_context *context)
2013-11-12 10:00:19 -06:00
{
if (context)
2013-12-08 17:52:38 -06:00
return getdns_dict_create_with_extended_memory_functions(
context->mf.mf_arg, context->mf.mf.ext.malloc,
context->mf.mf.ext.realloc, context->mf.mf.ext.free);
2013-11-12 10:00:19 -06:00
else
return getdns_dict_create_with_memory_functions(&malloc,
&realloc, &free);
} /* getdns_dict_create_with_context */
/*---------------------------------------- getdns_dict_create */
struct getdns_dict *
getdns_dict_create()
{
return getdns_dict_create_with_context(NULL);
} /* getdns_dict_create */
/*---------------------------------------- _getdns_dict_copy */
/**
* private function used to make a copy of a dict structure,
* the caller is responsible * for freeing storage allocated to returned value
* @param srcdict the dictionary structure to copy
2013-10-10 15:35:29 -05:00
* @param dstdict the copy destination
* @return the address of the copy of the dictionary structure on success
* @return NULL on error (out of memory, invalid srcdict)
*/
getdns_return_t
_getdns_dict_copy(const struct getdns_dict * srcdict,
struct getdns_dict ** dstdict)
{
struct getdns_dict_item *item;
char *key;
getdns_return_t retval;
if (!dstdict)
return GETDNS_RETURN_INVALID_PARAMETER;
if (!srcdict) {
*dstdict = NULL;
return GETDNS_RETURN_GOOD;
}
2013-12-08 17:52:38 -06:00
*dstdict = getdns_dict_create_with_extended_memory_functions(
srcdict->mf.mf_arg,
srcdict->mf.mf.ext.malloc,
srcdict->mf.mf.ext.realloc,
srcdict->mf.mf.ext.free);
if (!*dstdict)
return GETDNS_RETURN_GENERIC_ERROR;
retval = GETDNS_RETURN_GOOD;
RBTREE_FOR(item, struct getdns_dict_item *,
(struct _getdns_rbtree_t *)&(srcdict->root)) {
key = (char *) item->node.key;
2015-09-30 09:05:19 -05:00
switch (item->i.dtype) {
case t_bindata:
retval = getdns_dict_set_bindata(*dstdict, key,
2015-09-30 09:05:19 -05:00
item->i.data.bindata);
break;
case t_dict:
retval = getdns_dict_set_dict(*dstdict, key,
2015-09-30 09:05:19 -05:00
item->i.data.dict);
break;
case t_int:
retval = getdns_dict_set_int(*dstdict, key,
2015-09-30 09:05:19 -05:00
item->i.data.n);
break;
case t_list:
retval = getdns_dict_set_list(*dstdict, key,
2015-09-30 09:05:19 -05:00
item->i.data.list);
break;
}
if (retval != GETDNS_RETURN_GOOD) {
getdns_dict_destroy(*dstdict);;
*dstdict = NULL;
return retval;
}
}
return GETDNS_RETURN_GOOD;
} /* _getdns_dict_copy */
/*---------------------------------------- getdns_dict_item_free */
/**
* private function used to release storage associated with a dictionary item
2014-05-21 09:21:45 -05:00
* @param node all memory in this structure and its children will be freed
* @param arg is a dict who's custom memory function will be used
* to free the items
* @return void
*/
2015-08-19 09:15:26 -05:00
static void
getdns_dict_item_free(_getdns_rbnode_t * node, void *arg)
{
struct getdns_dict_item *item = (struct getdns_dict_item *) node;
2013-11-12 10:00:19 -06:00
struct getdns_dict *dict = (struct getdns_dict *)arg;
if (!item)
return;
2015-09-30 09:05:19 -05:00
switch (item->i.dtype) {
case t_bindata:
2015-09-30 09:05:19 -05:00
_getdns_bindata_destroy(&dict->mf, item->i.data.bindata);
break;
case t_dict:
2015-09-30 09:05:19 -05:00
getdns_dict_destroy(item->i.data.dict);
break;
case t_list:
2015-09-30 09:05:19 -05:00
getdns_list_destroy(item->i.data.list);
break;
default:
break;
}
if (item->node.key)
GETDNS_FREE(dict->mf, (void *)item->node.key);
GETDNS_FREE(dict->mf, item);
} /* getdns_dict_item_free */
/*---------------------------------------- getdns_dict_destroy */
void
getdns_dict_destroy(struct getdns_dict *dict)
{
if (!dict) return;
_getdns_traverse_postorder(&(dict->root), getdns_dict_item_free, dict);
GETDNS_FREE(dict->mf, dict);
} /* getdns_dict_destroy */
/*---------------------------------------- getdns_dict_set_dict */
getdns_return_t
2015-09-30 09:05:19 -05:00
getdns_dict_set_dict(
getdns_dict *dict, const char *name, const getdns_dict *child_dict)
{
2015-09-30 09:05:19 -05:00
getdns_item *item;
getdns_dict *newdict;
2015-10-02 08:26:05 -05:00
getdns_return_t r;
if (!dict || !name || !child_dict)
return GETDNS_RETURN_INVALID_PARAMETER;
2015-10-02 08:26:05 -05:00
if ((r = _getdns_dict_copy(child_dict, &newdict)))
return r;
2015-10-02 08:26:05 -05:00
if ((r = _getdns_dict_find_and_add(dict, name, &item))) {
getdns_dict_destroy(newdict);
2015-10-02 08:26:05 -05:00
return r;
}
item->dtype = t_dict;
item->data.dict = newdict;
return GETDNS_RETURN_GOOD;
} /* getdns_dict_set_dict */
/*---------------------------------------- getdns_dict_set_list */
getdns_return_t
2015-09-30 09:05:19 -05:00
getdns_dict_set_list(
getdns_dict *dict, const char *name, const getdns_list *child_list)
{
2015-09-30 09:05:19 -05:00
getdns_item *item;
getdns_list *newlist;
2015-10-02 08:26:05 -05:00
getdns_return_t r;
2014-01-10 09:35:03 -06:00
if (!dict || !name || !child_list)
return GETDNS_RETURN_INVALID_PARAMETER;
2015-10-02 08:26:05 -05:00
if ((r = _getdns_list_copy(child_list, &newlist)))
return r;
2015-10-02 08:26:05 -05:00
if ((r = _getdns_dict_find_and_add(dict, name, &item))) {
getdns_list_destroy(newlist);
2015-10-02 08:26:05 -05:00
return r;
}
item->dtype = t_list;
item->data.list = newlist;
return GETDNS_RETURN_GOOD;
} /* getdns_dict_set_list */
2013-07-15 17:43:30 -05:00
/*---------------------------------------- getdns_dict_set_bindata */
getdns_return_t
2015-09-30 09:05:19 -05:00
getdns_dict_set_bindata(
getdns_dict *dict, const char *name, const getdns_bindata *child_bindata)
{
2015-09-30 09:05:19 -05:00
getdns_item *item;
getdns_bindata *newbindata;
2015-10-02 08:26:05 -05:00
getdns_return_t r;
if (!dict || !name || !child_bindata)
2014-01-10 09:35:03 -06:00
return GETDNS_RETURN_INVALID_PARAMETER;
2015-10-02 08:26:05 -05:00
if (!(newbindata = _getdns_bindata_copy(&dict->mf, child_bindata)))
return GETDNS_RETURN_MEMORY_ERROR;
2015-10-02 08:26:05 -05:00
if ((r = _getdns_dict_find_and_add(dict, name, &item))) {
_getdns_bindata_destroy(&dict->mf, newbindata);
2015-10-02 08:26:05 -05:00
return r;
}
item->dtype = t_bindata;
item->data.bindata = newbindata;
return GETDNS_RETURN_GOOD;
} /* getdns_dict_set_bindata */
2013-07-15 17:43:30 -05:00
2015-07-13 08:41:40 -05:00
/*---------------------------------------- getdns_dict_set_bindata */
getdns_return_t
getdns_dict_util_set_string(getdns_dict *dict, char *name, const char *value)
{
2015-09-30 09:05:19 -05:00
getdns_item *item;
getdns_bindata *newbindata;
2015-10-02 08:26:05 -05:00
getdns_return_t r;
2015-07-13 08:41:40 -05:00
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);
2015-10-02 08:26:05 -05:00
if (!(newbindata->data = (void *)_getdns_strdup(&dict->mf, value))) {
GETDNS_FREE(dict->mf, newbindata);
return GETDNS_RETURN_MEMORY_ERROR;
2015-07-13 08:41:40 -05:00
}
2015-10-02 08:26:05 -05:00
if ((r = _getdns_dict_find_and_add(dict, name, &item))) {
_getdns_bindata_destroy(&dict->mf, newbindata);
return r;
}
item->dtype = t_bindata;
item->data.bindata = newbindata;
return GETDNS_RETURN_GOOD;
2015-07-13 08:41:40 -05:00
} /* getdns_dict_util_set_dict */
/*---------------------------------------- getdns_dict_set_int */
getdns_return_t
2015-09-30 09:05:19 -05:00
getdns_dict_set_int(getdns_dict *dict, const char *name, uint32_t child_uint32)
{
2015-10-02 08:26:05 -05:00
getdns_item *item;
getdns_return_t r;
if (!dict || !name)
2014-01-10 09:35:03 -06:00
return GETDNS_RETURN_INVALID_PARAMETER;
2015-10-02 08:26:05 -05:00
if ((r = _getdns_dict_find_and_add(dict, name, &item)))
return r;
item->dtype = t_int;
item->data.n = child_uint32;
return GETDNS_RETURN_GOOD;
} /* getdns_dict_set_int */
2013-07-15 17:43:30 -05:00
/*---------------------------------------- getdns_pp_dict */
/**
* private function to help with indenting.
* @param indent number of spaces to return
* @return a character string containing min(80, indent) spaces
*/
static const char *
getdns_indent(size_t indent)
{
static const char *spaces = " "
" ";
return spaces + 80 - (indent < 80 ? indent : 0);
} /* getdns_indent */
static int
_getdns_bindata_is_dname(getdns_bindata *bindata)
{
size_t i = 0, n_labels = 0;
while (i < bindata->size && bindata->data[i]) {
2014-02-20 16:35:27 -06:00
i += ((size_t)bindata->data[i]) + 1;
n_labels++;
}
2015-07-09 17:04:14 -05:00
if (i < bindata->size && !bindata->data[i]) {
n_labels++;
i++;
2015-07-09 17:04:14 -05:00
}
return i == bindata->size && n_labels > 1 &&
bindata->data[bindata->size - 1] == 0;
}
/*---------------------------------------- getdns_pp_bindata */
/**
2015-03-02 04:38:46 -06:00
* private function to pretty print bindata to a gldns_buffer
* @param buf buffer to write to
* @param indent number of spaces to append after newline
* @param bindata the bindata to print
* @return on success the number of written characters
* if an output error is encountered, a negative value
*/
static int
2015-03-02 04:38:46 -06:00
getdns_pp_bindata(gldns_buffer *buf, size_t indent,
2015-03-20 13:55:17 -05:00
getdns_bindata *bindata, int rdata_raw, int json)
{
2015-03-02 04:38:46 -06:00
size_t i, p = gldns_buffer_position(buf);
uint8_t *dptr;
2015-03-20 13:55:17 -05:00
char spc[1024];
2015-03-20 13:55:17 -05:00
if (!json && gldns_buffer_printf(buf, " <bindata ") < 0)
return -1;
/* Walk through all printable characters */
i = 0;
2015-02-17 10:06:36 -06:00
if (!rdata_raw)
while (i < bindata->size && isprint(bindata->data[i]))
i++;
if (bindata->size > 0 && i == bindata->size) { /* all printable? */
2015-02-17 10:06:36 -06:00
2015-03-20 13:55:17 -05:00
if (json)
(void)snprintf(spc, sizeof(spc), "\"%%.%ds\"", (int)i);
else
(void)snprintf(spc, sizeof(spc), "of \"%%.%ds\"%s>",
(int)(i > 32 ? 32 : i), (i > 32 ? "..." : ""));
if (gldns_buffer_printf(buf, spc, bindata->data) < 0)
return -1;
} else if (bindata->size > 1 && /* null terminated printable */
i == bindata->size - 1 && bindata->data[i] == 0) {
2015-03-20 13:55:17 -05:00
if (gldns_buffer_printf(
buf, (json ? "\"%s\"" : "of \"%s\">"), bindata->data) < 0)
return -1;
} else if (bindata->size == 1 && *bindata->data == 0) {
2015-03-20 13:55:17 -05:00
if (gldns_buffer_printf(buf, json ? "\".\"" : "for .>") < 0)
return -1;
} else if (_getdns_bindata_is_dname(bindata)) {
2015-03-20 13:55:17 -05:00
(void)gldns_wire2str_dname_buf(
bindata->data, bindata->size, spc, sizeof(spc));
if (gldns_buffer_printf(
buf, (json ? "\"%s\"" : "of \"%s\">"), spc) < 0)
return -1;
2015-03-20 13:55:17 -05:00
} else if (json) {
if (gldns_buffer_printf(buf, "[") < 0)
return -1;
for (dptr = bindata->data;
dptr < bindata->data + bindata->size; dptr++) {
if (dptr > bindata->data) {
if (gldns_buffer_printf(buf, ",") < 0)
return -1;
}
if (gldns_buffer_printf(buf, "%d", (int)*dptr) < 0)
return -1;
}
2015-03-20 13:55:17 -05:00
if (gldns_buffer_printf(buf, "]") < 0)
return -1;
} else {
2015-03-02 04:38:46 -06:00
if (gldns_buffer_printf(buf, "of 0x") < 0)
return -1;
for (dptr = bindata->data;
dptr < bindata->data + bindata->size; dptr++) {
if (dptr - bindata->data >= 16) {
2015-03-02 04:38:46 -06:00
if (gldns_buffer_printf(buf, "...") < 0)
return -1;
break;
}
2015-03-02 04:38:46 -06:00
if (gldns_buffer_printf(buf, "%.2x", *dptr) < 0)
return -1;
}
2015-03-02 04:38:46 -06:00
if (gldns_buffer_printf(buf, ">") < 0)
return -1;
}
2015-03-02 04:38:46 -06:00
return gldns_buffer_position(buf) - p;
} /* getdns_pp_bindata */
2013-10-29 18:29:30 -05:00
static int
2015-03-02 04:38:46 -06:00
getdns_pp_dict(gldns_buffer * buf, size_t indent,
2015-03-20 13:55:17 -05:00
const getdns_dict *dict, int json);
static const char *unknown_str_l[] = {" <unknown>", " null", "null"};
/*---------------------------------------- getdns_pp_list */
/**
2015-03-02 04:38:46 -06:00
* private function to pretty print list to a gldns_buffer
* @param buf buffer to write to
* @param indent number of spaces to append after newline
* @param list the to list print
* @param for_literals The list is a list of literals.
2014-11-10 08:57:17 -06:00
* Show the literal instead of the value.
* @return on success the number of written characters
* if an output error is encountered, a negative value
*/
static int
getdns_pp_list(gldns_buffer *buf, size_t indent, const getdns_list *list,
int for_literals, int json)
{
2015-03-02 04:38:46 -06:00
size_t i, length, p = gldns_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;
const char *strval;
if (list == NULL)
return 0;
2015-03-02 04:38:46 -06:00
if (gldns_buffer_printf(buf, "[") < 0)
return -1;
if (getdns_list_get_length(list, &length) != GETDNS_RETURN_GOOD)
return -1;
indent += 2;
for (i = 0; i < length; i++) {
2015-03-20 13:55:17 -05:00
if (i && gldns_buffer_printf(buf, ",") < 0)
return -1;
if (json < 2 && gldns_buffer_printf(buf, "\n%s",
getdns_indent(indent)) < 0)
return -1;
2015-03-20 13:55:17 -05:00
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))
return -1;
if (!json && for_literals &&
(strval =
_getdns_get_const_info(int_item)->name)) {
2015-03-02 04:38:46 -06:00
if (gldns_buffer_printf(buf, "%s", strval) < 0)
return -1;
} else if (0 >
2015-03-02 04:38:46 -06:00
gldns_buffer_printf(buf, "%d", (int)int_item))
return -1;
break;
case t_bindata:
if (getdns_list_get_bindata(list, i, &bindata_item) !=
2015-03-20 13:55:17 -05:00
GETDNS_RETURN_GOOD)
return -1;
2015-03-20 13:55:17 -05:00
if (getdns_pp_bindata(
buf, indent, bindata_item, 0, json) < 0)
return -1;
break;
case t_list:
if (getdns_list_get_list(list, i, &list_item) !=
2015-03-20 13:55:17 -05:00
GETDNS_RETURN_GOOD)
return -1;
2015-03-20 13:55:17 -05:00
if (getdns_pp_list(
buf, indent, list_item, 0, json) < 0)
return -1;
break;
case t_dict:
if (getdns_list_get_dict(list, i, &dict_item) !=
GETDNS_RETURN_GOOD)
return -1;
2015-03-20 13:55:17 -05:00
if (getdns_pp_dict(buf, indent, dict_item, json) < 0)
return -1;
break;
default:
2015-03-20 13:55:17 -05:00
if (gldns_buffer_printf(
buf, "%s", unknown_str_l[json]) < 0)
return -1;
}
}
indent -= 2;
2015-03-20 13:55:17 -05:00
if (json < 2 && i && gldns_buffer_printf(
buf, "\n%s", getdns_indent(indent)) < 0)
return -1;
if (gldns_buffer_printf(buf, "]") < 0)
return -1;
2015-03-02 04:38:46 -06:00
return gldns_buffer_position(buf) - p;
} /* getdns_pp_list */
static int
_getdns_print_class(gldns_buffer *buf, uint32_t klass)
{
switch (klass) {
case GETDNS_RRCLASS_IN:
2015-03-02 04:38:46 -06:00
(void) gldns_buffer_printf(buf, " GETDNS_RRCLASS_IN");
return 1;
case GETDNS_RRCLASS_CH:
2015-03-02 04:38:46 -06:00
(void) gldns_buffer_printf(buf, " GETDNS_RRCLASS_CH");
return 1;
case GETDNS_RRCLASS_HS:
2015-03-02 04:38:46 -06:00
(void) gldns_buffer_printf(buf, " GETDNS_RRCLASS_HS");
return 1;
case GETDNS_RRCLASS_NONE:
2015-03-02 04:38:46 -06:00
(void) gldns_buffer_printf(buf, " GETDNS_RRCLASS_NONE");
return 1;
case GETDNS_RRCLASS_ANY:
2015-03-02 04:38:46 -06:00
(void) gldns_buffer_printf(buf, " GETDNS_RRCLASS_ANY");
return 1;
}
return 0;
}
static int
_getdns_print_opcode(gldns_buffer *buf, uint32_t opcode)
{
switch (opcode) {
case GETDNS_OPCODE_QUERY:
2015-03-02 04:38:46 -06:00
(void) gldns_buffer_printf(buf, " GETDNS_OPCODE_QUERY");
return 1;
case GETDNS_OPCODE_IQUERY:
2015-03-02 04:38:46 -06:00
(void) gldns_buffer_printf(buf, " GETDNS_OPCODE_IQUERY");
return 1;
case GETDNS_OPCODE_STATUS:
2015-03-02 04:38:46 -06:00
(void) gldns_buffer_printf(buf, " GETDNS_OPCODE_STATUS");
return 1;
case GETDNS_OPCODE_NOTIFY:
2015-03-02 04:38:46 -06:00
(void) gldns_buffer_printf(buf, " GETDNS_OPCODE_NOTIFY");
return 1;
case GETDNS_OPCODE_UPDATE:
2015-03-02 04:38:46 -06:00
(void) gldns_buffer_printf(buf, " GETDNS_OPCODE_UPDATE");
return 1;
}
return 0;
}
static int
_getdns_print_rcode(gldns_buffer *buf, uint32_t rcode)
{
static const char *rcodes[] = {
" GETDNS_RCODE_NOERROR" , " GETDNS_RCODE_FORMERR" ,
" GETDNS_RCODE_SERVFAIL", " GETDNS_RCODE_NXDOMAIN",
" GETDNS_RCODE_NOTIMP" , " GETDNS_RCODE_REFUSED" ,
" GETDNS_RCODE_YXDOMAIN", " GETDNS_RCODE_YXRRSET" ,
" GETDNS_RCODE_NXRRSET" , " GETDNS_RCODE_NOTAUTH" ,
" GETDNS_RCODE_NOTZONE" ,
" GETDNS_RCODE_BADSIG" , " GETDNS_RCODE_BADKEY" ,
" GETDNS_RCODE_BADTIME" , " GETDNS_RCODE_BADMODE" ,
" GETDNS_RCODE_BADNAME" , " GETDNS_RCODE_BADALG" ,
" GETDNS_RCODE_BADTRUNC"
};
if (rcode <= 10)
2015-03-02 04:38:46 -06:00
(void) gldns_buffer_printf(buf, "%s", rcodes[rcode]);
else if (rcode >= 16 && rcode <= 22)
2015-03-02 04:38:46 -06:00
(void) gldns_buffer_printf(buf, "%s", rcodes[rcode-6]);
else
return 0;
return 1;
}
/*---------------------------------------- getdns_pp_dict */
/**
2015-03-02 04:38:46 -06:00
* private function to pretty print dict to a gldns_buffer
* @param buf buffer to write to
* @param indent number of spaces to append after newline
* @param dict the dict to print
* @return on success the number of written characters
* if an output error is encountered, a negative value
*/
static int
2015-03-02 04:38:46 -06:00
getdns_pp_dict(gldns_buffer * buf, size_t indent,
2015-03-20 13:55:17 -05:00
const getdns_dict *dict, int json)
{
2015-03-02 04:38:46 -06:00
size_t i, length, p = gldns_buffer_position(buf);
struct getdns_dict_item *item;
const char *strval;
char abuf[80];
if (dict == NULL)
return 0;
2015-03-02 04:38:46 -06:00
if (gldns_buffer_printf(buf, "{") < 0)
return -1;
i = 0;
indent += 2;
RBTREE_FOR(item, struct getdns_dict_item *,
(_getdns_rbtree_t *)&(dict->root)) {
2015-03-20 13:55:17 -05:00
if (i && gldns_buffer_printf(buf, ",") < 0)
return -1;
if (json < 2 && gldns_buffer_printf(
buf, "\n%s", getdns_indent(indent)) < 0)
return -1;
if (gldns_buffer_printf(
buf, "\"%s\":",(const char *)item->node.key) < 0)
return -1;
2015-09-30 09:05:19 -05:00
switch (item->i.dtype) {
case t_int:
2015-03-20 13:55:17 -05:00
if (!json &&
(strcmp(item->node.key, "type") == 0 ||
strcmp(item->node.key, "type_covered") == 0 ||
strcmp(item->node.key, "qtype") == 0) &&
2015-09-30 09:05:19 -05:00
(strval = _getdns_rr_type_name(item->i.data.n))) {
2015-03-02 04:38:46 -06:00
if (gldns_buffer_printf(
buf, " GETDNS_RRTYPE_%s", strval) < 0)
return -1;
break;
}
2015-03-20 13:55:17 -05:00
if (!json &&
(strcmp(item->node.key, "answer_type") == 0 ||
strcmp(item->node.key, "dnssec_status") == 0 ||
strcmp(item->node.key, "status") == 0 ||
strcmp(item->node.key, "append_name") == 0 ||
strcmp(item->node.key, "follow_redirects") == 0 ||
strcmp(item->node.key, "resolution_type") == 0) &&
(strval =
2015-09-30 09:05:19 -05:00
_getdns_get_const_info(item->i.data.n)->name)) {
2015-03-02 04:38:46 -06:00
if (gldns_buffer_printf(buf, " %s", strval) < 0)
return -1;
break;
}
2015-03-20 13:55:17 -05:00
if (!json &&
(strcmp(item->node.key, "class") == 0 ||
strcmp(item->node.key, "qclass") == 0) &&
2015-09-30 09:05:19 -05:00
_getdns_print_class(buf, item->i.data.n))
break;
2015-03-20 13:55:17 -05:00
if (!json && strcmp(item->node.key, "opcode") == 0 &&
2015-09-30 09:05:19 -05:00
_getdns_print_opcode(buf, item->i.data.n))
break;
2015-03-20 13:55:17 -05:00
if (!json && strcmp(item->node.key, "rcode") == 0 &&
2015-09-30 09:05:19 -05:00
_getdns_print_rcode(buf, item->i.data.n))
break;
2015-03-20 13:55:17 -05:00
if (gldns_buffer_printf(
2015-09-30 09:05:19 -05:00
buf,(json < 2 ? " %d" : "%d"), item->i.data.n) < 0)
return -1;
break;
case t_bindata:
if ((strcmp(item->node.key, "address_data") == 0 ||
strcmp(item->node.key, "ipv4_address") == 0 ||
strcmp(item->node.key, "ipv6_address") == 0 ) &&
2015-09-30 09:05:19 -05:00
(item->i.data.bindata->size == 4 ||
item->i.data.bindata->size == 16 )) {
2015-03-20 13:55:17 -05:00
if (gldns_buffer_printf(buf,
(json ? "\"%s\"" : " <bindata for %s>"),
2015-09-30 09:05:19 -05:00
inet_ntop(( item->i.data.bindata->size == 4
? AF_INET : AF_INET6)
2015-09-30 09:05:19 -05:00
, item->i.data.bindata->data
2015-03-20 13:55:17 -05:00
, abuf
, 40
)) < 0)
return -1;
2015-03-20 13:55:17 -05:00
} else if (getdns_pp_bindata(
2015-09-30 09:05:19 -05:00
buf, indent, item->i.data.bindata,
2015-03-20 13:55:17 -05:00
(strcmp(item->node.key, "rdata_raw") == 0),
json) < 0)
return -1;
break;
case t_list: /* Don't put empty lists on a new line */
2015-09-30 09:05:19 -05:00
if (getdns_list_get_length(item->i.data.list, &length) !=
2015-03-20 13:55:17 -05:00
GETDNS_RETURN_GOOD)
return -1;
if (length == 0) {
2015-03-20 13:55:17 -05:00
if (gldns_buffer_printf(
buf, (json < 2 ? " []" : "[]")) < 0)
return -1;
break;
}
2015-03-20 13:55:17 -05:00
if (json < 2 && gldns_buffer_printf(buf, "\n%s",
getdns_indent(indent)) < 0)
return -1;
2015-09-30 09:05:19 -05:00
if (getdns_pp_list(buf, indent, item->i.data.list,
(strcmp(item->node.key, "namespaces") == 0 ||
strcmp(item->node.key, "dns_transport_list") == 0),
2015-03-20 13:55:17 -05:00
json) < 0)
return -1;
break;
case t_dict:
2015-03-20 13:55:17 -05:00
if (json < 2 && gldns_buffer_printf(buf, "\n%s",
getdns_indent(indent)) < 0)
return -1;
2015-03-20 13:55:17 -05:00
if (getdns_pp_dict(
2015-09-30 09:05:19 -05:00
buf, indent, item->i.data.dict, json) < 0)
return -1;
break;
default:
2015-03-20 13:55:17 -05:00
if (gldns_buffer_printf(
buf, "%s", unknown_str_l[json]) < 0)
return -1;
}
i++;
}
indent -= 2;
2015-03-20 13:55:17 -05:00
if (json < 2 && i && gldns_buffer_printf(
buf, "\n%s", getdns_indent(indent)) < 0)
return -1;
if (gldns_buffer_printf(buf, "}") < 0)
return -1;
2015-03-02 04:38:46 -06:00
return gldns_buffer_position(buf) - p;
} /* getdns_pp_dict */
/*---------------------------------------- getdns_pretty_print_dict */
/**
* Return a character string containing a "human readable" representation
* of dict.
* @param dict the dict to pretty print
* @return the "human readable" representation of dict
* or NULL on error
*/
char *
getdns_pretty_print_dict(const struct getdns_dict *dict)
{
2015-03-02 04:38:46 -06:00
gldns_buffer *buf;
char *ret;
if (!dict)
return NULL;
buf = gldns_buffer_new(8192);
if (!buf)
return NULL;
2015-03-20 13:55:17 -05:00
if (getdns_pp_dict(buf, 0, dict, 0) < 0) {
2015-03-02 04:38:46 -06:00
gldns_buffer_free(buf);
return NULL;
}
2015-03-02 04:38:46 -06:00
ret = (char *) gldns_buffer_export(buf);
gldns_buffer_free(buf);
return ret;
} /* getdns_pretty_print_dict */
2013-07-15 17:43:30 -05:00
2015-03-20 12:19:06 -05:00
int
getdns_pretty_snprint_dict(char *str, size_t size, const getdns_dict *dict)
{
gldns_buffer buf;
if (!dict) return -1;
gldns_buffer_init_frm_data(&buf, str, size);
2015-03-20 13:55:17 -05:00
return getdns_pp_dict(&buf, 0, dict, 0) < 0
2015-03-20 12:19:06 -05:00
? -1 : gldns_buffer_position(&buf);
}
char *
getdns_pretty_print_list(const getdns_list *list)
{
gldns_buffer *buf;
char *ret;
if (!list)
return NULL;
buf = gldns_buffer_new(4096);
if (!buf)
return NULL;
2015-03-20 13:55:17 -05:00
if (getdns_pp_list(buf, 0, list, 0, 0) < 0) {
gldns_buffer_free(buf);
return NULL;
}
ret = (char *) gldns_buffer_export(buf);
gldns_buffer_free(buf);
return ret;
}
2015-03-20 12:19:06 -05:00
int
getdns_pretty_snprint_list(char *str, size_t size, const getdns_list *list)
{
gldns_buffer buf;
if (!list) return -1;
gldns_buffer_init_frm_data(&buf, str, size);
2015-03-20 13:55:17 -05:00
return getdns_pp_list(&buf, 0, list, 0, 0) < 0
? -1 : gldns_buffer_position(&buf);
}
char *
getdns_print_json_dict(const getdns_dict *dict, int pretty)
{
gldns_buffer *buf;
char *ret;
if (!dict)
return NULL;
buf = gldns_buffer_new(8192);
if (!buf)
return NULL;
if (getdns_pp_dict(buf, 0, dict, pretty ? 1 : 2) < 0) {
gldns_buffer_free(buf);
return NULL;
}
ret = (char *) gldns_buffer_export(buf);
gldns_buffer_free(buf);
return ret;
} /* getdns_print_json_dict */
int
getdns_snprint_json_dict(
char *str, size_t size, const getdns_dict *dict, int pretty)
{
gldns_buffer buf;
if (!dict) return -1;
gldns_buffer_init_frm_data(&buf, str, size);
return getdns_pp_dict(&buf, 0, dict, pretty ? 1 : 2) < 0
2015-03-20 12:19:06 -05:00
? -1 : gldns_buffer_position(&buf);
}
2015-03-20 13:55:17 -05:00
char *
getdns_print_json_list(const getdns_list *list, int pretty)
{
gldns_buffer *buf;
char *ret;
if (!list)
return NULL;
buf = gldns_buffer_new(4096);
if (!buf)
return NULL;
if (getdns_pp_list(buf, 0, list, 0, pretty ? 1 : 2) < 0) {
gldns_buffer_free(buf);
return NULL;
}
ret = (char *) gldns_buffer_export(buf);
gldns_buffer_free(buf);
return ret;
}
int
getdns_snprint_json_list(
char *str, size_t size, const getdns_list *list, int pretty)
{
gldns_buffer buf;
if (!list) return -1;
gldns_buffer_init_frm_data(&buf, str, size);
return getdns_pp_list(&buf, 0, list, 0, pretty ? 1 : 2) < 0
? -1 : gldns_buffer_position(&buf);
}
2015-03-20 12:19:06 -05:00
getdns_return_t
2015-09-30 09:05:19 -05:00
getdns_dict_remove_name(getdns_dict *dict, const char *name)
{
const char *next;
struct getdns_dict_item *d;
if (!dict || !name)
return GETDNS_RETURN_INVALID_PARAMETER;
if (!(d = _find_dict_item(dict, name)))
return GETDNS_RETURN_NO_SUCH_DICT_NAME;
if (*name != '/' || !(next = strchr(name + 1, '/'))) {
d = _delete_dict_item(dict, name);
getdns_dict_item_free(&d->node, dict);
return GETDNS_RETURN_GOOD;
} else switch (d->i.dtype) {
case t_dict: return getdns_dict_remove_name(d->i.data.dict, next);
case t_list: return _getdns_list_remove_name(d->i.data.list, next);
default : /* Trying to dereference a non list or dict */
return GETDNS_RETURN_WRONG_TYPE_REQUESTED;
}
}
/* dict.c */