X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-core%2Fcache.c;h=4ba88b5f62ff17b7ce88afe9a160221075a72a77;hb=5ed5452823a5da7d7a7b36d5a42d3edc09f2342f;hp=0307b8e58cf18f877af45d2440f046f2aba3d293;hpb=c58379bde376cb2298fca14f83a86626f1b76f2f;p=catta diff --git a/avahi-core/cache.c b/avahi-core/cache.c index 0307b8e..4ba88b5 100644 --- a/avahi-core/cache.c +++ b/avahi-core/cache.c @@ -24,79 +24,102 @@ #endif #include +#include +#include + +#include +#include -#include "util.h" #include "cache.h" +#include "log.h" +#include "rr-util.h" + +#define AVAHI_CACHE_ENTRIES_MAX 500 static void remove_entry(AvahiCache *c, AvahiCacheEntry *e) { AvahiCacheEntry *t; - g_assert(c); - g_assert(e); + assert(c); + assert(e); -/* g_message("removing from cache: %p %p", c, e); */ +/* avahi_log_debug("removing from cache: %p %p", c, e); */ /* Remove from hash table */ - t = g_hash_table_lookup(c->hash_table, e->record->key); + t = avahi_hashmap_lookup(c->hashmap, e->record->key); AVAHI_LLIST_REMOVE(AvahiCacheEntry, by_key, t, e); if (t) - g_hash_table_replace(c->hash_table, t->record->key, t); + avahi_hashmap_replace(c->hashmap, t->record->key, t); else - g_hash_table_remove(c->hash_table, e->record->key); + avahi_hashmap_remove(c->hashmap, e->record->key); /* Remove from linked list */ AVAHI_LLIST_REMOVE(AvahiCacheEntry, entry, c->entries, e); if (e->time_event) - avahi_time_event_queue_remove(c->server->time_event_queue, e->time_event); - - avahi_subscription_notify(c->server, c->interface, e->record, AVAHI_SUBSCRIPTION_REMOVE); + avahi_time_event_free(e->time_event); + avahi_multicast_lookup_engine_notify(c->server->multicast_lookup_engine, c->interface, e->record, AVAHI_BROWSER_REMOVE); + avahi_record_unref(e->record); - g_free(e); + avahi_free(e); + + assert(c->n_entries-- >= 1); } AvahiCache *avahi_cache_new(AvahiServer *server, AvahiInterface *iface) { AvahiCache *c; - g_assert(server); + assert(server); - c = g_new(AvahiCache, 1); + if (!(c = avahi_new(AvahiCache, 1))) { + avahi_log_error(__FILE__": Out of memory."); + return NULL; /* OOM */ + } + c->server = server; c->interface = iface; - c->hash_table = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal); + + if (!(c->hashmap = avahi_hashmap_new((AvahiHashFunc) avahi_key_hash, (AvahiEqualFunc) avahi_key_equal, NULL, NULL))) { + avahi_log_error(__FILE__": Out of memory."); + avahi_free(c); + return NULL; /* OOM */ + } AVAHI_LLIST_HEAD_INIT(AvahiCacheEntry, c->entries); + c->n_entries = 0; + + c->last_rand_timestamp = 0; return c; } void avahi_cache_free(AvahiCache *c) { - g_assert(c); + assert(c); while (c->entries) remove_entry(c, c->entries); + assert(c->n_entries == 0); - g_hash_table_destroy(c->hash_table); + avahi_hashmap_free(c->hashmap); - g_free(c); + avahi_free(c); } -AvahiCacheEntry *avahi_cache_lookup_key(AvahiCache *c, AvahiKey *k) { - g_assert(c); - g_assert(k); +static AvahiCacheEntry *lookup_key(AvahiCache *c, AvahiKey *k) { + assert(c); + assert(k); - g_assert(!avahi_key_is_pattern(k)); + assert(!avahi_key_is_pattern(k)); - return g_hash_table_lookup(c->hash_table, k); + return avahi_hashmap_lookup(c->hashmap, k); } -gpointer avahi_cache_walk(AvahiCache *c, AvahiKey *pattern, AvahiCacheWalkCallback cb, gpointer userdata) { - gpointer ret; +void* avahi_cache_walk(AvahiCache *c, AvahiKey *pattern, AvahiCacheWalkCallback cb, void* userdata) { + void* ret; - g_assert(c); - g_assert(pattern); - g_assert(cb); + assert(c); + assert(pattern); + assert(cb); if (avahi_key_is_pattern(pattern)) { AvahiCacheEntry *e, *n; @@ -112,7 +135,7 @@ gpointer avahi_cache_walk(AvahiCache *c, AvahiKey *pattern, AvahiCacheWalkCallba } else { AvahiCacheEntry *e, *n; - for (e = avahi_cache_lookup_key(c, pattern); e; e = n) { + for (e = lookup_key(c, pattern); e; e = n) { n = e->by_key_next; if ((ret = cb(c, pattern, e, userdata))) @@ -123,10 +146,10 @@ gpointer avahi_cache_walk(AvahiCache *c, AvahiKey *pattern, AvahiCacheWalkCallba return NULL; } -static gpointer lookup_record_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void *userdata) { - g_assert(c); - g_assert(pattern); - g_assert(e); +static void* lookup_record_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void *userdata) { + assert(c); + assert(pattern); + assert(e); if (avahi_record_equal_no_ttl(e->record, userdata)) return e; @@ -134,239 +157,352 @@ static gpointer lookup_record_callback(AvahiCache *c, AvahiKey *pattern, AvahiCa return NULL; } -AvahiCacheEntry *avahi_cache_lookup_record(AvahiCache *c, AvahiRecord *r) { - g_assert(c); - g_assert(r); +static AvahiCacheEntry *lookup_record(AvahiCache *c, AvahiRecord *r) { + assert(c); + assert(r); return avahi_cache_walk(c, r->key, lookup_record_callback, r); } -static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, guint percent); +static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, unsigned percent); static void elapse_func(AvahiTimeEvent *t, void *userdata) { AvahiCacheEntry *e = userdata; +/* char *txt; */ + unsigned percent = 0; - g_assert(t); - g_assert(e); + assert(t); + assert(e); - if (e->state == AVAHI_CACHE_FINAL) { - remove_entry(e->cache, e); - g_message("Removing entry from cache due to expiration"); - } else { - guint percent = 0; - - switch (e->state) { - case AVAHI_CACHE_VALID: - e->state = AVAHI_CACHE_EXPIRY1; - percent = 85; - break; - - case AVAHI_CACHE_EXPIRY1: - e->state = AVAHI_CACHE_EXPIRY2; - percent = 90; - break; - case AVAHI_CACHE_EXPIRY2: - e->state = AVAHI_CACHE_EXPIRY3; - percent = 95; - break; - - case AVAHI_CACHE_EXPIRY3: - e->state = AVAHI_CACHE_FINAL; - percent = 100; - break; +/* txt = avahi_record_to_string(e->record); */ - default: - ; - } + switch (e->state) { - g_assert(percent > 0); + case AVAHI_CACHE_EXPIRY_FINAL: + case AVAHI_CACHE_POOF_FINAL: + case AVAHI_CACHE_GOODBYE_FINAL: + case AVAHI_CACHE_REPLACE_FINAL: - g_message("Requesting cache entry update at %i%%.", percent); + remove_entry(e->cache, e); + + e = NULL; +/* avahi_log_debug("Removing entry from cache due to expiration (%s)", txt); */ + break; + + case AVAHI_CACHE_VALID: + case AVAHI_CACHE_POOF: + e->state = AVAHI_CACHE_EXPIRY1; + percent = 85; + break; + + case AVAHI_CACHE_EXPIRY1: + e->state = AVAHI_CACHE_EXPIRY2; + percent = 90; + break; + case AVAHI_CACHE_EXPIRY2: + e->state = AVAHI_CACHE_EXPIRY3; + percent = 95; + break; + + case AVAHI_CACHE_EXPIRY3: + e->state = AVAHI_CACHE_EXPIRY_FINAL; + percent = 100; + break; + } - /* Request a cache update, if we are subscribed to this entry */ - if (avahi_is_subscribed(e->cache->server, e->record->key)) - avahi_interface_post_query(e->cache->interface, e->record->key, TRUE); + if (e) { + assert(percent > 0); + + /* Request a cache update if we are subscribed to this entry */ + if (avahi_querier_shall_refresh_cache(e->cache->interface, e->record->key)) + avahi_interface_post_query(e->cache->interface, e->record->key, 0, NULL); + /* Check again later */ next_expiry(e->cache, e, percent); + } + +/* avahi_free(txt); */ } static void update_time_event(AvahiCache *c, AvahiCacheEntry *e) { - g_assert(c); - g_assert(e); + assert(c); + assert(e); if (e->time_event) - avahi_time_event_queue_update(c->server->time_event_queue, e->time_event, &e->expiry); + avahi_time_event_update(e->time_event, &e->expiry); else - e->time_event = avahi_time_event_queue_add(c->server->time_event_queue, &e->expiry, elapse_func, e); + e->time_event = avahi_time_event_new(c->server->time_event_queue, &e->expiry, elapse_func, e); } -static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, guint percent) { - gulong usec; +static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, unsigned percent) { + AvahiUsec usec, left, right; + time_t now; + + assert(c); + assert(e); + assert(percent > 0 && percent <= 100); + + usec = (AvahiUsec) e->record->ttl * 10000; - g_assert(c); - g_assert(e); - g_assert(percent > 0 && percent <= 100); + left = usec * percent; + right = usec * (percent+2); /* 2% jitter */ - e->expiry = e->timestamp; + now = time(NULL); - usec = e->record->ttl * 10000; + if (now >= c->last_rand_timestamp + 10) { + c->last_rand = rand(); + c->last_rand_timestamp = now; + } - /* 2% jitter */ - usec = g_random_int_range(usec*percent, usec*(percent+2)); + usec = left + (AvahiUsec) ((double) (right-left) * c->last_rand / (RAND_MAX+1.0)); + + e->expiry = e->timestamp; + avahi_timeval_add(&e->expiry, usec); + +/* g_message("wake up in +%lu seconds", e->expiry.tv_sec - e->timestamp.tv_sec); */ - g_time_val_add(&e->expiry, usec); update_time_event(c, e); } -void avahi_cache_update(AvahiCache *c, AvahiRecord *r, gboolean unique, const AvahiAddress *a) { - AvahiCacheEntry *e, *t; - gchar *txt; +static void expire_in_one_second(AvahiCache *c, AvahiCacheEntry *e, AvahiCacheEntryState state) { + assert(c); + assert(e); - g_assert(c); - g_assert(r && r->ref >= 1); + e->state = state; + gettimeofday(&e->expiry, NULL); + avahi_timeval_add(&e->expiry, 1000000); /* 1s */ + update_time_event(c, e); +} - g_message("cache update: %s", (txt = avahi_record_to_string(r))); - g_free(txt); +void avahi_cache_update(AvahiCache *c, AvahiRecord *r, int cache_flush, const AvahiAddress *a) { +/* char *txt; */ + + assert(c); + assert(r && r->ref >= 1); - if (r->ttl == 0) { +/* txt = avahi_record_to_string(r); */ + if (r->ttl == 0) { /* This is a goodbye request */ - if ((e = avahi_cache_lookup_record(c, r))) { + AvahiCacheEntry *e; - e->state = AVAHI_CACHE_FINAL; - g_get_current_time(&e->timestamp); - e->expiry = e->timestamp; - g_time_val_add(&e->expiry, 1000000); /* 1s */ - update_time_event(c, e); - } + if ((e = lookup_record(c, r))) + expire_in_one_second(c, e, AVAHI_CACHE_GOODBYE_FINAL); } else { + AvahiCacheEntry *e = NULL, *first; + struct timeval now; + + gettimeofday(&now, NULL); /* This is an update request */ - if ((t = e = avahi_cache_lookup_key(c, r->key))) { - - if (unique) { - - /* For unique records, remove all entries but one */ - while (e->by_key_next) - remove_entry(c, e->by_key_next); - - } else { - - /* For non-unique record, look for exactly the same entry */ - for (; e; e = e->by_key_next) - if (avahi_record_equal_no_ttl(e->record, r)) - break; + if ((first = lookup_key(c, r->key))) { + + if (cache_flush) { + + /* For unique entries drop all entries older than one second */ + for (e = first; e; e = e->by_key_next) { + AvahiUsec t; + + t = avahi_timeval_diff(&now, &e->timestamp); + + if (t > 1000000) + expire_in_one_second(c, e, AVAHI_CACHE_REPLACE_FINAL); + } } + + /* Look for exactly the same entry */ + for (e = first; e; e = e->by_key_next) + if (avahi_record_equal_no_ttl(e->record, r)) + break; } if (e) { -/* g_message("found matching cache entry"); */ - - /* We are the first in the linked list so let's replace the hash table key with the new one */ +/* avahi_log_debug("found matching cache entry"); */ + + /* We need to update the hash table key if we replace the + * record */ if (e->by_key_prev == NULL) - g_hash_table_replace(c->hash_table, r->key, e); - - /* Notify subscribers */ - if (!avahi_record_equal_no_ttl(e->record, r)) - avahi_subscription_notify(c->server, c->interface, r, AVAHI_SUBSCRIPTION_CHANGE); + avahi_hashmap_replace(c->hashmap, r->key, e); /* Update the record */ avahi_record_unref(e->record); e->record = avahi_record_ref(r); + +/* avahi_log_debug("cache: updating %s", txt); */ } else { /* No entry found, therefore we create a new one */ -/* g_message("couldn't find matching cache entry"); */ - - e = g_new(AvahiCacheEntry, 1); +/* avahi_log_debug("cache: couldn't find matching cache entry for %s", txt); */ + + if (c->n_entries >= AVAHI_CACHE_ENTRIES_MAX) + return; + + if (!(e = avahi_new(AvahiCacheEntry, 1))) { + avahi_log_error(__FILE__": Out of memory"); + return; + } + e->cache = c; e->time_event = NULL; e->record = avahi_record_ref(r); /* Append to hash table */ - AVAHI_LLIST_PREPEND(AvahiCacheEntry, by_key, t, e); - g_hash_table_replace(c->hash_table, e->record->key, t); + AVAHI_LLIST_PREPEND(AvahiCacheEntry, by_key, first, e); + avahi_hashmap_replace(c->hashmap, e->record->key, first); /* Append to linked list */ AVAHI_LLIST_PREPEND(AvahiCacheEntry, entry, c->entries, e); + c->n_entries++; + /* Notify subscribers */ - avahi_subscription_notify(c->server, c->interface, e->record, AVAHI_SUBSCRIPTION_NEW); + avahi_multicast_lookup_engine_notify(c->server->multicast_lookup_engine, c->interface, e->record, AVAHI_BROWSER_NEW); } e->origin = *a; - g_get_current_time(&e->timestamp); + e->timestamp = now; next_expiry(c, e, 80); e->state = AVAHI_CACHE_VALID; + e->cache_flush = cache_flush; } + +/* avahi_free(txt); */ } -static gpointer drop_key_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, gpointer userdata) { - g_assert(c); - g_assert(pattern); - g_assert(e); +struct dump_data { + AvahiDumpCallback callback; + void* userdata; +}; - remove_entry(c, e); - return NULL; +static void dump_callback(void* key, void* data, void* userdata) { + AvahiCacheEntry *e = data; + AvahiKey *k = key; + struct dump_data *dump_data = userdata; + + assert(k); + assert(e); + assert(data); + + for (; e; e = e->by_key_next) { + char *t; + + if (!(t = avahi_record_to_string(e->record))) + continue; /* OOM */ + + dump_data->callback(t, dump_data->userdata); + avahi_free(t); + } } -void avahi_cache_drop_key(AvahiCache *c, AvahiKey *k) { - g_assert(c); - g_assert(k); +int avahi_cache_dump(AvahiCache *c, AvahiDumpCallback callback, void* userdata) { + struct dump_data data; + + assert(c); + assert(callback); + + callback(";;; CACHE DUMP FOLLOWS ;;;", userdata); + + data.callback = callback; + data.userdata = userdata; - avahi_cache_walk(c, k, drop_key_callback, NULL); + avahi_hashmap_foreach(c->hashmap, dump_callback, &data); + + return 0; } -void avahi_cache_drop_record(AvahiCache *c, AvahiRecord *r) { - AvahiCacheEntry *e; +int avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e) { + struct timeval now; + unsigned age; - g_assert(c); - g_assert(r); + assert(c); + assert(e); - if ((e = avahi_cache_lookup_record(c, r))) - remove_entry(c, e); -} + gettimeofday(&now, NULL); -static void func(gpointer key, gpointer data, gpointer userdata) { - AvahiCacheEntry *e = data; - AvahiKey *k = key; - gchar *t; + age = (unsigned) (avahi_timeval_diff(&now, &e->timestamp)/1000000); - g_assert(k); - g_assert(e); +/* avahi_log_debug("age: %lli, ttl/2: %u", age, e->record->ttl); */ - t = avahi_record_to_string(e->record); - fprintf((FILE*) userdata, "%s\n", t); - g_free(t); + return age >= e->record->ttl/2; } -void avahi_cache_dump(AvahiCache *c, FILE *f) { - g_assert(c); - g_assert(f); +void avahi_cache_flush(AvahiCache *c) { + assert(c); - fprintf(f, ";;; CACHE DUMP FOLLOWS ;;;\n"); - g_hash_table_foreach(c->hash_table, func, f); + while (c->entries) + remove_entry(c, c->entries); } -gboolean avahi_cache_entry_half_ttl(AvahiCache *c, AvahiCacheEntry *e) { - GTimeVal now; - guint age; +/*** Passive observation of failure ***/ + +static void* start_poof_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void *userdata) { + AvahiAddress *a = userdata; + + assert(c); + assert(pattern); + assert(e); + assert(a); - g_assert(c); - g_assert(e); + switch (e->state) { + case AVAHI_CACHE_VALID: - g_get_current_time(&now); + /* The entry was perfectly valid till, now, so let's enter + * POOF mode */ - age = avahi_timeval_diff(&now, &e->timestamp)/1000000; + e->state = AVAHI_CACHE_POOF; + e->poof_address = *a; + + break; + + case AVAHI_CACHE_POOF: - g_message("age: %u, ttl/2: %u", age, e->record->ttl); + /* This is the second time we got no response, so let's + * fucking remove this entry. */ + + expire_in_one_second(c, e, AVAHI_CACHE_POOF_FINAL); + break; + + default: + ; + } - return age >= e->record->ttl/2; + return NULL; } + +void avahi_cache_start_poof(AvahiCache *c, AvahiKey *key, const AvahiAddress *a) { + assert(c); + assert(key); + + avahi_cache_walk(c, key, start_poof_callback, (void*) a); +} + +void avahi_cache_stop_poof(AvahiCache *c, AvahiRecord *record, const AvahiAddress *a) { + AvahiCacheEntry *e; + + assert(c); + assert(record); + assert(a); + + if (!(e = lookup_record(c, record))) + return; + + /* This function is called for each response suppression + record. If the matching cache entry is in POOF state and the + query address is the same, we put it back into valid mode */ + + if (e->state == AVAHI_CACHE_POOF || e->state == AVAHI_CACHE_POOF_FINAL) + if (avahi_address_cmp(a, &e->poof_address) == 0) { + e->state = AVAHI_CACHE_VALID; + next_expiry(c, e, 80); + } +} + + +