1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 /*! \file */
13 
14 #include <isc/once.h>
15 #include <isc/result.h>
16 #include <isc/util.h>
17 
18 #include <dns/result.h>
19 
20 #define DNS_RESULT_ISRCODE(result) (DNS_R_NOERROR == ((result)&0xFFFF0000))
21 
22 dns_rcode_t
dns_result_torcode(isc_result_t result)23 dns_result_torcode(isc_result_t result) {
24 	dns_rcode_t rcode = dns_rcode_servfail;
25 
26 	if (DNS_RESULT_ISRCODE(result)) {
27 		/*
28 		 * Rcodes can't be bigger than 12 bits, which is why we
29 		 * AND with 0xFFF instead of 0xFFFF.
30 		 */
31 		return ((dns_rcode_t)((result)&0xFFF));
32 	}
33 
34 	/*
35 	 * Try to supply an appropriate rcode.
36 	 */
37 	switch (result) {
38 	case ISC_R_SUCCESS:
39 		rcode = dns_rcode_noerror;
40 		break;
41 	case ISC_R_BADBASE64:
42 	case ISC_R_RANGE:
43 	case ISC_R_UNEXPECTEDEND:
44 	case DNS_R_BADAAAA:
45 	/* case DNS_R_BADBITSTRING: deprecated */
46 	case DNS_R_BADCKSUM:
47 	case DNS_R_BADCLASS:
48 	case DNS_R_BADLABELTYPE:
49 	case DNS_R_BADPOINTER:
50 	case DNS_R_BADTTL:
51 	case DNS_R_BADZONE:
52 	/* case DNS_R_BITSTRINGTOOLONG: deprecated */
53 	case DNS_R_EXTRADATA:
54 	case DNS_R_LABELTOOLONG:
55 	case DNS_R_NOREDATA:
56 	case DNS_R_SYNTAX:
57 	case DNS_R_TEXTTOOLONG:
58 	case DNS_R_TOOMANYHOPS:
59 	case DNS_R_TSIGERRORSET:
60 	case DNS_R_UNKNOWN:
61 	case DNS_R_NAMETOOLONG:
62 	case DNS_R_OPTERR:
63 		rcode = dns_rcode_formerr;
64 		break;
65 	case DNS_R_DISALLOWED:
66 		rcode = dns_rcode_refused;
67 		break;
68 	case DNS_R_TSIGVERIFYFAILURE:
69 	case DNS_R_CLOCKSKEW:
70 		rcode = dns_rcode_notauth;
71 		break;
72 	default:
73 		rcode = dns_rcode_servfail;
74 	}
75 
76 	return (rcode);
77 }
78 
79 isc_result_t
dns_result_fromrcode(dns_rcode_t rcode)80 dns_result_fromrcode(dns_rcode_t rcode) {
81 	/*
82 	 * Rcodes can't be bigger than 12 bits, which is why we
83 	 * AND with 0xFFF instead of 0xFFFF.
84 	 */
85 	REQUIRE((rcode & 0xFFF) == rcode);
86 
87 	return ((isc_result_t)rcode + DNS_R_NOERROR);
88 }
89