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 <avahi-common/timeval.h>
27 #include <avahi-common/malloc.h>
29 #include "response-sched.h"
32 #define AVAHI_RESPONSE_HISTORY_MSEC 500
33 #define AVAHI_RESPONSE_DEFER_MSEC 20
34 #define AVAHI_RESPONSE_JITTER_MSEC 100
35 #define AVAHI_RESPONSE_SUPPRESS_MSEC 700
37 typedef struct AvahiResponseJob AvahiResponseJob;
43 } AvahiResponseJobState;
45 struct AvahiResponseJob {
46 AvahiResponseScheduler *scheduler;
47 AvahiTimeEvent *time_event;
49 AvahiResponseJobState state;
50 struct timeval delivery;
57 AVAHI_LLIST_FIELDS(AvahiResponseJob, jobs);
60 struct AvahiResponseScheduler {
61 AvahiInterface *interface;
62 AvahiTimeEventQueue *time_event_queue;
64 AVAHI_LLIST_HEAD(AvahiResponseJob, jobs);
65 AVAHI_LLIST_HEAD(AvahiResponseJob, history);
66 AVAHI_LLIST_HEAD(AvahiResponseJob, suppressed);
69 static AvahiResponseJob* job_new(AvahiResponseScheduler *s, AvahiRecord *record, AvahiResponseJobState state) {
75 if (!(rj = avahi_new(AvahiResponseJob, 1))) {
76 avahi_log_error(__FILE__": Out of memory");
81 rj->record = avahi_record_ref(record);
82 rj->time_event = NULL;
84 rj->querier_valid = 0;
86 if ((rj->state = state) == AVAHI_SCHEDULED)
87 AVAHI_LLIST_PREPEND(AvahiResponseJob, jobs, s->jobs, rj);
88 else if (rj->state == AVAHI_DONE)
89 AVAHI_LLIST_PREPEND(AvahiResponseJob, jobs, s->history, rj);
90 else /* rj->state == AVAHI_SUPPRESSED */
91 AVAHI_LLIST_PREPEND(AvahiResponseJob, jobs, s->suppressed, rj);
96 static void job_free(AvahiResponseScheduler *s, AvahiResponseJob *rj) {
101 avahi_time_event_free(rj->time_event);
103 if (rj->state == AVAHI_SCHEDULED)
104 AVAHI_LLIST_REMOVE(AvahiResponseJob, jobs, s->jobs, rj);
105 else if (rj->state == AVAHI_DONE)
106 AVAHI_LLIST_REMOVE(AvahiResponseJob, jobs, s->history, rj);
107 else /* rj->state == AVAHI_SUPPRESSED */
108 AVAHI_LLIST_REMOVE(AvahiResponseJob, jobs, s->suppressed, rj);
110 avahi_record_unref(rj->record);
114 static void elapse_callback(AvahiTimeEvent *e, void* data);
116 static void job_set_elapse_time(AvahiResponseScheduler *s, AvahiResponseJob *rj, unsigned msec, unsigned jitter) {
122 avahi_elapse_time(&tv, msec, jitter);
125 avahi_time_event_update(rj->time_event, &tv);
127 rj->time_event = avahi_time_event_new(s->time_event_queue, &tv, elapse_callback, rj);
130 static void job_mark_done(AvahiResponseScheduler *s, AvahiResponseJob *rj) {
134 assert(rj->state == AVAHI_SCHEDULED);
136 AVAHI_LLIST_REMOVE(AvahiResponseJob, jobs, s->jobs, rj);
137 AVAHI_LLIST_PREPEND(AvahiResponseJob, jobs, s->history, rj);
139 rj->state = AVAHI_DONE;
141 job_set_elapse_time(s, rj, AVAHI_RESPONSE_HISTORY_MSEC, 0);
143 gettimeofday(&rj->delivery, NULL);
146 AvahiResponseScheduler *avahi_response_scheduler_new(AvahiInterface *i) {
147 AvahiResponseScheduler *s;
150 if (!(s = avahi_new(AvahiResponseScheduler, 1))) {
151 avahi_log_error(__FILE__": Out of memory");
156 s->time_event_queue = i->monitor->server->time_event_queue;
158 AVAHI_LLIST_HEAD_INIT(AvahiResponseJob, s->jobs);
159 AVAHI_LLIST_HEAD_INIT(AvahiResponseJob, s->history);
160 AVAHI_LLIST_HEAD_INIT(AvahiResponseJob, s->suppressed);
165 void avahi_response_scheduler_free(AvahiResponseScheduler *s) {
168 avahi_response_scheduler_clear(s);
172 void avahi_response_scheduler_clear(AvahiResponseScheduler *s) {
176 job_free(s, s->jobs);
178 job_free(s, s->history);
179 while (s->suppressed)
180 job_free(s, s->suppressed);
183 static void enumerate_aux_records_callback(AvahiServer *s, AvahiRecord *r, int flush_cache, void* userdata) {
184 AvahiResponseJob *rj = userdata;
189 avahi_response_scheduler_post(rj->scheduler, r, flush_cache, rj->querier_valid ? &rj->querier : NULL, 0);
192 static int packet_add_response_job(AvahiResponseScheduler *s, AvahiDnsPacket *p, AvahiResponseJob *rj) {
197 /* Try to add this record to the packet */
198 if (!avahi_dns_packet_append_record(p, rj->record, rj->flush_cache, 0))
201 /* Ok, this record will definitely be sent, so schedule the
202 * auxilliary packets, too */
203 avahi_server_enumerate_aux_records(s->interface->monitor->server, s->interface, rj->record, enumerate_aux_records_callback, rj);
204 job_mark_done(s, rj);
209 static void send_response_packet(AvahiResponseScheduler *s, AvahiResponseJob *rj) {
216 if (!(p = avahi_dns_packet_new_response(s->interface->hardware->mtu, 1)))
220 /* Put it in the packet. */
221 if (packet_add_response_job(s, p, rj)) {
223 /* Try to fill up packet with more responses, if available */
226 if (!packet_add_response_job(s, p, s->jobs))
235 avahi_dns_packet_free(p);
237 /* OK, the packet was too small, so create one that fits */
238 size = avahi_record_get_estimate_size(rj->record) + AVAHI_DNS_PACKET_HEADER_SIZE;
240 if (size > AVAHI_DNS_PACKET_MAX_SIZE)
241 size = AVAHI_DNS_PACKET_MAX_SIZE;
243 if (!(p = avahi_dns_packet_new_response(size, 1)))
246 if (!packet_add_response_job(s, p, rj)) {
247 avahi_dns_packet_free(p);
249 avahi_log_warn("Record too large, cannot send");
250 job_mark_done(s, rj);
255 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ANCOUNT, n);
256 avahi_interface_send_packet(s->interface, p);
257 avahi_dns_packet_free(p);
260 static void elapse_callback(AvahiTimeEvent *e, void* data) {
261 AvahiResponseJob *rj = data;
265 if (rj->state == AVAHI_DONE || rj->state == AVAHI_SUPPRESSED)
266 job_free(rj->scheduler, rj); /* Lets drop this entry */
268 send_response_packet(rj->scheduler, rj);
271 static AvahiResponseJob* find_scheduled_job(AvahiResponseScheduler *s, AvahiRecord *record) {
272 AvahiResponseJob *rj;
277 for (rj = s->jobs; rj; rj = rj->jobs_next) {
278 assert(rj->state == AVAHI_SCHEDULED);
280 if (avahi_record_equal_no_ttl(rj->record, record))
287 static AvahiResponseJob* find_history_job(AvahiResponseScheduler *s, AvahiRecord *record) {
288 AvahiResponseJob *rj;
293 for (rj = s->history; rj; rj = rj->jobs_next) {
294 assert(rj->state == AVAHI_DONE);
296 if (avahi_record_equal_no_ttl(rj->record, record)) {
297 /* Check whether this entry is outdated */
299 /* avahi_log_debug("history age: %u", (unsigned) (avahi_age(&rj->delivery)/1000)); */
301 if (avahi_age(&rj->delivery)/1000 > AVAHI_RESPONSE_HISTORY_MSEC) {
302 /* it is outdated, so let's remove it */
314 static AvahiResponseJob* find_suppressed_job(AvahiResponseScheduler *s, AvahiRecord *record, const AvahiAddress *querier) {
315 AvahiResponseJob *rj;
321 for (rj = s->suppressed; rj; rj = rj->jobs_next) {
322 assert(rj->state == AVAHI_SUPPRESSED);
323 assert(rj->querier_valid);
325 if (avahi_record_equal_no_ttl(rj->record, record) &&
326 avahi_address_cmp(&rj->querier, querier) == 0) {
327 /* Check whether this entry is outdated */
329 if (avahi_age(&rj->delivery) > AVAHI_RESPONSE_SUPPRESS_MSEC*1000) {
330 /* it is outdated, so let's remove it */
342 int avahi_response_scheduler_post(AvahiResponseScheduler *s, AvahiRecord *record, int flush_cache, const AvahiAddress *querier, int immediately) {
343 AvahiResponseJob *rj;
350 assert(!avahi_key_is_pattern(record->key));
352 /* t = avahi_record_to_string(record); */
353 /* avahi_log_debug("post %i %s", immediately, t); */
356 /* Check whether this response is suppressed */
358 (rj = find_suppressed_job(s, record, querier)) &&
359 avahi_record_is_goodbye(record) == avahi_record_is_goodbye(rj->record) &&
360 rj->record->ttl >= record->ttl/2) {
362 /* avahi_log_debug("Response suppressed by known answer suppression."); */
366 /* Check if we already sent this response recently */
367 if ((rj = find_history_job(s, record))) {
369 if (avahi_record_is_goodbye(record) == avahi_record_is_goodbye(rj->record) &&
370 rj->record->ttl >= record->ttl/2 &&
371 (rj->flush_cache || !flush_cache)) {
372 /* avahi_log_debug("Response suppressed by local duplicate suppression (history)"); */
380 avahi_elapse_time(&tv, immediately ? 0 : AVAHI_RESPONSE_DEFER_MSEC, immediately ? 0 : AVAHI_RESPONSE_JITTER_MSEC);
382 if ((rj = find_scheduled_job(s, record))) {
383 /* avahi_log_debug("Response suppressed by local duplicate suppression (scheduled)"); */
385 /* Update a little ... */
387 /* Update the time if the new is prior to the old */
388 if (avahi_timeval_compare(&tv, &rj->delivery) < 0) {
390 avahi_time_event_update(rj->time_event, &rj->delivery);
393 /* Update the flush cache bit */
397 /* Update the querier field */
398 if (!querier || (rj->querier_valid && avahi_address_cmp(querier, &rj->querier) != 0))
399 rj->querier_valid = 0;
401 /* Update record data (just for the TTL) */
402 avahi_record_unref(rj->record);
403 rj->record = avahi_record_ref(record);
407 /* avahi_log_debug("Accepted new response job."); */
409 /* Create a new job and schedule it */
410 if (!(rj = job_new(s, record, AVAHI_SCHEDULED)))
414 rj->time_event = avahi_time_event_new(s->time_event_queue, &rj->delivery, elapse_callback, rj);
415 rj->flush_cache = flush_cache;
417 if ((rj->querier_valid = !!querier))
418 rj->querier = *querier;
424 void avahi_response_scheduler_incoming(AvahiResponseScheduler *s, AvahiRecord *record, int flush_cache) {
425 AvahiResponseJob *rj;
428 /* This function is called whenever an incoming response was
429 * receieved. We drop scheduled responses which match here. The
430 * keyword is "DUPLICATE ANSWER SUPPRESION". */
432 if ((rj = find_scheduled_job(s, record))) {
434 if ((!rj->flush_cache || flush_cache) && /* flush cache bit was set correctly */
435 avahi_record_is_goodbye(record) == avahi_record_is_goodbye(rj->record) && /* both goodbye packets, or both not */
436 record->ttl >= rj->record->ttl/2) { /* sensible TTL */
438 /* A matching entry was found, so let's mark it done */
439 /* avahi_log_debug("Response suppressed by distributed duplicate suppression"); */
440 job_mark_done(s, rj);
446 if ((rj = find_history_job(s, record))) {
447 /* Found a history job, let's update it */
448 avahi_record_unref(rj->record);
449 rj->record = avahi_record_ref(record);
451 /* Found no existing history job, so let's create a new one */
452 if (!(rj = job_new(s, record, AVAHI_DONE)))
455 rj->flush_cache = flush_cache;
456 rj->querier_valid = 0;
458 gettimeofday(&rj->delivery, NULL);
459 job_set_elapse_time(s, rj, AVAHI_RESPONSE_HISTORY_MSEC, 0);
462 void avahi_response_scheduler_suppress(AvahiResponseScheduler *s, AvahiRecord *record, const AvahiAddress *querier) {
463 AvahiResponseJob *rj;
469 if ((rj = find_scheduled_job(s, record))) {
471 if (rj->querier_valid && avahi_address_cmp(querier, &rj->querier) == 0 && /* same originator */
472 avahi_record_is_goodbye(record) == avahi_record_is_goodbye(rj->record) && /* both goodbye packets, or both not */
473 record->ttl >= rj->record->ttl/2) { /* sensible TTL */
475 /* A matching entry was found, so let's drop it */
476 /* avahi_log_debug("Known answer suppression active!"); */
481 if ((rj = find_suppressed_job(s, record, querier))) {
483 /* Let's update the old entry */
484 avahi_record_unref(rj->record);
485 rj->record = avahi_record_ref(record);
489 /* Create a new entry */
490 if (!(rj = job_new(s, record, AVAHI_SUPPRESSED)))
492 rj->querier_valid = 1;
493 rj->querier = *querier;
496 gettimeofday(&rj->delivery, NULL);
497 job_set_elapse_time(s, rj, AVAHI_RESPONSE_SUPPRESS_MSEC, 0);
500 void avahi_response_scheduler_force(AvahiResponseScheduler *s) {
503 /* Send all scheduled responses immediately */
505 send_response_packet(s, s->jobs);