Split dns_read to read and dns_parse_reply for testing reasons

This commit is contained in:
Erik Ekman 2006-08-24 21:30:19 +00:00
parent 819f6bec81
commit 125bb57a2f
1 changed files with 56 additions and 46 deletions

58
dns.c
View File

@ -46,6 +46,7 @@
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];
@ -254,6 +255,38 @@ int
dns_read(int fd, char *buf, int buflen) dns_read(int fd, char *buf, int buflen)
{ {
int r; int r;
socklen_t addrlen;
char packet[64*1024];
struct sockaddr_in from;
HEADER *header;
addrlen = sizeof(struct sockaddr);
r = recvfrom(fd, packet, sizeof(packet), 0, (struct sockaddr*)&from, &addrlen);
if(r == -1) {
perror("recvfrom");
return 0;
}
header = (HEADER*)packet;
if (dns_sending() && chunkid == ntohs(header->id)) {
/* Got ACK on sent packet */
packetpos += lastlen;
if (packetpos == packetlen) {
/* Packet completed */
packetpos = 0;
packetlen = 0;
lastlen = 0;
} else {
/* More to send */
dns_send_chunk(fd);
}
}
return dns_parse_reply(buf, buflen, packet, r);
}
static int
dns_parse_reply(char *outbuf, int buflen, char *packet, int packetlen)
{
int rv; int rv;
long ttl; long ttl;
short rlen; short rlen;
@ -265,17 +298,8 @@ dns_read(int fd, char *buf, int buflen)
char name[255]; char name[255];
char rdata[4*1024]; char rdata[4*1024];
HEADER *header; HEADER *header;
socklen_t addrlen;
char packet[64*1024];
struct sockaddr_in from;
addrlen = sizeof(struct sockaddr);
r = recvfrom(fd, packet, sizeof(packet), 0, (struct sockaddr*)&from, &addrlen);
rv = 0; rv = 0;
if(r == -1) {
perror("recvfrom");
} else {
header = (HEADER*)packet; header = (HEADER*)packet;
data = packet + sizeof(HEADER); data = packet + sizeof(HEADER);
@ -299,26 +323,12 @@ dns_read(int fd, char *buf, int buflen)
readshort(packet, &data, &rlen); readshort(packet, &data, &rlen);
readdata(packet, &data, rdata, rlen); readdata(packet, &data, rdata, rlen);
} }
if (dns_sending() && chunkid == ntohs(header->id)) {
// Got ACK on sent packet
packetpos += lastlen;
if (packetpos == packetlen) {
// Packet completed
packetpos = 0;
packetlen = 0;
lastlen = 0;
} else {
// More to send
dns_send_chunk(fd);
}
}
if(type == T_NULL && rlen > 2) { if(type == T_NULL && rlen > 2) {
memcpy(buf, rdata, rlen); memcpy(outbuf, rdata, rlen);
rv = rlen; rv = rlen;
} }
} }
}
return rv; return rv;
} }