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
25 #define AVAHI_ANNOUNCEMENT_JITTER_MSEC 250
26 #define AVAHI_PROBE_JITTER_MSEC 250
27 #define AVAHI_PROBE_INTERVAL_MSEC 250
29 static void remove_announcement(AvahiServer *s, AvahiAnnouncement *a) {
34 avahi_time_event_queue_remove(s->time_event_queue, a->time_event);
36 AVAHI_LLIST_REMOVE(AvahiAnnouncement, by_interface, a->interface->announcements, a);
37 AVAHI_LLIST_REMOVE(AvahiAnnouncement, by_entry, a->entry->announcements, a);
42 static void elapse_announce(AvahiTimeEvent *e, void *userdata);
44 static void set_timeout(AvahiAnnouncement *a, const GTimeVal *tv) {
49 avahi_time_event_queue_remove(a->server->time_event_queue, a->time_event);
55 avahi_time_event_queue_update(a->server->time_event_queue, a->time_event, tv);
57 a->time_event = avahi_time_event_queue_add(a->server->time_event_queue, tv, elapse_announce, a);
61 static void next_state(AvahiAnnouncement *a);
63 void avahi_entry_group_check_probed(AvahiEntryGroup *g, gboolean immediately) {
68 /* Check whether all group members have been probed */
70 if (g->state != AVAHI_ENTRY_GROUP_REGISTERING || g->n_probing > 0)
73 avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_ESTABLISHED);
78 for (e = g->entries; e; e = e->entries_next) {
81 for (a = e->announcements; a; a = a->by_entry_next) {
83 if (a->state != AVAHI_WAITING)
86 a->state = AVAHI_ANNOUNCING;
96 avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
103 static void next_state(AvahiAnnouncement *a) {
106 /* g_message("%i -- %u", a->state, a->n_iteration); */
108 if (a->state == AVAHI_WAITING) {
110 g_assert(a->entry->group);
112 avahi_entry_group_check_probed(a->entry->group, TRUE);
114 } else if (a->state == AVAHI_PROBING) {
116 if (a->n_iteration >= 4) {
121 g_message("Enough probes for record [%s]", t = avahi_record_to_string(a->entry->record));
124 if (a->entry->group) {
125 g_assert(a->entry->group->n_probing);
126 a->entry->group->n_probing--;
129 if (a->entry->group && a->entry->group->state == AVAHI_ENTRY_GROUP_REGISTERING)
130 a->state = AVAHI_WAITING;
132 a->state = AVAHI_ANNOUNCING;
136 set_timeout(a, NULL);
141 avahi_interface_post_probe(a->interface, a->entry->record, FALSE);
143 avahi_elapse_time(&tv, AVAHI_PROBE_INTERVAL_MSEC, 0);
149 } else if (a->state == AVAHI_ANNOUNCING) {
151 avahi_interface_post_response(a->interface, NULL, a->entry->record, a->entry->flags & AVAHI_ENTRY_UNIQUE, FALSE);
153 if (++a->n_iteration >= 4) {
155 /* Announcing done */
157 g_message("Enough announcements for record [%s]", t = avahi_record_to_string(a->entry->record));
160 a->state = AVAHI_ESTABLISHED;
162 set_timeout(a, NULL);
165 avahi_elapse_time(&tv, a->sec_delay*1000, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
167 if (a->n_iteration < 10)
175 static void elapse_announce(AvahiTimeEvent *e, void *userdata) {
178 next_state(userdata);
181 AvahiAnnouncement *avahi_get_announcement(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
182 AvahiAnnouncement *a;
188 for (a = e->announcements; a; a = a->by_entry_next)
189 if (a->interface == i)
195 static void new_announcement(AvahiServer *s, AvahiInterface *i, AvahiEntry *e) {
196 AvahiAnnouncement *a;
205 /* g_message("NEW ANNOUNCEMENT: %s.%i [%s]", i->hardware->name, i->protocol, t = avahi_record_to_string(e->record)); */
208 if (!avahi_interface_match(i, e->interface, e->protocol) || !i->announcing || !avahi_entry_commited(e))
211 /* We don't want duplicate announcements */
212 if (avahi_get_announcement(s, e, i))
215 a = g_new(AvahiAnnouncement, 1);
220 if ((e->flags & AVAHI_ENTRY_UNIQUE) && !(e->flags & AVAHI_ENTRY_NOPROBE))
221 a->state = AVAHI_PROBING;
222 else if (!(e->flags & AVAHI_ENTRY_NOANNOUNCE)) {
224 if (!e->group || e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED)
225 a->state = AVAHI_ANNOUNCING;
227 a->state = AVAHI_WAITING;
230 a->state = AVAHI_ESTABLISHED;
233 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);
238 a->time_event = NULL;
240 if (a->state == AVAHI_PROBING)
242 e->group->n_probing++;
244 AVAHI_LLIST_PREPEND(AvahiAnnouncement, by_interface, i->announcements, a);
245 AVAHI_LLIST_PREPEND(AvahiAnnouncement, by_entry, e->announcements, a);
247 if (a->state == AVAHI_PROBING) {
248 avahi_elapse_time(&tv, 0, AVAHI_PROBE_JITTER_MSEC);
250 } else if (a->state == AVAHI_ANNOUNCING) {
251 avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
256 void avahi_announce_interface(AvahiServer *s, AvahiInterface *i) {
265 for (e = s->entries; e; e = e->entries_next)
267 new_announcement(s, i, e);
270 static void announce_walk_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
271 AvahiEntry *e = userdata;
278 new_announcement(m->server, i, e);
281 void avahi_announce_entry(AvahiServer *s, AvahiEntry *e) {
286 avahi_interface_monitor_walk(s->monitor, e->interface, e->protocol, announce_walk_callback, e);
289 void avahi_announce_group(AvahiServer *s, AvahiEntryGroup *g) {
295 for (e = g->entries; e; e = e->by_group_next)
297 avahi_announce_entry(s, e);
300 gboolean avahi_entry_registered(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
301 AvahiAnnouncement *a;
308 if (!(a = avahi_get_announcement(s, e, i)))
311 return a->state == AVAHI_ANNOUNCING || a->state == AVAHI_ESTABLISHED;
314 gboolean avahi_entry_registering(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
315 AvahiAnnouncement *a;
322 if (!(a = avahi_get_announcement(s, e, i)))
325 return a->state == AVAHI_PROBING || a->state == AVAHI_WAITING;
328 static AvahiRecord *make_goodbye_record(AvahiRecord *r) {
334 /* g_message("Preparing goodbye for record [%s]", t = avahi_record_to_string(r)); */
337 g = avahi_record_copy(r);
338 g_assert(g->ref == 1);
344 static void send_goodbye_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
345 AvahiEntry *e = userdata;
353 if (!avahi_interface_match(i, e->interface, e->protocol))
356 if (e->flags & AVAHI_ENTRY_NOANNOUNCE)
359 if (!avahi_entry_registered(m->server, e, i))
362 g = make_goodbye_record(e->record);
363 avahi_interface_post_response(i, NULL, g, e->flags & AVAHI_ENTRY_UNIQUE, TRUE);
364 avahi_record_unref(g);
367 void avahi_goodbye_interface(AvahiServer *s, AvahiInterface *i, gboolean goodbye) {
371 /* g_message("goodbye interface: %s.%u", i->hardware->name, i->protocol); */
373 if (goodbye && avahi_interface_relevant(i)) {
376 for (e = s->entries; e; e = e->entries_next)
378 send_goodbye_callback(s->monitor, i, e);
381 while (i->announcements)
382 remove_announcement(s, i->announcements);
384 /* g_message("goodbye interface done: %s.%u", i->hardware->name, i->protocol); */
388 void avahi_goodbye_entry(AvahiServer *s, AvahiEntry *e, gboolean goodbye) {
392 /* g_message("goodbye entry: %p", e); */
394 if (goodbye && !e->dead)
395 avahi_interface_monitor_walk(s->monitor, 0, AF_UNSPEC, send_goodbye_callback, e);
397 while (e->announcements)
398 remove_announcement(s, e->announcements);
400 /* g_message("goodbye entry done: %p", e); */
404 void avahi_goodbye_all(AvahiServer *s, gboolean goodbye) {
409 /* g_message("goodbye all"); */
411 for (e = s->entries; e; e = e->entries_next)
413 avahi_goodbye_entry(s, e, goodbye);
415 /* g_message("goodbye all done"); */