]> git.meshlink.io Git - catta/blobdiff - avahi-core/server.c
* add C examples
[catta] / avahi-core / server.c
index b00e4ee054832a560f8d645d0dec4bf15b431d6d..e3929dbd45d10654347044f882df9afc2b0e15a4 100644 (file)
 #include <string.h>
 #include <sys/utsname.h>
 #include <unistd.h>
+#include <errno.h>
+#include <stdio.h>
 
 #include "server.h"
 #include "util.h"
 #include "iface.h"
 #include "socket.h"
 #include "browse.h"
+#include "log.h"
 
-#define AVAHI_HOST_RR_HOLDOFF_MSEC 1000
+#define AVAHI_RR_HOLDOFF_MSEC 1000
+#define AVAHI_RR_HOLDOFF_MSEC_RATE_LIMIT 60000
+#define AVAHI_RR_RATE_LIMIT_COUNT 15
 
 static void free_entry(AvahiServer*s, AvahiEntry *e) {
     AvahiEntry *t;
@@ -71,6 +76,9 @@ static void free_group(AvahiServer *s, AvahiEntryGroup *g) {
     while (g->entries)
         free_entry(s, g->entries);
 
+    if (g->register_time_event)
+        avahi_time_event_queue_remove(s->time_event_queue, g->register_time_event);
+    
     AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, s->groups, g);
     g_free(g);
 }
