]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/mDNSMacOSX.h
mDNSResponder-878.270.2.tar.gz
[apple/mdnsresponder.git] / mDNSMacOSX / mDNSMacOSX.h
1 /* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2002-2015 Apple Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #ifndef __mDNSMacOSX_h
19 #define __mDNSMacOSX_h
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24
25 #include <SystemConfiguration/SystemConfiguration.h>
26 #include <IOKit/pwr_mgt/IOPM.h>
27 #include <IOKit/pwr_mgt/IOPMLib.h>
28 #include <IOKit/pwr_mgt/IOPMLibPrivate.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <network_information.h> // for nwi_state
32 #include "mDNSEmbeddedAPI.h" // for domain name structure
33
34 #include <net/if.h>
35 #include <os/log.h>
36
37 //#define MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
38 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
39 #include <dispatch/dispatch.h>
40 #include <dispatch/private.h>
41 #endif
42
43 #if TARGET_OS_EMBEDDED
44 #define NO_SECURITYFRAMEWORK 1
45 #define NO_CFUSERNOTIFICATION 1
46 #include <MobileGestalt.h> // for IsAppleTV()
47 #endif
48
49 #ifndef NO_SECURITYFRAMEWORK
50 #include <Security/SecureTransport.h>
51 #include <Security/Security.h>
52 #endif /* NO_SECURITYFRAMEWORK */
53
54 enum mDNSDynamicStoreSetConfigKey
55 {
56 kmDNSMulticastConfig = 1,
57 kmDNSDynamicConfig,
58 kmDNSPrivateConfig,
59 kmDNSBackToMyMacConfig,
60 kmDNSSleepProxyServersState,
61 kmDNSDebugState,
62 };
63
64 typedef struct NetworkInterfaceInfoOSX_struct NetworkInterfaceInfoOSX;
65
66 typedef void (*KQueueEventCallback)(int fd, short filter, void *context, mDNSBool encounteredEOF);
67 typedef struct
68 {
69 KQueueEventCallback KQcallback;
70 void *KQcontext;
71 const char *KQtask; // For debugging messages
72 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
73 dispatch_source_t readSource;
74 dispatch_source_t writeSource;
75 mDNSBool fdClosed;
76 #endif
77 } KQueueEntry;
78
79 typedef struct
80 {
81 mDNSIPPort port; // MUST BE FIRST FIELD -- UDPSocket_struct begins with a KQSocketSet,
82 // and mDNSCore requires every UDPSocket_struct to begin with a mDNSIPPort port
83 mDNS *m;
84 int sktv4;
85 KQueueEntry kqsv4;
86 int sktv6;
87 KQueueEntry kqsv6;
88 int *closeFlag;
89 mDNSBool proxy;
90 mDNSBool sktv4EOF;
91 mDNSBool sktv6EOF;
92 } KQSocketSet;
93
94 struct UDPSocket_struct
95 {
96 KQSocketSet ss; // First field of KQSocketSet has to be mDNSIPPort -- mDNSCore requires every UDPSocket_struct to begin with mDNSIPPort port
97 };
98
99 // TCP socket support
100
101 typedef enum
102 {
103 handshake_required,
104 handshake_in_progress,
105 handshake_completed,
106 handshake_to_be_closed
107 } handshakeStatus;
108
109 struct TCPSocket_struct
110 {
111 TCPSocketFlags flags; // MUST BE FIRST FIELD -- mDNSCore expects every TCPSocket_struct to begin with TCPSocketFlags flags
112 TCPConnectionCallback callback;
113 int fd;
114 KQueueEntry *kqEntry;
115 KQSocketSet ss;
116 #ifndef NO_SECURITYFRAMEWORK
117 SSLContextRef tlsContext;
118 pthread_t handshake_thread;
119 #endif /* NO_SECURITYFRAMEWORK */
120 domainname hostname;
121 void *context;
122 mDNSBool setup;
123 mDNSBool connected;
124 handshakeStatus handshake;
125 mDNS *m; // So we can call KQueueLock from the SSLHandshake thread
126 mStatus err;
127 };
128
129 // Value assiged to 'Exists' to indicate the multicast state of the interface has changed.
130 #define MulticastStateChanged 2
131
132 struct NetworkInterfaceInfoOSX_struct
133 {
134 NetworkInterfaceInfo ifinfo; // MUST be the first element in this structure
135 NetworkInterfaceInfoOSX *next;
136 mDNS *m;
137 mDNSu8 Exists; // 1 = currently exists in getifaddrs list; 0 = doesn't
138 // 2 = exists, but McastTxRx state changed
139 mDNSu8 Flashing; // Set if interface appeared for less than 60 seconds and then vanished
140 mDNSu8 Occulting; // Set if interface vanished for less than 60 seconds and then came back
141 mDNSu8 D2DInterface; // IFEF_LOCALNET_PRIVATE flag indicates we should call
142 // D2D plugin for operations over this interface
143 mDNSs32 AppearanceTime; // Time this interface appeared most recently in getifaddrs list
144 // i.e. the first time an interface is seen, AppearanceTime is set.
145 // If an interface goes away temporarily and then comes back then
146 // AppearanceTime is updated to the time of the most recent appearance.
147 mDNSs32 LastSeen; // If Exists==0, last time this interface appeared in getifaddrs list
148 unsigned int ifa_flags;
149 struct in_addr ifa_v4addr;
150 mDNSu32 scope_id; // interface index / IPv6 scope ID
151 mDNSEthAddr BSSID; // BSSID of 802.11 base station, if applicable
152 u_short sa_family;
153 int BPF_fd; // -1 uninitialized; -2 requested BPF; -3 failed
154 int BPF_mcfd; // Socket for our IPv6 ND group membership
155 u_int BPF_len;
156 mDNSBool isExpensive; // True if this interface has the IFEF_EXPENSIVE flag set.
157 mDNSBool isAWDL; // True if this interface has the IFEF_AWDL flag set.
158 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
159 dispatch_source_t BPF_source;
160 #else
161 CFSocketRef BPF_cfs;
162 CFRunLoopSourceRef BPF_rls;
163 #endif
164 NetworkInterfaceInfoOSX *Registered; // non-NULL means registered with mDNS Core
165 };
166
167 struct mDNS_PlatformSupport_struct
168 {
169 NetworkInterfaceInfoOSX *InterfaceList;
170 KQSocketSet permanentsockets;
171 int num_mcasts; // Number of multicasts received during this CPU scheduling period (used for CPU limiting)
172 domainlabel userhostlabel; // The hostlabel as it was set in System Preferences the last time we looked
173 domainlabel usernicelabel; // The nicelabel as it was set in System Preferences the last time we looked
174 // Following four variables are used for optimization where the helper is not
175 // invoked when not needed. It records the state of what we told helper the
176 // last time we invoked mDNSPreferencesSetName
177 domainlabel prevoldhostlabel; // Previous m->p->userhostlabel
178 domainlabel prevnewhostlabel; // Previous m->hostlabel
179 domainlabel prevoldnicelabel; // Previous m->p->usernicelabel
180 domainlabel prevnewnicelabel; // Previous m->nicelabel
181 mDNSs32 NotifyUser;
182 mDNSs32 HostNameConflict; // Time we experienced conflict on our link-local host name
183 mDNSs32 KeyChainTimer;
184
185 SCDynamicStoreRef Store;
186 CFRunLoopSourceRef StoreRLS;
187 CFRunLoopSourceRef PMRLS;
188 nwi_state_t NWIState;
189 int NWINotifyToken;
190 mDNSBool NWINotifyRegistered;
191 int SysEventNotifier;
192 KQueueEntry SysEventKQueue;
193 IONotificationPortRef PowerPortRef;
194 io_connect_t PowerConnection;
195 io_object_t PowerNotifier;
196 #ifdef kIOPMAcknowledgmentOptionSystemCapabilityRequirements
197 IOPMConnection IOPMConnection;
198 #endif
199 IOPMAssertionID IOPMAssertion;
200 long SleepCookie; // Cookie we need to pass to IOAllowPowerChange()
201 long WakeAtUTC;
202 mDNSs32 RequestReSleep;
203 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
204 dispatch_source_t timer;
205 dispatch_source_t custom;
206 #else
207 pthread_mutex_t BigMutex;
208 #endif
209 mDNSs32 BigMutexStartTime;
210 int WakeKQueueLoopFD;
211 mDNSu8 v4answers; // non-zero if we are receiving answers
212 mDNSu8 v6answers; // for A/AAAA from external DNS servers
213 mDNSs32 DNSTrigger; // Time the DNSTrigger was given
214 uint64_t LastConfigGeneration; // DNS configuration generation number
215 UDPSocket UDPProxy;
216 TCPSocket TCPProxy;
217 ProxyCallback *UDPProxyCallback;
218 ProxyCallback *TCPProxyCallback;
219 };
220
221 extern int OfferSleepProxyService;
222 extern int DisableSleepProxyClient;
223 extern int UseInternalSleepProxy;
224 extern int OSXVers, iOSVers;
225
226 extern int KQueueFD;
227
228 extern void NotifyOfElusiveBug(const char *title, const char *msg); // Both strings are UTF-8 text
229 extern void SetDomainSecrets(mDNS *m);
230 extern void mDNSMacOSXNetworkChanged(void);
231 extern void mDNSMacOSXSystemBuildNumber(char *HINFO_SWstring);
232 extern NetworkInterfaceInfoOSX *IfindexToInterfaceInfoOSX(mDNSInterfaceID ifindex);
233 extern void mDNSUpdatePacketFilter(const ResourceRecord *const excludeRecord);
234 extern void myKQSocketCallBack(int s1, short filter, void *context, mDNSBool encounteredEOF);
235 extern void mDNSDynamicStoreSetConfig(int key, const char *subkey, CFPropertyListRef value);
236 extern void UpdateDebugState(void);
237
238 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
239 extern int KQueueSet(int fd, u_short flags, short filter, KQueueEntry *const entryRef);
240 mDNSexport void TriggerEventCompletion(void);
241 #else
242 extern int KQueueSet(int fd, u_short flags, short filter, const KQueueEntry *const entryRef);
243 #endif
244
245 // When events are processed on the non-kqueue thread (i.e. CFRunLoop notifications like Sleep/Wake,
246 // Interface changes, Keychain changes, etc.) they must use KQueueLock/KQueueUnlock to lock out the kqueue thread
247 extern void KQueueLock(void);
248 extern void KQueueUnlock(const char* task);
249 extern void mDNSPlatformCloseFD(KQueueEntry *kq, int fd);
250
251 extern mDNSBool DictionaryIsEnabled(CFDictionaryRef dict);
252
253 extern const char *DNSScopeToString(mDNSu32 scope);
254
255 // If any event takes more than WatchDogReportingThreshold milliseconds to be processed, we log a warning message
256 // General event categories are:
257 // o Mach client request initiated / terminated
258 // o UDS client request
259 // o Handling UDP packets received from the network
260 // o Environmental change events:
261 // - network interface changes
262 // - sleep/wake
263 // - keychain changes
264 // o Name conflict dialog dismissal
265 // o Reception of Unix signal (e.g. SIGINFO)
266 // o Idle task processing
267 // If we find that we're getting warnings for any of these categories, and it's not evident
268 // what's causing the problem, we may need to subdivide some categories into finer-grained
269 // sub-categories (e.g. "Idle task processing" covers a pretty broad range of sub-tasks).
270
271 extern int WatchDogReportingThreshold;
272
273 struct CompileTimeAssertionChecks_mDNSMacOSX
274 {
275 // Check our structures are reasonable sizes. Including overly-large buffers, or embedding
276 // other overly-large structures instead of having a pointer to them, can inadvertently
277 // cause structure sizes (and therefore memory usage) to balloon unreasonably.
278 char sizecheck_NetworkInterfaceInfoOSX[(sizeof(NetworkInterfaceInfoOSX) <= 8488) ? 1 : -1];
279 char sizecheck_mDNS_PlatformSupport [(sizeof(mDNS_PlatformSupport) <= 1378) ? 1 : -1];
280 };
281
282 extern mDNSInterfaceID AWDLInterfaceID;
283 void initializeD2DPlugins(mDNS *const m);
284 void terminateD2DPlugins(void);
285
286 #ifdef __cplusplus
287 }
288 #endif
289
290 #endif