Various fixes for epoll handling without libevent.

This should take care of most compile issues as well as busy loops caused by unhandled recv() calls returning 0 and general cleanups.
This commit is contained in:
Jan Vidar Krey 2010-01-19 14:53:47 +01:00
parent af8421fa47
commit f84073f7cc
22 changed files with 315 additions and 400 deletions

View File

@ -1,6 +1,6 @@
## ##
## Makefile for uhub (Use GNU make) ## Makefile for uhub (Use GNU make)
## Copyright (C) 2007-2009, Jan Vidar Krey <janvidar@extatic.org> ## Copyright (C) 2007-2010, Jan Vidar Krey <janvidar@extatic.org>
# #
CC = gcc CC = gcc
@ -144,10 +144,11 @@ libuhub_SOURCES := \
src/core/route.c \ src/core/route.c \
src/core/user.c \ src/core/user.c \
src/core/usermanager.c \ src/core/usermanager.c \
src/util/ipcalc.c \ src/network/connection.c \
src/network/epoll.c \ src/network/epoll.c \
src/network/libevent.c \ src/network/libevent.c \
src/network/network.c \ src/network/network.c \
src/util/ipcalc.c \
src/util/list.c \ src/util/list.c \
src/util/log.c \ src/util/log.c \
src/util/memory.c \ src/util/memory.c \

View File

@ -42,9 +42,8 @@ static void inf_create_user()
{ {
if (inf_user) return; if (inf_user) return;
inf_user = (struct hub_user*) hub_malloc_zero(sizeof(struct hub_user)); inf_user = (struct hub_user*) hub_malloc_zero(sizeof(struct hub_user));
inf_user->connection = (struct net_connection*) hub_malloc_zero(sizeof(struct net_connection)); inf_user->connection = net_con_create();
inf_user->id.sid = 1; inf_user->id.sid = 1;
inf_user->connection->sd = -1;
inf_user->limits.upload_slots = 1; inf_user->limits.upload_slots = 1;
} }

View File

