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->by_group_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 if (a->state == AVAHI_WAITING) {
116 assert(a->entry->group);
118 avahi_s_entry_group_check_probed(a->entry->group, 1);
120 } else if (a->state == AVAHI_PROBING) {
122 if (a->n_iteration >= 4) {
125 if (a->entry->group) {
126 assert(a->entry->group->n_probing);
127 a->entry->group->n_probing--;
130 if (a->entry->group && a->entry->group->state == AVAHI_ENTRY_GROUP_REGISTERING)
131 a->state = AVAHI_WAITING;
133 a->state = AVAHI_ANNOUNCING;
137 set_timeout(a, NULL);
142 avahi_interface_post_probe(a->interface, a->entry->record, 0);
144 avahi_elapse_time(&tv, AVAHI_PROBE_INTERVAL_MSEC, 0);
150 } else if (a->state == AVAHI_ANNOUNCING) {
152 if (a->entry->flags & AVAHI_PUBLISH_UNIQUE)
153 /* Send the whole rrset at once */
154 avahi_server_prepare_matching_responses(a->server, a->interface, a->entry->record->key, 0);
156 avahi_server_prepare_response(a->server, a->interface, a->entry, 0, 0);
158 avahi_server_generate_response(a->server, a->interface, NULL, NULL, 0, 0, 0);
160 if (++a->n_iteration >= 4) {
161 /* Announcing done */
163 a->state = AVAHI_ESTABLISHED;
165 set_timeout(a, NULL);
168 avahi_elapse_time(&tv, a->sec_delay*1000, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
170 if (a->n_iteration < 10)
178 static void elapse_announce(AvahiTimeEvent *e, void *userdata) {
181 next_state(userdata);
184 static AvahiAnnouncer *get_announcer(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
191 for (a = e->announcers; a; a = a->by_entry_next)
192 if (a->interface == i)
198 static void go_to_initial_state(AvahiAnnouncer *a) {
205 if ((e->flags & AVAHI_PUBLISH_UNIQUE) && !(e->flags & AVAHI_PUBLISH_NO_PROBE))
206 a->state = AVAHI_PROBING;
207 else if (!(e->flags & AVAHI_PUBLISH_NO_ANNOUNCE)) {
209 if (!e->group || e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED)
210 a->state = AVAHI_ANNOUNCING;
212 a->state = AVAHI_WAITING;
215 a->state = AVAHI_ESTABLISHED;
220 if (a->state == AVAHI_PROBING && e->group)
221 e->group->n_probing++;
223 if (a->state == AVAHI_PROBING)
224 set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_PROBE_JITTER_MSEC));
225 else if (a->state == AVAHI_ANNOUNCING)
226 set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC));
228 set_timeout(a, NULL);
231 static void new_announcer(AvahiServer *s, AvahiInterface *i, AvahiEntry *e) {
239 if (!avahi_interface_match(i, e->interface, e->protocol) || !i->announcing || !avahi_entry_is_commited(e))
242 /* We don't want duplicate announcers */
243 if (get_announcer(s, e, i))
246 if ((!(a = avahi_new(AvahiAnnouncer, 1)))) {
247 avahi_log_error(__FILE__": Out of memory.");
254 a->time_event = NULL;
256 AVAHI_LLIST_PREPEND(AvahiAnnouncer, by_interface, i->announcers, a);
257 AVAHI_LLIST_PREPEND(AvahiAnnouncer, by_entry, e->announcers, a);
259 go_to_initial_state(a);
262 void avahi_announce_interface(AvahiServer *s, AvahiInterface *i) {
271 for (e = s->entries; e; e = e->entries_next)
273 new_announcer(s, i, e);
276 static void announce_walk_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
277 AvahiEntry *e = userdata;
284 new_announcer(m->server, i, e);
287 void avahi_announce_entry(AvahiServer *s, AvahiEntry *e) {
292 avahi_interface_monitor_walk(s->monitor, e->interface, e->protocol, announce_walk_callback, e);
295 void avahi_announce_group(AvahiServer *s, AvahiSEntryGroup *g) {
301 for (e = g->entries; e; e = e->by_group_next)
303 avahi_announce_entry(s, e);
306 int avahi_entry_is_registered(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
314 if (!(a = get_announcer(s, e, i)))
318 a->state == AVAHI_ANNOUNCING ||
319 a->state == AVAHI_ESTABLISHED ||
320 (a->state == AVAHI_WAITING && !(e->flags & AVAHI_PUBLISH_UNIQUE));
323 int avahi_entry_is_probing(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
331 if (!(a = get_announcer(s, e, i)))
335 a->state == AVAHI_PROBING ||
336 (a->state == AVAHI_WAITING && (e->flags & AVAHI_PUBLISH_UNIQUE));
339 void avahi_entry_return_to_initial_state(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
346 if (!(a = get_announcer(s, e, i)))
349 if (a->state == AVAHI_PROBING && a->entry->group)
350 a->entry->group->n_probing--;
352 go_to_initial_state(a);
355 static AvahiRecord *make_goodbye_record(AvahiRecord *r) {
360 if (!(g = avahi_record_copy(r)))
361 return NULL; /* OOM */
369 static int is_duplicate_entry(AvahiServer *s, AvahiEntry *e) {
375 for (i = avahi_hashmap_lookup(s->entries_by_key, e->record->key); i; i = i->by_key_next) {
380 if (!avahi_record_equal_no_ttl(i->record, e->record))
389 static void send_goodbye_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
390 AvahiEntry *e = userdata;
398 if (!avahi_interface_match(i, e->interface, e->protocol))
401 if (e->flags & AVAHI_PUBLISH_NO_ANNOUNCE)
404 if (!avahi_entry_is_registered(m->server, e, i))
407 if (is_duplicate_entry(m->server, e))
410 if (!(g = make_goodbye_record(e->record)))
413 avahi_interface_post_response(i, g, e->flags & AVAHI_PUBLISH_UNIQUE, NULL, 1);
414 avahi_record_unref(g);
417 static void reannounce(AvahiAnnouncer *a) {
424 /* If the group this entry belongs to is not even commited, there's nothing to reannounce */
425 if (e->group && (e->group->state == AVAHI_ENTRY_GROUP_UNCOMMITED || e->group->state == AVAHI_ENTRY_GROUP_COLLISION))
428 /* Because we might change state we decrease the probing counter first */
429 if (a->state == AVAHI_PROBING && a->entry->group)
430 a->entry->group->n_probing--;
432 if (a->state == AVAHI_PROBING ||
433 (a->state == AVAHI_WAITING && (e->flags & AVAHI_PUBLISH_UNIQUE) && !(e->flags & AVAHI_PUBLISH_NO_PROBE)))
435 /* We were probing or waiting after probe, so we restart probing from the beginning here */
437 a->state = AVAHI_PROBING;
438 else if (a->state == AVAHI_WAITING)
440 /* We were waiting, but were not probing before, so we continue waiting */
441 a->state = AVAHI_WAITING;
443 else if (e->flags & AVAHI_PUBLISH_NO_ANNOUNCE)
445 /* No announcer needed */
446 a->state = AVAHI_ESTABLISHED;
450 /* Ok, let's restart announcing */
451 a->state = AVAHI_ANNOUNCING;
454 /* Now let's increase the probing counter again */
455 if (a->state == AVAHI_PROBING && e->group)
456 e->group->n_probing++;
461 if (a->state == AVAHI_PROBING)
462 set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_PROBE_JITTER_MSEC));
463 else if (a->state == AVAHI_ANNOUNCING)
464 set_timeout(a, avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC));
466 set_timeout(a, NULL);
470 static void reannounce_walk_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
471 AvahiEntry *e = userdata;
479 if (!(a = get_announcer(m->server, e, i)))
485 void avahi_reannounce_entry(AvahiServer *s, AvahiEntry *e) {
491 avahi_interface_monitor_walk(s->monitor, e->interface, e->protocol, reannounce_walk_callback, e);
494 void avahi_goodbye_interface(AvahiServer *s, AvahiInterface *i, int send_goodbye, int remove) {
499 if (avahi_interface_is_relevant(i)) {
502 for (e = s->entries; e; e = e->entries_next)
504 send_goodbye_callback(s->monitor, i, e);
508 while (i->announcers)
509 remove_announcer(s, i->announcers);
512 void avahi_goodbye_entry(AvahiServer *s, AvahiEntry *e, int send_goodbye, int remove) {
518 avahi_interface_monitor_walk(s->monitor, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, send_goodbye_callback, e);
521 while (e->announcers)
522 remove_announcer(s, e->announcers);