From 33f8a07aa6d5225ed4371991bd5c2fcf0a7821d0 Mon Sep 17 00:00:00 2001 From: Erik Ekman Date: Mon, 5 Jun 2006 13:38:10 +0000 Subject: [PATCH] Added dns.[ch], for udp messaging --- Makefile | 2 +- dns.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ dns.h | 25 ++++++++++++++++++++ dnstun.c | 8 ++++++- 4 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 dns.c create mode 100644 dns.h diff --git a/Makefile b/Makefile index b11154b..79a6aff 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC = gcc OUT = dnstun -OBJS = dnstun.o tun.o +OBJS = dnstun.o tun.o dns.o OS = `uname | tr "a-z" "A-Z"` diff --git a/dns.c b/dns.c new file mode 100644 index 0000000..4a6bc93 --- /dev/null +++ b/dns.c @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2006 + * Bjorn Andersson , + * Erik Ekman + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dns.h" + +int +open_dns() +{ + int fd; + int flag; + struct sockaddr_in addr; + + bzero(&addr, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = 0; // choose an available port + addr.sin_addr.s_addr = INADDR_ANY; // listen on 0.0.0.0 + + fd = socket(AF_INET, SOCK_DGRAM, 0); + if(fd < 0) { + err(1, "Could not get UDP socket"); + } + + 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) { + err(1, "Could not bind UDP socket locally"); + } + + printf("Opened UDP socket\n"); + + return fd; +} + +void +close_dns(int fd) +{ + close(fd); +} diff --git a/dns.h b/dns.h new file mode 100644 index 0000000..b26b9fb --- /dev/null +++ b/dns.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2006 + * Bjorn Andersson , + * Erik Ekman + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef _DNS_H_ +#define _DNS_H_ + +int open_dns(); +void close_dns(int); + +#endif /* _DNS_H_ */ diff --git a/dnstun.c b/dnstun.c index ffbc370..ea66cc3 100644 --- a/dnstun.c +++ b/dnstun.c @@ -20,12 +20,18 @@ #include #include "tun.h" +#include "dns.h" int main() { - open_tun(); + int dnssock; + open_tun(); + dnssock = open_dns(); + + + close_dns(dnssock); close_tun(); return 0;