@@ -133,7 +141,7 @@ void avahi_server_enumerate_aux_records(AvahiServer *s, AvahiInterface *i, Avahi
     g_assert(r);
     g_assert(callback);
     
-    if (r->key->class == AVAHI_DNS_CLASS_IN) {
+    if (r->key->clazz == AVAHI_DNS_CLASS_IN) {
         if (r->key->type == AVAHI_DNS_TYPE_PTR) {
             enum_aux_records(s, i, r->data.ptr.name, AVAHI_DNS_TYPE_SRV, callback, userdata);
             enum_aux_records(s, i, r->data.ptr.name, AVAHI_DNS_TYPE_TXT, callback, userdata);
@@ -160,7 +168,7 @@ void avahi_server_prepare_matching_responses(AvahiServer *s, AvahiInterface *i,
     g_assert(i);
     g_assert(k);
 
-/*     g_message("Posting responses matching [%s]", txt = avahi_key_to_string(k)); */
+/*     avahi_log_debug("Posting responses matching [%s]", txt = avahi_key_to_string(k)); */
 /*     g_free(txt); */
 
     if (avahi_key_is_pattern(k)) {
@@ -189,10 +197,14 @@ static void withdraw_entry(AvahiServer *s, AvahiEntry *e) {
         AvahiEntry *k;
         
         for (k = e->group->entries; k; k = k->by_group_next) {
-            avahi_goodbye_entry(s, k, FALSE);
-            k->dead = TRUE;
+            if (!k->dead) {
+                avahi_goodbye_entry(s, k, FALSE);
+                k->dead = TRUE;
+            }
         }
-        
+
+        e->group->n_probing = 0;
+
         avahi_entry_group_change_state(e->group, AVAHI_ENTRY_GROUP_COLLISION);
     } else {
         avahi_goodbye_entry(s, e, FALSE);
@@ -221,32 +233,40 @@ static void incoming_probe(AvahiServer *s, AvahiRecord *record, AvahiInterface *
     g_assert(record);
     g_assert(i);
 
+    t = avahi_record_to_string(record);
+
+/*     avahi_log_debug("incoming_probe()");  */
+
     for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) {
         gint cmp;
         n = e->by_key_next;
 
-        if (e->dead || !avahi_entry_probing(s, e, i))
+        if (e->dead)
             continue;
         
         if ((cmp = avahi_record_lexicographical_compare(e->record, record)) == 0) {
             ours = TRUE;
             break;
-        } else if (cmp > 0)
-            won = TRUE;
-        else /* cmp < 0 */
-            lost = TRUE;
+        } else {
+            
+            if (avahi_entry_probing(s, e, i)) {
+                if (cmp > 0)
+                    won = TRUE;
+                else /* cmp < 0 */
+                    lost = TRUE;
+            }
+        }
     }
 
-    t = avahi_record_to_string(record);
-
     if (!ours) {
 
         if (won)
-            g_message("Recieved conflicting probe [%s]. Local host won.", t);
+            avahi_log_debug("Recieved conflicting probe [%s]. Local host won.", t);
         else if (lost) {
-            g_message("Recieved conflicting probe [%s]. Local host lost. Withdrawing.", t);
+            avahi_log_debug("Recieved conflicting probe [%s]. Local host lost. Withdrawing.", t);
             withdraw_rrset(s, record->key);
-        }
+        }/*  else */
+/*             avahi_log_debug("Not conflicting probe"); */
     }
 
     g_free(t);
@@ -261,7 +281,7 @@ static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *
     g_assert(record);
 
 
-/*     g_message("CHECKING FOR CONFLICT: [%s]", t);   */
+/*     avahi_log_debug("CHECKING FOR CONFLICT: [%s]", t);   */
 
     for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) {
         n = e->by_key_next;
@@ -271,36 +291,36 @@ static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *
 
         /* Either our entry or the other is intended to be unique, so let's check */
         
-        if (avahi_entry_registered(s, e, i)) {
-
-            if (avahi_record_equal_no_ttl(e->record, record)) {
-                ours = TRUE; /* We have an identical record, so this is no conflict */
+        if (avahi_record_equal_no_ttl(e->record, record)) {
+            ours = TRUE; /* We have an identical record, so this is no conflict */
+            
+            /* Check wheter there is a TTL conflict */
+            if (record->ttl <= e->record->ttl/2 &&
+                avahi_entry_registered(s, e, i)) {
+                gchar *t;
+                /* Refresh */
+                t = avahi_record_to_string(record); 
                 
-                /* Check wheter there is a TTL conflict */
-                if (record->ttl <= e->record->ttl/2) {
-                    gchar *t;
-                    /* Refresh */
-                    t = avahi_record_to_string(record); 
-                           
-                    g_message("Recieved record with bad TTL [%s]. Refreshing.", t);
-                    avahi_server_prepare_matching_responses(s, i, e->record->key, FALSE);
-                    valid = FALSE;
-
-                    g_free(t);
-                }
+                avahi_log_debug("Recieved record with bad TTL [%s]. Refreshing.", t);
+                avahi_server_prepare_matching_responses(s, i, e->record->key, FALSE);
+                valid = FALSE;
+                
+                g_free(t);
+            }
+                
+            /* There's no need to check the other entries of this RRset */
+            break;
+
+        } else {
+            
+            if (avahi_entry_registered(s, e, i)) {
                 
-                /* There's no need to check the other entries of this RRset */
-                break;
-            } else {
                 /* A conflict => we have to return to probe mode */
                 conflict = TRUE;
                 conflicting_entry = e;
-            }
 
-        } else if (avahi_entry_probing(s, e, i)) {
+            } else if (avahi_entry_probing(s, e, i)) {
 
-            if (!avahi_record_equal_no_ttl(record, e->record)) {
-            
                 /* We are currently registering a matching record, but
                  * someone else already claimed it, so let's
                  * withdraw */
@@ -310,7 +330,7 @@ static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *
         }
     }
 
-/*     g_message("ours=%i conflict=%i", ours, conflict); */
+/*     avahi_log_debug("ours=%i conflict=%i", ours, conflict); */
 
     if (!ours && conflict) {
         gchar *t;
@@ -320,14 +340,14 @@ static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *
         t = avahi_record_to_string(record); 
  
         if (withdraw_immediately) {
-            g_message("Recieved conflicting record [%s] with local record to be. Withdrawing.", t);
+            avahi_log_debug("Recieved conflicting record [%s] with local record to be. Withdrawing.", t);
             withdraw_rrset(s, record->key);
         } else {
             g_assert(conflicting_entry);
-            g_message("Recieved conflicting record [%s]. Resetting our record.", t);
+            avahi_log_debug("Recieved conflicting record [%s]. Resetting our record.", t);
             avahi_entry_return_to_initial_state(s, conflicting_entry, i);
 
-            /* Local unique records are returned to probin
+            /* Local unique records are returned to probing
              * state. Local shared records are reannounced. */
         }
 
@@ -354,7 +374,7 @@ static void append_aux_records_to_list(AvahiServer *s, AvahiInterface *i, AvahiR
     avahi_server_enumerate_aux_records(s, i, r, append_aux_callback, &unicast_response);
 }
 
-void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, guint16 port, gboolean legacy_unicast) {
+void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, guint16 port, gboolean legacy_unicast, gboolean immediately) {
 
     g_assert(s);
     g_assert(i);
@@ -374,7 +394,7 @@ void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsP
                 avahi_dns_packet_inc_field(reply, AVAHI_DNS_FIELD_ANCOUNT);
             else {
                 gchar *t = avahi_record_to_string(r);
-                g_warning("Record [%s] not fitting in legacy unicast packet, dropping.", t);
+                avahi_log_warn("Record [%s] not fitting in legacy unicast packet, dropping.", t);
                 g_free(t);
             }
 
@@ -398,7 +418,7 @@ void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsP
         
         while ((r = avahi_record_list_next(s->record_list, &flush_cache, &unicast_response, &auxiliary))) {
                         
-            if (!avahi_interface_post_response(i, r, flush_cache, a, !tc && flush_cache && !auxiliary) && unicast_response) {
+            if (!avahi_interface_post_response(i, r, flush_cache, a, immediately || (flush_cache && !tc && !auxiliary)) && unicast_response) {
 
                 append_aux_records_to_list(s, i, r, unicast_response);
                 
@@ -438,7 +458,7 @@ void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsP
                             avahi_dns_packet_free(reply);
                             
                             gchar *t = avahi_record_to_string(r);
-                            g_warning("Record [%s] too large, doesn't fit in any packet!", t);
+                            avahi_log_warn("Record [%s] too large, doesn't fit in any packet!", t);
                             g_free(t);
                             break;
                         } else
@@ -465,17 +485,85 @@ void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsP
     avahi_record_list_flush(s->record_list);
 }
 
-static void handle_query(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, guint16 port, gboolean legacy_unicast) {
+
+static void reflect_response(AvahiServer *s, AvahiInterface *i, AvahiRecord *r, gboolean flush_cache) {
+    AvahiInterface *j;
+    
+    g_assert(s);
+    g_assert(i);
+    g_assert(r);
+
+    if (!s->config.enable_reflector)
+        return;
+
+    for (j = s->monitor->interfaces; j; j = j->interface_next)
+        if (j != i && (s->config.reflect_ipv || j->protocol == i->protocol))
+            avahi_interface_post_response(j, r, flush_cache, NULL, TRUE);
+}
+
+static gpointer reflect_cache_walk_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, gpointer userdata) {
+    AvahiServer *s = userdata;
+
+    g_assert(c);
+    g_assert(pattern);
+    g_assert(e);
+    g_assert(s);
+
+    avahi_record_list_push(s->record_list, e->record, e->cache_flush, FALSE, FALSE);
+    return NULL;
+}
+
+static void reflect_query(AvahiServer *s, AvahiInterface *i, AvahiKey *k) {
+    AvahiInterface *j;
+    
+    g_assert(s);
+    g_assert(i);
+    g_assert(k);
+
+    if (!s->config.enable_reflector)
+        return;
+
+    for (j = s->monitor->interfaces; j; j = j->interface_next)
+        if (j != i && (s->config.reflect_ipv || j->protocol == i->protocol)) {
+            /* Post the query to other networks */
+            avahi_interface_post_query(j, k, TRUE);
+
+            /* Reply from caches of other network. This is needed to
+             * "work around" known answer suppression. */
+
+            avahi_cache_walk(j->cache, k, reflect_cache_walk_callback, s);
+        }
+}
+
+static void reflect_probe(AvahiServer *s, AvahiInterface *i, AvahiRecord *r) {
+    AvahiInterface *j;
+    
+    g_assert(s);
+    g_assert(i);
+    g_assert(r);
+
+    if (!s->config.enable_reflector)
+        return;
+
+    for (j = s->monitor->interfaces; j; j = j->interface_next)
+        if (j != i && (s->config.reflect_ipv || j->protocol == i->protocol))
+            avahi_interface_post_probe(j, r, TRUE);
+}
+
+static void handle_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, guint16 port, gboolean legacy_unicast) {
     guint n;
+    gboolean is_probe;
     
     g_assert(s);
     g_assert(p);
     g_assert(i);
     g_assert(a);
 
-/*     g_message("query"); */
+/*     avahi_log_debug("query"); */
 
     g_assert(avahi_record_list_empty(s->record_list));
+
+    is_probe = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) > 0;
     
     /* Handle the questions */
     for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n --) {
@@ -483,11 +571,19 @@ static void handle_query(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, c
         gboolean unicast_response = FALSE;
 
         if (!(key = avahi_dns_packet_consume_key(p, &unicast_response))) {
-            g_warning("Packet too short (1)");
+            avahi_log_warn("Packet too short (1)");
             goto fail;
         }
 
-        avahi_query_scheduler_incoming(i->query_scheduler, key);
+        if (!legacy_unicast)
+            reflect_query(s, i, key);
+
+        if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) == 0 &&
+            !(avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_TC))
+            /* Allow our own queries to be suppressed by incoming
+             * queries only when they do not include known answers */
+            avahi_query_scheduler_incoming(i->query_scheduler, key);
+        
         avahi_server_prepare_matching_responses(s, i, key, unicast_response);
         avahi_key_unref(key);
     }
@@ -498,7 +594,7 @@ static void handle_query(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, c
         gboolean unique = FALSE;
 
         if (!(record = avahi_dns_packet_consume_record(p, &unique))) {
-            g_warning("Packet too short (2)");
+            avahi_log_warn("Packet too short (2)");
             goto fail;
         }
 
@@ -516,27 +612,28 @@ static void handle_query(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, c
         gboolean unique = FALSE;
 
         if (!(record = avahi_dns_packet_consume_record(p, &unique))) {
-            g_warning("Packet too short (3)");
+            avahi_log_warn("Packet too short (3)");
             goto fail;
         }
 
-        if (record->key->type != AVAHI_DNS_TYPE_ANY)
+        if (!avahi_key_is_pattern(record->key)) {
+            reflect_probe(s, i, record);
             incoming_probe(s, record, i);
+        }
         
         avahi_record_unref(record);
     }
 
     if (!avahi_record_list_empty(s->record_list))
-        avahi_server_generate_response(s, i, p, a, port, legacy_unicast);
+        avahi_server_generate_response(s, i, p, a, port, legacy_unicast, is_probe);
 
     return;
     
 fail:
     avahi_record_list_flush(s->record_list);
-
 }
 
-static void handle_response(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a) {
+static void handle_response_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a) {
     guint n;
     
     g_assert(s);
@@ -544,7 +641,7 @@ static void handle_response(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i
     g_assert(i);
     g_assert(a);
 
-/*     g_message("response"); */
+/*     avahi_log_debug("response"); */
     
     for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) +
              avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT); n > 0; n--) {
@@ -553,16 +650,17 @@ static void handle_response(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i
 /*         gchar *txt; */
         
         if (!(record = avahi_dns_packet_consume_record(p, &cache_flush))) {
-            g_warning("Packet too short (4)");
+            avahi_log_warn("Packet too short (4)");
             break;
         }
 
-        if (record->key->type != AVAHI_DNS_TYPE_ANY) {
+        if (!avahi_key_is_pattern(record->key)) {
 
-/*             g_message("Handling response: %s", txt = avahi_record_to_string(record)); */
+/*             avahi_log_debug("Handling response: %s", txt = avahi_record_to_string(record)); */
 /*             g_free(txt); */
             
             if (handle_conflict(s, i, record, cache_flush, a)) {
+                reflect_response(s, i, record, cache_flush);
                 avahi_cache_update(i->cache, record, cache_flush, a);
                 avahi_response_scheduler_incoming(i->response_scheduler, record, cache_flush);
             }
@@ -570,9 +668,198 @@ static void handle_response(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i
             
         avahi_record_unref(record);
     }
+
+    /* If the incoming response contained a conflicting record, some
+       records have been scheduling for sending. We need to flush them
+       here. */
+    if (!avahi_record_list_empty(s->record_list))
+        avahi_server_generate_response(s, i, NULL, NULL, 0, FALSE, TRUE);
+}
+
+static AvahiLegacyUnicastReflectSlot* allocate_slot(AvahiServer *s) {
+    guint n, idx = (guint) -1;
+    AvahiLegacyUnicastReflectSlot *slot;
+    
+    g_assert(s);
+
+    if (!s->legacy_unicast_reflect_slots)
+        s->legacy_unicast_reflect_slots = g_new0(AvahiLegacyUnicastReflectSlot*, AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS);
+
+    for (n = 0; n < AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS; n++, s->legacy_unicast_reflect_id++) {
+        idx = s->legacy_unicast_reflect_id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS;
+        
+        if (!s->legacy_unicast_reflect_slots[idx])
+            break;
+    }
+
+    if (idx == (guint) -1 || s->legacy_unicast_reflect_slots[idx])
+        return NULL;
+
+    slot = s->legacy_unicast_reflect_slots[idx] = g_new(AvahiLegacyUnicastReflectSlot, 1);
+    slot->id = s->legacy_unicast_reflect_id++;
+    slot->server = s;
+    return slot;
+}
+
+static void deallocate_slot(AvahiServer *s, AvahiLegacyUnicastReflectSlot *slot) {
+    guint idx;
+
+    g_assert(s);
+    g_assert(slot);
+
+    idx = slot->id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS;
+
+    g_assert(s->legacy_unicast_reflect_slots[idx] == slot);
+
+    avahi_time_event_queue_remove(s->time_event_queue, slot->time_event);
+    
+    g_free(slot);
+    s->legacy_unicast_reflect_slots[idx] = NULL;
+}
+
+static void free_slots(AvahiServer *s) {
+    guint idx;
+    g_assert(s);
+
+    if (!s->legacy_unicast_reflect_slots)
+        return;
+
+    for (idx = 0; idx < AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS; idx ++)
+        if (s->legacy_unicast_reflect_slots[idx])
+            deallocate_slot(s, s->legacy_unicast_reflect_slots[idx]);
+
+    g_free(s->legacy_unicast_reflect_slots);
+    s->legacy_unicast_reflect_slots = NULL;
+}
+
+static AvahiLegacyUnicastReflectSlot* find_slot(AvahiServer *s, guint16 id) {
+    guint idx;
+    
+    g_assert(s);
+
+    if (!s->legacy_unicast_reflect_slots)
+        return NULL;
+    
+    idx = id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS;
+
+    if (!s->legacy_unicast_reflect_slots[idx] || s->legacy_unicast_reflect_slots[idx]->id != id)
+        return NULL;
+
+    return s->legacy_unicast_reflect_slots[idx];
+}
+
+static void legacy_unicast_reflect_slot_timeout(AvahiTimeEvent *e, void *userdata) {
+    AvahiLegacyUnicastReflectSlot *slot = userdata;
+
+    g_assert(e);
+    g_assert(slot);
+    g_assert(slot->time_event == e);
+
+    deallocate_slot(slot->server, slot);
+}
+
+static void reflect_legacy_unicast_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, guint16 port) {
+    AvahiLegacyUnicastReflectSlot *slot;
+    AvahiInterface *j;
+
+    g_assert(s);
+    g_assert(p);
+    g_assert(i);
+    g_assert(a);
+    g_assert(port > 0);
+    g_assert(i->protocol == a->family);
+    
+    if (!s->config.enable_reflector)
+        return;
+
+/*     avahi_log_debug("legacy unicast reflector"); */
+    
+    /* Reflecting legacy unicast queries is a little more complicated
+       than reflecting normal queries, since we must route the
+       responses back to the right client. Therefore we must store
+       some information for finding the right client contact data for
+       response packets. In contrast to normal queries legacy
+       unicast query and response packets are reflected untouched and
+       are not reassembled into larger packets */
+
+    if (!(slot = allocate_slot(s))) {
+        /* No slot available, we drop this legacy unicast query */
+        avahi_log_warn("No slot available for legacy unicast reflection, dropping query packet.");
+        return;
+    }
+
+    slot->original_id = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID);
+    slot->address = *a;
+    slot->port = port;
+    slot->interface = i->hardware->index;
+
+    avahi_elapse_time(&slot->elapse_time, 2000, 0);
+    slot->time_event = avahi_time_event_queue_add(s->time_event_queue, &slot->elapse_time, legacy_unicast_reflect_slot_timeout, slot);
+
+    /* Patch the packet with our new locally generatedt id */
+    avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->id);
+    
+    for (j = s->monitor->interfaces; j; j = j->interface_next)
+        if (avahi_interface_relevant(j) &&
+            j != i &&
+            (s->config.reflect_ipv || j->protocol == i->protocol)) {
+
+            if (j->protocol == AVAHI_PROTO_INET && s->fd_legacy_unicast_ipv4 >= 0) {
+                avahi_send_dns_packet_ipv4(s->fd_legacy_unicast_ipv4, j->hardware->index, p, NULL, 0);
+                } else if (j->protocol == AVAHI_PROTO_INET6 && s->fd_legacy_unicast_ipv6 >= 0)
+                avahi_send_dns_packet_ipv6(s->fd_legacy_unicast_ipv6, j->hardware->index, p, NULL, 0);
+        }
+
+    /* Reset the id */
+    avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->original_id);
+}
+
+static gboolean originates_from_local_legacy_unicast_socket(AvahiServer *s, const struct sockaddr *sa) {
+    AvahiAddress a;
+    g_assert(s);
+    g_assert(sa);
+
+    if (!s->config.enable_reflector)
+        return FALSE;
+    
+    avahi_address_from_sockaddr(sa, &a);
+
+    if (!avahi_address_is_local(s->monitor, &a))
+        return FALSE;
+    
+    if (sa->sa_family == AF_INET && s->fd_legacy_unicast_ipv4 >= 0) {
+        struct sockaddr_in lsa;
+        socklen_t l = sizeof(lsa);
+        
+        if (getsockname(s->fd_legacy_unicast_ipv4, &lsa, &l) != 0)
+            avahi_log_warn("getsockname(): %s", strerror(errno));
+        else
+            return lsa.sin_port == ((const struct sockaddr_in*) sa)->sin_port;
+
+    }
+
+    if (sa->sa_family == AF_INET6 && s->fd_legacy_unicast_ipv6 >= 0) {
+        struct sockaddr_in6 lsa;
+        socklen_t l = sizeof(lsa);
+
+        if (getsockname(s->fd_legacy_unicast_ipv6, &lsa, &l) != 0)
+            avahi_log_warn("getsockname(): %s", strerror(errno));
+        else
+            return lsa.sin6_port == ((const struct sockaddr_in6*) sa)->sin6_port;
+    }
+
+    return FALSE;
+}
+
+static gboolean is_mdns_mcast_address(const AvahiAddress *a) {
+    AvahiAddress b;
+    g_assert(a);
+
+    avahi_address_parse(a->family == AVAHI_PROTO_INET ? AVAHI_IPV4_MCAST_GROUP : AVAHI_IPV6_MCAST_GROUP, a->family, &b);
+    return avahi_address_cmp(a, &b) == 0;
 }
 
-static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) {
+static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sockaddr *sa, AvahiAddress *dest, AvahiIfIndex iface, gint ttl) {
     AvahiInterface *i;
     AvahiAddress a;
     guint16 port;
@@ -580,41 +867,38 @@ static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, struct sockaddr *
     g_assert(s);
     g_assert(p);
     g_assert(sa);
+    g_assert(dest);
     g_assert(iface > 0);
 
     if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, sa->sa_family)) ||
         !avahi_interface_relevant(i)) {
-        g_warning("Recieved packet from invalid interface.");
+        avahi_log_warn("Recieved packet from invalid interface.");
         return;
     }
 
-/*     g_message("new packet recieved on interface '%s.%i'.", i->hardware->name, i->protocol); */
-
-    if (sa->sa_family == AF_INET6) {
-        static const guint8 ipv4_in_ipv6[] = {
-            0x00, 0x00, 0x00, 0x00,
-            0x00, 0x00, 0x00, 0x00,
-            0xFF, 0xFF, 0xFF, 0xFF };
+/*     avahi_log_debug("new packet recieved on interface '%s.%i'.", i->hardware->name, i->protocol); */
 
+    port = avahi_port_from_sockaddr(sa);
+    avahi_address_from_sockaddr(sa, &a);
+    
+    if (avahi_address_is_ipv4_in_ipv6(&a))
         /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
+        return;
 
-        if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0)
-            return;
-    }
+    if (originates_from_local_legacy_unicast_socket(s, sa))
+        /* This originates from our local reflector, so let's ignore it */
+        return;
 
     if (avahi_dns_packet_check_valid(p) < 0) {
-        g_warning("Recieved invalid packet.");
+        avahi_log_warn("Recieved invalid packet.");
         return;
     }
 
