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>
33 #define AVAHI_ANNOUNCEMENT_JITTER_MSEC 250
34 #define AVAHI_PROBE_JITTER_MSEC 250
35 #define AVAHI_PROBE_INTERVAL_MSEC 250
37 static void remove_announcer(AvahiServer *s, AvahiAnnouncer *a) {
42 avahi_time_event_free(a->time_event);
44 AVAHI_LLIST_REMOVE(AvahiAnnouncer, by_interface, a->interface->announcers, a);
45 AVAHI_LLIST_REMOVE(AvahiAnnouncer, by_entry, a->entry->announcers, a);
50 static void elapse_announce(AvahiTimeEvent *e, void *userdata);
52 static void set_timeout(AvahiAnnouncer *a, const struct timeval *tv) {
57 avahi_time_event_free(a->time_event);
63 avahi_time_event_update(a->time_event, tv);
65 a->time_event = avahi_time_event_new(a->server->time_event_queue, tv, elapse_announce, a);
69 static void next_state(AvahiAnnouncer *a);
71 void avahi_s_entry_group_check_probed(AvahiSEntryGroup *g, int immediately) {
76 /* Check whether all group members have been probed */
78 if (g->state != AVAHI_ENTRY_GROUP_REGISTERING || g->n_probing > 0)
81 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_ESTABLISHED);
86 for (e = g->entries; e; e = e->entries_next) {
89 for (a = e->announcers; a; a = a->by_entry_next) {
91 if (a->state != AVAHI_WAITING)
94 a->state = AVAHI_ANNOUNCING;
104 avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
111 static void next_state(AvahiAnnouncer *a) {
114 /* avahi_log_debug("%i -- %u", a->state, a->n_iteration); */
116 if (a->state == AVAHI_WAITING) {
118 assert(a->entry->group);
120 avahi_s_entry_group_check_probed(a->entry->group, 1);
122 } else if (a->state == AVAHI_PROBING) {
124 if (a->n_iteration >= 4) {
129 /* avahi_log_debug("Enough probes for record [%s]", t = avahi_record_to_string(a->entry->record)); */
132 if (a->entry->group) {
133 assert(a->entry->group->n_probing);
134 a->entry->group->n_probing--;
137 if (a->entry->group && a->entry->group->state == AVAHI_ENTRY_GROUP_REGISTERING)
138 a->state = AVAHI_WAITING;
140 a->state = AVAHI_ANNOUNCING;
144 set_timeout(a, NULL);
149 avahi_interface_post_probe(a->interface, a->entry->record, 0);
151 avahi_elapse_time(&tv, AVAHI_PROBE_INTERVAL_MSEC, 0);
157 } else if (a->state == AVAHI_ANNOUNCING) {
159 if (a->entry->flags & AVAHI_PUBLISH_UNIQUE)
160 /* Send the whole rrset at once */
161 avahi_server_prepare_matching_responses(a->server, a->interface, a->entry->record->key, 0);
163 avahi_server_prepare_response(a->server, a->interface, a->entry, 0, 0);
165 avahi_server_generate_response(a->server, a->interface, NULL, NULL, 0, 0, 0);
167 if (++a->n_iteration >= 4) {
169 /* Announcing done */
171 /* avahi_log_debug("Enough announcements for record [%s]", t = avahi_record_to_string(a->entry->record)); */
174 a->state = AVAHI_ESTABLISHED;
176 set_timeout(a, NULL);
179 avahi_elapse_time(&tv, a->sec_delay*1000, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
181 if (a->n_iteration < 10)
189 static void elapse_announce(AvahiTimeEvent *e, void *userdata) {
192 next_state(userdata);
195 static AvahiAnnouncer *get_announcer(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
202 for (a = e->announcers; a; a = a->by_entry_next)
203 if (a->interface == i)
209 static void go_to_initial_state(AvahiAnnouncer *a) {
216 if ((e->flags & AVAHI_PUBLISH_UNIQUE) && !(e->flags & AVAHI_PUBLISH_NO_PROBE))
217 a->state = AVAHI_PROBING;
218 else if (!(e->flags & AVAHI_PUBLISH_NO_ANNOUNCE)) {
220 if (!e->group || e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED)
221 a->state = AVAHI_ANNOUNCING;
223 a->state = AVAHI_WAITING;
226 a->state = AVAHI_ESTABLISHED;
231 if (a->state == AVAHI_PROBING && e->group)
232 e->group->n_probing++;
234 if (a->state == AVAHI_PROBING)
235 set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_PROBE_JITTER_MSEC));
236 else if (a->state == AVAHI_ANNOUNCING)
237 set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC));
239 set_timeout(a, NULL);
242 static void new_announcer(AvahiServer *s, AvahiInterface *i, AvahiEntry *e) {
251 /* avahi_log_debug("NEW ANNOUNCER: %s.%i [%s]", i->hardware->name, i->protocol, t = avahi_record_to_string(e->record)); */
254 if (!avahi_interface_match(i, e->interface, e->protocol) || !i->announcing || !avahi_entry_is_commited(e))
257 /* We don't want duplicate announcers */
258 if (get_announcer(s, e, i))
261 if ((!(a = avahi_new(AvahiAnnouncer, 1)))) {
262 avahi_log_error(__FILE__": Out of memory.");
269 a->time_event = NULL;
271 AVAHI_LLIST_PREPEND(AvahiAnnouncer, by_interface, i->announcers, a);
272 AVAHI_LLIST_PREPEND(AvahiAnnouncer, by_entry, e->announcers, a);
274 go_to_initial_state(a);
276 /* 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); */
280 void avahi_announce_interface(AvahiServer *s, AvahiInterface *i) {
289 for (e = s->entries; e; e = e->entries_next)
291 new_announcer(s, i, e);
294 static void announce_walk_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
295 AvahiEntry *e = userdata;
302 new_announcer(m->server, i, e);
305 void avahi_announce_entry(AvahiServer *s, AvahiEntry *e) {
310 avahi_interface_monitor_walk(s->monitor, e->interface, e->protocol, announce_walk_callback, e);
313 void avahi_announce_group(AvahiServer *s, AvahiSEntryGroup *g) {
319 for (e = g->entries; e; e = e->by_group_next)
321 avahi_announce_entry(s, e);
324 int avahi_entry_is_registered(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
332 if (!(a = get_announcer(s, e, i)))
336 a->state == AVAHI_ANNOUNCING ||
337 a->state == AVAHI_ESTABLISHED ||
338 (a->state == AVAHI_WAITING && !(e->flags & AVAHI_PUBLISH_UNIQUE));
341 int avahi_entry_is_probing(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
349 if (!(a = get_announcer(s, e, i)))
352 /* avahi_log_debug("state: %i", a->state); */
355 a->state == AVAHI_PROBING ||
356 (a->state == AVAHI_WAITING && (e->flags & AVAHI_PUBLISH_UNIQUE));
359 void avahi_entry_return_to_initial_state(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
366 if (!(a = get_announcer(s, e, i)))
369 if (a->state == AVAHI_PROBING && a->entry->group)
370 a->entry->group->n_probing--;
372 go_to_initial_state(a);
375 static AvahiRecord *make_goodbye_record(AvahiRecord *r) {
381 /* avahi_log_debug("Preparing goodbye for record [%s]", t = avahi_record_to_string(r)); */
384 if (!(g = avahi_record_copy(r)))
385 return NULL; /* OOM */
393 static int is_duplicate_entry(AvahiServer *s, AvahiEntry *e) {
399 for (i = avahi_hashmap_lookup(s->entries_by_key, e->record->key); i; i = i->by_key_next) {
404 if (!avahi_record_equal_no_ttl(i->record, e->record))
413 static void send_goodbye_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
414 AvahiEntry *e = userdata;
422 if (!avahi_interface_match(i, e->interface, e->protocol))
425 if (e->flags & AVAHI_PUBLISH_NO_ANNOUNCE)
428 if (!avahi_entry_is_registered(m->server, e, i))
431 if (is_duplicate_entry(m->server, e))
434 if (!(g = make_goodbye_record(e->record)))
437 avahi_interface_post_response(i, g, e->flags & AVAHI_PUBLISH_UNIQUE, NULL, 1);
438 avahi_record_unref(g);
441 static void reannounce(AvahiAnnouncer *a) {
448 /* If the group this entry belongs to is not even commited, there's nothing to reannounce */
449 if (e->group && (e->group->state == AVAHI_ENTRY_GROUP_UNCOMMITED || e->group->state == AVAHI_ENTRY_GROUP_COLLISION))
452 /* Because we might change state we decrease the probing counter first */
453 if (a->state == AVAHI_PROBING && a->entry->group)
454 a->entry->group->n_probing--;
456 if (a->state == AVAHI_PROBING ||
457 (a->state == AVAHI_WAITING && (e->flags & AVAHI_PUBLISH_UNIQUE) && !(e->flags & AVAHI_PUBLISH_NO_PROBE)))
459 /* We were probing or waiting after probe, so we restart probing from the beginning here */
461 a->state = AVAHI_PROBING;
462 else if (a->state == AVAHI_WAITING)
464 /* We were waiting, but were not probing before, so we continue waiting */
465 a->state = AVAHI_WAITING;
467 else if (e->flags & AVAHI_PUBLISH_NO_ANNOUNCE)
469 /* No announcer needed */
470 a->state = AVAHI_ESTABLISHED;
474 /* Ok, let's restart announcing */
475 a->state = AVAHI_ANNOUNCING;
478 /* Now let's increase the probing counter again */
479 if (a->state == AVAHI_PROBING && e->group)
480 e->group->n_probing++;
485 if (a->state == AVAHI_PROBING)
486 set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_PROBE_JITTER_MSEC));
487 else if (a->state == AVAHI_ANNOUNCING)
488 set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC));
490 set_timeout(a, NULL);
494 static void reannounce_walk_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
495 AvahiEntry *e = userdata;
503 if (!(a = get_announcer(m->server, e, i)))
509 void avahi_reannounce_entry(AvahiServer *s, AvahiEntry *e) {
515 avahi_interface_monitor_walk(s->monitor, e->interface, e->protocol, reannounce_walk_callback, e);
518 void avahi_goodbye_interface(AvahiServer *s, AvahiInterface *i, int send_goodbye, int remove) {
523 if (avahi_interface_is_relevant(i)) {
526 for (e = s->entries; e; e = e->entries_next)
528 send_goodbye_callback(s->monitor, i, e);
532 while (i->announcers)
533 remove_announcer(s, i->announcers);
536 void avahi_goodbye_entry(AvahiServer *s, AvahiEntry *e, int send_goodbye, int remove) {
542 avahi_interface_monitor_walk(s->monitor, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, send_goodbye_callback, e);
545 while (e->announcers)
546 remove_announcer(s, e->announcers);