]> git.meshlink.io Git - catta/blob - avahi-core/cache.c
check MUSTs of RFC:
[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_subscription_notify(c->server, c->interface, e->record, AVAHI_SUBSCRIPTION_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->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 void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean unique, const AvahiAddress *a) {
224     AvahiCacheEntry *e, *t;
225     gchar *txt;
226     
227     g_assert(c);
228     g_assert(r && r->ref >= 1);
229
230     g_message("cache update: %s", (txt = avahi_record_to_string(r)));
231     g_free(txt);
232
233     if (r->ttl == 0) {
234
235         /* This is a goodbye request */
236
237         if ((e = avahi_cache_lookup_record(c, r))) {
238
239             e->state = AVAHI_CACHE_FINAL;
240             g_get_current_time(&e->timestamp);
241             e->expiry = e->timestamp;
242             g_time_val_add(&e->expiry, 1000000); /* 1s */
243             update_time_event(c, e);
244         }
245
246     } else {
247
248         /* This is an update request */
249
250         if ((t = e = avahi_cache_lookup_key(c, r->key))) {
251         
252             if (unique) {
253                 
254                 /* For unique records, remove all entries but one */
255                 while (e->by_key_next)
256                     remove_entry(c, e->by_key_next);
257                 
258             } else {
259                 
260                 /* For non-unique record, look for exactly the same entry */
261                 for (; e; e = e->by_key_next)
262                     if (avahi_record_equal_no_ttl(e->record, r))
263                         break;
264             }
265         }
266     
267         if (e) {
268             
269 /*         g_message("found matching cache entry"); */
270             
271             /* We are the first in the linked list so let's replace the hash table key with the new one */
272             if (e->by_key_prev == NULL)
273                 g_hash_table_replace(c->hash_table, r->key, e);
274             
275             /* Notify subscribers */
276             if (!avahi_record_equal_no_ttl(e->record, r))
277                 avahi_subscription_notify(c->server, c->interface, r, AVAHI_SUBSCRIPTION_CHANGE);    
278             
279             /* Update the record */
280             avahi_record_unref(e->record);
281             e->record = avahi_record_ref(r);
282             
283         } else {
284             /* No entry found, therefore we create a new one */
285             
286 /*         g_message("couldn't find matching cache entry"); */
287             
288             e = g_new(AvahiCacheEntry, 1);
289             e->cache = c;
290             e->time_event = NULL;
291             e->record = avahi_record_ref(r);
292
293             /* Append to hash table */
294             AVAHI_LLIST_PREPEND(AvahiCacheEntry, by_key, t, e);
295             g_hash_table_replace(c->hash_table, e->record->key, t);
296
297             /* Append to linked list */
298             AVAHI_LLIST_PREPEND(AvahiCacheEntry, entry, c->entries, e);
299
300             /* Notify subscribers */
301             avahi_subscription_notify(c->server, c->interface, e->record, AVAHI_SUBSCRIPTION_NEW);
302         } 
303         
304         e->origin = *a;
305         g_get_current_time(&e->timestamp);
306         next_expiry(c, e, 80);
307         e->state = AVAHI_CACHE_VALID;
308     }
309 }
310
311 static gpointer drop_key_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, gpointer userdata) {
312     g_assert(c);
313     g_assert(pattern);
314     g_assert(e);
315
316     remove_entry(c, e);
317     return NULL;
318 }
319
320 void avahi_cache_drop_key(AvahiCache *c, AvahiKey *k) {
321     g_assert(c);
322     g_assert(k);
323
324     avahi_cache_walk(c, k, drop_key_callback, NULL);
325 }
326
327 void avahi_cache_drop_record(AvahiCache *c, AvahiRecord *r) {
328     AvahiCacheEntry *e;
329     
330     g_assert(c);
331     g_assert(r);
332
333     if ((e = avahi_cache_lookup_record(c, r))) 
334         remove_entry(c, e);
335 }
336
337 static void func(gpointer key, gpointer data, gpointer userdata) {
338     AvahiCacheEntry *e = data;
339     AvahiKey *k = key;
340     gchar *t;
341
342     g_assert(k);
343     g_assert(e);
344     
345     t = avahi_record_to_string(e->record);
346     fprintf((FILE*) userdata, "%s\n", t);
347     g_free(t);
348 }
349
350 void avahi_cache_dump(AvahiCache *c, FILE *f) {
351     g_assert(c);
352     g_assert(f);
353
354     fprintf(f, ";;; CACHE DUMP FOLLOWS ;;;\n");
355     g_hash_table_foreach(c->hash_table, func, f);
356 }
357
358 gboolean avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e) {
359     GTimeVal now;
360     guint age;
361     
362     g_assert(c);
363     g_assert(e);
364
365     g_get_current_time(&now);
366
367     age = avahi_timeval_diff(&now, &e->timestamp)/1000000;
368
369     g_message("age: %u, ttl/2: %u", age, e->record->ttl);
370     
371     return age >= e->record->ttl/2;
372 }