2 * Copyright (c) 2017, 2018 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 _SCNETWORKREACHABILITYLOGGING_H
25 #define _SCNETWORKREACHABILITYLOGGING_H
27 #include <os/availability.h>
28 #include <TargetConditionals.h>
30 #include <sys/cdefs.h>
31 #include <SystemConfiguration/SystemConfiguration.h>
36 * __SCNetworkReachability_flags_string()
38 * Returns a string representation of the SCNetworkReachability flags.
39 * debug==FALSE : " Reachable,Transient Connection,WWAN,..."
40 * debug==TRUE : " 0x01234567 (Reachable,Transient Connection,WWAN,...)"
42 static __inline__
void
43 __SCNetworkReachability_flags_string(SCNetworkReachabilityFlags flags
, Boolean debug
, char *str
, size_t len
)
46 size_t op
; // open paren
47 SCNetworkReachabilityFlags remaining
;
49 assert((len
>= sizeof("Not Reachable," )) && // check min buffer size
50 (len
>= sizeof("0x01234567 (Not Reachable)")) &&
51 (len
>= sizeof("0x01234567 (0x01234567)" )));
57 n
= snprintf(str
, len
, "0x%08x (", flags
);
58 len
--; // leave room for the closing paren
63 if ((remaining
== 0) &&
64 (n
< len
) && ((len
- n
) > sizeof("Not Reachable,"))) {
65 n
= strlcat(str
, "Not Reachable,", len
);
68 if ((remaining
& kSCNetworkReachabilityFlagsReachable
) &&
69 (n
< len
) && ((len
- n
) > sizeof("Reachable,"))) {
70 n
= strlcat(str
, "Reachable,", len
);
71 remaining
&= ~kSCNetworkReachabilityFlagsReachable
;
74 if ((remaining
& kSCNetworkReachabilityFlagsTransientConnection
) &&
75 (n
< len
) && ((len
- n
) > sizeof("Transient Connection,"))) {
76 n
= strlcat(str
, "Transient Connection,", len
);
77 remaining
&= ~kSCNetworkReachabilityFlagsTransientConnection
;
80 if ((remaining
& kSCNetworkReachabilityFlagsConnectionRequired
) &&
81 (n
< len
) && ((len
- n
) > sizeof("Connection Required,"))) {
82 n
= strlcat(str
, "Connection Required,", len
);
83 remaining
&= ~kSCNetworkReachabilityFlagsConnectionRequired
;
86 if ((remaining
& kSCNetworkReachabilityFlagsConnectionOnTraffic
) &&
87 (n
< len
) && ((len
- n
) > sizeof("Automatic Connection On Traffic,"))) {
88 n
= strlcat(str
, "Automatic Connection On Traffic,", len
);
89 remaining
&= ~kSCNetworkReachabilityFlagsConnectionOnTraffic
;
92 if ((remaining
& kSCNetworkReachabilityFlagsConnectionOnDemand
) &&
93 (n
< len
) && ((len
- n
) > sizeof("Automatic Connection On Demand,"))) {
94 n
= strlcat(str
, "Automatic Connection On Demand,", len
);
95 remaining
&= ~kSCNetworkReachabilityFlagsConnectionOnDemand
;
98 if ((remaining
& kSCNetworkReachabilityFlagsInterventionRequired
) &&
99 (n
< len
) && ((len
- n
) > sizeof("Intervention Required,"))) {
100 n
= strlcat(str
, "Intervention Required,", len
);
101 remaining
&= ~kSCNetworkReachabilityFlagsInterventionRequired
;
104 if ((remaining
& kSCNetworkReachabilityFlagsIsLocalAddress
) &&
105 (n
< len
) && ((len
- n
) > sizeof("Local Address,"))) {
106 n
= strlcat(str
, "Local Address,", len
);
107 remaining
&= ~kSCNetworkReachabilityFlagsIsLocalAddress
;
110 if ((remaining
& kSCNetworkReachabilityFlagsIsDirect
) &&
111 (n
< len
) && ((len
- n
) > sizeof("Directly Reachable Address,"))) {
112 n
= strlcat(str
, "Directly Reachable Address,", len
);
113 remaining
&= ~kSCNetworkReachabilityFlagsIsDirect
;
117 if ((remaining
& kSCNetworkReachabilityFlagsIsWWAN
) &&
118 (n
< len
) && ((len
- n
) > sizeof("WWAN,"))) {
119 n
= strlcat(str
, "WWAN,", len
);
120 remaining
&= ~kSCNetworkReachabilityFlagsIsWWAN
;
122 #endif // TARGET_OS_IPHONE
124 if (remaining
!= 0) {
126 ((len
- n
) <= sizeof("0x01234567,"))) {
127 // if we don't have enough space, truncate and start over
133 n
+= snprintf(str
+ n
, len
- n
, "0x%08x,", remaining
);
138 str
[n
] = '\0'; // remove trailing ","
140 str
[n
] = ')'; // trailing "," --> ")"
149 #endif /* _SCNETWORKREACHABILITYLOGGING_H */