2 * Copyright (c) 2019 Apple Inc. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * https://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef __MDNS_PRIVATE_H__
18 #define __MDNS_PRIVATE_H__
20 #include <CoreFoundation/CoreFoundation.h>
21 #include <dispatch/dispatch.h>
23 #include <os/object.h>
25 #if OS_OBJECT_USE_OBJC
26 #define MDNS_DECL(NAME) OS_OBJECT_DECL_SUBCLASS(mdns_ ## NAME, mdns_object)
27 #define MDNS_RETURNS_RETAINED OS_OBJECT_RETURNS_RETAINED
29 OS_OBJECT_DECL(mdns_object
,);
31 #define MDNS_DECL(NAME) typedef struct mdns_ ## NAME ## _s * mdns_ ## NAME ## _t
32 #define MDNS_RETURNS_RETAINED
37 // mdns Object Declarations
39 MDNS_DECL(interface_monitor
);
41 OS_ASSUME_NONNULL_BEGIN
43 #if OS_OBJECT_USE_OBJC
44 typedef mdns_object_t mdns_any_t
;
46 #if !defined(__cplusplus)
49 mdns_interface_monitor_t interface_monitor
;
50 } mdns_any_t
__attribute__((__transparent_union__
));
52 typedef void * mdns_any_t
;
60 * Increments the reference count of an mdns object.
66 mdns_retain(mdns_any_t object
);
67 #if OS_OBJECT_USE_OBJC_RETAIN_RELEASE
69 #define mdns_retain(object) [(object) retain]
74 * Decrements the reference count of an mdns object.
80 mdns_release(mdns_any_t object
);
81 #if OS_OBJECT_USE_OBJC_RETAIN_RELEASE
83 #define mdns_release(object) [(object) release]
88 * Creates a human-readable description of an mdns object as a C string.
94 * A C string that must be freed with free(3).
98 mdns_copy_description(mdns_any_t object
);
102 * CFArray callbacks for mdns objects.
104 extern const CFArrayCallBacks mdns_cfarray_callbacks
;
108 * Creates an interface monitor.
110 * @param interface_index
111 * Index of the interface to monitor.
114 * A new interface monitor or NULL if there was a lack of resources.
117 * An interface monitor provides up-to-date information about an interface's properties, such as IPv4
118 * connectivity, IPv6 connectivity, whether the interface is expensive, and whether the interface is constrained.
120 * If this function returns non-NULL, then the caller has an ownership reference to the newly created interface
121 * monitor, which can be relinquished with <code>mdns_release()</code>.
123 MDNS_RETURNS_RETAINED mdns_interface_monitor_t _Nullable
124 mdns_interface_monitor_create(uint32_t interface_index
);
128 * Activates an interface monitor.
131 * The interface monitor.
134 * Successful activation enables interface monitor updates.
136 * This function has no effect on an interface monitor that has already been activated or one that has been
140 mdns_interface_monitor_activate(mdns_interface_monitor_t monitor
);
144 * Asynchronously invalidates an interface monitor.
147 * The interface monitor.
150 * This function should be called when the interface monitor is no longer needed.
152 * As a result of calling this function, the interface monitor's event handler will be invoked with a
153 * <code>mdns_event_invalidated</code> event, after which the interface monitor's event and update handlers will
154 * never be invoked again.
156 * This function has no effect on an interface monitor that has already been invalidated.
159 mdns_interface_monitor_invalidate(mdns_interface_monitor_t monitor
);
163 * Specifies the queue on which to invoke the interface monitor's event and update handlers.
166 * The interface monitor.
172 * This function must be called before activating the interface monitor.
174 * This function has no effect on an interface monitor that has been activated or invalidated.
177 mdns_interface_monitor_set_queue(mdns_interface_monitor_t monitor
, dispatch_queue_t queue
);
181 * Generic events that can occur during the lifetime of an mdns object.
183 OS_CLOSED_ENUM(mdns_event
, int,
184 /*! @const mdns_event_error A fatal error has occurred. */
185 mdns_event_error
= 1,
186 /*! @const mdns_event_invalidated The object has been invalidated. */
187 mdns_event_invalidated
= 2
190 static inline const char *
191 mdns_event_to_string(mdns_event_t event
)
194 case mdns_event_error
: return "Error";
195 case mdns_event_invalidated
: return "Invalidated";
202 * Generic event handler for mdns objects.
208 * The error associated with a <code>mdns_event_error</code> event. This argument should be ignored for all other
212 * After an <code>mdns_event_invalidated</code> event, none of the object's handlers will ever be invoked again.
214 typedef void (^mdns_event_handler_t
)(mdns_event_t event
, OSStatus error
);
218 * Sets an interface monitor's event handler.
221 * The interface monitor.
227 * The event handler will never be invoked prior to activation.
229 * The event handler will be invoked on the dispatch queue specified by
230 * <code>mdns_interface_monitor_set_queue()</code> with event <code>mdns_event_error</code> when a fatal error
231 * occurs and with event <code>mdns_event_invalidated</code> when the interface monitor has been invalidated.
233 * After an <code>mdns_event_invalidated</code> event, the event handler will never be invoked again.
236 mdns_interface_monitor_set_event_handler(mdns_interface_monitor_t monitor
, mdns_event_handler_t _Nullable handler
);
240 * Flags that represent the properties of a monitored interface.
243 * These flags don't represent the actual values of properties. The meaning of these flags depends on the context
244 * in which they're used. For example, as a parameter of mdns_interface_monitor_update_handler_t, a set flag
245 * means that the value of a given property has changed.
247 OS_CLOSED_OPTIONS(mdns_interface_flags
, uint32_t,
248 mdns_interface_flag_null
= 0,
249 mdns_interface_flag_ipv4_connectivity
= (1U << 0),
250 mdns_interface_flag_ipv6_connectivity
= (1U << 1),
251 mdns_interface_flag_expensive
= (1U << 2),
252 mdns_interface_flag_constrained
= (1U << 3),
253 mdns_interface_flag_clat46
= (1U << 4),
254 mdns_interface_flag_reserved
= (1U << 31)
259 * Update handler for an interface monitor.
261 * @param change_flags
262 * Each flag bit represents a property of a monitored interface. If the bit is set, then the value of that
263 * property has changed. If the bit is clear, then the value of that property has not changed.
265 typedef void (^mdns_interface_monitor_update_handler_t
)(mdns_interface_flags_t change_flags
);
269 * Sets an interface monitor's update handler.
272 * The interface monitor.
275 * The update handler.
278 * The update handler will never be invoked prior to activation.
280 * The update handler will be invoked on the dispatch queue specified by
281 * <code>mdns_interface_monitor_set_queue()</code> when any of the monitored interface's properties have been
284 * After an <code>mdns_event_invalidated</code> event, the update handler will ever be invoked again.
287 mdns_interface_monitor_set_update_handler(mdns_interface_monitor_t monitor
,
288 mdns_interface_monitor_update_handler_t _Nullable handler
);
292 * Returns the index of the monitored interface.
295 * The interface monitor.
298 mdns_interface_monitor_get_interface_index(mdns_interface_monitor_t monitor
);
302 * Determines whether the monitored interface currently has IPv4 connectivity.
305 * The interface monitor.
308 * mdns_interface_flag_ipv4_connectivity will be set in the update handler's change_flags argument when the value
309 * of this property has changed.
312 mdns_interface_monitor_has_ipv4_connectivity(mdns_interface_monitor_t monitor
);
316 * Determines whether the monitored interface currently has IPv6 connectivity.
319 * The interface monitor.
322 * mdns_interface_flag_ipv6_connectivity will be set in the update handler's change_flags argument when the value
323 * of this property has changed.
326 mdns_interface_monitor_has_ipv6_connectivity(mdns_interface_monitor_t monitor
);
330 * Determines whether the monitored interface is currently expensive.
333 * The interface monitor.
336 * mdns_interface_flag_expensive will be set in the update handler's change_flags argument when the value
337 * of this property has changed.
340 mdns_interface_monitor_is_expensive(mdns_interface_monitor_t monitor
);
344 * Determines whether the monitored interface is currently constrained.
347 * The interface monitor.
350 * mdns_interface_flag_constrained will be set in the update handler's change_flags argument when the value
351 * of this property has changed.
354 mdns_interface_monitor_is_constrained(mdns_interface_monitor_t monitor
);
358 * Determines whether the monitored interface has CLAT46 support.
361 * The interface monitor.
364 * mdns_interface_flag_clat46 will be set in the update handler's change_flags argument when the value
365 * of this property has changed.
368 mdns_interface_monitor_is_clat46(mdns_interface_monitor_t monitor
);
372 OS_ASSUME_NONNULL_END
374 #endif // __MDNS_PRIVATE_H__