]> git.meshlink.io Git - catta/blob - avahi-core/announce.c
5ee0f48f2e7240d58e857b061617588a8b33b7dc
[catta] / avahi-core / announce.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include "announce.h"
27 #include "util.h"
28
29 #define AVAHI_ANNOUNCEMENT_JITTER_MSEC 250
30 #define AVAHI_PROBE_JITTER_MSEC 250
31 #define AVAHI_PROBE_INTERVAL_MSEC 250
32
33 static void remove_announcement(AvahiServer *s, AvahiAnnouncement *a) {
34     g_assert(s);
35     g_assert(a);
36
37     if (a->time_event)
38         avahi_time_event_queue_remove(s->time_event_queue, a->time_event);
39
40     AVAHI_LLIST_REMOVE(AvahiAnnouncement, by_interface, a->interface->announcements, a);
41     AVAHI_LLIST_REMOVE(AvahiAnnouncement, by_entry, a->entry->announcements, a);
42     
43     g_free(a);
44 }
45
46 static void elapse_announce(AvahiTimeEvent *e, void *userdata);
47
48 static void set_timeout(AvahiAnnouncement *a, const GTimeVal *tv) {
49     g_assert(a);
50
51     if (!tv) {
52         if (a->time_event) {
53             avahi_time_event_queue_remove(a->server->time_event_queue, a->time_event);
54             a->time_event = NULL;
55         }
56     } else {
57
58         if (a->time_event) 
59             avahi_time_event_queue_update(a->server->time_event_queue, a->time_event, tv);
60         else
61             a->time_event = avahi_time_event_queue_add(a->server->time_event_queue, tv, elapse_announce, a);
62     }
63 }
64
65 static void next_state(AvahiAnnouncement *a);
66
67 void avahi_entry_group_check_probed(AvahiEntryGroup *g, gboolean immediately) {
68     AvahiEntry *e;
69     g_assert(g);
70     g_assert(!g->dead);
71
72     /* Check whether all group members have been probed */
73     
74     if (g->state != AVAHI_ENTRY_GROUP_REGISTERING || g->n_probing > 0) 
75         return;
76
77     avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_ESTABLISHED);
78
79     if (g->dead)
80         return;
81     
82     for (e = g->entries; e; e = e->entries_next) {
83         AvahiAnnouncement *a;
84         
85         for (a = e->announcements; a; a = a->by_entry_next) {
86
87             if (a->state != AVAHI_WAITING)
88                 continue;
89             
90             a->state = AVAHI_ANNOUNCING;
91
92             if (immediately) {
93                 /* Shortcut */
94                 
95                 a->n_iteration = 1;
96                 next_state(a);
97             } else {
98                 GTimeVal tv;
99                 a->n_iteration = 0;
100                 avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
101                 set_timeout(a, &tv);
102             }
103         }
104     }
105 }
106
107 static void next_state(AvahiAnnouncement *a) {
108     g_assert(a);
109
110 /*     g_message("%i -- %u", a->state, a->n_iteration);   */
111     
112     if (a->state == AVAHI_WAITING) {
113
114         g_assert(a->entry->group);
115
116         avahi_entry_group_check_probed(a->entry->group, TRUE);
117         
118     } else if (a->state == AVAHI_PROBING) {
119
120         if (a->n_iteration >= 4) {
121             /* Probing done */
122             
123             gchar *t;
124
125             g_message("Enough probes for record [%s]", t = avahi_record_to_string(a->entry->record));
126             g_free(t);
127
128             if (a->entry->group) {
129                 g_assert(a->entry->group->n_probing);
130                 a->entry->group->n_probing--;
131             }
132             
133             if (a->entry->group && a->entry->group->state == AVAHI_ENTRY_GROUP_REGISTERING)
134                 a->state = AVAHI_WAITING;
135             else {
136                 a->state = AVAHI_ANNOUNCING;
137                 a->n_iteration = 1;
138             }
139
140             set_timeout(a, NULL);
141             next_state(a);
142         } else {
143             GTimeVal tv;
144
145             avahi_interface_post_probe(a->interface, a->entry->record, FALSE);
146             
147             avahi_elapse_time(&tv, AVAHI_PROBE_INTERVAL_MSEC, 0);
148             set_timeout(a, &tv);
149             
150             a->n_iteration++;
151         }
152
153     } else if (a->state == AVAHI_ANNOUNCING) {
154
155         avahi_interface_post_response(a->interface, a->entry->record, a->entry->flags & AVAHI_ENTRY_UNIQUE, FALSE);
156
157         if (++a->n_iteration >= 4) {
158             gchar *t;
159             /* Announcing done */
160
161             g_message("Enough announcements for record [%s]", t = avahi_record_to_string(a->entry->record));
162             g_free(t);
163
164             a->state = AVAHI_ESTABLISHED;
165
166             set_timeout(a, NULL);
167         } else {
168             GTimeVal tv;
169             avahi_elapse_time(&tv, a->sec_delay*1000, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
170         
171             if (a->n_iteration < 10)
172                 a->sec_delay *= 2;
173             
174             set_timeout(a, &tv);
175         }
176     }
177 }
178
179 static void elapse_announce(AvahiTimeEvent *e, void *userdata) {
180     g_assert(e);
181
182     next_state(userdata);
183 }
184
185 AvahiAnnouncement *avahi_get_announcement(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
186     AvahiAnnouncement *a;
187     
188     g_assert(s);
189     g_assert(e);
190     g_assert(i);
191
192     for (a = e->announcements; a; a = a->by_entry_next)
193         if (a->interface == i)
194             return a;
195
196     return NULL;
197 }
198
199 static void new_announcement(AvahiServer *s, AvahiInterface *i, AvahiEntry *e) {
200     AvahiAnnouncement *a;
201     GTimeVal tv;
202     gchar *t; 
203
204     g_assert(s);
205     g_assert(i);
206     g_assert(e);
207     g_assert(!e->dead);
208
209 /*     g_message("NEW ANNOUNCEMENT: %s.%i [%s]", i->hardware->name, i->protocol, t = avahi_record_to_string(e->record)); */
210 /*     g_free(t); */
211     
212     if (!avahi_interface_match(i, e->interface, e->protocol) || !i->announcing || !avahi_entry_commited(e))
213         return;
214
215     /* We don't want duplicate announcements */
216     if (avahi_get_announcement(s, e, i))
217         return;
218
219     a = g_new(AvahiAnnouncement, 1);
220     a->server = s;
221     a->interface = i;
222     a->entry = e;
223
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)) {
227
228         if (!e->group || e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED)
229             a->state = AVAHI_ANNOUNCING;
230         else
231             a->state = AVAHI_WAITING;
232         
233     } else
234         a->state = AVAHI_ESTABLISHED;
235
236
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);
238     g_free(t);
239
240     a->n_iteration = 1;
241     a->sec_delay = 1;
242     a->time_event = NULL;
243
244     if (a->state == AVAHI_PROBING)
245         if (e->group)
246             e->group->n_probing++;
247     
248     AVAHI_LLIST_PREPEND(AvahiAnnouncement, by_interface, i->announcements, a);
249     AVAHI_LLIST_PREPEND(AvahiAnnouncement, by_entry, e->announcements, a);
250
251     if (a->state == AVAHI_PROBING) {
252         avahi_elapse_time(&tv, 0, AVAHI_PROBE_JITTER_MSEC);
253         set_timeout(a, &tv);
254     } else if (a->state == AVAHI_ANNOUNCING) {
255         avahi_elapse_time(&tv, 0, AVAHI_ANNOUNCEMENT_JITTER_MSEC);
256         set_timeout(a, &tv);
257     }
258 }
259
260 void avahi_announce_interface(AvahiServer *s, AvahiInterface *i) {
261     AvahiEntry *e;
262     
263     g_assert(s);
264     g_assert(i);
265
266     if (!i->announcing)
267         return;
268
269     for (e = s->entries; e; e = e->entries_next)
270         if (!e->dead)
271             new_announcement(s, i, e);
272 }
273
274 static void announce_walk_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
275     AvahiEntry *e = userdata;
276     
277     g_assert(m);
278     g_assert(i);
279     g_assert(e);
280     g_assert(!e->dead);
281
282     new_announcement(m->server, i, e);
283 }
284
285 void avahi_announce_entry(AvahiServer *s, AvahiEntry *e) {
286     g_assert(s);
287     g_assert(e);
288     g_assert(!e->dead);
289
290     avahi_interface_monitor_walk(s->monitor, e->interface, e->protocol, announce_walk_callback, e);
291 }
292
293 void avahi_announce_group(AvahiServer *s, AvahiEntryGroup *g) {
294     AvahiEntry *e;
295     
296     g_assert(s);
297     g_assert(g);
298
299     for (e = g->entries; e; e = e->by_group_next)
300         if (!e->dead)
301             avahi_announce_entry(s, e);
302 }
303
304 gboolean avahi_entry_registered(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
305     AvahiAnnouncement *a;
306
307     g_assert(s);
308     g_assert(e);
309     g_assert(i);
310     g_assert(!e->dead);
311
312     if (!(a = avahi_get_announcement(s, e, i)))
313         return FALSE;
314     
315     return a->state == AVAHI_ANNOUNCING || a->state == AVAHI_ESTABLISHED;
316 }
317
318 gboolean avahi_entry_registering(AvahiServer *s, AvahiEntry *e, AvahiInterface *i) {
319     AvahiAnnouncement *a;
320
321     g_assert(s);
322     g_assert(e);
323     g_assert(i);
324     g_assert(!e->dead);
325
326     if (!(a = avahi_get_announcement(s, e, i)))
327         return FALSE;
328     
329     return a->state == AVAHI_PROBING || a->state == AVAHI_WAITING;
330 }
331
332 static AvahiRecord *make_goodbye_record(AvahiRecord *r) {
333 /*     gchar *t; */
334     AvahiRecord *g;
335     
336     g_assert(r);
337
338 /*     g_message("Preparing goodbye for record [%s]", t = avahi_record_to_string(r)); */
339 /*     g_free(t); */
340
341     g = avahi_record_copy(r);
342     g_assert(g->ref == 1);
343     g->ttl = 0;
344
345     return g;
346 }
347
348 static void send_goodbye_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
349     AvahiEntry *e = userdata;
350     AvahiRecord *g;
351     
352     g_assert(m);
353     g_assert(i);
354     g_assert(e);
355     g_assert(!e->dead);
356
357     if (!avahi_interface_match(i, e->interface, e->protocol))
358         return;
359
360     if (e->flags & AVAHI_ENTRY_NOANNOUNCE)
361         return;
362
363     if (!avahi_entry_registered(m->server, e, i))
364         return;
365     
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);
369 }
370     
371 void avahi_goodbye_interface(AvahiServer *s, AvahiInterface *i, gboolean goodbye) {
372     g_assert(s);
373     g_assert(i);
374
375 /*     g_message("goodbye interface: %s.%u", i->hardware->name, i->protocol); */
376
377     if (goodbye && avahi_interface_relevant(i)) {
378         AvahiEntry *e;
379         
380         for (e = s->entries; e; e = e->entries_next)
381             if (!e->dead)
382                 send_goodbye_callback(s->monitor, i, e);
383     }
384
385     while (i->announcements)
386         remove_announcement(s, i->announcements);
387
388 /*     g_message("goodbye interface done: %s.%u", i->hardware->name, i->protocol); */
389
390 }
391
392 void avahi_goodbye_entry(AvahiServer *s, AvahiEntry *e, gboolean goodbye) {
393     g_assert(s);
394     g_assert(e);
395     
396 /*     g_message("goodbye entry: %p", e); */
397     
398     if (goodbye && !e->dead)
399         avahi_interface_monitor_walk(s->monitor, 0, AF_UNSPEC, send_goodbye_callback, e);
400
401     while (e->announcements)
402         remove_announcement(s, e->announcements);
403
404 /*     g_message("goodbye entry done: %p", e); */
405
406 }
407
408 void avahi_goodbye_all(AvahiServer *s, gboolean goodbye) {
409     AvahiEntry *e;
410     
411     g_assert(s);
412
413 /*     g_message("goodbye all"); */
414
415     for (e = s->entries; e; e = e->entries_next)
416         if (!e->dead)
417             avahi_goodbye_entry(s, e, goodbye);
418
419 /*     g_message("goodbye all done"); */
420
421 }
422