4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <avahi-common/timeval.h>
27 #include <avahi-common/malloc.h>
32 #define AVAHI_ANNOUNCEMENT_JITTER_MSEC 250
33 #define AVAHI_PROBE_JITTER_MSEC 250
34 #define AVAHI_PROBE_INTERVAL_MSEC 250
36 static void remove_announcer(AvahiServer *s, AvahiAnnouncer *a) {
41 avahi_time_event_free(a->time_event);
43 AVAHI_LLIST_REMOVE(AvahiAnnouncer, by_interface, a->interface->announcers, a);
44 AVAHI_LLIST_REMOVE(AvahiAnnouncer, by_entry, a->entry->announcers, a);
49 static void elapse_announce(AvahiTimeEvent *e, void *userdata);
51 static void set_timeout(AvahiAnnouncer *a, const struct timeval *tv) {
56 avahi_time_event_free(a->time_event);
62 avahi_time_event_update(a->time_event, tv);
64 a->time_event = avahi_time_event_new(a->server->time_event_queue, tv, elapse_announce, a);
68 static void next_state(AvahiAnnouncer *a);
70 void avahi_s_entry_group_check_probed(AvahiSEntryGroup *g, int immediately) {
75 /* Check whether all group members have been probed */
77 if (g->state != AVAHI_ENTRY_GROUP_REGISTERING || g->n_probing > 0)
80 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_ESTABLISHED);
85 for (e = g->entries; e; e = e->entries_next) {
88 for (a = e->announcers; a; a = a->by_entry_next) {
90 if (a->state != AVAHI_WAITING)
93 a->state = AVAHI_ANNOUNCING;
103 avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
110 static void next_state(AvahiAnnouncer *a) {
113 /* avahi_log_debug("%i -- %u", a->state, a->n_iteration); */
115 if (a->state == AVAHI_WAITING) {
117 assert(a->entry->group);
119 avahi_s_entry_group_check_probed(a->entry->group, 1);
121 } else if (a->state == AVAHI_PROBING) {
123 if (a->n_iteration >= 4) {
128 /* avahi_log_debug("Enough probes for record [%s]", t = avahi_record_to_string(a->entry->record)); */
131 if (a->entry->group) {
132 assert(a->entry->group->n_probing);
133 a->entry->group->n_probing--;
136 if (a->entry->group && a->entry->group->state == AVAHI_ENTRY_GROUP_REGISTERING)
137 a->state = AVAHI_WAITING;
139 a->state = AVAHI_ANNOUNCING;
143 set_timeout(a, NULL);
148 avahi_interface_post_probe(a->interface, a->entry->record, 0);
150 avahi_elapse_time(&tv, AVAHI_PROBE_INTERVAL_MSEC, 0);
156 } else if (a->state == AVAHI_ANNOUNCING) {
158 if (a->entry->flags & AVAHI_PUBLISH_UNIQUE)
159 /* Send the whole rrset at once */
160 avahi_server_prepare_matching_responses(a->server, a->interface, a->entry->record->key, 0);
162 avahi_server_prepare_response(a->server, a->interface, a->entry, 0, 0);
164 avahi_server_generate_response(a->server, a->interface, NULL, NULL, 0, 0, 0);
166 if (++a->n_iteration >= 4) {
168 /* Announcing done */
170 /* avahi_log_debug("Enough announcements for record [%s]", t = avahi_record_to_string(a->entry->record)); */
173 a->state = AVAHI_ESTABLISHED;
175 set_timeout(a, NULL);
178 avahi_elapse_time(&tv, a->sec_delay*1000, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
180 if (a->n_iteration < 10)
188 static void elapse_announce(AvahiTimeEvent *e, void *userdata) {
191 next_state(userdata);
194 AvahiAnnouncer *avahi_get_announcer(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
201 for (a = e->announcers; a; a = a->by_entry_next)
202 if (a->interface == i)
208 static void go_to_initial_state(AvahiAnnouncer *a, int immediately) {
215 if ((e->flags & AVAHI_PUBLISH_UNIQUE) && !(e->flags & AVAHI_PUBLISH_NO_PROBE))
216 a->state = AVAHI_PROBING;
217 else if (!(e->flags & AVAHI_PUBLISH_NO_ANNOUNCE)) {
219 if (!e->group || e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED)
220 a->state = AVAHI_ANNOUNCING;
222 a->state = AVAHI_WAITING;
225 a->state = AVAHI_ESTABLISHED;
230 if (a->state == AVAHI_PROBING && e->group)
231 e->group->n_probing++;
233 if (a->state == AVAHI_PROBING)
234 set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_PROBE_JITTER_MSEC));
235 else if (a->state == AVAHI_ANNOUNCING)
236 set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC));
238 set_timeout(a, NULL);
241 static void new_announcer(AvahiServer *s, AvahiInterface *i, AvahiEntry *e) {
250 /* avahi_log_debug("NEW ANNOUNCER: %s.%i [%s]", i->hardware->name, i->protocol, t = avahi_record_to_string(e->record)); */
253 if (!avahi_interface_match(i, e->interface, e->protocol) || !i->announcing || !avahi_entry_is_commited(e))
256 /* We don't want duplicate announcers */
257 if (avahi_get_announcer(s, e, i))
260 if ((!(a = avahi_new(AvahiAnnouncer, 1)))) {
261 avahi_log_error(__FILE__": Out of memory.");
268 a->time_event = NULL;
270 AVAHI_LLIST_PREPEND(AvahiAnnouncer, by_interface, i->announcers, a);
271 AVAHI_LLIST_PREPEND(AvahiAnnouncer, by_entry, e->announcers, a);
273 go_to_initial_state(a, 0);
275 /* avahi_log_debug("New announcer on interface %s.%i for entry [%s] state=%i", i->hardware->name, i->protocol, t = avahi_record_to_string(e->record), a->state); */
279 void avahi_announce_interface(AvahiServer *s, AvahiInterface *i) {
288 for (e = s->entries; e; e = e->entries_next)
290 new_announcer(s, i, e);
293 static void announce_walk_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
294 AvahiEntry *e = userdata;
301 new_announcer(m->server, i, e);
304 void avahi_announce_entry(AvahiServer *s, AvahiEntry *e) {
309 avahi_interface_monitor_walk(s->monitor, e->interface, e->protocol, announce_walk_callback, e);
312 void avahi_announce_group(AvahiServer *s, AvahiSEntryGroup *g) {
318 for (e = g->entries; e; e = e->by_group_next)
320 avahi_announce_entry(s, e);
323 int avahi_entry_is_registered(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
331 if (!(a = avahi_get_announcer(s, e, i)))
335 a->state == AVAHI_ANNOUNCING ||
336 a->state == AVAHI_ESTABLISHED ||
337 (a->state == AVAHI_WAITING && !(e->flags & AVAHI_PUBLISH_UNIQUE));
340 int avahi_entry_is_probing(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
348 if (!(a = avahi_get_announcer(s, e, i)))
351 /* avahi_log_debug("state: %i", a->state); */
354 a->state == AVAHI_PROBING ||
355 (a->state == AVAHI_WAITING && (e->flags & AVAHI_PUBLISH_UNIQUE));
358 void avahi_entry_return_to_initial_state(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
365 if (!(a = avahi_get_announcer(s, e, i)))
368 if (a->state == AVAHI_PROBING && a->entry->group)
369 a->entry->group->n_probing--;
371 go_to_initial_state(a, 1);
374 static AvahiRecord *make_goodbye_record(AvahiRecord *r) {
380 /* avahi_log_debug("Preparing goodbye for record [%s]", t = avahi_record_to_string(r)); */
383 if (!(g = avahi_record_copy(r)))
384 return NULL; /* OOM */
392 static int is_duplicate_entry(AvahiServer *s, AvahiEntry *e) {
398 for (i = avahi_hashmap_lookup(s->entries_by_key, e->record->key); i; i = i->by_key_next) {
403 if (!avahi_record_equal_no_ttl(i->record, e->record))
412 static void send_goodbye_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
413 AvahiEntry *e = userdata;
421 if (!avahi_interface_match(i, e->interface, e->protocol))
424 if (e->flags & AVAHI_PUBLISH_NO_ANNOUNCE)
427 if (!avahi_entry_is_registered(m->server, e, i))
430 if (is_duplicate_entry(m->server, e))
433 if (!(g = make_goodbye_record(e->record)))
436 avahi_interface_post_response(i, g, e->flags & AVAHI_PUBLISH_UNIQUE, NULL, 1);
437 avahi_record_unref(g);
440 static void reannounce(AvahiAnnouncer *a) {
447 /* If the group this entry belongs to is not even commited, there's nothing to reannounce */
448 if (e->group && (e->group->state == AVAHI_ENTRY_GROUP_UNCOMMITED || e->group->state == AVAHI_ENTRY_GROUP_COLLISION))
451 /* Because we might change state we decrease the probing counter first */
452 if (a->state == AVAHI_PROBING && a->entry->group)
453 a->entry->group->n_probing--;
455 if (a->state == AVAHI_PROBING ||
456 (a->state == AVAHI_WAITING && (e->flags & AVAHI_PUBLISH_UNIQUE) && !(e->flags & AVAHI_PUBLISH_NO_PROBE)))
458 /* We were probing or waiting after probe, so we restart probing from the beginning here */
460 a->state = AVAHI_PROBING;
461 else if (a->state == AVAHI_WAITING)
463 /* We were waiting, but were not probing before, so we continue waiting */
464 a->state = AVAHI_WAITING;
466 else if (e->flags & AVAHI_PUBLISH_NO_ANNOUNCE)
468 /* No announcer needed */
469 a->state = AVAHI_ESTABLISHED;
473 /* Ok, let's restart announcing */
474 a->state = AVAHI_ANNOUNCING;
477 /* Now let's increase the probing counter again */
478 if (a->state == AVAHI_PROBING && e->group)
479 e->group->n_probing++;
484 if (a->state == AVAHI_PROBING)
485 set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_PROBE_JITTER_MSEC));
486 else if (a->state == AVAHI_ANNOUNCING)
487 set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC));
489 set_timeout(a, NULL);
493 static void reannounce_walk_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
494 AvahiEntry *e = userdata;
502 if (!(a = avahi_get_announcer(m->server, e, i)))
508 void avahi_reannounce_entry(AvahiServer *s, AvahiEntry *e) {
514 avahi_interface_monitor_walk(s->monitor, e->interface, e->protocol, reannounce_walk_callback, e);
517 void avahi_goodbye_interface(AvahiServer *s, AvahiInterface *i, int send_goodbye, int remove) {
522 if (avahi_interface_is_relevant(i)) {
525 for (e = s->entries; e; e = e->entries_next)
527 send_goodbye_callback(s->monitor, i, e);
531 while (i->announcers)
532 remove_announcer(s, i->announcers);
535 void avahi_goodbye_entry(AvahiServer *s, AvahiEntry *e, int send_goodbye, int remove) {
541 avahi_interface_monitor_walk(s->monitor, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, send_goodbye_callback, e);
544 while (e->announcers)
545 remove_announcer(s, e->announcers);
548 void avahi_goodbye_all(AvahiServer *s, int send_goodbye, int remove) {
553 for (e = s->entries; e; e = e->entries_next)
555 avahi_goodbye_entry(s, e, send_goodbye, remove);