From 72f83260778805b22ab85018bba62de0ac5ba660 Mon Sep 17 00:00:00 2001 From: Bjorn Andersson Date: Thu, 24 Aug 2006 22:04:27 +0000 Subject: [PATCH] readname-tests --- dns.c | 3 +-- dns.h | 2 ++ read.c | 22 +++++++++++----- test.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 84 insertions(+), 23 deletions(-) diff --git a/dns.c b/dns.c index 6bc8bfb..a5ed106 100644 --- a/dns.c +++ b/dns.c @@ -46,7 +46,6 @@ static int host2dns(const char *, char *, int); static int dns_write(int, int, char *, int, char); static void dns_query(int, int, char *, int); -static int dns_parse_reply(char *, int, char *, int); struct sockaddr_in peer; char topdomain[256]; @@ -284,7 +283,7 @@ dns_read(int fd, char *buf, int buflen) return dns_parse_reply(buf, buflen, packet, r); } -static int +int dns_parse_reply(char *outbuf, int buflen, char *packet, int packetlen) { int rv; diff --git a/dns.h b/dns.h index c64f58c..f3807a1 100644 --- a/dns.h +++ b/dns.h @@ -37,5 +37,7 @@ int dnsd_hasack(); void dnsd_forceack(int); void dnsd_queuepacket(const char *, const int); +int dns_parse_reply(char *, int, char *, int); + #endif /* _DNS_H_ */ diff --git a/read.c b/read.c index 412e43d..028b0ca 100644 --- a/read.c +++ b/read.c @@ -16,35 +16,37 @@ #include -int -readname(char *packet, char **src, char *dst, size_t length) +static int +readname_loop(char *packet, char **src, char *dst, size_t length, size_t loop) { char *dummy; int len; char *p; char c; + if (loop <= 0) + return 0; + len = 0; p = *src; - while(*p && len < length) { + while(*p && len < length - 2) { c = *p++; - len++; /* is this a compressed label? */ if((c & 0xc0) == 0xc0) { dummy = packet + (((p[-1] & 0x3f) << 8) | p[0]); - readname(packet, &dummy, dst, length - len); + len += readname_loop(packet, &dummy, dst, length - len, loop - 1); break; } - while(c && len < length) { + while(c && len < length - 2) { *dst++ = *p++; len++; c--; } - if (*p != 0) + if (*p != 0 && len < length - 2) *dst++ = '.'; else *dst++ = '\0'; @@ -54,6 +56,12 @@ readname(char *packet, char **src, char *dst, size_t length) return strlen(dst); } +int +readname(char *packet, char **src, char *dst, size_t length) +{ + return readname_loop(packet, src, dst, length, 10); +} + int readshort(char *packet, char **src, short *dst) { diff --git a/test.c b/test.c index 57d4005..6d35150 100644 --- a/test.c +++ b/test.c @@ -14,36 +14,31 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include #include #include +#include #include +#include #include #include #include +#include #include "structs.h" #include "dns.h" #include "read.h" - -int -main() + +static void +test_readputshort() { short tshort; - short temps; short putted; - short *s; - - long tint; - long tempi; - long putint; - long *l; - - int i; + short temps; char buf[4]; + short *s; char* p; + int i; - printf("** iodine test suite\n"); printf(" * Testing read/putshort... "); fflush(stdout); @@ -69,6 +64,18 @@ main() } printf("OK\n"); +} + +static void +test_readputlong() +{ + char buf[4]; + long putint; + long tempi; + long tint; + long *l; + char* p; + int i; printf(" * Testing read/putlong... "); fflush(stdout); @@ -95,7 +102,52 @@ main() } printf("OK\n"); +} +static void +test_readname() +{ + char emptyloop[] = { + 'A', 'A', 0x81, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01 }; + char infloop[] = { + 'A', 'A', 0x81, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 'A', 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01 }; + char buf[1024]; + char *data; + int rv; + + printf(" * Testing readname... "); + fflush(stdout); + + bzero(buf, sizeof(buf)); + data = emptyloop + sizeof(HEADER); + buf[1023] = 'A'; + rv = readname(emptyloop, &data, buf, 1023); + assert(buf[1023] == 'A'); + + + bzero(buf, sizeof(buf)); + data = infloop + sizeof(HEADER); + + buf[4] = '\a'; + rv = readname(infloop, &data, buf, 4); + printf("%s\n", buf); + assert(buf[4] == '\a'); + + + + printf("OK\n"); +} + +int +main() +{ + printf("** iodine test suite\n"); + + test_readputshort(); + test_readputlong(); + test_readname(); printf("** All went well :)\n"); return 0;