Send dns query on start

This commit is contained in:
Erik Ekman 2006-06-05 14:37:58 +00:00
parent 97a58e51ee
commit f21e85b574
3 changed files with 67 additions and 0 deletions

65
dns.c
View file

@ -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;
}

1
dns.h
View file

@ -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_ */

View file

@ -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();