Skip to content
Snippets Groups Projects
Commit 1646067c authored by Jan Kadlec's avatar Jan Kadlec
Browse files

Initial dname table implementation.

Refs #793 #784
parent ce149032
No related branches found
No related tags found
No related merge requests found
......@@ -157,3 +157,5 @@ tests/querytcp.c
src/zcompile/tests/unittests_zp_main.c
src/zcompile/tests/zcompile_tests.c
src/zcompile/zcompile-error.c
src/dnslib/dname-table.h
src/dnslib/dname-table.c
......@@ -131,6 +131,7 @@ libknot_la_SOURCES = \
dnslib/rdata.c \
dnslib/descriptor.c \
dnslib/nsec3.c \
dnslib/dname-table.c \
dnslib/hash/hash-functions.c \
dnslib/hash/cuckoo-hash-table.c \
dnslib/hash/universal-system.c \
......@@ -156,6 +157,7 @@ libknot_la_SOURCES = \
dnslib/packet.h \
dnslib/debug.h \
dnslib/nsec3.h \
dnslib/dname-table.h \
knot/common.h \
knot/other/log.c \
knot/other/log.h \
......
#include <assert.h>
#include <string.h>
#include <malloc.h>
#include "dnslib/dname-table.h"
#include "dnslib/error.h"
/* Tree functions. */
TREE_DEFINE(dname_table_node, avl);
static int compare_dname_table_nodes(struct dname_table_node *n1,
struct dname_table_node *n2)
{
assert(n1 && n2);
return (strncmp((char *)n1->dname->name, (char *)n2->dname->name,
(n1->dname->size < n2->dname->size) ?
(n1->dname->size):(n2->dname->size)));
}
static void delete_dname_table_node(struct dname_table_node *node, void *data)
{
UNUSED(data);
/*!< \todo it would be nice to set pointers to NULL, too. */
free(node);
}
dnslib_dname_table_t *dnslib_dname_table_new()
{
dnslib_dname_table_t *ret = malloc(sizeof(dnslib_dname_table_t));
CHECK_ALLOC_LOG(ret, NULL);
ret->tree = malloc(sizeof(table_tree_t));
if (ret->tree == NULL) {
ERR_ALLOC_FAILED;
free(ret);
return NULL;
}
TREE_INIT(ret->tree, compare_dname_table_nodes);
return ret;
}
const dnslib_dname_t *dname_table_find_dname(const dnslib_dname_table_t *table,
const dnslib_dname_t *dname)
{
struct dname_table_node *node = NULL;
struct dname_table_node sought;
sought.dname = dname;
node = TREE_FIND(table->tree, dname_table_node, avl, &sought);
if (node == NULL) {
return NULL;
} else {
return node->dname;
}
}
int dname_table_add_dname(const dnslib_dname_table_t *table,
const dnslib_dname_t *dname)
{
/* Node for insertion has to be created */
struct dname_table_node *node =
malloc(sizeof(struct dname_table_node));
CHECK_ALLOC_LOG(node, DNSLIB_ENOMEM);
node->dname = dname;
TREE_INSERT(table->tree, dname_table_node, avl, node);
return DNSLIB_EOK;
}
void dname_table_free(dnslib_dname_table_t **table)
{
if (table == NULL || *table == NULL) {
return;
}
/* Walk the tree and free each node, but not the dnames. */
TREE_FORWARD_APPLY((*table)->tree, dname_table_node, avl,
delete_dname_table_node, NULL);
}
#ifndef DNAMETABLE_H
#define DNAMETABLE_H
#include <config.h>
#include "common/tree.h"
#include "dnslib/dname.h"
#include "dnslib/dnslib-common.h"
struct dname_table_node {
const dnslib_dname_t *dname;
TREE_ENTRY(dname_table_node) avl;
};
typedef TREE_HEAD(avl, dname_table_node) table_tree_t;
/*!< \note contains only tree now, but might change in the future. */
struct dnslib_dname_table {
table_tree_t *tree;
};
typedef struct dnslib_dname_table dnslib_dname_table_t;
dnslib_dname_table_t *dname_table_new();
const dnslib_dname_t *dname_table_find_dname(const dnslib_dname_table_t *table,
const dnslib_dname_t *dname);
int dname_table_add_dname(const dnslib_dname_table_t *table,
const dnslib_dname_t *dname);
void dname_table_free(dnslib_dname_table_t **table);
#endif // DNAMETABLE_H
......@@ -271,6 +271,7 @@ dnslib_dname_t *dnslib_dname_new()
dname->node = NULL;
dname->labels = NULL;
dname->label_count = -1;
dname->id = 0;
return dname;
}
......@@ -307,6 +308,7 @@ dnslib_dname_t *dnslib_dname_new_from_str(const char *name, uint size,
assert(dname->name != NULL);
dname->node = node;
dname->id = 0;
return dname;
}
......@@ -365,6 +367,7 @@ dnslib_dname_t *dnslib_dname_new_from_wire(const uint8_t *name, uint size,
assert(dname->label_count >= 0);
dname->node = node;
dname->id = 0;
return dname;
}
......@@ -374,7 +377,6 @@ dnslib_dname_t *dnslib_dname_new_from_wire(const uint8_t *name, uint size,
int dnslib_dname_from_wire(const uint8_t *name, uint size,
struct dnslib_node *node, dnslib_dname_t *target)
{
/* Change by JK, was target != NULL, which made no sense to me */
if (name == NULL || target == NULL) {
return DNSLIB_EBADARG;
}
......@@ -382,6 +384,7 @@ int dnslib_dname_from_wire(const uint8_t *name, uint size,
memcpy(target->name, name, size);
target->size = size;
target->node = node;
target->id = 0;
return dnslib_dname_find_labels(target, 0);
}
......@@ -854,3 +857,17 @@ dnslib_dname_t *dnslib_dname_cat(dnslib_dname_t *d1, const dnslib_dname_t *d2)
return d1;
}
void dnslib_dname_set_id(dnslib_dname_t *dname, unsigned int id)
{
dname->id = id;
}
unsigned int dnslib_dname_get_id(const dnslib_dname_t *dname)
{
if (dname != NULL) {
return dname->id;
} else {
return 0; /* 0 should never be used and is reserved for err. */
}
}
......@@ -34,6 +34,7 @@ struct dnslib_dname {
uint8_t *labels;
short label_count;
struct dnslib_node *node; /*!< Zone node the domain name belongs to. */
unsigned int id; /*!< ID of domain name used in zone dumping. */
};
typedef struct dnslib_dname dnslib_dname_t;
......@@ -310,6 +311,10 @@ int dnslib_dname_compare(const dnslib_dname_t *d1, const dnslib_dname_t *d2);
*/
dnslib_dname_t *dnslib_dname_cat(dnslib_dname_t *d1, const dnslib_dname_t *d2);
void dnslib_dname_set_id(dnslib_dname_t *dname, unsigned int id);
unsigned int dnslib_dname_get_id(const dnslib_dname_t *dname);
#endif /* _KNOT_DNSLIB_DNAME_H_ */
/*! @} */
......@@ -15,6 +15,7 @@
#include "dnslib/node.h"
#include "dnslib/dname.h"
#include "dnslib/nsec3.h"
#include "dnslib/dname-table.h"
#include "common/tree.h"
#include "dnslib/hash/cuckoo-hash-table.h"
......@@ -50,6 +51,7 @@ struct dnslib_zone {
ck_hash_table_t *table; /*!< Hash table for holding zone nodes. */
uint node_count;
dnslib_nsec3_params_t nsec3_params;
dnslib_dname_table_t *dname_table;
};
typedef struct dnslib_zone dnslib_zone_t;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment