X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-core%2Fannounce.c;h=a77fbb698874dedfbdd27f8c5745e4ba20899fb1;hb=acdec2c38860b6bc36328d20f5da6584787a368a;hp=025331d0b43249eb054b26112a8d95c623f69728;hpb=424aefe8a431b79496672799dc4f4430fa935252;p=catta diff --git a/avahi-core/announce.c b/avahi-core/announce.c index 025331d..a77fbb6 100644 --- a/avahi-core/announce.c +++ b/avahi-core/announce.c @@ -23,32 +23,35 @@ #include #endif +#include + #include #include #include "announce.h" #include "log.h" +#include "rr-util.h" #define AVAHI_ANNOUNCEMENT_JITTER_MSEC 250 #define AVAHI_PROBE_JITTER_MSEC 250 #define AVAHI_PROBE_INTERVAL_MSEC 250 -static void remove_announcement(AvahiServer *s, AvahiAnnouncement *a) { +static void remove_announcer(AvahiServer *s, AvahiAnnouncer *a) { assert(s); assert(a); if (a->time_event) avahi_time_event_free(a->time_event); - AVAHI_LLIST_REMOVE(AvahiAnnouncement, by_interface, a->interface->announcements, a); - AVAHI_LLIST_REMOVE(AvahiAnnouncement, by_entry, a->entry->announcements, a); + AVAHI_LLIST_REMOVE(AvahiAnnouncer, by_interface, a->interface->announcers, a); + AVAHI_LLIST_REMOVE(AvahiAnnouncer, by_entry, a->entry->announcers, a); avahi_free(a); } static void elapse_announce(AvahiTimeEvent *e, void *userdata); -static void set_timeout(AvahiAnnouncement *a, const struct timeval *tv) { +static void set_timeout(AvahiAnnouncer *a, const struct timeval *tv) { assert(a); if (!tv) { @@ -65,7 +68,7 @@ static void set_timeout(AvahiAnnouncement *a, const struct timeval *tv) { } } -static void next_state(AvahiAnnouncement *a); +static void next_state(AvahiAnnouncer *a); void avahi_s_entry_group_check_probed(AvahiSEntryGroup *g, int immediately) { AvahiEntry *e; @@ -82,11 +85,11 @@ void avahi_s_entry_group_check_probed(AvahiSEntryGroup *g, int immediately) { if (g->dead) return; - for (e = g->entries; e; e = e->entries_next) { - AvahiAnnouncement *a; + for (e = g->entries; e; e = e->by_group_next) { + AvahiAnnouncer *a; - for (a = e->announcements; a; a = a->by_entry_next) { - + for (a = e->announcers; a; a = a->by_entry_next) { + if (a->state != AVAHI_WAITING) continue; @@ -107,11 +110,9 @@ void avahi_s_entry_group_check_probed(AvahiSEntryGroup *g, int immediately) { } } -static void next_state(AvahiAnnouncement *a) { +static void next_state(AvahiAnnouncer *a) { assert(a); -/* avahi_log_debug("%i -- %u", a->state, a->n_iteration); */ - if (a->state == AVAHI_WAITING) { assert(a->entry->group); @@ -123,11 +124,6 @@ static void next_state(AvahiAnnouncement *a) { if (a->n_iteration >= 4) { /* Probing done */ -/* char *t; */ - -/* avahi_log_debug("Enough probes for record [%s]", t = avahi_record_to_string(a->entry->record)); */ -/* avahi_free(t); */ - if (a->entry->group) { assert(a->entry->group->n_probing); a->entry->group->n_probing--; @@ -164,12 +160,8 @@ static void next_state(AvahiAnnouncement *a) { avahi_server_generate_response(a->server, a->interface, NULL, NULL, 0, 0, 0); if (++a->n_iteration >= 4) { -/* char *t; */ /* Announcing done */ -/* avahi_log_debug("Enough announcements for record [%s]", t = avahi_record_to_string(a->entry->record)); */ -/* avahi_free(t); */ - a->state = AVAHI_ESTABLISHED; set_timeout(a, NULL); @@ -191,21 +183,21 @@ static void elapse_announce(AvahiTimeEvent *e, void *userdata) { next_state(userdata); } -AvahiAnnouncement *avahi_get_announcement(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) { - AvahiAnnouncement *a; +static AvahiAnnouncer *get_announcer(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) { + AvahiAnnouncer *a; assert(s); assert(e); assert(i); - for (a = e->announcements; a; a = a->by_entry_next) + for (a = e->announcers; a; a = a->by_entry_next) if (a->interface == i) return a; return NULL; } -static void go_to_initial_state(AvahiAnnouncement *a, int immediately) { +static void go_to_initial_state(AvahiAnnouncer *a) { AvahiEntry *e; struct timeval tv; @@ -230,36 +222,30 @@ static void go_to_initial_state(AvahiAnnouncement *a, int immediately) { if (a->state == AVAHI_PROBING && e->group) e->group->n_probing++; - if (a->state == AVAHI_PROBING) { - avahi_elapse_time(&tv, 0, immediately ? 0 : AVAHI_PROBE_JITTER_MSEC); - set_timeout(a, &tv); - } else if (a->state == AVAHI_ANNOUNCING) { - avahi_elapse_time(&tv, 0, immediately ? 0 : AVAHI_ANNOUNCEMENT_JITTER_MSEC); - set_timeout(a, &tv); - } else + if (a->state == AVAHI_PROBING) + set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_PROBE_JITTER_MSEC)); + else if (a->state == AVAHI_ANNOUNCING) + set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC)); + else set_timeout(a, NULL); } -static void new_announcement(AvahiServer *s, AvahiInterface *i, AvahiEntry *e) { - AvahiAnnouncement *a; -/* char *t; */ +static void new_announcer(AvahiServer *s, AvahiInterface *i, AvahiEntry *e) { + AvahiAnnouncer *a; assert(s); assert(i); assert(e); assert(!e->dead); -/* avahi_log_debug("NEW ANNOUNCEMENT: %s.%i [%s]", i->hardware->name, i->protocol, t = avahi_record_to_string(e->record)); */ -/* avahi_free(t); */ - if (!avahi_interface_match(i, e->interface, e->protocol) || !i->announcing || !avahi_entry_is_commited(e)) return; - /* We don't want duplicate announcements */ - if (avahi_get_announcement(s, e, i)) + /* We don't want duplicate announcers */ + if (get_announcer(s, e, i)) return; - if ((!(a = avahi_new(AvahiAnnouncement, 1)))) { + if ((!(a = avahi_new(AvahiAnnouncer, 1)))) { avahi_log_error(__FILE__": Out of memory."); return; } @@ -269,13 +255,10 @@ static void new_announcement(AvahiServer *s, AvahiInterface *i, AvahiEntry *e) { a->entry = e; a->time_event = NULL; - AVAHI_LLIST_PREPEND(AvahiAnnouncement, by_interface, i->announcements, a); - AVAHI_LLIST_PREPEND(AvahiAnnouncement, by_entry, e->announcements, a); + AVAHI_LLIST_PREPEND(AvahiAnnouncer, by_interface, i->announcers, a); + AVAHI_LLIST_PREPEND(AvahiAnnouncer, by_entry, e->announcers, a); - go_to_initial_state(a, 0); - -/* avahi_log_debug("New announcement on interface %s.%i for entry [%s] state=%i", i->hardware->name, i->protocol, t = avahi_record_to_string(e->record), a->state); */ -/* avahi_free(t); */ + go_to_initial_state(a); } void avahi_announce_interface(AvahiServer *s, AvahiInterface *i) { @@ -289,7 +272,7 @@ void avahi_announce_interface(AvahiServer *s, AvahiInterface *i) { for (e = s->entries; e; e = e->entries_next) if (!e->dead) - new_announcement(s, i, e); + new_announcer(s, i, e); } static void announce_walk_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) { @@ -300,7 +283,7 @@ static void announce_walk_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, assert(e); assert(!e->dead); - new_announcement(m->server, i, e); + new_announcer(m->server, i, e); } void avahi_announce_entry(AvahiServer *s, AvahiEntry *e) { @@ -323,14 +306,14 @@ void avahi_announce_group(AvahiServer *s, AvahiSEntryGroup *g) { } int avahi_entry_is_registered(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) { - AvahiAnnouncement *a; + AvahiAnnouncer *a; assert(s); assert(e); assert(i); assert(!e->dead); - if (!(a = avahi_get_announcement(s, e, i))) + if (!(a = get_announcer(s, e, i))) return 0; return @@ -340,17 +323,15 @@ int avahi_entry_is_registered(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) } int avahi_entry_is_probing(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) { - AvahiAnnouncement *a; + AvahiAnnouncer *a; assert(s); assert(e); assert(i); assert(!e->dead); - if (!(a = avahi_get_announcement(s, e, i))) + if (!(a = get_announcer(s, e, i))) return 0; - -/* avahi_log_debug("state: %i", a->state); */ return a->state == AVAHI_PROBING || @@ -358,30 +339,26 @@ int avahi_entry_is_probing(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) { } void avahi_entry_return_to_initial_state(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) { - AvahiAnnouncement *a; + AvahiAnnouncer *a; assert(s); assert(e); assert(i); - if (!(a = avahi_get_announcement(s, e, i))) + if (!(a = get_announcer(s, e, i))) return; if (a->state == AVAHI_PROBING && a->entry->group) a->entry->group->n_probing--; - go_to_initial_state(a, 1); + go_to_initial_state(a); } static AvahiRecord *make_goodbye_record(AvahiRecord *r) { -/* char *t; */ AvahiRecord *g; assert(r); -/* avahi_log_debug("Preparing goodbye for record [%s]", t = avahi_record_to_string(r)); */ -/* avahi_free(t); */ - if (!(g = avahi_record_copy(r))) return NULL; /* OOM */ @@ -391,6 +368,26 @@ static AvahiRecord *make_goodbye_record(AvahiRecord *r) { return g; } +static int is_duplicate_entry(AvahiServer *s, AvahiEntry *e) { + AvahiEntry *i; + + assert(s); + assert(e); + + for (i = avahi_hashmap_lookup(s->entries_by_key, e->record->key); i; i = i->by_key_next) { + + if (i == e) + continue; + + if (!avahi_record_equal_no_ttl(i->record, e->record)) + continue; + + return 1; + } + + return 0; +} + static void send_goodbye_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) { AvahiEntry *e = userdata; AvahiRecord *g; @@ -408,6 +405,9 @@ static void send_goodbye_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, v if (!avahi_entry_is_registered(m->server, e, i)) return; + + if (is_duplicate_entry(m->server, e)) + return; if (!(g = make_goodbye_record(e->record))) return; /* OOM */ @@ -415,55 +415,112 @@ static void send_goodbye_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, v avahi_interface_post_response(i, g, e->flags & AVAHI_PUBLISH_UNIQUE, NULL, 1); avahi_record_unref(g); } + +static void reannounce(AvahiAnnouncer *a) { + AvahiEntry *e; + struct timeval tv; + + assert(a); + e = a->entry; + + /* If the group this entry belongs to is not even commited, there's nothing to reannounce */ + if (e->group && (e->group->state == AVAHI_ENTRY_GROUP_UNCOMMITED || e->group->state == AVAHI_ENTRY_GROUP_COLLISION)) + return; + + /* Because we might change state we decrease the probing counter first */ + if (a->state == AVAHI_PROBING && a->entry->group) + a->entry->group->n_probing--; -void avahi_goodbye_interface(AvahiServer *s, AvahiInterface *i, int goodbye) { - assert(s); - assert(i); + if (a->state == AVAHI_PROBING || + (a->state == AVAHI_WAITING && (e->flags & AVAHI_PUBLISH_UNIQUE) && !(e->flags & AVAHI_PUBLISH_NO_PROBE))) + + /* We were probing or waiting after probe, so we restart probing from the beginning here */ + + a->state = AVAHI_PROBING; + else if (a->state == AVAHI_WAITING) -/* avahi_log_debug("goodbye interface: %s.%u", i->hardware->name, i->protocol); */ + /* We were waiting, but were not probing before, so we continue waiting */ + a->state = AVAHI_WAITING; - if (goodbye && avahi_interface_relevant(i)) { - AvahiEntry *e; + else if (e->flags & AVAHI_PUBLISH_NO_ANNOUNCE) - for (e = s->entries; e; e = e->entries_next) - if (!e->dead) - send_goodbye_callback(s->monitor, i, e); - } + /* No announcer needed */ + a->state = AVAHI_ESTABLISHED; - while (i->announcements) - remove_announcement(s, i->announcements); + else { + + /* Ok, let's restart announcing */ + a->state = AVAHI_ANNOUNCING; + } + + /* Now let's increase the probing counter again */ + if (a->state == AVAHI_PROBING && e->group) + e->group->n_probing++; + + a->n_iteration = 1; + a->sec_delay = 1; -/* avahi_log_debug("goodbye interface done: %s.%u", i->hardware->name, i->protocol); */ + if (a->state == AVAHI_PROBING) + set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_PROBE_JITTER_MSEC)); + else if (a->state == AVAHI_ANNOUNCING) + set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC)); + else + set_timeout(a, NULL); } -void avahi_goodbye_entry(AvahiServer *s, AvahiEntry *e, int goodbye) { - assert(s); - assert(e); - -/* avahi_log_debug("goodbye entry: %p", e); */ - - if (goodbye && !e->dead) - avahi_interface_monitor_walk(s->monitor, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, send_goodbye_callback, e); - while (e->announcements) - remove_announcement(s, e->announcements); +static void reannounce_walk_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) { + AvahiEntry *e = userdata; + AvahiAnnouncer *a; + + assert(m); + assert(i); + assert(e); + assert(!e->dead); -/* avahi_log_debug("goodbye entry done: %p", e); */ + if (!(a = get_announcer(m->server, e, i))) + return; + reannounce(a); } -void avahi_goodbye_all(AvahiServer *s, int goodbye) { - AvahiEntry *e; +void avahi_reannounce_entry(AvahiServer *s, AvahiEntry *e) { + + assert(s); + assert(e); + assert(!e->dead); + + avahi_interface_monitor_walk(s->monitor, e->interface, e->protocol, reannounce_walk_callback, e); +} +void avahi_goodbye_interface(AvahiServer *s, AvahiInterface *i, int send_goodbye, int remove) { assert(s); + assert(i); -/* avahi_log_debug("goodbye all"); */ + if (send_goodbye) + if (i->announcing) { + AvahiEntry *e; + + for (e = s->entries; e; e = e->entries_next) + if (!e->dead) + send_goodbye_callback(s->monitor, i, e); + } - for (e = s->entries; e; e = e->entries_next) - if (!e->dead) - avahi_goodbye_entry(s, e, goodbye); + if (remove) + while (i->announcers) + remove_announcer(s, i->announcers); +} -/* avahi_log_debug("goodbye all done"); */ +void avahi_goodbye_entry(AvahiServer *s, AvahiEntry *e, int send_goodbye, int remove) { + assert(s); + assert(e); + + if (send_goodbye) + if (!e->dead) + avahi_interface_monitor_walk(s->monitor, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, send_goodbye_callback, e); + if (remove) + while (e->announcers) + remove_announcer(s, e->announcers); }