]> git.meshlink.io Git - catta/blobdiff - avahi-core/server.c
* add C examples
[catta] / avahi-core / server.c
index 719f9a91fa1432108338cb6a86a41f8548d63c3f..e3929dbd45d10654347044f882df9afc2b0e15a4 100644 (file)
@@ -29,6 +29,7 @@
 #include <sys/utsname.h>
 #include <unistd.h>
 #include <errno.h>
+#include <stdio.h>
 
 #include "server.h"
 #include "util.h"
@@ -37,7 +38,9 @@
 #include "browse.h"
 #include "log.h"
 
-#define AVAHI_HOST_RR_HOLDOFF_MSEC 2000
+#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;
@@ -73,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);
 }
@@ -135,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);
@@ -191,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);
@@ -223,6 +233,10 @@ 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;
@@ -244,16 +258,15 @@ static void incoming_probe(AvahiServer *s, AvahiRecord *record, AvahiInterface *
         }
     }
 
-    t = avahi_record_to_string(record);
-
     if (!ours) {
 
         if (won)
-            avahi_log_debug("xxx Recieved conflicting probe [%s]. Local host won.", t);
+            avahi_log_debug("Recieved conflicting probe [%s]. Local host won.", t);
         else if (lost) {
-            avahi_log_debug("yyy 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);
@@ -334,7 +347,7 @@ static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *
             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. */
         }
 
@@ -361,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);
@@ -405,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);
                 
@@ -539,6 +552,7 @@ static void reflect_probe(AvahiServer *s, AvahiInterface *i, AvahiRecord *r) {
 
 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);
