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
26 #include "response-sched.h"
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
34 typedef struct AvahiResponseJob AvahiResponseJob;
40 } AvahiResponseJobState;
42 struct AvahiResponseJob {
43 AvahiResponseScheduler *scheduler;
44 AvahiTimeEvent *time_event;
46 AvahiResponseJobState state;
52 gboolean querier_valid;
54 AVAHI_LLIST_FIELDS(AvahiResponseJob, jobs);
57 struct AvahiResponseScheduler {
58 AvahiInterface *interface;
59 AvahiTimeEventQueue *time_event_queue;
61 AVAHI_LLIST_HEAD(AvahiResponseJob, jobs);
62 AVAHI_LLIST_HEAD(AvahiResponseJob, history);
63 AVAHI_LLIST_HEAD(AvahiResponseJob, suppressed);
66 static AvahiResponseJob* job_new(AvahiResponseScheduler *s, AvahiRecord *record, AvahiResponseJobState state) {
72 rj = g_new(AvahiResponseJob, 1);
74 rj->record = avahi_record_ref(record);
75 rj->time_event = NULL;
76 rj->flush_cache = FALSE;
77 rj->querier_valid = FALSE;
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);
89 static void job_free(AvahiResponseScheduler *s, AvahiResponseJob *rj) {
94 avahi_time_event_queue_remove(s->time_event_queue, rj->time_event);
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);
103 avahi_record_unref(rj->record);
107 static void elapse_callback(AvahiTimeEvent *e, gpointer data);
109 static void job_set_elapse_time(AvahiResponseScheduler *s, AvahiResponseJob *rj, guint msec, guint jitter) {
115 avahi_elapse_time(&tv, msec, jitter);
118 avahi_time_event_queue_update(s->time_event_queue, rj->time_event, &tv);
120 rj->time_event = avahi_time_event_queue_add(s->time_event_queue, &tv, elapse_callback, rj);
123 static void job_mark_done(AvahiResponseScheduler *s, AvahiResponseJob *rj) {
127 g_assert(rj->state == AVAHI_SCHEDULED);
129 AVAHI_LLIST_REMOVE(AvahiResponseJob, jobs, s->jobs, rj);
130 AVAHI_LLIST_PREPEND(AvahiResponseJob, jobs, s->history, rj);
132 rj->state = AVAHI_DONE;
134 job_set_elapse_time(s, rj, AVAHI_RESPONSE_HISTORY_MSEC, 0);
136 g_get_current_time(&rj->delivery);
139 AvahiResponseScheduler *avahi_response_scheduler_new(AvahiInterface *i) {
140 AvahiResponseScheduler *s;
143 s = g_new(AvahiResponseScheduler, 1);
145 s->time_event_queue = i->monitor->server->time_event_queue;
147 AVAHI_LLIST_HEAD_INIT(AvahiResponseJob, s->jobs);
148 AVAHI_LLIST_HEAD_INIT(AvahiResponseJob, s->history);
149 AVAHI_LLIST_HEAD_INIT(AvahiResponseJob, s->suppressed);
154 void avahi_response_scheduler_free(AvahiResponseScheduler *s) {
157 avahi_response_scheduler_clear(s);
161 void avahi_response_scheduler_clear(AvahiResponseScheduler *s) {
165 job_free(s, s->jobs);
167 job_free(s, s->history);
168 while (s->suppressed)
169 job_free(s, s->suppressed);
172 static void enumerate_aux_records_callback(AvahiServer *s, AvahiRecord *r, gboolean flush_cache, gpointer userdata) {
173 AvahiResponseJob *rj = userdata;
178 avahi_response_scheduler_post(rj->scheduler, r, flush_cache, rj->querier_valid ? &rj->querier : NULL, FALSE);
181 static gboolean packet_add_response_job(AvahiResponseScheduler *s, AvahiDnsPacket *p, AvahiResponseJob *rj) {
186 /* Try to add this record to the packet */
187 if (!avahi_dns_packet_append_record(p, rj->record, rj->flush_cache, 0))
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);
198 static void send_response_packet(AvahiResponseScheduler *s, AvahiResponseJob *rj) {
205 p = avahi_dns_packet_new_response(s->interface->hardware->mtu, TRUE);
208 /* Put it in the packet. */
209 if (packet_add_response_job(s, p, rj)) {
211 /* Try to fill up packet with more responses, if available */
214 if (!packet_add_response_job(s, p, s->jobs))
223 avahi_dns_packet_free(p);
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;
228 if (size > AVAHI_DNS_PACKET_MAX_SIZE)
229 size = AVAHI_DNS_PACKET_MAX_SIZE;
231 p = avahi_dns_packet_new_response(size, TRUE);
233 if (!packet_add_response_job(s, p, rj)) {
234 avahi_dns_packet_free(p);
236 g_warning("Record too large, cannot send");
237 job_mark_done(s, rj);
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);
247 static void elapse_callback(AvahiTimeEvent *e, gpointer data) {
248 AvahiResponseJob *rj = data;
252 if (rj->state == AVAHI_DONE || rj->state == AVAHI_SUPPRESSED)
253 job_free(rj->scheduler, rj); /* Lets drop this entry */
255 send_response_packet(rj->scheduler, rj);
258 static AvahiResponseJob* find_scheduled_job(AvahiResponseScheduler *s, AvahiRecord *record) {
259 AvahiResponseJob *rj;
264 for (rj = s->jobs; rj; rj = rj->jobs_next) {
265 g_assert(rj->state == AVAHI_SCHEDULED);
267 if (avahi_record_equal_no_ttl(rj->record, record))
274 static AvahiResponseJob* find_history_job(AvahiResponseScheduler *s, AvahiRecord *record) {
275 AvahiResponseJob *rj;
280 for (rj = s->history; rj; rj = rj->jobs_next) {
281 g_assert(rj->state == AVAHI_DONE);
283 if (avahi_record_equal_no_ttl(rj->record, record)) {
284 /* Check whether this entry is outdated */
286 if (avahi_age(&rj->delivery) > AVAHI_RESPONSE_HISTORY_MSEC*1000) {
287 /* it is outdated, so let's remove it */
299 static AvahiResponseJob* find_suppressed_job(AvahiResponseScheduler *s, AvahiRecord *record, const AvahiAddress *querier) {
300 AvahiResponseJob *rj;
306 for (rj = s->suppressed; rj; rj = rj->jobs_next) {
307 g_assert(rj->state == AVAHI_SUPPRESSED);
308 g_assert(rj->querier_valid);
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 */
314 if (avahi_age(&rj->delivery) > AVAHI_RESPONSE_SUPPRESS_MSEC*1000) {
315 /* it is outdated, so let's remove it */
327 gboolean avahi_response_scheduler_post(AvahiResponseScheduler *s, AvahiRecord *record, gboolean flush_cache, const AvahiAddress *querier, gboolean immediately) {
328 AvahiResponseJob *rj;
334 g_assert(!avahi_key_is_pattern(record->key));
336 /* Check whether this response is suppressed */
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) {
342 /* g_message("Response suppressed by known answer suppression."); */
346 /* Check if we already sent this response recently */
347 if ((rj = find_history_job(s, record))) {
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)"); */
360 avahi_elapse_time(&tv, immediately ? 0 : AVAHI_RESPONSE_DEFER_MSEC, immediately ? 0 : AVAHI_RESPONSE_JITTER_MSEC);
362 if ((rj = find_scheduled_job(s, record))) {
363 /* g_message("Response suppressed by local duplicate suppression (scheduled)"); */
365 /* Update a little ... */
367 /* Update the time if the new is prior to the old */
368 if (avahi_timeval_compare(&tv, &rj->delivery) < 0) {
370 avahi_time_event_queue_update(s->time_event_queue, rj->time_event, &rj->delivery);
373 /* Update the flush cache bit */
375 rj->flush_cache = TRUE;
377 /* Update the querier field */
378 if (!querier || (rj->querier_valid && avahi_address_cmp(querier, &rj->querier) != 0))
379 rj->querier_valid = FALSE;
381 /* Update record data (just for the TTL) */
382 avahi_record_unref(rj->record);
383 rj->record = avahi_record_ref(record);
387 /* g_message("Accepted new response job."); */
389 /* Create a new job and schedule it */
390 rj = job_new(s, record, AVAHI_SCHEDULED);
392 rj->time_event = avahi_time_event_queue_add(s->time_event_queue, &rj->delivery, elapse_callback, rj);
393 rj->flush_cache = flush_cache;
395 if ((rj->querier_valid = !!querier))
396 rj->querier = *querier;
402 void avahi_response_scheduler_incoming(AvahiResponseScheduler *s, AvahiRecord *record, gboolean flush_cache) {
403 AvahiResponseJob *rj;
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". */
410 if ((rj = find_scheduled_job(s, record))) {
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 */
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);
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);
429 /* Found no existing history job, so let's create a new one */
430 rj = job_new(s, record, AVAHI_DONE);
432 rj->flush_cache = flush_cache;
433 rj->querier_valid = FALSE;
435 g_get_current_time(&rj->delivery);
436 job_set_elapse_time(s, rj, AVAHI_RESPONSE_HISTORY_MSEC, 0);
439 void avahi_response_scheduler_suppress(AvahiResponseScheduler *s, AvahiRecord *record, const AvahiAddress *querier) {
440 AvahiResponseJob *rj;
446 if ((rj = find_scheduled_job(s, record))) {
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 */
452 /* A matching entry was found, so let's drop it */
453 /* g_message("Known answer suppression active!"); */
458 if ((rj = find_suppressed_job(s, record, querier))) {
460 /* Let's update the old entry */
461 avahi_record_unref(rj->record);
462 rj->record = avahi_record_ref(record);
466 /* Create a new entry */
467 rj = job_new(s, record, AVAHI_SUPPRESSED);
468 rj->querier_valid = TRUE;
469 rj->querier = *querier;
472 g_get_current_time(&rj->delivery);
473 job_set_elapse_time(s, rj, AVAHI_RESPONSE_SUPPRESS_MSEC, 0);
476 void avahi_response_scheduler_force(AvahiResponseScheduler *s) {
479 /* Send all scheduled responses immediately */
481 send_response_packet(s, s->jobs);