-    port = avahi_port_from_sockaddr(sa);
-    avahi_address_from_sockaddr(sa, &a);
-
     if (avahi_dns_packet_is_query(p)) {
         gboolean legacy_unicast = FALSE;
 
         if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT) != 0) {
-            g_warning("Invalid query packet.");
+            avahi_log_warn("Invalid query packet.");
             return;
         }
 
@@ -623,60 +907,136 @@ static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, struct sockaddr *
 
             if ((avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) != 0 ||
                  avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0)) {
-                g_warning("Invalid legacy unicast query packet.");
+                avahi_log_warn("Invalid legacy unicast query packet.");
                 return;
             }
         
             legacy_unicast = TRUE;
         }
 
-        handle_query(s, p, i, &a, port, legacy_unicast);
+        if (legacy_unicast)
+            reflect_legacy_unicast_query_packet(s, p, i, &a, port);
+        
+        handle_query_packet(s, p, i, &a, port, legacy_unicast);
         
-/*         g_message("Handled query"); */
+/*         avahi_log_debug("Handled query"); */
     } else {
-
         if (port != AVAHI_MDNS_PORT) {
-            g_warning("Recieved repsonse with invalid source port %u on interface '%s.%i'", port, i->hardware->name, i->protocol);
+            avahi_log_warn("Recieved repsonse with invalid source port %u on interface '%s.%i'", port, i->hardware->name, i->protocol);
             return;
         }
 
-        if (ttl != 255) {
-            g_warning("Recieved response with invalid TTL %u on interface '%s.%i'.", ttl, i->hardware->name, i->protocol);
-            if (s->config.check_response_ttl)
-                return;
+        if (ttl != 255 && s->config.check_response_ttl) {
+            avahi_log_warn("Recieved response with invalid TTL %u on interface '%s.%i'.", ttl, i->hardware->name, i->protocol);
+            return;
         }
 
+        if (!is_mdns_mcast_address(dest) &&
+            !avahi_interface_address_on_link(i, &a)) {
+            avahi_log_warn("Recivied non-local response on interface '%s.%i'.", i->hardware->name, i->protocol);
+            return;
+        }
+        
         if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT) != 0 ||
             avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) == 0 ||
             avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0) {
-            g_warning("Invalid response packet.");
+            avahi_log_warn("Invalid response packet.");
             return;
         }
 
