Commit fedbc3a9 authored by Grigorii Demidov's avatar Grigorii Demidov Committed by Petr Špaček

lib/utils: merge kr_rrkey() and kr_rrkey2() functions

The old kr_rrkey() was used only on one place (and incorrectly) so now
we are replacing both copies with single implementation for general
resolver and root zone import.
It should not make any practical difference.
parent 64050118
......@@ -485,6 +485,18 @@ static inline void pool_release(struct worker_ctx *worker, struct mempool *mp)
}
}
/** Create a key for an outgoing subrequest: qname, qclass, qtype.
* @param key Destination buffer for key size, MUST be SUBREQ_KEY_LEN or larger.
* @return key length if successful or an error
*/
static const size_t SUBREQ_KEY_LEN = KR_RRKEY_LEN;
static int subreq_key(char *dst, knot_pkt_t *pkt)
{
assert(pkt);
return kr_rrkey(dst, knot_pkt_qclass(pkt), knot_pkt_qname(pkt),
knot_pkt_qtype(pkt), knot_pkt_qtype(pkt));
}
/** Create and initialize a request_ctx (on a fresh mempool).
*
* handle and addr point to the source of the request, and they are NULL
......@@ -1404,35 +1416,6 @@ static int timer_start(struct session *session, uv_timer_cb cb,
return 0;
}
/** Create a key for an outgoing subrequest: qname, qclass, qtype.
* @param key Destination buffer for key size, MUST be SUBREQ_KEY_LEN or larger.
* @return key length if successful or an error
*/
static int subreq_key(char *dst, knot_pkt_t *pkt)
{
assert(dst && pkt);
const char * const dst_begin = dst;
int ret = knot_dname_to_wire((uint8_t *)dst, knot_pkt_qname(pkt), KNOT_DNAME_MAXLEN);
if (ret <= 0) {
assert(false); /*EINVAL*/
return kr_error(ret);
}
knot_dname_to_lower((knot_dname_t *)dst);
dst += ret;
const uint16_t qclass = knot_pkt_qclass(pkt);
memcpy(dst, &qclass, sizeof(qclass));
dst += sizeof(qclass);
const uint16_t qtype = knot_pkt_qtype(pkt);
memcpy(dst, &qtype, sizeof(qtype));
dst += sizeof(qtype);
return dst - dst_begin;
}
static const size_t SUBREQ_KEY_LEN = KNOT_DNAME_MAXLEN + 2 * sizeof(uint16_t);
static void subreq_finalize(struct qr_task *task, const struct sockaddr *packet_source, knot_pkt_t *pkt)
{
/* Close pending timer */
......
......@@ -176,13 +176,13 @@ static int zi_put_supplementary(struct zone_import_ctx *z_import,
{
assert(supp_type != KNOT_RRTYPE_RRSIG);
char key[KR_RRKEY2_LEN];
int err = kr_rrkey2(key, class, owner, supp_type, supp_type);
char key[KR_RRKEY_LEN];
int err = kr_rrkey(key, class, owner, supp_type, supp_type);
if (err <= 0) {
return -1;
}
knot_rrset_t *additional_rr = map_get(&z_import->rrset_sorted, key);
err = kr_rrkey2(key, class, owner, KNOT_RRTYPE_RRSIG, supp_type);
err = kr_rrkey(key, class, owner, KNOT_RRTYPE_RRSIG, supp_type);
if (err <= 0) {
return -1;
}
......@@ -255,7 +255,7 @@ static int zi_rrset_import(zone_import_ctx_t *z_import, knot_rrset_t *rr)
struct kr_request *request = worker_task_request(task);
struct kr_rplan *rplan = &request->rplan;
struct kr_query *qry = kr_rplan_push(rplan, NULL, dname, rrclass, rrtype);
char key[KR_RRKEY2_LEN];
char key[KR_RRKEY_LEN];
int err = 0;
int state = KR_STATE_FAIL;
bool origin_is_owner = knot_dname_is_equal(rr->owner, z_import->origin);
......@@ -283,7 +283,7 @@ static int zi_rrset_import(zone_import_ctx_t *z_import, knot_rrset_t *rr)
}
zi_rrset_mark_as_imported(rr);
err = kr_rrkey2(key, rr->rclass, rr->owner, KNOT_RRTYPE_RRSIG, rr->type);
err = kr_rrkey(key, rr->rclass, rr->owner, KNOT_RRTYPE_RRSIG, rr->type);
if (err <= 0) {
goto cleanup;
}
......@@ -413,9 +413,9 @@ static void zi_zone_process(uv_timer_t* handle)
/* TA have been found, zone is secured.
* DNSKEY must be somewhere amongst the imported records. Find it.
* TODO - For those zones that provenly do not have TA this step must be skipped. */
char key[KR_RRKEY2_LEN];
int err = kr_rrkey2(key, KNOT_CLASS_IN, z_import->origin,
KNOT_RRTYPE_DNSKEY, KNOT_RRTYPE_DNSKEY);
char key[KR_RRKEY_LEN];
int err = kr_rrkey(key, KNOT_CLASS_IN, z_import->origin,
KNOT_RRTYPE_DNSKEY, KNOT_RRTYPE_DNSKEY);
if (err <= 0) {
failed = 1;
goto finish;
......@@ -563,11 +563,11 @@ static int zi_record_store(zs_scanner_t *s)
return -1;
}
char key[KR_RRKEY2_LEN];
char key[KR_RRKEY_LEN];
uint16_t additional_key_field = kr_rrset_type_maysig(new_rr);
ret = kr_rrkey2(key, new_rr->rclass, new_rr->owner, new_rr->type,
additional_key_field);
ret = kr_rrkey(key, new_rr->rclass, new_rr->owner, new_rr->type,
additional_key_field);
if (ret <= 0) {
kr_log_error("[zscanner] line %lu: error constructing rrkey\n", s->line_counter);
return -1;
......
......@@ -581,27 +581,8 @@ int kr_bitcmp(const char *a, const char *b, int bits)
return ret;
}
int kr_rrkey(char *key, const knot_dname_t *owner, uint16_t type, uint8_t rank)
{
if (!key || !owner) {
return kr_error(EINVAL);
}
key[0] = (rank << 2) | 0x01; /* Must be non-zero */
uint8_t *key_buf = (uint8_t *)key + 1;
int ret = knot_dname_to_wire(key_buf, owner, KNOT_DNAME_MAXLEN);
if (ret <= 0) {
return ret;
}
knot_dname_to_lower(key_buf);
key_buf += ret - 1;
/* Must convert to string, as the key must not contain 0x00 */
ret = u16tostr(key_buf, type);
key_buf[ret] = '\0';
return (char *)&key_buf[ret] - key;
}
int kr_rrkey2(char *key, uint16_t class, const knot_dname_t *owner,
uint16_t type, uint16_t additional)
int kr_rrkey(char *key, uint16_t class, const knot_dname_t *owner,
uint16_t type, uint16_t additional)
{
if (!key || !owner) {
return kr_error(EINVAL);
......
......@@ -286,22 +286,10 @@ static inline uint8_t KEY_FLAG_RANK(const char *key)
static inline bool KEY_COVERING_RRSIG(const char *key)
{ return ((uint8_t)(key[0])) & KEY_FLAG_RRSIG; }
/* Stash key = {[1] flags, [1-255] owner, [5] type, [1] \x00 } */
#define KR_RRKEY_LEN (9 + KNOT_DNAME_MAXLEN)
/** Create unique null-terminated string key for RR.
* @param key Destination buffer for key size, MUST be KR_RRKEY_LEN or larger.
* @param owner RR owner domain name.
* @param type RR type.
* @param rank RR rank (8 bit tag usable for anything).
* @return key length if successful or an error
* */
KR_EXPORT
int kr_rrkey(char *key, const knot_dname_t *owner, uint16_t type, uint8_t rank);
/* Stash key = {[5] class, [1-255] owner, [5] type, [5] additional, [1] \x00 } */
#define KR_RRKEY2_LEN (16 + KNOT_DNAME_MAXLEN)
#define KR_RRKEY_LEN (16 + KNOT_DNAME_MAXLEN)
/** Create unique null-terminated string key for RR.
* @param key Destination buffer for key size, MUST be KR_RRKEY2_LEN or larger.
* @param key Destination buffer for key size, MUST be KR_RRKEY_LEN or larger.
* @param class RR class.
* @param owner RR owner name.
* @param type RR type.
......@@ -310,8 +298,8 @@ int kr_rrkey(char *key, const knot_dname_t *owner, uint16_t type, uint8_t rank);
* @return key length if successful or an error
* */
KR_EXPORT
int kr_rrkey2(char *key, uint16_t class, const knot_dname_t *owner,
uint16_t type, uint16_t additional);
int kr_rrkey(char *key, uint16_t class, const knot_dname_t *owner,
uint16_t type, uint16_t additional);
/** @internal Add RRSet copy to ranked RR array. */
KR_EXPORT
......
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