/*
- * Copyright (c) 2017 Apple Inc. All rights reserved.
+ * Copyright (c) 2017, 2020 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
static NS_RETURNS_RETAINED NSString *
_SC_OSStateCopyFormattedString_nwi(uint32_t data_size, void *data)
{
+ nwi_ifindex_t index;
+ nwi_ifindex_t *scan;
nwi_state_t state = (nwi_state_t)data;
NSMutableString *string;
if ((data_size == 0) || (data == NULL)) {
return @"No network information";
- } else if (data_size < sizeof(nwi_state)) {
+ }
+
+ if (data_size < sizeof(nwi_state)) {
return SCNSWithFormat(@"Network information: size error (%d < %zd)",
data_size,
sizeof(_dns_config_buf_t));
- } else if (state->version != NWI_STATE_VERSION) {
+ }
+
+ if (state->version != NWI_STATE_VERSION) {
return SCNSWithFormat(@"Network information: version error (%d != %d)",
state->version,
NWI_STATE_VERSION);
}
+ if (data_size != nwi_state_size(state)) {
+ return SCNSWithFormat(@"Network information: size error (%d != %zd)",
+ data_size,
+ nwi_state_size(state));
+ }
+
+ if (state->ipv4_count > state->max_if_count) {
+ return SCNSWithFormat(@"Network information: ipv4 count error (%d > %d)",
+ state->ipv4_count,
+ state->max_if_count);
+ }
+
+ if (state->ipv6_count > state->max_if_count) {
+ return SCNSWithFormat(@"Network information: ipv6 count error (%d > %d)",
+ state->ipv6_count,
+ state->max_if_count);
+ }
+
+ if (state->if_list_count > state->max_if_count) {
+ return SCNSWithFormat(@"Network information: if_list count error (%d > %d)",
+ state->if_list_count,
+ state->max_if_count);
+ }
+
+ for (index = 0; index < state->ipv4_count; index++) {
+ nwi_ifindex_t alias_index;
+ nwi_ifindex_t alias_offset = state->ifstate_list[index].af_alias_offset;
+
+ if (alias_offset == 0) {
+ continue;
+ }
+ alias_index = alias_offset + index - state->max_if_count;
+ if ((alias_index < 0) || (alias_index >= state->ipv6_count)) {
+ return SCNSWithFormat(@"Network information: IPv4 alias [%d] offset error (%d < 0 || %d >= %d)",
+ index,
+ alias_index,
+ alias_index,
+ state->ipv6_count);
+ }
+ }
+
+ for (index = 0; index < state->ipv6_count; index++) {
+ nwi_ifindex_t alias_index;
+ nwi_ifindex_t alias_offset = state->ifstate_list[state->max_if_count + index].af_alias_offset;
+
+ if (alias_offset == 0) {
+ continue;
+ }
+ alias_index = alias_offset + index + state->max_if_count;
+ if ((alias_index < 0) || (alias_index >= state->ipv4_count)) {
+ return SCNSWithFormat(@"Network information: IPv6 alias [%d] offset error (%d < 0 || %d >= %d)",
+ index,
+ alias_index,
+ alias_index,
+ state->ipv4_count);
+ }
+ }
+
+ // check if_list[] indices
+ for (index = 0, scan = nwi_state_if_list(state);
+ index < state->if_list_count;
+ index++, scan++) {
+ if (*scan >= (state->max_if_count * 2)) {
+ return SCNSWithFormat(@"Network information: if_list index error (%d > %d)",
+ *scan,
+ state->max_if_count * 2);
+ }
+ }
+
string = [NSMutableString string];
_nwi_state_log(state, TRUE, string);
if (string.length == 0) {