From 23ad29522b4e3fa0d624391fd185d721175187d0 Mon Sep 17 00:00:00 2001 From: Erik Ekman Date: Sun, 11 Feb 2007 11:21:18 +0000 Subject: [PATCH] #11 moved user code to user.c --- src/Makefile | 2 +- src/common.h | 13 ------- src/iodined.c | 77 ++------------------------------------- src/user.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/user.h | 42 ++++++++++++++++++++++ 5 files changed, 145 insertions(+), 88 deletions(-) create mode 100644 src/user.c create mode 100644 src/user.h diff --git a/src/Makefile b/src/Makefile index 093efd8..563088f 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,7 +2,7 @@ CC = gcc CLIENT = ../bin/iodine 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 common.o +SERVEROBJS = iodined.o tun.o dns.o read.o encoding.o login.o base32.o md5.o common.o user.o OS = `uname | tr "a-z" "A-Z"` ARCH = `uname -m` diff --git a/src/common.h b/src/common.h index 8c95767..ee97e1f 100644 --- a/src/common.h +++ b/src/common.h @@ -42,19 +42,6 @@ struct query { int fromlen; }; -struct user { - char id; - int active; - time_t last_pkt; - int seed; - in_addr_t tun_ip; - struct sockaddr host; - int addrlen; - struct query q; - struct packet inpacket; - struct packet outpacket; -}; - int open_dns(int, in_addr_t); void close_dns(int); diff --git a/src/iodined.c b/src/iodined.c index 121b7e2..0e98072 100644 --- a/src/iodined.c +++ b/src/iodined.c @@ -18,7 +18,6 @@ #include #include #include -#include #include #include #include @@ -36,6 +35,7 @@ #include "common.h" #include "dns.h" +#include "user.h" #include "login.h" #include "tun.h" #include "encoding.h" @@ -45,8 +45,6 @@ int running = 1; char *topdomain; -#define USERS 8 -struct user users[USERS]; char password[33]; int my_mtu; @@ -61,76 +59,6 @@ sigint(int sig) running = 0; } -static void -init_users() -{ - int i; - char newip[16]; - - memset(users, 0, USERS * sizeof(struct user)); - for (i = 0; i < USERS; i++) { - users[i].id = i; - snprintf(newip, sizeof(newip), "0.0.0.%d", i + 1); - users[i].tun_ip = my_ip + inet_addr(newip);; - users[i].inpacket.len = 0; - users[i].inpacket.offset = 0; - users[i].outpacket.len = 0; - users[i].q.id = 0; - } -} - -static int -users_waiting_on_reply() -{ - int ret; - int i; - - ret = 0; - for (i = 0; i < USERS; i++) { - if (users[i].active && users[i].last_pkt + 60 > time(NULL) && - users[i].q.id != 0) { - ret++; - } - } - - return ret; -} - -static int -find_user_by_ip(uint32_t ip) -{ - int ret; - int i; - - ret = -1; - for (i = 0; i < USERS; i++) { - if (users[i].active && users[i].last_pkt + 60 > time(NULL) && - ip == users[i].tun_ip) { - ret = i; - break; - } - } - return ret; -} - -static int -find_available_user() -{ - int ret = -1; - int i; - - for (i = 0; i < USERS; i++) { - /* Not used at all or not used in one minute */ - if (!users[i].active || users[i].last_pkt + 60 < time(NULL)) { - users[i].active = 1; - users[i].last_pkt = time(NULL); - ret = i; - break; - } - } - return ret; -} - static int tunnel_tun(int tun_fd, int dns_fd) { @@ -282,6 +210,7 @@ tunnel_dns(int tun_fd, int dns_fd) write_dns(dns_fd, &(dummy.q), "BADIP", 5); return 0; /* illegal id */ } + users[userid].last_pkt = time(NULL); } else if((in[0] >= '0' && in[0] <= '9') || (in[0] >= 'a' && in[0] <= 'f') || (in[0] >= 'A' && in[0] <= 'F')) { @@ -582,7 +511,7 @@ main(int argc, char **argv) my_ip = inet_addr(argv[0]); my_mtu = mtu; - init_users(); + init_users(my_ip); printf("Listening to dns for domain %s\n", argv[1]); diff --git a/src/user.c b/src/user.c new file mode 100644 index 0000000..a85e280 --- /dev/null +++ b/src/user.c @@ -0,0 +1,99 @@ +/* + * 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 "common.h" +#include "user.h" + +struct user users[USERS]; + +void +init_users(in_addr_t my_ip) +{ + int i; + char newip[16]; + + memset(users, 0, USERS * sizeof(struct user)); + for (i = 0; i < USERS; i++) { + users[i].id = i; + snprintf(newip, sizeof(newip), "0.0.0.%d", i + 1); + users[i].tun_ip = my_ip + inet_addr(newip);; + users[i].inpacket.len = 0; + users[i].inpacket.offset = 0; + users[i].outpacket.len = 0; + users[i].q.id = 0; + } +} + +int +users_waiting_on_reply() +{ + int ret; + int i; + + ret = 0; + for (i = 0; i < USERS; i++) { + if (users[i].active && users[i].last_pkt + 60 > time(NULL) && + users[i].q.id != 0) { + ret++; + } + } + + return ret; +} + +int +find_user_by_ip(uint32_t ip) +{ + int ret; + int i; + + ret = -1; + for (i = 0; i < USERS; i++) { + if (users[i].active && users[i].last_pkt + 60 > time(NULL) && + ip == users[i].tun_ip) { + ret = i; + break; + } + } + return ret; +} + +int +find_available_user() +{ + int ret = -1; + int i; + + for (i = 0; i < USERS; i++) { + /* Not used at all or not used in one minute */ + if (!users[i].active || users[i].last_pkt + 60 < time(NULL)) { + users[i].active = 1; + users[i].last_pkt = time(NULL); + ret = i; + break; + } + } + return ret; +} + diff --git a/src/user.h b/src/user.h new file mode 100644 index 0000000..f9bbfb1 --- /dev/null +++ b/src/user.h @@ -0,0 +1,42 @@ +/* + * 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 __USER_H__ +#define __USER_H__ + +#define USERS 8 + +struct user { + char id; + int active; + time_t last_pkt; + int seed; + in_addr_t tun_ip; + struct sockaddr host; + int addrlen; + struct query q; + struct packet inpacket; + struct packet outpacket; +}; + +extern struct user users[USERS]; + +void init_users(in_addr_t); +int users_waiting_on_reply(); +int find_user_by_ip(uint32_t); +int find_available_user(); + +#endif