2 * Copyright (c) 2009 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 ((struct sockaddr_in
*)sa
)->sin_port
= port
;
149 ((struct sockaddr_in6
*)sa
)->sin6_port
= port
;
159 * _dnsinfo_parse_sortaddr
161 * Parse arguments to the sortlist token.
163 static dns_sortaddr_t
*
164 _dnsinfo_parse_sortaddr(char *token
)
170 dns_sortaddr_t
*sortaddr
= NULL
;
172 slash
= strchr(token
, '/');
177 sa
= _dnsinfo_parse_address(token
);
179 // if we could not parse the address
181 } else if (sa
->sa_family
!= AF_INET
) {
185 addr
= ((struct sockaddr_in
*)sa
)->sin_addr
;
191 sa
= _dnsinfo_parse_address(slash
+ 1);
193 // if we could not parse the provided mask
195 } else if (sa
->sa_family
!= AF_INET
) {
196 // if mask not AF_INET
199 mask
= ((struct sockaddr_in
*)sa
)->sin_addr
;
207 a
= ntohl(addr
.s_addr
);
210 } else if (IN_CLASSB(a
)) {
212 } else if (IN_CLASSC(a
)) {
218 mask
.s_addr
= htonl(m
);
221 sortaddr
= malloc(sizeof(*sortaddr
));
222 sortaddr
->address
= addr
;
223 sortaddr
->mask
= mask
;
227 if (sa
!= NULL
) free(sa
);
233 * _dnsinfo_flatfile_create_resolver
235 * Create a new dns resolver configuration from the configuration file at the
236 * specified path. (e.g. /etc/resolv.conf or /etc/resolver/apple.com)
238 static dns_create_resolver_t
239 _dnsinfo_flatfile_create_resolver(const char *dir
, const char *path
)
243 char filename
[FILENAME_MAX
];
246 dns_create_resolver_t res
= NULL
;
247 const char *sep
= " \t";
248 int token_count
[TOKEN_MAX
] = { 0 };
252 strlcpy(filename
, dir
, sizeof(filename
));
253 strlcat(filename
, "/", sizeof(filename
));
255 strlcat(filename
, path
, sizeof(filename
));
257 f
= fopen(filename
, "r");
258 if (f
== NULL
) return NULL
;
260 while ((buf
= fgetln(f
, &len
)) != NULL
) {
267 if (len
== 0) continue;
268 if (buf
[len
-1] == '\n') buf
[len
-1] = '\0';
270 line
= reallocf(line
, len
+1);
271 if (line
== NULL
) continue;
273 strncpy(line
, buf
, len
);
276 // parse the first word of the line (the config token)
278 word
= strsep(&lineptr
, sep
);
283 if (word
[0] == ';' || word
[0] == '#') {
288 // translate config token to enumerated value
290 for (i
= 0; i
< sizeof(tokens
) / sizeof(tokens
[0]); i
++) {
291 if (strcasecmp(word
, tokens
[i
].name
) == 0) {
292 token
= tokens
[i
].token
;
293 max_count
= tokens
[i
].max_count
;
298 // if not a recognized token
302 // parse the next word of the line (the config option)
303 word
= strsep(&lineptr
, sep
);
308 if (++token_count
[token
] > max_count
) {
309 // if too many options
315 res
= _dns_resolver_create();
317 // if we could not create a resolver
327 while ((len
> 0) && (word
[len
- 1] == '.')) {
332 _dns_resolver_set_domain(&res
, word
);
340 while (word
!= NULL
) {
341 if (word
[0] != '\0') {
342 if (strcasecmp(word
, "scoped") == 0) {
343 flags
|= DNS_RESOLVER_FLAGS_SCOPED
;
346 word
= strsep(&lineptr
, sep
);
350 _dns_resolver_set_flags(&res
, flags
);
355 case TOKEN_INTERFACE
: {
356 unsigned int if_index
;
358 if_index
= if_nametoindex(word
);
360 _dns_resolver_set_if_index(&res
, if_index
);
365 case TOKEN_NAMESERVER
: {
368 sa
= _dnsinfo_parse_nameserver(word
);
370 _dns_resolver_add_nameserver(&res
, sa
);
376 case TOKEN_OPTIONS
: {
377 char *options
= NULL
;
379 while (word
!= NULL
) {
380 if (word
[0] != '\0') {
381 if (options
== NULL
) {
382 options
= malloc(len
+1);
383 if (options
== NULL
) break;
385 strlcpy(options
, word
, len
+1);
387 strlcat(options
, " ", len
+1);
388 strlcat(options
, word
, len
+1);
391 word
= strsep(&lineptr
, sep
);
394 if (options
!= NULL
) {
395 _dns_resolver_set_options(&res
, options
);
404 number
= strtol(word
, NULL
, 0);
405 if (number
< 0 || number
> UINT16_MAX
) break;
406 _dns_resolver_set_port(&res
, number
);
413 // multiple search domains are supported
414 while ((word
!= NULL
) && (n
++ < MAXDNSRCH
)) {
418 while ((len
> 0) && (word
[len
- 1] == '.')) {
423 _dns_resolver_add_search(&res
, word
);
425 word
= strsep(&lineptr
, sep
);
430 case TOKEN_SEARCH_ORDER
: {
433 number
= strtol(word
, NULL
, 0);
434 if (number
< 0 || number
> UINT32_MAX
) break;
435 _dns_resolver_set_order(&res
, number
);
439 case TOKEN_SORTLIST
: {
442 while ((word
!= NULL
) && (n
++ < MAXRESOLVSORT
)) {
443 dns_sortaddr_t
*sortaddr
;
445 sortaddr
= _dnsinfo_parse_sortaddr(word
);
446 if (sortaddr
== NULL
) break;
447 _dns_resolver_add_sortaddr(&res
, sortaddr
);
449 word
= strsep(&lineptr
, sep
);
454 case TOKEN_TIMEOUT
: {
457 number
= strtol(word
, NULL
, 0);
458 if (number
< 0 || number
> UINT32_MAX
) break;
459 _dns_resolver_set_timeout(&res
, number
);
464 if (line
!= NULL
) free(line
);
466 // set the domain to the basename of the path if not specified
467 if ((res
!= NULL
) && (token_count
[TOKEN_DOMAIN
] == 0)) {
470 domain
= strrchr(path
, '/');
471 if (domain
== NULL
) {
476 _dns_resolver_set_domain(&res
, domain
);
487 * _dnsinfo_flatfile_add_resolvers
489 * Parse the files in the resolver config directory (/etc/resolver) and add each
490 * resolver to the dns config.
493 _dnsinfo_flatfile_add_resolvers(dns_create_config_t
*config
)
497 dns_create_resolver_t res
;
499 dp
= opendir(_PATH_RESOLVER_DIR
);
504 while ((de
= readdir(dp
)) != NULL
) {
505 if (strcmp(de
->d_name
, ".") == 0 ||
506 strcmp(de
->d_name
, "..") == 0) continue;
508 res
= _dnsinfo_flatfile_create_resolver(_PATH_RESOLVER_DIR
, de
->d_name
);
510 _dns_configuration_add_resolver(config
, res
);
511 _dns_resolver_free(&res
);
523 #include "dnsinfo_copy.c"
526 main(int argc
, char **argv
)
529 dns_config_t
*config
;
530 dns_create_config_t create_config
;
531 _dns_config_buf_t
*config_buf
;
534 dns_create_resolver_t resolver
;
536 resolver
= _dnsinfo_flatfile_create_resolver(NULL
, _PATH_RESCONF
);
538 create_config
= _dns_configuration_create();
539 _dnsinfo_flatfile_add_resolvers(&create_config
);
541 config_buf
= (_dns_config_buf_t
*)create_config
;
542 n_config
= sizeof(_dns_config_buf_t
) + ntohl(config_buf
->n_attribute
);
543 n_padding
= ntohl(config_buf
->n_padding
);
544 buf
= malloc(n_config
+ n_padding
);
545 bcopy((void *)config_buf
, buf
, n_config
);
546 bzero(&buf
[n_config
], n_padding
);
547 config
= expand_config((_dns_config_buf_t
*)buf
);