-        handle_response(s, p, i, &a);
-/*         g_message("Handled response"); */
+        handle_response_packet(s, p, i, &a);
+/*         avahi_log_debug("Handled response"); */
     }
 }
 
+static void dispatch_legacy_unicast_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sockaddr *sa, AvahiIfIndex iface, gint ttl) {
+    AvahiInterface *i, *j;
+    AvahiAddress a;
+    guint16 port;
+    AvahiLegacyUnicastReflectSlot *slot;
+    
+    g_assert(s);
+    g_assert(p);
+    g_assert(sa);
+    g_assert(iface > 0);
+
+    if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, sa->sa_family)) ||
+        !avahi_interface_relevant(i)) {
+        avahi_log_warn("Recieved packet from invalid interface.");
+        return;
+    }
+
+/*     avahi_log_debug("new legacy unicast packet recieved on interface '%s.%i'.", i->hardware->name, i->protocol); */
+
+    port = avahi_port_from_sockaddr(sa);
+    avahi_address_from_sockaddr(sa, &a);
+    
+    if (avahi_address_is_ipv4_in_ipv6(&a))
+        /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
+        return;
+
+    if (avahi_dns_packet_check_valid(p) < 0 || avahi_dns_packet_is_query(p)) {
+        avahi_log_warn("Recieved invalid packet.");
+        return;
+    }
+
+    if (!(slot = find_slot(s, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID)))) {
+        avahi_log_warn("Recieved legacy unicast response with unknown id");
+        return;
+    }
+
+    if (!(j = avahi_interface_monitor_get_interface(s->monitor, slot->interface, slot->address.family)) ||
+        !avahi_interface_relevant(j))
+        return;
+
+    /* Patch the original ID into this response */
+    avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->original_id);
+
+    /* Forward the response to the correct client */
+    avahi_interface_send_packet_unicast(j, p, &slot->address, slot->port);
+
+    /* Undo changes to packet */
+    avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->id);
+}
+
 static void work(AvahiServer *s) {
     struct sockaddr_in6 sa6;
     struct sockaddr_in sa;
+    AvahiAddress dest;
     AvahiDnsPacket *p;
     gint iface = 0;
     guint8 ttl;
         
     g_assert(s);
 
-    if (s->pollfd_ipv4.revents & G_IO_IN) {
-        if ((p = avahi_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl))) {
-            dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
+    if (s->fd_ipv4 >= 0 && (s->pollfd_ipv4.revents & G_IO_IN)) {
+        dest.family = AVAHI_PROTO_INET;
+        if ((p = avahi_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &dest.data.ipv4, &iface, &ttl))) {
+            dispatch_packet(s, p, (struct sockaddr*) &sa, &dest, iface, ttl);
+            avahi_dns_packet_free(p);
+        }
+    }
+
+    if (s->fd_ipv6 >= 0 && (s->pollfd_ipv6.revents & G_IO_IN)) {
+        dest.family = AVAHI_PROTO_INET6;
+        if ((p = avahi_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &dest.data.ipv6, &iface, &ttl))) {
+            dispatch_packet(s, p, (struct sockaddr*) &sa6, &dest, iface, ttl);
             avahi_dns_packet_free(p);
         }
     }
 
-    if (s->pollfd_ipv6.revents & G_IO_IN) {
-        if ((p = avahi_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl))) {
-            dispatch_packet(s, p, (struct sockaddr*) &sa6, iface, ttl);
+    if (s->fd_legacy_unicast_ipv4 >= 0 && (s->pollfd_legacy_unicast_ipv4.revents & G_IO_IN)) {
+        dest.family = AVAHI_PROTO_INET;
+        if ((p = avahi_recv_dns_packet_ipv4(s->fd_legacy_unicast_ipv4, &sa, &dest.data.ipv4, &iface, &ttl))) {
+            dispatch_legacy_unicast_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
+            avahi_dns_packet_free(p);
+        }
+    }
+
+    if (s->fd_legacy_unicast_ipv6 >= 0 && (s->pollfd_legacy_unicast_ipv6.revents & G_IO_IN)) {
+        dest.family = AVAHI_PROTO_INET6;
+        if ((p = avahi_recv_dns_packet_ipv6(s->fd_legacy_unicast_ipv6, &sa6, &dest.data.ipv6, &iface, &ttl))) {
+            dispatch_legacy_unicast_packet(s, p, (struct sockaddr*) &sa6, iface, ttl);
             avahi_dns_packet_free(p);
         }
     }
@@ -692,12 +1052,23 @@ static gboolean prepare_func(GSource *source, gint *timeout) {
 
 static gboolean check_func(GSource *source) {
     AvahiServer* s;
+    gushort revents = 0;
+    
     g_assert(source);
 
     s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource)));
     g_assert(s);
+
+    if (s->fd_ipv4 >= 0)
+        revents |= s->pollfd_ipv4.revents;
+    if (s->fd_ipv6 >= 0)
+        revents |= s->pollfd_ipv6.revents;
+    if (s->fd_legacy_unicast_ipv4 >= 0)
+        revents |= s->pollfd_legacy_unicast_ipv4.revents;
+    if (s->fd_legacy_unicast_ipv6 >= 0)
+        revents |= s->pollfd_legacy_unicast_ipv6.revents;
     
-    return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR);
+    return !!(revents & (G_IO_IN | G_IO_HUP | G_IO_ERR));
 }
 
 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
