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"
30 #define AVAHI_RESPONSE_HISTORY_MSEC 700
31 #define AVAHI_RESPONSE_DEFER_MSEC 20
32 #define AVAHI_RESPONSE_JITTER_MSEC 100
33 #define AVAHI_RESPONSE_SUPPRESS_MSEC 700
35 typedef struct AvahiResponseJob AvahiResponseJob;
41 } AvahiResponseJobState;
43 struct AvahiResponseJob {
44 AvahiResponseScheduler *scheduler;
45 AvahiTimeEvent *time_event;
47 AvahiResponseJobState state;
53 gboolean querier_valid;
55 AVAHI_LLIST_FIELDS(AvahiResponseJob, jobs);
58 struct AvahiResponseScheduler {
59 AvahiInterface *interface;
60 AvahiTimeEventQueue *time_event_queue;
62 AVAHI_LLIST_HEAD(AvahiResponseJob, jobs);
63 AVAHI_LLIST_HEAD(AvahiResponseJob, history);
64 AVAHI_LLIST_HEAD(AvahiResponseJob, suppressed);
67 static AvahiResponseJob* job_new(AvahiResponseScheduler *s, AvahiRecord *record, AvahiResponseJobState state) {
73 rj = g_new(AvahiResponseJob, 1);
75 rj->record = avahi_record_ref(record);
76 rj->time_event = NULL;
77 rj->flush_cache = FALSE;
78 rj->querier_valid = FALSE;
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);
90 static void job_free(AvahiResponseScheduler *s, AvahiResponseJob *rj) {
95 avahi_time_event_queue_remove(s->time_event_queue, rj->time_event);
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);
104 avahi_record_unref(rj->record);
108 static void elapse_callback(AvahiTimeEvent *e, gpointer data);
110 static void job_set_elapse_time(AvahiResponseScheduler *s, AvahiResponseJob *rj, guint msec, guint jitter) {
116 avahi_elapse_time(&tv, msec, jitter);
119 avahi_time_event_queue_update(s->time_event_queue, rj->time_event, &tv);
121 rj->time_event = avahi_time_event_queue_add(s->time_event_queue, &tv, elapse_callback, rj);
124 static void job_mark_done(AvahiResponseScheduler *s, AvahiResponseJob *rj) {
128 g_assert(rj->state == AVAHI_SCHEDULED);
130 AVAHI_LLIST_REMOVE(AvahiResponseJob, jobs, s->jobs, rj);
131 AVAHI_LLIST_PREPEND(AvahiResponseJob, jobs, s->history, rj);
133 rj->state = AVAHI_DONE;
135 job_set_elapse_time(s, rj, AVAHI_RESPONSE_HISTORY_MSEC, 0);
137 g_get_current_time(&rj->delivery);
140 AvahiResponseScheduler *avahi_response_scheduler_new(AvahiInterface *i) {
141 AvahiResponseScheduler *s;
144 s = g_new(AvahiResponseScheduler, 1);
146 s->time_event_queue = i->monitor->server->time_event_queue;
148 AVAHI_LLIST_HEAD_INIT(AvahiResponseJob, s->jobs);
149 AVAHI_LLIST_HEAD_INIT(AvahiResponseJob, s->history);
150 AVAHI_LLIST_HEAD_INIT(AvahiResponseJob, s->suppressed);
155 void avahi_response_scheduler_free(AvahiResponseScheduler *s) {
158 avahi_response_scheduler_clear(s);
162 void avahi_response_scheduler_clear(AvahiResponseScheduler *s) {
166 job_free(s, s->jobs);
168 job_free(s, s->history);
169 while (s->suppressed)
170 job_free(s, s->suppressed);
173 static void enumerate_aux_records_callback(AvahiServer *s, AvahiRecord *r, gboolean flush_cache, gpointer userdata) {
174 AvahiResponseJob *rj = userdata;
179 avahi_response_scheduler_post(rj->scheduler, r, flush_cache, rj->querier_valid ? &rj->querier : NULL, FALSE);
182 static gboolean packet_add_response_job(AvahiResponseScheduler *s, AvahiDnsPacket *p, AvahiResponseJob *rj) {
187 /* Try to add this record to the packet */
188 if (!avahi_dns_packet_append_record(p, rj->record, rj->flush_cache, 0))
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);
199 static void send_response_packet(AvahiResponseScheduler *s, AvahiResponseJob *rj) {
206 p = avahi_dns_packet_new_response(s->interface->hardware->mtu, TRUE);
209 /* Put it in the packet. */
210 if (packet_add_response_job(s, p, rj)) {
212 /* Try to fill up packet with more responses, if available */
215 if (!packet_add_response_job(s, p, s->jobs))
224 avahi_dns_packet_free(p);
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;
229 if (size > AVAHI_DNS_PACKET_MAX_SIZE)
230 size = AVAHI_DNS_PACKET_MAX_SIZE;
232 p = avahi_dns_packet_new_response(size, TRUE);
234 if (!packet_add_response_job(s, p, rj)) {
235 avahi_dns_packet_free(p);
237 avahi_log_warn("Record too large, cannot send");
238 job_mark_done(s, rj);
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);
248 static void elapse_callback(AvahiTimeEvent *e, gpointer data) {
249 AvahiResponseJob *rj = data;
253 if (rj->state == AVAHI_DONE || rj->state == AVAHI_SUPPRESSED)
254 job_free(rj->scheduler, rj); /* Lets drop this entry */
256 send_response_packet(rj->scheduler, rj);
259 static AvahiResponseJob* find_scheduled_job(AvahiResponseScheduler *s, AvahiRecord *record) {
260 AvahiResponseJob *rj;
265 for (rj = s->jobs; rj; rj = rj->jobs_next) {
266 g_assert(rj->state == AVAHI_SCHEDULED);
268 if (avahi_record_equal_no_ttl(rj->record, record))
275 static AvahiResponseJob* find_history_job(AvahiResponseScheduler *s, AvahiRecord *record) {
276 AvahiResponseJob *rj;
281 for (rj = s->history; rj; rj = rj->jobs_next) {
282 g_assert(rj->state == AVAHI_DONE);
284 if (avahi_record_equal_no_ttl(rj->record, record)) {
285 /* Check whether this entry is outdated */
287 if (avahi_age(&rj->delivery) > AVAHI_RESPONSE_HISTORY_MSEC*1000) {
288 /* it is outdated, so let's remove it */
300 static AvahiResponseJob* find_suppressed_job(AvahiResponseScheduler *s, AvahiRecord *record, const AvahiAddress *querier) {
301 AvahiResponseJob *rj;
307 for (rj = s->suppressed; rj; rj = rj->jobs_next) {
308 g_assert(rj->state == AVAHI_SUPPRESSED);
309 g_assert(rj->querier_valid);
311 if (avahi_record_equal_no_ttl(rj->record, record) &&
312 avahi_address_cmp(&rj->querier, querier) == 0) {
313 /* Check whether this entry is outdated */
315 if (avahi_age(&rj->delivery) > AVAHI_RESPONSE_SUPPRESS_MSEC*1000) {
316 /* it is outdated, so let's remove it */
328 gboolean avahi_response_scheduler_post(AvahiResponseScheduler *s, AvahiRecord *record, gboolean flush_cache, const AvahiAddress *querier, gboolean immediately) {
329 AvahiResponseJob *rj;
335 g_assert(!avahi_key_is_pattern(record->key));
337 /* Check whether this response is suppressed */
339 (rj = find_suppressed_job(s, record, querier)) &&
340 avahi_record_is_goodbye(record) == avahi_record_is_goodbye(rj->record) &&
341 rj->record->ttl >= record->ttl/2) {
343 /* avahi_log_debug("Response suppressed by known answer suppression."); */
347 /* Check if we already sent this response recently */
348 if ((rj = find_history_job(s, record))) {
350 if (avahi_record_is_goodbye(record) == avahi_record_is_goodbye(rj->record) &&
351 rj->record->ttl >= record->ttl/2 &&
352 (rj->flush_cache || !flush_cache)) {
353 /* avahi_log_debug("Response suppressed by local duplicate suppression (history)"); */
361 avahi_elapse_time(&tv, immediately ? 0 : AVAHI_RESPONSE_DEFER_MSEC, immediately ? 0 : AVAHI_RESPONSE_JITTER_MSEC);
363 if ((rj = find_scheduled_job(s, record))) {
364 /* avahi_log_debug("Response suppressed by local duplicate suppression (scheduled)"); */
366 /* Update a little ... */
368 /* Update the time if the new is prior to the old */
369 if (avahi_timeval_compare(&tv, &rj->delivery) < 0) {
371 avahi_time_event_queue_update(s->time_event_queue, rj->time_event, &rj->delivery);
374 /* Update the flush cache bit */
376 rj->flush_cache = TRUE;
378 /* Update the querier field */
379 if (!querier || (rj->querier_valid && avahi_address_cmp(querier, &rj->querier) != 0))
380 rj->querier_valid = FALSE;
382 /* Update record data (just for the TTL) */
383 avahi_record_unref(rj->record);
384 rj->record = avahi_record_ref(record);
388 /* avahi_log_debug("Accepted new response job."); */
390 /* Create a new job and schedule it */
391 rj = job_new(s, record, AVAHI_SCHEDULED);
393 rj->time_event = avahi_time_event_queue_add(s->time_event_queue, &rj->delivery, elapse_callback, rj);
394 rj->flush_cache = flush_cache;
396 if ((rj->querier_valid = !!querier))
397 rj->querier = *querier;
403 void avahi_response_scheduler_incoming(AvahiResponseScheduler *s, AvahiRecord *record, gboolean flush_cache) {
404 AvahiResponseJob *rj;
407 /* This function is called whenever an incoming response was
408 * receieved. We drop scheduled responses which match here. The
409 * keyword is "DUPLICATE ANSWER SUPPRESION". */
411 if ((rj = find_scheduled_job(s, record))) {
413 if ((!rj->flush_cache || flush_cache) && /* flush cache bit was set correctly */
414 avahi_record_is_goodbye(record) == avahi_record_is_goodbye(rj->record) && /* both goodbye packets, or both not */
415 record->ttl >= rj->record->ttl/2) { /* sensible TTL */
417 /* A matching entry was found, so let's mark it done */
418 /* avahi_log_debug("Response suppressed by distributed duplicate suppression"); */
419 job_mark_done(s, rj);
425 if ((rj = find_history_job(s, record))) {
426 /* Found a history job, let's update it */
427 avahi_record_unref(rj->record);
428 rj->record = avahi_record_ref(record);
430 /* Found no existing history job, so let's create a new one */
431 rj = job_new(s, record, AVAHI_DONE);
433 rj->flush_cache = flush_cache;
434 rj->querier_valid = FALSE;
436 g_get_current_time(&rj->delivery);
437 job_set_elapse_time(s, rj, AVAHI_RESPONSE_HISTORY_MSEC, 0);
440 void avahi_response_scheduler_suppress(AvahiResponseScheduler *s, AvahiRecord *record, const AvahiAddress *querier) {
441 AvahiResponseJob *rj;
447 if ((rj = find_scheduled_job(s, record))) {
449 if (rj->querier_valid && avahi_address_cmp(querier, &rj->querier) == 0 && /* same originator */
450 avahi_record_is_goodbye(record) == avahi_record_is_goodbye(rj->record) && /* both goodbye packets, or both not */
451 record->ttl >= rj->record->ttl/2) { /* sensible TTL */
453 /* A matching entry was found, so let's drop it */
454 /* avahi_log_debug("Known answer suppression active!"); */
459 if ((rj = find_suppressed_job(s, record, querier))) {
461 /* Let's update the old entry */
462 avahi_record_unref(rj->record);
463 rj->record = avahi_record_ref(record);
467 /* Create a new entry */
468 rj = job_new(s, record, AVAHI_SUPPRESSED);
469 rj->querier_valid = TRUE;
470 rj->querier = *querier;
473 g_get_current_time(&rj->delivery);
474 job_set_elapse_time(s, rj, AVAHI_RESPONSE_SUPPRESS_MSEC, 0);
477 void avahi_response_scheduler_force(AvahiResponseScheduler *s) {
480 /* Send all scheduled responses immediately */
482 send_response_packet(s, s->jobs);