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"
|
|
|
|
|
|
|
|
|
|
|
|
void net_on_read(int fd, short ev, void *arg)
|
|
|
|
{
|
|
|
|
static char buf[MAX_RECV_BUF];
|
|
|
|
struct user* user = (struct user*) arg;
|
|
|
|
char* pos;
|
2009-03-21 01:58:53 +00:00
|
|
|
size_t offset;
|
|
|
|
size_t buflen;
|
2009-02-19 16:14:09 +00:00
|
|
|
ssize_t size;
|
|
|
|
int more = 1;
|
|
|
|
int flag_close = 0;
|
|
|
|
|
|
|
|
hub_log(log_trace, "net_on_read() : fd=%d, ev=%d, arg=%p", fd, (int) ev, arg);
|
|
|
|
|
|
|
|
if (ev == EV_TIMEOUT)
|
|
|
|
{
|
|
|
|
more = 0;
|
|
|
|
if (user_is_connecting(user))
|
|
|
|
{
|
|
|
|
flag_close = quit_timeout;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-05-15 16:45:26 +00:00
|
|
|
// FIXME: hub is not neccesarily set!
|
|
|
|
// hub_send_ping(hub, user);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (more)
|
|
|
|
{
|
|
|
|
offset = 0;
|
|
|
|
if (user->recv_buf)
|
|
|
|
{
|
|
|
|
memcpy(buf, user->recv_buf, user->recv_buf_offset);
|
|
|
|
offset = user->recv_buf_offset;
|
|
|
|
}
|
2009-03-21 01:58:53 +00:00
|
|
|
|
2009-02-19 16:14:09 +00:00
|
|
|
size = net_recv(fd, &buf[offset], MAX_RECV_BUF - offset, 0);
|
|
|
|
if (size == -1)
|
|
|
|
{
|
|
|
|
if (net_error() != EWOULDBLOCK)
|
|
|
|
flag_close = quit_socket_error;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (size == 0)
|
|
|
|
{
|
|
|
|
flag_close = quit_disconnected;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buflen = offset + size;
|
2009-03-21 01:58:53 +00:00
|
|
|
ssize_t handled = 0;
|
|
|
|
|
|
|
|
while ((pos = memchr(&buf[handled], '\n', (buflen - handled))))
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
|
|
|
pos[0] = '\0';
|
2009-03-21 01:58:53 +00:00
|
|
|
size_t msglen = &pos[0] - &buf[handled];
|
|
|
|
|
|
|
|
if (user_flag_get(user, flag_maxbuf))
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-03-21 01:58:53 +00:00
|
|
|
user_flag_unset(user, flag_maxbuf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (msglen < user->hub->config->max_recv_buffer)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-05-15 16:45:26 +00:00
|
|
|
// FIXME: hub is not set????
|
|
|
|
if (hub_handle_message(user->hub, user, &buf[handled], msglen) == -1)
|
2009-03-21 01:58:53 +00:00
|
|
|
{
|
|
|
|
flag_close = quit_protocol_error;
|
|
|
|
more = 0;
|
|
|
|
break;
|
|
|
|
}
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-21 01:58:53 +00:00
|
|
|
handled += msglen;
|
|
|
|
handled++;
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
2009-03-21 01:58:53 +00:00
|
|
|
if (handled == 0 && user_flag_get(user, flag_maxbuf))
|
|
|
|
handled = buflen;
|
|
|
|
|
|
|
|
if (!more)
|
|
|
|
break;
|
2009-02-19 16:14:09 +00:00
|
|
|
|
2009-03-21 01:58:53 +00:00
|
|
|
if (handled < buflen)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-03-21 01:58:53 +00:00
|
|
|
if ((buflen - handled) > user->hub->config->max_recv_buffer)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-03-21 01:58:53 +00:00
|
|
|
user_flag_set(user, flag_maxbuf);
|
|
|
|
hub_free(user->recv_buf);
|
|
|
|
user->recv_buf = 0;
|
|
|
|
user->recv_buf_offset = 0;
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-03-21 01:58:53 +00:00
|
|
|
if (!user->recv_buf)
|
|
|
|
user->recv_buf = hub_malloc(user->hub->config->max_recv_buffer);
|
|
|
|
|
|
|
|
if (user->recv_buf)
|
|
|
|
{
|
|
|
|
memcpy(user->recv_buf, &buf[handled], buflen - handled);
|
|
|
|
user->recv_buf_offset = buflen - handled;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flag_close = quit_memory_error;
|
|
|
|
break;
|
|
|
|
}
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (user->recv_buf)
|
|
|
|
{
|
|
|
|
hub_free(user->recv_buf);
|
|
|
|
user->recv_buf = 0;
|
|
|
|
user->recv_buf_offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flag_close)
|
|
|
|
{
|
|
|
|
user_disconnect(user, flag_close);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (user_is_logged_in(user))
|
|
|
|
{
|
|
|
|
if (user->ev_read)
|
|
|
|
{
|
|
|
|
struct timeval timeout = { TIMEOUT_IDLE, 0 };
|
|
|
|
event_add(user->ev_read, &timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (user_is_connecting(user))
|
|
|
|
{
|
|
|
|
if (user->ev_read)
|
|
|
|
{
|
|
|
|
struct timeval timeout = { TIMEOUT_HANDSHAKE, 0 };
|
|
|
|
event_add(user->ev_read, &timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void net_on_write(int fd, short ev, void *arg)
|
|
|
|
{
|
|
|
|
struct user* user = (struct user*) arg;
|
|
|
|
struct adc_message* msg;
|
|
|
|
int ret;
|
|
|
|
int length;
|
|
|
|
int close_flag = 0;
|
|
|
|
|
|
|
|
msg = list_get_first(user->send_queue);
|
|
|
|
while (msg)
|
|
|
|
{
|
|
|
|
length = msg->length - user->send_queue_offset;
|
|
|
|
ret = net_send(user->sd, &msg->cache[user->send_queue_offset], length, UHUB_SEND_SIGNAL);
|
|
|
|
|
|
|
|
if (ret == 0 || (ret == -1 && net_error() == EWOULDBLOCK))
|
|
|
|
{
|
|
|
|
close_flag = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (ret > 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
user->tm_last_write = time(NULL);
|
|
|
|
|
|
|
|
if (ret == length)
|
|
|
|
{
|
2009-03-14 01:09:45 +00:00
|
|
|
#ifdef DEBUG_SENDQ
|
2009-03-16 11:28:58 +00:00
|
|
|
hub_log(log_error, "SENDQ: sent=%d bytes/%d (all), send_queue_size=%d, offset=%d", ret, (int) msg->length, user->send_queue_size, user->send_queue_offset);
|
2009-03-14 01:09:45 +00:00
|
|
|
#endif
|
2009-02-19 16:14:09 +00:00
|
|
|
user->send_queue_size -= ret;
|
|
|
|
user->send_queue_offset = 0;
|
2009-03-16 11:28:58 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_SENDQ
|
2009-03-13 16:07:06 +00:00
|
|
|
if ((user->send_queue_size < 0) || (user->send_queue_offset < 0))
|
|
|
|
{
|
|
|
|
hub_log(log_error, "INVALID: send_queue_size=%d, send_queue_offset=%d", user->send_queue_size, user->send_queue_offset);
|
|
|
|
}
|
2009-03-16 11:40:12 +00:00
|
|
|
#endif
|
2009-03-13 00:29:21 +00:00
|
|
|
|
2009-02-19 16:14:09 +00:00
|
|
|
list_remove(user->send_queue, msg);
|
|
|
|
|
|
|
|
if (user_flag_get(user, flag_user_list) && (msg == user->info || user->send_queue_size == 0))
|
|
|
|
{
|
|
|
|
user_flag_unset(user, flag_user_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
adc_msg_free(msg);
|
|
|
|
msg = 0;
|
|
|
|
|
|
|
|
if (user->send_queue_size == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-03-14 01:09:45 +00:00
|
|
|
#ifdef DEBUG_SENDQ
|
2009-03-16 11:28:58 +00:00
|
|
|
hub_log(log_error, "SENDQ: sent=%d bytes/%d (part), send_queue_size=%d, offset=%d", ret, (int) msg->length, user->send_queue_size, user->send_queue_offset);
|
2009-03-14 01:09:45 +00:00
|
|
|
#endif
|
2009-02-19 16:14:09 +00:00
|
|
|
user->send_queue_size -= ret;
|
2009-03-13 15:51:27 +00:00
|
|
|
user->send_queue_offset += ret;
|
2009-03-13 00:29:21 +00:00
|
|
|
|
2009-03-16 11:28:58 +00:00
|
|
|
#ifdef DEBUG_SENDQ
|
2009-03-14 01:09:45 +00:00
|
|
|
if ((user->send_queue_size < 0) || (user->send_queue_offset < 0) || (user->send_queue_offset > msg->length))
|
2009-03-13 16:07:06 +00:00
|
|
|
{
|
|
|
|
hub_log(log_error, "INVALID: send_queue_size=%d, send_queue_offset=%d", user->send_queue_size, user->send_queue_offset);
|
|
|
|
}
|
2009-03-16 11:28:58 +00:00
|
|
|
#endif
|
2009-02-19 16:14:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
close_flag = quit_socket_error;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
msg = list_get_first(user->send_queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (close_flag)
|
|
|
|
{
|
|
|
|
user_disconnect(user, close_flag);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (user->send_queue_size > 0 && user->ev_write)
|
|
|
|
event_add(user->ev_write, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void net_on_accept(int server_fd, short ev, void *arg)
|
|
|
|
{
|
|
|
|
struct hub_info* hub = (struct hub_info*) arg;
|
|
|
|
struct user* user = 0;
|
2009-03-23 14:05:27 +00:00
|
|
|
struct ip_addr_encap ipaddr;
|
2009-02-19 16:14:09 +00:00
|
|
|
const char* addr;
|
|
|
|
struct timeval timeout = { TIMEOUT_CONNECTED, 0 };
|
|
|
|
|
2009-03-19 00:27:34 +00:00
|
|
|
for (;;)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-03-23 14:05:27 +00:00
|
|
|
int fd = net_accept(server_fd, &ipaddr);
|
2009-02-19 16:14:09 +00:00
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
if (net_error() == EWOULDBLOCK)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hub_log(log_error, "Accept error: %d %s", net_error(), strerror(net_error()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-23 14:05:27 +00:00
|
|
|
addr = ip_convert_to_string(&ipaddr);
|
2009-02-19 16:14:09 +00:00
|
|
|
|
|
|
|
/* FIXME: Should have a plugin log this */
|
|
|
|
hub_log(log_trace, "Got connection from %s", addr);
|
|
|
|
|
|
|
|
/* FIXME: A plugin should perform this check: is IP banned? */
|
|
|
|
if (acl_is_ip_banned(hub->acl, addr))
|
|
|
|
{
|
|
|
|
hub_log(log_info, "Denied [%s] (IP banned)", addr);
|
|
|
|
net_close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
user = user_create(hub, fd);
|
|
|
|
if (!user)
|
|
|
|
{
|
|
|
|
hub_log(log_error, "Unable to create user after socket accepted. Out of memory?");
|
|
|
|
net_close(fd);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store IP address in user object */
|
2009-03-23 14:05:27 +00:00
|
|
|
memcpy(&user->ipaddr, &ipaddr, sizeof(ipaddr));
|
2009-02-19 16:14:09 +00:00
|
|
|
|
|
|
|
net_set_nonblocking(fd, 1);
|
|
|
|
net_set_nosigpipe(fd, 1);
|
|
|
|
|
|
|
|
event_set(user->ev_read, fd, EV_READ | EV_PERSIST, net_on_read, user);
|
|
|
|
event_set(user->ev_write, fd, EV_WRITE, net_on_write, user);
|
2009-03-18 01:56:49 +00:00
|
|
|
event_base_set(hub->evbase, user->ev_read);
|
|
|
|
event_base_set(hub->evbase, user->ev_write);
|
2009-02-19 16:14:09 +00:00
|
|
|
event_add(user->ev_read, &timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|