dns_sd.h 129 KB

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