2004-04-25 08:42:57 -07:00
|
|
|
/*
|
2004-10-19 09:50:27 -07:00
|
|
|
* vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
|
2004-04-25 08:42:57 -07:00
|
|
|
*/
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
2006-03-13 00:50:26 -08:00
|
|
|
#include "config.h"
|
|
|
|
|
2004-03-31 08:47:45 -08:00
|
|
|
#if USE_OPENSSL
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
2006-01-06 09:58:12 -08:00
|
|
|
#include "Ecore.h"
|
|
|
|
#include "ecore_private.h"
|
|
|
|
#include "ecore_con_private.h"
|
|
|
|
#include "Ecore_Con.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#elif WIN32
|
|
|
|
#include <winsock.h>
|
|
|
|
#endif
|
|
|
|
|
2005-08-30 02:32:09 -07:00
|
|
|
static void _ecore_con_cb_dns_lookup(void *data, struct hostent *he);
|
2003-09-23 01:09:32 -07:00
|
|
|
static void _ecore_con_server_free(Ecore_Con_Server *svr);
|
|
|
|
static void _ecore_con_client_free(Ecore_Con_Client *cl);
|
|
|
|
static int _ecore_con_svr_handler(void *data, Ecore_Fd_Handler *fd_handler);
|
|
|
|
static int _ecore_con_cl_handler(void *data, Ecore_Fd_Handler *fd_handler);
|
|
|
|
static int _ecore_con_svr_cl_handler(void *data, Ecore_Fd_Handler *fd_handler);
|
|
|
|
static void _ecore_con_server_flush(Ecore_Con_Server *svr);
|
|
|
|
static void _ecore_con_client_flush(Ecore_Con_Client *cl);
|
2006-03-19 21:53:12 -08:00
|
|
|
static void _ecore_con_event_client_add_free(void *data, void *ev);
|
|
|
|
static void _ecore_con_event_client_del_free(void *data, void *ev);
|
2003-09-23 01:09:32 -07:00
|
|
|
static void _ecore_con_event_client_data_free(void *data, void *ev);
|
2006-03-19 21:53:12 -08:00
|
|
|
static void _ecore_con_event_server_add_free(void *data, void *ev);
|
|
|
|
static void _ecore_con_event_server_del_free(void *data, void *ev);
|
2003-09-23 01:09:32 -07:00
|
|
|
static void _ecore_con_event_server_data_free(void *data, void *ev);
|
|
|
|
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI int ECORE_CON_EVENT_CLIENT_ADD = 0;
|
|
|
|
EAPI int ECORE_CON_EVENT_CLIENT_DEL = 0;
|
|
|
|
EAPI int ECORE_CON_EVENT_SERVER_ADD = 0;
|
|
|
|
EAPI int ECORE_CON_EVENT_SERVER_DEL = 0;
|
|
|
|
EAPI int ECORE_CON_EVENT_CLIENT_DATA = 0;
|
|
|
|
EAPI int ECORE_CON_EVENT_SERVER_DATA = 0;
|
2003-09-23 01:09:32 -07:00
|
|
|
|
2005-04-28 21:51:31 -07:00
|
|
|
static Ecore_List *servers = NULL;
|
2003-09-23 01:09:32 -07:00
|
|
|
static int init_count = 0;
|
2005-11-26 23:18:36 -08:00
|
|
|
static int ssl_init_count = 0;
|
2003-09-23 01:09:32 -07:00
|
|
|
|
|
|
|
#define LENGTH_OF_SOCKADDR_UN(s) (strlen((s)->sun_path) + (size_t)(((struct sockaddr_un *)NULL)->sun_path))
|
|
|
|
|
2004-09-22 01:17:15 -07:00
|
|
|
/**
|
|
|
|
* @defgroup Ecore_Con_Lib_Group Ecore Connection Library Functions
|
|
|
|
*
|
|
|
|
* Utility functions that set up and shut down the Ecore Connection
|
|
|
|
* library.
|
|
|
|
*/
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
/**
|
2004-07-15 06:43:15 -07:00
|
|
|
* Initialises the Ecore_Con library.
|
2004-09-22 01:17:15 -07:00
|
|
|
* @return Number of times the library has been initialised without being
|
|
|
|
* shut down.
|
|
|
|
* @ingroup Ecore_Con_Lib_Group
|
2003-09-23 01:09:32 -07:00
|
|
|
*/
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI int
|
2003-09-23 01:09:32 -07:00
|
|
|
ecore_con_init(void)
|
|
|
|
{
|
2005-09-07 02:06:06 -07:00
|
|
|
if (++init_count != 1) return init_count;
|
|
|
|
|
2006-01-19 01:11:30 -08:00
|
|
|
ecore_init();
|
2005-09-07 02:06:06 -07:00
|
|
|
ECORE_CON_EVENT_CLIENT_ADD = ecore_event_type_new();
|
|
|
|
ECORE_CON_EVENT_CLIENT_DEL = ecore_event_type_new();
|
|
|
|
ECORE_CON_EVENT_SERVER_ADD = ecore_event_type_new();
|
|
|
|
ECORE_CON_EVENT_SERVER_DEL = ecore_event_type_new();
|
|
|
|
ECORE_CON_EVENT_CLIENT_DATA = ecore_event_type_new();
|
|
|
|
ECORE_CON_EVENT_SERVER_DATA = ecore_event_type_new();
|
2004-03-31 08:47:45 -08:00
|
|
|
|
2005-09-07 02:06:06 -07:00
|
|
|
/* TODO Remember return value, if it fails, use gethostbyname() */
|
|
|
|
ecore_con_dns_init();
|
|
|
|
|
|
|
|
servers = ecore_list_new();
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
return init_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2004-07-15 06:43:15 -07:00
|
|
|
* Shuts down the Ecore_Con library.
|
2004-09-22 01:17:15 -07:00
|
|
|
* @return Number of times the library has been initialised without being
|
|
|
|
* shut down.
|
|
|
|
* @ingroup Ecore_Con_Lib_Group
|
2003-09-23 01:09:32 -07:00
|
|
|
*/
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI int
|
2003-09-23 01:09:32 -07:00
|
|
|
ecore_con_shutdown(void)
|
|
|
|
{
|
2005-09-07 02:06:06 -07:00
|
|
|
if (--init_count != 0) return init_count;
|
|
|
|
|
|
|
|
while (!ecore_list_is_empty(servers))
|
|
|
|
_ecore_con_server_free(ecore_list_remove_first(servers));
|
|
|
|
ecore_list_destroy(servers);
|
|
|
|
servers = NULL;
|
|
|
|
|
|
|
|
ecore_con_dns_shutdown();
|
|
|
|
|
2006-01-19 01:11:30 -08:00
|
|
|
ecore_shutdown();
|
|
|
|
|
2005-09-07 02:06:06 -07:00
|
|
|
return init_count;
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
|
|
|
|
2004-09-22 01:17:15 -07:00
|
|
|
/**
|
|
|
|
* @defgroup Ecore_Con_Server_Group Ecore Connection Server Functions
|
|
|
|
*
|
|
|
|
* Functions that operate on Ecore server objects.
|
|
|
|
*/
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
/**
|
2004-07-15 06:43:15 -07:00
|
|
|
* Creates a server to listen for connections.
|
2003-09-23 01:09:32 -07:00
|
|
|
*
|
2004-07-15 06:43:15 -07:00
|
|
|
* The socket on which the server listens depends on the connection
|
|
|
|
* type:
|
|
|
|
* @li If @a compl_type is @c ECORE_CON_LOCAL_USER, the server will listen on
|
|
|
|
* the Unix socket "~/.ecore/[name]/[port]".
|
|
|
|
* @li If @a compl_type is @c ECORE_CON_LOCAL_SYSTEM, the server will listen
|
|
|
|
* on Unix socket "/tmp/.ecore_service|[name]|[port]".
|
|
|
|
* @li If @a compl_type is @c ECORE_CON_REMOTE_SYSTEM, the server will listen
|
|
|
|
* on TCP port @c port.
|
|
|
|
*
|
|
|
|
* @param compl_type The connection type.
|
|
|
|
* @param name Name to associate with the socket. It is used when
|
|
|
|
* generating the socket name of a Unix socket. Though
|
|
|
|
* it is not used for the TCP socket, it still needs to
|
|
|
|
* be a valid character array. @c NULL will not be
|
|
|
|
* accepted.
|
|
|
|
* @param port Number to identify socket. When a Unix socket is used,
|
|
|
|
* it becomes part of the socket name. When a TCP socket
|
|
|
|
* is used, it is used as the TCP port.
|
|
|
|
* @param data Data to associate with the created Ecore_Con_Server
|
|
|
|
* object.
|
|
|
|
* @return A new Ecore_Con_Server.
|
2004-09-22 01:17:15 -07:00
|
|
|
* @ingroup Ecore_Con_Server_Group
|
2003-09-23 01:09:32 -07:00
|
|
|
*/
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI Ecore_Con_Server *
|
2004-04-14 01:51:19 -07:00
|
|
|
ecore_con_server_add(Ecore_Con_Type compl_type,
|
2005-03-04 11:29:39 -08:00
|
|
|
const char *name,
|
2003-09-23 01:09:32 -07:00
|
|
|
int port,
|
|
|
|
const void *data)
|
|
|
|
{
|
|
|
|
Ecore_Con_Server *svr;
|
2004-04-14 01:51:19 -07:00
|
|
|
Ecore_Con_Type type;
|
2003-09-23 01:09:32 -07:00
|
|
|
struct sockaddr_in socket_addr;
|
|
|
|
struct sockaddr_un socket_unix;
|
|
|
|
struct linger lin;
|
|
|
|
char buf[4096];
|
|
|
|
|
|
|
|
if (port < 0) return NULL;
|
|
|
|
/* local user socket: FILE: ~/.ecore/[name]/[port] */
|
|
|
|
/* local system socket: FILE: /tmp/.ecore_service|[name]|[port] */
|
|
|
|
/* remote system socket: TCP/IP: [name]:[port] */
|
|
|
|
svr = calloc(1, sizeof(Ecore_Con_Server));
|
|
|
|
if (!svr) return NULL;
|
|
|
|
|
2004-04-14 01:51:19 -07:00
|
|
|
type = compl_type;
|
|
|
|
#if USE_OPENSSL
|
|
|
|
/* unset the SSL flag for the following checks */
|
|
|
|
type &= ~ECORE_CON_USE_SSL;
|
|
|
|
#endif
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
if ((type == ECORE_CON_LOCAL_USER) ||
|
|
|
|
(type == ECORE_CON_LOCAL_SYSTEM))
|
|
|
|
{
|
2005-03-01 23:06:44 -08:00
|
|
|
const char *homedir;
|
2003-09-23 01:09:32 -07:00
|
|
|
struct stat st;
|
|
|
|
mode_t pmode, mask;
|
|
|
|
|
|
|
|
if (!name) goto error;
|
|
|
|
mask =
|
|
|
|
S_IRGRP | S_IWGRP | S_IXGRP |
|
|
|
|
S_IROTH | S_IWOTH | S_IXOTH;
|
|
|
|
if (type == ECORE_CON_LOCAL_USER)
|
|
|
|
{
|
|
|
|
homedir = getenv("HOME");
|
|
|
|
if (!homedir) homedir = getenv("TMP");
|
|
|
|
if (!homedir) homedir = "/tmp";
|
|
|
|
mask = S_IRUSR | S_IWUSR | S_IXUSR;
|
|
|
|
snprintf(buf, sizeof(buf), "%s/.ecore", homedir);
|
|
|
|
if (stat(buf, &st) < 0) mkdir(buf, mask);
|
|
|
|
snprintf(buf, sizeof(buf), "%s/.ecore/%s", homedir, name);
|
|
|
|
if (stat(buf, &st) < 0) mkdir(buf, mask);
|
|
|
|
snprintf(buf, sizeof(buf), "%s/.ecore/%s/%i", homedir, name, port);
|
|
|
|
mask =
|
|
|
|
S_IRGRP | S_IWGRP | S_IXGRP |
|
|
|
|
S_IROTH | S_IWOTH | S_IXOTH;
|
|
|
|
}
|
|
|
|
else if (type == ECORE_CON_LOCAL_SYSTEM)
|
|
|
|
{
|
|
|
|
mask = 0;
|
2004-03-27 20:25:52 -08:00
|
|
|
if (name[0] == '/')
|
|
|
|
snprintf(buf, sizeof(buf), "%s|%i", name, port);
|
|
|
|
else
|
|
|
|
snprintf(buf, sizeof(buf), "/tmp/.ecore_service|%s|%i", name, port);
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
|
|
|
pmode = umask(mask);
|
2003-11-08 02:45:46 -08:00
|
|
|
start:
|
2003-09-23 01:09:32 -07:00
|
|
|
svr->fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (svr->fd < 0)
|
|
|
|
{
|
|
|
|
umask(pmode);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
|
|
|
|
{
|
|
|
|
umask(pmode);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
|
|
|
|
{
|
|
|
|
umask(pmode);
|
|
|
|
goto error;
|
|
|
|
}
|
2006-05-23 17:33:20 -07:00
|
|
|
lin.l_onoff = 1;
|
2006-04-19 00:27:52 -07:00
|
|
|
lin.l_linger = 0;
|
2003-09-23 01:09:32 -07:00
|
|
|
if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, &lin, sizeof(struct linger)) < 0)
|
|
|
|
{
|
|
|
|
umask(pmode);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
socket_unix.sun_family = AF_UNIX;
|
|
|
|
strncpy(socket_unix.sun_path, buf, sizeof(socket_unix.sun_path));
|
|
|
|
if (bind(svr->fd, (struct sockaddr *)&socket_unix, LENGTH_OF_SOCKADDR_UN(&socket_unix)) < 0)
|
|
|
|
{
|
2003-11-08 02:45:46 -08:00
|
|
|
if (connect(svr->fd, (struct sockaddr *)&socket_unix,
|
|
|
|
LENGTH_OF_SOCKADDR_UN(&socket_unix)) < 0)
|
|
|
|
{
|
|
|
|
if ((type == ECORE_CON_LOCAL_USER) ||
|
|
|
|
(type == ECORE_CON_LOCAL_SYSTEM))
|
|
|
|
{
|
|
|
|
if (unlink(buf) < 0)
|
|
|
|
{
|
|
|
|
umask(pmode);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
umask(pmode);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
umask(pmode);
|
|
|
|
goto error;
|
|
|
|
}
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
|
|
|
if (listen(svr->fd, 4096) < 0)
|
|
|
|
{
|
|
|
|
umask(pmode);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
svr->path = strdup(buf);
|
|
|
|
if (!svr->path)
|
|
|
|
{
|
|
|
|
umask(pmode);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
svr->fd_handler = ecore_main_fd_handler_add(svr->fd,
|
|
|
|
ECORE_FD_READ,
|
|
|
|
_ecore_con_svr_handler, svr,
|
|
|
|
NULL, NULL);
|
|
|
|
umask(pmode);
|
|
|
|
if (!svr->fd_handler) goto error;
|
|
|
|
}
|
|
|
|
else if (type == ECORE_CON_REMOTE_SYSTEM)
|
|
|
|
{
|
|
|
|
svr->fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (svr->fd < 0) goto error;
|
|
|
|
if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
|
|
|
|
if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
|
|
|
|
lin.l_onoff = 1;
|
|
|
|
lin.l_linger = 100;
|
|
|
|
if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, &lin, sizeof(struct linger)) < 0) goto error;
|
|
|
|
socket_addr.sin_family = AF_INET;
|
|
|
|
socket_addr.sin_port = htons(port);
|
|
|
|
socket_addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
if (bind(svr->fd, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in)) < 0) goto error;
|
|
|
|
if (listen(svr->fd, 4096) < 0) goto error;
|
|
|
|
svr->fd_handler = ecore_main_fd_handler_add(svr->fd,
|
|
|
|
ECORE_FD_READ,
|
|
|
|
_ecore_con_svr_handler, svr,
|
|
|
|
NULL, NULL);
|
|
|
|
if (!svr->fd_handler) goto error;
|
|
|
|
}
|
2004-04-14 01:51:19 -07:00
|
|
|
|
|
|
|
#if USE_OPENSSL
|
|
|
|
if (compl_type & ECORE_CON_USE_SSL)
|
|
|
|
{
|
2005-11-26 23:18:36 -08:00
|
|
|
if (!ssl_init_count)
|
|
|
|
{
|
|
|
|
SSL_library_init();
|
|
|
|
SSL_load_error_strings();
|
|
|
|
}
|
|
|
|
ssl_init_count++;
|
|
|
|
|
2004-04-14 01:51:19 -07:00
|
|
|
/* SSLv3 gives *weird* results on my box, don't use it yet */
|
|
|
|
if (!(svr->ssl_ctx = SSL_CTX_new(SSLv2_client_method())))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!(svr->ssl = SSL_new(svr->ssl_ctx)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
SSL_set_fd(svr->ssl, svr->fd);
|
|
|
|
}
|
|
|
|
#endif
|
2003-09-23 01:09:32 -07:00
|
|
|
|
|
|
|
svr->name = strdup(name);
|
|
|
|
if (!svr->name) goto error;
|
|
|
|
svr->type = type;
|
|
|
|
svr->port = port;
|
|
|
|
svr->data = (void *)data;
|
|
|
|
svr->created = 1;
|
2005-04-28 21:51:31 -07:00
|
|
|
svr->reject_excess_clients = 0;
|
|
|
|
svr->client_limit = -1;
|
|
|
|
svr->clients = ecore_list_new();
|
2006-01-10 21:13:23 -08:00
|
|
|
svr->ppid = getpid();
|
2005-04-28 21:51:31 -07:00
|
|
|
ecore_list_append(servers, svr);
|
2003-09-23 01:09:32 -07:00
|
|
|
ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
|
|
|
|
return svr;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (svr->name) free(svr->name);
|
|
|
|
if (svr->path) free(svr->path);
|
|
|
|
if (svr->fd >= 0) close(svr->fd);
|
|
|
|
if (svr->fd_handler) ecore_main_fd_handler_del(svr->fd_handler);
|
2004-04-03 07:03:33 -08:00
|
|
|
if (svr->write_buf) free(svr->write_buf);
|
2006-03-19 23:45:58 -08:00
|
|
|
if (svr->ip) free(svr->ip);
|
2004-04-14 01:51:19 -07:00
|
|
|
#if USE_OPENSSL
|
|
|
|
if (svr->ssl) SSL_free(svr->ssl);
|
|
|
|
if (svr->ssl_ctx) SSL_CTX_free(svr->ssl_ctx);
|
|
|
|
#endif
|
2003-09-23 01:09:32 -07:00
|
|
|
free(svr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2004-07-15 06:43:15 -07:00
|
|
|
* Creates a server object to represent the server listening at the
|
|
|
|
* given port.
|
|
|
|
*
|
|
|
|
* The socket to which the server connects depends on the connection type:
|
|
|
|
* @li If @a compl_type is @c ECORE_CON_LOCAL_USER, the function will
|
|
|
|
* connect to the server listening on the Unix socket
|
|
|
|
* "~/.ecore/[name]/[port]".
|
|
|
|
* @li If @a compl_type is @c ECORE_CON_LOCAL_SYSTEM, the function will
|
|
|
|
* connect to the server listening on the Unix socket
|
|
|
|
* "/tmp/.ecore_service|[name]|[port]".
|
|
|
|
* @li If @a compl_type is @c ECORE_CON_REMOTE_SYSTEM, the function will
|
|
|
|
* connect to the server listening on the TCP port "[name]:[port]".
|
2003-09-23 01:09:32 -07:00
|
|
|
*
|
2004-07-15 06:43:15 -07:00
|
|
|
* @param compl_type The connection type.
|
|
|
|
* @param name Name used when determining what socket to connect to.
|
|
|
|
* It is used to generate the socket name when the socket
|
|
|
|
* is a Unix socket. It is used as the hostname when
|
|
|
|
* connecting with a TCP socket.
|
2004-07-22 06:07:25 -07:00
|
|
|
* @param port Number to identify the socket to connect to. Used when
|
2004-07-15 06:43:15 -07:00
|
|
|
* generating the socket name for a Unix socket, or as the
|
|
|
|
* TCP port when connecting to a TCP socket.
|
|
|
|
* @param data Data to associate with the created Ecore_Con_Server
|
|
|
|
* object.
|
|
|
|
* @return A new Ecore_Con_Server.
|
2004-09-22 01:17:15 -07:00
|
|
|
* @ingroup Ecore_Con_Server_Group
|
2003-09-23 01:09:32 -07:00
|
|
|
*/
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI Ecore_Con_Server *
|
2004-03-31 08:47:45 -08:00
|
|
|
ecore_con_server_connect(Ecore_Con_Type compl_type,
|
2005-03-04 11:29:39 -08:00
|
|
|
const char *name,
|
2003-09-23 01:09:32 -07:00
|
|
|
int port,
|
|
|
|
const void *data)
|
|
|
|
{
|
|
|
|
Ecore_Con_Server *svr;
|
2004-04-14 01:51:19 -07:00
|
|
|
Ecore_Con_Type type;
|
2003-09-23 01:09:32 -07:00
|
|
|
struct sockaddr_un socket_unix;
|
2004-01-18 11:13:16 -08:00
|
|
|
int curstate = 0;
|
2003-09-23 01:09:32 -07:00
|
|
|
char buf[4096];
|
|
|
|
|
|
|
|
if (!name) return NULL;
|
|
|
|
if (port < 0) return NULL;
|
|
|
|
/* local user socket: FILE: ~/.ecore/[name]/[port] */
|
|
|
|
/* local system socket: FILE: /tmp/.ecore_service|[name]|[port] */
|
|
|
|
/* remote system socket: TCP/IP: [name]:[port] */
|
|
|
|
svr = calloc(1, sizeof(Ecore_Con_Server));
|
|
|
|
if (!svr) return NULL;
|
2004-04-14 01:51:19 -07:00
|
|
|
|
|
|
|
type = compl_type;
|
2004-04-01 08:37:47 -08:00
|
|
|
#if USE_OPENSSL
|
|
|
|
/* unset the SSL flag for the following checks */
|
|
|
|
type &= ~ECORE_CON_USE_SSL;
|
|
|
|
#endif
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
if ((type == ECORE_CON_LOCAL_USER) ||
|
|
|
|
(type == ECORE_CON_LOCAL_SYSTEM))
|
|
|
|
{
|
2005-03-01 23:06:44 -08:00
|
|
|
const char *homedir;
|
2003-09-23 01:09:32 -07:00
|
|
|
|
|
|
|
if (type == ECORE_CON_LOCAL_USER)
|
|
|
|
{
|
|
|
|
homedir = getenv("HOME");
|
|
|
|
if (!homedir) homedir = getenv("TMP");
|
|
|
|
if (!homedir) homedir = "/tmp";
|
|
|
|
snprintf(buf, sizeof(buf), "%s/.ecore/%s/%i", homedir, name, port);
|
|
|
|
}
|
|
|
|
else if (type == ECORE_CON_LOCAL_SYSTEM)
|
2004-04-14 01:51:19 -07:00
|
|
|
{
|
|
|
|
if (name[0] == '/')
|
|
|
|
snprintf(buf, sizeof(buf), "%s|%i", name, port);
|
|
|
|
else
|
2004-03-27 20:25:52 -08:00
|
|
|
snprintf(buf, sizeof(buf), "/tmp/.ecore_service|%s|%i", name, port);
|
2004-04-14 01:51:19 -07:00
|
|
|
}
|
2003-09-23 01:09:32 -07:00
|
|
|
svr->fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (svr->fd < 0) goto error;
|
|
|
|
if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
|
|
|
|
if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
|
|
|
|
if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, &curstate, sizeof(curstate)) < 0) goto error;
|
|
|
|
socket_unix.sun_family = AF_UNIX;
|
|
|
|
strncpy(socket_unix.sun_path, buf, sizeof(socket_unix.sun_path));
|
|
|
|
if (connect(svr->fd, (struct sockaddr *)&socket_unix, LENGTH_OF_SOCKADDR_UN(&socket_unix)) < 0) goto error;
|
|
|
|
svr->path = strdup(buf);
|
|
|
|
if (!svr->path) goto error;
|
|
|
|
svr->fd_handler = ecore_main_fd_handler_add(svr->fd,
|
|
|
|
ECORE_FD_READ,
|
|
|
|
_ecore_con_cl_handler, svr,
|
|
|
|
NULL, NULL);
|
|
|
|
if (!svr->fd_handler) goto error;
|
|
|
|
{
|
|
|
|
/* we got our server! */
|
|
|
|
Ecore_Con_Event_Server_Add *e;
|
|
|
|
|
|
|
|
e = calloc(1, sizeof(Ecore_Con_Event_Server_Add));
|
|
|
|
if (e)
|
|
|
|
{
|
2006-03-19 21:53:12 -08:00
|
|
|
svr->event_count++;
|
2003-09-23 01:09:32 -07:00
|
|
|
e->server = svr;
|
2006-03-19 21:53:12 -08:00
|
|
|
ecore_event_add(ECORE_CON_EVENT_SERVER_ADD, e,
|
|
|
|
_ecore_con_event_server_add_free, NULL);
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-03-31 08:47:45 -08:00
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
svr->name = strdup(name);
|
|
|
|
if (!svr->name) goto error;
|
2005-08-24 09:29:06 -07:00
|
|
|
svr->type = compl_type;
|
2003-09-23 01:09:32 -07:00
|
|
|
svr->port = port;
|
|
|
|
svr->data = (void *)data;
|
|
|
|
svr->created = 0;
|
2005-04-28 21:51:31 -07:00
|
|
|
svr->reject_excess_clients = 0;
|
|
|
|
svr->client_limit = -1;
|
|
|
|
svr->clients = ecore_list_new();
|
|
|
|
ecore_list_append(servers, svr);
|
2003-09-23 01:09:32 -07:00
|
|
|
ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
|
2005-09-02 20:07:29 -07:00
|
|
|
|
|
|
|
if (type == ECORE_CON_REMOTE_SYSTEM)
|
2006-01-19 01:11:30 -08:00
|
|
|
{
|
|
|
|
if (!ecore_con_dns_lookup(svr->name, _ecore_con_cb_dns_lookup, svr))
|
|
|
|
goto error;
|
|
|
|
}
|
2005-09-02 20:07:29 -07:00
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
return svr;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (svr->name) free(svr->name);
|
|
|
|
if (svr->path) free(svr->path);
|
|
|
|
if (svr->fd >= 0) close(svr->fd);
|
|
|
|
if (svr->fd_handler) ecore_main_fd_handler_del(svr->fd_handler);
|
2004-03-31 08:47:45 -08:00
|
|
|
#if USE_OPENSSL
|
|
|
|
if (svr->ssl) SSL_free(svr->ssl);
|
2004-04-02 10:32:55 -08:00
|
|
|
if (svr->ssl_ctx) SSL_CTX_free(svr->ssl_ctx);
|
2004-03-31 08:47:45 -08:00
|
|
|
#endif
|
2003-09-23 01:09:32 -07:00
|
|
|
free(svr);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2004-07-15 06:43:15 -07:00
|
|
|
* Closes the connection and frees the given server.
|
2004-09-22 01:17:15 -07:00
|
|
|
* @param svr The given server.
|
|
|
|
* @return Data associated with the server when it was created.
|
|
|
|
* @ingroup Ecore_Con_Server_Group
|
2003-09-23 01:09:32 -07:00
|
|
|
*/
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI void *
|
2003-09-23 01:09:32 -07:00
|
|
|
ecore_con_server_del(Ecore_Con_Server *svr)
|
|
|
|
{
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
|
|
|
|
{
|
|
|
|
ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
|
|
|
|
"ecore_con_server_del");
|
|
|
|
return NULL;
|
2006-03-19 21:53:12 -08:00
|
|
|
}
|
2003-09-23 01:09:32 -07:00
|
|
|
data = svr->data;
|
2006-03-19 21:59:21 -08:00
|
|
|
svr->data = NULL;
|
2006-03-19 21:53:12 -08:00
|
|
|
if (svr->event_count > 0)
|
|
|
|
svr->delete_me = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_ecore_con_server_free(svr);
|
|
|
|
if (ecore_list_goto(servers, svr)) ecore_list_remove(servers);
|
|
|
|
}
|
2003-09-23 01:09:32 -07:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2004-07-15 06:43:15 -07:00
|
|
|
* Retrieves the data associated with the given server.
|
2004-09-22 01:17:15 -07:00
|
|
|
* @param svr The given server.
|
|
|
|
* @return The associated data.
|
|
|
|
* @ingroup Ecore_Con_Server_Group
|
2003-09-23 01:09:32 -07:00
|
|
|
*/
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI void *
|
2003-09-23 01:09:32 -07:00
|
|
|
ecore_con_server_data_get(Ecore_Con_Server *svr)
|
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
|
|
|
|
{
|
|
|
|
ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
|
|
|
|
"ecore_con_server_data_get");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return svr->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2004-07-15 06:43:15 -07:00
|
|
|
* Retrieves whether the given server is currently connected.
|
|
|
|
* @todo Check that this function does what the documenter believes it does.
|
2004-09-22 01:17:15 -07:00
|
|
|
* @param svr The given server.
|
|
|
|
* @return @c 1 if the server is connected. @c 0 otherwise.
|
|
|
|
* @ingroup Ecore_Con_Server_Group
|
2003-09-23 01:09:32 -07:00
|
|
|
*/
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI int
|
2003-09-23 01:09:32 -07:00
|
|
|
ecore_con_server_connected_get(Ecore_Con_Server *svr)
|
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
|
|
|
|
{
|
|
|
|
ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
|
|
|
|
"ecore_con_server_connected_get");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (svr->connecting) return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-03-29 22:48:45 -08:00
|
|
|
/**
|
|
|
|
* Retrieves the current list of clients.
|
|
|
|
* @param svr The given server.
|
|
|
|
* @return The list of clients on this server.
|
|
|
|
* @ingroup Ecore_Con_Server_Group
|
|
|
|
*/
|
|
|
|
EAPI Ecore_List*
|
|
|
|
ecore_con_server_clients_get(Ecore_Con_Server *svr)
|
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
|
|
|
|
{
|
|
|
|
ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
|
|
|
|
"ecore_con_server_clients_get");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return svr->clients;
|
|
|
|
}
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
/**
|
2004-07-15 06:43:15 -07:00
|
|
|
* Sends the given data to the given server.
|
2004-09-22 01:17:15 -07:00
|
|
|
* @param svr The given server.
|
|
|
|
* @param data The given data.
|
|
|
|
* @param size Length of the data, in bytes, to send.
|
|
|
|
* @return The number of bytes sent. @c 0 will be returned if there is an
|
|
|
|
* error.
|
|
|
|
* @ingroup Ecore_Con_Server_Group
|
2003-09-23 01:09:32 -07:00
|
|
|
*/
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI int
|
2003-09-23 01:09:32 -07:00
|
|
|
ecore_con_server_send(Ecore_Con_Server *svr, void *data, int size)
|
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
|
|
|
|
{
|
|
|
|
ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
|
|
|
|
"ecore_con_server_send");
|
|
|
|
return 0;
|
|
|
|
}
|
2004-04-23 00:24:40 -07:00
|
|
|
if (svr->dead) return 0;
|
2003-09-23 01:09:32 -07:00
|
|
|
if (!data) return 0;
|
|
|
|
if (size < 1) return 0;
|
2005-09-22 22:46:56 -07:00
|
|
|
if (svr->fd_handler)
|
|
|
|
ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
|
2004-04-03 07:03:33 -08:00
|
|
|
if (svr->write_buf)
|
2003-09-23 01:09:32 -07:00
|
|
|
{
|
|
|
|
unsigned char *newbuf;
|
|
|
|
|
2004-04-03 07:03:33 -08:00
|
|
|
newbuf = realloc(svr->write_buf, svr->write_buf_size + size);
|
|
|
|
if (newbuf) svr->write_buf = newbuf;
|
2003-09-23 01:09:32 -07:00
|
|
|
else return 0;
|
2004-04-03 07:03:33 -08:00
|
|
|
memcpy(svr->write_buf + svr->write_buf_size, data, size);
|
|
|
|
svr->write_buf_size += size;
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-04-03 07:03:33 -08:00
|
|
|
svr->write_buf = malloc(size);
|
|
|
|
if (!svr->write_buf) return 0;
|
|
|
|
svr->write_buf_size = size;
|
|
|
|
memcpy(svr->write_buf, data, size);
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
2004-04-23 00:24:40 -07:00
|
|
|
return size;
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
2004-09-22 01:17:15 -07:00
|
|
|
|
2005-04-28 21:51:31 -07:00
|
|
|
/**
|
|
|
|
* Sets a limit on the number of clients that can be handled concurrently
|
|
|
|
* by the given server, and a policy on what to do if excess clients try to
|
|
|
|
* connect.
|
|
|
|
* Beware that if you set this once ecore is already running, you may
|
|
|
|
* already have pending CLIENT_ADD events in your event queue. Those
|
|
|
|
* clients have already connected and will not be affected by this call.
|
|
|
|
* Only clients subsequently trying to connect will be affected.
|
|
|
|
* @param svr The given server.
|
|
|
|
* @param client_limit The maximum number of clients to handle
|
|
|
|
* concurrently. -1 means unlimited (default). 0
|
|
|
|
* effectively disables the server.
|
|
|
|
* @param reject_excess_clients Set to 1 to automatically disconnect
|
|
|
|
* excess clients as soon as they connect if you are
|
|
|
|
* already handling client_limit clients. Set to 0
|
|
|
|
* (default) to just hold off on the "accept()"
|
|
|
|
* system call until the number of active clients
|
|
|
|
* drops. This causes the kernel to queue up to 4096
|
|
|
|
* connections (or your kernel's limit, whichever is
|
|
|
|
* lower).
|
|
|
|
* @ingroup Ecore_Con_Server_Group
|
|
|
|
*/
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI void
|
2005-04-28 21:51:31 -07:00
|
|
|
ecore_con_server_client_limit_set(Ecore_Con_Server *svr, int client_limit, char reject_excess_clients)
|
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
|
|
|
|
{
|
|
|
|
ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
|
|
|
|
"ecore_con_server_client_limit_set");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
svr->client_limit = client_limit;
|
|
|
|
svr->reject_excess_clients = reject_excess_clients;
|
|
|
|
}
|
|
|
|
|
2006-03-19 23:45:58 -08:00
|
|
|
/**
|
|
|
|
* Gets the IP address of a server that has been connected to.
|
|
|
|
*
|
|
|
|
* @param svr The given server.
|
|
|
|
* @return A pointer to an internal string that contains the IP address of
|
|
|
|
* the connected server in the form "XXX.YYY.ZZZ.AAA" IP notation.
|
|
|
|
* This string should not be modified or trusted to stay valid after
|
|
|
|
* deletion for the @p svr object. If no IP is known NULL is returned.
|
|
|
|
* @ingroup Ecore_Con_Server_Group
|
|
|
|
*/
|
|
|
|
EAPI char *
|
|
|
|
ecore_con_server_ip_get(Ecore_Con_Server *svr)
|
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
|
|
|
|
{
|
|
|
|
ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
|
|
|
|
"ecore_con_server_ip_get");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return svr->ip;
|
|
|
|
}
|
|
|
|
|
2004-09-22 01:17:15 -07:00
|
|
|
/**
|
|
|
|
* @defgroup Ecore_Con_Client_Group Ecore Connection Client Functions
|
|
|
|
*
|
|
|
|
* Functions that operate on Ecore connection client objects.
|
|
|
|
*/
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
/**
|
2004-07-15 06:43:15 -07:00
|
|
|
* Sends the given data to the given client.
|
2004-09-22 01:17:15 -07:00
|
|
|
* @param cl The given client.
|
|
|
|
* @param data The given data.
|
|
|
|
* @param size Length of the data, in bytes, to send.
|
|
|
|
* @return The number of bytes sent. @c 0 will be returned if there is an
|
|
|
|
* error.
|
|
|
|
* @ingroup Ecore_Con_Client_Group
|
2003-09-23 01:09:32 -07:00
|
|
|
*/
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI int
|
2003-09-23 01:09:32 -07:00
|
|
|
ecore_con_client_send(Ecore_Con_Client *cl, void *data, int size)
|
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
|
|
|
|
{
|
|
|
|
ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
|
|
|
|
"ecore_con_client_send");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (cl->dead) return 0;
|
|
|
|
if (!data) return 0;
|
|
|
|
if (size < 1) return 0;
|
2005-09-22 22:46:56 -07:00
|
|
|
if (cl->fd_handler)
|
|
|
|
ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
|
2003-09-23 01:09:32 -07:00
|
|
|
if (cl->buf)
|
|
|
|
{
|
|
|
|
unsigned char *newbuf;
|
|
|
|
|
|
|
|
newbuf = realloc(cl->buf, cl->buf_size + size);
|
|
|
|
if (newbuf) cl->buf = newbuf;
|
|
|
|
else return 0;
|
|
|
|
memcpy(cl->buf + cl->buf_size, data, size);
|
|
|
|
cl->buf_size += size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cl->buf = malloc(size);
|
|
|
|
if (!cl->buf) return 0;
|
|
|
|
cl->buf_size = size;
|
|
|
|
memcpy(cl->buf, data, size);
|
|
|
|
}
|
2004-04-23 00:24:40 -07:00
|
|
|
return size;
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2004-07-15 06:43:15 -07:00
|
|
|
* Retrieves the server representing the socket the client has
|
|
|
|
* connected to.
|
2004-09-22 01:17:15 -07:00
|
|
|
* @param cl The given client.
|
|
|
|
* @return The server that the client connected to.
|
|
|
|
* @ingroup Ecore_Con_Client_Group
|
2003-09-23 01:09:32 -07:00
|
|
|
*/
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI Ecore_Con_Server *
|
2003-09-23 01:09:32 -07:00
|
|
|
ecore_con_client_server_get(Ecore_Con_Client *cl)
|
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
|
|
|
|
{
|
|
|
|
ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
|
|
|
|
"ecore_con_client_server_get");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return cl->server;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2004-07-15 06:43:15 -07:00
|
|
|
* Closes the connection and frees memory allocated to the given client.
|
2004-09-22 01:17:15 -07:00
|
|
|
* @param cl The given client.
|
|
|
|
* @return Data associated with the client.
|
|
|
|
* @ingroup Ecore_Con_Client_Group
|
2003-09-23 01:09:32 -07:00
|
|
|
*/
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI void *
|
2003-09-23 01:09:32 -07:00
|
|
|
ecore_con_client_del(Ecore_Con_Client *cl)
|
|
|
|
{
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
|
|
|
|
{
|
|
|
|
ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
|
|
|
|
"ecore_con_client_del");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
data = cl->data;
|
2006-03-19 21:59:21 -08:00
|
|
|
cl->data = NULL;
|
2006-03-19 21:53:12 -08:00
|
|
|
if (cl->event_count > 0)
|
|
|
|
cl->delete_me = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ecore_list_goto(cl->server->clients, cl))
|
|
|
|
ecore_list_remove(cl->server->clients);
|
|
|
|
_ecore_con_client_free(cl);
|
|
|
|
}
|
2003-11-19 15:05:08 -08:00
|
|
|
return data;
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2004-07-15 06:43:15 -07:00
|
|
|
* Sets the data associated with the given client to @p data.
|
2004-09-22 01:17:15 -07:00
|
|
|
* @param cl The given client.
|
|
|
|
* @param data What to set the data to.
|
|
|
|
* @ingroup Ecore_Con_Client_Group
|
2003-09-23 01:09:32 -07:00
|
|
|
*/
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI void
|
2003-09-23 01:09:32 -07:00
|
|
|
ecore_con_client_data_set(Ecore_Con_Client *cl, const void *data)
|
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
|
|
|
|
{
|
|
|
|
ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
|
|
|
|
"ecore_con_client_data_set");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cl->data = (void *)data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2004-07-15 06:43:15 -07:00
|
|
|
* Retrieves the data associated with the given client.
|
2004-09-22 01:17:15 -07:00
|
|
|
* @param cl The given client.
|
|
|
|
* @return The data associated with @p cl.
|
|
|
|
* @ingroup Ecore_Con_Client_Group
|
2003-09-23 01:09:32 -07:00
|
|
|
*/
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI void *
|
2003-09-23 01:09:32 -07:00
|
|
|
ecore_con_client_data_get(Ecore_Con_Client *cl)
|
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
|
|
|
|
{
|
|
|
|
ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
|
|
|
|
"ecore_con_client_data_get");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return cl->data;
|
|
|
|
}
|
|
|
|
|
2006-03-19 23:45:58 -08:00
|
|
|
/**
|
|
|
|
* Gets the IP address of a cleint that has connected.
|
|
|
|
*
|
|
|
|
* @param cl The given client.
|
|
|
|
* @return A pointer to an internal string that contains the IP address of
|
|
|
|
* the connected client in the form "XXX.YYY.ZZZ.AAA" IP notation.
|
|
|
|
* This string should not be modified or trusted to stay valid after
|
|
|
|
* deletion for the @p cl object. If no IP is known NULL is returned.
|
|
|
|
* @ingroup Ecore_Con_Client_Group
|
|
|
|
*/
|
|
|
|
EAPI char *
|
|
|
|
ecore_con_client_ip_get(Ecore_Con_Client *cl)
|
|
|
|
{
|
|
|
|
if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
|
|
|
|
{
|
|
|
|
ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
|
|
|
|
"ecore_con_client_ip_get");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return cl->ip;
|
|
|
|
}
|
|
|
|
|
2004-10-21 20:41:49 -07:00
|
|
|
/**
|
|
|
|
* Returns if SSL support is available
|
|
|
|
* @return 1 if SSL is available, 0 if it is not.
|
|
|
|
* @ingroup Ecore_Con_Client_Group
|
|
|
|
*/
|
2006-01-06 09:58:12 -08:00
|
|
|
EAPI int
|
2004-10-21 20:41:49 -07:00
|
|
|
ecore_con_ssl_available_get(void)
|
|
|
|
{
|
|
|
|
#if USE_OPENSSL
|
|
|
|
return 1;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
static void
|
|
|
|
_ecore_con_server_free(Ecore_Con_Server *svr)
|
|
|
|
{
|
2006-05-18 20:52:24 -07:00
|
|
|
double t_start, t;
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
ECORE_MAGIC_SET(svr, ECORE_MAGIC_NONE);
|
2006-05-18 20:52:24 -07:00
|
|
|
t_start = ecore_time_get();
|
|
|
|
while ((svr->write_buf) && (!svr->dead))
|
|
|
|
{
|
|
|
|
_ecore_con_server_flush(svr);
|
|
|
|
t = ecore_time_get();
|
|
|
|
if ((t - t_start) > 5.0)
|
|
|
|
{
|
|
|
|
printf("ECORE_CON: EEK - stuck in _ecore_con_server_free() trying\n"
|
|
|
|
" to flush data out from the server, and have been for\n"
|
|
|
|
" %1.1f seconds. This is taking too long. Aborting flush.\n",
|
|
|
|
(t - t_start));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-04-03 07:03:33 -08:00
|
|
|
if (svr->write_buf) free(svr->write_buf);
|
2005-04-28 21:51:31 -07:00
|
|
|
while (!ecore_list_is_empty(svr->clients))
|
2006-05-18 20:52:24 -07:00
|
|
|
_ecore_con_client_free(ecore_list_remove_first(svr->clients));
|
2005-04-28 21:51:31 -07:00
|
|
|
ecore_list_destroy(svr->clients);
|
2006-01-10 21:13:23 -08:00
|
|
|
if ((svr->created) && (svr->path) && (svr->ppid == getpid())) unlink(svr->path);
|
2003-09-23 01:09:32 -07:00
|
|
|
if (svr->fd >= 0) close(svr->fd);
|
2004-03-31 08:47:45 -08:00
|
|
|
#if USE_OPENSSL
|
2004-04-14 01:51:19 -07:00
|
|
|
if (svr->ssl)
|
|
|
|
{
|
|
|
|
SSL_shutdown(svr->ssl);
|
|
|
|
SSL_free(svr->ssl);
|
|
|
|
}
|
2004-03-31 08:47:45 -08:00
|
|
|
if (svr->ssl_ctx) SSL_CTX_free(svr->ssl_ctx);
|
|
|
|
#endif
|
2003-09-23 01:09:32 -07:00
|
|
|
if (svr->name) free(svr->name);
|
|
|
|
if (svr->path) free(svr->path);
|
2006-03-19 23:45:58 -08:00
|
|
|
if (svr->ip) free(svr->ip);
|
2003-09-23 01:09:32 -07:00
|
|
|
if (svr->fd_handler) ecore_main_fd_handler_del(svr->fd_handler);
|
|
|
|
free(svr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_ecore_con_client_free(Ecore_Con_Client *cl)
|
|
|
|
{
|
2006-05-18 20:52:24 -07:00
|
|
|
double t_start, t;
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
ECORE_MAGIC_SET(cl, ECORE_MAGIC_NONE);
|
2006-05-18 20:52:24 -07:00
|
|
|
t_start = ecore_time_get();
|
|
|
|
while ((cl->buf) && (!cl->dead))
|
|
|
|
{
|
|
|
|
_ecore_con_client_flush(cl);
|
|
|
|
t = ecore_time_get();
|
|
|
|
if ((t - t_start) > 5.0)
|
|
|
|
{
|
|
|
|
printf("ECORE_CON: EEK - stuck in _ecore_con_client_free() trying\n"
|
|
|
|
" to flush data out from the client, and have been for\n"
|
|
|
|
" %1.1f seconds. This is taking too long. Aborting flush.\n",
|
|
|
|
(t - t_start));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2003-09-23 01:09:32 -07:00
|
|
|
if (cl->buf) free(cl->buf);
|
|
|
|
if (cl->fd >= 0) close(cl->fd);
|
|
|
|
if (cl->fd_handler) ecore_main_fd_handler_del(cl->fd_handler);
|
2006-03-19 23:45:58 -08:00
|
|
|
if (cl->ip) free(cl->ip);
|
2003-09-23 01:09:32 -07:00
|
|
|
free(cl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-03-01 23:06:44 -08:00
|
|
|
_ecore_con_svr_handler(void *data, Ecore_Fd_Handler *fd_handler __UNUSED__)
|
2003-09-23 01:09:32 -07:00
|
|
|
{
|
|
|
|
Ecore_Con_Server *svr;
|
|
|
|
int new_fd;
|
|
|
|
struct sockaddr_in incoming;
|
|
|
|
size_t size_in;
|
|
|
|
|
|
|
|
svr = data;
|
|
|
|
if (svr->dead) return 1;
|
2005-04-28 21:51:31 -07:00
|
|
|
if ((svr->client_limit >= 0) && (!svr->reject_excess_clients))
|
|
|
|
{
|
|
|
|
if (ecore_list_nodes(svr->clients) >= svr->client_limit) return 1;
|
|
|
|
}
|
2003-09-23 01:09:32 -07:00
|
|
|
/* a new client */
|
|
|
|
size_in = sizeof(struct sockaddr_in);
|
|
|
|
new_fd = accept(svr->fd, (struct sockaddr *)&incoming, &size_in);
|
|
|
|
if (new_fd >= 0)
|
|
|
|
{
|
|
|
|
Ecore_Con_Client *cl;
|
2006-03-19 23:45:58 -08:00
|
|
|
char buf[64];
|
|
|
|
uint32_t ip;
|
|
|
|
|
2005-04-28 21:51:31 -07:00
|
|
|
if ((svr->client_limit >= 0) && (svr->reject_excess_clients))
|
|
|
|
{
|
|
|
|
close(new_fd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
cl = calloc(1, sizeof(Ecore_Con_Client));
|
|
|
|
if (!cl)
|
|
|
|
{
|
|
|
|
close(new_fd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fcntl(new_fd, F_SETFL, O_NONBLOCK);
|
|
|
|
fcntl(new_fd, F_SETFD, FD_CLOEXEC);
|
|
|
|
cl->fd = new_fd;
|
|
|
|
cl->server = svr;
|
|
|
|
cl->fd_handler = ecore_main_fd_handler_add(cl->fd,
|
|
|
|
ECORE_FD_READ,
|
2004-04-14 01:51:19 -07:00
|
|
|
_ecore_con_svr_cl_handler,
|
|
|
|
cl, NULL, NULL);
|
2003-09-23 01:09:32 -07:00
|
|
|
ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
|
2005-04-28 21:51:31 -07:00
|
|
|
ecore_list_append(svr->clients, cl);
|
2006-03-19 23:45:58 -08:00
|
|
|
ip = incoming.sin_addr.s_addr;
|
|
|
|
snprintf(buf, sizeof(buf),
|
|
|
|
"%i.%i.%i.%i",
|
|
|
|
(ip ) & 0xff,
|
|
|
|
(ip >> 8 ) & 0xff,
|
|
|
|
(ip >> 16) & 0xff,
|
|
|
|
(ip >> 24) & 0xff);
|
|
|
|
cl->ip = strdup(buf);
|
2003-09-23 01:09:32 -07:00
|
|
|
{
|
|
|
|
Ecore_Con_Event_Client_Add *e;
|
|
|
|
|
|
|
|
e = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
|
|
|
|
if (e)
|
|
|
|
{
|
2006-03-19 21:53:12 -08:00
|
|
|
cl->event_count++;
|
2003-09-23 01:09:32 -07:00
|
|
|
e->client = cl;
|
2006-03-19 21:53:12 -08:00
|
|
|
ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD, e,
|
|
|
|
_ecore_con_event_client_add_free, NULL);
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-04-03 07:03:33 -08:00
|
|
|
#if USE_OPENSSL
|
2004-04-25 08:42:57 -07:00
|
|
|
/* Tries to connect an Ecore_Con_Server to an SSL host.
|
|
|
|
* Returns 1 on success, -1 on fatal errors and 0 if the caller
|
|
|
|
* should try again later.
|
|
|
|
*/
|
2004-04-14 01:51:19 -07:00
|
|
|
static int
|
|
|
|
svr_try_connect_ssl(Ecore_Con_Server *svr)
|
2004-04-03 07:03:33 -08:00
|
|
|
{
|
|
|
|
int res, ssl_err, flag = 0;
|
2005-11-26 23:18:36 -08:00
|
|
|
|
|
|
|
if (!ssl_init_count)
|
|
|
|
{
|
|
|
|
SSL_library_init();
|
|
|
|
SSL_load_error_strings();
|
|
|
|
}
|
|
|
|
ssl_init_count++;
|
2004-04-14 01:51:19 -07:00
|
|
|
|
|
|
|
res = SSL_connect(svr->ssl);
|
2004-04-14 09:14:24 -07:00
|
|
|
if (res == 1) return 1;
|
2004-04-12 12:29:37 -07:00
|
|
|
ssl_err = SSL_get_error(svr->ssl, res);
|
2004-04-14 01:51:19 -07:00
|
|
|
|
|
|
|
if (ssl_err == SSL_ERROR_NONE) return 1;
|
|
|
|
if (ssl_err == SSL_ERROR_WANT_READ) flag = ECORE_FD_READ;
|
|
|
|
else if (ssl_err == SSL_ERROR_WANT_WRITE) flag = ECORE_FD_WRITE;
|
2004-04-25 08:42:57 -07:00
|
|
|
else return -1;
|
2004-04-14 01:51:19 -07:00
|
|
|
if (flag) ecore_main_fd_handler_active_set(svr->fd_handler, flag);
|
2004-04-03 07:03:33 -08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-04-25 08:42:57 -07:00
|
|
|
static void
|
|
|
|
kill_server(Ecore_Con_Server *svr)
|
|
|
|
{
|
|
|
|
Ecore_Con_Event_Server_Del *e;
|
2005-03-03 00:54:09 -08:00
|
|
|
|
2004-04-25 08:42:57 -07:00
|
|
|
e = calloc(1, sizeof(Ecore_Con_Event_Server_Del));
|
|
|
|
if (e)
|
2005-03-03 00:54:09 -08:00
|
|
|
{
|
2006-03-19 21:53:12 -08:00
|
|
|
svr->event_count++;
|
2005-03-03 00:54:09 -08:00
|
|
|
e->server = svr;
|
2006-03-19 21:53:12 -08:00
|
|
|
ecore_event_add(ECORE_CON_EVENT_SERVER_DEL, e,
|
|
|
|
_ecore_con_event_server_del_free, NULL);
|
2005-03-03 00:54:09 -08:00
|
|
|
}
|
|
|
|
|
2004-04-25 08:42:57 -07:00
|
|
|
svr->dead = 1;
|
2005-08-16 06:15:24 -07:00
|
|
|
if (svr->fd_handler) ecore_main_fd_handler_del(svr->fd_handler);
|
2004-04-25 08:42:57 -07:00
|
|
|
svr->fd_handler = NULL;
|
|
|
|
}
|
|
|
|
|
2005-08-16 02:25:02 -07:00
|
|
|
static void
|
2005-08-30 02:32:09 -07:00
|
|
|
_ecore_con_cb_dns_lookup(void *data, struct hostent *he)
|
2005-08-16 02:25:02 -07:00
|
|
|
{
|
|
|
|
Ecore_Con_Server *svr;
|
|
|
|
struct sockaddr_in socket_addr;
|
|
|
|
int curstate = 0;
|
2006-03-19 23:45:58 -08:00
|
|
|
char buf[64];
|
|
|
|
uint32_t ip;
|
2005-08-16 02:25:02 -07:00
|
|
|
|
|
|
|
svr = data;
|
|
|
|
|
2005-08-16 04:26:05 -07:00
|
|
|
if (!he) goto error;
|
2005-08-16 02:25:02 -07:00
|
|
|
svr->fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (svr->fd < 0) goto error;
|
|
|
|
if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
|
|
|
|
if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
|
|
|
|
if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, &curstate, sizeof(curstate)) < 0) goto error;
|
|
|
|
socket_addr.sin_family = AF_INET;
|
|
|
|
socket_addr.sin_port = htons(svr->port);
|
|
|
|
memcpy((struct in_addr *)&socket_addr.sin_addr,
|
2005-08-16 04:26:05 -07:00
|
|
|
he->h_addr, sizeof(struct in_addr));
|
2005-08-16 02:25:02 -07:00
|
|
|
if (connect(svr->fd, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in)) < 0)
|
|
|
|
{
|
|
|
|
if (errno != EINPROGRESS)
|
|
|
|
goto error;
|
|
|
|
svr->connecting = 1;
|
|
|
|
svr->fd_handler = ecore_main_fd_handler_add(svr->fd,
|
2005-08-16 04:26:05 -07:00
|
|
|
ECORE_FD_READ | ECORE_FD_WRITE,
|
|
|
|
_ecore_con_cl_handler, svr,
|
|
|
|
NULL, NULL);
|
2005-08-16 02:25:02 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
svr->fd_handler = ecore_main_fd_handler_add(svr->fd,
|
2005-08-16 04:26:05 -07:00
|
|
|
ECORE_FD_READ,
|
|
|
|
_ecore_con_cl_handler, svr,
|
|
|
|
NULL, NULL);
|
2005-08-16 02:25:02 -07:00
|
|
|
|
|
|
|
if (!svr->fd_handler) goto error;
|
2006-03-19 23:45:58 -08:00
|
|
|
ip = socket_addr.sin_addr.s_addr;
|
|
|
|
snprintf(buf, sizeof(buf),
|
|
|
|
"%i.%i.%i.%i",
|
|
|
|
(ip ) & 0xff,
|
|
|
|
(ip >> 8 ) & 0xff,
|
|
|
|
(ip >> 16) & 0xff,
|
|
|
|
(ip >> 24) & 0xff);
|
|
|
|
svr->ip = strdup(buf);
|
2005-08-16 04:26:05 -07:00
|
|
|
|
2005-08-24 09:29:06 -07:00
|
|
|
#if USE_OPENSSL
|
|
|
|
if (svr->type & ECORE_CON_USE_SSL)
|
|
|
|
{
|
2005-11-26 23:18:36 -08:00
|
|
|
if (!ssl_init_count)
|
|
|
|
{
|
|
|
|
SSL_library_init();
|
|
|
|
SSL_load_error_strings();
|
|
|
|
}
|
|
|
|
ssl_init_count++;
|
|
|
|
|
2005-08-24 09:29:06 -07:00
|
|
|
/* SSLv3 gives *weird* results on my box, don't use it yet */
|
|
|
|
if (!(svr->ssl_ctx = SSL_CTX_new(SSLv2_client_method())))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!(svr->ssl = SSL_new(svr->ssl_ctx)))
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
SSL_set_fd(svr->ssl, svr->fd);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-08-16 02:25:02 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
kill_server(svr);
|
|
|
|
}
|
|
|
|
|
2004-04-14 01:51:19 -07:00
|
|
|
static int
|
2004-05-09 13:23:23 -07:00
|
|
|
svr_try_connect_plain(Ecore_Con_Server *svr)
|
2004-04-03 07:03:33 -08:00
|
|
|
{
|
2005-08-16 02:25:02 -07:00
|
|
|
int so_err = 0;
|
|
|
|
unsigned int size = sizeof(int);
|
2004-04-03 07:03:33 -08:00
|
|
|
|
|
|
|
if (getsockopt(svr->fd, SOL_SOCKET, SO_ERROR, &so_err, &size) < 0)
|
2004-04-14 01:51:19 -07:00
|
|
|
so_err = -1;
|
|
|
|
|
|
|
|
if (so_err != 0)
|
|
|
|
{
|
|
|
|
/* we lost our server! */
|
2004-04-25 08:42:57 -07:00
|
|
|
kill_server(svr);
|
2004-04-14 01:51:19 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* we got our server! */
|
|
|
|
Ecore_Con_Event_Server_Add *e;
|
|
|
|
|
|
|
|
svr->connecting = 0;
|
|
|
|
e = calloc(1, sizeof(Ecore_Con_Event_Server_Add));
|
|
|
|
if (e)
|
|
|
|
{
|
2006-03-19 21:53:12 -08:00
|
|
|
svr->event_count++;
|
2004-04-14 01:51:19 -07:00
|
|
|
e->server = svr;
|
2006-03-19 21:53:12 -08:00
|
|
|
ecore_event_add(ECORE_CON_EVENT_SERVER_ADD, e,
|
|
|
|
_ecore_con_event_server_add_free, NULL);
|
2004-04-14 01:51:19 -07:00
|
|
|
}
|
|
|
|
if (!svr->write_buf)
|
|
|
|
ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
|
|
|
|
}
|
2004-04-03 07:03:33 -08:00
|
|
|
return (!svr->dead);
|
|
|
|
}
|
|
|
|
|
2004-05-09 13:23:23 -07:00
|
|
|
/* returns 1 on success, 0 on failure */
|
2005-08-15 16:36:22 -07:00
|
|
|
static int svr_try_connect(Ecore_Con_Server *svr)
|
2004-05-09 13:23:23 -07:00
|
|
|
{
|
|
|
|
#if USE_OPENSSL
|
|
|
|
if (!svr->ssl)
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
return svr_try_connect_plain(svr);
|
|
|
|
#if USE_OPENSSL
|
|
|
|
}
|
|
|
|
else
|
|
|
|
switch (svr_try_connect_ssl(svr)) {
|
|
|
|
case 1:
|
|
|
|
return svr_try_connect_plain(svr);
|
|
|
|
case -1:
|
|
|
|
kill_server(svr);
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2004-04-03 07:03:33 -08:00
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
static int
|
|
|
|
_ecore_con_cl_handler(void *data, Ecore_Fd_Handler *fd_handler)
|
|
|
|
{
|
|
|
|
Ecore_Con_Server *svr;
|
2004-04-03 07:03:33 -08:00
|
|
|
#if USE_OPENSSL
|
2004-04-14 01:51:19 -07:00
|
|
|
int ssl_err = SSL_ERROR_NONE;
|
2004-04-03 07:03:33 -08:00
|
|
|
#endif
|
2003-09-23 01:09:32 -07:00
|
|
|
|
|
|
|
svr = data;
|
|
|
|
if (svr->dead) return 1;
|
|
|
|
if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
|
|
|
|
{
|
|
|
|
unsigned char *inbuf = NULL;
|
|
|
|
int inbuf_num = 0;
|
2004-04-03 07:03:33 -08:00
|
|
|
|
2004-05-09 13:23:23 -07:00
|
|
|
if (svr->connecting && !svr_try_connect(svr))
|
|
|
|
return 1;
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
for (;;)
|
|
|
|
{
|
2005-09-22 22:46:56 -07:00
|
|
|
int num, lost_server;
|
2004-04-25 08:52:53 -07:00
|
|
|
char buf[READBUFSIZ];
|
2004-04-03 07:03:33 -08:00
|
|
|
|
2005-09-22 22:46:56 -07:00
|
|
|
lost_server = 0;
|
2004-03-31 08:47:45 -08:00
|
|
|
#if USE_OPENSSL
|
2004-04-14 01:51:19 -07:00
|
|
|
if (!svr->ssl)
|
|
|
|
{
|
2004-03-31 08:47:45 -08:00
|
|
|
#endif
|
2004-04-25 08:52:53 -07:00
|
|
|
if ((num = read(svr->fd, buf, READBUFSIZ)) < 1)
|
2005-09-22 22:46:56 -07:00
|
|
|
{
|
|
|
|
lost_server = ((errno == EIO) ||
|
|
|
|
(errno == EBADF) ||
|
|
|
|
(errno == EPIPE) ||
|
|
|
|
(errno == EINVAL) ||
|
|
|
|
(errno == ENOSPC) ||
|
|
|
|
(num == 0));
|
|
|
|
/* is num == 0 is right - when the server closes us
|
|
|
|
* off we will get this (as this is called when select
|
|
|
|
* tells us there is data to read!)
|
|
|
|
*/
|
|
|
|
}
|
2004-04-01 08:41:27 -08:00
|
|
|
#if USE_OPENSSL
|
2004-04-14 01:51:19 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-04-25 08:52:53 -07:00
|
|
|
num = SSL_read(svr->ssl, buf, READBUFSIZ);
|
2004-04-14 01:51:19 -07:00
|
|
|
if (num < 1)
|
|
|
|
{
|
|
|
|
ssl_err = SSL_get_error(svr->ssl, num);
|
|
|
|
lost_server = (ssl_err == SSL_ERROR_ZERO_RETURN);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ssl_err = SSL_ERROR_NONE;
|
|
|
|
}
|
2004-03-31 08:47:45 -08:00
|
|
|
#endif
|
2003-09-23 01:09:32 -07:00
|
|
|
if (num < 1)
|
|
|
|
{
|
|
|
|
if (inbuf)
|
|
|
|
{
|
|
|
|
Ecore_Con_Event_Server_Data *e;
|
|
|
|
|
|
|
|
e = calloc(1, sizeof(Ecore_Con_Event_Server_Data));
|
|
|
|
if (e)
|
|
|
|
{
|
2006-03-19 21:53:12 -08:00
|
|
|
svr->event_count++;
|
2003-09-23 01:09:32 -07:00
|
|
|
e->server = svr;
|
|
|
|
e->data = inbuf;
|
|
|
|
e->size = inbuf_num;
|
|
|
|
ecore_event_add(ECORE_CON_EVENT_SERVER_DATA, e,
|
2006-03-19 21:53:12 -08:00
|
|
|
_ecore_con_event_server_data_free,
|
|
|
|
NULL);
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
|
|
|
}
|
2004-04-03 07:03:33 -08:00
|
|
|
if (lost_server)
|
2003-09-23 01:09:32 -07:00
|
|
|
{
|
|
|
|
/* we lost our server! */
|
2004-04-25 08:42:57 -07:00
|
|
|
kill_server(svr);
|
2004-04-03 07:03:33 -08:00
|
|
|
return 1;
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inbuf = realloc(inbuf, inbuf_num + num);
|
2004-04-25 08:52:53 -07:00
|
|
|
memcpy(inbuf + inbuf_num, buf, num);
|
2003-09-23 01:09:32 -07:00
|
|
|
inbuf_num += num;
|
|
|
|
}
|
|
|
|
}
|
2004-03-31 08:47:45 -08:00
|
|
|
|
|
|
|
#if USE_OPENSSL
|
2004-04-14 01:51:19 -07:00
|
|
|
if (svr->ssl && ssl_err == SSL_ERROR_WANT_READ)
|
|
|
|
ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
|
|
|
|
else if (svr->ssl && ssl_err == SSL_ERROR_WANT_WRITE)
|
|
|
|
ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_WRITE);
|
2004-03-31 08:47:45 -08:00
|
|
|
#endif
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
2004-04-14 01:51:19 -07:00
|
|
|
else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
|
|
|
|
{
|
2004-05-09 13:23:23 -07:00
|
|
|
if (svr->connecting && !svr_try_connect (svr))
|
|
|
|
return 1;
|
|
|
|
|
2004-04-14 01:51:19 -07:00
|
|
|
_ecore_con_server_flush(svr);
|
|
|
|
}
|
2004-04-03 07:03:33 -08:00
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_ecore_con_svr_cl_handler(void *data, Ecore_Fd_Handler *fd_handler)
|
|
|
|
{
|
|
|
|
Ecore_Con_Client *cl;
|
|
|
|
|
|
|
|
cl = data;
|
|
|
|
if (cl->dead) return 1;
|
|
|
|
if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
|
|
|
|
{
|
|
|
|
unsigned char *inbuf = NULL;
|
|
|
|
int inbuf_num = 0;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
char buf[65536];
|
|
|
|
int num;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
num = read(cl->fd, buf, 65536);
|
|
|
|
if (num < 1)
|
|
|
|
{
|
|
|
|
if (inbuf)
|
|
|
|
{
|
|
|
|
Ecore_Con_Event_Client_Data *e;
|
|
|
|
|
|
|
|
e = calloc(1, sizeof(Ecore_Con_Event_Client_Data));
|
|
|
|
if (e)
|
|
|
|
{
|
2006-03-19 21:53:12 -08:00
|
|
|
cl->event_count++;
|
2003-09-23 01:09:32 -07:00
|
|
|
e->client = cl;
|
|
|
|
e->data = inbuf;
|
|
|
|
e->size = inbuf_num;
|
|
|
|
ecore_event_add(ECORE_CON_EVENT_CLIENT_DATA, e,
|
2006-03-19 21:53:12 -08:00
|
|
|
_ecore_con_event_client_data_free,
|
|
|
|
NULL);
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((errno == EIO) || (errno == EBADF) ||
|
|
|
|
(errno == EPIPE) || (errno == EINVAL) ||
|
|
|
|
(errno == ENOSPC) || (num == 0)/* is num == 0 right? */)
|
|
|
|
{
|
|
|
|
/* we lost our client! */
|
|
|
|
Ecore_Con_Event_Client_Del *e;
|
|
|
|
|
|
|
|
e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
|
|
|
|
if (e)
|
|
|
|
{
|
2006-03-19 21:53:12 -08:00
|
|
|
cl->event_count++;
|
2003-09-23 01:09:32 -07:00
|
|
|
e->client = cl;
|
2006-03-19 21:53:12 -08:00
|
|
|
ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
|
|
|
|
_ecore_con_event_client_del_free,
|
|
|
|
NULL);
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
|
|
|
cl->dead = 1;
|
|
|
|
ecore_main_fd_handler_del(cl->fd_handler);
|
|
|
|
cl->fd_handler = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inbuf = realloc(inbuf, inbuf_num + num);
|
|
|
|
memcpy(inbuf + inbuf_num, buf, num);
|
|
|
|
inbuf_num += num;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
|
|
|
|
_ecore_con_client_flush(cl);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_ecore_con_server_flush(Ecore_Con_Server *svr)
|
|
|
|
{
|
2004-04-03 07:03:33 -08:00
|
|
|
int count, num, lost_server = 0;
|
|
|
|
#if USE_OPENSSL
|
2004-04-12 12:29:37 -07:00
|
|
|
int ssl_err = SSL_ERROR_NONE;
|
2004-04-03 07:03:33 -08:00
|
|
|
#endif
|
2003-09-23 01:09:32 -07:00
|
|
|
|
2004-04-03 07:03:33 -08:00
|
|
|
if (!svr->write_buf) return;
|
2004-04-02 10:32:55 -08:00
|
|
|
|
|
|
|
/* check whether we need to write anything at all.
|
|
|
|
* we must not write zero bytes with SSL_write() since it
|
|
|
|
* causes undefined behaviour
|
|
|
|
*/
|
2004-04-03 07:03:33 -08:00
|
|
|
if (svr->write_buf_size == svr->write_buf_offset)
|
2004-04-02 10:32:55 -08:00
|
|
|
return;
|
|
|
|
|
2004-04-03 07:03:33 -08:00
|
|
|
num = svr->write_buf_size - svr->write_buf_offset;
|
2004-03-31 08:47:45 -08:00
|
|
|
#if USE_OPENSSL
|
2004-04-14 01:51:19 -07:00
|
|
|
if (!svr->ssl)
|
|
|
|
{
|
2004-03-31 08:47:45 -08:00
|
|
|
#endif
|
2004-04-14 01:51:19 -07:00
|
|
|
count = write(svr->fd, svr->write_buf + svr->write_buf_offset, num);
|
|
|
|
if (count < 1)
|
|
|
|
lost_server = (errno == EIO || errno == EBADF ||
|
|
|
|
errno == EPIPE || errno == EINVAL ||
|
|
|
|
errno == ENOSPC);
|
2004-03-31 08:47:45 -08:00
|
|
|
#if USE_OPENSSL
|
2004-04-14 01:51:19 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count = SSL_write(svr->ssl, svr->write_buf + svr->write_buf_offset, num);
|
|
|
|
|
|
|
|
if (count < 1)
|
|
|
|
{
|
|
|
|
ssl_err = SSL_get_error(svr->ssl, count);
|
|
|
|
lost_server = (ssl_err == SSL_ERROR_ZERO_RETURN);
|
|
|
|
}
|
|
|
|
}
|
2004-03-31 08:47:45 -08:00
|
|
|
#endif
|
|
|
|
|
2004-04-14 01:51:19 -07:00
|
|
|
if (lost_server)
|
|
|
|
{
|
|
|
|
/* we lost our server! */
|
2004-04-25 08:42:57 -07:00
|
|
|
kill_server(svr);
|
2004-04-14 01:51:19 -07:00
|
|
|
return;
|
|
|
|
}
|
2004-04-02 10:32:55 -08:00
|
|
|
|
2004-04-14 01:51:19 -07:00
|
|
|
if (count < 1)
|
|
|
|
{
|
2004-04-03 07:03:33 -08:00
|
|
|
#if USE_OPENSSL
|
2004-04-14 01:51:19 -07:00
|
|
|
if (svr->ssl && ssl_err == SSL_ERROR_WANT_READ)
|
|
|
|
ecore_main_fd_handler_active_set(svr->fd_handler,
|
|
|
|
ECORE_FD_READ);
|
|
|
|
else if (svr->ssl && ssl_err == SSL_ERROR_WANT_WRITE)
|
|
|
|
ecore_main_fd_handler_active_set(svr->fd_handler,
|
|
|
|
ECORE_FD_WRITE);
|
2004-04-03 07:03:33 -08:00
|
|
|
#endif
|
2004-04-14 01:51:19 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-04-03 07:03:33 -08:00
|
|
|
svr->write_buf_offset += count;
|
|
|
|
if (svr->write_buf_offset >= svr->write_buf_size)
|
2003-09-23 01:09:32 -07:00
|
|
|
{
|
2004-04-03 07:03:33 -08:00
|
|
|
svr->write_buf_size = 0;
|
|
|
|
svr->write_buf_offset = 0;
|
|
|
|
free(svr->write_buf);
|
|
|
|
svr->write_buf = NULL;
|
2003-09-23 01:09:32 -07:00
|
|
|
ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_ecore_con_client_flush(Ecore_Con_Client *cl)
|
|
|
|
{
|
|
|
|
int count, num;
|
|
|
|
|
|
|
|
if (!cl->buf) return;
|
|
|
|
num = cl->buf_size - cl->buf_offset;
|
|
|
|
count = write(cl->fd, cl->buf + cl->buf_offset, num);
|
|
|
|
if (count < 1)
|
|
|
|
{
|
|
|
|
if ((errno == EIO) || (errno == EBADF) || (errno == EPIPE) ||
|
|
|
|
(errno == EINVAL) || (errno == ENOSPC))
|
|
|
|
{
|
|
|
|
/* we lost our client! */
|
|
|
|
Ecore_Con_Event_Client_Del *e;
|
|
|
|
|
|
|
|
e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
|
|
|
|
if (e)
|
|
|
|
{
|
2006-03-19 21:53:12 -08:00
|
|
|
cl->event_count++;
|
2003-09-23 01:09:32 -07:00
|
|
|
e->client = cl;
|
2006-03-19 21:53:12 -08:00
|
|
|
ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
|
|
|
|
_ecore_con_event_client_del_free, NULL);
|
2003-09-23 01:09:32 -07:00
|
|
|
}
|
|
|
|
cl->dead = 1;
|
|
|
|
ecore_main_fd_handler_del(cl->fd_handler);
|
|
|
|
cl->fd_handler = NULL;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cl->buf_offset += count;
|
|
|
|
if (cl->buf_offset >= cl->buf_size)
|
|
|
|
{
|
|
|
|
cl->buf_size = 0;
|
|
|
|
cl->buf_offset = 0;
|
|
|
|
free(cl->buf);
|
|
|
|
cl->buf = NULL;
|
|
|
|
ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-19 21:53:12 -08:00
|
|
|
static void
|
|
|
|
_ecore_con_event_client_add_free(void *data __UNUSED__, void *ev)
|
|
|
|
{
|
|
|
|
Ecore_Con_Event_Client_Add *e;
|
|
|
|
|
|
|
|
e = ev;
|
|
|
|
e->client->event_count--;
|
|
|
|
if ((e->client->event_count == 0) && (e->client->delete_me))
|
|
|
|
ecore_con_client_del(e->client);
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_ecore_con_event_client_del_free(void *data __UNUSED__, void *ev)
|
|
|
|
{
|
|
|
|
Ecore_Con_Event_Client_Del *e;
|
|
|
|
|
|
|
|
e = ev;
|
|
|
|
e->client->event_count--;
|
|
|
|
if ((e->client->event_count == 0) && (e->client->delete_me))
|
|
|
|
ecore_con_client_del(e->client);
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
2003-09-23 01:09:32 -07:00
|
|
|
static void
|
2005-03-01 23:06:44 -08:00
|
|
|
_ecore_con_event_client_data_free(void *data __UNUSED__, void *ev)
|
2003-09-23 01:09:32 -07:00
|
|
|
{
|
|
|
|
Ecore_Con_Event_Client_Data *e;
|
|
|
|
|
|
|
|
e = ev;
|
2006-03-19 21:53:12 -08:00
|
|
|
e->client->event_count--;
|
2003-09-23 01:09:32 -07:00
|
|
|
if (e->data) free(e->data);
|
2006-03-19 21:53:12 -08:00
|
|
|
if ((e->client->event_count == 0) && (e->client->delete_me))
|
|
|
|
ecore_con_client_del(e->client);
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_ecore_con_event_server_add_free(void *data __UNUSED__, void *ev)
|
|
|
|
{
|
|
|
|
Ecore_Con_Event_Server_Add *e;
|
|
|
|
|
|
|
|
e = ev;
|
|
|
|
e->server->event_count--;
|
|
|
|
if ((e->server->event_count == 0) && (e->server->delete_me))
|
|
|
|
ecore_con_server_del(e->server);
|
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_ecore_con_event_server_del_free(void *data __UNUSED__, void *ev)
|
|
|
|
{
|
|
|
|
Ecore_Con_Event_Server_Del *e;
|
|
|
|
|
|
|
|
e = ev;
|
|
|
|
e->server->event_count--;
|
|
|
|
if ((e->server->event_count == 0) && (e->server->delete_me))
|
|
|
|
ecore_con_server_del(e->server);
|
2003-09-23 01:09:32 -07:00
|
|
|
free(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-03-01 23:06:44 -08:00
|
|
|
_ecore_con_event_server_data_free(void *data __UNUSED__, void *ev)
|
2003-09-23 01:09:32 -07:00
|
|
|
{
|
|
|
|
Ecore_Con_Event_Server_Data *e;
|
|
|
|
|
|
|
|
e = ev;
|
2006-03-19 21:53:12 -08:00
|
|
|
e->server->event_count--;
|
2003-09-23 01:09:32 -07:00
|
|
|
if (e->data) free(e->data);
|
2006-03-19 21:53:12 -08:00
|
|
|
if ((e->server->event_count == 0) && (e->server->delete_me))
|
|
|
|
ecore_con_server_del(e->server);
|
2003-09-23 01:09:32 -07:00
|
|
|
free(e);
|
|
|
|
}
|