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"
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
int route_message(struct hub_info* hub, struct hub_user* u, struct adc_message* msg)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-07-25 23:47:17 +00:00
|
|
|
struct hub_user* target = NULL;
|
2009-02-19 16:14:09 +00:00
|
|
|
|
|
|
|
switch (msg->cache[0])
|
|
|
|
{
|
|
|
|
case 'B': /* Broadcast to all logged in clients */
|
2009-05-18 14:30:17 +00:00
|
|
|
route_to_all(hub, msg);
|
2009-02-19 16:14:09 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D':
|
2009-05-18 14:30:17 +00:00
|
|
|
target = uman_get_user_by_sid(hub, msg->target);
|
2009-02-19 16:14:09 +00:00
|
|
|
if (target)
|
|
|
|
{
|
2009-05-18 14:30:17 +00:00
|
|
|
route_to_user(hub, target, msg);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'E':
|
2009-05-18 14:30:17 +00:00
|
|
|
target = uman_get_user_by_sid(hub, msg->target);
|
2009-02-19 16:14:09 +00:00
|
|
|
if (target)
|
|
|
|
{
|
2009-05-18 14:30:17 +00:00
|
|
|
route_to_user(hub, target, msg);
|
|
|
|
route_to_user(hub, u, msg);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'F':
|
2009-05-18 14:30:17 +00:00
|
|
|
route_to_subscribers(hub, msg);
|
2009-02-19 16:14:09 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Ignore the message */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-01-02 01:39:25 +00:00
|
|
|
static size_t get_max_send_queue(struct hub_info* hub)
|
2009-03-12 15:00:56 +00:00
|
|
|
{
|
2009-03-16 11:28:58 +00:00
|
|
|
/* TODO: More dynamic send queue limit, for instance:
|
|
|
|
* return MAX(hub->config->max_send_buffer, (hub->config->max_recv_buffer * hub_get_user_count(hub)));
|
|
|
|
*/
|
|
|
|
return hub->config->max_send_buffer;
|
|
|
|
}
|
|
|
|
|
2011-01-02 01:39:25 +00:00
|
|
|
static size_t get_max_send_queue_soft(struct hub_info* hub)
|
2009-03-16 11:28:58 +00:00
|
|
|
{
|
|
|
|
return hub->config->max_send_buffer_soft;
|
2009-03-12 15:00:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @return 1 if send queue is OK.
|
|
|
|
* -1 if send queue is overflowed
|
|
|
|
* 0 if soft send queue is overflowed (not implemented at the moment)
|
|
|
|
*/
|
2011-01-02 01:39:25 +00:00
|
|
|
static int check_send_queue(struct hub_info* hub, struct hub_user* user, struct adc_message* msg)
|
2009-03-12 15:00:56 +00:00
|
|
|
{
|
|
|
|
if (user_flag_get(user, flag_user_list))
|
|
|
|
return 1;
|
|
|
|
|
2009-08-06 22:22:30 +00:00
|
|
|
if ((user->send_queue->size + msg->length) > get_max_send_queue(hub))
|
2009-10-30 17:31:27 +00:00
|
|
|
{
|
|
|
|
LOG_WARN("send queue overflowed, message discarded.");
|
2009-03-12 15:00:56 +00:00
|
|
|
return -1;
|
2009-10-30 17:31:27 +00:00
|
|
|
}
|
2009-03-16 11:28:58 +00:00
|
|
|
|
2009-11-18 16:41:28 +00:00
|
|
|
if (user->send_queue->size > get_max_send_queue_soft(hub))
|
2009-10-30 17:31:27 +00:00
|
|
|
{
|
|
|
|
LOG_WARN("send queue soft overflowed.");
|
2009-03-16 11:28:58 +00:00
|
|
|
return 0;
|
2009-10-30 17:31:27 +00:00
|
|
|
}
|
2009-03-16 11:28:58 +00:00
|
|
|
|
2009-03-12 15:00:56 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2009-02-19 16:14:09 +00:00
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
int route_to_user(struct hub_info* hub, struct hub_user* user, struct adc_message* msg)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-05-26 19:05:06 +00:00
|
|
|
#ifdef DEBUG_SENDQ
|
2009-02-19 16:14:09 +00:00
|
|
|
char* data = strndup(msg->cache, msg->length-1);
|
2009-07-25 23:38:38 +00:00
|
|
|
LOG_PROTO("send %s: \"%s\"", sid_to_string(user->id.sid), data);
|
2009-02-19 16:14:09 +00:00
|
|
|
free(data);
|
|
|
|
#endif
|
|
|
|
|
2009-10-07 15:37:31 +00:00
|
|
|
if (!user->connection)
|
|
|
|
return 0;
|
|
|
|
|
2010-02-15 18:03:19 +00:00
|
|
|
assert(msg->cache && *msg->cache);
|
|
|
|
|
2009-08-06 22:22:30 +00:00
|
|
|
if (hub_sendq_is_empty(user->send_queue) && !user_flag_get(user, flag_pipeline))
|
2009-06-22 18:25:22 +00:00
|
|
|
{
|
2009-06-22 23:57:26 +00:00
|
|
|
/* Perform oportunistic write */
|
2009-08-06 22:22:30 +00:00
|
|
|
hub_sendq_add(user->send_queue, msg);
|
2009-06-22 23:57:26 +00:00
|
|
|
handle_net_write(user);
|
2009-06-22 18:25:22 +00:00
|
|
|
}
|
2009-07-19 00:12:50 +00:00
|
|
|
else
|
2009-06-22 18:25:22 +00:00
|
|
|
{
|
2009-07-19 00:31:23 +00:00
|
|
|
if (check_send_queue(hub, user, msg) >= 0)
|
|
|
|
{
|
2009-08-06 22:22:30 +00:00
|
|
|
hub_sendq_add(user->send_queue, msg);
|
2009-07-25 01:54:59 +00:00
|
|
|
if (!user_flag_get(user, flag_pipeline))
|
|
|
|
user_net_io_want_write(user);
|
2009-07-19 00:31:23 +00:00
|
|
|
}
|
2009-06-22 18:25:22 +00:00
|
|
|
}
|
2009-02-19 16:14:09 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
int route_flush_pipeline(struct hub_info* hub, struct hub_user* u)
|
2009-07-25 01:54:59 +00:00
|
|
|
{
|
2009-08-06 22:22:30 +00:00
|
|
|
if (hub_sendq_is_empty(u->send_queue))
|
2009-07-25 01:54:59 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
handle_net_write(u);
|
|
|
|
user_flag_unset(u, flag_pipeline);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-19 16:14:09 +00:00
|
|
|
int route_to_all(struct hub_info* hub, struct adc_message* command) /* iterate users */
|
|
|
|
{
|
2009-07-25 23:47:17 +00:00
|
|
|
struct hub_user* user = (struct hub_user*) list_get_first(hub->users->list);
|
2009-02-19 16:14:09 +00:00
|
|
|
while (user)
|
|
|
|
{
|
2009-05-18 14:30:17 +00:00
|
|
|
route_to_user(hub, user, command);
|
2009-07-25 23:47:17 +00:00
|
|
|
user = (struct hub_user*) list_get_next(hub->users->list);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
2010-05-25 20:21:56 +00:00
|
|
|
|
2009-02-19 16:14:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int route_to_subscribers(struct hub_info* hub, struct adc_message* command) /* iterate users */
|
|
|
|
{
|
|
|
|
int do_send;
|
|
|
|
char* tmp;
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
struct hub_user* user = (struct hub_user*) list_get_first(hub->users->list);
|
2009-02-19 16:14:09 +00:00
|
|
|
while (user)
|
|
|
|
{
|
|
|
|
if (user->feature_cast)
|
|
|
|
{
|
|
|
|
do_send = 1;
|
|
|
|
|
|
|
|
tmp = list_get_first(command->feature_cast_include);
|
|
|
|
while (tmp)
|
|
|
|
{
|
|
|
|
if (!user_have_feature_cast_support(user, tmp))
|
|
|
|
{
|
|
|
|
do_send = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tmp = list_get_next(command->feature_cast_include);;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!do_send) {
|
2009-07-25 23:47:17 +00:00
|
|
|
user = (struct hub_user*) list_get_next(hub->users->list);
|
2009-02-19 16:14:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = list_get_first(command->feature_cast_exclude);
|
|
|
|
while (tmp)
|
|
|
|
{
|
|
|
|
if (user_have_feature_cast_support(user, tmp))
|
|
|
|
{
|
|
|
|
do_send = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tmp = list_get_next(command->feature_cast_exclude);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (do_send)
|
|
|
|
{
|
2009-05-18 14:30:17 +00:00
|
|
|
route_to_user(hub, user, command);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-25 23:47:17 +00:00
|
|
|
user = (struct hub_user*) list_get_next(hub->users->list);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
int route_info_message(struct hub_info* hub, struct hub_user* u)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
|
|
|
if (!user_is_nat_override(u))
|
|
|
|
{
|
2009-05-18 14:30:17 +00:00
|
|
|
return route_to_all(hub, u->info);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct adc_message* cmd = adc_msg_copy(u->info);
|
2009-09-28 21:15:15 +00:00
|
|
|
const char* address = user_get_address(u);
|
2009-07-25 23:47:17 +00:00
|
|
|
struct hub_user* user = 0;
|
2009-02-19 16:14:09 +00:00
|
|
|
|
|
|
|
adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_IPV4_ADDR);
|
|
|
|
adc_msg_add_named_argument(cmd, ADC_INF_FLAG_IPV4_ADDR, address);
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
user = (struct hub_user*) list_get_first(hub->users->list);
|
2009-02-19 16:14:09 +00:00
|
|
|
while (user)
|
|
|
|
{
|
|
|
|
if (user_is_nat_override(user))
|
2009-05-18 14:30:17 +00:00
|
|
|
route_to_user(hub, user, cmd);
|
2009-02-19 16:14:09 +00:00
|
|
|
else
|
2009-05-18 14:30:17 +00:00
|
|
|
route_to_user(hub, user, u->info);
|
2009-02-19 16:14:09 +00:00
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
user = (struct hub_user*) list_get_next(hub->users->list);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
adc_msg_free(cmd);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|