]> git.meshlink.io Git - catta/blobdiff - avahi-core/cache.c
don't allow registration of address records for invalid host names according to avahi...
[catta] / avahi-core / cache.c
index ef7293d9a7f3601297c512259451586449480e15..3a8f849ba98c9f41e13e16b067f10a3d7dc72c54 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 500
+#define AVAHI_CACHE_ENTRIES_MAX 500
 
 static void remove_entry(AvahiCache *c, AvahiCacheEntry *e) {
     AvahiCacheEntry *t;
@@ -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;
 }
@@ -210,11 +214,9 @@ static void elapse_func(AvahiTimeEvent *t, void *userdata) {
 
         assert(percent > 0);
 
-        /* Request a cache update, if we are subscribed to this entry */
-        if (avahi_querier_exists(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);
@@ -236,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);
@@ -246,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);
@@ -331,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))) {