@@ -728,15 +1099,11 @@ static void server_set_state(AvahiServer *s, AvahiServerState state) {
 static void withdraw_host_rrs(AvahiServer *s) {
     g_assert(s);
 
-    if (s->hinfo_entry_group) {
-        avahi_entry_group_free(s->hinfo_entry_group);
-        s->hinfo_entry_group = NULL;
-    }
+    if (s->hinfo_entry_group)
+        avahi_entry_group_reset(s->hinfo_entry_group);
 
-    if (s->browse_domain_entry_group) {
-        avahi_entry_group_free(s->browse_domain_entry_group);
-        s->browse_domain_entry_group = NULL;
-    }
+    if (s->browse_domain_entry_group)
+        avahi_entry_group_reset(s->browse_domain_entry_group);
 
     avahi_update_host_rrs(s->monitor, TRUE);
     s->n_host_rr_pending = 0;
@@ -779,17 +1146,20 @@ static void register_hinfo(AvahiServer *s) {
     
     g_assert(s);
     
-    if (!s->config.register_hinfo || s->hinfo_entry_group)
+    if (!s->config.publish_hinfo)
         return;
-    
-    s->hinfo_entry_group = avahi_entry_group_new(s, avahi_host_rr_entry_group_callback, NULL);
+
+    if (s->hinfo_entry_group)
+        g_assert(avahi_entry_group_is_empty(s->hinfo_entry_group));
+    else
+        s->hinfo_entry_group = avahi_entry_group_new(s, avahi_host_rr_entry_group_callback, NULL);
     
     /* Fill in HINFO rr */
-    r = avahi_record_new_full(s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO);
+    r = avahi_record_new_full(s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO, AVAHI_DEFAULT_TTL_HOST_NAME);
     uname(&utsname);
     r->data.hinfo.cpu = g_strdup(g_strup(utsname.machine));
     r->data.hinfo.os = g_strdup(g_strup(utsname.sysname));
-    avahi_server_add(s, s->hinfo_entry_group, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE, r);
+    avahi_server_add(s, s->hinfo_entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_ENTRY_UNIQUE, r);
     avahi_record_unref(r);
 
     avahi_entry_group_commit(s->hinfo_entry_group);
@@ -800,21 +1170,25 @@ static void register_localhost(AvahiServer *s) {
     g_assert(s);
     
     /* Add localhost entries */
-    avahi_address_parse("127.0.0.1", AF_INET, &a);
-    avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "localhost", &a);
+    avahi_address_parse("127.0.0.1", AVAHI_PROTO_INET, &a);
+    avahi_server_add_address(s, NULL, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "localhost", &a);
 
-    avahi_address_parse("::1", AF_INET6, &a);
-    avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "ip6-localhost", &a);
+    avahi_address_parse("::1", AVAHI_PROTO_INET6, &a);
+    avahi_server_add_address(s, NULL, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "ip6-localhost", &a);
 }
 
 static void register_browse_domain(AvahiServer *s) {
     g_assert(s);
 
-    if (!s->config.announce_domain || s->browse_domain_entry_group)
+    if (!s->config.publish_domain)
         return;
 
-    s->browse_domain_entry_group = avahi_entry_group_new(s, NULL, NULL);
-    avahi_server_add_ptr(s, s->browse_domain_entry_group, 0, AF_UNSPEC, 0, "_browse._dns-sd._udp.local", s->domain_name);
+    if (s->browse_domain_entry_group)
+        g_assert(avahi_entry_group_is_empty(s->browse_domain_entry_group));
+    else
+        s->browse_domain_entry_group = avahi_entry_group_new(s, NULL, NULL);
+    
+    avahi_server_add_ptr(s, s->browse_domain_entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, AVAHI_DEFAULT_TTL, "b._dns-sd._udp.local", s->domain_name);
     avahi_entry_group_commit(s->browse_domain_entry_group);
 }
 
@@ -840,38 +1214,10 @@ static void update_fqdn(AvahiServer *s) {
     s->host_name_fqdn = g_strdup_printf("%s.%s", s->host_name, s->domain_name);
 }
 
-static void register_time_event_callback(AvahiTimeEvent *e, gpointer userdata) {
-    AvahiServer *s = userdata;
-    
-    g_assert(e);
-    g_assert(s);
-
-    g_assert(e == s->register_time_event);
-    avahi_time_event_queue_remove(s->time_event_queue, s->register_time_event);
-    s->register_time_event = NULL;
-
-    if (s->state == AVAHI_SERVER_SLEEPING)
-        register_stuff(s);
-}
-
-static void delayed_register_stuff(AvahiServer *s) {
-    GTimeVal tv;
-    
-    g_assert(s);
-
-    avahi_elapse_time(&tv, AVAHI_HOST_RR_HOLDOFF_MSEC, 0);
-
-    if (s->register_time_event)
-        avahi_time_event_queue_update(s->time_event_queue, s->register_time_event, &tv);
-    else
-        s->register_time_event = avahi_time_event_queue_add(s->time_event_queue, &tv, register_time_event_callback, s);
-}
-
-void avahi_server_set_host_name(AvahiServer *s, const gchar *host_name) {
+gint avahi_server_set_host_name(AvahiServer *s, const gchar *host_name) {
     g_assert(s);
     g_assert(host_name);
 
-    server_set_state(s, AVAHI_SERVER_SLEEPING);
     withdraw_host_rrs(s);
 
     g_free(s->host_name);
@@ -879,21 +1225,33 @@ void avahi_server_set_host_name(AvahiServer *s, const gchar *host_name) {
     s->host_name[strcspn(s->host_name, ".")] = 0;
     update_fqdn(s);
 
-    delayed_register_stuff(s);
+    register_stuff(s);
+    return AVAHI_OK;
 }
 
-void avahi_server_set_domain_name(AvahiServer *s, const gchar *domain_name) {
+gint avahi_server_set_domain_name(AvahiServer *s, const gchar *domain_name) {
     g_assert(s);
     g_assert(domain_name);
 
-    server_set_state(s, AVAHI_SERVER_SLEEPING);
     withdraw_host_rrs(s);
 
     g_free(s->domain_name);
-    s->domain_name = domain_name ? avahi_normalize_name(domain_name) : g_strdup("local.");
+    s->domain_name = domain_name ? avahi_normalize_name(domain_name) : g_strdup("local");
     update_fqdn(s);
 
-    delayed_register_stuff(s);
+    register_stuff(s);
+    return AVAHI_OK;
+}
+
+static void prepare_pollfd(AvahiServer *s, GPollFD *pollfd, gint fd) {
+    g_assert(s);
+    g_assert(pollfd);
+    g_assert(fd >= 0);
+
+    memset(pollfd, 0, sizeof(GPollFD));
+    pollfd->fd = fd;
+    pollfd->events = G_IO_IN|G_IO_ERR|G_IO_HUP;
+    g_source_add_poll(s->source, pollfd);
 }
 
 AvahiServer *avahi_server_new(GMainContext *c, const AvahiServerConfig *sc, AvahiServerCallback callback, gpointer userdata) {
@@ -928,29 +1286,28 @@ AvahiServer *avahi_server_new(GMainContext *c, const AvahiServerConfig *sc, Avah
     }
 
     if (s->fd_ipv4 < 0 && s->config.use_ipv4)
-        g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode");
+        avahi_log_notice("Failed to create IPv4 socket, proceeding in IPv6 only mode");
     else if (s->fd_ipv6 < 0 && s->config.use_ipv6)
-        g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode");
-    
-    if (c)
-        g_main_context_ref(s->context = c);
-    else
-        s->context = g_main_context_default();
+        avahi_log_notice("Failed to create IPv6 socket, proceeding in IPv4 only mode");
+
+    s->fd_legacy_unicast_ipv4 = s->fd_ipv4 >= 0 && s->config.enable_reflector ? avahi_open_legacy_unicast_socket_ipv4() : -1;
+    s->fd_legacy_unicast_ipv6 = s->fd_ipv6 >= 0 && s->config.enable_reflector ? avahi_open_legacy_unicast_socket_ipv6() : -1;
+
+    g_main_context_ref(s->context = (c ? c : g_main_context_default()));
 
     /* Prepare IO source registration */
     s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(AvahiServer*));
     *((AvahiServer**) (((guint8*) s->source) + sizeof(GSource))) = s;
 
-    memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4));
-    s->pollfd_ipv4.fd = s->fd_ipv4;
-    s->pollfd_ipv4.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
-    g_source_add_poll(s->source, &s->pollfd_ipv4);
+    if (s->fd_ipv4 >= 0)
+        prepare_pollfd(s, &s->pollfd_ipv4, s->fd_ipv4);
+    if (s->fd_ipv6 >= 0)
+        prepare_pollfd(s, &s->pollfd_ipv6, s->fd_ipv6);
+    if (s->fd_legacy_unicast_ipv4 >= 0)
+        prepare_pollfd(s, &s->pollfd_legacy_unicast_ipv4, s->fd_legacy_unicast_ipv4);
+    if (s->fd_legacy_unicast_ipv6 >= 0)
+        prepare_pollfd(s, &s->pollfd_legacy_unicast_ipv6, s->fd_legacy_unicast_ipv6);
     
-    memset(&s->pollfd_ipv6, 0, sizeof(s->pollfd_ipv6));
-    s->pollfd_ipv6.fd = s->fd_ipv6;
-    s->pollfd_ipv6.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
-    g_source_add_poll(s->source, &s->pollfd_ipv6);
-
     g_source_attach(s->source, s->context);
     
     s->callback = callback;
@@ -965,18 +1322,24 @@ AvahiServer *avahi_server_new(GMainContext *c, const AvahiServerConfig *sc, Avah
     AVAHI_LLIST_HEAD_INIT(AvahiHostNameResolver, s->host_name_resolvers);
     AVAHI_LLIST_HEAD_INIT(AvahiAddressResolver, s->address_resolvers);
     AVAHI_LLIST_HEAD_INIT(AvahiDomainBrowser, s->domain_browsers);
+    AVAHI_LLIST_HEAD_INIT(AvahiServiceTypeBrowser, s->service_type_browsers);
+    AVAHI_LLIST_HEAD_INIT(AvahiServiceBrowser, s->service_browsers);
+    AVAHI_LLIST_HEAD_INIT(AvahiServiceResolver, s->service_resolvers);
+    AVAHI_LLIST_HEAD_INIT(AvahiDNSServerBrowser, s->dns_server_browsers);
 
+    s->legacy_unicast_reflect_slots = NULL;
+    s->legacy_unicast_reflect_id = 0;
+    
     /* Get host name */
     s->host_name = s->config.host_name ? avahi_normalize_name(s->config.host_name) : avahi_get_host_name();
     s->host_name[strcspn(s->host_name, ".")] = 0;
-    s->domain_name = s->config.domain_name ? avahi_normalize_name(s->config.domain_name) : g_strdup("local.");
+    s->domain_name = s->config.domain_name ? avahi_normalize_name(s->config.domain_name) : g_strdup("local");
     s->host_name_fqdn = NULL;
     update_fqdn(s);
 
     s->record_list = avahi_record_list_new();
 
     s->time_event_queue = avahi_time_event_queue_new(s->context, G_PRIORITY_DEFAULT+10); /* Slightly less priority than the FDs */
-    s->register_time_event = NULL;
     
     s->state = AVAHI_SERVER_INVALID;
 
@@ -1003,20 +1366,28 @@ void avahi_server_free(AvahiServer* s) {
     while (s->groups)
         free_group(s, s->groups);
 
+    free_slots(s);
+
+    while (s->dns_server_browsers)
+        avahi_dns_server_browser_free(s->dns_server_browsers);
     while (s->host_name_resolvers)
         avahi_host_name_resolver_free(s->host_name_resolvers);
     while (s->address_resolvers)
         avahi_address_resolver_free(s->address_resolvers);
     while (s->domain_browsers)
         avahi_domain_browser_free(s->domain_browsers);
+    while (s->service_type_browsers)
+        avahi_service_type_browser_free(s->service_type_browsers);
+    while (s->service_browsers)
+        avahi_service_browser_free(s->service_browsers);
+    while (s->service_resolvers)
+        avahi_service_resolver_free(s->service_resolvers);
     while (s->record_browsers)
         avahi_record_browser_destroy(s->record_browsers);
     g_hash_table_destroy(s->record_browser_hashtable);
 
     g_hash_table_destroy(s->entries_by_key);
 
-    if (s->register_time_event)
-        avahi_time_event_queue_remove(s->time_event_queue, s->register_time_event);
     avahi_time_event_queue_free(s->time_event_queue);
 
     avahi_record_list_free(s->record_list);
@@ -1025,6 +1396,10 @@ void avahi_server_free(AvahiServer* s) {
         close(s->fd_ipv4);
     if (s->fd_ipv6 >= 0)
         close(s->fd_ipv6);
+    if (s->fd_legacy_unicast_ipv4 >= 0)
+        close(s->fd_legacy_unicast_ipv4);
+    if (s->fd_legacy_unicast_ipv6 >= 0)
+        close(s->fd_legacy_unicast_ipv6);
 
     g_free(s->host_name);
     g_free(s->domain_name);
@@ -1039,19 +1414,57 @@ void avahi_server_free(AvahiServer* s) {
     g_free(s);
 }
 
-void avahi_server_add(
+static gint check_record_conflict(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *r, AvahiEntryFlags flags) {
+    AvahiEntry *e;
+    
+    g_assert(s);
+    g_assert(r);
+
+    for (e = g_hash_table_lookup(s->entries_by_key, r->key); e; e = e->by_key_next) {
+        if (e->dead)
+            continue;
+
+        if (!(flags & AVAHI_ENTRY_UNIQUE) && !(e->flags & AVAHI_ENTRY_UNIQUE))
+            continue;
+        
+        if ((flags & AVAHI_ENTRY_ALLOWMUTIPLE) && (e->flags & AVAHI_ENTRY_ALLOWMUTIPLE) )
+            continue;
+
+        if (interface <= 0 ||
+            e->interface <= 0 ||
+            e->interface == interface ||
+            protocol == AVAHI_PROTO_UNSPEC ||
+            e->protocol == AVAHI_PROTO_UNSPEC ||
+            e->protocol == protocol)
+
+            return -1;
+
+    }
+
+    return 0;
+}
+
+gint avahi_server_add(
     AvahiServer *s,
     AvahiEntryGroup *g,
-    gint interface,
-    guchar protocol,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
     AvahiEntryFlags flags,
     AvahiRecord *r) {
     
     AvahiEntry *e, *t;
+    
     g_assert(s);
     g_assert(r);
 
-    g_assert(r->key->type != AVAHI_DNS_TYPE_ANY);
+    if (r->ttl == 0)
+        return -1;
+
+    if (avahi_key_is_pattern(r->key))
+        return -1;
+
+    if (check_record_conflict(s, interface, protocol, r, flags) < 0)
+        return -1;
 
     e = g_new(AvahiEntry, 1);
     e->server = s;
@@ -1076,7 +1489,10 @@ void avahi_server_add(
         AVAHI_LLIST_PREPEND(AvahiEntry, by_group, g->entries, e); 
 
     avahi_announce_entry(s, e);
+
+    return 0;
 }
+
 const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiEntryGroup *g, void **state) {
     AvahiEntry **e = (AvahiEntry**) state;
     g_assert(s);
@@ -1094,144 +1510,176 @@ const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiEntryGroup *g, void
     return avahi_record_ref((*e)->record);
 }
 
-void avahi_server_dump(AvahiServer *s, FILE *f) {
+void avahi_server_dump(AvahiServer *s, AvahiDumpCallback callback, gpointer userdata) {
     AvahiEntry *e;
+    
     g_assert(s);
-    g_assert(f);
+    g_assert(callback);
 
-    fprintf(f, "\n;;; ZONE DUMP FOLLOWS ;;;\n");
+    callback(";;; ZONE DUMP FOLLOWS ;;;", userdata);
 
     for (e = s->entries; e; e = e->entries_next) {
         gchar *t;
+        gchar ln[256];
 
         if (e->dead)
             continue;
         
         t = avahi_record_to_string(e->record);
-        fprintf(f, "%s ; iface=%i proto=%i\n", t, e->interface, e->protocol);
+        g_snprintf(ln, sizeof(ln), "%s ; iface=%i proto=%i", t, e->interface, e->protocol);
         g_free(t);
+
+        callback(ln, userdata);
     }
 
-    avahi_dump_caches(s->monitor, f);
+    avahi_dump_caches(s->monitor, callback, userdata);
 }
 
-void avahi_server_add_ptr(
+gint avahi_server_add_ptr(
     AvahiServer *s,
     AvahiEntryGroup *g,
-    gint interface,
-    guchar protocol,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
     AvahiEntryFlags flags,
+    guint32 ttl,
     const gchar *name,
     const gchar *dest) {
 
     AvahiRecord *r;
+    gint ret;
 
     g_assert(dest);
 
-    r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
+    r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR, ttl);
     r->data.ptr.name = avahi_normalize_name(dest);
-    avahi_server_add(s, g, interface, protocol, flags, r);
+    ret = avahi_server_add(s, g, interface, protocol, flags, r);
     avahi_record_unref(r);
+    return ret;
 }
 
-void avahi_server_add_address(
+gint avahi_server_add_address(
     AvahiServer *s,
     AvahiEntryGroup *g,
-    gint interface,
-    guchar protocol,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
     AvahiEntryFlags flags,
     const gchar *name,
     AvahiAddress *a) {
 
     gchar *n = NULL;
+    gint ret = 0;
     g_assert(s);
     g_assert(a);
 
     name = name ? (n = avahi_normalize_name(name)) : s->host_name_fqdn;
     
-    if (a->family == AF_INET) {
+    if (a->family == AVAHI_PROTO_INET) {
         gchar *reverse;
         AvahiRecord  *r;
 
-        r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
+        r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME);
         r->data.a.address = a->data.ipv4;
-        avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, r);
+        ret = avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE | AVAHI_ENTRY_ALLOWMUTIPLE, r);
         avahi_record_unref(r);
         
         reverse = avahi_reverse_lookup_name_ipv4(&a->data.ipv4);
-        avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name);
+        ret |= avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse, name);
         g_free(reverse);
         
     } else {
         gchar *reverse;
         AvahiRecord *r;
             
-        r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
+        r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME);
         r->data.aaaa.address = a->data.ipv6;
-        avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, r);
+        ret = avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE | AVAHI_ENTRY_ALLOWMUTIPLE, r);
         avahi_record_unref(r);
 
         reverse = avahi_reverse_lookup_name_ipv6_arpa(&a->data.ipv6);
-        avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name);
+        ret |= avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse, name);
         g_free(reverse);
     
         reverse = avahi_reverse_lookup_name_ipv6_int(&a->data.ipv6);
-        avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name);
+        ret |= avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse, name);
         g_free(reverse);
     }
     
     g_free(n);
