2 * Copyright (c) 2017, 2018 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #ifndef _S_DNSINFO_LOGGING_H
25 #define _S_DNSINFO_LOGGING_H
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>
38 #include <net/if_var.h>
41 #include "dnsinfo_private.h"
46 #define my_log(__level, __format, ...) SC_log(__level, __format, ## __VA_ARGS__)
47 #define MY_LOG_DEFINED_LOCALLY
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
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
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
)
63 #if defined(MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY) && defined(MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY)
64 #pragma unused(my_log_context_name)
69 CFMutableStringRef str
;
71 my_log(LOG_INFO
, "%s", "");
72 my_log(LOG_INFO
, "resolver #%d", index
);
74 if (resolver
->domain
!= NULL
) {
75 my_log(LOG_INFO
, " domain : %s", resolver
->domain
);
78 for (i
= 0; i
< resolver
->n_search
; i
++) {
79 my_log(LOG_INFO
, " search domain[%d] : %s", i
, resolver
->search
[i
]);
82 for (i
= 0; i
< resolver
->n_nameserver
; i
++) {
85 _SC_sockaddr_to_string(resolver
->nameserver
[i
], buf
, sizeof(buf
));
86 my_log(LOG_INFO
, " nameserver[%d] : %s", i
, buf
);
89 for (i
= 0; i
< resolver
->n_sortaddr
; i
++) {
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
);
98 if (resolver
->options
!= NULL
) {
99 my_log(LOG_INFO
, " options : %s", resolver
->options
);
102 if (resolver
->port
!= 0) {
103 my_log(LOG_INFO
, " port : %hd", resolver
->port
);
106 if (resolver
->timeout
!= 0) {
107 my_log(LOG_INFO
, " timeout : %d", resolver
->timeout
);
110 if (resolver
->if_index
!= 0) {
111 #ifndef _LIBLOG_SYSTEMCONFIGURATION_
113 #endif // !_LIBLOG_SYSTEMCONFIGURATION_
114 char *if_name
= NULL
;
116 if ((version
>= 20170629) && (resolver
->if_name
!= NULL
)) {
117 if_name
= resolver
->if_name
;
118 #ifndef _LIBLOG_SYSTEMCONFIGURATION_
120 if_name
= if_indextoname(resolver
->if_index
, buf
);
121 #endif // !_LIBLOG_SYSTEMCONFIGURATION_
123 my_log(LOG_INFO
, " if_index : %d (%s)",
125 (if_name
!= NULL
) ? if_name
: "?");
128 if (resolver
->service_identifier
!= 0) {
129 my_log(LOG_INFO
, " service_identifier : %d",
130 resolver
->service_identifier
);
133 flags
= resolver
->flags
;
134 str
= CFStringCreateMutable(NULL
, 0);
135 CFStringAppend(str
, CFSTR(" flags : "));
137 CFStringAppendFormat(str
, NULL
, CFSTR("0x%08x"), flags
);
141 CFStringAppendFormat(str
, NULL
, CFSTR(" ("));
143 if (flags
& DNS_RESOLVER_FLAGS_SCOPED
) {
144 flags
&= ~DNS_RESOLVER_FLAGS_SCOPED
;
145 CFStringAppendFormat(str
, NULL
, CFSTR("Scoped%s"), flags
!= 0 ? ", " : "");
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 ? ", " : "");
151 if (flags
& DNS_RESOLVER_FLAGS_SUPPLEMENTAL
) {
152 flags
&= ~DNS_RESOLVER_FLAGS_SUPPLEMENTAL
;
153 CFStringAppendFormat(str
, NULL
, CFSTR("Supplemental%s"), flags
!= 0 ? ", " : "");
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 ? ", " : "");
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 ? ", " : "");
164 CFStringAppendFormat(str
, NULL
, CFSTR("0x%08x"), flags
);
167 CFStringAppend(str
, CFSTR(")"));
170 my_log(LOG_INFO
, "%@", str
);
173 __SCNetworkReachability_flags_string(resolver
->reach_flags
, TRUE
, reach_str
, sizeof(reach_str
));
174 my_log(LOG_INFO
, " reach : %s", reach_str
);
176 if (resolver
->search_order
!= 0) {
177 my_log(LOG_INFO
, " order : %d", resolver
->search_order
);
180 if (debug
&& (resolver
->cid
!= NULL
)) {
181 my_log(LOG_INFO
, " config id: %s", resolver
->cid
);
187 static __inline__
void
188 _dns_configuration_log(dns_config_t
*dns_config
, Boolean debug
, my_log_context_type my_log_context_name
)
190 #if defined(MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY) && defined(MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY)
191 #pragma unused(my_log_context_name)
195 my_log(LOG_INFO
, "%s", "DNS configuration");
197 for (i
= 0; i
< dns_config
->n_resolver
; i
++) {
198 dns_resolver_t
*resolver
= dns_config
->resolver
[i
];
200 _dns_resolver_log(dns_config
->version
, resolver
, i
+ 1, debug
, my_log_context_name
);
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)");
207 for (i
= 0; i
< dns_config
->n_scoped_resolver
; i
++) {
208 dns_resolver_t
*resolver
= dns_config
->scoped_resolver
[i
];
210 _dns_resolver_log(dns_config
->version
, resolver
, i
+ 1, debug
, my_log_context_name
);
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)");
218 for (i
= 0; i
< dns_config
->n_service_specific_resolver
; i
++) {
219 dns_resolver_t
*resolver
= dns_config
->service_specific_resolver
[i
];
221 _dns_resolver_log(dns_config
->version
, resolver
, i
+ 1, debug
, my_log_context_name
);
228 #ifdef MY_LOG_DEFINED_LOCALLY
230 #undef MY_LOG_DEFINED_LOCALLY
231 #endif // MY_LOG_DEFINED_LOCALLY
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
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
245 #endif /* !_S_DNSINFO_LOGGING_H */