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 500
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 /* avahi_log_debug("history age: %u", (unsigned) (avahi_age(&rj->delivery)/1000)); */
289 if (avahi_age(&rj->delivery)/1000 > AVAHI_RESPONSE_HISTORY_MSEC) {
290 /* it is outdated, so let's remove it */
302 static AvahiResponseJob* find_suppressed_job(AvahiResponseScheduler *s, AvahiRecord *record, const AvahiAddress *querier) {
303 AvahiResponseJob *rj;
309 for (rj = s->suppressed; rj; rj = rj->jobs_next) {
310 g_assert(rj->state == AVAHI_SUPPRESSED);
311 g_assert(rj->querier_valid);
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 */
317 if (avahi_age(&rj->delivery) > AVAHI_RESPONSE_SUPPRESS_MSEC*1000) {
318 /* it is outdated, so let's remove it */
330 gboolean avahi_response_scheduler_post(AvahiResponseScheduler *s, AvahiRecord *record, gboolean flush_cache, const AvahiAddress *querier, gboolean immediately) {
331 AvahiResponseJob *rj;
338 g_assert(!avahi_key_is_pattern(record->key));
340 /* t = avahi_record_to_string(record); */
341 /* avahi_log_debug("post %i %s", immediately, t); */
344 /* Check whether this response is suppressed */
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) {
350 /* avahi_log_debug("Response suppressed by known answer suppression."); */
354 /* Check if we already sent this response recently */
355 if ((rj = find_history_job(s, record))) {
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)"); */
368 avahi_elapse_time(&tv, immediately ? 0 : AVAHI_RESPONSE_DEFER_MSEC, immediately ? 0 : AVAHI_RESPONSE_JITTER_MSEC);
370 if ((rj = find_scheduled_job(s, record))) {
371 /* avahi_log_debug("Response suppressed by local duplicate suppression (scheduled)"); */
373 /* Update a little ... */
375 /* Update the time if the new is prior to the old */
376 if (avahi_timeval_compare(&tv, &rj->delivery) < 0) {
378 avahi_time_event_queue_update(s->time_event_queue, rj->time_event, &rj->delivery);
381 /* Update the flush cache bit */
383 rj->flush_cache = TRUE;
385 /* Update the querier field */
386 if (!querier || (rj->querier_valid && avahi_address_cmp(querier, &rj->querier) != 0))
387 rj->querier_valid = FALSE;
389 /* Update record data (just for the TTL) */
390 avahi_record_unref(rj->record);
391 rj->record = avahi_record_ref(record);
395 /* avahi_log_debug("Accepted new response job."); */
397 /* Create a new job and schedule it */
398 rj = job_new(s, record, AVAHI_SCHEDULED);
400 rj->time_event = avahi_time_event_queue_add(s->time_event_queue, &rj->delivery, elapse_callback, rj);
401 rj->flush_cache = flush_cache;
403 if ((rj->querier_valid = !!querier))
404 rj->querier = *querier;
410 void avahi_response_scheduler_incoming(AvahiResponseScheduler *s, AvahiRecord *record, gboolean flush_cache) {
411 AvahiResponseJob *rj;
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". */
418 if ((rj = find_scheduled_job(s, record))) {
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 */
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);
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);
437 /* Found no existing history job, so let's create a new one */
438 rj = job_new(s, record, AVAHI_DONE);
440 rj->flush_cache = flush_cache;
441 rj->querier_valid = FALSE;
443 g_get_current_time(&rj->delivery);
444 job_set_elapse_time(s, rj, AVAHI_RESPONSE_HISTORY_MSEC, 0);
447 void avahi_response_scheduler_suppress(AvahiResponseScheduler *s, AvahiRecord *record, const AvahiAddress *querier) {
448 AvahiResponseJob *rj;
454 if ((rj = find_scheduled_job(s, record))) {
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 */
460 /* A matching entry was found, so let's drop it */
461 /* avahi_log_debug("Known answer suppression active!"); */
466 if ((rj = find_suppressed_job(s, record, querier))) {
468 /* Let's update the old entry */
469 avahi_record_unref(rj->record);
470 rj->record = avahi_record_ref(record);
474 /* Create a new entry */
475 rj = job_new(s, record, AVAHI_SUPPRESSED);
476 rj->querier_valid = TRUE;
477 rj->querier = *querier;
480 g_get_current_time(&rj->delivery);
481 job_set_elapse_time(s, rj, AVAHI_RESPONSE_SUPPRESS_MSEC, 0);
484 void avahi_response_scheduler_force(AvahiResponseScheduler *s) {
487 /* Send all scheduled responses immediately */
489 send_response_packet(s, s->jobs);