]> git.meshlink.io Git - catta/blob - compat-libdns_sd/dns_sd.h
fa18cce743257ce9dac49ffc5ec66af445fa04d1
[catta] / compat-libdns_sd / dns_sd.h
1 /*
2  * Copyright (c) 2003-2004, Apple Computer, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without 
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1.  Redistributions of source code must retain the above copyright notice, 
8  *     this list of conditions and the following disclaimer. 
9  * 2.  Redistributions in binary form must reproduce the above copyright notice, 
10  *     this list of conditions and the following disclaimer in the documentation 
11  *     and/or other materials provided with the distribution. 
12  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of its 
13  *     contributors may be used to endorse or promote products derived from this 
14  *     software without specific prior written permission. 
15  *
16  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
19  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #ifndef _DNS_SD_H
29 #define _DNS_SD_H
30
31 #ifdef  __cplusplus
32     extern "C" {
33 #endif
34
35 /* standard calling convention under Win32 is __stdcall */
36 #if defined(_WIN32)
37 #define DNSSD_API __stdcall
38 #else
39 #define DNSSD_API
40 #endif
41
42 #if defined(__FreeBSD_version) && (__FreeBSD_version < 500000)
43 /* stdint.h does not exist on FreeBSD 4.x; its types are defined in sys/types.h instead */
44 #include <sys/types.h>
45 #elif defined(__sun__)
46 #include <sys/types.h>
47 #elif defined(_WIN32)
48 #include <windows.h>
49 #define _UNUSED
50 #define bzero(a, b) memset(a, 0, b)
51 #ifndef _MSL_STDINT_H
52 typedef UINT8       uint8_t;
53 typedef INT8        int8_t;
54 typedef UINT16      uint16_t;
55 typedef INT16       int16_t;
56 typedef UINT32      uint32_t;
57 typedef INT32       int32_t;
58 #endif
59 #else
60 #include <stdint.h>
61 #endif
62
63 /* DNSServiceRef, DNSRecordRef
64  *
65  * Opaque internal data types.
66  * Note: client is responsible for serializing access to these structures if
67  * they are shared between concurrent threads.
68  */
69
70 typedef struct _DNSServiceRef_t *DNSServiceRef;
71 typedef struct _DNSRecordRef_t *DNSRecordRef;
72
73 /* General flags used in functions defined below */
74 enum
75     {
76     kDNSServiceFlagsMoreComing          = 0x1,
77     /* MoreComing indicates to a callback that at least one more result is
78      * queued and will be delivered following immediately after this one.
79      * Applications should not update their UI to display browse
80      * results when the MoreComing flag is set, because this would
81      * result in a great deal of ugly flickering on the screen.
82      * Applications should instead wait until until MoreComing is not set,
83      * and then update their UI.
84      * When MoreComing is not set, that doesn't mean there will be no more
85      * answers EVER, just that there are no more answers immediately
86      * available right now at this instant. If more answers become available
87      * in the future they will be delivered as usual.
88      */
89
90     kDNSServiceFlagsAdd                 = 0x2,
91     kDNSServiceFlagsDefault             = 0x4,
92     /* Flags for domain enumeration and browse/query reply callbacks.
93      * "Default" applies only to enumeration and is only valid in
94      * conjuction with "Add".  An enumeration callback with the "Add"
95      * flag NOT set indicates a "Remove", i.e. the domain is no longer
96      * valid.
97      */
98
99     kDNSServiceFlagsNoAutoRename        = 0x8,
100     /* Flag for specifying renaming behavior on name conflict when registering
101      * non-shared records. By default, name conflicts are automatically handled
102      * by renaming the service.  NoAutoRename overrides this behavior - with this
103      * flag set, name conflicts will result in a callback.  The NoAutorename flag
104      * is only valid if a name is explicitly specified when registering a service
105      * (i.e. the default name is not used.)
106      */
107
108     kDNSServiceFlagsShared              = 0x10,
109     kDNSServiceFlagsUnique              = 0x20,
110     /* Flag for registering individual records on a connected
111      * DNSServiceRef.  Shared indicates that there may be multiple records
112      * with this name on the network (e.g. PTR records).  Unique indicates that the
113      * record's name is to be unique on the network (e.g. SRV records).
114      */
115
116     kDNSServiceFlagsBrowseDomains       = 0x40,
117     kDNSServiceFlagsRegistrationDomains = 0x80,
118     /* Flags for specifying domain enumeration type in DNSServiceEnumerateDomains.
119      * BrowseDomains enumerates domains recommended for browsing, RegistrationDomains
120      * enumerates domains recommended for registration.
121      */
122
123     kDNSServiceFlagsLongLivedQuery      = 0x100,
124     /* Flag for creating a long-lived unicast query for the DNSServiceQueryRecord call. */
125
126     kDNSServiceFlagsAllowRemoteQuery    = 0x200,
127     /* Flag for creating a record for which we will answer remote queries
128      * (queries from hosts more than one hop away; hosts not directly connected to the local link).
129      */
130
131     kDNSServiceFlagsForceMulticast      = 0x400
132     /* Flag for signifying that a query or registration should be performed exclusively via multicast DNS,
133      * even for a name in a domain (e.g. foo.apple.com.) that would normally imply unicast DNS.
134      */
135     };
136
137 /*
138  * The values for DNS Classes and Types are listed in RFC 1035, and are available
139  * on every OS in its DNS header file. Unfortunately every OS does not have the
140  * same header file containing DNS Class and Type constants, and the names of
141  * the constants are not consistent. For example, BIND 8 uses "T_A",
142  * BIND 9 uses "ns_t_a", Windows uses "DNS_TYPE_A", etc.
143  * For this reason, these constants are also listed here, so that code using
144  * the DNS-SD programming APIs can use these constants, so that the same code
145  * can compile on all our supported platforms.
146  */
147
148 enum
149     {
150     kDNSServiceClass_IN       = 1       /* Internet */
151     };
152
153 enum
154     {
155     kDNSServiceType_A         = 1,      /* Host address. */
156     kDNSServiceType_NS        = 2,      /* Authoritative server. */
157     kDNSServiceType_MD        = 3,      /* Mail destination. */
158     kDNSServiceType_MF        = 4,      /* Mail forwarder. */
159     kDNSServiceType_CNAME     = 5,      /* Canonical name. */
160     kDNSServiceType_SOA       = 6,      /* Start of authority zone. */
161     kDNSServiceType_MB        = 7,      /* Mailbox domain name. */
162     kDNSServiceType_MG        = 8,      /* Mail group member. */
163     kDNSServiceType_MR        = 9,      /* Mail rename name. */
164     kDNSServiceType_NULL      = 10,     /* Null resource record. */
165     kDNSServiceType_WKS       = 11,     /* Well known service. */
166     kDNSServiceType_PTR       = 12,     /* Domain name pointer. */
167     kDNSServiceType_HINFO     = 13,     /* Host information. */
168     kDNSServiceType_MINFO     = 14,     /* Mailbox information. */
169     kDNSServiceType_MX        = 15,     /* Mail routing information. */
170     kDNSServiceType_TXT       = 16,     /* One or more text strings. */
171     kDNSServiceType_RP        = 17,     /* Responsible person. */
172     kDNSServiceType_AFSDB     = 18,     /* AFS cell database. */
173     kDNSServiceType_X25       = 19,     /* X_25 calling address. */
174     kDNSServiceType_ISDN      = 20,     /* ISDN calling address. */
175     kDNSServiceType_RT        = 21,     /* Router. */
176     kDNSServiceType_NSAP      = 22,     /* NSAP address. */
177     kDNSServiceType_NSAP_PTR  = 23,     /* Reverse NSAP lookup (deprecated). */
178     kDNSServiceType_SIG       = 24,     /* Security signature. */
179     kDNSServiceType_KEY       = 25,     /* Security key. */
180     kDNSServiceType_PX        = 26,     /* X.400 mail mapping. */
181     kDNSServiceType_GPOS      = 27,     /* Geographical position (withdrawn). */
182     kDNSServiceType_AAAA      = 28,     /* Ip6 Address. */
183     kDNSServiceType_LOC       = 29,     /* Location Information. */
184     kDNSServiceType_NXT       = 30,     /* Next domain (security). */
185     kDNSServiceType_EID       = 31,     /* Endpoint identifier. */
186     kDNSServiceType_NIMLOC    = 32,     /* Nimrod Locator. */
187     kDNSServiceType_SRV       = 33,     /* Server Selection. */
188     kDNSServiceType_ATMA      = 34,     /* ATM Address */
189     kDNSServiceType_NAPTR     = 35,     /* Naming Authority PoinTeR */
190     kDNSServiceType_KX        = 36,     /* Key Exchange */
191     kDNSServiceType_CERT      = 37,     /* Certification record */
192     kDNSServiceType_A6        = 38,     /* IPv6 address (deprecates AAAA) */
193     kDNSServiceType_DNAME     = 39,     /* Non-terminal DNAME (for IPv6) */
194     kDNSServiceType_SINK      = 40,     /* Kitchen sink (experimentatl) */
195     kDNSServiceType_OPT       = 41,     /* EDNS0 option (meta-RR) */
196     kDNSServiceType_TKEY      = 249,    /* Transaction key */
197     kDNSServiceType_TSIG      = 250,    /* Transaction signature. */
198     kDNSServiceType_IXFR      = 251,    /* Incremental zone transfer. */
199     kDNSServiceType_AXFR      = 252,    /* Transfer zone of authority. */
200     kDNSServiceType_MAILB     = 253,    /* Transfer mailbox records. */
201     kDNSServiceType_MAILA     = 254,    /* Transfer mail agent records. */
202     kDNSServiceType_ANY       = 255     /* Wildcard match. */
203     };
204
205
206 /* possible error code values */
207 enum
208     {
209     kDNSServiceErr_NoError             = 0,
210     kDNSServiceErr_Unknown             = -65537,       /* 0xFFFE FFFF */
211     kDNSServiceErr_NoSuchName          = -65538,
212     kDNSServiceErr_NoMemory            = -65539,
213     kDNSServiceErr_BadParam            = -65540,
214     kDNSServiceErr_BadReference        = -65541,
215     kDNSServiceErr_BadState            = -65542,
216     kDNSServiceErr_BadFlags            = -65543,
217     kDNSServiceErr_Unsupported         = -65544,
218     kDNSServiceErr_NotInitialized      = -65545,
219     kDNSServiceErr_AlreadyRegistered   = -65547,
220     kDNSServiceErr_NameConflict        = -65548,
221     kDNSServiceErr_Invalid             = -65549,
222     kDNSServiceErr_Firewall            = -65550,
223     kDNSServiceErr_Incompatible        = -65551,        /* client library incompatible with daemon */
224     kDNSServiceErr_BadInterfaceIndex   = -65552,
225     kDNSServiceErr_Refused             = -65553,
226     kDNSServiceErr_NoSuchRecord        = -65554,
227     kDNSServiceErr_NoAuth              = -65555,
228     kDNSServiceErr_NoSuchKey           = -65556,
229     kDNSServiceErr_NATTraversal        = -65557,
230     kDNSServiceErr_DoubleNAT           = -65558,
231     kDNSServiceErr_BadTime             = -65559
232     /* mDNS Error codes are in the range
233      * FFFE FF00 (-65792) to FFFE FFFF (-65537) */
234     };
235
236
237 /* Maximum length, in bytes, of a service name represented as a */
238 /* literal C-String, including the terminating NULL at the end. */
239
240 #define kDNSServiceMaxServiceName 64
241
242 /* Maximum length, in bytes, of a domain name represented as an *escaped* C-String */
243 /* including the final trailing dot, and the C-String terminating NULL at the end. */
244
245 #define kDNSServiceMaxDomainName 1005
246
247 /*
248  * Notes on DNS Name Escaping
249  *   -- or --
250  * "Why is kDNSServiceMaxDomainName 1005, when the maximum legal domain name is 255 bytes?"
251  *
252  * All strings used in DNS-SD are UTF-8 strings.
253  * With few exceptions, most are also escaped using standard DNS escaping rules:
254  *
255  *   '\\' represents a single literal '\' in the name
256  *   '\.' represents a single literal '.' in the name
257  *   '\ddd', where ddd is a three-digit decimal value from 000 to 255,
258  *        represents a single literal byte with that value.
259  *   A bare unescaped '.' is a label separator, marking a boundary between domain and subdomain.
260  *
261  * The exceptions, that do not use escaping, are the routines where the full
262  * DNS name of a resource is broken, for convenience, into servicename/regtype/domain.
263  * In these routines, the "servicename" is NOT escaped. It does not need to be, since
264  * it is, by definition, just a single literal string. Any characters in that string
265  * represent exactly what they are. The "regtype" portion is, technically speaking,
266  * escaped, but since legal regtypes are only allowed to contain letters, digits,
267  * and hyphens, there is nothing to escape, so the issue is moot. The "domain"
268  * portion is also escaped, though most domains in use on the public Internet
269  * today, like regtypes, don't contain any characters that need to be escaped.
270  * As DNS-SD becomes more popular, rich-text domains for service discovery will
271  * become common, so software should be written to cope with domains with escaping.
272  *
273  * The servicename may be up to 63 bytes of UTF-8 text (not counting the C-String
274  * terminating NULL at the end). The regtype is of the form _service._tcp or
275  * _service._udp, where the "service" part is 1-14 characters, which may be
276  * letters, digits, or hyphens. The domain part of the three-part name may be
277  * any legal domain, providing that the resulting servicename+regtype+domain
278  * name does not exceed 255 bytes.
279  *
280  * For most software, these issues are transparent. When browsing, the discovered
281  * servicenames should simply be displayed as-is. When resolving, the discovered
282  * servicename/regtype/domain are simply passed unchanged to DNSServiceResolve().
283  * When a DNSServiceResolve() succeeds, the returned fullname is already in
284  * the correct format to pass to standard system DNS APIs such as res_query().
285  * For converting from servicename/regtype/domain to a single properly-escaped
286  * full DNS name, the helper function DNSServiceConstructFullName() is provided.
287  *
288  * The following (highly contrived) example illustrates the escaping process.
289  * Suppose you have an service called "Dr. Smith\Dr. Johnson", of type "_ftp._tcp"
290  * in subdomain "4th. Floor" of subdomain "Building 2" of domain "apple.com."
291  * The full (escaped) DNS name of this service's SRV record would be:
292  * Dr\.\032Smith\\Dr\.\032Johnson._ftp._tcp.4th\.\032Floor.Building\0322.apple.com.
293  */
294
295
296 /* 
297  * Constants for specifying an interface index
298  *
299  * Specific interface indexes are identified via a 32-bit unsigned integer returned
300  * by the if_nametoindex() family of calls.
301  * 
302  * If the client passes 0 for interface index, that means "do the right thing",
303  * which (at present) means, "if the name is in an mDNS local multicast domain
304  * (e.g. 'local.', '254.169.in-addr.arpa.', '0.8.E.F.ip6.arpa.') then multicast
305  * on all applicable interfaces, otherwise send via unicast to the appropriate
306  * DNS server." Normally, most clients will use 0 for interface index to
307  * automatically get the default sensible behaviour.
308  * 
309  * If the client passes a positive interface index, then for multicast names that
310  * indicates to do the operation only on that one interface. For unicast names the
311  * interface index is ignored unless kDNSServiceFlagsForceMulticast is also set.
312  * 
313  * If the client passes kDNSServiceInterfaceIndexLocalOnly when registering
314  * a service, then that service will be found *only* by other local clients
315  * on the same machine that are browsing using kDNSServiceInterfaceIndexLocalOnly
316  * or kDNSServiceInterfaceIndexAny.
317  * If a client has a 'private' service, accessible only to other processes
318  * running on the same machine, this allows the client to advertise that service
319  * in a way such that it does not inadvertently appear in service lists on
320  * all the other machines on the network.
321  * 
322  * If the client passes kDNSServiceInterfaceIndexLocalOnly when browsing
323  * then it will find *all* records registered on that same local machine.
324  * Clients explicitly wishing to discover *only* LocalOnly services can
325  * accomplish this by inspecting the interfaceIndex of each service reported
326  * to their DNSServiceBrowseReply() callback function, and discarding those
327  * where the interface index is not kDNSServiceInterfaceIndexLocalOnly.
328  */
329
330 #define kDNSServiceInterfaceIndexAny 0
331 #define kDNSServiceInterfaceIndexLocalOnly ( (uint32_t) -1 )
332
333
334 typedef uint32_t DNSServiceFlags;
335 typedef int32_t DNSServiceErrorType;
336
337
338 /*********************************************************************************************
339  *
340  * Unix Domain Socket access, DNSServiceRef deallocation, and data processing functions
341  *
342  *********************************************************************************************/
343
344
345 /* DNSServiceRefSockFD()
346  *
347  * Access underlying Unix domain socket for an initialized DNSServiceRef.
348  * The DNS Service Discovery implmementation uses this socket to communicate between
349  * the client and the mDNSResponder daemon.  The application MUST NOT directly read from
350  * or write to this socket.  Access to the socket is provided so that it can be used as a
351  * run loop source, or in a select() loop: when data is available for reading on the socket,
352  * DNSServiceProcessResult() should be called, which will extract the daemon's reply from
353  * the socket, and pass it to the appropriate application callback.  By using a run loop or
354  * select(), results from the daemon can be processed asynchronously.  Without using these
355  * constructs, DNSServiceProcessResult() will block until the response from the daemon arrives.
356  * The client is responsible for ensuring that the data on the socket is processed in a timely
357  * fashion - the daemon may terminate its connection with a client that does not clear its
358  * socket buffer.
359  *
360  * sdRef:            A DNSServiceRef initialized by any of the DNSService calls.
361  *
362  * return value:    The DNSServiceRef's underlying socket descriptor, or -1 on
363  *                  error.
364  */
365
366 int DNSSD_API DNSServiceRefSockFD(DNSServiceRef sdRef);
367
368
369 /* DNSServiceProcessResult()
370  *
371  * Read a reply from the daemon, calling the appropriate application callback.  This call will
372  * block until the daemon's response is received.  Use DNSServiceRefSockFD() in
373  * conjunction with a run loop or select() to determine the presence of a response from the
374  * server before calling this function to process the reply without blocking.  Call this function
375  * at any point if it is acceptable to block until the daemon's response arrives.  Note that the
376  * client is responsible for ensuring that DNSServiceProcessResult() is called whenever there is
377  * a reply from the daemon - the daemon may terminate its connection with a client that does not
378  * process the daemon's responses.
379  *
380  * sdRef:           A DNSServiceRef initialized by any of the DNSService calls
381  *                  that take a callback parameter.
382  *
383  * return value:    Returns kDNSServiceErr_NoError on success, otherwise returns
384  *                  an error code indicating the specific failure that occurred.
385  */
386
387 DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdRef);
388
389
390 /* DNSServiceRefDeallocate()
391  *
392  * Terminate a connection with the daemon and free memory associated with the DNSServiceRef.
393  * Any services or records registered with this DNSServiceRef will be deregistered. Any
394  * Browse, Resolve, or Query operations called with this reference will be terminated.
395  *
396  * Note: If the reference's underlying socket is used in a run loop or select() call, it should
397  * be removed BEFORE DNSServiceRefDeallocate() is called, as this function closes the reference's
398  * socket.
399  *
400  * Note: If the reference was initialized with DNSServiceCreateConnection(), any DNSRecordRefs
401  * created via this reference will be invalidated by this call - the resource records are
402  * deregistered, and their DNSRecordRefs may not be used in subsequent functions.  Similarly,
403  * if the reference was initialized with DNSServiceRegister, and an extra resource record was
404  * added to the service via DNSServiceAddRecord(), the DNSRecordRef created by the Add() call
405  * is invalidated when this function is called - the DNSRecordRef may not be used in subsequent
406  * functions.
407  *
408  * Note: This call is to be used only with the DNSServiceRef defined by this API.  It is
409  * not compatible with dns_service_discovery_ref objects defined in the legacy Mach-based
410  * DNSServiceDiscovery.h API.
411  *
412  * sdRef:           A DNSServiceRef initialized by any of the DNSService calls.
413  *
414  */
415
416 void DNSSD_API DNSServiceRefDeallocate(DNSServiceRef sdRef);
417
418
419 /*********************************************************************************************
420  *
421  * Domain Enumeration
422  *
423  *********************************************************************************************/
424
425 /* DNSServiceEnumerateDomains()
426  *
427  * Asynchronously enumerate domains available for browsing and registration.
428  *
429  * The enumeration MUST be cancelled via DNSServiceRefDeallocate() when no more domains
430  * are to be found.
431  *
432  * Note that the names returned are (like all of DNS-SD) UTF-8 strings,
433  * and are escaped using standard DNS escaping rules.
434  * (See "Notes on DNS Name Escaping" earlier in this file for more details.)
435  * A graphical browser displaying a hierarchical tree-structured view should cut
436  * the names at the bare dots to yield individual labels, then de-escape each
437  * label according to the escaping rules, and then display the resulting UTF-8 text.
438  *
439  * DNSServiceDomainEnumReply Callback Parameters:
440  *
441  * sdRef:           The DNSServiceRef initialized by DNSServiceEnumerateDomains().
442  *
443  * flags:           Possible values are:
444  *                  kDNSServiceFlagsMoreComing
445  *                  kDNSServiceFlagsAdd
446  *                  kDNSServiceFlagsDefault
447  *
448  * interfaceIndex:  Specifies the interface on which the domain exists.  (The index for a given
449  *                  interface is determined via the if_nametoindex() family of calls.)
450  *
451  * errorCode:       Will be kDNSServiceErr_NoError (0) on success, otherwise indicates
452  *                  the failure that occurred (other parameters are undefined if errorCode is nonzero).
453  *
454  * replyDomain:     The name of the domain.
455  *
456  * context:         The context pointer passed to DNSServiceEnumerateDomains.
457  *
458  */
459
460 typedef void (DNSSD_API *DNSServiceDomainEnumReply)
461     (
462     DNSServiceRef                       sdRef,
463     DNSServiceFlags                     flags,
464     uint32_t                            interfaceIndex,
465     DNSServiceErrorType                 errorCode,
466     const char                          *replyDomain,
467     void                                *context
468     );
469
470
471 /* DNSServiceEnumerateDomains() Parameters:
472  *
473  *
474  * sdRef:           A pointer to an uninitialized DNSServiceRef. If the call succeeds 
475  *                  then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError,
476  *                  and the enumeration operation will run indefinitely until the client
477  *                  terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate().
478  *
479  * flags:           Possible values are:
480  *                  kDNSServiceFlagsBrowseDomains to enumerate domains recommended for browsing.
481  *                  kDNSServiceFlagsRegistrationDomains to enumerate domains recommended
482  *                  for registration.
483  *
484  * interfaceIndex:  If non-zero, specifies the interface on which to look for domains.
485  *                  (the index for a given interface is determined via the if_nametoindex()
486  *                  family of calls.)  Most applications will pass 0 to enumerate domains on
487  *                  all interfaces. See "Constants for specifying an interface index" for more details.
488  *
489  * callBack:        The function to be called when a domain is found or the call asynchronously
490  *                  fails.
491  *
492  * context:         An application context pointer which is passed to the callback function
493  *                  (may be NULL).
494  *
495  * return value:    Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous
496  *                  errors are delivered to the callback), otherwise returns an error code indicating
497  *                  the error that occurred (the callback is not invoked and the DNSServiceRef
498  *                  is not initialized.)
499  */
500
501 DNSServiceErrorType DNSSD_API DNSServiceEnumerateDomains
502     (
503     DNSServiceRef                       *sdRef,
504     DNSServiceFlags                     flags,
505     uint32_t                            interfaceIndex,
506     DNSServiceDomainEnumReply           callBack,
507     void                                *context  /* may be NULL */
508     );
509
510
511 /*********************************************************************************************
512  *
513  *  Service Registration
514  *
515  *********************************************************************************************/
516
517 /* Register a service that is discovered via Browse() and Resolve() calls.
518  *
519  *
520  * DNSServiceRegisterReply() Callback Parameters:
521  *
522  * sdRef:           The DNSServiceRef initialized by DNSServiceRegister().
523  *
524  * flags:           Currently unused, reserved for future use.
525  *
526  * errorCode:       Will be kDNSServiceErr_NoError on success, otherwise will
527  *                  indicate the failure that occurred (including name conflicts,
528  *                  if the kDNSServiceFlagsNoAutoRename flag was used when registering.)
529  *                  Other parameters are undefined if errorCode is nonzero.
530  *
531  * name:            The service name registered (if the application did not specify a name in
532  *                  DNSServiceRegister(), this indicates what name was automatically chosen).
533  *
534  * regtype:         The type of service registered, as it was passed to the callout.
535  *
536  * domain:          The domain on which the service was registered (if the application did not
537  *                  specify a domain in DNSServiceRegister(), this indicates the default domain
538  *                  on which the service was registered).
539  *
540  * context:         The context pointer that was passed to the callout.
541  *
542  */
543
544 typedef void (DNSSD_API *DNSServiceRegisterReply)
545     (
546     DNSServiceRef                       sdRef,
547     DNSServiceFlags                     flags,
548     DNSServiceErrorType                 errorCode,
549     const char                          *name,
550     const char                          *regtype,
551     const char                          *domain,
552     void                                *context
553     );
554
555
556 /* DNSServiceRegister()  Parameters:
557  *
558  * sdRef:           A pointer to an uninitialized DNSServiceRef. If the call succeeds 
559  *                  then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError,
560  *                  and the registration will remain active indefinitely until the client
561  *                  terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate().
562  *
563  * interfaceIndex:  If non-zero, specifies the interface on which to register the service
564  *                  (the index for a given interface is determined via the if_nametoindex()
565  *                  family of calls.)  Most applications will pass 0 to register on all
566  *                  available interfaces. See "Constants for specifying an interface index" for more details.
567  *
568  * flags:           Indicates the renaming behavior on name conflict (most applications
569  *                  will pass 0).  See flag definitions above for details.
570  *
571  * name:            If non-NULL, specifies the service name to be registered.
572  *                  Most applications will not specify a name, in which case the computer
573  *                  name is used (this name is communicated to the client via the callback).
574  *                  If a name is specified, it must be 1-63 bytes of UTF-8 text.
575  *                  If the name is longer than 63 bytes it will be automatically truncated
576  *                  to a legal length, unless the NoAutoRename flag is set,
577  *                  in which case kDNSServiceErr_BadParam will be returned.
578  *
579  * regtype:         The service type followed by the protocol, separated by a dot
580  *                  (e.g. "_ftp._tcp"). The service type must be an underscore, followed
581  *                  by 1-14 characters, which may be letters, digits, or hyphens.
582  *                  The transport protocol must be "_tcp" or "_udp". New service types
583  *                  should be registered at <http://www.dns-sd.org/ServiceTypes.html>.
584  *
585  * domain:          If non-NULL, specifies the domain on which to advertise the service.
586  *                  Most applications will not specify a domain, instead automatically
587  *                  registering in the default domain(s).
588  *
589  * host:            If non-NULL, specifies the SRV target host name.  Most applications
590  *                  will not specify a host, instead automatically using the machine's
591  *                  default host name(s).  Note that specifying a non-NULL host does NOT
592  *                  create an address record for that host - the application is responsible
593  *                  for ensuring that the appropriate address record exists, or creating it
594  *                  via DNSServiceRegisterRecord().
595  *
596  * port:            The port, in network byte order, on which the service accepts connections.
597  *                  Pass 0 for a "placeholder" service (i.e. a service that will not be discovered
598  *                  by browsing, but will cause a name conflict if another client tries to
599  *                  register that same name).  Most clients will not use placeholder services.
600  *
601  * txtLen:          The length of the txtRecord, in bytes.  Must be zero if the txtRecord is NULL.
602  *
603  * txtRecord:       The TXT record rdata. A non-NULL txtRecord MUST be a properly formatted DNS
604  *                  TXT record, i.e. <length byte> <data> <length byte> <data> ...
605  *                  Passing NULL for the txtRecord is allowed as a synonym for txtLen=1, txtRecord="",
606  *                  i.e. it creates a TXT record of length one containing a single empty string.
607  *                  RFC 1035 doesn't allow a TXT record to contain *zero* strings, so a single empty
608  *                  string is the smallest legal DNS TXT record.
609  *
610  * callBack:        The function to be called when the registration completes or asynchronously
611  *                  fails.  The client MAY pass NULL for the callback -  The client will NOT be notified
612  *                  of the default values picked on its behalf, and the client will NOT be notified of any
613  *                  asynchronous errors (e.g. out of memory errors, etc.) that may prevent the registration
614  *                  of the service.  The client may NOT pass the NoAutoRename flag if the callback is NULL.
615  *                  The client may still deregister the service at any time via DNSServiceRefDeallocate().
616  *
617  * context:         An application context pointer which is passed to the callback function
618  *                  (may be NULL).
619  *
620  * return value:    Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous
621  *                  errors are delivered to the callback), otherwise returns an error code indicating
622  *                  the error that occurred (the callback is never invoked and the DNSServiceRef
623  *                  is not initialized.)
624  *
625  */
626
627 DNSServiceErrorType DNSSD_API DNSServiceRegister
628     (
629     DNSServiceRef                       *sdRef,
630     DNSServiceFlags                     flags,
631     uint32_t                            interfaceIndex,
632     const char                          *name,         /* may be NULL */
633     const char                          *regtype,
634     const char                          *domain,       /* may be NULL */
635     const char                          *host,         /* may be NULL */
636     uint16_t                            port,
637     uint16_t                            txtLen,
638     const void                          *txtRecord,    /* may be NULL */
639     DNSServiceRegisterReply             callBack,      /* may be NULL */
640     void                                *context       /* may be NULL */
641     );
642
643
644 /* DNSServiceAddRecord()
645  *
646  * Add a record to a registered service.  The name of the record will be the same as the
647  * registered service's name.
648  * The record can later be updated or deregistered by passing the RecordRef initialized
649  * by this function to DNSServiceUpdateRecord() or DNSServiceRemoveRecord().
650  *
651  *
652  * Parameters;
653  *
654  * sdRef:           A DNSServiceRef initialized by DNSServiceRegister().
655  *
656  * RecordRef:       A pointer to an uninitialized DNSRecordRef.  Upon succesfull completion of this
657  *                  call, this ref may be passed to DNSServiceUpdateRecord() or DNSServiceRemoveRecord().
658  *                  If the above DNSServiceRef is passed to DNSServiceRefDeallocate(), RecordRef is also
659  *                  invalidated and may not be used further.
660  *
661  * flags:           Currently ignored, reserved for future use.
662  *
663  * rrtype:          The type of the record (e.g. kDNSServiceType_TXT, kDNSServiceType_SRV, etc)
664  *
665  * rdlen:           The length, in bytes, of the rdata.
666  *
667  * rdata:           The raw rdata to be contained in the added resource record.
668  *
669  * ttl:             The time to live of the resource record, in seconds.  Pass 0 to use a default value.
670  *
671  * return value:    Returns kDNSServiceErr_NoError on success, otherwise returns an
672  *                  error code indicating the error that occurred (the RecordRef is not initialized).
673  */
674
675 DNSServiceErrorType DNSSD_API DNSServiceAddRecord
676     (
677     DNSServiceRef                       sdRef,
678     DNSRecordRef                        *RecordRef,
679     DNSServiceFlags                     flags,
680     uint16_t                            rrtype,
681     uint16_t                            rdlen,
682     const void                          *rdata,
683     uint32_t                            ttl
684     );
685
686
687 /* DNSServiceUpdateRecord
688  *
689  * Update a registered resource record.  The record must either be:
690  *   - The primary txt record of a service registered via DNSServiceRegister()
691  *   - A record added to a registered service via DNSServiceAddRecord()
692  *   - An individual record registered by DNSServiceRegisterRecord()
693  *
694  *
695  * Parameters:
696  *
697  * sdRef:           A DNSServiceRef that was initialized by DNSServiceRegister()
698  *                  or DNSServiceCreateConnection().
699  *
700  * RecordRef:       A DNSRecordRef initialized by DNSServiceAddRecord, or NULL to update the
701  *                  service's primary txt record.
702  *
703  * flags:           Currently ignored, reserved for future use.
704  *
705  * rdlen:           The length, in bytes, of the new rdata.
706  *
707  * rdata:           The new rdata to be contained in the updated resource record.
708  *
709  * ttl:             The time to live of the updated resource record, in seconds.
710  *
711  * return value:    Returns kDNSServiceErr_NoError on success, otherwise returns an
712  *                  error code indicating the error that occurred.
713  */
714
715 DNSServiceErrorType DNSSD_API DNSServiceUpdateRecord
716     (
717     DNSServiceRef                       sdRef,
718     DNSRecordRef                        RecordRef,     /* may be NULL */
719     DNSServiceFlags                     flags,
720     uint16_t                            rdlen,
721     const void                          *rdata,
722     uint32_t                            ttl
723     );
724
725
726 /* DNSServiceRemoveRecord
727  *
728  * Remove a record previously added to a service record set via DNSServiceAddRecord(), or deregister
729  * an record registered individually via DNSServiceRegisterRecord().
730  *
731  * Parameters:
732  *
733  * sdRef:           A DNSServiceRef initialized by DNSServiceRegister() (if the
734  *                  record being removed was registered via DNSServiceAddRecord()) or by
735  *                  DNSServiceCreateConnection() (if the record being removed was registered via
736  *                  DNSServiceRegisterRecord()).
737  *
738  * recordRef:       A DNSRecordRef initialized by a successful call to DNSServiceAddRecord()
739  *                  or DNSServiceRegisterRecord().
740  *
741  * flags:           Currently ignored, reserved for future use.
742  *
743  * return value:    Returns kDNSServiceErr_NoError on success, otherwise returns an
744  *                  error code indicating the error that occurred.
745  */
746
747 DNSServiceErrorType DNSSD_API DNSServiceRemoveRecord
748     (
749     DNSServiceRef                 sdRef,
750     DNSRecordRef                  RecordRef,
751     DNSServiceFlags               flags
752     );
753
754
755 /*********************************************************************************************
756  *
757  *  Service Discovery
758  *
759  *********************************************************************************************/
760
761 /* Browse for instances of a service.
762  *
763  *
764  * DNSServiceBrowseReply() Parameters:
765  *
766  * sdRef:           The DNSServiceRef initialized by DNSServiceBrowse().
767  *
768  * flags:           Possible values are kDNSServiceFlagsMoreComing and kDNSServiceFlagsAdd.
769  *                  See flag definitions for details.
770  *
771  * interfaceIndex:  The interface on which the service is advertised.  This index should
772  *                  be passed to DNSServiceResolve() when resolving the service.
773  *
774  * errorCode:       Will be kDNSServiceErr_NoError (0) on success, otherwise will
775  *                  indicate the failure that occurred.  Other parameters are undefined if
776  *                  the errorCode is nonzero.
777  *
778  * serviceName:     The discovered service name. This name should be displayed to the user,
779  *                  and stored for subsequent use in the DNSServiceResolve() call.
780  *
781  * regtype:         The service type, which is usually (but not always) the same as was passed
782  *                  to DNSServiceBrowse(). One case where the discovered service type may
783  *                  not be the same as the requested service type is when using subtypes:
784  *                  The client may want to browse for only those ftp servers that allow
785  *                  anonymous connections. The client will pass the string "_ftp._tcp,_anon"
786  *                  to DNSServiceBrowse(), but the type of the service that's discovered
787  *                  is simply "_ftp._tcp". The regtype for each discovered service instance
788  *                  should be stored along with the name, so that it can be passed to
789  *                  DNSServiceResolve() when the service is later resolved.
790  *
791  * domain:          The domain of the discovered service instance. This may or may not be the
792  *                  same as the domain that was passed to DNSServiceBrowse(). The domain for each
793  *                  discovered service instance should be stored along with the name, so that
794  *                  it can be passed to DNSServiceResolve() when the service is later resolved.
795  *
796  * context:         The context pointer that was passed to the callout.
797  *
798  */
799
800 typedef void (DNSSD_API *DNSServiceBrowseReply)
801     (
802     DNSServiceRef                       sdRef,
803     DNSServiceFlags                     flags,
804     uint32_t                            interfaceIndex,
805     DNSServiceErrorType                 errorCode,
806     const char                          *serviceName,
807     const char                          *regtype,
808     const char                          *replyDomain,
809     void                                *context
810     );
811
812
813 /* DNSServiceBrowse() Parameters:
814  *
815  * sdRef:           A pointer to an uninitialized DNSServiceRef. If the call succeeds 
816  *                  then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError,
817  *                  and the browse operation will run indefinitely until the client
818  *                  terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate().
819  *
820  * flags:           Currently ignored, reserved for future use.
821  *
822  * interfaceIndex:  If non-zero, specifies the interface on which to browse for services
823  *                  (the index for a given interface is determined via the if_nametoindex()
824  *                  family of calls.)  Most applications will pass 0 to browse on all available
825  *                  interfaces. See "Constants for specifying an interface index" for more details.
826  *
827  * regtype:         The service type being browsed for followed by the protocol, separated by a
828  *                  dot (e.g. "_ftp._tcp").  The transport protocol must be "_tcp" or "_udp".
829  *
830  * domain:          If non-NULL, specifies the domain on which to browse for services.
831  *                  Most applications will not specify a domain, instead browsing on the
832  *                  default domain(s).
833  *
834  * callBack:        The function to be called when an instance of the service being browsed for
835  *                  is found, or if the call asynchronously fails.
836  *
837  * context:         An application context pointer which is passed to the callback function
838  *                  (may be NULL).
839  *
840  * return value:    Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous
841  *                  errors are delivered to the callback), otherwise returns an error code indicating
842  *                  the error that occurred (the callback is not invoked and the DNSServiceRef
843  *                  is not initialized.)
844  */
845
846 DNSServiceErrorType DNSSD_API DNSServiceBrowse
847     (
848     DNSServiceRef                       *sdRef,
849     DNSServiceFlags                     flags,
850     uint32_t                            interfaceIndex,
851     const char                          *regtype,
852     const char                          *domain,    /* may be NULL */
853     DNSServiceBrowseReply               callBack,
854     void                                *context    /* may be NULL */
855     );
856
857
858 /* DNSServiceResolve()
859  *
860  * Resolve a service name discovered via DNSServiceBrowse() to a target host name, port number, and
861  * txt record.
862  *
863  * Note: Applications should NOT use DNSServiceResolve() solely for txt record monitoring - use
864  * DNSServiceQueryRecord() instead, as it is more efficient for this task.
865  *
866  * Note: When the desired results have been returned, the client MUST terminate the resolve by calling
867  * DNSServiceRefDeallocate().
868  *
869  * Note: DNSServiceResolve() behaves correctly for typical services that have a single SRV record
870  * and a single TXT record. To resolve non-standard services with multiple SRV or TXT records,
871  * DNSServiceQueryRecord() should be used.
872  *
873  * DNSServiceResolveReply Callback Parameters:
874  *
875  * sdRef:           The DNSServiceRef initialized by DNSServiceResolve().
876  *
877  * flags:           Currently unused, reserved for future use.
878  *
879  * interfaceIndex:  The interface on which the service was resolved.
880  *
881  * errorCode:       Will be kDNSServiceErr_NoError (0) on success, otherwise will
882  *                  indicate the failure that occurred.  Other parameters are undefined if
883  *                  the errorCode is nonzero.
884  *
885  * fullname:        The full service domain name, in the form <servicename>.<protocol>.<domain>.
886  *                  (This name is escaped following standard DNS rules, making it suitable for
887  *                  passing to standard system DNS APIs such as res_query(), or to the
888  *                  special-purpose functions included in this API that take fullname parameters.
889  *                  See "Notes on DNS Name Escaping" earlier in this file for more details.)
890  *
891  * hosttarget:      The target hostname of the machine providing the service.  This name can
892  *                  be passed to functions like gethostbyname() to identify the host's IP address.
893  *
894  * port:            The port, in network byte order, on which connections are accepted for this service.
895  *
896  * txtLen:          The length of the txt record, in bytes.
897  *
898  * txtRecord:       The service's primary txt record, in standard txt record format.
899  *
900
901  * context:         The context pointer that was passed to the callout.
902  *
903  */
904
905 typedef void (DNSSD_API *DNSServiceResolveReply)
906     (
907     DNSServiceRef                       sdRef,
908     DNSServiceFlags                     flags,
909     uint32_t                            interfaceIndex,
910     DNSServiceErrorType                 errorCode,
911     const char                          *fullname,
912     const char                          *hosttarget,
913     uint16_t                            port,
914     uint16_t                            txtLen,
915     const char                          *txtRecord,
916     void                                *context
917     );
918
919
920 /* DNSServiceResolve() Parameters
921  *
922  * sdRef:           A pointer to an uninitialized DNSServiceRef. If the call succeeds 
923  *                  then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError,
924  *                  and the resolve operation will run indefinitely until the client
925  *                  terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate().
926  *
927  * flags:           Currently ignored, reserved for future use.
928  *
929  * interfaceIndex:  The interface on which to resolve the service. If this resolve call is
930  *                  as a result of a currently active DNSServiceBrowse() operation, then the
931  *                  interfaceIndex should be the index reported in the DNSServiceBrowseReply
932  *                  callback. If this resolve call is using information previously saved
933  *                  (e.g. in a preference file) for later use, then use interfaceIndex 0, because
934  *                  the desired service may now be reachable via a different physical interface.
935  *                  See "Constants for specifying an interface index" for more details.
936  *
937  * name:            The name of the service instance to be resolved, as reported to the
938  *                  DNSServiceBrowseReply() callback.
939  *
940  * regtype:         The type of the service instance to be resolved, as reported to the
941  *                  DNSServiceBrowseReply() callback.
942  *
943  * domain:          The domain of the service instance to be resolved, as reported to the
944  *                  DNSServiceBrowseReply() callback.
945  *
946  * callBack:        The function to be called when a result is found, or if the call
947  *                  asynchronously fails.
948  *
949  * context:         An application context pointer which is passed to the callback function
950  *                  (may be NULL).
951  *
952  * return value:    Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous
953  *                  errors are delivered to the callback), otherwise returns an error code indicating
954  *                  the error that occurred (the callback is never invoked and the DNSServiceRef
955  *                  is not initialized.)
956  */
957
958 DNSServiceErrorType DNSSD_API DNSServiceResolve
959     (
960     DNSServiceRef                       *sdRef,
961     DNSServiceFlags                     flags,
962     uint32_t                            interfaceIndex,
963     const char                          *name,
964     const char                          *regtype,
965     const char                          *domain,
966     DNSServiceResolveReply              callBack,
967     void                                *context  /* may be NULL */
968     );
969
970
971 /*********************************************************************************************
972  *
973  *  Special Purpose Calls (most applications will not use these)
974  *
975  *********************************************************************************************/
976
977 /* DNSServiceCreateConnection()
978  *
979  * Create a connection to the daemon allowing efficient registration of
980  * multiple individual records.
981  *
982  *
983  * Parameters:
984  *
985  * sdRef:           A pointer to an uninitialized DNSServiceRef.  Deallocating
986  *                  the reference (via DNSServiceRefDeallocate()) severs the
987  *                  connection and deregisters all records registered on this connection.
988  *
989  * return value:    Returns kDNSServiceErr_NoError on success, otherwise returns
990  *                  an error code indicating the specific failure that occurred (in which
991  *                  case the DNSServiceRef is not initialized).
992  */
993
994 DNSServiceErrorType DNSSD_API DNSServiceCreateConnection(DNSServiceRef *sdRef);
995
996
997 /* DNSServiceRegisterRecord
998  *
999  * Register an individual resource record on a connected DNSServiceRef.
1000  *
1001  * Note that name conflicts occurring for records registered via this call must be handled
1002  * by the client in the callback.
1003  *
1004  *
1005  * DNSServiceRegisterRecordReply() parameters:
1006  *
1007  * sdRef:           The connected DNSServiceRef initialized by
1008  *                  DNSServiceDiscoveryConnect().
1009  *
1010  * RecordRef:       The DNSRecordRef initialized by DNSServiceRegisterRecord().  If the above
1011  *                  DNSServiceRef is passed to DNSServiceRefDeallocate(), this DNSRecordRef is
1012  *                  invalidated, and may not be used further.
1013  *
1014  * flags:           Currently unused, reserved for future use.
1015  *
1016  * errorCode:       Will be kDNSServiceErr_NoError on success, otherwise will
1017  *                  indicate the failure that occurred (including name conflicts.)
1018  *                  Other parameters are undefined if errorCode is nonzero.
1019  *
1020  * context:         The context pointer that was passed to the callout.
1021  *
1022  */
1023
1024  typedef void (DNSSD_API *DNSServiceRegisterRecordReply)
1025     (
1026     DNSServiceRef                       sdRef,
1027     DNSRecordRef                        RecordRef,
1028     DNSServiceFlags                     flags,
1029     DNSServiceErrorType                 errorCode,
1030     void                                *context
1031     );
1032
1033
1034 /* DNSServiceRegisterRecord() Parameters:
1035  *
1036  * sdRef:           A DNSServiceRef initialized by DNSServiceCreateConnection().
1037  *
1038  * RecordRef:       A pointer to an uninitialized DNSRecordRef.  Upon succesfull completion of this
1039  *                  call, this ref may be passed to DNSServiceUpdateRecord() or DNSServiceRemoveRecord().
1040  *                  (To deregister ALL records registered on a single connected DNSServiceRef
1041  *                  and deallocate each of their corresponding DNSServiceRecordRefs, call
1042  *                  DNSServiceRefDealloocate()).
1043  *
1044  * flags:           Possible values are kDNSServiceFlagsShared or kDNSServiceFlagsUnique
1045  *                  (see flag type definitions for details).
1046  *
1047  * interfaceIndex:  If non-zero, specifies the interface on which to register the record
1048  *                  (the index for a given interface is determined via the if_nametoindex()
1049  *                  family of calls.)  Passing 0 causes the record to be registered on all interfaces.
1050  *                  See "Constants for specifying an interface index" for more details.
1051  *
1052  * fullname:        The full domain name of the resource record.
1053  *
1054  * rrtype:          The numerical type of the resource record (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc)
1055  *
1056  * rrclass:         The class of the resource record (usually kDNSServiceClass_IN)
1057  *
1058  * rdlen:           Length, in bytes, of the rdata.
1059  *
1060  * rdata:           A pointer to the raw rdata, as it is to appear in the DNS record.
1061  *
1062  * ttl:             The time to live of the resource record, in seconds.  Pass 0 to use a default value.
1063  *
1064  * callBack:        The function to be called when a result is found, or if the call
1065  *                  asynchronously fails (e.g. because of a name conflict.)
1066  *
1067  * context:         An application context pointer which is passed to the callback function
1068  *                  (may be NULL).
1069  *
1070  * return value:    Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous
1071  *                  errors are delivered to the callback), otherwise returns an error code indicating
1072  *                  the error that occurred (the callback is never invoked and the DNSRecordRef is
1073  *                  not initialized.)
1074  */
1075
1076 DNSServiceErrorType DNSSD_API DNSServiceRegisterRecord
1077     (
1078     DNSServiceRef                       sdRef,
1079     DNSRecordRef                        *RecordRef,
1080     DNSServiceFlags                     flags,
1081     uint32_t                            interfaceIndex,
1082     const char                          *fullname,
1083     uint16_t                            rrtype,
1084     uint16_t                            rrclass,
1085     uint16_t                            rdlen,
1086     const void                          *rdata,
1087     uint32_t                            ttl,
1088     DNSServiceRegisterRecordReply       callBack,
1089     void                                *context    /* may be NULL */
1090     );
1091
1092
1093 /* DNSServiceQueryRecord
1094  *
1095  * Query for an arbitrary DNS record.
1096  *
1097  *
1098  * DNSServiceQueryRecordReply() Callback Parameters:
1099  *
1100  * sdRef:           The DNSServiceRef initialized by DNSServiceQueryRecord().
1101  *
1102  * flags:           Possible values are kDNSServiceFlagsMoreComing and
1103  *                  kDNSServiceFlagsAdd.  The Add flag is NOT set for PTR records
1104  *                  with a ttl of 0, i.e. "Remove" events.
1105  *
1106  * interfaceIndex:  The interface on which the query was resolved (the index for a given
1107  *                  interface is determined via the if_nametoindex() family of calls).
1108  *                  See "Constants for specifying an interface index" for more details.
1109  *
1110  * errorCode:       Will be kDNSServiceErr_NoError on success, otherwise will
1111  *                  indicate the failure that occurred.  Other parameters are undefined if
1112  *                  errorCode is nonzero.
1113  *
1114  * fullname:        The resource record's full domain name.
1115  *
1116  * rrtype:          The resource record's type (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc)
1117  *
1118  * rrclass:         The class of the resource record (usually kDNSServiceClass_IN).
1119  *
1120  * rdlen:           The length, in bytes, of the resource record rdata.
1121  *
1122  * rdata:           The raw rdata of the resource record.
1123  *
1124  * ttl:             The resource record's time to live, in seconds.
1125  *
1126  * context:         The context pointer that was passed to the callout.
1127  *
1128  */
1129
1130 typedef void (DNSSD_API *DNSServiceQueryRecordReply)
1131     (
1132     DNSServiceRef                       DNSServiceRef,
1133     DNSServiceFlags                     flags,
1134     uint32_t                            interfaceIndex,
1135     DNSServiceErrorType                 errorCode,
1136     const char                          *fullname,
1137     uint16_t                            rrtype,
1138     uint16_t                            rrclass,
1139     uint16_t                            rdlen,
1140     const void                          *rdata,
1141     uint32_t                            ttl,
1142     void                                *context
1143     );
1144
1145
1146 /* DNSServiceQueryRecord() Parameters:
1147  *
1148  * sdRef:           A pointer to an uninitialized DNSServiceRef. If the call succeeds 
1149  *                  then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError,
1150  *                  and the query operation will run indefinitely until the client
1151  *                  terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate().
1152  *
1153  * flags:           Pass kDNSServiceFlagsLongLivedQuery to create a "long-lived" unicast
1154  *                  query in a non-local domain.  Without setting this flag, unicast queries
1155  *                  will be one-shot - that is, only answers available at the time of the call
1156  *                  will be returned.  By setting this flag, answers (including Add and Remove
1157  *                  events) that become available after the initial call is made will generate
1158  *                  callbacks.  This flag has no effect on link-local multicast queries.
1159  *
1160  * interfaceIndex:  If non-zero, specifies the interface on which to issue the query
1161  *                  (the index for a given interface is determined via the if_nametoindex()
1162  *                  family of calls.)  Passing 0 causes the name to be queried for on all
1163  *                  interfaces. See "Constants for specifying an interface index" for more details.
1164  *
1165  * fullname:        The full domain name of the resource record to be queried for.
1166  *
1167  * rrtype:          The numerical type of the resource record to be queried for
1168  *                  (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc)
1169  *
1170  * rrclass:         The class of the resource record (usually kDNSServiceClass_IN).
1171  *
1172  * callBack:        The function to be called when a result is found, or if the call
1173  *                  asynchronously fails.
1174  *
1175  * context:         An application context pointer which is passed to the callback function
1176  *                  (may be NULL).
1177  *
1178  * return value:    Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous
1179  *                  errors are delivered to the callback), otherwise returns an error code indicating
1180  *                  the error that occurred (the callback is never invoked and the DNSServiceRef
1181  *                  is not initialized.)
1182  */
1183
1184 DNSServiceErrorType DNSSD_API DNSServiceQueryRecord
1185     (
1186     DNSServiceRef                       *sdRef,
1187     DNSServiceFlags                     flags,
1188     uint32_t                            interfaceIndex,
1189     const char                          *fullname,
1190     uint16_t                            rrtype,
1191     uint16_t                            rrclass,
1192     DNSServiceQueryRecordReply          callBack,
1193     void                                *context  /* may be NULL */
1194     );
1195
1196
1197 /* DNSServiceReconfirmRecord
1198  *
1199  * Instruct the daemon to verify the validity of a resource record that appears to
1200  * be out of date (e.g. because tcp connection to a service's target failed.)
1201  * Causes the record to be flushed from the daemon's cache (as well as all other
1202  * daemons' caches on the network) if the record is determined to be invalid.
1203  *
1204  * Parameters:
1205  *
1206  * flags:           Currently unused, reserved for future use.
1207  *
1208  * interfaceIndex:  If non-zero, specifies the interface of the record in question.
1209  *                  Passing 0 causes all instances of this record to be reconfirmed.
1210  *
1211  * fullname:        The resource record's full domain name.
1212  *
1213  * rrtype:          The resource record's type (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc)
1214  *
1215  * rrclass:         The class of the resource record (usually kDNSServiceClass_IN).
1216  *
1217  * rdlen:           The length, in bytes, of the resource record rdata.
1218  *
1219  * rdata:           The raw rdata of the resource record.
1220  *
1221  */
1222
1223 void DNSSD_API DNSServiceReconfirmRecord
1224     (
1225     DNSServiceFlags                    flags,
1226     uint32_t                           interfaceIndex,
1227     const char                         *fullname,
1228     uint16_t                           rrtype,
1229     uint16_t                           rrclass,
1230     uint16_t                           rdlen,
1231     const void                         *rdata
1232     );
1233
1234
1235 /*********************************************************************************************
1236  *
1237  *  General Utility Functions
1238  *
1239  *********************************************************************************************/
1240
1241 /* DNSServiceConstructFullName()
1242  *
1243  * Concatenate a three-part domain name (as returned by the above callbacks) into a
1244  * properly-escaped full domain name. Note that callbacks in the above functions ALREADY ESCAPE
1245  * strings where necessary.
1246  *
1247  * Parameters:
1248  *
1249  * fullName:        A pointer to a buffer that where the resulting full domain name is to be written.
1250  *                  The buffer must be kDNSServiceMaxDomainName (1005) bytes in length to
1251  *                  accommodate the longest legal domain name without buffer overrun.
1252  *
1253  * service:         The service name - any dots or backslashes must NOT be escaped.
1254  *                  May be NULL (to construct a PTR record name, e.g.
1255  *                  "_ftp._tcp.apple.com.").
1256  *
1257  * regtype:         The service type followed by the protocol, separated by a dot
1258  *                  (e.g. "_ftp._tcp").
1259  *
1260  * domain:          The domain name, e.g. "apple.com.".  Literal dots or backslashes,
1261  *                  if any, must be escaped, e.g. "1st\. Floor.apple.com."
1262  *
1263  * return value:    Returns 0 on success, -1 on error.
1264  *
1265  */
1266
1267 int DNSSD_API DNSServiceConstructFullName
1268     (
1269     char                            *fullName,
1270     const char                      *service,      /* may be NULL */
1271     const char                      *regtype,
1272     const char                      *domain
1273     );
1274
1275
1276 /*********************************************************************************************
1277  *
1278  *   TXT Record Construction Functions
1279  *
1280  *********************************************************************************************/
1281
1282 /*
1283  * A typical calling sequence for TXT record construction is something like:
1284  *
1285  * Client allocates storage for TXTRecord data (e.g. declare buffer on the stack)
1286  * TXTRecordCreate();
1287  * TXTRecordSetValue();
1288  * TXTRecordSetValue();
1289  * TXTRecordSetValue();
1290  * ...
1291  * DNSServiceRegister( ... TXTRecordGetLength(), TXTRecordGetBytesPtr() ... );
1292  * TXTRecordDeallocate();
1293  * Explicitly deallocate storage for TXTRecord data (if not allocated on the stack)
1294  */
1295
1296
1297 /* TXTRecordRef
1298  *
1299  * Opaque internal data type.
1300  * Note: Represents a DNS-SD TXT record.
1301  */
1302
1303 typedef struct _TXTRecordRef_t { char privatedata[16]; } TXTRecordRef;
1304
1305
1306 /* TXTRecordCreate()
1307  *
1308  * Creates a new empty TXTRecordRef referencing the specified storage.
1309  *
1310  * If the buffer parameter is NULL, or the specified storage size is not
1311  * large enough to hold a key subsequently added using TXTRecordSetValue(),
1312  * then additional memory will be added as needed using malloc().
1313  *
1314  * On some platforms, when memory is low, malloc() may fail. In this
1315  * case, TXTRecordSetValue() will return kDNSServiceErr_NoMemory, and this
1316  * error condition will need to be handled as appropriate by the caller.
1317  *
1318  * You can avoid the need to handle this error condition if you ensure
1319  * that the storage you initially provide is large enough to hold all
1320  * the key/value pairs that are to be added to the record.
1321  * The caller can precompute the exact length required for all of the
1322  * key/value pairs to be added, or simply provide a fixed-sized buffer
1323  * known in advance to be large enough.
1324  * A no-value (key-only) key requires  (1 + key length) bytes.
1325  * A key with empty value requires     (1 + key length + 1) bytes.
1326  * A key with non-empty value requires (1 + key length + 1 + value length).
1327  * For most applications, DNS-SD TXT records are generally
1328  * less than 100 bytes, so in most cases a simple fixed-sized
1329  * 256-byte buffer will be more than sufficient.
1330  * Recommended size limits for DNS-SD TXT Records are discussed in
1331  * <http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt>
1332  *
1333  * Note: When passing parameters to and from these TXT record APIs,
1334  * the key name does not include the '=' character. The '=' character
1335  * is the separator between the key and value in the on-the-wire
1336  * packet format; it is not part of either the key or the value.
1337  *
1338  * txtRecord:       A pointer to an uninitialized TXTRecordRef.
1339  *
1340  * bufferLen:       The size of the storage provided in the "buffer" parameter.
1341  *
1342  * buffer:          Optional caller-supplied storage used to hold the TXTRecord data.
1343  *                  This storage must remain valid for as long as
1344  *                  the TXTRecordRef.
1345  */
1346
1347 void DNSSD_API TXTRecordCreate
1348     (
1349     TXTRecordRef     *txtRecord,
1350     uint16_t         bufferLen,
1351     void             *buffer
1352     );
1353
1354
1355 /* TXTRecordDeallocate()
1356  *
1357  * Releases any resources allocated in the course of preparing a TXT Record
1358  * using TXTRecordCreate()/TXTRecordSetValue()/TXTRecordRemoveValue().
1359  * Ownership of the buffer provided in TXTRecordCreate() returns to the client.
1360  *
1361  * txtRecord:           A TXTRecordRef initialized by calling TXTRecordCreate().
1362  *
1363  */
1364
1365 void DNSSD_API TXTRecordDeallocate
1366     (
1367     TXTRecordRef     *txtRecord
1368     );
1369
1370
1371 /* TXTRecordSetValue()
1372  *
1373  * Adds a key (optionally with value) to a TXTRecordRef. If the "key" already
1374  * exists in the TXTRecordRef, then the current value will be replaced with
1375  * the new value.
1376  * Keys may exist in four states with respect to a given TXT record:
1377  *  - Absent (key does not appear at all)
1378  *  - Present with no value ("key" appears alone)
1379  *  - Present with empty value ("key=" appears in TXT record)
1380  *  - Present with non-empty value ("key=value" appears in TXT record)
1381  * For more details refer to "Data Syntax for DNS-SD TXT Records" in
1382  * <http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt>
1383  *
1384  * txtRecord:       A TXTRecordRef initialized by calling TXTRecordCreate().
1385  *
1386  * key:             A null-terminated string which only contains printable ASCII
1387  *                  values (0x20-0x7E), excluding '=' (0x3D). Keys should be
1388  *                  8 characters or less (not counting the terminating null).
1389  *
1390  * valueSize:       The size of the value.
1391  *
1392  * value:           Any binary value. For values that represent
1393  *                  textual data, UTF-8 is STRONGLY recommended.
1394  *                  For values that represent textual data, valueSize
1395  *                  should NOT include the terminating null (if any)
1396  *                  at the end of the string.
1397  *                  If NULL, then "key" will be added with no value.
1398  *                  If non-NULL but valueSize is zero, then "key=" will be
1399  *                  added with empty value.
1400  *
1401  * return value:    Returns kDNSServiceErr_NoError on success.
1402  *                  Returns kDNSServiceErr_Invalid if the "key" string contains
1403  *                  illegal characters.
1404  *                  Returns kDNSServiceErr_NoMemory if adding this key would
1405  *                  exceed the available storage.
1406  */
1407
1408 DNSServiceErrorType DNSSD_API TXTRecordSetValue
1409     (
1410     TXTRecordRef     *txtRecord,
1411     const char       *key,
1412     uint8_t          valueSize,        /* may be zero */
1413     const void       *value            /* may be NULL */
1414     );
1415
1416
1417 /* TXTRecordRemoveValue()
1418  *
1419  * Removes a key from a TXTRecordRef.  The "key" must be an
1420  * ASCII string which exists in the TXTRecordRef.
1421  *
1422  * txtRecord:       A TXTRecordRef initialized by calling TXTRecordCreate().
1423  *
1424  * key:             A key name which exists in the TXTRecordRef.
1425  *
1426  * return value:    Returns kDNSServiceErr_NoError on success.
1427  *                  Returns kDNSServiceErr_NoSuchKey if the "key" does not
1428  *                  exist in the TXTRecordRef.
1429  *
1430  */
1431
1432 DNSServiceErrorType DNSSD_API TXTRecordRemoveValue
1433     (
1434     TXTRecordRef     *txtRecord,
1435     const char       *key
1436     );
1437
1438
1439 /* TXTRecordGetLength()
1440  *
1441  * Allows you to determine the length of the raw bytes within a TXTRecordRef.
1442  *
1443  * txtRecord:       A TXTRecordRef initialized by calling TXTRecordCreate().
1444  *
1445  * return value:    Returns the size of the raw bytes inside a TXTRecordRef
1446  *                  which you can pass directly to DNSServiceRegister() or
1447  *                  to DNSServiceUpdateRecord().
1448  *                  Returns 0 if the TXTRecordRef is empty.
1449  *
1450  */
1451
1452 uint16_t DNSSD_API TXTRecordGetLength
1453     (
1454     const TXTRecordRef *txtRecord
1455     );
1456
1457
1458 /* TXTRecordGetBytesPtr()
1459  *
1460  * Allows you to retrieve a pointer to the raw bytes within a TXTRecordRef.
1461  *
1462  * txtRecord:       A TXTRecordRef initialized by calling TXTRecordCreate().
1463  *
1464  * return value:    Returns a pointer to the raw bytes inside the TXTRecordRef
1465  *                  which you can pass directly to DNSServiceRegister() or
1466  *                  to DNSServiceUpdateRecord().
1467  *
1468  */
1469
1470 const void * DNSSD_API TXTRecordGetBytesPtr
1471     (
1472     const TXTRecordRef *txtRecord
1473     );
1474
1475
1476 /*********************************************************************************************
1477  *
1478  *   TXT Record Parsing Functions
1479  *
1480  *********************************************************************************************/
1481
1482 /*
1483  * A typical calling sequence for TXT record parsing is something like:
1484  *
1485  * Receive TXT record data in DNSServiceResolve() callback
1486  * if (TXTRecordContainsKey(txtLen, txtRecord, "key")) then do something
1487  * val1ptr = TXTRecordGetValuePtr(txtLen, txtRecord, "key1", &len1);
1488  * val2ptr = TXTRecordGetValuePtr(txtLen, txtRecord, "key2", &len2);
1489  * ...
1490  * bcopy(val1ptr, myval1, len1);
1491  * bcopy(val2ptr, myval2, len2);
1492  * ...
1493  * return;
1494  *
1495  * If you wish to retain the values after return from the DNSServiceResolve()
1496  * callback, then you need to copy the data to your own storage using bcopy()
1497  * or similar, as shown in the example above.
1498  *
1499  * If for some reason you need to parse a TXT record you built yourself
1500  * using the TXT record construction functions above, then you can do
1501  * that using TXTRecordGetLength and TXTRecordGetBytesPtr calls:
1502  * TXTRecordGetValue(TXTRecordGetLength(x), TXTRecordGetBytesPtr(x), key, &len);
1503  *
1504  * Most applications only fetch keys they know about from a TXT record and
1505  * ignore the rest.
1506  * However, some debugging tools wish to fetch and display all keys.
1507  * To do that, use the TXTRecordGetCount() and TXTRecordGetItemAtIndex() calls.
1508  */
1509
1510 /* TXTRecordContainsKey()
1511  *
1512  * Allows you to determine if a given TXT Record contains a specified key.
1513  *
1514  * txtLen:          The size of the received TXT Record.
1515  *
1516  * txtRecord:       Pointer to the received TXT Record bytes.
1517  *
1518  * key:             A null-terminated ASCII string containing the key name.
1519  *
1520  * return value:    Returns 1 if the TXT Record contains the specified key.
1521  *                  Otherwise, it returns 0.
1522  *
1523  */
1524
1525 int DNSSD_API TXTRecordContainsKey
1526     (
1527     uint16_t         txtLen,
1528     const void       *txtRecord,
1529     const char       *key
1530     );
1531
1532
1533 /* TXTRecordGetValuePtr()
1534  *
1535  * Allows you to retrieve the value for a given key from a TXT Record.
1536  *
1537  * txtLen:          The size of the received TXT Record
1538  *
1539  * txtRecord:       Pointer to the received TXT Record bytes.
1540  *
1541  * key:             A null-terminated ASCII string containing the key name.
1542  *
1543  * valueLen:        On output, will be set to the size of the "value" data.
1544  *
1545  * return value:    Returns NULL if the key does not exist in this TXT record,
1546  *                  or exists with no value (to differentiate between
1547  *                  these two cases use TXTRecordContainsKey()).
1548  *                  Returns pointer to location within TXT Record bytes
1549  *                  if the key exists with empty or non-empty value.
1550  *                  For empty value, valueLen will be zero.
1551  *                  For non-empty value, valueLen will be length of value data.
1552  */
1553
1554 const void * DNSSD_API TXTRecordGetValuePtr
1555     (
1556     uint16_t         txtLen,
1557     const void       *txtRecord,
1558     const char       *key,
1559     uint8_t          *valueLen
1560     );
1561
1562
1563 /* TXTRecordGetCount()
1564  *
1565  * Returns the number of keys stored in the TXT Record.  The count
1566  * can be used with TXTRecordGetItemAtIndex() to iterate through the keys.
1567  *
1568  * txtLen:          The size of the received TXT Record.
1569  *
1570  * txtRecord:       Pointer to the received TXT Record bytes.
1571  *
1572  * return value:    Returns the total number of keys in the TXT Record.
1573  *
1574  */
1575
1576 uint16_t DNSSD_API TXTRecordGetCount
1577     (
1578     uint16_t         txtLen,
1579     const void       *txtRecord
1580     );
1581
1582
1583 /* TXTRecordGetItemAtIndex()
1584  *
1585  * Allows you to retrieve a key name and value pointer, given an index into
1586  * a TXT Record.  Legal index values range from zero to TXTRecordGetCount()-1.
1587  * It's also possible to iterate through keys in a TXT record by simply
1588  * calling TXTRecordGetItemAtIndex() repeatedly, beginning with index zero
1589  * and increasing until TXTRecordGetItemAtIndex() returns kDNSServiceErr_Invalid.
1590  *
1591  * On return:
1592  * For keys with no value, *value is set to NULL and *valueLen is zero.
1593  * For keys with empty value, *value is non-NULL and *valueLen is zero.
1594  * For keys with non-empty value, *value is non-NULL and *valueLen is non-zero.
1595  *
1596  * txtLen:          The size of the received TXT Record.
1597  *
1598  * txtRecord:       Pointer to the received TXT Record bytes.
1599  *
1600  * index:           An index into the TXT Record.
1601  *
1602  * keyBufLen:       The size of the string buffer being supplied.
1603  *
1604  * key:             A string buffer used to store the key name.
1605  *                  On return, the buffer contains a null-terminated C string
1606  *                  giving the key name. DNS-SD TXT keys are usually
1607  *                  8 characters or less. To hold the maximum possible
1608  *                  key name, the buffer should be 256 bytes long.
1609  *
1610  * valueLen:        On output, will be set to the size of the "value" data.
1611  *
1612  * value:           On output, *value is set to point to location within TXT
1613  *                  Record bytes that holds the value data.
1614  *
1615  * return value:    Returns kDNSServiceErr_NoError on success.
1616  *                  Returns kDNSServiceErr_NoMemory if keyBufLen is too short.
1617  *                  Returns kDNSServiceErr_Invalid if index is greater than
1618  *                  TXTRecordGetCount()-1.
1619  */
1620
1621 DNSServiceErrorType DNSSD_API TXTRecordGetItemAtIndex
1622     (
1623     uint16_t         txtLen,
1624     const void       *txtRecord,
1625     uint16_t         index,
1626     uint16_t         keyBufLen,
1627     char             *key,
1628     uint8_t          *valueLen,
1629     const void       **value
1630     );
1631
1632 #ifdef __APPLE_API_PRIVATE
1633
1634 /*
1635  * Mac OS X specific functionality
1636  * 3rd party clients of this API should not depend on future support or availability of this routine
1637  */
1638
1639 /* DNSServiceSetDefaultDomainForUser()
1640  *
1641  * Set the default domain for the caller's UID.  Future browse and registration
1642  * calls by this user that do not specify an explicit domain will browse and
1643  * register in this wide-area domain in addition to .local.  In addition, this
1644  * domain will be returned as a Browse domain via domain enumeration calls.
1645  * 
1646  *
1647  * Parameters:
1648  *
1649  * flags:           Pass kDNSServiceFlagsAdd to add a domain for a user.  Call without
1650  *                  this flag set to clear a previously added domain.
1651  *
1652  * domain:          The domain to be used for the caller's UID.
1653  *
1654  * return value:    Returns kDNSServiceErr_NoError on succeses, otherwise returns
1655  *                  an error code indicating the error that occurred
1656  */
1657
1658 DNSServiceErrorType DNSSD_API DNSServiceSetDefaultDomainForUser
1659     (
1660     DNSServiceFlags                    flags,
1661     const char                         *domain
1662     );  
1663         
1664 #endif //__APPLE_API_PRIVATE
1665
1666 #ifdef  __cplusplus
1667     }
1668 #endif
1669
1670 #endif  /* _DNS_SD_H */