]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSShared/dns_sd.h
mDNSResponder-66.3.tar.gz
[apple/mdnsresponder.git] / mDNSShared / dns_sd.h
1 /*
2 * Copyright (c) 2003-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24
25 Change History (most recent first):
26
27 $Log: dns_sd.h,v $
28 Revision 1.17 2004/06/01 14:34:48 cheshire
29 For compatibility with older compilers, change '//' comments to ' / * ... * / '
30
31 Revision 1.16 2004/05/25 17:08:55 cheshire
32 Fix compiler warning (doesn't make sense for function return type to be const)
33
34 Revision 1.15 2004/05/21 21:41:35 cheshire
35 Add TXT record building and parsing APIs
36
37 Revision 1.14 2004/05/20 18:40:31 cheshire
38 Remove trailing comma that breaks build on strict compilers
39
40 Revision 1.13 2004/05/18 23:51:27 cheshire
41 Tidy up all checkin comments to use consistent "<rdar://problem/xxxxxxx>" format for bug numbers
42
43 Revision 1.12 2004/05/07 21:11:07 ksekar
44 API Update: Exposed new core error codes. Added constants for
45 InterfaceIndexAny and InterfaceIndexLocalOnly. Added flag for
46 long-lived unicast queries via DNSServiceQueryRecord.
47
48 Revision 1.11 2004/05/07 20:51:18 ksekar
49 <rdar://problem/3608226>: dns_sd.h needs to direct developers to
50 register their services at <http://www.dns-sd.org/ServiceTypes.html>
51
52 Revision 1.10 2004/05/06 18:42:58 ksekar
53 General dns_sd.h API cleanup, including the following radars:
54 <rdar://problem/3592068>: Remove flags with zero value
55 <rdar://problem/3479569>: Passing in NULL causes a crash.
56
57 Revision 1.9 2004/03/20 05:43:39 cheshire
58 Fix contributed by Terry Lambert & Alfred Perlstein:
59 On FreeBSD 4.x we need to include <sys/types.h> instead of <stdint.h>
60
61 Revision 1.8 2004/03/19 17:50:40 cheshire
62 Clarify comment about kDNSServiceMaxDomainName
63
64 Revision 1.7 2004/03/12 08:00:06 cheshire
65 Minor comment changes, headers, and wrap file in extern "C" for the benefit of C++ clients
66
67 Revision 1.6 2003/12/04 06:24:33 cheshire
68 Clarify meaning of MoreComing/Finished flag
69
70 Revision 1.5 2003/11/13 23:35:35 ksekar
71 <rdar://problem/3483020>: Header doesn't say that add/remove are possible values for flags
72 Bringing mDNSResponder project copy of dns_sd.h header up to date with
73 Libinfo copy
74
75 Revision 1.4 2003/10/13 23:50:53 ksekar
76 Updated dns_sd clientstub files to bring copies in synch with
77 top-of-tree Libinfo: A memory leak in dnssd_clientstub.c is fixed,
78 and comments in dns_sd.h are improved.
79
80 Revision 1.3 2003/08/12 19:51:51 cheshire
81 Update to APSL 2.0
82 */
83
84 #ifndef _DNS_SD_H
85 #define _DNS_SD_H
86
87 #ifdef __cplusplus
88 extern "C" {
89 #endif
90
91 #if defined(__FreeBSD__) && (__FreeBSD_version < 500000)
92 /* stdint.h does not exist on FreeBSD 4.x; its types are defined in sys/types.h instead */
93 #include <sys/types.h>
94 #else
95 #include <stdint.h>
96 #endif
97
98 /* DNSServiceRef, DNSRecordRef
99 *
100 * Opaque internal data types.
101 * Note: client is responsible for serializing access to these structures if
102 * they are shared between concurrent threads.
103 */
104
105 typedef struct _DNSServiceRef_t *DNSServiceRef;
106 typedef struct _DNSRecordRef_t *DNSRecordRef;
107
108 /* General flags used in functions defined below */
109 enum
110 {
111 kDNSServiceFlagsMoreComing = 0x1,
112 /* MoreComing indicates to a callback that at least one more result is
113 * queued and will be delivered following immediately after this one.
114 * Applications should not update their UI to display browse
115 * results when the MoreComing flag is set, because this would
116 * result in a great deal of ugly flickering on the screen.
117 * Applications should instead wait until until MoreComing is not set,
118 * and then update their UI.
119 * When MoreComing is not set, that doesn't mean there will be no more
120 * answers EVER, just that there are no more answers immediately
121 * available right now at this instant. If more answers become available
122 * in the future they will be delivered as usual.
123 */
124
125 kDNSServiceFlagsAdd = 0x2,
126 kDNSServiceFlagsDefault = 0x4,
127 /* Flags for domain enumeration and browse/query reply callbacks.
128 * "Default" applies only to enumeration and is only valid in
129 * conjuction with "Add". An enumeration callback with the "Add"
130 * flag NOT set indicates a "Remove", i.e. the domain is no longer
131 * valid.
132 */
133
134 kDNSServiceFlagsNoAutoRename = 0x8,
135 /* Flag for specifying renaming behavior on name conflict when registering
136 * non-shared records. By default, name conflicts are automatically handled
137 * by renaming the service. NoAutoRename overrides this behavior - with this
138 * flag set, name conflicts will result in a callback. The NoAutorename flag
139 * is only valid if a name is explicitly specified when registering a service
140 * (ie the default name is not used.)
141 */
142
143 kDNSServiceFlagsShared = 0x10,
144 kDNSServiceFlagsUnique = 0x20,
145 /* Flag for registering individual records on a connected
146 * DNSServiceRef. Shared indicates that there may be multiple records
147 * with this name on the network (e.g. PTR records). Unique indicates that the
148 * record's name is to be unique on the network (e.g. SRV records).
149 */
150
151 kDNSServiceFlagsBrowseDomains = 0x40,
152 kDNSServiceFlagsRegistrationDomains = 0x80,
153 /* Flags for specifying domain enumeration type in DNSServiceEnumerateDomains.
154 * BrowseDomains enumerates domains recommended for browsing, RegistrationDomains
155 * enumerates domains recommended for registration.
156 */
157
158 kDNSServiceFlagsLongLivedQuery = 0x100
159 /* Flag for creating a long-lived unicast query for the DNSServiceQueryRecord call. */
160 };
161
162 /* possible error code values */
163 enum
164 {
165 kDNSServiceErr_NoError = 0,
166 kDNSServiceErr_Unknown = -65537, /* 0xFFFE FFFF */
167 kDNSServiceErr_NoSuchName = -65538,
168 kDNSServiceErr_NoMemory = -65539,
169 kDNSServiceErr_BadParam = -65540,
170 kDNSServiceErr_BadReference = -65541,
171 kDNSServiceErr_BadState = -65542,
172 kDNSServiceErr_BadFlags = -65543,
173 kDNSServiceErr_Unsupported = -65544,
174 kDNSServiceErr_NotInitialized = -65545,
175 kDNSServiceErr_AlreadyRegistered = -65547,
176 kDNSServiceErr_NameConflict = -65548,
177 kDNSServiceErr_Invalid = -65549,
178 kDNSServiceErr_Incompatible = -65551, /* client library incompatible with daemon */
179 kDNSServiceErr_BadInterfaceIndex = -65552,
180 kDNSServiceErr_Refused = -65553,
181 kDNSServiceErr_NoSuchRecord = -65554,
182 kDNSServiceErr_NoAuth = -65555,
183 kDNSServiceErr_NoSuchKey = -65556
184 /* mDNS Error codes are in the range
185 * FFFE FF00 (-65792) to FFFE FFFF (-65537) */
186 };
187
188
189 /* Maximum length, in bytes, of a domain name represented as an escaped C-String */
190 /* including the final trailing dot, and the C-String terminating NULL at the end */
191
192 #define kDNSServiceMaxDomainName 1005
193
194 /* Constants for specifying an interface index. Specific interface indexes are
195 * identified via a 32-bit unsigned integer returned by the if_nametoindex()
196 * family of calls
197 */
198
199 #define kDNSServiceInterfaceIndexAny 0
200 #define kDNSServiceInterfaceIndexLocalOnly ( (uint32_t) ~0 )
201
202
203 typedef uint32_t DNSServiceFlags;
204 typedef int32_t DNSServiceErrorType;
205
206
207 /*********************************************************************************************
208 *
209 * Unix Domain Socket access, DNSServiceRef deallocation, and data processing functions
210 *
211 *********************************************************************************************/
212
213
214 /* DNSServiceRefSockFD()
215 *
216 * Access underlying Unix domain socket for an initialized DNSServiceRef.
217 * The DNS Service Discovery implmementation uses this socket to communicate between
218 * the client and the mDNSResponder daemon. The application MUST NOT directly read from
219 * or write to this socket. Access to the socket is provided so that it can be used as a
220 * run loop source, or in a select() loop: when data is available for reading on the socket,
221 * DNSServiceProcessResult() should be called, which will extract the daemon's reply from
222 * the socket, and pass it to the appropriate application callback. By using a run loop or
223 * select(), results from the daemon can be processed asynchronously. Without using these
224 * constructs, DNSServiceProcessResult() will block until the response from the daemon arrives.
225 * The client is responsible for ensuring that the data on the socket is processed in a timely
226 * fashion - the daemon may terminate its connection with a client that does not clear its
227 * socket buffer.
228 *
229 * sdRef: A DNSServiceRef initialized by any of the DNSService calls.
230 *
231 * return value: The DNSServiceRef's underlying socket descriptor, or -1 on
232 * error.
233 */
234
235 int DNSServiceRefSockFD(DNSServiceRef sdRef);
236
237
238 /* DNSServiceProcessResult()
239 *
240 * Read a reply from the daemon, calling the appropriate application callback. This call will
241 * block until the daemon's response is received. Use DNSServiceRefSockFD() in
242 * conjunction with a run loop or select() to determine the presence of a response from the
243 * server before calling this function to process the reply without blocking. Call this function
244 * at any point if it is acceptable to block until the daemon's response arrives. Note that the
245 * client is responsible for ensuring that DNSServiceProcessResult() is called whenever there is
246 * a reply from the daemon - the daemon may terminate its connection with a client that does not
247 * process the daemon's responses.
248 *
249 * sdRef: A DNSServiceRef initialized by any of the DNSService calls
250 * that take a callback parameter.
251 *
252 * return value: Returns kDNSServiceErr_NoError on success, otherwise returns
253 * an error code indicating the specific failure that occurred.
254 */
255
256 DNSServiceErrorType DNSServiceProcessResult(DNSServiceRef sdRef);
257
258
259 /* DNSServiceRefDeallocate()
260 *
261 * Terminate a connection with the daemon and free memory associated with the DNSServiceRef.
262 * Any services or records registered with this DNSServiceRef will be deregistered. Any
263 * Browse, Resolve, or Query operations called with this reference will be terminated.
264 *
265 * Note: If the reference's underlying socket is used in a run loop or select() call, it should
266 * be removed BEFORE DNSServiceRefDeallocate() is called, as this function closes the reference's
267 * socket.
268 *
269 * Note: If the reference was initialized with DNSServiceCreateConnection(), any DNSRecordRefs
270 * created via this reference will be invalidated by this call - the resource records are
271 * deregistered, and their DNSRecordRefs may not be used in subsequent functions. Similarly,
272 * if the reference was initialized with DNSServiceRegister, and an extra resource record was
273 * added to the service via DNSServiceAddRecord(), the DNSRecordRef created by the Add() call
274 * is invalidated when this function is called - the DNSRecordRef may not be used in subsequent
275 * functions.
276 *
277 * Note: This call is to be used only with the DNSServiceRef defined by this API. It is
278 * not compatible with dns_service_discovery_ref objects defined in the legacy Mach-based
279 * DNSServiceDiscovery.h API.
280 *
281 * sdRef: A DNSServiceRef initialized by any of the DNSService calls.
282 *
283 */
284
285 void DNSServiceRefDeallocate(DNSServiceRef sdRef);
286
287
288 /*********************************************************************************************
289 *
290 * Domain Enumeration
291 *
292 *********************************************************************************************/
293
294 /* DNSServiceEnumerateDomains()
295 *
296 * Asynchronously enumerate domains available for browsing and registration.
297 * Currently, the only domain returned is "local.", but other domains will be returned in future.
298 *
299 * The enumeration MUST be cancelled via DNSServiceRefDeallocate() when no more domains
300 * are to be found.
301 *
302 *
303 * DNSServiceDomainEnumReply Callback Parameters:
304 *
305 * sdRef: The DNSServiceRef initialized by DNSServiceEnumerateDomains().
306 *
307 * flags: Possible values are:
308 * kDNSServiceFlagsMoreComing
309 * kDNSServiceFlagsAdd
310 * kDNSServiceFlagsDefault
311 *
312 * interfaceIndex: Specifies the interface on which the domain exists. (The index for a given
313 * interface is determined via the if_nametoindex() family of calls.)
314 *
315 * errorCode: Will be kDNSServiceErr_NoError (0) on success, otherwise indicates
316 * the failure that occurred (other parameters are undefined if errorCode is nonzero).
317 *
318 * replyDomain: The name of the domain.
319 *
320 * context: The context pointer passed to DNSServiceEnumerateDomains.
321 *
322 */
323
324 typedef void (*DNSServiceDomainEnumReply)
325 (
326 DNSServiceRef sdRef,
327 DNSServiceFlags flags,
328 uint32_t interfaceIndex,
329 DNSServiceErrorType errorCode,
330 const char *replyDomain,
331 void *context
332 );
333
334
335 /* DNSServiceEnumerateDomains() Parameters:
336 *
337 *
338 * sdRef: A pointer to an uninitialized DNSServiceRef. May be passed to
339 * DNSServiceRefDeallocate() to cancel the enumeration.
340 *
341 * flags: Possible values are:
342 * kDNSServiceFlagsBrowseDomains to enumerate domains recommended for browsing.
343 * kDNSServiceFlagsRegistrationDomains to enumerate domains recommended
344 * for registration.
345 *
346 * interfaceIndex: If non-zero, specifies the interface on which to look for domains.
347 * (the index for a given interface is determined via the if_nametoindex()
348 * family of calls.) Most applications will pass 0 to enumerate domains on
349 * all interfaces.
350 *
351 * callBack: The function to be called when a domain is found or the call asynchronously
352 * fails.
353 *
354 * context: An application context pointer which is passed to the callback function
355 * (may be NULL).
356 *
357 * return value: Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous
358 * errors are delivered to the callback), otherwise returns an error code indicating
359 * the error that occurred (the callback is not invoked and the DNSServiceRef
360 * is not initialized.)
361 */
362
363 DNSServiceErrorType DNSServiceEnumerateDomains
364 (
365 DNSServiceRef *sdRef,
366 DNSServiceFlags flags,
367 uint32_t interfaceIndex,
368 DNSServiceDomainEnumReply callBack,
369 void *context /* may be NULL */
370 );
371
372
373 /*********************************************************************************************
374 *
375 * Service Registration
376 *
377 *********************************************************************************************/
378
379 /* Register a service that is discovered via Browse() and Resolve() calls.
380 *
381 *
382 * DNSServiceRegisterReply() Callback Parameters:
383 *
384 * sdRef: The DNSServiceRef initialized by DNSServiceRegister().
385 *
386 * flags: Currently unused, reserved for future use.
387 *
388 * errorCode: Will be kDNSServiceErr_NoError on success, otherwise will
389 * indicate the failure that occurred (including name conflicts, if the
390 * kDNSServiceFlagsNoAutoRename flag was passed to the
391 * callout.) Other parameters are undefined if errorCode is nonzero.
392 *
393 * name: The service name registered (if the application did not specify a name in
394 * DNSServiceRegister(), this indicates what name was automatically chosen).
395 *
396 * regtype: The type of service registered, as it was passed to the callout.
397 *
398 * domain: The domain on which the service was registered (if the application did not
399 * specify a domain in DNSServiceRegister(), this indicates the default domain
400 * on which the service was registered).
401 *
402 * context: The context pointer that was passed to the callout.
403 *
404 */
405
406 typedef void (*DNSServiceRegisterReply)
407 (
408 DNSServiceRef sdRef,
409 DNSServiceFlags flags,
410 DNSServiceErrorType errorCode,
411 const char *name,
412 const char *regtype,
413 const char *domain,
414 void *context
415 );
416
417
418 /* DNSServiceRegister() Parameters:
419 *
420 * sdRef: A pointer to an uninitialized DNSServiceRef. If this call succeeds, the reference
421 * may be passed to
422 * DNSServiceRefDeallocate() to deregister the service.
423 *
424 * interfaceIndex: If non-zero, specifies the interface on which to register the service
425 * (the index for a given interface is determined via the if_nametoindex()
426 * family of calls.) Most applications will pass 0 to register on all
427 * available interfaces. Pass -1 to register a service only on the local
428 * machine (service will not be visible to remote hosts.)
429 *
430 * flags: Indicates the renaming behavior on name conflict (most applications
431 * will pass 0). See flag definitions above for details.
432 *
433 * name: If non-NULL, specifies the service name to be registered.
434 * Most applications will not specify a name, in which case the
435 * computer name is used (this name is communicated to the client via
436 * the callback).
437 *
438 * regtype: The service type followed by the protocol, separated by a dot
439 * (e.g. "_ftp._tcp"). The transport protocol must be "_tcp" or "_udp".
440 * New service types should be registered at htp://www.dns-sd.org/ServiceTypes.html.
441 *
442 * domain: If non-NULL, specifies the domain on which to advertise the service.
443 * Most applications will not specify a domain, instead automatically
444 * registering in the default domain(s).
445 *
446 * host: If non-NULL, specifies the SRV target host name. Most applications
447 * will not specify a host, instead automatically using the machine's
448 * default host name(s). Note that specifying a non-NULL host does NOT
449 * create an address record for that host - the application is responsible
450 * for ensuring that the appropriate address record exists, or creating it
451 * via DNSServiceRegisterRecord().
452 *
453 * port: The port, in network byte order, on which the service accepts connections.
454 * Pass 0 for a "placeholder" service (i.e. a service that will not be discovered
455 * by browsing, but will cause a name conflict if another client tries to
456 * register that same name). Most clients will not use placeholder services.
457 *
458 * txtLen: The length of the txtRecord, in bytes. Must be zero if the txtRecord is NULL.
459 *
460 * txtRecord: The txt record rdata. May be NULL. Note that a non-NULL txtRecord
461 * MUST be a properly formatted DNS TXT record, i.e. <length byte> <data>
462 * <length byte> <data> ...
463 *
464 * callBack: The function to be called when the registration completes or asynchronously
465 * fails. The client MAY pass NULL for the callback - The client will NOT be notified
466 * of the default values picked on its behalf, and the client will NOT be notified of any
467 * asynchronous errors (e.g. out of memory errors, etc.) that may prevent the registration
468 * of the service. The client may NOT pass the NoAutoRename flag if the callback is NULL.
469 * The client may still deregister the service at any time via DNSServiceRefDeallocate().
470 *
471 * context: An application context pointer which is passed to the callback function
472 * (may be NULL).
473 *
474 * return value: Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous
475 * errors are delivered to the callback), otherwise returns an error code indicating
476 * the error that occurred (the callback is never invoked and the DNSServiceRef
477 * is not initialized.)
478 *
479 */
480
481 DNSServiceErrorType DNSServiceRegister
482 (
483 DNSServiceRef *sdRef,
484 DNSServiceFlags flags,
485 uint32_t interfaceIndex,
486 const char *name, /* may be NULL */
487 const char *regtype,
488 const char *domain, /* may be NULL */
489 const char *host, /* may be NULL */
490 uint16_t port,
491 uint16_t txtLen,
492 const void *txtRecord, /* may be NULL */
493 DNSServiceRegisterReply callBack, /* may be NULL */
494 void *context /* may be NULL */
495 );
496
497
498 /* DNSServiceAddRecord()
499 *
500 * Add a record to a registered service. The name of the record will be the same as the
501 * registered service's name.
502 * The record can later be updated or deregistered by passing the RecordRef initialized
503 * by this function to DNSServiceUpdateRecord() or DNSServiceRemoveRecord().
504 *
505 *
506 * Parameters;
507 *
508 * sdRef: A DNSServiceRef initialized by DNSServiceRegister().
509 *
510 * RecordRef: A pointer to an uninitialized DNSRecordRef. Upon succesfull completion of this
511 * call, this ref may be passed to DNSServiceUpdateRecord() or DNSServiceRemoveRecord().
512 * If the above DNSServiceRef is passed to DNSServiceRefDeallocate(), RecordRef is also
513 * invalidated and may not be used further.
514 *
515 * flags: Currently ignored, reserved for future use.
516 *
517 * rrtype: The type of the record (e.g. TXT, SRV, etc), as defined in nameser.h.
518 *
519 * rdlen: The length, in bytes, of the rdata.
520 *
521 * rdata: The raw rdata to be contained in the added resource record.
522 *
523 * ttl: The time to live of the resource record, in seconds.
524 *
525 * return value: Returns kDNSServiceErr_NoError on success, otherwise returns an
526 * error code indicating the error that occurred (the RecordRef is not initialized).
527 */
528
529 DNSServiceErrorType DNSServiceAddRecord
530 (
531 DNSServiceRef sdRef,
532 DNSRecordRef *RecordRef,
533 DNSServiceFlags flags,
534 uint16_t rrtype,
535 uint16_t rdlen,
536 const void *rdata,
537 uint32_t ttl
538 );
539
540
541 /* DNSServiceUpdateRecord
542 *
543 * Update a registered resource record. The record must either be:
544 * - The primary txt record of a service registered via DNSServiceRegister()
545 * - A record added to a registered service via DNSServiceAddRecord()
546 * - An individual record registered by DNSServiceRegisterRecord()
547 *
548 *
549 * Parameters:
550 *
551 * sdRef: A DNSServiceRef that was initialized by DNSServiceRegister()
552 * or DNSServiceCreateConnection().
553 *
554 * RecordRef: A DNSRecordRef initialized by DNSServiceAddRecord, or NULL to update the
555 * service's primary txt record.
556 *
557 * flags: Currently ignored, reserved for future use.
558 *
559 * rdlen: The length, in bytes, of the new rdata.
560 *
561 * rdata: The new rdata to be contained in the updated resource record.
562 *
563 * ttl: The time to live of the updated resource record, in seconds.
564 *
565 * return value: Returns kDNSServiceErr_NoError on success, otherwise returns an
566 * error code indicating the error that occurred.
567 */
568
569 DNSServiceErrorType DNSServiceUpdateRecord
570 (
571 DNSServiceRef sdRef,
572 DNSRecordRef RecordRef, /* may be NULL */
573 DNSServiceFlags flags,
574 uint16_t rdlen,
575 const void *rdata,
576 uint32_t ttl
577 );
578
579
580 /* DNSServiceRemoveRecord
581 *
582 * Remove a record previously added to a service record set via DNSServiceAddRecord(), or deregister
583 * an record registered individually via DNSServiceRegisterRecord().
584 *
585 * Parameters:
586 *
587 * sdRef: A DNSServiceRef initialized by DNSServiceRegister() (if the
588 * record being removed was registered via DNSServiceAddRecord()) or by
589 * DNSServiceCreateConnection() (if the record being removed was registered via
590 * DNSServiceRegisterRecord()).
591 *
592 * recordRef: A DNSRecordRef initialized by a successful call to DNSServiceAddRecord()
593 * or DNSServiceRegisterRecord().
594 *
595 * flags: Currently ignored, reserved for future use.
596 *
597 * return value: Returns kDNSServiceErr_NoError on success, otherwise returns an
598 * error code indicating the error that occurred.
599 */
600
601 DNSServiceErrorType DNSServiceRemoveRecord
602 (
603 DNSServiceRef sdRef,
604 DNSRecordRef RecordRef,
605 DNSServiceFlags flags
606 );
607
608
609 /*********************************************************************************************
610 *
611 * Service Discovery
612 *
613 *********************************************************************************************/
614
615 /* Browse for instances of a service.
616 *
617 *
618 * DNSServiceBrowseReply() Parameters:
619 *
620 * sdRef: The DNSServiceRef initialized by DNSServiceBrowse().
621 *
622 * flags: Possible values are kDNSServiceFlagsMoreComing and kDNSServiceFlagsAdd.
623 * See flag definitions for details.
624 *
625 * interfaceIndex: The interface on which the service is advertised. This index should
626 * be passed to DNSServiceResolve() when resolving the service.
627 *
628 * errorCode: Will be kDNSServiceErr_NoError (0) on success, otherwise will
629 * indicate the failure that occurred. Other parameters are undefined if
630 * the errorCode is nonzero.
631 *
632 * serviceName: The service name discovered.
633 *
634 * regtype: The service type, as passed in to DNSServiceBrowse().
635 *
636 * domain: The domain on which the service was discovered (if the application did not
637 * specify a domain in DNSServicBrowse(), this indicates the domain on which the
638 * service was discovered.)
639 *
640 * context: The context pointer that was passed to the callout.
641 *
642 */
643
644 typedef void (*DNSServiceBrowseReply)
645 (
646 DNSServiceRef sdRef,
647 DNSServiceFlags flags,
648 uint32_t interfaceIndex,
649 DNSServiceErrorType errorCode,
650 const char *serviceName,
651 const char *regtype,
652 const char *replyDomain,
653 void *context
654 );
655
656
657 /* DNSServiceBrowse() Parameters:
658 *
659 * sdRef: A pointer to an uninitialized DNSServiceRef. May be passed to
660 * DNSServiceRefDeallocate() to terminate the browse.
661 *
662 * flags: Currently ignored, reserved for future use.
663 *
664 * interfaceIndex: If non-zero, specifies the interface on which to browse for services
665 * (the index for a given interface is determined via the if_nametoindex()
666 * family of calls.) Most applications will pass 0 to browse on all available
667 * interfaces. Pass -1 to only browse for services provided on the local host.
668 *
669 * regtype: The service type being browsed for followed by the protocol, separated by a
670 * dot (e.g. "_ftp._tcp"). The transport protocol must be "_tcp" or "_udp".
671 *
672 * domain: If non-NULL, specifies the domain on which to browse for services.
673 * Most applications will not specify a domain, instead browsing on the
674 * default domain(s).
675 *
676 * callBack: The function to be called when an instance of the service being browsed for
677 * is found, or if the call asynchronously fails.
678 *
679 * context: An application context pointer which is passed to the callback function
680 * (may be NULL).
681 *
682 * return value: Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous
683 * errors are delivered to the callback), otherwise returns an error code indicating
684 * the error that occurred (the callback is not invoked and the DNSServiceRef
685 * is not initialized.)
686 */
687
688 DNSServiceErrorType DNSServiceBrowse
689 (
690 DNSServiceRef *sdRef,
691 DNSServiceFlags flags,
692 uint32_t interfaceIndex,
693 const char *regtype,
694 const char *domain, /* may be NULL */
695 DNSServiceBrowseReply callBack,
696 void *context /* may be NULL */
697 );
698
699
700 /* DNSServiceResolve()
701 *
702 * Resolve a service name discovered via DNSServiceBrowse() to a target host name, port number, and
703 * txt record.
704 *
705 * Note: Applications should NOT use DNSServiceResolve() solely for txt record monitoring - use
706 * DNSServiceQueryRecord() instead, as it is more efficient for this task.
707 *
708 * Note: When the desired results have been returned, the client MUST terminate the resolve by calling
709 * DNSServiceRefDeallocate().
710 *
711 * Note: DNSServiceResolve() behaves correctly for typical services that have a single SRV record and
712 * a single TXT record (the TXT record may be empty.) To resolve non-standard services with multiple
713 * SRV or TXT records, DNSServiceQueryRecord() should be used.
714 *
715 * DNSServiceResolveReply Callback Parameters:
716 *
717 * sdRef: The DNSServiceRef initialized by DNSServiceResolve().
718 *
719 * flags: Currently unused, reserved for future use.
720 *
721 * interfaceIndex: The interface on which the service was resolved.
722 *
723 * errorCode: Will be kDNSServiceErr_NoError (0) on success, otherwise will
724 * indicate the failure that occurred. Other parameters are undefined if
725 * the errorCode is nonzero.
726 *
727 * fullname: The full service domain name, in the form <servicename>.<protocol>.<domain>.
728 * (Any literal dots (".") are escaped with a backslash ("\."), and literal
729 * backslashes are escaped with a second backslash ("\\"), e.g. a web server
730 * named "Dr. Pepper" would have the fullname "Dr\.\032Pepper._http._tcp.local.").
731 * This is the appropriate format to pass to standard system DNS APIs such as
732 * res_query(), or to the special-purpose functions included in this API that
733 * take fullname parameters.
734 *
735 * hosttarget: The target hostname of the machine providing the service. This name can
736 * be passed to functions like gethostbyname() to identify the host's IP address.
737 *
738 * port: The port, in network byte order, on which connections are accepted for this service.
739 *
740 * txtLen: The length of the txt record, in bytes.
741 *
742 * txtRecord: The service's primary txt record, in standard txt record format.
743 *
744
745 * context: The context pointer that was passed to the callout.
746 *
747 */
748
749 typedef void (*DNSServiceResolveReply)
750 (
751 DNSServiceRef sdRef,
752 DNSServiceFlags flags,
753 uint32_t interfaceIndex,
754 DNSServiceErrorType errorCode,
755 const char *fullname,
756 const char *hosttarget,
757 uint16_t port,
758 uint16_t txtLen,
759 const char *txtRecord,
760 void *context
761 );
762
763
764 /* DNSServiceResolve() Parameters
765 *
766 * sdRef: A pointer to an uninitialized DNSServiceRef. May be passed to
767 * DNSServiceRefDeallocate() to terminate the resolve.
768 *
769 * flags: Currently ignored, reserved for future use.
770 *
771 * interfaceIndex: The interface on which to resolve the service. The client should
772 * pass the interface on which the servicename was discovered, i.e.
773 * the interfaceIndex passed to the DNSServiceBrowseReply callback,
774 * or 0 to resolve the named service on all available interfaces.
775 *
776 * name: The servicename to be resolved.
777 *
778 * regtype: The service type being resolved followed by the protocol, separated by a
779 * dot (e.g. "_ftp._tcp"). The transport protocol must be "_tcp" or "_udp".
780 *
781 * domain: The domain on which the service is registered, i.e. the domain passed
782 * to the DNSServiceBrowseReply callback.
783 *
784 * callBack: The function to be called when a result is found, or if the call
785 * asynchronously fails.
786 *
787 * context: An application context pointer which is passed to the callback function
788 * (may be NULL).
789 *
790 * return value: Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous
791 * errors are delivered to the callback), otherwise returns an error code indicating
792 * the error that occurred (the callback is never invoked and the DNSServiceRef
793 * is not initialized.)
794 */
795
796 DNSServiceErrorType DNSServiceResolve
797 (
798 DNSServiceRef *sdRef,
799 DNSServiceFlags flags,
800 uint32_t interfaceIndex,
801 const char *name,
802 const char *regtype,
803 const char *domain,
804 DNSServiceResolveReply callBack,
805 void *context /* may be NULL */
806 );
807
808
809 /*********************************************************************************************
810 *
811 * Special Purpose Calls (most applications will not use these)
812 *
813 *********************************************************************************************/
814
815 /* Note on DNS Naming Conventions:
816 *
817 * The functions below refer to resource records by their full domain name, unlike the
818 * functions above which divide the name into servicename/regtype/domain fields. In the
819 * functions above, a dot (".") is considered to be a literal dot in the servicename field
820 * (e.g. "Dr. Pepper") and a label separator in the regtype ("_ftp._tcp") or domain
821 * ("apple.com") fields. Literal dots in the domain field would be escaped with a backslash,
822 * and literal backslashes would be escaped with a second backslash (this is generally not an
823 * issue, as domain names on the Internet today almost never use characters other than
824 * letters, digits, or hyphens, and the dots are label separators.) Furthermore, this is
825 * transparent to the caller, so long as the fields are passed between functions without
826 * manipulation. However, the following, special-purpose calls use a single, full domain
827 * name. As such, all dots are considered to be label separators, unless escaped, and all
828 * backslashes are considered to be escape characters, unless preceded by a second backslash.
829 * For example, the name "Dr. Smith \ Dr. Johnson" could be passed literally as a service
830 * name parameter in the above calls, but in the special purpose calls, the dots and backslash
831 * would have to be escaped (e.g. "Dr\. Smith \\ Dr\. Johnson._ftp._tcp.apple.com" for an ftp
832 * service on the apple.com domain.) The function DNSServiceConstructFullName() is provided
833 * to aid in this conversion from servicename/regtype/domain to a single fully-qualified DNS
834 * name with proper escaping.
835 */
836
837 /* DNSServiceCreateConnection()
838 *
839 * Create a connection to the daemon allowing efficient registration of
840 * multiple individual records.
841 *
842 *
843 * Parameters:
844 *
845 * sdRef: A pointer to an uninitialized DNSServiceRef. Deallocating
846 * the reference (via DNSServiceRefDeallocate()) severs the
847 * connection and deregisters all records registered on this connection.
848 *
849 * return value: Returns kDNSServiceErr_NoError on success, otherwise returns
850 * an error code indicating the specific failure that occurred (in which
851 * case the DNSServiceRef is not initialized).
852 */
853
854 DNSServiceErrorType DNSServiceCreateConnection(DNSServiceRef *sdRef);
855
856
857 /* DNSServiceRegisterRecord
858 *
859 * Register an individual resource record on a connected DNSServiceRef.
860 *
861 * Note that name conflicts occurring for records registered via this call must be handled
862 * by the client in the callback.
863 *
864 *
865 * DNSServiceRegisterRecordReply() parameters:
866 *
867 * sdRef: The connected DNSServiceRef initialized by
868 * DNSServiceDiscoveryConnect().
869 *
870 * RecordRef: The DNSRecordRef initialized by DNSServiceRegisterRecord(). If the above
871 * DNSServiceRef is passed to DNSServiceRefDeallocate(), this DNSRecordRef is
872 * invalidated, and may not be used further.
873 *
874 * flags: Currently unused, reserved for future use.
875 *
876 * errorCode: Will be kDNSServiceErr_NoError on success, otherwise will
877 * indicate the failure that occurred (including name conflicts.)
878 * Other parameters are undefined if errorCode is nonzero.
879 *
880 * context: The context pointer that was passed to the callout.
881 *
882 */
883
884 typedef void (*DNSServiceRegisterRecordReply)
885 (
886 DNSServiceRef sdRef,
887 DNSRecordRef RecordRef,
888 DNSServiceFlags flags,
889 DNSServiceErrorType errorCode,
890 void *context
891 );
892
893
894 /* DNSServiceRegisterRecord() Parameters:
895 *
896 * sdRef: A DNSServiceRef initialized by DNSServiceCreateConnection().
897 *
898 * RecordRef: A pointer to an uninitialized DNSRecordRef. Upon succesfull completion of this
899 * call, this ref may be passed to DNSServiceUpdateRecord() or DNSServiceRemoveRecord().
900 * (To deregister ALL records registered on a single connected DNSServiceRef
901 * and deallocate each of their corresponding DNSServiceRecordRefs, call
902 * DNSServiceRefDealloocate()).
903 *
904 * flags: Possible values are kDNSServiceFlagsShared or kDNSServiceFlagsUnique
905 * (see flag type definitions for details).
906 *
907 * interfaceIndex: If non-zero, specifies the interface on which to register the record
908 * (the index for a given interface is determined via the if_nametoindex()
909 * family of calls.) Passing 0 causes the record to be registered on all interfaces.
910 * Passing -1 causes the record to only be visible on the local host.
911 *
912 * fullname: The full domain name of the resource record.
913 *
914 * rrtype: The numerical type of the resource record (e.g. PTR, SRV, etc), as defined
915 * in nameser.h.
916 *
917 * rrclass: The class of the resource record, as defined in nameser.h (usually 1 for the
918 * Internet class).
919 *
920 * rdlen: Length, in bytes, of the rdata.
921 *
922 * rdata: A pointer to the raw rdata, as it is to appear in the DNS record.
923 *
924 * ttl: The time to live of the resource record, in seconds.
925 *
926 * callBack: The function to be called when a result is found, or if the call
927 * asynchronously fails (e.g. because of a name conflict.)
928 *
929 * context: An application context pointer which is passed to the callback function
930 * (may be NULL).
931 *
932 * return value: Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous
933 * errors are delivered to the callback), otherwise returns an error code indicating
934 * the error that occurred (the callback is never invoked and the DNSRecordRef is
935 * not initialized.)
936 */
937
938 DNSServiceErrorType DNSServiceRegisterRecord
939 (
940 DNSServiceRef sdRef,
941 DNSRecordRef *RecordRef,
942 DNSServiceFlags flags,
943 uint32_t interfaceIndex,
944 const char *fullname,
945 uint16_t rrtype,
946 uint16_t rrclass,
947 uint16_t rdlen,
948 const void *rdata,
949 uint32_t ttl,
950 DNSServiceRegisterRecordReply callBack,
951 void *context /* may be NULL */
952 );
953
954
955 /* DNSServiceQueryRecord
956 *
957 * Query for an arbitrary DNS record.
958 *
959 *
960 * DNSServiceQueryRecordReply() Callback Parameters:
961 *
962 * sdRef: The DNSServiceRef initialized by DNSServiceQueryRecord().
963 *
964 * flags: Possible values are kDNSServiceFlagsMoreComing and
965 * kDNSServiceFlagsAdd. The Add flag is NOT set for PTR records
966 * with a ttl of 0, i.e. "Remove" events.
967 *
968 * interfaceIndex: The interface on which the query was resolved (the index for a given
969 * interface is determined via the if_nametoindex() family of calls).
970 *
971 * errorCode: Will be kDNSServiceErr_NoError on success, otherwise will
972 * indicate the failure that occurred. Other parameters are undefined if
973 * errorCode is nonzero.
974 *
975 * fullname: The resource record's full domain name.
976 *
977 * rrtype: The resource record's type (e.g. PTR, SRV, etc) as defined in nameser.h.
978 *
979 * rrclass: The class of the resource record, as defined in nameser.h (usually 1).
980 *
981 * rdlen: The length, in bytes, of the resource record rdata.
982 *
983 * rdata: The raw rdata of the resource record.
984 *
985 * ttl: The resource record's time to live, in seconds.
986 *
987 * context: The context pointer that was passed to the callout.
988 *
989 */
990
991 typedef void (*DNSServiceQueryRecordReply)
992 (
993 DNSServiceRef DNSServiceRef,
994 DNSServiceFlags flags,
995 uint32_t interfaceIndex,
996 DNSServiceErrorType errorCode,
997 const char *fullname,
998 uint16_t rrtype,
999 uint16_t rrclass,
1000 uint16_t rdlen,
1001 const void *rdata,
1002 uint32_t ttl,
1003 void *context
1004 );
1005
1006
1007 /* DNSServiceQueryRecord() Parameters:
1008 *
1009 * sdRef: A pointer to an uninitialized DNSServiceRef.
1010 *
1011 * flags: Pass kDNSServiceFlagsLongLivedQuery to create a "long-lived" unicast
1012 * query in a non-local domain. Without setting this flag, unicast queries
1013 * will be one-shot - that is, only answers available at the time of the call
1014 * will be returned. By setting this flag, answers (including Add and Remove
1015 * events) that become available after the initial call is made will generate
1016 * callbacks. This flag has no effect on link-local multicast queries.
1017 *
1018 * interfaceIndex: If non-zero, specifies the interface on which to issue the query
1019 * (the index for a given interface is determined via the if_nametoindex()
1020 * family of calls.) Passing 0 causes the name to be queried for on all
1021 * interfaces. Passing -1 causes the name to be queried for only on the
1022 * local host.
1023 *
1024 * fullname: The full domain name of the resource record to be queried for.
1025 *
1026 * rrtype: The numerical type of the resource record to be queried for (e.g. PTR, SRV, etc)
1027 * as defined in nameser.h.
1028 *
1029 * rrclass: The class of the resource record, as defined in nameser.h
1030 * (usually 1 for the Internet class).
1031 *
1032 * callBack: The function to be called when a result is found, or if the call
1033 * asynchronously fails.
1034 *
1035 * context: An application context pointer which is passed to the callback function
1036 * (may be NULL).
1037 *
1038 * return value: Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous
1039 * errors are delivered to the callback), otherwise returns an error code indicating
1040 * the error that occurred (the callback is never invoked and the DNSServiceRef
1041 * is not initialized.)
1042 */
1043
1044 DNSServiceErrorType DNSServiceQueryRecord
1045 (
1046 DNSServiceRef *sdRef,
1047 DNSServiceFlags flags,
1048 uint32_t interfaceIndex,
1049 const char *fullname,
1050 uint16_t rrtype,
1051 uint16_t rrclass,
1052 DNSServiceQueryRecordReply callBack,
1053 void *context /* may be NULL */
1054 );
1055
1056
1057 /* DNSServiceReconfirmRecord
1058 *
1059 * Instruct the daemon to verify the validity of a resource record that appears to
1060 * be out of date (e.g. because tcp connection to a service's target failed.)
1061 * Causes the record to be flushed from the daemon's cache (as well as all other
1062 * daemons' caches on the network) if the record is determined to be invalid.
1063 *
1064 * Parameters:
1065 *
1066 * flags: Currently unused, reserved for future use.
1067 *
1068 * fullname: The resource record's full domain name.
1069 *
1070 * rrtype: The resource record's type (e.g. PTR, SRV, etc) as defined in nameser.h.
1071 *
1072 * rrclass: The class of the resource record, as defined in nameser.h (usually 1).
1073 *
1074 * rdlen: The length, in bytes, of the resource record rdata.
1075 *
1076 * rdata: The raw rdata of the resource record.
1077 *
1078 */
1079
1080 void DNSServiceReconfirmRecord
1081 (
1082 DNSServiceFlags flags,
1083 uint32_t interfaceIndex,
1084 const char *fullname,
1085 uint16_t rrtype,
1086 uint16_t rrclass,
1087 uint16_t rdlen,
1088 const void *rdata
1089 );
1090
1091
1092 /*********************************************************************************************
1093 *
1094 * General Utility Functions
1095 *
1096 *********************************************************************************************/
1097
1098 /* DNSServiceConstructFullName()
1099 *
1100 * Concatenate a three-part domain name (as returned by the above callbacks) into a
1101 * properly-escaped full domain name. Note that callbacks in the above functions ALREADY ESCAPE
1102 * strings where necessary.
1103 *
1104 * Parameters:
1105 *
1106 * fullName: A pointer to a buffer that where the resulting full domain name is to be written.
1107 * The buffer must be kDNSServiceMaxDomainName (1005) bytes in length to
1108 * accommodate the longest legal domain name without buffer overrun.
1109 *
1110 * service: The service name - any dots or slashes must NOT be escaped.
1111 * May be NULL (to construct a PTR record name, e.g.
1112 * "_ftp._tcp.apple.com").
1113 *
1114 * regtype: The service type followed by the protocol, separated by a dot
1115 * (e.g. "_ftp._tcp").
1116 *
1117 * domain: The domain name, e.g. "apple.com". Any literal dots or backslashes
1118 * must be escaped.
1119 *
1120 * return value: Returns 0 on success, -1 on error.
1121 *
1122 */
1123
1124 int DNSServiceConstructFullName
1125 (
1126 char *fullName,
1127 const char *service, /* may be NULL */
1128 const char *regtype,
1129 const char *domain
1130 );
1131
1132
1133 /*********************************************************************************************
1134 *
1135 * TXT Record Construction Functions
1136 *
1137 *********************************************************************************************/
1138
1139 /*
1140 * A typical calling sequence for TXT record construction is something like:
1141 *
1142 * Client allocates storage for TXTRecord data (e.g. declare buffer on the stack)
1143 * TXTRecordCreate();
1144 * TXTRecordSetValue();
1145 * TXTRecordSetValue();
1146 * TXTRecordSetValue();
1147 * ...
1148 * DNSServiceRegister( ... TXTRecordGetLength(), TXTRecordGetBytesPtr() ... );
1149 * TXTRecordDeallocate();
1150 * Explicitly deallocate storage for TXTRecord data (if not allocated on the stack)
1151 */
1152
1153
1154 /* TXTRecordRef
1155 *
1156 * Opaque internal data type.
1157 * Note: Represents a DNS-SD TXT record.
1158 */
1159
1160 typedef struct _TXTRecordRef_t { char private[16]; } TXTRecordRef;
1161
1162
1163 /* TXTRecordCreate()
1164 *
1165 * Creates a new empty TXTRecordRef referencing the specified storage.
1166 *
1167 * If the buffer parameter is NULL, or the specified storage size is not
1168 * large enough to hold a key subsequently added using TXTRecordSetValue(),
1169 * then additional memory will be added as needed using malloc().
1170 *
1171 * On some platforms, when memory is low, malloc() may fail. In this
1172 * case, TXTRecordSetValue() will return kDNSServiceErr_NoMemory, and this
1173 * error condition will need to be handled as appropriate by the caller.
1174 *
1175 * You can avoid the need to handle this error condition if you ensure
1176 * that the storage you initially provide is large enough to hold all
1177 * the key/value pairs that are to be added to the record.
1178 * The caller can precompute the exact length required for all of the
1179 * key/value pairs to be added, or simply provide a fixed-sized buffer
1180 * known in advance to be large enough.
1181 * A no-value (key-only) key requires (1 + key length) bytes.
1182 * A key with empty value requires (1 + key length + 1) bytes.
1183 * A key with non-empty value requires (1 + key length + 1 + value length).
1184 * For most applications, DNS-SD TXT records are generally
1185 * less than 100 bytes, so in most cases a simple fixed-sized
1186 * 256-byte buffer will be more than sufficient.
1187 * Recommended size limits for DNS-SD TXT Records are discussed in
1188 * <http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt>
1189 *
1190 * txtRecord: A pointer to an uninitialized TXTRecordRef.
1191 *
1192 * bufferLen: The size of the storage provided in the "buffer" parameter.
1193 *
1194 * buffer: The storage used to hold the TXTRecord data.
1195 * This storage must remain valid for as long as
1196 * the TXTRecordRef.
1197 */
1198
1199 void TXTRecordCreate
1200 (
1201 TXTRecordRef *txtRecord,
1202 uint16_t bufferLen,
1203 void *buffer
1204 );
1205
1206
1207 /* TXTRecordDeallocate()
1208 *
1209 * Releases any resources allocated in the course of preparing a TXT Record
1210 * using TXTRecordCreate()/TXTRecordSetValue()/TXTRecordRemoveValue().
1211 * Ownership of the buffer provided in TXTRecordCreate() returns to the client.
1212 *
1213 * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate().
1214 *
1215 */
1216
1217 void TXTRecordDeallocate
1218 (
1219 TXTRecordRef *txtRecord
1220 );
1221
1222
1223 /* TXTRecordSetValue()
1224 *
1225 * Adds a key (optionally with value) to a TXTRecordRef. If the "key" already
1226 * exists in the TXTRecordRef, then the current value will be replaced with
1227 * the new value.
1228 * Keys may exist in four states with respect to a given TXT record:
1229 * - Absent (key does not appear at all)
1230 * - Present with no value ("key" appears alone)
1231 * - Present with empty value ("key=" appears in TXT record)
1232 * - Present with non-empty value ("key=value" appears in TXT record)
1233 * For more details refer to "Data Syntax for DNS-SD TXT Records" in
1234 * <http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt>
1235 *
1236 * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate().
1237 *
1238 * key: A null-terminated string which only contains printable ASCII
1239 * values (0x20-0x7E), excluding '=' (0x3D). Keys should be
1240 * 14 characters or less (not counting the terminating null).
1241 *
1242 * valueSize: The size of the value.
1243 *
1244 * value: Any binary value. For values that represent
1245 * textual data, UTF-8 is STRONGLY recommended.
1246 * For values that represent textual data, valueSize
1247 * should NOT include the terminating null (if any)
1248 * at the end of the string.
1249 * If NULL, then "key" will be added with no value.
1250 * If non-NULL but valueSize is zero, then "key=" will be
1251 * added with empty value.
1252 *
1253 * return value: Returns kDNSServiceErr_NoError on success.
1254 * Returns kDNSServiceErr_Invalid if the "key" string contains
1255 * illegal characters.
1256 * Returns kDNSServiceErr_NoMemory if adding this key would
1257 * exceed the available storage.
1258 */
1259
1260 DNSServiceErrorType TXTRecordSetValue
1261 (
1262 TXTRecordRef *txtRecord,
1263 const char *key,
1264 uint8_t valueSize, /* may be zero */
1265 const void *value /* may be NULL */
1266 );
1267
1268
1269 /* TXTRecordRemoveValue()
1270 *
1271 * Removes a key from a TXTRecordRef. The "key" must be an
1272 * ASCII string which exists in the TXTRecordRef.
1273 *
1274 * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate().
1275 *
1276 * key: A key name which exists in the TXTRecordRef.
1277 *
1278 * return value: Returns kDNSServiceErr_NoError on success.
1279 * Returns kDNSServiceErr_NoSuchKey if the "key" does not
1280 * exist in the TXTRecordRef.
1281 *
1282 */
1283
1284 DNSServiceErrorType TXTRecordRemoveValue
1285 (
1286 TXTRecordRef *txtRecord,
1287 const char *key
1288 );
1289
1290
1291 /* TXTRecordGetLength()
1292 *
1293 * Allows you to determine the length of the raw bytes within a TXTRecordRef.
1294 *
1295 * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate().
1296 *
1297 * return value: Returns the size of the raw bytes inside a TXTRecordRef
1298 * which you can pass directly to DNSServiceRegister() or
1299 * to DNSServiceUpdateRecord().
1300 * Returns 0 if the TXTRecordRef is empty.
1301 *
1302 */
1303
1304 uint16_t TXTRecordGetLength
1305 (
1306 const TXTRecordRef *txtRecord
1307 );
1308
1309
1310 /* TXTRecordGetBytesPtr()
1311 *
1312 * Allows you to retrieve a pointer to the raw bytes within a TXTRecordRef.
1313 *
1314 * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate().
1315 *
1316 * return value: Returns a pointer to the raw bytes inside the TXTRecordRef
1317 * which you can pass directly to DNSServiceRegister() or
1318 * to DNSServiceUpdateRecord().
1319 *
1320 */
1321
1322 const void * TXTRecordGetBytesPtr
1323 (
1324 const TXTRecordRef *txtRecord
1325 );
1326
1327
1328 /*********************************************************************************************
1329 *
1330 * TXT Record Parsing Functions
1331 *
1332 *********************************************************************************************/
1333
1334 /*
1335 * A typical calling sequence for TXT record parsing is something like:
1336 *
1337 * Receive TXT record data in DNSServiceResolve() callback
1338 * if (TXTRecordContainsKey(txtLen, txtRecord, "key")) then do something
1339 * val1ptr = TXTRecordGetValuePtr(txtLen, txtRecord, "key1", &len1);
1340 * val2ptr = TXTRecordGetValuePtr(txtLen, txtRecord, "key2", &len2);
1341 * ...
1342 * bcopy(val1ptr, myval1, len1);
1343 * bcopy(val2ptr, myval2, len2);
1344 * ...
1345 * return;
1346 *
1347 * If you wish to retain the values after return from the DNSServiceResolve()
1348 * callback, then you need to copy the data to your own storage using bcopy()
1349 * or similar, as shown in the example above.
1350 *
1351 * If for some reason you need to parse a TXT record you built yourself
1352 * using the TXT record construction functions above, then you can do
1353 * that using TXTRecordGetLength and TXTRecordGetBytesPtr calls:
1354 * TXTRecordGetValue(TXTRecordGetLength(x), TXTRecordGetBytesPtr(x), key, &len);
1355 *
1356 * Most applications only fetch keys they know about from a TXT record and
1357 * ignore the rest.
1358 * However, some debugging tools wish to fetch and display all keys.
1359 * To do that, use the TXTRecordGetCount() and TXTRecordGetItemAtIndex() calls.
1360 */
1361
1362 /* TXTRecordContainsKey()
1363 *
1364 * Allows you to determine if a given TXT Record contains a specified key.
1365 *
1366 * txtLen: The size of the received TXT Record.
1367 *
1368 * txtRecord: Pointer to the received TXT Record bytes.
1369 *
1370 * key: A null-terminated ASCII string containing the key name.
1371 *
1372 * return value: Returns 1 if the TXT Record contains the specified key.
1373 * Otherwise, it returns 0.
1374 *
1375 */
1376
1377 int TXTRecordContainsKey
1378 (
1379 uint16_t txtLen,
1380 const void *txtRecord,
1381 const char *key
1382 );
1383
1384
1385 /* TXTRecordGetValuePtr()
1386 *
1387 * Allows you to retrieve the value for a given key from a TXT Record.
1388 *
1389 * txtLen: The size of the received TXT Record
1390 *
1391 * txtRecord: Pointer to the received TXT Record bytes.
1392 *
1393 * key: A null-terminated ASCII string containing the key name.
1394 *
1395 * valueLen: On output, will be set to the size of the "value" data.
1396 *
1397 * return value: Returns NULL if the key does not exist in this TXT record,
1398 * or exists with no value (to differentiate between
1399 * these two cases use TXTRecordContainsKey()).
1400 * Returns pointer to location within TXT Record bytes
1401 * if the key exists with empty or non-empty value.
1402 * For empty value, valueLen will be zero.
1403 * For non-empty value, valueLen will be length of value data.
1404 */
1405
1406 const void * TXTRecordGetValuePtr
1407 (
1408 uint16_t txtLen,
1409 const void *txtRecord,
1410 const char *key,
1411 uint8_t *valueLen
1412 );
1413
1414
1415 /* TXTRecordGetCount()
1416 *
1417 * Returns the number of keys stored in the TXT Record. The count
1418 * can be used with TXTRecordGetItemAtIndex() to iterate through the keys.
1419 *
1420 * txtLen: The size of the received TXT Record.
1421 *
1422 * txtRecord: Pointer to the received TXT Record bytes.
1423 *
1424 * return value: Returns the total number of keys in the TXT Record.
1425 *
1426 */
1427
1428 uint16_t TXTRecordGetCount
1429 (
1430 uint16_t txtLen,
1431 const void *txtRecord
1432 );
1433
1434
1435 /* TXTRecordGetItemAtIndex()
1436 *
1437 * Allows you to retrieve a key name and value pointer, given an index into
1438 * a TXT Record. Legal index values range from zero to TXTRecordGetCount()-1.
1439 * It's also possible to iterate through keys in a TXT record by simply
1440 * calling TXTRecordGetItemAtIndex() repeatedly, beginning with index zero
1441 * and increasing until TXTRecordGetItemAtIndex() returns kDNSServiceErr_Invalid.
1442 *
1443 * On return:
1444 * For keys with no value, *value is set to NULL and *valueLen is zero.
1445 * For keys with empty value, *value is non-NULL and *valueLen is zero.
1446 * For keys with non-empty value, *value is non-NULL and *valueLen is non-zero.
1447 *
1448 * txtLen: The size of the received TXT Record.
1449 *
1450 * txtRecord: Pointer to the received TXT Record bytes.
1451 *
1452 * index: An index into the TXT Record.
1453 *
1454 * keyBufLen: The size of the string buffer being supplied.
1455 *
1456 * key: A string buffer used to store the key name.
1457 * On return, the buffer contains a null-terminated C string
1458 * giving the key name. DNS-SD TXT keys are usually
1459 * 14 characters or less. To hold the maximum possible
1460 * key name, the buffer should be 256 bytes long.
1461 *
1462 * valueLen: On output, will be set to the size of the "value" data.
1463 *
1464 * value: On output, *value is set to point to location within TXT
1465 * Record bytes that holds the value data.
1466 *
1467 * return value: Returns kDNSServiceErr_NoError on success.
1468 * Returns kDNSServiceErr_NoMemory if keyBufLen is too short.
1469 * Returns kDNSServiceErr_Invalid if index is greater than
1470 * TXTRecordGetCount()-1.
1471 */
1472
1473 DNSServiceErrorType TXTRecordGetItemAtIndex
1474 (
1475 uint16_t txtLen,
1476 const void *txtRecord,
1477 uint16_t index,
1478 uint16_t keyBufLen,
1479 char *key,
1480 uint8_t *valueLen,
1481 const void **value
1482 );
1483
1484
1485 #ifdef __cplusplus
1486 }
1487 #endif
1488
1489 #endif /* _DNS_SD_H */