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