2 * Copyright (c) 2017 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@
25 * Modification History
27 * May 18, 2017 Allan Nathanson <ajn@apple.com>
31 #define _LIBLOG_SYSTEMCONFIGURATION_
33 #import <Foundation/Foundation.h>
34 #import <os/log_private.h>
35 #import <os/state_private.h>
39 #import "dnsinfo_internal.h"
40 #import <network_information.h>
42 #define my_log(__level, __format, ...) [string appendFormat:@(__format "\n"), ## __VA_ARGS__]
43 #define my_log_context_type NSMutableString *
44 #define my_log_context_name string
45 #import "dnsinfo_logging.h"
46 #import "network_state_information_logging.h"
47 #undef my_log_context_name
48 #undef my_log_context_type
52 [[NSAttributedString alloc] initWithString:str]
54 #define SCASWithFormat(format, ...) \
55 SCAS(([[NSString alloc] initWithFormat:format, ##__VA_ARGS__]))
58 #pragma mark os_log formatting entry point
60 struct SC_OSLog_Formatters {
62 NS_RETURNS_RETAINED NSAttributedString * (*function)(id value);
67 OSLogCopyFormattedString(const char *type, id value, os_log_type_info_t info)
70 // add functions for each type into this list
71 static const struct SC_OSLog_Formatters formatters[] = {
72 // { .type = "???", .function = _SC_OSLogCopyFormattedString_??? },
75 for (int i = 0; i < (int)(sizeof(formatters) / sizeof(formatters[0])); i++) {
76 if (strcmp(type, formatters[i].type) == 0) {
77 return formatters[i].function(value);
81 return SCASWithFormat(@"liblog_SystemConfiguration: Not yet supported os_log formatting type: %s", type);
85 #pragma mark os_state formatting entry point
88 [[NSString alloc] initWithString:(str)]
90 #define SCNSWithFormat(format, ...) \
91 [[NSString alloc] initWithFormat:format, ##__VA_ARGS__]
93 static NS_RETURNS_RETAINED NSString *
94 _SC_OSStateCopyFormattedString_dnsinfo(uint32_t data_size, void *data)
96 dns_config_t *dns_config = NULL;
97 _dns_config_buf_t *dns_config_buf;
98 NSMutableString *string;
100 // os_state_add_handler w/
101 // osd_type = OS_STATE_DATA_CUSTOM
102 // osd_decoder.osdd_library = "SystemConfiguration
103 // osd_decoder.osdd_type = "dnsinfo"
105 if ((data_size == 0) || (data == NULL)) {
106 return @"No DNS configuration";
107 } else if (data_size < sizeof(_dns_config_buf_t)) {
108 return SCNSWithFormat(@"DNS configuration: size error (%d < %zd)",
110 sizeof(_dns_config_buf_t));
113 dns_config_buf = _dns_configuration_buffer_create(data, data_size);
114 if (dns_config_buf == NULL) {
115 return @"DNS configuration: data error";
118 dns_config = _dns_configuration_buffer_expand(dns_config_buf);
119 if (dns_config == NULL) {
120 // if we were unable to expand the configuration
121 _dns_configuration_buffer_free(&dns_config_buf);
122 return @"DNS configuration: expansion error";
125 string = [NSMutableString string];
126 _dns_configuration_log(dns_config, TRUE, string);
127 if (string.length == 0) {
128 [string appendString:@"DNS configuration: not available"];
135 static NS_RETURNS_RETAINED NSString *
136 _SC_OSStateCopyFormattedString_nwi(uint32_t data_size, void *data)
138 nwi_state_t state = (nwi_state_t)data;
139 NSMutableString *string;
141 // os_state_add_handler w/
142 // osd_type = OS_STATE_DATA_CUSTOM
143 // osd_decoder.osdd_library = "SystemConfiguration
144 // osd_decoder.osdd_type = "nwi"
146 if ((data_size == 0) || (data == NULL)) {
147 return @"No network information";
148 } else if (data_size < sizeof(nwi_state)) {
149 return SCNSWithFormat(@"Network information: size error (%d < %zd)",
151 sizeof(_dns_config_buf_t));
152 } else if (state->version != NWI_STATE_VERSION) {
153 return SCNSWithFormat(@"Network information: version error (%d != %d)",
158 string = [NSMutableString string];
159 _nwi_state_log(state, TRUE, string);
160 if (string.length == 0) {
161 [string appendString:@"Network information: not available"];
167 struct SC_OSState_Formatters {
169 NS_RETURNS_RETAINED NSString * (*function)(uint32_t data_size, void *data);
174 OSStateCreateStringWithData(const char *type, uint32_t data_size, void *data)
176 // add functions for each type into this list
177 static const struct SC_OSState_Formatters formatters[] = {
178 { .type = "dnsinfo", .function = _SC_OSStateCopyFormattedString_dnsinfo },
179 { .type = "nwi", .function = _SC_OSStateCopyFormattedString_nwi },
182 for (int i = 0; i < (int)(sizeof(formatters) / sizeof(formatters[0])); i++) {
183 if (strcmp(type, formatters[i].type) == 0) {
184 return formatters[i].function(data_size, data);
188 return SCNSWithFormat(@"liblog_SystemConfiguration: Not yet supported os_state formatting type: %s", type);