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-05-18 14:30:17 +00:00
|
|
|
int route_message(struct hub_info* hub, struct user* u, struct adc_message* msg)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
|
|
|
struct user* target = NULL;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void queue_command(struct user* user, struct adc_message* msg__, int offset)
|
|
|
|
{
|
|
|
|
struct adc_message* msg = adc_msg_incref(msg__);
|
|
|
|
list_append(user->send_queue, msg);
|
2009-03-16 11:28:58 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_SENDQ
|
|
|
|
hub_log(log_trace, "SENDQ: user=%p, msg=%p (%zu), offset=%d, length=%d, total_length=%d", user, msg, msg->references, offset, msg->length, user->send_queue_size);
|
|
|
|
#endif
|
2009-03-16 11:33:10 +00:00
|
|
|
|
|
|
|
user->send_queue_size += msg->length - offset;
|
|
|
|
if (list_size(user->send_queue) == 1)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
|
|
|
user->send_queue_offset = offset;
|
|
|
|
user->tm_last_write = time(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// #define ALWAYS_QUEUE_MESSAGES
|
2009-03-12 15:00:56 +00:00
|
|
|
static size_t get_max_send_queue(struct hub_info* hub)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_max_send_queue_soft(struct hub_info* hub)
|
|
|
|
{
|
|
|
|
return hub->config->max_send_buffer_soft;
|
2009-03-12 15:00:56 +00:00
|
|
|
}
|
|
|
|
|
2009-03-16 11:28:58 +00:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
*/
|
|
|
|
static int check_send_queue(struct user* user, struct adc_message* msg)
|
|
|
|
{
|
|
|
|
if (user_flag_get(user, flag_user_list))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if ((user->send_queue_size + msg->length) > get_max_send_queue(user->hub))
|
|
|
|
return -1;
|
2009-03-16 11:28:58 +00:00
|
|
|
|
|
|
|
if (user->send_queue_size > get_max_send_queue_soft(user->hub) && msg->priority < 0)
|
|
|
|
return 0;
|
|
|
|
|
2009-03-12 15:00:56 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2009-02-19 16:14:09 +00:00
|
|
|
|
2009-05-18 14:30:17 +00:00
|
|
|
int route_to_user(struct hub_info* hub, struct user* user, struct adc_message* msg)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
#if LOG_SEND_MESSAGES_WHEN_ROUTED
|
|
|
|
char* data = strndup(msg->cache, msg->length-1);
|
|
|
|
hub_log(log_protocol, "send %s: %s", sid_to_string(user->sid), data);
|
|
|
|
free(data);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ALWAYS_QUEUE_MESSAGES
|
|
|
|
if (user->send_queue_size == 0 && !user_is_disconnecting(user))
|
|
|
|
{
|
|
|
|
ret = net_send(user->sd, msg->cache, msg->length, UHUB_SEND_SIGNAL);
|
|
|
|
|
|
|
|
if (ret == msg->length)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret >= 0 || (ret == -1 && net_error() == EWOULDBLOCK))
|
|
|
|
{
|
|
|
|
queue_command(user, msg, ret);
|
|
|
|
|
|
|
|
if (user->send_queue_size && user->ev_write)
|
|
|
|
event_add(user->ev_write, NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* A socket error occured */
|
2009-05-18 14:30:17 +00:00
|
|
|
hub_disconnect_user(hub, user, quit_socket_error);
|
2009-02-19 16:14:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2009-03-12 15:00:56 +00:00
|
|
|
ret = check_send_queue(user, msg);
|
|
|
|
if (ret == -1)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
|
|
|
/* User is not able to swallow the data, let's cut our losses and disconnect. */
|
2009-05-18 14:30:17 +00:00
|
|
|
hub_disconnect_user(hub, user, quit_send_queue);
|
2009-03-12 15:00:56 +00:00
|
|
|
}
|
|
|
|
else if (ret == 1)
|
|
|
|
{
|
|
|
|
/* queue command */
|
|
|
|
queue_command(user, msg, 0);
|
|
|
|
if (user->ev_write)
|
|
|
|
event_add(user->ev_write, NULL);
|
|
|
|
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-03-12 15:00:56 +00:00
|
|
|
/* do not queue command as our soft-limits are exceeded */
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int route_to_all(struct hub_info* hub, struct adc_message* command) /* iterate users */
|
|
|
|
{
|
|
|
|
struct user* user = (struct user*) list_get_first(hub->users->list);
|
|
|
|
while (user)
|
|
|
|
{
|
2009-05-18 14:30:17 +00:00
|
|
|
route_to_user(hub, user, command);
|
2009-02-19 16:14:09 +00:00
|
|
|
user = (struct user*) list_get_next(hub->users->list);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int route_to_subscribers(struct hub_info* hub, struct adc_message* command) /* iterate users */
|
|
|
|
{
|
|
|
|
int do_send;
|
|
|
|
char* tmp;
|
|
|
|
|
|
|
|
struct user* user = (struct user*) list_get_first(hub->users->list);
|
|
|
|
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) {
|
|
|
|
user = (struct user*) list_get_next(hub->users->list);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
user = (struct user*) list_get_next(hub->users->list);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-18 14:30:17 +00:00
|
|
|
int route_info_message(struct hub_info* hub, struct 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-03-16 11:45:13 +00:00
|
|
|
const char* address = ip_convert_to_string(&u->ipaddr);
|
2009-02-19 16:14:09 +00:00
|
|
|
struct user* user = 0;
|
|
|
|
|
|
|
|
adc_msg_remove_named_argument(cmd, ADC_INF_FLAG_IPV4_ADDR);
|
|
|
|
adc_msg_add_named_argument(cmd, ADC_INF_FLAG_IPV4_ADDR, address);
|
|
|
|
|
2009-05-18 14:30:17 +00:00
|
|
|
user = (struct 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-05-18 14:30:17 +00:00
|
|
|
user = (struct user*) list_get_next(hub->users->list);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
adc_msg_free(cmd);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|