@@ -548,6 +562,8 @@ static void handle_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterfac
 /*     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 --) {
@@ -609,7 +625,7 @@ static void handle_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterfac
     }
 
     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;
     
@@ -657,11 +673,11 @@ static void handle_response_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInter
        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);
+        avahi_server_generate_response(s, i, NULL, NULL, 0, FALSE, TRUE);
 }
 
 static AvahiLegacyUnicastReflectSlot* allocate_slot(AvahiServer *s) {
-    guint n, index = (guint) -1;
+    guint n, idx = (guint) -1;
     AvahiLegacyUnicastReflectSlot *slot;
     
     g_assert(s);
@@ -670,66 +686,66 @@ static AvahiLegacyUnicastReflectSlot* allocate_slot(AvahiServer *s) {
         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++) {
-        index = s->legacy_unicast_reflect_id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS;
+        idx = s->legacy_unicast_reflect_id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS;
         
-        if (!s->legacy_unicast_reflect_slots[index])
+        if (!s->legacy_unicast_reflect_slots[idx])
             break;
     }
 
-    if (index == (guint) -1 || s->legacy_unicast_reflect_slots[index])
+    if (idx == (guint) -1 || s->legacy_unicast_reflect_slots[idx])
         return NULL;
 
-    slot = s->legacy_unicast_reflect_slots[index] = g_new(AvahiLegacyUnicastReflectSlot, 1);
+    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 index;
+    guint idx;
 
     g_assert(s);
     g_assert(slot);
 
-    index = slot->id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS;
+    idx = slot->id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS;
 
-    g_assert(s->legacy_unicast_reflect_slots[index] == slot);
+    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[index] = NULL;
+    s->legacy_unicast_reflect_slots[idx] = NULL;
 }
 
 static void free_slots(AvahiServer *s) {
-    guint index;
+    guint idx;
     g_assert(s);
 
     if (!s->legacy_unicast_reflect_slots)
         return;
 
-    for (index = 0; index < AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS; index ++)
-        if (s->legacy_unicast_reflect_slots[index])
-            deallocate_slot(s, s->legacy_unicast_reflect_slots[index]);
+    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 index;
+    guint idx;
     
     g_assert(s);
 
     if (!s->legacy_unicast_reflect_slots)
         return NULL;
     
-    index = id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS;
+    idx = id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS;
 
-    if (!s->legacy_unicast_reflect_slots[index] || s->legacy_unicast_reflect_slots[index]->id != id)
+    if (!s->legacy_unicast_reflect_slots[idx] || s->legacy_unicast_reflect_slots[idx]->id != id)
         return NULL;
 
-    return s->legacy_unicast_reflect_slots[index];
+    return s->legacy_unicast_reflect_slots[idx];
 }
 
 static void legacy_unicast_reflect_slot_timeout(AvahiTimeEvent *e, void *userdata) {
@@ -756,7 +772,7 @@ static void reflect_legacy_unicast_query_packet(AvahiServer *s, AvahiDnsPacket *
     if (!s->config.enable_reflector)
         return;
 
-/*     avahi_log_debug("legacy unicast reflectr"); */
+/*     avahi_log_debug("legacy unicast reflector"); */
     
     /* Reflecting legacy unicast queries is a little more complicated
        than reflecting normal queries, since we must route the
@@ -818,7 +834,7 @@ static gboolean originates_from_local_legacy_unicast_socket(AvahiServer *s, cons
         if (getsockname(s->fd_legacy_unicast_ipv4, &lsa, &l) != 0)
             avahi_log_warn("getsockname(): %s", strerror(errno));
         else
-            return lsa.sin_port == ((struct sockaddr_in*) sa)->sin_port;
+            return lsa.sin_port == ((const struct sockaddr_in*) sa)->sin_port;
 
     }
 
@@ -829,7 +845,7 @@ static gboolean originates_from_local_legacy_unicast_socket(AvahiServer *s, cons
         if (getsockname(s->fd_legacy_unicast_ipv6, &lsa, &l) != 0)
             avahi_log_warn("getsockname(): %s", strerror(errno));
         else
-            return lsa.sin6_port == ((struct sockaddr_in6*) sa)->sin6_port;
+            return lsa.sin6_port == ((const struct sockaddr_in6*) sa)->sin6_port;
     }
 
     return FALSE;
@@ -1083,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;
@@ -1134,10 +1146,13 @@ static void register_hinfo(AvahiServer *s) {
     
     g_assert(s);
     
-    if (!s->config.publish_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, AVAHI_DEFAULT_TTL_HOST_NAME);
@@ -1165,10 +1180,14 @@ static void register_localhost(AvahiServer *s) {
 static void register_browse_domain(AvahiServer *s) {
     g_assert(s);
 
-    if (!s->config.publish_domain || s->browse_domain_entry_group)
+    if (!s->config.publish_domain)
         return;
 
-    s->browse_domain_entry_group = avahi_entry_group_new(s, NULL, NULL);
+    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);
 }
@@ -1195,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);
-}
-
 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);
@@ -1234,26 +1225,24 @@ gint 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);
-    return 0;
+    register_stuff(s);
+    return AVAHI_OK;
 }
 
 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);
-    return 0;
+    register_stuff(s);
+    return AVAHI_OK;
 }
 
-
 static void prepare_pollfd(AvahiServer *s, GPollFD *pollfd, gint fd) {
     g_assert(s);
     g_assert(pollfd);
@@ -1344,14 +1333,13 @@ AvahiServer *avahi_server_new(GMainContext *c, const AvahiServerConfig *sc, Avah
     /* 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;
 
@@ -1400,8 +1388,6 @@ void avahi_server_free(AvahiServer* s) {
 
     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);
@@ -1428,7 +1414,7 @@ void avahi_server_free(AvahiServer* s) {
     g_free(s);
 }
 
-static gint check_record_conflict(AvahiServer *s, gint interface, guchar protocol, AvahiRecord *r, AvahiEntryFlags flags) {
+static gint check_record_conflict(AvahiServer *s, AvahiIfIndex interface, AvahiProtocol protocol, AvahiRecord *r, AvahiEntryFlags flags) {
     AvahiEntry *e;
     
     g_assert(s);
@@ -1461,8 +1447,8 @@ static gint check_record_conflict(AvahiServer *s, gint interface, guchar protoco
 gint avahi_server_add(
     AvahiServer *s,
     AvahiEntryGroup *g,
-    gint interface,
-    guchar protocol,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
     AvahiEntryFlags flags,
     AvahiRecord *r) {
     
@@ -1524,32 +1510,36 @@ 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);
 }
 
 gint avahi_server_add_ptr(
     AvahiServer *s,
     AvahiEntryGroup *g,
-    gint interface,
-    guchar protocol,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
     AvahiEntryFlags flags,
     guint32 ttl,
     const gchar *name,
@@ -1570,8 +1560,8 @@ gint avahi_server_add_ptr(
 gint avahi_server_add_address(
     AvahiServer *s,
     AvahiEntryGroup *g,
-    gint interface,
-    guchar protocol,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
     AvahiEntryFlags flags,
     const gchar *name,
     AvahiAddress *a) {
@@ -1619,11 +1609,11 @@ gint avahi_server_add_address(
     return ret;
 }
 
-gint avahi_server_add_txt_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,
@@ -1642,26 +1632,39 @@ gint avahi_server_add_txt_strlst(
     return ret;
 }
 
+gint avahi_server_add_txt_strlst(
+    AvahiServer *s,
+    AvahiEntryGroup *g,
+    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,
-    gint interface,
-    guchar protocol,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
     AvahiEntryFlags flags,
     guint32 ttl,
     const gchar *name,
     va_list va) {
-    
+
     g_assert(s);
 
-    return avahi_server_add_txt_strlst(s, g, interface, protocol, flags, ttl, 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));
 }
 
 gint avahi_server_add_txt(
     AvahiServer *s,
     AvahiEntryGroup *g,
-    gint interface,
-    guchar protocol,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
     AvahiEntryFlags flags,
     guint32 ttl,
     const gchar *name,
@@ -1701,19 +1704,20 @@ static void escape_service_name(gchar *d, guint size, const gchar *s) {
     *(d++) = 0;
 }
 
-gint 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;
     
@@ -1732,9 +1736,12 @@ gint 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);
     
+    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, AVAHI_DEFAULT_TTL_HOST_NAME);
@@ -1745,21 +1752,39 @@ gint avahi_server_add_service_strlst(
     ret |= avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, r);
     avahi_record_unref(r);
 
-    ret |= avahi_server_add_txt_strlst(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL, 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);
+    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;
 }
 
-gint 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,
@@ -1769,16 +1794,16 @@ gint avahi_server_add_service_va(
     g_assert(type);
     g_assert(name);
 
-    return 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));
 }
 
 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,
@@ -1792,7 +1817,7 @@ gint avahi_server_add_service(
     g_assert(name);
 
     va_start(va, port);
-    ret = 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;
 }
@@ -1822,8 +1847,8 @@ static void hexstring(gchar *s, size_t sl, const void *p, size_t pl) {
 gint avahi_server_add_dns_server_address(
     AvahiServer *s,
     AvahiEntryGroup *g,
-    gint interface,
-    guchar protocol,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
     const gchar *domain,
     AvahiDNSServerType type,
     const AvahiAddress *address,
@@ -1859,15 +1884,15 @@ gint avahi_server_add_dns_server_address(
 gint avahi_server_add_dns_server_name(
     AvahiServer *s,
     AvahiEntryGroup *g,
-    gint interface,
-    guchar protocol,
+    AvahiIfIndex interface,
+    AvahiProtocol protocol,
     const gchar *domain,
     AvahiDNSServerType type,
     const gchar *name,
     guint16 port /** should be 53 */) {
 
     gint ret = -1;
-    gchar t[256];
+    gchar t[256], *d;
     AvahiRecord *r;
     
     g_assert(s);
@@ -1880,7 +1905,9 @@ gint avahi_server_add_dns_server_name(
     } else
         domain = s->domain_name;
 
-    snprintf(t, sizeof(t), "%s.%s", type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", domain);
+    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;
@@ -1893,7 +1920,6 @@ gint avahi_server_add_dns_server_name(
     return ret;
 }
 
-
 static void post_query_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
     AvahiKey *k = userdata;
 
@@ -1904,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);
 
@@ -1937,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);
@@ -1950,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;
@@ -1960,18 +1997,85 @@ void avahi_entry_group_free(AvahiEntryGroup *g) {
     g->server->need_entry_cleanup = TRUE;
 }
 
-gint 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 -1;
 
     avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
     avahi_announce_group(g->server, g);
     avahi_entry_group_check_probed(g, FALSE);
 
-    return 0;
+    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) {
@@ -2002,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);