@ -4,7 +4,7 @@
static struct hub_info um_hub; static struct hub_info um_hub;
static struct hub_user um_user[MAX_USERS]; static struct hub_user um_user[MAX_USERS];
static struct net_connection um_cons[MAX_USERS]; // static struct net_connection um_cons[MAX_USERS];
EXO_TEST(um_test_setup, { EXO_TEST(um_test_setup, {
int i = 0; int i = 0;
@ -12,12 +12,12 @@ EXO_TEST(um_test_setup, {
for (i = 0; i < MAX_USERS; i++) for (i = 0; i < MAX_USERS; i++)
{ {
memset(&um_cons[i], 0, sizeof(struct net_connection)); /* memset(&um_cons[i], 0, sizeof(struct net_connection));
um_cons[i].sd = -1; um_cons[i].sd = -1;
memset(&um_user[i], 0, sizeof(struct hub_user)); memset(&um_user[i], 0, sizeof(struct hub_user));
um_user[i].id.sid = i+1; um_user[i].id.sid = i+1;
um_user[i].connection = &um_cons[i]; um_user[i].connection = &um_cons[i];*/
} }
return 1; return 1;
}); });

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * 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 * 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 * it under the terms of the GNU General Public License as published by

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * 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 * 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 * it under the terms of the GNU General Public License as published by

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * 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 * 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 * it under the terms of the GNU General Public License as published by
@ -20,11 +20,6 @@
#include "uhub.h" #include "uhub.h"
#include "hubio.h" #include "hubio.h"
// #define SEND_CHUNKS 1
/* FIXME: This should not be needed! */
extern struct hub_info* g_hub;
#ifdef DEBUG_SENDQ #ifdef DEBUG_SENDQ
static void debug_msg(const char* prefix, struct adc_message* msg) static void debug_msg(const char* prefix, struct adc_message* msg)
{ {

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * 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 * 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 * it under the terms of the GNU General Public License as published by

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * 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 * 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 * it under the terms of the GNU General Public License as published by
@ -53,22 +53,12 @@ void debug_sendq_recv(struct hub_user* user, int received, int max, const char*
} }
#endif #endif
int net_user_recv(void* ptr, void* buf, size_t len)
{
struct hub_user* user = (struct hub_user*) ptr;
int ret = net_con_recv(user->connection, buf, len);
#ifdef DEBUG_SENDQ
debug_sendq_recv(user, ret, len, buf);
#endif
return ret;
}
int handle_net_read(struct hub_user* user) int handle_net_read(struct hub_user* user)
{ {
static char buf[MAX_RECV_BUF]; static char buf[MAX_RECV_BUF];
struct hub_recvq* q = user->recv_queue; struct hub_recvq* q = user->recv_queue;
size_t buf_size = hub_recvq_get(q, buf, MAX_RECV_BUF); size_t buf_size = hub_recvq_get(q, buf, MAX_RECV_BUF);
ssize_t size = net_user_recv(user, &buf[buf_size], MAX_RECV_BUF - buf_size); ssize_t size = net_con_recv(user->connection, buf, MAX_RECV_BUF);
if (size > 0) if (size > 0)
buf_size += size; buf_size += size;

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * 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 * 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 * it under the terms of the GNU General Public License as published by

View File

@ -70,7 +70,7 @@ static void probe_net_event(struct net_connection* con, int events, void *arg)
{ {
probe->connection = 0; probe->connection = 0;
} }
net_con_ssl_handshake(con, NET_CON_SSL_MODE_SERVER); net_con_ssl_handshake(con, net_con_ssl_mode_server, probe->hub->ssl_ctx);
} }
else else
{ {
@ -83,7 +83,7 @@ static void probe_net_event(struct net_connection* con, int events, void *arg)
{ {
LOG_TRACE("Probed TLS %d.%d connection", (int) probe_recvbuf[1], (int) probe_recvbuf[2]); LOG_TRACE("Probed TLS %d.%d connection", (int) probe_recvbuf[1], (int) probe_recvbuf[2]);
net_con_ssl_handshake(con, NET_CON_SSL_MODE_SERVER); net_con_ssl_handshake(con, net_con_ssl_mode_server, probe->hub->ssl_ctx);
return; return;
} }
#endif #endif

39
src/network/backend.h Normal file
View File

@ -0,0 +1,39 @@
/*
* 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/>.
*
*/
#ifndef HAVE_UHUB_NETWORK_BACKEND_H
#define HAVE_UHUB_NETWORK_BACKEND_H
/**
* Initialize the network backend.
* Returns 1 on success, or 0 on failure.
*/
extern int net_backend_initialize();
/**
* Shutdown the network connection backend.
*/
extern void net_backend_shutdown();
/**
* Process the network backend.
*/
extern int net_backend_process();
#endif /* HAVE_UHUB_NETWORK_BACKEND_H */

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * 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 * 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 * it under the terms of the GNU General Public License as published by
@ -18,6 +18,193 @@
*/ */
#include "uhub.h" #include "uhub.h"
#include "network/common.h"
#ifdef SSL_SUPPORT
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);
switch (error)
{
case SSL_ERROR_ZERO_RETURN:
LOG_PROTO("SSL_get_error: ret=%d, error=%d: SSL_ERROR_ZERO_RETURN", ret, 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);
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);
return 0;
case SSL_ERROR_SYSCALL:
LOG_PROTO("SSL_get_error: ret=%d, error=%d: SSL_ERROR_SYSCALL", ret, error);
/* if ret == 0, connection closed, if ret == -1, check with errno */
if (ret == 0)
return -1;
else
return -net_error();
case SSL_ERROR_SSL:
LOG_PROTO("SSL_get_error: ret=%d, error=%d: SSL_ERROR_SSL", ret, error);
/* internal openssl error */
return -1;
}
return -1;
}
ssize_t net_con_ssl_accept(struct net_connection* con)
{
uhub_assert(con);
ssize_t ret = SSL_accept(net_con_get_ssl(con));
#ifdef NETWORK_DUMP_DEBUG
LOG_PROTO("SSL_accept() ret=%d", ret);
#endif
if (ret > 0)
{
net_con_update(con, NET_EVENT_READ);
}
else
{
return handle_openssl_error(con, ret);
}
return ret;
}
ssize_t net_con_ssl_connect(struct net_connection* con)
{
uhub_assert(con);
ssize_t ret = SSL_connect(net_con_get_ssl(con));
#ifdef NETWORK_DUMP_DEBUG
LOG_PROTO("SSL_connect() ret=%d", ret);
#endif
if (ret > 0)
{
net_con_update(con, NET_EVENT_READ);
}
else
{
return handle_openssl_error(con, ret);
}
return ret;
}
ssize_t net_con_ssl_handshake(struct net_connection* con, enum net_con_ssl_mode ssl_mode, SSL_CTX* ssl_ctx)
{
uhub_assert(con);
SSL* ssl = 0;
if (ssl_mode == net_con_ssl_mode_server)
{
ssl = SSL_new(ssl_ctx);
SSL_set_fd(ssl, net_con_get_sd(con));
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));
net_con_set_ssl(con, ssl);
return net_con_ssl_connect(con);
}
}
#endif /* SSL_SUPPORT */
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)
{
if (net_error() == EWOULDBLOCK || net_error() == EINTR)
return 0;
return -1;
}
return ret;
}
ssize_t net_con_recv(struct net_connection* con, void* buf, size_t len)
{
uhub_assert(con);
#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
if (ret == -1)
{
if (net_error() == EWOULDBLOCK || net_error() == EINTR)
return 0;
return -net_error();
}
else if (ret == 0)
{
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 (ret > 0)
{
net_con_update(con, NET_EVENT_READ);
}
else
{
return -handle_openssl_error(con, ret);
}
return ret;
}
#endif
}
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);
if (ret == -1)
{
if (net_error() == EWOULDBLOCK || net_error() == EINTR)
return 0;
return -net_error();
}
else if (ret == 0)
return -1;
return ret;
}

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * 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 * 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 * it under the terms of the GNU General Public License as published by
@ -21,6 +21,7 @@
#define HAVE_UHUB_NETWORK_CONNECTION_H #define HAVE_UHUB_NETWORK_CONNECTION_H
#include "uhub.h" #include "uhub.h"
#include "network/backend.h"
#define NET_EVENT_TIMEOUT 0x0001 #define NET_EVENT_TIMEOUT 0x0001
#define NET_EVENT_READ 0x0002 #define NET_EVENT_READ 0x0002
@ -29,32 +30,7 @@
#define NET_EVENT_CLOSED 0x2000 /* Socket closed */ #define NET_EVENT_CLOSED 0x2000 /* Socket closed */
struct net_connection; struct net_connection;
struct net_timer;
struct net_backend;
/**
* Initialize the network backend.
* Returns 1 on success, or 0 on failure.
*/
extern int net_backend_initialize();
/**
* Shutdown the network connection backend.
*/
extern void net_backend_shutdown();
/**
* Process the network backend.
*/
extern int net_backend_process();
typedef void (*net_connection_cb)(struct net_connection*, int event, void* ptr); typedef void (*net_connection_cb)(struct net_connection*, int event, void* ptr);
typedef void (*net_timeout_cb)(struct net_timer*, void* ptr);
extern void net_timer_initialize(struct net_timer* timer, net_timeout_cb callback, void* ptr);
extern void net_timer_reset(struct net_timer* timer, int seconds);
extern void net_timer_shutdown(struct net_timer* timer);
extern int net_con_get_sd(struct net_connection* con); extern int net_con_get_sd(struct net_connection* con);
extern void* net_con_get_ptr(struct net_connection* con); extern void* net_con_get_ptr(struct net_connection* con);
@ -123,8 +99,11 @@ enum net_con_ssl_mode
net_con_ssl_mode_client, net_con_ssl_mode_client,
}; };
extern ssize_t net_con_ssl_handshake(struct net_connection* con, enum net_con_ssl_mode, void* ssl_ctx); extern ssize_t net_con_ssl_handshake(struct net_connection* con, enum net_con_ssl_mode, SSL_CTX* ssl_ctx);
extern int net_con_is_ssl(struct net_connection* con);
extern SSL* net_con_get_ssl(struct net_connection* con);
extern void net_con_set_ssl(struct net_connection* con, SSL*);
#endif /* SSL_SUPPORT */ #endif /* SSL_SUPPORT */
#endif /* HAVE_UHUB_NETWORK_CONNECTION_H */ #endif /* HAVE_UHUB_NETWORK_CONNECTION_H */

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * 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 * 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 * it under the terms of the GNU General Public License as published by
@ -23,6 +23,7 @@
#include "network/connection.h" #include "network/connection.h"
#include "network/common.h" #include "network/common.h"
#include "network/backend.h"
#define EPOLL_EVBUFFER 512 #define EPOLL_EVBUFFER 512
@ -34,6 +35,10 @@ struct net_connection
void* ptr; void* ptr;
struct epoll_event ev; struct epoll_event ev;
struct timeout_evt* timeout; 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
}; };
struct net_backend struct net_backend
@ -49,7 +54,6 @@ struct net_backend
static struct net_backend* g_backend = 0; 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* con)
{ {
char buf[512]; char buf[512];
@ -130,6 +134,7 @@ int net_backend_process()
struct net_connection* net_con_create() 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));
con->sd = -1;
return con; return con;
} }
@ -177,6 +182,28 @@ void net_con_update(struct net_connection* con, int events)
if (events & NET_EVENT_READ) con->ev.events |= EPOLLIN; if (events & NET_EVENT_READ) con->ev.events |= EPOLLIN;
if (events & NET_EVENT_WRITE) con->ev.events |= EPOLLOUT; if (events & NET_EVENT_WRITE) con->ev.events |= 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 (epoll_ctl(g_backend->epfd, EPOLL_CTL_MOD, con->sd, &con->ev) == -1) if (epoll_ctl(g_backend->epfd, EPOLL_CTL_MOD, con->sd, &con->ev) == -1)
{ {
LOG_TRACE("epoll_ctl() modify failed."); LOG_TRACE("epoll_ctl() modify failed.");
@ -212,6 +239,23 @@ int net_con_close(struct net_connection* con)
return 0; 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) int net_con_get_sd(struct net_connection* con)
{ {
@ -223,71 +267,6 @@ void* net_con_get_ptr(struct net_connection* con)
return con->ptr; return con->ptr;
} }
ssize_t net_con_send(struct net_connection* con, const void* buf, size_t len)
{
int ret = net_send(con->sd, buf, len, UHUB_SEND_SIGNAL);
if (ret == -1)
{
if (net_error() == EWOULDBLOCK || net_error() == EINTR)
return 0;
return -1;
}
return ret;
}
ssize_t net_con_recv(struct net_connection* con, void* buf, size_t len)
{
uhub_assert(con);
#ifdef SSL_SUPPORT
if (!con->ssl)
{
#endif
int ret = net_recv(con->sd, buf, len, 0);
#ifdef NETWORK_DUMP_DEBUG
LOG_PROTO("net_recv: ret=%d", ret);
#endif
if (ret == -1)
{
if (net_error() == EWOULDBLOCK || net_error() == EINTR)
return 0;
return -1;
}
return ret;
#ifdef SSL_SUPPORT
}
else
{
int ret = SSL_read(con->ssl, buf, len);
#ifdef NETWORK_DUMP_DEBUG
LOG_PROTO("net_recv: ret=%d", ret);
#endif
if (ret > 0)
{
con->last_recv = time(0);
net_con_flag_unset(con, NET_WANT_SSL_WRITE);
}
else
{
return handle_openssl_error(con, ret);
}
return ret;
}
#endif
}
ssize_t net_con_peek(struct net_connection* con, void* buf, size_t len)
{
int ret = net_recv(con->sd, buf, len, MSG_PEEK);
if (ret == -1)
{
if (net_error() == EWOULDBLOCK || net_error() == EINTR)
return 0;
return -1;
}
return ret;
}
void timeout_evt_initialize(struct timeout_evt*, timeout_evt_cb, void* ptr); void timeout_evt_initialize(struct timeout_evt*, timeout_evt_cb, void* ptr);
void timeout_evt_reset(struct timeout_evt*); void timeout_evt_reset(struct timeout_evt*);
@ -324,5 +303,4 @@ void net_con_clear_timeout(struct net_connection* con)
} }
} }
#endif /* USE_EPOLL */ #endif /* USE_EPOLL */

