2009-02-19 16:14:09 +00:00
|
|
|
/*
|
|
|
|
* uhub - A tiny ADC p2p connection hub
|
2010-01-19 13:53:47 +00:00
|
|
|
* Copyright (C) 2007-2010, Jan Vidar Krey
|
2009-02-19 16:14:09 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-10-07 15:37:31 +00:00
|
|
|
#include <uhub.h>
|
2009-05-19 20:57:50 +00:00
|
|
|
#include "hubio.h"
|
2009-10-07 15:37:31 +00:00
|
|
|
#include "probe.h"
|
2009-02-19 16:14:09 +00:00
|
|
|
|
2009-05-18 14:30:17 +00:00
|
|
|
/* FIXME: This should not be needed! */
|
|
|
|
extern struct hub_info* g_hub;
|
2009-02-19 16:14:09 +00:00
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
int handle_net_read(struct hub_user* user)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-05-26 17:46:51 +00:00
|
|
|
static char buf[MAX_RECV_BUF];
|
2009-08-06 22:22:30 +00:00
|
|
|
struct hub_recvq* q = user->recv_queue;
|
2009-06-30 20:15:08 +00:00
|
|
|
size_t buf_size = hub_recvq_get(q, buf, MAX_RECV_BUF);
|
2010-02-12 19:33:17 +00:00
|
|
|
ssize_t size;
|
|
|
|
|
|
|
|
if (user_flag_get(user, flag_maxbuf))
|
|
|
|
buf_size = 0;
|
|
|
|
size = net_con_recv(user->connection, buf + buf_size, MAX_RECV_BUF - buf_size);
|
2009-05-26 17:46:51 +00:00
|
|
|
|
2009-06-22 17:50:10 +00:00
|
|
|
if (size > 0)
|
|
|
|
buf_size += size;
|
2009-05-26 17:46:51 +00:00
|
|
|
|
2009-08-03 14:20:32 +00:00
|
|
|
if (size < 0)
|
2009-06-22 17:50:10 +00:00
|
|
|
{
|
2009-08-03 14:20:32 +00:00
|
|
|
if (size == -1)
|
|
|
|
return quit_disconnected;
|
|
|
|
else
|
|
|
|
return quit_socket_error;
|
2009-06-22 17:50:10 +00:00
|
|
|
}
|
|
|
|
else if (size == 0)
|
|
|
|
{
|
2009-08-03 14:20:32 +00:00
|
|
|
return 0;
|
2009-06-22 17:50:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-30 20:15:08 +00:00
|
|
|
char* lastPos = 0;
|
2009-06-22 17:50:10 +00:00
|
|
|
char* start = buf;
|
|
|
|
char* pos = 0;
|
2009-06-30 20:15:08 +00:00
|
|
|
size_t remaining = buf_size;
|
|
|
|
|
|
|
|
while ((pos = memchr(start, '\n', remaining)))
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-06-30 20:15:08 +00:00
|
|
|
lastPos = pos;
|
2009-06-22 17:50:10 +00:00
|
|
|
pos[0] = '\0';
|
2009-05-26 19:05:06 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_SENDQ
|
2009-07-25 23:38:38 +00:00
|
|
|
LOG_DUMP("PROC: \"%s\" (%d)\n", start, (int) (pos - start));
|
2009-05-26 19:05:06 +00:00
|
|
|
#endif
|
|
|
|
|
2009-06-30 20:15:08 +00:00
|
|
|
if (user_flag_get(user, flag_maxbuf))
|
|
|
|
{
|
|
|
|
user_flag_unset(user, flag_maxbuf);
|
|
|
|
}
|
|
|
|
else
|
2009-05-26 19:05:06 +00:00
|
|
|
{
|
2009-07-01 09:31:55 +00:00
|
|
|
if (((pos - start) > 0) && g_hub->config->max_recv_buffer > (pos - start))
|
2009-06-30 20:15:08 +00:00
|
|
|
{
|
|
|
|
if (hub_handle_message(g_hub, user, start, (pos - start)) == -1)
|
|
|
|
{
|
|
|
|
return quit_protocol_error;
|
|
|
|
}
|
|
|
|
}
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
2009-06-22 17:50:10 +00:00
|
|
|
|
2009-06-30 20:15:08 +00:00
|
|
|
pos[0] = '\n'; /* FIXME: not needed */
|
|
|
|
pos ++;
|
|
|
|
remaining -= (pos - start);
|
2009-06-22 17:50:10 +00:00
|
|
|
start = pos;
|
|
|
|
}
|
|
|
|
|
2009-12-04 07:12:54 +00:00
|
|
|
if (lastPos || remaining)
|
2009-06-22 17:50:10 +00:00
|
|
|
{
|
2009-06-30 20:15:08 +00:00
|
|
|
if (remaining < g_hub->config->max_recv_buffer)
|
|
|
|
{
|
2009-12-04 07:12:54 +00:00
|
|
|
hub_recvq_set(q, lastPos ? lastPos : buf, remaining);
|
2009-06-30 20:15:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hub_recvq_set(q, 0, 0);
|
|
|
|
user_flag_set(user, flag_maxbuf);
|
2009-10-30 17:31:27 +00:00
|
|
|
LOG_WARN("Received message past max_recv_buffer, dropping message.");
|
2009-06-30 20:15:08 +00:00
|
|
|
}
|
2009-06-22 17:50:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hub_recvq_set(q, 0, 0);
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-22 23:57:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-25 23:47:17 +00:00
|
|
|
int handle_net_write(struct hub_user* user)
|
2009-06-22 23:57:26 +00:00
|
|
|
{
|
2009-08-03 14:20:32 +00:00
|
|
|
int ret = 0;
|
2009-08-06 22:22:30 +00:00
|
|
|
while (hub_sendq_get_bytes(user->send_queue))
|
2009-06-22 23:57:26 +00:00
|
|
|
{
|
2009-10-07 15:37:31 +00:00
|
|
|
ret = hub_sendq_send(user->send_queue, user);
|
2009-07-19 12:45:15 +00:00
|
|
|
if (ret <= 0)
|
2009-08-03 14:20:32 +00:00
|
|
|
break;
|
2009-06-22 23:57:26 +00:00
|
|
|
}
|
|
|
|
|
2009-08-03 14:20:32 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return quit_socket_error;
|
|
|
|
|
2009-08-06 22:22:30 +00:00
|
|
|
if (hub_sendq_get_bytes(user->send_queue))
|
2009-06-22 23:57:26 +00:00
|
|
|
{
|
|
|
|
user_net_io_want_write(user);
|
|
|
|
}
|
2009-07-19 00:12:50 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
user_net_io_want_read(user);
|
|
|
|
}
|
2009-06-22 23:57:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-03 19:57:18 +00:00
|
|
|
void net_event(struct net_connection* con, int event, void *arg)
|
2009-06-22 23:57:26 +00:00
|
|
|
{
|
2009-07-25 23:47:17 +00:00
|
|
|
struct hub_user* user = (struct hub_user*) arg;
|
2009-06-22 23:57:26 +00:00
|
|
|
int flag_close = 0;
|
2009-06-22 17:50:10 +00:00
|
|
|
|
2009-06-22 23:57:26 +00:00
|
|
|
#ifdef DEBUG_SENDQ
|
2010-01-22 22:40:41 +00:00
|
|
|
LOG_TRACE("net_event() : fd=%d, ev=%d, arg=%p", con->sd, (int) event, arg);
|
2009-06-22 23:57:26 +00:00
|
|
|
#endif
|
|
|
|
|
2010-01-21 16:26:27 +00:00
|
|
|
if (event == NET_EVENT_TIMEOUT)
|
2009-06-22 23:57:26 +00:00
|
|
|
{
|
|
|
|
if (user_is_connecting(user))
|
|
|
|
{
|
2009-08-03 19:57:18 +00:00
|
|
|
hub_disconnect_user(g_hub, user, quit_timeout);
|
2009-06-22 23:57:26 +00:00
|
|
|
}
|
2010-01-21 16:26:27 +00:00
|
|
|
return;
|
2009-06-22 23:57:26 +00:00
|
|
|
}
|
2009-10-07 15:37:31 +00:00
|
|
|
|
|
|
|
if (event & NET_EVENT_READ)
|
2009-06-22 23:57:26 +00:00
|
|
|
{
|
|
|
|
flag_close = handle_net_read(user);
|
2009-10-07 15:37:31 +00:00
|
|
|
if (flag_close)
|
|
|
|
{
|
|
|
|
hub_disconnect_user(g_hub, user, flag_close);
|
|
|
|
return;
|
|
|
|
}
|
2009-06-22 23:57:26 +00:00
|
|
|
}
|
2009-07-19 00:12:50 +00:00
|
|
|
|
2009-10-08 10:36:36 +00:00
|
|
|
if (event & NET_EVENT_WRITE)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-10-07 15:37:31 +00:00
|
|
|
flag_close = handle_net_write(user);
|
|
|
|
if (flag_close)
|
|
|
|
{
|
|
|
|
hub_disconnect_user(g_hub, user, flag_close);
|
|
|
|
return;
|
|
|
|
}
|
2009-02-19 16:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-17 21:59:51 +00:00
|
|
|
void net_on_accept(struct net_connection* con, int event, void *arg)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
|
|
|
struct hub_info* hub = (struct hub_info*) arg;
|
2009-10-07 15:37:31 +00:00
|
|
|
struct hub_probe* probe = 0;
|
2009-03-23 14:05:27 +00:00
|
|
|
struct ip_addr_encap ipaddr;
|
2010-01-17 21:59:51 +00:00
|
|
|
int server_fd = net_con_get_sd(con);
|
2010-06-22 14:04:33 +00:00
|
|
|
#ifdef PLUGIN_SUPPORT
|
|
|
|
plugin_st status;
|
|
|
|
#endif
|
2009-08-03 14:20:32 +00:00
|
|
|
|
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
|
|
|
|
{
|
2009-07-25 23:38:38 +00:00
|
|
|
LOG_ERROR("Accept error: %d %s", net_error(), strerror(net_error()));
|
2009-02-19 16:14:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-08-03 14:20:32 +00:00
|
|
|
|
2010-06-22 14:04:33 +00:00
|
|
|
#ifdef PLUGIN_SUPPORT
|
|
|
|
status = plugin_check_ip_early(hub, &ipaddr);
|
|
|
|
if (status == st_deny)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2010-06-22 14:04:33 +00:00
|
|
|
plugin_log_connection_denied(hub, &ipaddr);
|
2010-04-05 11:44:17 +00:00
|
|
|
net_close(fd);
|
2009-02-19 16:14:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-08-03 14:20:32 +00:00
|
|
|
|
2010-06-22 14:04:33 +00:00
|
|
|
plugin_log_connection_accepted(hub, &ipaddr);
|
|
|
|
#endif
|
|
|
|
|
2009-10-07 15:37:31 +00:00
|
|
|
probe = probe_create(hub, fd, &ipaddr);
|
|
|
|
if (!probe)
|
2009-02-19 16:14:09 +00:00
|
|
|
{
|
2009-10-07 15:37:31 +00:00
|
|
|
LOG_ERROR("Unable to create probe after socket accepted. Out of memory?");
|
2010-04-05 11:44:17 +00:00
|
|
|
net_close(fd);
|
2009-02-19 16:14:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|