Now init peer and domain in open_dns(), use argv values

This commit is contained in:
Erik Ekman 2006-06-05 17:22:35 +00:00
parent 5eb02a4bdc
commit ecb97656ab
3 changed files with 26 additions and 20 deletions

31
dns.c
View File

@ -83,11 +83,12 @@ readname(char *packet, char *dst, char *src)
memcpy((dst), (src), (len)); (src)+=(len); memcpy((dst), (src), (len)); (src)+=(len);
int int
open_dns() open_dns(const char *host, const char *domain)
{ {
int fd; int fd;
int flag; int flag;
struct sockaddr_in addr; struct sockaddr_in addr;
struct hostent *h;
bzero(&addr, sizeof(addr)); bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
@ -113,6 +114,20 @@ open_dns()
printf("Opened UDP socket\n"); 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; return fd;
} }
@ -122,22 +137,10 @@ close_dns(int fd)
close(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 void
dns_ping(int dns_fd) dns_ping(int dns_fd)
{ {
dns_query(dns_fd, "kryo.se", 1); dns_query(dns_fd, "4.kryo.se", 1);
} }
void void

3
dns.h
View File

@ -19,10 +19,9 @@
extern struct sockaddr_in peer; extern struct sockaddr_in peer;
int open_dns(); int open_dns(const char *, const char *);
void close_dns(int); void close_dns(int);
void dns_set_peer(const char *);
void dns_ping(int); void dns_ping(int);
void dns_query(int, char *, int); void dns_query(int, char *, int);
int dns_read(int, char *, int); int dns_read(int, char *, int);

View File

@ -16,6 +16,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <signal.h> #include <signal.h>
#include <unistd.h> #include <unistd.h>
@ -76,18 +77,21 @@ tunnel(int tun_fd, int dns_fd)
} }
int int
main() main(int argc, char **argv)
{ {
int tun_fd; int tun_fd;
int dns_fd; int dns_fd;
if (argc != 3) {
printf("Usage: %s nameserver topdomain\n", argv[0]);
exit(2);
}
tun_fd = open_tun(); tun_fd = open_tun();
dns_fd = open_dns(); dns_fd = open_dns(argv[1], argv[2]);
signal(SIGINT, sigint); signal(SIGINT, sigint);
dns_set_peer("192.168.11.101");
tunnel(tun_fd, dns_fd); tunnel(tun_fd, dns_fd);
printf("Closing tunnel\n"); printf("Closing tunnel\n");