2007-02-11 13:21:18 +02:00
|
|
|
/*
|
2014-06-01 09:46:42 +03:00
|
|
|
* Copyright (c) 2006-2014 Erik Ekman <yarrick@kryo.se>,
|
|
|
|
* 2006-2009 Bjorn Andersson <flex@kryo.se>
|
2007-02-11 13:21:18 +02:00
|
|
|
*
|
2014-08-07 22:14:10 +03:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
2007-02-11 13:21:18 +02:00
|
|
|
* 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 15:19:50 +03:00
|
|
|
#include <stdint.h>
|
2007-02-11 13:21:18 +02:00
|
|
|
#include <time.h>
|
2007-06-17 15:19:50 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2009-01-25 00:19:11 +02:00
|
|
|
|
|
|
|
#ifdef WINDOWS32
|
2009-03-07 02:00:58 +02:00
|
|
|
#include <winsock2.h>
|
2009-01-25 00:19:11 +02:00
|
|
|
#else
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
2007-02-11 13:21:18 +02:00
|
|
|
|
|
|
|
#include "common.h"
|
2007-06-09 19:18:59 +03:00
|
|
|
#include "encoding.h"
|
2007-02-11 13:21:18 +02:00
|
|
|
#include "user.h"
|
|
|
|
|
2012-02-05 01:49:30 +02:00
|
|
|
struct tun_user *users;
|
2011-04-23 21:55:59 +03:00
|
|
|
unsigned usercount;
|
2007-02-11 13:21:18 +02:00
|
|
|
|
2017-03-11 12:36:54 +02:00
|
|
|
int init_users(in_addr_t my_ip, int netbits)
|
2007-02-11 13:21:18 +02:00
|
|
|
{
|
|
|
|
int i;
|
2009-01-04 14:03:35 +02:00
|
|
|
int skip = 0;
|
2007-02-11 13:21:18 +02:00
|
|
|
char newip[16];
|
2009-01-04 14:03:35 +02:00
|
|
|
|
|
|
|
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 */
|
2011-04-23 21:55:59 +03:00
|
|
|
usercount = MIN(maxusers, USERS);
|
2014-06-01 09:34:18 +03:00
|
|
|
|
2012-02-05 01:49:30 +02:00
|
|
|
users = calloc(usercount, sizeof(struct tun_user));
|
2011-04-23 21:55:59 +03:00
|
|
|
for (i = 0; i < usercount; i++) {
|
2009-01-04 14:03:35 +02:00
|
|
|
in_addr_t ip;
|
2007-02-11 13:21:18 +02:00
|
|
|
users[i].id = i;
|
2009-01-04 14:03:35 +02: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;
|
2011-04-23 21:55:59 +03:00
|
|
|
users[i].disabled = 0;
|
2014-06-16 22:12:49 +03:00
|
|
|
users[i].authenticated = 0;
|
|
|
|
users[i].authenticated_raw = 0;
|
2016-10-22 07:46:48 +03:00
|
|
|
users[i].options_locked = 0;
|
2009-09-21 00:10:39 +03:00
|
|
|
users[i].active = 0;
|
|
|
|
/* Rest is reset on login ('V' packet) */
|
2007-02-11 13:21:18 +02:00
|
|
|
}
|
2009-01-04 14:03:35 +02:00
|
|
|
|
2011-04-23 21:55:59 +03:00
|
|
|
return usercount;
|
2007-02-11 13:21:18 +02:00
|
|
|
}
|
|
|
|
|
2017-03-11 12:36:54 +02:00
|
|
|
const char *users_get_first_ip(void)
|
2010-02-08 18:50:45 +02:00
|
|
|
{
|
|
|
|
struct in_addr ip;
|
|
|
|
ip.s_addr = users[0].tun_ip;
|
2010-03-02 00:05:55 +02:00
|
|
|
return strdup(inet_ntoa(ip));
|
2010-02-08 18:50:45 +02:00
|
|
|
}
|
|
|
|
|
2017-03-11 12:36:54 +02:00
|
|
|
int find_user_by_ip(uint32_t ip)
|
2007-02-11 13:21:18 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ret = -1;
|
2011-04-23 21:55:59 +03:00
|
|
|
for (i = 0; i < usercount; i++) {
|
2014-06-16 22:12:49 +03:00
|
|
|
if (users[i].active &&
|
|
|
|
users[i].authenticated &&
|
|
|
|
!users[i].disabled &&
|
2009-01-04 14:03:35 +02:00
|
|
|
users[i].last_pkt + 60 > time(NULL) &&
|
2007-02-11 13:21:18 +02:00
|
|
|
ip == users[i].tun_ip) {
|
|
|
|
ret = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-09-21 00:10:39 +03:00
|
|
|
/* If this returns true, then reading from tun device is blocked.
|
|
|
|
So only return true when all clients have at least one packet in
|
|
|
|
the outpacket-queue, so that sending back-to-back is possible
|
|
|
|
without going through another select loop.
|
|
|
|
*/
|
2017-03-11 12:36:54 +02:00
|
|
|
int all_users_waiting_to_send(void)
|
2007-02-11 13:51:30 +02:00
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
now = time(NULL);
|
2011-04-23 21:55:59 +03:00
|
|
|
for (i = 0; i < usercount; i++) {
|
2009-01-04 14:03:35 +02:00
|
|
|
if (users[i].active && !users[i].disabled &&
|
|
|
|
users[i].last_pkt + 60 > now &&
|
2014-06-01 09:34:18 +03:00
|
|
|
((users[i].conn == CONN_RAW_UDP) ||
|
|
|
|
((users[i].conn == CONN_DNS_NULL)
|
2009-09-21 00:10:39 +03:00
|
|
|
#ifdef OUTPACKETQ_LEN
|
|
|
|
&& users[i].outpacketq_filled < 1
|
|
|
|
#else
|
|
|
|
&& users[i].outpacket.len == 0
|
|
|
|
#endif
|
|
|
|
))) {
|
2009-08-06 23:42:26 +03:00
|
|
|
|
2007-02-11 13:51:30 +02:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-11 12:36:54 +02:00
|
|
|
int find_available_user(void)
|
2007-02-11 13:21:18 +02:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
int i;
|
2011-04-23 21:55:59 +03:00
|
|
|
for (i = 0; i < usercount; i++) {
|
2007-02-11 13:21:18 +02:00
|
|
|
/* Not used at all or not used in one minute */
|
2009-01-04 14:03:35 +02:00
|
|
|
if ((!users[i].active || users[i].last_pkt + 60 < time(NULL)) && !users[i].disabled) {
|
2007-02-11 13:21:18 +02:00
|
|
|
users[i].active = 1;
|
2014-06-16 22:12:49 +03:00
|
|
|
users[i].authenticated = 0;
|
|
|
|
users[i].authenticated_raw = 0;
|
2016-10-22 07:46:48 +03:00
|
|
|
users[i].options_locked = 0;
|
2007-02-11 13:21:18 +02:00
|
|
|
users[i].last_pkt = time(NULL);
|
2009-08-06 22:48:55 +03:00
|
|
|
users[i].fragsize = 4096;
|
|
|
|
users[i].conn = CONN_DNS_NULL;
|
2007-02-11 13:21:18 +02:00
|
|
|
ret = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-03-12 00:35:27 +02:00
|
|
|
void user_switch_codec(int userid, const struct encoder *enc)
|
2008-09-14 14:34:56 +03:00
|
|
|
{
|
2011-04-23 21:55:59 +03:00
|
|
|
if (userid < 0 || userid >= usercount)
|
2008-09-14 14:34:56 +03:00
|
|
|
return;
|
2014-06-01 09:34:18 +03:00
|
|
|
|
2008-09-14 14:34:56 +03:00
|
|
|
users[userid].encoder = enc;
|
|
|
|
}
|
2009-08-06 22:48:55 +03:00
|
|
|
|
2017-03-11 12:36:54 +02:00
|
|
|
void user_set_conn_type(int userid, enum connection c)
|
2009-08-06 22:48:55 +03:00
|
|
|
{
|
2011-04-23 21:55:59 +03:00
|
|
|
if (userid < 0 || userid >= usercount)
|
2009-08-06 22:48:55 +03:00
|
|
|
return;
|
|
|
|
|
2014-08-09 04:59:10 +03:00
|
|
|
if (c < CONN_RAW_UDP || c >= CONN_MAX)
|
2009-08-06 22:48:55 +03:00
|
|
|
return;
|
2014-06-01 09:34:18 +03:00
|
|
|
|
2009-08-06 22:48:55 +03:00
|
|
|
users[userid].conn = c;
|
|
|
|
}
|
2014-06-01 09:34:18 +03:00
|
|
|
|