]> git.meshlink.io Git - catta/blobdiff - avahi-core/cache.c
l10n: Updated Italian (it) translation to 100%
[catta] / avahi-core / cache.c
index 924b8c958d83c84e3aa2b1a2b4b0c92f3e092bbe..9d97b6cd0a55ab01fbb609ba164529e848112cbf 100644 (file)
 
 #include <string.h>
 #include <stdlib.h>
+#include <time.h>
 
 #include <avahi-common/timeval.h>
 #include <avahi-common/malloc.h>
 
 #include "cache.h"
 #include "log.h"
+#include "rr-util.h"
 
-#define AVAHI_MAX_CACHE_ENTRIES 200
+#define AVAHI_CACHE_ENTRIES_MAX 500
 
 static void remove_entry(AvahiCache *c, AvahiCacheEntry *e) {
     AvahiCacheEntry *t;
@@ -56,8 +58,8 @@ static void remove_entry(AvahiCache *c, AvahiCacheEntry *e) {
     if (e->time_event)
         avahi_time_event_free(e->time_event);
 
-    avahi_browser_notify(c->server, c->interface, e->record, AVAHI_BROWSER_REMOVE);
-
+    avahi_multicast_lookup_engine_notify(c->server->multicast_lookup_engine, c->interface, e->record, AVAHI_BROWSER_REMOVE);
+    
     avahi_record_unref(e->record);
     
     avahi_free(e);
@@ -85,6 +87,8 @@ AvahiCache *avahi_cache_new(AvahiServer *server, AvahiInterface *iface) {
 
     AVAHI_LLIST_HEAD_INIT(AvahiCacheEntry, c->entries);
     c->n_entries = 0;
+
+    c->last_rand_timestamp = 0;
     
     return c;
 }
@@ -101,7 +105,7 @@ void avahi_cache_free(AvahiCache *c) {
     avahi_free(c);
 }
 
-AvahiCacheEntry *avahi_cache_lookup_key(AvahiCache *c, AvahiKey *k) {
+static AvahiCacheEntry *lookup_key(AvahiCache *c, AvahiKey *k) {
     assert(c);
     assert(k);
 
@@ -131,7 +135,7 @@ void* avahi_cache_walk(AvahiCache *c, AvahiKey *pattern, AvahiCacheWalkCallback
     } 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)))
@@ -153,7 +157,7 @@ static void* lookup_record_callback(AvahiCache *c, AvahiKey *pattern, AvahiCache
     return NULL;
 }
 
-AvahiCacheEntry *avahi_cache_lookup_record(AvahiCache *c, AvahiRecord *r) {
+static AvahiCacheEntry *lookup_record(AvahiCache *c, AvahiRecord *r) {
     assert(c);
     assert(r);
 
@@ -165,52 +169,58 @@ 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;
     
     assert(t);
     assert(e);
 
 /*     txt = avahi_record_to_string(e->record); */
 
-    if (e->state == AVAHI_CACHE_FINAL) {
-        remove_entry(e->cache, e);
+    switch (e->state) {
+
+        case AVAHI_CACHE_EXPIRY_FINAL:
+        case AVAHI_CACHE_POOF_FINAL:
+        case AVAHI_CACHE_GOODBYE_FINAL:
+        case AVAHI_CACHE_REPLACE_FINAL:
+
+            remove_entry(e->cache, e);
+            
+            e = NULL;
 /*         avahi_log_debug("Removing entry from cache due to expiration (%s)", txt); */
-    } else {
-        unsigned 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;
+            break;
+            
+        case AVAHI_CACHE_VALID:
+        case AVAHI_CACHE_POOF:
+            e->state = AVAHI_CACHE_EXPIRY1;
+            percent = 85;
+            break;
                 
-            case AVAHI_CACHE_EXPIRY3:
-                e->state = AVAHI_CACHE_FINAL;
-                percent = 100;
-                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;
+    }
 
-            default:
-                ;
-        }
+    if (e) {
 
         assert(percent > 0);
 
-        /* Request a cache update, if we are subscribed to this entry */
-        if (avahi_is_subscribed(e->cache->server, e->cache->interface, e->record->key)) {
-/*             avahi_log_debug("Requesting cache entry update at %i%% for %s.", percent, txt);   */
-            avahi_interface_post_query(e->cache->interface, e->record->key, 1);
-        }
-
+        /* 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); */
@@ -228,6 +238,7 @@ static void update_time_event(AvahiCache *c, AvahiCacheEntry *e) {
 
 static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, unsigned percent) {
     AvahiUsec usec, left, right;
+    time_t now;
     
     assert(c);
     assert(e);
@@ -238,7 +249,14 @@ static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, unsigned percent) {
     left = usec * percent;
     right = usec * (percent+2); /* 2% jitter */
 
-    usec = left + (AvahiUsec) ((double) (right-left) * rand() / (RAND_MAX+1.0));
+    now = time(NULL);
+
+    if (now >= c->last_rand_timestamp + 10) {
+        c->last_rand = rand();
+        c->last_rand_timestamp = now;
+    }
+
+    usec = left + (AvahiUsec) ((double) (right-left) * c->last_rand / (RAND_MAX+1.0));
     
     e->expiry = e->timestamp;
     avahi_timeval_add(&e->expiry, usec);
@@ -248,11 +266,11 @@ static void next_expiry(AvahiCache *c, AvahiCacheEntry *e, unsigned percent) {
     update_time_event(c, e);
 }
 
-static void expire_in_one_second(AvahiCache *c, AvahiCacheEntry *e) {
+static void expire_in_one_second(AvahiCache *c, AvahiCacheEntry *e, AvahiCacheEntryState state) {
     assert(c);
     assert(e);
     
-    e->state = AVAHI_CACHE_FINAL;
+    e->state = state;
     gettimeofday(&e->expiry, NULL);
     avahi_timeval_add(&e->expiry, 1000000); /* 1s */
     update_time_event(c, e);
@@ -271,8 +289,8 @@ void avahi_cache_update(AvahiCache *c, AvahiRecord *r, int cache_flush, const Av
 
         AvahiCacheEntry *e;
 
-        if ((e = avahi_cache_lookup_record(c, r)))
-            expire_in_one_second(c, e);
+        if ((e = lookup_record(c, r)))
+            expire_in_one_second(c, e, AVAHI_CACHE_GOODBYE_FINAL);
 
     } else {
         AvahiCacheEntry *e = NULL, *first;
@@ -282,7 +300,7 @@ void avahi_cache_update(AvahiCache *c, AvahiRecord *r, int cache_flush, const Av
 
         /* This is an update request */
 
-        if ((first = avahi_cache_lookup_key(c, r->key))) {
+        if ((first = lookup_key(c, r->key))) {
             
             if (cache_flush) {
 
@@ -293,7 +311,7 @@ void avahi_cache_update(AvahiCache *c, AvahiRecord *r, int cache_flush, const Av
                     t = avahi_timeval_diff(&now, &e->timestamp);
 
                     if (t > 1000000)
-                        expire_in_one_second(c, e);
+                        expire_in_one_second(c, e, AVAHI_CACHE_REPLACE_FINAL);
                 }
             }
                 
@@ -323,7 +341,7 @@ void avahi_cache_update(AvahiCache *c, AvahiRecord *r, int cache_flush, const Av
             
 /*             avahi_log_debug("cache: couldn't find matching cache entry for %s", txt);   */
 
-            if (c->n_entries >= AVAHI_MAX_CACHE_ENTRIES)
+            if (c->n_entries >= AVAHI_CACHE_ENTRIES_MAX)
                 return;
 
             if (!(e = avahi_new(AvahiCacheEntry, 1))) {
@@ -345,7 +363,7 @@ void avahi_cache_update(AvahiCache *c, AvahiRecord *r, int cache_flush, const Av
             c->n_entries++;
 
             /* Notify subscribers */
-            avahi_browser_notify(c->server, c->interface, e->record, AVAHI_BROWSER_NEW);
+            avahi_multicast_lookup_engine_notify(c->server->multicast_lookup_engine, c->interface, e->record, AVAHI_BROWSER_NEW);
         } 
         
         e->origin = *a;
@@ -421,3 +439,81 @@ void avahi_cache_flush(AvahiCache *c) {
     while (c->entries)
         remove_entry(c, c->entries);
 }
+
+/*** Passive observation of failure ***/
+
+static void* start_poof_callback(AvahiCache *c, AvahiKey *pattern, AvahiCacheEntry *e, void *userdata) {
+    AvahiAddress *a = userdata;
+    struct timeval now;
+
+    assert(c);
+    assert(pattern);
+    assert(e);
+    assert(a);
+
+    gettimeofday(&now, NULL);
+
+    switch (e->state) {
+        case AVAHI_CACHE_VALID:
+
+            /* The entry was perfectly valid till, now, so let's enter
+             * POOF mode */
+
+            e->state = AVAHI_CACHE_POOF;
+            e->poof_address = *a;
+            e->poof_timestamp = now;
+            e->poof_num = 0;
+
+            break;
+
+        case AVAHI_CACHE_POOF:
+            if (avahi_timeval_diff(&now, &e->poof_timestamp) < 1000000)
+              break;
+
+            e->poof_timestamp = now;
+            e->poof_address = *a;
+            e->poof_num ++;
+
+            /* This is the 4th time we got no response, so let's
+             * fucking remove this entry. */
+            if (e->poof_num > 3)
+              expire_in_one_second(c, e, AVAHI_CACHE_POOF_FINAL);
+            break;
+
+        default:
+            ;
+    }
+    
+    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);
+        }
+}
+
+
+