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
28 #include <avahi-common/timeval.h>
29 #include <avahi-common/malloc.h>
35 #define AVAHI_ANNOUNCEMENT_JITTER_MSEC 250
36 #define AVAHI_PROBE_JITTER_MSEC 250
37 #define AVAHI_PROBE_INTERVAL_MSEC 250
39 static void remove_announcer(AvahiServer *s, AvahiAnnouncer *a) {
44 avahi_time_event_free(a->time_event);
46 AVAHI_LLIST_REMOVE(AvahiAnnouncer, by_interface, a->interface->announcers, a);
47 AVAHI_LLIST_REMOVE(AvahiAnnouncer, by_entry, a->entry->announcers, a);
52 static void elapse_announce(AvahiTimeEvent *e, void *userdata);
54 static void set_timeout(AvahiAnnouncer *a, const struct timeval *tv) {
59 avahi_time_event_free(a->time_event);
65 avahi_time_event_update(a->time_event, tv);
67 a->time_event = avahi_time_event_new(a->server->time_event_queue, tv, elapse_announce, a);
71 static void next_state(AvahiAnnouncer *a);
73 void avahi_s_entry_group_check_probed(AvahiSEntryGroup *g, int immediately) {
78 /* Check whether all group members have been probed */
80 if (g->state != AVAHI_ENTRY_GROUP_REGISTERING || g->n_probing > 0)
83 avahi_s_entry_group_change_state(g, AVAHI_ENTRY_GROUP_ESTABLISHED);
88 for (e = g->entries; e; e = e->by_group_next) {
91 for (a = e->announcers; a; a = a->by_entry_next) {
93 if (a->state != AVAHI_WAITING)
96 a->state = AVAHI_ANNOUNCING;
106 avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
113 static void next_state(AvahiAnnouncer *a) {
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) {
127 if (a->entry->group) {
128 assert(a->entry->group->n_probing);
129 a->entry->group->n_probing--;
132 if (a->entry->group && a->entry->group->state == AVAHI_ENTRY_GROUP_REGISTERING)
133 a->state = AVAHI_WAITING;
135 a->state = AVAHI_ANNOUNCING;
139 set_timeout(a, NULL);
144 avahi_interface_post_probe(a->interface, a->entry->record, 0);
146 avahi_elapse_time(&tv, AVAHI_PROBE_INTERVAL_MSEC, 0);
152 } else if (a->state == AVAHI_ANNOUNCING) {
154 if (a->entry->flags & AVAHI_PUBLISH_UNIQUE)
155 /* Send the whole rrset at once */
156 avahi_server_prepare_matching_responses(a->server, a->interface, a->entry->record->key, 0);
158 avahi_server_prepare_response(a->server, a->interface, a->entry, 0, 0);
160 avahi_server_generate_response(a->server, a->interface, NULL, NULL, 0, 0, 0);
162 if (++a->n_iteration >= 4) {
163 /* Announcing done */
165 a->state = AVAHI_ESTABLISHED;
167 set_timeout(a, NULL);
170 avahi_elapse_time(&tv, a->sec_delay*1000, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
172 if (a->n_iteration < 10)
180 static void elapse_announce(AvahiTimeEvent *e, void *userdata) {
183 next_state(userdata);
186 static AvahiAnnouncer *get_announcer(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
193 for (a = e->announcers; a; a = a->by_entry_next)
194 if (a->interface == i)
200 static void go_to_initial_state(AvahiAnnouncer *a) {
207 if ((e->flags & AVAHI_PUBLISH_UNIQUE) && !(e->flags & AVAHI_PUBLISH_NO_PROBE))
208 a->state = AVAHI_PROBING;
209 else if (!(e->flags & AVAHI_PUBLISH_NO_ANNOUNCE)) {
211 if (!e->group || e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED)
212 a->state = AVAHI_ANNOUNCING;
214 a->state = AVAHI_WAITING;
217 a->state = AVAHI_ESTABLISHED;
222 if (a->state == AVAHI_PROBING && e->group)
223 e->group->n_probing++;
225 if (a->state == AVAHI_PROBING)
226 set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_PROBE_JITTER_MSEC));
227 else if (a->state == AVAHI_ANNOUNCING)
228 set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC));
230 set_timeout(a, NULL);
233 static void new_announcer(AvahiServer *s, AvahiInterface *i, AvahiEntry *e) {
241 if (!avahi_interface_match(i, e->interface, e->protocol) || !i->announcing || !avahi_entry_is_commited(e))
244 /* We don't want duplicate announcers */
245 if (get_announcer(s, e, i))
248 if ((!(a = avahi_new(AvahiAnnouncer, 1)))) {
249 avahi_log_error(__FILE__": Out of memory.");
256 a->time_event = NULL;
258 AVAHI_LLIST_PREPEND(AvahiAnnouncer, by_interface, i->announcers, a);
259 AVAHI_LLIST_PREPEND(AvahiAnnouncer, by_entry, e->announcers, a);
261 go_to_initial_state(a);
264 void avahi_announce_interface(AvahiServer *s, AvahiInterface *i) {
273 for (e = s->entries; e; e = e->entries_next)
275 new_announcer(s, i, e);
278 static void announce_walk_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
279 AvahiEntry *e = userdata;
286 new_announcer(m->server, i, e);
289 void avahi_announce_entry(AvahiServer *s, AvahiEntry *e) {
294 avahi_interface_monitor_walk(s->monitor, e->interface, e->protocol, announce_walk_callback, e);
297 void avahi_announce_group(AvahiServer *s, AvahiSEntryGroup *g) {
303 for (e = g->entries; e; e = e->by_group_next)
305 avahi_announce_entry(s, e);
308 int avahi_entry_is_registered(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
316 if (!(a = get_announcer(s, e, i)))
320 a->state == AVAHI_ANNOUNCING ||
321 a->state == AVAHI_ESTABLISHED ||
322 (a->state == AVAHI_WAITING && !(e->flags & AVAHI_PUBLISH_UNIQUE));
325 int avahi_entry_is_probing(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
333 if (!(a = get_announcer(s, e, i)))
337 a->state == AVAHI_PROBING ||
338 (a->state == AVAHI_WAITING && (e->flags & AVAHI_PUBLISH_UNIQUE));
341 void avahi_entry_return_to_initial_state(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
348 if (!(a = get_announcer(s, e, i)))
351 if (a->state == AVAHI_PROBING && a->entry->group)
352 a->entry->group->n_probing--;
354 go_to_initial_state(a);
357 static AvahiRecord *make_goodbye_record(AvahiRecord *r) {
362 if (!(g = avahi_record_copy(r)))
363 return NULL; /* OOM */
371 static int is_duplicate_entry(AvahiServer *s, AvahiEntry *e) {
377 for (i = avahi_hashmap_lookup(s->entries_by_key, e->record->key); i; i = i->by_key_next) {
382 if (!avahi_record_equal_no_ttl(i->record, e->record))
391 static void send_goodbye_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
392 AvahiEntry *e = userdata;
400 if (!avahi_interface_match(i, e->interface, e->protocol))
403 if (e->flags & AVAHI_PUBLISH_NO_ANNOUNCE)
406 if (!avahi_entry_is_registered(m->server, e, i))
409 if (is_duplicate_entry(m->server, e))
412 if (!(g = make_goodbye_record(e->record)))
415 avahi_interface_post_response(i, g, e->flags & AVAHI_PUBLISH_UNIQUE, NULL, 1);
416 avahi_record_unref(g);
419 static void reannounce(AvahiAnnouncer *a) {
426 /* If the group this entry belongs to is not even commited, there's nothing to reannounce */
427 if (e->group && (e->group->state == AVAHI_ENTRY_GROUP_UNCOMMITED || e->group->state == AVAHI_ENTRY_GROUP_COLLISION))
430 /* Because we might change state we decrease the probing counter first */
431 if (a->state == AVAHI_PROBING && a->entry->group)
432 a->entry->group->n_probing--;
434 if (a->state == AVAHI_PROBING ||
435 (a->state == AVAHI_WAITING && (e->flags & AVAHI_PUBLISH_UNIQUE) && !(e->flags & AVAHI_PUBLISH_NO_PROBE)))
437 /* We were probing or waiting after probe, so we restart probing from the beginning here */
439 a->state = AVAHI_PROBING;
440 else if (a->state == AVAHI_WAITING)
442 /* We were waiting, but were not probing before, so we continue waiting */
443 a->state = AVAHI_WAITING;
445 else if (e->flags & AVAHI_PUBLISH_NO_ANNOUNCE)
447 /* No announcer needed */
448 a->state = AVAHI_ESTABLISHED;
452 /* Ok, let's restart announcing */
453 a->state = AVAHI_ANNOUNCING;
456 /* Now let's increase the probing counter again */
457 if (a->state == AVAHI_PROBING && e->group)
458 e->group->n_probing++;
463 if (a->state == AVAHI_PROBING)
464 set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_PROBE_JITTER_MSEC));
465 else if (a->state == AVAHI_ANNOUNCING)
466 set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC));
468 set_timeout(a, NULL);
472 static void reannounce_walk_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
473 AvahiEntry *e = userdata;
481 if (!(a = get_announcer(m->server, e, i)))
487 void avahi_reannounce_entry(AvahiServer *s, AvahiEntry *e) {
493 avahi_interface_monitor_walk(s->monitor, e->interface, e->protocol, reannounce_walk_callback, e);
496 void avahi_goodbye_interface(AvahiServer *s, AvahiInterface *i, int send_goodbye, int remove) {
504 for (e = s->entries; e; e = e->entries_next)
506 send_goodbye_callback(s->monitor, i, e);
510 while (i->announcers)
511 remove_announcer(s, i->announcers);
514 void avahi_goodbye_entry(AvahiServer *s, AvahiEntry *e, int send_goodbye, int remove) {
520 avahi_interface_monitor_walk(s->monitor, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, send_goodbye_callback, e);
523 while (e->announcers)
524 remove_announcer(s, e->announcers);