/*
- * Copyright (c) 2009, 2011, 2012, 2014, 2015 Apple Inc. All rights reserved.
+ * Copyright (c) 2009, 2011, 2012, 2014, 2015, 2017-2020 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
- *
+ *
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
- *
+ *
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
- *
+ *
* @APPLE_LICENSE_HEADER_END@
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
+#include <SystemConfiguration/SCPrivate.h> // for SC_log
+
+#ifdef SC_LOG_HANDLE
+#include <os/log.h>
+os_log_t SC_LOG_HANDLE(void);
+#endif //SC_LOG_HANDLE
#include "dnsinfo.h"
#include "dnsinfo_private.h"
if (f == NULL) return NULL;
while ((buf = fgetln(f, &len)) != NULL) {
- int i;
char *lineptr;
int max_count;
int token;
line = reallocf(line, len+1);
if (line == NULL) continue;
- strncpy(line, buf, len);
- line[len] = '\0';
+ strlcpy(line, buf, len+1);
// parse the first word of the line (the config token)
lineptr = line;
// translate config token to enumerated value
token = -1;
- for (i = 0; i < sizeof(tokens) / sizeof(tokens[0]); i++) {
+ for (size_t i = 0; i < sizeof(tokens) / sizeof(tokens[0]); i++) {
if (strcasecmp(word, tokens[i].name) == 0) {
token = tokens[i].token;
max_count = tokens[i].max_count;
}
if (token == -1) {
// if not a recognized token
+ SC_log(LOG_NOTICE,
+ "Unrecognized token (%s) found in: %s",
+ word,
+ filename);
continue;
}
if_index = if_nametoindex(word);
if (if_index > 0) {
- _dns_resolver_set_if_index(&res, if_index);
+ _dns_resolver_set_if_index(&res, if_index, word);
}
break;
}
long number = -1;
number = strtol(word, NULL, 0);
- if (number < 0 || number > UINT32_MAX) break;
+ if (number < 0 || number > (long)UINT32_MAX) break;
_dns_resolver_set_order(&res, (uint32_t)number);
break;
}
long number = -1;
number = strtol(word, NULL, 0);
- if (number < 0 || number > UINT32_MAX) break;
+ if (number < 0 || number > (long)UINT32_MAX) break;
_dns_resolver_set_timeout(&res, (uint32_t)number);
break;
}
}
}
- if (line != NULL) free(line);
// set the domain to the basename of the path if not specified
if ((res != NULL) && (token_count[TOKEN_DOMAIN] == 0)) {
done :
+ if (line != NULL) free(line);
fclose(f);
return res;
}
#ifdef MAIN
#undef MAIN
+#include "dnsinfo_logging.h"
#include "dnsinfo_copy.c"
int
main(int argc, char **argv)
{
- uint8_t *buf;
- dns_config_t *config;
- dns_create_config_t create_config;
- _dns_config_buf_t *config_buf;
- uint32_t n_config;
- uint32_t n_padding;
- dns_create_resolver_t resolver;
-
- resolver = _dnsinfo_flatfile_create_resolver(NULL, _PATH_RESCONF);
-
- create_config = _dns_configuration_create();
- _dnsinfo_flatfile_add_resolvers(&create_config);
-
- config_buf = (_dns_config_buf_t *)create_config;
- n_config = sizeof(_dns_config_buf_t) + ntohl(config_buf->n_attribute);
- n_padding = ntohl(config_buf->n_padding);
- buf = malloc(n_config + n_padding);
- bcopy((void *)config_buf, buf, n_config);
- bzero(&buf[n_config], n_padding);
- config = _dns_configuration_expand_config((_dns_config_buf_t *)buf);
+ dns_config_t *dns_config = NULL;
+ _dns_config_buf_t *dns_config_buf = NULL;
+ dns_create_config_t dns_create_config;
+ dns_create_resolver_t dns_create_resolver;
+
+ dns_create_resolver = _dnsinfo_flatfile_create_resolver(NULL, _PATH_RESCONF);
+ _dns_resolver_free(&dns_create_resolver);
+
+ dns_create_config = _dns_configuration_create();
+ if (dns_create_config != NULL) {
+ size_t n;
+
+ _dnsinfo_flatfile_add_resolvers(&dns_create_config);
+
+ n = sizeof(_dns_config_buf_t);
+ n += ntohl(((_dns_config_buf_t *)dns_create_config)->n_attribute);
+ dns_config_buf = _dns_configuration_buffer_create((void *)dns_create_config, n);
+ _dns_configuration_free(&dns_create_config);
+ }
+
+ if (dns_config_buf != NULL) {
+ dns_config = _dns_configuration_buffer_expand(dns_config_buf);
+ if (dns_config == NULL) {
+ // if we were unable to expand the configuration
+ _dns_configuration_buffer_free(&dns_config_buf);
+ }
+ }
+
+ if (dns_config != NULL) {
+ _dns_configuration_log(dns_config, TRUE, NULL);
+ free(dns_config);
+ }
return 0;
}