2 * Copyright (c) 2019-2020 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_INTERFACE_MONITOR_H__
18 #define __MDNS_INTERFACE_MONITOR_H__
20 #include "mdns_base.h"
21 #include "mdns_object.h"
25 #include <dispatch/dispatch.h>
28 MDNS_DECL(interface_monitor
);
30 MDNS_ASSUME_NONNULL_BEGIN
36 * Creates an interface monitor.
38 * @param interface_index
39 * Index of the interface to monitor.
42 * A new interface monitor or NULL if there was a lack of resources.
45 * An interface monitor provides up-to-date information about an interface's properties, such as IPv4
46 * connectivity, IPv6 connectivity, whether the interface is expensive, and whether the interface is constrained.
48 * If this function returns non-NULL, then the caller has an ownership reference to the newly created interface
49 * monitor, which can be relinquished with <code>mdns_release()</code>.
51 MDNS_RETURNS_RETAINED mdns_interface_monitor_t _Nullable
52 mdns_interface_monitor_create(uint32_t interface_index
);
56 * Activates an interface monitor.
59 * The interface monitor.
62 * Successful activation enables interface monitor updates.
64 * This function has no effect on an interface monitor that has already been activated or one that has been
68 mdns_interface_monitor_activate(mdns_interface_monitor_t monitor
);
72 * Asynchronously invalidates an interface monitor.
75 * The interface monitor.
78 * This function should be called when the interface monitor is no longer needed.
80 * As a result of calling this function, the interface monitor's event handler will be invoked with a
81 * <code>mdns_event_invalidated</code> event, after which the interface monitor's event and update handlers will
82 * never be invoked again.
84 * This function has no effect on an interface monitor that has already been invalidated.
87 mdns_interface_monitor_invalidate(mdns_interface_monitor_t monitor
);
91 * Specifies the queue on which to invoke the interface monitor's event and update handlers.
94 * The interface monitor.
100 * This function must be called before activating the interface monitor.
102 * This function has no effect on an interface monitor that has been activated or invalidated.
105 mdns_interface_monitor_set_queue(mdns_interface_monitor_t monitor
, dispatch_queue_t queue
);
109 * Sets an interface monitor's event handler.
112 * The interface monitor.
118 * The event handler will never be invoked prior to activation.
120 * The event handler will be invoked on the dispatch queue specified by
121 * <code>mdns_interface_monitor_set_queue()</code> with event <code>mdns_event_error</code> when a fatal error
122 * occurs and with event <code>mdns_event_invalidated</code> when the interface monitor has been invalidated.
124 * After an <code>mdns_event_invalidated</code> event, the event handler will never be invoked again.
127 mdns_interface_monitor_set_event_handler(mdns_interface_monitor_t monitor
, mdns_event_handler_t _Nullable handler
);
131 * Flags that represent the properties of a monitored interface.
134 * These flags don't represent the actual values of properties. The meaning of these flags depends on the
135 * context in which they're used. For example, as a parameter of mdns_interface_monitor_update_handler_t, a set
136 * flag means that the value of a given property has changed.
138 OS_CLOSED_OPTIONS(mdns_interface_flags
, uint32_t,
139 mdns_interface_flag_null
= 0,
140 mdns_interface_flag_ipv4_connectivity
= (1U << 0),
141 mdns_interface_flag_ipv6_connectivity
= (1U << 1),
142 mdns_interface_flag_expensive
= (1U << 2),
143 mdns_interface_flag_constrained
= (1U << 3),
144 mdns_interface_flag_clat46
= (1U << 4),
145 mdns_interface_flag_vpn
= (1U << 5),
146 mdns_interface_flag_reserved
= (1U << 31)
151 * Update handler for an interface monitor.
153 * @param change_flags
154 * Each flag bit represents a property of a monitored interface. If the bit is set, then the value of that
155 * property has changed. If the bit is clear, then the value of that property has not changed.
157 typedef void (^mdns_interface_monitor_update_handler_t
)(mdns_interface_flags_t change_flags
);
161 * Sets an interface monitor's update handler.
164 * The interface monitor.
167 * The update handler.
170 * The update handler will never be invoked prior to activation.
172 * The update handler will be invoked on the dispatch queue specified by
173 * <code>mdns_interface_monitor_set_queue()</code> when any of the monitored interface's properties have been
176 * After an <code>mdns_event_invalidated</code> event, the update handler will ever be invoked again.
179 mdns_interface_monitor_set_update_handler(mdns_interface_monitor_t monitor
,
180 mdns_interface_monitor_update_handler_t _Nullable handler
);
184 * Returns the index of the monitored interface.
187 * The interface monitor.
190 mdns_interface_monitor_get_interface_index(mdns_interface_monitor_t monitor
);
194 * Determines whether the monitored interface currently has IPv4 connectivity.
197 * The interface monitor.
200 * mdns_interface_flag_ipv4_connectivity will be set in the update handler's change_flags argument when the
201 * value of this property has changed.
204 mdns_interface_monitor_has_ipv4_connectivity(mdns_interface_monitor_t monitor
);
208 * Determines whether the monitored interface currently has IPv6 connectivity.
211 * The interface monitor.
214 * mdns_interface_flag_ipv6_connectivity will be set in the update handler's change_flags argument when the
215 * value of this property has changed.
218 mdns_interface_monitor_has_ipv6_connectivity(mdns_interface_monitor_t monitor
);
222 * Determines whether the monitored interface is currently expensive.
225 * The interface monitor.
228 * mdns_interface_flag_expensive will be set in the update handler's change_flags argument when the value
229 * of this property has changed.
232 mdns_interface_monitor_is_expensive(mdns_interface_monitor_t monitor
);
236 * Determines whether the monitored interface is currently constrained.
239 * The interface monitor.
242 * mdns_interface_flag_constrained will be set in the update handler's change_flags argument when the value
243 * of this property has changed.
246 mdns_interface_monitor_is_constrained(mdns_interface_monitor_t monitor
);
250 * Determines whether the monitored interface has CLAT46 support.
253 * The interface monitor.
256 * mdns_interface_flag_clat46 will be set in the update handler's change_flags argument when the value
257 * of this property has changed.
260 mdns_interface_monitor_is_clat46(mdns_interface_monitor_t monitor
);
264 * Determines whether the monitored interface is used for VPN.
267 * The interface monitor.
270 * mdns_interface_flag_vpn will be set in the update handler's change_flags argument when the value of this
271 * property has changed.
274 mdns_interface_monitor_is_vpn(mdns_interface_monitor_t monitor
);
278 MDNS_ASSUME_NONNULL_END
280 #define mdns_interface_monitor_forget(X) mdns_forget_with_invalidation(X, interface_monitor)
282 #endif // __MDNS_INTERFACE_MONITOR_H__