]> git.saurik.com Git - apple/configd.git/blob - dnsinfo/dnsinfo_logging.h
configd-963.200.27.tar.gz
[apple/configd.git] / dnsinfo / dnsinfo_logging.h
1 /*
2 * Copyright (c) 2017, 2018 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #ifndef _S_DNSINFO_LOGGING_H
25 #define _S_DNSINFO_LOGGING_H
26
27 #include <os/availability.h>
28 #include <TargetConditionals.h>
29 #include <sys/cdefs.h>
30 #include <CoreFoundation/CoreFoundation.h>
31 #include <SystemConfiguration/SystemConfiguration.h>
32 #include <SystemConfiguration/SCPrivate.h>
33 #include "SCNetworkReachabilityLogging.h"
34 #include <arpa/inet.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <net/if.h>
38 #include <net/if_var.h>
39
40 #include <dnsinfo.h>
41 #include "dnsinfo_private.h"
42
43 __BEGIN_DECLS
44
45 #ifndef my_log
46 #define my_log(__level, __format, ...) SC_log(__level, __format, ## __VA_ARGS__)
47 #define MY_LOG_DEFINED_LOCALLY
48 #endif // !my_log
49
50 #ifndef my_log_context_type
51 #define MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY
52 #define my_log_context_type void *
53 #endif // !my_log_context_type
54
55 #ifndef my_log_context_name
56 #define MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY
57 #define my_log_context_name context
58 #endif // !my_log_context_name
59
60 static __inline__ void
61 _dns_resolver_log(uint32_t version, dns_resolver_t *resolver, int index, Boolean debug, my_log_context_type my_log_context_name)
62 {
63 #if defined(MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY) && defined(MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY)
64 #pragma unused(my_log_context_name)
65 #endif
66 int i;
67 uint32_t flags;
68 char reach_str[100];
69 CFMutableStringRef str;
70
71 my_log(LOG_INFO, "%s", "");
72 my_log(LOG_INFO, "resolver #%d", index);
73
74 if (resolver->domain != NULL) {
75 my_log(LOG_INFO, " domain : %s", resolver->domain);
76 }
77
78 for (i = 0; i < resolver->n_search; i++) {
79 my_log(LOG_INFO, " search domain[%d] : %s", i, resolver->search[i]);
80 }
81
82 for (i = 0; i < resolver->n_nameserver; i++) {
83 char buf[128];
84
85 _SC_sockaddr_to_string(resolver->nameserver[i], buf, sizeof(buf));
86 my_log(LOG_INFO, " nameserver[%d] : %s", i, buf);
87 }
88
89 for (i = 0; i < resolver->n_sortaddr; i++) {
90 char abuf[32];
91 char mbuf[32];
92
93 (void)inet_ntop(AF_INET, &resolver->sortaddr[i]->address, abuf, sizeof(abuf));
94 (void)inet_ntop(AF_INET, &resolver->sortaddr[i]->mask, mbuf, sizeof(mbuf));
95 my_log(LOG_INFO, " sortaddr[%d] : %s/%s", i, abuf, mbuf);
96 }
97
98 if (resolver->options != NULL) {
99 my_log(LOG_INFO, " options : %s", resolver->options);
100 }
101
102 if (resolver->port != 0) {
103 my_log(LOG_INFO, " port : %hd", resolver->port);
104 }
105
106 if (resolver->timeout != 0) {
107 my_log(LOG_INFO, " timeout : %d", resolver->timeout);
108 }
109
110 if (resolver->if_index != 0) {
111 #ifndef _LIBLOG_SYSTEMCONFIGURATION_
112 char buf[IFNAMSIZ];
113 #endif // !_LIBLOG_SYSTEMCONFIGURATION_
114 char *if_name = NULL;
115
116 if ((version >= 20170629) && (resolver->if_name != NULL)) {
117 if_name = resolver->if_name;
118 #ifndef _LIBLOG_SYSTEMCONFIGURATION_
119 } else {
120 if_name = if_indextoname(resolver->if_index, buf);
121 #endif // !_LIBLOG_SYSTEMCONFIGURATION_
122 }
123 my_log(LOG_INFO, " if_index : %d (%s)",
124 resolver->if_index,
125 (if_name != NULL) ? if_name : "?");
126 }
127
128 if (resolver->service_identifier != 0) {
129 my_log(LOG_INFO, " service_identifier : %d",
130 resolver->service_identifier);
131 }
132
133 flags = resolver->flags;
134 str = CFStringCreateMutable(NULL, 0);
135 CFStringAppend(str, CFSTR(" flags : "));
136 if (debug) {
137 CFStringAppendFormat(str, NULL, CFSTR("0x%08x"), flags);
138 }
139 if (flags != 0) {
140 if (debug) {
141 CFStringAppendFormat(str, NULL, CFSTR(" ("));
142 }
143 if (flags & DNS_RESOLVER_FLAGS_SCOPED) {
144 flags &= ~DNS_RESOLVER_FLAGS_SCOPED;
145 CFStringAppendFormat(str, NULL, CFSTR("Scoped%s"), flags != 0 ? ", " : "");
146 }
147 if (flags & DNS_RESOLVER_FLAGS_SERVICE_SPECIFIC) {
148 flags &= ~DNS_RESOLVER_FLAGS_SERVICE_SPECIFIC;
149 CFStringAppendFormat(str, NULL, CFSTR("Service-specific%s"), flags != 0 ? ", " : "");
150 }
151 if (flags & DNS_RESOLVER_FLAGS_SUPPLEMENTAL) {
152 flags &= ~DNS_RESOLVER_FLAGS_SUPPLEMENTAL;
153 CFStringAppendFormat(str, NULL, CFSTR("Supplemental%s"), flags != 0 ? ", " : "");
154 }
155 if (flags & DNS_RESOLVER_FLAGS_REQUEST_A_RECORDS) {
156 flags &= ~DNS_RESOLVER_FLAGS_REQUEST_A_RECORDS;
157 CFStringAppendFormat(str, NULL, CFSTR("Request A records%s"), flags != 0 ? ", " : "");
158 }
159 if (flags & DNS_RESOLVER_FLAGS_REQUEST_AAAA_RECORDS) {
160 flags &= ~DNS_RESOLVER_FLAGS_REQUEST_AAAA_RECORDS;
161 CFStringAppendFormat(str, NULL, CFSTR("Request AAAA records%s"), flags != 0 ? ", " : "");
162 }
163 if (flags != 0) {
164 CFStringAppendFormat(str, NULL, CFSTR("0x%08x"), flags);
165 }
166 if (debug) {
167 CFStringAppend(str, CFSTR(")"));
168 }
169 }
170 my_log(LOG_INFO, "%@", str);
171 CFRelease(str);
172
173 __SCNetworkReachability_flags_string(resolver->reach_flags, TRUE, reach_str, sizeof(reach_str));
174 my_log(LOG_INFO, " reach : %s", reach_str);
175
176 if (resolver->search_order != 0) {
177 my_log(LOG_INFO, " order : %d", resolver->search_order);
178 }
179
180 if (debug && (resolver->cid != NULL)) {
181 my_log(LOG_INFO, " config id: %s", resolver->cid);
182 }
183
184 return;
185 }
186
187 static __inline__ void
188 _dns_configuration_log(dns_config_t *dns_config, Boolean debug, my_log_context_type my_log_context_name)
189 {
190 #if defined(MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY) && defined(MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY)
191 #pragma unused(my_log_context_name)
192 #endif
193 int i;
194
195 my_log(LOG_INFO, "%s", "DNS configuration");
196
197 for (i = 0; i < dns_config->n_resolver; i++) {
198 dns_resolver_t *resolver = dns_config->resolver[i];
199
200 _dns_resolver_log(dns_config->version, resolver, i + 1, debug, my_log_context_name);
201 }
202
203 if ((dns_config->n_scoped_resolver > 0) && (dns_config->scoped_resolver != NULL)) {
204 my_log(LOG_INFO, "%s", "");
205 my_log(LOG_INFO, "%s", "DNS configuration (for scoped queries)");
206
207 for (i = 0; i < dns_config->n_scoped_resolver; i++) {
208 dns_resolver_t *resolver = dns_config->scoped_resolver[i];
209
210 _dns_resolver_log(dns_config->version, resolver, i + 1, debug, my_log_context_name);
211 }
212 }
213
214 if ((dns_config->n_service_specific_resolver > 0) && (dns_config->service_specific_resolver != NULL)) {
215 my_log(LOG_INFO, "%s", "");
216 my_log(LOG_INFO, "%s", "DNS configuration (for service-specific queries)");
217
218 for (i = 0; i < dns_config->n_service_specific_resolver; i++) {
219 dns_resolver_t *resolver = dns_config->service_specific_resolver[i];
220
221 _dns_resolver_log(dns_config->version, resolver, i + 1, debug, my_log_context_name);
222 }
223 }
224
225 return;
226 }
227
228 #ifdef MY_LOG_DEFINED_LOCALLY
229 #undef my_log
230 #undef MY_LOG_DEFINED_LOCALLY
231 #endif // MY_LOG_DEFINED_LOCALLY
232
233 #ifdef MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY
234 #undef my_log_context_type
235 #undef MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY
236 #endif // MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY
237
238 #ifdef MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY
239 #undef my_log_context_name
240 #undef MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY
241 #endif // MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY
242
243 __END_DECLS
244
245 #endif /* !_S_DNSINFO_LOGGING_H */