2007-02-11 11:21:18 +00:00
|
|
|
/*
|
2009-01-03 23:27:21 +00:00
|
|
|
* Copyright (c) 2006-2009 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
|
2007-02-11 11:21:18 +00:00
|
|
|
*
|
|
|
|
* 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 <stdio.h>
|
2007-06-17 12:19:50 +00:00
|
|
|
#include <stdint.h>
|
2007-02-11 11:21:18 +00:00
|
|
|
#include <time.h>
|
2007-06-17 12:19:50 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
2007-02-11 11:21:18 +00:00
|
|
|
#include <sys/types.h>
|
2007-06-17 12:19:50 +00:00
|
|
|
#include <fcntl.h>
|
2009-01-24 22:19:11 +00:00
|
|
|
|
|
|
|
#ifdef WINDOWS32
|
2009-03-07 00:00:58 +00:00
|
|
|
#include <winsock2.h>
|
2009-01-24 22:19:11 +00:00
|
|
|
#else
|
2007-06-17 12:19:50 +00:00
|
|
|
#include <err.h>
|
2007-02-11 11:21:18 +00:00
|
|
|
#include <arpa/inet.h>
|
2009-01-24 22:19:11 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
2007-02-11 11:21:18 +00:00
|
|
|
|
|
|
|
#include "common.h"
|
2007-06-09 16:18:59 +00:00
|
|
|
#include "encoding.h"
|
2007-02-11 11:21:18 +00:00
|
|
|
#include "user.h"
|
|
|
|
|
|
|
|
struct user users[USERS];
|
|
|
|
|
2009-01-04 12:03:35 +00:00
|
|
|
int
|
|
|
|
init_users(in_addr_t my_ip, int netbits)
|
2007-02-11 11:21:18 +00:00
|
|
|
{
|
|
|
|
int i;
|
2009-01-04 12:03:35 +00:00
|
|
|
int skip = 0;
|
2007-02-11 11:21:18 +00:00
|
|
|
char newip[16];
|
2009-01-04 12:03:35 +00:00
|
|
|
int created_users = 0;
|
|
|
|
|
|
|
|
int maxusers;
|
|
|
|
|
|
|
|
in_addr_t netmask = 0;
|
|
|
|
struct in_addr net;
|
|
|
|
struct in_addr ipstart;
|
|
|
|
|
|
|
|
for (i = 0; i < netbits; i++) {
|
|
|
|
netmask = (netmask << 1) | 1;
|
|
|
|
}
|
|
|
|
netmask <<= (32 - netbits);
|
|
|
|
net.s_addr = htonl(netmask);
|
|
|
|
ipstart.s_addr = my_ip & net.s_addr;
|
|
|
|
|
|
|
|
maxusers = (1 << (32-netbits)) - 3; /* 3: Net addr, broadcast addr, iodined addr */
|
2007-02-11 11:21:18 +00:00
|
|
|
|
|
|
|
memset(users, 0, USERS * sizeof(struct user));
|
|
|
|
for (i = 0; i < USERS; i++) {
|
2009-01-04 12:03:35 +00:00
|
|
|
in_addr_t ip;
|
2007-02-11 11:21:18 +00:00
|
|
|
users[i].id = i;
|
2009-01-04 12:03:35 +00:00
|
|
|
snprintf(newip, sizeof(newip), "0.0.0.%d", i + skip + 1);
|
|
|
|
ip = ipstart.s_addr + inet_addr(newip);
|
|
|
|
if (ip == my_ip && skip == 0) {
|
|
|
|
/* This IP was taken by iodined */
|
|
|
|
skip++;
|
|
|
|
snprintf(newip, sizeof(newip), "0.0.0.%d", i + skip + 1);
|
|
|
|
ip = ipstart.s_addr + inet_addr(newip);
|
|
|
|
}
|
|
|
|
users[i].tun_ip = ip;
|
|
|
|
net.s_addr = ip;
|
|
|
|
if (maxusers-- < 1) {
|
|
|
|
users[i].disabled = 1;
|
|
|
|
} else {
|
|
|
|
users[i].disabled = 0;
|
|
|
|
created_users++;
|
|
|
|
}
|
2008-07-12 11:36:39 +00:00
|
|
|
users[i].inpacket.len = 0;
|
|
|
|
users[i].inpacket.offset = 0;
|
|
|
|
users[i].outpacket.len = 0;
|
2007-02-11 11:21:18 +00:00
|
|
|
users[i].q.id = 0;
|
2008-12-11 22:39:06 +00:00
|
|
|
users[i].out_acked_seqno = 0;
|
|
|
|
users[i].out_acked_fragment = 0;
|
2009-01-05 18:50:46 +00:00
|
|
|
users[i].fragsize = 4096;
|
2009-08-06 19:48:55 +00:00
|
|
|
users[i].conn = CONN_DNS_NULL;
|
2007-02-11 11:21:18 +00:00
|
|
|
}
|
2009-01-04 12:03:35 +00:00
|
|
|
|
|
|
|
return created_users;
|
2007-02-11 11:21:18 +00:00
|
|
|
}
|
|
|
|
|
2008-09-14 12:44:55 +00:00
|
|
|
int
|
|
|
|
users_waiting_on_reply()
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
for (i = 0; i < USERS; i++) {
|
2009-01-04 12:03:35 +00:00
|
|
|
if (users[i].active && !users[i].disabled &&
|
|
|
|
users[i].last_pkt + 60 > time(NULL) &&
|
2008-09-14 12:44:55 +00:00
|
|
|
users[i].q.id != 0) {
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-02-11 11:21:18 +00:00
|
|
|
int
|
|
|
|
find_user_by_ip(uint32_t ip)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ret = -1;
|
|
|
|
for (i = 0; i < USERS; i++) {
|
2009-01-04 12:03:35 +00:00
|
|
|
if (users[i].active && !users[i].disabled &&
|
|
|
|
users[i].last_pkt + 60 > time(NULL) &&
|
2007-02-11 11:21:18 +00:00
|
|
|
ip == users[i].tun_ip) {
|
|
|
|
ret = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-02-11 11:51:30 +00:00
|
|
|
int
|
|
|
|
all_users_waiting_to_send()
|
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
now = time(NULL);
|
|
|
|
for (i = 0; i < USERS; i++) {
|
2009-01-04 12:03:35 +00:00
|
|
|
if (users[i].active && !users[i].disabled &&
|
|
|
|
users[i].last_pkt + 60 > now &&
|
2007-02-11 11:51:30 +00:00
|
|
|
users[i].outpacket.len == 0) {
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-02-11 11:21:18 +00:00
|
|
|
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 */
|
2009-01-04 12:03:35 +00:00
|
|
|
if ((!users[i].active || users[i].last_pkt + 60 < time(NULL)) && !users[i].disabled) {
|
2007-02-11 11:21:18 +00:00
|
|
|
users[i].active = 1;
|
|
|
|
users[i].last_pkt = time(NULL);
|
2009-08-06 19:48:55 +00:00
|
|
|
users[i].fragsize = 4096;
|
|
|
|
users[i].conn = CONN_DNS_NULL;
|
2007-02-11 11:21:18 +00:00
|
|
|
ret = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-09-14 11:34:56 +00:00
|
|
|
void
|
|
|
|
user_switch_codec(int userid, struct encoder *enc)
|
|
|
|
{
|
|
|
|
if (userid < 0 || userid >= USERS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
users[userid].encoder = enc;
|
|
|
|
}
|
2009-08-06 19:48:55 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
user_set_conn_type(int userid, enum connection c)
|
|
|
|
{
|
|
|
|
if (userid < 0 || userid >= USERS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (c < 0 || c >= CONN_MAX)
|
|
|
|
return;
|
|
|
|
|
|
|
|
users[userid].conn = c;
|
|
|
|
}
|
|
|
|
|