]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Refactor how we map isc_result_t <-> dns_rcode_t
authorOndřej Surý <ondrej@isc.org>
Thu, 15 Jun 2023 08:24:21 +0000 (10:24 +0200)
committerOndřej Surý <ondrej@isc.org>
Thu, 15 Jun 2023 14:27:17 +0000 (16:27 +0200)
The mapping functions between isc_result_t and dns_rcode_t could return
both isc_result_t values not defined in the header and dns_rcode_t
values not defined in the header because it blindly maps anything
withing full 12-bits defined for RCODEs to isc_result_t and back.

Refactor the dns_result_{from,to}rcode() functions to always return
valid isc_result_t and dns_rcode_t values by explicitly mapping the
values to each other and returning DNS_R_SERVFAIL (dns_rcode_servfail)
when encountering value out of the defined range.

(cherry picked from commit b53d1d7069d7fd9fc71d94d0cced572ac6574f3f)

lib/dns/result.c

index 65e2a4a4546f78bc7aa16649ab8e55fd662d54d8..bb9afa9187ced7ddb7f4413e985f2b42534c9305 100644 (file)
 
 #include <dns/result.h>
 
-#define DNS_RESULT_ISRCODE(result) (DNS_R_NOERROR == ((result)&0xFFFF0000))
-
 dns_rcode_t
 dns_result_torcode(isc_result_t result) {
-       dns_rcode_t rcode = dns_rcode_servfail;
-
-       if (DNS_RESULT_ISRCODE(result)) {
-               /*
-                * Rcodes can't be bigger than 12 bits, which is why we
-                * AND with 0xFFF instead of 0xFFFF.
-                */
-               return ((dns_rcode_t)((result)&0xFFF));
-       }
-
-       /*
-        * Try to supply an appropriate rcode.
-        */
+       /* Try to supply an appropriate rcode. */
        switch (result) {
+       case DNS_R_NOERROR:
        case ISC_R_SUCCESS:
-               rcode = dns_rcode_noerror;
-               break;
+               return (dns_rcode_noerror);
+       case DNS_R_FORMERR:
        case ISC_R_BADBASE64:
        case ISC_R_RANGE:
        case ISC_R_UNEXPECTEDEND:
@@ -62,29 +49,73 @@ dns_result_torcode(isc_result_t result) {
        case DNS_R_UNKNOWN:
        case DNS_R_NAMETOOLONG:
        case DNS_R_OPTERR:
-               rcode = dns_rcode_formerr;
-               break;
+               return (dns_rcode_formerr);
+       case DNS_R_SERVFAIL:
+               return (dns_rcode_servfail);
+       case DNS_R_NXDOMAIN:
+               return (dns_rcode_nxdomain);
+       case DNS_R_NOTIMP:
+               return (dns_rcode_notimp);
+       case DNS_R_REFUSED:
        case DNS_R_DISALLOWED:
-               rcode = dns_rcode_refused;
-               break;
+               return (dns_rcode_refused);
+       case DNS_R_YXDOMAIN:
+               return (dns_rcode_yxdomain);
+       case DNS_R_YXRRSET:
+               return (dns_rcode_yxrrset);
+       case DNS_R_NXRRSET:
+               return (dns_rcode_nxrrset);
+       case DNS_R_NOTAUTH:
        case DNS_R_TSIGVERIFYFAILURE:
        case DNS_R_CLOCKSKEW:
-               rcode = dns_rcode_notauth;
-               break;
+               return (dns_rcode_notauth);
+       case DNS_R_NOTZONE:
+               return (dns_rcode_notzone);
+       case DNS_R_RCODE11:
+       case DNS_R_RCODE12:
+       case DNS_R_RCODE13:
+       case DNS_R_RCODE14:
+       case DNS_R_RCODE15:
+               return (result - DNS_R_NOERROR);
+       case DNS_R_BADVERS:
+               return (dns_rcode_badvers);
+       case DNS_R_BADCOOKIE:
+               return (dns_rcode_badcookie);
        default:
-               rcode = dns_rcode_servfail;
+               return (dns_rcode_servfail);
        }
-
-       return (rcode);
 }
 
 isc_result_t
 dns_result_fromrcode(dns_rcode_t rcode) {
-       /*
-        * Rcodes can't be bigger than 12 bits, which is why we
-        * AND with 0xFFF instead of 0xFFFF.
-        */
-       REQUIRE((rcode & 0xFFF) == rcode);
-
-       return ((isc_result_t)rcode + DNS_R_NOERROR);
+       switch (rcode) {
+       case dns_rcode_noerror:
+               return (DNS_R_NOERROR);
+       case dns_rcode_formerr:
+               return (DNS_R_FORMERR);
+       case dns_rcode_servfail:
+               return (DNS_R_SERVFAIL);
+       case dns_rcode_nxdomain:
+               return (DNS_R_NXDOMAIN);
+       case dns_rcode_notimp:
+               return (DNS_R_NOTIMP);
+       case dns_rcode_refused:
+               return (DNS_R_REFUSED);
+       case dns_rcode_yxdomain:
+               return (DNS_R_YXDOMAIN);
+       case dns_rcode_yxrrset:
+               return (DNS_R_YXRRSET);
+       case dns_rcode_nxrrset:
+               return (DNS_R_NXRRSET);
+       case dns_rcode_notauth:
+               return (DNS_R_NOTAUTH);
+       case dns_rcode_notzone:
+               return (DNS_R_NOTZONE);
+       case dns_rcode_badvers:
+               return (DNS_R_BADVERS);
+       case dns_rcode_badcookie:
+               return (DNS_R_BADCOOKIE);
+       default:
+               return (DNS_R_SERVFAIL);
+       }
 }