#11 moved user code to user.c

This commit is contained in:
Erik Ekman 2007-02-11 11:21:18 +00:00
parent 265332f39b
commit 23ad29522b
5 changed files with 145 additions and 88 deletions

View File

@ -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`

View File

@ -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);

View File

@ -18,7 +18,6 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
@ -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]);

99
src/user.c Normal file
View File

@ -0,0 +1,99 @@
/*
* Copyright (c) 2006 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
*
* 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 <string.h>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#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;
}

42
src/user.h Normal file
View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2006 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
*
* 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