2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <avahi/domain.h>
27 #include <avahi/timeval.h>
28 #include <avahi/malloc.h>
30 #include "probe-sched.h"
34 #define AVAHI_PROBE_HISTORY_MSEC 150
35 #define AVAHI_PROBE_DEFER_MSEC 50
37 typedef struct AvahiProbeJob AvahiProbeJob;
39 struct AvahiProbeJob {
40 AvahiProbeScheduler *scheduler;
41 AvahiTimeEvent *time_event;
43 int chosen; /* Use for packet assembling */
45 struct timeval delivery;
49 AVAHI_LLIST_FIELDS(AvahiProbeJob, jobs);
52 struct AvahiProbeScheduler {
53 AvahiInterface *interface;
54 AvahiTimeEventQueue *time_event_queue;
56 AVAHI_LLIST_HEAD(AvahiProbeJob, jobs);
57 AVAHI_LLIST_HEAD(AvahiProbeJob, history);
60 static AvahiProbeJob* job_new(AvahiProbeScheduler *s, AvahiRecord *record, int done) {
66 if (!(pj = avahi_new(AvahiProbeJob, 1))) {
67 avahi_log_error(__FILE__": Out of memory");
68 return NULL; /* OOM */
72 pj->record = avahi_record_ref(record);
73 pj->time_event = NULL;
76 if ((pj->done = done))
77 AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->history, pj);
79 AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->jobs, pj);
84 static void job_free(AvahiProbeScheduler *s, AvahiProbeJob *pj) {
88 avahi_time_event_free(pj->time_event);
91 AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->history, pj);
93 AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj);
95 avahi_record_unref(pj->record);
99 static void elapse_callback(AvahiTimeEvent *e, void* data);
101 static void job_set_elapse_time(AvahiProbeScheduler *s, AvahiProbeJob *pj, unsigned msec, unsigned jitter) {
107 avahi_elapse_time(&tv, msec, jitter);
110 avahi_time_event_update(pj->time_event, &tv);
112 pj->time_event = avahi_time_event_new(s->time_event_queue, &tv, elapse_callback, pj);
115 static void job_mark_done(AvahiProbeScheduler *s, AvahiProbeJob *pj) {
121 AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj);
122 AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->history, pj);
126 job_set_elapse_time(s, pj, AVAHI_PROBE_HISTORY_MSEC, 0);
127 gettimeofday(&pj->delivery, NULL);
130 AvahiProbeScheduler *avahi_probe_scheduler_new(AvahiInterface *i) {
131 AvahiProbeScheduler *s;
135 if (!(s = avahi_new(AvahiProbeScheduler, 1))) {
136 avahi_log_error(__FILE__": Out of memory");
141 s->time_event_queue = i->monitor->server->time_event_queue;
143 AVAHI_LLIST_HEAD_INIT(AvahiProbeJob, s->jobs);
144 AVAHI_LLIST_HEAD_INIT(AvahiProbeJob, s->history);
149 void avahi_probe_scheduler_free(AvahiProbeScheduler *s) {
152 avahi_probe_scheduler_clear(s);
156 void avahi_probe_scheduler_clear(AvahiProbeScheduler *s) {
160 job_free(s, s->jobs);
162 job_free(s, s->history);
165 static int packet_add_probe_query(AvahiProbeScheduler *s, AvahiDnsPacket *p, AvahiProbeJob *pj) {
176 /* Estimate the size for this record */
178 avahi_key_get_estimate_size(pj->record->key) +
179 avahi_record_get_estimate_size(pj->record);
182 if (size > avahi_dns_packet_space(p))
185 /* Create the probe query */
186 if (!(k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY)))
189 b = !!avahi_dns_packet_append_key(p, k, 0);
192 /* Mark this job for addition to the packet */
195 /* Scan for more jobs whith matching key pattern */
196 for (pj = s->jobs; pj; pj = pj->jobs_next) {
200 /* Does the record match the probe? */
201 if (k->clazz != pj->record->key->clazz || !avahi_domain_equal(k->name, pj->record->key->name))
204 /* This job wouldn't fit in */
205 if (avahi_record_get_estimate_size(pj->record) > avahi_dns_packet_space(p))
208 /* Mark this job for addition to the packet */
217 static void elapse_callback(AVAHI_GCC_UNUSED AvahiTimeEvent *e, void* data) {
218 AvahiProbeJob *pj = data, *next;
219 AvahiProbeScheduler *s;
227 /* Lets remove it from the history */
232 if (!(p = avahi_dns_packet_new_query(s->interface->hardware->mtu)))
236 /* Add the import probe */
237 if (!packet_add_probe_query(s, p, pj)) {
242 avahi_dns_packet_free(p);
244 /* The probe didn't fit in the package, so let's allocate a larger one */
247 avahi_key_get_estimate_size(pj->record->key) +
248 avahi_record_get_estimate_size(pj->record) +
249 AVAHI_DNS_PACKET_HEADER_SIZE;
251 if (!(p = avahi_dns_packet_new_query(size + AVAHI_DNS_PACKET_EXTRA_SIZE)))
254 if (!(k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY))) {
255 avahi_dns_packet_free(p);
259 b = avahi_dns_packet_append_key(p, k, 0) && avahi_dns_packet_append_record(p, pj->record, 0, 0);
263 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_NSCOUNT, 1);
264 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_QDCOUNT, 1);
265 avahi_interface_send_packet(s->interface, p);
267 avahi_log_warn("Probe record too large, cannot send");
269 avahi_dns_packet_free(p);
270 job_mark_done(s, pj);
275 /* Try to fill up packet with more probes, if available */
276 for (pj = s->jobs; pj; pj = pj->jobs_next) {
281 if (!packet_add_probe_query(s, p, pj))
287 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_QDCOUNT, n);
291 /* Now add the chosen records to the authorative section */
292 for (pj = s->jobs; pj; pj = next) {
294 next = pj->jobs_next;
299 if (!avahi_dns_packet_append_record(p, pj->record, 0, 0)) {
300 /* avahi_log_warn("Bad probe size estimate!"); */
302 /* Unmark all following jobs */
303 for (; pj; pj = pj->jobs_next)
309 job_mark_done(s, pj);
314 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_NSCOUNT, n);
317 avahi_interface_send_packet(s->interface, p);
318 avahi_dns_packet_free(p);
321 static AvahiProbeJob* find_scheduled_job(AvahiProbeScheduler *s, AvahiRecord *record) {
327 for (pj = s->jobs; pj; pj = pj->jobs_next) {
330 if (avahi_record_equal_no_ttl(pj->record, record))
337 static AvahiProbeJob* find_history_job(AvahiProbeScheduler *s, AvahiRecord *record) {
343 for (pj = s->history; pj; pj = pj->jobs_next) {
346 if (avahi_record_equal_no_ttl(pj->record, record)) {
347 /* Check whether this entry is outdated */
349 if (avahi_age(&pj->delivery) > AVAHI_PROBE_HISTORY_MSEC*1000) {
350 /* it is outdated, so let's remove it */
362 int avahi_probe_scheduler_post(AvahiProbeScheduler *s, AvahiRecord *record, int immediately) {
368 assert(!avahi_key_is_pattern(record->key));
370 if ((pj = find_history_job(s, record)))
373 avahi_elapse_time(&tv, immediately ? 0 : AVAHI_PROBE_DEFER_MSEC, 0);
375 if ((pj = find_scheduled_job(s, record))) {
377 if (avahi_timeval_compare(&tv, &pj->delivery) < 0) {
378 /* If the new entry should be scheduled earlier, update the old entry */
380 avahi_time_event_update(pj->time_event, &pj->delivery);
385 /* Create a new job and schedule it */
386 if (!(pj = job_new(s, record, 0)))
390 pj->time_event = avahi_time_event_new(s->time_event_queue, &pj->delivery, elapse_callback, pj);
393 /* avahi_log_debug("Accepted new probe job."); */