Merged open_dns(d), close_dns(d)

This commit is contained in:
Bjorn Andersson 2006-06-11 19:54:23 +00:00
parent 1eda2d0472
commit 863dceb4cd
4 changed files with 26 additions and 64 deletions

72
dns.c
View File

@ -49,16 +49,15 @@ uint16_t pingid;
int
open_dns(const char *host, const char *domain)
open_dns(const char *domain, int localport)
{
int fd;
int flag;
struct sockaddr_in addr;
struct hostent *h;
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(0);
addr.sin_port = htons(localport);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
fd = socket(AF_INET, SOCK_DGRAM, 0);
@ -78,8 +77,18 @@ open_dns(const char *host, const char *domain)
return -1;
}
// Save top domain used
strncpy(topdomain, domain, sizeof(topdomain) - 2);
topdomain[sizeof(topdomain) - 1] = 0;
printf("Opened UDP socket\n");
printf("Sending queries for %s to %s\n", domain, host);
return fd;
}
int
dns_settarget(const char *host)
{
struct hostent *h;
// Init dns target struct
h = gethostbyname(host);
@ -87,64 +96,19 @@ open_dns(const char *host, const char *domain)
printf("Could not resolve name %s, exiting\n", host);
return -1;
}
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;
// Init chunk id
chunkid = 0;
pingid = 0;
return fd;
return 0;
}
int
open_dnsd(const char *domain)
{
int fd;
int flag;
struct sockaddr_in addr;
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(53);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd < 0) {
warn("socket");
return -1;
}
flag = 1;
#ifdef SO_REUSEPORT
setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &flag, sizeof(flag));
#endif
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
if(bind(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
warn("bind");
return -1;
}
printf("Opened UDP socket\n");
printf("Listening to dns for domain %s\n", domain);
// Save top domain used
strncpy(topdomain, domain, sizeof(topdomain) - 2);
topdomain[sizeof(topdomain) - 1] = 0;
packetlen = 0;
return fd;
}
void
close_dns(int fd)
{
@ -497,12 +461,6 @@ decodepacket(const char *name, char *buf, int buflen)
return len;
}
void
close_dnsd(int fd)
{
close(fd);
}
int
dnsd_read(int fd, struct query *q, char *buf, int buflen)
{

6
dns.h
View File

@ -17,7 +17,8 @@
#ifndef _DNS_H_
#define _DNS_H_
int open_dns(const char *, const char *);
int open_dns(const char *, int);
int dns_settarget(const char*);
void close_dns(int);
int dns_sending();
@ -29,9 +30,6 @@ int dns_read(int, char *, int);
extern struct sockaddr_in peer;
int open_dnsd(const char *);
void close_dnsd(int);
int dnsd_read(int, struct query*, char *, int);
void dnsd_send(int, struct query*, char *, int);

View File

@ -247,8 +247,12 @@ main(int argc, char **argv)
if ((tun_fd = open_tun()) == -1)
goto cleanup1;
if ((dns_fd = open_dns(argv[0], argv[1])) == -1)
if ((dns_fd = open_dns(argv[0], 0)) == -1)
goto cleanup2;
if (dns_settarget(argv[1]) == -1)
goto cleanup2;
printf("Sending queries for %s to %s\n", argv[1], argv[0]);
signal(SIGINT, sighandler);
signal(SIGTERM, sighandler);

View File

@ -249,9 +249,11 @@ main(int argc, char **argv)
goto cleanup0;
if (tun_setip(argv[0]) != 0 || tun_setmtu(mtu) != 0)
goto cleanup1;
if ((dnsd_fd = open_dnsd(argv[1])) == -1)
if ((dnsd_fd = open_dns(argv[1], 53)) == -1)
goto cleanup2;
printf("Listening to dns for domain %s\n", argv[1]);
if (newroot) {
if (chroot(newroot) != 0 || chdir("/") != 0)
err(1, "%s", newroot);
@ -277,7 +279,7 @@ main(int argc, char **argv)
tunnel(tun_fd, dnsd_fd);
cleanup2:
close_dnsd(dnsd_fd);
close_dns(dnsd_fd);
cleanup1:
close_tun(tun_fd);
cleanup0: