]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/dnssec_v2/dnssec_v2_helper.h
mDNSResponder-1310.40.42.tar.gz
[apple/mdnsresponder.git] / mDNSMacOSX / dnssec_v2 / dnssec_v2_helper.h
1 //
2 // dnssec_v2_helper.h
3 // mDNSResponder
4 //
5 // Copyright (c) 2020 Apple Inc. All rights reserved.
6 //
7
8 #ifndef DNSSEC_v2_HELPER_H
9 #define DNSSEC_v2_HELPER_H
10
11 #pragma mark - Includes
12 #include "mDNSEmbeddedAPI.h"
13 #if MDNSRESPONDER_SUPPORTS(APPLE, DNSSECv2)
14
15 #pragma mark - Macros
16
17 // DNSSEC helper
18
19 // Used to get the primary question from the current request, it is possible that the current request is started by
20 // the DNSSEC handler to follow the CNAME, so the primary question should be get from primary_dnssec_context field.
21 #define GET_PRIMARY_DNSSEC_CONTEXT(C) ((C)->primary_dnssec_context == mDNSNULL) ? \
22 (C) : ((C)->primary_dnssec_context)
23 #define GET_PRIMARY_REQUEST(C) (GET_PRIMARY_DNSSEC_CONTEXT(C))->me
24 #define GET_PRIMARY_QUESTION(C) (&((GET_PRIMARY_REQUEST(C))->op.q))
25 #define GET_REQUEST_ID(C) ((GET_PRIMARY_REQUEST(C))->op.reqID)
26 #define GET_QUESTION(C) (&((C)->me->op.q))
27 #define GET_QUESTION_ID_FROM_Q(Q) (mDNSVal16((Q)->TargetQID))
28 #define GET_QUESTION_ID(C) (GET_QUESTION_ID_FROM_Q(GET_QUESTION(C)))
29
30 // string helper
31 #define DOMAIN_NAME_EQUALS(A, B) SameDomainName((const domainname *)(A), (const domainname *)(B))
32 #define DOMAIN_NAME_LENGTH(NAME) DomainNameLength((const domainname *)(NAME))
33 #define IS_UPPER_CASE(CH) ((CH) >= 'A') && ((CH) <= 'Z')
34 #define IS_LOWER_CASE(CH) ((CH) >= 'a') && ((CH) <= 'z')
35 #define TO_LOWER_CASE(CH) ((CH) + ('a' - 'A'))
36
37 // print helper
38 #define DNS_TYPE_STR(TYPE) DNSTypeName((TYPE))
39 #define NUM_OF_SPACES_PER_TAB 4
40 #define TAB_STR "%{public}*s"
41 #define TAB_PARAM(NUM_OF_TABS) ((NUM_OF_TABS) * (NUM_OF_SPACES_PER_TAB)), ""
42 // BASE64_STR should be used with BASE64_PARAM
43 #define BASE64_STR "%{private, mask.hash}.10s%s"
44 #define BASE64_PARAM(BASE64) (BASE64), strlen(BASE64) < 11 ? "" : "..."
45
46 #pragma mark - Functions
47
48
49
50 #pragma mark deep_copy_resource_record
51 /*!
52 * @brief
53 * Deep copy the record including the name and the rdata.
54 * @param dst
55 * The copy destination.
56 * @param src
57 * The copy source.
58 * @return
59 * Returns mStatus_NoError if no error occurs, other error codes if error occurs
60 * @discussion
61 * Remember to free the malloced ResourceRecord->name and ResourceRecord->rdata.
62 */
63 mDNSexport mStatus
64 deep_copy_resource_record(ResourceRecord * const _Nonnull dst, const ResourceRecord * const _Nonnull src);
65
66 #pragma mark free_resource_record_deep_copied
67 /*!
68 * @brief
69 * Free the malloced memory in the ResourceRecord.
70 * @param rr
71 * The ResourceRecord that has malloced memory inside.
72 * @discussion
73 * It will only free malloced fields in ResourceRecord, remember to free ResourceRecord itself.
74 */
75 mDNSexport void
76 free_resource_record_deep_copied(ResourceRecord * const _Nonnull rr);
77
78 #pragma mark is_root_domain
79 /*!
80 * @brief
81 * Given a DNS format DNS name, determine if it is root.
82 * @param domain_name
83 * The DNS format domain name.
84 * @return
85 * Returns true if the domain is root, otherwise return false.
86 */
87 mDNSexport mDNSBool
88 is_root_domain(const mDNSu8 * const _Nonnull domain_name);
89
90 #pragma mark is_a_subdomain_of_b
91 /*!
92 * @brief
93 * Determine if a_name is a sub domain of b_name
94 * @param a_name
95 * The child zone or sub domain.
96 * @param b_name
97 * The parent zone.
98 * @return
99 * Returns true if a_name is a subdomain of b_name
100 */
101 mDNSexport mDNSBool
102 is_a_subdomain_of_b(const mDNSu8 * const _Nonnull a_name, const mDNSu8 * const _Nonnull b_name);
103
104 #pragma mark resource_records_equal
105 /*!
106 * @brief
107 * Determine if two records are identical.
108 * @return
109 * Returns true if two records are identical, false if they are not equal.
110 */
111 mDNSexport mDNSBool
112 resource_records_equal(
113 const mDNSu16 rr_type_0, const mDNSu16 rr_type_1,
114 const mDNSu16 rr_class_0, const mDNSu16 rr_clasee_1,
115 const mDNSu16 rdata_length_0, const mDNSu16 rdata_length_1,
116 const mDNSu32 name_hash_0, const mDNSu32 name_hash_1,
117 const mDNSu32 rdata_hash_0, const mDNSu32 rdata_hash_1,
118 const mDNSu8 * const _Nonnull name_0, const mDNSu8 * const _Nonnull name_1,
119 const mDNSu8 * const _Nonnull rdata_0, const mDNSu8 * const _Nonnull rdata_1);
120
121 #pragma mark dnssec_algorithm_value_to_string
122 /*!
123 * @brief
124 * Convert DNSKEY type to string description.
125 * @param algorithm
126 * The DNSKEY type.
127 * @return
128 * The corresponding string description of the DNSKEY type.
129 */
130 mDNSexport const char * _Nonnull
131 dnssec_algorithm_value_to_string(const mDNSu8 algorithm);
132
133 #pragma mark dnssec_dnskey_flags_to_string
134 /*!
135 * @brief
136 * Convert DS hash type to string description.
137 * @param digest_type
138 * The DS digest type.
139 * @return
140 * The corresponding string description of the DS digest type.
141 */
142 mDNSexport const char * _Nonnull
143 dnssec_digest_type_value_to_string(const mDNSu8 digest_type);
144
145 #pragma mark deep_copy_resource_record
146 /*!
147 * @brief
148 * Convert DNSKEY flags to string description.
149 * @param flags
150 * The DNSKEY flags field
151 * @param buffer
152 * The string buffer that stores the converted string description.
153 * @param buffer_size
154 * The total size of the buffer.
155 * @return
156 * The pointer to the start of the buffer, aka the start of the description.
157 */
158 mDNSexport const char * _Nonnull
159 dnssec_dnskey_flags_to_string(const mDNSu16 flags, char * const _Nonnull buffer, const mDNSu32 buffer_size);
160
161 #pragma mark dnssec_epoch_time_to_date_string
162 /*!
163 * @brief
164 * Convert epoch time field in RRSIG to string description.
165 * @param epoch
166 * The epoch time.
167 * @param buffer
168 * The string buffer that stores the converted string description.
169 * @param buffer_size
170 * The total size of the buffer.
171 * @return
172 * The pointer to the start of the buffer, aka the start of the description.
173 */
174 mDNSexport const char * _Nonnull
175 dnssec_epoch_time_to_date_string(const mDNSu32 epoch, char * const _Nonnull buffer, const mDNSu32 buffer_size);
176
177 #pragma mark dnssec_nsec3_flags_to_string
178 /*!
179 * @brief
180 * Convert NSEC3 flags to string description.
181 * @param flags
182 * The DNSKEY flags field
183 * @param buffer
184 * The string buffer that stores the converted string description.
185 * @param buffer_size
186 * The total size of the buffer.
187 * @return
188 * The pointer to the start of the buffer, aka the start of the description.
189 */
190 mDNSexport const char * _Nonnull
191 dnssec_nsec3_flags_to_string(const mDNSu8 flags, char * const _Nonnull buffer, const mDNSu32 buffer_size);
192
193 #pragma mark get_number_of_labels
194 /*!
195 * @brief
196 * Count the number of labels for the given DNS format name.
197 * @name
198 * The DNS format name.
199 * @return
200 * The number of labels.
201 */
202 mDNSexport mDNSu8
203 get_number_of_labels(const mDNSu8 * _Nonnull name);
204
205 #pragma mark to_lowercase_if_char
206 /*!
207 * @brief
208 * Convert the character to lower string if it is alphbeta.
209 * @param ch
210 * The character.
211 * @return
212 * The converted character.
213 */
214 mDNSexport mDNSu8
215 to_lowercase_if_char(const mDNSu8 ch);
216
217 #endif // MDNSRESPONDER_SUPPORTS(APPLE, DNSSECv2)
218 #endif // DNSSEC_v2_HELPER_H