]> git.saurik.com Git - apple/configd.git/blob - dnsinfo/dnsinfo_copy.c
configd-801.1.1.tar.gz
[apple/configd.git] / dnsinfo / dnsinfo_copy.c
1 /*
2 * Copyright (c) 2004, 2006, 2008-2013, 2015 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 /*
25 * Modification History
26 *
27 * March 9, 2004 Allan Nathanson <ajn@apple.com>
28 * - initial revision
29 */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <pthread.h>
34 #include <mach/mach.h>
35 #include <mach/mach_error.h>
36 #include <dispatch/dispatch.h>
37 #include <xpc/xpc.h>
38
39 #include "libSystemConfiguration_client.h"
40 #include "dnsinfo.h"
41 #include "dnsinfo_private.h"
42 #include "dnsinfo_internal.h"
43
44
45 const char *
46 dns_configuration_notify_key()
47 {
48 const char *key;
49
50 key = "com.apple.system.SystemConfiguration.dns_configuration";
51 return key;
52 }
53
54
55 #pragma mark -
56 #pragma mark DNS configuration [dnsinfo] client support
57
58
59 // Note: protected by __dns_configuration_queue()
60 static int dnsinfo_active = 0;
61 static libSC_info_client_t *dnsinfo_client = NULL;
62
63
64 static dispatch_queue_t
65 __dns_configuration_queue()
66 {
67 static dispatch_once_t once;
68 static dispatch_queue_t q;
69
70 dispatch_once(&once, ^{
71 q = dispatch_queue_create(DNSINFO_SERVICE_NAME, NULL);
72 });
73
74 return q;
75 }
76
77
78 dns_config_t *
79 dns_configuration_copy()
80 {
81 uint8_t *buf = NULL;
82 dns_config_t *config = NULL;
83 static const char *proc_name = NULL;
84 xpc_object_t reqdict;
85 xpc_object_t reply;
86
87 dispatch_sync(__dns_configuration_queue(), ^{
88 if ((dnsinfo_active++ == 0) || (dnsinfo_client == NULL)) {
89 static dispatch_once_t once;
90 static const char *service_name = DNSINFO_SERVICE_NAME;
91
92 dispatch_once(&once, ^{
93 const char *name;
94
95 // get [XPC] service name
96 name = getenv(service_name);
97 if ((name != NULL) && (issetugid() == 0)) {
98 service_name = strdup(name);
99 }
100
101 // get process name
102 proc_name = getprogname();
103 });
104
105 dnsinfo_client =
106 libSC_info_client_create(__dns_configuration_queue(), // dispatch queue
107 service_name, // XPC service name
108 "DNS configuration"); // service description
109 if (dnsinfo_client == NULL) {
110 --dnsinfo_active;
111 }
112 }
113 });
114
115 if ((dnsinfo_client == NULL) || !dnsinfo_client->active) {
116 // if DNS configuration server not available
117 return NULL;
118 }
119
120 // create message
121 reqdict = xpc_dictionary_create(NULL, NULL, 0);
122
123 // set process name
124 if (proc_name != NULL) {
125 xpc_dictionary_set_string(reqdict, DNSINFO_PROC_NAME, proc_name);
126 }
127
128 // set request
129 xpc_dictionary_set_int64(reqdict, DNSINFO_REQUEST, DNSINFO_REQUEST_COPY);
130
131 // send request to the DNS configuration server
132 reply = libSC_send_message_with_reply_sync(dnsinfo_client, reqdict);
133 xpc_release(reqdict);
134
135 if (reply != NULL) {
136 const void *dataRef;
137 size_t dataLen = 0;
138
139 dataRef = xpc_dictionary_get_data(reply, DNSINFO_CONFIGURATION, &dataLen);
140 if ((dataRef != NULL) &&
141 ((dataLen >= sizeof(_dns_config_buf_t)) && (dataLen <= DNS_CONFIG_BUF_MAX))) {
142 _dns_config_buf_t *config = (_dns_config_buf_t *)(void *)dataRef;
143 uint32_t n_padding = ntohl(config->n_padding);
144
145 if (n_padding <= (DNS_CONFIG_BUF_MAX - dataLen)) {
146 size_t len;
147
148 len = dataLen + n_padding;
149 buf = malloc(len);
150 bcopy((void *)dataRef, buf, dataLen);
151 bzero(&buf[dataLen], n_padding);
152 }
153 }
154
155 xpc_release(reply);
156 }
157
158 if (buf != NULL) {
159 /* ALIGN: cast okay since _dns_config_buf_t is int aligned */
160 config = _dns_configuration_expand_config((_dns_config_buf_t *)(void *)buf);
161 if (config == NULL) {
162 free(buf);
163 }
164 }
165
166 return config;
167 }
168
169
170 void
171 dns_configuration_free(dns_config_t *config)
172 {
173 if (config == NULL) {
174 return; // ASSERT
175 }
176
177 dispatch_sync(__dns_configuration_queue(), ^{
178 if (--dnsinfo_active == 0) {
179 // if last reference, drop connection
180 libSC_info_client_release(dnsinfo_client);
181 dnsinfo_client = NULL;
182 }
183 });
184
185 free((void *)config);
186 return;
187 }
188
189
190 void
191 _dns_configuration_ack(dns_config_t *config, const char *bundle_id)
192 {
193 xpc_object_t reqdict;
194
195 if (config == NULL) {
196 return; // ASSERT
197 }
198
199 if ((dnsinfo_client == NULL) || !dnsinfo_client->active) {
200 // if DNS configuration server not available
201 return;
202 }
203
204 dispatch_sync(__dns_configuration_queue(), ^{
205 dnsinfo_active++; // keep connection active (for the life of the process)
206 });
207
208 // create message
209 reqdict = xpc_dictionary_create(NULL, NULL, 0);
210
211 // set request
212 xpc_dictionary_set_int64(reqdict, DNSINFO_REQUEST, DNSINFO_REQUEST_ACKNOWLEDGE);
213
214 // set generation
215 xpc_dictionary_set_uint64(reqdict, DNSINFO_GENERATION, config->generation);
216
217 // send acknowledgement to the DNS configuration server
218 xpc_connection_send_message(dnsinfo_client->connection, reqdict);
219
220 xpc_release(reqdict);
221 return;
222 }
223
224 #ifdef MAIN
225
226 int
227 main(int argc, char **argv)
228 {
229 dns_config_t *config;
230
231 config = dns_configuration_copy();
232 if (config != NULL) {
233 dns_configuration_free(config);
234 }
235
236 exit(0);
237 }
238
239 #endif