1 /***
2   This file is part of avahi.
3 
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.
8 
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.
13 
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
17   USA.
18 ***/
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 
24 #include <stdlib.h>
25 
26 #include <avahi-common/domain.h>
27 #include <avahi-common/timeval.h>
28 #include <avahi-common/malloc.h>
29 
30 #include "probe-sched.h"
31 #include "log.h"
32 #include "rr-util.h"
33 
34 #define AVAHI_PROBE_HISTORY_MSEC 150
35 #define AVAHI_PROBE_DEFER_MSEC 50
36 
37 typedef struct AvahiProbeJob AvahiProbeJob;
38 
39 struct AvahiProbeJob {
40     AvahiProbeScheduler *scheduler;
41     AvahiTimeEvent *time_event;
42 
43     int chosen; /* Use for packet assembling */
44     int done;
45     struct timeval delivery;
46 
47     AvahiRecord *record;
48 
49     AVAHI_LLIST_FIELDS(AvahiProbeJob, jobs);
50 };
51 
52 struct AvahiProbeScheduler {
53     AvahiInterface *interface;
54     AvahiTimeEventQueue *time_event_queue;
55 
56     AVAHI_LLIST_HEAD(AvahiProbeJob, jobs);
57     AVAHI_LLIST_HEAD(AvahiProbeJob, history);
58 };
59 
job_new(AvahiProbeScheduler * s,AvahiRecord * record,int done)60 static AvahiProbeJob* job_new(AvahiProbeScheduler *s, AvahiRecord *record, int done) {
61     AvahiProbeJob *pj;
62 
63     assert(s);
64     assert(record);
65 
66     if (!(pj = avahi_new(AvahiProbeJob, 1))) {
67         avahi_log_error(__FILE__": Out of memory");
68         return NULL; /* OOM */
69     }
70 
71     pj->scheduler = s;
72     pj->record = avahi_record_ref(record);
73     pj->time_event = NULL;
74     pj->chosen = 0;
75 
76     if ((pj->done = done))
77         AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->history, pj);
78     else
79         AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->jobs, pj);
80 
81     return pj;
82 }
83 
job_free(AvahiProbeScheduler * s,AvahiProbeJob * pj)84 static void job_free(AvahiProbeScheduler *s, AvahiProbeJob *pj) {
85     assert(pj);
86 
87     if (pj->time_event)
88         avahi_time_event_free(pj->time_event);
89 
90     if (pj->done)
91         AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->history, pj);
92     else
93         AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj);
94 
95     avahi_record_unref(pj->record);
96     avahi_free(pj);
97 }
98 
99 static void elapse_callback(AvahiTimeEvent *e, void* data);
100 
job_set_elapse_time(AvahiProbeScheduler * s,AvahiProbeJob * pj,unsigned msec,unsigned jitter)101 static void job_set_elapse_time(AvahiProbeScheduler *s, AvahiProbeJob *pj, unsigned msec, unsigned jitter) {
102     struct timeval tv;
103 
104     assert(s);
105     assert(pj);
106 
107     avahi_elapse_time(&tv, msec, jitter);
108 
109     if (pj->time_event)
110         avahi_time_event_update(pj->time_event, &tv);
111     else
112         pj->time_event = avahi_time_event_new(s->time_event_queue, &tv, elapse_callback, pj);
113 }
114 
job_mark_done(AvahiProbeScheduler * s,AvahiProbeJob * pj)115 static void job_mark_done(AvahiProbeScheduler *s, AvahiProbeJob *pj) {
116     assert(s);
117     assert(pj);
118 
119     assert(!pj->done);
120 
121     AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj);
122     AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->history, pj);
123 
124     pj->done = 1;
125 
126     job_set_elapse_time(s, pj, AVAHI_PROBE_HISTORY_MSEC, 0);
127     gettimeofday(&pj->delivery, NULL);
128 }
129 
avahi_probe_scheduler_new(AvahiInterface * i)130 AvahiProbeScheduler *avahi_probe_scheduler_new(AvahiInterface *i) {
131     AvahiProbeScheduler *s;
132 
133     assert(i);
134 
135     if (!(s = avahi_new(AvahiProbeScheduler, 1))) {
136         avahi_log_error(__FILE__": Out of memory");
137         return NULL;
138     }
139 
140     s->interface = i;
141     s->time_event_queue = i->monitor->server->time_event_queue;
142 
143     AVAHI_LLIST_HEAD_INIT(AvahiProbeJob, s->jobs);
144     AVAHI_LLIST_HEAD_INIT(AvahiProbeJob, s->history);
145 
146     return s;
147 }
148 
avahi_probe_scheduler_free(AvahiProbeScheduler * s)149 void avahi_probe_scheduler_free(AvahiProbeScheduler *s) {
150     assert(s);
151 
152     avahi_probe_scheduler_clear(s);
153     avahi_free(s);
154 }
155 
avahi_probe_scheduler_clear(AvahiProbeScheduler * s)156 void avahi_probe_scheduler_clear(AvahiProbeScheduler *s) {
157     assert(s);
158 
159     while (s->jobs)
160         job_free(s, s->jobs);
161     while (s->history)
162         job_free(s, s->history);
163 }
164 
packet_add_probe_query(AvahiProbeScheduler * s,AvahiDnsPacket * p,AvahiProbeJob * pj)165 static int packet_add_probe_query(AvahiProbeScheduler *s, AvahiDnsPacket *p, AvahiProbeJob *pj) {
166     size_t size;
167     AvahiKey *k;
168     int b;
169 
170     assert(s);
171     assert(p);
172     assert(pj);
173 
174     assert(!pj->chosen);
175 
176     /* Estimate the size for this record */
177     size =
178         avahi_key_get_estimate_size(pj->record->key) +
179         avahi_record_get_estimate_size(pj->record);
180 
181     /* Too large */
182     if (size > avahi_dns_packet_reserved_space(p))
183         return 0;
184 
185     /* Create the probe query */
186     if (!(k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY)))
187         return 0; /* OOM */
188 
189     b = !!avahi_dns_packet_append_key(p, k, 0);
190     assert(b);
191 
192     /* reserve size for record data */
193     avahi_dns_packet_reserve_size(p, avahi_record_get_estimate_size(pj->record));
194 
195     /* Mark this job for addition to the packet */
196     pj->chosen = 1;
197 
198     /* Scan for more jobs whith matching key pattern */
199     for (pj = s->jobs; pj; pj = pj->jobs_next) {
200         if (pj->chosen)
201             continue;
202 
203         /* Does the record match the probe? */
204         if (k->clazz != pj->record->key->clazz || !avahi_domain_equal(k->name, pj->record->key->name))
205             continue;
206 
207         /* This job wouldn't fit in */
208         if (avahi_record_get_estimate_size(pj->record) > avahi_dns_packet_reserved_space(p))
209             break;
210 
211    /* reserve size for record data */
212    avahi_dns_packet_reserve_size(p, avahi_record_get_estimate_size(pj->record));
213 
214         /* Mark this job for addition to the packet */
215         pj->chosen = 1;
216     }
217 
218     avahi_key_unref(k);
219 
220     return 1;
221 }
222 
elapse_callback(AVAHI_GCC_UNUSED AvahiTimeEvent * e,void * data)223 static void elapse_callback(AVAHI_GCC_UNUSED AvahiTimeEvent *e, void* data) {
224     AvahiProbeJob *pj = data, *next;
225     AvahiProbeScheduler *s;
226     AvahiDnsPacket *p;
227     unsigned n;
228 
229     assert(pj);
230     s = pj->scheduler;
231 
232     if (pj->done) {
233         /* Lets remove it  from the history */
234         job_free(s, pj);
235         return;
236     }
237 
238     if (!(p = avahi_dns_packet_new_query(s->interface->hardware->mtu)))
239         return; /* OOM */
240     n = 1;
241 
242     /* Add the import probe */
243     if (!packet_add_probe_query(s, p, pj)) {
244         size_t size;
245         AvahiKey *k;
246         int b;
247 
248         avahi_dns_packet_free(p);
249 
250         /* The probe didn't fit in the package, so let's allocate a larger one */
251 
252         size =
253             avahi_key_get_estimate_size(pj->record->key) +
254             avahi_record_get_estimate_size(pj->record) +
255             AVAHI_DNS_PACKET_HEADER_SIZE;
256 
257         if (!(p = avahi_dns_packet_new_query(size + AVAHI_DNS_PACKET_EXTRA_SIZE)))
258             return; /* OOM */
259 
260         if (!(k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY))) {
261             avahi_dns_packet_free(p);
262             return;  /* OOM */
263         }
264 
265         b = avahi_dns_packet_append_key(p, k, 0) && avahi_dns_packet_append_record(p, pj->record, 0, 0);
266         avahi_key_unref(k);
267 
268         if (b) {
269             avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_NSCOUNT, 1);
270             avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_QDCOUNT, 1);
271             avahi_interface_send_packet(s->interface, p);
272         } else
273             avahi_log_warn("Probe record too large, cannot send");
274 
275         avahi_dns_packet_free(p);
276         job_mark_done(s, pj);
277 
278         return;
279     }
280 
281     /* Try to fill up packet with more probes, if available */
282     for (pj = s->jobs; pj; pj = pj->jobs_next) {
283 
284         if (pj->chosen)
285             continue;
286 
287         if (!packet_add_probe_query(s, p, pj))
288             break;
289 
290         n++;
291     }
292 
293     avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_QDCOUNT, n);
294 
295     n = 0;
296 
297     /* Now add the chosen records to the authorative section */
298     for (pj = s->jobs; pj; pj = next) {
299 
300         next = pj->jobs_next;
301 
302         if (!pj->chosen)
303             continue;
304 
305         if (!avahi_dns_packet_append_record(p, pj->record, 0, 0)) {
306 /*             avahi_log_warn("Bad probe size estimate!"); */
307 
308             /* Unmark all following jobs */
309             for (; pj; pj = pj->jobs_next)
310                 pj->chosen = 0;
311 
312             break;
313         }
314 
315         job_mark_done(s, pj);
316 
317         n ++;
318     }
319 
320     avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_NSCOUNT, n);
321 
322     /* Send it now */
323     avahi_interface_send_packet(s->interface, p);
324     avahi_dns_packet_free(p);
325 }
326 
find_scheduled_job(AvahiProbeScheduler * s,AvahiRecord * record)327 static AvahiProbeJob* find_scheduled_job(AvahiProbeScheduler *s, AvahiRecord *record) {
328     AvahiProbeJob *pj;
329 
330     assert(s);
331     assert(record);
332 
333     for (pj = s->jobs; pj; pj = pj->jobs_next) {
334         assert(!pj->done);
335 
336         if (avahi_record_equal_no_ttl(pj->record, record))
337             return pj;
338     }
339 
340     return NULL;
341 }
342 
find_history_job(AvahiProbeScheduler * s,AvahiRecord * record)343 static AvahiProbeJob* find_history_job(AvahiProbeScheduler *s, AvahiRecord *record) {
344     AvahiProbeJob *pj;
345 
346     assert(s);
347     assert(record);
348 
349     for (pj = s->history; pj; pj = pj->jobs_next) {
350         assert(pj->done);
351 
352         if (avahi_record_equal_no_ttl(pj->record, record)) {
353             /* Check whether this entry is outdated */
354 
355             if (avahi_age(&pj->delivery) > AVAHI_PROBE_HISTORY_MSEC*1000) {
356                 /* it is outdated, so let's remove it */
357                 job_free(s, pj);
358                 return NULL;
359             }
360 
361             return pj;
362         }
363     }
364 
365     return NULL;
366 }
367 
avahi_probe_scheduler_post(AvahiProbeScheduler * s,AvahiRecord * record,int immediately)368 int avahi_probe_scheduler_post(AvahiProbeScheduler *s, AvahiRecord *record, int immediately) {
369     AvahiProbeJob *pj;
370     struct timeval tv;
371 
372     assert(s);
373     assert(record);
374     assert(!avahi_key_is_pattern(record->key));
375 
376     if ((pj = find_history_job(s, record)))
377         return 0;
378 
379     avahi_elapse_time(&tv, immediately ? 0 : AVAHI_PROBE_DEFER_MSEC, 0);
380 
381     if ((pj = find_scheduled_job(s, record))) {
382 
383         if (avahi_timeval_compare(&tv, &pj->delivery) < 0) {
384             /* If the new entry should be scheduled earlier, update the old entry */
385             pj->delivery = tv;
386             avahi_time_event_update(pj->time_event, &pj->delivery);
387         }
388 
389         return 1;
390     } else {
391         /* Create a new job and schedule it */
392         if (!(pj = job_new(s, record, 0)))
393             return 0; /* OOM */
394 
395         pj->delivery = tv;
396         pj->time_event = avahi_time_event_new(s->time_event_queue, &pj->delivery, elapse_callback, pj);
397 
398 
399 /*     avahi_log_debug("Accepted new probe job."); */
400 
401         return 1;
402     }
403 }
404