Commit 18b54da2 authored by Marek Vavrusa's avatar Marek Vavrusa

Unified basic checks in functions, asserts if the func ret is bool.

Also updated documentation for functions where it was wrong.
parent db1b3303
......@@ -44,6 +44,9 @@ static int knot_label_is_equal(const uint8_t *lb1, const uint8_t *lb2)
knot_dname_t *knot_dname_parse(const uint8_t *pkt, size_t *pos, size_t maxpos)
{
if (pkt == NULL || pos == NULL)
return NULL;
const uint8_t *name = pkt + *pos;
const uint8_t *endp = pkt + maxpos;
int parsed = knot_dname_wire_check(name, endp, pkt);
......@@ -86,7 +89,9 @@ knot_dname_t *knot_dname_copy(const knot_dname_t *name)
knot_dname_t *knot_dname_copy_part(const knot_dname_t *name, unsigned len)
{
assert(name && len > 0);
if (name == NULL || len == 0)
return NULL;
knot_dname_t *dst = malloc(len);
if (knot_dname_to_wire(dst, name, len) < 1) {
free(dst);
......@@ -100,6 +105,9 @@ knot_dname_t *knot_dname_copy_part(const knot_dname_t *name, unsigned len)
int knot_dname_to_wire(uint8_t *dst, const knot_dname_t *src, size_t maxlen)
{
if (dst == NULL || src == NULL)
return KNOT_EINVAL;
/* Write out non or partially compressed name. */
int len = 0;
while (*src != '\0' && !knot_wire_is_pointer(src)) {
......@@ -241,7 +249,7 @@ char *knot_dname_to_str(const knot_dname_t *name)
knot_dname_t *knot_dname_from_str(const char *name, unsigned len)
{
if (len == 0 || len > KNOT_DNAME_MAXLEN) {
if (name == NULL || len == 0 || len > KNOT_DNAME_MAXLEN) {
return NULL;
}
......@@ -294,6 +302,9 @@ knot_dname_t *knot_dname_from_str(const char *name, unsigned len)
int knot_dname_to_lower(knot_dname_t *name)
{
if (name == NULL)
return KNOT_EINVAL;
/*! \todo Faster with \xdfdf mask. */
while (*name != '\0') {
for (uint8_t i = 0; i < *name; ++i)
......@@ -309,6 +320,9 @@ int knot_dname_to_lower(knot_dname_t *name)
int knot_dname_size(const knot_dname_t *name)
{
if (name == NULL)
return KNOT_EINVAL;
/* Count name size without terminal label. */
int len = 0;
while (*name != '\0' && !knot_wire_is_pointer(name)) {
......@@ -339,6 +353,7 @@ bool knot_dname_is_sub(const knot_dname_t *sub, const knot_dname_t *domain)
return false;
/* Count labels. */
assert(sub != NULL && domain != NULL);
int sub_l = knot_dname_labels(sub, NULL);
int domain_l = knot_dname_labels(domain, NULL);
......@@ -364,33 +379,37 @@ bool knot_dname_is_sub(const knot_dname_t *sub, const knot_dname_t *domain)
/*----------------------------------------------------------------------------*/
int knot_dname_is_wildcard(const knot_dname_t *name)
bool knot_dname_is_wildcard(const knot_dname_t *name)
{
assert(name != NULL);
return name[0] == 1 && name[1] == '*';
}
/*----------------------------------------------------------------------------*/
int knot_dname_matched_labels(const knot_dname_t *dname1, const knot_dname_t *dname2)
int knot_dname_matched_labels(const knot_dname_t *d1, const knot_dname_t *d2)
{
if (d1 == NULL || d2 == NULL)
return KNOT_EINVAL;
/* Count labels. */
int l1 = knot_dname_labels(dname1, NULL);
int l2 = knot_dname_labels(dname2, NULL);
int l1 = knot_dname_labels(d1, NULL);
int l2 = knot_dname_labels(d2, NULL);
/* Align end-to-end to common suffix. */
int common = knot_dname_align(&dname1, l1, &dname2, l2, NULL);
int common = knot_dname_align(&d1, l1, &d2, l2, NULL);
/* Count longest chain leading to root label. */
int matched = 0;
while (common > 0) {
if (knot_label_is_equal(dname1, dname2))
if (knot_label_is_equal(d1, d2))
++matched;
else
matched = 0; /* Broken chain. */
/* Next label. */
dname1 = knot_wire_next_label(dname1, NULL);
dname2 = knot_wire_next_label(dname2, NULL);
d1 = knot_wire_next_label(d1, NULL);
d2 = knot_wire_next_label(d2, NULL);
--common;
}
......@@ -399,16 +418,19 @@ int knot_dname_matched_labels(const knot_dname_t *dname1, const knot_dname_t *dn
/*----------------------------------------------------------------------------*/
knot_dname_t *knot_dname_replace_suffix(const knot_dname_t *dname, unsigned labels,
knot_dname_t *knot_dname_replace_suffix(const knot_dname_t *name, unsigned labels,
const knot_dname_t *suffix)
{
if (name == NULL)
return NULL;
/* Calculate prefix and suffix lengths. */
int dname_lbs = knot_dname_labels(dname, NULL);
int dname_lbs = knot_dname_labels(name, NULL);
assert(dname_lbs >= labels);
unsigned prefix_lbs = dname_lbs - labels;
/* Trim 1 octet from prefix, as it is measured as FQDN. */
int prefix_len = knot_dname_prefixlen(dname, prefix_lbs, NULL) - 1;
int prefix_len = knot_dname_prefixlen(name, prefix_lbs, NULL) - 1;
int suffix_len = knot_dname_size(suffix);
if (prefix_len < 0 || suffix_len < 0)
return NULL;
......@@ -422,9 +444,9 @@ knot_dname_t *knot_dname_replace_suffix(const knot_dname_t *dname, unsigned labe
/* Copy prefix. */
uint8_t *dst = out;
while (prefix_lbs > 0) {
memcpy(dst, dname, *dname + 1);
dst += *dname + 1;
dname = knot_wire_next_label(dname, NULL);
memcpy(dst, name, *name + 1);
dst += *name + 1;
name = knot_wire_next_label(name, NULL);
--prefix_lbs;
}
......@@ -440,14 +462,13 @@ knot_dname_t *knot_dname_replace_suffix(const knot_dname_t *dname, unsigned labe
/*----------------------------------------------------------------------------*/
void knot_dname_free(knot_dname_t **dname)
void knot_dname_free(knot_dname_t **name)
{
if (dname == NULL || *dname == NULL) {
if (name == NULL || *name == NULL)
return;
}
free(*dname);
*dname = NULL;
free(*name);
*name = NULL;
}
/*----------------------------------------------------------------------------*/
......@@ -462,10 +483,15 @@ int knot_dname_cmp(const knot_dname_t *d1, const knot_dname_t *d2)
int knot_dname_cmp_wire(const knot_dname_t *d1, const knot_dname_t *d2,
const uint8_t *pkt)
{
/* This would be hard to catch since -1 is a good result, assert instead. */
assert(d1 != NULL || d2 != NULL);
/* Convert to lookup format. */
uint8_t d1_lf[KNOT_DNAME_MAXLEN], d2_lf[KNOT_DNAME_MAXLEN];
if (knot_dname_lf(d1_lf, d1, pkt) < 0 || knot_dname_lf(d2_lf, d2, pkt) < 0)
if (knot_dname_lf(d1_lf, d1, pkt) < 0 || knot_dname_lf(d2_lf, d2, pkt) < 0) {
assert(0); /* This must not happend as the d1, d2 are checked. */
return KNOT_EINVAL;
}
/* Compare common part. */
uint8_t common = d1_lf[0];
......@@ -486,7 +512,7 @@ int knot_dname_cmp_wire(const knot_dname_t *d1, const knot_dname_t *d2,
/*----------------------------------------------------------------------------*/
int knot_dname_is_equal(const knot_dname_t *d1, const knot_dname_t *d2)
bool knot_dname_is_equal(const knot_dname_t *d1, const knot_dname_t *d2)
{
/*! \todo Could be implemented more efficiently, check profile first. */
return (knot_dname_cmp(d1, d2) == 0);
......@@ -496,6 +522,9 @@ int knot_dname_is_equal(const knot_dname_t *d1, const knot_dname_t *d2)
knot_dname_t *knot_dname_cat(knot_dname_t *d1, const knot_dname_t *d2)
{
if (d1 == NULL || d2 == NULL)
return NULL;
/* This is problem equal to replacing last \x00 from d1 with d2. */
knot_dname_t *ret = knot_dname_replace_suffix(d1, 0, d2);
/* Like if we are reallocating d1. */
......@@ -570,7 +599,7 @@ int knot_dname_wire_check(const uint8_t *name, const uint8_t *endp,
int knot_dname_prefixlen(const uint8_t *name, unsigned nlabels, const uint8_t *pkt)
{
if (!name)
if (name == NULL)
return KNOT_EINVAL;
/* Zero labels means 1 octet \x00 */
......@@ -593,6 +622,9 @@ int knot_dname_prefixlen(const uint8_t *name, unsigned nlabels, const uint8_t *p
int knot_dname_labels(const uint8_t *name, const uint8_t *pkt)
{
if (name == NULL)
return KNOT_EINVAL;
uint8_t count = 0;
while (*name != '\0') {
++count;
......@@ -607,6 +639,9 @@ int knot_dname_align(const uint8_t **d1, uint8_t d1_labels,
const uint8_t **d2, uint8_t d2_labels,
uint8_t *wire)
{
if (d1 == NULL || d2 == NULL)
return KNOT_EINVAL;
for (unsigned j = d1_labels; j < d2_labels; ++j)
*d2 = knot_wire_next_label(*d2, wire);
......@@ -618,6 +653,9 @@ int knot_dname_align(const uint8_t **d1, uint8_t d1_labels,
int knot_dname_lf(uint8_t *dst, const knot_dname_t *src, const uint8_t *pkt)
{
if (dst == NULL || src == NULL)
return KNOT_EINVAL;
uint8_t *len = dst++;
*len = '\0';
*dst = '\0';
......
......@@ -66,7 +66,7 @@ knot_dname_t *knot_dname_parse(const uint8_t *pkt, size_t *pos, size_t maxpos);
/*!
* \brief Duplicates the given domain name.
*
* \param dname Domain name to be copied.
* \param name Domain name to be copied.
*
* \return New domain name which is an exact copy of \a dname.
*/
......@@ -75,7 +75,7 @@ knot_dname_t *knot_dname_copy(const knot_dname_t *name);
/*!
* \brief Duplicates part of the given domain name.
*
* \param dname Domain name to be copied.
* \param name Domain name to be copied.
* \param len Part length.
*
* \return New domain name which is an partial copy of \a dname.
......@@ -119,7 +119,7 @@ int knot_dname_unpack(uint8_t *dst, const knot_dname_t *src,
*
* \todo The function doesn't process escaped characters like \DDD or \X.
*
* \param dname Domain name to be converted.
* \param name Domain name to be converted.
*
* \return 0-terminated string representing the given domain name in
* presentation format.
......@@ -154,7 +154,7 @@ int knot_dname_to_lower(knot_dname_t *name);
/*!
* \brief Returns size of the given domain name.
*
* \param dname Domain name to get the size of.
* \param name Domain name to get the size of.
*
* \retval size of the domain name
* \retval KNOT_ERROR
......@@ -164,7 +164,7 @@ int knot_dname_size(const knot_dname_t *name);
/*!
* \brief Returns wire size of the given domain name (expaned compression ptrs).
*
* \param dname Domain name to get the size of.
* \param name Domain name to get the size of.
* \param pkt Related packet (or NULL if unpacked)
*
* \retval size of the domain name
......@@ -178,45 +178,44 @@ int knot_dname_realsize(const knot_dname_t *name, const uint8_t *pkt);
* \param sub Domain name to be the possible subdomain.
* \param domain Domain name to be the possible parent domain.
*
* \retval <> 0 if \a sub is a subdomain of \a domain.
* \retval 0 otherwise.
* \retval true \a sub is a subdomain of \a domain.
* \retval false otherwise.
*/
bool knot_dname_is_sub(const knot_dname_t *sub, const knot_dname_t *domain);
/*!
* \brief Checks if the domain name is a wildcard.
*
* \param dname Domain name to check.
* \param name Domain name to check.
*
* \retval <> 0 if \a dname is a wildcard domain name.
* \retval 0 otherwise.
* \retval true if \a dname is a wildcard domain name.
* \retval false otherwise.
*/
int knot_dname_is_wildcard(const knot_dname_t *name);
bool knot_dname_is_wildcard(const knot_dname_t *name);
/*!
* \brief Returns the number of labels common for the two domain names (counted
* from the rightmost label.
*
* \param dname1 First domain name.
* \param dname2 Second domain name.
* \param d1 First domain name.
* \param d2 Second domain name.
*
* \return Number of labels common for the two domain names.
*/
int knot_dname_matched_labels(const knot_dname_t *dname1,
const knot_dname_t *dname2);
int knot_dname_matched_labels(const knot_dname_t *d1, const knot_dname_t *d2);
/*!
* \brief Replaces the suffix of given size in one domain name with other domain
* name.
*
* \param dname Domain name where to replace the suffix.
* \param name Domain name where to replace the suffix.
* \param labels Size of the suffix to be replaced.
* \param suffix New suffix to be used as a replacement.
*
* \return New domain name created by replacing suffix of \a dname of size
* \a size with \a suffix.
*/
knot_dname_t *knot_dname_replace_suffix(const knot_dname_t *dname,
knot_dname_t *knot_dname_replace_suffix(const knot_dname_t *name,
unsigned labels,
const knot_dname_t *suffix);
......@@ -229,9 +228,9 @@ knot_dname_t *knot_dname_replace_suffix(const knot_dname_t *dname,
*
* Sets the given pointer to NULL.
*
* \param dname Domain name to be destroyed.
* \param name Domain name to be destroyed.
*/
void knot_dname_free(knot_dname_t **dname);
void knot_dname_free(knot_dname_t **name);
/*!
* \brief Compares two domain names (case insensitive).
......@@ -250,10 +249,17 @@ int knot_dname_cmp(const knot_dname_t *d1, const knot_dname_t *d2);
*
* \todo No case insensitivity, flags...
*
* \warning Since it would be hard to catch errors, because negative value
* is also a good result, there are assertions that expect neither
* d1 or d2 to be NULL.
*
* \param d1 Domain name.
* \param d2 Domain name.
* \param pkt Packet wire related to names (or NULL).
* \return
*
* \retval 0 if they are identical
* \retval 1 if d1 > d2
* \retval -1 if d1 < d2
*/
int knot_dname_cmp_wire(const knot_dname_t *d1, const knot_dname_t *d2,
const uint8_t *pkt);
......@@ -264,16 +270,15 @@ int knot_dname_cmp_wire(const knot_dname_t *d1, const knot_dname_t *d2,
* \param d1 First domain name.
* \param d2 Second domain name.
*
* \retval < 0 if \a d1 goes before \a d2 in canonical order.
* \retval > 0 if \a d1 goes after \a d2 in canonical order.
* \retval 0 if the domain names are identical.
* \retval true if the domain names are identical
* \retval false if the domain names are NOT identical
*/
int knot_dname_is_equal(const knot_dname_t *d1, const knot_dname_t *d2);
bool knot_dname_is_equal(const knot_dname_t *d1, const knot_dname_t *d2);
/*!
* \brief Concatenates two domain names.
*
* \param d1 First domain name (will be modified).
* \param d1 First domain name (will be modified).
* \param d2 Second domain name (will not be modified).
*
* \return The concatenated domain name or NULL
......@@ -303,6 +308,12 @@ int knot_dname_labels(const uint8_t *name, const uint8_t *pkt);
/*!
* \brief Align name end-to-end and return number of common suffix labels.
*
* \param d1 Domain name.
* \param d1_labels Number of labels in d1.
* \param d2 Domain name.
* \param d2_labels Number of labels in d2.
* \param wire Packet wire related to names (or NULL).
*/
int knot_dname_align(const uint8_t **d1, uint8_t d1_labels,
const uint8_t **d2, uint8_t d2_labels,
......@@ -326,7 +337,6 @@ int knot_dname_align(const uint8_t **d1, uint8_t d1_labels,
* \param pkt Source name packet (NULL if not any).
*
* \retval KNOT_EOK if successful
* \retval KNOT_ESPACE when not enough memory.
* \retval KNOT_EINVAL on invalid parameters
*/
int knot_dname_lf(uint8_t *dst, const knot_dname_t *src, const uint8_t *pkt);
......
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