1 /* -*- Mode: C; tab-width: 4 -*- 2 * 3 * Copyright (c) 2003-2004, Apple Computer, Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of its 14 * contributors may be used to endorse or promote products derived from this 15 * software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 30 /*! @header DNS Service Discovery 31 * 32 * @discussion This section describes the functions, callbacks, and data structures 33 * that make up the DNS Service Discovery API. 34 * 35 * The DNS Service Discovery API is part of Bonjour, Apple's implementation 36 * of zero-configuration networking (ZEROCONF). 37 * 38 * Bonjour allows you to register a network service, such as a 39 * printer or file server, so that it can be found by name or browsed 40 * for by service type and domain. Using Bonjour, applications can 41 * discover what services are available on the network, along with 42 * all the information -- such as name, IP address, and port -- 43 * necessary to access a particular service. 44 * 45 * In effect, Bonjour combines the functions of a local DNS server and 46 * AppleTalk. Bonjour allows applications to provide user-friendly printer 47 * and server browsing, among other things, over standard IP networks. 48 * This behavior is a result of combining protocols such as multicast and 49 * DNS to add new functionality to the network (such as multicast DNS). 50 * 51 * Bonjour gives applications easy access to services over local IP 52 * networks without requiring the service or the application to support 53 * an AppleTalk or a Netbeui stack, and without requiring a DNS server 54 * for the local network. 55 */ 56 57 58 /* _DNS_SD_H contains the mDNSResponder version number for this header file, formatted as follows: 59 * Major part of the build number * 10000 + 60 * minor part of the build number * 100 61 * For example, Mac OS X 10.4.9 has mDNSResponder-108.4, which would be represented as 62 * version 1080400. This allows C code to do simple greater-than and less-than comparisons: 63 * e.g. an application that requires the DNSServiceGetProperty() call (new in mDNSResponder-126) can check: 64 * 65 * #if _DNS_SD_H+0 >= 1260000 66 * ... some C code that calls DNSServiceGetProperty() ... 67 * #endif 68 * 69 * The version defined in this header file symbol allows for compile-time 70 * checking, so that C code building with earlier versions of the header file 71 * can avoid compile errors trying to use functions that aren't even defined 72 * in those earlier versions. Similar checks may also be performed at run-time: 73 * => weak linking -- to avoid link failures if run with an earlier 74 * version of the library that's missing some desired symbol, or 75 * => DNSServiceGetProperty(DaemonVersion) -- to verify whether the running daemon 76 * ("system service" on Windows) meets some required minimum functionality level. 77 */ 78 79 #ifndef _DNS_SD_H 80 #define _DNS_SD_H 2120100 81 82 #ifdef __cplusplus 83 extern "C" { 84 #endif 85 86 /* standard calling convention under Win32 is __stdcall */ 87 /* Note: When compiling Intel EFI (Extensible Firmware Interface) under MS Visual Studio, the */ 88 /* _WIN32 symbol is defined by the compiler even though it's NOT compiling code for Windows32 */ 89 #if defined(_WIN32) && !defined(EFI32) && !defined(EFI64) 90 #define DNSSD_API __stdcall 91 #else 92 #define DNSSD_API 93 #endif 94 95 /* stdint.h does not exist on FreeBSD 4.x; its types are defined in sys/types.h instead */ 96 #if defined(__FreeBSD__) && (__FreeBSD__ < 5) 97 #include <sys/types.h> 98 99 /* Likewise, on Sun, standard integer types are in sys/types.h */ 100 #elif defined(__sun__) 101 #include <sys/types.h> 102 103 /* EFI does not have stdint.h, or anything else equivalent */ 104 #elif defined(EFI32) || defined(EFI64) || defined(EFIX64) 105 #include "Tiano.h" 106 #if !defined(_STDINT_H_) 107 typedef UINT8 uint8_t; 108 typedef INT8 int8_t; 109 typedef UINT16 uint16_t; 110 typedef INT16 int16_t; 111 typedef UINT32 uint32_t; 112 typedef INT32 int32_t; 113 #endif 114 /* Windows has its own differences */ 115 #elif defined(_WIN32) 116 #include <windows.h> 117 #define _UNUSED 118 #ifndef _MSL_STDINT_H 119 typedef UINT8 uint8_t; 120 typedef INT8 int8_t; 121 typedef UINT16 uint16_t; 122 typedef INT16 int16_t; 123 typedef UINT32 uint32_t; 124 typedef INT32 int32_t; 125 #endif 126 127 /* All other Posix platforms use stdint.h */ 128 #else 129 #include <stdint.h> 130 #endif 131 132 /* DNSServiceRef, DNSRecordRef 133 * 134 * Opaque internal data types. 135 * Note: client is responsible for serializing access to these structures if 136 * they are shared between concurrent threads. 137 */ 138 139 typedef struct _DNSServiceRef_t *DNSServiceRef; 140 typedef struct _DNSRecordRef_t *DNSRecordRef; 141 142 struct sockaddr; 143 144 /*! @enum General flags 145 * Most DNS-SD API functions and callbacks include a DNSServiceFlags parameter. 146 * As a general rule, any given bit in the 32-bit flags field has a specific fixed meaning, 147 * regardless of the function or callback being used. For any given function or callback, 148 * typically only a subset of the possible flags are meaningful, and all others should be zero. 149 * The discussion section for each API call describes which flags are valid for that call 150 * and callback. In some cases, for a particular call, it may be that no flags are currently 151 * defined, in which case the DNSServiceFlags parameter exists purely to allow future expansion. 152 * In all cases, developers should expect that in future releases, it is possible that new flag 153 * values will be defined, and write code with this in mind. For example, code that tests 154 * if (flags == kDNSServiceFlagsAdd) ... 155 * will fail if, in a future release, another bit in the 32-bit flags field is also set. 156 * The reliable way to test whether a particular bit is set is not with an equality test, 157 * but with a bitwise mask: 158 * if (flags & kDNSServiceFlagsAdd) ... 159 */ 160 enum 161 { 162 kDNSServiceFlagsMoreComing = 0x1, 163 /* MoreComing indicates to a callback that at least one more result is 164 * queued and will be delivered following immediately after this one. 165 * When the MoreComing flag is set, applications should not immediately 166 * update their UI, because this can result in a great deal of ugly flickering 167 * on the screen, and can waste a great deal of CPU time repeatedly updating 168 * the screen with content that is then immediately erased, over and over. 169 * Applications should wait until until MoreComing is not set, and then 170 * update their UI when no more changes are imminent. 171 * When MoreComing is not set, that doesn't mean there will be no more 172 * answers EVER, just that there are no more answers immediately 173 * available right now at this instant. If more answers become available 174 * in the future they will be delivered as usual. 175 */ 176 177 kDNSServiceFlagsAdd = 0x2, 178 kDNSServiceFlagsDefault = 0x4, 179 /* Flags for domain enumeration and browse/query reply callbacks. 180 * "Default" applies only to enumeration and is only valid in 181 * conjunction with "Add". An enumeration callback with the "Add" 182 * flag NOT set indicates a "Remove", i.e. the domain is no longer 183 * valid. 184 */ 185 186 kDNSServiceFlagsNoAutoRename = 0x8, 187 /* Flag for specifying renaming behavior on name conflict when registering 188 * non-shared records. By default, name conflicts are automatically handled 189 * by renaming the service. NoAutoRename overrides this behavior - with this 190 * flag set, name conflicts will result in a callback. The NoAutorename flag 191 * is only valid if a name is explicitly specified when registering a service 192 * (i.e. the default name is not used.) 193 */ 194 195 kDNSServiceFlagsShared = 0x10, 196 kDNSServiceFlagsUnique = 0x20, 197 /* Flag for registering individual records on a connected 198 * DNSServiceRef. Shared indicates that there may be multiple records 199 * with this name on the network (e.g. PTR records). Unique indicates that the 200 * record's name is to be unique on the network (e.g. SRV records). 201 */ 202 203 kDNSServiceFlagsBrowseDomains = 0x40, 204 kDNSServiceFlagsRegistrationDomains = 0x80, 205 /* Flags for specifying domain enumeration type in DNSServiceEnumerateDomains. 206 * BrowseDomains enumerates domains recommended for browsing, RegistrationDomains 207 * enumerates domains recommended for registration. 208 */ 209 210 kDNSServiceFlagsLongLivedQuery = 0x100, 211 /* Flag for creating a long-lived unicast query for the DNSServiceQueryRecord call. */ 212 213 kDNSServiceFlagsAllowRemoteQuery = 0x200, 214 /* Flag for creating a record for which we will answer remote queries 215 * (queries from hosts more than one hop away; hosts not directly connected to the local link). 216 */ 217 218 kDNSServiceFlagsForceMulticast = 0x400, 219 /* Flag for signifying that a query or registration should be performed exclusively via multicast 220 * DNS, even for a name in a domain (e.g. foo.apple.com.) that would normally imply unicast DNS. 221 */ 222 223 kDNSServiceFlagsForce = 0x800, 224 /* Flag for signifying a "stronger" variant of an operation. 225 * Currently defined only for DNSServiceReconfirmRecord(), where it forces a record to 226 * be removed from the cache immediately, instead of querying for a few seconds before 227 * concluding that the record is no longer valid and then removing it. This flag should 228 * be used with caution because if a service browsing PTR record is indeed still valid 229 * on the network, forcing its removal will result in a user-interface flap -- the 230 * discovered service instance will disappear, and then re-appear moments later. 231 */ 232 233 kDNSServiceFlagsReturnIntermediates = 0x1000, 234 /* Flag for returning intermediate results. 235 * For example, if a query results in an authoritative NXDomain (name does not exist) 236 * then that result is returned to the client. However the query is not implicitly 237 * cancelled -- it remains active and if the answer subsequently changes 238 * (e.g. because a VPN tunnel is subsequently established) then that positive 239 * result will still be returned to the client. 240 * Similarly, if a query results in a CNAME record, then in addition to following 241 * the CNAME referral, the intermediate CNAME result is also returned to the client. 242 * When this flag is not set, NXDomain errors are not returned, and CNAME records 243 * are followed silently without informing the client of the intermediate steps. 244 * (In earlier builds this flag was briefly calledkDNSServiceFlagsReturnCNAME) 245 */ 246 247 kDNSServiceFlagsNonBrowsable = 0x2000, 248 /* A service registered with the NonBrowsable flag set can be resolved using 249 * DNSServiceResolve(), but will not be discoverable using DNSServiceBrowse(). 250 * This is for cases where the name is actually a GUID; it is found by other means; 251 * there is no end-user benefit to browsing to find a long list of opaque GUIDs. 252 * Using the NonBrowsable flag creates SRV+TXT without the cost of also advertising 253 * an associated PTR record. 254 */ 255 256 kDNSServiceFlagsShareConnection = 0x4000, 257 /* For efficiency, clients that perform many concurrent operations may want to use a 258 * single Unix Domain Socket connection with the background daemon, instead of having a 259 * separate connection for each independent operation. To use this mode, clients first 260 * call DNSServiceCreateConnection(&MainRef) to initialize the main DNSServiceRef. 261 * For each subsequent operation that is to share that same connection, the client copies 262 * the MainRef, and then passes the address of that copy, setting the ShareConnection flag 263 * to tell the library that this DNSServiceRef is not a typical uninitialized DNSServiceRef; 264 * it's a copy of an existing DNSServiceRef whose connection information should be reused. 265 * 266 * For example: 267 * 268 * DNSServiceErrorType error; 269 * DNSServiceRef MainRef; 270 * error = DNSServiceCreateConnection(&MainRef); 271 * if (error) ... 272 * DNSServiceRef BrowseRef = MainRef; // Important: COPY the primary DNSServiceRef first... 273 * error = DNSServiceBrowse(&BrowseRef, kDNSServiceFlagsShareConnection, ...); // then use the copy 274 * if (error) ... 275 * ... 276 * DNSServiceRefDeallocate(BrowseRef); // Terminate the browse operation 277 * DNSServiceRefDeallocate(MainRef); // Terminate the shared connection 278 * 279 * Notes: 280 * 281 * 1. Collective kDNSServiceFlagsMoreComing flag 282 * When callbacks are invoked using a shared DNSServiceRef, the 283 * kDNSServiceFlagsMoreComing flag applies collectively to *all* active 284 * operations sharing the same parent DNSServiceRef. If the MoreComing flag is 285 * set it means that there are more results queued on this parent DNSServiceRef, 286 * but not necessarily more results for this particular callback function. 287 * The implication of this for client programmers is that when a callback 288 * is invoked with the MoreComing flag set, the code should update its 289 * internal data structures with the new result, and set a variable indicating 290 * that its UI needs to be updated. Then, later when a callback is eventually 291 * invoked with the MoreComing flag not set, the code should update *all* 292 * stale UI elements related to that shared parent DNSServiceRef that need 293 * updating, not just the UI elements related to the particular callback 294 * that happened to be the last one to be invoked. 295 * 296 * 2. Canceling operations and kDNSServiceFlagsMoreComing 297 * Whenever you cancel any operation for which you had deferred UI updates 298 * waiting because of a kDNSServiceFlagsMoreComing flag, you should perform 299 * those deferred UI updates. This is because, after cancelling the operation, 300 * you can no longer wait for a callback *without* MoreComing set, to tell 301 * you do perform your deferred UI updates (the operation has been canceled, 302 * so there will be no more callbacks). An implication of the collective 303 * kDNSServiceFlagsMoreComing flag for shared connections is that this 304 * guideline applies more broadly -- any time you cancel an operation on 305 * a shared connection, you should perform all deferred UI updates for all 306 * operations sharing that connection. This is because the MoreComing flag 307 * might have been referring to events coming for the operation you canceled, 308 * which will now not be coming because the operation has been canceled. 309 * 310 * 3. Only share DNSServiceRef's created with DNSServiceCreateConnection 311 * Calling DNSServiceCreateConnection(&ref) creates a special shareable DNSServiceRef. 312 * DNSServiceRef's created by other calls like DNSServiceBrowse() or DNSServiceResolve() 313 * cannot be shared by copying them and using kDNSServiceFlagsShareConnection. 314 * 315 * 4. Don't Double-Deallocate 316 * Calling DNSServiceRefDeallocate(ref) for a particular operation's DNSServiceRef terminates 317 * just that operation. Calling DNSServiceRefDeallocate(ref) for the main shared DNSServiceRef 318 * (the parent DNSServiceRef, originally created by DNSServiceCreateConnection(&ref)) 319 * automatically terminates the shared connection and all operations that were still using it. 320 * After doing this, DO NOT then attempt to deallocate any remaining subordinate DNSServiceRef's. 321 * The memory used by those subordinate DNSServiceRef's has already been freed, so any attempt 322 * to do a DNSServiceRefDeallocate (or any other operation) on them will result in accesses 323 * to freed memory, leading to crashes or other equally undesirable results. 324 * 325 * 5. Thread Safety 326 * The dns_sd.h API does not presuppose any particular threading model, and consequently 327 * does no locking of its own (which would require linking some specific threading library). 328 * If client code calls API routines on the same DNSServiceRef concurrently 329 * from multiple threads, it is the client's responsibility to use a mutext 330 * lock or take similar appropriate precautions to serialize those calls. 331 */ 332 333 kDNSServiceFlagsSuppressUnusable = 0x8000 334 /* Placeholder definition, for future use 335 */ 336 }; 337 338 /* Possible protocols for DNSServiceNATPortMappingCreate(). */ 339 enum 340 { 341 kDNSServiceProtocol_IPv4 = 0x01, 342 kDNSServiceProtocol_IPv6 = 0x02, 343 /* 0x04 and 0x08 reserved for future internetwork protocols */ 344 345 kDNSServiceProtocol_UDP = 0x10, 346 kDNSServiceProtocol_TCP = 0x20 347 /* 0x40 and 0x80 reserved for future transport protocols, e.g. SCTP [RFC 2960] 348 * or DCCP [RFC 4340]. If future NAT gateways are created that support port 349 * mappings for these protocols, new constants will be defined here. 350 */ 351 }; 352 353 /* 354 * The values for DNS Classes and Types are listed in RFC 1035, and are available 355 * on every OS in its DNS header file. Unfortunately every OS does not have the 356 * same header file containing DNS Class and Type constants, and the names of 357 * the constants are not consistent. For example, BIND 8 uses "T_A", 358 * BIND 9 uses "ns_t_a", Windows uses "DNS_TYPE_A", etc. 359 * For this reason, these constants are also listed here, so that code using 360 * the DNS-SD programming APIs can use these constants, so that the same code 361 * can compile on all our supported platforms. 362 */ 363 364 enum 365 { 366 kDNSServiceClass_IN = 1 /* Internet */ 367 }; 368 369 enum 370 { 371 kDNSServiceType_A = 1, /* Host address. */ 372 kDNSServiceType_NS = 2, /* Authoritative server. */ 373 kDNSServiceType_MD = 3, /* Mail destination. */ 374 kDNSServiceType_MF = 4, /* Mail forwarder. */ 375 kDNSServiceType_CNAME = 5, /* Canonical name. */ 376 kDNSServiceType_SOA = 6, /* Start of authority zone. */ 377 kDNSServiceType_MB = 7, /* Mailbox domain name. */ 378 kDNSServiceType_MG = 8, /* Mail group member. */ 379 kDNSServiceType_MR = 9, /* Mail rename name. */ 380 kDNSServiceType_NULL = 10, /* Null resource record. */ 381 kDNSServiceType_WKS = 11, /* Well known service. */ 382 kDNSServiceType_PTR = 12, /* Domain name pointer. */ 383 kDNSServiceType_HINFO = 13, /* Host information. */ 384 kDNSServiceType_MINFO = 14, /* Mailbox information. */ 385 kDNSServiceType_MX = 15, /* Mail routing information. */ 386 kDNSServiceType_TXT = 16, /* One or more text strings (NOT "zero or more..."). */ 387 kDNSServiceType_RP = 17, /* Responsible person. */ 388 kDNSServiceType_AFSDB = 18, /* AFS cell database. */ 389 kDNSServiceType_X25 = 19, /* X_25 calling address. */ 390 kDNSServiceType_ISDN = 20, /* ISDN calling address. */ 391 kDNSServiceType_RT = 21, /* Router. */ 392 kDNSServiceType_NSAP = 22, /* NSAP address. */ 393 kDNSServiceType_NSAP_PTR = 23, /* Reverse NSAP lookup (deprecated). */ 394 kDNSServiceType_SIG = 24, /* Security signature. */ 395 kDNSServiceType_KEY = 25, /* Security key. */ 396 kDNSServiceType_PX = 26, /* X.400 mail mapping. */ 397 kDNSServiceType_GPOS = 27, /* Geographical position (withdrawn). */ 398 kDNSServiceType_AAAA = 28, /* IPv6 Address. */ 399 kDNSServiceType_LOC = 29, /* Location Information. */ 400 kDNSServiceType_NXT = 30, /* Next domain (security). */ 401 kDNSServiceType_EID = 31, /* Endpoint identifier. */ 402 kDNSServiceType_NIMLOC = 32, /* Nimrod Locator. */ 403 kDNSServiceType_SRV = 33, /* Server Selection. */ 404 kDNSServiceType_ATMA = 34, /* ATM Address */ 405 kDNSServiceType_NAPTR = 35, /* Naming Authority PoinTeR */ 406 kDNSServiceType_KX = 36, /* Key Exchange */ 407 kDNSServiceType_CERT = 37, /* Certification record */ 408 kDNSServiceType_A6 = 38, /* IPv6 Address (deprecated) */ 409 kDNSServiceType_DNAME = 39, /* Non-terminal DNAME (for IPv6) */ 410 kDNSServiceType_SINK = 40, /* Kitchen sink (experimental) */ 411 kDNSServiceType_OPT = 41, /* EDNS0 option (meta-RR) */ 412 kDNSServiceType_APL = 42, /* Address Prefix List */ 413 kDNSServiceType_DS = 43, /* Delegation Signer */ 414 kDNSServiceType_SSHFP = 44, /* SSH Key Fingerprint */ 415 kDNSServiceType_IPSECKEY = 45, /* IPSECKEY */ 416 kDNSServiceType_RRSIG = 46, /* RRSIG */ 417 kDNSServiceType_NSEC = 47, /* Denial of Existence */ 418 kDNSServiceType_DNSKEY = 48, /* DNSKEY */ 419 kDNSServiceType_DHCID = 49, /* DHCP Client Identifier */ 420 kDNSServiceType_NSEC3 = 50, /* Hashed Authenticated Denial of Existence */ 421 kDNSServiceType_NSEC3PARAM= 51, /* Hashed Authenticated Denial of Existence */ 422 423 kDNSServiceType_HIP = 55, /* Host Identity Protocol */ 424 425 kDNSServiceType_SPF = 99, /* Sender Policy Framework for E-Mail */ 426 kDNSServiceType_UINFO = 100, /* IANA-Reserved */ 427 kDNSServiceType_UID = 101, /* IANA-Reserved */ 428 kDNSServiceType_GID = 102, /* IANA-Reserved */ 429 kDNSServiceType_UNSPEC = 103, /* IANA-Reserved */ 430 431 kDNSServiceType_TKEY = 249, /* Transaction key */ 432 kDNSServiceType_TSIG = 250, /* Transaction signature. */ 433 kDNSServiceType_IXFR = 251, /* Incremental zone transfer. */ 434 kDNSServiceType_AXFR = 252, /* Transfer zone of authority. */ 435 kDNSServiceType_MAILB = 253, /* Transfer mailbox records. */ 436 kDNSServiceType_MAILA = 254, /* Transfer mail agent records. */ 437 kDNSServiceType_ANY = 255 /* Wildcard match. */ 438 }; 439 440 /* possible error code values */ 441 enum 442 { 443 kDNSServiceErr_NoError = 0, 444 kDNSServiceErr_Unknown = -65537, /* 0xFFFE FFFF */ 445 kDNSServiceErr_NoSuchName = -65538, 446 kDNSServiceErr_NoMemory = -65539, 447 kDNSServiceErr_BadParam = -65540, 448 kDNSServiceErr_BadReference = -65541, 449 kDNSServiceErr_BadState = -65542, 450 kDNSServiceErr_BadFlags = -65543, 451 kDNSServiceErr_Unsupported = -65544, 452 kDNSServiceErr_NotInitialized = -65545, 453 kDNSServiceErr_AlreadyRegistered = -65547, 454 kDNSServiceErr_NameConflict = -65548, 455 kDNSServiceErr_Invalid = -65549, 456 kDNSServiceErr_Firewall = -65550, 457 kDNSServiceErr_Incompatible = -65551, /* client library incompatible with daemon */ 458 kDNSServiceErr_BadInterfaceIndex = -65552, 459 kDNSServiceErr_Refused = -65553, 460 kDNSServiceErr_NoSuchRecord = -65554, 461 kDNSServiceErr_NoAuth = -65555, 462 kDNSServiceErr_NoSuchKey = -65556, 463 kDNSServiceErr_NATTraversal = -65557, 464 kDNSServiceErr_DoubleNAT = -65558, 465 kDNSServiceErr_BadTime = -65559, /* Codes up to here existed in Tiger */ 466 kDNSServiceErr_BadSig = -65560, 467 kDNSServiceErr_BadKey = -65561, 468 kDNSServiceErr_Transient = -65562, 469 kDNSServiceErr_ServiceNotRunning = -65563, /* Background daemon not running */ 470 kDNSServiceErr_NATPortMappingUnsupported = -65564, /* NAT doesn't support NAT-PMP or UPnP */ 471 kDNSServiceErr_NATPortMappingDisabled = -65565, /* NAT supports NAT-PMP or UPnP but it's disabled by the administrator */ 472 kDNSServiceErr_NoRouter = -65566, /* No router currently configured (probably no network connectivity) */ 473 kDNSServiceErr_PollingMode = -65567 474 475 /* mDNS Error codes are in the range 476 * FFFE FF00 (-65792) to FFFE FFFF (-65537) */ 477 }; 478 479 /* Maximum length, in bytes, of a service name represented as a */ 480 /* literal C-String, including the terminating NULL at the end. */ 481 482 #define kDNSServiceMaxServiceName 64 483 484 /* Maximum length, in bytes, of a domain name represented as an *escaped* C-String */ 485 /* including the final trailing dot, and the C-String terminating NULL at the end. */ 486 487 #define kDNSServiceMaxDomainName 1009 488 489 /* 490 * Notes on DNS Name Escaping 491 * -- or -- 492 * "Why is kDNSServiceMaxDomainName 1009, when the maximum legal domain name is 256 bytes?" 493 * 494 * All strings used in the DNS-SD APIs are UTF-8 strings. Apart from the exceptions noted below, 495 * the APIs expect the strings to be properly escaped, using the conventional DNS escaping rules: 496 * 497 * '\\' represents a single literal '\' in the name 498 * '\.' represents a single literal '.' in the name 499 * '\ddd', where ddd is a three-digit decimal value from 000 to 255, 500 * represents a single literal byte with that value. 501 * A bare unescaped '.' is a label separator, marking a boundary between domain and subdomain. 502 * 503 * The exceptions, that do not use escaping, are the routines where the full 504 * DNS name of a resource is broken, for convenience, into servicename/regtype/domain. 505 * In these routines, the "servicename" is NOT escaped. It does not need to be, since 506 * it is, by definition, just a single literal string. Any characters in that string 507 * represent exactly what they are. The "regtype" portion is, technically speaking, 508 * escaped, but since legal regtypes are only allowed to contain letters, digits, 509 * and hyphens, there is nothing to escape, so the issue is moot. The "domain" 510 * portion is also escaped, though most domains in use on the public Internet 511 * today, like regtypes, don't contain any characters that need to be escaped. 512 * As DNS-SD becomes more popular, rich-text domains for service discovery will 513 * become common, so software should be written to cope with domains with escaping. 514 * 515 * The servicename may be up to 63 bytes of UTF-8 text (not counting the C-String 516 * terminating NULL at the end). The regtype is of the form _service._tcp or 517 * _service._udp, where the "service" part is 1-14 characters, which may be 518 * letters, digits, or hyphens. The domain part of the three-part name may be 519 * any legal domain, providing that the resulting servicename+regtype+domain 520 * name does not exceed 256 bytes. 521 * 522 * For most software, these issues are transparent. When browsing, the discovered 523 * servicenames should simply be displayed as-is. When resolving, the discovered 524 * servicename/regtype/domain are simply passed unchanged to DNSServiceResolve(). 525 * When a DNSServiceResolve() succeeds, the returned fullname is already in 526 * the correct format to pass to standard system DNS APIs such as res_query(). 527 * For converting from servicename/regtype/domain to a single properly-escaped 528 * full DNS name, the helper function DNSServiceConstructFullName() is provided. 529 * 530 * The following (highly contrived) example illustrates the escaping process. 531 * Suppose you have an service called "Dr. Smith\Dr. Johnson", of type "_ftp._tcp" 532 * in subdomain "4th. Floor" of subdomain "Building 2" of domain "apple.com." 533 * The full (escaped) DNS name of this service's SRV record would be: 534 * Dr\.\032Smith\\Dr\.\032Johnson._ftp._tcp.4th\.\032Floor.Building\0322.apple.com. 535 */ 536 537 538 /* 539 * Constants for specifying an interface index 540 * 541 * Specific interface indexes are identified via a 32-bit unsigned integer returned 542 * by the if_nametoindex() family of calls. 543 * 544 * If the client passes 0 for interface index, that means "do the right thing", 545 * which (at present) means, "if the name is in an mDNS local multicast domain 546 * (e.g. 'local.', '254.169.in-addr.arpa.', '{8,9,A,B}.E.F.ip6.arpa.') then multicast 547 * on all applicable interfaces, otherwise send via unicast to the appropriate 548 * DNS server." Normally, most clients will use 0 for interface index to 549 * automatically get the default sensible behaviour. 550 * 551 * If the client passes a positive interface index, then for multicast names that 552 * indicates to do the operation only on that one interface. For unicast names the 553 * interface index is ignored unless kDNSServiceFlagsForceMulticast is also set. 554 * 555 * If the client passes kDNSServiceInterfaceIndexLocalOnly when registering 556 * a service, then that service will be found *only* by other local clients 557 * on the same machine that are browsing using kDNSServiceInterfaceIndexLocalOnly 558 * or kDNSServiceInterfaceIndexAny. 559 * If a client has a 'private' service, accessible only to other processes 560 * running on the same machine, this allows the client to advertise that service 561 * in a way such that it does not inadvertently appear in service lists on 562 * all the other machines on the network. 563 * 564 * If the client passes kDNSServiceInterfaceIndexLocalOnly when browsing 565 * then it will find *all* records registered on that same local machine. 566 * Clients explicitly wishing to discover *only* LocalOnly services can 567 * accomplish this by inspecting the interfaceIndex of each service reported 568 * to their DNSServiceBrowseReply() callback function, and discarding those 569 * where the interface index is not kDNSServiceInterfaceIndexLocalOnly. 570 */ 571 572 #define kDNSServiceInterfaceIndexAny 0 573 #define kDNSServiceInterfaceIndexLocalOnly ((uint32_t)-1) 574 #define kDNSServiceInterfaceIndexUnicast ((uint32_t)-2) 575 576 typedef uint32_t DNSServiceFlags; 577 typedef uint32_t DNSServiceProtocol; 578 typedef int32_t DNSServiceErrorType; 579 580 581 /********************************************************************************************* 582 * 583 * Version checking 584 * 585 *********************************************************************************************/ 586 587 /* DNSServiceGetProperty() Parameters: 588 * 589 * property: The requested property. 590 * Currently the only property defined is kDNSServiceProperty_DaemonVersion. 591 * 592 * result: Place to store result. 593 * For retrieving DaemonVersion, this should be the address of a uint32_t. 594 * 595 * size: Pointer to uint32_t containing size of the result location. 596 * For retrieving DaemonVersion, this should be sizeof(uint32_t). 597 * On return the uint32_t is updated to the size of the data returned. 598 * For DaemonVersion, the returned size is always sizeof(uint32_t), but 599 * future properties could be defined which return variable-sized results. 600 * 601 * return value: Returns kDNSServiceErr_NoError on success, or kDNSServiceErr_ServiceNotRunning 602 * if the daemon (or "system service" on Windows) is not running. 603 */ 604 605 DNSServiceErrorType DNSSD_API DNSServiceGetProperty 606 ( 607 const char *property, /* Requested property (i.e. kDNSServiceProperty_DaemonVersion) */ 608 void *result, /* Pointer to place to store result */ 609 uint32_t *size /* size of result location */ 610 ); 611 612 /* 613 * When requesting kDNSServiceProperty_DaemonVersion, the result pointer must point 614 * to a 32-bit unsigned integer, and the size parameter must be set to sizeof(uint32_t). 615 * 616 * On return, the 32-bit unsigned integer contains the version number, formatted as follows: 617 * Major part of the build number * 10000 + 618 * minor part of the build number * 100 619 * 620 * For example, Mac OS X 10.4.9 has mDNSResponder-108.4, which would be represented as 621 * version 1080400. This allows applications to do simple greater-than and less-than comparisons: 622 * e.g. an application that requires at least mDNSResponder-108.4 can check: 623 * 624 * if (version >= 1080400) ... 625 * 626 * Example usage: 627 * 628 * uint32_t version; 629 * uint32_t size = sizeof(version); 630 * DNSServiceErrorType err = DNSServiceGetProperty(kDNSServiceProperty_DaemonVersion, &version, &size); 631 * if (!err) printf("Bonjour version is %d.%d\n", version / 10000, version / 100 % 100); 632 */ 633 634 #define kDNSServiceProperty_DaemonVersion "DaemonVersion" 635 636 637 /********************************************************************************************* 638 * 639 * Unix Domain Socket access, DNSServiceRef deallocation, and data processing functions 640 * 641 *********************************************************************************************/ 642 643 /* DNSServiceRefSockFD() 644 * 645 * Access underlying Unix domain socket for an initialized DNSServiceRef. 646 * The DNS Service Discovery implementation uses this socket to communicate between the client and 647 * the mDNSResponder daemon. The application MUST NOT directly read from or write to this socket. 648 * Access to the socket is provided so that it can be used as a kqueue event source, a CFRunLoop 649 * event source, in a select() loop, etc. When the underlying event management subsystem (kqueue/ 650 * select/CFRunLoop etc.) indicates to the client that data is available for reading on the 651 * socket, the client should call DNSServiceProcessResult(), which will extract the daemon's 652 * reply from the socket, and pass it to the appropriate application callback. By using a run 653 * loop or select(), results from the daemon can be processed asynchronously. Alternatively, 654 * a client can choose to fork a thread and have it loop calling "DNSServiceProcessResult(ref);" 655 * If DNSServiceProcessResult() is called when no data is available for reading on the socket, it 656 * will block until data does become available, and then process the data and return to the caller. 657 * When data arrives on the socket, the client is responsible for calling DNSServiceProcessResult(ref) 658 * in a timely fashion -- if the client allows a large backlog of data to build up the daemon 659 * may terminate the connection. 660 * 661 * sdRef: A DNSServiceRef initialized by any of the DNSService calls. 662 * 663 * return value: The DNSServiceRef's underlying socket descriptor, or -1 on 664 * error. 665 */ 666 667 int DNSSD_API DNSServiceRefSockFD(DNSServiceRef sdRef); 668 669 670 /* DNSServiceProcessResult() 671 * 672 * Read a reply from the daemon, calling the appropriate application callback. This call will 673 * block until the daemon's response is received. Use DNSServiceRefSockFD() in 674 * conjunction with a run loop or select() to determine the presence of a response from the 675 * server before calling this function to process the reply without blocking. Call this function 676 * at any point if it is acceptable to block until the daemon's response arrives. Note that the 677 * client is responsible for ensuring that DNSServiceProcessResult() is called whenever there is 678 * a reply from the daemon - the daemon may terminate its connection with a client that does not 679 * process the daemon's responses. 680 * 681 * sdRef: A DNSServiceRef initialized by any of the DNSService calls 682 * that take a callback parameter. 683 * 684 * return value: Returns kDNSServiceErr_NoError on success, otherwise returns 685 * an error code indicating the specific failure that occurred. 686 */ 687 688 DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdRef); 689 690 691 /* DNSServiceRefDeallocate() 692 * 693 * Terminate a connection with the daemon and free memory associated with the DNSServiceRef. 694 * Any services or records registered with this DNSServiceRef will be deregistered. Any 695 * Browse, Resolve, or Query operations called with this reference will be terminated. 696 * 697 * Note: If the reference's underlying socket is used in a run loop or select() call, it should 698 * be removed BEFORE DNSServiceRefDeallocate() is called, as this function closes the reference's 699 * socket. 700 * 701 * Note: If the reference was initialized with DNSServiceCreateConnection(), any DNSRecordRefs 702 * created via this reference will be invalidated by this call - the resource records are 703 * deregistered, and their DNSRecordRefs may not be used in subsequent functions. Similarly, 704 * if the reference was initialized with DNSServiceRegister, and an extra resource record was 705 * added to the service via DNSServiceAddRecord(), the DNSRecordRef created by the Add() call 706 * is invalidated when this function is called - the DNSRecordRef may not be used in subsequent 707 * functions. 708 * 709 * Note: This call is to be used only with the DNSServiceRef defined by this API. It is 710 * not compatible with dns_service_discovery_ref objects defined in the legacy Mach-based 711 * DNSServiceDiscovery.h API. 712 * 713 * sdRef: A DNSServiceRef initialized by any of the DNSService calls. 714 * 715 */ 716 717 void DNSSD_API DNSServiceRefDeallocate(DNSServiceRef sdRef); 718 719 720 /********************************************************************************************* 721 * 722 * Domain Enumeration 723 * 724 *********************************************************************************************/ 725 726 /* DNSServiceEnumerateDomains() 727 * 728 * Asynchronously enumerate domains available for browsing and registration. 729 * 730 * The enumeration MUST be cancelled via DNSServiceRefDeallocate() when no more domains 731 * are to be found. 732 * 733 * Note that the names returned are (like all of DNS-SD) UTF-8 strings, 734 * and are escaped using standard DNS escaping rules. 735 * (See "Notes on DNS Name Escaping" earlier in this file for more details.) 736 * A graphical browser displaying a hierarchical tree-structured view should cut 737 * the names at the bare dots to yield individual labels, then de-escape each 738 * label according to the escaping rules, and then display the resulting UTF-8 text. 739 * 740 * DNSServiceDomainEnumReply Callback Parameters: 741 * 742 * sdRef: The DNSServiceRef initialized by DNSServiceEnumerateDomains(). 743 * 744 * flags: Possible values are: 745 * kDNSServiceFlagsMoreComing 746 * kDNSServiceFlagsAdd 747 * kDNSServiceFlagsDefault 748 * 749 * interfaceIndex: Specifies the interface on which the domain exists. (The index for a given 750 * interface is determined via the if_nametoindex() family of calls.) 751 * 752 * errorCode: Will be kDNSServiceErr_NoError (0) on success, otherwise indicates 753 * the failure that occurred (other parameters are undefined if errorCode is nonzero). 754 * 755 * replyDomain: The name of the domain. 756 * 757 * context: The context pointer passed to DNSServiceEnumerateDomains. 758 * 759 */ 760 761 typedef void (DNSSD_API *DNSServiceDomainEnumReply) 762 ( 763 DNSServiceRef sdRef, 764 DNSServiceFlags flags, 765 uint32_t interfaceIndex, 766 DNSServiceErrorType errorCode, 767 const char *replyDomain, 768 void *context 769 ); 770 771 772 /* DNSServiceEnumerateDomains() Parameters: 773 * 774 * sdRef: A pointer to an uninitialized DNSServiceRef. If the call succeeds 775 * then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError, 776 * and the enumeration operation will run indefinitely until the client 777 * terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate(). 778 * 779 * flags: Possible values are: 780 * kDNSServiceFlagsBrowseDomains to enumerate domains recommended for browsing. 781 * kDNSServiceFlagsRegistrationDomains to enumerate domains recommended 782 * for registration. 783 * 784 * interfaceIndex: If non-zero, specifies the interface on which to look for domains. 785 * (the index for a given interface is determined via the if_nametoindex() 786 * family of calls.) Most applications will pass 0 to enumerate domains on 787 * all interfaces. See "Constants for specifying an interface index" for more details. 788 * 789 * callBack: The function to be called when a domain is found or the call asynchronously 790 * fails. 791 * 792 * context: An application context pointer which is passed to the callback function 793 * (may be NULL). 794 * 795 * return value: Returns kDNSServiceErr_NoError on success (any subsequent, asynchronous 796 * errors are delivered to the callback), otherwise returns an error code indicating 797 * the error that occurred (the callback is not invoked and the DNSServiceRef 798 * is not initialized). 799 */ 800 801 DNSServiceErrorType DNSSD_API DNSServiceEnumerateDomains 802 ( 803 DNSServiceRef *sdRef, 804 DNSServiceFlags flags, 805 uint32_t interfaceIndex, 806 DNSServiceDomainEnumReply callBack, 807 void *context /* may be NULL */ 808 ); 809 810 811 /********************************************************************************************* 812 * 813 * Service Registration 814 * 815 *********************************************************************************************/ 816 817 /* Register a service that is discovered via Browse() and Resolve() calls. 818 * 819 * DNSServiceRegisterReply() Callback Parameters: 820 * 821 * sdRef: The DNSServiceRef initialized by DNSServiceRegister(). 822 * 823 * flags: When a name is successfully registered, the callback will be 824 * invoked with the kDNSServiceFlagsAdd flag set. When Wide-Area 825 * DNS-SD is in use, it is possible for a single service to get 826 * more than one success callback (e.g. one in the "local" multicast 827 * DNS domain, and another in a wide-area unicast DNS domain). 828 * If a successfully-registered name later suffers a name conflict 829 * or similar problem and has to be deregistered, the callback will 830 * be invoked with the kDNSServiceFlagsAdd flag not set. The callback 831 * is *not* invoked in the case where the caller explicitly terminates 832 * the service registration by calling DNSServiceRefDeallocate(ref); 833 * 834 * errorCode: Will be kDNSServiceErr_NoError on success, otherwise will 835 * indicate the failure that occurred (including name conflicts, 836 * if the kDNSServiceFlagsNoAutoRename flag was used when registering.) 837 * Other parameters are undefined if errorCode is nonzero. 838 * 839 * name: The service name registered (if the application did not specify a name in 840 * DNSServiceRegister(), this indicates what name was automatically chosen). 841 * 842 * regtype: The type of service registered, as it was passed to the callout. 843 * 844 * domain: The domain on which the service was registered (if the application did not 845 * specify a domain in DNSServiceRegister(), this indicates the default domain 846 * on which the service was registered). 847 * 848 * context: The context pointer that was passed to the callout. 849 * 850 */ 851 852 typedef void (DNSSD_API *DNSServiceRegisterReply) 853 ( 854 DNSServiceRef sdRef, 855 DNSServiceFlags flags, 856 DNSServiceErrorType errorCode, 857 const char *name, 858 const char *regtype, 859 const char *domain, 860 void *context 861 ); 862 863 864 /* DNSServiceRegister() Parameters: 865 * 866 * sdRef: A pointer to an uninitialized DNSServiceRef. If the call succeeds 867 * then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError, 868 * and the registration will remain active indefinitely until the client 869 * terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate(). 870 * 871 * interfaceIndex: If non-zero, specifies the interface on which to register the service 872 * (the index for a given interface is determined via the if_nametoindex() 873 * family of calls.) Most applications will pass 0 to register on all 874 * available interfaces. See "Constants for specifying an interface index" for more details. 875 * 876 * flags: Indicates the renaming behavior on name conflict (most applications 877 * will pass 0). See flag definitions above for details. 878 * 879 * name: If non-NULL, specifies the service name to be registered. 880 * Most applications will not specify a name, in which case the computer 881 * name is used (this name is communicated to the client via the callback). 882 * If a name is specified, it must be 1-63 bytes of UTF-8 text. 883 * If the name is longer than 63 bytes it will be automatically truncated 884 * to a legal length, unless the NoAutoRename flag is set, 885 * in which case kDNSServiceErr_BadParam will be returned. 886 * 887 * regtype: The service type followed by the protocol, separated by a dot 888 * (e.g. "_ftp._tcp"). The service type must be an underscore, followed 889 * by 1-14 characters, which may be letters, digits, or hyphens. 890 * The transport protocol must be "_tcp" or "_udp". New service types 891 * should be registered at <http://www.dns-sd.org/ServiceTypes.html>. 892 * 893 * Additional subtypes of the primary service type (where a service 894 * type has defined subtypes) follow the primary service type in a 895 * comma-separated list, with no additional spaces, e.g. 896 * "_primarytype._tcp,_subtype1,_subtype2,_subtype3" 897 * Subtypes provide a mechanism for filtered browsing: A client browsing 898 * for "_primarytype._tcp" will discover all instances of this type; 899 * a client browsing for "_primarytype._tcp,_subtype2" will discover only 900 * those instances that were registered with "_subtype2" in their list of 901 * registered subtypes. 902 * 903 * The subtype mechanism can be illustrated with some examples using the 904 * dns-sd command-line tool: 905 * 906 * % dns-sd -R Simple _test._tcp "" 1001 & 907 * % dns-sd -R Better _test._tcp,HasFeatureA "" 1002 & 908 * % dns-sd -R Best _test._tcp,HasFeatureA,HasFeatureB "" 1003 & 909 * 910 * Now: 911 * % dns-sd -B _test._tcp # will find all three services 912 * % dns-sd -B _test._tcp,HasFeatureA # finds "Better" and "Best" 913 * % dns-sd -B _test._tcp,HasFeatureB # finds only "Best" 914 * 915 * domain: If non-NULL, specifies the domain on which to advertise the service. 916 * Most applications will not specify a domain, instead automatically 917 * registering in the default domain(s). 918 * 919 * host: If non-NULL, specifies the SRV target host name. Most applications 920 * will not specify a host, instead automatically using the machine's 921 * default host name(s). Note that specifying a non-NULL host does NOT 922 * create an address record for that host - the application is responsible 923 * for ensuring that the appropriate address record exists, or creating it 924 * via DNSServiceRegisterRecord(). 925 * 926 * port: The port, in network byte order, on which the service accepts connections. 927 * Pass 0 for a "placeholder" service (i.e. a service that will not be discovered 928 * by browsing, but will cause a name conflict if another client tries to 929 * register that same name). Most clients will not use placeholder services. 930 * 931 * txtLen: The length of the txtRecord, in bytes. Must be zero if the txtRecord is NULL. 932 * 933 * txtRecord: The TXT record rdata. A non-NULL txtRecord MUST be a properly formatted DNS 934 * TXT record, i.e. <length byte> <data> <length byte> <data> ... 935 * Passing NULL for the txtRecord is allowed as a synonym for txtLen=1, txtRecord="", 936 * i.e. it creates a TXT record of length one containing a single empty string. 937 * RFC 1035 doesn't allow a TXT record to contain *zero* strings, so a single empty 938 * string is the smallest legal DNS TXT record. 939 * As with the other parameters, the DNSServiceRegister call copies the txtRecord 940 * data; e.g. if you allocated the storage for the txtRecord parameter with malloc() 941 * then you can safely free that memory right after the DNSServiceRegister call returns. 942 * 943 * callBack: The function to be called when the registration completes or asynchronously 944 * fails. The client MAY pass NULL for the callback - The client will NOT be notified 945 * of the default values picked on its behalf, and the client will NOT be notified of any 946 * asynchronous errors (e.g. out of memory errors, etc.) that may prevent the registration 947 * of the service. The client may NOT pass the NoAutoRename flag if the callback is NULL. 948 * The client may still deregister the service at any time via DNSServiceRefDeallocate(). 949 * 950 * context: An application context pointer which is passed to the callback function 951 * (may be NULL). 952 * 953 * return value: Returns kDNSServiceErr_NoError on success (any subsequent, asynchronous 954 * errors are delivered to the callback), otherwise returns an error code indicating 955 * the error that occurred (the callback is never invoked and the DNSServiceRef 956 * is not initialized). 957 */ 958 959 DNSServiceErrorType DNSSD_API DNSServiceRegister 960 ( 961 DNSServiceRef *sdRef, 962 DNSServiceFlags flags, 963 uint32_t interfaceIndex, 964 const char *name, /* may be NULL */ 965 const char *regtype, 966 const char *domain, /* may be NULL */ 967 const char *host, /* may be NULL */ 968 uint16_t port, 969 uint16_t txtLen, 970 const void *txtRecord, /* may be NULL */ 971 DNSServiceRegisterReply callBack, /* may be NULL */ 972 void *context /* may be NULL */ 973 ); 974 975 976 /* DNSServiceAddRecord() 977 * 978 * Add a record to a registered service. The name of the record will be the same as the 979 * registered service's name. 980 * The record can later be updated or deregistered by passing the RecordRef initialized 981 * by this function to DNSServiceUpdateRecord() or DNSServiceRemoveRecord(). 982 * 983 * Note that the DNSServiceAddRecord/UpdateRecord/RemoveRecord are *NOT* thread-safe 984 * with respect to a single DNSServiceRef. If you plan to have multiple threads 985 * in your program simultaneously add, update, or remove records from the same 986 * DNSServiceRef, then it's the caller's responsibility to use a mutext lock 987 * or take similar appropriate precautions to serialize those calls. 988 * 989 * Parameters; 990 * 991 * sdRef: A DNSServiceRef initialized by DNSServiceRegister(). 992 * 993 * RecordRef: A pointer to an uninitialized DNSRecordRef. Upon succesfull completion of this 994 * call, this ref may be passed to DNSServiceUpdateRecord() or DNSServiceRemoveRecord(). 995 * If the above DNSServiceRef is passed to DNSServiceRefDeallocate(), RecordRef is also 996 * invalidated and may not be used further. 997 * 998 * flags: Currently ignored, reserved for future use. 999 * 1000 * rrtype: The type of the record (e.g. kDNSServiceType_TXT, kDNSServiceType_SRV, etc) 1001 * 1002 * rdlen: The length, in bytes, of the rdata. 1003 * 1004 * rdata: The raw rdata to be contained in the added resource record. 1005 * 1006 * ttl: The time to live of the resource record, in seconds. 1007 * Most clients should pass 0 to indicate that the system should 1008 * select a sensible default value. 1009 * 1010 * return value: Returns kDNSServiceErr_NoError on success, otherwise returns an 1011 * error code indicating the error that occurred (the RecordRef is not initialized). 1012 */ 1013 1014 DNSServiceErrorType DNSSD_API DNSServiceAddRecord 1015 ( 1016 DNSServiceRef sdRef, 1017 DNSRecordRef *RecordRef, 1018 DNSServiceFlags flags, 1019 uint16_t rrtype, 1020 uint16_t rdlen, 1021 const void *rdata, 1022 uint32_t ttl 1023 ); 1024 1025 1026 /* DNSServiceUpdateRecord 1027 * 1028 * Update a registered resource record. The record must either be: 1029 * - The primary txt record of a service registered via DNSServiceRegister() 1030 * - A record added to a registered service via DNSServiceAddRecord() 1031 * - An individual record registered by DNSServiceRegisterRecord() 1032 * 1033 * Parameters: 1034 * 1035 * sdRef: A DNSServiceRef that was initialized by DNSServiceRegister() 1036 * or DNSServiceCreateConnection(). 1037 * 1038 * RecordRef: A DNSRecordRef initialized by DNSServiceAddRecord, or NULL to update the 1039 * service's primary txt record. 1040 * 1041 * flags: Currently ignored, reserved for future use. 1042 * 1043 * rdlen: The length, in bytes, of the new rdata. 1044 * 1045 * rdata: The new rdata to be contained in the updated resource record. 1046 * 1047 * ttl: The time to live of the updated resource record, in seconds. 1048 * Most clients should pass 0 to indicate that the system should 1049 * select a sensible default value. 1050 * 1051 * return value: Returns kDNSServiceErr_NoError on success, otherwise returns an 1052 * error code indicating the error that occurred. 1053 */ 1054 1055 DNSServiceErrorType DNSSD_API DNSServiceUpdateRecord 1056 ( 1057 DNSServiceRef sdRef, 1058 DNSRecordRef RecordRef, /* may be NULL */ 1059 DNSServiceFlags flags, 1060 uint16_t rdlen, 1061 const void *rdata, 1062 uint32_t ttl 1063 ); 1064 1065 1066 /* DNSServiceRemoveRecord 1067 * 1068 * Remove a record previously added to a service record set via DNSServiceAddRecord(), or deregister 1069 * an record registered individually via DNSServiceRegisterRecord(). 1070 * 1071 * Parameters: 1072 * 1073 * sdRef: A DNSServiceRef initialized by DNSServiceRegister() (if the 1074 * record being removed was registered via DNSServiceAddRecord()) or by 1075 * DNSServiceCreateConnection() (if the record being removed was registered via 1076 * DNSServiceRegisterRecord()). 1077 * 1078 * recordRef: A DNSRecordRef initialized by a successful call to DNSServiceAddRecord() 1079 * or DNSServiceRegisterRecord(). 1080 * 1081 * flags: Currently ignored, reserved for future use. 1082 * 1083 * return value: Returns kDNSServiceErr_NoError on success, otherwise returns an 1084 * error code indicating the error that occurred. 1085 */ 1086 1087 DNSServiceErrorType DNSSD_API DNSServiceRemoveRecord 1088 ( 1089 DNSServiceRef sdRef, 1090 DNSRecordRef RecordRef, 1091 DNSServiceFlags flags 1092 ); 1093 1094 1095 /********************************************************************************************* 1096 * 1097 * Service Discovery 1098 * 1099 *********************************************************************************************/ 1100 1101 /* Browse for instances of a service. 1102 * 1103 * DNSServiceBrowseReply() Parameters: 1104 * 1105 * sdRef: The DNSServiceRef initialized by DNSServiceBrowse(). 1106 * 1107 * flags: Possible values are kDNSServiceFlagsMoreComing and kDNSServiceFlagsAdd. 1108 * See flag definitions for details. 1109 * 1110 * interfaceIndex: The interface on which the service is advertised. This index should 1111 * be passed to DNSServiceResolve() when resolving the service. 1112 * 1113 * errorCode: Will be kDNSServiceErr_NoError (0) on success, otherwise will 1114 * indicate the failure that occurred. Other parameters are undefined if 1115 * the errorCode is nonzero. 1116 * 1117 * serviceName: The discovered service name. This name should be displayed to the user, 1118 * and stored for subsequent use in the DNSServiceResolve() call. 1119 * 1120 * regtype: The service type, which is usually (but not always) the same as was passed 1121 * to DNSServiceBrowse(). One case where the discovered service type may 1122 * not be the same as the requested service type is when using subtypes: 1123 * The client may want to browse for only those ftp servers that allow 1124 * anonymous connections. The client will pass the string "_ftp._tcp,_anon" 1125 * to DNSServiceBrowse(), but the type of the service that's discovered 1126 * is simply "_ftp._tcp". The regtype for each discovered service instance 1127 * should be stored along with the name, so that it can be passed to 1128 * DNSServiceResolve() when the service is later resolved. 1129 * 1130 * domain: The domain of the discovered service instance. This may or may not be the 1131 * same as the domain that was passed to DNSServiceBrowse(). The domain for each 1132 * discovered service instance should be stored along with the name, so that 1133 * it can be passed to DNSServiceResolve() when the service is later resolved. 1134 * 1135 * context: The context pointer that was passed to the callout. 1136 * 1137 */ 1138 1139 typedef void (DNSSD_API *DNSServiceBrowseReply) 1140 ( 1141 DNSServiceRef sdRef, 1142 DNSServiceFlags flags, 1143 uint32_t interfaceIndex, 1144 DNSServiceErrorType errorCode, 1145 const char *serviceName, 1146 const char *regtype, 1147 const char *replyDomain, 1148 void *context 1149 ); 1150 1151 1152 /* DNSServiceBrowse() Parameters: 1153 * 1154 * sdRef: A pointer to an uninitialized DNSServiceRef. If the call succeeds 1155 * then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError, 1156 * and the browse operation will run indefinitely until the client 1157 * terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate(). 1158 * 1159 * flags: Currently ignored, reserved for future use. 1160 * 1161 * interfaceIndex: If non-zero, specifies the interface on which to browse for services 1162 * (the index for a given interface is determined via the if_nametoindex() 1163 * family of calls.) Most applications will pass 0 to browse on all available 1164 * interfaces. See "Constants for specifying an interface index" for more details. 1165 * 1166 * regtype: The service type being browsed for followed by the protocol, separated by a 1167 * dot (e.g. "_ftp._tcp"). The transport protocol must be "_tcp" or "_udp". 1168 * A client may optionally specify a single subtype to perform filtered browsing: 1169 * e.g. browsing for "_primarytype._tcp,_subtype" will discover only those 1170 * instances of "_primarytype._tcp" that were registered specifying "_subtype" 1171 * in their list of registered subtypes. 1172 * 1173 * domain: If non-NULL, specifies the domain on which to browse for services. 1174 * Most applications will not specify a domain, instead browsing on the 1175 * default domain(s). 1176 * 1177 * callBack: The function to be called when an instance of the service being browsed for 1178 * is found, or if the call asynchronously fails. 1179 * 1180 * context: An application context pointer which is passed to the callback function 1181 * (may be NULL). 1182 * 1183 * return value: Returns kDNSServiceErr_NoError on success (any subsequent, asynchronous 1184 * errors are delivered to the callback), otherwise returns an error code indicating 1185 * the error that occurred (the callback is not invoked and the DNSServiceRef 1186 * is not initialized). 1187 */ 1188 1189 DNSServiceErrorType DNSSD_API DNSServiceBrowse 1190 ( 1191 DNSServiceRef *sdRef, 1192 DNSServiceFlags flags, 1193 uint32_t interfaceIndex, 1194 const char *regtype, 1195 const char *domain, /* may be NULL */ 1196 DNSServiceBrowseReply callBack, 1197 void *context /* may be NULL */ 1198 ); 1199 1200 1201 /* DNSServiceResolve() 1202 * 1203 * Resolve a service name discovered via DNSServiceBrowse() to a target host name, port number, and 1204 * txt record. 1205 * 1206 * Note: Applications should NOT use DNSServiceResolve() solely for txt record monitoring - use 1207 * DNSServiceQueryRecord() instead, as it is more efficient for this task. 1208 * 1209 * Note: When the desired results have been returned, the client MUST terminate the resolve by calling 1210 * DNSServiceRefDeallocate(). 1211 * 1212 * Note: DNSServiceResolve() behaves correctly for typical services that have a single SRV record 1213 * and a single TXT record. To resolve non-standard services with multiple SRV or TXT records, 1214 * DNSServiceQueryRecord() should be used. 1215 * 1216 * DNSServiceResolveReply Callback Parameters: 1217 * 1218 * sdRef: The DNSServiceRef initialized by DNSServiceResolve(). 1219 * 1220 * flags: Possible values: kDNSServiceFlagsMoreComing 1221 * 1222 * interfaceIndex: The interface on which the service was resolved. 1223 * 1224 * errorCode: Will be kDNSServiceErr_NoError (0) on success, otherwise will 1225 * indicate the failure that occurred. Other parameters are undefined if 1226 * the errorCode is nonzero. 1227 * 1228 * fullname: The full service domain name, in the form <servicename>.<protocol>.<domain>. 1229 * (This name is escaped following standard DNS rules, making it suitable for 1230 * passing to standard system DNS APIs such as res_query(), or to the 1231 * special-purpose functions included in this API that take fullname parameters. 1232 * See "Notes on DNS Name Escaping" earlier in this file for more details.) 1233 * 1234 * hosttarget: The target hostname of the machine providing the service. This name can 1235 * be passed to functions like gethostbyname() to identify the host's IP address. 1236 * 1237 * port: The port, in network byte order, on which connections are accepted for this service. 1238 * 1239 * txtLen: The length of the txt record, in bytes. 1240 * 1241 * txtRecord: The service's primary txt record, in standard txt record format. 1242 * 1243 * context: The context pointer that was passed to the callout. 1244 * 1245 * NOTE: In earlier versions of this header file, the txtRecord parameter was declared "const char *" 1246 * This is incorrect, since it contains length bytes which are values in the range 0 to 255, not -128 to +127. 1247 * Depending on your compiler settings, this change may cause signed/unsigned mismatch warnings. 1248 * These should be fixed by updating your own callback function definition to match the corrected 1249 * function signature using "const unsigned char *txtRecord". Making this change may also fix inadvertent 1250 * bugs in your callback function, where it could have incorrectly interpreted a length byte with value 250 1251 * as being -6 instead, with various bad consequences ranging from incorrect operation to software crashes. 1252 * If you need to maintain portable code that will compile cleanly with both the old and new versions of 1253 * this header file, you should update your callback function definition to use the correct unsigned value, 1254 * and then in the place where you pass your callback function to DNSServiceResolve(), use a cast to eliminate 1255 * the compiler warning, e.g.: 1256 * DNSServiceResolve(sd, flags, index, name, regtype, domain, (DNSServiceResolveReply)MyCallback, context); 1257 * This will ensure that your code compiles cleanly without warnings (and more importantly, works correctly) 1258 * with both the old header and with the new corrected version. 1259 * 1260 */ 1261 1262 typedef void (DNSSD_API *DNSServiceResolveReply) 1263 ( 1264 DNSServiceRef sdRef, 1265 DNSServiceFlags flags, 1266 uint32_t interfaceIndex, 1267 DNSServiceErrorType errorCode, 1268 const char *fullname, 1269 const char *hosttarget, 1270 uint16_t port, 1271 uint16_t txtLen, 1272 const unsigned char *txtRecord, 1273 void *context 1274 ); 1275 1276 1277 /* DNSServiceResolve() Parameters 1278 * 1279 * sdRef: A pointer to an uninitialized DNSServiceRef. If the call succeeds 1280 * then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError, 1281 * and the resolve operation will run indefinitely until the client 1282 * terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate(). 1283 * 1284 * flags: Specifying kDNSServiceFlagsForceMulticast will cause query to be 1285 * performed with a link-local mDNS query, even if the name is an 1286 * apparently non-local name (i.e. a name not ending in ".local.") 1287 * 1288 * interfaceIndex: The interface on which to resolve the service. If this resolve call is 1289 * as a result of a currently active DNSServiceBrowse() operation, then the 1290 * interfaceIndex should be the index reported in the DNSServiceBrowseReply 1291 * callback. If this resolve call is using information previously saved 1292 * (e.g. in a preference file) for later use, then use interfaceIndex 0, because 1293 * the desired service may now be reachable via a different physical interface. 1294 * See "Constants for specifying an interface index" for more details. 1295 * 1296 * name: The name of the service instance to be resolved, as reported to the 1297 * DNSServiceBrowseReply() callback. 1298 * 1299 * regtype: The type of the service instance to be resolved, as reported to the 1300 * DNSServiceBrowseReply() callback. 1301 * 1302 * domain: The domain of the service instance to be resolved, as reported to the 1303 * DNSServiceBrowseReply() callback. 1304 * 1305 * callBack: The function to be called when a result is found, or if the call 1306 * asynchronously fails. 1307 * 1308 * context: An application context pointer which is passed to the callback function 1309 * (may be NULL). 1310 * 1311 * return value: Returns kDNSServiceErr_NoError on success (any subsequent, asynchronous 1312 * errors are delivered to the callback), otherwise returns an error code indicating 1313 * the error that occurred (the callback is never invoked and the DNSServiceRef 1314 * is not initialized). 1315 */ 1316 1317 DNSServiceErrorType DNSSD_API DNSServiceResolve 1318 ( 1319 DNSServiceRef *sdRef, 1320 DNSServiceFlags flags, 1321 uint32_t interfaceIndex, 1322 const char *name, 1323 const char *regtype, 1324 const char *domain, 1325 DNSServiceResolveReply callBack, 1326 void *context /* may be NULL */ 1327 ); 1328 1329 1330 /********************************************************************************************* 1331 * 1332 * Querying Individual Specific Records 1333 * 1334 *********************************************************************************************/ 1335 1336 /* DNSServiceQueryRecord 1337 * 1338 * Query for an arbitrary DNS record. 1339 * 1340 * DNSServiceQueryRecordReply() Callback Parameters: 1341 * 1342 * sdRef: The DNSServiceRef initialized by DNSServiceQueryRecord(). 1343 * 1344 * flags: Possible values are kDNSServiceFlagsMoreComing and 1345 * kDNSServiceFlagsAdd. The Add flag is NOT set for PTR records 1346 * with a ttl of 0, i.e. "Remove" events. 1347 * 1348 * interfaceIndex: The interface on which the query was resolved (the index for a given 1349 * interface is determined via the if_nametoindex() family of calls). 1350 * See "Constants for specifying an interface index" for more details. 1351 * 1352 * errorCode: Will be kDNSServiceErr_NoError on success, otherwise will 1353 * indicate the failure that occurred. Other parameters are undefined if 1354 * errorCode is nonzero. 1355 * 1356 * fullname: The resource record's full domain name. 1357 * 1358 * rrtype: The resource record's type (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc) 1359 * 1360 * rrclass: The class of the resource record (usually kDNSServiceClass_IN). 1361 * 1362 * rdlen: The length, in bytes, of the resource record rdata. 1363 * 1364 * rdata: The raw rdata of the resource record. 1365 * 1366 * ttl: If the client wishes to cache the result for performance reasons, 1367 * the TTL indicates how long the client may legitimately hold onto 1368 * this result, in seconds. After the TTL expires, the client should 1369 * consider the result no longer valid, and if it requires this data 1370 * again, it should be re-fetched with a new query. Of course, this 1371 * only applies to clients that cancel the asynchronous operation when 1372 * they get a result. Clients that leave the asynchronous operation 1373 * running can safely assume that the data remains valid until they 1374 * get another callback telling them otherwise. 1375 * 1376 * context: The context pointer that was passed to the callout. 1377 * 1378 */ 1379 1380 typedef void (DNSSD_API *DNSServiceQueryRecordReply) 1381 ( 1382 DNSServiceRef sdRef, 1383 DNSServiceFlags flags, 1384 uint32_t interfaceIndex, 1385 DNSServiceErrorType errorCode, 1386 const char *fullname, 1387 uint16_t rrtype, 1388 uint16_t rrclass, 1389 uint16_t rdlen, 1390 const void *rdata, 1391 uint32_t ttl, 1392 void *context 1393 ); 1394 1395 1396 /* DNSServiceQueryRecord() Parameters: 1397 * 1398 * sdRef: A pointer to an uninitialized DNSServiceRef. If the call succeeds 1399 * then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError, 1400 * and the query operation will run indefinitely until the client 1401 * terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate(). 1402 * 1403 * flags: kDNSServiceFlagsForceMulticast or kDNSServiceFlagsLongLivedQuery. 1404 * Pass kDNSServiceFlagsLongLivedQuery to create a "long-lived" unicast 1405 * query in a non-local domain. Without setting this flag, unicast queries 1406 * will be one-shot - that is, only answers available at the time of the call 1407 * will be returned. By setting this flag, answers (including Add and Remove 1408 * events) that become available after the initial call is made will generate 1409 * callbacks. This flag has no effect on link-local multicast queries. 1410 * 1411 * interfaceIndex: If non-zero, specifies the interface on which to issue the query 1412 * (the index for a given interface is determined via the if_nametoindex() 1413 * family of calls.) Passing 0 causes the name to be queried for on all 1414 * interfaces. See "Constants for specifying an interface index" for more details. 1415 * 1416 * fullname: The full domain name of the resource record to be queried for. 1417 * 1418 * rrtype: The numerical type of the resource record to be queried for 1419 * (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc) 1420 * 1421 * rrclass: The class of the resource record (usually kDNSServiceClass_IN). 1422 * 1423 * callBack: The function to be called when a result is found, or if the call 1424 * asynchronously fails. 1425 * 1426 * context: An application context pointer which is passed to the callback function 1427 * (may be NULL). 1428 * 1429 * return value: Returns kDNSServiceErr_NoError on success (any subsequent, asynchronous 1430 * errors are delivered to the callback), otherwise returns an error code indicating 1431 * the error that occurred (the callback is never invoked and the DNSServiceRef 1432 * is not initialized). 1433 */ 1434 1435 DNSServiceErrorType DNSSD_API DNSServiceQueryRecord 1436 ( 1437 DNSServiceRef *sdRef, 1438 DNSServiceFlags flags, 1439 uint32_t interfaceIndex, 1440 const char *fullname, 1441 uint16_t rrtype, 1442 uint16_t rrclass, 1443 DNSServiceQueryRecordReply callBack, 1444 void *context /* may be NULL */ 1445 ); 1446 1447 1448 /********************************************************************************************* 1449 * 1450 * Unified lookup of both IPv4 and IPv6 addresses for a fully qualified hostname 1451 * 1452 *********************************************************************************************/ 1453 1454 /* DNSServiceGetAddrInfo 1455 * 1456 * Queries for the IP address of a hostname by using either Multicast or Unicast DNS. 1457 * 1458 * DNSServiceGetAddrInfoReply() parameters: 1459 * 1460 * sdRef: The DNSServiceRef initialized by DNSServiceGetAddrInfo(). 1461 * 1462 * flags: Possible values are kDNSServiceFlagsMoreComing and 1463 * kDNSServiceFlagsAdd. 1464 * 1465 * interfaceIndex: The interface to which the answers pertain. 1466 * 1467 * errorCode: Will be kDNSServiceErr_NoError on success, otherwise will 1468 * indicate the failure that occurred. Other parameters are 1469 * undefined if errorCode is nonzero. 1470 * 1471 * hostname: The fully qualified domain name of the host to be queried for. 1472 * 1473 * address: IPv4 or IPv6 address. 1474 * 1475 * ttl: If the client wishes to cache the result for performance reasons, 1476 * the TTL indicates how long the client may legitimately hold onto 1477 * this result, in seconds. After the TTL expires, the client should 1478 * consider the result no longer valid, and if it requires this data 1479 * again, it should be re-fetched with a new query. Of course, this 1480 * only applies to clients that cancel the asynchronous operation when 1481 * they get a result. Clients that leave the asynchronous operation 1482 * running can safely assume that the data remains valid until they 1483 * get another callback telling them otherwise. 1484 * 1485 * context: The context pointer that was passed to the callout. 1486 * 1487 */ 1488 1489 typedef void (DNSSD_API *DNSServiceGetAddrInfoReply) 1490 ( 1491 DNSServiceRef sdRef, 1492 DNSServiceFlags flags, 1493 uint32_t interfaceIndex, 1494 DNSServiceErrorType errorCode, 1495 const char *hostname, 1496 const struct sockaddr *address, 1497 uint32_t ttl, 1498 void *context 1499 ); 1500 1501 1502 /* DNSServiceGetAddrInfo() Parameters: 1503 * 1504 * sdRef: A pointer to an uninitialized DNSServiceRef. If the call succeeds then it 1505 * initializes the DNSServiceRef, returns kDNSServiceErr_NoError, and the query 1506 * begins and will last indefinitely until the client terminates the query 1507 * by passing this DNSServiceRef to DNSServiceRefDeallocate(). 1508 * 1509 * flags: kDNSServiceFlagsForceMulticast or kDNSServiceFlagsLongLivedQuery. 1510 * Pass kDNSServiceFlagsLongLivedQuery to create a "long-lived" unicast 1511 * query in a non-local domain. Without setting this flag, unicast queries 1512 * will be one-shot - that is, only answers available at the time of the call 1513 * will be returned. By setting this flag, answers (including Add and Remove 1514 * events) that become available after the initial call is made will generate 1515 * callbacks. This flag has no effect on link-local multicast queries. 1516 * 1517 * interfaceIndex: The interface on which to issue the query. Passing 0 causes the query to be 1518 * sent on all active interfaces via Multicast or the primary interface via Unicast. 1519 * 1520 * protocol: Pass in kDNSServiceProtocol_IPv4 to look up IPv4 addresses, or kDNSServiceProtocol_IPv6 1521 * to look up IPv6 addresses, or both to look up both kinds. If neither flag is 1522 * set, the system will apply an intelligent heuristic, which is (currently) 1523 * that it will attempt to look up both, except: 1524 * 1525 * * If "hostname" is a wide-area unicast DNS hostname (i.e. not a ".local." name) 1526 * but this host has no routable IPv6 address, then the call will not try to 1527 * look up IPv6 addresses for "hostname", since any addresses it found would be 1528 * unlikely to be of any use anyway. Similarly, if this host has no routable 1529 * IPv4 address, the call will not try to look up IPv4 addresses for "hostname". 1530 * 1531 * * If "hostname" is a link-local multicast DNS hostname (i.e. a ".local." name) 1532 * but this host has no IPv6 address of any kind, then it will not try to look 1533 * up IPv6 addresses for "hostname". Similarly, if this host has no IPv4 address 1534 * of any kind, the call will not try to look up IPv4 addresses for "hostname". 1535 * 1536 * hostname: The fully qualified domain name of the host to be queried for. 1537 * 1538 * callBack: The function to be called when the query succeeds or fails asynchronously. 1539 * 1540 * context: An application context pointer which is passed to the callback function 1541 * (may be NULL). 1542 * 1543 * return value: Returns kDNSServiceErr_NoError on success (any subsequent, asynchronous 1544 * errors are delivered to the callback), otherwise returns an error code indicating 1545 * the error that occurred. 1546 */ 1547 1548 DNSServiceErrorType DNSSD_API DNSServiceGetAddrInfo 1549 ( 1550 DNSServiceRef *sdRef, 1551 DNSServiceFlags flags, 1552 uint32_t interfaceIndex, 1553 DNSServiceProtocol protocol, 1554 const char *hostname, 1555 DNSServiceGetAddrInfoReply callBack, 1556 void *context /* may be NULL */ 1557 ); 1558 1559 1560 /********************************************************************************************* 1561 * 1562 * Special Purpose Calls: 1563 * DNSServiceCreateConnection(), DNSServiceRegisterRecord(), DNSServiceReconfirmRecord() 1564 * (most applications will not use these) 1565 * 1566 *********************************************************************************************/ 1567 1568 /* DNSServiceCreateConnection() 1569 * 1570 * Create a connection to the daemon allowing efficient registration of 1571 * multiple individual records. 1572 * 1573 * Parameters: 1574 * 1575 * sdRef: A pointer to an uninitialized DNSServiceRef. Deallocating 1576 * the reference (via DNSServiceRefDeallocate()) severs the 1577 * connection and deregisters all records registered on this connection. 1578 * 1579 * return value: Returns kDNSServiceErr_NoError on success, otherwise returns 1580 * an error code indicating the specific failure that occurred (in which 1581 * case the DNSServiceRef is not initialized). 1582 */ 1583 1584 DNSServiceErrorType DNSSD_API DNSServiceCreateConnection(DNSServiceRef *sdRef); 1585 1586 1587 /* DNSServiceRegisterRecord 1588 * 1589 * Register an individual resource record on a connected DNSServiceRef. 1590 * 1591 * Note that name conflicts occurring for records registered via this call must be handled 1592 * by the client in the callback. 1593 * 1594 * DNSServiceRegisterRecordReply() parameters: 1595 * 1596 * sdRef: The connected DNSServiceRef initialized by 1597 * DNSServiceCreateConnection(). 1598 * 1599 * RecordRef: The DNSRecordRef initialized by DNSServiceRegisterRecord(). If the above 1600 * DNSServiceRef is passed to DNSServiceRefDeallocate(), this DNSRecordRef is 1601 * invalidated, and may not be used further. 1602 * 1603 * flags: Currently unused, reserved for future use. 1604 * 1605 * errorCode: Will be kDNSServiceErr_NoError on success, otherwise will 1606 * indicate the failure that occurred (including name conflicts.) 1607 * Other parameters are undefined if errorCode is nonzero. 1608 * 1609 * context: The context pointer that was passed to the callout. 1610 * 1611 */ 1612 1613 typedef void (DNSSD_API *DNSServiceRegisterRecordReply) 1614 ( 1615 DNSServiceRef sdRef, 1616 DNSRecordRef RecordRef, 1617 DNSServiceFlags flags, 1618 DNSServiceErrorType errorCode, 1619 void *context 1620 ); 1621 1622 1623 /* DNSServiceRegisterRecord() Parameters: 1624 * 1625 * sdRef: A DNSServiceRef initialized by DNSServiceCreateConnection(). 1626 * 1627 * RecordRef: A pointer to an uninitialized DNSRecordRef. Upon succesfull completion of this 1628 * call, this ref may be passed to DNSServiceUpdateRecord() or DNSServiceRemoveRecord(). 1629 * (To deregister ALL records registered on a single connected DNSServiceRef 1630 * and deallocate each of their corresponding DNSServiceRecordRefs, call 1631 * DNSServiceRefDeallocate()). 1632 * 1633 * flags: Possible values are kDNSServiceFlagsShared or kDNSServiceFlagsUnique 1634 * (see flag type definitions for details). 1635 * 1636 * interfaceIndex: If non-zero, specifies the interface on which to register the record 1637 * (the index for a given interface is determined via the if_nametoindex() 1638 * family of calls.) Passing 0 causes the record to be registered on all interfaces. 1639 * See "Constants for specifying an interface index" for more details. 1640 * 1641 * fullname: The full domain name of the resource record. 1642 * 1643 * rrtype: The numerical type of the resource record (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc) 1644 * 1645 * rrclass: The class of the resource record (usually kDNSServiceClass_IN) 1646 * 1647 * rdlen: Length, in bytes, of the rdata. 1648 * 1649 * rdata: A pointer to the raw rdata, as it is to appear in the DNS record. 1650 * 1651 * ttl: The time to live of the resource record, in seconds. 1652 * Most clients should pass 0 to indicate that the system should 1653 * select a sensible default value. 1654 * 1655 * callBack: The function to be called when a result is found, or if the call 1656 * asynchronously fails (e.g. because of a name conflict.) 1657 * 1658 * context: An application context pointer which is passed to the callback function 1659 * (may be NULL). 1660 * 1661 * return value: Returns kDNSServiceErr_NoError on success (any subsequent, asynchronous 1662 * errors are delivered to the callback), otherwise returns an error code indicating 1663 * the error that occurred (the callback is never invoked and the DNSRecordRef is 1664 * not initialized). 1665 */ 1666 1667 DNSServiceErrorType DNSSD_API DNSServiceRegisterRecord 1668 ( 1669 DNSServiceRef sdRef, 1670 DNSRecordRef *RecordRef, 1671 DNSServiceFlags flags, 1672 uint32_t interfaceIndex, 1673 const char *fullname, 1674 uint16_t rrtype, 1675 uint16_t rrclass, 1676 uint16_t rdlen, 1677 const void *rdata, 1678 uint32_t ttl, 1679 DNSServiceRegisterRecordReply callBack, 1680 void *context /* may be NULL */ 1681 ); 1682 1683 1684 /* DNSServiceReconfirmRecord 1685 * 1686 * Instruct the daemon to verify the validity of a resource record that appears 1687 * to be out of date (e.g. because TCP connection to a service's target failed.) 1688 * Causes the record to be flushed from the daemon's cache (as well as all other 1689 * daemons' caches on the network) if the record is determined to be invalid. 1690 * Use this routine conservatively. Reconfirming a record necessarily consumes 1691 * network bandwidth, so this should not be done indiscriminately. 1692 * 1693 * Parameters: 1694 * 1695 * flags: Pass kDNSServiceFlagsForce to force immediate deletion of record, 1696 * instead of after some number of reconfirmation queries have gone unanswered. 1697 * 1698 * interfaceIndex: Specifies the interface of the record in question. 1699 * The caller must specify the interface. 1700 * This API (by design) causes increased network traffic, so it requires 1701 * the caller to be precise about which record should be reconfirmed. 1702 * It is not possible to pass zero for the interface index to perform 1703 * a "wildcard" reconfirmation, where *all* matching records are reconfirmed. 1704 * 1705 * fullname: The resource record's full domain name. 1706 * 1707 * rrtype: The resource record's type (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc) 1708 * 1709 * rrclass: The class of the resource record (usually kDNSServiceClass_IN). 1710 * 1711 * rdlen: The length, in bytes, of the resource record rdata. 1712 * 1713 * rdata: The raw rdata of the resource record. 1714 * 1715 */ 1716 1717 DNSServiceErrorType DNSSD_API DNSServiceReconfirmRecord 1718 ( 1719 DNSServiceFlags flags, 1720 uint32_t interfaceIndex, 1721 const char *fullname, 1722 uint16_t rrtype, 1723 uint16_t rrclass, 1724 uint16_t rdlen, 1725 const void *rdata 1726 ); 1727 1728 1729 /********************************************************************************************* 1730 * 1731 * NAT Port Mapping 1732 * 1733 *********************************************************************************************/ 1734 1735 /* DNSServiceNATPortMappingCreate 1736 * 1737 * Request a port mapping in the NAT gateway, which maps a port on the local machine 1738 * to an external port on the NAT. 1739 * 1740 * The port mapping will be renewed indefinitely until the client process exits, or 1741 * explicitly terminates the port mapping request by calling DNSServiceRefDeallocate(). 1742 * The client callback will be invoked, informing the client of the NAT gateway's 1743 * external IP address and the external port that has been allocated for this client. 1744 * The client should then record this external IP address and port using whatever 1745 * directory service mechanism it is using to enable peers to connect to it. 1746 * (Clients advertising services using Wide-Area DNS-SD DO NOT need to use this API 1747 * -- when a client calls DNSServiceRegister() NAT mappings are automatically created 1748 * and the external IP address and port for the service are recorded in the global DNS. 1749 * Only clients using some directory mechanism other than Wide-Area DNS-SD need to use 1750 * this API to explicitly map their own ports.) 1751 * 1752 * It's possible that the client callback could be called multiple times, for example 1753 * if the NAT gateway's IP address changes, or if a configuration change results in a 1754 * different external port being mapped for this client. Over the lifetime of any long-lived 1755 * port mapping, the client should be prepared to handle these notifications of changes 1756 * in the environment, and should update its recorded address and/or port as appropriate. 1757 * 1758 * NOTE: There are two unusual aspects of how the DNSServiceNATPortMappingCreate API works, 1759 * which were intentionally designed to help simplify client code: 1760 * 1761 * 1. It's not an error to request a NAT mapping when the machine is not behind a NAT gateway. 1762 * In other NAT mapping APIs, if you request a NAT mapping and the machine is not behind a NAT 1763 * gateway, then the API returns an error code -- it can't get you a NAT mapping if there's no 1764 * NAT gateway. The DNSServiceNATPortMappingCreate API takes a different view. Working out 1765 * whether or not you need a NAT mapping can be tricky and non-obvious, particularly on 1766 * a machine with multiple active network interfaces. Rather than make every client recreate 1767 * this logic for deciding whether a NAT mapping is required, the PortMapping API does that 1768 * work for you. If the client calls the PortMapping API when the machine already has a 1769 * routable public IP address, then instead of complaining about it and giving an error, 1770 * the PortMapping API just invokes your callback, giving the machine's public address 1771 * and your own port number. This means you don't need to write code to work out whether 1772 * your client needs to call the PortMapping API -- just call it anyway, and if it wasn't 1773 * necessary, no harm is done: 1774 * 1775 * - If the machine already has a routable public IP address, then your callback 1776 * will just be invoked giving your own address and port. 1777 * - If a NAT mapping is required and obtained, then your callback will be invoked 1778 * giving you the external address and port. 1779 * - If a NAT mapping is required but not obtained from the local NAT gateway, 1780 * or the machine has no network connectivity, then your callback will be 1781 * invoked giving zero address and port. 1782 * 1783 * 2. In other NAT mapping APIs, if a laptop computer is put to sleep and woken up on a new 1784 * network, it's the client's job to notice this, and work out whether a NAT mapping 1785 * is required on the new network, and make a new NAT mapping request if necessary. 1786 * The DNSServiceNATPortMappingCreate API does this for you, automatically. 1787 * The client just needs to make one call to the PortMapping API, and its callback will 1788 * be invoked any time the mapping state changes. This property complements point (1) above. 1789 * If the client didn't make a NAT mapping request just because it determined that one was 1790 * not required at that particular moment in time, the client would then have to monitor 1791 * for network state changes to determine if a NAT port mapping later became necessary. 1792 * By unconditionally making a NAT mapping request, even when a NAT mapping not to be 1793 * necessary, the PortMapping API will then begin monitoring network state changes on behalf of 1794 * the client, and if a NAT mapping later becomes necessary, it will automatically create a NAT 1795 * mapping and inform the client with a new callback giving the new address and port information. 1796 * 1797 * DNSServiceNATPortMappingReply() parameters: 1798 * 1799 * sdRef: The DNSServiceRef initialized by DNSServiceNATPortMappingCreate(). 1800 * 1801 * flags: Currently unused, reserved for future use. 1802 * 1803 * interfaceIndex: The interface through which the NAT gateway is reached. 1804 * 1805 * errorCode: Will be kDNSServiceErr_NoError on success. 1806 * Will be kDNSServiceErr_DoubleNAT when the NAT gateway is itself behind one or 1807 * more layers of NAT, in which case the other parameters have the defined values. 1808 * For other failures, will indicate the failure that occurred, and the other 1809 * parameters are undefined. 1810 * 1811 * externalAddress: Four byte IPv4 address in network byte order. 1812 * 1813 * protocol: Will be kDNSServiceProtocol_UDP or kDNSServiceProtocol_TCP or both. 1814 * 1815 * internalPort: The port on the local machine that was mapped. 1816 * 1817 * externalPort: The actual external port in the NAT gateway that was mapped. 1818 * This is likely to be different than the requested external port. 1819 * 1820 * ttl: The lifetime of the NAT port mapping created on the gateway. 1821 * This controls how quickly stale mappings will be garbage-collected 1822 * if the client machine crashes, suffers a power failure, is disconnected 1823 * from the network, or suffers some other unfortunate demise which 1824 * causes it to vanish without explicitly removing its NAT port mapping. 1825 * It's possible that the ttl value will differ from the requested ttl value. 1826 * 1827 * context: The context pointer that was passed to the callout. 1828 * 1829 */ 1830 1831 typedef void (DNSSD_API *DNSServiceNATPortMappingReply) 1832 ( 1833 DNSServiceRef sdRef, 1834 DNSServiceFlags flags, 1835 uint32_t interfaceIndex, 1836 DNSServiceErrorType errorCode, 1837 uint32_t externalAddress, /* four byte IPv4 address in network byte order */ 1838 DNSServiceProtocol protocol, 1839 uint16_t internalPort, 1840 uint16_t externalPort, /* may be different than the requested port */ 1841 uint32_t ttl, /* may be different than the requested ttl */ 1842 void *context 1843 ); 1844 1845 1846 /* DNSServiceNATPortMappingCreate() Parameters: 1847 * 1848 * sdRef: A pointer to an uninitialized DNSServiceRef. If the call succeeds then it 1849 * initializes the DNSServiceRef, returns kDNSServiceErr_NoError, and the nat 1850 * port mapping will last indefinitely until the client terminates the port 1851 * mapping request by passing this DNSServiceRef to DNSServiceRefDeallocate(). 1852 * 1853 * flags: Currently ignored, reserved for future use. 1854 * 1855 * interfaceIndex: The interface on which to create port mappings in a NAT gateway. Passing 0 causes 1856 * the port mapping request to be sent on the primary interface. 1857 * 1858 * protocol: To request a port mapping, pass in kDNSServiceProtocol_UDP, or kDNSServiceProtocol_TCP, 1859 * or (kDNSServiceProtocol_UDP | kDNSServiceProtocol_TCP) to map both. 1860 * The local listening port number must also be specified in the internalPort parameter. 1861 * To just discover the NAT gateway's external IP address, pass zero for protocol, 1862 * internalPort, externalPort and ttl. 1863 * 1864 * internalPort: The port number in network byte order on the local machine which is listening for packets. 1865 * 1866 * externalPort: The requested external port in network byte order in the NAT gateway that you would 1867 * like to map to the internal port. Pass 0 if you don't care which external port is chosen for you. 1868 * 1869 * ttl: The requested renewal period of the NAT port mapping, in seconds. 1870 * If the client machine crashes, suffers a power failure, is disconnected from 1871 * the network, or suffers some other unfortunate demise which causes it to vanish 1872 * unexpectedly without explicitly removing its NAT port mappings, then the NAT gateway 1873 * will garbage-collect old stale NAT port mappings when their lifetime expires. 1874 * Requesting a short TTL causes such orphaned mappings to be garbage-collected 1875 * more promptly, but consumes system resources and network bandwidth with 1876 * frequent renewal packets to keep the mapping from expiring. 1877 * Requesting a long TTL is more efficient on the network, but in the event of the 1878 * client vanishing, stale NAT port mappings will not be garbage-collected as quickly. 1879 * Most clients should pass 0 to use a system-wide default value. 1880 * 1881 * callBack: The function to be called when the port mapping request succeeds or fails asynchronously. 1882 * 1883 * context: An application context pointer which is passed to the callback function 1884 * (may be NULL). 1885 * 1886 * return value: Returns kDNSServiceErr_NoError on success (any subsequent, asynchronous 1887 * errors are delivered to the callback), otherwise returns an error code indicating 1888 * the error that occurred. 1889 * 1890 * If you don't actually want a port mapped, and are just calling the API 1891 * because you want to find out the NAT's external IP address (e.g. for UI 1892 * display) then pass zero for protocol, internalPort, externalPort and ttl. 1893 */ 1894 1895 DNSServiceErrorType DNSSD_API DNSServiceNATPortMappingCreate 1896 ( 1897 DNSServiceRef *sdRef, 1898 DNSServiceFlags flags, 1899 uint32_t interfaceIndex, 1900 DNSServiceProtocol protocol, /* TCP and/or UDP */ 1901 uint16_t internalPort, /* network byte order */ 1902 uint16_t externalPort, /* network byte order */ 1903 uint32_t ttl, /* time to live in seconds */ 1904 DNSServiceNATPortMappingReply callBack, 1905 void *context /* may be NULL */ 1906 ); 1907 1908 1909 /********************************************************************************************* 1910 * 1911 * General Utility Functions 1912 * 1913 *********************************************************************************************/ 1914 1915 /* DNSServiceConstructFullName() 1916 * 1917 * Concatenate a three-part domain name (as returned by the above callbacks) into a 1918 * properly-escaped full domain name. Note that callbacks in the above functions ALREADY ESCAPE 1919 * strings where necessary. 1920 * 1921 * Parameters: 1922 * 1923 * fullName: A pointer to a buffer that where the resulting full domain name is to be written. 1924 * The buffer must be kDNSServiceMaxDomainName (1009) bytes in length to 1925 * accommodate the longest legal domain name without buffer overrun. 1926 * 1927 * service: The service name - any dots or backslashes must NOT be escaped. 1928 * May be NULL (to construct a PTR record name, e.g. 1929 * "_ftp._tcp.apple.com."). 1930 * 1931 * regtype: The service type followed by the protocol, separated by a dot 1932 * (e.g. "_ftp._tcp"). 1933 * 1934 * domain: The domain name, e.g. "apple.com.". Literal dots or backslashes, 1935 * if any, must be escaped, e.g. "1st\. Floor.apple.com." 1936 * 1937 * return value: Returns kDNSServiceErr_NoError (0) on success, kDNSServiceErr_BadParam on error. 1938 * 1939 */ 1940 1941 DNSServiceErrorType DNSSD_API DNSServiceConstructFullName 1942 ( 1943 char *fullName, 1944 const char *service, /* may be NULL */ 1945 const char *regtype, 1946 const char *domain 1947 ); 1948 1949 1950 /********************************************************************************************* 1951 * 1952 * TXT Record Construction Functions 1953 * 1954 *********************************************************************************************/ 1955 1956 /* 1957 * A typical calling sequence for TXT record construction is something like: 1958 * 1959 * Client allocates storage for TXTRecord data (e.g. declare buffer on the stack) 1960 * TXTRecordCreate(); 1961 * TXTRecordSetValue(); 1962 * TXTRecordSetValue(); 1963 * TXTRecordSetValue(); 1964 * ... 1965 * DNSServiceRegister( ... TXTRecordGetLength(), TXTRecordGetBytesPtr() ... ); 1966 * TXTRecordDeallocate(); 1967 * Explicitly deallocate storage for TXTRecord data (if not allocated on the stack) 1968 */ 1969 1970 1971 /* TXTRecordRef 1972 * 1973 * Opaque internal data type. 1974 * Note: Represents a DNS-SD TXT record. 1975 */ 1976 1977 typedef union _TXTRecordRef_t { char PrivateData[16]; char *ForceNaturalAlignment; } TXTRecordRef; 1978 1979 1980 /* TXTRecordCreate() 1981 * 1982 * Creates a new empty TXTRecordRef referencing the specified storage. 1983 * 1984 * If the buffer parameter is NULL, or the specified storage size is not 1985 * large enough to hold a key subsequently added using TXTRecordSetValue(), 1986 * then additional memory will be added as needed using malloc(). 1987 * 1988 * On some platforms, when memory is low, malloc() may fail. In this 1989 * case, TXTRecordSetValue() will return kDNSServiceErr_NoMemory, and this 1990 * error condition will need to be handled as appropriate by the caller. 1991 * 1992 * You can avoid the need to handle this error condition if you ensure 1993 * that the storage you initially provide is large enough to hold all 1994 * the key/value pairs that are to be added to the record. 1995 * The caller can precompute the exact length required for all of the 1996 * key/value pairs to be added, or simply provide a fixed-sized buffer 1997 * known in advance to be large enough. 1998 * A no-value (key-only) key requires (1 + key length) bytes. 1999 * A key with empty value requires (1 + key length + 1) bytes. 2000 * A key with non-empty value requires (1 + key length + 1 + value length). 2001 * For most applications, DNS-SD TXT records are generally 2002 * less than 100 bytes, so in most cases a simple fixed-sized 2003 * 256-byte buffer will be more than sufficient. 2004 * Recommended size limits for DNS-SD TXT Records are discussed in 2005 * <http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt> 2006 * 2007 * Note: When passing parameters to and from these TXT record APIs, 2008 * the key name does not include the '=' character. The '=' character 2009 * is the separator between the key and value in the on-the-wire 2010 * packet format; it is not part of either the key or the value. 2011 * 2012 * txtRecord: A pointer to an uninitialized TXTRecordRef. 2013 * 2014 * bufferLen: The size of the storage provided in the "buffer" parameter. 2015 * 2016 * buffer: Optional caller-supplied storage used to hold the TXTRecord data. 2017 * This storage must remain valid for as long as 2018 * the TXTRecordRef. 2019 */ 2020 2021 void DNSSD_API TXTRecordCreate 2022 ( 2023 TXTRecordRef *txtRecord, 2024 uint16_t bufferLen, 2025 void *buffer 2026 ); 2027 2028 2029 /* TXTRecordDeallocate() 2030 * 2031 * Releases any resources allocated in the course of preparing a TXT Record 2032 * using TXTRecordCreate()/TXTRecordSetValue()/TXTRecordRemoveValue(). 2033 * Ownership of the buffer provided in TXTRecordCreate() returns to the client. 2034 * 2035 * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate(). 2036 * 2037 */ 2038 2039 void DNSSD_API TXTRecordDeallocate 2040 ( 2041 TXTRecordRef *txtRecord 2042 ); 2043 2044 2045 /* TXTRecordSetValue() 2046 * 2047 * Adds a key (optionally with value) to a TXTRecordRef. If the "key" already 2048 * exists in the TXTRecordRef, then the current value will be replaced with 2049 * the new value. 2050 * Keys may exist in four states with respect to a given TXT record: 2051 * - Absent (key does not appear at all) 2052 * - Present with no value ("key" appears alone) 2053 * - Present with empty value ("key=" appears in TXT record) 2054 * - Present with non-empty value ("key=value" appears in TXT record) 2055 * For more details refer to "Data Syntax for DNS-SD TXT Records" in 2056 * <http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt> 2057 * 2058 * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate(). 2059 * 2060 * key: A null-terminated string which only contains printable ASCII 2061 * values (0x20-0x7E), excluding '=' (0x3D). Keys should be 2062 * 9 characters or fewer (not counting the terminating null). 2063 * 2064 * valueSize: The size of the value. 2065 * 2066 * value: Any binary value. For values that represent 2067 * textual data, UTF-8 is STRONGLY recommended. 2068 * For values that represent textual data, valueSize 2069 * should NOT include the terminating null (if any) 2070 * at the end of the string. 2071 * If NULL, then "key" will be added with no value. 2072 * If non-NULL but valueSize is zero, then "key=" will be 2073 * added with empty value. 2074 * 2075 * return value: Returns kDNSServiceErr_NoError on success. 2076 * Returns kDNSServiceErr_Invalid if the "key" string contains 2077 * illegal characters. 2078 * Returns kDNSServiceErr_NoMemory if adding this key would 2079 * exceed the available storage. 2080 */ 2081 2082 DNSServiceErrorType DNSSD_API TXTRecordSetValue 2083 ( 2084 TXTRecordRef *txtRecord, 2085 const char *key, 2086 uint8_t valueSize, /* may be zero */ 2087 const void *value /* may be NULL */ 2088 ); 2089 2090 2091 /* TXTRecordRemoveValue() 2092 * 2093 * Removes a key from a TXTRecordRef. The "key" must be an 2094 * ASCII string which exists in the TXTRecordRef. 2095 * 2096 * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate(). 2097 * 2098 * key: A key name which exists in the TXTRecordRef. 2099 * 2100 * return value: Returns kDNSServiceErr_NoError on success. 2101 * Returns kDNSServiceErr_NoSuchKey if the "key" does not 2102 * exist in the TXTRecordRef. 2103 */ 2104 2105 DNSServiceErrorType DNSSD_API TXTRecordRemoveValue 2106 ( 2107 TXTRecordRef *txtRecord, 2108 const char *key 2109 ); 2110 2111 2112 /* TXTRecordGetLength() 2113 * 2114 * Allows you to determine the length of the raw bytes within a TXTRecordRef. 2115 * 2116 * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate(). 2117 * 2118 * return value: Returns the size of the raw bytes inside a TXTRecordRef 2119 * which you can pass directly to DNSServiceRegister() or 2120 * to DNSServiceUpdateRecord(). 2121 * Returns 0 if the TXTRecordRef is empty. 2122 */ 2123 2124 uint16_t DNSSD_API TXTRecordGetLength 2125 ( 2126 const TXTRecordRef *txtRecord 2127 ); 2128 2129 2130 /* TXTRecordGetBytesPtr() 2131 * 2132 * Allows you to retrieve a pointer to the raw bytes within a TXTRecordRef. 2133 * 2134 * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate(). 2135 * 2136 * return value: Returns a pointer to the raw bytes inside the TXTRecordRef 2137 * which you can pass directly to DNSServiceRegister() or 2138 * to DNSServiceUpdateRecord(). 2139 */ 2140 2141 const void * DNSSD_API TXTRecordGetBytesPtr 2142 ( 2143 const TXTRecordRef *txtRecord 2144 ); 2145 2146 2147 /********************************************************************************************* 2148 * 2149 * TXT Record Parsing Functions 2150 * 2151 *********************************************************************************************/ 2152 2153 /* 2154 * A typical calling sequence for TXT record parsing is something like: 2155 * 2156 * Receive TXT record data in DNSServiceResolve() callback 2157 * if (TXTRecordContainsKey(txtLen, txtRecord, "key")) then do something 2158 * val1ptr = TXTRecordGetValuePtr(txtLen, txtRecord, "key1", &len1); 2159 * val2ptr = TXTRecordGetValuePtr(txtLen, txtRecord, "key2", &len2); 2160 * ... 2161 * memcpy(myval1, val1ptr, len1); 2162 * memcpy(myval2, val2ptr, len2); 2163 * ... 2164 * return; 2165 * 2166 * If you wish to retain the values after return from the DNSServiceResolve() 2167 * callback, then you need to copy the data to your own storage using memcpy() 2168 * or similar, as shown in the example above. 2169 * 2170 * If for some reason you need to parse a TXT record you built yourself 2171 * using the TXT record construction functions above, then you can do 2172 * that using TXTRecordGetLength and TXTRecordGetBytesPtr calls: 2173 * TXTRecordGetValue(TXTRecordGetLength(x), TXTRecordGetBytesPtr(x), key, &len); 2174 * 2175 * Most applications only fetch keys they know about from a TXT record and 2176 * ignore the rest. 2177 * However, some debugging tools wish to fetch and display all keys. 2178 * To do that, use the TXTRecordGetCount() and TXTRecordGetItemAtIndex() calls. 2179 */ 2180 2181 /* TXTRecordContainsKey() 2182 * 2183 * Allows you to determine if a given TXT Record contains a specified key. 2184 * 2185 * txtLen: The size of the received TXT Record. 2186 * 2187 * txtRecord: Pointer to the received TXT Record bytes. 2188 * 2189 * key: A null-terminated ASCII string containing the key name. 2190 * 2191 * return value: Returns 1 if the TXT Record contains the specified key. 2192 * Otherwise, it returns 0. 2193 */ 2194 2195 int DNSSD_API TXTRecordContainsKey 2196 ( 2197 uint16_t txtLen, 2198 const void *txtRecord, 2199 const char *key 2200 ); 2201 2202 2203 /* TXTRecordGetValuePtr() 2204 * 2205 * Allows you to retrieve the value for a given key from a TXT Record. 2206 * 2207 * txtLen: The size of the received TXT Record 2208 * 2209 * txtRecord: Pointer to the received TXT Record bytes. 2210 * 2211 * key: A null-terminated ASCII string containing the key name. 2212 * 2213 * valueLen: On output, will be set to the size of the "value" data. 2214 * 2215 * return value: Returns NULL if the key does not exist in this TXT record, 2216 * or exists with no value (to differentiate between 2217 * these two cases use TXTRecordContainsKey()). 2218 * Returns pointer to location within TXT Record bytes 2219 * if the key exists with empty or non-empty value. 2220 * For empty value, valueLen will be zero. 2221 * For non-empty value, valueLen will be length of value data. 2222 */ 2223 2224 const void * DNSSD_API TXTRecordGetValuePtr 2225 ( 2226 uint16_t txtLen, 2227 const void *txtRecord, 2228 const char *key, 2229 uint8_t *valueLen 2230 ); 2231 2232 2233 /* TXTRecordGetCount() 2234 * 2235 * Returns the number of keys stored in the TXT Record. The count 2236 * can be used with TXTRecordGetItemAtIndex() to iterate through the keys. 2237 * 2238 * txtLen: The size of the received TXT Record. 2239 * 2240 * txtRecord: Pointer to the received TXT Record bytes. 2241 * 2242 * return value: Returns the total number of keys in the TXT Record. 2243 * 2244 */ 2245 2246 uint16_t DNSSD_API TXTRecordGetCount 2247 ( 2248 uint16_t txtLen, 2249 const void *txtRecord 2250 ); 2251 2252 2253 /* TXTRecordGetItemAtIndex() 2254 * 2255 * Allows you to retrieve a key name and value pointer, given an index into 2256 * a TXT Record. Legal index values range from zero to TXTRecordGetCount()-1. 2257 * It's also possible to iterate through keys in a TXT record by simply 2258 * calling TXTRecordGetItemAtIndex() repeatedly, beginning with index zero 2259 * and increasing until TXTRecordGetItemAtIndex() returns kDNSServiceErr_Invalid. 2260 * 2261 * On return: 2262 * For keys with no value, *value is set to NULL and *valueLen is zero. 2263 * For keys with empty value, *value is non-NULL and *valueLen is zero. 2264 * For keys with non-empty value, *value is non-NULL and *valueLen is non-zero. 2265 * 2266 * txtLen: The size of the received TXT Record. 2267 * 2268 * txtRecord: Pointer to the received TXT Record bytes. 2269 * 2270 * itemIndex: An index into the TXT Record. 2271 * 2272 * keyBufLen: The size of the string buffer being supplied. 2273 * 2274 * key: A string buffer used to store the key name. 2275 * On return, the buffer contains a null-terminated C string 2276 * giving the key name. DNS-SD TXT keys are usually 2277 * 9 characters or fewer. To hold the maximum possible 2278 * key name, the buffer should be 256 bytes long. 2279 * 2280 * valueLen: On output, will be set to the size of the "value" data. 2281 * 2282 * value: On output, *value is set to point to location within TXT 2283 * Record bytes that holds the value data. 2284 * 2285 * return value: Returns kDNSServiceErr_NoError on success. 2286 * Returns kDNSServiceErr_NoMemory if keyBufLen is too short. 2287 * Returns kDNSServiceErr_Invalid if index is greater than 2288 * TXTRecordGetCount()-1. 2289 */ 2290 2291 DNSServiceErrorType DNSSD_API TXTRecordGetItemAtIndex 2292 ( 2293 uint16_t txtLen, 2294 const void *txtRecord, 2295 uint16_t itemIndex, 2296 uint16_t keyBufLen, 2297 char *key, 2298 uint8_t *valueLen, 2299 const void **value 2300 ); 2301 2302 #ifdef __APPLE_API_PRIVATE 2303 2304 /* 2305 * Mac OS X specific functionality 2306 * 3rd party clients of this API should not depend on future support or availability of this routine 2307 */ 2308 2309 /* DNSServiceSetDefaultDomainForUser() 2310 * 2311 * Set the default domain for the caller's UID. Future browse and registration 2312 * calls by this user that do not specify an explicit domain will browse and 2313 * register in this wide-area domain in addition to .local. In addition, this 2314 * domain will be returned as a Browse domain via domain enumeration calls. 2315 * 2316 * Parameters: 2317 * 2318 * flags: Pass kDNSServiceFlagsAdd to add a domain for a user. Call without 2319 * this flag set to clear a previously added domain. 2320 * 2321 * domain: The domain to be used for the caller's UID. 2322 * 2323 * return value: Returns kDNSServiceErr_NoError on success, otherwise returns 2324 * an error code indicating the error that occurred. 2325 */ 2326 2327 DNSServiceErrorType DNSSD_API DNSServiceSetDefaultDomainForUser 2328 ( 2329 DNSServiceFlags flags, 2330 const char *domain 2331 ); 2332 2333 /* Symbol defined to tell System Configuration Framework where to look in the Dynamic Store 2334 * for the list of PrivateDNS domains that need to be handed off to mDNSResponder 2335 * (the complete key is "State:/Network/PrivateDNS") 2336 */ 2337 #define kDNSServiceCompPrivateDNS "PrivateDNS" 2338 #define kDNSServiceCompMulticastDNS "MulticastDNS" 2339 2340 #endif //__APPLE_API_PRIVATE 2341 2342 #if !defined(__NetBSD__) 2343 /* Some C compiler cleverness. We can make the compiler check certain things for us, 2344 * and report errors at compile-time if anything is wrong. The usual way to do this would 2345 * be to use a run-time "if" statement or the conventional run-time "assert" mechanism, but 2346 * then you don't find out what's wrong until you run the software. This way, if the assertion 2347 * condition is false, the array size is negative, and the complier complains immediately. 2348 */ 2349 2350 struct CompileTimeAssertionChecks_DNS_SD 2351 { 2352 char assert0[(sizeof(union _TXTRecordRef_t) == 16) ? 1 : -1]; 2353 }; 2354 #endif 2355 2356 #ifdef __cplusplus 2357 } 2358 #endif 2359 2360 #endif /* _DNS_SD_H */ 2361