2 * Copyright (c) 2009, 2011 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@
34 #include <sys/types.h>
35 #include <sys/socket.h>
39 #include "dnsinfo_private.h"
40 #include "dnsinfo_create.h"
58 * The supported configuration token strings and enumerated values.
65 { "domain", TOKEN_DOMAIN
, 1 },
66 { "flags", TOKEN_FLAGS
, 1 },
67 { "interface", TOKEN_INTERFACE
, 1 },
68 { "nameserver", TOKEN_NAMESERVER
, MAXNS
},
69 { "options", TOKEN_OPTIONS
, 1 },
70 { "port", TOKEN_PORT
, 1 },
71 { "search", TOKEN_SEARCH
, 1 },
72 { "search_order", TOKEN_SEARCH_ORDER
, 1 },
73 { "sortlist", TOKEN_SORTLIST
, 1 },
74 { "timeout", TOKEN_TIMEOUT
, 1 },
79 * _dnsinfo_parse_address
83 static struct sockaddr
*
84 _dnsinfo_parse_address(char *nameserver
)
87 struct addrinfo hints
;
89 struct sockaddr
*sa
= NULL
;
91 memset(&hints
, 0, sizeof(hints
));
92 hints
.ai_flags
= AI_NUMERICHOST
;
94 res
= getaddrinfo(nameserver
, NULL
, &hints
, &ai
);
96 if ((ai
->ai_family
== AF_INET
) || (ai
->ai_family
== AF_INET6
)) {
97 sa
= malloc(ai
->ai_addrlen
);
98 memcpy(sa
, ai
->ai_addr
, ai
->ai_addrlen
);
108 * _dnsinfo_parse_nameserver
110 * Parse arguments to the nameserver token. This is essentially a getaddrinfo(3)
111 * with AI_NUMERICHOST. However, if the conversion fails, check if the address
112 * contains an optional trailing '.' followed by a numeric port number. If found,
113 * remove the port number and retry the conversion (e.g. 127.0.0.1.55 or ::1.55).
115 static struct sockaddr
*
116 _dnsinfo_parse_nameserver(char *token
)
122 sa
= _dnsinfo_parse_address(token
);
127 // if we could not parse address, attempt to remove
128 // an optional trailing port number
129 dot
= strrchr(token
, '.');
134 number
= strtol(dot
+ 1, NULL
, 10);
135 if ((number
< 0) || (number
> UINT16_MAX
)) {
140 sa
= _dnsinfo_parse_address(token
);
142 in_port_t port
= htons(number
);
144 switch (sa
->sa_family
) {
146 /* ALIGN: cast ok, sockaddr was malloc'd */
147 ((struct sockaddr_in
*)(void *)sa
)->sin_port
= port
;
150 /* ALIGN: cast ok, sockaddr was malloc'd */
151 ((struct sockaddr_in6
*)(void *)sa
)->sin6_port
= port
;
161 * _dnsinfo_parse_sortaddr
163 * Parse arguments to the sortlist token.
165 static dns_sortaddr_t
*
166 _dnsinfo_parse_sortaddr(char *token
)
172 dns_sortaddr_t
*sortaddr
= NULL
;
174 slash
= strchr(token
, '/');
179 sa
= _dnsinfo_parse_address(token
);
181 // if we could not parse the address
183 } else if (sa
->sa_family
!= AF_INET
) {
187 /* ALIGN: cast ok, sockaddr was malloc'd */
188 addr
= ((struct sockaddr_in
*)(void *)sa
)->sin_addr
;
194 sa
= _dnsinfo_parse_address(slash
+ 1);
196 // if we could not parse the provided mask
198 } else if (sa
->sa_family
!= AF_INET
) {
199 // if mask not AF_INET
202 /* ALIGN: cast ok, sockaddr was malloc'd */
203 mask
= ((struct sockaddr_in
*)(void *)sa
)->sin_addr
;
211 a
= ntohl(addr
.s_addr
);
214 } else if (IN_CLASSB(a
)) {
216 } else if (IN_CLASSC(a
)) {
222 mask
.s_addr
= htonl(m
);
225 sortaddr
= malloc(sizeof(*sortaddr
));
226 sortaddr
->address
= addr
;
227 sortaddr
->mask
= mask
;
231 if (sa
!= NULL
) free(sa
);
237 * _dnsinfo_flatfile_create_resolver
239 * Create a new dns resolver configuration from the configuration file at the
240 * specified path. (e.g. /etc/resolv.conf or /etc/resolver/apple.com)
242 static dns_create_resolver_t
243 _dnsinfo_flatfile_create_resolver(const char *dir
, const char *path
)
247 char filename
[FILENAME_MAX
];
250 dns_create_resolver_t res
= NULL
;
251 const char *sep
= " \t";
252 int token_count
[TOKEN_MAX
] = { 0 };
256 strlcpy(filename
, dir
, sizeof(filename
));
257 strlcat(filename
, "/", sizeof(filename
));
259 strlcat(filename
, path
, sizeof(filename
));
261 f
= fopen(filename
, "r");
262 if (f
== NULL
) return NULL
;
264 while ((buf
= fgetln(f
, &len
)) != NULL
) {
271 if (len
== 0) continue;
272 if (buf
[len
-1] == '\n') buf
[len
-1] = '\0';
274 line
= reallocf(line
, len
+1);
275 if (line
== NULL
) continue;
277 strncpy(line
, buf
, len
);
280 // parse the first word of the line (the config token)
282 word
= strsep(&lineptr
, sep
);
287 if (word
[0] == ';' || word
[0] == '#') {
292 // translate config token to enumerated value
294 for (i
= 0; i
< sizeof(tokens
) / sizeof(tokens
[0]); i
++) {
295 if (strcasecmp(word
, tokens
[i
].name
) == 0) {
296 token
= tokens
[i
].token
;
297 max_count
= tokens
[i
].max_count
;
302 // if not a recognized token
306 // parse the next word of the line (the config option)
307 word
= strsep(&lineptr
, sep
);
312 if (++token_count
[token
] > max_count
) {
313 // if too many options
319 res
= _dns_resolver_create();
321 // if we could not create a resolver
331 while ((len
> 0) && (word
[len
- 1] == '.')) {
336 _dns_resolver_set_domain(&res
, word
);
344 while (word
!= NULL
) {
345 if (word
[0] != '\0') {
346 if (strcasecmp(word
, "scoped") == 0) {
347 flags
|= DNS_RESOLVER_FLAGS_SCOPED
;
350 word
= strsep(&lineptr
, sep
);
354 _dns_resolver_set_flags(&res
, flags
);
359 case TOKEN_INTERFACE
: {
360 unsigned int if_index
;
362 if_index
= if_nametoindex(word
);
364 _dns_resolver_set_if_index(&res
, if_index
);
369 case TOKEN_NAMESERVER
: {
372 sa
= _dnsinfo_parse_nameserver(word
);
374 _dns_resolver_add_nameserver(&res
, sa
);
380 case TOKEN_OPTIONS
: {
381 char *options
= NULL
;
383 while (word
!= NULL
) {
384 if (word
[0] != '\0') {
385 if (options
== NULL
) {
386 options
= malloc(len
+1);
387 if (options
== NULL
) break;
389 strlcpy(options
, word
, len
+1);
391 strlcat(options
, " ", len
+1);
392 strlcat(options
, word
, len
+1);
395 word
= strsep(&lineptr
, sep
);
398 if (options
!= NULL
) {
399 _dns_resolver_set_options(&res
, options
);
408 number
= strtol(word
, NULL
, 0);
409 if (number
< 0 || number
> UINT16_MAX
) break;
410 _dns_resolver_set_port(&res
, number
);
417 // multiple search domains are supported
418 while ((word
!= NULL
) && (n
++ < MAXDNSRCH
)) {
422 while ((len
> 0) && (word
[len
- 1] == '.')) {
427 _dns_resolver_add_search(&res
, word
);
429 word
= strsep(&lineptr
, sep
);
434 case TOKEN_SEARCH_ORDER
: {
437 number
= strtol(word
, NULL
, 0);
438 if (number
< 0 || number
> UINT32_MAX
) break;
439 _dns_resolver_set_order(&res
, number
);
443 case TOKEN_SORTLIST
: {
446 while ((word
!= NULL
) && (n
++ < MAXRESOLVSORT
)) {
447 dns_sortaddr_t
*sortaddr
;
449 sortaddr
= _dnsinfo_parse_sortaddr(word
);
450 if (sortaddr
== NULL
) break;
451 _dns_resolver_add_sortaddr(&res
, sortaddr
);
453 word
= strsep(&lineptr
, sep
);
458 case TOKEN_TIMEOUT
: {
461 number
= strtol(word
, NULL
, 0);
462 if (number
< 0 || number
> UINT32_MAX
) break;
463 _dns_resolver_set_timeout(&res
, number
);
468 if (line
!= NULL
) free(line
);
470 // set the domain to the basename of the path if not specified
471 if ((res
!= NULL
) && (token_count
[TOKEN_DOMAIN
] == 0)) {
474 domain
= strrchr(path
, '/');
475 if (domain
== NULL
) {
480 _dns_resolver_set_domain(&res
, domain
);
491 * _dnsinfo_flatfile_add_resolvers
493 * Parse the files in the resolver config directory (/etc/resolver) and add each
494 * resolver to the dns config.
497 _dnsinfo_flatfile_add_resolvers(dns_create_config_t
*config
)
501 dns_create_resolver_t res
;
503 dp
= opendir(_PATH_RESOLVER_DIR
);
508 while ((de
= readdir(dp
)) != NULL
) {
509 if (strcmp(de
->d_name
, ".") == 0 ||
510 strcmp(de
->d_name
, "..") == 0) continue;
512 res
= _dnsinfo_flatfile_create_resolver(_PATH_RESOLVER_DIR
, de
->d_name
);
514 _dns_configuration_add_resolver(config
, res
);
515 _dns_resolver_free(&res
);
527 #include "dnsinfo_copy.c"
530 main(int argc
, char **argv
)
533 dns_config_t
*config
;
534 dns_create_config_t create_config
;
535 _dns_config_buf_t
*config_buf
;
538 dns_create_resolver_t resolver
;
540 resolver
= _dnsinfo_flatfile_create_resolver(NULL
, _PATH_RESCONF
);
542 create_config
= _dns_configuration_create();
543 _dnsinfo_flatfile_add_resolvers(&create_config
);
545 config_buf
= (_dns_config_buf_t
*)create_config
;
546 n_config
= sizeof(_dns_config_buf_t
) + ntohl(config_buf
->n_attribute
);
547 n_padding
= ntohl(config_buf
->n_padding
);
548 buf
= malloc(n_config
+ n_padding
);
549 bcopy((void *)config_buf
, buf
, n_config
);
550 bzero(&buf
[n_config
], n_padding
);
551 config
= expand_config((_dns_config_buf_t
*)buf
);