+
+    return ret;
 }
 
-void avahi_server_add_text_strlst(
+static gint server_add_txt_strlst_nocopy(
     AvahiServer *s,
     AvahiEntryGroup *g,
-    gint interface,
-    guchar protocol,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
     AvahiEntryFlags flags,
+    guint32 ttl,
     const gchar *name,
     AvahiStringList *strlst) {
 
     AvahiRecord *r;
+    gint ret;
     
     g_assert(s);
     
-    r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT);
+    r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT, ttl);
     r->data.txt.string_list = strlst;
-    avahi_server_add(s, g, interface, protocol, flags, r);
+    ret = avahi_server_add(s, g, interface, protocol, flags, r);
     avahi_record_unref(r);
+
+    return ret;
 }
 
-void avahi_server_add_text_va(
+gint avahi_server_add_txt_strlst(
     AvahiServer *s,
     AvahiEntryGroup *g,
-    gint interface,
-    guchar protocol,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
     AvahiEntryFlags flags,
+    guint32 ttl,
+    const gchar *name,
+    AvahiStringList *strlst) {
+
+    return server_add_txt_strlst_nocopy(s, g, interface, protocol, flags, ttl, name, avahi_string_list_copy(strlst));
+}
+
+gint avahi_server_add_txt_va(
+    AvahiServer *s,
+    AvahiEntryGroup *g,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
+    AvahiEntryFlags flags,
+    guint32 ttl,
     const gchar *name,
     va_list va) {
-    
+
     g_assert(s);
 
-    avahi_server_add_text_strlst(s, g, interface, protocol, flags, name, avahi_string_list_new_va(va));
+    return server_add_txt_strlst_nocopy(s, g, interface, protocol, flags, ttl, name, avahi_string_list_new_va(va));
 }
 
-void avahi_server_add_text(
+gint avahi_server_add_txt(
     AvahiServer *s,
     AvahiEntryGroup *g,
-    gint interface,
-    guchar protocol,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
     AvahiEntryFlags flags,
+    guint32 ttl,
     const gchar *name,
     ...) {
 
     va_list va;
+    gint ret;
     
     g_assert(s);
 
     va_start(va, name);
-    avahi_server_add_text_va(s, g, interface, protocol, flags, name, va);
+    ret = avahi_server_add_txt_va(s, g, interface, protocol, flags, ttl, name, va);
     va_end(va);
+
+    return ret;
 }
 
 static void escape_service_name(gchar *d, guint size, const gchar *s) {
@@ -1256,20 +1704,22 @@ static void escape_service_name(gchar *d, guint size, const gchar *s) {
     *(d++) = 0;
 }
 
-void avahi_server_add_service_strlst(
+static gint server_add_service_strlst_nocopy(
     AvahiServer *s,
     AvahiEntryGroup *g,
-    gint interface,
-    guchar protocol,
-    const gchar *type,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
     const gchar *name,
+    const gchar *type,
     const gchar *domain,
     const gchar *host,
     guint16 port,
     AvahiStringList *strlst) {
 
     gchar ptr_name[256], svc_name[256], ename[64], enum_ptr[256];
+    gchar *t, *d;
     AvahiRecord *r;
+    gint ret = 0;
     
     g_assert(s);
     g_assert(type);
@@ -1286,32 +1736,55 @@ void avahi_server_add_service_strlst(
     if (!host)
         host = s->host_name_fqdn;
 
-    snprintf(ptr_name, sizeof(ptr_name), "%s.%s", type, domain);
-    snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, type, domain);
+    d = avahi_normalize_name(domain);
+    t = avahi_normalize_name(type);
     
-    avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, ptr_name, svc_name);
+    g_snprintf(ptr_name, sizeof(ptr_name), "%s.%s", t, d);
+    g_snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, t, d);
+
+    ret = avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, AVAHI_DEFAULT_TTL, ptr_name, svc_name);
 
-    r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV);
+    r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME);
     r->data.srv.priority = 0;
     r->data.srv.weight = 0;
     r->data.srv.port = port;
     r->data.srv.name = avahi_normalize_name(host);
