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 "mDNSEmbeddedAPI.h" // for domain name structure
36 //#define MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
37 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
38 #include <dispatch/dispatch.h>
39 #include <dispatch/private.h>
42 #if TARGET_OS_EMBEDDED
43 #define NO_SECURITYFRAMEWORK 1
44 #define NO_CFUSERNOTIFICATION 1
45 #include <MobileGestalt.h> // for IsAppleTV()
48 #ifndef NO_SECURITYFRAMEWORK
49 #include <Security/SecureTransport.h>
50 #include <Security/Security.h>
51 #endif /* NO_SECURITYFRAMEWORK */
53 enum mDNSDynamicStoreSetConfigKey
55 kmDNSMulticastConfig
= 1,
58 kmDNSBackToMyMacConfig
,
59 kmDNSSleepProxyServersState
,
63 typedef struct NetworkInterfaceInfoOSX_struct NetworkInterfaceInfoOSX
;
65 typedef void (*KQueueEventCallback
)(int fd
, short filter
, void *context
, mDNSBool encounteredEOF
);
68 KQueueEventCallback KQcallback
;
70 const char *KQtask
; // For debugging messages
71 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
72 dispatch_source_t readSource
;
73 dispatch_source_t writeSource
;
80 mDNSIPPort port
; // MUST BE FIRST FIELD -- UDPSocket_struct begins with a KQSocketSet,
81 // and mDNSCore requires every UDPSocket_struct to begin with a mDNSIPPort port
93 struct UDPSocket_struct
95 KQSocketSet ss
; // First field of KQSocketSet has to be mDNSIPPort -- mDNSCore requires every UDPSocket_struct to begin with mDNSIPPort port
103 handshake_in_progress
,
105 handshake_to_be_closed
108 struct TCPSocket_struct
110 TCPSocketFlags flags
; // MUST BE FIRST FIELD -- mDNSCore expects every TCPSocket_struct to begin with TCPSocketFlags flags
111 TCPConnectionCallback callback
;
113 KQueueEntry
*kqEntry
;
115 #ifndef NO_SECURITYFRAMEWORK
116 SSLContextRef tlsContext
;
117 pthread_t handshake_thread
;
118 #endif /* NO_SECURITYFRAMEWORK */
123 handshakeStatus handshake
;
124 mDNS
*m
; // So we can call KQueueLock from the SSLHandshake thread
128 // Value assiged to 'Exists' to indicate the multicast state of the interface has changed.
129 #define MulticastStateChanged 2
131 struct NetworkInterfaceInfoOSX_struct
133 NetworkInterfaceInfo ifinfo
; // MUST be the first element in this structure
134 NetworkInterfaceInfoOSX
*next
;
136 mDNSu8 Exists
; // 1 = currently exists in getifaddrs list; 0 = doesn't
137 // 2 = exists, but McastTxRx state changed
138 mDNSu8 Flashing
; // Set if interface appeared for less than 60 seconds and then vanished
139 mDNSu8 Occulting
; // Set if interface vanished for less than 60 seconds and then came back
140 mDNSu8 D2DInterface
; // IFEF_LOCALNET_PRIVATE flag indicates we should call
141 // D2D plugin for operations over this interface
142 mDNSs32 AppearanceTime
; // Time this interface appeared most recently in getifaddrs list
143 // i.e. the first time an interface is seen, AppearanceTime is set.
144 // If an interface goes away temporarily and then comes back then
145 // AppearanceTime is updated to the time of the most recent appearance.
146 mDNSs32 LastSeen
; // If Exists==0, last time this interface appeared in getifaddrs list
147 unsigned int ifa_flags
;
148 struct in_addr ifa_v4addr
;
149 mDNSu32 scope_id
; // interface index / IPv6 scope ID
150 mDNSEthAddr BSSID
; // BSSID of 802.11 base station, if applicable
152 int BPF_fd
; // -1 uninitialized; -2 requested BPF; -3 failed
153 int BPF_mcfd
; // Socket for our IPv6 ND group membership
155 mDNSBool isExpensive
; // True if this interface has the IFEF_EXPENSIVE flag set.
156 mDNSBool isAWDL
; // True if this interface has the IFEF_AWDL flag set.
157 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
158 dispatch_source_t BPF_source
;
161 CFRunLoopSourceRef BPF_rls
;
163 NetworkInterfaceInfoOSX
*Registered
; // non-NULL means registered with mDNS Core
166 struct mDNS_PlatformSupport_struct
168 NetworkInterfaceInfoOSX
*InterfaceList
;
169 KQSocketSet permanentsockets
;
170 int num_mcasts
; // Number of multicasts received during this CPU scheduling period (used for CPU limiting)
171 domainlabel userhostlabel
; // The hostlabel as it was set in System Preferences the last time we looked
172 domainlabel usernicelabel
; // The nicelabel as it was set in System Preferences the last time we looked
173 // Following four variables are used for optimization where the helper is not
174 // invoked when not needed. It records the state of what we told helper the
175 // last time we invoked mDNSPreferencesSetName
176 domainlabel prevoldhostlabel
; // Previous m->p->userhostlabel
177 domainlabel prevnewhostlabel
; // Previous m->hostlabel
178 domainlabel prevoldnicelabel
; // Previous m->p->usernicelabel
179 domainlabel prevnewnicelabel
; // Previous m->nicelabel
181 mDNSs32 HostNameConflict
; // Time we experienced conflict on our link-local host name
182 mDNSs32 KeyChainTimer
;
184 SCDynamicStoreRef Store
;
185 CFRunLoopSourceRef StoreRLS
;
186 CFRunLoopSourceRef PMRLS
;
187 int SysEventNotifier
;
188 KQueueEntry SysEventKQueue
;
189 IONotificationPortRef PowerPortRef
;
190 io_connect_t PowerConnection
;
191 io_object_t PowerNotifier
;
192 #ifdef kIOPMAcknowledgmentOptionSystemCapabilityRequirements
193 IOPMConnection IOPMConnection
;
195 IOPMAssertionID IOPMAssertion
;
196 long SleepCookie
; // Cookie we need to pass to IOAllowPowerChange()
198 mDNSs32 RequestReSleep
;
199 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
200 dispatch_source_t timer
;
201 dispatch_source_t custom
;
203 pthread_mutex_t BigMutex
;
205 mDNSs32 BigMutexStartTime
;
206 int WakeKQueueLoopFD
;
207 mDNSu8 v4answers
; // non-zero if we are receiving answers
208 mDNSu8 v6answers
; // for A/AAAA from external DNS servers
209 mDNSs32 DNSTrigger
; // Time the DNSTrigger was given
210 uint64_t LastConfigGeneration
; // DNS configuration generation number
213 ProxyCallback
*UDPProxyCallback
;
214 ProxyCallback
*TCPProxyCallback
;
217 extern int OfferSleepProxyService
;
218 extern int DisableSleepProxyClient
;
219 extern int UseInternalSleepProxy
;
220 extern int OSXVers
, iOSVers
;
224 extern void NotifyOfElusiveBug(const char *title
, const char *msg
); // Both strings are UTF-8 text
225 extern void SetDomainSecrets(mDNS
*m
);
226 extern void mDNSMacOSXNetworkChanged(void);
227 extern void mDNSMacOSXSystemBuildNumber(char *HINFO_SWstring
);
228 extern NetworkInterfaceInfoOSX
*IfindexToInterfaceInfoOSX(mDNSInterfaceID ifindex
);
229 extern void mDNSUpdatePacketFilter(const ResourceRecord
*const excludeRecord
);
230 extern void myKQSocketCallBack(int s1
, short filter
, void *context
, mDNSBool encounteredEOF
);
231 extern void mDNSDynamicStoreSetConfig(int key
, const char *subkey
, CFPropertyListRef value
);
232 extern void UpdateDebugState(void);
234 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
235 extern int KQueueSet(int fd
, u_short flags
, short filter
, KQueueEntry
*const entryRef
);
236 mDNSexport
void TriggerEventCompletion(void);
238 extern int KQueueSet(int fd
, u_short flags
, short filter
, const KQueueEntry
*const entryRef
);
241 // When events are processed on the non-kqueue thread (i.e. CFRunLoop notifications like Sleep/Wake,
242 // Interface changes, Keychain changes, etc.) they must use KQueueLock/KQueueUnlock to lock out the kqueue thread
243 extern void KQueueLock(void);
244 extern void KQueueUnlock(const char* task
);
245 extern void mDNSPlatformCloseFD(KQueueEntry
*kq
, int fd
);
246 extern ssize_t
myrecvfrom(const int s
, void *const buffer
, const size_t max
,
247 struct sockaddr
*const from
, size_t *const fromlen
, mDNSAddr
*dstaddr
, char *ifname
, mDNSu8
*ttl
);
249 extern mDNSBool
DictionaryIsEnabled(CFDictionaryRef dict
);
251 extern const char *DNSScopeToString(mDNSu32 scope
);
253 // If any event takes more than WatchDogReportingThreshold milliseconds to be processed, we log a warning message
254 // General event categories are:
255 // o Mach client request initiated / terminated
256 // o UDS client request
257 // o Handling UDP packets received from the network
258 // o Environmental change events:
259 // - network interface changes
261 // - keychain changes
262 // o Name conflict dialog dismissal
263 // o Reception of Unix signal (e.g. SIGINFO)
264 // o Idle task processing
265 // If we find that we're getting warnings for any of these categories, and it's not evident
266 // what's causing the problem, we may need to subdivide some categories into finer-grained
267 // sub-categories (e.g. "Idle task processing" covers a pretty broad range of sub-tasks).
269 extern int WatchDogReportingThreshold
;
271 struct CompileTimeAssertionChecks_mDNSMacOSX
273 // Check our structures are reasonable sizes. Including overly-large buffers, or embedding
274 // other overly-large structures instead of having a pointer to them, can inadvertently
275 // cause structure sizes (and therefore memory usage) to balloon unreasonably.
276 char sizecheck_NetworkInterfaceInfoOSX
[(sizeof(NetworkInterfaceInfoOSX
) <= 8488) ? 1 : -1];
277 char sizecheck_mDNS_PlatformSupport
[(sizeof(mDNS_PlatformSupport
) <= 1378) ? 1 : -1];
280 extern mDNSInterfaceID AWDLInterfaceID
;
281 void initializeD2DPlugins(mDNS
*const m
);
282 void terminateD2DPlugins(void);