From f21e85b574d807e2dd4feafcb51adfc538a199ec Mon Sep 17 00:00:00 2001 From: Erik Ekman Date: Mon, 5 Jun 2006 14:37:58 +0000 Subject: [PATCH] Send dns query on start --- dns.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ dns.h | 1 + dnstun.c | 1 + 3 files changed, 67 insertions(+) diff --git a/dns.c b/dns.c index ec96328..069f2e3 100644 --- a/dns.c +++ b/dns.c @@ -30,6 +30,8 @@ #include "dns.h" +static int host2dns(const char *, char *, int); + struct sockaddr_in peer; int @@ -83,3 +85,66 @@ dns_set_peer(const char *host) peer.sin_port = htons(53); peer.sin_addr = *((struct in_addr *) h->h_addr); } + +void +dns_query(int fd, char *host, int type) +{ + char *p; + int len; + int peerlen; + char buf[1024]; + HEADER *header; + + len = 0; + memset(buf, 0, sizeof(buf)); + + header = (HEADER*)buf; + + header->id = 0; + header->qr = 0; + header->opcode = 0; + header->aa = 0; + header->tc = 0; + header->rd = 1; + header->ra = 0; + + header->qdcount = htons(1); + + p = buf + sizeof(HEADER); + p += host2dns(host, p, strlen(host)); + + PUTSHORT(type, p); + PUTSHORT(C_IN, p); + + peerlen = sizeof(peer); + + len = p - buf; + sendto(fd, buf, len+1, 0, (struct sockaddr*)&peer, peerlen); +} + +static int +host2dns(const char *host, char *buffer, int size) +{ + char *h; + char *p; + char *word; + + h = strdup(host); + memset(buffer, 0, size); + p = buffer; + + word = strtok(h, "."); + while(word) { + *p++ = (char)strlen(word); + memcpy(p, word, strlen(word)); + p += strlen(word); + + word = strtok(NULL, "."); + } + + *p++ = 0; + + free(h); + + return p - buffer; +} diff --git a/dns.h b/dns.h index 647febe..21f9a0b 100644 --- a/dns.h +++ b/dns.h @@ -25,5 +25,6 @@ int open_dns(); void close_dns(int); void dns_set_peer(const char *); +void dns_query(int, char *, int); #endif /* _DNS_H_ */ diff --git a/dnstun.c b/dnstun.c index df6ab2e..c8e0b02 100644 --- a/dnstun.c +++ b/dnstun.c @@ -28,6 +28,7 @@ main() open_tun(); dnssock = open_dns(); dns_set_peer("192.168.11.101"); + dns_query(dnssock, "kryo.se", 1); close_dns(dnssock); close_tun();