2009-08-02 19:37:55 +00:00
|
|
|
/*
|
|
|
|
* uhub - A tiny ADC p2p connection hub
|
|
|
|
* Copyright (C) 2007-2009, Jan Vidar Krey
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "uhub.h"
|
|
|
|
|
|
|
|
extern struct hub_info* g_hub;
|
|
|
|
|
2009-08-03 00:50:51 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void net_con_event(int fd, short ev, void *arg)
|
|
|
|
{
|
|
|
|
struct net_connection* con = (struct net_connection*) arg;
|
2009-08-03 19:57:18 +00:00
|
|
|
int events = 0;
|
|
|
|
|
|
|
|
if (ev & EV_READ) events |= NET_EVENT_READ;
|
|
|
|
if (ev & EV_WRITE) events |= NET_EVENT_WRITE;
|
|
|
|
if (ev == EV_TIMEOUT) events = NET_EVENT_TIMEOUT;
|
|
|
|
|
2009-08-03 00:50:51 +00:00
|
|
|
#ifdef SSL_SUPPORT
|
|
|
|
if (!con->ssl)
|
|
|
|
{
|
|
|
|
#endif
|
2009-08-03 19:57:18 +00:00
|
|
|
con->callback(con, events, con->ptr);
|
2009-08-03 00:50:51 +00:00
|
|
|
#ifdef SSL_SUPPORT
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-08-03 19:57:18 +00:00
|
|
|
LOG_DUMP("net_con_event: events=%d, con=%p", ev, con);
|
2009-08-03 00:50:51 +00:00
|
|
|
if (ev & (EV_READ | EV_WRITE))
|
|
|
|
{
|
|
|
|
if (net_con_flag_get(con, NET_WANT_SSL_ACCEPT))
|
|
|
|
{
|
2009-08-03 14:20:32 +00:00
|
|
|
if (net_con_ssl_accept(con) < 0)
|
2009-08-03 19:57:18 +00:00
|
|
|
net_event(con, NET_EVENT_SOCKERROR, con->ptr);
|
2009-08-03 00:50:51 +00:00
|
|
|
}
|
|
|
|
else if (net_con_flag_get(con, NET_WANT_SSL_CONNECT))
|
|
|
|
{
|
2009-08-03 14:20:32 +00:00
|
|
|
if (net_con_ssl_connect(con) < 0)
|
2009-08-03 19:57:18 +00:00
|
|
|
con->callback(con, NET_EVENT_SOCKERROR, con->ptr);
|
2009-08-03 00:50:51 +00:00
|
|
|
}
|
|
|
|
else if (ev == EV_READ && net_con_flag_get(con, NET_WANT_SSL_READ))
|
|
|
|
{
|
2009-08-03 19:57:18 +00:00
|
|
|
con->callback(con, NET_EVENT_WRITE, con->ptr);
|
2009-08-03 00:50:51 +00:00
|
|
|
}
|
|
|
|
else if (ev == EV_WRITE && net_con_flag_get(con, NET_WANT_SSL_WRITE))
|
|
|
|
{
|
2009-08-03 19:57:18 +00:00
|
|
|
con->callback(con, events & NET_EVENT_READ, con->ptr);
|
2009-08-03 00:50:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-08-03 19:57:18 +00:00
|
|
|
con->callback(con, events, con->ptr);
|
2009-08-03 00:50:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-08-03 19:57:18 +00:00
|
|
|
con->callback(con, events, con->ptr);
|
2009-08-03 00:50:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-08-03 19:57:18 +00:00
|
|
|
void net_con_initialize(struct net_connection* con, int sd, struct ip_addr_encap* addr, net_connection_cb callback, const void* ptr, int events)
|
2009-08-02 19:37:55 +00:00
|
|
|
{
|
2009-08-03 17:55:57 +00:00
|
|
|
LOG_DUMP("net_con_initialize: sd=%d, ptr=%p", sd, ptr);
|
2009-08-02 19:37:55 +00:00
|
|
|
con->sd = sd;
|
|
|
|
con->ptr = (void*) ptr;
|
2009-08-03 19:57:18 +00:00
|
|
|
con->callback = callback;
|
2009-08-03 00:50:51 +00:00
|
|
|
con->last_send = time(0);
|
|
|
|
con->last_recv = con->last_send;
|
|
|
|
|
2009-08-03 16:14:34 +00:00
|
|
|
/** IP address of peer */
|
|
|
|
if (addr)
|
|
|
|
{
|
|
|
|
memcpy(&con->ipaddr, addr, sizeof(struct ip_addr_encap));
|
|
|
|
}
|
|
|
|
|
2009-08-03 00:50:51 +00:00
|
|
|
if (events & EV_READ) net_con_flag_set(con, NET_WANT_READ);
|
|
|
|
if (events & EV_WRITE) net_con_flag_set(con, NET_WANT_WRITE);
|
|
|
|
|
|
|
|
event_set(&con->event, con->sd, events | EV_PERSIST, net_con_event, con);
|
2009-08-02 19:37:55 +00:00
|
|
|
event_add(&con->event, 0);
|
2009-08-03 14:20:32 +00:00
|
|
|
|
|
|
|
net_set_nonblocking(sd, 1);
|
|
|
|
net_set_nosigpipe(sd, 1);
|
|
|
|
|
|
|
|
#ifdef SSL_SUPPORT
|
|
|
|
con->ssl = NULL;
|
|
|
|
con->write_len = 0;
|
|
|
|
#endif
|
2009-08-02 19:37:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void net_con_update(struct net_connection* con, int events)
|
|
|
|
{
|
|
|
|
if (event_pending(&con->event, EV_READ | EV_WRITE, 0) == events)
|
|
|
|
return;
|
|
|
|
|
2009-08-03 00:50:51 +00:00
|
|
|
if (events & EV_READ) net_con_flag_set(con, NET_WANT_READ);
|
|
|
|
if (events & EV_WRITE) net_con_flag_set(con, NET_WANT_WRITE);
|
|
|
|
|
2009-08-02 19:37:55 +00:00
|
|
|
event_del(&con->event);
|
2009-08-03 00:50:51 +00:00
|
|
|
event_set(&con->event, con->sd, events | EV_PERSIST, net_con_event, con);
|
2009-08-02 19:37:55 +00:00
|
|
|
event_add(&con->event, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void net_con_close(struct net_connection* con)
|
|
|
|
{
|
2009-08-03 21:59:02 +00:00
|
|
|
if (event_pending(&con->event, EV_READ | EV_WRITE, 0))
|
|
|
|
{
|
|
|
|
event_del(&con->event);
|
|
|
|
}
|
|
|
|
|
|
|
|
net_con_clear_timeout(con);
|
2009-08-02 19:46:57 +00:00
|
|
|
net_close(con->sd);
|
|
|
|
con->sd = -1;
|
2009-08-02 19:37:55 +00:00
|
|
|
}
|
|
|
|
|
2009-08-03 00:50:51 +00:00
|
|
|
#ifdef SSL_SUPPORT
|
|
|
|
static int handle_openssl_error(struct net_connection* con, int ret)
|
|
|
|
{
|
|
|
|
int error = SSL_get_error(con->ssl, ret);
|
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
2009-08-03 17:55:57 +00:00
|
|
|
LOG_DUMP("SSL_get_error: ret=%d, error=%d: SSL_ERROR_ZERO_RETURN", ret, error);
|
2009-08-03 16:20:08 +00:00
|
|
|
return -1;
|
2009-08-03 00:50:51 +00:00
|
|
|
|
|
|
|
case SSL_ERROR_WANT_READ:
|
2009-08-03 17:55:57 +00:00
|
|
|
LOG_DUMP("SSL_get_error: ret=%d, error=%d: SSL_ERROR_WANT_READ", ret, error);
|
2009-08-03 00:50:51 +00:00
|
|
|
net_con_update(con, EV_READ);
|
|
|
|
net_con_flag_set(con, NET_WANT_SSL_READ);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2009-08-03 17:55:57 +00:00
|
|
|
LOG_DUMP("SSL_get_error: ret=%d, error=%d: SSL_ERROR_WANT_WRITE", ret, error);
|
2009-08-03 00:50:51 +00:00
|
|
|
net_con_update(con, EV_READ | EV_WRITE);
|
|
|
|
net_con_flag_set(con, NET_WANT_SSL_WRITE);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_CONNECT:
|
2009-08-03 17:55:57 +00:00
|
|
|
LOG_DUMP("SSL_get_error: ret=%d, error=%d: SSL_ERROR_WANT_CONNECT", ret, error);
|
2009-08-03 00:50:51 +00:00
|
|
|
net_con_update(con, EV_READ | EV_WRITE);
|
|
|
|
net_con_flag_set(con, NET_WANT_SSL_CONNECT);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SSL_ERROR_WANT_ACCEPT:
|
2009-08-03 17:55:57 +00:00
|
|
|
LOG_DUMP("SSL_get_error: ret=%d, error=%d: SSL_ERROR_WANT_ACCEPT", ret, error);
|
2009-08-03 00:50:51 +00:00
|
|
|
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:
|
2009-08-03 17:55:57 +00:00
|
|
|
LOG_DUMP("SSL_get_error: ret=%d, error=%d: SSL_ERROR_WANT_X509_LOOKUP", ret, error);
|
2009-08-03 00:50:51 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SSL_ERROR_SYSCALL:
|
2009-08-03 17:55:57 +00:00
|
|
|
LOG_DUMP("SSL_get_error: ret=%d, error=%d: SSL_ERROR_SYSCALL", ret, error);
|
2009-08-03 00:50:51 +00:00
|
|
|
/* if ret == 0, connection closed, if ret == -1, check with errno */
|
2009-08-03 14:20:32 +00:00
|
|
|
if (ret == 0)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return -net_error();
|
2009-08-03 00:50:51 +00:00
|
|
|
|
|
|
|
case SSL_ERROR_SSL:
|
2009-08-03 17:55:57 +00:00
|
|
|
LOG_DUMP("SSL_get_error: ret=%d, error=%d: SSL_ERROR_SSL", ret, error);
|
2009-08-03 00:50:51 +00:00
|
|
|
/* internal openssl error */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
ssize_t net_con_send(struct net_connection* con, const void* buf, size_t len)
|
|
|
|
{
|
|
|
|
#ifdef SSL_SUPPORT
|
|
|
|
if (!con->ssl)
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
int ret = net_send(con->sd, buf, len, UHUB_SEND_SIGNAL);
|
2009-08-03 17:55:57 +00:00
|
|
|
LOG_DUMP("net_send: ret=%d", ret);
|
2009-08-03 00:50:51 +00:00
|
|
|
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
|
|
|
|
{
|
2009-08-03 17:54:28 +00:00
|
|
|
if (net_con_flag_get(con, NET_WANT_SSL_READ) && con->write_len)
|
|
|
|
len = con->write_len;
|
|
|
|
|
2009-08-03 00:50:51 +00:00
|
|
|
int ret = SSL_write(con->ssl, buf, len);
|
2009-08-03 17:55:57 +00:00
|
|
|
LOG_DUMP("net_send: ret=%d", ret);
|
2009-08-03 00:50:51 +00:00
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
con->last_send = time(0);
|
|
|
|
net_con_flag_unset(con, NET_WANT_SSL_READ);
|
2009-08-03 17:54:28 +00:00
|
|
|
con->write_len = 0;
|
2009-08-03 00:50:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-08-03 17:54:28 +00:00
|
|
|
con->write_len = len;
|
2009-08-03 00:50:51 +00:00
|
|
|
return handle_openssl_error(con, ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t net_con_recv(struct net_connection* con, void* buf, size_t len)
|
|
|
|
{
|
|
|
|
#ifdef SSL_SUPPORT
|
|
|
|
if (!con->ssl)
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
int ret = net_recv(con->sd, buf, len, 0);
|
2009-08-03 17:55:57 +00:00
|
|
|
LOG_DUMP("net_send: ret=%d", ret);
|
2009-08-03 00:50:51 +00:00
|
|
|
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);
|
2009-08-03 17:55:57 +00:00
|
|
|
LOG_DUMP("net_send: ret=%d", ret);
|
2009-08-03 00:50:51 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2009-08-03 19:57:18 +00:00
|
|
|
void net_con_set_timeout(struct net_connection* con, int seconds)
|
|
|
|
{
|
|
|
|
struct timeval timeout = { seconds, 0 };
|
|
|
|
evtimer_set(&con->timeout, net_con_event, con);
|
|
|
|
evtimer_add(&con->timeout, &timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void net_con_clear_timeout(struct net_connection* con)
|
|
|
|
{
|
|
|
|
if (evtimer_pending(&con->timeout, 0))
|
|
|
|
{
|
|
|
|
evtimer_del(&con->timeout);
|
|
|
|
memset(&con->timeout, 0, sizeof(&con->timeout));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-03 00:50:51 +00:00
|
|
|
#ifdef SSL_SUPPORT
|
2009-08-03 12:21:53 +00:00
|
|
|
ssize_t net_con_ssl_accept(struct net_connection* con)
|
2009-08-03 00:50:51 +00:00
|
|
|
{
|
2009-08-03 14:20:32 +00:00
|
|
|
net_con_flag_set(con, NET_WANT_SSL_ACCEPT);
|
2009-08-03 00:50:51 +00:00
|
|
|
ssize_t ret = SSL_accept(con->ssl);
|
2009-08-03 17:55:57 +00:00
|
|
|
LOG_DUMP("SSL_accept() ret=%d", ret);
|
2009-08-03 00:50:51 +00:00
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
net_con_flag_unset(con, NET_WANT_SSL_ACCEPT);
|
2009-08-03 14:20:32 +00:00
|
|
|
net_con_flag_unset(con, NET_WANT_SSL_READ);
|
2009-08-03 00:50:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return handle_openssl_error(con, ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-08-03 12:21:53 +00:00
|
|
|
ssize_t net_con_ssl_connect(struct net_connection* con)
|
2009-08-03 00:50:51 +00:00
|
|
|
{
|
2009-08-03 14:20:32 +00:00
|
|
|
net_con_flag_set(con, NET_WANT_SSL_CONNECT);
|
2009-08-03 00:50:51 +00:00
|
|
|
ssize_t ret = SSL_connect(con->ssl);
|
2009-08-03 17:55:57 +00:00
|
|
|
LOG_DUMP("SSL_connect() ret=%d", ret);
|
2009-08-03 00:50:51 +00:00
|
|
|
if (ret > 0)
|
|
|
|
{
|
|
|
|
net_con_flag_unset(con, NET_WANT_SSL_CONNECT);
|
2009-08-03 17:55:57 +00:00
|
|
|
net_con_flag_unset(con, NET_WANT_SSL_WRITE);
|
2009-08-03 00:50:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return handle_openssl_error(con, ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2009-08-03 18:21:59 +00:00
|
|
|
|
|
|
|
ssize_t net_con_ssl_handshake(struct net_connection* con, int ssl_mode)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2009-08-03 12:21:53 +00:00
|
|
|
#endif /* SSL_SUPPORT */
|
|
|
|
|
2009-08-03 00:50:51 +00:00
|
|
|
|