Commit f93b35e1 authored by Daniel Salzman's avatar Daniel Salzman

dname: change int to size_t return type where reasonable

parent 398cef3c
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> /* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
...@@ -159,7 +159,7 @@ conf_val_t conf_zone_get_txn( ...@@ -159,7 +159,7 @@ conf_val_t conf_zone_get_txn(
return val; return val;
} }
int dname_size = knot_dname_size(dname); size_t dname_size = knot_dname_size(dname);
// Try to get explicit value. // Try to get explicit value.
conf_db_get(conf, txn, C_ZONE, key1_name, dname, dname_size, &val); conf_db_get(conf, txn, C_ZONE, key1_name, dname, dname_size, &val);
...@@ -888,9 +888,9 @@ static int str_label( ...@@ -888,9 +888,9 @@ static int str_label(
const knot_dname_t *zone, const knot_dname_t *zone,
char *buff, char *buff,
size_t buff_len, size_t buff_len,
unsigned right_index) size_t right_index)
{ {
int labels = knot_dname_labels(zone, NULL); size_t labels = knot_dname_labels(zone, NULL);
// Check for root label of the root zone. // Check for root label of the root zone.
if (labels == 0 && right_index == 0) { if (labels == 0 && right_index == 0) {
...@@ -906,12 +906,12 @@ static int str_label( ...@@ -906,12 +906,12 @@ static int str_label(
// Compute the index from the left. // Compute the index from the left.
assert(labels > right_index); assert(labels > right_index);
unsigned index = labels - right_index - 1; size_t index = labels - right_index - 1;
// Create a dname from the single label. // Create a dname from the single label.
int prefix = (index > 0) ? knot_dname_prefixlen(zone, index, NULL) : 0; size_t prefix_len = knot_dname_prefixlen(zone, index, NULL);
unsigned label_len = *(zone + prefix); size_t label_len = *(zone + prefix_len);
memcpy(label, zone + prefix, 1 + label_len); memcpy(label, zone + prefix_len, 1 + label_len);
label[1 + label_len] = '\0'; label[1 + label_len] = '\0';
return str_zone(label, buff, buff_len); return str_zone(label, buff, buff_len);
......
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> /* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
...@@ -579,7 +579,7 @@ static int get_owner(uint8_t *out, size_t out_len, knot_dname_t *origin, ...@@ -579,7 +579,7 @@ static int get_owner(uint8_t *out, size_t out_len, knot_dname_t *origin,
assert(owner != NULL); assert(owner != NULL);
bool fqdn = false; bool fqdn = false;
int prefix_len = 0; size_t prefix_len = 0;
size_t owner_len = strlen(owner); size_t owner_len = strlen(owner);
if (owner_len > 0 && (owner_len != 1 || owner[0] != '@')) { if (owner_len > 0 && (owner_len != 1 || owner[0] != '@')) {
...@@ -599,7 +599,7 @@ static int get_owner(uint8_t *out, size_t out_len, knot_dname_t *origin, ...@@ -599,7 +599,7 @@ static int get_owner(uint8_t *out, size_t out_len, knot_dname_t *origin,
} }
prefix_len = knot_dname_size(out); prefix_len = knot_dname_size(out);
if (prefix_len <= 0) { if (prefix_len == 0) {
return KNOT_EINVAL; return KNOT_EINVAL;
} }
...@@ -609,8 +609,8 @@ static int get_owner(uint8_t *out, size_t out_len, knot_dname_t *origin, ...@@ -609,8 +609,8 @@ static int get_owner(uint8_t *out, size_t out_len, knot_dname_t *origin,
// Append the origin. // Append the origin.
if (!fqdn) { if (!fqdn) {
int origin_len = knot_dname_size(origin); size_t origin_len = knot_dname_size(origin);
if (origin_len <= 0 || origin_len > out_len - prefix_len) { if (origin_len == 0 || origin_len > out_len - prefix_len) {
return KNOT_EINVAL; return KNOT_EINVAL;
} }
memcpy(out + prefix_len, origin, origin_len); memcpy(out + prefix_len, origin, origin_len);
......
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> /* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
...@@ -170,7 +170,7 @@ void kasp_db_close(kasp_db_t **db) ...@@ -170,7 +170,7 @@ void kasp_db_close(kasp_db_t **db)
static knot_db_val_t make_key(keyclass_t kclass, const knot_dname_t *dname, const char *str) static knot_db_val_t make_key(keyclass_t kclass, const knot_dname_t *dname, const char *str)
{ {
size_t dnlen = (dname == NULL ? 0 : knot_dname_size((const knot_dname_t *)dname)); size_t dnlen = knot_dname_size(dname);
size_t slen = (str == NULL ? 0 : strlen(str) + 1); size_t slen = (str == NULL ? 0 : strlen(str) + 1);
knot_db_val_t res = { .len = 1 + dnlen + slen, .data = malloc(1 + dnlen + slen) }; knot_db_val_t res = { .len = 1 + dnlen + slen, .data = malloc(1 + dnlen + slen) };
if (res.data != NULL) { if (res.data != NULL) {
...@@ -195,7 +195,7 @@ static char *keyid_fromkey(const knot_db_val_t *key) ...@@ -195,7 +195,7 @@ static char *keyid_fromkey(const knot_db_val_t *key)
if (key->len < 2 || *(uint8_t *)key->data != KASPDBKEY_PARAMS) { if (key->len < 2 || *(uint8_t *)key->data != KASPDBKEY_PARAMS) {
return NULL; return NULL;
} }
size_t skip = knot_dname_size((const knot_dname_t *)key->data + 1); size_t skip = knot_dname_size((const uint8_t *)key->data + 1);
return (key->len < skip + 2 ? NULL : strdup(key->data + skip + 1)); return (key->len < skip + 2 ? NULL : strdup(key->data + skip + 1));
} }
......
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> /* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
...@@ -52,9 +52,7 @@ static size_t rrsig_rdata_header_size(const dnssec_key_t *key) ...@@ -52,9 +52,7 @@ static size_t rrsig_rdata_header_size(const dnssec_key_t *key)
// variable part // variable part
const uint8_t *signer = dnssec_key_get_dname(key); size += knot_dname_size(dnssec_key_get_dname(key));
assert(signer);
size += knot_dname_size(signer);
return size; return size;
} }
......
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> /* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
...@@ -152,14 +152,9 @@ int knot_create_nsec3_owner(uint8_t *out, size_t out_size, ...@@ -152,14 +152,9 @@ int knot_create_nsec3_owner(uint8_t *out, size_t out_size,
return KNOT_EINVAL; return KNOT_EINVAL;
} }
int owner_size = knot_dname_size(owner);
if (owner_size < 0) {
return KNOT_EINVAL;
}
dnssec_binary_t data = { dnssec_binary_t data = {
.data = (uint8_t *)owner, .data = (uint8_t *)owner,
.size = owner_size .size = knot_dname_size(owner)
}; };
dnssec_binary_t hash = { 0 }; dnssec_binary_t hash = { 0 };
......
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> /* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
...@@ -170,8 +170,7 @@ static void txn_init(txn_t *txn, knot_db_txn_t *db_txn, journal_t *j) ...@@ -170,8 +170,7 @@ static void txn_init(txn_t *txn, knot_db_txn_t *db_txn, journal_t *j)
static void txn_key_str(txn_t *txn, const knot_dname_t *zone, const char *key) static void txn_key_str(txn_t *txn, const knot_dname_t *zone, const char *key)
{ {
size_t zone_size = 0; size_t zone_size = knot_dname_size(zone);
if (zone != NULL) zone_size = knot_dname_size(zone);
txn->key.len = zone_size + DB_KEY_UNUSED_ZERO + strlen(key) + 1; txn->key.len = zone_size + DB_KEY_UNUSED_ZERO + strlen(key) + 1;
if (txn->key.len > 512) { if (txn->key.len > 512) {
txn->ret = KNOT_ERROR; txn->ret = KNOT_ERROR;
...@@ -184,8 +183,7 @@ static void txn_key_str(txn_t *txn, const knot_dname_t *zone, const char *key) ...@@ -184,8 +183,7 @@ static void txn_key_str(txn_t *txn, const knot_dname_t *zone, const char *key)
static void txn_key_2u32(txn_t *txn, const knot_dname_t *zone, uint32_t key1, uint32_t key2) static void txn_key_2u32(txn_t *txn, const knot_dname_t *zone, uint32_t key1, uint32_t key2)
{ {
size_t zone_size = 0; size_t zone_size = knot_dname_size(zone);
if (zone != NULL) zone_size = knot_dname_size(zone);
txn->key.len = zone_size + DB_KEY_UNUSED_ZERO + 2*sizeof(uint32_t); txn->key.len = zone_size + DB_KEY_UNUSED_ZERO + 2*sizeof(uint32_t);
if (txn->key.len > 512) { if (txn->key.len > 512) {
txn->ret = KNOT_ERROR; txn->ret = KNOT_ERROR;
...@@ -202,8 +200,7 @@ static void txn_key_2u32(txn_t *txn, const knot_dname_t *zone, uint32_t key1, ui ...@@ -202,8 +200,7 @@ static void txn_key_2u32(txn_t *txn, const knot_dname_t *zone, uint32_t key1, ui
static void txn_key_str_u32(txn_t *txn, const knot_dname_t *zone, const char *key1, uint32_t key2) static void txn_key_str_u32(txn_t *txn, const knot_dname_t *zone, const char *key1, uint32_t key2)
{ {
size_t zone_size = 0; size_t zone_size = knot_dname_size(zone);
if (zone != NULL) zone_size = knot_dname_size(zone);
txn->key.len = zone_size + DB_KEY_UNUSED_ZERO + strlen(key1) + 1 + sizeof(uint32_t); txn->key.len = zone_size + DB_KEY_UNUSED_ZERO + strlen(key1) + 1 + sizeof(uint32_t);
if (txn->key.len > 512) { if (txn->key.len > 512) {
txn->ret = KNOT_ERROR; txn->ret = KNOT_ERROR;
......
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> /* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
...@@ -66,10 +66,7 @@ static int deserialize_rrset(wire_ctx_t *wire, knot_rrset_t *rrset, long *phase) ...@@ -66,10 +66,7 @@ static int deserialize_rrset(wire_ctx_t *wire, knot_rrset_t *rrset, long *phase)
if (*phase == SERIALIZE_RRSET_INIT && wire_ctx_available(wire) > 0) { if (*phase == SERIALIZE_RRSET_INIT && wire_ctx_available(wire) > 0) {
// Read owner, rtype, rclass and RR count. // Read owner, rtype, rclass and RR count.
int size = knot_dname_size(wire->position); size_t size = knot_dname_size(wire->position);
if (size < 0) {
return size;
}
knot_dname_t *owner = knot_dname_copy_part(wire->position, size, NULL); knot_dname_t *owner = knot_dname_copy_part(wire->position, size, NULL);
if (owner == NULL || wire_ctx_available(wire) < size + 3 * sizeof(uint16_t)) { if (owner == NULL || wire_ctx_available(wire) < size + 3 * sizeof(uint16_t)) {
return KNOT_EMALF; return KNOT_EMALF;
......
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> /* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
...@@ -78,8 +78,8 @@ knot_dname_t *online_nsec_next(const knot_dname_t *dname, const knot_dname_t *ap ...@@ -78,8 +78,8 @@ knot_dname_t *online_nsec_next(const knot_dname_t *dname, const knot_dname_t *ap
// right aligned copy of the domain name // right aligned copy of the domain name
uint8_t copy[KNOT_DNAME_MAXLEN] = { 0 }; uint8_t copy[KNOT_DNAME_MAXLEN] = { 0 };
int dname_len = knot_dname_size(dname); size_t dname_len = knot_dname_size(dname);
int empty_len = sizeof(copy) - dname_len; size_t empty_len = sizeof(copy) - dname_len;
uint8_t *pos = copy + empty_len; uint8_t *pos = copy + empty_len;
memmove(pos, dname, dname_len); memmove(pos, dname, dname_len);
...@@ -92,7 +92,7 @@ knot_dname_t *online_nsec_next(const knot_dname_t *dname, const knot_dname_t *ap ...@@ -92,7 +92,7 @@ knot_dname_t *online_nsec_next(const knot_dname_t *dname, const knot_dname_t *ap
} }
// find apex position in the buffer // find apex position in the buffer
int apex_len = knot_dname_size(apex); size_t apex_len = knot_dname_size(apex);
const uint8_t *apex_pos = copy + sizeof(copy) - apex_len; const uint8_t *apex_pos = copy + sizeof(copy) - apex_len;
assert(knot_dname_cmp(apex, apex_pos) == 0); assert(knot_dname_cmp(apex, apex_pos) == 0);
......
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> /* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
...@@ -73,7 +73,7 @@ static int dname_cname_synth(const knot_rrset_t *dname_rr, ...@@ -73,7 +73,7 @@ static int dname_cname_synth(const knot_rrset_t *dname_rr,
/* Replace last labels of qname with DNAME. */ /* Replace last labels of qname with DNAME. */
const knot_dname_t *dname_wire = dname_rr->owner; const knot_dname_t *dname_wire = dname_rr->owner;
const knot_dname_t *dname_tgt = knot_dname_target(&dname_rr->rrs); const knot_dname_t *dname_tgt = knot_dname_target(&dname_rr->rrs);
int labels = knot_dname_labels(dname_wire, NULL); size_t labels = knot_dname_labels(dname_wire, NULL);
knot_dname_t *cname = knot_dname_replace_suffix(qname, labels, dname_tgt); knot_dname_t *cname = knot_dname_replace_suffix(qname, labels, dname_tgt);
if (cname == NULL) { if (cname == NULL) {
knot_dname_free(&owner_copy, mm); knot_dname_free(&owner_copy, mm);
...@@ -81,7 +81,7 @@ static int dname_cname_synth(const knot_rrset_t *dname_rr, ...@@ -81,7 +81,7 @@ static int dname_cname_synth(const knot_rrset_t *dname_rr,
} }
/* Store DNAME into RDATA. */ /* Store DNAME into RDATA. */
int cname_size = knot_dname_size(cname); size_t cname_size = knot_dname_size(cname);
uint8_t cname_rdata[cname_size]; uint8_t cname_rdata[cname_size];
memcpy(cname_rdata, cname, cname_size); memcpy(cname_rdata, cname, cname_size);
knot_dname_free(&cname, NULL); knot_dname_free(&cname, NULL);
......
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> /* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
...@@ -117,8 +117,8 @@ static const zone_node_t *nsec3_encloser(const zone_node_t *closest) ...@@ -117,8 +117,8 @@ static const zone_node_t *nsec3_encloser(const zone_node_t *closest)
static const knot_dname_t *get_next_closer(const knot_dname_t *closest_encloser, static const knot_dname_t *get_next_closer(const knot_dname_t *closest_encloser,
const knot_dname_t *name) const knot_dname_t *name)
{ {
int ce_labels = knot_dname_labels(closest_encloser, NULL); size_t ce_labels = knot_dname_labels(closest_encloser, NULL);
int qname_labels = knot_dname_labels(name, NULL); size_t qname_labels = knot_dname_labels(name, NULL);
// the common labels should match // the common labels should match
assert(knot_dname_matched_labels(closest_encloser, name) == ce_labels); assert(knot_dname_matched_labels(closest_encloser, name) == ce_labels);
......
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> /* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
...@@ -818,7 +818,7 @@ int zone_contents_find_dname(const zone_contents_t *zone, ...@@ -818,7 +818,7 @@ int zone_contents_find_dname(const zone_contents_t *zone,
assert(!node && prev); assert(!node && prev);
node = prev; node = prev;
int matched_labels = knot_dname_matched_labels(node->owner, name); size_t matched_labels = knot_dname_matched_labels(node->owner, name);
while (matched_labels < knot_dname_labels(node->owner, NULL)) { while (matched_labels < knot_dname_labels(node->owner, NULL)) {
node = node->parent; node = node->parent;
assert(node); assert(node);
......
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> /* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
...@@ -306,13 +306,13 @@ static int check_rrsig_rdata(sem_handler_t *handler, ...@@ -306,13 +306,13 @@ static int check_rrsig_rdata(sem_handler_t *handler,
/* label number at the 2nd index should be same as owner's */ /* label number at the 2nd index should be same as owner's */
uint8_t labels_rdata = knot_rrsig_labels(rrsig, rr_pos); uint8_t labels_rdata = knot_rrsig_labels(rrsig, rr_pos);
int tmp = knot_dname_labels(rrset->owner, NULL) - labels_rdata; size_t tmp = knot_dname_labels(rrset->owner, NULL) - labels_rdata;
if (tmp != 0) { if (tmp != 0) {
/* if name has wildcard, label must not be included */ /* if name has wildcard, label must not be included */
if (!knot_dname_is_wildcard(rrset->owner)) { if (!knot_dname_is_wildcard(rrset->owner)) {
handler->cb(handler, zone, node, SEM_ERR_RRSIG_RDATA_LABELS, handler->cb(handler, zone, node, SEM_ERR_RRSIG_RDATA_LABELS,
info_str); info_str);
} else if (abs(tmp) != 1) { } else if (tmp != 1) {
handler->cb(handler, zone, node, SEM_ERR_RRSIG_RDATA_LABELS, handler->cb(handler, zone, node, SEM_ERR_RRSIG_RDATA_LABELS,
info_str); info_str);
} }
......
/* Copyright (C) 2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz> /* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
...@@ -107,8 +107,8 @@ knot_dname_t *knot_dname_parse(const uint8_t *pkt, size_t *pos, size_t maxpos, ...@@ -107,8 +107,8 @@ knot_dname_t *knot_dname_parse(const uint8_t *pkt, size_t *pos, size_t maxpos,
} }
/* Calculate decompressed length. */ /* Calculate decompressed length. */
int decompressed_len = knot_dname_realsize(name, pkt); size_t decompressed_len = knot_dname_realsize(name, pkt);
if (decompressed_len < 1) { if (decompressed_len == 0) {
return NULL; return NULL;
} }
...@@ -159,7 +159,7 @@ int knot_dname_to_wire(uint8_t *dst, const knot_dname_t *src, size_t maxlen) ...@@ -159,7 +159,7 @@ int knot_dname_to_wire(uint8_t *dst, const knot_dname_t *src, size_t maxlen)
return KNOT_EINVAL; return KNOT_EINVAL;
} }
int len = knot_dname_size(src); size_t len = knot_dname_size(src);
if (len > maxlen) { if (len > maxlen) {
return KNOT_ESPACE; return KNOT_ESPACE;
} }
...@@ -204,10 +204,7 @@ char *knot_dname_to_str(char *dst, const knot_dname_t *name, size_t maxlen) ...@@ -204,10 +204,7 @@ char *knot_dname_to_str(char *dst, const knot_dname_t *name, size_t maxlen)
return NULL; return NULL;
} }
int dname_size = knot_dname_size(name); size_t dname_size = knot_dname_size(name);
if (dname_size <= 0) {
return NULL;
}
/* Check the size for len(dname) + 1 char termination. */ /* Check the size for len(dname) + 1 char termination. */
size_t alloc_size = (dst == NULL) ? dname_size + 1 : maxlen; size_t alloc_size = (dst == NULL) ? dname_size + 1 : maxlen;
...@@ -464,53 +461,62 @@ int knot_dname_to_lower(knot_dname_t *name) ...@@ -464,53 +461,62 @@ int knot_dname_to_lower(knot_dname_t *name)
} }
_public_ _public_
int knot_dname_size(const knot_dname_t *name) size_t knot_dname_size(const knot_dname_t *name)
{ {
if (name == NULL) if (name == NULL) {
return KNOT_EINVAL; return 0;
}
/* Count name size without terminal label. */ /* Count name size without terminal label. */
int len = 0; size_t len = 0;
while (*name != '\0' && !knot_wire_is_pointer(name)) { while (*name != '\0' && !knot_wire_is_pointer(name)) {
uint8_t lblen = *name + 1; uint8_t lblen = *name + 1;
len += lblen; len += lblen;
name += lblen; name += lblen;
} }
/* Compression pointer is 2 octets. */ if (knot_wire_is_pointer(name)) {
if (knot_wire_is_pointer(name)) /* Add 2-octet compression pointer. */
return len + 2; return len + 2;
} else {
return len + 1; /* Add 1-octet terminal label. */
return len + 1;
}
} }
_public_ _public_
int knot_dname_realsize(const knot_dname_t *name, const uint8_t *pkt) size_t knot_dname_realsize(const knot_dname_t *name, const uint8_t *pkt)
{ {
/* Add zero label size for FQDN. */ if (name == NULL) {
return knot_dname_prefixlen(name, KNOT_DNAME_MAXLABELS, pkt) + 1; return 0;
}
/* Seek first real label occurrence. */
name = knot_wire_seek_label(name, pkt);
size_t len = 0;
while (*name != '\0') {
len += *name + 1;
name = knot_wire_next_label(name, pkt);
}
/* Add 1-octet terminal label. */
return len + 1;
} }
_public_ _public_
bool knot_dname_is_sub(const knot_dname_t *sub, const knot_dname_t *domain) bool knot_dname_is_sub(const knot_dname_t *sub, const knot_dname_t *domain)
{ {
if (sub == domain) if (sub == NULL || domain == NULL || sub == 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);
if (sub_l < 0 || domain_l < 0)
return false; return false;
}
assert(sub_l >= 0 && sub_l <= KNOT_DNAME_MAXLABELS); /* Subdomain must have more labels than parent. */
assert(domain_l >= 0 && domain_l <= KNOT_DNAME_MAXLABELS); size_t sub_l = knot_dname_labels(sub, NULL);
size_t domain_l = knot_dname_labels(domain, NULL);
/* Subdomain must have more labels as parent. */ if (sub_l <= domain_l) {
if (sub_l <= domain_l)
return false; return false;
}
/* Align end-to-end to common suffix. */ /* Align end-to-end to common suffix. */
int common = knot_dname_align(&sub, sub_l, &domain, domain_l, NULL); int common = knot_dname_align(&sub, sub_l, &domain, domain_l, NULL);
...@@ -518,8 +524,9 @@ bool knot_dname_is_sub(const knot_dname_t *sub, const knot_dname_t *domain) ...@@ -518,8 +524,9 @@ bool knot_dname_is_sub(const knot_dname_t *sub, const knot_dname_t *domain)
/* Compare common suffix. */ /* Compare common suffix. */
while (common > 0) { while (common > 0) {
/* Compare label. */ /* Compare label. */
if (!label_is_equal(sub, domain)) if (!label_is_equal(sub, domain)) {
return false; return false;
}
/* Next label. */ /* Next label. */
sub = knot_wire_next_label(sub, NULL); sub = knot_wire_next_label(sub, NULL);
domain = knot_wire_next_label(domain, NULL); domain = knot_wire_next_label(domain, NULL);
...@@ -537,36 +544,34 @@ bool knot_dname_in(const knot_dname_t *domain, const knot_dname_t *sub) ...@@ -537,36 +544,34 @@ bool knot_dname_in(const knot_dname_t *domain, const knot_dname_t *sub)
_public_ _public_
bool knot_dname_is_wildcard(const knot_dname_t *name) bool knot_dname_is_wildcard(const knot_dname_t *name)
{ {
assert(name != NULL); if (name == NULL) {
return false;
}
return name[0] == 1 && name[1] == '*'; return name[0] == 1 && name[1] == '*';
} }
_public_ _public_
int knot_dname_matched_labels(const knot_dname_t *d1, const knot_dname_t *d2) size_t knot_dname_matched_labels(const knot_dname_t *d1, const knot_dname_t *d2)
{ {
if (d1 == NULL || d2 == NULL)
return KNOT_EINVAL;
/* Count labels. */ /* Count labels. */
int l1 = knot_dname_labels(d1, NULL); size_t l1 = knot_dname_labels(d1, NULL);
int l2 = knot_dname_labels(d2, NULL); size_t l2 = knot_dname_labels(d2, NULL);
if (l1 == 0 || l2 == 0) {
if (l1 < 0 || l2 < 0) return 0;
return KNOT_EINVAL; }
assert(l1 >= 0 && l1 <= KNOT_DNAME_MAXLABELS);
assert(l2 >= 0 && l2 <= KNOT_DNAME_MAXLABELS);
/* Align end-to-end to common suffix. */ /* Align end-to-end to common suffix. */
int common = knot_dname_align(&d1, l1, &d2, l2, NULL); int common = knot_dname_align(&d1, l1, &d2, l2, NULL);
/* Count longest chain leading to root label. */ /* Count longest chain leading to root label. */
int matched = 0; size_t matched = 0;
while (common > 0) { while (common > 0) {
if (label_is_equal(d1, d2)) if (label_is_equal(d1, d2)) {
++matched; ++matched;
else } else {
matched = 0; /* Broken chain. */ matched = 0; /* Broken chain. */
}
/* Next label. */ /* Next label. */
d1 = knot_wire_next_label(d1, NULL); d1 = knot_wire_next_label(d1, NULL);
...@@ -581,24 +586,29 @@ _public_ ...@@ -581,24 +586,29 @@ _public_
knot_dname_t *knot_dname_replace_suffix(const knot_dname_t *name, unsigned labels, knot_dname_t *knot_dname_replace_suffix(const knot_dname_t *name, unsigned labels,
const knot_dname_t *suffix) const knot_dname_t *suffix)
{ {
if (name == NULL) if (name == NULL) {
return NULL; return NULL;
}
/* Calculate prefix and suffix lengths. */ /* Calculate prefix and suffix lengths. */
int dname_lbs = knot_dname_labels(name, NULL); size_t dname_lbs = knot_dname_labels(name, NULL);
assert(dname_lbs >= labels); if (dname_lbs < labels) {
unsigned prefix_lbs = dname_lbs - labels; return NULL;
}
size_t prefix_lbs = dname_lbs - labels;
int prefix_len = knot_dname_prefixlen(name, prefix_lbs, NULL); size_t prefix_len = knot_dname_prefixlen(name, prefix_lbs, NULL);
int suffix_len = knot_dname_size(suffix); size_t suffix_len = knot_dname_size(suffix);
if (prefix_len < 0 || suffix_len < 0) if (prefix_len == 0 || suffix_len == 0) {
return NULL; return NULL;
}
/* Create target name. */ /* Create target name. */
int new_len = prefix_len + suffix_len; size_t new_len = prefix_len + suffix_len;
knot_dname_t *out = malloc(new_len); knot_dname_t *out = malloc(new_len);
if (out == NULL) if (out == NULL) {
return NULL; return NULL;
}
/* Copy prefix. */ /* Copy prefix. */
uint8_t *dst = out; uint8_t *dst = out;
...@@ -718,41 +728,46 @@ knot_dname_t *knot_dname_cat(knot_dname_t *d1, const knot_dname_t *d2) ...@@ -718,41 +728,46 @@ knot_dname_t *knot_dname_cat(knot_dname_t *d1, const knot_dname_t *d2)
} }
_public_ _public_
int knot_dname_prefixlen(const uint8_t *name, unsigned nlabels, const uint8_t *pkt) size_t knot_dname_prefixlen(const uint8_t *name, unsigned nlabels, const uint8_t *pkt)
{ {
if (name == NULL) if (name == NULL) {
return KNOT_EINVAL; return 0;
}
/* Zero labels means no prefix. */