]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCNetworkReachabilityLogging.h
configd-1061.141.1.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCNetworkReachabilityLogging.h
1 /*
2 * Copyright (c) 2017, 2018 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 #ifndef _SCNETWORKREACHABILITYLOGGING_H
25 #define _SCNETWORKREACHABILITYLOGGING_H
26
27 #include <os/availability.h>
28 #include <TargetConditionals.h>
29 #include <assert.h>
30 #include <sys/cdefs.h>
31 #include <SystemConfiguration/SystemConfiguration.h>
32
33 __BEGIN_DECLS
34
35 /*
36 * __SCNetworkReachability_flags_string()
37 *
38 * Returns a string representation of the SCNetworkReachability flags.
39 * debug==FALSE : " Reachable,Transient Connection,WWAN,..."
40 * debug==TRUE : " 0x01234567 (Reachable,Transient Connection,WWAN,...)"
41 */
42 static __inline__ void
43 __SCNetworkReachability_flags_string(SCNetworkReachabilityFlags flags, Boolean debug, char *str, size_t len)
44 {
45 size_t n;
46 size_t op; // open paren
47 SCNetworkReachabilityFlags remaining;
48
49 assert((len >= sizeof("Not Reachable," )) && // check min buffer size
50 (len >= sizeof("0x01234567 (Not Reachable)")) &&
51 (len >= sizeof("0x01234567 (0x01234567)" )));
52
53 if (!debug) {
54 n = 0;
55 str[n] = '\0';
56 } else {
57 n = snprintf(str, len, "0x%08x (", flags);
58 len--; // leave room for the closing paren
59 }
60 op = n;
61 remaining = flags;
62
63 if ((remaining == 0) &&
64 (n < len) && ((len - n) > sizeof("Not Reachable,"))) {
65 n = strlcat(str, "Not Reachable,", len);
66 }
67
68 if ((remaining & kSCNetworkReachabilityFlagsReachable) &&
69 (n < len) && ((len - n) > sizeof("Reachable,"))) {
70 n = strlcat(str, "Reachable,", len);
71 remaining &= ~kSCNetworkReachabilityFlagsReachable;
72 }
73
74 if ((remaining & kSCNetworkReachabilityFlagsTransientConnection) &&
75 (n < len) && ((len - n) > sizeof("Transient Connection,"))) {
76 n = strlcat(str, "Transient Connection,", len);
77 remaining &= ~kSCNetworkReachabilityFlagsTransientConnection;
78 }
79
80 if ((remaining & kSCNetworkReachabilityFlagsConnectionRequired) &&
81 (n < len) && ((len - n) > sizeof("Connection Required,"))) {
82 n = strlcat(str, "Connection Required,", len);
83 remaining &= ~kSCNetworkReachabilityFlagsConnectionRequired;
84 }
85
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;
90 }
91
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;
96 }
97
98 if ((remaining & kSCNetworkReachabilityFlagsInterventionRequired) &&
99 (n < len) && ((len - n) > sizeof("Intervention Required,"))) {
100 n = strlcat(str, "Intervention Required,", len);
101 remaining &= ~kSCNetworkReachabilityFlagsInterventionRequired;
102 }
103
104 if ((remaining & kSCNetworkReachabilityFlagsIsLocalAddress) &&
105 (n < len) && ((len - n) > sizeof("Local Address,"))) {
106 n = strlcat(str, "Local Address,", len);
107 remaining &= ~kSCNetworkReachabilityFlagsIsLocalAddress;
108 }
109
110 if ((remaining & kSCNetworkReachabilityFlagsIsDirect) &&
111 (n < len) && ((len - n) > sizeof("Directly Reachable Address,"))) {
112 n = strlcat(str, "Directly Reachable Address,", len);
113 remaining &= ~kSCNetworkReachabilityFlagsIsDirect;
114 }
115
116 #if TARGET_OS_IPHONE
117 if ((remaining & kSCNetworkReachabilityFlagsIsWWAN) &&
118 (n < len) && ((len - n) > sizeof("WWAN,"))) {
119 n = strlcat(str, "WWAN,", len);
120 remaining &= ~kSCNetworkReachabilityFlagsIsWWAN;
121 }
122 #endif // TARGET_OS_IPHONE
123
124 if (remaining != 0) {
125 if ((n >= len) ||
126 ((len - n) <= sizeof("0x01234567,"))) {
127 // if we don't have enough space, truncate and start over
128 str[op] = '\0';
129 n = op;
130 remaining = flags;
131 }
132
133 n += snprintf(str + n, len - n, "0x%08x,", remaining);
134 }
135
136 if (n-- > 0) {
137 if (!debug) {
138 str[n] = '\0'; // remove trailing ","
139 } else {
140 str[n] = ')'; // trailing "," --> ")"
141 }
142 }
143
144 return;
145 }
146
147 __END_DECLS
148
149 #endif /* _SCNETWORKREACHABILITYLOGGING_H */