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@
24 #ifndef _NETWORK_STATE_INFORMATION_LOGGING_H
25 #define _NETWORK_STATE_INFORMATION_LOGGING_H
27 #include <Availability.h>
28 #include <TargetConditionals.h>
29 #include <sys/cdefs.h>
30 #include <CoreFoundation/CoreFoundation.h>
31 #include <SystemConfiguration/SystemConfiguration.h>
32 #include <SystemConfiguration/SCPrivate.h>
34 #include <network_information.h>
35 #include "network_state_information_priv.c"
40 #define my_log(__level, __format, ...) SC_log(__level, __format, ## __VA_ARGS__)
41 #define MY_LOG_DEFINED_LOCALLY
44 #ifndef my_log_context_type
45 #define my_log_context_type void *
46 #define MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY
47 #endif // !my_log_context_type
49 #ifndef my_log_context_name
50 #define my_log_context_name context
51 #define MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY
52 #endif // !my_log_context_name
54 #include "SCNetworkReachabilityLogging.h"
57 * _nwi_ifstate_flags_str()
59 * Returns a string representation of the nwi_state flags.
60 * "(IPv4,IPv6,DNS,...)"
62 static __inline__
void
63 _nwi_ifstate_flags_str(nwi_ifstate_flags flags
, char *str
, size_t len
)
66 nwi_ifstate_flags remaining
;
68 assert(len
>= sizeof("(0x01234567)")); // check min buffer size
70 flags
&= NWI_IFSTATE_FLAGS_MASK
;
76 len
--; // leave room for the closing paren
77 n
= strlcpy(str
, "(", len
);
80 if ((remaining
& NWI_IFSTATE_FLAGS_HAS_IPV4
) &&
81 (n
< len
) && ((len
- n
) > sizeof("IPv4,"))) {
82 n
= strlcat(str
, "IPv4,", len
);
83 remaining
&= ~NWI_IFSTATE_FLAGS_HAS_IPV4
;
86 if ((remaining
& NWI_IFSTATE_FLAGS_HAS_IPV6
) &&
87 (n
< len
) && ((len
- n
) > sizeof("IPv6,"))) {
88 n
= strlcat(str
, "IPv6,", len
);
89 remaining
&= ~NWI_IFSTATE_FLAGS_HAS_IPV6
;
92 if ((remaining
& NWI_IFSTATE_FLAGS_HAS_DNS
) &&
93 (n
< len
) && ((len
- n
) > sizeof("DNS,"))) {
94 n
= strlcat(str
, "DNS,", len
);
95 remaining
&= ~NWI_IFSTATE_FLAGS_HAS_DNS
;
98 if ((remaining
& NWI_IFSTATE_FLAGS_NOT_IN_LIST
) &&
99 (n
< len
) && ((len
- n
) > sizeof("NOT-IN-LIST,"))) {
100 n
= strlcat(str
, "NOT-IN-LIST,", len
);
101 remaining
&= ~NWI_IFSTATE_FLAGS_NOT_IN_LIST
;
104 if ((remaining
& NWI_IFSTATE_FLAGS_HAS_SIGNATURE
) &&
105 (n
< len
) && ((len
- n
) > sizeof("SIGNATURE,"))) {
106 n
= strlcat(str
, "SIGNATURE,", len
);
107 remaining
&= ~NWI_IFSTATE_FLAGS_HAS_SIGNATURE
;
110 if ((remaining
& NWI_IFSTATE_FLAGS_NOT_IN_IFLIST
) &&
111 (n
< len
) && ((len
- n
) > sizeof("NOT-IN-IFLIST,"))) {
112 n
= strlcat(str
, "NOT-IN-IFLIST,", len
);
113 remaining
&= ~NWI_IFSTATE_FLAGS_NOT_IN_IFLIST
;
116 if (remaining
!= 0) {
118 ((len
- n
) <= sizeof("0x01234567,"))) {
119 // if we don't have enough space, truncate and start over
120 n
= strlcpy(str
, "(", len
);
124 n
+= snprintf(str
+ n
, len
- n
, ",%p", (void *)remaining
);
128 str
[n
] = ')'; // trailing "," --> ")"
134 static __inline__
const char *
135 _nwi_ifstate_rank_str(Rank rank
)
137 const char *str
= "???";
139 switch (RANK_ASSERTION_MASK(rank
)) {
140 case kRankAssertionFirst
:
143 case kRankAssertionDefault
:
146 case kRankAssertionLast
:
149 case kRankAssertionNever
:
152 case kRankAssertionScoped
:
163 static __inline__
void
164 _nwi_ifstate_log(nwi_ifstate_t ifstate
, boolean_t debug
, my_log_context_type my_log_context_name
)
166 #if defined(MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY) && defined(MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY)
167 #pragma unused(my_log_context_name)
171 const struct in_addr
*ia
;
172 const struct in6_addr
*ia6
;
174 char addr_str
[INET6_ADDRSTRLEN
];
175 nwi_ifstate_flags flags_ifstate
;
178 SCNetworkReachabilityFlags reach_flags
;
180 const struct sockaddr
*vpn_addr
;
183 flags_ifstate
= nwi_ifstate_get_flags(ifstate
);
185 flags_ifstate
|= NWI_IFSTATE_FLAGS(ifstate
->flags
);
187 flags_ifstate
&= ~NWI_IFSTATE_FLAGS_HAS_SIGNATURE
; // exclude flag ('cause we'll report the signature only if present)
188 _nwi_ifstate_flags_str(flags_ifstate
, flags_str
, sizeof(flags_str
));
191 if_name
= nwi_ifstate_get_ifname(ifstate
);
193 // reachability flags
194 reach_flags
= nwi_ifstate_get_reachability_flags(ifstate
);
197 addr
.bytes
= nwi_ifstate_get_address(ifstate
);
198 if (inet_ntop(ifstate
->af
, addr
.bytes
, addr_str
, sizeof(addr_str
)) == NULL
) {
199 strlcpy(addr_str
, "???", sizeof(addr_str
));
202 // verbose format (e.g. scutil)
203 my_log(LOG_INFO
, " %7s : flags : %p %s",
205 (void *)flags_ifstate
,
208 my_log(LOG_INFO
, " address : %s", addr_str
);
210 // VPN server address
211 vpn_addr
= nwi_ifstate_get_vpn_server(ifstate
);
212 if (vpn_addr
!= NULL
) {
213 char vpn_str
[INET6_ADDRSTRLEN
];
215 _SC_sockaddr_to_string(vpn_addr
, vpn_str
, sizeof(vpn_str
));
216 my_log(LOG_INFO
, " VPN server : %s", vpn_str
);
220 __SCNetworkReachability_flags_string(reach_flags
, TRUE
, reach_str
, sizeof(reach_str
));
221 my_log(LOG_INFO
, " reach : %s", reach_str
);
227 const char *rank_str
;
228 const uint8_t *signature
;
229 int signature_length
;
232 rank
= ifstate
->rank
;
233 rank_str
= _nwi_ifstate_rank_str(rank
);
234 rank_index
= RANK_INDEX_MASK(rank
);
235 if (rank_index
!= kRankIndexMask
) {
236 my_log(LOG_INFO
, " rank : 0x%08x (%s, %u)", rank
, rank_str
, rank_index
);
238 my_log(LOG_INFO
, " rank : 0x%08x (%s, Last)", rank
, rank_str
);
242 signature
= nwi_ifstate_get_signature(ifstate
, AF_UNSPEC
, &signature_length
);
243 if (signature
!= NULL
) {
246 digest
= CFDataCreate(NULL
, signature
, CC_SHA1_DIGEST_LENGTH
);
247 my_log(LOG_INFO
, " signature : %@", digest
);
252 generation
= nwi_ifstate_get_generation(ifstate
);
253 my_log(LOG_INFO
, " generation : %llu", generation
);
259 static __inline__
void
260 _nwi_state_reachability_log(nwi_state_t state
, boolean_t debug
, int af
, my_log_context_type my_log_context_name
)
262 #if defined(MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY) && defined(MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY)
263 #pragma unused(my_log_context_name)
268 flags
= nwi_state_get_reachability_flags(state
, af
);
269 __SCNetworkReachability_flags_string(flags
, TRUE
, flags_str
, sizeof(flags_str
));
271 my_log(LOG_INFO
, "%s", "");
273 my_log(LOG_INFO
, " REACH : flags %s", flags_str
);
278 static __inline__
void
279 _nwi_state_log(nwi_state_t state
, boolean_t debug
, my_log_context_type my_log_context_name
)
281 #if defined(MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY) && defined(MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY)
282 #pragma unused(my_log_context_name)
286 nwi_ifstate_t ifstate
;
289 my_log(LOG_INFO
, "%s", "Network information");
292 "Network information (generation %llu size=%lu)",
293 nwi_state_get_generation(state
),
294 nwi_state_size(state
));
299 // show regular interfaces
300 my_log(LOG_INFO
, "%s", "");
301 my_log(LOG_INFO
, "%s", "IPv4 network interface information");
302 ifstate
= nwi_state_get_first_ifstate(state
, AF_INET
);
303 if (ifstate
== NULL
) {
304 my_log(LOG_INFO
, "%s", " No IPv4 states found");
306 while (ifstate
!= NULL
) {
307 _nwi_ifstate_log(ifstate
, debug
, my_log_context_name
);
308 ifstate
= nwi_ifstate_get_next(ifstate
, AF_INET
);
312 my_log(LOG_INFO
, "%s", "IPv4 network interface information");
313 if (state
->ipv4_count
> 0) {
314 // show ALL interfaces
315 for (i
= 0, ifstate
= nwi_state_ifstate_list(state
, AF_INET
);
316 i
< state
->ipv4_count
; i
++, ifstate
++) {
317 _nwi_ifstate_log(ifstate
, debug
, my_log_context_name
);
320 my_log(LOG_INFO
, "%s", " No IPv4 states found");
323 _nwi_state_reachability_log(state
, debug
, AF_INET
, my_log_context_name
);
327 // show regular interfaces
328 my_log(LOG_INFO
, "%s", "");
329 my_log(LOG_INFO
, "%s", "IPv6 network interface information");
330 ifstate
= nwi_state_get_first_ifstate(state
, AF_INET6
);
331 if (ifstate
== NULL
) {
332 my_log(LOG_INFO
, "%s", " No IPv6 states found\n");
334 while (ifstate
!= NULL
) {
335 _nwi_ifstate_log(ifstate
, debug
, my_log_context_name
);
336 ifstate
= nwi_ifstate_get_next(ifstate
, AF_INET6
);
340 my_log(LOG_INFO
, "%s", "IPv6 network interface information");
341 if (state
->ipv6_count
> 0) {
342 // show ALL interfaces
343 for (i
= 0, ifstate
= nwi_state_ifstate_list(state
, AF_INET6
);
344 i
< state
->ipv6_count
; i
++, ifstate
++) {
345 _nwi_ifstate_log(ifstate
, debug
, my_log_context_name
);
348 my_log(LOG_INFO
, "%s", " No IPv6 states found\n");
351 _nwi_state_reachability_log(state
, debug
, AF_INET6
, my_log_context_name
);
353 count
= nwi_state_get_interface_names(state
, NULL
, 0);
355 const char *names
[count
];
357 count
= nwi_state_get_interface_names(state
, names
, count
);
359 char str
[count
* (IFNAMSIZ
+ 1)];
361 memset(str
, 0, sizeof(str
));
362 for (unsigned int i
= 0; i
< count
; i
++) {
364 strlcat(str
, " ", sizeof(str
));
366 strlcat(str
, names
[i
], sizeof(str
));
370 my_log(LOG_INFO
, "%s", "");
372 my_log(LOG_INFO
, "Network interfaces: %s", str
);
379 #ifdef MY_LOG_DEFINED_LOCALLY
381 #undef MY_LOG_DEFINED_LOCALLY
382 #endif // MY_LOG_DEFINED_LOCALLY
384 #ifdef MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY
385 #undef my_log_context_type
386 #undef MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY
387 #endif // MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY
389 #ifdef MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY
390 #undef my_log_context_name
391 #undef MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY
392 #endif // MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY
396 #endif // _NETWORK_STATE_INFORMATION_LOGGING_H