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 mDNSu8 D2DInterface
; // IFEF_LOCALNET_PRIVATE flag indicates we should call
82 // D2D plugin for operations over this interface
84 mDNSs32 AppearanceTime
; // Time this interface appeared most recently in getifaddrs list
85 // i.e. the first time an interface is seen, AppearanceTime is set.
86 // If an interface goes away temporarily and then comes back then
87 // AppearanceTime is updated to the time of the most recent appearance.
88 mDNSs32 LastSeen
; // If Exists==0, last time this interface appeared in getifaddrs list
89 unsigned int ifa_flags
;
90 struct in_addr ifa_v4addr
;
91 mDNSu32 scope_id
; // interface index / IPv6 scope ID
92 mDNSEthAddr BSSID
; // BSSID of 802.11 base station, if applicable
94 int BPF_fd
; // -1 uninitialized; -2 requested BPF; -3 failed
95 int BPF_mcfd
; // Socket for our IPv6 ND group membership
97 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
98 dispatch_source_t BPF_source
;
101 CFRunLoopSourceRef BPF_rls
;
103 NetworkInterfaceInfoOSX
*Registered
; // non-NULL means registered with mDNS Core
106 struct mDNS_PlatformSupport_struct
108 NetworkInterfaceInfoOSX
*InterfaceList
;
109 KQSocketSet permanentsockets
;
110 domainlabel userhostlabel
; // The hostlabel as it was set in System Preferences the last time we looked
111 domainlabel usernicelabel
; // The nicelabel as it was set in System Preferences the last time we looked
112 // Following four variables are used for optimization where the helper is not
113 // invoked when not needed. It records the state of what we told helper the
114 // last time we invoked mDNSPreferencesSetName
115 domainlabel prevoldhostlabel
; // Previous m->p->userhostlabel
116 domainlabel prevnewhostlabel
; // Previous m->hostlabel
117 domainlabel prevoldnicelabel
; // Previous m->p->usernicelabel
118 domainlabel prevnewnicelabel
; // Previous m->nicelabel
120 mDNSs32 HostNameConflict
; // Time we experienced conflict on our link-local host name
121 mDNSs32 NetworkChanged
;
122 mDNSs32 KeyChainTimer
;
124 CFRunLoopRef CFRunLoop
;
125 SCDynamicStoreRef Store
;
126 CFRunLoopSourceRef StoreRLS
;
127 CFRunLoopSourceRef PMRLS
;
128 int SysEventNotifier
;
129 KQueueEntry SysEventKQueue
;
130 IONotificationPortRef PowerPortRef
;
131 io_connect_t PowerConnection
;
132 io_object_t PowerNotifier
;
133 #ifdef kIOPMAcknowledgmentOptionSystemCapabilityRequirements
134 IOPMConnection IOPMConnection
;
136 IOPMAssertionID IOPMAssertion
;
137 long SleepCookie
; // Cookie we need to pass to IOAllowPowerChange()
139 mDNSs32 RequestReSleep
;
140 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
141 dispatch_source_t timer
;
142 dispatch_source_t custom
;
144 pthread_mutex_t BigMutex
;
146 mDNSs32 BigMutexStartTime
;
147 int WakeKQueueLoopFD
;
150 extern int OfferSleepProxyService
;
151 extern int DisableSleepProxyClient
;
152 extern int UseInternalSleepProxy
;
153 extern int OSXVers
, iOSVers
;
154 #define OSXVers_Base 4
155 #define OSXVers_10_0_Cheetah 4
156 #define OSXVers_10_1_Puma 5
157 #define OSXVers_10_2_Jaguar 6
158 #define OSXVers_10_3_Panther 7
159 #define OSXVers_10_4_Tiger 8
160 #define OSXVers_10_5_Leopard 9
161 #define OSXVers_10_6_SnowLeopard 10
165 extern void NotifyOfElusiveBug(const char *title
, const char *msg
); // Both strings are UTF-8 text
166 extern void SetDomainSecrets(mDNS
*m
);
167 extern void mDNSMacOSXNetworkChanged(mDNS
*const m
);
168 extern void mDNSMacOSXSystemBuildNumber(char *HINFO_SWstring
);
169 extern NetworkInterfaceInfoOSX
*IfindexToInterfaceInfoOSX(const mDNS
*const m
, mDNSInterfaceID ifindex
);
170 extern void mDNSUpdatePacketFilter(const ResourceRecord
*const excludeRecord
);
172 #ifdef MDNSRESPONDER_USES_LIB_DISPATCH_AS_PRIMARY_EVENT_LOOP_MECHANISM
173 extern int KQueueSet(int fd
, u_short flags
, short filter
, KQueueEntry
*const entryRef
);
174 mDNSexport
void TriggerEventCompletion(void);
176 extern int KQueueSet(int fd
, u_short flags
, short filter
, const KQueueEntry
*const entryRef
);
179 // When events are processed on the non-kqueue thread (i.e. CFRunLoop notifications like Sleep/Wake,
180 // Interface changes, Keychain changes, etc.) they must use KQueueLock/KQueueUnlock to lock out the kqueue thread
181 extern void KQueueLock(mDNS
*const m
);
182 extern void KQueueUnlock(mDNS
*const m
, const char const *task
);
183 extern void mDNSPlatformCloseFD(KQueueEntry
*kq
, int fd
);
185 extern mDNSBool
DictionaryIsEnabled(CFDictionaryRef dict
);
187 extern void mDNSPlatformToggleInterfaceAdvt(mDNS
*const m
, mDNSBool stopAdvt
);
189 // If any event takes more than WatchDogReportingThreshold milliseconds to be processed, we log a warning message
190 // General event categories are:
191 // o Mach client request initiated / terminated
192 // o UDS client request
193 // o Handling UDP packets received from the network
194 // o Environmental change events:
195 // - network interface changes
197 // - keychain changes
198 // o Name conflict dialog dismissal
199 // o Reception of Unix signal (e.g. SIGINFO)
200 // o Idle task processing
201 // If we find that we're getting warnings for any of these categories, and it's not evident
202 // what's causing the problem, we may need to subdivide some categories into finer-grained
203 // sub-categories (e.g. "Idle task processing" covers a pretty broad range of sub-tasks).
205 extern int WatchDogReportingThreshold
;
207 struct CompileTimeAssertionChecks_mDNSMacOSX
209 // Check our structures are reasonable sizes. Including overly-large buffers, or embedding
210 // other overly-large structures instead of having a pointer to them, can inadvertently
211 // cause structure sizes (and therefore memory usage) to balloon unreasonably.
212 char sizecheck_NetworkInterfaceInfoOSX
[(sizeof(NetworkInterfaceInfoOSX
) <= 7000) ? 1 : -1];
213 char sizecheck_mDNS_PlatformSupport
[(sizeof(mDNS_PlatformSupport
) <= 768) ? 1 : -1];