diff --git a/dns.c b/dns.c index 9e3102e..6878f84 100644 --- a/dns.c +++ b/dns.c @@ -83,11 +83,12 @@ readname(char *packet, char *dst, char *src) memcpy((dst), (src), (len)); (src)+=(len); int -open_dns() +open_dns(const char *host, const char *domain) { int fd; int flag; struct sockaddr_in addr; + struct hostent *h; bzero(&addr, sizeof(addr)); addr.sin_family = AF_INET; @@ -113,6 +114,20 @@ open_dns() printf("Opened UDP socket\n"); + // Init dns target struct + h = gethostbyname(host); + if (!h) { + perror("gethostbyname"); + } + bzero(&peer, sizeof(peer)); + peer.sin_family = AF_INET; + peer.sin_port = htons(53); + peer.sin_addr = *((struct in_addr *) h->h_addr); + + // Save top domain used + strncpy(topdomain, domain, sizeof(topdomain) - 2); + topdomain[sizeof(topdomain) - 1] = 0; + return fd; } @@ -122,22 +137,10 @@ close_dns(int fd) close(fd); } -void -dns_set_peer(const char *host) -{ - struct hostent *h; - - h = gethostbyname(host); - bzero(&peer, sizeof(peer)); - peer.sin_family = AF_INET; - peer.sin_port = htons(53); - peer.sin_addr = *((struct in_addr *) h->h_addr); -} - void dns_ping(int dns_fd) { - dns_query(dns_fd, "kryo.se", 1); + dns_query(dns_fd, "4.kryo.se", 1); } void diff --git a/dns.h b/dns.h index ea3be50..e0c4a64 100644 --- a/dns.h +++ b/dns.h @@ -19,10 +19,9 @@ extern struct sockaddr_in peer; -int open_dns(); +int open_dns(const char *, const char *); void close_dns(int); -void dns_set_peer(const char *); void dns_ping(int); void dns_query(int, char *, int); int dns_read(int, char *, int); diff --git a/dnstun.c b/dnstun.c index dff2a88..d8ea510 100644 --- a/dnstun.c +++ b/dnstun.c @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -76,18 +77,21 @@ tunnel(int tun_fd, int dns_fd) } int -main() +main(int argc, char **argv) { int tun_fd; int dns_fd; + if (argc != 3) { + printf("Usage: %s nameserver topdomain\n", argv[0]); + exit(2); + } + tun_fd = open_tun(); - dns_fd = open_dns(); + dns_fd = open_dns(argv[1], argv[2]); signal(SIGINT, sigint); - dns_set_peer("192.168.11.101"); - tunnel(tun_fd, dns_fd); printf("Closing tunnel\n");