From 941853354848fca4cc39bd72e6f34ca54443a9c8 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 4 Oct 2005 19:20:47 +0000 Subject: [PATCH] rename compat-bonjour to compat-libdns_sd part II git-svn-id: file:///home/lennart/svn/public/avahi/trunk@696 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- Makefile.am | 6 +- avahi-compat-bonjour.pc.in | 10 - avahi-compat-libdns_sd.pc.in | 10 + compat-libdns_sd/Makefile.am | 59 ++ compat-libdns_sd/compat.c | 1004 +++++++++++++++++++ compat-libdns_sd/dns_sd.h | 1670 ++++++++++++++++++++++++++++++++ compat-libdns_sd/funcs.txt | 32 + compat-libdns_sd/txt-test.c | 129 +++ compat-libdns_sd/txt.c | 491 ++++++++++ compat-libdns_sd/unsupported.c | 120 +++ compat-libdns_sd/warn.c | 85 ++ compat-libdns_sd/warn.h | 32 + configure.ac | 2 +- docs/TODO | 6 +- 14 files changed, 3640 insertions(+), 16 deletions(-) delete mode 100644 avahi-compat-bonjour.pc.in create mode 100644 avahi-compat-libdns_sd.pc.in create mode 100644 compat-libdns_sd/Makefile.am create mode 100644 compat-libdns_sd/compat.c create mode 100644 compat-libdns_sd/dns_sd.h create mode 100644 compat-libdns_sd/funcs.txt create mode 100644 compat-libdns_sd/txt-test.c create mode 100644 compat-libdns_sd/txt.c create mode 100644 compat-libdns_sd/unsupported.c create mode 100644 compat-libdns_sd/warn.c create mode 100644 compat-libdns_sd/warn.h diff --git a/Makefile.am b/Makefile.am index 6ada70a..ffbc6f4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -60,7 +60,7 @@ SUBDIRS = \ man \ tests \ service-type-database \ - compat-bonjour + compat-libdns_sd DX_INPUT = \ @@ -94,13 +94,13 @@ DX_EXAMPLE_PATH = $(srcdir)/examples DX_EXAMPLE_PATTERNS = *.c pkgconfigdir = $(libdir)/pkgconfig -pkgconfig_DATA = avahi-core.pc avahi-compat-bonjour.pc +pkgconfig_DATA = avahi-core.pc avahi-compat-libdns_sd.pc avahi-core.pc: avahi-core.pc.in sed -e 's,@prefix\@,$(prefix),g' \ -e 's,@PACKAGE_VERSION\@,$(PACKAGE_VERSION),g' $< > $@ -avahi-compat-bonjour.pc: avahi-compat-bonjour.pc.in +avahi-compat-libdns_sd.pc: avahi-compat-libdns_sd.pc.in sed -e 's,@prefix\@,$(prefix),g' \ -e 's,@PACKAGE_VERSION\@,$(PACKAGE_VERSION),g' $< > $@ diff --git a/avahi-compat-bonjour.pc.in b/avahi-compat-bonjour.pc.in deleted file mode 100644 index 14be662..0000000 --- a/avahi-compat-bonjour.pc.in +++ /dev/null @@ -1,10 +0,0 @@ -prefix=@prefix@ -exec_prefix=${prefix} -libdir=${exec_prefix}/lib -includedir=${prefix}/include/avahi-compat-bonjour/ - -Name: avahi-core -Description: Avahi Multicast DNS Responder (Apple Bonjour Compatibility) -Version: @PACKAGE_VERSION@ -Libs: -L${libdir} -lavahi-common -lavahi-client -lavahi-compat-bonjour -Cflags: -D_REENTRANT -I${includedir} diff --git a/avahi-compat-libdns_sd.pc.in b/avahi-compat-libdns_sd.pc.in new file mode 100644 index 0000000..52ef14e --- /dev/null +++ b/avahi-compat-libdns_sd.pc.in @@ -0,0 +1,10 @@ +prefix=@prefix@ +exec_prefix=${prefix} +libdir=${exec_prefix}/lib +includedir=${prefix}/include/avahi-compat-libdns_sd/ + +Name: avahi-compat-libdns_sd +Description: Avahi Multicast DNS Responder (libdns_sd Compatibility) +Version: @PACKAGE_VERSION@ +Libs: -L${libdir} -lavahi-common -lavahi-client -lavahi-compat-libdns_sd +Cflags: -D_REENTRANT -I${includedir} diff --git a/compat-libdns_sd/Makefile.am b/compat-libdns_sd/Makefile.am new file mode 100644 index 0000000..5a66743 --- /dev/null +++ b/compat-libdns_sd/Makefile.am @@ -0,0 +1,59 @@ +# $Id$ + +# This file is part of avahi. +# +# avahi is free software; you can redistribute it and/or modify it +# under the terms of the GNU Lesser General Public License as +# published by the Free Software Foundation; either version 2 of the +# License, or (at your option) any later version. +# +# avahi is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public +# License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with avahi; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +# USA. + +AM_CFLAGS=-I$(top_srcdir) + +# This cool debug trap works on i386/gcc only +AM_CFLAGS+='-DDEBUG_TRAP=__asm__("int $$3")' + +avahi_compat_libdns_sddir=$(includedir)/avahi-compat-libdns_sd + +avahi_compat_libdns_sd_HEADERS = dns_sd.h + +lib_LTLIBRARIES = libavahi-compat-libdns_sd.la + +noinst_PROGRAMS = txt-test + +libavahi_compat_libdns_sd_la_SOURCES = \ + dns_sd.h \ + compat.c \ + txt.c \ + unsupported.c \ + warn.c warn.h +libavahi_compat_libdns_sd_la_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) +libavahi_compat_libdns_sd_la_LDFLAGS = $(AM_LDFLAGS) -export-dynamic -version-info $(LIBAVAHI_COMPAT_LIBDNS_SD_VERSION_INFO) $(PTHREAD_LIBS) ../avahi-common/libavahi-common.la ../avahi-client/libavahi-client.la + +txt_test_SOURCES = \ + dns_sd.h \ + txt.c \ + txt-test.c \ + warn.c warn.h +txt_test_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) +txt_test_LDADD = $(AM_LDADD) ../avahi-common/libavahi-common.la + +# You can test the compatibility layer by sticking in mDNSResponder's +# dns-sd.c source here, naming it "libdns_sd-test.c" and running "make +# libdns_sd-test" manually. We do not distribute that file due to +# licensing restrictions. + +libdns_sd-test: libdns_sd-test.c libavahi-compat-libdns_sd.la + $(LIBTOOL) --mode=compile $(CC) $(CFLAGS) $(PTHREAD_CFLAGS) -o libdns_sd-test.o -c libdns_sd-test.c + $(LIBTOOL) --mode=link $(CC) $(CFLAGS) $(PTHREAD_CFLAGS) -o libdns_sd-test libdns_sd-test.o $(PTHREAD_LIBS) ../avahi-common/libavahi-common.la libavahi-compat-libdns_sd.la + +CLEANFILES = libdns_sd-test.o libdns_sd-test diff --git a/compat-libdns_sd/compat.c b/compat-libdns_sd/compat.c new file mode 100644 index 0000000..3e2eb9d --- /dev/null +++ b/compat-libdns_sd/compat.c @@ -0,0 +1,1004 @@ +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + avahi is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General + Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "warn.h" +#include "dns_sd.h" + +enum { + COMMAND_POLL = 'P', + COMMAND_QUIT = 'Q', + COMMAND_POLLED = 'D' +}; + +struct _DNSServiceRef_t { + int n_ref; + + AvahiSimplePoll *simple_poll; + + int thread_fd, main_fd; + + pthread_t thread; + int thread_running; + + pthread_mutex_t mutex; + + void *context; + DNSServiceBrowseReply service_browser_callback; + DNSServiceResolveReply service_resolver_callback; + DNSServiceDomainEnumReply domain_browser_callback; + DNSServiceRegisterReply service_register_callback; + + AvahiClient *client; + AvahiServiceBrowser *service_browser; + AvahiServiceResolver *service_resolver; + AvahiDomainBrowser *domain_browser; + + char *service_name, *service_name_chosen, *service_regtype, *service_domain, *service_host; + uint16_t service_port; + AvahiIfIndex service_interface; + AvahiStringList *service_txt; + + AvahiEntryGroup *entry_group; +}; + +#define ASSERT_SUCCESS(r) { int __ret = (r); assert(__ret == 0); } + +static DNSServiceErrorType map_error(int error) { + switch (error) { + case AVAHI_OK : + return kDNSServiceErr_NoError; + + case AVAHI_ERR_BAD_STATE : + return kDNSServiceErr_BadState; + + case AVAHI_ERR_INVALID_HOST_NAME: + case AVAHI_ERR_INVALID_DOMAIN_NAME: + case AVAHI_ERR_INVALID_TTL: + case AVAHI_ERR_IS_PATTERN: + case AVAHI_ERR_INVALID_RECORD: + case AVAHI_ERR_INVALID_SERVICE_NAME: + case AVAHI_ERR_INVALID_SERVICE_TYPE: + case AVAHI_ERR_INVALID_PORT: + case AVAHI_ERR_INVALID_KEY: + case AVAHI_ERR_INVALID_ADDRESS: + return kDNSServiceErr_BadParam; + + + case AVAHI_ERR_LOCAL_COLLISION: + return kDNSServiceErr_NameConflict; + + case AVAHI_ERR_TOO_MANY_CLIENTS: + case AVAHI_ERR_TOO_MANY_OBJECTS: + case AVAHI_ERR_TOO_MANY_ENTRIES: + case AVAHI_ERR_ACCESS_DENIED: + return kDNSServiceErr_Refused; + + case AVAHI_ERR_INVALID_OPERATION: + case AVAHI_ERR_INVALID_OBJECT: + return kDNSServiceErr_Invalid; + + case AVAHI_ERR_NO_MEMORY: + return kDNSServiceErr_NoMemory; + + case AVAHI_ERR_INVALID_INTERFACE: + case AVAHI_ERR_INVALID_PROTOCOL: + return kDNSServiceErr_BadInterfaceIndex; + + case AVAHI_ERR_INVALID_FLAGS: + return kDNSServiceErr_BadFlags; + + case AVAHI_ERR_NOT_FOUND: + return kDNSServiceErr_NoSuchName; + + case AVAHI_ERR_VERSION_MISMATCH: + return kDNSServiceErr_Incompatible; + + case AVAHI_ERR_NO_NETWORK: + case AVAHI_ERR_OS: + case AVAHI_ERR_INVALID_CONFIG: + case AVAHI_ERR_TIMEOUT: + case AVAHI_ERR_DBUS_ERROR: + case AVAHI_ERR_NOT_CONNECTED: + case AVAHI_ERR_NO_DAEMON: + break; + + } + + return kDNSServiceErr_Unknown; +} + +static int read_command(int fd) { + ssize_t r; + char command; + + assert(fd >= 0); + + if ((r = read(fd, &command, 1)) != 1) { + fprintf(stderr, __FILE__": read() failed: %s\n", r < 0 ? strerror(errno) : "EOF"); + return -1; + } + + return command; +} + +static int write_command(int fd, char reply) { + assert(fd >= 0); + + if (write(fd, &reply, 1) != 1) { + fprintf(stderr, __FILE__": write() failed: %s\n", strerror(errno)); + return -1; + } + + return 0; +} + +static int poll_func(struct pollfd *ufds, unsigned int nfds, int timeout, void *userdata) { + DNSServiceRef sdref = userdata; + int ret; + + assert(sdref); + + ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex)); + +/* fprintf(stderr, "pre-syscall\n"); */ + ret = poll(ufds, nfds, timeout); +/* fprintf(stderr, "post-syscall\n"); */ + + ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex)); + + return ret; +} + +static void * thread_func(void *data) { + DNSServiceRef sdref = data; + sigset_t mask; + + sigfillset(&mask); + pthread_sigmask(SIG_BLOCK, &mask, NULL); + + sdref->thread = pthread_self(); + sdref->thread_running = 1; + + for (;;) { + char command; + + if ((command = read_command(sdref->thread_fd)) < 0) + break; + +/* fprintf(stderr, "Command: %c\n", command); */ + + switch (command) { + + case COMMAND_POLL: + + ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex)); + + + if (avahi_simple_poll_run(sdref->simple_poll) < 0) { + fprintf(stderr, __FILE__": avahi_simple_poll_run() failed.\n"); + ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex)); + break; + } + + if (write_command(sdref->thread_fd, COMMAND_POLLED) < 0) { + ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex)); + break; + } + + ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex)); + + break; + + case COMMAND_QUIT: + return NULL; + } + + } + + return NULL; +} + +static DNSServiceRef sdref_new(void) { + int fd[2] = { -1, -1 }; + DNSServiceRef sdref = NULL; + pthread_mutexattr_t mutex_attr; + + if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) + goto fail; + + if (!(sdref = avahi_new(struct _DNSServiceRef_t, 1))) + goto fail; + + sdref->n_ref = 1; + sdref->thread_fd = fd[0]; + sdref->main_fd = fd[1]; + + sdref->client = NULL; + sdref->service_browser = NULL; + sdref->service_resolver = NULL; + sdref->domain_browser = NULL; + sdref->entry_group = NULL; + + sdref->service_name = sdref->service_name_chosen = sdref->service_regtype = sdref->service_domain = sdref->service_host = NULL; + sdref->service_txt = NULL; + + ASSERT_SUCCESS(pthread_mutexattr_init(&mutex_attr)); + pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE); + ASSERT_SUCCESS(pthread_mutex_init(&sdref->mutex, NULL)); + + sdref->thread_running = 0; + + if (!(sdref->simple_poll = avahi_simple_poll_new())) + goto fail; + + avahi_simple_poll_set_func(sdref->simple_poll, poll_func, sdref); + + /* Start simple poll */ + if (avahi_simple_poll_prepare(sdref->simple_poll, -1) < 0) + goto fail; + + /* Queue a initiall POLL command for the thread */ + if (write_command(sdref->main_fd, COMMAND_POLL) < 0) + goto fail; + + if (pthread_create(&sdref->thread, NULL, thread_func, sdref) != 0) + goto fail; + + sdref->thread_running = 1; + + return sdref; + +fail: + + if (sdref) + DNSServiceRefDeallocate(sdref); + + return NULL; +} + +static void sdref_free(DNSServiceRef sdref) { + assert(sdref); + + if (sdref->thread_running) { + write_command(sdref->main_fd, COMMAND_QUIT); + avahi_simple_poll_wakeup(sdref->simple_poll); + pthread_join(sdref->thread, NULL); + } + + if (sdref->client) + avahi_client_free(sdref->client); + + if (sdref->thread_fd >= 0) + close(sdref->thread_fd); + + if (sdref->main_fd >= 0) + close(sdref->main_fd); + + if (sdref->simple_poll) + avahi_simple_poll_free(sdref->simple_poll); + + pthread_mutex_destroy(&sdref->mutex); + + avahi_free(sdref->service_name); + avahi_free(sdref->service_name_chosen); + avahi_free(sdref->service_regtype); + avahi_free(sdref->service_domain); + avahi_free(sdref->service_host); + + avahi_string_list_free(sdref->service_txt); + + avahi_free(sdref); +} + +static void sdref_ref(DNSServiceRef sdref) { + assert(sdref); + assert(sdref->n_ref >= 1); + + sdref->n_ref++; +} + +static void sdref_unref(DNSServiceRef sdref) { + assert(sdref); + assert(sdref->n_ref >= 1); + + if (--(sdref->n_ref) <= 0) + sdref_free(sdref); +} + +int DNSSD_API DNSServiceRefSockFD(DNSServiceRef sdref) { + assert(sdref); + assert(sdref->n_ref >= 1); + + AVAHI_WARN_LINKAGE; + + return sdref->main_fd; +} + +DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdref) { + DNSServiceErrorType ret = kDNSServiceErr_Unknown; + + assert(sdref); + assert(sdref->n_ref >= 1); + + AVAHI_WARN_LINKAGE; + + sdref_ref(sdref); + + ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex)); + + /* Cleanup notification socket */ + if (read_command(sdref->main_fd) != COMMAND_POLLED) + goto finish; + + if (avahi_simple_poll_dispatch(sdref->simple_poll) < 0) + goto finish; + + if (sdref->n_ref > 1) /* Perhaps we should die */ + + /* Dispatch events */ + if (avahi_simple_poll_prepare(sdref->simple_poll, -1) < 0) + goto finish; + + if (sdref->n_ref > 1) + + /* Request the poll */ + if (write_command(sdref->main_fd, COMMAND_POLL) < 0) + goto finish; + + ret = kDNSServiceErr_NoError; + +finish: + + ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex)); + + sdref_unref(sdref); + + return ret; +} + +void DNSSD_API DNSServiceRefDeallocate(DNSServiceRef sdref) { + assert(sdref); + assert(sdref->n_ref >= 1); + + AVAHI_WARN_LINKAGE; + + sdref_unref(sdref); +} + +static void service_browser_callback( + AvahiServiceBrowser *b, + AvahiIfIndex interface, + AvahiProtocol protocol, + AvahiBrowserEvent event, + const char *name, + const char *type, + const char *domain, + AvahiLookupResultFlags flags, + void *userdata) { + + DNSServiceRef sdref = userdata; + + assert(b); + assert(sdref); + assert(sdref->n_ref >= 1); + + switch (event) { + case AVAHI_BROWSER_NEW: + sdref->service_browser_callback(sdref, kDNSServiceFlagsAdd, interface, kDNSServiceErr_NoError, name, type, domain, sdref->context); + break; + + case AVAHI_BROWSER_REMOVE: + sdref->service_browser_callback(sdref, 0, interface, kDNSServiceErr_NoError, name, type, domain, sdref->context); + break; + + case AVAHI_BROWSER_FAILURE: + sdref->service_browser_callback(sdref, 0, interface, kDNSServiceErr_Unknown, NULL, NULL, NULL, sdref->context); + break; + + case AVAHI_BROWSER_NOT_FOUND: + sdref->service_browser_callback(sdref, 0, interface, kDNSServiceErr_NoSuchName, NULL, NULL, NULL, sdref->context); + break; + + case AVAHI_BROWSER_CACHE_EXHAUSTED: + case AVAHI_BROWSER_ALL_FOR_NOW: + break; + } +} + +static void generic_client_callback(AvahiClient *s, AvahiClientState state, void* userdata) { + DNSServiceRef sdref = userdata; + + assert(s); + assert(sdref); + assert(sdref->n_ref >= 1); + + switch (state) { + case AVAHI_CLIENT_DISCONNECTED: { + + if (sdref->service_browser_callback) + sdref->service_browser_callback(sdref, 0, 0, kDNSServiceErr_Unknown, NULL, NULL, NULL, sdref->context); + else if (sdref->service_resolver_callback) + sdref->service_resolver_callback(sdref, 0, 0, kDNSServiceErr_Unknown, NULL, NULL, 0, 0, NULL, sdref->context); + else if (sdref->domain_browser_callback) + sdref->domain_browser_callback(sdref, 0, 0, kDNSServiceErr_Unknown, NULL, sdref->context); + + break; + } + + case AVAHI_CLIENT_S_RUNNING: + case AVAHI_CLIENT_S_COLLISION: + case AVAHI_CLIENT_S_INVALID: + case AVAHI_CLIENT_S_REGISTERING: + break; + } +} + +DNSServiceErrorType DNSSD_API DNSServiceBrowse( + DNSServiceRef *ret_sdref, + DNSServiceFlags flags, + uint32_t interface, + const char *regtype, + const char *domain, + DNSServiceBrowseReply callback, + void *context) { + + DNSServiceErrorType ret = kDNSServiceErr_Unknown; + int error; + DNSServiceRef sdref = NULL; + AvahiIfIndex ifindex; + + AVAHI_WARN_LINKAGE; + + assert(ret_sdref); + assert(regtype); + assert(domain); + assert(callback); + + if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0) + return kDNSServiceErr_Unsupported; + + if (!(sdref = sdref_new())) + return kDNSServiceErr_Unknown; + + sdref->context = context; + sdref->service_browser_callback = callback; + + ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex)); + + if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), generic_client_callback, sdref, &error))) { + ret = map_error(error); + goto finish; + } + + ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface; + + if (!(sdref->service_browser = avahi_service_browser_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, regtype, domain, 0, service_browser_callback, sdref))) { + ret = map_error(avahi_client_errno(sdref->client)); + goto finish; + } + + ret = kDNSServiceErr_NoError; + *ret_sdref = sdref; + +finish: + + ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex)); + + if (ret != kDNSServiceErr_NoError) + DNSServiceRefDeallocate(sdref); + + return ret; +} + +static void service_resolver_callback( + AvahiServiceResolver *r, + AvahiIfIndex interface, + AvahiProtocol protocol, + AvahiResolverEvent event, + const char *name, + const char *type, + const char *domain, + const char *host_name, + const AvahiAddress *a, + uint16_t port, + AvahiStringList *txt, + AvahiLookupResultFlags flags, + void *userdata) { + + DNSServiceRef sdref = userdata; + + assert(r); + assert(sdref); + assert(sdref->n_ref >= 1); + + switch (event) { + case AVAHI_RESOLVER_FOUND: { + + char full_name[kDNSServiceMaxDomainName]; + int ret; + char *p = NULL; + size_t l = 0; + + if ((p = avahi_new0(char, (l = avahi_string_list_serialize(txt, NULL, 0))+1))) + avahi_string_list_serialize(txt, p, l); + + ret = avahi_service_name_snprint(full_name, sizeof(full_name), name, type, domain); + assert(ret == AVAHI_OK); + + sdref->service_resolver_callback(sdref, 0, interface, kDNSServiceErr_NoError, full_name, host_name, htons(port), l, p, sdref->context); + + avahi_free(p); + break; + } + + case AVAHI_RESOLVER_TIMEOUT: + case AVAHI_RESOLVER_NOT_FOUND: + sdref->service_resolver_callback(sdref, 0, interface, kDNSServiceErr_NoSuchName, NULL, NULL, 0, 0, NULL, sdref->context); + break; + + case AVAHI_RESOLVER_FAILURE: + sdref->service_resolver_callback(sdref, 0, interface, kDNSServiceErr_Unknown, NULL, NULL, 0, 0, NULL, sdref->context); + + } +} + +DNSServiceErrorType DNSSD_API DNSServiceResolve( + DNSServiceRef *ret_sdref, + DNSServiceFlags flags, + uint32_t interface, + const char *name, + const char *regtype, + const char *domain, + DNSServiceResolveReply callback, + void *context) { + + DNSServiceErrorType ret = kDNSServiceErr_Unknown; + int error; + DNSServiceRef sdref = NULL; + AvahiIfIndex ifindex; + + AVAHI_WARN_LINKAGE; + + assert(ret_sdref); + assert(name); + assert(regtype); + assert(domain); + assert(callback); + + if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0) + return kDNSServiceErr_Unsupported; + + if (!(sdref = sdref_new())) + return kDNSServiceErr_Unknown; + + sdref->context = context; + sdref->service_resolver_callback = callback; + + ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex)); + + if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), generic_client_callback, sdref, &error))) { + ret = map_error(error); + goto finish; + } + + ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface; + + if (!(sdref->service_resolver = avahi_service_resolver_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, name, regtype, domain, AVAHI_PROTO_UNSPEC, 0, service_resolver_callback, sdref))) { + ret = map_error(avahi_client_errno(sdref->client)); + goto finish; + } + + + ret = kDNSServiceErr_NoError; + *ret_sdref = sdref; + +finish: + + ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex)); + + if (ret != kDNSServiceErr_NoError) + DNSServiceRefDeallocate(sdref); + + return ret; +} + +int DNSSD_API DNSServiceConstructFullName ( + char *fullName, + const char *service, + const char *regtype, + const char *domain) { + + AVAHI_WARN_LINKAGE; + + assert(fullName); + assert(regtype); + assert(domain); + + if (avahi_service_name_snprint(fullName, kDNSServiceMaxDomainName, service, regtype, domain) < 0) + return -1; + + return 0; +} + +static void domain_browser_callback( + AvahiDomainBrowser *b, + AvahiIfIndex interface, + AvahiProtocol protocol, + AvahiBrowserEvent event, + const char *domain, + AvahiLookupResultFlags flags, + void *userdata) { + + DNSServiceRef sdref = userdata; + + assert(b); + assert(sdref); + assert(sdref->n_ref >= 1); + + switch (event) { + case AVAHI_BROWSER_NEW: + sdref->domain_browser_callback(sdref, kDNSServiceFlagsAdd, interface, kDNSServiceErr_NoError, domain, sdref->context); + break; + + case AVAHI_BROWSER_REMOVE: + sdref->domain_browser_callback(sdref, 0, interface, kDNSServiceErr_NoError, domain, sdref->context); + break; + + case AVAHI_BROWSER_FAILURE: + sdref->domain_browser_callback(sdref, 0, interface, kDNSServiceErr_Unknown, domain, sdref->context); + break; + + case AVAHI_BROWSER_NOT_FOUND: + sdref->domain_browser_callback(sdref, 0, interface, kDNSServiceErr_NoSuchName, domain, sdref->context); + break; + + case AVAHI_BROWSER_CACHE_EXHAUSTED: + case AVAHI_BROWSER_ALL_FOR_NOW: + break; + } +} + +DNSServiceErrorType DNSSD_API DNSServiceEnumerateDomains( + DNSServiceRef *ret_sdref, + DNSServiceFlags flags, + uint32_t interface, + DNSServiceDomainEnumReply callback, + void *context) { + + DNSServiceErrorType ret = kDNSServiceErr_Unknown; + int error; + DNSServiceRef sdref = NULL; + AvahiIfIndex ifindex; + + AVAHI_WARN_LINKAGE; + + assert(ret_sdref); + assert(callback); + + if (interface == kDNSServiceInterfaceIndexLocalOnly || + (flags != kDNSServiceFlagsBrowseDomains && flags != kDNSServiceFlagsRegistrationDomains)) + return kDNSServiceErr_Unsupported; + + if (!(sdref = sdref_new())) + return kDNSServiceErr_Unknown; + + sdref->context = context; + sdref->domain_browser_callback = callback; + + ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex)); + + if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), generic_client_callback, sdref, &error))) { + ret = map_error(error); + goto finish; + } + + ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface; + + if (!(sdref->domain_browser = avahi_domain_browser_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, NULL, + flags == kDNSServiceFlagsRegistrationDomains ? AVAHI_DOMAIN_BROWSER_REGISTER : AVAHI_DOMAIN_BROWSER_BROWSE, + 0, domain_browser_callback, sdref))) { + ret = map_error(avahi_client_errno(sdref->client)); + goto finish; + } + + ret = kDNSServiceErr_NoError; + *ret_sdref = sdref; + +finish: + + ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex)); + + if (ret != kDNSServiceErr_NoError) + DNSServiceRefDeallocate(sdref); + + return ret; +} + +static void reg_report_error(DNSServiceRef sdref, DNSServiceErrorType error) { + assert(sdref); + assert(sdref->n_ref >= 1); + + assert(sdref->service_register_callback); + + sdref->service_register_callback( + sdref, 0, error, + sdref->service_name_chosen ? sdref->service_name_chosen : sdref->service_name, + sdref->service_regtype, + sdref->service_domain, + sdref->context); +} + +static int reg_create_service(DNSServiceRef sdref) { + int ret; + assert(sdref); + assert(sdref->n_ref >= 1); + + if ((ret = avahi_entry_group_add_service_strlst( + sdref->entry_group, + sdref->service_interface, + AVAHI_PROTO_UNSPEC, + 0, + sdref->service_name_chosen, + sdref->service_regtype, + sdref->service_domain, + sdref->service_host, + sdref->service_port, + sdref->service_txt)) < 0) + return ret; + + if ((ret = avahi_entry_group_commit(sdref->entry_group)) < 0) + return ret; + + return 0; +} + +static void reg_client_callback(AvahiClient *s, AvahiClientState state, void* userdata) { + DNSServiceRef sdref = userdata; + + assert(s); + assert(sdref); + assert(sdref->n_ref >= 1); + + /* We've not been setup completely */ + if (!sdref->entry_group) + return; + + switch (state) { + case AVAHI_CLIENT_DISCONNECTED: { + + reg_report_error(sdref, kDNSServiceErr_NoError); + break; + } + + case AVAHI_CLIENT_S_RUNNING: { + int ret; + + if (!sdref->service_name) { + const char *n; + /* If the service name is taken from the host name, copy that */ + + avahi_free(sdref->service_name_chosen); + + if (!(n = avahi_client_get_host_name(sdref->client))) { + reg_report_error(sdref, map_error(avahi_client_errno(sdref->client))); + return; + } + + if (!(sdref->service_name_chosen = avahi_strdup(n))) { + reg_report_error(sdref, kDNSServiceErr_NoMemory); + return; + } + } + + /* Register the service */ + + if ((ret = reg_create_service(sdref)) < 0) { + reg_report_error(sdref, map_error(ret)); + return; + } + + break; + } + + case AVAHI_CLIENT_S_COLLISION: + + /* Remove our entry */ + avahi_entry_group_reset(sdref->entry_group); + + break; + + case AVAHI_CLIENT_S_INVALID: + case AVAHI_CLIENT_S_REGISTERING: + /* Ignore */ + break; + } + +} + +static void reg_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, void *userdata) { + DNSServiceRef sdref = userdata; + + assert(g); + + switch (state) { + case AVAHI_ENTRY_GROUP_ESTABLISHED: + /* Inform the user */ + reg_report_error(sdref, kDNSServiceErr_NoError); + + break; + + case AVAHI_ENTRY_GROUP_COLLISION: { + char *n; + int ret; + + /* Remove our entry */ + avahi_entry_group_reset(sdref->entry_group); + + assert(sdref->service_name_chosen); + + /* Pick a new name */ + if (!(n = avahi_alternative_service_name(sdref->service_name_chosen))) { + reg_report_error(sdref, kDNSServiceErr_NoMemory); + return; + } + avahi_free(sdref->service_name_chosen); + sdref->service_name_chosen = n; + + /* Register the service with that new name */ + if ((ret = reg_create_service(sdref)) < 0) { + reg_report_error(sdref, map_error(ret)); + return; + } + + break; + } + + case AVAHI_ENTRY_GROUP_REGISTERING: + case AVAHI_ENTRY_GROUP_UNCOMMITED: + /* Ignore */ + break; + } +} + +DNSServiceErrorType DNSSD_API DNSServiceRegister ( + DNSServiceRef *ret_sdref, + DNSServiceFlags flags, + uint32_t interface, + const char *name, + const char *regtype, + const char *domain, + const char *host, + uint16_t port, + uint16_t txtLen, + const void *txtRecord, + DNSServiceRegisterReply callback, + void *context) { + + DNSServiceErrorType ret = kDNSServiceErr_Unknown; + int error; + DNSServiceRef sdref = NULL; + + AVAHI_WARN_LINKAGE; + + assert(ret_sdref); + assert(callback); + assert(regtype); + + if (interface == kDNSServiceInterfaceIndexLocalOnly || flags) + return kDNSServiceErr_Unsupported; + + if (!(sdref = sdref_new())) + return kDNSServiceErr_Unknown; + + sdref->context = context; + sdref->service_register_callback = callback; + + sdref->service_name = avahi_strdup(name); + sdref->service_regtype = avahi_strdup(regtype); + sdref->service_domain = avahi_strdup(domain); + sdref->service_host = avahi_strdup(host); + sdref->service_interface = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface; + sdref->service_port = ntohs(port); + sdref->service_txt = txtRecord ? avahi_string_list_parse(txtRecord, txtLen) : NULL; + + ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex)); + + if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), reg_client_callback, sdref, &error))) { + ret = map_error(error); + goto finish; + } + + if (!sdref->service_domain) { + const char *d; + + if (!(d = avahi_client_get_domain_name(sdref->client))) { + ret = map_error(avahi_client_errno(sdref->client)); + goto finish; + } + + if (!(sdref->service_domain = avahi_strdup(d))) { + ret = kDNSServiceErr_NoMemory; + goto finish; + } + } + + if (!(sdref->entry_group = avahi_entry_group_new(sdref->client, reg_entry_group_callback, sdref))) { + ret = map_error(avahi_client_errno(sdref->client)); + goto finish; + } + + if (avahi_client_get_state(sdref->client) == AVAHI_CLIENT_S_RUNNING) { + const char *n; + + if (sdref->service_name) + n = sdref->service_name; + else { + if (!(n = avahi_client_get_host_name(sdref->client))) { + ret = map_error(avahi_client_errno(sdref->client)); + goto finish; + } + } + + if (!(sdref->service_name_chosen = avahi_strdup(n))) { + ret = kDNSServiceErr_NoMemory; + goto finish; + } + + + if ((error = reg_create_service(sdref)) < 0) { + ret = map_error(error); + goto finish; + } + } + + ret = kDNSServiceErr_NoError; + *ret_sdref = sdref; + +finish: + + ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex)); + + if (ret != kDNSServiceErr_NoError) + DNSServiceRefDeallocate(sdref); + + return ret; +} + diff --git a/compat-libdns_sd/dns_sd.h b/compat-libdns_sd/dns_sd.h new file mode 100644 index 0000000..fa18cce --- /dev/null +++ b/compat-libdns_sd/dns_sd.h @@ -0,0 +1,1670 @@ +/* + * Copyright (c) 2003-2004, Apple Computer, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _DNS_SD_H +#define _DNS_SD_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* standard calling convention under Win32 is __stdcall */ +#if defined(_WIN32) +#define DNSSD_API __stdcall +#else +#define DNSSD_API +#endif + +#if defined(__FreeBSD_version) && (__FreeBSD_version < 500000) +/* stdint.h does not exist on FreeBSD 4.x; its types are defined in sys/types.h instead */ +#include +#elif defined(__sun__) +#include +#elif defined(_WIN32) +#include +#define _UNUSED +#define bzero(a, b) memset(a, 0, b) +#ifndef _MSL_STDINT_H +typedef UINT8 uint8_t; +typedef INT8 int8_t; +typedef UINT16 uint16_t; +typedef INT16 int16_t; +typedef UINT32 uint32_t; +typedef INT32 int32_t; +#endif +#else +#include +#endif + +/* DNSServiceRef, DNSRecordRef + * + * Opaque internal data types. + * Note: client is responsible for serializing access to these structures if + * they are shared between concurrent threads. + */ + +typedef struct _DNSServiceRef_t *DNSServiceRef; +typedef struct _DNSRecordRef_t *DNSRecordRef; + +/* General flags used in functions defined below */ +enum + { + kDNSServiceFlagsMoreComing = 0x1, + /* MoreComing indicates to a callback that at least one more result is + * queued and will be delivered following immediately after this one. + * Applications should not update their UI to display browse + * results when the MoreComing flag is set, because this would + * result in a great deal of ugly flickering on the screen. + * Applications should instead wait until until MoreComing is not set, + * and then update their UI. + * When MoreComing is not set, that doesn't mean there will be no more + * answers EVER, just that there are no more answers immediately + * available right now at this instant. If more answers become available + * in the future they will be delivered as usual. + */ + + kDNSServiceFlagsAdd = 0x2, + kDNSServiceFlagsDefault = 0x4, + /* Flags for domain enumeration and browse/query reply callbacks. + * "Default" applies only to enumeration and is only valid in + * conjuction with "Add". An enumeration callback with the "Add" + * flag NOT set indicates a "Remove", i.e. the domain is no longer + * valid. + */ + + kDNSServiceFlagsNoAutoRename = 0x8, + /* Flag for specifying renaming behavior on name conflict when registering + * non-shared records. By default, name conflicts are automatically handled + * by renaming the service. NoAutoRename overrides this behavior - with this + * flag set, name conflicts will result in a callback. The NoAutorename flag + * is only valid if a name is explicitly specified when registering a service + * (i.e. the default name is not used.) + */ + + kDNSServiceFlagsShared = 0x10, + kDNSServiceFlagsUnique = 0x20, + /* Flag for registering individual records on a connected + * DNSServiceRef. Shared indicates that there may be multiple records + * with this name on the network (e.g. PTR records). Unique indicates that the + * record's name is to be unique on the network (e.g. SRV records). + */ + + kDNSServiceFlagsBrowseDomains = 0x40, + kDNSServiceFlagsRegistrationDomains = 0x80, + /* Flags for specifying domain enumeration type in DNSServiceEnumerateDomains. + * BrowseDomains enumerates domains recommended for browsing, RegistrationDomains + * enumerates domains recommended for registration. + */ + + kDNSServiceFlagsLongLivedQuery = 0x100, + /* Flag for creating a long-lived unicast query for the DNSServiceQueryRecord call. */ + + kDNSServiceFlagsAllowRemoteQuery = 0x200, + /* Flag for creating a record for which we will answer remote queries + * (queries from hosts more than one hop away; hosts not directly connected to the local link). + */ + + kDNSServiceFlagsForceMulticast = 0x400 + /* Flag for signifying that a query or registration should be performed exclusively via multicast DNS, + * even for a name in a domain (e.g. foo.apple.com.) that would normally imply unicast DNS. + */ + }; + +/* + * The values for DNS Classes and Types are listed in RFC 1035, and are available + * on every OS in its DNS header file. Unfortunately every OS does not have the + * same header file containing DNS Class and Type constants, and the names of + * the constants are not consistent. For example, BIND 8 uses "T_A", + * BIND 9 uses "ns_t_a", Windows uses "DNS_TYPE_A", etc. + * For this reason, these constants are also listed here, so that code using + * the DNS-SD programming APIs can use these constants, so that the same code + * can compile on all our supported platforms. + */ + +enum + { + kDNSServiceClass_IN = 1 /* Internet */ + }; + +enum + { + kDNSServiceType_A = 1, /* Host address. */ + kDNSServiceType_NS = 2, /* Authoritative server. */ + kDNSServiceType_MD = 3, /* Mail destination. */ + kDNSServiceType_MF = 4, /* Mail forwarder. */ + kDNSServiceType_CNAME = 5, /* Canonical name. */ + kDNSServiceType_SOA = 6, /* Start of authority zone. */ + kDNSServiceType_MB = 7, /* Mailbox domain name. */ + kDNSServiceType_MG = 8, /* Mail group member. */ + kDNSServiceType_MR = 9, /* Mail rename name. */ + kDNSServiceType_NULL = 10, /* Null resource record. */ + kDNSServiceType_WKS = 11, /* Well known service. */ + kDNSServiceType_PTR = 12, /* Domain name pointer. */ + kDNSServiceType_HINFO = 13, /* Host information. */ + kDNSServiceType_MINFO = 14, /* Mailbox information. */ + kDNSServiceType_MX = 15, /* Mail routing information. */ + kDNSServiceType_TXT = 16, /* One or more text strings. */ + kDNSServiceType_RP = 17, /* Responsible person. */ + kDNSServiceType_AFSDB = 18, /* AFS cell database. */ + kDNSServiceType_X25 = 19, /* X_25 calling address. */ + kDNSServiceType_ISDN = 20, /* ISDN calling address. */ + kDNSServiceType_RT = 21, /* Router. */ + kDNSServiceType_NSAP = 22, /* NSAP address. */ + kDNSServiceType_NSAP_PTR = 23, /* Reverse NSAP lookup (deprecated). */ + kDNSServiceType_SIG = 24, /* Security signature. */ + kDNSServiceType_KEY = 25, /* Security key. */ + kDNSServiceType_PX = 26, /* X.400 mail mapping. */ + kDNSServiceType_GPOS = 27, /* Geographical position (withdrawn). */ + kDNSServiceType_AAAA = 28, /* Ip6 Address. */ + kDNSServiceType_LOC = 29, /* Location Information. */ + kDNSServiceType_NXT = 30, /* Next domain (security). */ + kDNSServiceType_EID = 31, /* Endpoint identifier. */ + kDNSServiceType_NIMLOC = 32, /* Nimrod Locator. */ + kDNSServiceType_SRV = 33, /* Server Selection. */ + kDNSServiceType_ATMA = 34, /* ATM Address */ + kDNSServiceType_NAPTR = 35, /* Naming Authority PoinTeR */ + kDNSServiceType_KX = 36, /* Key Exchange */ + kDNSServiceType_CERT = 37, /* Certification record */ + kDNSServiceType_A6 = 38, /* IPv6 address (deprecates AAAA) */ + kDNSServiceType_DNAME = 39, /* Non-terminal DNAME (for IPv6) */ + kDNSServiceType_SINK = 40, /* Kitchen sink (experimentatl) */ + kDNSServiceType_OPT = 41, /* EDNS0 option (meta-RR) */ + kDNSServiceType_TKEY = 249, /* Transaction key */ + kDNSServiceType_TSIG = 250, /* Transaction signature. */ + kDNSServiceType_IXFR = 251, /* Incremental zone transfer. */ + kDNSServiceType_AXFR = 252, /* Transfer zone of authority. */ + kDNSServiceType_MAILB = 253, /* Transfer mailbox records. */ + kDNSServiceType_MAILA = 254, /* Transfer mail agent records. */ + kDNSServiceType_ANY = 255 /* Wildcard match. */ + }; + + +/* possible error code values */ +enum + { + kDNSServiceErr_NoError = 0, + kDNSServiceErr_Unknown = -65537, /* 0xFFFE FFFF */ + kDNSServiceErr_NoSuchName = -65538, + kDNSServiceErr_NoMemory = -65539, + kDNSServiceErr_BadParam = -65540, + kDNSServiceErr_BadReference = -65541, + kDNSServiceErr_BadState = -65542, + kDNSServiceErr_BadFlags = -65543, + kDNSServiceErr_Unsupported = -65544, + kDNSServiceErr_NotInitialized = -65545, + kDNSServiceErr_AlreadyRegistered = -65547, + kDNSServiceErr_NameConflict = -65548, + kDNSServiceErr_Invalid = -65549, + kDNSServiceErr_Firewall = -65550, + kDNSServiceErr_Incompatible = -65551, /* client library incompatible with daemon */ + kDNSServiceErr_BadInterfaceIndex = -65552, + kDNSServiceErr_Refused = -65553, + kDNSServiceErr_NoSuchRecord = -65554, + kDNSServiceErr_NoAuth = -65555, + kDNSServiceErr_NoSuchKey = -65556, + kDNSServiceErr_NATTraversal = -65557, + kDNSServiceErr_DoubleNAT = -65558, + kDNSServiceErr_BadTime = -65559 + /* mDNS Error codes are in the range + * FFFE FF00 (-65792) to FFFE FFFF (-65537) */ + }; + + +/* Maximum length, in bytes, of a service name represented as a */ +/* literal C-String, including the terminating NULL at the end. */ + +#define kDNSServiceMaxServiceName 64 + +/* Maximum length, in bytes, of a domain name represented as an *escaped* C-String */ +/* including the final trailing dot, and the C-String terminating NULL at the end. */ + +#define kDNSServiceMaxDomainName 1005 + +/* + * Notes on DNS Name Escaping + * -- or -- + * "Why is kDNSServiceMaxDomainName 1005, when the maximum legal domain name is 255 bytes?" + * + * All strings used in DNS-SD are UTF-8 strings. + * With few exceptions, most are also escaped using standard DNS escaping rules: + * + * '\\' represents a single literal '\' in the name + * '\.' represents a single literal '.' in the name + * '\ddd', where ddd is a three-digit decimal value from 000 to 255, + * represents a single literal byte with that value. + * A bare unescaped '.' is a label separator, marking a boundary between domain and subdomain. + * + * The exceptions, that do not use escaping, are the routines where the full + * DNS name of a resource is broken, for convenience, into servicename/regtype/domain. + * In these routines, the "servicename" is NOT escaped. It does not need to be, since + * it is, by definition, just a single literal string. Any characters in that string + * represent exactly what they are. The "regtype" portion is, technically speaking, + * escaped, but since legal regtypes are only allowed to contain letters, digits, + * and hyphens, there is nothing to escape, so the issue is moot. The "domain" + * portion is also escaped, though most domains in use on the public Internet + * today, like regtypes, don't contain any characters that need to be escaped. + * As DNS-SD becomes more popular, rich-text domains for service discovery will + * become common, so software should be written to cope with domains with escaping. + * + * The servicename may be up to 63 bytes of UTF-8 text (not counting the C-String + * terminating NULL at the end). The regtype is of the form _service._tcp or + * _service._udp, where the "service" part is 1-14 characters, which may be + * letters, digits, or hyphens. The domain part of the three-part name may be + * any legal domain, providing that the resulting servicename+regtype+domain + * name does not exceed 255 bytes. + * + * For most software, these issues are transparent. When browsing, the discovered + * servicenames should simply be displayed as-is. When resolving, the discovered + * servicename/regtype/domain are simply passed unchanged to DNSServiceResolve(). + * When a DNSServiceResolve() succeeds, the returned fullname is already in + * the correct format to pass to standard system DNS APIs such as res_query(). + * For converting from servicename/regtype/domain to a single properly-escaped + * full DNS name, the helper function DNSServiceConstructFullName() is provided. + * + * The following (highly contrived) example illustrates the escaping process. + * Suppose you have an service called "Dr. Smith\Dr. Johnson", of type "_ftp._tcp" + * in subdomain "4th. Floor" of subdomain "Building 2" of domain "apple.com." + * The full (escaped) DNS name of this service's SRV record would be: + * Dr\.\032Smith\\Dr\.\032Johnson._ftp._tcp.4th\.\032Floor.Building\0322.apple.com. + */ + + +/* + * Constants for specifying an interface index + * + * Specific interface indexes are identified via a 32-bit unsigned integer returned + * by the if_nametoindex() family of calls. + * + * If the client passes 0 for interface index, that means "do the right thing", + * which (at present) means, "if the name is in an mDNS local multicast domain + * (e.g. 'local.', '254.169.in-addr.arpa.', '0.8.E.F.ip6.arpa.') then multicast + * on all applicable interfaces, otherwise send via unicast to the appropriate + * DNS server." Normally, most clients will use 0 for interface index to + * automatically get the default sensible behaviour. + * + * If the client passes a positive interface index, then for multicast names that + * indicates to do the operation only on that one interface. For unicast names the + * interface index is ignored unless kDNSServiceFlagsForceMulticast is also set. + * + * If the client passes kDNSServiceInterfaceIndexLocalOnly when registering + * a service, then that service will be found *only* by other local clients + * on the same machine that are browsing using kDNSServiceInterfaceIndexLocalOnly + * or kDNSServiceInterfaceIndexAny. + * If a client has a 'private' service, accessible only to other processes + * running on the same machine, this allows the client to advertise that service + * in a way such that it does not inadvertently appear in service lists on + * all the other machines on the network. + * + * If the client passes kDNSServiceInterfaceIndexLocalOnly when browsing + * then it will find *all* records registered on that same local machine. + * Clients explicitly wishing to discover *only* LocalOnly services can + * accomplish this by inspecting the interfaceIndex of each service reported + * to their DNSServiceBrowseReply() callback function, and discarding those + * where the interface index is not kDNSServiceInterfaceIndexLocalOnly. + */ + +#define kDNSServiceInterfaceIndexAny 0 +#define kDNSServiceInterfaceIndexLocalOnly ( (uint32_t) -1 ) + + +typedef uint32_t DNSServiceFlags; +typedef int32_t DNSServiceErrorType; + + +/********************************************************************************************* + * + * Unix Domain Socket access, DNSServiceRef deallocation, and data processing functions + * + *********************************************************************************************/ + + +/* DNSServiceRefSockFD() + * + * Access underlying Unix domain socket for an initialized DNSServiceRef. + * The DNS Service Discovery implmementation uses this socket to communicate between + * the client and the mDNSResponder daemon. The application MUST NOT directly read from + * or write to this socket. Access to the socket is provided so that it can be used as a + * run loop source, or in a select() loop: when data is available for reading on the socket, + * DNSServiceProcessResult() should be called, which will extract the daemon's reply from + * the socket, and pass it to the appropriate application callback. By using a run loop or + * select(), results from the daemon can be processed asynchronously. Without using these + * constructs, DNSServiceProcessResult() will block until the response from the daemon arrives. + * The client is responsible for ensuring that the data on the socket is processed in a timely + * fashion - the daemon may terminate its connection with a client that does not clear its + * socket buffer. + * + * sdRef: A DNSServiceRef initialized by any of the DNSService calls. + * + * return value: The DNSServiceRef's underlying socket descriptor, or -1 on + * error. + */ + +int DNSSD_API DNSServiceRefSockFD(DNSServiceRef sdRef); + + +/* DNSServiceProcessResult() + * + * Read a reply from the daemon, calling the appropriate application callback. This call will + * block until the daemon's response is received. Use DNSServiceRefSockFD() in + * conjunction with a run loop or select() to determine the presence of a response from the + * server before calling this function to process the reply without blocking. Call this function + * at any point if it is acceptable to block until the daemon's response arrives. Note that the + * client is responsible for ensuring that DNSServiceProcessResult() is called whenever there is + * a reply from the daemon - the daemon may terminate its connection with a client that does not + * process the daemon's responses. + * + * sdRef: A DNSServiceRef initialized by any of the DNSService calls + * that take a callback parameter. + * + * return value: Returns kDNSServiceErr_NoError on success, otherwise returns + * an error code indicating the specific failure that occurred. + */ + +DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdRef); + + +/* DNSServiceRefDeallocate() + * + * Terminate a connection with the daemon and free memory associated with the DNSServiceRef. + * Any services or records registered with this DNSServiceRef will be deregistered. Any + * Browse, Resolve, or Query operations called with this reference will be terminated. + * + * Note: If the reference's underlying socket is used in a run loop or select() call, it should + * be removed BEFORE DNSServiceRefDeallocate() is called, as this function closes the reference's + * socket. + * + * Note: If the reference was initialized with DNSServiceCreateConnection(), any DNSRecordRefs + * created via this reference will be invalidated by this call - the resource records are + * deregistered, and their DNSRecordRefs may not be used in subsequent functions. Similarly, + * if the reference was initialized with DNSServiceRegister, and an extra resource record was + * added to the service via DNSServiceAddRecord(), the DNSRecordRef created by the Add() call + * is invalidated when this function is called - the DNSRecordRef may not be used in subsequent + * functions. + * + * Note: This call is to be used only with the DNSServiceRef defined by this API. It is + * not compatible with dns_service_discovery_ref objects defined in the legacy Mach-based + * DNSServiceDiscovery.h API. + * + * sdRef: A DNSServiceRef initialized by any of the DNSService calls. + * + */ + +void DNSSD_API DNSServiceRefDeallocate(DNSServiceRef sdRef); + + +/********************************************************************************************* + * + * Domain Enumeration + * + *********************************************************************************************/ + +/* DNSServiceEnumerateDomains() + * + * Asynchronously enumerate domains available for browsing and registration. + * + * The enumeration MUST be cancelled via DNSServiceRefDeallocate() when no more domains + * are to be found. + * + * Note that the names returned are (like all of DNS-SD) UTF-8 strings, + * and are escaped using standard DNS escaping rules. + * (See "Notes on DNS Name Escaping" earlier in this file for more details.) + * A graphical browser displaying a hierarchical tree-structured view should cut + * the names at the bare dots to yield individual labels, then de-escape each + * label according to the escaping rules, and then display the resulting UTF-8 text. + * + * DNSServiceDomainEnumReply Callback Parameters: + * + * sdRef: The DNSServiceRef initialized by DNSServiceEnumerateDomains(). + * + * flags: Possible values are: + * kDNSServiceFlagsMoreComing + * kDNSServiceFlagsAdd + * kDNSServiceFlagsDefault + * + * interfaceIndex: Specifies the interface on which the domain exists. (The index for a given + * interface is determined via the if_nametoindex() family of calls.) + * + * errorCode: Will be kDNSServiceErr_NoError (0) on success, otherwise indicates + * the failure that occurred (other parameters are undefined if errorCode is nonzero). + * + * replyDomain: The name of the domain. + * + * context: The context pointer passed to DNSServiceEnumerateDomains. + * + */ + +typedef void (DNSSD_API *DNSServiceDomainEnumReply) + ( + DNSServiceRef sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + DNSServiceErrorType errorCode, + const char *replyDomain, + void *context + ); + + +/* DNSServiceEnumerateDomains() Parameters: + * + * + * sdRef: A pointer to an uninitialized DNSServiceRef. If the call succeeds + * then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError, + * and the enumeration operation will run indefinitely until the client + * terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate(). + * + * flags: Possible values are: + * kDNSServiceFlagsBrowseDomains to enumerate domains recommended for browsing. + * kDNSServiceFlagsRegistrationDomains to enumerate domains recommended + * for registration. + * + * interfaceIndex: If non-zero, specifies the interface on which to look for domains. + * (the index for a given interface is determined via the if_nametoindex() + * family of calls.) Most applications will pass 0 to enumerate domains on + * all interfaces. See "Constants for specifying an interface index" for more details. + * + * callBack: The function to be called when a domain is found or the call asynchronously + * fails. + * + * context: An application context pointer which is passed to the callback function + * (may be NULL). + * + * return value: Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous + * errors are delivered to the callback), otherwise returns an error code indicating + * the error that occurred (the callback is not invoked and the DNSServiceRef + * is not initialized.) + */ + +DNSServiceErrorType DNSSD_API DNSServiceEnumerateDomains + ( + DNSServiceRef *sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + DNSServiceDomainEnumReply callBack, + void *context /* may be NULL */ + ); + + +/********************************************************************************************* + * + * Service Registration + * + *********************************************************************************************/ + +/* Register a service that is discovered via Browse() and Resolve() calls. + * + * + * DNSServiceRegisterReply() Callback Parameters: + * + * sdRef: The DNSServiceRef initialized by DNSServiceRegister(). + * + * flags: Currently unused, reserved for future use. + * + * errorCode: Will be kDNSServiceErr_NoError on success, otherwise will + * indicate the failure that occurred (including name conflicts, + * if the kDNSServiceFlagsNoAutoRename flag was used when registering.) + * Other parameters are undefined if errorCode is nonzero. + * + * name: The service name registered (if the application did not specify a name in + * DNSServiceRegister(), this indicates what name was automatically chosen). + * + * regtype: The type of service registered, as it was passed to the callout. + * + * domain: The domain on which the service was registered (if the application did not + * specify a domain in DNSServiceRegister(), this indicates the default domain + * on which the service was registered). + * + * context: The context pointer that was passed to the callout. + * + */ + +typedef void (DNSSD_API *DNSServiceRegisterReply) + ( + DNSServiceRef sdRef, + DNSServiceFlags flags, + DNSServiceErrorType errorCode, + const char *name, + const char *regtype, + const char *domain, + void *context + ); + + +/* DNSServiceRegister() Parameters: + * + * sdRef: A pointer to an uninitialized DNSServiceRef. If the call succeeds + * then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError, + * and the registration will remain active indefinitely until the client + * terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate(). + * + * interfaceIndex: If non-zero, specifies the interface on which to register the service + * (the index for a given interface is determined via the if_nametoindex() + * family of calls.) Most applications will pass 0 to register on all + * available interfaces. See "Constants for specifying an interface index" for more details. + * + * flags: Indicates the renaming behavior on name conflict (most applications + * will pass 0). See flag definitions above for details. + * + * name: If non-NULL, specifies the service name to be registered. + * Most applications will not specify a name, in which case the computer + * name is used (this name is communicated to the client via the callback). + * If a name is specified, it must be 1-63 bytes of UTF-8 text. + * If the name is longer than 63 bytes it will be automatically truncated + * to a legal length, unless the NoAutoRename flag is set, + * in which case kDNSServiceErr_BadParam will be returned. + * + * regtype: The service type followed by the protocol, separated by a dot + * (e.g. "_ftp._tcp"). The service type must be an underscore, followed + * by 1-14 characters, which may be letters, digits, or hyphens. + * The transport protocol must be "_tcp" or "_udp". New service types + * should be registered at . + * + * domain: If non-NULL, specifies the domain on which to advertise the service. + * Most applications will not specify a domain, instead automatically + * registering in the default domain(s). + * + * host: If non-NULL, specifies the SRV target host name. Most applications + * will not specify a host, instead automatically using the machine's + * default host name(s). Note that specifying a non-NULL host does NOT + * create an address record for that host - the application is responsible + * for ensuring that the appropriate address record exists, or creating it + * via DNSServiceRegisterRecord(). + * + * port: The port, in network byte order, on which the service accepts connections. + * Pass 0 for a "placeholder" service (i.e. a service that will not be discovered + * by browsing, but will cause a name conflict if another client tries to + * register that same name). Most clients will not use placeholder services. + * + * txtLen: The length of the txtRecord, in bytes. Must be zero if the txtRecord is NULL. + * + * txtRecord: The TXT record rdata. A non-NULL txtRecord MUST be a properly formatted DNS + * TXT record, i.e. ... + * Passing NULL for the txtRecord is allowed as a synonym for txtLen=1, txtRecord="", + * i.e. it creates a TXT record of length one containing a single empty string. + * RFC 1035 doesn't allow a TXT record to contain *zero* strings, so a single empty + * string is the smallest legal DNS TXT record. + * + * callBack: The function to be called when the registration completes or asynchronously + * fails. The client MAY pass NULL for the callback - The client will NOT be notified + * of the default values picked on its behalf, and the client will NOT be notified of any + * asynchronous errors (e.g. out of memory errors, etc.) that may prevent the registration + * of the service. The client may NOT pass the NoAutoRename flag if the callback is NULL. + * The client may still deregister the service at any time via DNSServiceRefDeallocate(). + * + * context: An application context pointer which is passed to the callback function + * (may be NULL). + * + * return value: Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous + * errors are delivered to the callback), otherwise returns an error code indicating + * the error that occurred (the callback is never invoked and the DNSServiceRef + * is not initialized.) + * + */ + +DNSServiceErrorType DNSSD_API DNSServiceRegister + ( + DNSServiceRef *sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + const char *name, /* may be NULL */ + const char *regtype, + const char *domain, /* may be NULL */ + const char *host, /* may be NULL */ + uint16_t port, + uint16_t txtLen, + const void *txtRecord, /* may be NULL */ + DNSServiceRegisterReply callBack, /* may be NULL */ + void *context /* may be NULL */ + ); + + +/* DNSServiceAddRecord() + * + * Add a record to a registered service. The name of the record will be the same as the + * registered service's name. + * The record can later be updated or deregistered by passing the RecordRef initialized + * by this function to DNSServiceUpdateRecord() or DNSServiceRemoveRecord(). + * + * + * Parameters; + * + * sdRef: A DNSServiceRef initialized by DNSServiceRegister(). + * + * RecordRef: A pointer to an uninitialized DNSRecordRef. Upon succesfull completion of this + * call, this ref may be passed to DNSServiceUpdateRecord() or DNSServiceRemoveRecord(). + * If the above DNSServiceRef is passed to DNSServiceRefDeallocate(), RecordRef is also + * invalidated and may not be used further. + * + * flags: Currently ignored, reserved for future use. + * + * rrtype: The type of the record (e.g. kDNSServiceType_TXT, kDNSServiceType_SRV, etc) + * + * rdlen: The length, in bytes, of the rdata. + * + * rdata: The raw rdata to be contained in the added resource record. + * + * ttl: The time to live of the resource record, in seconds. Pass 0 to use a default value. + * + * return value: Returns kDNSServiceErr_NoError on success, otherwise returns an + * error code indicating the error that occurred (the RecordRef is not initialized). + */ + +DNSServiceErrorType DNSSD_API DNSServiceAddRecord + ( + DNSServiceRef sdRef, + DNSRecordRef *RecordRef, + DNSServiceFlags flags, + uint16_t rrtype, + uint16_t rdlen, + const void *rdata, + uint32_t ttl + ); + + +/* DNSServiceUpdateRecord + * + * Update a registered resource record. The record must either be: + * - The primary txt record of a service registered via DNSServiceRegister() + * - A record added to a registered service via DNSServiceAddRecord() + * - An individual record registered by DNSServiceRegisterRecord() + * + * + * Parameters: + * + * sdRef: A DNSServiceRef that was initialized by DNSServiceRegister() + * or DNSServiceCreateConnection(). + * + * RecordRef: A DNSRecordRef initialized by DNSServiceAddRecord, or NULL to update the + * service's primary txt record. + * + * flags: Currently ignored, reserved for future use. + * + * rdlen: The length, in bytes, of the new rdata. + * + * rdata: The new rdata to be contained in the updated resource record. + * + * ttl: The time to live of the updated resource record, in seconds. + * + * return value: Returns kDNSServiceErr_NoError on success, otherwise returns an + * error code indicating the error that occurred. + */ + +DNSServiceErrorType DNSSD_API DNSServiceUpdateRecord + ( + DNSServiceRef sdRef, + DNSRecordRef RecordRef, /* may be NULL */ + DNSServiceFlags flags, + uint16_t rdlen, + const void *rdata, + uint32_t ttl + ); + + +/* DNSServiceRemoveRecord + * + * Remove a record previously added to a service record set via DNSServiceAddRecord(), or deregister + * an record registered individually via DNSServiceRegisterRecord(). + * + * Parameters: + * + * sdRef: A DNSServiceRef initialized by DNSServiceRegister() (if the + * record being removed was registered via DNSServiceAddRecord()) or by + * DNSServiceCreateConnection() (if the record being removed was registered via + * DNSServiceRegisterRecord()). + * + * recordRef: A DNSRecordRef initialized by a successful call to DNSServiceAddRecord() + * or DNSServiceRegisterRecord(). + * + * flags: Currently ignored, reserved for future use. + * + * return value: Returns kDNSServiceErr_NoError on success, otherwise returns an + * error code indicating the error that occurred. + */ + +DNSServiceErrorType DNSSD_API DNSServiceRemoveRecord + ( + DNSServiceRef sdRef, + DNSRecordRef RecordRef, + DNSServiceFlags flags + ); + + +/********************************************************************************************* + * + * Service Discovery + * + *********************************************************************************************/ + +/* Browse for instances of a service. + * + * + * DNSServiceBrowseReply() Parameters: + * + * sdRef: The DNSServiceRef initialized by DNSServiceBrowse(). + * + * flags: Possible values are kDNSServiceFlagsMoreComing and kDNSServiceFlagsAdd. + * See flag definitions for details. + * + * interfaceIndex: The interface on which the service is advertised. This index should + * be passed to DNSServiceResolve() when resolving the service. + * + * errorCode: Will be kDNSServiceErr_NoError (0) on success, otherwise will + * indicate the failure that occurred. Other parameters are undefined if + * the errorCode is nonzero. + * + * serviceName: The discovered service name. This name should be displayed to the user, + * and stored for subsequent use in the DNSServiceResolve() call. + * + * regtype: The service type, which is usually (but not always) the same as was passed + * to DNSServiceBrowse(). One case where the discovered service type may + * not be the same as the requested service type is when using subtypes: + * The client may want to browse for only those ftp servers that allow + * anonymous connections. The client will pass the string "_ftp._tcp,_anon" + * to DNSServiceBrowse(), but the type of the service that's discovered + * is simply "_ftp._tcp". The regtype for each discovered service instance + * should be stored along with the name, so that it can be passed to + * DNSServiceResolve() when the service is later resolved. + * + * domain: The domain of the discovered service instance. This may or may not be the + * same as the domain that was passed to DNSServiceBrowse(). The domain for each + * discovered service instance should be stored along with the name, so that + * it can be passed to DNSServiceResolve() when the service is later resolved. + * + * context: The context pointer that was passed to the callout. + * + */ + +typedef void (DNSSD_API *DNSServiceBrowseReply) + ( + DNSServiceRef sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + DNSServiceErrorType errorCode, + const char *serviceName, + const char *regtype, + const char *replyDomain, + void *context + ); + + +/* DNSServiceBrowse() Parameters: + * + * sdRef: A pointer to an uninitialized DNSServiceRef. If the call succeeds + * then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError, + * and the browse operation will run indefinitely until the client + * terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate(). + * + * flags: Currently ignored, reserved for future use. + * + * interfaceIndex: If non-zero, specifies the interface on which to browse for services + * (the index for a given interface is determined via the if_nametoindex() + * family of calls.) Most applications will pass 0 to browse on all available + * interfaces. See "Constants for specifying an interface index" for more details. + * + * regtype: The service type being browsed for followed by the protocol, separated by a + * dot (e.g. "_ftp._tcp"). The transport protocol must be "_tcp" or "_udp". + * + * domain: If non-NULL, specifies the domain on which to browse for services. + * Most applications will not specify a domain, instead browsing on the + * default domain(s). + * + * callBack: The function to be called when an instance of the service being browsed for + * is found, or if the call asynchronously fails. + * + * context: An application context pointer which is passed to the callback function + * (may be NULL). + * + * return value: Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous + * errors are delivered to the callback), otherwise returns an error code indicating + * the error that occurred (the callback is not invoked and the DNSServiceRef + * is not initialized.) + */ + +DNSServiceErrorType DNSSD_API DNSServiceBrowse + ( + DNSServiceRef *sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + const char *regtype, + const char *domain, /* may be NULL */ + DNSServiceBrowseReply callBack, + void *context /* may be NULL */ + ); + + +/* DNSServiceResolve() + * + * Resolve a service name discovered via DNSServiceBrowse() to a target host name, port number, and + * txt record. + * + * Note: Applications should NOT use DNSServiceResolve() solely for txt record monitoring - use + * DNSServiceQueryRecord() instead, as it is more efficient for this task. + * + * Note: When the desired results have been returned, the client MUST terminate the resolve by calling + * DNSServiceRefDeallocate(). + * + * Note: DNSServiceResolve() behaves correctly for typical services that have a single SRV record + * and a single TXT record. To resolve non-standard services with multiple SRV or TXT records, + * DNSServiceQueryRecord() should be used. + * + * DNSServiceResolveReply Callback Parameters: + * + * sdRef: The DNSServiceRef initialized by DNSServiceResolve(). + * + * flags: Currently unused, reserved for future use. + * + * interfaceIndex: The interface on which the service was resolved. + * + * errorCode: Will be kDNSServiceErr_NoError (0) on success, otherwise will + * indicate the failure that occurred. Other parameters are undefined if + * the errorCode is nonzero. + * + * fullname: The full service domain name, in the form ... + * (This name is escaped following standard DNS rules, making it suitable for + * passing to standard system DNS APIs such as res_query(), or to the + * special-purpose functions included in this API that take fullname parameters. + * See "Notes on DNS Name Escaping" earlier in this file for more details.) + * + * hosttarget: The target hostname of the machine providing the service. This name can + * be passed to functions like gethostbyname() to identify the host's IP address. + * + * port: The port, in network byte order, on which connections are accepted for this service. + * + * txtLen: The length of the txt record, in bytes. + * + * txtRecord: The service's primary txt record, in standard txt record format. + * + + * context: The context pointer that was passed to the callout. + * + */ + +typedef void (DNSSD_API *DNSServiceResolveReply) + ( + DNSServiceRef sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + DNSServiceErrorType errorCode, + const char *fullname, + const char *hosttarget, + uint16_t port, + uint16_t txtLen, + const char *txtRecord, + void *context + ); + + +/* DNSServiceResolve() Parameters + * + * sdRef: A pointer to an uninitialized DNSServiceRef. If the call succeeds + * then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError, + * and the resolve operation will run indefinitely until the client + * terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate(). + * + * flags: Currently ignored, reserved for future use. + * + * interfaceIndex: The interface on which to resolve the service. If this resolve call is + * as a result of a currently active DNSServiceBrowse() operation, then the + * interfaceIndex should be the index reported in the DNSServiceBrowseReply + * callback. If this resolve call is using information previously saved + * (e.g. in a preference file) for later use, then use interfaceIndex 0, because + * the desired service may now be reachable via a different physical interface. + * See "Constants for specifying an interface index" for more details. + * + * name: The name of the service instance to be resolved, as reported to the + * DNSServiceBrowseReply() callback. + * + * regtype: The type of the service instance to be resolved, as reported to the + * DNSServiceBrowseReply() callback. + * + * domain: The domain of the service instance to be resolved, as reported to the + * DNSServiceBrowseReply() callback. + * + * callBack: The function to be called when a result is found, or if the call + * asynchronously fails. + * + * context: An application context pointer which is passed to the callback function + * (may be NULL). + * + * return value: Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous + * errors are delivered to the callback), otherwise returns an error code indicating + * the error that occurred (the callback is never invoked and the DNSServiceRef + * is not initialized.) + */ + +DNSServiceErrorType DNSSD_API DNSServiceResolve + ( + DNSServiceRef *sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + const char *name, + const char *regtype, + const char *domain, + DNSServiceResolveReply callBack, + void *context /* may be NULL */ + ); + + +/********************************************************************************************* + * + * Special Purpose Calls (most applications will not use these) + * + *********************************************************************************************/ + +/* DNSServiceCreateConnection() + * + * Create a connection to the daemon allowing efficient registration of + * multiple individual records. + * + * + * Parameters: + * + * sdRef: A pointer to an uninitialized DNSServiceRef. Deallocating + * the reference (via DNSServiceRefDeallocate()) severs the + * connection and deregisters all records registered on this connection. + * + * return value: Returns kDNSServiceErr_NoError on success, otherwise returns + * an error code indicating the specific failure that occurred (in which + * case the DNSServiceRef is not initialized). + */ + +DNSServiceErrorType DNSSD_API DNSServiceCreateConnection(DNSServiceRef *sdRef); + + +/* DNSServiceRegisterRecord + * + * Register an individual resource record on a connected DNSServiceRef. + * + * Note that name conflicts occurring for records registered via this call must be handled + * by the client in the callback. + * + * + * DNSServiceRegisterRecordReply() parameters: + * + * sdRef: The connected DNSServiceRef initialized by + * DNSServiceDiscoveryConnect(). + * + * RecordRef: The DNSRecordRef initialized by DNSServiceRegisterRecord(). If the above + * DNSServiceRef is passed to DNSServiceRefDeallocate(), this DNSRecordRef is + * invalidated, and may not be used further. + * + * flags: Currently unused, reserved for future use. + * + * errorCode: Will be kDNSServiceErr_NoError on success, otherwise will + * indicate the failure that occurred (including name conflicts.) + * Other parameters are undefined if errorCode is nonzero. + * + * context: The context pointer that was passed to the callout. + * + */ + + typedef void (DNSSD_API *DNSServiceRegisterRecordReply) + ( + DNSServiceRef sdRef, + DNSRecordRef RecordRef, + DNSServiceFlags flags, + DNSServiceErrorType errorCode, + void *context + ); + + +/* DNSServiceRegisterRecord() Parameters: + * + * sdRef: A DNSServiceRef initialized by DNSServiceCreateConnection(). + * + * RecordRef: A pointer to an uninitialized DNSRecordRef. Upon succesfull completion of this + * call, this ref may be passed to DNSServiceUpdateRecord() or DNSServiceRemoveRecord(). + * (To deregister ALL records registered on a single connected DNSServiceRef + * and deallocate each of their corresponding DNSServiceRecordRefs, call + * DNSServiceRefDealloocate()). + * + * flags: Possible values are kDNSServiceFlagsShared or kDNSServiceFlagsUnique + * (see flag type definitions for details). + * + * interfaceIndex: If non-zero, specifies the interface on which to register the record + * (the index for a given interface is determined via the if_nametoindex() + * family of calls.) Passing 0 causes the record to be registered on all interfaces. + * See "Constants for specifying an interface index" for more details. + * + * fullname: The full domain name of the resource record. + * + * rrtype: The numerical type of the resource record (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc) + * + * rrclass: The class of the resource record (usually kDNSServiceClass_IN) + * + * rdlen: Length, in bytes, of the rdata. + * + * rdata: A pointer to the raw rdata, as it is to appear in the DNS record. + * + * ttl: The time to live of the resource record, in seconds. Pass 0 to use a default value. + * + * callBack: The function to be called when a result is found, or if the call + * asynchronously fails (e.g. because of a name conflict.) + * + * context: An application context pointer which is passed to the callback function + * (may be NULL). + * + * return value: Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous + * errors are delivered to the callback), otherwise returns an error code indicating + * the error that occurred (the callback is never invoked and the DNSRecordRef is + * not initialized.) + */ + +DNSServiceErrorType DNSSD_API DNSServiceRegisterRecord + ( + DNSServiceRef sdRef, + DNSRecordRef *RecordRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + const char *fullname, + uint16_t rrtype, + uint16_t rrclass, + uint16_t rdlen, + const void *rdata, + uint32_t ttl, + DNSServiceRegisterRecordReply callBack, + void *context /* may be NULL */ + ); + + +/* DNSServiceQueryRecord + * + * Query for an arbitrary DNS record. + * + * + * DNSServiceQueryRecordReply() Callback Parameters: + * + * sdRef: The DNSServiceRef initialized by DNSServiceQueryRecord(). + * + * flags: Possible values are kDNSServiceFlagsMoreComing and + * kDNSServiceFlagsAdd. The Add flag is NOT set for PTR records + * with a ttl of 0, i.e. "Remove" events. + * + * interfaceIndex: The interface on which the query was resolved (the index for a given + * interface is determined via the if_nametoindex() family of calls). + * See "Constants for specifying an interface index" for more details. + * + * errorCode: Will be kDNSServiceErr_NoError on success, otherwise will + * indicate the failure that occurred. Other parameters are undefined if + * errorCode is nonzero. + * + * fullname: The resource record's full domain name. + * + * rrtype: The resource record's type (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc) + * + * rrclass: The class of the resource record (usually kDNSServiceClass_IN). + * + * rdlen: The length, in bytes, of the resource record rdata. + * + * rdata: The raw rdata of the resource record. + * + * ttl: The resource record's time to live, in seconds. + * + * context: The context pointer that was passed to the callout. + * + */ + +typedef void (DNSSD_API *DNSServiceQueryRecordReply) + ( + DNSServiceRef DNSServiceRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + DNSServiceErrorType errorCode, + const char *fullname, + uint16_t rrtype, + uint16_t rrclass, + uint16_t rdlen, + const void *rdata, + uint32_t ttl, + void *context + ); + + +/* DNSServiceQueryRecord() Parameters: + * + * sdRef: A pointer to an uninitialized DNSServiceRef. If the call succeeds + * then it initializes the DNSServiceRef, returns kDNSServiceErr_NoError, + * and the query operation will run indefinitely until the client + * terminates it by passing this DNSServiceRef to DNSServiceRefDeallocate(). + * + * flags: Pass kDNSServiceFlagsLongLivedQuery to create a "long-lived" unicast + * query in a non-local domain. Without setting this flag, unicast queries + * will be one-shot - that is, only answers available at the time of the call + * will be returned. By setting this flag, answers (including Add and Remove + * events) that become available after the initial call is made will generate + * callbacks. This flag has no effect on link-local multicast queries. + * + * interfaceIndex: If non-zero, specifies the interface on which to issue the query + * (the index for a given interface is determined via the if_nametoindex() + * family of calls.) Passing 0 causes the name to be queried for on all + * interfaces. See "Constants for specifying an interface index" for more details. + * + * fullname: The full domain name of the resource record to be queried for. + * + * rrtype: The numerical type of the resource record to be queried for + * (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc) + * + * rrclass: The class of the resource record (usually kDNSServiceClass_IN). + * + * callBack: The function to be called when a result is found, or if the call + * asynchronously fails. + * + * context: An application context pointer which is passed to the callback function + * (may be NULL). + * + * return value: Returns kDNSServiceErr_NoError on succeses (any subsequent, asynchronous + * errors are delivered to the callback), otherwise returns an error code indicating + * the error that occurred (the callback is never invoked and the DNSServiceRef + * is not initialized.) + */ + +DNSServiceErrorType DNSSD_API DNSServiceQueryRecord + ( + DNSServiceRef *sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + const char *fullname, + uint16_t rrtype, + uint16_t rrclass, + DNSServiceQueryRecordReply callBack, + void *context /* may be NULL */ + ); + + +/* DNSServiceReconfirmRecord + * + * Instruct the daemon to verify the validity of a resource record that appears to + * be out of date (e.g. because tcp connection to a service's target failed.) + * Causes the record to be flushed from the daemon's cache (as well as all other + * daemons' caches on the network) if the record is determined to be invalid. + * + * Parameters: + * + * flags: Currently unused, reserved for future use. + * + * interfaceIndex: If non-zero, specifies the interface of the record in question. + * Passing 0 causes all instances of this record to be reconfirmed. + * + * fullname: The resource record's full domain name. + * + * rrtype: The resource record's type (e.g. kDNSServiceType_PTR, kDNSServiceType_SRV, etc) + * + * rrclass: The class of the resource record (usually kDNSServiceClass_IN). + * + * rdlen: The length, in bytes, of the resource record rdata. + * + * rdata: The raw rdata of the resource record. + * + */ + +void DNSSD_API DNSServiceReconfirmRecord + ( + DNSServiceFlags flags, + uint32_t interfaceIndex, + const char *fullname, + uint16_t rrtype, + uint16_t rrclass, + uint16_t rdlen, + const void *rdata + ); + + +/********************************************************************************************* + * + * General Utility Functions + * + *********************************************************************************************/ + +/* DNSServiceConstructFullName() + * + * Concatenate a three-part domain name (as returned by the above callbacks) into a + * properly-escaped full domain name. Note that callbacks in the above functions ALREADY ESCAPE + * strings where necessary. + * + * Parameters: + * + * fullName: A pointer to a buffer that where the resulting full domain name is to be written. + * The buffer must be kDNSServiceMaxDomainName (1005) bytes in length to + * accommodate the longest legal domain name without buffer overrun. + * + * service: The service name - any dots or backslashes must NOT be escaped. + * May be NULL (to construct a PTR record name, e.g. + * "_ftp._tcp.apple.com."). + * + * regtype: The service type followed by the protocol, separated by a dot + * (e.g. "_ftp._tcp"). + * + * domain: The domain name, e.g. "apple.com.". Literal dots or backslashes, + * if any, must be escaped, e.g. "1st\. Floor.apple.com." + * + * return value: Returns 0 on success, -1 on error. + * + */ + +int DNSSD_API DNSServiceConstructFullName + ( + char *fullName, + const char *service, /* may be NULL */ + const char *regtype, + const char *domain + ); + + +/********************************************************************************************* + * + * TXT Record Construction Functions + * + *********************************************************************************************/ + +/* + * A typical calling sequence for TXT record construction is something like: + * + * Client allocates storage for TXTRecord data (e.g. declare buffer on the stack) + * TXTRecordCreate(); + * TXTRecordSetValue(); + * TXTRecordSetValue(); + * TXTRecordSetValue(); + * ... + * DNSServiceRegister( ... TXTRecordGetLength(), TXTRecordGetBytesPtr() ... ); + * TXTRecordDeallocate(); + * Explicitly deallocate storage for TXTRecord data (if not allocated on the stack) + */ + + +/* TXTRecordRef + * + * Opaque internal data type. + * Note: Represents a DNS-SD TXT record. + */ + +typedef struct _TXTRecordRef_t { char privatedata[16]; } TXTRecordRef; + + +/* TXTRecordCreate() + * + * Creates a new empty TXTRecordRef referencing the specified storage. + * + * If the buffer parameter is NULL, or the specified storage size is not + * large enough to hold a key subsequently added using TXTRecordSetValue(), + * then additional memory will be added as needed using malloc(). + * + * On some platforms, when memory is low, malloc() may fail. In this + * case, TXTRecordSetValue() will return kDNSServiceErr_NoMemory, and this + * error condition will need to be handled as appropriate by the caller. + * + * You can avoid the need to handle this error condition if you ensure + * that the storage you initially provide is large enough to hold all + * the key/value pairs that are to be added to the record. + * The caller can precompute the exact length required for all of the + * key/value pairs to be added, or simply provide a fixed-sized buffer + * known in advance to be large enough. + * A no-value (key-only) key requires (1 + key length) bytes. + * A key with empty value requires (1 + key length + 1) bytes. + * A key with non-empty value requires (1 + key length + 1 + value length). + * For most applications, DNS-SD TXT records are generally + * less than 100 bytes, so in most cases a simple fixed-sized + * 256-byte buffer will be more than sufficient. + * Recommended size limits for DNS-SD TXT Records are discussed in + * + * + * Note: When passing parameters to and from these TXT record APIs, + * the key name does not include the '=' character. The '=' character + * is the separator between the key and value in the on-the-wire + * packet format; it is not part of either the key or the value. + * + * txtRecord: A pointer to an uninitialized TXTRecordRef. + * + * bufferLen: The size of the storage provided in the "buffer" parameter. + * + * buffer: Optional caller-supplied storage used to hold the TXTRecord data. + * This storage must remain valid for as long as + * the TXTRecordRef. + */ + +void DNSSD_API TXTRecordCreate + ( + TXTRecordRef *txtRecord, + uint16_t bufferLen, + void *buffer + ); + + +/* TXTRecordDeallocate() + * + * Releases any resources allocated in the course of preparing a TXT Record + * using TXTRecordCreate()/TXTRecordSetValue()/TXTRecordRemoveValue(). + * Ownership of the buffer provided in TXTRecordCreate() returns to the client. + * + * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate(). + * + */ + +void DNSSD_API TXTRecordDeallocate + ( + TXTRecordRef *txtRecord + ); + + +/* TXTRecordSetValue() + * + * Adds a key (optionally with value) to a TXTRecordRef. If the "key" already + * exists in the TXTRecordRef, then the current value will be replaced with + * the new value. + * Keys may exist in four states with respect to a given TXT record: + * - Absent (key does not appear at all) + * - Present with no value ("key" appears alone) + * - Present with empty value ("key=" appears in TXT record) + * - Present with non-empty value ("key=value" appears in TXT record) + * For more details refer to "Data Syntax for DNS-SD TXT Records" in + * + * + * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate(). + * + * key: A null-terminated string which only contains printable ASCII + * values (0x20-0x7E), excluding '=' (0x3D). Keys should be + * 8 characters or less (not counting the terminating null). + * + * valueSize: The size of the value. + * + * value: Any binary value. For values that represent + * textual data, UTF-8 is STRONGLY recommended. + * For values that represent textual data, valueSize + * should NOT include the terminating null (if any) + * at the end of the string. + * If NULL, then "key" will be added with no value. + * If non-NULL but valueSize is zero, then "key=" will be + * added with empty value. + * + * return value: Returns kDNSServiceErr_NoError on success. + * Returns kDNSServiceErr_Invalid if the "key" string contains + * illegal characters. + * Returns kDNSServiceErr_NoMemory if adding this key would + * exceed the available storage. + */ + +DNSServiceErrorType DNSSD_API TXTRecordSetValue + ( + TXTRecordRef *txtRecord, + const char *key, + uint8_t valueSize, /* may be zero */ + const void *value /* may be NULL */ + ); + + +/* TXTRecordRemoveValue() + * + * Removes a key from a TXTRecordRef. The "key" must be an + * ASCII string which exists in the TXTRecordRef. + * + * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate(). + * + * key: A key name which exists in the TXTRecordRef. + * + * return value: Returns kDNSServiceErr_NoError on success. + * Returns kDNSServiceErr_NoSuchKey if the "key" does not + * exist in the TXTRecordRef. + * + */ + +DNSServiceErrorType DNSSD_API TXTRecordRemoveValue + ( + TXTRecordRef *txtRecord, + const char *key + ); + + +/* TXTRecordGetLength() + * + * Allows you to determine the length of the raw bytes within a TXTRecordRef. + * + * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate(). + * + * return value: Returns the size of the raw bytes inside a TXTRecordRef + * which you can pass directly to DNSServiceRegister() or + * to DNSServiceUpdateRecord(). + * Returns 0 if the TXTRecordRef is empty. + * + */ + +uint16_t DNSSD_API TXTRecordGetLength + ( + const TXTRecordRef *txtRecord + ); + + +/* TXTRecordGetBytesPtr() + * + * Allows you to retrieve a pointer to the raw bytes within a TXTRecordRef. + * + * txtRecord: A TXTRecordRef initialized by calling TXTRecordCreate(). + * + * return value: Returns a pointer to the raw bytes inside the TXTRecordRef + * which you can pass directly to DNSServiceRegister() or + * to DNSServiceUpdateRecord(). + * + */ + +const void * DNSSD_API TXTRecordGetBytesPtr + ( + const TXTRecordRef *txtRecord + ); + + +/********************************************************************************************* + * + * TXT Record Parsing Functions + * + *********************************************************************************************/ + +/* + * A typical calling sequence for TXT record parsing is something like: + * + * Receive TXT record data in DNSServiceResolve() callback + * if (TXTRecordContainsKey(txtLen, txtRecord, "key")) then do something + * val1ptr = TXTRecordGetValuePtr(txtLen, txtRecord, "key1", &len1); + * val2ptr = TXTRecordGetValuePtr(txtLen, txtRecord, "key2", &len2); + * ... + * bcopy(val1ptr, myval1, len1); + * bcopy(val2ptr, myval2, len2); + * ... + * return; + * + * If you wish to retain the values after return from the DNSServiceResolve() + * callback, then you need to copy the data to your own storage using bcopy() + * or similar, as shown in the example above. + * + * If for some reason you need to parse a TXT record you built yourself + * using the TXT record construction functions above, then you can do + * that using TXTRecordGetLength and TXTRecordGetBytesPtr calls: + * TXTRecordGetValue(TXTRecordGetLength(x), TXTRecordGetBytesPtr(x), key, &len); + * + * Most applications only fetch keys they know about from a TXT record and + * ignore the rest. + * However, some debugging tools wish to fetch and display all keys. + * To do that, use the TXTRecordGetCount() and TXTRecordGetItemAtIndex() calls. + */ + +/* TXTRecordContainsKey() + * + * Allows you to determine if a given TXT Record contains a specified key. + * + * txtLen: The size of the received TXT Record. + * + * txtRecord: Pointer to the received TXT Record bytes. + * + * key: A null-terminated ASCII string containing the key name. + * + * return value: Returns 1 if the TXT Record contains the specified key. + * Otherwise, it returns 0. + * + */ + +int DNSSD_API TXTRecordContainsKey + ( + uint16_t txtLen, + const void *txtRecord, + const char *key + ); + + +/* TXTRecordGetValuePtr() + * + * Allows you to retrieve the value for a given key from a TXT Record. + * + * txtLen: The size of the received TXT Record + * + * txtRecord: Pointer to the received TXT Record bytes. + * + * key: A null-terminated ASCII string containing the key name. + * + * valueLen: On output, will be set to the size of the "value" data. + * + * return value: Returns NULL if the key does not exist in this TXT record, + * or exists with no value (to differentiate between + * these two cases use TXTRecordContainsKey()). + * Returns pointer to location within TXT Record bytes + * if the key exists with empty or non-empty value. + * For empty value, valueLen will be zero. + * For non-empty value, valueLen will be length of value data. + */ + +const void * DNSSD_API TXTRecordGetValuePtr + ( + uint16_t txtLen, + const void *txtRecord, + const char *key, + uint8_t *valueLen + ); + + +/* TXTRecordGetCount() + * + * Returns the number of keys stored in the TXT Record. The count + * can be used with TXTRecordGetItemAtIndex() to iterate through the keys. + * + * txtLen: The size of the received TXT Record. + * + * txtRecord: Pointer to the received TXT Record bytes. + * + * return value: Returns the total number of keys in the TXT Record. + * + */ + +uint16_t DNSSD_API TXTRecordGetCount + ( + uint16_t txtLen, + const void *txtRecord + ); + + +/* TXTRecordGetItemAtIndex() + * + * Allows you to retrieve a key name and value pointer, given an index into + * a TXT Record. Legal index values range from zero to TXTRecordGetCount()-1. + * It's also possible to iterate through keys in a TXT record by simply + * calling TXTRecordGetItemAtIndex() repeatedly, beginning with index zero + * and increasing until TXTRecordGetItemAtIndex() returns kDNSServiceErr_Invalid. + * + * On return: + * For keys with no value, *value is set to NULL and *valueLen is zero. + * For keys with empty value, *value is non-NULL and *valueLen is zero. + * For keys with non-empty value, *value is non-NULL and *valueLen is non-zero. + * + * txtLen: The size of the received TXT Record. + * + * txtRecord: Pointer to the received TXT Record bytes. + * + * index: An index into the TXT Record. + * + * keyBufLen: The size of the string buffer being supplied. + * + * key: A string buffer used to store the key name. + * On return, the buffer contains a null-terminated C string + * giving the key name. DNS-SD TXT keys are usually + * 8 characters or less. To hold the maximum possible + * key name, the buffer should be 256 bytes long. + * + * valueLen: On output, will be set to the size of the "value" data. + * + * value: On output, *value is set to point to location within TXT + * Record bytes that holds the value data. + * + * return value: Returns kDNSServiceErr_NoError on success. + * Returns kDNSServiceErr_NoMemory if keyBufLen is too short. + * Returns kDNSServiceErr_Invalid if index is greater than + * TXTRecordGetCount()-1. + */ + +DNSServiceErrorType DNSSD_API TXTRecordGetItemAtIndex + ( + uint16_t txtLen, + const void *txtRecord, + uint16_t index, + uint16_t keyBufLen, + char *key, + uint8_t *valueLen, + const void **value + ); + +#ifdef __APPLE_API_PRIVATE + +/* + * Mac OS X specific functionality + * 3rd party clients of this API should not depend on future support or availability of this routine + */ + +/* DNSServiceSetDefaultDomainForUser() + * + * Set the default domain for the caller's UID. Future browse and registration + * calls by this user that do not specify an explicit domain will browse and + * register in this wide-area domain in addition to .local. In addition, this + * domain will be returned as a Browse domain via domain enumeration calls. + * + * + * Parameters: + * + * flags: Pass kDNSServiceFlagsAdd to add a domain for a user. Call without + * this flag set to clear a previously added domain. + * + * domain: The domain to be used for the caller's UID. + * + * return value: Returns kDNSServiceErr_NoError on succeses, otherwise returns + * an error code indicating the error that occurred + */ + +DNSServiceErrorType DNSSD_API DNSServiceSetDefaultDomainForUser + ( + DNSServiceFlags flags, + const char *domain + ); + +#endif //__APPLE_API_PRIVATE + +#ifdef __cplusplus + } +#endif + +#endif /* _DNS_SD_H */ diff --git a/compat-libdns_sd/funcs.txt b/compat-libdns_sd/funcs.txt new file mode 100644 index 0000000..8bafc1a --- /dev/null +++ b/compat-libdns_sd/funcs.txt @@ -0,0 +1,32 @@ +Supported: + +DNSServiceRefSockFD +DNSServiceProcessResult +DNSServiceRefDeallocate +DNSServiceEnumerateDomains +DNSServiceRegister +DNSServiceBrowse +DNSServiceResolve +DNSServiceConstructFullName + +TXTRecordCreate +TXTRecordDeallocate +TXTRecordSetValue +TXTRecordRemoveValue +TXTRecordGetLength +TXTRecordGetBytesPtr +TXTRecordContainsKey +TXTRecordGetValuePtr +TXTRecordGetCount +TXTRecordGetItemAtIndex + +Unsupported: + +DNSServiceRegisterRecord +DNSServiceQueryRecord +DNSServiceReconfirmRecord +DNSServiceCreateConnection +DNSServiceAddRecord +DNSServiceUpdateRecord +DNSServiceRemoveRecord +DNSServiceSetDefaultDomainForUser diff --git a/compat-libdns_sd/txt-test.c b/compat-libdns_sd/txt-test.c new file mode 100644 index 0000000..61f1292 --- /dev/null +++ b/compat-libdns_sd/txt-test.c @@ -0,0 +1,129 @@ +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + avahi is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General + Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include + +#include "dns_sd.h" + +static void hexdump(const void* p, size_t size) { + const uint8_t *c = p; + assert(p); + + printf("Dumping %u bytes from %p:\n", size, p); + + while (size > 0) { + unsigned i; + + for (i = 0; i < 16; i++) { + if (i < size) + printf("%02x ", c[i]); + else + printf(" "); + } + + for (i = 0; i < 16; i++) { + if (i < size) + printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.'); + else + printf(" "); + } + + printf("\n"); + + c += 16; + + if (size <= 16) + break; + + size -= 16; + } +} + + +int main(int argc, char *argv[]) { + const char *r; + TXTRecordRef ref; + uint8_t l; + const void *p; + char k[256]; + + TXTRecordCreate(&ref, 0, NULL); + + hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref)); + + TXTRecordSetValue(&ref, "foo", 7, "lennart"); + hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref)); + + TXTRecordSetValue(&ref, "waldo", 5, "rocks"); + hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref)); + + TXTRecordSetValue(&ref, "quux", 9, "the_house"); + hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref)); + + TXTRecordSetValue(&ref, "yeah", 0, NULL); + hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref)); + + TXTRecordSetValue(&ref, "waldo", 6, "rocked"); + hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref)); + + TXTRecordRemoveValue(&ref, "foo"); + hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref)); + + TXTRecordRemoveValue(&ref, "waldo"); + hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref)); + + TXTRecordSetValue(&ref, "kawumm", 6, "bloerb"); + hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref)); + + TXTRecordSetValue(&ref, "one", 1, "1"); + hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref)); + + TXTRecordSetValue(&ref, "two", 1, "2"); + hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref)); + + TXTRecordSetValue(&ref, "three", 1, "3"); + hexdump(TXTRecordGetBytesPtr(&ref), TXTRecordGetLength(&ref)); + + assert(TXTRecordContainsKey(TXTRecordGetLength(&ref), TXTRecordGetBytesPtr(&ref), "two")); + assert(!TXTRecordContainsKey(TXTRecordGetLength(&ref), TXTRecordGetBytesPtr(&ref), "four")); + + r = TXTRecordGetValuePtr(TXTRecordGetLength(&ref), TXTRecordGetBytesPtr(&ref), "kawumm", &l); + + hexdump(r, l); + + assert(TXTRecordGetCount(TXTRecordGetLength(&ref), TXTRecordGetBytesPtr(&ref)) == 6); + + TXTRecordGetItemAtIndex(TXTRecordGetLength(&ref), TXTRecordGetBytesPtr(&ref), 2, sizeof(k), k, &l, &p); + + fprintf(stderr, "key=<%s>\n", k); + + hexdump(p, l); + + assert(TXTRecordGetItemAtIndex(TXTRecordGetLength(&ref), TXTRecordGetBytesPtr(&ref), 20, sizeof(k), k, &l, &p) == kDNSServiceErr_Invalid); + + TXTRecordDeallocate(&ref); +} diff --git a/compat-libdns_sd/txt.c b/compat-libdns_sd/txt.c new file mode 100644 index 0000000..c85837a --- /dev/null +++ b/compat-libdns_sd/txt.c @@ -0,0 +1,491 @@ +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + avahi is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General + Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include + +#include + +#include "dns_sd.h" +#include "warn.h" + +typedef struct TXTRecordInternal { + uint8_t *buffer, *malloc_buffer; + size_t size, max_size; +} TXTRecordInternal; + +#define INTERNAL_PTR(txtref) (* (TXTRecordInternal**) (txtref)) +#define INTERNAL_PTR_CONST(txtref) (* (const TXTRecordInternal* const *) (txtref)) + +void DNSSD_API TXTRecordCreate( + TXTRecordRef *txtref, + uint16_t length, + void *buffer) { + + TXTRecordInternal *t; + + AVAHI_WARN_LINKAGE; + + assert(txtref); + + /* Apple's API design is flawed in so many ways, including the + * fact that it isn't compatible with 64 bit processors. To work + * around this we need some magic here which involves allocating + * our own memory. Please, Apple, do your homework next time + * before designing an API! */ + + if ((t = avahi_new(TXTRecordInternal, 1))) { + t->buffer = buffer; + t->max_size = length; + t->size = 0; + t->malloc_buffer = NULL; + } + + /* If we were unable to allocate memory, we store a NULL pointer + * and return a NoMemory error later, is somewhat unclean, but + * should work. */ + INTERNAL_PTR(txtref) = t; +} + +void DNSSD_API TXTRecordDeallocate(TXTRecordRef *txtref) { + TXTRecordInternal *t; + + AVAHI_WARN_LINKAGE; + + assert(txtref); + t = INTERNAL_PTR(txtref); + if (!t) + return; + + avahi_free(t->malloc_buffer); + avahi_free(t); + + /* Just in case ... */ + INTERNAL_PTR(txtref) = NULL; +} + +static int make_sure_fits_in(TXTRecordInternal *t, size_t size) { + uint8_t *n; + size_t nsize; + + assert(t); + + if (t->size + size <= t->max_size) + return 0; + + nsize = t->size + size + 100; + + if (nsize > 0xFFFF) + return -1; + + if (!(n = avahi_realloc(t->malloc_buffer, nsize))) + return -1; + + if (!t->malloc_buffer && t->size) + memcpy(n, t->buffer, t->size); + + t->buffer = t->malloc_buffer = n; + t->max_size = nsize; + + return 0; +} + +static int remove_key(TXTRecordInternal *t, const char *key) { + size_t i; + uint8_t *p; + size_t key_len; + int found = 0; + + key_len = strlen(key); + assert(key_len <= 0xFF); + + p = t->buffer; + i = 0; + + while (i < t->size) { + + /* Does the item fit in? */ + assert(*p <= t->size - i - 1); + + /* Key longer than buffer */ + if (key_len > t->size - i - 1) + break; + + if (key_len <= *p && + strncmp(key, (char*) p+1, key_len) == 0 && + (key_len == *p || p[1+key_len] == '=')) { + + uint8_t s; + + /* Key matches, so let's remove it */ + + s = *p; + memmove(p, p + 1 + *p, t->size - i - *p -1); + t->size -= s + 1; + + found = 1; + } else { + /* Skip to next */ + + i += *p +1; + p += *p +1; + } + } + + return found; +} + +DNSServiceErrorType DNSSD_API TXTRecordSetValue( + TXTRecordRef *txtref, + const char *key, + uint8_t length, + const void *value) { + + TXTRecordInternal *t; + uint8_t *p; + size_t l, n; + + AVAHI_WARN_LINKAGE; + + assert(key); + assert(txtref); + + l = strlen(key); + + if (*key == 0 || strchr(key, '=') || l > 0xFF) /* Empty or invalid key */ + return kDNSServiceErr_Invalid; + + if (!(t = INTERNAL_PTR(txtref))) + return kDNSServiceErr_NoMemory; + + n = l + (value ? length + 1 : 0); + + if (n > 0xFF) + return kDNSServiceErr_Invalid; + + if (make_sure_fits_in(t, 1 + n) < 0) + return kDNSServiceErr_NoMemory; + + remove_key(t, key); + + p = t->buffer + t->size; + + *(p++) = (uint8_t) n; + t->size ++; + + memcpy(p, key, l); + p += l; + t->size += l; + + if (value) { + *(p++) = '='; + memcpy(p, value, length); + t->size += length + 1; + } + + assert(t->size <= t->max_size); + + return kDNSServiceErr_NoError; +} + +DNSServiceErrorType DNSSD_API TXTRecordRemoveValue(TXTRecordRef *txtref, const char *key) { + TXTRecordInternal *t; + int found; + + AVAHI_WARN_LINKAGE; + + assert(key); + assert(txtref); + + if (*key == 0 || strchr(key, '=') || strlen(key) > 0xFF) /* Empty or invalid key */ + return kDNSServiceErr_Invalid; + + if (!(t = INTERNAL_PTR(txtref))) + return kDNSServiceErr_NoError; + + found = remove_key(t, key); + + return found ? kDNSServiceErr_NoError : kDNSServiceErr_NoSuchKey; +} + +uint16_t DNSSD_API TXTRecordGetLength(const TXTRecordRef *txtref) { + const TXTRecordInternal *t; + + AVAHI_WARN_LINKAGE; + + assert(txtref); + + if (!(t = INTERNAL_PTR_CONST(txtref))) + return 0; + + assert(t->size <= 0xFFFF); + return (uint16_t) t->size; +} + +const void * DNSSD_API TXTRecordGetBytesPtr(const TXTRecordRef *txtref) { + const TXTRecordInternal *t; + + AVAHI_WARN_LINKAGE; + + assert(txtref); + + if (!(t = INTERNAL_PTR_CONST(txtref)) || !t->buffer) + return ""; + + return t->buffer; +} + +static const uint8_t *find_key(const uint8_t *buffer, size_t size, const char *key) { + size_t i; + const uint8_t *p; + size_t key_len; + + key_len = strlen(key); + + assert(key_len <= 0xFF); + + p = buffer; + i = 0; + + while (i < size) { + + /* Does the item fit in? */ + if (*p > size - i - 1) + return NULL; + + /* Key longer than buffer */ + if (key_len > size - i - 1) + return NULL; + + if (key_len <= *p && + strncmp(key, (const char*) p+1, key_len) == 0 && + (key_len == *p || p[1+key_len] == '=')) { + + /* Key matches, so let's return it */ + + return p; + } + + /* Skip to next */ + i += *p +1; + p += *p +1; + } + + return NULL; +} + +int DNSSD_API TXTRecordContainsKey ( + uint16_t size, + const void *buffer, + const char *key) { + + AVAHI_WARN_LINKAGE; + + assert(key); + + if (!size) + return 0; + + assert(buffer); + + if (!(find_key(buffer, size, key))) + return 0; + + return 1; +} + +const void * DNSSD_API TXTRecordGetValuePtr( + uint16_t size, + const void *buffer, + const char *key, + uint8_t *value_len) { + + const uint8_t *p; + size_t n, l; + + AVAHI_WARN_LINKAGE; + + assert(key); + + if (!size) + goto fail; + + if (*key == 0 || strchr(key, '=') || strlen(key) > 0xFF) /* Empty or invalid key */ + return NULL; + + assert(buffer); + + if (!(p = find_key(buffer, size, key))) + goto fail; + + n = *p; + l = strlen(key); + + assert(n >= l); + p += 1 + l; + n -= l; + + if (n <= 0) + goto fail; + + assert(*p == '='); + p++; + n--; + + if (value_len) + *value_len = n; + + return p; + +fail: + if (value_len) + *value_len = 0; + + return NULL; +} + + +uint16_t DNSSD_API TXTRecordGetCount( + uint16_t size, + const void *buffer) { + + const uint8_t *p; + unsigned n = 0; + size_t i; + + AVAHI_WARN_LINKAGE; + + if (!size) + return 0; + + assert(buffer); + + p = buffer; + i = 0; + + while (i < size) { + + /* Does the item fit in? */ + if (*p > size - i - 1) + break; + + n++; + + /* Skip to next */ + i += *p +1; + p += *p +1; + } + + assert(n <= 0xFFFF); + + return (uint16_t) n; +} + +DNSServiceErrorType DNSSD_API TXTRecordGetItemAtIndex( + uint16_t size, + const void *buffer, + uint16_t idx, + uint16_t key_len, + char *key, + uint8_t *value_len, + const void **value) { + + const uint8_t *p; + size_t i; + unsigned n = 0; + DNSServiceErrorType ret = kDNSServiceErr_Invalid; + + AVAHI_WARN_LINKAGE; + + if (!size) + goto fail; + + assert(buffer); + + p = buffer; + i = 0; + + while (i < size) { + + /* Does the item fit in? */ + if (*p > size - i - 1) + goto fail; + + if (n >= idx) { + size_t l; + const uint8_t *d; + + d = memchr(p+1, '=', *p); + + /* Length of key */ + l = d ? d - p - 1 : *p; + + if (key_len < l+1) { + ret = kDNSServiceErr_NoMemory; + goto fail; + } + + strncpy(key, (const char*) p + 1, l); + key[l] = 0; + + if (d) { + if (value_len) + *value_len = *p - l - 1; + + if (value) + *value = d + 1; + } else { + + if (value_len) + *value_len = 0; + + if (value) + *value = NULL; + } + + return kDNSServiceErr_NoError; + } + + n++; + + /* Skip to next */ + i += *p +1; + p += *p +1; + } + + +fail: + + if (value) + *value = NULL; + + if (value_len) + *value_len = 0; + + return ret; + +} diff --git a/compat-libdns_sd/unsupported.c b/compat-libdns_sd/unsupported.c new file mode 100644 index 0000000..73e4697 --- /dev/null +++ b/compat-libdns_sd/unsupported.c @@ -0,0 +1,120 @@ +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + avahi is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General + Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include "dns_sd.h" +#include "warn.h" + +DNSServiceErrorType DNSSD_API DNSServiceRegisterRecord ( + DNSServiceRef sdRef, + DNSRecordRef *RecordRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + const char *fullname, + uint16_t rrtype, + uint16_t rrclass, + uint16_t rdlen, + const void *rdata, + uint32_t ttl, + DNSServiceRegisterRecordReply callBack, + void *context) { + + AVAHI_WARN_UNSUPPORTED; + + return kDNSServiceErr_Unsupported; +} + +DNSServiceErrorType DNSSD_API DNSServiceQueryRecord ( + DNSServiceRef *sdRef, + DNSServiceFlags flags, + uint32_t interfaceIndex, + const char *fullname, + uint16_t rrtype, + uint16_t rrclass, + DNSServiceQueryRecordReply callBack, + void *context) { + + AVAHI_WARN_UNSUPPORTED; + + return kDNSServiceErr_Unsupported; +} + +void DNSSD_API DNSServiceReconfirmRecord ( + DNSServiceFlags flags, + uint32_t interfaceIndex, + const char *fullname, + uint16_t rrtype, + uint16_t rrclass, + uint16_t rdlen, + const void *rdata) { + + AVAHI_WARN_UNSUPPORTED; + + return; +} + +DNSServiceErrorType DNSSD_API DNSServiceCreateConnection(DNSServiceRef *sdRef) { + AVAHI_WARN_UNSUPPORTED; + + return kDNSServiceErr_Unsupported; +} + +DNSServiceErrorType DNSSD_API DNSServiceAddRecord( + DNSServiceRef sdRef, + DNSRecordRef *RecordRef, + DNSServiceFlags flags, + uint16_t rrtype, + uint16_t rdlen, + const void *rdata, + uint32_t ttl) { + + AVAHI_WARN_UNSUPPORTED; + + return kDNSServiceErr_Unsupported; +} + +DNSServiceErrorType DNSSD_API DNSServiceUpdateRecord( + DNSServiceRef sdRef, + DNSRecordRef RecordRef, + DNSServiceFlags flags, + uint16_t rdlen, + const void *rdata, + uint32_t ttl) { + + AVAHI_WARN_UNSUPPORTED; + + return kDNSServiceErr_Unsupported; +} + +DNSServiceErrorType DNSSD_API DNSServiceRemoveRecord( + DNSServiceRef sdRef, + DNSRecordRef RecordRef, + DNSServiceFlags flags) { + + AVAHI_WARN_UNSUPPORTED; + + return kDNSServiceErr_Unsupported; +} + + diff --git a/compat-libdns_sd/warn.c b/compat-libdns_sd/warn.c new file mode 100644 index 0000000..859cfb0 --- /dev/null +++ b/compat-libdns_sd/warn.c @@ -0,0 +1,85 @@ +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + avahi is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General + Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include + +#include "warn.h" + +static pthread_mutex_t linkage_mutex = PTHREAD_MUTEX_INITIALIZER; +static int linkage_warning = 0; + +static void get_exe_name(char *t, size_t l) { + int k; + char fn[1024]; + + /* Yes, I know, this is not portable. But who cares? It's + * for cosmetics only, anyway. */ + + snprintf(fn, sizeof(fn), "/proc/%lu/exe", (unsigned long) getpid()); + + if ((k = readlink(fn, t, l-1)) < 0) + snprintf(t, l, "(unknown)"); + else { + char *slash; + + assert((size_t) k <= l-1); + t[k] = 0; + + if ((slash = strrchr(t, '/'))) + memmove(t, slash+1, strlen(slash)+1); + } +} + +void avahi_warn_linkage(void) { + int w; + + pthread_mutex_lock(&linkage_mutex); + w = linkage_warning; + linkage_warning = 1; + pthread_mutex_unlock(&linkage_mutex); + + if (!w && !getenv("AVAHI_BONJOUR_NOWARN")) { + char exename[256]; + get_exe_name(exename, sizeof(exename)); + + fprintf(stderr, "*** WARNING: The application '%s' uses the Bonjour compatiblity layer of Avahi. Please fix it to use the native API! ***\n", exename); + } +} + +void avahi_warn_unsupported(const char *function) { + char exename[256]; + get_exe_name(exename, sizeof(exename)); + + fprintf(stderr, "*** WARNING: The application '%s' called '%s()' which is not supported in the Bonjour compatiblity layer of Avahi. Please fix it to use the native API! ***\n", exename, function); +} + + + diff --git a/compat-libdns_sd/warn.h b/compat-libdns_sd/warn.h new file mode 100644 index 0000000..b419cd8 --- /dev/null +++ b/compat-libdns_sd/warn.h @@ -0,0 +1,32 @@ +#ifndef foowarnhfoo +#define foowarnhfoo + +/* $Id$ */ + +/*** + This file is part of avahi. + + avahi is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 2.1 of the + License, or (at your option) any later version. + + avahi is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General + Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with avahi; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + USA. +***/ + +void avahi_warn_unsupported(const char *function); + +void avahi_warn_linkage(void); + +#define AVAHI_WARN_LINKAGE { avahi_warn_linkage(); } +#define AVAHI_WARN_UNSUPPORTED { avahi_warn_linkage(); avahi_warn_unsupported(__FUNCTION__); } + +#endif diff --git a/configure.ac b/configure.ac index 60843bd..83a687b 100644 --- a/configure.ac +++ b/configure.ac @@ -555,7 +555,7 @@ man/Makefile tests/Makefile service-type-database/Makefile avahi-sharp/Makefile -compat-bonjour/Makefile +compat-libdns_sd/Makefile ]) AC_OUTPUT diff --git a/docs/TODO b/docs/TODO index 6a90889..b048095 100644 --- a/docs/TODO +++ b/docs/TODO @@ -8,6 +8,7 @@ for 0.6: * add API to allow user to tell the server that some service is not reachable * Add static host configuration like static services [lathiat] +* wrap avahi_server_add_record() via DBUS and in avahi-client [lathiat] * consolidate browsing failure events and add an API to query the reason @@ -23,8 +24,9 @@ later: * DNSSEC * Wide area DNS Update * long lived queries -* wrap avahi_server_add_record() via DBUS and in avahi-client [lathiat] - +* libavahi-compat-libdns_sd: implement kDNSServiceFlagsNoAutoRename + and missing functions + done: * drop glib from avahi-daemon * doxygen updates -- 2.39.2