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
28 #include <avahi-common/domain.h>
29 #include <avahi-common/timeval.h>
30 #include <avahi-common/malloc.h>
32 #include "probe-sched.h"
36 #define AVAHI_PROBE_HISTORY_MSEC 150
37 #define AVAHI_PROBE_DEFER_MSEC 50
39 typedef struct AvahiProbeJob AvahiProbeJob;
41 struct AvahiProbeJob {
42 AvahiProbeScheduler *scheduler;
43 AvahiTimeEvent *time_event;
45 int chosen; /* Use for packet assembling */
47 struct timeval delivery;
51 AVAHI_LLIST_FIELDS(AvahiProbeJob, jobs);
54 struct AvahiProbeScheduler {
55 AvahiInterface *interface;
56 AvahiTimeEventQueue *time_event_queue;
58 AVAHI_LLIST_HEAD(AvahiProbeJob, jobs);
59 AVAHI_LLIST_HEAD(AvahiProbeJob, history);
62 static AvahiProbeJob* job_new(AvahiProbeScheduler *s, AvahiRecord *record, int done) {
68 if (!(pj = avahi_new(AvahiProbeJob, 1))) {
69 avahi_log_error(__FILE__": Out of memory");
70 return NULL; /* OOM */
74 pj->record = avahi_record_ref(record);
75 pj->time_event = NULL;
78 if ((pj->done = done))
79 AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->history, pj);
81 AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->jobs, pj);
86 static void job_free(AvahiProbeScheduler *s, AvahiProbeJob *pj) {
90 avahi_time_event_free(pj->time_event);
93 AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->history, pj);
95 AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj);
97 avahi_record_unref(pj->record);
101 static void elapse_callback(AvahiTimeEvent *e, void* data);
103 static void job_set_elapse_time(AvahiProbeScheduler *s, AvahiProbeJob *pj, unsigned msec, unsigned jitter) {
109 avahi_elapse_time(&tv, msec, jitter);
112 avahi_time_event_update(pj->time_event, &tv);
114 pj->time_event = avahi_time_event_new(s->time_event_queue, &tv, elapse_callback, pj);
117 static void job_mark_done(AvahiProbeScheduler *s, AvahiProbeJob *pj) {
123 AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj);
124 AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->history, pj);
128 job_set_elapse_time(s, pj, AVAHI_PROBE_HISTORY_MSEC, 0);
129 gettimeofday(&pj->delivery, NULL);
132 AvahiProbeScheduler *avahi_probe_scheduler_new(AvahiInterface *i) {
133 AvahiProbeScheduler *s;
137 if (!(s = avahi_new(AvahiProbeScheduler, 1))) {
138 avahi_log_error(__FILE__": Out of memory");
143 s->time_event_queue = i->monitor->server->time_event_queue;
145 AVAHI_LLIST_HEAD_INIT(AvahiProbeJob, s->jobs);
146 AVAHI_LLIST_HEAD_INIT(AvahiProbeJob, s->history);
151 void avahi_probe_scheduler_free(AvahiProbeScheduler *s) {
154 avahi_probe_scheduler_clear(s);
158 void avahi_probe_scheduler_clear(AvahiProbeScheduler *s) {
162 job_free(s, s->jobs);
164 job_free(s, s->history);
167 static int packet_add_probe_query(AvahiProbeScheduler *s, AvahiDnsPacket *p, AvahiProbeJob *pj) {
178 /* Estimate the size for this record */
180 avahi_key_get_estimate_size(pj->record->key) +
181 avahi_record_get_estimate_size(pj->record);
184 if (size > avahi_dns_packet_space(p))
187 /* Create the probe query */
188 if (!(k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY)))
191 b = !!avahi_dns_packet_append_key(p, k, 0);
194 /* Mark this job for addition to the packet */
197 /* Scan for more jobs whith matching key pattern */
198 for (pj = s->jobs; pj; pj = pj->jobs_next) {
202 /* Does the record match the probe? */
203 if (k->clazz != pj->record->key->clazz || !avahi_domain_equal(k->name, pj->record->key->name))
206 /* This job wouldn't fit in */
207 if (avahi_record_get_estimate_size(pj->record) > avahi_dns_packet_space(p))
210 /* Mark this job for addition to the packet */
219 static void elapse_callback(AVAHI_GCC_UNUSED AvahiTimeEvent *e, void* data) {
220 AvahiProbeJob *pj = data, *next;
221 AvahiProbeScheduler *s;
229 /* Lets remove it from the history */
234 if (!(p = avahi_dns_packet_new_query(s->interface->hardware->mtu)))
238 /* Add the import probe */
239 if (!packet_add_probe_query(s, p, pj)) {
244 avahi_dns_packet_free(p);
246 /* The probe didn't fit in the package, so let's allocate a larger one */
249 avahi_key_get_estimate_size(pj->record->key) +
250 avahi_record_get_estimate_size(pj->record) +
251 AVAHI_DNS_PACKET_HEADER_SIZE;
253 if (size > AVAHI_DNS_PACKET_SIZE_MAX)
254 size = AVAHI_DNS_PACKET_SIZE_MAX;
256 if (!(p = avahi_dns_packet_new_query(size)))
259 if (!(k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY))) {
260 avahi_dns_packet_free(p);
264 b = avahi_dns_packet_append_key(p, k, 0) && avahi_dns_packet_append_record(p, pj->record, 0, 0);
268 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_NSCOUNT, 1);
269 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_QDCOUNT, 1);
270 avahi_interface_send_packet(s->interface, p);
272 avahi_log_warn("Probe record too large, cannot send");
274 avahi_dns_packet_free(p);
275 job_mark_done(s, pj);
280 /* Try to fill up packet with more probes, if available */
281 for (pj = s->jobs; pj; pj = pj->jobs_next) {
286 if (!packet_add_probe_query(s, p, pj))
292 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_QDCOUNT, n);
296 /* Now add the chosen records to the authorative section */
297 for (pj = s->jobs; pj; pj = next) {
299 next = pj->jobs_next;
304 if (!avahi_dns_packet_append_record(p, pj->record, 0, 0)) {
305 avahi_log_warn("Bad probe size estimate!");
307 /* Unmark all following jobs */
308 for (; pj; pj = pj->jobs_next)
314 job_mark_done(s, pj);
319 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_NSCOUNT, n);
322 avahi_interface_send_packet(s->interface, p);
323 avahi_dns_packet_free(p);
326 static AvahiProbeJob* find_scheduled_job(AvahiProbeScheduler *s, AvahiRecord *record) {
332 for (pj = s->jobs; pj; pj = pj->jobs_next) {
335 if (avahi_record_equal_no_ttl(pj->record, record))
342 static AvahiProbeJob* find_history_job(AvahiProbeScheduler *s, AvahiRecord *record) {
348 for (pj = s->history; pj; pj = pj->jobs_next) {
351 if (avahi_record_equal_no_ttl(pj->record, record)) {
352 /* Check whether this entry is outdated */
354 if (avahi_age(&pj->delivery) > AVAHI_PROBE_HISTORY_MSEC*1000) {
355 /* it is outdated, so let's remove it */
367 int avahi_probe_scheduler_post(AvahiProbeScheduler *s, AvahiRecord *record, int immediately) {
373 assert(!avahi_key_is_pattern(record->key));
375 if ((pj = find_history_job(s, record)))
378 avahi_elapse_time(&tv, immediately ? 0 : AVAHI_PROBE_DEFER_MSEC, 0);
380 if ((pj = find_scheduled_job(s, record))) {
382 if (avahi_timeval_compare(&tv, &pj->delivery) < 0) {
383 /* If the new entry should be scheduled earlier, update the old entry */
385 avahi_time_event_update(pj->time_event, &pj->delivery);
390 /* Create a new job and schedule it */
391 if (!(pj = job_new(s, record, 0)))
395 pj->time_event = avahi_time_event_new(s->time_event_queue, &pj->delivery, elapse_callback, pj);
398 /* avahi_log_debug("Accepted new probe job."); */