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