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 strncpy(line
, buf
, len
);
311 // parse the first word of the line (the config token)
313 word
= strsep(&lineptr
, sep
);
318 if (word
[0] == ';' || word
[0] == '#') {
323 // translate config token to enumerated value
325 for (i
= 0; i
< sizeof(tokens
) / sizeof(tokens
[0]); i
++) {
326 if (strcasecmp(word
, tokens
[i
].name
) == 0) {
327 token
= tokens
[i
].token
;
328 max_count
= tokens
[i
].max_count
;
333 // if not a recognized token
337 // parse the next word of the line (the config option)
338 word
= strsep(&lineptr
, sep
);
343 if (++token_count
[token
] > max_count
) {
344 // if too many options
350 res
= _dns_resolver_create();
352 // if we could not create a resolver
362 while ((len
> 0) && (word
[len
- 1] == '.')) {
367 _dns_resolver_set_domain(&res
, word
);
373 while (word
!= NULL
) {
374 if (word
[0] != '\0') {
375 if (strcasecmp(word
, "scoped") == 0) {
376 config_flags
|= DNS_RESOLVER_FLAGS_SCOPED
;
377 } else if (strcasecmp(word
, "a") == 0) {
378 config_flags
|= DNS_RESOLVER_FLAGS_REQUEST_A_RECORDS
;
379 } else if (strcasecmp(word
, "aaaa") == 0) {
380 config_flags
|= DNS_RESOLVER_FLAGS_REQUEST_AAAA_RECORDS
;
383 word
= strsep(&lineptr
, sep
);
388 case TOKEN_INTERFACE
: {
389 unsigned int if_index
;
391 if_index
= if_nametoindex(word
);
393 _dns_resolver_set_if_index(&res
, if_index
);
398 case TOKEN_NAMESERVER
: {
401 sa
= _dnsinfo_parse_nameserver(word
);
403 _dns_resolver_add_nameserver(&res
, sa
);
409 case TOKEN_OPTIONS
: {
410 char *options
= NULL
;
412 while (word
!= NULL
) {
413 if (word
[0] != '\0') {
414 if (options
== NULL
) {
415 options
= malloc(len
+1);
416 if (options
== NULL
) break;
418 strlcpy(options
, word
, len
+1);
420 strlcat(options
, " ", len
+1);
421 strlcat(options
, word
, len
+1);
424 word
= strsep(&lineptr
, sep
);
427 if (options
!= NULL
) {
428 _dns_resolver_set_options(&res
, options
);
437 number
= strtol(word
, NULL
, 0);
438 if (number
< 0 || number
> UINT16_MAX
) break;
439 _dns_resolver_set_port(&res
, number
);
446 // multiple search domains are supported
447 while ((word
!= NULL
) && (n
++ < MAXDNSRCH
)) {
451 while ((len
> 0) && (word
[len
- 1] == '.')) {
456 _dns_resolver_add_search(&res
, word
);
458 word
= strsep(&lineptr
, sep
);
463 case TOKEN_SEARCH_ORDER
: {
466 number
= strtol(word
, NULL
, 0);
467 if (number
< 0 || number
> UINT32_MAX
) break;
468 _dns_resolver_set_order(&res
, (uint32_t)number
);
472 case TOKEN_SORTLIST
: {
475 while ((word
!= NULL
) && (n
++ < MAXRESOLVSORT
)) {
476 dns_sortaddr_t
*sortaddr
;
478 sortaddr
= _dnsinfo_parse_sortaddr(word
);
479 if (sortaddr
== NULL
) break;
480 _dns_resolver_add_sortaddr(&res
, sortaddr
);
482 word
= strsep(&lineptr
, sep
);
487 case TOKEN_TIMEOUT
: {
490 number
= strtol(word
, NULL
, 0);
491 if (number
< 0 || number
> UINT32_MAX
) break;
492 _dns_resolver_set_timeout(&res
, (uint32_t)number
);
497 if (line
!= NULL
) free(line
);
499 // set the domain to the basename of the path if not specified
500 if ((res
!= NULL
) && (token_count
[TOKEN_DOMAIN
] == 0)) {
503 domain
= strrchr(path
, '/');
504 if (domain
== NULL
) {
509 _dns_resolver_set_domain(&res
, domain
);
513 // config flags should overwrite any default flags
514 if (config_flags
!= 0) {
515 _dns_resolver_set_flags(&res
, config_flags
);
517 _dnsinfo_flatfile_update_flags(&res
);
529 * _dnsinfo_flatfile_add_resolvers
531 * Parse the files in the resolver config directory (/etc/resolver) and add each
532 * resolver to the dns config.
536 _dnsinfo_flatfile_add_resolvers(dns_create_config_t
*config
)
540 dns_create_resolver_t res
;
542 dp
= opendir(_PATH_RESOLVER_DIR
);
547 while ((de
= readdir(dp
)) != NULL
) {
548 if (strcmp(de
->d_name
, ".") == 0 ||
549 strcmp(de
->d_name
, "..") == 0) continue;
551 res
= _dnsinfo_flatfile_create_resolver(_PATH_RESOLVER_DIR
, de
->d_name
);
553 _dns_configuration_add_resolver(config
, res
);
554 _dns_resolver_free(&res
);
566 #include "dnsinfo_copy.c"
569 main(int argc
, char **argv
)
572 dns_config_t
*config
;
573 dns_create_config_t create_config
;
574 _dns_config_buf_t
*config_buf
;
577 dns_create_resolver_t resolver
;
579 resolver
= _dnsinfo_flatfile_create_resolver(NULL
, _PATH_RESCONF
);
581 create_config
= _dns_configuration_create();
582 _dnsinfo_flatfile_add_resolvers(&create_config
);
584 config_buf
= (_dns_config_buf_t
*)create_config
;
585 n_config
= sizeof(_dns_config_buf_t
) + ntohl(config_buf
->n_attribute
);
586 n_padding
= ntohl(config_buf
->n_padding
);
587 buf
= malloc(n_config
+ n_padding
);
588 bcopy((void *)config_buf
, buf
, n_config
);
589 bzero(&buf
[n_config
], n_padding
);
590 config
= _dns_configuration_expand_config((_dns_config_buf_t
*)buf
);