2009-02-19 16:14:09 +00:00
|
|
|
/*
|
|
|
|
* uhub - A tiny ADC p2p connection hub
|
|
|
|
* Copyright (C) 2007-2009, Jan Vidar Krey
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "uhub.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This callback function is used to clear user objects from the userlist.
|
2009-05-16 01:14:20 +00:00
|
|
|
* Should only be used in uman_shutdown().
|
2009-02-19 16:14:09 +00:00
|
|
|
*/
|
|
|
|
static void clear_user_list_callback(void* ptr)
|
|
|
|
{
|
|
|
|
if (ptr)
|
|
|
|
{
|
|
|
|
struct user* u = (struct user*) ptr;
|
|
|
|
|
|
|
|
/* Mark the user as already being disconnected.
|
|
|
|
* This prevents the hub from trying to send
|
|
|
|
* quit messages to other users.
|
|
|
|
*/
|
|
|
|
u->credentials = cred_none;
|
|
|
|
user_destroy(u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-16 01:14:20 +00:00
|
|
|
void uman_update_stats(struct hub_info* hub)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-03-04 17:36:45 +00:00
|
|
|
const int factor = TIMEOUT_STATS;
|
2009-02-19 16:14:09 +00:00
|
|
|
struct net_statistics* total;
|
|
|
|
struct net_statistics* intermediate;
|
|
|
|
net_stats_get(&intermediate, &total);
|
|
|
|
|
2009-03-04 17:36:45 +00:00
|
|
|
hub->stats.net_tx = (intermediate->tx / factor);
|
|
|
|
hub->stats.net_rx = (intermediate->rx / factor);
|
|
|
|
hub->stats.net_tx_peak = MAX(hub->stats.net_tx, hub->stats.net_tx_peak);
|
2009-03-09 23:17:49 +00:00
|
|
|
hub->stats.net_rx_peak = MAX(hub->stats.net_rx, hub->stats.net_rx_peak);
|
2009-03-04 17:36:45 +00:00
|
|
|
hub->stats.net_tx_total = total->tx;
|
|
|
|
hub->stats.net_rx_total = total->rx;
|
2009-02-19 16:14:09 +00:00
|
|
|
|
|
|
|
net_stats_reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-16 01:14:20 +00:00
|
|
|
void uman_print_stats(struct hub_info* hub)
|
2009-03-04 17:36:45 +00:00
|
|
|
{
|
|
|
|
hub_log(log_info, "Statistics users=%zu (peak_users=%zu), net_tx=%d KB/s, net_rx=%d KB/s (peak_tx=%d KB/s, peak_rx=%d KB/s)",
|
|
|
|
hub->users->count,
|
|
|
|
hub->users->count_peak,
|
|
|
|
(int) hub->stats.net_tx / 1024,
|
|
|
|
(int) hub->stats.net_rx / 1024,
|
|
|
|
(int) hub->stats.net_tx_peak / 1024,
|
|
|
|
(int) hub->stats.net_rx_peak / 1024);
|
|
|
|
}
|
|
|
|
|
2009-05-15 16:45:26 +00:00
|
|
|
#ifdef USERMANAGER_TIMER
|
2009-02-19 16:14:09 +00:00
|
|
|
static void timer_statistics(int fd, short ev, void *arg)
|
|
|
|
{
|
|
|
|
struct hub_info* hub = (struct hub_info*) arg;
|
|
|
|
struct timeval timeout = { TIMEOUT_STATS, 0 };
|
2009-05-16 01:14:20 +00:00
|
|
|
uman_update_stats(hub);
|
2009-02-19 16:14:09 +00:00
|
|
|
evtimer_set(&hub->ev_timer, timer_statistics, hub);
|
2009-03-18 01:56:49 +00:00
|
|
|
event_base_set(hub->evbase, &hub->ev_timer);
|
2009-02-19 16:14:09 +00:00
|
|
|
evtimer_add(&hub->ev_timer, &timeout);
|
|
|
|
}
|
2009-05-15 16:45:26 +00:00
|
|
|
#endif
|
2009-02-19 16:14:09 +00:00
|
|
|
|
|
|
|
|
2009-05-16 01:14:20 +00:00
|
|
|
int uman_init(struct hub_info* hub)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
|
|
|
struct user_manager* users = NULL;
|
2009-05-15 16:45:26 +00:00
|
|
|
#ifdef USERMANAGER_TIMER
|
2009-02-19 16:14:09 +00:00
|
|
|
struct timeval timeout = { TIMEOUT_STATS, 0 };
|
2009-05-15 16:45:26 +00:00
|
|
|
#endif
|
|
|
|
if (!hub)
|
|
|
|
return -1;
|
2009-02-19 16:14:09 +00:00
|
|
|
|
|
|
|
users = (struct user_manager*) hub_malloc_zero(sizeof(struct user_manager));
|
2009-05-15 16:45:26 +00:00
|
|
|
if (!users)
|
|
|
|
return -1;
|
2009-02-19 16:14:09 +00:00
|
|
|
|
|
|
|
users->list = list_create();
|
|
|
|
users->free_sid = 1;
|
|
|
|
|
|
|
|
if (!users->list)
|
|
|
|
{
|
|
|
|
list_destroy(users->list);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
hub->users = users;
|
2009-05-15 16:45:26 +00:00
|
|
|
|
|
|
|
#ifdef USERMANAGER_TIMER
|
2009-02-19 16:14:09 +00:00
|
|
|
evtimer_set(&hub->ev_timer, timer_statistics, hub);
|
2009-03-18 01:56:49 +00:00
|
|
|
event_base_set(hub->evbase, &hub->ev_timer);
|
2009-02-19 16:14:09 +00:00
|
|
|
evtimer_add(&hub->ev_timer, &timeout);
|
2009-05-15 16:45:26 +00:00
|
|
|
#endif // 0
|
2009-02-19 16:14:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-16 01:14:20 +00:00
|
|
|
int uman_shutdown(struct hub_info* hub)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-05-15 16:45:26 +00:00
|
|
|
if (!hub || !hub->users)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#ifdef USERMANAGER_TIMER
|
2009-02-19 16:14:09 +00:00
|
|
|
event_del(&hub->ev_timer);
|
2009-05-15 16:45:26 +00:00
|
|
|
#endif
|
2009-02-19 16:14:09 +00:00
|
|
|
|
2009-05-15 16:45:26 +00:00
|
|
|
if (hub->users->list)
|
|
|
|
{
|
|
|
|
list_clear(hub->users->list, &clear_user_list_callback);
|
|
|
|
list_destroy(hub->users->list);
|
|
|
|
}
|
2009-02-19 16:14:09 +00:00
|
|
|
hub_free(hub->users);
|
2009-05-15 16:45:26 +00:00
|
|
|
hub->users = 0;
|
|
|
|
|
|
|
|
return 0;
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-16 01:14:20 +00:00
|
|
|
int uman_add(struct hub_info* hub, struct user* user)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-05-15 16:45:26 +00:00
|
|
|
if (!hub || !user)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (user->hub)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
list_append(hub->users->list, user);
|
|
|
|
hub->users->count++;
|
|
|
|
hub->users->count_peak = MAX(hub->users->count, hub->users->count_peak);
|
|
|
|
|
|
|
|
hub->users->shared_size += user->limits.shared_size;
|
|
|
|
hub->users->shared_files += user->limits.shared_files;
|
2009-02-19 16:14:09 +00:00
|
|
|
|
2009-05-15 16:45:26 +00:00
|
|
|
user->hub = hub;
|
|
|
|
return 0;
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2009-05-16 01:14:20 +00:00
|
|
|
int uman_remove(struct hub_info* hub, struct user* user)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-05-15 16:45:26 +00:00
|
|
|
if (!hub || !user)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
list_remove(hub->users->list, user);
|
2009-05-19 07:38:57 +00:00
|
|
|
|
|
|
|
if (hub->users->count > 0)
|
|
|
|
{
|
|
|
|
hub->users->count--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(!"negative count!");
|
|
|
|
}
|
2009-02-19 16:14:09 +00:00
|
|
|
|
2009-05-15 16:45:26 +00:00
|
|
|
hub->users->shared_size -= user->limits.shared_size;
|
|
|
|
hub->users->shared_files -= user->limits.shared_files;
|
|
|
|
|
2009-05-16 01:06:14 +00:00
|
|
|
user->hub = 0;
|
|
|
|
|
2009-05-15 16:45:26 +00:00
|
|
|
return 0;
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-16 10:42:30 +00:00
|
|
|
struct user* uman_get_user_by_sid(struct hub_info* hub, sid_t sid)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
|
|
|
struct user* user = (struct user*) list_get_first(hub->users->list); /* iterate users */
|
|
|
|
while (user)
|
|
|
|
{
|
|
|
|
if (user->id.sid == sid)
|
|
|
|
return user;
|
|
|
|
user = (struct user*) list_get_next(hub->users->list);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-16 10:42:30 +00:00
|
|
|
struct user* uman_get_user_by_cid(struct hub_info* hub, const char* cid)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
|
|
|
struct user* user = (struct user*) list_get_first(hub->users->list); /* iterate users - only on incoming INF msg */
|
|
|
|
while (user)
|
|
|
|
{
|
|
|
|
if (strcmp(user->id.cid, cid) == 0)
|
|
|
|
return user;
|
|
|
|
user = (struct user*) list_get_next(hub->users->list);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-16 10:42:30 +00:00
|
|
|
struct user* uman_get_user_by_nick(struct hub_info* hub, const char* nick)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
|
|
|
struct user* user = (struct user*) list_get_first(hub->users->list); /* iterate users - only on incoming INF msg */
|
|
|
|
while (user)
|
|
|
|
{
|
|
|
|
if (strcmp(user->id.nick, nick) == 0)
|
|
|
|
return user;
|
|
|
|
user = (struct user*) list_get_next(hub->users->list);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-18 14:30:17 +00:00
|
|
|
int uman_send_user_list(struct hub_info* hub, struct user* target)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
user_flag_set(target, flag_user_list);
|
2009-05-18 14:30:17 +00:00
|
|
|
struct user* user = (struct user*) list_get_first(hub->users->list); /* iterate users - only on INF or PAS msg */
|
2009-02-19 16:14:09 +00:00
|
|
|
while (user)
|
|
|
|
{
|
|
|
|
if (user_is_logged_in(user))
|
|
|
|
{
|
2009-05-18 14:30:17 +00:00
|
|
|
ret = route_to_user(hub, target, user->info);
|
2009-02-19 16:14:09 +00:00
|
|
|
if (!ret)
|
|
|
|
break;
|
|
|
|
}
|
2009-05-18 14:30:17 +00:00
|
|
|
user = (struct user*) list_get_next(hub->users->list);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!target->send_queue_size)
|
|
|
|
{
|
|
|
|
user_flag_unset(target, flag_user_list);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-18 14:30:17 +00:00
|
|
|
void uman_send_quit_message(struct hub_info* hub, struct user* leaving)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
|
|
|
struct adc_message* command = adc_msg_construct(ADC_CMD_IQUI, 6);
|
|
|
|
adc_msg_add_argument(command, (const char*) sid_to_string(leaving->id.sid));
|
|
|
|
|
|
|
|
if (leaving->quit_reason == quit_banned || leaving->quit_reason == quit_kicked)
|
|
|
|
{
|
|
|
|
adc_msg_add_argument(command, ADC_QUI_FLAG_DISCONNECT);
|
|
|
|
}
|
|
|
|
|
2009-05-18 14:30:17 +00:00
|
|
|
route_to_all(hub, command);
|
2009-02-19 16:14:09 +00:00
|
|
|
adc_msg_free(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-16 01:14:20 +00:00
|
|
|
sid_t uman_get_free_sid(struct hub_info* hub)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
struct user* user;
|
|
|
|
user = (struct user*) list_get_first(hub->users->list); /* iterate normal users */
|
|
|
|
while (user)
|
|
|
|
{
|
|
|
|
if (user->sid == hub->users->free_sid)
|
|
|
|
{
|
|
|
|
hub->users->free_sid++;
|
|
|
|
if (hub->users->free_sid >= SID_MAX) hub->users->free_sid = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
user = (struct user*) list_get_next(hub->users->list);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return hub->users->free_sid++;
|
|
|
|
}
|
|
|
|
|