]> git.meshlink.io Git - catta/blobdiff - avahi-compat-libdns_sd/compat.c
fix avahi_netlink_new to allow multiple netlinks per process
[catta] / avahi-compat-libdns_sd / compat.c
index adca034a9fb9b43873153101056226b4104fce9d..3ea359e612757c9556dc402cceb1a83293a033cd 100644 (file)
@@ -1,18 +1,16 @@
-/* $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
 #include <string.h>
 #include <signal.h>
 #include <netinet/in.h>
+#include <fcntl.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
 
 #include <avahi-common/simple-watch.h>
 #include <avahi-common/malloc.h>
 #include <avahi-common/error.h>
 #include <avahi-common/domain.h>
 #include <avahi-common/alternative.h>
+
 #include <avahi-client/client.h>
+#include <avahi-client/publish.h>
+#include <avahi-client/lookup.h>
 
 #include "warn.h"
 #include "dns_sd.h"
 
 enum {
-    COMMAND_POLL = 'P',
-    COMMAND_QUIT = 'Q',
-    COMMAND_POLLED = 'D'
+    COMMAND_POLL = 'p',
+    COMMAND_QUIT = 'q',
+    COMMAND_POLL_DONE = 'P',
+    COMMAND_POLL_FAILED = 'F'
+};
+
+struct type_info {
+    char *type;
+    AvahiStringList *subtypes;
+    int n_subtypes;
 };
 
 struct _DNSServiceRef_t {
     int n_ref;
-    
+
     AvahiSimplePoll *simple_poll;
 
     int thread_fd, main_fd;
@@ -65,13 +77,16 @@ struct _DNSServiceRef_t {
     DNSServiceResolveReply service_resolver_callback;
     DNSServiceDomainEnumReply domain_browser_callback;
     DNSServiceRegisterReply service_register_callback;
+    DNSServiceQueryRecordReply query_resolver_callback;
 
     AvahiClient *client;
     AvahiServiceBrowser *service_browser;
     AvahiServiceResolver *service_resolver;
     AvahiDomainBrowser *domain_browser;
+    AvahiRecordBrowser *record_browser;
 
-    char *service_name, *service_name_chosen, *service_regtype, *service_domain, *service_host;
+    struct type_info type_info;
+    char *service_name, *service_name_chosen, *service_domain, *service_host;
     uint16_t service_port;
     AvahiIfIndex service_interface;
     AvahiStringList *service_txt;
@@ -85,10 +100,10 @@ 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:
@@ -99,10 +114,11 @@ static DNSServiceErrorType map_error(int error) {
         case AVAHI_ERR_INVALID_PORT:
         case AVAHI_ERR_INVALID_KEY:
         case AVAHI_ERR_INVALID_ADDRESS:
+        case AVAHI_ERR_INVALID_SERVICE_SUBTYPE:
             return kDNSServiceErr_BadParam;
 
 
-        case AVAHI_ERR_LOCAL_COLLISION:
+        case AVAHI_ERR_COLLISION:
             return kDNSServiceErr_NameConflict;
 
         case AVAHI_ERR_TOO_MANY_CLIENTS:
@@ -121,13 +137,13 @@ static DNSServiceErrorType map_error(int error) {
         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;
 
@@ -136,7 +152,7 @@ static DNSServiceErrorType map_error(int error) {
         case AVAHI_ERR_INVALID_CONFIG:
         case AVAHI_ERR_TIMEOUT:
         case AVAHI_ERR_DBUS_ERROR:
-        case AVAHI_ERR_NOT_CONNECTED:
+        case AVAHI_ERR_DISCONNECTED:
         case AVAHI_ERR_NO_DAEMON:
             break;
 
@@ -145,6 +161,91 @@ static DNSServiceErrorType map_error(int error) {
     return kDNSServiceErr_Unknown;
 }
 
+static void type_info_init(struct type_info *i) {
+    assert(i);
+    i->type = NULL;
+    i->subtypes = NULL;
+    i->n_subtypes = 0;
+}
+
+static void type_info_free(struct type_info *i) {
+    assert(i);
+
+    avahi_free(i->type);
+    avahi_string_list_free(i->subtypes);
+
+    type_info_init(i);
+}
+
+static int type_info_parse(struct type_info *i, const char *t) {
+    char *token = NULL;
+
+    assert(i);
+    assert(t);
+
+    type_info_init(i);
+
+    for (;;) {
+        size_t l;
+
+        if (*t == 0)
+            break;
+
+        l = strcspn(t, ",");
+
+        if (l <= 0)
+            goto fail;
+
+        token = avahi_strndup(t, l);
+
+        if (!token)
+            goto fail;
+
+        if (!i->type) {
+            /* This is the first token, hence the main type */
+
+            if (!avahi_is_valid_service_type_strict(token))
+                goto fail;
+
+            i->type = token;
+            token = NULL;
+        } else {
+            char *fst;
+
+            /* This is not the first token, hence a subtype */
+
+            if (!(fst = avahi_strdup_printf("%s._sub.%s", token, i->type)))
+                goto fail;
+
+            if (!avahi_is_valid_service_subtype(fst)) {
+                avahi_free(fst);
+                goto fail;
+            }
+
+            i->subtypes = avahi_string_list_add(i->subtypes, fst);
+            avahi_free(fst);
+
+            avahi_free(token);
+            token = NULL;
+
+            i->n_subtypes++;
+        }
+
+        t += l;
+
+        if (*t == ',')
+            t++;
+    }
+
+    if (i->type)
+        return 0;
+
+fail:
+    type_info_free(i);
+    avahi_free(token);
+    return -1;
+}
+
 static const char *add_trailing_dot(const char *s, char *buf, size_t buf_len) {
     if (!s)
         return NULL;
@@ -164,7 +265,7 @@ static int read_command(int fd) {
     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;
@@ -187,15 +288,15 @@ static int write_command(int fd, char reply) {
 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;
@@ -207,7 +308,7 @@ static void * thread_func(void *data) {
 
     sigfillset(&mask);
     pthread_sigmask(SIG_BLOCK, &mask, NULL);
-    
+
     sdref->thread = pthread_self();
     sdref->thread_running = 1;
 
@@ -218,33 +319,40 @@ static void * thread_func(void *data) {
             break;
 
 /*         fprintf(stderr, "Command: %c\n", command); */
-        
+
         switch (command) {
 
-            case COMMAND_POLL:
+            case COMMAND_POLL: {
+                int ret;
 
                 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));
+                for (;;) {
+                    errno = 0;
+
+                    if ((ret = avahi_simple_poll_run(sdref->simple_poll)) < 0) {
+
+                        if (errno == EINTR)
+                            continue;
+
+                        fprintf(stderr, __FILE__": avahi_simple_poll_run() failed: %s\n", strerror(errno));
+                    }
+
                     break;
                 }
 
                 ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
-                
+
+                if (write_command(sdref->thread_fd, ret < 0 ? COMMAND_POLL_FAILED : COMMAND_POLL_DONE) < 0)
+                    break;
+
                 break;
+            }
 
             case COMMAND_QUIT:
                 return NULL;
         }
-        
+
     }
 
     return NULL;
@@ -271,12 +379,14 @@ static DNSServiceRef sdref_new(void) {
     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_name = sdref->service_name_chosen = sdref->service_domain = sdref->service_host = NULL;
     sdref->service_txt = NULL;
 
+    type_info_init(&sdref->type_info);
+
     ASSERT_SUCCESS(pthread_mutexattr_init(&mutex_attr));
     pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_RECURSIVE);
-    ASSERT_SUCCESS(pthread_mutex_init(&sdref->mutex, NULL));
+    ASSERT_SUCCESS(pthread_mutex_init(&sdref->mutex, &mutex_attr));
 
     sdref->thread_running = 0;
 
@@ -289,15 +399,15 @@ static DNSServiceRef sdref_new(void) {
     if (avahi_simple_poll_prepare(sdref->simple_poll, -1) < 0)
         goto fail;
 
-    /* Queue a initiall POLL command for the thread */
+    /* Queue an initial 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:
@@ -310,35 +420,36 @@ fail:
 
 static void sdref_free(DNSServiceRef sdref) {
     assert(sdref);
-    
+
     if (sdref->thread_running) {
-        write_command(sdref->main_fd, COMMAND_QUIT);
+        ASSERT_SUCCESS(write_command(sdref->main_fd, COMMAND_QUIT));
         avahi_simple_poll_wakeup(sdref->simple_poll);
-        pthread_join(sdref->thread, NULL);
+        ASSERT_SUCCESS(pthread_join(sdref->thread, NULL));
     }
 
     if (sdref->client)
         avahi_client_free(sdref->client);
 
+    if (sdref->simple_poll)
+        avahi_simple_poll_free(sdref->simple_poll);
+
     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);
+    ASSERT_SUCCESS(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);
 
+    type_info_free(&sdref->type_info);
+
     avahi_string_list_free(sdref->service_txt);
-    
+
     avahi_free(sdref);
 }
 
@@ -358,30 +469,31 @@ static void sdref_unref(DNSServiceRef sdref) {
 }
 
 int DNSSD_API DNSServiceRefSockFD(DNSServiceRef sdref) {
-    assert(sdref);
-    assert(sdref->n_ref >= 1);
 
     AVAHI_WARN_LINKAGE;
-    
+
+    if (!sdref || sdref->n_ref <= 0)
+        return -1;
+
     return sdref->main_fd;
 }
 
 DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdref) {
     DNSServiceErrorType ret = kDNSServiceErr_Unknown;
 
-    assert(sdref);
-    assert(sdref->n_ref >= 1);
-    
     AVAHI_WARN_LINKAGE;
 
+    if (!sdref || sdref->n_ref <= 0)
+        return kDNSServiceErr_BadParam;
+
     sdref_ref(sdref);
 
     ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
-    
+
     /* Cleanup notification socket */
-    if (read_command(sdref->main_fd) != COMMAND_POLLED)
+    if (read_command(sdref->main_fd) != COMMAND_POLL_DONE)
         goto finish;
-    
+
     if (avahi_simple_poll_dispatch(sdref->simple_poll) < 0)
         goto finish;
 
@@ -396,36 +508,34 @@ DNSServiceErrorType DNSSD_API DNSServiceProcessResult(DNSServiceRef sdref) {
         /* 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);
+    if (sdref)
+        sdref_unref(sdref);
 }
 
 static void service_browser_callback(
     AvahiServiceBrowser *b,
     AvahiIfIndex interface,
-    AvahiProtocol protocol,
+    AVAHI_GCC_UNUSED AvahiProtocol protocol,
     AvahiBrowserEvent event,
     const char *name,
     const char *type,
     const char *domain,
-    AvahiLookupResultFlags flags,
+    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
     void *userdata) {
 
     DNSServiceRef sdref = userdata;
@@ -436,7 +546,7 @@ static void service_browser_callback(
 
     type = add_trailing_dot(type, type_fixed, sizeof(type_fixed));
     domain  = add_trailing_dot(domain, domain_fixed, sizeof(domain_fixed));
-    
+
     switch (event) {
         case AVAHI_BROWSER_NEW:
             sdref->service_browser_callback(sdref, kDNSServiceFlagsAdd, interface, kDNSServiceErr_NoError, name, type, domain, sdref->context);
@@ -447,13 +557,9 @@ static void service_browser_callback(
             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);
+            sdref->service_browser_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), NULL, NULL, NULL, sdref->context);
             break;
-            
+
         case AVAHI_BROWSER_CACHE_EXHAUSTED:
         case AVAHI_BROWSER_ALL_FOR_NOW:
             break;
@@ -462,145 +568,157 @@ static void service_browser_callback(
 
 static void generic_client_callback(AvahiClient *s, AvahiClientState state, void* userdata) {
     DNSServiceRef sdref = userdata;
+    int error = kDNSServiceErr_Unknown;
 
     assert(s);
     assert(sdref);
     assert(sdref->n_ref >= 1);
 
     switch (state) {
-        case AVAHI_CLIENT_DISCONNECTED: {
+
+        case AVAHI_CLIENT_FAILURE:
 
             if (sdref->service_browser_callback)
-                sdref->service_browser_callback(sdref, 0, 0, kDNSServiceErr_Unknown, NULL, NULL, NULL, sdref->context);
+                sdref->service_browser_callback(sdref, 0, 0, error, 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);
+                sdref->service_resolver_callback(sdref, 0, 0, error, 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);
+                sdref->domain_browser_callback(sdref, 0, 0, error, NULL, sdref->context);
+            else if (sdref->query_resolver_callback)
+                sdref->query_resolver_callback(sdref, 0, 0, error, NULL, 0, 0, 0, NULL, 0, sdref->context);
 
             break;
-        }
 
         case AVAHI_CLIENT_S_RUNNING:
         case AVAHI_CLIENT_S_COLLISION:
-        case AVAHI_CLIENT_S_INVALID:
         case AVAHI_CLIENT_S_REGISTERING:
+        case AVAHI_CLIENT_CONNECTING:
             break;
     }
 }
 
 DNSServiceErrorType DNSSD_API DNSServiceBrowse(
-    DNSServiceRef *ret_sdref,
-    DNSServiceFlags flags,
-    uint32_t interface,
-    const char *regtype,
-    const char *domain,
-    DNSServiceBrowseReply callback,
-    void *context) {
+        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;
-    
+    struct type_info type_info;
+
     AVAHI_WARN_LINKAGE;
-    
-    assert(ret_sdref);
-    assert(regtype);
-    assert(domain);
-    assert(callback);
+
+    if (!ret_sdref || !regtype)
+        return kDNSServiceErr_BadParam;
+    *ret_sdref = NULL;
 
     if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0) {
         AVAHI_WARN_UNSUPPORTED;
         return kDNSServiceErr_Unsupported;
     }
 
-    if (!(sdref = sdref_new()))
+    type_info_init(&type_info);
+
+    if (type_info_parse(&type_info, regtype) < 0 || type_info.n_subtypes > 1) {
+        type_info_free(&type_info);
+
+        if (!avahi_is_valid_service_type_generic(regtype))
+            return kDNSServiceErr_Unsupported;
+    } else
+        regtype = type_info.subtypes ? (char*) type_info.subtypes->text : type_info.type;
+
+    if (!(sdref = sdref_new())) {
+        type_info_free(&type_info);
         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))) {
+
+    if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, 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);
 
+    type_info_free(&type_info);
+
     return ret;
 }
 
 static void service_resolver_callback(
     AvahiServiceResolver *r,
     AvahiIfIndex interface,
-    AvahiProtocol protocol,
+    AVAHI_GCC_UNUSED AvahiProtocol protocol,
     AvahiResolverEvent event,
     const char *name,
     const char *type,
     const char *domain,
     const char *host_name,
-    const AvahiAddress *a,
+    AVAHI_GCC_UNUSED const AvahiAddress *a,
     uint16_t port,
     AvahiStringList *txt,
-    AvahiLookupResultFlags flags,
+    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
     void *userdata) {
 
     DNSServiceRef sdref = userdata;
-    char host_name_fixed[AVAHI_DOMAIN_NAME_MAX];
 
     assert(r);
     assert(sdref);
     assert(sdref->n_ref >= 1);
 
-    host_name = add_trailing_dot(host_name, host_name_fixed, sizeof(host_name_fixed));
-
     switch (event) {
         case AVAHI_RESOLVER_FOUND: {
 
+            char host_name_fixed[AVAHI_DOMAIN_NAME_MAX];
             char full_name[AVAHI_DOMAIN_NAME_MAX];
             int ret;
             char *p = NULL;
             size_t l = 0;
 
+            host_name = add_trailing_dot(host_name, host_name_fixed, sizeof(host_name_fixed));
+
             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);
+            ret = avahi_service_name_join(full_name, sizeof(full_name), name, type, domain);
             assert(ret == AVAHI_OK);
 
             strcat(full_name, ".");
-            
-            sdref->service_resolver_callback(sdref, 0, interface, kDNSServiceErr_NoError, full_name, host_name, htons(port), l, p, sdref->context);
+
+            sdref->service_resolver_callback(sdref, 0, interface, kDNSServiceErr_NoError, full_name, host_name, htons(port), l, (unsigned char*) 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);
-            
+            sdref->service_resolver_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), NULL, NULL, 0, 0, NULL, sdref->context);
+            break;
     }
 }
 
@@ -621,11 +739,9 @@ DNSServiceErrorType DNSSD_API DNSServiceResolve(
 
     AVAHI_WARN_LINKAGE;
 
-    assert(ret_sdref);
-    assert(name);
-    assert(regtype);
-    assert(domain);
-    assert(callback);
+    if (!ret_sdref || !name || !regtype || !domain || !callback)
+        return kDNSServiceErr_BadParam;
+    *ret_sdref = NULL;
 
     if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0) {
         AVAHI_WARN_UNSUPPORTED;
@@ -639,27 +755,27 @@ DNSServiceErrorType DNSSD_API DNSServiceResolve(
     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))) {
+
+    if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, 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);
 
@@ -668,29 +784,28 @@ finish:
 
 int DNSSD_API DNSServiceConstructFullName (
     char *fullName,
-    const char *service,   
+    const char *service,
     const char *regtype,
     const char *domain) {
 
     AVAHI_WARN_LINKAGE;
 
-    assert(fullName);
-    assert(regtype);
-    assert(domain);
+    if (!fullName || !regtype || !domain)
+        return -1;
 
-    if (avahi_service_name_snprint(fullName, kDNSServiceMaxDomainName, service, regtype, domain) < 0)
+    if (avahi_service_name_join(fullName, kDNSServiceMaxDomainName, service, regtype, domain) < 0)
         return -1;
-    
+
     return 0;
 }
 
 static void domain_browser_callback(
     AvahiDomainBrowser *b,
     AvahiIfIndex interface,
-    AvahiProtocol protocol,
+    AVAHI_GCC_UNUSED AvahiProtocol protocol,
     AvahiBrowserEvent event,
     const char *domain,
-    AvahiLookupResultFlags flags,
+    AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
     void *userdata) {
 
     DNSServiceRef sdref = userdata;
@@ -712,13 +827,9 @@ static void domain_browser_callback(
             break;
 
         case AVAHI_BROWSER_FAILURE:
-            sdref->domain_browser_callback(sdref, 0, interface, kDNSServiceErr_Unknown, domain, sdref->context);
+            sdref->domain_browser_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), 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;
@@ -739,8 +850,9 @@ DNSServiceErrorType DNSSD_API DNSServiceEnumerateDomains(
 
     AVAHI_WARN_LINKAGE;
 
-    assert(ret_sdref);
-    assert(callback);
+    if (!ret_sdref || !callback)
+        return kDNSServiceErr_BadParam;
+    *ret_sdref = NULL;
 
     if (interface == kDNSServiceInterfaceIndexLocalOnly ||
         (flags != kDNSServiceFlagsBrowseDomains &&  flags != kDNSServiceFlagsRegistrationDomains)) {
@@ -755,28 +867,28 @@ DNSServiceErrorType DNSSD_API DNSServiceEnumerateDomains(
     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))) {
+
+    if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, 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, "local",
                                                            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);
 
@@ -789,11 +901,12 @@ static void reg_report_error(DNSServiceRef sdref, DNSServiceErrorType error) {
     assert(sdref);
     assert(sdref->n_ref >= 1);
 
-    assert(sdref->service_register_callback);
+    if (!sdref->service_register_callback)
+        return;
 
-    regtype = add_trailing_dot(sdref->service_regtype, regtype_fixed, sizeof(regtype_fixed));
+    regtype = add_trailing_dot(sdref->type_info.type, regtype_fixed, sizeof(regtype_fixed));
     domain = add_trailing_dot(sdref->service_domain, domain_fixed, sizeof(domain_fixed));
-    
+
     sdref->service_register_callback(
         sdref, 0, error,
         sdref->service_name_chosen ? sdref->service_name_chosen : sdref->service_name,
@@ -804,6 +917,8 @@ static void reg_report_error(DNSServiceRef sdref, DNSServiceErrorType error) {
 
 static int reg_create_service(DNSServiceRef sdref) {
     int ret;
+    AvahiStringList *l;
+
     assert(sdref);
     assert(sdref->n_ref >= 1);
 
@@ -813,13 +928,28 @@ static int reg_create_service(DNSServiceRef sdref) {
         AVAHI_PROTO_UNSPEC,
         0,
         sdref->service_name_chosen,
-        sdref->service_regtype,
+        sdref->type_info.type,
         sdref->service_domain,
         sdref->service_host,
         sdref->service_port,
         sdref->service_txt)) < 0)
         return ret;
 
+    for (l = sdref->type_info.subtypes; l; l = l->next) {
+        /* Create a subtype entry */
+
+        if (avahi_entry_group_add_service_subtype(
+                sdref->entry_group,
+                sdref->service_interface,
+                AVAHI_PROTO_UNSPEC,
+                0,
+                sdref->service_name_chosen,
+                sdref->type_info.type,
+                sdref->service_domain,
+                (const char*) l->text) < 0)
+            return ret;
+    }
+
     if ((ret = avahi_entry_group_commit(sdref->entry_group)) < 0)
         return ret;
 
@@ -836,14 +966,12 @@ static void reg_client_callback(AvahiClient *s, AvahiClientState state, void* us
     /* We've not been setup completely */
     if (!sdref->entry_group)
         return;
-    
-    switch (state) {
-        case AVAHI_CLIENT_DISCONNECTED: {
 
-            reg_report_error(sdref, kDNSServiceErr_NoError);
+    switch (state) {
+        case AVAHI_CLIENT_FAILURE:
+            reg_report_error(sdref, kDNSServiceErr_Unknown);
             break;
-        }
-        
+
         case AVAHI_CLIENT_S_RUNNING: {
             int ret;
 
@@ -852,6 +980,7 @@ static void reg_client_callback(AvahiClient *s, AvahiClientState state, void* us
                 /* If the service name is taken from the host name, copy that */
 
                 avahi_free(sdref->service_name_chosen);
+                sdref->service_name_chosen = NULL;
 
                 if (!(n = avahi_client_get_host_name(sdref->client))) {
                     reg_report_error(sdref, map_error(avahi_client_errno(sdref->client)));
@@ -863,26 +992,36 @@ static void reg_client_callback(AvahiClient *s, AvahiClientState state, void* us
                     return;
                 }
             }
-            
+
+            if (!sdref->service_name_chosen) {
+
+                assert(sdref->service_name);
+
+                if (!(sdref->service_name_chosen = avahi_strdup(sdref->service_name))) {
+                    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:
+        case AVAHI_CLIENT_S_REGISTERING:
 
             /* Remove our entry */
             avahi_entry_group_reset(sdref->entry_group);
-            
+
             break;
 
-        case AVAHI_CLIENT_S_INVALID:
-        case AVAHI_CLIENT_S_REGISTERING:
+        case AVAHI_CLIENT_CONNECTING:
             /* Ignore */
             break;
     }
@@ -896,6 +1035,7 @@ static void reg_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState st
 
     switch (state) {
         case AVAHI_ENTRY_GROUP_ESTABLISHED:
+
             /* Inform the user */
             reg_report_error(sdref, kDNSServiceErr_NoError);
 
@@ -904,7 +1044,7 @@ static void reg_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState st
         case AVAHI_ENTRY_GROUP_COLLISION: {
             char *n;
             int ret;
-            
+
             /* Remove our entry */
             avahi_entry_group_reset(sdref->entry_group);
 
@@ -923,7 +1063,7 @@ static void reg_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState st
                 reg_report_error(sdref, map_error(ret));
                 return;
             }
-            
+
             break;
         }
 
@@ -931,55 +1071,82 @@ static void reg_entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState st
         case AVAHI_ENTRY_GROUP_UNCOMMITED:
             /* Ignore */
             break;
+
+        case AVAHI_ENTRY_GROUP_FAILURE:
+            /* Inform the user */
+            reg_report_error(sdref, map_error(avahi_client_errno(sdref->client)));
+            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) {
+        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;
+    AvahiStringList *txt = NULL;
+    struct type_info type_info;
 
     AVAHI_WARN_LINKAGE;
 
-    assert(ret_sdref);
-    assert(callback);
-    assert(regtype);
+    if (!ret_sdref || !regtype)
+        return kDNSServiceErr_BadParam;
+    *ret_sdref = NULL;
+
+    if (!txtRecord) {
+        txtLen = 1;
+        txtRecord = "";
+    }
 
     if (interface == kDNSServiceInterfaceIndexLocalOnly || flags) {
         AVAHI_WARN_UNSUPPORTED;
         return kDNSServiceErr_Unsupported;
     }
 
-    if (!(sdref = sdref_new()))
+    if (txtLen > 0)
+        if (avahi_string_list_parse(txtRecord, txtLen, &txt) < 0)
+            return kDNSServiceErr_Invalid;
+
+    if (type_info_parse(&type_info, regtype) < 0) {
+        avahi_string_list_free(txt);
+        return kDNSServiceErr_Invalid;
+    }
+
+    if (!(sdref = sdref_new())) {
+        avahi_string_list_free(txt);
+        type_info_free(&type_info);
         return kDNSServiceErr_Unknown;
+    }
 
     sdref->context = context;
     sdref->service_register_callback = callback;
 
+    sdref->type_info = type_info;
     sdref->service_name = avahi_strdup(name);
-    sdref->service_regtype = regtype ? avahi_normalize_name(regtype) : NULL;
-    sdref->service_domain = domain ? avahi_normalize_name(domain) : NULL;
-    sdref->service_host = host ? avahi_normalize_name(host) : NULL;
+    sdref->service_domain = domain ? avahi_normalize_name_strdup(domain) : NULL;
+    sdref->service_host = host ? avahi_normalize_name_strdup(host) : NULL;
     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;
-    
+    sdref->service_txt = txt;
+
+    /* Some OOM checking would be cool here */
+
     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))) {
+
+    if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, reg_client_callback, sdref, &error))) {
         ret =  map_error(error);
         goto finish;
     }
@@ -1020,23 +1187,186 @@ DNSServiceErrorType DNSSD_API DNSServiceRegister (
             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;
 }
 
+DNSServiceErrorType DNSSD_API DNSServiceUpdateRecord(
+         DNSServiceRef sdref,
+         DNSRecordRef rref,
+         DNSServiceFlags flags,
+         uint16_t rdlen,
+         const void *rdata,
+         AVAHI_GCC_UNUSED uint32_t ttl) {
+
+    int ret = kDNSServiceErr_Unknown;
+    AvahiStringList *txt = NULL;
+
+    AVAHI_WARN_LINKAGE;
+
+    if (!sdref || sdref->n_ref <= 0)
+        return kDNSServiceErr_BadParam;
+
+    if (flags || rref) {
+        AVAHI_WARN_UNSUPPORTED;
+        return kDNSServiceErr_Unsupported;
+    }
+
+    if (rdlen > 0)
+        if (avahi_string_list_parse(rdata, rdlen, &txt) < 0)
+            return kDNSServiceErr_Invalid;
+
+    ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
+
+    if (!avahi_string_list_equal(txt, sdref->service_txt)) {
+
+        avahi_string_list_free(sdref->service_txt);
+        sdref->service_txt = txt;
+
+        if (avahi_client_get_state(sdref->client) == AVAHI_CLIENT_S_RUNNING &&
+            sdref->entry_group &&
+            (avahi_entry_group_get_state(sdref->entry_group) == AVAHI_ENTRY_GROUP_ESTABLISHED ||
+            avahi_entry_group_get_state(sdref->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING))
+
+            if (avahi_entry_group_update_service_txt_strlst(
+                        sdref->entry_group,
+                        sdref->service_interface,
+                        AVAHI_PROTO_UNSPEC,
+                        0,
+                        sdref->service_name_chosen,
+                        sdref->type_info.type,
+                        sdref->service_domain,
+                        sdref->service_txt) < 0) {
+
+                ret = map_error(avahi_client_errno(sdref->client));
+                goto finish;
+            }
+
+    } else
+        avahi_string_list_free(txt);
+
+    ret = kDNSServiceErr_NoError;
+
+finish:
+    ASSERT_SUCCESS(pthread_mutex_unlock(&sdref->mutex));
+
+    return ret;
+}
+
+static void query_resolver_callback(
+        AvahiRecordBrowser *r,
+        AvahiIfIndex interface,
+        AVAHI_GCC_UNUSED AvahiProtocol protocol,
+        AvahiBrowserEvent event,
+        const char *name,
+        uint16_t clazz,
+        uint16_t type,
+        const void* rdata,
+        size_t size,
+        AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
+        void *userdata) {
+
+    DNSServiceRef sdref = userdata;
+
+    assert(r);
+    assert(sdref);
+    assert(sdref->n_ref >= 1);
+
+    switch (event) {
+
+    case AVAHI_BROWSER_NEW:
+    case AVAHI_BROWSER_REMOVE: {
+
+        DNSServiceFlags qflags = 0;
+        if (event == AVAHI_BROWSER_NEW)
+            qflags |= kDNSServiceFlagsAdd;
+
+        sdref->query_resolver_callback(sdref, qflags, interface, kDNSServiceErr_NoError, name, type, clazz, size, rdata, 0, sdref->context);
+        break;
+    }
+
+    case AVAHI_BROWSER_ALL_FOR_NOW:
+    case AVAHI_BROWSER_CACHE_EXHAUSTED:
+        /* not implemented */
+        break;
+
+    case AVAHI_BROWSER_FAILURE:
+        sdref->query_resolver_callback(sdref, 0, interface, map_error(avahi_client_errno(sdref->client)), NULL, 0, 0, 0, NULL, 0, sdref->context);
+        break;
+    }
+}
+
+DNSServiceErrorType DNSSD_API DNSServiceQueryRecord (
+    DNSServiceRef *ret_sdref,
+    DNSServiceFlags flags,
+    uint32_t interface,
+    const char *fullname,
+    uint16_t type,
+    uint16_t clazz,
+    DNSServiceQueryRecordReply callback,
+    void *context) {
+
+    DNSServiceErrorType ret = kDNSServiceErr_Unknown;
+    int error;
+    DNSServiceRef sdref = NULL;
+    AvahiIfIndex ifindex;
+
+    AVAHI_WARN_LINKAGE;
+
+    if (!ret_sdref || !fullname)
+        return kDNSServiceErr_BadParam;
+    *ret_sdref = NULL;
+
+    if (interface == kDNSServiceInterfaceIndexLocalOnly || flags != 0) {
+        AVAHI_WARN_UNSUPPORTED;
+        return kDNSServiceErr_Unsupported;
+    }
+
+    if (!(sdref = sdref_new()))
+        return kDNSServiceErr_Unknown;
+
+    sdref->context = context;
+    sdref->query_resolver_callback = callback;
+
+    ASSERT_SUCCESS(pthread_mutex_lock(&sdref->mutex));
+
+    if (!(sdref->client = avahi_client_new(avahi_simple_poll_get(sdref->simple_poll), 0, generic_client_callback, sdref, &error))) {
+        ret =  map_error(error);
+        goto finish;
+    }
+
+    ifindex = interface == kDNSServiceInterfaceIndexAny ? AVAHI_IF_UNSPEC : (AvahiIfIndex) interface;
+
+    if (!(sdref->record_browser = avahi_record_browser_new(sdref->client, ifindex, AVAHI_PROTO_UNSPEC, fullname, clazz, type, 0, query_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;
+}