1/*
2 * Copyright (c) 2013 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_INTERNAL_H
25#define _S_DNSINFO_INTERNAL_H
26
27#include <Availability.h>
28#include <TargetConditionals.h>
29#include <sys/cdefs.h>
30#include <SystemConfiguration/SystemConfiguration.h>
31#include <SystemConfiguration/SCPrivate.h>
32#include "SCNetworkReachabilityInternal.h"
33#include <arpa/inet.h>
34
35#include <dnsinfo.h>
36
37__BEGIN_DECLS
38
39static __inline__ void
40_dns_resolver_print(dns_resolver_t *resolver, int index)
41{
42	int	i;
43
44	SCPrint(TRUE, stdout, CFSTR("\nresolver #%d\n"), index);
45
46	if (resolver->domain != NULL) {
47		SCPrint(TRUE, stdout, CFSTR("  domain   : %s\n"), resolver->domain);
48	}
49
50	for (i = 0; i < resolver->n_search; i++) {
51		SCPrint(TRUE, stdout, CFSTR("  search domain[%d] : %s\n"), i, resolver->search[i]);
52	}
53
54	for (i = 0; i < resolver->n_nameserver; i++) {
55		char	buf[128];
56
57		_SC_sockaddr_to_string(resolver->nameserver[i], buf, sizeof(buf));
58		SCPrint(TRUE, stdout, CFSTR("  nameserver[%d] : %s\n"), i, buf);
59	}
60
61	for (i = 0; i < resolver->n_sortaddr; i++) {
62		char	abuf[32];
63		char	mbuf[32];
64
65		(void)inet_ntop(AF_INET, &resolver->sortaddr[i]->address, abuf, sizeof(abuf));
66		(void)inet_ntop(AF_INET, &resolver->sortaddr[i]->mask,    mbuf, sizeof(mbuf));
67		SCPrint(TRUE, stdout, CFSTR("  sortaddr[%d] : %s/%s\n"), i, abuf, mbuf);
68	}
69
70	if (resolver->options != NULL) {
71		SCPrint(TRUE, stdout, CFSTR("  options  : %s\n"), resolver->options);
72	}
73
74	if (resolver->port != 0) {
75		SCPrint(TRUE, stdout, CFSTR("  port     : %hd\n"), resolver->port);
76	}
77
78	if (resolver->timeout != 0) {
79		SCPrint(TRUE, stdout, CFSTR("  timeout  : %d\n"), resolver->timeout);
80	}
81
82	if (resolver->if_index != 0) {
83		char	buf[IFNAMSIZ];
84		char	*if_name;
85
86		if_name = if_indextoname(resolver->if_index, buf);
87		SCPrint(TRUE, stdout, CFSTR("  if_index : %d (%s)\n"),
88			resolver->if_index,
89			(if_name != NULL) ? if_name : "?");
90	}
91
92	if (resolver->service_identifier != 0) {
93		SCPrint(TRUE, stdout, CFSTR("  service_identifier : %d\n"),
94			resolver->service_identifier);
95	}
96
97	if (resolver->flags != 0) {
98		uint32_t	flags	= resolver->flags;
99
100		SCPrint(TRUE, stdout, CFSTR("  flags    : "));
101		SCPrint(_sc_debug, stdout, CFSTR("0x%08x ("), flags);
102		if (flags & DNS_RESOLVER_FLAGS_SCOPED) {
103			SCPrint(TRUE, stdout, CFSTR("Scoped"));
104			flags &= ~DNS_RESOLVER_FLAGS_SCOPED;
105			SCPrint(flags != 0, stdout, CFSTR(", "));
106		}
107		if (flags & DNS_RESOLVER_FLAGS_SERVICE_SPECIFIC) {
108			SCPrint(TRUE, stdout, CFSTR("Service-specific"));
109			flags &= ~DNS_RESOLVER_FLAGS_SERVICE_SPECIFIC;
110			SCPrint(flags != 0, stdout, CFSTR(", "));
111		}
112		if (flags & DNS_RESOLVER_FLAGS_REQUEST_A_RECORDS) {
113			SCPrint(TRUE, stdout, CFSTR("Request A records"));
114			flags &= ~DNS_RESOLVER_FLAGS_REQUEST_A_RECORDS;
115			SCPrint(flags != 0, stdout, CFSTR(", "));
116		}
117		if (flags & DNS_RESOLVER_FLAGS_REQUEST_AAAA_RECORDS) {
118			SCPrint(TRUE, stdout, CFSTR("Request AAAA records"));
119			flags &= ~DNS_RESOLVER_FLAGS_REQUEST_AAAA_RECORDS;
120			SCPrint(flags != 0, stdout, CFSTR(", "));
121		}
122		if (flags != 0) {
123			SCPrint(TRUE, stdout, CFSTR("0x%08x"), flags);
124		}
125		SCPrint(_sc_debug, stdout, CFSTR(")"));
126		SCPrint(TRUE, stdout, CFSTR("\n"));
127	}
128
129	if (resolver->reach_flags != 0) {
130		uint32_t	flags	= resolver->reach_flags;
131
132		SCPrint(TRUE, stdout, CFSTR("  reach    : "));
133		SCPrint(_sc_debug, stdout, CFSTR("0x%08x ("), flags);
134		__SCNetworkReachabilityPrintFlags(flags);
135		SCPrint(_sc_debug, stdout, CFSTR(")"));
136		SCPrint(TRUE, stdout, CFSTR("\n"));
137	}
138
139	if (resolver->search_order != 0) {
140		SCPrint(TRUE, stdout, CFSTR("  order    : %d\n"), resolver->search_order);
141	}
142
143	return;
144}
145
146
147static __inline__ void
148_dns_configuration_print(dns_config_t *dns_config)
149{
150	int	i;
151
152	SCPrint(TRUE, stdout, CFSTR("DNS configuration\n"));
153
154	for (i = 0; i < dns_config->n_resolver; i++) {
155		dns_resolver_t	*resolver	= dns_config->resolver[i];
156
157		_dns_resolver_print(resolver, i + 1);
158	}
159
160	if ((dns_config->n_scoped_resolver > 0) && (dns_config->scoped_resolver != NULL)) {
161		SCPrint(TRUE, stdout, CFSTR("\nDNS configuration (for scoped queries)\n"));
162
163		for (i = 0; i < dns_config->n_scoped_resolver; i++) {
164			dns_resolver_t	*resolver	= dns_config->scoped_resolver[i];
165
166			_dns_resolver_print(resolver, i + 1);
167		}
168	}
169
170	if ((dns_config->n_service_specific_resolver > 0) && (dns_config->service_specific_resolver != NULL)) {
171		SCPrint(TRUE, stdout, CFSTR("\nDNS configuration (for service-specific queries)\n"));
172
173		for (i = 0; i < dns_config->n_service_specific_resolver; i++) {
174			dns_resolver_t	*resolver	= dns_config->service_specific_resolver[i];
175
176			_dns_resolver_print(resolver, i + 1);
177		}
178	}
179
180	return;
181}
182
183__END_DECLS
184
185#endif	/* !_S_DNSINFO_INTERNAL_H */
186