1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2002-2003 Apple Computer, 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.
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
33 //#define MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
34 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
35 #include <dispatch/dispatch.h>
38 typedef struct NetworkInterfaceInfoOSX_struct NetworkInterfaceInfoOSX
;
40 typedef void (*KQueueEventCallback
)(int fd
, short filter
, void *context
);
43 KQueueEventCallback KQcallback
;
45 const char const *KQtask
; // For debugging messages
46 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
47 dispatch_source_t readSource
;
48 dispatch_source_t writeSource
;
55 mDNSIPPort port
; // MUST BE FIRST FIELD -- UDPSocket_struct begins with a KQSocketSet,
56 // and mDNSCore requires every UDPSocket_struct to begin with a mDNSIPPort port
67 struct UDPSocket_struct
69 KQSocketSet ss
; // First field of KQSocketSet has to be mDNSIPPort -- mDNSCore requires every UDPSocket_struct to begin with mDNSIPPort port
72 struct NetworkInterfaceInfoOSX_struct
74 NetworkInterfaceInfo ifinfo
; // MUST be the first element in this structure
75 NetworkInterfaceInfoOSX
*next
;
77 mDNSu8 Exists
; // 1 = currently exists in getifaddrs list; 0 = doesn't
78 // 2 = exists, but McastTxRx state changed
79 mDNSu8 Flashing
; // Set if interface appeared for less than 60 seconds and then vanished
80 mDNSu8 Occulting
; // Set if interface vanished for less than 60 seconds and then came back
81 mDNSs32 AppearanceTime
; // Time this interface appeared most recently in getifaddrs list
82 // i.e. the first time an interface is seen, AppearanceTime is set.
83 // If an interface goes away temporarily and then comes back then
84 // AppearanceTime is updated to the time of the most recent appearance.
85 mDNSs32 LastSeen
; // If Exists==0, last time this interface appeared in getifaddrs list
86 unsigned int ifa_flags
;
87 struct in_addr ifa_v4addr
;
88 mDNSu32 scope_id
; // interface index / IPv6 scope ID
89 mDNSEthAddr BSSID
; // BSSID of 802.11 base station, if applicable
91 int BPF_fd
; // -1 uninitialized; -2 requested BPF; -3 failed
92 int BPF_mcfd
; // Socket for our IPv6 ND group membership
94 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
95 dispatch_source_t BPF_source
;
98 CFRunLoopSourceRef BPF_rls
;
100 NetworkInterfaceInfoOSX
*Registered
; // non-NULL means registered with mDNS Core
103 struct mDNS_PlatformSupport_struct
105 NetworkInterfaceInfoOSX
*InterfaceList
;
106 KQSocketSet permanentsockets
;
107 domainlabel userhostlabel
; // The hostlabel as it was set in System Preferences the last time we looked
108 domainlabel usernicelabel
; // The nicelabel as it was set in System Preferences the last time we looked
109 // Following four variables are used for optimization where the helper is not
110 // invoked when not needed. It records the state of what we told helper the
111 // last time we invoked mDNSPreferencesSetName
112 domainlabel prevoldhostlabel
; // Previous m->p->userhostlabel
113 domainlabel prevnewhostlabel
; // Previous m->hostlabel
114 domainlabel prevoldnicelabel
; // Previous m->p->usernicelabel
115 domainlabel prevnewnicelabel
; // Previous m->nicelabel
117 mDNSs32 HostNameConflict
; // Time we experienced conflict on our link-local host name
118 mDNSs32 NetworkChanged
;
119 mDNSs32 KeyChainTimer
;
121 CFRunLoopRef CFRunLoop
;
122 SCDynamicStoreRef Store
;
123 CFRunLoopSourceRef StoreRLS
;
124 CFRunLoopSourceRef PMRLS
;
125 int SysEventNotifier
;
126 KQueueEntry SysEventKQueue
;
127 IONotificationPortRef PowerPortRef
;
128 io_connect_t PowerConnection
;
129 io_object_t PowerNotifier
;
130 #ifdef kIOPMAcknowledgmentOptionSystemCapabilityRequirements
131 IOPMConnection IOPMConnection
;
133 IOPMAssertionID IOPMAssertion
;
134 SCPreferencesRef SCPrefs
;
135 long SleepCookie
; // Cookie we need to pass to IOAllowPowerChange()
137 mDNSs32 RequestReSleep
;
138 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
139 dispatch_source_t timer
;
140 dispatch_source_t custom
;
142 pthread_mutex_t BigMutex
;
144 mDNSs32 BigMutexStartTime
;
145 int WakeKQueueLoopFD
;
146 #if APPLE_OSX_mDNSResponder
147 CFDictionaryRef ConndBTMMDict
; // Dictionary of tunnel name/value pairs
151 extern int OfferSleepProxyService
;
152 extern int DisableSleepProxyClient
;
153 extern int UseInternalSleepProxy
;
154 extern int OSXVers
, iOSVers
;
155 extern mDNSBool DisableInboundRelayConnection
;
156 #define OSXVers_Base 4
157 #define OSXVers_10_0_Cheetah 4
158 #define OSXVers_10_1_Puma 5
159 #define OSXVers_10_2_Jaguar 6
160 #define OSXVers_10_3_Panther 7
161 #define OSXVers_10_4_Tiger 8
162 #define OSXVers_10_5_Leopard 9
163 #define OSXVers_10_6_SnowLeopard 10
167 extern void NotifyOfElusiveBug(const char *title
, const char *msg
); // Both strings are UTF-8 text
168 extern void SetDomainSecrets(mDNS
*m
);
169 extern void mDNSMacOSXNetworkChanged(mDNS
*const m
);
170 extern void mDNSMacOSXSystemBuildNumber(char *HINFO_SWstring
);
171 extern NetworkInterfaceInfoOSX
*IfindexToInterfaceInfoOSX(const mDNS
*const m
, mDNSInterfaceID ifindex
);
173 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
174 extern int KQueueSet(int fd
, u_short flags
, short filter
, KQueueEntry
*const entryRef
);
175 mDNSexport
void TriggerEventCompletion(void);
177 extern int KQueueSet(int fd
, u_short flags
, short filter
, const KQueueEntry
*const entryRef
);
180 // When events are processed on the non-kqueue thread (i.e. CFRunLoop notifications like Sleep/Wake,
181 // Interface changes, Keychain changes, etc.) they must use KQueueLock/KQueueUnlock to lock out the kqueue thread
182 extern void KQueueLock(mDNS
*const m
);
183 extern void KQueueUnlock(mDNS
*const m
, const char const *task
);
184 extern void mDNSPlatformCloseFD(KQueueEntry
*kq
, int fd
);
186 extern mDNSBool
DictionaryIsEnabled(CFDictionaryRef dict
);
188 // If any event takes more than WatchDogReportingThreshold milliseconds to be processed, we log a warning message
189 // General event categories are:
190 // o Mach client request initiated / terminated
191 // o UDS client request
192 // o Handling UDP packets received from the network
193 // o Environmental change events:
194 // - network interface changes
196 // - keychain changes
197 // o Name conflict dialog dismissal
198 // o Reception of Unix signal (e.g. SIGINFO)
199 // o Idle task processing
200 // If we find that we're getting warnings for any of these categories, and it's not evident
201 // what's causing the problem, we may need to subdivide some categories into finer-grained
202 // sub-categories (e.g. "Idle task processing" covers a pretty broad range of sub-tasks).
204 extern int WatchDogReportingThreshold
;
206 struct CompileTimeAssertionChecks_mDNSMacOSX
208 // Check our structures are reasonable sizes. Including overly-large buffers, or embedding
209 // other overly-large structures instead of having a pointer to them, can inadvertently
210 // cause structure sizes (and therefore memory usage) to balloon unreasonably.
211 char sizecheck_NetworkInterfaceInfoOSX
[(sizeof(NetworkInterfaceInfoOSX
) <= 7000) ? 1 : -1];
212 char sizecheck_mDNS_PlatformSupport
[(sizeof(mDNS_PlatformSupport
) <= 768) ? 1 : -1];