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_announcement(AvahiServer *s, AvahiAnnouncement *a) {
41 avahi_time_event_free(a->time_event);
43 AVAHI_LLIST_REMOVE(AvahiAnnouncement, by_interface, a->interface->announcements, a);
44 AVAHI_LLIST_REMOVE(AvahiAnnouncement, by_entry, a->entry->announcements, a);
49 static void elapse_announce(AvahiTimeEvent *e, void *userdata);
51 static void set_timeout(AvahiAnnouncement *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(AvahiAnnouncement *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->announcements; 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(AvahiAnnouncement *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_ENTRY_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 AvahiAnnouncement *avahi_get_announcement(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
195 AvahiAnnouncement *a;
201 for (a = e->announcements; a; a = a->by_entry_next)
202 if (a->interface == i)
208 static void go_to_initial_state(AvahiAnnouncement *a, int immediately) {
215 if ((e->flags & AVAHI_ENTRY_UNIQUE) && !(e->flags & AVAHI_ENTRY_NOPROBE))
216 a->state = AVAHI_PROBING;
217 else if (!(e->flags & AVAHI_ENTRY_NOANNOUNCE)) {
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 avahi_elapse_time(&tv, 0, immediately ? 0 : AVAHI_PROBE_JITTER_MSEC);
236 } else if (a->state == AVAHI_ANNOUNCING) {
237 avahi_elapse_time(&tv, 0, immediately ? 0 : AVAHI_ANNOUNCEMENT_JITTER_MSEC);
240 set_timeout(a, NULL);
243 static void new_announcement(AvahiServer *s, AvahiInterface *i, AvahiEntry *e) {
244 AvahiAnnouncement *a;
252 /* avahi_log_debug("NEW ANNOUNCEMENT: %s.%i [%s]", i->hardware->name, i->protocol, t = avahi_record_to_string(e->record)); */
255 if (!avahi_interface_match(i, e->interface, e->protocol) || !i->announcing || !avahi_entry_is_commited(e))
258 /* We don't want duplicate announcements */
259 if (avahi_get_announcement(s, e, i))
262 if ((!(a = avahi_new(AvahiAnnouncement, 1)))) {
263 avahi_log_error(__FILE__": Out of memory.");
270 a->time_event = NULL;
272 AVAHI_LLIST_PREPEND(AvahiAnnouncement, by_interface, i->announcements, a);
273 AVAHI_LLIST_PREPEND(AvahiAnnouncement, by_entry, e->announcements, a);
275 go_to_initial_state(a, 0);
277 /* 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); */
281 void avahi_announce_interface(AvahiServer *s, AvahiInterface *i) {
290 for (e = s->entries; e; e = e->entries_next)
292 new_announcement(s, i, e);
295 static void announce_walk_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
296 AvahiEntry *e = userdata;
303 new_announcement(m->server, i, e);
306 void avahi_announce_entry(AvahiServer *s, AvahiEntry *e) {
311 avahi_interface_monitor_walk(s->monitor, e->interface, e->protocol, announce_walk_callback, e);
314 void avahi_announce_group(AvahiServer *s, AvahiSEntryGroup *g) {
320 for (e = g->entries; e; e = e->by_group_next)
322 avahi_announce_entry(s, e);
325 int avahi_entry_is_registered(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
326 AvahiAnnouncement *a;
333 if (!(a = avahi_get_announcement(s, e, i)))
337 a->state == AVAHI_ANNOUNCING ||
338 a->state == AVAHI_ESTABLISHED ||
339 (a->state == AVAHI_WAITING && !(e->flags & AVAHI_ENTRY_UNIQUE));
342 int avahi_entry_is_probing(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
343 AvahiAnnouncement *a;
350 if (!(a = avahi_get_announcement(s, e, i)))
353 /* avahi_log_debug("state: %i", a->state); */
356 a->state == AVAHI_PROBING ||
357 (a->state == AVAHI_WAITING && (e->flags & AVAHI_ENTRY_UNIQUE));
360 void avahi_entry_return_to_initial_state(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
361 AvahiAnnouncement *a;
367 if (!(a = avahi_get_announcement(s, e, i)))
370 if (a->state == AVAHI_PROBING && a->entry->group)
371 a->entry->group->n_probing--;
373 go_to_initial_state(a, 1);
376 static AvahiRecord *make_goodbye_record(AvahiRecord *r) {
382 /* avahi_log_debug("Preparing goodbye for record [%s]", t = avahi_record_to_string(r)); */
385 if (!(g = avahi_record_copy(r)))
386 return NULL; /* OOM */
394 static void send_goodbye_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, void* userdata) {
395 AvahiEntry *e = userdata;
403 if (!avahi_interface_match(i, e->interface, e->protocol))
406 if (e->flags & AVAHI_ENTRY_NOANNOUNCE)
409 if (!avahi_entry_is_registered(m->server, e, i))
412 if (!(g = make_goodbye_record(e->record)))
415 avahi_interface_post_response(i, g, e->flags & AVAHI_ENTRY_UNIQUE, NULL, 1);
416 avahi_record_unref(g);
419 void avahi_goodbye_interface(AvahiServer *s, AvahiInterface *i, int goodbye) {
423 /* avahi_log_debug("goodbye interface: %s.%u", i->hardware->name, i->protocol); */
425 if (goodbye && avahi_interface_relevant(i)) {
428 for (e = s->entries; e; e = e->entries_next)
430 send_goodbye_callback(s->monitor, i, e);
433 while (i->announcements)
434 remove_announcement(s, i->announcements);
436 /* avahi_log_debug("goodbye interface done: %s.%u", i->hardware->name, i->protocol); */
439 void avahi_goodbye_entry(AvahiServer *s, AvahiEntry *e, int goodbye) {
443 /* avahi_log_debug("goodbye entry: %p", e); */
445 if (goodbye && !e->dead)
446 avahi_interface_monitor_walk(s->monitor, 0, AF_UNSPEC, send_goodbye_callback, e);
448 while (e->announcements)
449 remove_announcement(s, e->announcements);
451 /* avahi_log_debug("goodbye entry done: %p", e); */
455 void avahi_goodbye_all(AvahiServer *s, int goodbye) {
460 /* avahi_log_debug("goodbye all"); */
462 for (e = s->entries; e; e = e->entries_next)
464 avahi_goodbye_entry(s, e, goodbye);
466 /* avahi_log_debug("goodbye all done"); */