-    avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, r);
+    ret |= avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, r);
     avahi_record_unref(r);
 
-    avahi_server_add_text_strlst(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, svc_name, strlst);
+    ret |= server_add_txt_strlst_nocopy(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL, svc_name, strlst);
 
-    snprintf(enum_ptr, sizeof(enum_ptr), "_services._dns-sd._udp.%s", domain);
-    avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, enum_ptr, ptr_name);
+    g_snprintf(enum_ptr, sizeof(enum_ptr), "_services._dns-sd._udp.%s", d);
+    ret |=avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, AVAHI_DEFAULT_TTL, enum_ptr, ptr_name);
+
+    g_free(d);
+    g_free(t);
+    
+    return ret;
 }
 
-void avahi_server_add_service_va(
+gint avahi_server_add_service_strlst(
     AvahiServer *s,
     AvahiEntryGroup *g,
-    gint interface,
-    guchar protocol,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
+    const gchar *name,
     const gchar *type,
+    const gchar *domain,
+    const gchar *host,
+    guint16 port,
+    AvahiStringList *strlst) {
+
+    return server_add_service_strlst_nocopy(s, g, interface, protocol, name, type, domain, host, port, avahi_string_list_copy(strlst));
+}
+
+gint avahi_server_add_service_va(
+    AvahiServer *s,
+    AvahiEntryGroup *g,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
     const gchar *name,
+    const gchar *type,
     const gchar *domain,
     const gchar *host,
     guint16 port,
@@ -1321,30 +1794,130 @@ void avahi_server_add_service_va(
     g_assert(type);
     g_assert(name);
 
-    avahi_server_add_service_strlst(s, g, interface, protocol, type, name, domain, host, port, avahi_string_list_new_va(va));
+    return server_add_service_strlst_nocopy(s, g, interface, protocol, name, type, domain, host, port, avahi_string_list_new_va(va));
 }
 
-void avahi_server_add_service(
+gint avahi_server_add_service(
     AvahiServer *s,
     AvahiEntryGroup *g,
-    gint interface,
-    guchar protocol,
-    const gchar *type,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
     const gchar *name,
+    const gchar *type,
     const gchar *domain,
     const gchar *host,
     guint16 port,
     ... ){
 
     va_list va;
+    gint ret;
     
     g_assert(s);
     g_assert(type);
     g_assert(name);
 
     va_start(va, port);
-    avahi_server_add_service_va(s, g, interface, protocol, type, name, domain, host, port, va);
+    ret = avahi_server_add_service_va(s, g, interface, protocol, name, type, domain, host, port, va);
     va_end(va);
+    return ret;
+}
+
+static void hexstring(gchar *s, size_t sl, const void *p, size_t pl) {
+    static const gchar hex[] = "0123456789abcdef";
+    gboolean b = FALSE;
+    const guint8 *k = p;
+
+    while (sl > 1 && pl > 0) {
+        *(s++) = hex[(b ? *k : *k >> 4) & 0xF];
+
+        if (b) {
+            k++;
+            pl--;
+        }
+        
+        b = !b;
+
+        sl--;
+    }
+
+    if (sl > 0)
+        *s = 0;
+}
+
+gint avahi_server_add_dns_server_address(
+    AvahiServer *s,
+    AvahiEntryGroup *g,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
+    const gchar *domain,
+    AvahiDNSServerType type,
+    const AvahiAddress *address,
+    guint16 port /** should be 53 */) {
+
+    AvahiRecord *r;
+    gint ret;
+    gchar n[64] = "ip-";
+
+    g_assert(s);
+    g_assert(address);
+    g_assert(type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE);
+    g_assert(address->family == AVAHI_PROTO_INET || address->family == AVAHI_PROTO_INET6);
+
+    if (address->family == AVAHI_PROTO_INET) {
+        hexstring(n+3, sizeof(n)-3, &address->data, 4);
+        r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME);
+        r->data.a.address = address->data.ipv4;
+    } else {
+        hexstring(n+3, sizeof(n)-3, &address->data, 6);
+        r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME);
+        r->data.aaaa.address = address->data.ipv6;
+    }
+    
+    ret = avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE | AVAHI_ENTRY_ALLOWMUTIPLE, r);
+    avahi_record_unref(r);
+    
+    ret |= avahi_server_add_dns_server_name(s, g, interface, protocol, domain, type, n, port);
+
+    return ret;
+}
+
+gint avahi_server_add_dns_server_name(
+    AvahiServer *s,
+    AvahiEntryGroup *g,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
+    const gchar *domain,
+    AvahiDNSServerType type,
+    const gchar *name,
+    guint16 port /** should be 53 */) {
+
+    gint ret = -1;
+    gchar t[256], *d;
+    AvahiRecord *r;
+    
+    g_assert(s);
+    g_assert(name);
+    g_assert(type == AVAHI_DNS_SERVER_UPDATE || type == AVAHI_DNS_SERVER_RESOLVE);
+
+    if (domain) {
+        while (domain[0] == '.')
+            domain++;
+    } else
+        domain = s->domain_name;
+
+    d = avahi_normalize_name(domain);
+    g_snprintf(t, sizeof(t), "%s.%s", type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", d);
+    g_free(d);
+    
+    r = avahi_record_new_full(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME);
+    r->data.srv.priority = 0;
+    r->data.srv.weight = 0;
+    r->data.srv.port = port;
+    r->data.srv.name = avahi_normalize_name(name);
+    ret = avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_NULL, r);
+    avahi_record_unref(r);
+
+    return ret;
 }
 
 static void post_query_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
