Commit 930dd52e authored by Lubos Slovak's avatar Lubos Slovak

First dname functions' API and its implementation.

- creating dname (empty, from string, from wire format)
- deleting dname
- converting dname to string

Minor change in node - added linked list pointer to next node.

refs #5, #88
parent 74e9f634
#include <stdint.h>
#include "common.h"
#include "consts.h"
#include "dname.h"
/*----------------------------------------------------------------------------*/
/* Non-API functions */
/*----------------------------------------------------------------------------*/
/*!
* \todo handle \X and \DDD (RFC 1035 5.1) or it can be handled by the parser?
*/
int dnslib_dname_str_to_wire( const char *name, uint size, uint8_t **wire )
{
if (size > DNSLIB_MAX_DNAME_LENGTH) {
return 0;
}
// signed / unsigned issues??
*wire = (uint8_t *)malloc((size + 1) * sizeof(uint8_t));
if (*wire == NULL) {
return 0;
}
const uint8_t *ch = (const uint8_t *)name;
uint8_t *label_start = *wire;
uint8_t *w = *wire;
uint8_t label_length = 0;
while (ch - name < size) {
assert(w - *wire < size);
assert(w - *wire == ch - name);
if (*ch == '.' ) {
*label_start = label_length;
label_start = w;
} else {
*w = *ch;
}
++w;
++ch;
assert(ch >= name);
}
// put 0 for root label regardless whether the name ended with .
*w = 0;
//memcpy(*wire, name, size);
return size;
}
/*----------------------------------------------------------------------------*/
/* API functions */
/*----------------------------------------------------------------------------*/
dnslib_dname_t *dnslib_dname_new()
{
dnslib_dname_t *dname = (dnslib_dname_t *)malloc(sizeof(dnslib_dname_t));
if (name == NULL) {
ERR_ALLOC_FAILED;
return NULL;
}
dname->name = NULL;
dname->size = 0;
dname->node = NULL;
return dname;
}
/*----------------------------------------------------------------------------*/
dnslib_dname_t *dnslib_dname_new_from_str( char *name, uint size,
dnslib_node_t *node )
{
if (name == NULL || size == 0) {
return NULL;
}
dnslib_dname_t *dname = (dnslib_dname_t *)malloc(sizeof(dnslib_dname_t));
if (name == NULL) {
ERR_ALLOC_FAILED;
return NULL;
}
dname->size = dnslib_dname_str_to_wire(name, size, &dname->name);
if (dname->size <= 0) {
log_warning("Could not parse domain name from string: '%.*s'\n",
size, name);
}
dname->node = node;
return dname;
}
/*----------------------------------------------------------------------------*/
dnslib_dname_t *dnslib_dname_new_from_wire( uint8_t *name, uint size,
dnslib_node_t *node )
{
dnslib_dname_t *dname = (dnslib_dname_t *)malloc(sizeof(dnslib_dname_t));
if (name == NULL) {
ERR_ALLOC_FAILED;
return NULL;
}
dname->name = name;
dname->size = size;
dname->node = node;
return dname;
}
/*----------------------------------------------------------------------------*/
char *dnslib_dname_to_str( dnslib_dname_t *dname )
{
char *name = (char *)malloc(dname->size * sizeof(char));
uint8_t *w = dname->name;
char *ch = name;
while (*w != 0) {
// skip label length
uint8_t *next = w + *w + 1;
while (w != next) {
*(ch++) = *(w++);
}
}
*ch = 0;
assert(ch - name == dname->size);
return name;
}
/*----------------------------------------------------------------------------*/
void dnslib_dname_free( dnslib_dname *dname )
{
free(dname->name);
free(dname);
}
......@@ -5,12 +5,45 @@
#include "common.h"
#include "node.h"
/*----------------------------------------------------------------------------*/
struct dnslib_dname {
uint8_t *dname;
uint length; // is this needed? every dname should end with \0 or pointer
uint8_t *name; // wire format of a domain name; always ends with 0!!
uint size; // is this needed? every dname should end with \0 or pointer
dnslib_node_t *node; // NULL if not in zone
};
typedef struct dname dnslib_dname_t;
/*----------------------------------------------------------------------------*/
/*!
* \todo Possibly useless.
*/
dnslib_dname_t *dnslib_dname_new();
/*!
* \note \a name must be 0-terminated.
* \note \a node may be NULL.
*/
dnslib_dname_t *dnslib_dname_new_from_str( char *name, uint size,
dnslib_node_t *node );
/*!
* \note Copies the name.
*/
dnslib_dname_t *dnslib_dname_new_from_wire( uint8_t *name, uint size );
/*!
* \note Allocates new memory, remember to free it. Returns 0-terminated string.
*/
char *dnslib_dname_to_str( dnslib_dname_t *dname );
/*!
* \note Frees also the data within the struct.
*/
void dnslib_dname_free( dnslib_dname *dname );
#endif /* _CUTEDNS_DNAME_H */
......@@ -7,7 +7,9 @@
struct dnslib_node {
dnslib_dname_t *owner;
struct dnslib_node *parent;
skip_list *rrsets;
skip_list *rrsets; // key - RRTYPE (uint16_t); value - dnslib_rrset_t *
struct dnslib_node *next; // temporary
};
typedef struct dnslib_node dnslib_node_t;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment