diff --git a/src/Makefile b/src/Makefile index fce45ea..093efd8 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,8 +1,8 @@ CC = gcc CLIENT = ../bin/iodine -CLIENTOBJS = iodine.o tun.o dns.o read.o encoding.o login.o base32.o md5.o +CLIENTOBJS = iodine.o tun.o dns.o read.o encoding.o login.o base32.o md5.o common.o SERVER = ../bin/iodined -SERVEROBJS = iodined.o tun.o dns.o read.o encoding.o login.o base32.o md5.o +SERVEROBJS = iodined.o tun.o dns.o read.o encoding.o login.o base32.o md5.o common.o OS = `uname | tr "a-z" "A-Z"` ARCH = `uname -m` diff --git a/src/dns.c b/src/dns.c index 32ce5d5..0006561 100644 --- a/src/dns.c +++ b/src/dns.c @@ -33,7 +33,6 @@ #include #include -#include "structs.h" #include "dns.h" #include "encoding.h" #include "read.h" @@ -61,42 +60,6 @@ static uint16_t chunkid; static uint16_t pingid; -int -open_dns(int localport, in_addr_t listen_ip) -{ - int fd; - int flag; - struct sockaddr_in addr; - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(localport); - /* listen_ip already in network byte order from inet_addr, or 0 */ - addr.sin_addr.s_addr = listen_ip; - - fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - 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"); - - return fd; -} - void dns_set_topdomain(const char *domain) { @@ -128,11 +91,6 @@ dns_settarget(const char *host) return 0; } -void -close_dns(int fd) -{ - close(fd); -} int dns_sending() diff --git a/src/dns.h b/src/dns.h index a6ea64f..5a96f81 100644 --- a/src/dns.h +++ b/src/dns.h @@ -14,18 +14,18 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#ifndef _DNS_H_ -#define _DNS_H_ +#ifndef __DNS_H__ +#define __DNS_H__ + +#include "common.h" typedef enum { QR_QUERY = 0, QR_ANSWER = 1 } qr_t; -int open_dns(int, in_addr_t); int dns_settarget(const char*); void dns_set_topdomain(const char*); -void close_dns(int); int dns_sending(); void dns_handle_tun(int, char *, int); diff --git a/src/iodine.c b/src/iodine.c index 6dcc6cd..e3798f4 100644 --- a/src/iodine.c +++ b/src/iodine.c @@ -30,16 +30,16 @@ #include #include -#include "tun.h" -#include "structs.h" +#include "common.h" #include "dns.h" -#include "version.h" #include "login.h" +#include "tun.h" +#include "version.h" #ifndef MAX #define MAX(a,b) ((a)>(b)?(a):(b)) #endif - + int running = 1; char password[33]; @@ -49,14 +49,49 @@ sighandler(int sig) { } static int -tunnel(int tun_fd, int dns_fd) +tunnel_tun(int tun_fd, int dns_fd) { char out[64*1024]; char in[64*1024]; - struct timeval tv; - long outlen; - fd_set fds; + size_t outlen; int read; + + read = read_tun(tun_fd, in, sizeof(in)); + if(read > 0) { + outlen = sizeof(out); + compress2(out, &outlen, in, read, 9); + dns_handle_tun(dns_fd, out, outlen); + } + + return read; +} + +static int +tunnel_dns(int tun_fd, int dns_fd) +{ + char out[64*1024]; + char in[64*1024]; + size_t outlen; + int read; + + read = dns_read(dns_fd, in, sizeof(in)); + if (read > 0) { + outlen = sizeof(out); + uncompress(out, &outlen, in, read); + + write_tun(tun_fd, out, outlen); + if (!dns_sending()) + dns_ping(dns_fd); + } + + return read; +} + +static int +tunnel(int tun_fd, int dns_fd) +{ + struct timeval tv; + fd_set fds; int i; int rv; @@ -73,39 +108,23 @@ tunnel(int tun_fd, int dns_fd) i = select(MAX(tun_fd, dns_fd) + 1, &fds, NULL, NULL, &tv); - if (!running) { + if (running == 0 || i < 0) { rv = 1; break; } - if(i < 0) { - warn("select"); - rv = 1; - break; - } else if (i > 0) { + if (i == 0) /* timeout */ + dns_ping(dns_fd); + else { if(FD_ISSET(tun_fd, &fds)) { - read = read_tun(tun_fd, in, sizeof(in)); - if(read <= 0) + if (tunnel_tun(tun_fd, dns_fd) <= 0) continue; - - outlen = sizeof(out); - compress2(out, &outlen, in, read, 9); - dns_handle_tun(dns_fd, out, outlen); } if(FD_ISSET(dns_fd, &fds)) { - read = dns_read(dns_fd, in, sizeof(in)); - if (read <= 0) + if (tunnel_dns(tun_fd, dns_fd) <= 0) continue; - - outlen = sizeof(out); - uncompress(out, &outlen, in, read); - - write_tun(tun_fd, out, outlen); - if (!dns_sending()) - dns_ping(dns_fd); } - } else - dns_ping(dns_fd); + } } return rv; diff --git a/src/iodined.c b/src/iodined.c index f7ae771..a115f90 100644 --- a/src/iodined.c +++ b/src/iodined.c @@ -32,10 +32,10 @@ #include #include -#include "tun.h" -#include "structs.h" +#include "common.h" #include "dns.h" #include "login.h" +#include "tun.h" #include "version.h" #ifndef MAX diff --git a/src/structs.h b/src/structs.h deleted file mode 100644 index 61b92e6..0000000 --- a/src/structs.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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 _STRUCTS_H_ -#define _STRUCTS_H_ - -struct packet -{ - int len; - int offset; - char data[64*1024]; -}; - -struct query { - char name[258]; - short type; - short id; - struct sockaddr from; - int fromlen; -}; - -struct user { - int id; - struct sockaddr host; - int addrlen; -}; - -#endif /* _STRUCTS_H_ */