server ok

This commit is contained in:
Bjorn Andersson 2006-06-06 02:27:10 +00:00
parent e6da8c66d8
commit 1fd044cbff
2 changed files with 25 additions and 20 deletions

40
dnsd.c
View File

@ -39,6 +39,7 @@ char topdomain[256];
int packetlen;
char activepacket[4096];
int outid;
int outbuflen;
char outbuf[64*1024];
@ -174,10 +175,11 @@ dnsd_queuepacket(const char *buf, const int buflen)
memcpy(outbuf, buf, buflen);
outbuflen = buflen;
outid++;
}
static void
dnsd_respond(int fd, short id, struct sockaddr_in from)
dnsd_respond(int fd, char *name, short type, short id, struct sockaddr_in from)
{
int len;
char *p;
@ -189,7 +191,7 @@ dnsd_respond(int fd, short id, struct sockaddr_in from)
len = 0;
header = (HEADER*)buf;
header->id = id;
header->id = htons(id);
header->qr = 1;
header->opcode = 0;
header->aa = 1;
@ -197,22 +199,28 @@ dnsd_respond(int fd, short id, struct sockaddr_in from)
header->rd = 0;
header->ra = 0;
if(outbuflen > 0)
header->ancount = htons(1);
else
header->ancount = htons(0);
header->qdcount = htons(1);
header->ancount = htons(1);
p = buf + sizeof(HEADER);
p += host2dns("fluff", p, 5);
PUTSHORT(T_NULL, p);
p += host2dns(name, p, strlen(name));
PUTSHORT(type, p);
PUTSHORT(C_IN, p);
PUTLONG(htons(0), p);
p += host2dns(name, p, strlen(name));
PUTSHORT(type, p);
PUTSHORT(C_IN, p);
PUTLONG(0, p);
//size = host2dns(outbuf, p+2, outbuflen);
PUTSHORT(outbuflen, p);
memcpy(p, outbuf, outbuflen);
p += outbuflen;
if(outbuflen > 0) {
printf("%d\n", outid);
PUTSHORT(outbuflen, p);
memcpy(p, outbuf, outbuflen);
p += outbuflen;
} else {
PUTSHORT(0, p);
}
len = p - buf;
printf("Responding with %d\n", len);
@ -224,7 +232,6 @@ dnsd_respond(int fd, short id, struct sockaddr_in from)
int
dnsd_read(int fd, char *buf, int buflen)
{
int i;
int r;
short id;
short type;
@ -261,12 +268,14 @@ dnsd_read(int fd, char *buf, int buflen)
if(!header->qr) {
qdcount = ntohs(header->qdcount);
for(i=0;i<qdcount;i++) {
if(qdcount == 1) {
bzero(name, sizeof(name));
READNAME(packet, name, data);
READSHORT(type, data);
READSHORT(class, data);
dnsd_respond(fd, name, type, id, from);
lastblock = name[0] - '0';
np = name;
np++; // skip first byte, it has only fragmentation info
@ -289,7 +298,6 @@ dnsd_read(int fd, char *buf, int buflen)
packetp++;
packetlen++;
}
dnsd_respond(fd, id, from);
if (lastblock && packetlen < 2) {
// Skipping ping packet
packetlen = 0;

View File

@ -57,10 +57,8 @@ tunnel(int tun_fd, int dns_fd)
tv.tv_usec = 0;
FD_ZERO(&fds);
if(!dnsd_haspacket()) {
printf("There is room for more\n");
if(!dnsd_haspacket())
FD_SET(tun_fd, &fds);
}
FD_SET(dns_fd, &fds);
i = select(MAX(tun_fd, dns_fd) + 1, &fds, NULL, NULL, &tv);
@ -77,7 +75,6 @@ tunnel(int tun_fd, int dns_fd)
printf("data on tun\n");
read = read_tun(tun_fd, frame, 64*1024);
if(read > 0) {
printf("Sending response\n");
dnsd_queuepacket(frame->data, read - 4);
}
}