From 756cb869640fa3df0b9eb8e50ea593adfbae9e6a Mon Sep 17 00:00:00 2001 From: Wouter Wijngaards Date: Tue, 1 May 2007 10:18:37 +0000 Subject: [PATCH] Tests for compression, and decompression in query section fix. git-svn-id: file:///svn/unbound/trunk@270 be551aaa-1e26-0410-a405-d3ace91eadb9 --- doc/Changelog | 5 ++++ testcode/unitmsgparse.c | 7 +++++- testdata/test_packets.5 | 56 +++++++++++++++++++++++++++++++++++++++++ util/data/msgparse.c | 2 +- util/data/msgreply.c | 21 +++++----------- 5 files changed, 74 insertions(+), 17 deletions(-) create mode 100644 testdata/test_packets.5 diff --git a/doc/Changelog b/doc/Changelog index 2315a69f5..75ec347f0 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -1,3 +1,8 @@ +1 May 2007: Wouter + - decompress query section, extremely lenient acceptance. + But only for answers from other servers, not for plain queries. + - compression and decompression test cases. + 27 April 2007: Wouter - removed iov usage, it is not good for dns message encoding. - owner name compression more optimal. diff --git a/testcode/unitmsgparse.c b/testcode/unitmsgparse.c index cbabbcb38..ffa342ca6 100644 --- a/testcode/unitmsgparse.c +++ b/testcode/unitmsgparse.c @@ -255,7 +255,9 @@ testpkt(ldns_buffer* pkt, struct alloc_cache* alloc, ldns_buffer* out, hex_to_buf(pkt, hex); memmove(&id, ldns_buffer_begin(pkt), sizeof(id)); - memmove(&flags, ldns_buffer_at(pkt, 2), sizeof(flags)); + if(ldns_buffer_limit(pkt) < 2) + flags = 0; + else memmove(&flags, ldns_buffer_at(pkt, 2), sizeof(flags)); flags = ntohs(flags); ret = reply_info_parse(pkt, alloc, &qi, &rep); if(ret != 0) { @@ -366,6 +368,7 @@ testfromdrillfile(ldns_buffer* pkt, struct alloc_cache* alloc, FILE* in = fopen(fname, "r"); char buf[102400]; char* np = buf; + buf[0]=0; if(!in) { perror("fname"); return; @@ -378,6 +381,7 @@ testfromdrillfile(ldns_buffer* pkt, struct alloc_cache* alloc, testpkt(pkt, alloc, out, buf); /* set for new entry */ np = buf; + buf[0]=0; continue; } if(np[0] == ';') /* comment */ @@ -405,6 +409,7 @@ void msgparse_test() testfromfile(pkt, &alloc, out, "testdata/test_packets.3"); /* like from drill -w - */ testfromdrillfile(pkt, &alloc, out, "testdata/test_packets.4"); + testfromdrillfile(pkt, &alloc, out, "testdata/test_packets.5"); /* cleanup */ alloc_clear(&alloc); diff --git a/testdata/test_packets.5 b/testdata/test_packets.5 new file mode 100644 index 000000000..1845faab5 --- /dev/null +++ b/testdata/test_packets.5 @@ -0,0 +1,56 @@ +; Hand made test packets. +; By Wouter Wijngaards. +; These DNS packets contain interesting compression cases. +; +;-- next packet -- +; 0. A valid packet (handmade) +; id flags qd an ns ar -- header +4242 0000 0001 0001 0000 0000 +; query: qname example.com. qtype A(1) qclass IN(1) +07 6578616d706c65 03 636f6d 00 0001 0001 +; answer: example.com type class ttl rdatalen 10.x address. +07 6578616d706c65 03 636f6d 00 0001 0001 00000101 0004 0a203040 + +;-- next packet -- +; 0b. correct compression from answer to query. +4242 0000 0001 0001 0000 0000 +07 6578616d706c65 03 636f6d 00 0001 0001 +c00c 0001 0001 00000101 0004 0a203040 + +;-- next packet -- +; 1. Compression from query to answer. +4242 0000 0001 0001 0000 0000 +c012 0001 0001 +07 6578616d706c65 03 636f6d 00 0001 0001 00000101 0004 0a203040 + +;-- next packet -- +; 2. Compression loop answer 1 to answer 2. +4242 0000 0001 0002 0000 0000 +07 6578616d706c65 03 636f6d 00 0001 0001 +c02d 0001 0001 00000101 0004 0a203040 +07 6578616d706c65 03 636f6d 00 0001 0001 00000101 0004 0a203050 + +;-- next packet -- +; 2b. Compression loop answer 2 to answer 1. +4242 0000 0001 0002 0000 0000 +07 6578616d706c65 03 636f6d 00 0001 0001 +07 6578616d706c65 03 636f6d 00 0001 0001 00000101 0004 0a203050 +c01d 0001 0001 00000101 0004 0a203040 + +;-- next packet -- +; 3. Compression loop to self (in answer section). +4242 0000 0001 0001 0000 0000 +07 6578616d706c65 03 636f6d 00 0001 0001 +c01d 0001 0001 00000101 0004 0a203040 + +;-- next packet -- +; 4. bad compression pointer - to header. +4242 0000 0001 0001 0000 0000 +07 6578616d706c65 03 636f6d 00 0001 0001 +c004 0001 0001 00000101 0004 0a203040 + +;-- next packet -- +; 5. bad compression pointer - exceeds packet. +4242 0000 0001 0001 0000 0000 +07 6578616d706c65 03 636f6d 00 0001 0001 +c0bb 0001 0001 00000101 0004 0a203040 diff --git a/util/data/msgparse.c b/util/data/msgparse.c index 8154d9f7e..6a0176c51 100644 --- a/util/data/msgparse.c +++ b/util/data/msgparse.c @@ -528,7 +528,7 @@ parse_query_section(ldns_buffer* pkt, struct msg_parse* msg) if(ldns_buffer_remaining(pkt) <= 0) return LDNS_RCODE_FORMERR; msg->qname = ldns_buffer_current(pkt); - if((msg->qname_len = query_dname_len(pkt)) == 0) + if((msg->qname_len = pkt_dname_len(pkt)) == 0) return LDNS_RCODE_FORMERR; if(ldns_buffer_remaining(pkt) < sizeof(uint16_t)*2) return LDNS_RCODE_FORMERR; diff --git a/util/data/msgreply.c b/util/data/msgreply.c index fb7d78e32..1f0aaf0b7 100644 --- a/util/data/msgreply.c +++ b/util/data/msgreply.c @@ -50,24 +50,15 @@ #include "util/region-allocator.h" #include "util/data/msgparse.h" -/** copy and allocate an uncompressed dname. */ -static uint8_t* -copy_uncompr(uint8_t* dname, size_t len) -{ - uint8_t* p = (uint8_t*)malloc(len); - if(!p) - return 0; - memmove(p, dname, len); - return p; -} - /** allocate qinfo, return 0 on error. */ static int -parse_create_qinfo(struct msg_parse* msg, struct query_info* qinf) +parse_create_qinfo(ldns_buffer* pkt, struct msg_parse* msg, + struct query_info* qinf) { if(msg->qname) { - if(!(qinf->qname = copy_uncompr(msg->qname, msg->qname_len))) - return 0; + qinf->qname = (uint8_t*)malloc(msg->qname_len); + if(!qinf->qname) return 0; + dname_pkt_copy(pkt, qinf->qname, msg->qname); } else qinf->qname = 0; qinf->qnamesize = msg->qname_len; qinf->qtype = msg->qtype; @@ -298,7 +289,7 @@ parse_create_msg(ldns_buffer* pkt, struct msg_parse* msg, { int ret; log_assert(pkt && msg); - if(!parse_create_qinfo(msg, qinf)) + if(!parse_create_qinfo(pkt, msg, qinf)) return LDNS_RCODE_SERVFAIL; if(!parse_create_repinfo(msg, rep)) return LDNS_RCODE_SERVFAIL; -- 2.47.2