Commit b9e3f901 authored by Dominik Taborsky's avatar Dominik Taborsky

ddns: remove redundant code, have changeset add removal regardless of addition

parent 317f2810
......@@ -29,12 +29,14 @@
static int add_rr_to_contents(zone_contents_t *z, knot_rrset_t **soa, const knot_rrset_t *rrset)
{
if (rrset->type == KNOT_RRTYPE_SOA) {
if (*soa != NULL) {
knot_rrset_free(soa, NULL);
}
*soa = knot_rrset_copy(rrset, NULL);
if (*soa == NULL) {
*soa = knot_rrset_copy(rrset, NULL);
if (*soa == NULL) {
return KNOT_ENOMEM;
}
return KNOT_ENOMEM;
}
/* Do not add SOAs into actual contents. */
return KNOT_EOK;
}
......@@ -167,7 +169,7 @@ static bool need_to_insert(zone_contents_t *counterpart, const knot_rrset_t *rr)
}
// Subtract the data from node's RRSet.
int ret = knot_rdataset_subtract(&node->rrs->rrs, &rr->rrs, NULL);
int ret = knot_rdataset_subtract(&node_rr.rrs, &rr->rrs, NULL);
if (ret != KNOT_EOK) {
return true;
}
......@@ -281,11 +283,13 @@ int changeset_add_rrset(changeset_t *ch, const knot_rrset_t *rrset, bool check_r
int changeset_rem_rrset(changeset_t *ch, const knot_rrset_t *rrset, bool check_redundancy)
{
if (!check_redundancy || need_to_insert(ch->add, rrset)) {
return add_rr_to_contents(ch->remove, &ch->soa_from, rrset);
} else {
return KNOT_EOK;
/* Check if there's any addition and remove that, then add this
* removal anyway. */
if (check_redundancy) {
need_to_insert(ch->add, rrset);
}
return add_rr_to_contents(ch->remove, &ch->soa_from, rrset);
}
int changeset_merge(changeset_t *ch1, const changeset_t *ch2)
......
......@@ -224,54 +224,6 @@ static int process_prereq(const knot_rrset_t *rrset, uint16_t qclass,
/* --------------------------- DDNS processing ------------------------------ */
/*!< \brief Checks whether RR was already removed. */
static bool removed_rr(zone_contents_t *z, const knot_rrset_t *rr)
{
const zone_node_t *n = zone_contents_find_node_for_rr(z, rr);
if (n == NULL) {
return false;
}
const knot_rdataset_t *rrs = node_rdataset(n, rr->type);
if (rrs == NULL) {
return false;
}
return knot_rdataset_eq(rrs, &rr->rrs);
}
/*!< \brief Checks whether any CNAME RR under dname was added. */
static bool cname_added(const changeset_t *changeset, const knot_dname_t *d)
{
const zone_node_t *n = zone_contents_find_node(changeset->add, d);
return n && node_rrtype_exists(n, KNOT_RRTYPE_CNAME);
}
/*!< \brief Checks whether any RR under given name was added. */
static bool name_added(const changeset_t *changeset, const knot_dname_t *d)
{
return zone_contents_find_node(changeset->add, d);
}
/*!< \brief Removes RR from changeset, full equality check. */
static void remove_rr_from_changeset(zone_contents_t *z, const knot_rrset_t *rr)
{
zone_node_t *n = zone_contents_find_node_for_rr(z, rr);
if (n == NULL) {
return;
}
knot_rdataset_t *rrs = node_rdataset(n, rr->type);
if (rrs == NULL) {
return;
}
knot_rdataset_subtract(rrs, &rr->rrs, NULL);
if (rrs->rr_count == 0) {
node_remove_rdataset(n, rr->type);
}
}
/* --------------------- true/false helper functions ------------------------ */
static inline bool is_addition(const knot_rrset_t *rr)
......@@ -306,42 +258,6 @@ static bool should_replace(const knot_rrset_t *rrset)
rrset->type == KNOT_RRTYPE_NSEC3PARAM;
}
/*!< \brief Returns true if node will be empty after update application. */
static bool node_will_be_empty(const zone_node_t *node, knot_dname_t *owner,
const changeset_t *changeset)
{
if (node == NULL && name_added(changeset, owner)) {
// Node created in update.
return false;
}
if (node == NULL) {
// Node empty.
return true;
}
for (uint16_t i = 0; i < node->rrset_count; ++i) {
knot_rrset_t node_rrset = node_rrset_at(node, i);
knot_rrset_t node_rr;
knot_rrset_init(&node_rr, node->owner, node_rrset.type, KNOT_CLASS_IN);
for (uint16_t j = 0; j < node_rrset.rrs.rr_count; ++j) {
knot_rdata_t *add_rr = knot_rdataset_at(&node_rrset.rrs, j);
int ret = knot_rdataset_add(&node_rr.rrs, add_rr, NULL);
if (ret != KNOT_EOK) {
return false;
}
if (!removed_rr(changeset->remove, &node_rr)) {
// One of the RRs from node was not removed.
knot_rdataset_clear(&node_rr.rrs, NULL);
return false;
}
knot_rdataset_clear(&node_rr.rrs, NULL);
}
}
return true;
}
/*!< \brief Returns true if node contains given RR in its RRSets. */
static bool node_contains_rr(const zone_node_t *node,
const knot_rrset_t *rr)
......@@ -360,14 +276,8 @@ static bool node_contains_rr(const zone_node_t *node,
/*!< \brief Returns true if CNAME is in this node. */
static bool adding_to_cname(const knot_dname_t *owner,
const zone_node_t *node,
changeset_t *changeset)
const zone_node_t *node)
{
if (cname_added(changeset, owner)) {
// Added a CNAME in this update.
return true;
}
if (node == NULL) {
// Node did not exist before update.
return false;
......@@ -379,11 +289,6 @@ static bool adding_to_cname(const knot_dname_t *owner,
return false;
}
if (removed_rr(changeset->remove, &cname)) {
// Node did contain CNAME, but it was removed in this update.
return false;
}
// CNAME present
return true;
}
......@@ -490,7 +395,7 @@ static int process_add_cname(const zone_node_t *node,
}
return add_rr_to_chgset(rr, update);
} else if (!node_will_be_empty(node, rr->owner, &update->change)) {
} else if (!node_empty(node)) {
// Other occupied node => ignore.
return KNOT_EOK;
} else {
......@@ -513,7 +418,7 @@ static int process_add_nsec3param(const zone_node_t *node,
return KNOT_EDENIED;
}
knot_rrset_t param = node_rrset(node, KNOT_RRTYPE_NSEC3PARAM);
if (knot_rrset_empty(&param) || removed_rr(update->change.remove, &param)) {
if (knot_rrset_empty(&param)) {
return add_rr_to_chgset(rr, update);
}
......@@ -544,19 +449,7 @@ static int process_add_soa(const zone_node_t *node,
return KNOT_EOK;
}
// Store SOA copy into changeset.
if (update->change.soa_to) {
// Discard previous SOA - "There can be only one!"
knot_rrset_free(&update->change.soa_to, NULL);
}
knot_rrset_t *soa_cpy = knot_rrset_copy(rr, NULL);
if (soa_cpy == NULL) {
return KNOT_ENOMEM;
}
update->change.soa_to = soa_cpy;
return KNOT_EOK;
return add_rr_to_chgset(rr, update);
}
/*!< \brief Adds normal RR, ignores when CNAME exists in node. */
......@@ -564,15 +457,13 @@ static int process_add_normal(const zone_node_t *node,
const knot_rrset_t *rr,
zone_update_t *update)
{
if (adding_to_cname(rr->owner, node, &update->change)) {
if (adding_to_cname(rr->owner, node)) {
// Adding RR to CNAME node, ignore.
return KNOT_EOK;
}
if (node && node_contains_rr(node, rr)) {
// Adding existing RR, remove removal from changeset if it's there.
remove_rr_from_changeset(update->change.remove, rr);
// And ignore.
// Adding existing RR, ignore.
return KNOT_EOK;
}
......
......@@ -54,8 +54,8 @@ int main(int argc, char *argv[])
ok(changeset_size(ch) == 1, "changeset: size add");
ret = changeset_rem_rrset(ch, apex_txt_rr, true);
ok(ret == KNOT_EOK, "changeset: rem RRSet");
ok(changeset_size(ch) == 0, "changeset: size remove");
ok(changeset_empty(ch), "changeset: empty");
ok(changeset_size(ch) == 1, "changeset: size remove");
ok(!changeset_empty(ch), "changeset: empty");
changeset_add_rrset(ch, apex_txt_rr, true);
// Add another RR to node.
......
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