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
|
2015-08-19 09:07:01 -05:00
|
|
|
* accomplished via an _getdns_rbtree_t
|
2014-01-10 09:09:07 -06:00
|
|
|
*
|
2013-07-30 16:36:14 -05:00
|
|
|
* Interfaces originally taken from the getdns API description pseudo implementation.
|
2013-07-15 17:43:30 -05:00
|
|
|
*
|
|
|
|
*/
|
2013-11-04 17:37:54 -06:00
|
|
|
|
|
|
|
/*
|
2014-02-25 07:12:33 -06:00
|
|
|
* Copyright (c) 2013, NLnet Labs, Verisign, Inc.
|
2013-11-04 17:37:54 -06:00
|
|
|
* All rights reserved.
|
2014-01-10 09:09:07 -06:00
|
|
|
*
|
2013-11-04 17:37:54 -06:00
|
|
|
* 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.
|
2014-02-25 07:23:19 -06:00
|
|
|
* * Neither the names of the copyright holders nor the
|
2013-11-04 17:37:54 -06:00
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
2013-10-30 17:23:53 -05:00
|
|
|
#include <ctype.h>
|
2015-11-22 21:38:13 -06:00
|
|
|
#include "config.h"
|
|
|
|
#ifndef USE_WINSOCK
|
2014-05-22 11:31:30 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2015-11-22 21:38:13 -06:00
|
|
|
#endif
|
2014-05-22 11:31:30 -05:00
|
|
|
|
2013-11-12 10:00:19 -06:00
|
|
|
#include "types-internal.h"
|
2013-12-03 09:09:26 -06:00
|
|
|
#include "util-internal.h"
|
2013-10-29 15:02:21 -05:00
|
|
|
#include "dict.h"
|
2015-10-01 08:43:17 -05:00
|
|
|
#include "list.h"
|
2014-01-30 17:16:34 -06:00
|
|
|
#include "rr-dict.h"
|
2014-02-12 16:10:29 -06:00
|
|
|
#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
|
|
|
|
2015-10-05 10:18:32 -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;
|
2015-10-05 10:18:32 -05:00
|
|
|
char *j;
|
2015-09-30 09:05:19 -05:00
|
|
|
|
2015-10-05 10:18:32 -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
|
|
|
|
2016-04-29 05:00:17 -05:00
|
|
|
if (next_ref - jptr + 1 > first_sz || !first)
|
2015-10-05 10:18:32 -05:00
|
|
|
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';
|
2015-10-05 10:18:32 -05:00
|
|
|
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';
|
|
|
|
|
2015-10-05 10:18:32 -05:00
|
|
|
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(
|
2015-10-05 10:18:32 -05:00
|
|
|
(_getdns_rbtree_t *)&(dict->root), first);
|
2015-09-30 09:05:19 -05:00
|
|
|
|
2015-10-05 10:18:32 -05:00
|
|
|
if (first && first != jptr && first != first_spc)
|
|
|
|
GETDNS_FREE(dict->mf, first);
|
2015-09-30 09:05:19 -05:00
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2015-10-05 10:18:32 -05:00
|
|
|
|
|
|
|
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;
|
2015-10-05 10:18:32 -05:00
|
|
|
|
|
|
|
if ((first = GETDNS_XMALLOC(*mf, char, sz)))
|
|
|
|
memcpy(first, jptr, sz);
|
|
|
|
}
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-01 08:43:17 -05:00
|
|
|
getdns_return_t
|
|
|
|
_getdns_dict_find(const getdns_dict *dict, const char *key, getdns_item **item)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2015-10-01 08:43:17 -05:00
|
|
|
const char *next;
|
|
|
|
struct getdns_dict_item *d;
|
|
|
|
|
|
|
|
if (!(d = _find_dict_item(dict, key)))
|
|
|
|
return GETDNS_RETURN_NO_SUCH_DICT_NAME;
|
|
|
|
|
2015-10-05 10:18:32 -05:00
|
|
|
if (*key != '/' || !(next = strchr(key + 1, '/'))) {
|
2015-10-01 08:43:17 -05:00
|
|
|
*item = &d->i;
|
2015-10-05 10:18:32 -05:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2015-10-01 08:43:17 -05:00
|
|
|
|
2015-10-05 10:18:32 -05:00
|
|
|
} else switch (d->i.dtype) { /* Key was nested reference */
|
2015-10-01 08:43:17 -05:00
|
|
|
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);
|
2015-10-05 10:18:32 -05:00
|
|
|
default : /* Trying to dereference a non list or dict */
|
|
|
|
return GETDNS_RETURN_WRONG_TYPE_REQUESTED;
|
2015-10-01 08:43:17 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2015-10-08 05:54:30 -05:00
|
|
|
|
|
|
|
/*---------------------------------------- getdns_dict_item_free */
|
|
|
|
/**
|
|
|
|
* private function used to release storage associated with a dictionary item
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
getdns_dict_item_free(_getdns_rbnode_t *node, void *arg)
|
|
|
|
{
|
|
|
|
struct getdns_dict_item *d = (struct getdns_dict_item *)node;
|
|
|
|
struct getdns_dict *dict = (struct getdns_dict *)arg;
|
|
|
|
|
|
|
|
assert(node);
|
|
|
|
assert(arg);
|
|
|
|
|
|
|
|
switch (d->i.dtype) {
|
|
|
|
case t_dict : getdns_dict_destroy(d->i.data.dict); break;
|
|
|
|
case t_list : getdns_list_destroy(d->i.data.list); break;
|
|
|
|
case t_bindata: _getdns_bindata_destroy(&dict->mf, d->i.data.bindata);
|
|
|
|
default : break;
|
|
|
|
}
|
|
|
|
if (node->key)
|
|
|
|
GETDNS_FREE(dict->mf, (void *)node->key);
|
|
|
|
GETDNS_FREE(dict->mf, node);
|
|
|
|
} /* getdns_dict_item_free */
|
|
|
|
|
|
|
|
|
|
|
|
getdns_return_t
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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)
|
2014-01-14 10:25:23 -06:00
|
|
|
{
|
2015-10-02 08:26:05 -05:00
|
|
|
const char *next;
|
2015-09-30 09:05:19 -05:00
|
|
|
struct getdns_dict_item *d;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
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);
|
2015-10-05 10:18:32 -05:00
|
|
|
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));
|
2015-10-08 05:54:30 -05:00
|
|
|
d->i.dtype = t_int;
|
|
|
|
d->i.data.n = 55555333;
|
2015-10-02 08:26:05 -05:00
|
|
|
*item = &d->i;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
if ((next[1] == '0' || next[1] == '-') &&
|
|
|
|
(next[2] == '/' || next[2] == '\0')) {
|
|
|
|
|
2015-10-08 05:54:30 -05:00
|
|
|
d->i.dtype = t_list;
|
2015-10-02 08:26:05 -05:00
|
|
|
d->i.data.list =_getdns_list_create_with_mf(&dict->mf);
|
|
|
|
return _getdns_list_find_and_add(
|
|
|
|
d->i.data.list, next, item);
|
|
|
|
}
|
2015-10-08 05:54:30 -05:00
|
|
|
d->i.dtype = t_dict;
|
2015-10-02 08:26:05 -05:00
|
|
|
d->i.data.dict = _getdns_dict_create_with_mf(&dict->mf);
|
2015-10-08 05:54:30 -05:00
|
|
|
return _getdns_dict_find_and_add(d->i.data.dict, next, item);
|
2015-10-02 08:26:05 -05:00
|
|
|
}
|
|
|
|
if (*key != '/' || !(next = strchr(key + 1, '/'))) {
|
2015-10-08 05:54:30 -05:00
|
|
|
switch (d->i.dtype) {
|
|
|
|
case t_dict : getdns_dict_destroy(d->i.data.dict); break;
|
|
|
|
case t_list : getdns_list_destroy(d->i.data.list); break;
|
|
|
|
case t_bindata: _getdns_bindata_destroy(
|
|
|
|
&dict->mf, d->i.data.bindata); break;
|
|
|
|
default : break;
|
|
|
|
}
|
|
|
|
d->i.dtype = t_int;
|
|
|
|
d->i.data.n = 33355555;
|
2015-10-02 08:26:05 -05:00
|
|
|
*item = &d->i;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
|
2015-10-05 10:18:32 -05:00
|
|
|
} else switch (d->i.dtype) { /* Key was nested reference */
|
2015-10-08 05:54:30 -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;
|
2013-11-05 14:03:44 -06:00
|
|
|
}
|
2014-01-14 10:25:23 -06:00
|
|
|
} /* getdns_dict_find_and_add */
|
|
|
|
|
2013-07-31 15:21:42 -05:00
|
|
|
|
|
|
|
/*---------------------------------------- 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)
|
2013-07-31 15:21:42 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
struct getdns_dict_item *item;
|
|
|
|
|
2013-11-11 16:10:22 -06:00
|
|
|
if (!dict || !answer)
|
2014-01-15 15:25:46 -06:00
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2013-12-08 17:52:38 -06:00
|
|
|
*answer = getdns_list_create_with_extended_memory_functions(
|
2014-03-07 09:42:37 -06:00
|
|
|
dict->mf.mf_arg, dict->mf.mf.ext.malloc,
|
|
|
|
dict->mf.mf.ext.realloc, dict->mf.mf.ext.free);
|
2013-11-11 16:10:22 -06:00
|
|
|
if (!*answer)
|
|
|
|
return GETDNS_RETURN_NO_SUCH_DICT_NAME;
|
|
|
|
|
2014-10-23 16:00:30 -05:00
|
|
|
RBTREE_FOR(item, struct getdns_dict_item *,
|
2015-08-19 09:07:01 -05:00
|
|
|
(_getdns_rbtree_t *)&(dict->root)) {
|
2016-05-22 14:58:50 -05:00
|
|
|
_getdns_list_append_string(*answer, item->node.key);
|
2013-11-05 14:03:44 -06:00
|
|
|
}
|
2013-11-11 16:10:22 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-11-05 14:03:44 -06:00
|
|
|
} /* getdns_dict_get_names */
|
2013-07-15 17:43:30 -05:00
|
|
|
|
2013-07-30 16:36:14 -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)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2015-10-01 08:43:17 -05:00
|
|
|
getdns_return_t r;
|
2015-09-30 09:05:19 -05:00
|
|
|
getdns_item *item;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2013-11-11 16:10:22 -06:00
|
|
|
if (!dict || !name || !answer)
|
2014-01-10 09:45:45 -06:00
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2015-10-01 08:43:17 -05:00
|
|
|
if ((r = _getdns_dict_find(dict, name, &item)))
|
|
|
|
return r;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2013-11-11 16:10:22 -06:00
|
|
|
*answer = item->dtype;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-11-05 14:03:44 -06:00
|
|
|
} /* getdns_dict_get_data_type */
|
2013-07-30 16:36:14 -05:00
|
|
|
|
|
|
|
/*---------------------------------------- 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)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2015-10-01 08:43:17 -05:00
|
|
|
getdns_return_t r;
|
2015-09-30 09:05:19 -05:00
|
|
|
getdns_item *item;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2013-11-11 16:10:22 -06:00
|
|
|
if (!dict || !name || !answer)
|
2014-01-15 15:25:46 -06:00
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2015-10-01 08:43:17 -05:00
|
|
|
if ((r = _getdns_dict_find(dict, name, &item)))
|
|
|
|
return r;
|
2014-01-10 09:45:45 -06:00
|
|
|
|
2014-03-07 09:42:37 -06:00
|
|
|
if (item->dtype != t_dict)
|
2014-01-10 09:45:45 -06:00
|
|
|
return GETDNS_RETURN_WRONG_TYPE_REQUESTED;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
|
|
|
*answer = item->data.dict;
|
2015-10-01 08:43:17 -05:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-11-05 14:03:44 -06:00
|
|
|
} /* getdns_dict_get_dict */
|
2013-07-30 16:36:14 -05:00
|
|
|
|
|
|
|
/*---------------------------------------- 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)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2015-10-01 08:43:17 -05:00
|
|
|
getdns_return_t r;
|
2015-09-30 09:05:19 -05:00
|
|
|
getdns_item *item;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2013-11-11 16:10:22 -06:00
|
|
|
if (!dict || !name || !answer)
|
2014-01-15 15:25:46 -06:00
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2015-10-01 08:43:17 -05:00
|
|
|
if ((r = _getdns_dict_find(dict, name, &item)))
|
|
|
|
return r;
|
2014-01-10 09:45:45 -06:00
|
|
|
|
2014-03-07 09:42:37 -06:00
|
|
|
if (item->dtype != t_list)
|
2013-11-11 16:10:22 -06:00
|
|
|
return GETDNS_RETURN_WRONG_TYPE_REQUESTED;
|
|
|
|
|
|
|
|
*answer = item->data.list;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-11-05 14:03:44 -06:00
|
|
|
} /* getdns_dict_get_list */
|
2013-07-30 16:36:14 -05:00
|
|
|
|
|
|
|
/*---------------------------------------- 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)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2015-10-01 08:43:17 -05:00
|
|
|
getdns_return_t r;
|
2015-09-30 09:05:19 -05:00
|
|
|
getdns_item *item;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2013-11-11 16:10:22 -06:00
|
|
|
if (!dict || !name || !answer)
|
2014-01-15 15:25:46 -06:00
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2015-10-01 08:43:17 -05:00
|
|
|
if ((r = _getdns_dict_find(dict, name, &item)))
|
2014-03-07 09:42:37 -06:00
|
|
|
return GETDNS_RETURN_NO_SUCH_DICT_NAME;
|
2014-01-10 09:45:45 -06:00
|
|
|
|
2014-03-07 09:42:37 -06:00
|
|
|
if (item->dtype != t_bindata)
|
2013-11-11 16:10:22 -06:00
|
|
|
return GETDNS_RETURN_WRONG_TYPE_REQUESTED;
|
|
|
|
|
|
|
|
*answer = item->data.bindata;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-11-05 14:03:44 -06:00
|
|
|
} /* getdns_dict_get_bindata */
|
2013-07-15 17:43:30 -05:00
|
|
|
|
2013-07-30 16:36:14 -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)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2015-10-01 08:43:17 -05:00
|
|
|
getdns_return_t r;
|
2015-09-30 09:05:19 -05:00
|
|
|
getdns_item *item;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2013-11-11 16:10:22 -06:00
|
|
|
if (!dict || !name || !answer)
|
2014-01-15 15:25:46 -06:00
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2015-10-01 08:43:17 -05:00
|
|
|
if ((r = _getdns_dict_find(dict, name, &item)))
|
|
|
|
return r;
|
2014-01-10 09:45:45 -06:00
|
|
|
|
2014-03-07 09:42:37 -06:00
|
|
|
if (item->dtype != t_int)
|
2013-11-11 16:10:22 -06:00
|
|
|
return GETDNS_RETURN_WRONG_TYPE_REQUESTED;
|
|
|
|
|
|
|
|
*answer = item->data.n;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-11-05 14:03:44 -06:00
|
|
|
} /* getdns_dict_get_int */
|
2013-07-15 17:43:30 -05:00
|
|
|
|
2013-07-30 16:36:14 -05:00
|
|
|
struct getdns_dict *
|
2013-12-08 17:52:38 -06:00
|
|
|
getdns_dict_create_with_extended_memory_functions(
|
2014-03-07 09:42:37 -06:00
|
|
|
void *userarg,
|
|
|
|
void *(*malloc)(void *userarg, size_t),
|
|
|
|
void *(*realloc)(void *userarg, void *, size_t),
|
|
|
|
void (*free)(void *userarg, void *))
|
2013-07-30 16:36:14 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
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
|
2014-03-07 09:42:37 -06:00
|
|
|
? (struct getdns_dict*)(*mf.pln.malloc)(
|
|
|
|
sizeof(struct getdns_dict))
|
|
|
|
: (struct getdns_dict*)(*mf.ext.malloc)(userarg,
|
|
|
|
sizeof(struct getdns_dict));
|
2013-11-11 16:10:22 -06:00
|
|
|
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;
|
2013-12-08 15:56:34 -06:00
|
|
|
|
2015-08-19 09:07:01 -05:00
|
|
|
_getdns_rbtree_init(&(dict->root),
|
2014-03-07 09:42:37 -06:00
|
|
|
(int (*)(const void *, const void *)) strcmp);
|
2013-11-05 14:03:44 -06:00
|
|
|
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),
|
2014-03-07 09:42:37 -06:00
|
|
|
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(
|
2014-03-07 09:42:37 -06:00
|
|
|
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(
|
2014-03-07 09:42:37 -06:00
|
|
|
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,
|
2014-03-07 09:42:37 -06:00
|
|
|
&realloc, &free);
|
2013-11-11 16:10:22 -06:00
|
|
|
} /* getdns_dict_create_with_context */
|
|
|
|
|
|
|
|
/*---------------------------------------- getdns_dict_create */
|
|
|
|
struct getdns_dict *
|
|
|
|
getdns_dict_create()
|
|
|
|
{
|
|
|
|
return getdns_dict_create_with_context(NULL);
|
|
|
|
} /* getdns_dict_create */
|
2013-07-30 16:36:14 -05:00
|
|
|
|
2015-08-19 09:30:15 -05:00
|
|
|
/*---------------------------------------- _getdns_dict_copy */
|
2013-07-30 16:36:14 -05:00
|
|
|
/**
|
2013-10-30 11:05:49 -05:00
|
|
|
* private function used to make a copy of a dict structure,
|
|
|
|
* the caller is responsible * for freeing storage allocated to returned value
|
2013-07-30 16:36:14 -05:00
|
|
|
* @param srcdict the dictionary structure to copy
|
2013-10-10 15:35:29 -05:00
|
|
|
* @param dstdict the copy destination
|
2013-07-30 16:36:14 -05:00
|
|
|
* @return the address of the copy of the dictionary structure on success
|
|
|
|
* @return NULL on error (out of memory, invalid srcdict)
|
|
|
|
*/
|
2013-07-31 15:21:42 -05:00
|
|
|
getdns_return_t
|
2015-08-19 09:30:15 -05:00
|
|
|
_getdns_dict_copy(const struct getdns_dict * srcdict,
|
2014-03-07 09:42:37 -06:00
|
|
|
struct getdns_dict ** dstdict)
|
2013-07-30 16:36:14 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
struct getdns_dict_item *item;
|
|
|
|
char *key;
|
2013-11-11 16:10:22 -06:00
|
|
|
getdns_return_t retval;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2013-11-11 16:10:22 -06:00
|
|
|
if (!dstdict)
|
2014-01-15 15:25:46 -06:00
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
|
|
|
if (!srcdict) {
|
2013-11-08 17:13:49 -06:00
|
|
|
*dstdict = NULL;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
2013-12-08 17:52:38 -06:00
|
|
|
*dstdict = getdns_dict_create_with_extended_memory_functions(
|
2014-03-07 09:42:37 -06:00
|
|
|
srcdict->mf.mf_arg,
|
|
|
|
srcdict->mf.mf.ext.malloc,
|
|
|
|
srcdict->mf.mf.ext.realloc,
|
|
|
|
srcdict->mf.mf.ext.free);
|
2013-11-11 16:10:22 -06:00
|
|
|
if (!*dstdict)
|
2014-01-15 15:25:46 -06:00
|
|
|
return GETDNS_RETURN_GENERIC_ERROR;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2014-10-23 16:00:30 -05:00
|
|
|
RBTREE_FOR(item, struct getdns_dict_item *,
|
2015-08-19 09:07:01 -05:00
|
|
|
(struct _getdns_rbtree_t *)&(srcdict->root)) {
|
2013-11-08 17:13:49 -06:00
|
|
|
key = (char *) item->node.key;
|
2015-09-30 09:05:19 -05:00
|
|
|
switch (item->i.dtype) {
|
2013-11-08 17:13:49 -06:00
|
|
|
case t_bindata:
|
2013-11-11 16:10:22 -06:00
|
|
|
retval = getdns_dict_set_bindata(*dstdict, key,
|
2015-09-30 09:05:19 -05:00
|
|
|
item->i.data.bindata);
|
2013-11-08 17:13:49 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case t_dict:
|
2013-11-11 16:10:22 -06:00
|
|
|
retval = getdns_dict_set_dict(*dstdict, key,
|
2015-09-30 09:05:19 -05:00
|
|
|
item->i.data.dict);
|
2013-11-08 17:13:49 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case t_int:
|
2013-11-11 16:10:22 -06:00
|
|
|
retval = getdns_dict_set_int(*dstdict, key,
|
2015-09-30 09:05:19 -05:00
|
|
|
item->i.data.n);
|
2013-11-08 17:13:49 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case t_list:
|
2013-11-11 16:10:22 -06:00
|
|
|
retval = getdns_dict_set_list(*dstdict, key,
|
2015-09-30 09:05:19 -05:00
|
|
|
item->i.data.list);
|
2013-11-08 17:13:49 -06:00
|
|
|
break;
|
2016-04-29 05:00:17 -05:00
|
|
|
default:
|
|
|
|
retval = GETDNS_RETURN_WRONG_TYPE_REQUESTED;
|
|
|
|
break;
|
2013-11-05 14:03:44 -06:00
|
|
|
}
|
2013-11-11 16:10:22 -06:00
|
|
|
if (retval != GETDNS_RETURN_GOOD) {
|
|
|
|
getdns_dict_destroy(*dstdict);;
|
|
|
|
*dstdict = NULL;
|
|
|
|
return retval;
|
|
|
|
}
|
2013-11-05 14:03:44 -06:00
|
|
|
}
|
2013-11-08 17:13:49 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2015-08-19 09:30:15 -05:00
|
|
|
} /* _getdns_dict_copy */
|
2013-07-30 16:36:14 -05:00
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------- getdns_dict_destroy */
|
2014-03-11 10:43:41 -05:00
|
|
|
void
|
2013-07-30 16:36:14 -05:00
|
|
|
getdns_dict_destroy(struct getdns_dict *dict)
|
|
|
|
{
|
2014-10-23 16:00:30 -05:00
|
|
|
if (!dict) return;
|
2015-08-19 09:07:01 -05:00
|
|
|
_getdns_traverse_postorder(&(dict->root), getdns_dict_item_free, dict);
|
2013-12-08 17:05:18 -06:00
|
|
|
GETDNS_FREE(dict->mf, dict);
|
2013-11-05 14:03:44 -06:00
|
|
|
} /* getdns_dict_destroy */
|
2013-07-30 16:36:14 -05:00
|
|
|
|
|
|
|
/*---------------------------------------- getdns_dict_set_dict */
|
|
|
|
getdns_return_t
|
2016-03-21 05:54:55 -05:00
|
|
|
_getdns_dict_set_this_dict(
|
|
|
|
getdns_dict *dict, const char *name, getdns_dict *child_dict)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2015-09-30 09:05:19 -05:00
|
|
|
getdns_item *item;
|
2015-10-02 08:26:05 -05:00
|
|
|
getdns_return_t r;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2014-01-10 09:09:07 -06:00
|
|
|
if (!dict || !name || !child_dict)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2016-03-21 05:54:55 -05:00
|
|
|
if ((r = _getdns_dict_find_and_add(dict, name, &item)))
|
2015-10-02 08:26:05 -05:00
|
|
|
return r;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
|
|
|
item->dtype = t_dict;
|
2016-03-21 05:54:55 -05:00
|
|
|
item->data.dict = child_dict;
|
2013-11-11 16:10:22 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-11-05 14:03:44 -06:00
|
|
|
} /* getdns_dict_set_dict */
|
2013-07-31 15:21:42 -05:00
|
|
|
|
2016-03-21 05:54:55 -05:00
|
|
|
getdns_return_t
|
|
|
|
getdns_dict_set_dict(
|
|
|
|
getdns_dict *dict, const char *name, const getdns_dict *child_dict)
|
|
|
|
{
|
|
|
|
getdns_dict *newdict;
|
|
|
|
getdns_return_t r;
|
|
|
|
|
|
|
|
if ((r = _getdns_dict_copy(child_dict, &newdict)))
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if ((r = _getdns_dict_set_this_dict(dict, name, newdict)))
|
|
|
|
getdns_dict_destroy(newdict);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-31 15:21:42 -05:00
|
|
|
/*---------------------------------------- getdns_dict_set_list */
|
|
|
|
getdns_return_t
|
2016-03-21 05:54:55 -05:00
|
|
|
_getdns_dict_set_this_list(
|
|
|
|
getdns_dict *dict, const char *name, getdns_list *child_list)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2015-09-30 09:05:19 -05:00
|
|
|
getdns_item *item;
|
2015-10-02 08:26:05 -05:00
|
|
|
getdns_return_t r;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2014-01-10 09:35:03 -06:00
|
|
|
if (!dict || !name || !child_list)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2016-03-21 05:54:55 -05:00
|
|
|
if ((r = _getdns_dict_find_and_add(dict, name, &item)))
|
2015-10-02 08:26:05 -05:00
|
|
|
return r;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
|
|
|
item->dtype = t_list;
|
2016-03-21 05:54:55 -05:00
|
|
|
item->data.list = child_list;
|
2013-11-11 16:10:22 -06:00
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-11-05 14:03:44 -06:00
|
|
|
} /* getdns_dict_set_list */
|
2013-07-15 17:43:30 -05:00
|
|
|
|
2016-03-21 05:54:55 -05:00
|
|
|
getdns_return_t
|
|
|
|
getdns_dict_set_list(
|
|
|
|
getdns_dict *dict, const char *name, const getdns_list *child_list)
|
|
|
|
{
|
|
|
|
getdns_list *newlist;
|
|
|
|
getdns_return_t r;
|
|
|
|
|
|
|
|
if ((r = _getdns_list_copy(child_list, &newlist)))
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if ((r = _getdns_dict_set_this_list(dict, name, newlist)))
|
|
|
|
getdns_list_destroy(newlist);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-31 15:21:42 -05:00
|
|
|
/*---------------------------------------- getdns_dict_set_bindata */
|
2016-04-18 08:36:39 -05:00
|
|
|
getdns_return_t
|
|
|
|
_getdns_dict_set_this_bindata(
|
|
|
|
getdns_dict *dict, const char *name, getdns_bindata *bindata)
|
|
|
|
{
|
|
|
|
getdns_item *item;
|
|
|
|
getdns_return_t r;
|
|
|
|
|
|
|
|
if (!dict || !name || !bindata)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if ((r = _getdns_dict_find_and_add(dict, name, &item)))
|
|
|
|
return r;
|
|
|
|
|
|
|
|
item->dtype = t_bindata;
|
|
|
|
item->data.bindata = bindata;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
|
|
|
}
|
|
|
|
|
2013-07-31 15:21:42 -05:00
|
|
|
getdns_return_t
|
2015-12-07 09:43:41 -06:00
|
|
|
_getdns_dict_set_const_bindata(
|
2015-12-29 08:57:55 -06:00
|
|
|
getdns_dict *dict, const char *name, size_t size, const void *data)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2016-03-21 07:07:20 -05:00
|
|
|
getdns_item *item;
|
2015-09-30 09:05:19 -05:00
|
|
|
getdns_bindata *newbindata;
|
2015-10-02 08:26:05 -05:00
|
|
|
getdns_return_t r;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2016-03-21 07:07:20 -05:00
|
|
|
if (!dict || !name)
|
2014-01-10 09:35:03 -06:00
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2015-12-07 09:43:41 -06:00
|
|
|
if (!(newbindata = _getdns_bindata_copy(&dict->mf, size, data)))
|
2015-10-02 08:26:05 -05:00
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2016-03-21 07:07:20 -05:00
|
|
|
if ((r = _getdns_dict_find_and_add(dict, name, &item))) {
|
2015-08-19 09:30:15 -05:00
|
|
|
_getdns_bindata_destroy(&dict->mf, newbindata);
|
2016-03-21 07:07:20 -05:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
item->dtype = t_bindata;
|
|
|
|
item->data.bindata = newbindata;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2016-03-21 05:54:55 -05:00
|
|
|
}
|
2013-07-15 17:43:30 -05:00
|
|
|
|
2015-12-07 09:43:41 -06:00
|
|
|
getdns_return_t
|
|
|
|
getdns_dict_set_bindata(
|
|
|
|
getdns_dict *dict, const char *name, const getdns_bindata *child_bindata)
|
|
|
|
{
|
|
|
|
return !child_bindata ? GETDNS_RETURN_INVALID_PARAMETER
|
|
|
|
: _getdns_dict_set_const_bindata(
|
|
|
|
dict, name, child_bindata->size, child_bindata->data);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2016-05-22 14:58:50 -05:00
|
|
|
getdns_item *item;
|
|
|
|
getdns_bindata *newbindata;
|
|
|
|
getdns_return_t r;
|
|
|
|
|
|
|
|
if (!dict || !name || !value)
|
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if (!(newbindata = _getdns_bindata_copy(
|
|
|
|
&dict->mf, strlen(value) + 1, (uint8_t *)value)))
|
|
|
|
return GETDNS_RETURN_MEMORY_ERROR;
|
|
|
|
|
|
|
|
newbindata->size -= 1;
|
|
|
|
|
|
|
|
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 */
|
|
|
|
|
2013-07-30 16:36:14 -05:00
|
|
|
/*---------------------------------------- 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)
|
2013-11-05 14:03:44 -06:00
|
|
|
{
|
2015-10-02 08:26:05 -05:00
|
|
|
getdns_item *item;
|
|
|
|
getdns_return_t r;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2013-11-11 16:10:22 -06:00
|
|
|
if (!dict || !name)
|
2014-01-10 09:35:03 -06:00
|
|
|
return GETDNS_RETURN_INVALID_PARAMETER;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
2015-10-02 08:26:05 -05:00
|
|
|
if ((r = _getdns_dict_find_and_add(dict, name, &item)))
|
|
|
|
return r;
|
2013-11-11 16:10:22 -06:00
|
|
|
|
|
|
|
item->dtype = t_int;
|
|
|
|
item->data.n = child_uint32;
|
|
|
|
return GETDNS_RETURN_GOOD;
|
2013-11-05 14:03:44 -06:00
|
|
|
} /* getdns_dict_set_int */
|
2013-07-15 17:43:30 -05:00
|
|
|
|
2013-11-01 12:07:03 -05:00
|
|
|
/*---------------------------------------- getdns_pp_dict */
|
|
|
|
/**
|
2014-01-10 09:09:07 -06:00
|
|
|
* private function to help with indenting.
|
2013-11-01 12:07:03 -05:00
|
|
|
* @param indent number of spaces to return
|
|
|
|
* @return a character string containing min(80, indent) spaces
|
|
|
|
*/
|
2013-10-30 17:23:53 -05:00
|
|
|
static const char *
|
2013-10-30 11:05:49 -05:00
|
|
|
getdns_indent(size_t indent)
|
2013-07-31 15:21:42 -05:00
|
|
|
{
|
2013-11-05 14:03:44 -06:00
|
|
|
static const char *spaces = " "
|
2014-03-07 09:42:37 -06:00
|
|
|
" ";
|
2013-11-05 14:03:44 -06:00
|
|
|
return spaces + 80 - (indent < 80 ? indent : 0);
|
|
|
|
} /* getdns_indent */
|
2013-07-31 15:21:42 -05:00
|
|
|
|
2015-12-21 05:22:59 -06:00
|
|
|
int
|
2015-08-19 09:22:38 -05:00
|
|
|
_getdns_bindata_is_dname(getdns_bindata *bindata)
|
2014-01-30 17:16:34 -06:00
|
|
|
{
|
|
|
|
size_t i = 0, n_labels = 0;
|
2015-07-08 08:36:39 -05:00
|
|
|
|
|
|
|
while (i < bindata->size && bindata->data[i]) {
|
2015-12-21 05:22:59 -06:00
|
|
|
if (bindata->data[i] & 0xC0) /* Compression pointer! */
|
|
|
|
return 0;
|
|
|
|
|
2014-02-20 16:35:27 -06:00
|
|
|
i += ((size_t)bindata->data[i]) + 1;
|
2014-01-30 17:16:34 -06:00
|
|
|
n_labels++;
|
|
|
|
}
|
2015-07-09 17:04:14 -05:00
|
|
|
if (i < bindata->size && !bindata->data[i]) {
|
|
|
|
n_labels++;
|
2015-07-08 08:36:39 -05:00
|
|
|
i++;
|
2015-07-09 17:04:14 -05:00
|
|
|
}
|
2015-07-08 08:36:39 -05:00
|
|
|
|
2014-01-30 17:16:34 -06:00
|
|
|
return i == bindata->size && n_labels > 1 &&
|
2014-03-07 09:42:37 -06:00
|
|
|
bindata->data[bindata->size - 1] == 0;
|
2014-01-30 17:16:34 -06:00
|
|
|
}
|
|
|
|
|
2013-11-01 12:07:03 -05:00
|
|
|
/*---------------------------------------- getdns_pp_bindata */
|
|
|
|
/**
|
2015-03-02 04:38:46 -06:00
|
|
|
* private function to pretty print bindata to a gldns_buffer
|
2013-11-01 12:07:03 -05:00
|
|
|
* @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
|
|
|
|
*/
|
2013-10-30 17:23:53 -05:00
|
|
|
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)
|
2013-10-30 11:05:49 -05:00
|
|
|
{
|
2015-03-02 04:38:46 -06:00
|
|
|
size_t i, p = gldns_buffer_position(buf);
|
2013-11-05 14:03:44 -06:00
|
|
|
uint8_t *dptr;
|
2015-03-20 13:55:17 -05:00
|
|
|
char spc[1024];
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2015-03-20 13:55:17 -05:00
|
|
|
if (!json && gldns_buffer_printf(buf, " <bindata ") < 0)
|
2013-11-05 14:03:44 -06:00
|
|
|
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]))
|
2013-11-05 14:03:44 -06:00
|
|
|
i++;
|
|
|
|
|
2015-02-17 14:44:16 -06:00
|
|
|
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)
|
2014-01-30 17:16:34 -06:00
|
|
|
return -1;
|
|
|
|
|
2015-02-17 14:44:16 -06:00
|
|
|
} 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)
|
2015-02-17 14:44:16 -06:00
|
|
|
return -1;
|
|
|
|
|
2014-01-30 17:16:34 -06:00
|
|
|
} else if (bindata->size == 1 && *bindata->data == 0) {
|
2015-03-20 13:55:17 -05:00
|
|
|
if (gldns_buffer_printf(buf, json ? "\".\"" : "for .>") < 0)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
2014-01-30 17:16:34 -06:00
|
|
|
|
2015-08-19 09:22:38 -05:00
|
|
|
} 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(
|
2016-05-03 06:35:01 -05:00
|
|
|
buf, (json ? "\"%s\"" : "for %s>"), spc) < 0)
|
2014-01-30 17:16:34 -06:00
|
|
|
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;
|
2014-01-30 17:16:34 -06:00
|
|
|
}
|
2015-03-20 13:55:17 -05:00
|
|
|
if (gldns_buffer_printf(buf, "]") < 0)
|
|
|
|
return -1;
|
2013-11-05 14:03:44 -06:00
|
|
|
} else {
|
2015-03-02 04:38:46 -06:00
|
|
|
if (gldns_buffer_printf(buf, "of 0x") < 0)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
|
|
|
for (dptr = bindata->data;
|
2014-03-07 09:42:37 -06:00
|
|
|
dptr < bindata->data + bindata->size; dptr++) {
|
2013-11-05 14:03:44 -06:00
|
|
|
if (dptr - bindata->data >= 16) {
|
2015-03-02 04:38:46 -06:00
|
|
|
if (gldns_buffer_printf(buf, "...") < 0)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
2015-03-02 04:38:46 -06:00
|
|
|
if (gldns_buffer_printf(buf, "%.2x", *dptr) < 0)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
|
|
|
}
|
2015-03-02 04:38:46 -06:00
|
|
|
if (gldns_buffer_printf(buf, ">") < 0)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
|
|
|
}
|
2015-03-02 04:38:46 -06:00
|
|
|
return gldns_buffer_position(buf) - p;
|
2013-11-05 14:03:44 -06:00
|
|
|
} /* getdns_pp_bindata */
|
2013-10-29 18:29:30 -05:00
|
|
|
|
2013-10-30 17:23:53 -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"};
|
2013-10-30 17:23:53 -05:00
|
|
|
|
2013-11-01 12:07:03 -05:00
|
|
|
/*---------------------------------------- getdns_pp_list */
|
2013-10-30 11:05:49 -05:00
|
|
|
/**
|
2015-03-02 04:38:46 -06:00
|
|
|
* private function to pretty print list to a gldns_buffer
|
2013-11-01 12:07:03 -05:00
|
|
|
* @param buf buffer to write to
|
2013-10-30 11:05:49 -05:00
|
|
|
* @param indent number of spaces to append after newline
|
2013-11-01 12:07:03 -05:00
|
|
|
* @param list the to list print
|
2015-06-17 11:18:09 -05:00
|
|
|
* @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.
|
2013-11-01 12:07:03 -05:00
|
|
|
* @return on success the number of written characters
|
|
|
|
* if an output error is encountered, a negative value
|
2013-10-30 11:05:49 -05:00
|
|
|
*/
|
2013-10-30 17:23:53 -05:00
|
|
|
static int
|
2015-03-15 15:23:44 -05:00
|
|
|
getdns_pp_list(gldns_buffer *buf, size_t indent, const getdns_list *list,
|
2015-06-17 11:18:09 -05:00
|
|
|
int for_literals, int json)
|
2013-10-30 11:05:49 -05:00
|
|
|
{
|
2015-03-02 04:38:46 -06:00
|
|
|
size_t i, length, p = gldns_buffer_position(buf);
|
2013-11-05 14:03:44 -06:00
|
|
|
getdns_data_type dtype;
|
|
|
|
struct getdns_dict *dict_item;
|
|
|
|
struct getdns_list *list_item;
|
|
|
|
struct getdns_bindata *bindata_item;
|
|
|
|
uint32_t int_item;
|
2014-10-28 10:36:41 -05:00
|
|
|
const char *strval;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2015-03-02 04:38:46 -06:00
|
|
|
if (gldns_buffer_printf(buf, "[") < 0)
|
2013-11-05 14:03:44 -06:00
|
|
|
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)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
|
|
|
|
2015-03-20 13:55:17 -05:00
|
|
|
if (getdns_list_get_data_type(list, i, &dtype) !=
|
|
|
|
GETDNS_RETURN_GOOD)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
switch (dtype) {
|
|
|
|
case t_int:
|
2014-10-28 10:36:41 -05:00
|
|
|
|
|
|
|
if (getdns_list_get_int(list, i, &int_item))
|
|
|
|
return -1;
|
2015-06-17 11:18:09 -05:00
|
|
|
if (!json && for_literals &&
|
2014-10-28 10:36:41 -05:00
|
|
|
(strval =
|
2015-08-19 09:22:38 -05:00
|
|
|
_getdns_get_const_info(int_item)->name)) {
|
2015-03-02 04:38:46 -06:00
|
|
|
if (gldns_buffer_printf(buf, "%s", strval) < 0)
|
2014-10-28 10:36:41 -05:00
|
|
|
return -1;
|
|
|
|
} else if (0 >
|
2015-03-02 04:38:46 -06:00
|
|
|
gldns_buffer_printf(buf, "%d", (int)int_item))
|
2014-02-04 03:10:12 -06:00
|
|
|
return -1;
|
2013-11-05 14:03:44 -06:00
|
|
|
break;
|
|
|
|
|
|
|
|
case t_bindata:
|
|
|
|
if (getdns_list_get_bindata(list, i, &bindata_item) !=
|
2015-03-20 13:55:17 -05:00
|
|
|
GETDNS_RETURN_GOOD)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
2015-03-20 13:55:17 -05:00
|
|
|
if (getdns_pp_bindata(
|
|
|
|
buf, indent, bindata_item, 0, json) < 0)
|
2013-11-05 14:03:44 -06:00
|
|
|
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)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
2015-03-20 13:55:17 -05:00
|
|
|
if (getdns_pp_list(
|
|
|
|
buf, indent, list_item, 0, json) < 0)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case t_dict:
|
|
|
|
if (getdns_list_get_dict(list, i, &dict_item) !=
|
2014-03-07 09:42:37 -06:00
|
|
|
GETDNS_RETURN_GOOD)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
2015-03-20 13:55:17 -05:00
|
|
|
if (getdns_pp_dict(buf, indent, dict_item, json) < 0)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-03-20 13:55:17 -05:00
|
|
|
if (gldns_buffer_printf(
|
|
|
|
buf, "%s", unknown_str_l[json]) < 0)
|
2013-11-05 14:03:44 -06:00
|
|
|
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)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
|
|
|
|
2015-03-02 04:38:46 -06:00
|
|
|
return gldns_buffer_position(buf) - p;
|
2013-11-05 14:03:44 -06:00
|
|
|
} /* getdns_pp_list */
|
2013-11-01 12:07:03 -05:00
|
|
|
|
2014-02-19 07:02:02 -06:00
|
|
|
static int
|
2015-08-19 09:22:38 -05:00
|
|
|
_getdns_print_class(gldns_buffer *buf, uint32_t klass)
|
2014-02-19 07:02:02 -06:00
|
|
|
{
|
|
|
|
switch (klass) {
|
|
|
|
case GETDNS_RRCLASS_IN:
|
2015-03-02 04:38:46 -06:00
|
|
|
(void) gldns_buffer_printf(buf, " GETDNS_RRCLASS_IN");
|
2014-02-19 07:02:02 -06:00
|
|
|
return 1;
|
|
|
|
case GETDNS_RRCLASS_CH:
|
2015-03-02 04:38:46 -06:00
|
|
|
(void) gldns_buffer_printf(buf, " GETDNS_RRCLASS_CH");
|
2014-02-19 07:02:02 -06:00
|
|
|
return 1;
|
|
|
|
case GETDNS_RRCLASS_HS:
|
2015-03-02 04:38:46 -06:00
|
|
|
(void) gldns_buffer_printf(buf, " GETDNS_RRCLASS_HS");
|
2014-02-19 07:02:02 -06:00
|
|
|
return 1;
|
|
|
|
case GETDNS_RRCLASS_NONE:
|
2015-03-02 04:38:46 -06:00
|
|
|
(void) gldns_buffer_printf(buf, " GETDNS_RRCLASS_NONE");
|
2014-02-19 07:02:02 -06:00
|
|
|
return 1;
|
|
|
|
case GETDNS_RRCLASS_ANY:
|
2015-03-02 04:38:46 -06:00
|
|
|
(void) gldns_buffer_printf(buf, " GETDNS_RRCLASS_ANY");
|
2014-02-19 07:02:02 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-08-19 09:22:38 -05:00
|
|
|
_getdns_print_opcode(gldns_buffer *buf, uint32_t opcode)
|
2014-02-19 07:02:02 -06:00
|
|
|
{
|
|
|
|
switch (opcode) {
|
|
|
|
case GETDNS_OPCODE_QUERY:
|
2015-03-02 04:38:46 -06:00
|
|
|
(void) gldns_buffer_printf(buf, " GETDNS_OPCODE_QUERY");
|
2014-02-19 07:02:02 -06:00
|
|
|
return 1;
|
|
|
|
case GETDNS_OPCODE_IQUERY:
|
2015-03-02 04:38:46 -06:00
|
|
|
(void) gldns_buffer_printf(buf, " GETDNS_OPCODE_IQUERY");
|
2014-02-19 07:02:02 -06:00
|
|
|
return 1;
|
|
|
|
case GETDNS_OPCODE_STATUS:
|
2015-03-02 04:38:46 -06:00
|
|
|
(void) gldns_buffer_printf(buf, " GETDNS_OPCODE_STATUS");
|
2014-02-19 07:02:02 -06:00
|
|
|
return 1;
|
|
|
|
case GETDNS_OPCODE_NOTIFY:
|
2015-03-02 04:38:46 -06:00
|
|
|
(void) gldns_buffer_printf(buf, " GETDNS_OPCODE_NOTIFY");
|
2014-02-19 07:02:02 -06:00
|
|
|
return 1;
|
|
|
|
case GETDNS_OPCODE_UPDATE:
|
2015-03-02 04:38:46 -06:00
|
|
|
(void) gldns_buffer_printf(buf, " GETDNS_OPCODE_UPDATE");
|
2014-02-19 07:02:02 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-08-19 09:22:38 -05:00
|
|
|
_getdns_print_rcode(gldns_buffer *buf, uint32_t rcode)
|
2014-02-19 07:02:02 -06:00
|
|
|
{
|
|
|
|
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"
|
|
|
|
};
|
2014-02-19 12:15:27 -06:00
|
|
|
if (rcode <= 10)
|
2015-03-02 04:38:46 -06:00
|
|
|
(void) gldns_buffer_printf(buf, "%s", rcodes[rcode]);
|
2014-02-19 07:02:02 -06:00
|
|
|
else if (rcode >= 16 && rcode <= 22)
|
2015-03-02 04:38:46 -06:00
|
|
|
(void) gldns_buffer_printf(buf, "%s", rcodes[rcode-6]);
|
2014-02-19 07:02:02 -06:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-11-01 12:07:03 -05:00
|
|
|
/*---------------------------------------- getdns_pp_dict */
|
|
|
|
/**
|
2015-03-02 04:38:46 -06:00
|
|
|
* private function to pretty print dict to a gldns_buffer
|
2013-11-01 12:07:03 -05:00
|
|
|
* @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
|
|
|
|
*/
|
2013-10-30 17:23:53 -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)
|
2013-10-30 17:23:53 -05:00
|
|
|
{
|
2015-03-02 04:38:46 -06:00
|
|
|
size_t i, length, p = gldns_buffer_position(buf);
|
2013-11-05 14:03:44 -06:00
|
|
|
struct getdns_dict_item *item;
|
2014-01-30 17:16:34 -06:00
|
|
|
const char *strval;
|
2013-11-05 14:03:44 -06:00
|
|
|
|
2014-05-22 11:31:30 -05:00
|
|
|
char abuf[80];
|
|
|
|
|
2013-11-05 14:03:44 -06:00
|
|
|
if (dict == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2015-03-02 04:38:46 -06:00
|
|
|
if (gldns_buffer_printf(buf, "{") < 0)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
indent += 2;
|
2014-10-23 16:00:30 -05:00
|
|
|
RBTREE_FOR(item, struct getdns_dict_item *,
|
2015-08-19 09:07:01 -05:00
|
|
|
(_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)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
|
|
|
|
2015-09-30 09:05:19 -05:00
|
|
|
switch (item->i.dtype) {
|
2013-11-05 14:03:44 -06:00
|
|
|
case t_int:
|
2015-03-20 13:55:17 -05:00
|
|
|
if (!json &&
|
|
|
|
(strcmp(item->node.key, "type") == 0 ||
|
2014-10-28 10:36:41 -05:00
|
|
|
strcmp(item->node.key, "type_covered") == 0 ||
|
2015-10-31 22:47:49 -05:00
|
|
|
strcmp(item->node.key, "query_type") == 0 ||
|
2014-10-28 10:36:41 -05:00
|
|
|
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(
|
2014-03-07 09:42:37 -06:00
|
|
|
buf, " GETDNS_RRTYPE_%s", strval) < 0)
|
2014-01-30 17:16:34 -06:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
2015-03-20 13:55:17 -05:00
|
|
|
if (!json &&
|
|
|
|
(strcmp(item->node.key, "answer_type") == 0 ||
|
2014-10-28 10:36:41 -05:00
|
|
|
strcmp(item->node.key, "dnssec_status") == 0 ||
|
2015-12-21 18:03:31 -06:00
|
|
|
strcmp(item->node.key, "tsig_status") == 0 ||
|
2014-10-28 10:36:41 -05:00
|
|
|
strcmp(item->node.key, "status") == 0 ||
|
|
|
|
strcmp(item->node.key, "append_name") == 0 ||
|
|
|
|
strcmp(item->node.key, "follow_redirects") == 0 ||
|
2016-06-17 05:25:21 -05:00
|
|
|
strcmp(item->node.key, "transport") == 0 ||
|
2015-11-13 07:28:43 -06:00
|
|
|
strcmp(item->node.key, "resolution_type") == 0 ||
|
|
|
|
strcmp(item->node.key, "tls_authentication") == 0 ) &&
|
2014-10-28 10:36:41 -05:00
|
|
|
(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)
|
2014-02-12 16:10:29 -06:00
|
|
|
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))
|
2014-02-19 07:02:02 -06:00
|
|
|
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))
|
2014-02-19 07:02:02 -06:00
|
|
|
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))
|
2014-02-19 07:02:02 -06:00
|
|
|
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)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case t_bindata:
|
2014-05-22 11:31:30 -05:00
|
|
|
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 )) {
|
2014-05-22 11:31:30 -05:00
|
|
|
|
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
|
2014-05-22 11:31:30 -05:00
|
|
|
? 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)
|
2014-05-22 11:31:30 -05:00
|
|
|
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)
|
2013-11-05 14:03:44 -06:00
|
|
|
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)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
|
|
|
if (length == 0) {
|
2015-03-20 13:55:17 -05:00
|
|
|
if (gldns_buffer_printf(
|
|
|
|
buf, (json < 2 ? " []" : "[]")) < 0)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
2015-03-20 13:55:17 -05:00
|
|
|
if (json < 2 && gldns_buffer_printf(buf, "\n%s",
|
|
|
|
getdns_indent(indent)) < 0)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
2015-09-30 09:05:19 -05:00
|
|
|
if (getdns_pp_list(buf, indent, item->i.data.list,
|
2015-06-17 11:18:09 -05:00
|
|
|
(strcmp(item->node.key, "namespaces") == 0 ||
|
2015-12-31 05:40:20 -06:00
|
|
|
strcmp(item->node.key, "dns_transport_list") == 0
|
|
|
|
|| strcmp(item->node.key, "bad_dns") == 0),
|
2015-03-20 13:55:17 -05:00
|
|
|
json) < 0)
|
2013-11-05 14:03:44 -06:00
|
|
|
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)
|
2013-11-05 14:03:44 -06:00
|
|
|
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)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2015-03-20 13:55:17 -05:00
|
|
|
if (gldns_buffer_printf(
|
|
|
|
buf, "%s", unknown_str_l[json]) < 0)
|
2013-11-05 14:03:44 -06:00
|
|
|
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)
|
2013-11-05 14:03:44 -06:00
|
|
|
return -1;
|
|
|
|
|
2015-03-02 04:38:46 -06:00
|
|
|
return gldns_buffer_position(buf) - p;
|
2013-11-05 14:03:44 -06:00
|
|
|
} /* getdns_pp_dict */
|
2013-10-30 17:23:53 -05:00
|
|
|
|
2013-10-30 11:05:49 -05:00
|
|
|
/*---------------------------------------- getdns_pretty_print_dict */
|
2013-11-01 12:07:03 -05:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2013-10-30 11:05:49 -05:00
|
|
|
char *
|
2014-01-14 10:25:23 -06:00
|
|
|
getdns_pretty_print_dict(const struct getdns_dict *dict)
|
2013-10-30 11:05:49 -05:00
|
|
|
{
|
2015-03-02 04:38:46 -06:00
|
|
|
gldns_buffer *buf;
|
2013-11-05 14:03:44 -06:00
|
|
|
char *ret;
|
|
|
|
|
2013-11-11 16:10:22 -06:00
|
|
|
if (!dict)
|
2013-11-05 14:03:44 -06:00
|
|
|
return NULL;
|
|
|
|
|
2015-03-15 15:23:44 -05:00
|
|
|
buf = gldns_buffer_new(8192);
|
2013-11-05 14:03:44 -06:00
|
|
|
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);
|
2013-11-05 14:03:44 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-03-02 04:38:46 -06:00
|
|
|
ret = (char *) gldns_buffer_export(buf);
|
|
|
|
gldns_buffer_free(buf);
|
2013-11-05 14:03:44 -06:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-03-15 15:23:44 -05:00
|
|
|
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) {
|
2015-03-15 15:23:44 -05:00
|
|
|
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
|
|
|
|
2013-11-04 17:37:54 -06:00
|
|
|
/* dict.c */
|