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
29 #define AVAHI_ANNOUNCEMENT_JITTER_MSEC 250
30 #define AVAHI_PROBE_JITTER_MSEC 250
31 #define AVAHI_PROBE_INTERVAL_MSEC 250
33 static void remove_announcement(AvahiServer *s, AvahiAnnouncement *a) {
38 avahi_time_event_queue_remove(s->time_event_queue, a->time_event);
40 AVAHI_LLIST_REMOVE(AvahiAnnouncement, by_interface, a->interface->announcements, a);
41 AVAHI_LLIST_REMOVE(AvahiAnnouncement, by_entry, a->entry->announcements, a);
46 static void elapse_announce(AvahiTimeEvent *e, void *userdata);
48 static void set_timeout(AvahiAnnouncement *a, const GTimeVal *tv) {
53 avahi_time_event_queue_remove(a->server->time_event_queue, a->time_event);
59 avahi_time_event_queue_update(a->server->time_event_queue, a->time_event, tv);
61 a->time_event = avahi_time_event_queue_add(a->server->time_event_queue, tv, elapse_announce, a);
65 static void next_state(AvahiAnnouncement *a);
67 void avahi_entry_group_check_probed(AvahiEntryGroup *g, gboolean immediately) {
72 /* Check whether all group members have been probed */
74 if (g->state != AVAHI_ENTRY_GROUP_REGISTERING || g->n_probing > 0)
77 avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_ESTABLISHED);
82 for (e = g->entries; e; e = e->entries_next) {
85 for (a = e->announcements; a; a = a->by_entry_next) {
87 if (a->state != AVAHI_WAITING)
90 a->state = AVAHI_ANNOUNCING;
100 avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
107 static void next_state(AvahiAnnouncement *a) {
110 /* g_message("%i -- %u", a->state, a->n_iteration); */
112 if (a->state == AVAHI_WAITING) {
114 g_assert(a->entry->group);
116 avahi_entry_group_check_probed(a->entry->group, TRUE);
118 } else if (a->state == AVAHI_PROBING) {
120 if (a->n_iteration >= 4) {
125 g_message("Enough probes for record [%s]", t = avahi_record_to_string(a->entry->record));
128 if (a->entry->group) {
129 g_assert(a->entry->group->n_probing);
130 a->entry->group->n_probing--;
133 if (a->entry->group && a->entry->group->state == AVAHI_ENTRY_GROUP_REGISTERING)
134 a->state = AVAHI_WAITING;
136 a->state = AVAHI_ANNOUNCING;
140 set_timeout(a, NULL);
145 avahi_interface_post_probe(a->interface, a->entry->record, FALSE);
147 avahi_elapse_time(&tv, AVAHI_PROBE_INTERVAL_MSEC, 0);
153 } else if (a->state == AVAHI_ANNOUNCING) {
155 avahi_interface_post_response(a->interface, a->entry->record, a->entry->flags & AVAHI_ENTRY_UNIQUE, FALSE);
157 if (++a->n_iteration >= 4) {
159 /* Announcing done */
161 g_message("Enough announcements for record [%s]", t = avahi_record_to_string(a->entry->record));
164 a->state = AVAHI_ESTABLISHED;
166 set_timeout(a, NULL);
169 avahi_elapse_time(&tv, a->sec_delay*1000, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
171 if (a->n_iteration < 10)
179 static void elapse_announce(AvahiTimeEvent *e, void *userdata) {
182 next_state(userdata);
185 AvahiAnnouncement *avahi_get_announcement(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
186 AvahiAnnouncement *a;
192 for (a = e->announcements; a; a = a->by_entry_next)
193 if (a->interface == i)
199 static void new_announcement(AvahiServer *s, AvahiInterface *i, AvahiEntry *e) {
200 AvahiAnnouncement *a;
209 /* g_message("NEW ANNOUNCEMENT: %s.%i [%s]", i->hardware->name, i->protocol, t = avahi_record_to_string(e->record)); */
212 if (!avahi_interface_match(i, e->interface, e->protocol) || !i->announcing || !avahi_entry_commited(e))
215 /* We don't want duplicate announcements */
216 if (avahi_get_announcement(s, e, i))
219 a = g_new(AvahiAnnouncement, 1);
224 if ((e->flags & AVAHI_ENTRY_UNIQUE) && !(e->flags & AVAHI_ENTRY_NOPROBE))
225 a->state = AVAHI_PROBING;
226 else if (!(e->flags & AVAHI_ENTRY_NOANNOUNCE)) {
228 if (!e->group || e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED)
229 a->state = AVAHI_ANNOUNCING;
231 a->state = AVAHI_WAITING;
234 a->state = AVAHI_ESTABLISHED;
237 g_message("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);
242 a->time_event = NULL;
244 if (a->state == AVAHI_PROBING)
246 e->group->n_probing++;
248 AVAHI_LLIST_PREPEND(AvahiAnnouncement, by_interface, i->announcements, a);
249 AVAHI_LLIST_PREPEND(AvahiAnnouncement, by_entry, e->announcements, a);
251 if (a->state == AVAHI_PROBING) {
252 avahi_elapse_time(&tv, 0, AVAHI_PROBE_JITTER_MSEC);
254 } else if (a->state == AVAHI_ANNOUNCING) {
255 avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
260 void avahi_announce_interface(AvahiServer *s, AvahiInterface *i) {
269 for (e = s->entries; e; e = e->entries_next)
271 new_announcement(s, i, e);
274 static void announce_walk_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
275 AvahiEntry *e = userdata;
282 new_announcement(m->server, i, e);
285 void avahi_announce_entry(AvahiServer *s, AvahiEntry *e) {
290 avahi_interface_monitor_walk(s->monitor, e->interface, e->protocol, announce_walk_callback, e);
293 void avahi_announce_group(AvahiServer *s, AvahiEntryGroup *g) {
299 for (e = g->entries; e; e = e->by_group_next)
301 avahi_announce_entry(s, e);
304 gboolean avahi_entry_registered(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
305 AvahiAnnouncement *a;
312 if (!(a = avahi_get_announcement(s, e, i)))
315 return a->state == AVAHI_ANNOUNCING || a->state == AVAHI_ESTABLISHED;
318 gboolean avahi_entry_registering(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
319 AvahiAnnouncement *a;
326 if (!(a = avahi_get_announcement(s, e, i)))
329 return a->state == AVAHI_PROBING || a->state == AVAHI_WAITING;
332 static AvahiRecord *make_goodbye_record(AvahiRecord *r) {
338 /* g_message("Preparing goodbye for record [%s]", t = avahi_record_to_string(r)); */
341 g = avahi_record_copy(r);
342 g_assert(g->ref == 1);
348 static void send_goodbye_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
349 AvahiEntry *e = userdata;
357 if (!avahi_interface_match(i, e->interface, e->protocol))
360 if (e->flags & AVAHI_ENTRY_NOANNOUNCE)
363 if (!avahi_entry_registered(m->server, e, i))
366 g = make_goodbye_record(e->record);
367 avahi_interface_post_response(i, g, e->flags & AVAHI_ENTRY_UNIQUE, TRUE);
368 avahi_record_unref(g);
371 void avahi_goodbye_interface(AvahiServer *s, AvahiInterface *i, gboolean goodbye) {
375 /* g_message("goodbye interface: %s.%u", i->hardware->name, i->protocol); */
377 if (goodbye && avahi_interface_relevant(i)) {
380 for (e = s->entries; e; e = e->entries_next)
382 send_goodbye_callback(s->monitor, i, e);
385 while (i->announcements)
386 remove_announcement(s, i->announcements);
388 /* g_message("goodbye interface done: %s.%u", i->hardware->name, i->protocol); */
392 void avahi_goodbye_entry(AvahiServer *s, AvahiEntry *e, gboolean goodbye) {
396 /* g_message("goodbye entry: %p", e); */
398 if (goodbye && !e->dead)
399 avahi_interface_monitor_walk(s->monitor, 0, AF_UNSPEC, send_goodbye_callback, e);
401 while (e->announcements)
402 remove_announcement(s, e->announcements);
404 /* g_message("goodbye entry done: %p", e); */
408 void avahi_goodbye_all(AvahiServer *s, gboolean goodbye) {
413 /* g_message("goodbye all"); */
415 for (e = s->entries; e; e = e->entries_next)
417 avahi_goodbye_entry(s, e, goodbye);
419 /* g_message("goodbye all done"); */