]> git.saurik.com Git - apple/configd.git/blob - logging/liblog_SystemConfiguration.m
configd-1109.101.1.tar.gz
[apple/configd.git] / logging / liblog_SystemConfiguration.m
1 /*
2 * Copyright (c) 2017, 2020 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * Modification History
26 *
27 * May 18, 2017 Allan Nathanson <ajn@apple.com>
28 * - initial revision
29 */
30
31 #define _LIBLOG_SYSTEMCONFIGURATION_
32
33 #import <Foundation/Foundation.h>
34 #import <os/log_private.h>
35 #import <os/state_private.h>
36 #import <string.h>
37
38 #import <dnsinfo.h>
39 #import "dnsinfo_internal.h"
40 #import <network_information.h>
41
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
49 #undef my_log
50
51 #define SCAS(str) \
52 [[NSAttributedString alloc] initWithString:str]
53
54 #define SCASWithFormat(format, ...) \
55 SCAS(([[NSString alloc] initWithFormat:format, ##__VA_ARGS__]))
56
57 #pragma mark -
58 #pragma mark os_log formatting entry point
59
60 struct SC_OSLog_Formatters {
61 const char *type;
62 NS_RETURNS_RETAINED NSAttributedString * (*function)(id value);
63 };
64
65 NS_RETURNS_RETAINED
66 NSAttributedString *
67 OSLogCopyFormattedString(const char *type, id value, os_log_type_info_t info)
68 {
69 #pragma unused(info)
70 // add functions for each type into this list
71 static const struct SC_OSLog_Formatters formatters[] = {
72 // { .type = "???", .function = _SC_OSLogCopyFormattedString_??? },
73 };
74
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);
78 }
79 }
80
81 return SCASWithFormat(@"liblog_SystemConfiguration: Not yet supported os_log formatting type: %s", type);
82 }
83
84 #pragma mark -
85 #pragma mark os_state formatting entry point
86
87 #define SCNS(str) \
88 [[NSString alloc] initWithString:(str)]
89
90 #define SCNSWithFormat(format, ...) \
91 [[NSString alloc] initWithFormat:format, ##__VA_ARGS__]
92
93 static NS_RETURNS_RETAINED NSString *
94 _SC_OSStateCopyFormattedString_dnsinfo(uint32_t data_size, void *data)
95 {
96 dns_config_t *dns_config = NULL;
97 _dns_config_buf_t *dns_config_buf;
98 NSMutableString *string;
99
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"
104
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)",
109 data_size,
110 sizeof(_dns_config_buf_t));
111 }
112
113 dns_config_buf = _dns_configuration_buffer_create(data, data_size);
114 if (dns_config_buf == NULL) {
115 return @"DNS configuration: data error";
116 }
117
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";
123 }
124
125 string = [NSMutableString string];
126 _dns_configuration_log(dns_config, TRUE, string);
127 if (string.length == 0) {
128 [string appendString:@"DNS configuration: not available"];
129 }
130 free(dns_config);
131
132 return string;
133 }
134
135 static NS_RETURNS_RETAINED NSString *
136 _SC_OSStateCopyFormattedString_nwi(uint32_t data_size, void *data)
137 {
138 nwi_ifindex_t index;
139 nwi_ifindex_t *scan;
140 nwi_state_t state = (nwi_state_t)data;
141 NSMutableString *string;
142
143 // os_state_add_handler w/
144 // osd_type = OS_STATE_DATA_CUSTOM
145 // osd_decoder.osdd_library = "SystemConfiguration
146 // osd_decoder.osdd_type = "nwi"
147
148 if ((data_size == 0) || (data == NULL)) {
149 return @"No network information";
150 }
151
152 if (data_size < sizeof(nwi_state)) {
153 return SCNSWithFormat(@"Network information: size error (%d < %zd)",
154 data_size,
155 sizeof(_dns_config_buf_t));
156 }
157
158 if (state->version != NWI_STATE_VERSION) {
159 return SCNSWithFormat(@"Network information: version error (%d != %d)",
160 state->version,
161 NWI_STATE_VERSION);
162 }
163
164 if (data_size != nwi_state_size(state)) {
165 return SCNSWithFormat(@"Network information: size error (%d != %zd)",
166 data_size,
167 nwi_state_size(state));
168 }
169
170 if (state->ipv4_count > state->max_if_count) {
171 return SCNSWithFormat(@"Network information: ipv4 count error (%d > %d)",
172 state->ipv4_count,
173 state->max_if_count);
174 }
175
176 if (state->ipv6_count > state->max_if_count) {
177 return SCNSWithFormat(@"Network information: ipv6 count error (%d > %d)",
178 state->ipv6_count,
179 state->max_if_count);
180 }
181
182 if (state->if_list_count > state->max_if_count) {
183 return SCNSWithFormat(@"Network information: if_list count error (%d > %d)",
184 state->if_list_count,
185 state->max_if_count);
186 }
187
188 for (index = 0; index < state->ipv4_count; index++) {
189 nwi_ifindex_t alias_index;
190 nwi_ifindex_t alias_offset = state->ifstate_list[index].af_alias_offset;
191
192 if (alias_offset == 0) {
193 continue;
194 }
195 alias_index = alias_offset + index - state->max_if_count;
196 if ((alias_index < 0) || (alias_index >= state->ipv6_count)) {
197 return SCNSWithFormat(@"Network information: IPv4 alias [%d] offset error (%d < 0 || %d >= %d)",
198 index,
199 alias_index,
200 alias_index,
201 state->ipv6_count);
202 }
203 }
204
205 for (index = 0; index < state->ipv6_count; index++) {
206 nwi_ifindex_t alias_index;
207 nwi_ifindex_t alias_offset = state->ifstate_list[state->max_if_count + index].af_alias_offset;
208
209 if (alias_offset == 0) {
210 continue;
211 }
212 alias_index = alias_offset + index + state->max_if_count;
213 if ((alias_index < 0) || (alias_index >= state->ipv4_count)) {
214 return SCNSWithFormat(@"Network information: IPv6 alias [%d] offset error (%d < 0 || %d >= %d)",
215 index,
216 alias_index,
217 alias_index,
218 state->ipv4_count);
219 }
220 }
221
222 // check if_list[] indices
223 for (index = 0, scan = nwi_state_if_list(state);
224 index < state->if_list_count;
225 index++, scan++) {
226 if (*scan >= (state->max_if_count * 2)) {
227 return SCNSWithFormat(@"Network information: if_list index error (%d > %d)",
228 *scan,
229 state->max_if_count * 2);
230 }
231 }
232
233 string = [NSMutableString string];
234 _nwi_state_log(state, TRUE, string);
235 if (string.length == 0) {
236 [string appendString:@"Network information: not available"];
237 }
238
239 return string;
240 }
241
242 struct SC_OSState_Formatters {
243 const char *type;
244 NS_RETURNS_RETAINED NSString * (*function)(uint32_t data_size, void *data);
245 };
246
247 NS_RETURNS_RETAINED
248 NSString *
249 OSStateCreateStringWithData(const char *type, uint32_t data_size, void *data)
250 {
251 // add functions for each type into this list
252 static const struct SC_OSState_Formatters formatters[] = {
253 { .type = "dnsinfo", .function = _SC_OSStateCopyFormattedString_dnsinfo },
254 { .type = "nwi", .function = _SC_OSStateCopyFormattedString_nwi },
255 };
256
257 for (int i = 0; i < (int)(sizeof(formatters) / sizeof(formatters[0])); i++) {
258 if (strcmp(type, formatters[i].type) == 0) {
259 return formatters[i].function(data_size, data);
260 }
261 }
262
263 return SCNSWithFormat(@"liblog_SystemConfiguration: Not yet supported os_state formatting type: %s", type);
264 }
265