Compare commits
30 Commits
sans_libev
...
0.3.0-fina
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
59f67f9e17 | ||
|
|
bfc7c184a7 | ||
|
|
92c448b855 | ||
|
|
304ecda16a | ||
|
|
c670d13369 | ||
|
|
b2e7a2848f | ||
|
|
84bd2591d6 | ||
|
|
4aa65733d0 | ||
|
|
230ca28b46 | ||
|
|
5f353ebe28 | ||
|
|
68a26e1160 | ||
|
|
4711d26c11 | ||
|
|
1bb3bd90c1 | ||
|
|
5e253e8442 | ||
|
|
26a2a384b6 | ||
|
|
8b7f09c7f4 | ||
|
|
b29da11f3b | ||
|
|
78ad9b8572 | ||
|
|
b04a20c66e | ||
|
|
21a5981905 | ||
|
|
c47ea14047 | ||
|
|
9f5aaf0148 | ||
|
|
f645811f37 | ||
|
|
2f09fcea84 | ||
|
|
5e679e2d4f | ||
|
|
efeb36c492 | ||
|
|
7e60919596 | ||
|
|
e7cb4cd277 | ||
|
|
e45511827f | ||
|
|
80c6ad9d76 |
12
ChangeLog
12
ChangeLog
@@ -1,3 +1,15 @@
|
||||
0.3.0:
|
||||
- More user commands: ban, broadcast, mute, rules, history, myip, whoip, log
|
||||
- Experimental SSL support
|
||||
- Large rewrite of the network stack in order to support SSL.
|
||||
- Added rule file for defining hub rules.
|
||||
- Many crash fixes and other important bug fixes.
|
||||
- Optimizations: O(1) timeout scheduler
|
||||
- New sid allocation code.
|
||||
- Added configurable server_listen_backlog (default 50).
|
||||
- Added init.d scripts for RedHat/CentOS
|
||||
|
||||
|
||||
0.2.8:
|
||||
- Fix bug #13: getsockname() failure, use sockaddr from accept() instead.
|
||||
- Fix bug #10: Improve logging, ensure logs are machine readable.
|
||||
|
||||
17
GNUmakefile
17
GNUmakefile
@@ -9,7 +9,6 @@ MV := mv
|
||||
RANLIB := ranlib
|
||||
CFLAGS += -pipe -Wall
|
||||
USE_SSL ?= NO
|
||||
USE_LIBEVENT ?= NO
|
||||
USE_BIGENDIAN ?= AUTO
|
||||
BITS ?= AUTO
|
||||
SILENT ?= YES
|
||||
@@ -63,9 +62,6 @@ else
|
||||
MSG_CLEAN="Clean as a whistle"
|
||||
endif
|
||||
|
||||
# CFLAGS += -I/source/libevent
|
||||
# LDFLAGS += -L/source/libevent
|
||||
|
||||
ifeq ($(RELEASE),YES)
|
||||
CFLAGS += -O3 -DNDEBUG
|
||||
GIT_REVISION ?= NO
|
||||
@@ -116,15 +112,6 @@ CFLAGS += -DSSL_SUPPORT
|
||||
LDLIBS += -lssl
|
||||
endif
|
||||
|
||||
ifeq ($(USE_LIBEVENT),YES)
|
||||
CFLAGS += -DUSE_LIBEVENT
|
||||
LDLIBS += -levent
|
||||
ifneq ($(LIBEVENT_PATH),)
|
||||
CFLAGS += -I$(LIBEVENT_PATH)
|
||||
LDFLAGS += -L$(LIBEVENT_PATH)
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(GIT_REVISION),YES)
|
||||
CFLAGS += -DGIT_REVISION=\"$(shell git show --abbrev-commit | head -n 1 | cut -f 2 -d " ")\"
|
||||
endif
|
||||
@@ -144,10 +131,12 @@ libuhub_SOURCES := \
|
||||
src/core/route.c \
|
||||
src/core/user.c \
|
||||
src/core/usermanager.c \
|
||||
src/network/backend.c \
|
||||
src/network/connection.c \
|
||||
src/network/epoll.c \
|
||||
src/network/libevent.c \
|
||||
src/network/network.c \
|
||||
src/network/select.c \
|
||||
src/network/timer.c \
|
||||
src/util/ipcalc.c \
|
||||
src/util/list.c \
|
||||
src/util/log.c \
|
||||
|
||||
2
README
2
README
@@ -1,5 +1,5 @@
|
||||
Welcome and thanks for downloading uHub, a high performance ADC p2p hub.
|
||||
|
||||
For the official documentation, bugs and other information, please visit:
|
||||
http://www.extatic.org/uhub/
|
||||
http://www.uhub.org/
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ EXO_TEST(hub_net_startup, {
|
||||
|
||||
EXO_TEST(hub_config_initialize, {
|
||||
config_defaults(&g_config);
|
||||
g_config.server_port = 15111;
|
||||
return 1;
|
||||
});
|
||||
|
||||
|
||||
13
doc/init.d.RedHat/etc/logrotator.d/uhub
Normal file
13
doc/init.d.RedHat/etc/logrotator.d/uhub
Normal file
@@ -0,0 +1,13 @@
|
||||
# Log rotate for Uhub
|
||||
# see man logrotate
|
||||
#
|
||||
#
|
||||
/var/log/uhub.log {
|
||||
compress
|
||||
size 10M
|
||||
rotate 10
|
||||
missingok
|
||||
notifempty
|
||||
|
||||
|
||||
}
|
||||
5
doc/rules.txt
Normal file
5
doc/rules.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
1. rule #1
|
||||
2. rule #2
|
||||
3. rule #3
|
||||
......
|
||||
34. Rule #34
|
||||
@@ -39,6 +39,11 @@ file_acl=/etc/uhub/users.conf
|
||||
# Normally this message is sent to clients when connecting.
|
||||
file_motd=/etc/uhub/motd.txt
|
||||
|
||||
# This file can contain a rules of the hub.
|
||||
# Normally this message is sent to clients when write in chat !rules
|
||||
file_rules=/etc/uhub/rules.txt
|
||||
|
||||
|
||||
# Slots\share\hubs limits
|
||||
limit_max_hubs_user = 0
|
||||
limit_max_hubs_reg = 0
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
* Copyright (C) 2007-2010, 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
|
||||
@@ -276,6 +276,28 @@ static int command_unban(struct hub_info* hub, struct hub_user* user, struct hub
|
||||
return command_status(hub, user, cmd, "Not implemented");
|
||||
}
|
||||
|
||||
static int command_mute(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
char* nick = list_get_first(cmd->args);
|
||||
if (!nick)
|
||||
return -1; // FIXME: bad syntax.
|
||||
|
||||
struct hub_user* target = uman_get_user_by_nick(hub, nick);
|
||||
|
||||
if (!target)
|
||||
return command_status_user_not_found(hub, user, cmd, nick);
|
||||
|
||||
if (strlen(cmd->prefix) == 4)
|
||||
{
|
||||
user_flag_set(target, flag_muted);
|
||||
}
|
||||
else
|
||||
{
|
||||
user_flag_unset(target, flag_muted);
|
||||
}
|
||||
return command_status(hub, user, cmd, nick);
|
||||
}
|
||||
|
||||
static int command_reload(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
hub->status = hub_status_restart;
|
||||
@@ -377,8 +399,8 @@ static int command_whoip(struct hub_info* hub, struct hub_user* user, struct hub
|
||||
|
||||
static int command_broadcast(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
struct adc_message* command = adc_msg_construct(ADC_CMD_IMSG, strlen((cmd->message + 10)) + 6);
|
||||
adc_msg_add_argument(command, (cmd->message + 10));
|
||||
struct adc_message* command = adc_msg_construct(ADC_CMD_IMSG, strlen((cmd->message + 12)) + 6);
|
||||
adc_msg_add_argument(command, (cmd->message + 12));
|
||||
route_to_all(hub, command);
|
||||
adc_msg_free(command);
|
||||
return 0;
|
||||
@@ -496,6 +518,20 @@ static int command_log(struct hub_info* hub, struct hub_user* user, struct hub_c
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int command_rules(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
if (!hub_send_rules(hub, user))
|
||||
return command_status(hub, user, cmd, "no rules defined.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int command_motd(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
if (!hub_send_motd(hub, user))
|
||||
return command_status(hub, user, cmd, "no motd defined.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef CRASH_DEBUG
|
||||
static int command_crash(struct hub_info* hub, struct hub_user* user, struct hub_command* cmd)
|
||||
{
|
||||
@@ -550,24 +586,28 @@ int command_dipatcher(struct hub_info* hub, struct hub_user* user, const char* m
|
||||
}
|
||||
|
||||
static struct commands_handler command_handlers[] = {
|
||||
{ "help", 4, 0, cred_guest, command_help, "Show this help message." },
|
||||
{ "stats", 5, 0, cred_super, command_stats, "Show hub statistics." },
|
||||
{ "version", 7, 0, cred_guest, command_version, "Show hub version info." },
|
||||
{ "history", 7, 0, cred_guest, command_history, "Show the last chat messages." },
|
||||
{ "uptime", 6, 0, cred_guest, command_uptime, "Display hub uptime info." },
|
||||
{ "kick", 4, "n", cred_operator, command_kick, "Kick a user" },
|
||||
{ "ban", 3, "n", cred_operator, command_ban, "Ban a user" },
|
||||
{ "unban", 5, "n", cred_operator, command_unban, "Lift ban on a user" },
|
||||
{ "reload", 6, 0, cred_admin, command_reload, "Reload configuration files." },
|
||||
{ "shutdown", 8, 0, cred_admin, command_shutdown, "Shutdown hub." },
|
||||
{ "myip", 4, 0, cred_guest, command_myip, "Show your own IP." },
|
||||
{ "getip", 5, "n", cred_operator, command_getip, "Show IP address for a user" },
|
||||
{ "whoip", 5, "a", cred_operator, command_whoip, "Show users matching IP range" },
|
||||
{ "broadcast", 9, "m", cred_operator, command_broadcast,"Send a message to all users" },
|
||||
{ "log", 3, 0, cred_operator, command_log, "Display log" },
|
||||
#ifdef CRASH_DEBUG
|
||||
{ "crash", 5, 0, cred_admin, command_crash, "Crash the hub (DEBUG)." },
|
||||
#endif
|
||||
{ "getip", 5, "n", cred_operator, command_getip, "Show IP address for a user" },
|
||||
{ "help", 4, 0, cred_guest, command_help, "Show this help message." },
|
||||
{ "history", 7, 0, cred_guest, command_history, "Show the last chat messages." },
|
||||
{ "kick", 4, "n", cred_operator, command_kick, "Kick a user" },
|
||||
{ "log", 3, 0, cred_operator, command_log, "Display log" },
|
||||
{ "motd", 4, 0, cred_guest, command_motd, "Show the message of the day" },
|
||||
{ "mute", 4, "n", cred_operator, command_mute, "Mute user" },
|
||||
{ "myip", 4, 0, cred_guest, command_myip, "Show your own IP." },
|
||||
{ "reload", 6, 0, cred_admin, command_reload, "Reload configuration files." },
|
||||
{ "rules", 5, 0, cred_guest, command_rules, "Show the hub rules" },
|
||||
{ "shutdown", 8, 0, cred_admin, command_shutdown, "Shutdown hub." },
|
||||
{ "stats", 5, 0, cred_super, command_stats, "Show hub statistics." },
|
||||
{ "unban", 5, "n", cred_operator, command_unban, "Lift ban on a user" },
|
||||
{ "unmute", 6, "n", cred_operator, command_mute, "Unmute user" },
|
||||
{ "uptime", 6, 0, cred_guest, command_uptime, "Display hub uptime info." },
|
||||
{ "version", 7, 0, cred_guest, command_version, "Show hub version info." },
|
||||
{ "whoip", 5, "a", cred_operator, command_whoip, "Show users matching IP range" },
|
||||
{ 0, 0, 0, cred_none, command_help, "" }
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
* Copyright (C) 2007-2010, 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
|
||||
|
||||
@@ -116,6 +116,7 @@
|
||||
#define DEF_HUB_ENABLED 1
|
||||
#define DEF_FILE_ACL ""
|
||||
#define DEF_FILE_MOTD ""
|
||||
#define DEF_FILE_RULES ""
|
||||
#define DEF_MAX_USERS 500
|
||||
#define DEF_MAX_CHAT_HISTORY 20
|
||||
#define DEF_MAX_LOGOUT_LOG 100
|
||||
@@ -180,6 +181,7 @@ void config_defaults(struct hub_config* config)
|
||||
DEFAULT_BOOLEAN(hub_enabled, DEF_HUB_ENABLED);
|
||||
DEFAULT_STRING (file_acl, DEF_FILE_ACL);
|
||||
DEFAULT_STRING (file_motd, DEF_FILE_MOTD);
|
||||
DEFAULT_STRING (file_rules, DEF_FILE_RULES);
|
||||
DEFAULT_INTEGER(server_port, DEF_SERVER_PORT);
|
||||
DEFAULT_INTEGER(server_listen_backlog, DEF_SERVER_BACKLOG);
|
||||
DEFAULT_INTEGER(max_users, DEF_MAX_USERS);
|
||||
@@ -249,6 +251,7 @@ static int apply_config(struct hub_config* config, char* key, char* data, int li
|
||||
{
|
||||
GET_STR (file_acl);
|
||||
GET_STR (file_motd);
|
||||
GET_STR (file_rules);
|
||||
GET_STR (server_bind_addr);
|
||||
GET_INT (server_port);
|
||||
GET_INT (server_listen_backlog);
|
||||
@@ -327,6 +330,7 @@ void free_config(struct hub_config* config)
|
||||
hub_free(config->server_bind_addr);
|
||||
hub_free(config->file_motd);
|
||||
hub_free(config->file_acl);
|
||||
hub_free(config->file_rules);
|
||||
hub_free(config->hub_name);
|
||||
hub_free(config->hub_description);
|
||||
|
||||
@@ -398,6 +402,7 @@ void dump_config(struct hub_config* config, int ignore_defaults)
|
||||
{
|
||||
DUMP_STR (file_acl, DEF_FILE_ACL);
|
||||
DUMP_STR (file_motd, DEF_FILE_MOTD);
|
||||
DUMP_STR (file_rules, DEF_FILE_RULES);
|
||||
DUMP_STR (server_bind_addr, DEF_SERVER_BIND_ADDR);
|
||||
DUMP_INT (server_port, DEF_SERVER_PORT);
|
||||
DUMP_INT (server_listen_backlog, DEF_SERVER_BACKLOG);
|
||||
|
||||
@@ -33,6 +33,7 @@ struct hub_config
|
||||
int chat_is_privileged; /**<<< "Allow chat for operators and above only (default: 0) */
|
||||
char* file_motd; /**<<< "File containing the 'message of the day' (default: '' - no motd)" */
|
||||
char* file_acl; /**<<< "File containing user database (default: '' - no known users)" */
|
||||
char* file_rules; /**<<< "File containing the rules (default: '' - no rules)" */
|
||||
char* hub_name; /**<<< "Name of hub (default: 'My uhub hub')" */
|
||||
char* hub_description; /**<<< "Name of hub (default: 'no description')" */
|
||||
int max_recv_buffer; /**<<< "Max read buffer before parse, per user (default: 4096)" */
|
||||
|
||||
165
src/core/hub.c
165
src/core/hub.c
@@ -206,7 +206,7 @@ int hub_handle_chat_message(struct hub_info* hub, struct hub_user* u, struct adc
|
||||
}
|
||||
}
|
||||
|
||||
if (hub->config->chat_is_privileged && !user_is_protected(u) && (cmd->cache[0] == 'B' || cmd->cache[0] == 'F'))
|
||||
if (((hub->config->chat_is_privileged && !user_is_protected(u)) || (user_flag_get(u, flag_muted))) && (cmd->cache[0] == 'B' || cmd->cache[0] == 'F'))
|
||||
{
|
||||
relay = 0;
|
||||
}
|
||||
@@ -361,14 +361,27 @@ void hub_send_handshake(struct hub_info* hub, struct hub_user* u)
|
||||
}
|
||||
}
|
||||
|
||||
void hub_send_motd(struct hub_info* hub, struct hub_user* u)
|
||||
int hub_send_motd(struct hub_info* hub, struct hub_user* u)
|
||||
{
|
||||
if (hub->command_motd)
|
||||
{
|
||||
route_to_user(hub, u, hub->command_motd);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int hub_send_rules(struct hub_info* hub, struct hub_user* u)
|
||||
{
|
||||
if (hub->command_rules)
|
||||
{
|
||||
route_to_user(hub, u, hub->command_rules);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void hub_send_password_challenge(struct hub_info* hub, struct hub_user* u)
|
||||
{
|
||||
struct adc_message* igpa;
|
||||
@@ -437,89 +450,79 @@ static void hub_event_dispatcher(void* callback_data, struct event_data* message
|
||||
}
|
||||
}
|
||||
|
||||
static struct net_connection* start_listening_socket(const char* bind_addr, uint16_t port, int backlog, struct hub_info* hub)
|
||||
{
|
||||
struct net_connection* server;
|
||||
struct sockaddr_storage addr;
|
||||
socklen_t sockaddr_size;
|
||||
int sd, ret;
|
||||
|
||||
if (ip_convert_address(bind_addr, port, (struct sockaddr*) &addr, &sockaddr_size) == -1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
sd = net_socket_create(addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (sd == -1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((net_set_reuseaddress(sd, 1) == -1) || (net_set_nonblocking(sd, 1) == -1))
|
||||
{
|
||||
net_close(sd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = net_bind(sd, (struct sockaddr*) &addr, sockaddr_size);
|
||||
if (ret == -1)
|
||||
{
|
||||
LOG_ERROR("hub_start_service(): Unable to bind to TCP local address. errno=%d, str=%s", net_error(), net_error_string(net_error()));
|
||||
net_close(sd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = net_listen(sd, backlog);
|
||||
if (ret == -1)
|
||||
{
|
||||
LOG_ERROR("hub_start_service(): Unable to listen to socket");
|
||||
net_close(sd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
server = net_con_create();
|
||||
net_con_initialize(server, sd, net_on_accept, hub, NET_EVENT_READ);
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
struct hub_info* hub_start_service(struct hub_config* config)
|
||||
{
|
||||
struct hub_info* hub = 0;
|
||||
struct sockaddr_storage addr;
|
||||
socklen_t sockaddr_size;
|
||||
int server_tcp, ret, ipv6_supported, af;
|
||||
char address_buf[INET6_ADDRSTRLEN+1];
|
||||
|
||||
int ipv6_supported;
|
||||
|
||||
hub = hub_malloc_zero(sizeof(struct hub_info));
|
||||
if (!hub)
|
||||
{
|
||||
LOG_FATAL("Unable to allocate memory for hub");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
hub->tm_started = time(0);
|
||||
|
||||
ipv6_supported = net_is_ipv6_supported();
|
||||
|
||||
if (ipv6_supported)
|
||||
LOG_DEBUG("IPv6 supported.");
|
||||
else
|
||||
LOG_DEBUG("IPv6 not supported.");
|
||||
|
||||
if (ip_convert_address(config->server_bind_addr, config->server_port, (struct sockaddr*) &addr, &sockaddr_size) == -1)
|
||||
{
|
||||
hub_free(hub);
|
||||
return 0;
|
||||
}
|
||||
|
||||
af = addr.ss_family;
|
||||
if (af == AF_INET)
|
||||
{
|
||||
net_address_to_string(AF_INET, &((struct sockaddr_in*) &addr)->sin_addr, address_buf, INET6_ADDRSTRLEN);
|
||||
}
|
||||
else if (af == AF_INET6)
|
||||
{
|
||||
net_address_to_string(AF_INET6, &((struct sockaddr_in6*) &addr)->sin6_addr, address_buf, INET6_ADDRSTRLEN);
|
||||
}
|
||||
|
||||
LOG_INFO("Starting " PRODUCT "/" VERSION ", listening on %s:%d...", address_buf, config->server_port);
|
||||
|
||||
server_tcp = net_socket_create(af, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (server_tcp == -1)
|
||||
hub->server = start_listening_socket(config->server_bind_addr, config->server_port, config->server_listen_backlog, hub);
|
||||
if (!hub->server)
|
||||
{
|
||||
hub_free(hub);
|
||||
LOG_FATAL("Unable to start hub service");
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = net_set_reuseaddress(server_tcp, 1);
|
||||
if (ret == -1)
|
||||
{
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = net_set_nonblocking(server_tcp, 1);
|
||||
if (ret == -1)
|
||||
{
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = net_bind(server_tcp, (struct sockaddr*) &addr, sockaddr_size);
|
||||
if (ret == -1)
|
||||
{
|
||||
LOG_FATAL("hub_start_service(): Unable to bind to TCP local address. errno=%d, str=%s", net_error(), net_error_string(net_error()));
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ret = net_listen(server_tcp, config->server_listen_backlog);
|
||||
if (ret == -1)
|
||||
{
|
||||
LOG_FATAL("hub_start_service(): Unable to listen to socket");
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
return 0;
|
||||
}
|
||||
LOG_INFO("Starting " PRODUCT "/" VERSION ", listening on %s:%d...", net_get_local_address(hub->server->sd), config->server_port);
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
if (config->tls_enable)
|
||||
@@ -549,22 +552,21 @@ struct hub_info* hub_start_service(struct hub_config* config)
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
hub->config = config;
|
||||
hub->users = NULL;
|
||||
|
||||
if (uman_init(hub) == -1)
|
||||
{
|
||||
net_con_close(hub->server);
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (event_queue_initialize(&hub->queue, hub_event_dispatcher, (void*) hub) == -1)
|
||||
{
|
||||
net_con_close(hub->server);
|
||||
uman_shutdown(hub);
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -572,11 +574,11 @@ struct hub_info* hub_start_service(struct hub_config* config)
|
||||
hub->sendbuf = hub_malloc(MAX_SEND_BUF);
|
||||
if (!hub->recvbuf || !hub->sendbuf)
|
||||
{
|
||||
net_con_close(hub->server);
|
||||
hub_free(hub->recvbuf);
|
||||
hub_free(hub->sendbuf);
|
||||
uman_shutdown(hub);
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -584,21 +586,18 @@ struct hub_info* hub_start_service(struct hub_config* config)
|
||||
hub->logout_info = (struct linked_list*) list_create();
|
||||
if (!hub->chat_history)
|
||||
{
|
||||
net_con_close(hub->server);
|
||||
list_destroy(hub->chat_history);
|
||||
list_destroy(hub->logout_info);
|
||||
hub_free(hub->recvbuf);
|
||||
hub_free(hub->sendbuf);
|
||||
uman_shutdown(hub);
|
||||
hub_free(hub);
|
||||
net_close(server_tcp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
hub->status = hub_status_running;
|
||||
|
||||
hub->server = net_con_create();
|
||||
net_con_initialize(hub->server, server_tcp, net_on_accept, hub, NET_EVENT_READ);
|
||||
|
||||
g_hub = hub;
|
||||
return hub;
|
||||
}
|
||||
@@ -609,9 +608,6 @@ void hub_shutdown_service(struct hub_info* hub)
|
||||
LOG_DEBUG("hub_shutdown_service()");
|
||||
|
||||
event_queue_shutdown(hub->queue);
|
||||
#ifdef USE_LIBEVENT
|
||||
event_del(&hub->ev_accept);
|
||||
#endif
|
||||
net_con_close(hub->server);
|
||||
hub_free(hub->server);
|
||||
uman_shutdown(hub);
|
||||
@@ -669,6 +665,22 @@ void hub_set_variables(struct hub_info* hub, struct acl_handle* acl)
|
||||
close(fd);
|
||||
}
|
||||
|
||||
hub->command_rules = 0;
|
||||
fd = (hub->config->file_rules && *hub->config->file_rules) ? open(hub->config->file_rules, 0) : -1;
|
||||
if (fd != -1)
|
||||
{
|
||||
ret = read(fd, buf, MAX_RECV_BUF);
|
||||
if (ret > 0)
|
||||
{
|
||||
buf[ret] = 0;
|
||||
tmp = adc_msg_escape(buf);
|
||||
hub->command_rules = adc_msg_construct(ADC_CMD_IMSG, 6 + strlen(tmp));
|
||||
adc_msg_add_argument(hub->command_rules, tmp);
|
||||
hub_free(tmp);
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
hub->command_support = adc_msg_construct(ADC_CMD_ISUP, 6 + strlen(ADC_PROTO_SUPPORT));
|
||||
if (hub->command_support)
|
||||
{
|
||||
@@ -697,6 +709,9 @@ void hub_free_variables(struct hub_info* hub)
|
||||
if (hub->command_motd)
|
||||
adc_msg_free(hub->command_motd);
|
||||
|
||||
if (hub->command_rules)
|
||||
adc_msg_free(hub->command_rules);
|
||||
|
||||
adc_msg_free(hub->command_support);
|
||||
}
|
||||
|
||||
|
||||
@@ -91,10 +91,6 @@ struct hub_logout_info
|
||||
struct hub_info
|
||||
{
|
||||
struct net_connection* server;
|
||||
#ifdef USE_LIBEVENT
|
||||
struct event ev_accept;
|
||||
struct event ev_timer;
|
||||
#endif
|
||||
struct hub_stats stats;
|
||||
struct event_queue* queue;
|
||||
struct hub_config* config;
|
||||
@@ -103,6 +99,7 @@ struct hub_info
|
||||
struct adc_message* command_info; /* The hub's INF command */
|
||||
struct adc_message* command_support; /* The hub's SUP command */
|
||||
struct adc_message* command_motd; /* The message of the day */
|
||||
struct adc_message* command_rules; /* The hub rules */
|
||||
struct adc_message* command_banner; /* The default welcome message */
|
||||
time_t tm_started;
|
||||
int status;
|
||||
@@ -203,8 +200,15 @@ extern void hub_send_handshake(struct hub_info* hub, struct hub_user* u);
|
||||
/**
|
||||
* Send a welcome message containing the message of the day to
|
||||
* one particular user. This can be sent in any point in time.
|
||||
* @return 1 if the motd were sent.
|
||||
*/
|
||||
extern void hub_send_motd(struct hub_info* hub, struct hub_user* u);
|
||||
extern int hub_send_motd(struct hub_info* hub, struct hub_user* u);
|
||||
|
||||
/**
|
||||
* Send the rules if configured.
|
||||
* @return 1 if the rules were sent.
|
||||
*/
|
||||
extern int hub_send_rules(struct hub_info* hub, struct hub_user* u);
|
||||
|
||||
/**
|
||||
* Send a password challenge to a user.
|
||||
|
||||
@@ -68,6 +68,10 @@ void on_login_success(struct hub_info* hub, struct hub_user* u)
|
||||
if (user_is_logged_in(u)) /* Previous send() can fail! */
|
||||
hub_send_motd(hub, u);
|
||||
|
||||
/* Send message of the day (if any) */
|
||||
if (user_is_logged_in(u)) /* Previous send() can fail! */
|
||||
hub_send_rules(hub, u);
|
||||
|
||||
/* reset timeout */
|
||||
net_con_clear_timeout(u->connection);
|
||||
}
|
||||
|
||||
@@ -33,13 +33,13 @@ static const char* arg_pid = 0;
|
||||
static int arg_log_syslog = 0;
|
||||
|
||||
|
||||
#if !defined(WIN32) && defined(USE_LIBEVENT)
|
||||
void hub_handle_signal(int fd, short events, void* arg)
|
||||
#if !defined(WIN32)
|
||||
extern struct hub_info* g_hub;
|
||||
void hub_handle_signal(int sig)
|
||||
{
|
||||
struct hub_info* hub = (struct hub_info*) arg;
|
||||
int signal = fd;
|
||||
struct hub_info* hub = g_hub;
|
||||
|
||||
switch (signal)
|
||||
switch (sig)
|
||||
{
|
||||
case SIGINT:
|
||||
LOG_INFO("Interrupted. Shutting down...");
|
||||
@@ -65,7 +65,6 @@ void hub_handle_signal(int fd, short events, void* arg)
|
||||
}
|
||||
}
|
||||
|
||||
static struct event signal_events[10];
|
||||
static int signals[] =
|
||||
{
|
||||
SIGINT, /* Interrupt the application */
|
||||
@@ -77,11 +76,16 @@ static int signals[] =
|
||||
|
||||
void setup_signal_handlers(struct hub_info* hub)
|
||||
{
|
||||
sigset_t sig_set;
|
||||
sigemptyset(&sig_set);
|
||||
struct sigaction act;
|
||||
act.sa_mask = sig_set;
|
||||
act.sa_flags = SA_ONSTACK | SA_RESTART;
|
||||
act.sa_handler = hub_handle_signal;
|
||||
int i = 0;
|
||||
for (i = 0; signals[i]; i++)
|
||||
{
|
||||
signal_set(&signal_events[i], signals[i], hub_handle_signal, hub);
|
||||
if (signal_add(&signal_events[i], NULL))
|
||||
if (sigaction(signals[i], &act, 0) != 0)
|
||||
{
|
||||
LOG_ERROR("Error setting signal handler %d", signals[i]);
|
||||
}
|
||||
@@ -90,14 +94,8 @@ void setup_signal_handlers(struct hub_info* hub)
|
||||
|
||||
void shutdown_signal_handlers(struct hub_info* hub)
|
||||
{
|
||||
int i = 0;
|
||||
for (i = 0; signals[i]; i++)
|
||||
{
|
||||
signal_del(&signal_events[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* !WIN32 && USE_LIBEVENT*/
|
||||
#endif /* !WIN32 */
|
||||
|
||||
|
||||
int main_loop()
|
||||
@@ -133,7 +131,7 @@ int main_loop()
|
||||
hub = hub_start_service(&configuration);
|
||||
if (!hub)
|
||||
return -1;
|
||||
#if !defined(WIN32) && defined(USE_LIBEVENT)
|
||||
#if !defined(WIN32)
|
||||
setup_signal_handlers(hub);
|
||||
#endif
|
||||
}
|
||||
@@ -148,7 +146,7 @@ int main_loop()
|
||||
|
||||
} while (hub->status == hub_status_restart);
|
||||
|
||||
#if !defined(WIN32) && defined(USE_LIBEVENT)
|
||||
#if !defined(WIN32)
|
||||
shutdown_signal_handlers(hub);
|
||||
#endif
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ extern struct hub_info* g_hub;
|
||||
#ifdef DEBUG_SENDQ
|
||||
void debug_sendq_send(struct hub_user* user, int sent, int total)
|
||||
{
|
||||
LOG_DUMP("SEND: sd=%d, %d/%d bytes\n", user->net.connection.sd, sent, total);
|
||||
LOG_DUMP("SEND: sd=%d, %d/%d bytes\n", user->connection->sd, sent, total);
|
||||
if (sent == -1)
|
||||
{
|
||||
int err = net_error();
|
||||
@@ -162,26 +162,16 @@ void net_event(struct net_connection* con, int event, void *arg)
|
||||
int flag_close = 0;
|
||||
|
||||
#ifdef DEBUG_SENDQ
|
||||
LOG_TRACE("net_event() : fd=%d, ev=%d, arg=%p", fd, (int) event, arg);
|
||||
LOG_TRACE("net_event() : fd=%d, ev=%d, arg=%p", con->sd, (int) event, arg);
|
||||
#endif
|
||||
|
||||
if (event == NET_EVENT_SOCKERROR)
|
||||
{
|
||||
hub_disconnect_user(g_hub, user, quit_socket_error);
|
||||
return;
|
||||
}
|
||||
else if (event == NET_EVENT_CLOSED)
|
||||
{
|
||||
hub_disconnect_user(g_hub, user, quit_disconnected);
|
||||
return;
|
||||
}
|
||||
else if (event == NET_EVENT_TIMEOUT)
|
||||
if (event == NET_EVENT_TIMEOUT)
|
||||
{
|
||||
if (user_is_connecting(user))
|
||||
{
|
||||
hub_disconnect_user(g_hub, user, quit_timeout);
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (event & NET_EVENT_READ)
|
||||
@@ -238,7 +228,7 @@ void net_on_accept(struct net_connection* con, int event, void *arg)
|
||||
if (acl_is_ip_banned(hub->acl, addr))
|
||||
{
|
||||
LOG_INFO("Denied [%s] (IP banned)", addr);
|
||||
net_close(fd);
|
||||
net_con_close(con);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -246,7 +236,7 @@ void net_on_accept(struct net_connection* con, int event, void *arg)
|
||||
if (!probe)
|
||||
{
|
||||
LOG_ERROR("Unable to create probe after socket accepted. Out of memory?");
|
||||
net_close(fd);
|
||||
net_con_close(con);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,8 +26,7 @@ static char probe_recvbuf[PROBE_RECV_SIZE];
|
||||
static void probe_net_event(struct net_connection* con, int events, void *arg)
|
||||
{
|
||||
struct hub_probe* probe = (struct hub_probe*) net_con_get_ptr(con);
|
||||
|
||||
if (events == NET_EVENT_SOCKERROR || events == NET_EVENT_CLOSED || events == NET_EVENT_TIMEOUT)
|
||||
if (events == NET_EVENT_TIMEOUT)
|
||||
{
|
||||
probe_destroy(probe);
|
||||
return;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
* Copyright (C) 2007-2010, 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
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2009, Jan Vidar Krey
|
||||
* Copyright (C) 2007-2010, 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
|
||||
@@ -45,6 +45,7 @@ enum user_flags
|
||||
feature_ping = 0x00000080, /** PING: Hub pinger information extension */
|
||||
feature_link = 0x00000100, /** LINK: Hub link (not supported) */
|
||||
feature_adcs = 0x00000200, /** ADCS: ADC over TLS/SSL */
|
||||
flag_muted = 0x00800000, /** User is muted (cannot chat) */
|
||||
flag_ignore = 0x01000000, /** Ignore further reads */
|
||||
flag_maxbuf = 0x02000000, /** Hit max buf read, ignore msg */
|
||||
flag_choke = 0x04000000, /** Choked: Cannot send, waiting for write event */
|
||||
|
||||
@@ -19,8 +19,6 @@
|
||||
|
||||
#include "uhub.h"
|
||||
|
||||
#define USERMANAGER_TIMER
|
||||
|
||||
/*
|
||||
* This callback function is used to clear user objects from the userlist.
|
||||
* Should only be used in uman_shutdown().
|
||||
@@ -40,7 +38,6 @@ static void clear_user_list_callback(void* ptr)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void uman_update_stats(struct hub_info* hub)
|
||||
{
|
||||
const int factor = TIMEOUT_STATS;
|
||||
@@ -58,7 +55,6 @@ void uman_update_stats(struct hub_info* hub)
|
||||
net_stats_reset();
|
||||
}
|
||||
|
||||
|
||||
void uman_print_stats(struct hub_info* hub)
|
||||
{
|
||||
LOG_INFO("Statistics users=" PRINTF_SIZE_T " (peak_users=" PRINTF_SIZE_T "), net_tx=%d KB/s, net_rx=%d KB/s (peak_tx=%d KB/s, peak_rx=%d KB/s)",
|
||||
@@ -70,28 +66,16 @@ void uman_print_stats(struct hub_info* hub)
|
||||
(int) hub->stats.net_rx_peak / 1024);
|
||||
}
|
||||
|
||||
#ifdef USERMANAGER_TIMER
|
||||
#ifdef USE_LIBEVENT
|
||||
static void timer_statistics(int fd, short ev, void *arg)
|
||||
static void timer_statistics(struct timeout_evt* t)
|
||||
{
|
||||
struct hub_info* hub = (struct hub_info*) arg;
|
||||
struct timeval timeout = { TIMEOUT_STATS, 0 };
|
||||
struct hub_info* hub = (struct hub_info*) t->ptr;
|
||||
uman_update_stats(hub);
|
||||
evtimer_set(&hub->ev_timer, timer_statistics, hub);
|
||||
evtimer_add(&hub->ev_timer, &timeout);
|
||||
timeout_queue_reschedule(net_backend_get_timeout_queue(), hub->users->timeout, TIMEOUT_STATS);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
int uman_init(struct hub_info* hub)
|
||||
{
|
||||
struct hub_user_manager* users = NULL;
|
||||
#ifdef USERMANAGER_TIMER
|
||||
#ifdef USE_LIBEVENT
|
||||
struct timeval timeout = { TIMEOUT_STATS, 0 };
|
||||
#endif
|
||||
#endif
|
||||
if (!hub)
|
||||
return -1;
|
||||
|
||||
@@ -109,17 +93,11 @@ int uman_init(struct hub_info* hub)
|
||||
return -1;
|
||||
}
|
||||
|
||||
hub->users = users;
|
||||
users->timeout = hub_malloc_zero(sizeof(struct timeout_evt));
|
||||
timeout_evt_initialize(users->timeout, timer_statistics, hub);
|
||||
timeout_queue_insert(net_backend_get_timeout_queue(), users->timeout, TIMEOUT_STATS);
|
||||
|
||||
#ifdef USERMANAGER_TIMER
|
||||
#ifdef USE_LIBEVENT
|
||||
if (net_get_evbase())
|
||||
{
|
||||
evtimer_set(&hub->ev_timer, timer_statistics, hub);
|
||||
evtimer_add(&hub->ev_timer, &timeout);
|
||||
}
|
||||
#endif
|
||||
#endif // 0
|
||||
hub->users = users;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -129,12 +107,8 @@ int uman_shutdown(struct hub_info* hub)
|
||||
if (!hub || !hub->users)
|
||||
return -1;
|
||||
|
||||
#ifdef USERMANAGER_TIMER
|
||||
#ifdef USE_LIBEVENT
|
||||
if (evtimer_pending(&hub->ev_timer, 0))
|
||||
evtimer_del(&hub->ev_timer);
|
||||
#endif
|
||||
#endif
|
||||
timeout_queue_remove(net_backend_get_timeout_queue(), hub->users->timeout);
|
||||
hub_free(hub->users->timeout);
|
||||
|
||||
if (hub->users->list)
|
||||
{
|
||||
@@ -145,6 +119,7 @@ int uman_shutdown(struct hub_info* hub)
|
||||
hub_free(hub->users);
|
||||
hub->users = 0;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ struct hub_user_manager
|
||||
uint64_t shared_size; /**<< "The total number of shared bytes among fully connected users." */
|
||||
uint64_t shared_files; /**<< "The total number of shared files among fully connected users." */
|
||||
struct linked_list* list; /**<< "Contains all logged in users" */
|
||||
struct timeout_evt* timeout; /**<< "Timeout handler for statistics" */
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
63
src/network/backend.c
Normal file
63
src/network/backend.c
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, 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"
|
||||
|
||||
#include "network/connection.h"
|
||||
|
||||
struct net_cleanup_handler
|
||||
{
|
||||
size_t num;
|
||||
size_t max;
|
||||
struct net_connection** queue;
|
||||
};
|
||||
|
||||
struct net_cleanup_handler* net_cleanup_initialize(size_t max)
|
||||
{
|
||||
struct net_cleanup_handler* handler = (struct net_cleanup_handler*) hub_malloc(sizeof(struct net_cleanup_handler));
|
||||
handler->num = 0;
|
||||
handler->max = max;
|
||||
handler->queue = hub_malloc_zero(sizeof(struct net_connection*) * max);
|
||||
return handler;
|
||||
}
|
||||
|
||||
void net_cleanup_shutdown(struct net_cleanup_handler* handler)
|
||||
{
|
||||
hub_free(handler->queue);
|
||||
hub_free(handler);
|
||||
}
|
||||
|
||||
void net_cleanup_delayed_free(struct net_cleanup_handler* handler, struct net_connection* con)
|
||||
{
|
||||
handler->queue[handler->num++] = con;
|
||||
con->flags |= NET_CLEANUP;
|
||||
}
|
||||
|
||||
void net_cleanup_process(struct net_cleanup_handler* handler)
|
||||
{
|
||||
size_t n;
|
||||
for (n = 0; n < handler->num; n++)
|
||||
{
|
||||
struct net_connection* con = handler->queue[n];
|
||||
LOG_TRACE("net_cleanup_process: free: %p", con);
|
||||
hub_free(con);
|
||||
}
|
||||
handler->num = 0;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,9 @@
|
||||
#ifndef HAVE_UHUB_NETWORK_BACKEND_H
|
||||
#define HAVE_UHUB_NETWORK_BACKEND_H
|
||||
|
||||
struct net_cleanup_handler;
|
||||
struct net_connection;
|
||||
|
||||
/**
|
||||
* Initialize the network backend.
|
||||
* Returns 1 on success, or 0 on failure.
|
||||
@@ -36,4 +39,15 @@ extern void net_backend_shutdown();
|
||||
*/
|
||||
extern int net_backend_process();
|
||||
|
||||
extern struct timeout_queue* net_backend_get_timeout_queue();
|
||||
|
||||
struct net_cleanup_handler* net_cleanup_initialize(size_t max);
|
||||
|
||||
void net_cleanup_shutdown(struct net_cleanup_handler* handler);
|
||||
|
||||
void net_cleanup_delayed_free(struct net_cleanup_handler* handler, struct net_connection* con);
|
||||
|
||||
void net_cleanup_process(struct net_cleanup_handler* handler);
|
||||
|
||||
|
||||
#endif /* HAVE_UHUB_NETWORK_BACKEND_H */
|
||||
|
||||
@@ -1,37 +1,51 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#define NET_WANT_READ NET_EVENT_READ
|
||||
#define NET_WANT_WRITE NET_EVENT_WRITE
|
||||
#define NET_WANT_ACCEPT 0x0008
|
||||
#define NET_WANT_ACCEPT NET_EVENT_READ
|
||||
#define NET_WANT_SSL_READ 0x0010
|
||||
#define NET_WANT_SSL_WRITE 0x0020
|
||||
#define NET_WANT_SSL_ACCEPT 0x0040
|
||||
#define NET_WANT_SSL_CONNECT 0x0080
|
||||
#define NET_WANT_SSL_X509_LOOKUP 0x0100
|
||||
|
||||
#define NET_PROCESSING_BUSY 0x8000
|
||||
#define NET_CLEANUP 0x4000
|
||||
#define NET_INITIALIZED 0x2000
|
||||
#define NET_TIMER_ENABLED 0x1000
|
||||
|
||||
/* FIXME: Meant for debugging */
|
||||
#define NET_EVENT_SET 0x0800
|
||||
#define NET_CLEANUP 0x8000
|
||||
|
||||
#define NET_CON_STRUCT_BASIC \
|
||||
int sd; /** socket descriptor */ \
|
||||
uint32_t flags; /** Connection flags */ \
|
||||
void* ptr; /** data pointer */ \
|
||||
net_connection_cb callback; /** Callback function */ \
|
||||
struct timeout_evt* timeout; /** timeout event handler */
|
||||
|
||||
#define NET_CON_STRUCT_SSL \
|
||||
SSL* ssl; /** SSL handle */ \
|
||||
uint32_t ssl_state; /** SSL state */ \
|
||||
size_t write_len; /** Length of last SSL_write(), only used if flags is NET_WANT_SSL_READ. */ \
|
||||
|
||||
#ifdef USE_SSL
|
||||
#ifdef SSL_SUPPORT
|
||||
#define NET_CON_STRUCT_COMMON \
|
||||
NET_CON_STRUCT_BASIC \
|
||||
NET_CON_STRUCT_SSL
|
||||
#else
|
||||
#define NET_CON_STRUCT_COMMON \
|
||||
NET_CON_STRUCT_BASIC
|
||||
#endif
|
||||
#endif /* SSL_SUPPORT */
|
||||
|
||||
|
||||
@@ -21,39 +21,39 @@
|
||||
#include "network/common.h"
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
|
||||
enum uhub_tls_state
|
||||
{
|
||||
tls_st_none,
|
||||
tls_st_error,
|
||||
tls_st_accepting,
|
||||
tls_st_connecting,
|
||||
tls_st_connected,
|
||||
tls_st_disconnecting,
|
||||
};
|
||||
|
||||
static int handle_openssl_error(struct net_connection* con, int ret)
|
||||
{
|
||||
uhub_assert(con);
|
||||
|
||||
int error = SSL_get_error(net_con_get_ssl(con), ret);
|
||||
int error = SSL_get_error(con->ssl, ret);
|
||||
switch (error)
|
||||
{
|
||||
case SSL_ERROR_ZERO_RETURN:
|
||||
LOG_PROTO("SSL_get_error: ret=%d, error=%d: SSL_ERROR_ZERO_RETURN", ret, error);
|
||||
con->ssl_state = tls_st_error;
|
||||
return -1;
|
||||
|
||||
case SSL_ERROR_WANT_READ:
|
||||
LOG_PROTO("SSL_get_error: ret=%d, error=%d: SSL_ERROR_WANT_READ", ret, error);
|
||||
net_con_update(con, NET_EVENT_READ | NET_WANT_SSL_READ);
|
||||
con->flags |= NET_WANT_SSL_READ;
|
||||
net_con_update(con, NET_EVENT_READ);
|
||||
return 0;
|
||||
|
||||
case SSL_ERROR_WANT_WRITE:
|
||||
LOG_PROTO("SSL_get_error: ret=%d, error=%d: SSL_ERROR_WANT_WRITE", ret, error);
|
||||
net_con_update(con, NET_EVENT_READ | NET_EVENT_WRITE | NET_WANT_SSL_WRITE);
|
||||
return 0;
|
||||
|
||||
case SSL_ERROR_WANT_CONNECT:
|
||||
LOG_PROTO("SSL_get_error: ret=%d, error=%d: SSL_ERROR_WANT_CONNECT", ret, error);
|
||||
net_con_update(con, NET_EVENT_READ | NET_EVENT_WRITE | NET_WANT_SSL_CONNECT);
|
||||
return 0;
|
||||
|
||||
case SSL_ERROR_WANT_ACCEPT:
|
||||
LOG_PROTO("SSL_get_error: ret=%d, error=%d: SSL_ERROR_WANT_ACCEPT", ret, error);
|
||||
net_con_update(con, NET_EVENT_READ | NET_EVENT_WRITE | NET_WANT_SSL_ACCEPT);
|
||||
return 0;
|
||||
|
||||
case SSL_ERROR_WANT_X509_LOOKUP:
|
||||
LOG_PROTO("SSL_get_error: ret=%d, error=%d: SSL_ERROR_WANT_X509_LOOKUP", ret, error);
|
||||
con->flags |= NET_WANT_SSL_WRITE;
|
||||
net_con_update(con, NET_EVENT_READ | NET_EVENT_WRITE);
|
||||
return 0;
|
||||
|
||||
case SSL_ERROR_SYSCALL:
|
||||
@@ -67,6 +67,7 @@ static int handle_openssl_error(struct net_connection* con, int ret)
|
||||
case SSL_ERROR_SSL:
|
||||
LOG_PROTO("SSL_get_error: ret=%d, error=%d: SSL_ERROR_SSL", ret, error);
|
||||
/* internal openssl error */
|
||||
con->ssl_state = tls_st_error;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -76,14 +77,15 @@ static int handle_openssl_error(struct net_connection* con, int ret)
|
||||
ssize_t net_con_ssl_accept(struct net_connection* con)
|
||||
{
|
||||
uhub_assert(con);
|
||||
|
||||
ssize_t ret = SSL_accept(net_con_get_ssl(con));
|
||||
con->ssl_state = tls_st_accepting;
|
||||
ssize_t ret = SSL_accept(con->ssl);
|
||||
#ifdef NETWORK_DUMP_DEBUG
|
||||
LOG_PROTO("SSL_accept() ret=%d", ret);
|
||||
#endif
|
||||
if (ret > 0)
|
||||
{
|
||||
net_con_update(con, NET_EVENT_READ);
|
||||
con->ssl_state = tls_st_connected;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -96,12 +98,14 @@ ssize_t net_con_ssl_connect(struct net_connection* con)
|
||||
{
|
||||
uhub_assert(con);
|
||||
|
||||
ssize_t ret = SSL_connect(net_con_get_ssl(con));
|
||||
con->ssl_state = tls_st_connecting;
|
||||
ssize_t ret = SSL_connect(con->ssl);
|
||||
#ifdef NETWORK_DUMP_DEBUG
|
||||
LOG_PROTO("SSL_connect() ret=%d", ret);
|
||||
#endif
|
||||
if (ret > 0)
|
||||
{
|
||||
con->ssl_state = tls_st_connected;
|
||||
net_con_update(con, NET_EVENT_READ);
|
||||
}
|
||||
else
|
||||
@@ -119,14 +123,14 @@ ssize_t net_con_ssl_handshake(struct net_connection* con, enum net_con_ssl_mode
|
||||
if (ssl_mode == net_con_ssl_mode_server)
|
||||
{
|
||||
ssl = SSL_new(ssl_ctx);
|
||||
SSL_set_fd(ssl, net_con_get_sd(con));
|
||||
SSL_set_fd(ssl, con->sd);
|
||||
net_con_set_ssl(con, ssl);
|
||||
return net_con_ssl_accept(con);
|
||||
}
|
||||
else
|
||||
{
|
||||
ssl = SSL_new(SSL_CTX_new(TLSv1_method()));
|
||||
SSL_set_fd(ssl, net_con_get_sd(con));
|
||||
SSL_set_fd(ssl, con->sd);
|
||||
net_con_set_ssl(con, ssl);
|
||||
return net_con_ssl_connect(con);
|
||||
}
|
||||
@@ -136,28 +140,42 @@ ssize_t net_con_ssl_handshake(struct net_connection* con, enum net_con_ssl_mode
|
||||
|
||||
ssize_t net_con_send(struct net_connection* con, const void* buf, size_t len)
|
||||
{
|
||||
int ret = net_send(net_con_get_sd(con), buf, len, UHUB_SEND_SIGNAL);
|
||||
if (ret == -1)
|
||||
int ret;
|
||||
#ifdef SSL_SUPPORT
|
||||
if (!con->ssl)
|
||||
{
|
||||
if (net_error() == EWOULDBLOCK || net_error() == EINTR)
|
||||
return 0;
|
||||
return -1;
|
||||
#endif
|
||||
ret = net_send(con->sd, buf, len, UHUB_SEND_SIGNAL);
|
||||
if (ret == -1)
|
||||
{
|
||||
if (net_error() == EWOULDBLOCK || net_error() == EINTR)
|
||||
return 0;
|
||||
return -1;
|
||||
}
|
||||
#ifdef SSL_SUPPORT
|
||||
}
|
||||
else
|
||||
{
|
||||
con->write_len = len;
|
||||
ret = SSL_write(con->ssl, buf, len);
|
||||
LOG_PROTO("SSL_write(con=%p, buf=%p, len=" PRINTF_SIZE_T ") => %d", con, buf, len, ret);
|
||||
if (ret <= 0)
|
||||
{
|
||||
return -handle_openssl_error(con, ret);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t net_con_recv(struct net_connection* con, void* buf, size_t len)
|
||||
{
|
||||
uhub_assert(con);
|
||||
|
||||
int ret;
|
||||
#ifdef SSL_SUPPORT
|
||||
if (!net_con_is_ssl(con))
|
||||
{
|
||||
#endif
|
||||
int ret = net_recv(net_con_get_sd(con), buf, len, 0);
|
||||
#ifdef NETWORK_DUMP_DEBUG
|
||||
LOG_PROTO("net_recv: ret=%d", ret);
|
||||
#endif
|
||||
ret = net_recv(con->sd, buf, len, 0);
|
||||
if (ret == -1)
|
||||
{
|
||||
if (net_error() == EWOULDBLOCK || net_error() == EINTR)
|
||||
@@ -168,16 +186,15 @@ ssize_t net_con_recv(struct net_connection* con, void* buf, size_t len)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
#ifdef SSL_SUPPORT
|
||||
}
|
||||
else
|
||||
{
|
||||
int ret = SSL_read(net_con_get_ssl(con), buf, len);
|
||||
#ifdef NETWORK_DUMP_DEBUG
|
||||
LOG_PROTO("net_recv: ret=%d", ret);
|
||||
#endif
|
||||
if (con->ssl_state == tls_st_error)
|
||||
return -1;
|
||||
|
||||
ret = SSL_read(con->ssl, buf, len);
|
||||
LOG_PROTO("SSL_read(con=%p, buf=%p, len=" PRINTF_SIZE_T ") => %d", con, buf, len, ret);
|
||||
if (ret > 0)
|
||||
{
|
||||
net_con_update(con, NET_EVENT_READ);
|
||||
@@ -186,14 +203,14 @@ ssize_t net_con_recv(struct net_connection* con, void* buf, size_t len)
|
||||
{
|
||||
return -handle_openssl_error(con, ret);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssize_t net_con_peek(struct net_connection* con, void* buf, size_t len)
|
||||
{
|
||||
int ret = net_recv(net_con_get_sd(con), buf, len, MSG_PEEK);
|
||||
int ret = net_recv(con->sd, buf, len, MSG_PEEK);
|
||||
if (ret == -1)
|
||||
{
|
||||
if (net_error() == EWOULDBLOCK || net_error() == EINTR)
|
||||
@@ -205,6 +222,102 @@ ssize_t net_con_peek(struct net_connection* con, void* buf, size_t len)
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
int net_con_is_ssl(struct net_connection* con)
|
||||
{
|
||||
return con->ssl != 0;
|
||||
}
|
||||
|
||||
SSL* net_con_get_ssl(struct net_connection* con)
|
||||
{
|
||||
return con->ssl;
|
||||
}
|
||||
|
||||
void net_con_set_ssl(struct net_connection* con, SSL* ssl)
|
||||
{
|
||||
con->ssl = ssl;
|
||||
}
|
||||
#endif /* SSL_SUPPORT */
|
||||
|
||||
int net_con_get_sd(struct net_connection* con)
|
||||
{
|
||||
return con->sd;
|
||||
}
|
||||
|
||||
void* net_con_get_ptr(struct net_connection* con)
|
||||
{
|
||||
return con->ptr;
|
||||
}
|
||||
|
||||
void net_con_callback(struct net_connection* con, int events)
|
||||
{
|
||||
if (con->flags & NET_CLEANUP)
|
||||
return;
|
||||
|
||||
if (events == NET_EVENT_TIMEOUT)
|
||||
{
|
||||
LOG_TRACE("net_con_callback(%p, TIMEOUT", con);
|
||||
con->callback(con, events, con->ptr);
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
if (!con->ssl)
|
||||
{
|
||||
#endif
|
||||
con->callback(con, events, con->ptr);
|
||||
#ifdef SSL_SUPPORT
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef NETWORK_DUMP_DEBUG
|
||||
LOG_PROTO("net_con_event: events=%d, con=%p, state=%d", events, con, con->ssl_state);
|
||||
#endif
|
||||
switch (con->ssl_state)
|
||||
{
|
||||
case tls_st_none:
|
||||
con->callback(con, events, con->ptr);
|
||||
break;
|
||||
|
||||
case tls_st_error:
|
||||
con->callback(con, NET_EVENT_READ, con->ptr);
|
||||
break;
|
||||
|
||||
case tls_st_accepting:
|
||||
if (net_con_ssl_accept(con) < 0)
|
||||
{
|
||||
con->callback(con, NET_EVENT_READ, con->ptr);
|
||||
}
|
||||
break;
|
||||
|
||||
case tls_st_connecting:
|
||||
if (net_con_ssl_connect(con) < 0)
|
||||
{
|
||||
con->callback(con, NET_EVENT_READ, con->ptr);
|
||||
}
|
||||
break;
|
||||
|
||||
case tls_st_connected:
|
||||
LOG_PROTO("tls_st_connected, events=%s%s, ssl_flags=%s%s", (events & NET_EVENT_READ ? "R" : ""), (events & NET_EVENT_WRITE ? "W" : ""), con->flags & NET_WANT_SSL_READ ? "R" : "", con->flags & NET_WANT_SSL_WRITE ? "W" : "");
|
||||
if (events & NET_EVENT_WRITE && con->flags & NET_WANT_SSL_READ)
|
||||
{
|
||||
con->callback(con, events & NET_EVENT_READ, con->ptr);
|
||||
return;
|
||||
}
|
||||
|
||||
if (events & NET_EVENT_READ && con->flags & NET_WANT_SSL_WRITE)
|
||||
{
|
||||
con->callback(con, events & NET_EVENT_READ, con->ptr);
|
||||
return;
|
||||
}
|
||||
|
||||
con->callback(con, events, con->ptr);
|
||||
break;
|
||||
|
||||
case tls_st_disconnecting:
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -21,17 +21,22 @@
|
||||
#define HAVE_UHUB_NETWORK_CONNECTION_H
|
||||
|
||||
#include "uhub.h"
|
||||
#include "network/common.h"
|
||||
#include "network/backend.h"
|
||||
|
||||
#define NET_EVENT_TIMEOUT 0x0001
|
||||
#define NET_EVENT_READ 0x0002
|
||||
#define NET_EVENT_WRITE 0x0004
|
||||
#define NET_EVENT_SOCKERROR 0x1000 /* Socket error, closed */
|
||||
#define NET_EVENT_CLOSED 0x2000 /* Socket closed */
|
||||
|
||||
struct net_connection;
|
||||
|
||||
typedef void (*net_connection_cb)(struct net_connection*, int event, void* ptr);
|
||||
|
||||
struct net_connection
|
||||
{
|
||||
NET_CON_STRUCT_COMMON
|
||||
};
|
||||
|
||||
extern int net_con_get_sd(struct net_connection* con);
|
||||
extern void* net_con_get_ptr(struct net_connection* con);
|
||||
|
||||
@@ -40,14 +45,14 @@ extern void net_con_destroy(struct net_connection*);
|
||||
extern void net_con_initialize(struct net_connection* con, int sd, net_connection_cb callback, const void* ptr, int events);
|
||||
extern void net_con_reinitialize(struct net_connection* con, net_connection_cb callback, const void* ptr, int events);
|
||||
extern void net_con_update(struct net_connection* con, int events);
|
||||
extern void net_con_callback(struct net_connection* con, int events);
|
||||
|
||||
/**
|
||||
* Close the connection.
|
||||
* This will ensure a connection is closed properly and will generate a NET_EVENT_DESTROYED event which indicates
|
||||
* that the con can safely be deleted (or set to NULL).
|
||||
* @returns 1 if the memory pointed to by con can be freed immediately, or 0 if it needs to go through the NET_EVENT_DESTROYED event.
|
||||
*/
|
||||
extern int net_con_close(struct net_connection* con);
|
||||
extern void net_con_close(struct net_connection* con);
|
||||
|
||||
/**
|
||||
* Send data
|
||||
|
||||
@@ -27,18 +27,10 @@
|
||||
|
||||
#define EPOLL_EVBUFFER 512
|
||||
|
||||
struct net_connection
|
||||
struct net_connection_epoll
|
||||
{
|
||||
int sd;
|
||||
uint32_t flags;
|
||||
net_connection_cb callback;
|
||||
void* ptr;
|
||||
struct epoll_event ev;
|
||||
struct timeout_evt* timeout;
|
||||
#ifdef SSL_SUPPORT
|
||||
SSL* ssl;
|
||||
size_t write_len; /** Length of last SSL_write(), only used if flags is NET_WANT_SSL_READ. */
|
||||
#endif
|
||||
NET_CON_STRUCT_COMMON
|
||||
struct epoll_event ev;
|
||||
};
|
||||
|
||||
struct net_backend
|
||||
@@ -46,15 +38,16 @@ struct net_backend
|
||||
int epfd;
|
||||
size_t num;
|
||||
size_t max;
|
||||
struct net_connection** conns;
|
||||
struct net_connection_epoll** conns;
|
||||
struct epoll_event events[EPOLL_EVBUFFER];
|
||||
time_t now;
|
||||
struct timeout_queue timeout_queue;
|
||||
struct net_cleanup_handler* cleaner;
|
||||
};
|
||||
|
||||
static struct net_backend* g_backend = 0;
|
||||
|
||||
static void net_con_print(const char* prefix, struct net_connection* con)
|
||||
static void net_con_print(const char* prefix, struct net_connection_epoll* con)
|
||||
{
|
||||
char buf[512];
|
||||
int off = snprintf(buf, 512, "%s: net_connection={ sd=%d, flags=%u, callback=%p, ptr=%p, ev={ events=%s%s, data.ptr=%p }",
|
||||
@@ -87,11 +80,12 @@ int net_backend_initialize()
|
||||
|
||||
g_backend->num = 0;
|
||||
g_backend->max = max;
|
||||
g_backend->conns = hub_malloc_zero(sizeof(struct net_connection*) * max);
|
||||
g_backend->conns = hub_malloc_zero(sizeof(struct net_connection_epoll*) * max);
|
||||
memset(g_backend->events, 0, sizeof(g_backend->events));
|
||||
|
||||
g_backend->now = time(0);
|
||||
timeout_queue_initialize(&g_backend->timeout_queue, g_backend->now, 600); /* look max 10 minutes into the future. */
|
||||
g_backend->cleaner = net_cleanup_initialize(max);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -101,6 +95,8 @@ int net_backend_initialize()
|
||||
void net_backend_shutdown()
|
||||
{
|
||||
close(g_backend->epfd);
|
||||
timeout_queue_shutdown(&g_backend->timeout_queue);
|
||||
net_cleanup_shutdown(g_backend->cleaner);
|
||||
hub_free(g_backend->conns);
|
||||
hub_free(g_backend);
|
||||
}
|
||||
@@ -111,8 +107,13 @@ void net_backend_shutdown()
|
||||
int net_backend_process()
|
||||
{
|
||||
int n;
|
||||
LOG_TRACE("epoll_wait: fd=%d, events=%x, max=%zu", g_backend->epfd, g_backend->events, MIN(g_backend->num, EPOLL_EVBUFFER));
|
||||
int res = epoll_wait(g_backend->epfd, g_backend->events, MIN(g_backend->num, EPOLL_EVBUFFER), 1000);
|
||||
size_t secs = timeout_queue_get_next_timeout(&g_backend->timeout_queue, g_backend->now);
|
||||
LOG_TRACE("epoll_wait: fd=%d, events=%x, max=%zu, seconds=%d", g_backend->epfd, g_backend->events, MIN(g_backend->num, EPOLL_EVBUFFER), (int) secs);
|
||||
int res = epoll_wait(g_backend->epfd, g_backend->events, MIN(g_backend->num, EPOLL_EVBUFFER), secs * 1000);
|
||||
|
||||
g_backend->now = time(0);
|
||||
timeout_queue_process(&g_backend->timeout_queue, g_backend->now);
|
||||
|
||||
if (res == -1)
|
||||
{
|
||||
LOG_WARN("epoll_wait returned -1");
|
||||
@@ -121,19 +122,25 @@ int net_backend_process()
|
||||
|
||||
for (n = 0; n < res; n++)
|
||||
{
|
||||
struct net_connection* con = (struct net_connection*) g_backend->events[n].data.ptr;
|
||||
struct net_connection_epoll* con = (struct net_connection_epoll*) g_backend->events[n].data.ptr;
|
||||
int ev = 0;
|
||||
if (g_backend->events[n].events & EPOLLIN) ev |= NET_EVENT_READ;
|
||||
if (g_backend->events[n].events & EPOLLOUT) ev |= NET_EVENT_WRITE;
|
||||
con->callback(con, ev, con->ptr);
|
||||
net_con_callback((struct net_connection*) con, ev);
|
||||
}
|
||||
|
||||
net_cleanup_process(g_backend->cleaner);
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct timeout_queue* net_backend_get_timeout_queue()
|
||||
{
|
||||
return &g_backend->timeout_queue;
|
||||
}
|
||||
|
||||
struct net_connection* net_con_create()
|
||||
{
|
||||
struct net_connection* con = (struct net_connection*) hub_malloc_zero(sizeof(struct net_connection));
|
||||
struct net_connection* con = (struct net_connection*) hub_malloc_zero(sizeof(struct net_connection_epoll));
|
||||
con->sd = -1;
|
||||
return con;
|
||||
}
|
||||
@@ -143,10 +150,11 @@ void net_con_destroy(struct net_connection* con)
|
||||
hub_free(con);
|
||||
}
|
||||
|
||||
void net_con_initialize(struct net_connection* con, int sd, net_connection_cb callback, const void* ptr, int events)
|
||||
void net_con_initialize(struct net_connection* con_, int sd, net_connection_cb callback, const void* ptr, int events)
|
||||
{
|
||||
struct net_connection_epoll* con = (struct net_connection_epoll*) con_;
|
||||
con->sd = sd;
|
||||
con->flags = NET_INITIALIZED;
|
||||
con->flags = 0;
|
||||
con->callback = callback;
|
||||
con->ev.events = 0;
|
||||
con->ptr = (void*) ptr;
|
||||
@@ -176,34 +184,17 @@ void net_con_reinitialize(struct net_connection* con, net_connection_cb callback
|
||||
net_con_update(con, events);
|
||||
}
|
||||
|
||||
void net_con_update(struct net_connection* con, int events)
|
||||
void net_con_update(struct net_connection* con_, int events)
|
||||
{
|
||||
con->ev.events = 0;
|
||||
if (events & NET_EVENT_READ) con->ev.events |= EPOLLIN;
|
||||
if (events & NET_EVENT_WRITE) con->ev.events |= EPOLLOUT;
|
||||
struct net_connection_epoll* con = (struct net_connection_epoll*) con_;
|
||||
int newev = 0;
|
||||
if (events & NET_EVENT_READ) newev |= EPOLLIN;
|
||||
if (events & NET_EVENT_WRITE) newev |= EPOLLOUT;
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
if (events & NET_WANT_SSL_WRITE)
|
||||
con->flags |= NET_WANT_SSL_WRITE;
|
||||
else
|
||||
con->flags &= ~NET_WANT_SSL_WRITE;
|
||||
|
||||
if (events & NET_WANT_SSL_READ)
|
||||
con->flags |= NET_WANT_SSL_READ;
|
||||
else
|
||||
con->flags &= ~NET_WANT_SSL_READ;
|
||||
|
||||
if (events & NET_WANT_SSL_ACCEPT)
|
||||
con->flags |= NET_WANT_SSL_ACCEPT;
|
||||
else
|
||||
con->flags &= ~NET_WANT_SSL_ACCEPT;
|
||||
|
||||
if (events & NET_WANT_SSL_CONNECT)
|
||||
con->flags |= NET_WANT_SSL_CONNECT;
|
||||
else
|
||||
con->flags &= ~NET_WANT_SSL_CONNECT;
|
||||
#endif /* SSL_SUPPORT */
|
||||
if (newev == con->ev.events)
|
||||
return;
|
||||
|
||||
con->ev.events = newev;
|
||||
if (epoll_ctl(g_backend->epfd, EPOLL_CTL_MOD, con->sd, &con->ev) == -1)
|
||||
{
|
||||
LOG_TRACE("epoll_ctl() modify failed.");
|
||||
@@ -211,12 +202,11 @@ void net_con_update(struct net_connection* con, int events)
|
||||
net_con_print("MOD", con);
|
||||
}
|
||||
|
||||
int net_con_close(struct net_connection* con)
|
||||
void net_con_close(struct net_connection* con_)
|
||||
{
|
||||
if (!(con->flags & NET_INITIALIZED))
|
||||
return 0;
|
||||
|
||||
con->flags &= ~NET_INITIALIZED;
|
||||
struct net_connection_epoll* con = (struct net_connection_epoll*) con_;
|
||||
if (con->flags & NET_CLEANUP)
|
||||
return;
|
||||
|
||||
if (con->sd != -1)
|
||||
{
|
||||
@@ -224,83 +214,18 @@ int net_con_close(struct net_connection* con)
|
||||
g_backend->num--;
|
||||
}
|
||||
|
||||
if (timeout_evt_is_scheduled(con->timeout))
|
||||
{
|
||||
timeout_queue_remove(&g_backend->timeout_queue, con->timeout);
|
||||
hub_free(con->timeout);
|
||||
con->timeout = 0;
|
||||
}
|
||||
net_con_clear_timeout(con_);
|
||||
|
||||
if (epoll_ctl(g_backend->epfd, EPOLL_CTL_DEL, con->sd, &con->ev) == -1)
|
||||
{
|
||||
LOG_WARN("epoll_ctl() delete failed.");
|
||||
}
|
||||
|
||||
net_close(con->sd);
|
||||
con->sd = -1;
|
||||
|
||||
net_con_print("DEL", con);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
int net_con_is_ssl(struct net_connection* con)
|
||||
{
|
||||
|
||||
return con->ssl != 0;
|
||||
}
|
||||
|
||||
SSL* net_con_get_ssl(struct net_connection* con)
|
||||
{
|
||||
return con->ssl;
|
||||
}
|
||||
|
||||
void net_con_set_ssl(struct net_connection* con, SSL* ssl)
|
||||
{
|
||||
con->ssl = ssl;
|
||||
}
|
||||
#endif
|
||||
|
||||
int net_con_get_sd(struct net_connection* con)
|
||||
{
|
||||
return con->sd;
|
||||
}
|
||||
|
||||
void* net_con_get_ptr(struct net_connection* con)
|
||||
{
|
||||
return con->ptr;
|
||||
}
|
||||
|
||||
|
||||
void timeout_evt_initialize(struct timeout_evt*, timeout_evt_cb, void* ptr);
|
||||
void timeout_evt_reset(struct timeout_evt*);
|
||||
int timeout_evt_is_scheduled(struct timeout_evt*);
|
||||
|
||||
static void timeout_callback(struct timeout_evt* evt)
|
||||
{
|
||||
struct net_connection* con = (struct net_connection*) evt->ptr;
|
||||
con->callback(con, NET_EVENT_TIMEOUT, con->ptr);
|
||||
}
|
||||
|
||||
|
||||
void net_con_set_timeout(struct net_connection* con, int seconds)
|
||||
{
|
||||
if (!con->timeout)
|
||||
{
|
||||
con->timeout = hub_malloc_zero(sizeof(struct timeout_evt));
|
||||
timeout_evt_initialize(con->timeout, timeout_callback, con);
|
||||
timeout_queue_insert(&g_backend->timeout_queue, con->timeout, seconds);
|
||||
}
|
||||
else
|
||||
{
|
||||
timeout_queue_reschedule(&g_backend->timeout_queue, con->timeout, seconds);
|
||||
}
|
||||
}
|
||||
|
||||
void net_con_clear_timeout(struct net_connection* con)
|
||||
{
|
||||
if (con->timeout && timeout_evt_is_scheduled(con->timeout))
|
||||
{
|
||||
timeout_queue_remove(&g_backend->timeout_queue, con->timeout);
|
||||
hub_free(con->timeout);
|
||||
con->timeout = 0;
|
||||
}
|
||||
net_cleanup_delayed_free(g_backend->cleaner, con_);
|
||||
}
|
||||
|
||||
#endif /* USE_EPOLL */
|
||||
|
||||
@@ -1,399 +0,0 @@
|
||||
/*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef USE_LIBEVENT
|
||||
|
||||
struct net_connection
|
||||
{
|
||||
NET_CON_STRUCT_COMMON
|
||||
struct event event; /** libevent struct for read/write events */
|
||||
struct event timeout; /** Used for internal timeout handling */
|
||||
};
|
||||
|
||||
struct net_timer
|
||||
{
|
||||
unsigned int initialized;
|
||||
struct event timeout;
|
||||
net_timeout_cb callback;
|
||||
void* ptr;
|
||||
};
|
||||
|
||||
int net_con_get_sd(struct net_connection* con)
|
||||
{
|
||||
return con->sd;
|
||||
}
|
||||
|
||||
void* net_con_get_ptr(struct net_connection* con)
|
||||
{
|
||||
return con->ptr;
|
||||
}
|
||||
|
||||
struct net_connection* net_con_create()
|
||||
{
|
||||
struct net_connection* con = (struct net_connection*) hub_malloc(sizeof(struct net_connection));
|
||||
return con;
|
||||
}
|
||||
|
||||
static inline int net_con_flag_get(struct net_connection* con, unsigned int flag)
|
||||
{
|
||||
return con->flags & flag;
|
||||
}
|
||||
|
||||
static inline void net_con_flag_set(struct net_connection* con, unsigned int flag)
|
||||
{
|
||||
con->flags |= flag;
|
||||
}
|
||||
|
||||
static inline void net_con_flag_unset(struct net_connection* con, unsigned int flag)
|
||||
{
|
||||
con->flags &= ~flag;
|
||||
}
|
||||
|
||||
void net_con_destroy(struct net_connection* con)
|
||||
{
|
||||
hub_free(con);
|
||||
}
|
||||
|
||||
static inline int net_con_convert_to_libevent_mask(int ev)
|
||||
{
|
||||
int events = 0;
|
||||
if (ev & NET_EVENT_READ) events |= EV_READ;
|
||||
if (ev & NET_EVENT_WRITE) events |= EV_WRITE;
|
||||
return events;
|
||||
}
|
||||
|
||||
static inline int net_con_convert_from_libevent_mask(int ev)
|
||||
{
|
||||
int events = 0;
|
||||
if (ev & EV_TIMEOUT) events |= NET_EVENT_TIMEOUT;
|
||||
if (ev & EV_READ) events |= NET_EVENT_READ;
|
||||
if (ev & EV_WRITE) events |= NET_EVENT_WRITE;
|
||||
return events;
|
||||
}
|
||||
|
||||
static void net_con_event(int fd, short ev, void *arg);
|
||||
|
||||
|
||||
int net_con_close(struct net_connection* con)
|
||||
{
|
||||
uhub_assert(con);
|
||||
|
||||
con->ptr = 0;
|
||||
|
||||
if (net_con_flag_get(con, NET_CLEANUP))
|
||||
return 0;
|
||||
|
||||
if (net_con_flag_get(con, NET_PROCESSING_BUSY))
|
||||
{
|
||||
net_con_flag_set(con, NET_CLEANUP);
|
||||
return 0;
|
||||
}
|
||||
|
||||
net_con_after_close(con);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void net_con_set(struct net_connection* con)
|
||||
{
|
||||
uhub_assert(con);
|
||||
|
||||
int ev = 0;
|
||||
if (net_con_flag_get(con, NET_WANT_READ | NET_WANT_SSL_READ)) ev |= EV_READ;
|
||||
if (net_con_flag_get(con, NET_WANT_WRITE | NET_WANT_SSL_WRITE)) ev |= EV_WRITE;
|
||||
|
||||
if (net_con_flag_get(con, NET_EVENT_SET) != 0)
|
||||
{
|
||||
event_del(&con->event);
|
||||
}
|
||||
net_con_flag_set(con, NET_EVENT_SET);
|
||||
|
||||
LOG_MEMORY("SET: set+add: CON={ %p, %p, %d, %d}", con, &con->event, con->sd, ev);
|
||||
event_set(&con->event, con->sd, ev, net_con_event, con);
|
||||
event_add(&con->event, 0);
|
||||
|
||||
net_con_flag_set(con, NET_INITIALIZED);
|
||||
}
|
||||
|
||||
static void net_con_after_close(struct net_connection* con)
|
||||
{
|
||||
if (net_con_flag_get(con, NET_INITIALIZED))
|
||||
{
|
||||
LOG_MEMORY("DEL: close: CON={ %p, %p, %d, %d}", con, &con->event, con->sd, -1);
|
||||
net_con_flag_unset(con, NET_EVENT_SET);
|
||||
|
||||
event_del(&con->event);
|
||||
net_con_flag_unset(con, NET_INITIALIZED);
|
||||
}
|
||||
|
||||
net_con_clear_timeout(con);
|
||||
net_close(con->sd);
|
||||
con->sd = -1;
|
||||
|
||||
hub_free(con);
|
||||
}
|
||||
|
||||
void net_con_initialize(struct net_connection* con, int sd, net_connection_cb callback, const void* ptr, int ev)
|
||||
{
|
||||
uhub_assert(con);
|
||||
|
||||
int events = net_con_convert_to_libevent_mask(ev);
|
||||
if (ev & NET_EVENT_READ) net_con_flag_set(con, NET_WANT_READ);
|
||||
if (ev & NET_EVENT_WRITE) net_con_flag_set(con, NET_WANT_WRITE);
|
||||
|
||||
con->sd = sd;
|
||||
con->flags = 0;
|
||||
con->ptr = (void*) ptr;
|
||||
con->callback = callback;
|
||||
con->last_send = time(0);
|
||||
con->last_recv = con->last_send;
|
||||
|
||||
if (ev)
|
||||
{
|
||||
uhub_assert(net_con_flag_get(con, NET_EVENT_SET) == 0);
|
||||
net_con_flag_set(con, NET_EVENT_SET);
|
||||
|
||||
LOG_MEMORY("SET: init: CON={ %p, %p, %d, %d}", con, &con->event, con->sd, ev);
|
||||
event_set(&con->event, con->sd, events, net_con_event, con);
|
||||
event_add(&con->event, 0);
|
||||
net_con_flag_set(con, NET_INITIALIZED);
|
||||
}
|
||||
|
||||
net_set_nonblocking(sd, 1);
|
||||
net_set_nosigpipe(sd, 1);
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
con->ssl = NULL;
|
||||
con->write_len = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void net_con_reinitialize(struct net_connection* con, net_connection_cb callback, const void* ptr, int events)
|
||||
{
|
||||
uhub_assert(con);
|
||||
con->callback = callback;
|
||||
con->ptr = (void*) ptr;
|
||||
net_con_update(con, events);
|
||||
}
|
||||
|
||||
void net_con_update(struct net_connection* con, int ev)
|
||||
{
|
||||
uhub_assert(con);
|
||||
|
||||
if (ev & NET_EVENT_READ)
|
||||
net_con_flag_set(con, NET_EVENT_READ);
|
||||
else
|
||||
net_con_flag_unset(con, NET_EVENT_READ);
|
||||
|
||||
if (ev & NET_EVENT_WRITE)
|
||||
net_con_flag_set(con, NET_EVENT_WRITE);
|
||||
else
|
||||
net_con_flag_unset(con, NET_EVENT_WRITE);
|
||||
|
||||
if (!net_con_flag_get(con, NET_PROCESSING_BUSY))
|
||||
{
|
||||
net_con_set(con);
|
||||
}
|
||||
}
|
||||
|
||||
#define CALLBACK(CON, EVENTS) \
|
||||
if (CON->callback) \
|
||||
CON->callback(con, EVENTS, CON->ptr);
|
||||
|
||||
static void net_con_event(int fd, short ev, void *arg)
|
||||
{
|
||||
struct net_connection* con = (struct net_connection*) arg;
|
||||
int events = net_con_convert_from_libevent_mask(ev);
|
||||
|
||||
if (!net_con_flag_get(con, NET_INITIALIZED))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (net_con_flag_get(con, NET_CLEANUP))
|
||||
{
|
||||
net_con_after_close(con);
|
||||
return;
|
||||
}
|
||||
|
||||
net_con_flag_set(con, NET_PROCESSING_BUSY);
|
||||
|
||||
// uhub_assert(net_con_flag_get(con, NET_EVENT_SET) != 0);
|
||||
net_con_flag_unset(con, NET_EVENT_SET);
|
||||
|
||||
LOG_MEMORY("EVT: process: CON={ %p, %p, %d, %d}", con, &con->event, con->sd, (int) ev);
|
||||
|
||||
#ifdef SSL_SUPPORT
|
||||
if (!con->ssl)
|
||||
{
|
||||
#endif
|
||||
CALLBACK(con, events);
|
||||
#ifdef SSL_SUPPORT
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef NETWORK_DUMP_DEBUG
|
||||
LOG_PROTO("net_con_event: events=%d, con=%p", ev, con);
|
||||
#endif
|
||||
if (ev & (EV_READ | EV_WRITE))
|
||||
{
|
||||
if (net_con_flag_get(con, NET_WANT_SSL_ACCEPT))
|
||||
{
|
||||
if (net_con_ssl_accept(con) < 0)
|
||||
CALLBACK(con, NET_EVENT_SOCKERROR);
|
||||
}
|
||||
else if (net_con_flag_get(con, NET_WANT_SSL_CONNECT))
|
||||
{
|
||||
if (net_con_ssl_connect(con) < 0)
|
||||
CALLBACK(con, NET_EVENT_SOCKERROR);
|
||||
}
|
||||
else if (ev == EV_READ && net_con_flag_get(con, NET_WANT_SSL_READ))
|
||||
{
|
||||
CALLBACK(con, NET_EVENT_WRITE);
|
||||
}
|
||||
else if (ev == EV_WRITE && net_con_flag_get(con, NET_WANT_SSL_WRITE))
|
||||
{
|
||||
CALLBACK(con, events & NET_EVENT_READ);
|
||||
}
|
||||
else
|
||||
{
|
||||
CALLBACK(con, events);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
CALLBACK(con, events);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
net_con_flag_unset(con, NET_PROCESSING_BUSY);
|
||||
|
||||
if (net_con_flag_get(con, NET_CLEANUP))
|
||||
{
|
||||
net_con_after_close(con);
|
||||
}
|
||||
else
|
||||
{
|
||||
net_con_set(con);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void net_timer_event(int fd, short ev, void *arg)
|
||||
{
|
||||
struct net_timer* timer = (struct net_timer*) arg;
|
||||
timer->callback(timer, timer->ptr);
|
||||
}
|
||||
|
||||
void net_timer_initialize(struct net_timer* timer, net_timeout_cb callback, void* ptr)
|
||||
{
|
||||
timer->initialized = 0;
|
||||
timer->callback = callback;
|
||||
timer->ptr = ptr;
|
||||
}
|
||||
|
||||
void net_timer_reset(struct net_timer* timer, int seconds)
|
||||
{
|
||||
struct timeval timeout = { seconds, 0 };
|
||||
if (timer->initialized)
|
||||
{
|
||||
evtimer_del(&timer->timeout);
|
||||
timer->initialized = 0;
|
||||
}
|
||||
evtimer_set(&timer->timeout, net_timer_event, timer);
|
||||
evtimer_add(&timer->timeout, &timeout);
|
||||
}
|
||||
|
||||
void net_timer_shutdown(struct net_timer* timer)
|
||||
{
|
||||
if (timer->initialized)
|
||||
{
|
||||
evtimer_del(&timer->timeout);
|
||||
timer->initialized = 0;
|
||||
}
|
||||
timer->callback = 0;
|
||||
timer->ptr = 0;
|
||||
}
|
||||
|
||||
|
||||
void net_con_set_timeout(struct net_connection* con, int seconds)
|
||||
{
|
||||
uhub_assert(con);
|
||||
|
||||
struct timeval timeout = { seconds, 0 };
|
||||
net_con_clear_timeout(con);
|
||||
|
||||
evtimer_set(&con->timeout, net_con_event, con);
|
||||
evtimer_add(&con->timeout, &timeout);
|
||||
net_con_flag_set(con, NET_TIMER_ENABLED);
|
||||
}
|
||||
|
||||
void net_con_clear_timeout(struct net_connection* con)
|
||||
{
|
||||
uhub_assert(con);
|
||||
|
||||
if (net_con_flag_get(con, NET_TIMER_ENABLED))
|
||||
{
|
||||
evtimer_del(&con->timeout);
|
||||
net_con_flag_unset(con, NET_TIMER_ENABLED);
|
||||
}
|
||||
}
|
||||
|
||||
static struct event_base g_evbase = 0;
|
||||
|
||||
/**
|
||||
* Initialize the network backend.
|
||||
* Returns 1 on success, or 0 on failure.
|
||||
*/
|
||||
int net_backend_initialize()
|
||||
{
|
||||
g_evbase = event_init();
|
||||
if (!g_evbase)
|
||||
{
|
||||
LOG_ERROR("Unable to initialize libevent.");
|
||||
return 0;
|
||||
}
|
||||
LOG_DEBUG("Using libevent %s, backend: %s", event_get_version(), event_get_method());
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown the network connection backend.
|
||||
*/
|
||||
void net_backend_shutdown()
|
||||
{
|
||||
event_base_free(g_evbase);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the network backend.
|
||||
*/
|
||||
int net_backend_process()
|
||||
{
|
||||
int ret = event_base_loop(g_evbase, EVLOOP_ONCE);
|
||||
if (ret != 0)
|
||||
{
|
||||
LOG_DEBUG("event_base_loop returned: %d", (int) ret);
|
||||
}
|
||||
if (ret < 0)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
#endif /* USE_LIBEVENT */
|
||||
@@ -376,16 +376,16 @@ int net_accept(int fd, struct ip_addr_encap* ipaddr)
|
||||
if (ipaddr)
|
||||
{
|
||||
memset(ipaddr, 0, sizeof(struct ip_addr_encap));
|
||||
ipaddr->af = addr4->sin_family;
|
||||
ipaddr->af = addr.ss_family;;
|
||||
if (ipaddr->af == AF_INET6)
|
||||
{
|
||||
char address[INET6_ADDRSTRLEN+1] = { 0, };
|
||||
net_address_to_string(AF_INET6, (void*) &addr6->sin6_addr, address, INET6_ADDRSTRLEN+1);
|
||||
if (!strncmp(address, "::ffff:", 7))
|
||||
if (strchr(address, '.'))
|
||||
{
|
||||
/* Hack to convert IPv6 mapped IPv4 addresses to true IPv4 addresses */
|
||||
net_string_to_address(AF_INET, address + 7, (void*) &ipaddr->internal_ip_data.in);
|
||||
ipaddr->af = AF_INET;
|
||||
net_string_to_address(AF_INET, address, (void*) &ipaddr->internal_ip_data.in);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -526,7 +526,15 @@ const char* net_address_to_string(int af, const void* src, char* dst, socklen_t
|
||||
|
||||
return NULL;
|
||||
#else
|
||||
return inet_ntop(af, src, dst, cnt);
|
||||
if (inet_ntop(af, src, dst, cnt))
|
||||
{
|
||||
if (af == AF_INET6 && strncmp(dst, "::ffff:", 7) == 0) /* IPv6 mapped IPv4 address. */
|
||||
{
|
||||
memmove(dst, dst + 7, cnt - 7);
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -573,9 +581,6 @@ int net_string_to_address(int af, const char* src, void* dst)
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
const char* net_get_peer_address(int fd)
|
||||
{
|
||||
static char address[INET6_ADDRSTRLEN+1];
|
||||
@@ -594,21 +599,16 @@ const char* net_get_peer_address(int fd)
|
||||
|
||||
if (getpeername(fd, (struct sockaddr*) name, &namelen) != -1)
|
||||
{
|
||||
int af = name4->sin_family;
|
||||
int af = storage.ss_family;
|
||||
if (af == AF_INET6)
|
||||
{
|
||||
net_address_to_string(af, (void*) &name6->sin6_addr, address, INET6_ADDRSTRLEN);
|
||||
if (strncmp(address, "::ffff:", 7) == 0) /* IPv6 mapped IPv4 address. */
|
||||
{
|
||||
return &address[7];
|
||||
}
|
||||
return address;
|
||||
}
|
||||
else
|
||||
{
|
||||
net_address_to_string(af, (void*) &name4->sin_addr, address, INET6_ADDRSTRLEN);
|
||||
return address;
|
||||
}
|
||||
return address;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -619,6 +619,45 @@ const char* net_get_peer_address(int fd)
|
||||
return "0.0.0.0";
|
||||
}
|
||||
|
||||
const char* net_get_local_address(int fd)
|
||||
{
|
||||
static char address[INET6_ADDRSTRLEN+1];
|
||||
struct sockaddr_storage storage;
|
||||
struct sockaddr_in6* name6;
|
||||
struct sockaddr_in* name4;
|
||||
struct sockaddr* name;
|
||||
|
||||
memset(address, 0, INET6_ADDRSTRLEN);
|
||||
socklen_t namelen = sizeof(struct sockaddr_storage);
|
||||
memset(&storage, 0, namelen);
|
||||
|
||||
name6 = (struct sockaddr_in6*) &storage;
|
||||
name4 = (struct sockaddr_in*) &storage;
|
||||
name = (struct sockaddr*) &storage;
|
||||
|
||||
if (getsockname(fd, (struct sockaddr*) name, &namelen) != -1)
|
||||
{
|
||||
int af = storage.ss_family;
|
||||
if (af == AF_INET6)
|
||||
{
|
||||
net_address_to_string(af, (void*) &name6->sin6_addr, address, INET6_ADDRSTRLEN);
|
||||
}
|
||||
else
|
||||
{
|
||||
net_address_to_string(af, (void*) &name4->sin_addr, address, INET6_ADDRSTRLEN);
|
||||
}
|
||||
return address;
|
||||
}
|
||||
else
|
||||
{
|
||||
net_error_out(fd, "net_get_local_address");
|
||||
net_stats_add_error();
|
||||
}
|
||||
|
||||
return "0.0.0.0";
|
||||
}
|
||||
|
||||
|
||||
|
||||
ssize_t net_recv(int fd, void* buf, size_t len, int flags)
|
||||
{
|
||||
|
||||
@@ -224,6 +224,8 @@ extern int net_is_ipv6_supported();
|
||||
*/
|
||||
extern const char* net_get_peer_address(int fd);
|
||||
|
||||
extern const char* net_get_local_address(int fd);
|
||||
|
||||
/**
|
||||
* See man(3) inet_ntop.
|
||||
*/
|
||||
|
||||
216
src/network/select.c
Normal file
216
src/network/select.c
Normal file
@@ -0,0 +1,216 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, 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"
|
||||
|
||||
#ifdef USE_SELECT
|
||||
|
||||
#include "network/connection.h"
|
||||
#include "network/common.h"
|
||||
#include "network/backend.h"
|
||||
|
||||
struct net_connection_select
|
||||
{
|
||||
NET_CON_STRUCT_COMMON
|
||||
};
|
||||
|
||||
struct net_backend
|
||||
{
|
||||
size_t num;
|
||||
size_t max;
|
||||
struct net_connection_select** conns;
|
||||
fd_set rfds;
|
||||
fd_set wfds;
|
||||
time_t now;
|
||||
struct timeout_queue timeout_queue;
|
||||
struct net_cleanup_handler* cleaner;
|
||||
};
|
||||
|
||||
static struct net_backend* g_backend = 0;
|
||||
|
||||
static void net_con_print(const char* prefix, struct net_connection_select* con)
|
||||
{
|
||||
char buf[512];
|
||||
int off = snprintf(buf, 512, "%s: net_connection={ sd=%d, flags=%u, callback=%p, ptr=%p, events=%s%s",
|
||||
prefix, con->sd, con->flags, con->callback, con->ptr, (con->flags & NET_EVENT_READ ? "R" : ""),(con->flags & NET_EVENT_WRITE ? "W" : ""));
|
||||
if (con->timeout)
|
||||
{
|
||||
sprintf(buf + off, ", timeout={ %d seconds left }", (int) (con->timeout->timestamp - g_backend->now));
|
||||
}
|
||||
else
|
||||
{
|
||||
sprintf(buf + off, ", timeout=NULL");
|
||||
}
|
||||
LOG_TRACE(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the network backend.
|
||||
* Returns 1 on success, or 0 on failure.
|
||||
*/
|
||||
int net_backend_initialize()
|
||||
{
|
||||
size_t max = net_get_max_sockets();
|
||||
g_backend = hub_malloc(sizeof(struct net_backend));
|
||||
g_backend->num = 0;
|
||||
g_backend->max = max;
|
||||
g_backend->conns = hub_malloc_zero(sizeof(struct net_connection_select*) * max);
|
||||
FD_ZERO(&g_backend->rfds);
|
||||
FD_ZERO(&g_backend->wfds);
|
||||
g_backend->now = time(0);
|
||||
timeout_queue_initialize(&g_backend->timeout_queue, g_backend->now, 600); /* look max 10 minutes into the future. */
|
||||
g_backend->cleaner = net_cleanup_initialize(max);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shutdown the network connection backend.
|
||||
*/
|
||||
void net_backend_shutdown()
|
||||
{
|
||||
timeout_queue_shutdown(&g_backend->timeout_queue);
|
||||
net_cleanup_shutdown(g_backend->cleaner);
|
||||
hub_free(g_backend->conns);
|
||||
hub_free(g_backend);
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the network backend.
|
||||
*/
|
||||
int net_backend_process()
|
||||
{
|
||||
int n, found, maxfd;
|
||||
struct timeval tval;
|
||||
FD_ZERO(&g_backend->rfds);
|
||||
FD_ZERO(&g_backend->wfds);
|
||||
|
||||
size_t secs = timeout_queue_get_next_timeout(&g_backend->timeout_queue, g_backend->now);
|
||||
tval.tv_secs = secs;
|
||||
tval.tv_usecs = 0;
|
||||
|
||||
for (n = 0, found = 0; found < g_backend->num && n < g_backend->max; n++)
|
||||
{
|
||||
struct net_connection_select* con = g_backend->conns[n];
|
||||
if (con)
|
||||
{
|
||||
if (con->flags & NET_EVENT_READ) FD_SET(con->sd, &g_backend->rfds);
|
||||
if (con->flags & NET_EVENT_WRITE) FD_SET(con->sd, &g_backend->wfds);
|
||||
found++;
|
||||
maxfd = con->sd;
|
||||
}
|
||||
}
|
||||
|
||||
int res = select(maxfd+1, &g_backend->rfds, &g_backend->wfds, 0, &tval);
|
||||
g_backend->now = time(0);
|
||||
timeout_queue_process(&g_backend->timeout_queue, g_backend->now);
|
||||
|
||||
if (res == -1)
|
||||
{
|
||||
LOG_WARN("select returned -1");
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (n = 0, found = 0; found < res && n < (maxfd+1); n++)
|
||||
{
|
||||
struct net_connection_select* con = g_backend->conns[n];
|
||||
if (con)
|
||||
{
|
||||
int ev = 0;
|
||||
if (FD_ISSET(con->sd, &g_backend->rfds)) ev |= NET_EVENT_READ;
|
||||
if (FD_ISSET(con->sd, &g_backend->wfds)) ev |= NET_EVENT_WRITE;
|
||||
|
||||
if (ev)
|
||||
{
|
||||
net_con_callback((struct net_connection*) con, ev);
|
||||
found++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
net_cleanup_process(g_backend->cleaner);
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct timeout_queue* net_backend_get_timeout_queue()
|
||||
{
|
||||
return &g_backend->timeout_queue;
|
||||
}
|
||||
|
||||
struct net_connection* net_con_create()
|
||||
{
|
||||
struct net_connection* con = (struct net_connection*) hub_malloc_zero(sizeof(struct net_connection_select));
|
||||
con->sd = -1;
|
||||
return con;
|
||||
}
|
||||
|
||||
void net_con_destroy(struct net_connection* con)
|
||||
{
|
||||
hub_free(con);
|
||||
}
|
||||
|
||||
void net_con_initialize(struct net_connection* con_, int sd, net_connection_cb callback, const void* ptr, int events)
|
||||
{
|
||||
struct net_connection_select* con = (struct net_connection_select*) con_;
|
||||
con->sd = sd;
|
||||
con->flags = events;
|
||||
con->callback = callback;
|
||||
con->ptr = (void*) ptr;
|
||||
|
||||
net_set_nonblocking(con->sd, 1);
|
||||
net_set_nosigpipe(con->sd, 1);
|
||||
|
||||
g_backend->conns[sd] = con;
|
||||
g_backend->num++;
|
||||
net_con_print("ADD", con);
|
||||
}
|
||||
|
||||
void net_con_reinitialize(struct net_connection* con, net_connection_cb callback, const void* ptr, int events)
|
||||
{
|
||||
con->callback = callback;
|
||||
con->ptr = (void*) ptr;
|
||||
net_con_update(con, events);
|
||||
}
|
||||
|
||||
void net_con_update(struct net_connection* con, int events)
|
||||
{
|
||||
con->flags = events;
|
||||
net_con_print("MOD", (struct net_connection_select*) con);
|
||||
}
|
||||
|
||||
void net_con_close(struct net_connection* con)
|
||||
{
|
||||
if (con->flags & NET_CLEANUP)
|
||||
return;
|
||||
|
||||
if (con->sd != -1)
|
||||
{
|
||||
g_backend->conns[con->sd] = 0;
|
||||
g_backend->num--;
|
||||
}
|
||||
|
||||
net_con_clear_timeout(con);
|
||||
|
||||
net_close(con->sd);
|
||||
con->sd = -1;
|
||||
|
||||
net_con_print("DEL", (struct net_connection_select*) con);
|
||||
net_cleanup_delayed_free(g_backend->cleaner, con);
|
||||
}
|
||||
|
||||
#endif /* USE_SELECT */
|
||||
50
src/network/timer.c
Normal file
50
src/network/timer.c
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* uhub - A tiny ADC p2p connection hub
|
||||
* Copyright (C) 2007-2010, 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"
|
||||
#include "network/connection.h"
|
||||
|
||||
static void timeout_callback(struct timeout_evt* evt)
|
||||
{
|
||||
net_con_callback((struct net_connection*) evt->ptr, NET_EVENT_TIMEOUT);
|
||||
}
|
||||
|
||||
void net_con_set_timeout(struct net_connection* con, int seconds)
|
||||
{
|
||||
if (!con->timeout)
|
||||
{
|
||||
con->timeout = hub_malloc_zero(sizeof(struct timeout_evt));
|
||||
timeout_evt_initialize(con->timeout, timeout_callback, con);
|
||||
timeout_queue_insert(net_backend_get_timeout_queue(), con->timeout, seconds);
|
||||
}
|
||||
else
|
||||
{
|
||||
timeout_queue_reschedule(net_backend_get_timeout_queue(), con->timeout, seconds);
|
||||
}
|
||||
}
|
||||
|
||||
void net_con_clear_timeout(struct net_connection* con)
|
||||
{
|
||||
if (con->timeout && timeout_evt_is_scheduled(con->timeout))
|
||||
{
|
||||
timeout_queue_remove(net_backend_get_timeout_queue(), con->timeout);
|
||||
hub_free(con->timeout);
|
||||
con->timeout = 0;
|
||||
}
|
||||
}
|
||||
@@ -102,13 +102,12 @@
|
||||
|
||||
#define uhub_assert assert
|
||||
|
||||
#ifdef USE_LIBEVENT
|
||||
#include <event.h>
|
||||
#else
|
||||
#ifdef __linux__
|
||||
#define USE_EPOLL
|
||||
#include <sys/epoll.h>
|
||||
#endif
|
||||
#else
|
||||
#define USE_SELECT
|
||||
#include <sys/select.h>
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__sun__)
|
||||
|
||||
@@ -75,19 +75,13 @@ static void event_callback(struct net_connection* con, int events, void *arg)
|
||||
{
|
||||
struct ADC_client* client = (struct ADC_client*) net_con_get_ptr(con);
|
||||
|
||||
if (events == NET_EVENT_SOCKERROR || events == NET_EVENT_CLOSED)
|
||||
{
|
||||
printf("NET_EVENT_SOCKERROR || NET_EVENT_CLOSED\n");
|
||||
client->callback(client, ADC_CLIENT_DISCONNECTED, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (events == NET_EVENT_TIMEOUT)
|
||||
{
|
||||
if (client->state == ps_conn)
|
||||
{
|
||||
client->callback(client, ADC_CLIENT_DISCONNECTED, 0);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (events & NET_EVENT_READ)
|
||||
|
||||
@@ -330,7 +330,7 @@ void runloop(size_t clients)
|
||||
|
||||
while (running)
|
||||
{
|
||||
event_base_loop(net_get_evbase(), EVLOOP_ONCE);
|
||||
net_backend_process();
|
||||
}
|
||||
|
||||
for (n = 0; n < clients; n++)
|
||||
@@ -413,63 +413,17 @@ void parse_command_line(int argc, char** argv)
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef WIN32
|
||||
static void handle_signal(int signal, short events, void* arg)
|
||||
{
|
||||
switch (signal)
|
||||
{
|
||||
case SIGINT:
|
||||
case SIGTERM:
|
||||
running = 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static struct event signal_events[10];
|
||||
static int signals[] =
|
||||
{
|
||||
SIGINT, /* Interrupt the application */
|
||||
SIGTERM, /* Terminate the application */
|
||||
0
|
||||
};
|
||||
|
||||
void setup_signal_handlers()
|
||||
{
|
||||
int i = 0;
|
||||
for (i = 0; signals[i]; i++)
|
||||
{
|
||||
signal_set(&signal_events[i], signals[i], handle_signal, NULL);
|
||||
signal_add(&signal_events[i], NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void shutdown_signal_handlers()
|
||||
{
|
||||
int i = 0;
|
||||
for (i = 0; signals[i]; i++)
|
||||
{
|
||||
signal_del(&signal_events[i]);
|
||||
}
|
||||
}
|
||||
#endif /* WIN32 */
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
parse_command_line(argc, argv);
|
||||
|
||||
net_initialize();
|
||||
setup_signal_handlers();
|
||||
|
||||
hub_log_initialize(NULL, 0);
|
||||
hub_set_log_verbosity(1000);
|
||||
|
||||
runloop(cfg_clients);
|
||||
|
||||
shutdown_signal_handlers();
|
||||
net_destroy();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -63,6 +63,7 @@ static int handle(struct ADC_client* client, enum ADC_client_callback_type type,
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int running = 1;
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
@@ -79,7 +80,10 @@ int main(int argc, char** argv)
|
||||
ADC_client_set_callback(&client, handle);
|
||||
ADC_client_connect(&client, argv[1]);
|
||||
|
||||
event_dispatch();
|
||||
while (running)
|
||||
{
|
||||
net_backend_process();
|
||||
}
|
||||
|
||||
ADC_client_destroy(&client);
|
||||
net_destroy();
|
||||
|
||||
@@ -36,7 +36,6 @@ void timeout_evt_reset(struct timeout_evt* t)
|
||||
|
||||
int timeout_evt_is_scheduled(struct timeout_evt* t)
|
||||
{
|
||||
if (!t) return 0;
|
||||
return !!t->prev;
|
||||
}
|
||||
|
||||
@@ -59,12 +58,11 @@ size_t timeout_queue_process(struct timeout_queue* t, time_t now)
|
||||
size_t pos;
|
||||
size_t events = 0;
|
||||
struct timeout_evt* evt = 0;
|
||||
for (pos = t->last; pos < now; pos++)
|
||||
for (pos = t->last; pos <= now; pos++)
|
||||
{
|
||||
while ((evt = t->events[pos % t->max]))
|
||||
{
|
||||
timeout_queue_remove(t, evt);
|
||||
timeout_evt_reset(evt);
|
||||
evt->callback(evt);
|
||||
events++;
|
||||
}
|
||||
@@ -73,6 +71,19 @@ size_t timeout_queue_process(struct timeout_queue* t, time_t now)
|
||||
return events;
|
||||
}
|
||||
|
||||
size_t timeout_queue_get_next_timeout(struct timeout_queue* t, time_t now)
|
||||
{
|
||||
size_t seconds = 0;
|
||||
while (t->events[(now + seconds) % t->max] == NULL && seconds < t->max)
|
||||
{
|
||||
seconds++;
|
||||
}
|
||||
if (seconds == 0)
|
||||
return 1;
|
||||
return seconds;
|
||||
}
|
||||
|
||||
|
||||
void timeout_queue_insert(struct timeout_queue* t, struct timeout_evt* evt, size_t seconds)
|
||||
{
|
||||
struct timeout_evt* first;
|
||||
@@ -82,16 +93,18 @@ void timeout_queue_insert(struct timeout_queue* t, struct timeout_evt* evt, size
|
||||
|
||||
first = t->events[pos];
|
||||
|
||||
if (!first)
|
||||
if (first)
|
||||
{
|
||||
first->prev->next = evt;
|
||||
evt->prev = first->prev;
|
||||
first->prev = evt;
|
||||
}
|
||||
else
|
||||
{
|
||||
t->events[pos] = evt;
|
||||
evt->prev = evt;
|
||||
}
|
||||
else
|
||||
{
|
||||
evt->prev = first->prev;
|
||||
first->prev = evt;
|
||||
}
|
||||
evt->next = 0;
|
||||
}
|
||||
|
||||
void timeout_queue_remove(struct timeout_queue* t, struct timeout_evt* evt)
|
||||
@@ -99,23 +112,32 @@ void timeout_queue_remove(struct timeout_queue* t, struct timeout_evt* evt)
|
||||
size_t pos = (evt->timestamp % t->max);
|
||||
struct timeout_evt* first = t->events[pos];
|
||||
|
||||
if (!first)
|
||||
if (!first || !evt->prev)
|
||||
return;
|
||||
|
||||
if (first == evt)
|
||||
{
|
||||
if (first->next)
|
||||
first->next->prev = first->prev;
|
||||
t->events[pos] = first->next;
|
||||
if (first->prev != first)
|
||||
{
|
||||
t->events[pos] = first->next;
|
||||
t->events[pos]->prev = evt->prev;
|
||||
}
|
||||
else
|
||||
{
|
||||
t->events[pos] = 0;
|
||||
}
|
||||
}
|
||||
else if (evt == first->prev)
|
||||
{
|
||||
first->prev = evt->prev;
|
||||
evt->prev->next = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
evt->prev->next = evt->next;
|
||||
if (evt->next)
|
||||
evt->next->prev = evt->prev;
|
||||
else
|
||||
first->prev = evt->prev;
|
||||
evt->next->prev = evt->prev;
|
||||
}
|
||||
timeout_evt_reset(evt);
|
||||
}
|
||||
|
||||
void timeout_queue_reschedule(struct timeout_queue* t, struct timeout_evt* evt, size_t seconds)
|
||||
@@ -123,4 +145,5 @@ void timeout_queue_reschedule(struct timeout_queue* t, struct timeout_evt* evt,
|
||||
if (timeout_evt_is_scheduled(evt))
|
||||
timeout_queue_remove(t, evt);
|
||||
timeout_queue_insert(t, evt, seconds);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -53,4 +53,6 @@ void timeout_queue_insert(struct timeout_queue*, struct timeout_evt*, size_t sec
|
||||
void timeout_queue_remove(struct timeout_queue*, struct timeout_evt*);
|
||||
void timeout_queue_reschedule(struct timeout_queue*, struct timeout_evt*, size_t seconds);
|
||||
|
||||
size_t timeout_queue_get_next_timeout(struct timeout_queue*, time_t now);
|
||||
|
||||
#endif /* HAVE_UHUB_TIMEOUT_HANDLER_H */
|
||||
|
||||
16
tools/create_certificate.sh
Executable file
16
tools/create_certificate.sh
Executable file
@@ -0,0 +1,16 @@
|
||||
#!/bin/sh
|
||||
|
||||
OPENSSL=/usr/bin/openssl
|
||||
NAME=certificate
|
||||
|
||||
if [ ! -x ${OPENSSL} ]; then
|
||||
echo "Cannot locate the openssl utility: ${OPENSSL}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
${OPENSSL} genrsa -out ${NAME}.key 1024 &&
|
||||
${OPENSSL} req -new -x509 -nodes -sha1 -days 365 -key ${NAME}.key > ${NAME}.crt &&
|
||||
cat ${NAME}.key ${NAME}.crt > ${NAME}.pem && rm -f ${NAME}.key ${NAME}.crt
|
||||
|
||||
echo "Created certificate ${NAME}.pem"
|
||||
|
||||
Reference in New Issue
Block a user