readname-tests

This commit is contained in:
Bjorn Andersson 2006-08-24 22:04:27 +00:00
parent 966f095d1e
commit 72f8326077
4 changed files with 84 additions and 23 deletions

3
dns.c
View file

@ -46,7 +46,6 @@
static int host2dns(const char *, char *, int); static int host2dns(const char *, char *, int);
static int dns_write(int, int, char *, int, char); static int dns_write(int, int, char *, int, char);
static void dns_query(int, int, char *, int); static void dns_query(int, int, char *, int);
static int dns_parse_reply(char *, int, char *, int);
struct sockaddr_in peer; struct sockaddr_in peer;
char topdomain[256]; char topdomain[256];
@ -284,7 +283,7 @@ dns_read(int fd, char *buf, int buflen)
return dns_parse_reply(buf, buflen, packet, r); return dns_parse_reply(buf, buflen, packet, r);
} }
static int int
dns_parse_reply(char *outbuf, int buflen, char *packet, int packetlen) dns_parse_reply(char *outbuf, int buflen, char *packet, int packetlen)
{ {
int rv; int rv;

2
dns.h
View file

@ -37,5 +37,7 @@ int dnsd_hasack();
void dnsd_forceack(int); void dnsd_forceack(int);
void dnsd_queuepacket(const char *, const int); void dnsd_queuepacket(const char *, const int);
int dns_parse_reply(char *, int, char *, int);
#endif /* _DNS_H_ */ #endif /* _DNS_H_ */

22
read.c
View file

@ -16,35 +16,37 @@
#include <string.h> #include <string.h>
int static int
readname(char *packet, char **src, char *dst, size_t length) readname_loop(char *packet, char **src, char *dst, size_t length, size_t loop)
{ {
char *dummy; char *dummy;
int len; int len;
char *p; char *p;
char c; char c;
if (loop <= 0)
return 0;
len = 0; len = 0;
p = *src; p = *src;
while(*p && len < length) { while(*p && len < length - 2) {
c = *p++; c = *p++;
len++;
/* is this a compressed label? */ /* is this a compressed label? */
if((c & 0xc0) == 0xc0) { if((c & 0xc0) == 0xc0) {
dummy = packet + (((p[-1] & 0x3f) << 8) | p[0]); 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; break;
} }
while(c && len < length) { while(c && len < length - 2) {
*dst++ = *p++; *dst++ = *p++;
len++; len++;
c--; c--;
} }
if (*p != 0) if (*p != 0 && len < length - 2)
*dst++ = '.'; *dst++ = '.';
else else
*dst++ = '\0'; *dst++ = '\0';
@ -54,6 +56,12 @@ readname(char *packet, char **src, char *dst, size_t length)
return strlen(dst); return strlen(dst);
} }
int
readname(char *packet, char **src, char *dst, size_t length)
{
return readname_loop(packet, src, dst, length, 10);
}
int int
readshort(char *packet, char **src, short *dst) readshort(char *packet, char **src, short *dst)
{ {

78
test.c
View file

@ -14,36 +14,31 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include <netinet/in.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <arpa/nameser.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h>
#include "structs.h" #include "structs.h"
#include "dns.h" #include "dns.h"
#include "read.h" #include "read.h"
int static void
main() test_readputshort()
{ {
short tshort; short tshort;
short temps;
short putted; short putted;
short *s; short temps;
long tint;
long tempi;
long putint;
long *l;
int i;
char buf[4]; char buf[4];
short *s;
char* p; char* p;
int i;
printf("** iodine test suite\n");
printf(" * Testing read/putshort... "); printf(" * Testing read/putshort... ");
fflush(stdout); fflush(stdout);
@ -69,6 +64,18 @@ main()
} }
printf("OK\n"); 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... "); printf(" * Testing read/putlong... ");
fflush(stdout); fflush(stdout);
@ -95,7 +102,52 @@ main()
} }
printf("OK\n"); 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"); printf("** All went well :)\n");
return 0; return 0;