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/domain.h>
27 #include <avahi-common/timeval.h>
29 #include "probe-sched.h"
32 #define AVAHI_PROBE_HISTORY_MSEC 150
33 #define AVAHI_PROBE_DEFER_MSEC 50
35 typedef struct AvahiProbeJob AvahiProbeJob;
37 struct AvahiProbeJob {
38 AvahiProbeScheduler *scheduler;
39 AvahiTimeEvent *time_event;
41 gboolean chosen; /* Use for packet assembling */
43 struct timeval delivery;
47 AVAHI_LLIST_FIELDS(AvahiProbeJob, jobs);
50 struct AvahiProbeScheduler {
51 AvahiInterface *interface;
52 AvahiTimeEventQueue *time_event_queue;
54 AVAHI_LLIST_HEAD(AvahiProbeJob, jobs);
55 AVAHI_LLIST_HEAD(AvahiProbeJob, history);
58 static AvahiProbeJob* job_new(AvahiProbeScheduler *s, AvahiRecord *record, gboolean done) {
64 pj = g_new(AvahiProbeJob, 1);
66 pj->record = avahi_record_ref(record);
67 pj->time_event = NULL;
70 if ((pj->done = done))
71 AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->history, pj);
73 AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->jobs, pj);
78 static void job_free(AvahiProbeScheduler *s, AvahiProbeJob *pj) {
82 avahi_time_event_queue_remove(s->time_event_queue, pj->time_event);
85 AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->history, pj);
87 AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj);
89 avahi_record_unref(pj->record);
93 static void elapse_callback(AvahiTimeEvent *e, gpointer data);
95 static void job_set_elapse_time(AvahiProbeScheduler *s, AvahiProbeJob *pj, guint msec, guint jitter) {
101 avahi_elapse_time(&tv, msec, jitter);
104 avahi_time_event_queue_update(s->time_event_queue, pj->time_event, &tv);
106 pj->time_event = avahi_time_event_queue_add(s->time_event_queue, &tv, elapse_callback, pj);
109 static void job_mark_done(AvahiProbeScheduler *s, AvahiProbeJob *pj) {
115 AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj);
116 AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->history, pj);
120 job_set_elapse_time(s, pj, AVAHI_PROBE_HISTORY_MSEC, 0);
121 gettimeofday(&pj->delivery, NULL);
124 AvahiProbeScheduler *avahi_probe_scheduler_new(AvahiInterface *i) {
125 AvahiProbeScheduler *s;
129 s = g_new(AvahiProbeScheduler, 1);
131 s->time_event_queue = i->monitor->server->time_event_queue;
133 AVAHI_LLIST_HEAD_INIT(AvahiProbeJob, s->jobs);
134 AVAHI_LLIST_HEAD_INIT(AvahiProbeJob, s->history);
139 void avahi_probe_scheduler_free(AvahiProbeScheduler *s) {
142 avahi_probe_scheduler_clear(s);
146 void avahi_probe_scheduler_clear(AvahiProbeScheduler *s) {
150 job_free(s, s->jobs);
152 job_free(s, s->history);
155 static gboolean packet_add_probe_query(AvahiProbeScheduler *s, AvahiDnsPacket *p, AvahiProbeJob *pj) {
164 g_assert(!pj->chosen);
166 /* Estimate the size for this record */
168 avahi_key_get_estimate_size(pj->record->key) +
169 avahi_record_get_estimate_size(pj->record);
172 if (size > avahi_dns_packet_space(p))
175 /* Create the probe query */
176 k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY);
177 b = !!avahi_dns_packet_append_key(p, k, FALSE);
180 /* Mark this job for addition to the packet */
183 /* Scan for more jobs whith matching key pattern */
184 for (pj = s->jobs; pj; pj = pj->jobs_next) {
188 /* Does the record match the probe? */
189 if (k->clazz != pj->record->key->clazz || !avahi_domain_equal(k->name, pj->record->key->name))
192 /* This job wouldn't fit in */
193 if (avahi_record_get_estimate_size(pj->record) > avahi_dns_packet_space(p))
196 /* Mark this job for addition to the packet */
205 static void elapse_callback(AvahiTimeEvent *e, gpointer data) {
206 AvahiProbeJob *pj = data, *next;
207 AvahiProbeScheduler *s;
215 /* Lets remove it from the history */
220 p = avahi_dns_packet_new_query(s->interface->hardware->mtu);
223 /* Add the import probe */
224 if (!packet_add_probe_query(s, p, pj)) {
229 avahi_dns_packet_free(p);
231 /* The probe didn't fit in the package, so let's allocate a larger one */
234 avahi_key_get_estimate_size(pj->record->key) +
235 avahi_record_get_estimate_size(pj->record) +
236 AVAHI_DNS_PACKET_HEADER_SIZE;
238 if (size > AVAHI_DNS_PACKET_MAX_SIZE)
239 size = AVAHI_DNS_PACKET_MAX_SIZE;
241 p = avahi_dns_packet_new_query(size);
243 k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY);
244 b = avahi_dns_packet_append_key(p, k, FALSE) && avahi_dns_packet_append_record(p, pj->record, FALSE, 0);
248 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_NSCOUNT, 1);
249 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_QDCOUNT, 1);
250 avahi_interface_send_packet(s->interface, p);
252 avahi_log_warn("Probe record too large, cannot send");
254 avahi_dns_packet_free(p);
255 job_mark_done(s, pj);
260 /* Try to fill up packet with more probes, if available */
261 for (pj = s->jobs; pj; pj = pj->jobs_next) {
266 if (!packet_add_probe_query(s, p, pj))
272 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_QDCOUNT, n);
276 /* Now add the chosen records to the authorative section */
277 for (pj = s->jobs; pj; pj = next) {
279 next = pj->jobs_next;
284 if (!avahi_dns_packet_append_record(p, pj->record, FALSE, 0)) {
285 avahi_log_warn("Bad probe size estimate!");
287 /* Unmark all following jobs */
288 for (; pj; pj = pj->jobs_next)
294 job_mark_done(s, pj);
299 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_NSCOUNT, n);
302 avahi_interface_send_packet(s->interface, p);
303 avahi_dns_packet_free(p);
306 static AvahiProbeJob* find_scheduled_job(AvahiProbeScheduler *s, AvahiRecord *record) {
312 for (pj = s->jobs; pj; pj = pj->jobs_next) {
315 if (avahi_record_equal_no_ttl(pj->record, record))
322 static AvahiProbeJob* find_history_job(AvahiProbeScheduler *s, AvahiRecord *record) {
328 for (pj = s->history; pj; pj = pj->jobs_next) {
331 if (avahi_record_equal_no_ttl(pj->record, record)) {
332 /* Check whether this entry is outdated */
334 if (avahi_age(&pj->delivery) > AVAHI_PROBE_HISTORY_MSEC*1000) {
335 /* it is outdated, so let's remove it */
347 gboolean avahi_probe_scheduler_post(AvahiProbeScheduler *s, AvahiRecord *record, gboolean immediately) {
353 g_assert(!avahi_key_is_pattern(record->key));
355 if ((pj = find_history_job(s, record)))
358 avahi_elapse_time(&tv, immediately ? 0 : AVAHI_PROBE_DEFER_MSEC, 0);
360 if ((pj = find_scheduled_job(s, record))) {
362 if (avahi_timeval_compare(&tv, &pj->delivery) < 0) {
363 /* If the new entry should be scheduled earlier, update the old entry */
365 avahi_time_event_queue_update(s->time_event_queue, pj->time_event, &pj->delivery);
370 /* Create a new job and schedule it */
371 pj = job_new(s, record, FALSE);
373 pj->time_event = avahi_time_event_queue_add(s->time_event_queue, &pj->delivery, elapse_callback, pj);
376 /* avahi_log_debug("Accepted new probe job."); */