]> git.meshlink.io Git - catta/blobdiff - dns.c
add client part of known answer suppresion
[catta] / dns.c
diff --git a/dns.c b/dns.c
index a33f1ab12303073c71236c2abe10f8c9ec476469..bed7344d81d4fe5341d84567d63f2052eb299825 100644 (file)
--- a/dns.c
+++ b/dns.c
@@ -131,7 +131,7 @@ guint8 *flx_dns_packet_append_bytes(flxDnsPacket  *p, gconstpointer b, guint l)
     g_assert(p);
     g_assert(b);
     g_assert(l);
-    
+
     if (!(d = flx_dns_packet_extend(p, l)))
         return NULL;
 
@@ -373,9 +373,14 @@ flxRecord* flx_dns_packet_consume_record(flxDnsPacket *p, gboolean *ret_cache_fl
         }
             
         default:
-            if (!(data = flx_dns_packet_get_rptr(p)) ||
-                flx_dns_packet_skip(p, rdlength) < 0)
-                return NULL;
+
+            if (rdlength > 0) {
+
+                if (!(data = flx_dns_packet_get_rptr(p)) ||
+                    flx_dns_packet_skip(p, rdlength) < 0)
+                    return NULL;
+            } else
+                data = NULL;
 
             break;
     }
@@ -404,29 +409,37 @@ flxKey* flx_dns_packet_consume_key(flxDnsPacket *p) {
 
 guint8* flx_dns_packet_append_key(flxDnsPacket *p, flxKey *k) {
     guint8 *t;
+    guint size;
     
     g_assert(p);
     g_assert(k);
 
+    size = p->size;
+    
     if (!(t = flx_dns_packet_append_name(p, k->name)) ||
         !flx_dns_packet_append_uint16(p, k->type) ||
-        !flx_dns_packet_append_uint16(p, k->class))
+        !flx_dns_packet_append_uint16(p, k->class)) {
+        p->size = size;
         return NULL;
+    }
 
     return t;
 }
 
 guint8* flx_dns_packet_append_record(flxDnsPacket *p, flxRecord *r, gboolean cache_flush) {
     guint8 *t;
+    guint size;
 
     g_assert(p);
     g_assert(r);
 
+    size = p->size;
+
     if (!(t = flx_dns_packet_append_name(p, r->key->name)) ||
         !flx_dns_packet_append_uint16(p, r->key->type) ||
         !flx_dns_packet_append_uint16(p, cache_flush ? (r->key->class | MDNS_CACHE_FLUSH) : (r->key->class &~ MDNS_CACHE_FLUSH)) ||
         !flx_dns_packet_append_uint32(p, r->ttl))
-        return NULL;
+        goto fail;
 
     switch (r->key->type) {
         
@@ -438,9 +451,9 @@ guint8* flx_dns_packet_append_record(flxDnsPacket *p, flxRecord *r, gboolean cac
             memcpy(ptr_name, r->data, r->size);
             ptr_name[r->size] = 0;
             
-            if (!flx_dns_packet_append_uint16(p, strlen(ptr_name)) ||
+            if (!flx_dns_packet_append_uint16(p, strlen(ptr_name)+1) ||
                 !flx_dns_packet_append_name(p, ptr_name))
-                return NULL;
+                goto fail;
 
             break;
         }
@@ -452,19 +465,30 @@ guint8* flx_dns_packet_append_record(flxDnsPacket *p, flxRecord *r, gboolean cac
             memcpy(name, r->data+6, r->size-6);
             name[r->size-6] = 0;
 
-            if (!flx_dns_packet_append_uint16(p, strlen(name+6)) ||
+            if (!flx_dns_packet_append_uint16(p, strlen(name+6)+1+6) ||
                 !flx_dns_packet_append_bytes(p, r->data, 6) ||
                 !flx_dns_packet_append_name(p, name))
-                return NULL;
+                goto fail;
 
             break;
         }
 
         default:
             if (!flx_dns_packet_append_uint16(p, r->size) ||
-                !flx_dns_packet_append_bytes(p, r->data, r->size))
-                return NULL;
+                (r->size != 0 && !flx_dns_packet_append_bytes(p, r->data, r->size)))
+                goto fail;
     }
 
     return t;
+
+
+fail:
+    p->size = size;
+    return NULL;
+}
+
+gboolean flx_dns_packet_is_empty(flxDnsPacket *p) {
+    g_assert(p);
+
+    return p->size <= FLX_DNS_PACKET_HEADER_SIZE;
 }