2 * Copyright (c) 2009, 2011, 2012, 2014, 2015 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"
42 static uint32_t _dnsinfo_flatfile_flags
;
60 * The supported configuration token strings and enumerated values.
67 { "domain", TOKEN_DOMAIN
, 1 },
68 { "flags", TOKEN_FLAGS
, 1 },
69 { "interface", TOKEN_INTERFACE
, 1 },
70 { "nameserver", TOKEN_NAMESERVER
, MAXNS
},
71 { "options", TOKEN_OPTIONS
, 1 },
72 { "port", TOKEN_PORT
, 1 },
73 { "search", TOKEN_SEARCH
, 1 },
74 { "search_order", TOKEN_SEARCH_ORDER
, 1 },
75 { "sortlist", TOKEN_SORTLIST
, 1 },
76 { "timeout", TOKEN_TIMEOUT
, 1 },
81 * _dnsinfo_parse_address
85 static struct sockaddr
*
86 _dnsinfo_parse_address(char *nameserver
)
89 struct addrinfo hints
;
91 struct sockaddr
*sa
= NULL
;
93 memset(&hints
, 0, sizeof(hints
));
94 hints
.ai_flags
= AI_NUMERICHOST
;
96 res
= getaddrinfo(nameserver
, NULL
, &hints
, &ai
);
98 if ((ai
->ai_family
== AF_INET
) || (ai
->ai_family
== AF_INET6
)) {
99 sa
= malloc(ai
->ai_addrlen
);
100 memcpy(sa
, ai
->ai_addr
, ai
->ai_addrlen
);
110 * _dnsinfo_parse_nameserver
112 * Parse arguments to the nameserver token. This is essentially a getaddrinfo(3)
113 * with AI_NUMERICHOST. However, if the conversion fails, check if the address
114 * contains an optional trailing '.' followed by a numeric port number. If found,
115 * remove the port number and retry the conversion (e.g. 127.0.0.1.55 or ::1.55).
117 static struct sockaddr
*
118 _dnsinfo_parse_nameserver(char *token
)
124 sa
= _dnsinfo_parse_address(token
);
129 // if we could not parse address, attempt to remove
130 // an optional trailing port number
131 dot
= strrchr(token
, '.');
136 number
= strtol(dot
+ 1, NULL
, 10);
137 if ((number
< 0) || (number
> UINT16_MAX
)) {
142 sa
= _dnsinfo_parse_address(token
);
144 in_port_t port
= htons(number
);
146 switch (sa
->sa_family
) {
148 /* ALIGN: cast ok, sockaddr was malloc'd */
149 ((struct sockaddr_in
*)(void *)sa
)->sin_port
= port
;
152 /* ALIGN: cast ok, sockaddr was malloc'd */
153 ((struct sockaddr_in6
*)(void *)sa
)->sin6_port
= port
;
163 * _dnsinfo_parse_sortaddr
165 * Parse arguments to the sortlist token.
167 static dns_sortaddr_t
*
168 _dnsinfo_parse_sortaddr(char *token
)
174 dns_sortaddr_t
*sortaddr
= NULL
;
176 slash
= strchr(token
, '/');
181 sa
= _dnsinfo_parse_address(token
);
183 // if we could not parse the address
185 } else if (sa
->sa_family
!= AF_INET
) {
189 /* ALIGN: cast ok, sockaddr was malloc'd */
190 addr
= ((struct sockaddr_in
*)(void *)sa
)->sin_addr
;
196 sa
= _dnsinfo_parse_address(slash
+ 1);
198 // if we could not parse the provided mask
200 } else if (sa
->sa_family
!= AF_INET
) {
201 // if mask not AF_INET
204 /* ALIGN: cast ok, sockaddr was malloc'd */
205 mask
= ((struct sockaddr_in
*)(void *)sa
)->sin_addr
;
213 a
= ntohl(addr
.s_addr
);
216 } else if (IN_CLASSB(a
)) {
218 } else if (IN_CLASSC(a
)) {
224 mask
.s_addr
= htonl(m
);
227 sortaddr
= malloc(sizeof(*sortaddr
));
228 sortaddr
->address
= addr
;
229 sortaddr
->mask
= mask
;
233 if (sa
!= NULL
) free(sa
);
239 * _dnsinfo_flatfile_set_flags
241 * Set the default resolver flags.
245 _dnsinfo_flatfile_set_flags(uint32_t flags
)
247 _dnsinfo_flatfile_flags
= flags
;
253 _dnsinfo_flatfile_update_flags(dns_create_resolver_t
*_resolver
)
257 _dns_resolver_buf_t
*resolver
= (_dns_resolver_buf_t
*)*_resolver
;
259 old_flags
= ntohl(resolver
->resolver
.flags
);
260 new_flags
= old_flags
| _dnsinfo_flatfile_flags
;
261 _dns_resolver_set_flags(_resolver
, new_flags
);
267 * _dnsinfo_flatfile_create_resolver
269 * Create a new dns resolver configuration from the configuration file at the
270 * specified path. (e.g. /etc/resolv.conf or /etc/resolver/apple.com)
272 static dns_create_resolver_t
273 _dnsinfo_flatfile_create_resolver(const char *dir
, const char *path
)
276 uint32_t config_flags
= 0;
278 char filename
[FILENAME_MAX
];
281 dns_create_resolver_t res
= NULL
;
282 const char *sep
= " \t";
283 int token_count
[TOKEN_MAX
] = { 0 };
287 strlcpy(filename
, dir
, sizeof(filename
));
288 strlcat(filename
, "/", sizeof(filename
));
290 strlcat(filename
, path
, sizeof(filename
));
292 f
= fopen(filename
, "r");
293 if (f
== NULL
) return NULL
;
295 while ((buf
= fgetln(f
, &len
)) != NULL
) {
302 if (len
== 0) continue;
303 if (buf
[len
-1] == '\n') buf
[len
-1] = '\0';
305 line
= reallocf(line
, len
+1);
306 if (line
== NULL
) continue;
308 strlcpy(line
, buf
, len
+1);
310 // parse the first word of the line (the config token)
312 word
= strsep(&lineptr
, sep
);
317 if (word
[0] == ';' || word
[0] == '#') {
322 // translate config token to enumerated value
324 for (i
= 0; i
< sizeof(tokens
) / sizeof(tokens
[0]); i
++) {
325 if (strcasecmp(word
, tokens
[i
].name
) == 0) {
326 token
= tokens
[i
].token
;
327 max_count
= tokens
[i
].max_count
;
332 // if not a recognized token
336 // parse the next word of the line (the config option)
337 word
= strsep(&lineptr
, sep
);
342 if (++token_count
[token
] > max_count
) {
343 // if too many options
349 res
= _dns_resolver_create();
351 // if we could not create a resolver
361 while ((len
> 0) && (word
[len
- 1] == '.')) {
366 _dns_resolver_set_domain(&res
, word
);
372 while (word
!= NULL
) {
373 if (word
[0] != '\0') {
374 if (strcasecmp(word
, "scoped") == 0) {
375 config_flags
|= DNS_RESOLVER_FLAGS_SCOPED
;
376 } else if (strcasecmp(word
, "a") == 0) {
377 config_flags
|= DNS_RESOLVER_FLAGS_REQUEST_A_RECORDS
;
378 } else if (strcasecmp(word
, "aaaa") == 0) {
379 config_flags
|= DNS_RESOLVER_FLAGS_REQUEST_AAAA_RECORDS
;
382 word
= strsep(&lineptr
, sep
);
387 case TOKEN_INTERFACE
: {
388 unsigned int if_index
;
390 if_index
= if_nametoindex(word
);
392 _dns_resolver_set_if_index(&res
, if_index
);
397 case TOKEN_NAMESERVER
: {
400 sa
= _dnsinfo_parse_nameserver(word
);
402 _dns_resolver_add_nameserver(&res
, sa
);
408 case TOKEN_OPTIONS
: {
409 char *options
= NULL
;
411 while (word
!= NULL
) {
412 if (word
[0] != '\0') {
413 if (options
== NULL
) {
414 options
= malloc(len
+1);
415 if (options
== NULL
) break;
417 strlcpy(options
, word
, len
+1);
419 strlcat(options
, " ", len
+1);
420 strlcat(options
, word
, len
+1);
423 word
= strsep(&lineptr
, sep
);
426 if (options
!= NULL
) {
427 _dns_resolver_set_options(&res
, options
);
436 number
= strtol(word
, NULL
, 0);
437 if (number
< 0 || number
> UINT16_MAX
) break;
438 _dns_resolver_set_port(&res
, number
);
445 // multiple search domains are supported
446 while ((word
!= NULL
) && (n
++ < MAXDNSRCH
)) {
450 while ((len
> 0) && (word
[len
- 1] == '.')) {
455 _dns_resolver_add_search(&res
, word
);
457 word
= strsep(&lineptr
, sep
);
462 case TOKEN_SEARCH_ORDER
: {
465 number
= strtol(word
, NULL
, 0);
466 if (number
< 0 || number
> UINT32_MAX
) break;
467 _dns_resolver_set_order(&res
, (uint32_t)number
);
471 case TOKEN_SORTLIST
: {
474 while ((word
!= NULL
) && (n
++ < MAXRESOLVSORT
)) {
475 dns_sortaddr_t
*sortaddr
;
477 sortaddr
= _dnsinfo_parse_sortaddr(word
);
478 if (sortaddr
== NULL
) break;
479 _dns_resolver_add_sortaddr(&res
, sortaddr
);
481 word
= strsep(&lineptr
, sep
);
486 case TOKEN_TIMEOUT
: {
489 number
= strtol(word
, NULL
, 0);
490 if (number
< 0 || number
> UINT32_MAX
) break;
491 _dns_resolver_set_timeout(&res
, (uint32_t)number
);
496 if (line
!= NULL
) free(line
);
498 // set the domain to the basename of the path if not specified
499 if ((res
!= NULL
) && (token_count
[TOKEN_DOMAIN
] == 0)) {
502 domain
= strrchr(path
, '/');
503 if (domain
== NULL
) {
508 _dns_resolver_set_domain(&res
, domain
);
512 // config flags should overwrite any default flags
513 if (config_flags
!= 0) {
514 _dns_resolver_set_flags(&res
, config_flags
);
516 _dnsinfo_flatfile_update_flags(&res
);
528 * _dnsinfo_flatfile_add_resolvers
530 * Parse the files in the resolver config directory (/etc/resolver) and add each
531 * resolver to the dns config.
535 _dnsinfo_flatfile_add_resolvers(dns_create_config_t
*config
)
539 dns_create_resolver_t res
;
541 dp
= opendir(_PATH_RESOLVER_DIR
);
546 while ((de
= readdir(dp
)) != NULL
) {
547 if (strcmp(de
->d_name
, ".") == 0 ||
548 strcmp(de
->d_name
, "..") == 0) continue;
550 res
= _dnsinfo_flatfile_create_resolver(_PATH_RESOLVER_DIR
, de
->d_name
);
552 _dns_configuration_add_resolver(config
, res
);
553 _dns_resolver_free(&res
);
565 #include "dnsinfo_copy.c"
568 main(int argc
, char **argv
)
571 dns_config_t
*config
;
572 dns_create_config_t create_config
;
573 _dns_config_buf_t
*config_buf
;
576 dns_create_resolver_t resolver
;
578 resolver
= _dnsinfo_flatfile_create_resolver(NULL
, _PATH_RESCONF
);
580 create_config
= _dns_configuration_create();
581 _dnsinfo_flatfile_add_resolvers(&create_config
);
583 config_buf
= (_dns_config_buf_t
*)create_config
;
584 n_config
= sizeof(_dns_config_buf_t
) + ntohl(config_buf
->n_attribute
);
585 n_padding
= ntohl(config_buf
->n_padding
);
586 buf
= malloc(n_config
+ n_padding
);
587 bcopy((void *)config_buf
, buf
, n_config
);
588 bzero(&buf
[n_config
], n_padding
);
589 config
= _dns_configuration_expand_config((_dns_config_buf_t
*)buf
);