X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-core%2Fserver.c;h=3c385070b20e23bd8b95213eee4992eef3750d97;hb=8c585e0162e7d773a18ef179e7e8561c2aa71e17;hp=b3ab0090fc176a0ec7e7ad6909e02443ff5b98d4;hpb=de0452f2ed4520c48fcd30e3999a044844b89f37;p=catta diff --git a/avahi-core/server.c b/avahi-core/server.c index b3ab009..3c38507 100644 --- a/avahi-core/server.c +++ b/avahi-core/server.c @@ -29,6 +29,7 @@ #include #include #include +#include #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); } @@ -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); @@ -211,7 +221,8 @@ static void withdraw_rrset(AvahiServer *s, AvahiKey *key) { g_assert(key); for (e = g_hash_table_lookup(s->entries_by_key, key); e; e = e->by_key_next) - withdraw_entry(s, e); + if (!e->dead) + withdraw_entry(s, e); } static void incoming_probe(AvahiServer *s, AvahiRecord *record, AvahiInterface *i) { @@ -223,6 +234,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,8 +259,6 @@ static void incoming_probe(AvahiServer *s, AvahiRecord *record, AvahiInterface * } } - t = avahi_record_to_string(record); - if (!ours) { if (won) @@ -253,7 +266,8 @@ static void incoming_probe(AvahiServer *s, AvahiRecord *record, AvahiInterface * else if (lost) { 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 +348,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. */ } @@ -664,7 +678,7 @@ static void handle_response_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInter } static AvahiLegacyUnicastReflectSlot* allocate_slot(AvahiServer *s) { - guint n, index = (guint) -1; + guint n, idx = (guint) -1; AvahiLegacyUnicastReflectSlot *slot; g_assert(s); @@ -673,66 +687,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) { @@ -759,7 +773,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 @@ -821,7 +835,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; } @@ -832,7 +846,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; @@ -1086,15 +1100,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; @@ -1122,10 +1132,12 @@ void avahi_host_rr_entry_group_callback(AvahiServer *s, AvahiEntryGroup *g, Avah if (state == AVAHI_ENTRY_GROUP_REGISTERING && s->state == AVAHI_SERVER_REGISTERING) avahi_server_increase_host_rr_pending(s); + else if (state == AVAHI_ENTRY_GROUP_COLLISION && (s->state == AVAHI_SERVER_REGISTERING || s->state == AVAHI_SERVER_RUNNING)) { withdraw_host_rrs(s); server_set_state(s, AVAHI_SERVER_COLLISION); + } else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED && s->state == AVAHI_SERVER_REGISTERING) avahi_server_decrease_host_rr_pending(s); @@ -1137,10 +1149,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); @@ -1168,10 +1183,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); } @@ -1198,38 +1217,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); @@ -1237,26 +1228,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"); 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); @@ -1354,7 +1343,6 @@ AvahiServer *avahi_server_new(GMainContext *c, const AvahiServerConfig *sc, Avah 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; @@ -1403,8 +1391,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); @@ -1431,7 +1417,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); @@ -1464,8 +1450,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) { @@ -1527,32 +1513,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, @@ -1573,8 +1563,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) { @@ -1622,11 +1612,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, @@ -1645,26 +1635,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, @@ -1704,11 +1707,11 @@ 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, + AvahiIfIndex interface, + AvahiProtocol protocol, const gchar *name, const gchar *type, const gchar *domain, @@ -1739,8 +1742,8 @@ gint avahi_server_add_service_strlst( d = avahi_normalize_name(domain); t = avahi_normalize_name(type); - snprintf(ptr_name, sizeof(ptr_name), "%s.%s", t, d); - snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, t, d); + 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); @@ -1752,9 +1755,9 @@ 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", d); + 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); @@ -1763,11 +1766,26 @@ gint avahi_server_add_service_strlst( return ret; } +gint avahi_server_add_service_strlst( + AvahiServer *s, + AvahiEntryGroup *g, + 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, - gint interface, - guchar protocol, + AvahiIfIndex interface, + AvahiProtocol protocol, const gchar *name, const gchar *type, const gchar *domain, @@ -1779,14 +1797,14 @@ gint avahi_server_add_service_va( g_assert(type); g_assert(name); - return avahi_server_add_service_strlst(s, g, interface, protocol, name, type, 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, + AvahiIfIndex interface, + AvahiProtocol protocol, const gchar *name, const gchar *type, const gchar *domain, @@ -1832,8 +1850,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, @@ -1869,8 +1887,8 @@ 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, @@ -1891,7 +1909,7 @@ gint avahi_server_add_dns_server_name( domain = s->domain_name; d = avahi_normalize_name(domain); - snprintf(t, sizeof(t), "%s.%s", type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", d); + 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); @@ -1915,7 +1933,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); @@ -1930,10 +1948,8 @@ void avahi_entry_group_change_state(AvahiEntryGroup *g, AvahiEntryGroupState sta g->state = state; - if (g->callback) { + if (g->callback) g->callback(g->server, g, state, g->userdata); - return; - } } AvahiEntryGroup *avahi_entry_group_new(AvahiServer *s, AvahiEntryGroupCallback callback, gpointer userdata) { @@ -1948,6 +1964,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); @@ -1961,8 +1981,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; @@ -1971,18 +1998,93 @@ void avahi_entry_group_free(AvahiEntryGroup *g) { g->server->need_entry_cleanup = TRUE; } +static void entry_group_commit_real(AvahiEntryGroup *g) { + g_assert(g); + + g_get_current_time(&g->register_time); + + avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING); + + if (!g->dead) { + avahi_announce_group(g->server, g); + avahi_entry_group_check_probed(g, FALSE); + } +} + +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) - return -1; + if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED && g->state != AVAHI_ENTRY_GROUP_COLLISION) + return AVAHI_ERR_BAD_STATE; - avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING); - avahi_announce_group(g->server, g); - avahi_entry_group_check_probed(g, FALSE); + g->n_register_try++; - return 0; + 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 */ + 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); + + avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING); + } + + 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; + } + } + + 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->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) { @@ -2013,6 +2115,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);