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 static void remove_entry(AvahiCache *c, AvahiCacheEntry *e) {
37 /* g_message("removing from cache: %p %p", c, e); */
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);
43 g_hash_table_replace(c->hash_table, t->record->key, t);
45 g_hash_table_remove(c->hash_table, e->record->key);
47 /* Remove from linked list */
48 AVAHI_LLIST_REMOVE(AvahiCacheEntry, entry, c->entries, e);
51 avahi_time_event_queue_remove(c->server->time_event_queue, e->time_event);
53 avahi_subscription_notify(c->server, c->interface, e->record, AVAHI_SUBSCRIPTION_REMOVE);
55 avahi_record_unref(e->record);
60 AvahiCache *avahi_cache_new(AvahiServer *server, AvahiInterface *iface) {
64 c = g_new(AvahiCache, 1);
67 c->hash_table = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal);
69 AVAHI_LLIST_HEAD_INIT(AvahiCacheEntry, c->entries);
74 void avahi_cache_free(AvahiCache *c) {
78 remove_entry(c, c->entries);
80 g_hash_table_destroy(c->hash_table);
85 AvahiCacheEntry *avahi_cache_lookup_key(AvahiCache *c, AvahiKey *k) {
89 g_assert(!avahi_key_is_pattern(k));
91 return g_hash_table_lookup(c->hash_table, k);
94 gpointer avahi_cache_walk(AvahiCache *c, AvahiKey *pattern, AvahiCacheWalkCallback cb, gpointer userdata) {
101 if (avahi_key_is_pattern(pattern)) {
102 AvahiCacheEntry *e, *n;
104 for (e = c->entries; e; e = n) {
107 if (avahi_key_pattern_match(pattern, e->record->key))
108 if ((ret = cb(c, pattern, e, userdata)))
113 AvahiCacheEntry *e, *n;
115 for (e = avahi_cache_lookup_key(c, pattern); e; e = n) {
118 if ((ret = cb(c, pattern, e, userdata)))
126 static gpointer lookup_record_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void *userdata) {
131 if (avahi_record_equal_no_ttl(e->record, userdata))
137 AvahiCacheEntry *avahi_cache_lookup_record(AvahiCache *c, AvahiRecord *r) {
141 return avahi_cache_walk(c, r->key, lookup_record_callback, r);
144 static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, guint percent);
146 static void elapse_func(AvahiTimeEvent *t, void *userdata) {
147 AvahiCacheEntry *e = userdata;
152 if (e->state == AVAHI_CACHE_FINAL) {
153 remove_entry(e->cache, e);
154 /* g_message("Removing entry from cache due to expiration"); */
159 case AVAHI_CACHE_VALID:
160 e->state = AVAHI_CACHE_EXPIRY1;
164 case AVAHI_CACHE_EXPIRY1:
165 e->state = AVAHI_CACHE_EXPIRY2;
168 case AVAHI_CACHE_EXPIRY2:
169 e->state = AVAHI_CACHE_EXPIRY3;
173 case AVAHI_CACHE_EXPIRY3:
174 e->state = AVAHI_CACHE_FINAL;
182 g_assert(percent > 0);
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);
190 /* Check again later */
191 next_expiry(e->cache, e, percent);
195 static void update_time_event(AvahiCache *c, AvahiCacheEntry *e) {
200 avahi_time_event_queue_update(c->server->time_event_queue, e->time_event, &e->expiry);
202 e->time_event = avahi_time_event_queue_add(c->server->time_event_queue, &e->expiry, elapse_func, e);
205 static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, guint percent) {
210 g_assert(percent > 0 && percent <= 100);
212 e->expiry = e->timestamp;
214 usec = e->record->ttl * 10000;
217 usec = g_random_int_range(usec*percent, usec*(percent+2));
219 g_time_val_add(&e->expiry, usec);
220 update_time_event(c, e);
223 static void expire_in_one_second(AvahiCache *c, AvahiCacheEntry *e) {
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);
233 void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean unique, const AvahiAddress *a) {
237 g_assert(r && r->ref >= 1);
239 /* g_message("cache update: %s", (txt = avahi_record_to_string(r))); */
243 /* This is a goodbye request */
247 if ((e = avahi_cache_lookup_record(c, r)))
248 expire_in_one_second(c, e);
251 AvahiCacheEntry *e = NULL, *first;
254 g_get_current_time(&now);
256 /* This is an update request */
258 if ((first = avahi_cache_lookup_key(c, r->key))) {
262 /* For unique entries drop all entries older than one second */
263 for (e = first; e; e = e->by_key_next) {
266 t = avahi_timeval_diff(&now, &e->timestamp);
269 expire_in_one_second(c, e);
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))
281 /* g_message("found matching cache entry"); */
283 /* We need to update the hash table key if we replace the
285 if (e->by_key_prev == NULL)
286 g_hash_table_replace(c->hash_table, r->key, e);
288 /* Update the record */
289 avahi_record_unref(e->record);
290 e->record = avahi_record_ref(r);
293 /* No entry found, therefore we create a new one */
295 /* g_message("couldn't find matching cache entry"); */
297 e = g_new(AvahiCacheEntry, 1);
299 e->time_event = NULL;
300 e->record = avahi_record_ref(r);
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);
306 /* Append to linked list */
307 AVAHI_LLIST_PREPEND(AvahiCacheEntry, entry, c->entries, e);
309 /* Notify subscribers */
310 avahi_subscription_notify(c->server, c->interface, e->record, AVAHI_SUBSCRIPTION_NEW);
315 next_expiry(c, e, 80);
316 e->state = AVAHI_CACHE_VALID;
320 static void dump_callback(gpointer key, gpointer data, gpointer userdata) {
321 AvahiCacheEntry *e = data;
327 for (; e; e = e->by_key_next) {
328 gchar *t = avahi_record_to_string(e->record);
329 fprintf((FILE*) userdata, "%s\n", t);
334 void avahi_cache_dump(AvahiCache *c, FILE *f) {
338 fprintf(f, ";;; CACHE DUMP FOLLOWS ;;;\n");
339 g_hash_table_foreach(c->hash_table, dump_callback, f);
342 gboolean avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e) {
349 g_get_current_time(&now);
351 age = avahi_timeval_diff(&now, &e->timestamp)/1000000;
353 /* g_message("age: %u, ttl/2: %u", age, e->record->ttl); */
355 return age >= e->record->ttl/2;
358 void avahi_cache_flush(AvahiCache *c) {
362 remove_entry(c, c->entries);