]> git.meshlink.io Git - catta/blob - avahi-core/cache.c
* implement reflection (including legacy unicast reflection)
[catta] / avahi-core / cache.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 <string.h>
27
28 #include "util.h"
29 #include "cache.h"
30
31 static void remove_entry(AvahiCache *c, AvahiCacheEntry *e) {
32     AvahiCacheEntry *t;
33
34     g_assert(c);
35     g_assert(e);
36
37 /*     g_message("removing from cache: %p %p", c, e); */
38
39     /* Remove from hash table */
40     t = g_hash_table_lookup(c->hash_table, e->record->key);
41     AVAHI_LLIST_REMOVE(AvahiCacheEntry, by_key, t, e);
42     if (t)
43         g_hash_table_replace(c->hash_table, t->record->key, t);
44     else
45         g_hash_table_remove(c->hash_table, e->record->key);
46
47     /* Remove from linked list */
48     AVAHI_LLIST_REMOVE(AvahiCacheEntry, entry, c->entries, e);
49         
50     if (e->time_event)
51         avahi_time_event_queue_remove(c->server->time_event_queue, e->time_event);
52
53     avahi_browser_notify(c->server, c->interface, e->record, AVAHI_BROWSER_REMOVE);
54
55     avahi_record_unref(e->record);
56     
57     g_free(e);
58 }
59
60 AvahiCache *avahi_cache_new(AvahiServer *server, AvahiInterface *iface) {
61     AvahiCache *c;
62     g_assert(server);
63
64     c = g_new(AvahiCache, 1);
65     c->server = server;
66     c->interface = iface;
67     c->hash_table = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal);
68
69     AVAHI_LLIST_HEAD_INIT(AvahiCacheEntry, c->entries);
70     
71     return c;
72 }
73
74 void avahi_cache_free(AvahiCache *c) {
75     g_assert(c);
76
77     while (c->entries)
78         remove_entry(c, c->entries);
79     
80     g_hash_table_destroy(c->hash_table);
81     
82     g_free(c);
83 }
84
85 AvahiCacheEntry *avahi_cache_lookup_key(AvahiCache *c, AvahiKey *k) {
86     g_assert(c);
87     g_assert(k);
88
89     g_assert(!avahi_key_is_pattern(k));
90     
91     return g_hash_table_lookup(c->hash_table, k);
92 }
93
94 gpointer avahi_cache_walk(AvahiCache *c, AvahiKey *pattern, AvahiCacheWalkCallback cb, gpointer userdata) {
95     gpointer ret;
96     
97     g_assert(c);
98     g_assert(pattern);
99     g_assert(cb);
100     
101     if (avahi_key_is_pattern(pattern)) {
102         AvahiCacheEntry *e, *n;
103         
104         for (e = c->entries; e; e = n) {
105             n = e->entry_next;
106             
107             if (avahi_key_pattern_match(pattern, e->record->key))
108                 if ((ret = cb(c, pattern, e, userdata)))
109                     return ret;
110         }
111         
112     } else {
113         AvahiCacheEntry *e, *n;
114
115         for (e = avahi_cache_lookup_key(c, pattern); e; e = n) {
116             n = e->by_key_next;
117                 
118             if ((ret = cb(c, pattern, e, userdata)))
119                 return ret;
120         }
121     }
122
123     return NULL;
124 }
125
126 static gpointer lookup_record_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void *userdata) {
127     g_assert(c);
128     g_assert(pattern);
129     g_assert(e);
130
131     if (avahi_record_equal_no_ttl(e->record, userdata))
132         return e;
133
134     return NULL;
135 }
136
137 AvahiCacheEntry *avahi_cache_lookup_record(AvahiCache *c, AvahiRecord *r) {
138     g_assert(c);
139     g_assert(r);
140
141     return avahi_cache_walk(c, r->key, lookup_record_callback, r);
142 }
143
144 static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, guint percent);
145
146 static void elapse_func(AvahiTimeEvent *t, void *userdata) {
147     AvahiCacheEntry *e = userdata;
148     
149     g_assert(t);
150     g_assert(e);
151
152     if (e->state == AVAHI_CACHE_FINAL) {
153         remove_entry(e->cache, e);
154 /*         g_message("Removing entry from cache due to expiration"); */
155     } else {
156         guint percent = 0;
157     
158         switch (e->state) {
159             case AVAHI_CACHE_VALID:
160                 e->state = AVAHI_CACHE_EXPIRY1;
161                 percent = 85;
162                 break;
163                 
164             case AVAHI_CACHE_EXPIRY1:
165                 e->state = AVAHI_CACHE_EXPIRY2;
166                 percent = 90;
167                 break;
168             case AVAHI_CACHE_EXPIRY2:
169                 e->state = AVAHI_CACHE_EXPIRY3;
170                 percent = 95;
171                 break;
172                 
173             case AVAHI_CACHE_EXPIRY3:
174                 e->state = AVAHI_CACHE_FINAL;
175                 percent = 100;
176                 break;
177
178             default:
179                 ;
180         }
181
182         g_assert(percent > 0);
183
184         /* Request a cache update, if we are subscribed to this entry */
185         if (avahi_is_subscribed(e->cache->server, e->cache->interface, e->record->key)) {
186 /*             g_message("Requesting cache entry update at %i%%.", percent); */
187             avahi_interface_post_query(e->cache->interface, e->record->key, TRUE);
188         }
189
190         /* Check again later */
191         next_expiry(e->cache, e, percent);
192     }
193 }
194
195 static void update_time_event(AvahiCache *c, AvahiCacheEntry *e) {
196     g_assert(c);
197     g_assert(e);
198     
199     if (e->time_event)
200         avahi_time_event_queue_update(c->server->time_event_queue, e->time_event, &e->expiry);
201     else
202         e->time_event = avahi_time_event_queue_add(c->server->time_event_queue, &e->expiry, elapse_func, e);
203 }
204
205 static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, guint percent) {
206     gulong usec;
207
208     g_assert(c);
209     g_assert(e);
210     g_assert(percent > 0 && percent <= 100);
211
212     e->expiry = e->timestamp;
213
214     usec = e->record->ttl * 10000;
215
216     /* 2% jitter */
217     usec = g_random_int_range(usec*percent, usec*(percent+2));
218     
219     g_time_val_add(&e->expiry, usec);
220     update_time_event(c, e);
221 }
222
223 static void expire_in_one_second(AvahiCache *c, AvahiCacheEntry *e) {
224     g_assert(c);
225     g_assert(e);
226     
227     e->state = AVAHI_CACHE_FINAL;
228     g_get_current_time(&e->expiry);
229     g_time_val_add(&e->expiry, 1000000); /* 1s */
230     update_time_event(c, e);
231 }
232
233 void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean cache_flush, const AvahiAddress *a) {
234 /*     gchar *txt; */
235     
236     g_assert(c);
237     g_assert(r && r->ref >= 1);
238
239 /*     g_message("cache update: %s", (txt = avahi_record_to_string(r))); */
240 /*     g_free(txt); */
241
242     if (r->ttl == 0) {
243         /* This is a goodbye request */
244
245         AvahiCacheEntry *e;
246
247         if ((e = avahi_cache_lookup_record(c, r)))
248             expire_in_one_second(c, e);
249
250     } else {
251         AvahiCacheEntry *e = NULL, *first;
252         GTimeVal now;
253
254         g_get_current_time(&now);
255
256         /* This is an update request */
257
258         if ((first = avahi_cache_lookup_key(c, r->key))) {
259             
260             if (cache_flush) {
261
262                 /* For unique entries drop all entries older than one second */
263                 for (e = first; e; e = e->by_key_next) {
264                     glong t;
265
266                     t = avahi_timeval_diff(&now, &e->timestamp);
267
268                     if (t > 1000000)
269                         expire_in_one_second(c, e);
270                 }
271             }
272                 
273             /* Look for exactly the same entry */
274             for (e = first; e; e = e->by_key_next)
275                 if (avahi_record_equal_no_ttl(e->record, r))
276                     break;
277         }
278     
279         if (e) {
280             
281 /*             g_message("found matching cache entry");  */
282
283             /* We need to update the hash table key if we replace the
284              * record */
285             if (e->by_key_prev == NULL)
286                 g_hash_table_replace(c->hash_table, r->key, e);
287             
288             /* Update the record */
289             avahi_record_unref(e->record);
290             e->record = avahi_record_ref(r);
291
292         } else {
293             /* No entry found, therefore we create a new one */
294             
295 /*             g_message("couldn't find matching cache entry");  */
296             
297             e = g_new(AvahiCacheEntry, 1);
298             e->cache = c;
299             e->time_event = NULL;
300             e->record = avahi_record_ref(r);
301
302             /* Append to hash table */
303             AVAHI_LLIST_PREPEND(AvahiCacheEntry, by_key, first, e);
304             g_hash_table_replace(c->hash_table, e->record->key, first);
305
306             /* Append to linked list */
307             AVAHI_LLIST_PREPEND(AvahiCacheEntry, entry, c->entries, e);
308
309             /* Notify subscribers */
310             avahi_browser_notify(c->server, c->interface, e->record, AVAHI_BROWSER_NEW);
311         } 
312         
313         e->origin = *a;
314         e->timestamp = now;
315         next_expiry(c, e, 80);
316         e->state = AVAHI_CACHE_VALID;
317         e->cache_flush = cache_flush;
318     }
319 }
320
321 static void dump_callback(gpointer key, gpointer data, gpointer userdata) {
322     AvahiCacheEntry *e = data;
323     AvahiKey *k = key;
324
325     g_assert(k);
326     g_assert(e);
327
328     for (; e; e = e->by_key_next) {
329         gchar *t = avahi_record_to_string(e->record);
330         fprintf((FILE*) userdata, "%s\n", t);
331         g_free(t);
332     }
333 }
334
335 void avahi_cache_dump(AvahiCache *c, FILE *f) {
336     g_assert(c);
337     g_assert(f);
338
339     fprintf(f, ";;; CACHE DUMP FOLLOWS ;;;\n");
340     g_hash_table_foreach(c->hash_table, dump_callback, f);
341 }
342
343 gboolean avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e) {
344     GTimeVal now;
345     guint age;
346     
347     g_assert(c);
348     g_assert(e);
349
350     g_get_current_time(&now);
351
352     age = avahi_timeval_diff(&now, &e->timestamp)/1000000;
353
354 /*     g_message("age: %u, ttl/2: %u", age, e->record->ttl); */
355     
356     return age >= e->record->ttl/2;
357 }
358
359 void avahi_cache_flush(AvahiCache *c) {
360     g_assert(c);
361
362     while (c->entries)
363         remove_entry(c, c->entries);
364 }