Use monotonic clock for timeouts
[project/mdnsd.git] / cache.c
diff --git a/cache.c b/cache.c
index 190290a9afa3ab7a9343355051d82dea82866646..11d359dff9c512c3608058bb150ea039d4a60181 100644 (file)
--- a/cache.c
+++ b/cache.c
@@ -61,9 +61,9 @@ cache_service_free(struct cache_service *s)
 }
 
 static int
-cache_is_expired(time_t t, uint32_t ttl)
+cache_is_expired(time_t t, uint32_t ttl, int frac)
 {
-       if (time(NULL) - t >= ttl)
+       if (monotonic_time() - t >= ttl * frac / 100)
                return 1;
 
        return 0;
@@ -76,14 +76,20 @@ cache_gc_timer(struct uloop_timeout *timeout)
        struct cache_service *s, *t;
 
        avl_for_each_element_safe(&records, r, avl, p)
-               if (cache_is_expired(r->time, r->ttl))
+               if (cache_is_expired(r->time, r->ttl, 100))
                        cache_record_free(r);
 
        avl_for_each_element_safe(&services, s, avl, t) {
                if (!s->host)
                        continue;
-               if (cache_is_expired(s->time, s->ttl))
+               if (!cache_is_expired(s->time, s->ttl, s->refresh))
+                       continue;
+               if (s->refresh >= 100) {
                        cache_service_free(s);
+                       continue;
+               }
+               s->refresh += 50;
+               dns_send_question(s->iface, s->entry, TYPE_PTR, 1);
        }
 
        uloop_timeout_set(timeout, 10000);
@@ -100,16 +106,18 @@ cache_init(void)
        return 0;
 }
 
-void cache_cleanup(void)
+void cache_cleanup(struct interface *iface)
 {
        struct cache_record *r, *p;
        struct cache_service *s, *t;
 
-       avl_for_each_element_safe(&records, r, avl, p)
-               cache_record_free(r);
-
        avl_for_each_element_safe(&services, s, avl, t)
-               cache_service_free(s);
+               if (!iface || iface == s->iface)
+                       cache_service_free(s);
+
+       avl_for_each_element_safe(&records, r, avl, p)
+               if (!iface || iface == r->iface)
+                       cache_record_free(r);
 }
 
 void
@@ -132,16 +140,21 @@ cache_service(struct interface *iface, char *entry, int hlen, int ttl)
        char *type;
 
        avl_for_each_element_safe(&services, s, avl, t)
-               if (!strcmp(s->entry, entry))
+               if (!strcmp(s->entry, entry)) {
+                       s->refresh = 50;
+                       s->time = monotonic_time();
                        return s;
+               }
 
        s = calloc_a(sizeof(*s),
                &entry_buf, strlen(entry) + 1,
                &host_buf, hlen ? hlen + 1 : 0);
 
        s->avl.key = s->entry = strcpy(entry_buf, entry);
-       s->time = time(NULL);
+       s->time = monotonic_time();
        s->ttl = ttl;
+       s->iface = iface;
+       s->refresh = 50;
 
        if (hlen)
                s->host = strncpy(host_buf, s->entry, hlen);
@@ -298,7 +311,7 @@ cache_answer(struct interface *iface, uint8_t *base, int blen, char *name, struc
        if (r) {
                if (!a->ttl) {
                        DBG(1, "D -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
-                       r->time = time(0) + 1 - r->ttl;
+                       r->time = monotonic_time() + 1 - r->ttl;
                } else {
                        r->ttl = a->ttl;
                        DBG(1, "A -> %s %s ttl:%d\n", dns_type_string(r->type), r->record, r->ttl);
@@ -319,7 +332,8 @@ cache_answer(struct interface *iface, uint8_t *base, int blen, char *name, struc
        r->ttl = a->ttl;
        r->port = port;
        r->rdlength = dlen;
-       r->time = time(NULL);
+       r->time = monotonic_time();
+       r->iface = iface;
 
        if (tlen)
                r->txt = memcpy(txt_ptr, rdata_buffer, tlen);