]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Small refactor lib/dns/zone.c
authorMatthijs Mekking <matthijs@isc.org>
Tue, 23 Mar 2021 08:57:43 +0000 (09:57 +0100)
committerMatthijs Mekking <matthijs@isc.org>
Tue, 13 Apr 2021 12:19:52 +0000 (14:19 +0200)
Introduce some macros that can be reused in 'zone_load_soa_rr()' and
'zone_get_from_db()' to make those functions more readable.

(cherry picked from commit 8fcbef242392280191e9347e17b005ae70133883)

lib/dns/zone.c

index 4c8c8fe7b5426be13cd1736823866276d3cb57b1..01bc1c04a2f51eaa2d28201a2fbc8250a08cbc49 100644 (file)
@@ -5412,43 +5412,42 @@ invalidate_rdataset:
        return (result);
 }
 
+#define SET_IF_NOT_NULL(obj, val) \
+       if (obj != NULL) {        \
+               *obj = val;       \
+       }
+
+#define SET_SOA_VALUES(soattl_v, serial_v, refresh_v, retry_v, expire_v, \
+                      minimum_v)                                        \
+       {                                                                \
+               SET_IF_NOT_NULL(soattl, soattl_v);                       \
+               SET_IF_NOT_NULL(serial, serial_v);                       \
+               SET_IF_NOT_NULL(refresh, refresh_v);                     \
+               SET_IF_NOT_NULL(retry, retry_v);                         \
+               SET_IF_NOT_NULL(expire, expire_v);                       \
+               SET_IF_NOT_NULL(minimum, minimum_v);                     \
+       }
+
+#define CLR_SOA_VALUES()                          \
+       {                                         \
+               SET_SOA_VALUES(0, 0, 0, 0, 0, 0); \
+       }
+
 static isc_result_t
 zone_load_soa_rr(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
                 unsigned int *soacount, uint32_t *soattl, uint32_t *serial,
                 uint32_t *refresh, uint32_t *retry, uint32_t *expire,
                 uint32_t *minimum) {
        isc_result_t result;
-       unsigned int count;
+       unsigned int count = 0;
        dns_rdataset_t rdataset;
        dns_rdata_t rdata = DNS_RDATA_INIT;
-       dns_rdata_soa_t soa;
 
        dns_rdataset_init(&rdataset);
        result = dns_db_findrdataset(db, node, version, dns_rdatatype_soa,
                                     dns_rdatatype_none, 0, &rdataset, NULL);
        if (result == ISC_R_NOTFOUND) {
                INSIST(!dns_rdataset_isassociated(&rdataset));
-               if (soacount != NULL) {
-                       *soacount = 0;
-               }
-               if (soattl != NULL) {
-                       *soattl = 0;
-               }
-               if (serial != NULL) {
-                       *serial = 0;
-               }
-               if (refresh != NULL) {
-                       *refresh = 0;
-               }
-               if (retry != NULL) {
-                       *retry = 0;
-               }
-               if (expire != NULL) {
-                       *expire = 0;
-               }
-               if (minimum != NULL) {
-                       *minimum = 0;
-               }
                result = ISC_R_SUCCESS;
                goto invalidate_rdataset;
        }
@@ -5457,17 +5456,16 @@ zone_load_soa_rr(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
                goto invalidate_rdataset;
        }
 
-       count = 0;
        result = dns_rdataset_first(&rdataset);
        while (result == ISC_R_SUCCESS) {
                dns_rdata_init(&rdata);
                dns_rdataset_current(&rdataset, &rdata);
                count++;
                if (count == 1) {
+                       dns_rdata_soa_t soa;
                        result = dns_rdata_tostruct(&rdata, &soa, NULL);
-                       if (soattl != NULL) {
-                               *soattl = rdataset.ttl;
-                       }
+                       SET_SOA_VALUES(rdataset.ttl, soa.serial, soa.refresh,
+                                      soa.retry, soa.expire, soa.minimum);
                        RUNTIME_CHECK(result == ISC_R_SUCCESS);
                }
 
@@ -5476,53 +5474,14 @@ zone_load_soa_rr(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
        }
        dns_rdataset_disassociate(&rdataset);
 
-       if (soacount != NULL) {
-               *soacount = count;
-       }
-
-       if (count > 0) {
-               if (serial != NULL) {
-                       *serial = soa.serial;
-               }
-               if (refresh != NULL) {
-                       *refresh = soa.refresh;
-               }
-               if (retry != NULL) {
-                       *retry = soa.retry;
-               }
-               if (expire != NULL) {
-                       *expire = soa.expire;
-               }
-               if (minimum != NULL) {
-                       *minimum = soa.minimum;
-               }
-       } else {
-               if (soacount != NULL) {
-                       *soacount = 0;
-               }
-               if (soattl != NULL) {
-                       *soattl = 0;
-               }
-               if (serial != NULL) {
-                       *serial = 0;
-               }
-               if (refresh != NULL) {
-                       *refresh = 0;
-               }
-               if (retry != NULL) {
-                       *retry = 0;
-               }
-               if (expire != NULL) {
-                       *expire = 0;
-               }
-               if (minimum != NULL) {
-                       *minimum = 0;
-               }
-       }
-
        result = ISC_R_SUCCESS;
 
 invalidate_rdataset:
+       SET_IF_NOT_NULL(soacount, count);
+       if (count == 0) {
+               CLR_SOA_VALUES();
+       }
+
        dns_rdataset_invalidate(&rdataset);
 
        return (result);
@@ -5546,30 +5505,10 @@ zone_get_from_db(dns_zone_t *zone, dns_db_t *db, unsigned int *nscount,
 
        dns_db_currentversion(db, &version);
 
-       if (nscount != NULL) {
-               *nscount = 0;
-       }
-       if (soacount != NULL) {
-               *soacount = 0;
-       }
-       if (soattl != NULL) {
-               *soattl = 0;
-       }
-       if (serial != NULL) {
-               *serial = 0;
-       }
-       if (refresh != NULL) {
-               *refresh = 0;
-       }
-       if (retry != NULL) {
-               *retry = 0;
-       }
-       if (expire != NULL) {
-               *expire = 0;
-       }
-       if (errors != NULL) {
-               *errors = 0;
-       }
+       SET_IF_NOT_NULL(nscount, 0);
+       SET_IF_NOT_NULL(soacount, 0);
+       SET_IF_NOT_NULL(errors, 0);
+       CLR_SOA_VALUES();
 
        node = NULL;
        result = dns_db_findnode(db, &zone->origin, false, &node);