@@ -1357,7 +1930,7 @@ static void post_query_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpo
     avahi_interface_post_query(i, k, FALSE);
 }
 
-void avahi_server_post_query(AvahiServer *s, gint interface, guchar protocol, AvahiKey *key) {
+void avahi_server_post_query(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiKey *key) {
     g_assert(s);
     g_assert(key);
 
@@ -1390,6 +1963,10 @@ AvahiEntryGroup *avahi_entry_group_new(AvahiServer *s, AvahiEntryGroupCallback c
     g->dead = FALSE;
     g->state = AVAHI_ENTRY_GROUP_UNCOMMITED;
     g->n_probing = 0;
+    g->n_register_try = 0;
+    g->register_time_event = NULL;
+    g->register_time.tv_sec = 0;
+    g->register_time.tv_usec = 0;
     AVAHI_LLIST_HEAD_INIT(AvahiEntry, g->entries);
 
     AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, s->groups, g);
@@ -1403,8 +1980,15 @@ void avahi_entry_group_free(AvahiEntryGroup *g) {
     g_assert(g->server);
 
     for (e = g->entries; e; e = e->by_group_next) {
-        avahi_goodbye_entry(g->server, e, TRUE);
-        e->dead = TRUE;
+        if (!e->dead) {
+            avahi_goodbye_entry(g->server, e, TRUE);
+            e->dead = TRUE;
+        }
+    }
+
+    if (g->register_time_event) {
+        avahi_time_event_queue_remove(g->server->time_event_queue, g->register_time_event);
+        g->register_time_event = NULL;
     }
 
     g->dead = TRUE;
@@ -1413,16 +1997,85 @@ void avahi_entry_group_free(AvahiEntryGroup *g) {
     g->server->need_entry_cleanup = TRUE;
 }
 
-void avahi_entry_group_commit(AvahiEntryGroup *g) {
+static void entry_group_commit_real(AvahiEntryGroup *g) {
     g_assert(g);
-    g_assert(!g->dead);
-
-    if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED)
-        return;
 
     avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
     avahi_announce_group(g->server, g);
     avahi_entry_group_check_probed(g, FALSE);
+
+    g_get_current_time(&g->register_time);
+}
+
+static void entry_group_register_time_event_callback(AvahiTimeEvent *e, gpointer userdata) {
+    AvahiEntryGroup *g = userdata;
+    g_assert(g);
+
+    avahi_log_debug("Holdoff passed, waking up and going on.");
+
+    avahi_time_event_queue_remove(g->server->time_event_queue, g->register_time_event);
+    g->register_time_event = NULL;
+    
+    /* Holdoff time passed, so let's start probing */
+    entry_group_commit_real(g);
+}
+
+gint avahi_entry_group_commit(AvahiEntryGroup *g) {
+    GTimeVal now;
+    
+    g_assert(g);
+    g_assert(!g->dead);
+
+    if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED && g->state != AVAHI_ENTRY_GROUP_COLLISION)
+        return AVAHI_ERR_BAD_STATE;
+
+    g->n_register_try++;
+
+    avahi_timeval_add(&g->register_time,
+                      1000*(g->n_register_try >= AVAHI_RR_RATE_LIMIT_COUNT ?
+                            AVAHI_RR_HOLDOFF_MSEC_RATE_LIMIT :
+                            AVAHI_RR_HOLDOFF_MSEC));
+
+    g_get_current_time(&now);
+
+    if (avahi_timeval_compare(&g->register_time, &now) <= 0) {
+        /* Holdoff time passed, so let's start probing */
+        avahi_log_debug("Holdoff passed, directly going on.");
+
+        entry_group_commit_real(g);
+    } else {
+        avahi_log_debug("Holdoff not passed, sleeping.");
+
+        /* Holdoff time has not yet passed, so let's wait */
+        avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_SLEEPING);
+        
+        g_assert(!g->register_time_event);
+        g->register_time_event = avahi_time_event_queue_add(g->server->time_event_queue, &g->register_time, entry_group_register_time_event_callback, g);
+    }
+
+    return AVAHI_OK;
+}
+
+void avahi_entry_group_reset(AvahiEntryGroup *g) {
+    AvahiEntry *e;
+    g_assert(g);
+    
+    if (g->register_time_event) {
+        avahi_time_event_queue_remove(g->server->time_event_queue, g->register_time_event);
+        g->register_time_event = NULL;
+    }
+    
+    for (e = g->entries; e; e = e->by_group_next) {
+        if (!e->dead) {
+            avahi_goodbye_entry(g->server, e, TRUE);
+            e->dead = TRUE;
+        }
+    }
+    
+    g->server->need_entry_cleanup = TRUE;
+    g->n_probing = 0;
+
+    avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_UNCOMMITED);
 }
 
 gboolean avahi_entry_commited(AvahiEntry *e) {
@@ -1453,6 +2106,18 @@ gpointer avahi_entry_group_get_data(AvahiEntryGroup *g) {
     return g->userdata;
 }
 
+gboolean avahi_entry_group_is_empty(AvahiEntryGroup *g) {
+    AvahiEntry *e;
+    g_assert(g);
+
+    /* Look for an entry that is not dead */
+    for (e = g->entries; e; e = e->by_group_next)
+        if (!e->dead)
+            return FALSE;
+
+    return TRUE;
+}
+
 const gchar* avahi_server_get_domain_name(AvahiServer *s) {
     g_assert(s);
 
@@ -1493,15 +2158,18 @@ AvahiServerConfig* avahi_server_config_init(AvahiServerConfig *c) {
     g_assert(c);
 
     memset(c, 0, sizeof(AvahiServerConfig));
-    c->register_hinfo = TRUE;
-    c->register_addresses = TRUE;
     c->use_ipv6 = TRUE;
     c->use_ipv4 = TRUE;
     c->host_name = NULL;
     c->domain_name = NULL;
-    c->check_response_ttl = TRUE;
-    c->announce_domain = TRUE;
+    c->check_response_ttl = FALSE;
+    c->publish_hinfo = TRUE;
+    c->publish_addresses = TRUE;
+    c->publish_workstation = TRUE;
+    c->publish_domain = TRUE;
     c->use_iff_running = FALSE;
+    c->enable_reflector = FALSE;
+    c->reflect_ipv = FALSE;
     
     return c;
 }