]> git.meshlink.io Git - catta/blob - avahi-core/response-sched.c
Add support for server state change callbacks
[catta] / avahi-core / response-sched.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 "response-sched.h"
27 #include "util.h"
28
29 #define AVAHI_RESPONSE_HISTORY_MSEC 700
30 #define AVAHI_RESPONSE_DEFER_MSEC 20
31 #define AVAHI_RESPONSE_JITTER_MSEC 100
32 #define AVAHI_RESPONSE_SUPPRESS_MSEC 700
33
34 typedef struct AvahiResponseJob AvahiResponseJob;
35
36 typedef enum {
37     AVAHI_SCHEDULED,
38     AVAHI_DONE,
39     AVAHI_SUPPRESSED
40 } AvahiResponseJobState;
41
42 struct AvahiResponseJob {
43     AvahiResponseScheduler *scheduler;
44     AvahiTimeEvent *time_event;
45     
46     AvahiResponseJobState state;
47     GTimeVal delivery;
48
49     AvahiRecord *record;
50     gboolean flush_cache;
51     AvahiAddress querier;
52     gboolean querier_valid;
53     
54     AVAHI_LLIST_FIELDS(AvahiResponseJob, jobs);
55 };
56
57 struct AvahiResponseScheduler {
58     AvahiInterface *interface;
59     AvahiTimeEventQueue *time_event_queue;
60
61     AVAHI_LLIST_HEAD(AvahiResponseJob, jobs);
62     AVAHI_LLIST_HEAD(AvahiResponseJob, history);
63     AVAHI_LLIST_HEAD(AvahiResponseJob, suppressed);
64 };
65
66 static AvahiResponseJob* job_new(AvahiResponseScheduler *s, AvahiRecord *record, AvahiResponseJobState state) {
67     AvahiResponseJob *rj;
68     
69     g_assert(s);
70     g_assert(record);
71
72     rj = g_new(AvahiResponseJob, 1);
73     rj->scheduler = s;
74     rj->record = avahi_record_ref(record);
75     rj->time_event = NULL;
76     rj->flush_cache = FALSE;
77     rj->querier_valid = FALSE;
78     
79     if ((rj->state = state) == AVAHI_SCHEDULED) 
80         AVAHI_LLIST_PREPEND(AvahiResponseJob, jobs, s->jobs, rj);
81     else if (rj->state == AVAHI_DONE)
82         AVAHI_LLIST_PREPEND(AvahiResponseJob, jobs, s->history, rj);
83     else  /* rj->state == AVAHI_SUPPRESSED */
84         AVAHI_LLIST_PREPEND(AvahiResponseJob, jobs, s->suppressed, rj);
85
86     return rj;
87 }
88
89 static void job_free(AvahiResponseScheduler *s, AvahiResponseJob *rj) {
90     g_assert(s);
91     g_assert(rj);
92
93     if (rj->time_event)
94         avahi_time_event_queue_remove(s->time_event_queue, rj->time_event);
95
96     if (rj->state == AVAHI_SCHEDULED)
97         AVAHI_LLIST_REMOVE(AvahiResponseJob, jobs, s->jobs, rj);
98     else if (rj->state == AVAHI_DONE)
99         AVAHI_LLIST_REMOVE(AvahiResponseJob, jobs, s->history, rj);
100     else /* rj->state == AVAHI_SUPPRESSED */
101         AVAHI_LLIST_REMOVE(AvahiResponseJob, jobs, s->suppressed, rj);
102
103     avahi_record_unref(rj->record);
104     g_free(rj);
105 }
106
107 static void elapse_callback(AvahiTimeEvent *e, gpointer data);
108
109 static void job_set_elapse_time(AvahiResponseScheduler *s, AvahiResponseJob *rj, guint msec, guint jitter) {
110     GTimeVal tv;
111
112     g_assert(s);
113     g_assert(rj);
114
115     avahi_elapse_time(&tv, msec, jitter);
116
117     if (rj->time_event)
118         avahi_time_event_queue_update(s->time_event_queue, rj->time_event, &tv);
119     else
120         rj->time_event = avahi_time_event_queue_add(s->time_event_queue, &tv, elapse_callback, rj);
121 }
122
123 static void job_mark_done(AvahiResponseScheduler *s, AvahiResponseJob *rj) {
124     g_assert(s);
125     g_assert(rj);
126
127     g_assert(rj->state == AVAHI_SCHEDULED);
128
129     AVAHI_LLIST_REMOVE(AvahiResponseJob, jobs, s->jobs, rj);
130     AVAHI_LLIST_PREPEND(AvahiResponseJob, jobs, s->history, rj);
131
132     rj->state = AVAHI_DONE;
133
134     job_set_elapse_time(s, rj, AVAHI_RESPONSE_HISTORY_MSEC, 0);
135
136     g_get_current_time(&rj->delivery);
137 }
138
139 AvahiResponseScheduler *avahi_response_scheduler_new(AvahiInterface *i) {
140     AvahiResponseScheduler *s;
141     g_assert(i);
142
143     s = g_new(AvahiResponseScheduler, 1);
144     s->interface = i;
145     s->time_event_queue = i->monitor->server->time_event_queue;
146     
147     AVAHI_LLIST_HEAD_INIT(AvahiResponseJob, s->jobs);
148     AVAHI_LLIST_HEAD_INIT(AvahiResponseJob, s->history);
149     AVAHI_LLIST_HEAD_INIT(AvahiResponseJob, s->suppressed);
150
151     return s;
152 }
153
154 void avahi_response_scheduler_free(AvahiResponseScheduler *s) {
155     g_assert(s);
156
157     avahi_response_scheduler_clear(s);
158     g_free(s);
159 }
160
161 void avahi_response_scheduler_clear(AvahiResponseScheduler *s) {
162     g_assert(s);
163     
164     while (s->jobs)
165         job_free(s, s->jobs);
166     while (s->history)
167         job_free(s, s->history);
168     while (s->suppressed)
169         job_free(s, s->suppressed);
170 }
171
172 static void enumerate_aux_records_callback(AvahiServer *s, AvahiRecord *r, gboolean flush_cache, gpointer userdata) {
173     AvahiResponseJob *rj = userdata;
174     
175     g_assert(r);
176     g_assert(rj);
177
178     avahi_response_scheduler_post(rj->scheduler, r, flush_cache, rj->querier_valid ? &rj->querier : NULL, FALSE);
179 }
180
181 static gboolean packet_add_response_job(AvahiResponseScheduler *s, AvahiDnsPacket *p, AvahiResponseJob *rj) {
182     g_assert(s);
183     g_assert(p);
184     g_assert(rj);
185
186     /* Try to add this record to the packet */
187     if (!avahi_dns_packet_append_record(p, rj->record, rj->flush_cache, 0))
188         return FALSE;
189
190     /* Ok, this record will definitely be sent, so schedule the
191      * auxilliary packets, too */
192     avahi_server_enumerate_aux_records(s->interface->monitor->server, s->interface, rj->record, enumerate_aux_records_callback, rj);
193     job_mark_done(s, rj);
194     
195     return TRUE;
196 }
197
198 static void send_response_packet(AvahiResponseScheduler *s, AvahiResponseJob *rj) {
199     AvahiDnsPacket *p;
200     guint n;
201
202     g_assert(s);
203     g_assert(rj);
204
205     p = avahi_dns_packet_new_response(s->interface->hardware->mtu, TRUE);
206     n = 1;
207
208     /* Put it in the packet. */
209     if (packet_add_response_job(s, p, rj)) {
210
211         /* Try to fill up packet with more responses, if available */
212         while (s->jobs) {
213             
214             if (!packet_add_response_job(s, p, s->jobs))
215                 break;
216             
217             n++;
218         }
219         
220     } else {
221         guint size;
222         
223         avahi_dns_packet_free(p);
224
225         /* OK, the packet was too small, so create one that fits */
226         size = avahi_record_get_estimate_size(rj->record) + AVAHI_DNS_PACKET_HEADER_SIZE;
227
228         if (size > AVAHI_DNS_PACKET_MAX_SIZE)
229             size = AVAHI_DNS_PACKET_MAX_SIZE;
230         
231         p = avahi_dns_packet_new_response(size, TRUE);
232
233         if (!packet_add_response_job(s, p, rj)) {
234             avahi_dns_packet_free(p);
235
236             g_warning("Record too large, cannot send");
237             job_mark_done(s, rj);
238             return;
239         }
240     }
241
242     avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ANCOUNT, n);
243     avahi_interface_send_packet(s->interface, p);
244     avahi_dns_packet_free(p);
245 }
246
247 static void elapse_callback(AvahiTimeEvent *e, gpointer data) {
248     AvahiResponseJob *rj = data;
249
250     g_assert(rj);
251
252     if (rj->state == AVAHI_DONE || rj->state == AVAHI_SUPPRESSED) 
253         job_free(rj->scheduler, rj);         /* Lets drop this entry */
254     else
255         send_response_packet(rj->scheduler, rj);
256 }
257
258 static AvahiResponseJob* find_scheduled_job(AvahiResponseScheduler *s, AvahiRecord *record) {
259     AvahiResponseJob *rj;
260
261     g_assert(s);
262     g_assert(record);
263
264     for (rj = s->jobs; rj; rj = rj->jobs_next) {
265         g_assert(rj->state == AVAHI_SCHEDULED);
266     
267         if (avahi_record_equal_no_ttl(rj->record, record))
268             return rj;
269     }
270
271     return NULL;
272 }
273
274 static AvahiResponseJob* find_history_job(AvahiResponseScheduler *s, AvahiRecord *record) {
275     AvahiResponseJob *rj;
276     
277     g_assert(s);
278     g_assert(record);
279
280     for (rj = s->history; rj; rj = rj->jobs_next) {
281         g_assert(rj->state == AVAHI_DONE);
282
283         if (avahi_record_equal_no_ttl(rj->record, record)) {
284             /* Check whether this entry is outdated */
285
286             if (avahi_age(&rj->delivery) > AVAHI_RESPONSE_HISTORY_MSEC*1000) {
287                 /* it is outdated, so let's remove it */
288                 job_free(s, rj);
289                 return NULL;
290             }
291                 
292             return rj;
293         }
294     }
295
296     return NULL;
297 }
298
299 static AvahiResponseJob* find_suppressed_job(AvahiResponseScheduler *s, AvahiRecord *record, const AvahiAddress *querier) {
300     AvahiResponseJob *rj;
301     
302     g_assert(s);
303     g_assert(record);
304     g_assert(querier);
305
306     for (rj = s->suppressed; rj; rj = rj->jobs_next) {
307         g_assert(rj->state == AVAHI_SUPPRESSED);
308         g_assert(rj->querier_valid);
309         
310         if (avahi_record_equal_no_ttl(rj->record, record) &&
311             avahi_address_cmp(&rj->querier, querier) == 0) {
312             /* Check whether this entry is outdated */
313
314             if (avahi_age(&rj->delivery) > AVAHI_RESPONSE_SUPPRESS_MSEC*1000) {
315                 /* it is outdated, so let's remove it */
316                 job_free(s, rj);
317                 return NULL;
318             }
319
320             return rj;
321         }
322     }
323
324     return NULL;
325 }
326
327 gboolean avahi_response_scheduler_post(AvahiResponseScheduler *s, AvahiRecord *record, gboolean flush_cache, const AvahiAddress *querier, gboolean immediately) {
328     AvahiResponseJob *rj;
329     GTimeVal tv;
330     
331     g_assert(s);
332     g_assert(record);
333
334     g_assert(!avahi_key_is_pattern(record->key));
335
336     /* Check whether this response is suppressed */
337     if (querier &&
338         (rj = find_suppressed_job(s, record, querier)) &&
339         avahi_record_is_goodbye(record) == avahi_record_is_goodbye(rj->record) &&
340         rj->record->ttl >= record->ttl/2) {
341
342 /*         g_message("Response suppressed by known answer suppression."); */
343         return FALSE;
344     }
345
346     /* Check if we already sent this response recently */
347     if ((rj = find_history_job(s, record))) {
348
349         if (avahi_record_is_goodbye(record) == avahi_record_is_goodbye(rj->record) &&
350             rj->record->ttl >= record->ttl/2 &&
351             (rj->flush_cache || !flush_cache)) {
352 /*             g_message("Response suppressed by local duplicate suppression (history)"); */
353             return FALSE;
354         }
355
356         /* Outdated ... */
357         job_free(s, rj);
358     }
359
360     avahi_elapse_time(&tv, immediately ? 0 : AVAHI_RESPONSE_DEFER_MSEC, immediately ? 0 : AVAHI_RESPONSE_JITTER_MSEC);
361          
362     if ((rj = find_scheduled_job(s, record))) {
363 /*         g_message("Response suppressed by local duplicate suppression (scheduled)"); */
364
365         /* Update a little ... */
366
367         /* Update the time if the new is prior to the old */
368         if (avahi_timeval_compare(&tv, &rj->delivery) < 0) {
369             rj->delivery = tv;
370             avahi_time_event_queue_update(s->time_event_queue, rj->time_event, &rj->delivery);
371         }
372
373         /* Update the flush cache bit */
374         if (flush_cache)
375             rj->flush_cache = TRUE;
376
377         /* Update the querier field */
378         if (!querier || (rj->querier_valid && avahi_address_cmp(querier, &rj->querier) != 0))
379             rj->querier_valid = FALSE;
380
381         /* Update record data (just for the TTL) */
382         avahi_record_unref(rj->record);
383         rj->record = avahi_record_ref(record);
384
385         return TRUE;
386     } else {
387 /*         g_message("Accepted new response job."); */
388
389         /* Create a new job and schedule it */
390         rj = job_new(s, record, AVAHI_SCHEDULED);
391         rj->delivery = tv;
392         rj->time_event = avahi_time_event_queue_add(s->time_event_queue, &rj->delivery, elapse_callback, rj);
393         rj->flush_cache = flush_cache;
394
395         if ((rj->querier_valid = !!querier))
396             rj->querier = *querier;
397
398         return TRUE;
399     }
400 }
401
402 void avahi_response_scheduler_incoming(AvahiResponseScheduler *s, AvahiRecord *record, gboolean flush_cache) {
403     AvahiResponseJob *rj;
404     g_assert(s);
405
406     /* This function is called whenever an incoming response was
407      * receieved. We drop scheduled responses which match here. The
408      * keyword is "DUPLICATE ANSWER SUPPRESION". */
409     
410     if ((rj = find_scheduled_job(s, record))) {
411
412         if ((!rj->flush_cache || flush_cache) &&    /* flush cache bit was set correctly */
413             avahi_record_is_goodbye(record) == avahi_record_is_goodbye(rj->record) &&   /* both goodbye packets, or both not */
414             record->ttl >= rj->record->ttl/2) {     /* sensible TTL */
415
416             /* A matching entry was found, so let's mark it done */
417 /*             g_message("Response suppressed by distributed duplicate suppression"); */
418             job_mark_done(s, rj);
419         }
420
421         return;
422     }
423
424     if ((rj = find_history_job(s, record))) {
425         /* Found a history job, let's update it */
426         avahi_record_unref(rj->record);
427         rj->record = avahi_record_ref(record);
428     } else
429         /* Found no existing history job, so let's create a new one */
430         rj = job_new(s, record, AVAHI_DONE);
431
432     rj->flush_cache = flush_cache;
433     rj->querier_valid = FALSE;
434     
435     g_get_current_time(&rj->delivery);
436     job_set_elapse_time(s, rj, AVAHI_RESPONSE_HISTORY_MSEC, 0);
437 }
438
439 void avahi_response_scheduler_suppress(AvahiResponseScheduler *s, AvahiRecord *record, const AvahiAddress *querier) {
440     AvahiResponseJob *rj;
441     
442     g_assert(s);
443     g_assert(record);
444     g_assert(querier);
445
446     if ((rj = find_scheduled_job(s, record))) {
447         
448         if (rj->querier_valid && avahi_address_cmp(querier, &rj->querier) == 0 && /* same originator */
449             avahi_record_is_goodbye(record) == avahi_record_is_goodbye(rj->record) && /* both goodbye packets, or both not */
450             record->ttl >= rj->record->ttl/2) {                                  /* sensible TTL */
451
452             /* A matching entry was found, so let's drop it */
453 /*             g_message("Known answer suppression active!"); */
454             job_free(s, rj);
455         }
456     }
457
458     if ((rj = find_suppressed_job(s, record, querier))) {
459
460         /* Let's update the old entry */
461         avahi_record_unref(rj->record);
462         rj->record = avahi_record_ref(record);
463         
464     } else {
465
466         /* Create a new entry */
467         rj = job_new(s, record, AVAHI_SUPPRESSED);
468         rj->querier_valid = TRUE;
469         rj->querier = *querier;
470     }
471
472     g_get_current_time(&rj->delivery);
473     job_set_elapse_time(s, rj, AVAHI_RESPONSE_SUPPRESS_MSEC, 0);
474 }
475
476 void avahi_response_scheduler_force(AvahiResponseScheduler *s) {
477     g_assert(s);
478
479     /* Send all scheduled responses immediately */
480     while (s->jobs)
481         send_response_packet(s, s->jobs);
482 }