1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2002-2015 Apple Inc. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef __mDNSMacOSX_h
19 #define __mDNSMacOSX_h
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
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>
43 #if TARGET_OS_EMBEDDED
44 #define NO_SECURITYFRAMEWORK 1
45 #define NO_CFUSERNOTIFICATION 1
46 #include <MobileGestalt.h> // for IsAppleTV()
49 #ifndef NO_SECURITYFRAMEWORK
50 #include <Security/SecureTransport.h>
51 #include <Security/Security.h>
52 #endif /* NO_SECURITYFRAMEWORK */
54 enum mDNSDynamicStoreSetConfigKey
56 kmDNSMulticastConfig
= 1,
59 kmDNSBackToMyMacConfig
,
60 kmDNSSleepProxyServersState
,
64 typedef struct NetworkInterfaceInfoOSX_struct NetworkInterfaceInfoOSX
;
66 typedef void (*KQueueEventCallback
)(int fd
, short filter
, void *context
, mDNSBool encounteredEOF
);
69 KQueueEventCallback KQcallback
;
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
;
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
94 struct UDPSocket_struct
96 KQSocketSet ss
; // First field of KQSocketSet has to be mDNSIPPort -- mDNSCore requires every UDPSocket_struct to begin with mDNSIPPort port
104 handshake_in_progress
,
106 handshake_to_be_closed
109 struct TCPSocket_struct
111 TCPSocketFlags flags
; // MUST BE FIRST FIELD -- mDNSCore expects every TCPSocket_struct to begin with TCPSocketFlags flags
112 TCPConnectionCallback callback
;
114 KQueueEntry
*kqEntry
;
116 #ifndef NO_SECURITYFRAMEWORK
117 SSLContextRef tlsContext
;
118 pthread_t handshake_thread
;
119 #endif /* NO_SECURITYFRAMEWORK */
124 handshakeStatus handshake
;
125 mDNS
*m
; // So we can call KQueueLock from the SSLHandshake thread
129 // Value assiged to 'Exists' to indicate the multicast state of the interface has changed.
130 #define MulticastStateChanged 2
132 struct NetworkInterfaceInfoOSX_struct
134 NetworkInterfaceInfo ifinfo
; // MUST be the first element in this structure
135 NetworkInterfaceInfoOSX
*next
;
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
153 int BPF_fd
; // -1 uninitialized; -2 requested BPF; -3 failed
154 int BPF_mcfd
; // Socket for our IPv6 ND group membership
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
;
162 CFRunLoopSourceRef BPF_rls
;
164 NetworkInterfaceInfoOSX
*Registered
; // non-NULL means registered with mDNS Core
167 struct mDNS_PlatformSupport_struct
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
182 mDNSs32 HostNameConflict
; // Time we experienced conflict on our link-local host name
183 mDNSs32 KeyChainTimer
;
185 SCDynamicStoreRef Store
;
186 CFRunLoopSourceRef StoreRLS
;
187 CFRunLoopSourceRef PMRLS
;
188 nwi_state_t NWIState
;
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
;
199 IOPMAssertionID IOPMAssertion
;
200 long SleepCookie
; // Cookie we need to pass to IOAllowPowerChange()
202 mDNSs32 RequestReSleep
;
203 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
204 dispatch_source_t timer
;
205 dispatch_source_t custom
;
207 pthread_mutex_t BigMutex
;
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
217 ProxyCallback
*UDPProxyCallback
;
218 ProxyCallback
*TCPProxyCallback
;
221 extern int OfferSleepProxyService
;
222 extern int DisableSleepProxyClient
;
223 extern int UseInternalSleepProxy
;
224 extern int OSXVers
, iOSVers
;
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);
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);
242 extern int KQueueSet(int fd
, u_short flags
, short filter
, const KQueueEntry
*const entryRef
);
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
);
251 extern mDNSBool
DictionaryIsEnabled(CFDictionaryRef dict
);
253 extern const char *DNSScopeToString(mDNSu32 scope
);
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
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).
271 extern int WatchDogReportingThreshold
;
273 struct CompileTimeAssertionChecks_mDNSMacOSX
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];
282 extern mDNSInterfaceID AWDLInterfaceID
;
283 void initializeD2DPlugins(mDNS
*const m
);
284 void terminateD2DPlugins(void);