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