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
31 #define AVAHI_MAX_CACHE_ENTRIES 200
33 static void remove_entry(AvahiCache *c, AvahiCacheEntry *e) {
39 /* avahi_log_debug("removing from cache: %p %p", c, e); */
41 /* Remove from hash table */
42 t = g_hash_table_lookup(c->hash_table, e->record->key);
43 AVAHI_LLIST_REMOVE(AvahiCacheEntry, by_key, t, e);
45 g_hash_table_replace(c->hash_table, t->record->key, t);
47 g_hash_table_remove(c->hash_table, e->record->key);
49 /* Remove from linked list */
50 AVAHI_LLIST_REMOVE(AvahiCacheEntry, entry, c->entries, e);
53 avahi_time_event_queue_remove(c->server->time_event_queue, e->time_event);
55 avahi_browser_notify(c->server, c->interface, e->record, AVAHI_BROWSER_REMOVE);
57 avahi_record_unref(e->record);
61 g_assert(c->n_entries-- >= 1);
64 AvahiCache *avahi_cache_new(AvahiServer *server, AvahiInterface *iface) {
68 c = g_new(AvahiCache, 1);
71 c->hash_table = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal);
73 AVAHI_LLIST_HEAD_INIT(AvahiCacheEntry, c->entries);
79 void avahi_cache_free(AvahiCache *c) {
83 remove_entry(c, c->entries);
84 g_assert(c->n_entries == 0);
86 g_hash_table_destroy(c->hash_table);
91 AvahiCacheEntry *avahi_cache_lookup_key(AvahiCache *c, AvahiKey *k) {
95 g_assert(!avahi_key_is_pattern(k));
97 return g_hash_table_lookup(c->hash_table, k);
100 gpointer avahi_cache_walk(AvahiCache *c, AvahiKey *pattern, AvahiCacheWalkCallback cb, gpointer userdata) {
107 if (avahi_key_is_pattern(pattern)) {
108 AvahiCacheEntry *e, *n;
110 for (e = c->entries; e; e = n) {
113 if (avahi_key_pattern_match(pattern, e->record->key))
114 if ((ret = cb(c, pattern, e, userdata)))
119 AvahiCacheEntry *e, *n;
121 for (e = avahi_cache_lookup_key(c, pattern); e; e = n) {
124 if ((ret = cb(c, pattern, e, userdata)))
132 static gpointer lookup_record_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void *userdata) {
137 if (avahi_record_equal_no_ttl(e->record, userdata))
143 AvahiCacheEntry *avahi_cache_lookup_record(AvahiCache *c, AvahiRecord *r) {
147 return avahi_cache_walk(c, r->key, lookup_record_callback, r);
150 static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, guint percent);
152 static void elapse_func(AvahiTimeEvent *t, void *userdata) {
153 AvahiCacheEntry *e = userdata;
158 if (e->state == AVAHI_CACHE_FINAL) {
159 remove_entry(e->cache, e);
160 /* avahi_log_debug("Removing entry from cache due to expiration"); */
165 case AVAHI_CACHE_VALID:
166 e->state = AVAHI_CACHE_EXPIRY1;
170 case AVAHI_CACHE_EXPIRY1:
171 e->state = AVAHI_CACHE_EXPIRY2;
174 case AVAHI_CACHE_EXPIRY2:
175 e->state = AVAHI_CACHE_EXPIRY3;
179 case AVAHI_CACHE_EXPIRY3:
180 e->state = AVAHI_CACHE_FINAL;
188 g_assert(percent > 0);
190 /* Request a cache update, if we are subscribed to this entry */
191 if (avahi_is_subscribed(e->cache->server, e->cache->interface, e->record->key)) {
192 /* avahi_log_debug("Requesting cache entry update at %i%%.", percent); */
193 avahi_interface_post_query(e->cache->interface, e->record->key, TRUE);
196 /* Check again later */
197 next_expiry(e->cache, e, percent);
201 static void update_time_event(AvahiCache *c, AvahiCacheEntry *e) {
206 avahi_time_event_queue_update(c->server->time_event_queue, e->time_event, &e->expiry);
208 e->time_event = avahi_time_event_queue_add(c->server->time_event_queue, &e->expiry, elapse_func, e);
211 static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, guint percent) {
216 g_assert(percent > 0 && percent <= 100);
218 e->expiry = e->timestamp;
220 usec = e->record->ttl * 10000;
223 usec = g_random_int_range(usec*percent, usec*(percent+2));
225 g_time_val_add(&e->expiry, usec);
226 update_time_event(c, e);
229 static void expire_in_one_second(AvahiCache *c, AvahiCacheEntry *e) {
233 e->state = AVAHI_CACHE_FINAL;
234 g_get_current_time(&e->expiry);
235 g_time_val_add(&e->expiry, 1000000); /* 1s */
236 update_time_event(c, e);
239 void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean cache_flush, const AvahiAddress *a) {
243 g_assert(r && r->ref >= 1);
245 /* avahi_log_debug("cache update: %s", (txt = avahi_record_to_string(r))); */
249 /* This is a goodbye request */
253 if ((e = avahi_cache_lookup_record(c, r)))
254 expire_in_one_second(c, e);
257 AvahiCacheEntry *e = NULL, *first;
260 g_get_current_time(&now);
262 /* This is an update request */
264 if ((first = avahi_cache_lookup_key(c, r->key))) {
268 /* For unique entries drop all entries older than one second */
269 for (e = first; e; e = e->by_key_next) {
272 t = avahi_timeval_diff(&now, &e->timestamp);
275 expire_in_one_second(c, e);
279 /* Look for exactly the same entry */
280 for (e = first; e; e = e->by_key_next)
281 if (avahi_record_equal_no_ttl(e->record, r))
287 /* avahi_log_debug("found matching cache entry"); */
289 /* We need to update the hash table key if we replace the
291 if (e->by_key_prev == NULL)
292 g_hash_table_replace(c->hash_table, r->key, e);
294 /* Update the record */
295 avahi_record_unref(e->record);
296 e->record = avahi_record_ref(r);
299 /* No entry found, therefore we create a new one */
301 /* avahi_log_debug("couldn't find matching cache entry"); */
303 if (c->n_entries >= AVAHI_MAX_CACHE_ENTRIES)
308 e = g_new(AvahiCacheEntry, 1);
310 e->time_event = NULL;
311 e->record = avahi_record_ref(r);
313 /* Append to hash table */
314 AVAHI_LLIST_PREPEND(AvahiCacheEntry, by_key, first, e);
315 g_hash_table_replace(c->hash_table, e->record->key, first);
317 /* Append to linked list */
318 AVAHI_LLIST_PREPEND(AvahiCacheEntry, entry, c->entries, e);
320 /* Notify subscribers */
321 avahi_browser_notify(c->server, c->interface, e->record, AVAHI_BROWSER_NEW);
326 next_expiry(c, e, 80);
327 e->state = AVAHI_CACHE_VALID;
328 e->cache_flush = cache_flush;
332 static void dump_callback(gpointer key, gpointer data, gpointer userdata) {
333 AvahiCacheEntry *e = data;
339 for (; e; e = e->by_key_next) {
340 gchar *t = avahi_record_to_string(e->record);
341 fprintf((FILE*) userdata, "%s\n", t);
346 void avahi_cache_dump(AvahiCache *c, FILE *f) {
350 fprintf(f, ";;; CACHE DUMP FOLLOWS ;;;\n");
351 g_hash_table_foreach(c->hash_table, dump_callback, f);
354 gboolean avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e) {
361 g_get_current_time(&now);
363 age = avahi_timeval_diff(&now, &e->timestamp)/1000000;
365 /* avahi_log_debug("age: %u, ttl/2: %u", age, e->record->ttl); */
367 return age >= e->record->ttl/2;
370 void avahi_cache_flush(AvahiCache *c) {
374 remove_entry(c, c->entries);