Split dns_read to read and dns_parse_reply for testing reasons
This commit is contained in:
parent
819f6bec81
commit
125bb57a2f
102
dns.c
102
dns.c
|
@ -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,58 +298,35 @@ 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) {
|
header = (HEADER*)packet;
|
||||||
perror("recvfrom");
|
|
||||||
} else {
|
data = packet + sizeof(HEADER);
|
||||||
header = (HEADER*)packet;
|
|
||||||
|
|
||||||
data = packet + sizeof(HEADER);
|
|
||||||
|
|
||||||
if(header->qr) { /* qr=1 => response */
|
if(header->qr) { /* qr=1 => response */
|
||||||
qdcount = ntohs(header->qdcount);
|
qdcount = ntohs(header->qdcount);
|
||||||
ancount = ntohs(header->ancount);
|
ancount = ntohs(header->ancount);
|
||||||
|
|
||||||
rlen = 0;
|
rlen = 0;
|
||||||
|
|
||||||
if(qdcount == 1) {
|
if(qdcount == 1) {
|
||||||
readname(packet, &data, name, sizeof(name));
|
readname(packet, &data, name, sizeof(name));
|
||||||
readshort(packet, &data, &type);
|
readshort(packet, &data, &type);
|
||||||
readshort(packet, &data, &class);
|
readshort(packet, &data, &class);
|
||||||
}
|
}
|
||||||
if(ancount == 1) {
|
if(ancount == 1) {
|
||||||
readname(packet, &data, name, sizeof(name));
|
readname(packet, &data, name, sizeof(name));
|
||||||
readshort(packet, &data, &type);
|
readshort(packet, &data, &type);
|
||||||
readshort(packet, &data, &class);
|
readshort(packet, &data, &class);
|
||||||
readlong(packet, &data, &ttl);
|
readlong(packet, &data, &ttl);
|
||||||
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;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue