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)
|
|
|
|
{
|
2009-07-25 23:47:17 +00:00
|
|
|
struct hub_user* u = (struct hub_user*) ptr;
|
2013-03-21 23:58:14 +00:00
|
|
|
|
2009-02-19 16:14:09 +00:00
|
|
|
/* Mark the user as already being disconnected.
|
|
|
|
* This prevents the hub from trying to send
|
|
|
|
* quit messages to other users.
|
|
|
|
*/
|
2010-07-12 15:00:42 +00:00
|
|
|
u->credentials = auth_cred_none;
|
2009-02-19 16:14:09 +00:00
|
|
|
user_destroy(u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-22 23:43:38 +00:00
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
struct hub_user_manager* uman_init()
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2012-09-27 08:43:53 +00:00
|
|
|
struct hub_user_manager* users = (struct hub_user_manager*) hub_malloc_zero(sizeof(struct hub_user_manager));
|
2009-05-15 16:45:26 +00:00
|
|
|
if (!users)
|
2012-09-27 08:43:53 +00:00
|
|
|
return NULL;
|
2009-02-19 16:14:09 +00:00
|
|
|
|
|
|
|
users->list = list_create();
|
2009-08-02 20:29:12 +00:00
|
|
|
users->sids = sid_pool_create(net_get_max_sockets());
|
2009-08-02 20:53:25 +00:00
|
|
|
|
2009-02-19 16:14:09 +00:00
|
|
|
if (!users->list)
|
|
|
|
{
|
|
|
|
list_destroy(users->list);
|
2009-11-18 16:41:28 +00:00
|
|
|
hub_free(users);
|
2012-09-27 08:43:53 +00:00
|
|
|
return NULL;
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
2009-11-18 16:41:28 +00:00
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
return users;
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
int uman_shutdown(struct hub_user_manager* users)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2012-09-27 08:43:53 +00:00
|
|
|
if (!users)
|
2009-05-15 16:45:26 +00:00
|
|
|
return -1;
|
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
if (users->list)
|
2009-05-15 16:45:26 +00:00
|
|
|
{
|
2012-09-27 08:43:53 +00:00
|
|
|
list_clear(users->list, &clear_user_list_callback);
|
|
|
|
list_destroy(users->list);
|
2009-05-15 16:45:26 +00:00
|
|
|
}
|
2012-09-27 08:43:53 +00:00
|
|
|
sid_pool_destroy(users->sids);
|
2010-01-22 23:43:38 +00:00
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
hub_free(users);
|
2009-05-15 16:45:26 +00:00
|
|
|
return 0;
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
int uman_add(struct hub_user_manager* users, struct hub_user* user)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2012-09-27 08:43:53 +00:00
|
|
|
if (!users || !user)
|
2009-05-15 16:45:26 +00:00
|
|
|
return -1;
|
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
list_append(users->list, user);
|
|
|
|
users->count++;
|
|
|
|
users->count_peak = MAX(users->count, users->count_peak);
|
2009-05-15 16:45:26 +00:00
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
users->shared_size += user->limits.shared_size;
|
|
|
|
users->shared_files += user->limits.shared_files;
|
2009-05-15 16:45:26 +00:00
|
|
|
return 0;
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
int uman_remove(struct hub_user_manager* users, struct hub_user* user)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2012-09-27 08:43:53 +00:00
|
|
|
if (!users || !user)
|
2009-05-15 16:45:26 +00:00
|
|
|
return -1;
|
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
list_remove(users->list, user);
|
2010-02-16 16:41:56 +00:00
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
if (users->count > 0)
|
2009-05-19 07:38:57 +00:00
|
|
|
{
|
2012-09-27 08:43:53 +00:00
|
|
|
users->count--;
|
2009-05-19 07:38:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-02 19:06:46 +00:00
|
|
|
uhub_assert(!"negative count!");
|
2009-05-19 07:38:57 +00:00
|
|
|
}
|
2010-02-16 16:41:56 +00:00
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
users->shared_size -= user->limits.shared_size;
|
|
|
|
users->shared_files -= user->limits.shared_files;
|
2009-05-15 16:45:26 +00:00
|
|
|
return 0;
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
struct hub_user* uman_get_user_by_sid(struct hub_user_manager* users, sid_t sid)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2012-09-27 08:43:53 +00:00
|
|
|
return sid_lookup(users->sids, sid);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
struct hub_user* uman_get_user_by_cid(struct hub_user_manager* users, const char* cid)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2013-03-21 23:58:14 +00:00
|
|
|
struct hub_user* user;
|
|
|
|
LIST_FOREACH(struct hub_user*, user, users->list,
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
|
|
|
if (strcmp(user->id.cid, cid) == 0)
|
|
|
|
return user;
|
2013-03-21 23:58:14 +00:00
|
|
|
});
|
2009-02-19 16:14:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
struct hub_user* uman_get_user_by_nick(struct hub_user_manager* users, const char* nick)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2013-03-21 23:58:14 +00:00
|
|
|
struct hub_user* user;
|
|
|
|
LIST_FOREACH(struct hub_user*, user, users->list,
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
|
|
|
if (strcmp(user->id.nick, nick) == 0)
|
|
|
|
return user;
|
2013-03-21 23:58:14 +00:00
|
|
|
});
|
2009-02-19 16:14:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
size_t uman_get_user_by_addr(struct hub_user_manager* users, struct linked_list* target, struct ip_range* range)
|
2009-07-26 02:02:14 +00:00
|
|
|
{
|
|
|
|
size_t num = 0;
|
2013-03-21 23:58:14 +00:00
|
|
|
struct hub_user* user;
|
|
|
|
LIST_FOREACH(struct hub_user*, user, users->list,
|
2009-07-26 02:02:14 +00:00
|
|
|
{
|
2009-09-28 21:15:15 +00:00
|
|
|
if (ip_in_range(&user->id.addr, range))
|
2009-07-26 02:02:14 +00:00
|
|
|
{
|
2012-09-27 08:43:53 +00:00
|
|
|
list_append(target, user);
|
2009-07-26 02:02:14 +00:00
|
|
|
num++;
|
|
|
|
}
|
2013-03-21 23:58:14 +00:00
|
|
|
});
|
2009-07-26 02:02:14 +00:00
|
|
|
return num;
|
|
|
|
}
|
2009-02-19 16:14:09 +00:00
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
int uman_send_user_list(struct hub_info* hub, struct hub_user_manager* users, struct hub_user* target)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
|
|
|
int ret = 1;
|
2010-01-28 00:06:41 +00:00
|
|
|
struct hub_user* user;
|
2009-02-19 16:14:09 +00:00
|
|
|
user_flag_set(target, flag_user_list);
|
2013-03-21 23:58:14 +00:00
|
|
|
|
|
|
|
LIST_FOREACH(struct hub_user*, user, users->list,
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2013-03-21 23:58:14 +00:00
|
|
|
});
|
2010-02-16 16:41:56 +00:00
|
|
|
|
2009-05-26 17:46:51 +00:00
|
|
|
#if 0
|
|
|
|
FIXME: FIXME FIXME handle send queue excess
|
2009-02-19 16:14:09 +00:00
|
|
|
if (!target->send_queue_size)
|
|
|
|
{
|
|
|
|
user_flag_unset(target, flag_user_list);
|
|
|
|
}
|
2009-05-26 17:46:51 +00:00
|
|
|
#endif
|
2009-02-19 16:14:09 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
void uman_send_quit_message(struct hub_info* hub, struct hub_user_manager* users, struct hub_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));
|
2010-02-16 16:41:56 +00:00
|
|
|
|
2009-02-19 16:14:09 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-09-27 08:43:53 +00:00
|
|
|
sid_t uman_get_free_sid(struct hub_user_manager* users, struct hub_user* user)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2012-09-27 08:43:53 +00:00
|
|
|
sid_t sid = sid_alloc(users->sids, user);
|
2009-08-02 19:04:10 +00:00
|
|
|
user->id.sid = sid;
|
|
|
|
return sid;
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|