cache: fix host length calculation
authorFelix Fietkau <nbd@openwrt.org>
Tue, 3 Jun 2014 23:17:46 +0000 (01:17 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Tue, 3 Jun 2014 23:17:49 +0000 (01:17 +0200)
The previous version was returning negative values for some records due
to unchecked name matching

Signed-off-by: Felix Fietkau <nbd@openwrt.org>
cache.c

diff --git a/cache.c b/cache.c
index b5653dd4f02bac38d6598498817da82409e225d4..be26fb6e1b4494d52f7d453bc01d27993fefde78 100644 (file)
--- a/cache.c
+++ b/cache.c
@@ -248,6 +248,7 @@ cache_answer(struct uloop_fd *u, uint8_t *base, int blen, char *name, struct dns
        char *p = NULL;
        char *name_buf;
        void *rdata_ptr, *txt_ptr;
+       int host_len = 0;
 
        if (!(a->class & CLASS_IN))
                return;
@@ -268,15 +269,11 @@ cache_answer(struct uloop_fd *u, uint8_t *base, int blen, char *name, struct dns
 
                rdlength = strlen(rdata_buffer);
 
-               if (!strcmp(C_DNS_SD, name)) {
-                       cache_entry(u, rdata_buffer, 0, a->ttl);
-                       return;
-               }
-
-               if ((rdlength < nlen) && (rdlength - nlen - 1 > 0))
-                       return;
+               if (strcmp(C_DNS_SD, name) != 0 &&
+                   nlen + 1 < rdlength && !strcmp(rdata_buffer + rdlength - nlen, name))
+                       host_len = rdlength - nlen - 1;
 
-               cache_entry(u, rdata_buffer, rdlength - nlen - 1, a->ttl);
+               cache_entry(u, rdata_buffer, host_len, a->ttl);
                return;
 
        case TYPE_SRV: