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 void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean unique, const AvahiAddress *a) {
224 AvahiCacheEntry *e, *t;
228 g_assert(r && r->ref >= 1);
230 g_message("cache update: %s", (txt = avahi_record_to_string(r)));
235 /* This is a goodbye request */
237 if ((e = avahi_cache_lookup_record(c, r))) {
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);
248 /* This is an update request */
250 if ((t = e = avahi_cache_lookup_key(c, r->key))) {
254 /* For unique records, remove all entries but one */
255 while (e->by_key_next)
256 remove_entry(c, e->by_key_next);
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))
269 /* g_message("found matching cache entry"); */
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);
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);
279 /* Update the record */
280 avahi_record_unref(e->record);
281 e->record = avahi_record_ref(r);
284 /* No entry found, therefore we create a new one */
286 /* g_message("couldn't find matching cache entry"); */
288 e = g_new(AvahiCacheEntry, 1);
290 e->time_event = NULL;
291 e->record = avahi_record_ref(r);
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);
297 /* Append to linked list */
298 AVAHI_LLIST_PREPEND(AvahiCacheEntry, entry, c->entries, e);
300 /* Notify subscribers */
301 avahi_subscription_notify(c->server, c->interface, e->record, AVAHI_SUBSCRIPTION_NEW);
305 g_get_current_time(&e->timestamp);
306 next_expiry(c, e, 80);
307 e->state = AVAHI_CACHE_VALID;
311 static gpointer drop_key_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, gpointer userdata) {
320 void avahi_cache_drop_key(AvahiCache *c, AvahiKey *k) {
324 avahi_cache_walk(c, k, drop_key_callback, NULL);
327 void avahi_cache_drop_record(AvahiCache *c, AvahiRecord *r) {
333 if ((e = avahi_cache_lookup_record(c, r)))
337 static void func(gpointer key, gpointer data, gpointer userdata) {
338 AvahiCacheEntry *e = data;
345 t = avahi_record_to_string(e->record);
346 fprintf((FILE*) userdata, "%s\n", t);
350 void avahi_cache_dump(AvahiCache *c, FILE *f) {
354 fprintf(f, ";;; CACHE DUMP FOLLOWS ;;;\n");
355 g_hash_table_foreach(c->hash_table, func, f);
358 gboolean avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e) {
365 g_get_current_time(&now);
367 age = avahi_timeval_diff(&now, &e->timestamp)/1000000;
369 g_message("age: %u, ttl/2: %u", age, e->record->ttl);
371 return age >= e->record->ttl/2;