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"
33 /* Local packets are supressed this long after sending them */
34 #define AVAHI_RESPONSE_HISTORY_MSEC 500
36 /* Local packets are deferred this long before sending them */
37 #define AVAHI_RESPONSE_DEFER_MSEC 20
39 /* Additional jitter for deferred packets */
40 #define AVAHI_RESPONSE_JITTER_MSEC 100
42 /* Remote packets can suppress local traffic as long as this value */
43 #define AVAHI_RESPONSE_SUPPRESS_MSEC 700
45 typedef struct AvahiResponseJob AvahiResponseJob;
51 } AvahiResponseJobState;
53 struct AvahiResponseJob {
54 AvahiResponseScheduler *scheduler;
55 AvahiTimeEvent *time_event;
57 AvahiResponseJobState state;
58 struct timeval delivery;
65 AVAHI_LLIST_FIELDS(AvahiResponseJob, jobs);
68 struct AvahiResponseScheduler {
69 AvahiInterface *interface;
70 AvahiTimeEventQueue *time_event_queue;
72 AVAHI_LLIST_HEAD(AvahiResponseJob, jobs);
73 AVAHI_LLIST_HEAD(AvahiResponseJob, history);
74 AVAHI_LLIST_HEAD(AvahiResponseJob, suppressed);
77 static AvahiResponseJob* job_new(AvahiResponseScheduler *s, AvahiRecord *record, AvahiResponseJobState state) {
83 if (!(rj = avahi_new(AvahiResponseJob, 1))) {
84 avahi_log_error(__FILE__": Out of memory");
89 rj->record = avahi_record_ref(record);
90 rj->time_event = NULL;
92 rj->querier_valid = 0;
94 if ((rj->state = state) == AVAHI_SCHEDULED)
95 AVAHI_LLIST_PREPEND(AvahiResponseJob, jobs, s->jobs, rj);
96 else if (rj->state == AVAHI_DONE)
97 AVAHI_LLIST_PREPEND(AvahiResponseJob, jobs, s->history, rj);
98 else /* rj->state == AVAHI_SUPPRESSED */
99 AVAHI_LLIST_PREPEND(AvahiResponseJob, jobs, s->suppressed, rj);
104 static void job_free(AvahiResponseScheduler *s, AvahiResponseJob *rj) {
109 avahi_time_event_free(rj->time_event);
111 if (rj->state == AVAHI_SCHEDULED)
112 AVAHI_LLIST_REMOVE(AvahiResponseJob, jobs, s->jobs, rj);
113 else if (rj->state == AVAHI_DONE)
114 AVAHI_LLIST_REMOVE(AvahiResponseJob, jobs, s->history, rj);
115 else /* rj->state == AVAHI_SUPPRESSED */
116 AVAHI_LLIST_REMOVE(AvahiResponseJob, jobs, s->suppressed, rj);
118 avahi_record_unref(rj->record);
122 static void elapse_callback(AvahiTimeEvent *e, void* data);
124 static void job_set_elapse_time(AvahiResponseScheduler *s, AvahiResponseJob *rj, unsigned msec, unsigned jitter) {
130 avahi_elapse_time(&tv, msec, jitter);
133 avahi_time_event_update(rj->time_event, &tv);
135 rj->time_event = avahi_time_event_new(s->time_event_queue, &tv, elapse_callback, rj);
138 static void job_mark_done(AvahiResponseScheduler *s, AvahiResponseJob *rj) {
142 assert(rj->state == AVAHI_SCHEDULED);
144 AVAHI_LLIST_REMOVE(AvahiResponseJob, jobs, s->jobs, rj);
145 AVAHI_LLIST_PREPEND(AvahiResponseJob, jobs, s->history, rj);
147 rj->state = AVAHI_DONE;
149 job_set_elapse_time(s, rj, AVAHI_RESPONSE_HISTORY_MSEC, 0);
151 gettimeofday(&rj->delivery, NULL);
154 AvahiResponseScheduler *avahi_response_scheduler_new(AvahiInterface *i) {
155 AvahiResponseScheduler *s;
158 if (!(s = avahi_new(AvahiResponseScheduler, 1))) {
159 avahi_log_error(__FILE__": Out of memory");
164 s->time_event_queue = i->monitor->server->time_event_queue;
166 AVAHI_LLIST_HEAD_INIT(AvahiResponseJob, s->jobs);
167 AVAHI_LLIST_HEAD_INIT(AvahiResponseJob, s->history);
168 AVAHI_LLIST_HEAD_INIT(AvahiResponseJob, s->suppressed);
173 void avahi_response_scheduler_free(AvahiResponseScheduler *s) {
176 avahi_response_scheduler_clear(s);
180 void avahi_response_scheduler_clear(AvahiResponseScheduler *s) {
184 job_free(s, s->jobs);
186 job_free(s, s->history);
187 while (s->suppressed)
188 job_free(s, s->suppressed);
191 static void enumerate_aux_records_callback(AVAHI_GCC_UNUSED AvahiServer *s, AvahiRecord *r, int flush_cache, void* userdata) {
192 AvahiResponseJob *rj = userdata;
197 avahi_response_scheduler_post(rj->scheduler, r, flush_cache, rj->querier_valid ? &rj->querier : NULL, 0);
200 static int packet_add_response_job(AvahiResponseScheduler *s, AvahiDnsPacket *p, AvahiResponseJob *rj) {
205 /* Try to add this record to the packet */
206 if (!avahi_dns_packet_append_record(p, rj->record, rj->flush_cache, 0))
209 /* Ok, this record will definitely be sent, so schedule the
210 * auxilliary packets, too */
211 avahi_server_enumerate_aux_records(s->interface->monitor->server, s->interface, rj->record, enumerate_aux_records_callback, rj);
212 job_mark_done(s, rj);
217 static void send_response_packet(AvahiResponseScheduler *s, AvahiResponseJob *rj) {
224 if (!(p = avahi_dns_packet_new_response(s->interface->hardware->mtu, 1)))
228 /* Put it in the packet. */
229 if (packet_add_response_job(s, p, rj)) {
231 /* Try to fill up packet with more responses, if available */
234 if (!packet_add_response_job(s, p, s->jobs))
243 avahi_dns_packet_free(p);
245 /* OK, the packet was too small, so create one that fits */
246 size = avahi_record_get_estimate_size(rj->record) + AVAHI_DNS_PACKET_HEADER_SIZE;
248 if (size > AVAHI_DNS_PACKET_SIZE_MAX)
249 size = AVAHI_DNS_PACKET_SIZE_MAX;
251 if (!(p = avahi_dns_packet_new_response(size, 1)))
254 if (!packet_add_response_job(s, p, rj)) {
255 avahi_dns_packet_free(p);
257 avahi_log_warn("Record too large, cannot send");
258 job_mark_done(s, rj);
263 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ANCOUNT, n);
264 avahi_interface_send_packet(s->interface, p);
265 avahi_dns_packet_free(p);
268 static void elapse_callback(AVAHI_GCC_UNUSED AvahiTimeEvent *e, void* data) {
269 AvahiResponseJob *rj = data;
273 if (rj->state == AVAHI_DONE || rj->state == AVAHI_SUPPRESSED)
274 job_free(rj->scheduler, rj); /* Lets drop this entry */
276 send_response_packet(rj->scheduler, rj);
279 static AvahiResponseJob* find_scheduled_job(AvahiResponseScheduler *s, AvahiRecord *record) {
280 AvahiResponseJob *rj;
285 for (rj = s->jobs; rj; rj = rj->jobs_next) {
286 assert(rj->state == AVAHI_SCHEDULED);
288 if (avahi_record_equal_no_ttl(rj->record, record))
295 static AvahiResponseJob* find_history_job(AvahiResponseScheduler *s, AvahiRecord *record) {
296 AvahiResponseJob *rj;
301 for (rj = s->history; rj; rj = rj->jobs_next) {
302 assert(rj->state == AVAHI_DONE);
304 if (avahi_record_equal_no_ttl(rj->record, record)) {
305 /* Check whether this entry is outdated */
307 /* avahi_log_debug("history age: %u", (unsigned) (avahi_age(&rj->delivery)/1000)); */
309 if (avahi_age(&rj->delivery)/1000 > AVAHI_RESPONSE_HISTORY_MSEC) {
310 /* it is outdated, so let's remove it */
322 static AvahiResponseJob* find_suppressed_job(AvahiResponseScheduler *s, AvahiRecord *record, const AvahiAddress *querier) {
323 AvahiResponseJob *rj;
329 for (rj = s->suppressed; rj; rj = rj->jobs_next) {
330 assert(rj->state == AVAHI_SUPPRESSED);
331 assert(rj->querier_valid);
333 if (avahi_record_equal_no_ttl(rj->record, record) &&
334 avahi_address_cmp(&rj->querier, querier) == 0) {
335 /* Check whether this entry is outdated */
337 if (avahi_age(&rj->delivery) > AVAHI_RESPONSE_SUPPRESS_MSEC*1000) {
338 /* it is outdated, so let's remove it */
350 int avahi_response_scheduler_post(AvahiResponseScheduler *s, AvahiRecord *record, int flush_cache, const AvahiAddress *querier, int immediately) {
351 AvahiResponseJob *rj;
358 assert(!avahi_key_is_pattern(record->key));
360 /* t = avahi_record_to_string(record); */
361 /* avahi_log_debug("post %i %s", immediately, t); */
364 /* Check whether this response is suppressed */
366 (rj = find_suppressed_job(s, record, querier)) &&
367 avahi_record_is_goodbye(record) == avahi_record_is_goodbye(rj->record) &&
368 rj->record->ttl >= record->ttl/2) {
370 /* avahi_log_debug("Response suppressed by known answer suppression."); */
374 /* Check if we already sent this response recently */
375 if ((rj = find_history_job(s, record))) {
377 if (avahi_record_is_goodbye(record) == avahi_record_is_goodbye(rj->record) &&
378 rj->record->ttl >= record->ttl/2 &&
379 (rj->flush_cache || !flush_cache)) {
380 /* avahi_log_debug("Response suppressed by local duplicate suppression (history)"); */
388 avahi_elapse_time(&tv, immediately ? 0 : AVAHI_RESPONSE_DEFER_MSEC, immediately ? 0 : AVAHI_RESPONSE_JITTER_MSEC);
390 if ((rj = find_scheduled_job(s, record))) {
391 /* avahi_log_debug("Response suppressed by local duplicate suppression (scheduled)"); */
393 /* Update a little ... */
395 /* Update the time if the new is prior to the old */
396 if (avahi_timeval_compare(&tv, &rj->delivery) < 0) {
398 avahi_time_event_update(rj->time_event, &rj->delivery);
401 /* Update the flush cache bit */
405 /* Update the querier field */
406 if (!querier || (rj->querier_valid && avahi_address_cmp(querier, &rj->querier) != 0))
407 rj->querier_valid = 0;
409 /* Update record data (just for the TTL) */
410 avahi_record_unref(rj->record);
411 rj->record = avahi_record_ref(record);
415 /* avahi_log_debug("Accepted new response job."); */
417 /* Create a new job and schedule it */
418 if (!(rj = job_new(s, record, AVAHI_SCHEDULED)))
422 rj->time_event = avahi_time_event_new(s->time_event_queue, &rj->delivery, elapse_callback, rj);
423 rj->flush_cache = flush_cache;
425 if ((rj->querier_valid = !!querier))
426 rj->querier = *querier;
432 void avahi_response_scheduler_incoming(AvahiResponseScheduler *s, AvahiRecord *record, int flush_cache) {
433 AvahiResponseJob *rj;
436 /* This function is called whenever an incoming response was
437 * receieved. We drop scheduled responses which match here. The
438 * keyword is "DUPLICATE ANSWER SUPPRESION". */
440 if ((rj = find_scheduled_job(s, record))) {
442 if ((!rj->flush_cache || flush_cache) && /* flush cache bit was set correctly */
443 avahi_record_is_goodbye(record) == avahi_record_is_goodbye(rj->record) && /* both goodbye packets, or both not */
444 record->ttl >= rj->record->ttl/2) { /* sensible TTL */
446 /* A matching entry was found, so let's mark it done */
447 /* avahi_log_debug("Response suppressed by distributed duplicate suppression"); */
448 job_mark_done(s, rj);
454 if ((rj = find_history_job(s, record))) {
455 /* Found a history job, let's update it */
456 avahi_record_unref(rj->record);
457 rj->record = avahi_record_ref(record);
459 /* Found no existing history job, so let's create a new one */
460 if (!(rj = job_new(s, record, AVAHI_DONE)))
463 rj->flush_cache = flush_cache;
464 rj->querier_valid = 0;
466 gettimeofday(&rj->delivery, NULL);
467 job_set_elapse_time(s, rj, AVAHI_RESPONSE_HISTORY_MSEC, 0);
470 void avahi_response_scheduler_suppress(AvahiResponseScheduler *s, AvahiRecord *record, const AvahiAddress *querier) {
471 AvahiResponseJob *rj;
477 if ((rj = find_scheduled_job(s, record))) {
479 if (rj->querier_valid && avahi_address_cmp(querier, &rj->querier) == 0 && /* same originator */
480 avahi_record_is_goodbye(record) == avahi_record_is_goodbye(rj->record) && /* both goodbye packets, or both not */
481 record->ttl >= rj->record->ttl/2) { /* sensible TTL */
483 /* A matching entry was found, so let's drop it */
484 /* avahi_log_debug("Known answer suppression active!"); */
489 if ((rj = find_suppressed_job(s, record, querier))) {
491 /* Let's update the old entry */
492 avahi_record_unref(rj->record);
493 rj->record = avahi_record_ref(record);
497 /* Create a new entry */
498 if (!(rj = job_new(s, record, AVAHI_SUPPRESSED)))
500 rj->querier_valid = 1;
501 rj->querier = *querier;
504 gettimeofday(&rj->delivery, NULL);
505 job_set_elapse_time(s, rj, AVAHI_RESPONSE_SUPPRESS_MSEC, 0);
508 void avahi_response_scheduler_force(AvahiResponseScheduler *s) {
511 /* Send all scheduled responses immediately */
513 send_response_packet(s, s->jobs);