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 * http://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 __DNSSD_PRIVATE_H__
18 #define __DNSSD_PRIVATE_H__
20 #include <dispatch/dispatch.h>
22 #include <os/object.h>
24 #if OS_OBJECT_USE_OBJC
25 #define DNSSD_DECL(NAME) OS_OBJECT_DECL_SUBCLASS(dnssd_ ## NAME, dnssd_object)
26 #define DNSSD_RETURNS_RETAINED OS_OBJECT_RETURNS_RETAINED
28 OS_OBJECT_DECL(dnssd_object
,);
30 #define DNSSD_DECL(NAME) typedef struct dnssd_ ## NAME ## _s * dnssd_ ## NAME ## _t
31 #define DNSSD_RETURNS_RETAINED
36 #define DNSSD_ASSUME_NONNULL_BEGIN OS_ASSUME_NONNULL_BEGIN
37 #define DNSSD_ASSUME_NONNULL_END OS_ASSUME_NONNULL_END
39 DNSSD_DECL(getaddrinfo
);
40 DNSSD_DECL(getaddrinfo_result
);
42 DNSSD_ASSUME_NONNULL_BEGIN
44 #if OS_OBJECT_USE_OBJC
45 typedef dnssd_object_t dnssd_any_t
;
47 #if !defined(__cplusplus)
50 dnssd_getaddrinfo_t gai
;
51 dnssd_getaddrinfo_result_t gai_result
;
52 } dnssd_any_t
__attribute__((__transparent_union__
));
54 typedef void * dnssd_any_t
;
58 #define DNSSD_MALLOC __attribute__((__malloc__))
59 #define DNSSD_AVAILABLE SPI_AVAILABLE(macos(10.15), ios(13.0), watchos(6.0), tvos(13.0))
65 * Increments the reference count of a dnssd object.
71 * Calls to dnssd_retain() must be balanced with calls to dnssd_release().
75 dnssd_retain(dnssd_any_t object
);
76 #if OS_OBJECT_USE_OBJC_RETAIN_RELEASE
78 #define dnssd_retain(object) [(object) retain]
83 * Decrements the reference count of a dnssd object.
89 * Calls to dnssd_retain() must be balanced with calls to dnssd_release().
93 dnssd_release(dnssd_any_t object
);
94 #if OS_OBJECT_USE_OBJC_RETAIN_RELEASE
96 #define dnssd_release(object) [(object) release]
102 * Provides a textual description of a dnssd object.
108 * Textual description of the object as a C string.
111 * The string returned by this function must be released with <code>free(3)</code>.
114 DNSSD_MALLOC
char * _Nullable
115 dnssd_copy_description(dnssd_any_t object
);
119 * Creates a getaddrinfo object.
122 * A new getaddrinfo object.
125 * A getaddrinfo object resolves a hostname to its IPv4 and IPv6 addresses.
128 DNSSD_RETURNS_RETAINED dnssd_getaddrinfo_t _Nullable
129 dnssd_getaddrinfo_create(void);
133 * Specifies the queue on which to invoke the getaddrinfo object's result and event blocks.
136 * The getaddrinfo object.
142 * This call must be made before activating the getaddrinfo object.
144 * This function has no effect on a getaddrinfo object that has been activated or invalidated.
148 dnssd_getaddrinfo_set_queue(dnssd_getaddrinfo_t gai
, dispatch_queue_t queue
);
152 * Specifies the DNSServiceFlags to use for the getaddrinfo operation.
155 * The getaddrinfo object.
161 * This function has no effect on a getaddrinfo object that has been activated or invalidated.
165 dnssd_getaddrinfo_set_flags(dnssd_getaddrinfo_t gai
, DNSServiceFlags flags
);
169 * Specifies the hostname to resolve.
172 * The getaddrinfo object.
175 * Hostname as a fully-qualified domain name.
178 * This function has no effect on a getaddrinfo object that has been activated or invalidated.
182 dnssd_getaddrinfo_set_hostname(dnssd_getaddrinfo_t gai
, const char *hostname
);
186 * Specifies the index of the interface via which to resolve the hostname.
189 * The getaddrinfo object.
191 * @param interface_index
195 * If <code>kDNSServiceInterfaceIndexAny</code> is used as the interface index, then special behavior applies. If
196 * the hostname is in the "local." domain, then an attempt will be made to resolve the hostname via all active
197 * mDNS-capable interfaces. If the hostname is in any other domain, then the hostname will be resolved via the
200 * This function has no effect on a getaddrinfo object that has been activated or invalidated.
204 dnssd_getaddrinfo_set_interface_index(dnssd_getaddrinfo_t gai
, uint32_t interface_index
);
208 * Specifies the types of addresses to which to resolve the hostname.
211 * The getaddrinfo object.
217 * Set <code>protocols</code> to <code>kDNSServiceProtocol_IPv4</code> to resolve the hostname to IPv4 addresses.
219 * Set <code>protocols</code> to <code>kDNSServiceProtocol_IPv6</code> to resolve the hostname to IPv6 addresses.
221 * Set <code>protocols</code> to <code>kDNSServiceProtocol_IPv4 | kDNSServiceProtocol_IPv6</code> to resolve the
222 * hostname to both IPv4 and IPv6 addresses.
224 * Set <code>protocols</code> to 0 to limit resolution to addresses of protocols of which the host has routable
225 * addresses. That is, an attempt will be made to resolve the hostname to IPv4 addresses if and only if the host
226 * has a routable IPv4 address. Likewise, an attempt will be made to resolve the hostname to IPv6 addresses if and
227 * only if the host has a routable IPv6 address.
229 * This function has no effect on a getaddrinfo object that has been activated or invalidated.
233 dnssd_getaddrinfo_set_protocols(dnssd_getaddrinfo_t gai
, DNSServiceProtocol protocols
);
237 * Sets the process ID (PID) of the process on whose behalf the getaddrinfo operation is being performed.
240 * The getaddrinfo object.
243 * PID of the process being represented.
246 * If a delegate PID is set, then the calling process must have the proper entitlement in order for the
247 * getaddrinfo operation to not fail with a kDNSServiceErr_NotAuth error.
249 * This function is an alternative to <code>dnssd_getaddrinfo_set_delegate_uuid()</code>.
251 * This function has no effect on a getaddrinfo object that has been activated or invalidated.
255 dnssd_getaddrinfo_set_delegate_pid(dnssd_getaddrinfo_t gai
, pid_t pid
);
259 * Sets the UUID of the process on whose behalf the getaddrinfo operation is being performed.
262 * The getaddrinfo object.
265 * UUID of the process being represented.
268 * If a delegate UUID is set, then the calling process must have the proper entitlement in order for the
269 * getaddrinfo operation to not fail with the <code>kDNSServiceErr_NotAuth</code> error.
271 * This function is an alternative to <code>dnssd_getaddrinfo_set_delegate_pid()</code>.
273 * This function has no effect on a getaddrinfo object that has been activated or invalidated.
277 dnssd_getaddrinfo_set_delegate_uuid(dnssd_getaddrinfo_t gai
, uuid_t _Nonnull uuid
);
281 * Specifies whether or not getaddrinfo results (of types <code>dnssd_getaddrinfo_result_type_add</code> and
282 * <code>dnssd_getaddrinfo_result_type_expired</code>) should include authentication tags from the stub resolver.
285 * The getaddrinfo object.
288 * Pass <code>true</code> if authenticated results are needed, otherwise, pass <code>false</code>.
291 * This function has no effect on a getaddrinfo object that has been activated or invalidated.
295 dnssd_getaddrinfo_set_need_authenticated_results(dnssd_getaddrinfo_t gai
, bool need
);
299 * Activates a getaddrinfo object.
302 * The getaddrinfo object.
305 * This function has no effect on a getaddrinfo object that has already been activated or has been invalidated.
309 dnssd_getaddrinfo_activate(dnssd_getaddrinfo_t gai
);
313 * Asynchronously invalidates a getaddrinfo object.
316 * The getaddrinfo object.
319 * As a result of calling this function, the getaddrinfo object's event handler will be invoked with a
320 * <code>dnssd_event_invalidated</code> event. After this, the object's event and result handlers will never be
323 * This function has no effect on a getaddrinfo object that has already been invalidated.
327 dnssd_getaddrinfo_invalidate(dnssd_getaddrinfo_t gai
);
331 * Handler for getaddrinfo results.
333 * @param result_array
334 * C array of getaddrinfo results.
336 * @param result_count
337 * Size of the array in terms of number of results.
339 typedef void (^dnssd_getaddrinfo_result_handler_t
)(dnssd_getaddrinfo_result_t _Nonnull
* _Nonnull result_array
,
340 size_t result_count
);
344 * Specifies a getaddrinfo object's result handler.
347 * The getaddrinfo object.
354 dnssd_getaddrinfo_set_result_handler(dnssd_getaddrinfo_t gai
, dnssd_getaddrinfo_result_handler_t _Nullable handler
);
358 * Events that can occur during the lifetime of a getaddrinfo object.
361 /*! @const dnssd_event_error An error occurred. */
362 dnssd_event_error
= 1,
363 /*! @const dnssd_event_remove_all All results prior to this event are no longer valid. */
364 dnssd_event_remove_all
= 2,
365 /*! @const dnssd_event_invalidated The object has been invalidated. */
366 dnssd_event_invalidated
= 3,
371 * Handler for getaddrinfo events.
377 * The error associated with a <code>dnssd_event_error</code> event. Ignore for all other types of events.
380 * After a <code>dnssd_event_invalidated</code> event, a getaddrinfo object's result and event handlers will never
383 typedef void (^dnssd_event_handler_t
)(dnssd_event_t event
, DNSServiceErrorType error
);
387 * Sets a getaddrinfo object's event handler.
390 * The getaddrinfo object.
397 dnssd_getaddrinfo_set_event_handler(dnssd_getaddrinfo_t gai
, dnssd_event_handler_t _Nullable handler
);
401 * Types of getaddrinfo results.
404 /*! @const dnssd_getaddrinfo_result_type_add The contained hostname and address pair is valid. */
405 dnssd_getaddrinfo_result_type_add
= 1,
406 /*! @const dnssd_getaddrinfo_result_type_remove The contained hostname and address pair is no longer valid. */
407 dnssd_getaddrinfo_result_type_remove
= 2,
408 /*! @const dnssd_getaddrinfo_result_type_no_address The contained hostname has no address of a particular type. */
409 dnssd_getaddrinfo_result_type_no_address
= 3,
410 /*! @const dnssd_getaddrinfo_result_type_expired A hostname and address pair contained came from an expired cached record and may no longer be valid. */
411 dnssd_getaddrinfo_result_type_expired
= 4,
412 } dnssd_getaddrinfo_result_type_t
;
416 * Gets a getaddrinfo result's type.
419 * The getaddrinfo result.
425 dnssd_getaddrinfo_result_type_t
426 dnssd_getaddrinfo_result_get_type(dnssd_getaddrinfo_result_t gai_result
);
430 * Gets a getaddrinfo result's actual hostname.
433 * The getaddrinfo result.
436 * The getaddrinfo result's actual hostname.
439 * The hostname returned by this function is the canonical name of the hostname that was requested. In other
440 * words, it's the canonical name of the hostname set with <code>dnssd_getaddrinfo_set_hostname()</code>.
442 * The pointer returned by this function is valid until the getaddrinfo result is released.
446 dnssd_getaddrinfo_result_get_actual_hostname(dnssd_getaddrinfo_result_t gai_result
);
450 * Gets a getaddrinfo result's address.
453 * The getaddrinfo result.
456 * The getaddrinfo result's address as a sockaddr structure.
459 * For getaddrinfo results of type <code>dnssd_getaddrinfo_result_type_no_address</code>, the sockaddr structure's
460 * sa_family member variable can be used to determine the type of address that the hostname lacks.
463 const struct sockaddr
*
464 dnssd_getaddrinfo_result_get_address(dnssd_getaddrinfo_result_t gai_result
);
468 * Gets a getaddrinfo result's hostname.
471 * The getaddrinfo result.
474 * The getaddrinfo result's hostname.
477 * The hostname returned by this function is the hostname whose resolution was requested. In other words, it's
478 * equal to the hostname set with <code>dnssd_getaddrinfo_set_hostname()</code>.
480 * The pointer returned by this function is valid until the getaddrinfo result is released.
484 dnssd_getaddrinfo_result_get_hostname(dnssd_getaddrinfo_result_t gai_result
);
488 * Gets the interface index to which a getaddrinfo result pertains.
491 * The getaddrinfo result.
494 * For hostnames that were resolved via mDNS, the return value is the index of the interface via which the
495 * hostname was resolved. For hostnames that were resolved via DNS, the return value is 0.
499 dnssd_getaddrinfo_result_get_interface_index(dnssd_getaddrinfo_result_t gai_result
);
503 * Gets a getaddrinfo result's authentication tag.
506 * The getaddrinfo result.
509 * If non-NULL, gets set to the length of the authentication tag.
512 * A pointer to the getaddrinfo result's authentication tag, if it has one. Otherwise, NULL.
515 * The returned pointer, if non-NULL, is valid until the getaddrinfo result is released.
518 const void * _Nullable
519 dnssd_getaddrinfo_result_get_authentication_tag(dnssd_getaddrinfo_result_t gai_result
, size_t *_Nullable out_length
);
521 static inline const char *
522 dnssd_getaddrinfo_result_type_to_string(dnssd_getaddrinfo_result_type_t result
)
525 case dnssd_getaddrinfo_result_type_add
: return "Add";
526 case dnssd_getaddrinfo_result_type_remove
: return "Remove";
527 case dnssd_getaddrinfo_result_type_no_address
: return "NoAddress";
528 case dnssd_getaddrinfo_result_type_expired
: return "Expired";
533 static inline const char *
534 dnssd_event_to_string(dnssd_event_t event
)
537 case dnssd_event_remove_all
: return "RemoveAll";
538 case dnssd_event_error
: return "Error";
539 case dnssd_event_invalidated
: return "Invalidated";
546 DNSSD_ASSUME_NONNULL_END
548 #endif // __DNSSD_PRIVATE_H__