View File

@ -330,198 +330,6 @@ void net_timer_shutdown(struct net_timer* timer)
timer->ptr = 0; timer->ptr = 0;
} }
#ifdef SSL_SUPPORT
static int handle_openssl_error(struct net_connection* con, int ret)
{
uhub_assert(con);
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);
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, EV_READ);
net_con_flag_set(con, NET_WANT_SSL_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, EV_READ | EV_WRITE);
net_con_flag_set(con, 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, EV_READ | EV_WRITE);
net_con_flag_set(con, 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, EV_READ | EV_WRITE);
net_con_flag_set(con, 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);
return 0;
case SSL_ERROR_SYSCALL:
LOG_PROTO("SSL_get_error: ret=%d, error=%d: SSL_ERROR_SYSCALL", ret, error);
/* if ret == 0, connection closed, if ret == -1, check with errno */
if (ret == 0)
return -1;
else
return -net_error();
case SSL_ERROR_SSL:
LOG_PROTO("SSL_get_error: ret=%d, error=%d: SSL_ERROR_SSL", ret, error);
/* internal openssl error */
return -1;
}
return -1;
}
#endif
ssize_t net_con_send(struct net_connection* con, const void* buf, size_t len)
{
uhub_assert(con);
#ifdef SSL_SUPPORT
if (!con->ssl)
{
#endif
int ret = net_send(con->sd, buf, len, UHUB_SEND_SIGNAL);
#ifdef NETWORK_DUMP_DEBUG
LOG_PROTO("net_send: ret=%d", ret);
#endif
if (ret > 0)
{
con->last_send = time(0);
}
else if (ret == -1 && (net_error() == EWOULDBLOCK || net_error() == EINTR))
{
return 0;
}
else
{
return -1;
}
return ret;
#ifdef SSL_SUPPORT
}
else
{
if (net_con_flag_get(con, NET_WANT_SSL_READ) && con->write_len)
len = con->write_len;
int ret = SSL_write(con->ssl, buf, len);
#ifdef NETWORK_DUMP_DEBUG
LOG_PROTO("net_send: ret=%d", ret);
#endif
if (ret > 0)
{
con->last_send = time(0);
net_con_flag_unset(con, NET_WANT_SSL_READ);
con->write_len = 0;
}
else
{
con->write_len = len;
return handle_openssl_error(con, ret);
}
return ret;
}
#endif
}
ssize_t net_con_recv(struct net_connection* con, void* buf, size_t len)
{
uhub_assert(con);
#ifdef SSL_SUPPORT
if (!con->ssl)
{
#endif
int ret = net_recv(con->sd, buf, len, 0);
#ifdef NETWORK_DUMP_DEBUG
LOG_PROTO("net_recv: ret=%d", ret);
#endif
if (ret > 0)
{
con->last_recv = time(0);
}
else if (ret == -1 && (net_error() == EWOULDBLOCK || net_error() == EINTR))
{
return 0;
}
else
{
return -1;
}
return ret;
#ifdef SSL_SUPPORT
}
else
{
int ret = SSL_read(con->ssl, buf, len);
#ifdef NETWORK_DUMP_DEBUG
LOG_PROTO("net_recv: ret=%d", ret);
#endif
if (ret > 0)
{
con->last_recv = time(0);
net_con_flag_unset(con, NET_WANT_SSL_WRITE);
}
else
{
return handle_openssl_error(con, ret);
}
return ret;
}
#endif
}
ssize_t net_con_peek(struct net_connection* con, void* buf, size_t len)
{
uhub_assert(con);
#ifdef SSL_SUPPORT
if (!con->ssl)
{
#endif
int ret = net_recv(con->sd, buf, len, MSG_PEEK);
#ifdef NETWORK_DUMP_DEBUG
LOG_PROTO("net_recv: ret=%d (MSG_PEEK)", ret);
#endif
if (ret > 0)
{
con->last_recv = time(0);
}
else if (ret == -1 && (net_error() == EWOULDBLOCK || net_error() == EINTR))
{
return 0;
}
else
{
return -1;
}
return ret;
#ifdef SSL_SUPPORT
}
else
{
// FIXME: Not able to do this!
return 0;
}
#endif
}
void net_con_set_timeout(struct net_connection* con, int seconds) void net_con_set_timeout(struct net_connection* con, int seconds)
{ {
@ -546,69 +354,6 @@ void net_con_clear_timeout(struct net_connection* con)
} }
} }
#ifdef SSL_SUPPORT
ssize_t net_con_ssl_accept(struct net_connection* con)
{
uhub_assert(con);
net_con_flag_set(con, NET_WANT_SSL_ACCEPT);
ssize_t ret = SSL_accept(con->ssl);
#ifdef NETWORK_DUMP_DEBUG
LOG_PROTO("SSL_accept() ret=%d", ret);
#endif
if (ret > 0)
{
net_con_flag_unset(con, NET_WANT_SSL_ACCEPT);
net_con_flag_unset(con, NET_WANT_SSL_READ);
}
else
{
return handle_openssl_error(con, ret);
}
return ret;
}
ssize_t net_con_ssl_connect(struct net_connection* con)
{
uhub_assert(con);
net_con_flag_set(con, NET_WANT_SSL_CONNECT);
ssize_t ret = SSL_connect(con->ssl);
#ifdef NETWORK_DUMP_DEBUG
LOG_PROTO("SSL_connect() ret=%d", ret);
#endif
if (ret > 0)
{
net_con_flag_unset(con, NET_WANT_SSL_CONNECT);
net_con_flag_unset(con, NET_WANT_SSL_WRITE);
}
else
{
return handle_openssl_error(con, ret);
}
return ret;
}
ssize_t net_con_ssl_handshake(struct net_connection* con, int ssl_mode)
{
uhub_assert(con);
if (ssl_mode == net_con_ssl_mode_server)
{
con->ssl = SSL_new(g_hub->ssl_ctx);
SSL_set_fd(con->ssl, con->sd);
return net_con_ssl_accept(con);
}
else
{
con->ssl = SSL_new(SSL_CTX_new(TLSv1_method()));
SSL_set_fd(con->ssl, con->sd);
return net_con_ssl_connect(con);
}
}
#endif /* SSL_SUPPORT */
static struct event_base g_evbase = 0; static struct event_base g_evbase = 0;
/** /**

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * 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 * 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 * it under the terms of the GNU General Public License as published by

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * 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 * 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 * it under the terms of the GNU General Public License as published by

View File

@ -71,15 +71,9 @@ static void adc_cid_pid(struct ADC_client* client)
adc_msg_add_named_argument(client->info, ADC_INF_FLAG_CLIENT_ID, cid); adc_msg_add_named_argument(client->info, ADC_INF_FLAG_CLIENT_ID, cid);
} }
static void timer_callback(struct net_timer* t, void* arg)
{
}
static void event_callback(struct net_connection* con, int events, void *arg) static void event_callback(struct net_connection* con, int events, void *arg)
{ {
struct ADC_client* client = (struct ADC_client*) con->ptr; struct ADC_client* client = (struct ADC_client*) net_con_get_ptr(con);
if (events == NET_EVENT_SOCKERROR || events == NET_EVENT_CLOSED) if (events == NET_EVENT_SOCKERROR || events == NET_EVENT_CLOSED)
{ {
@ -347,10 +341,16 @@ int ADC_client_create(struct ADC_client* client, const char* nickname, const cha
int sd = net_socket_create(PF_INET, SOCK_STREAM, IPPROTO_TCP); int sd = net_socket_create(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sd == -1) return -1; if (sd == -1) return -1;
client->con = hub_malloc(sizeof(struct net_connection)); client->con = net_con_create();
client->timer = hub_malloc(sizeof(struct net_timer)); #if 0
/* FIXME */
client->timer = 0; /* FIXME: hub_malloc(sizeof(struct net_timer)); */
#endif
net_con_initialize(client->con, sd, event_callback, client, 0); net_con_initialize(client->con, sd, event_callback, client, 0);
#if 0
/* FIXME */
net_timer_initialize(client->timer, timer_callback, client); net_timer_initialize(client->timer, timer_callback, client);
#endif
ADC_client_set_state(client, ps_none); ADC_client_set_state(client, ps_none);
client->nick = hub_strdup(nickname); client->nick = hub_strdup(nickname);
@ -362,7 +362,10 @@ int ADC_client_create(struct ADC_client* client, const char* nickname, const cha
void ADC_client_destroy(struct ADC_client* client) void ADC_client_destroy(struct ADC_client* client)
{ {
ADC_client_disconnect(client); ADC_client_disconnect(client);
#if 0
/* FIXME */
net_timer_shutdown(client->timer); net_timer_shutdown(client->timer);
#endif
hub_free(client->timer); hub_free(client->timer);
adc_msg_free(client->info); adc_msg_free(client->info);
hub_free(client->nick); hub_free(client->nick);
@ -380,7 +383,7 @@ int ADC_client_connect(struct ADC_client* client, const char* address)
client->callback(client, ADC_CLIENT_CONNECTING, 0); client->callback(client, ADC_CLIENT_CONNECTING, 0);
} }
int ret = net_connect(client->con->sd, (struct sockaddr*) &client->addr, sizeof(struct sockaddr_in)); int ret = net_connect(net_con_get_sd(client->con), (struct sockaddr*) &client->addr, sizeof(struct sockaddr_in));
if (ret == 0 || (ret == -1 && net_error() == EISCONN)) if (ret == 0 || (ret == -1 && net_error() == EISCONN))
{ {
ADC_client_on_connected(client); ADC_client_on_connected(client);
@ -424,7 +427,7 @@ static void ADC_client_on_login(struct ADC_client* client)
void ADC_client_disconnect(struct ADC_client* client) void ADC_client_disconnect(struct ADC_client* client)
{ {
if (client->con && client->con->sd != -1) if (client->con && net_con_get_sd(client->con) != -1)
{ {
net_con_close(client->con); net_con_close(client->con);
client->con = 0; client->con = 0;

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * 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 * 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 * it under the terms of the GNU General Public License as published by

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * 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 * 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 * it under the terms of the GNU General Public License as published by
@ -41,7 +41,6 @@
#define TIMEOUT_CONNECTED 15 #define TIMEOUT_CONNECTED 15
#define TIMEOUT_HANDSHAKE 30 #define TIMEOUT_HANDSHAKE 30
#define TIMEOUT_SENDQ 120 #define TIMEOUT_SENDQ 120
#define TIMEOUT_IDLE 7200
#define TIMEOUT_STATS 60 #define TIMEOUT_STATS 60
#define MAX_CID_LEN 39 #define MAX_CID_LEN 39

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * 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 * 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 * it under the terms of the GNU General Public License as published by

View File

@ -1,6 +1,6 @@
/* /*
* uhub - A tiny ADC p2p connection hub * 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 * 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 * it under the terms of the GNU General Public License as published by