2 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
7 #include <sys/socket.h>
18 #include "ecore_private.h"
19 #include "Ecore_Con.h"
20 #include "ecore_con_private.h"
22 #ifdef HAVE_NETINET_IN_H
23 # include <netinet/in.h>
25 #ifdef HAVE_WINSOCK2_H
26 # include <winsock2.h>
29 static void _ecore_con_cb_dns_lookup(void *data, struct hostent *he);
30 static void _ecore_con_server_free(Ecore_Con_Server *svr);
31 static void _ecore_con_client_free(Ecore_Con_Client *cl);
32 static int _ecore_con_svr_handler(void *data, Ecore_Fd_Handler *fd_handler);
33 static int _ecore_con_cl_handler(void *data, Ecore_Fd_Handler *fd_handler);
34 static int _ecore_con_svr_cl_handler(void *data, Ecore_Fd_Handler *fd_handler);
35 static void _ecore_con_server_flush(Ecore_Con_Server *svr);
36 static void _ecore_con_client_flush(Ecore_Con_Client *cl);
37 static void _ecore_con_event_client_add_free(void *data, void *ev);
38 static void _ecore_con_event_client_del_free(void *data, void *ev);
39 static void _ecore_con_event_client_data_free(void *data, void *ev);
40 static void _ecore_con_event_server_add_free(void *data, void *ev);
41 static void _ecore_con_event_server_del_free(void *data, void *ev);
42 static void _ecore_con_event_server_data_free(void *data, void *ev);
44 EAPI int ECORE_CON_EVENT_CLIENT_ADD = 0;
45 EAPI int ECORE_CON_EVENT_CLIENT_DEL = 0;
46 EAPI int ECORE_CON_EVENT_SERVER_ADD = 0;
47 EAPI int ECORE_CON_EVENT_SERVER_DEL = 0;
48 EAPI int ECORE_CON_EVENT_CLIENT_DATA = 0;
49 EAPI int ECORE_CON_EVENT_SERVER_DATA = 0;
51 static Ecore_List *servers = NULL;
52 static int init_count = 0;
54 static int ssl_init_count = 0;
57 #define LENGTH_OF_SOCKADDR_UN(s) (strlen((s)->sun_path) + (size_t)(((struct sockaddr_un *)NULL)->sun_path))
58 #define LENGTH_OF_ABSTRACT_SOCKADDR_UN(s, path) (strlen(path) + 1 + (size_t)(((struct sockaddr_un *)NULL)->sun_path))
61 * @defgroup Ecore_Con_Lib_Group Ecore Connection Library Functions
63 * Utility functions that set up and shut down the Ecore Connection
68 * Initialises the Ecore_Con library.
69 * @return Number of times the library has been initialised without being
71 * @ingroup Ecore_Con_Lib_Group
76 if (++init_count != 1) return init_count;
79 ECORE_CON_EVENT_CLIENT_ADD = ecore_event_type_new();
80 ECORE_CON_EVENT_CLIENT_DEL = ecore_event_type_new();
81 ECORE_CON_EVENT_SERVER_ADD = ecore_event_type_new();
82 ECORE_CON_EVENT_SERVER_DEL = ecore_event_type_new();
83 ECORE_CON_EVENT_CLIENT_DATA = ecore_event_type_new();
84 ECORE_CON_EVENT_SERVER_DATA = ecore_event_type_new();
86 /* TODO Remember return value, if it fails, use gethostbyname() */
89 servers = ecore_list_new();
95 * Shuts down the Ecore_Con library.
96 * @return Number of times the library has been initialised without being
98 * @ingroup Ecore_Con_Lib_Group
101 ecore_con_shutdown(void)
103 if (--init_count != 0) return init_count;
105 while (!ecore_list_empty_is(servers))
106 _ecore_con_server_free(ecore_list_first_remove(servers));
107 ecore_list_destroy(servers);
110 ecore_con_dns_shutdown();
118 * @defgroup Ecore_Con_Server_Group Ecore Connection Server Functions
120 * Functions that operate on Ecore server objects.
124 * Creates a server to listen for connections.
126 * The socket on which the server listens depends on the connection
128 * @li If @a compl_type is @c ECORE_CON_LOCAL_USER, the server will listen on
129 * the Unix socket "~/.ecore/[name]/[port]".
130 * @li If @a compl_type is @c ECORE_CON_LOCAL_SYSTEM, the server will listen
131 * on Unix socket "/tmp/.ecore_service|[name]|[port]".
132 * @li If @a compl_type is @c ECORE_CON_REMOTE_SYSTEM, the server will listen
133 * on TCP port @c port.
135 * @param compl_type The connection type.
136 * @param name Name to associate with the socket. It is used when
137 * generating the socket name of a Unix socket. Though
138 * it is not used for the TCP socket, it still needs to
139 * be a valid character array. @c NULL will not be
141 * @param port Number to identify socket. When a Unix socket is used,
142 * it becomes part of the socket name. When a TCP socket
143 * is used, it is used as the TCP port.
144 * @param data Data to associate with the created Ecore_Con_Server
146 * @return A new Ecore_Con_Server.
147 * @ingroup Ecore_Con_Server_Group
149 EAPI Ecore_Con_Server *
150 ecore_con_server_add(Ecore_Con_Type compl_type, const char *name, int port,
153 Ecore_Con_Server *svr;
155 struct sockaddr_in socket_addr;
156 struct sockaddr_un socket_unix;
160 if (port < 0) return NULL;
161 /* local user socket: FILE: ~/.ecore/[name]/[port] */
162 /* local system socket: FILE: /tmp/.ecore_service|[name]|[port] */
163 /* remote system socket: TCP/IP: [name]:[port] */
164 svr = calloc(1, sizeof(Ecore_Con_Server));
165 if (!svr) return NULL;
169 /* unset the SSL flag for the following checks */
170 type &= ECORE_CON_TYPE;
173 if ((type == ECORE_CON_LOCAL_USER) || (type == ECORE_CON_LOCAL_SYSTEM) ||
174 (type == ECORE_CON_LOCAL_ABSTRACT))
181 if (!name) goto error;
182 mask = S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH;
184 if (type == ECORE_CON_LOCAL_USER)
186 homedir = getenv("HOME");
187 if (!homedir) homedir = getenv("TMP");
188 if (!homedir) homedir = "/tmp";
189 mask = S_IRUSR | S_IWUSR | S_IXUSR;
190 snprintf(buf, sizeof(buf), "%s/.ecore", homedir);
191 if (stat(buf, &st) < 0) mkdir(buf, mask);
192 snprintf(buf, sizeof(buf), "%s/.ecore/%s", homedir, name);
193 if (stat(buf, &st) < 0) mkdir(buf, mask);
194 snprintf(buf, sizeof(buf), "%s/.ecore/%s/%i", homedir, name, port);
195 mask = S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH;
197 else if (type == ECORE_CON_LOCAL_SYSTEM)
203 snprintf(buf, sizeof(buf), "%s|%i", name, port);
205 snprintf(buf, sizeof(buf), "%s", name);
208 snprintf(buf, sizeof(buf), "/tmp/.ecore_service|%s|%i", name, port);
210 else if (type == ECORE_CON_LOCAL_ABSTRACT)
211 strncpy(buf, name, sizeof(buf));
214 svr->fd = socket(AF_UNIX, SOCK_STREAM, 0);
220 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
225 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
232 if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, &lin, sizeof(struct linger)) < 0)
237 socket_unix.sun_family = AF_UNIX;
238 if (type == ECORE_CON_LOCAL_ABSTRACT)
240 #ifdef HAVE_ABSTRACT_SOCKET
241 /* . is a placeholder */
242 snprintf(socket_unix.sun_path, sizeof(socket_unix.sun_path), ".%s", name);
243 /* first char null indicates abstract namespace */
244 socket_unix.sun_path[0] = '\0';
245 socket_unix_len = LENGTH_OF_ABSTRACT_SOCKADDR_UN(&socket_unix, name);
247 fprintf(stderr, "Your system does not support abstract sockets!\n");
254 strncpy(socket_unix.sun_path, buf, sizeof(socket_unix.sun_path));
255 socket_unix_len = LENGTH_OF_SOCKADDR_UN(&socket_unix);
257 if (bind(svr->fd, (struct sockaddr *)&socket_unix, socket_unix_len) < 0)
259 if (connect(svr->fd, (struct sockaddr *)&socket_unix,
260 socket_unix_len) < 0)
262 if ((type == ECORE_CON_LOCAL_USER) ||
263 (type == ECORE_CON_LOCAL_SYSTEM))
285 if (listen(svr->fd, 4096) < 0)
290 svr->path = strdup(buf);
297 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
298 _ecore_con_svr_handler, svr, NULL, NULL);
300 if (!svr->fd_handler) goto error;
302 else if (type == ECORE_CON_REMOTE_SYSTEM)
304 svr->fd = socket(AF_INET, SOCK_STREAM, 0);
305 if (svr->fd < 0) goto error;
306 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
307 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
310 if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, &lin, sizeof(struct linger)) < 0) goto error;
311 socket_addr.sin_family = AF_INET;
312 socket_addr.sin_port = htons(port);
313 socket_addr.sin_addr.s_addr = htonl(INADDR_ANY);
314 if (bind(svr->fd, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in)) < 0) goto error;
315 if (listen(svr->fd, 4096) < 0) goto error;
317 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
318 _ecore_con_svr_handler, svr, NULL, NULL);
319 if (!svr->fd_handler) goto error;
323 if (compl_type & ECORE_CON_SSL)
328 SSL_load_error_strings();
332 switch (compl_type & ECORE_CON_SSL)
334 case ECORE_CON_USE_SSL2:
335 if (!(svr->ssl_ctx = SSL_CTX_new(SSLv2_client_method())))
338 case ECORE_CON_USE_SSL3:
339 if (!(svr->ssl_ctx = SSL_CTX_new(SSLv3_client_method())))
342 case ECORE_CON_USE_TLS:
343 if (!(svr->ssl_ctx = SSL_CTX_new(TLSv1_client_method())))
348 if (!(svr->ssl = SSL_new(svr->ssl_ctx)))
351 SSL_set_fd(svr->ssl, svr->fd);
355 svr->name = strdup(name);
356 if (!svr->name) goto error;
359 svr->data = (void *)data;
361 svr->reject_excess_clients = 0;
362 svr->client_limit = -1;
363 svr->clients = ecore_list_new();
364 svr->ppid = getpid();
365 ecore_list_append(servers, svr);
366 ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
370 if (svr->name) free(svr->name);
371 if (svr->path) free(svr->path);
372 if (svr->fd >= 0) close(svr->fd);
373 if (svr->fd_handler) ecore_main_fd_handler_del(svr->fd_handler);
374 if (svr->write_buf) free(svr->write_buf);
375 if (svr->ip) free(svr->ip);
377 if (svr->ssl) SSL_free(svr->ssl);
378 if (svr->ssl_ctx) SSL_CTX_free(svr->ssl_ctx);
385 * Creates a server object to represent the server listening at the
388 * The socket to which the server connects depends on the connection type:
389 * @li If @a compl_type is @c ECORE_CON_LOCAL_USER, the function will
390 * connect to the server listening on the Unix socket
391 * "~/.ecore/[name]/[port]".
392 * @li If @a compl_type is @c ECORE_CON_LOCAL_SYSTEM, the function will
393 * connect to the server listening on the Unix socket
394 * "/tmp/.ecore_service|[name]|[port]".
395 * @li If @a compl_type is @c ECORE_CON_REMOTE_SYSTEM, the function will
396 * connect to the server listening on the TCP port "[name]:[port]".
398 * @param compl_type The connection type.
399 * @param name Name used when determining what socket to connect to.
400 * It is used to generate the socket name when the socket
401 * is a Unix socket. It is used as the hostname when
402 * connecting with a TCP socket.
403 * @param port Number to identify the socket to connect to. Used when
404 * generating the socket name for a Unix socket, or as the
405 * TCP port when connecting to a TCP socket.
406 * @param data Data to associate with the created Ecore_Con_Server
408 * @return A new Ecore_Con_Server.
409 * @ingroup Ecore_Con_Server_Group
411 EAPI Ecore_Con_Server *
412 ecore_con_server_connect(Ecore_Con_Type compl_type, const char *name, int port,
415 Ecore_Con_Server *svr;
417 struct sockaddr_un socket_unix;
421 if (!name) return NULL;
422 /* local user socket: FILE: ~/.ecore/[name]/[port] */
423 /* local system socket: FILE: /tmp/.ecore_service|[name]|[port] */
424 /* remote system socket: TCP/IP: [name]:[port] */
425 svr = calloc(1, sizeof(Ecore_Con_Server));
426 if (!svr) return NULL;
430 /* unset the SSL flag for the following checks */
431 type &= ECORE_CON_TYPE;
433 if ((type == ECORE_CON_REMOTE_SYSTEM) && (port < 0)) return NULL;
435 if ((type == ECORE_CON_LOCAL_USER) || (type == ECORE_CON_LOCAL_SYSTEM) ||
436 (type == ECORE_CON_LOCAL_ABSTRACT))
441 if (type == ECORE_CON_LOCAL_USER)
443 homedir = getenv("HOME");
444 if (!homedir) homedir = getenv("TMP");
445 if (!homedir) homedir = "/tmp";
446 snprintf(buf, sizeof(buf), "%s/.ecore/%s/%i", homedir, name, port);
448 else if (type == ECORE_CON_LOCAL_SYSTEM)
453 strncpy(buf, name, sizeof(buf));
455 snprintf(buf, sizeof(buf), "/tmp/.ecore_service|%s", name);
460 snprintf(buf, sizeof(buf), "%s|%i", name, port);
462 snprintf(buf, sizeof(buf), "/tmp/.ecore_service|%s|%i", name, port);
465 else if (type == ECORE_CON_LOCAL_ABSTRACT)
466 strncpy(buf, name, sizeof(buf));
468 svr->fd = socket(AF_UNIX, SOCK_STREAM, 0);
469 if (svr->fd < 0) goto error;
470 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
471 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
472 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, &curstate, sizeof(curstate)) < 0) goto error;
473 socket_unix.sun_family = AF_UNIX;
475 if (type == ECORE_CON_LOCAL_ABSTRACT)
477 #ifdef HAVE_ABSTRACT_SOCKETS
478 /* copy name insto sun_path, prefixed by null to indicate abstract namespace */
479 snprintf(socket_unix.sun_path, sizeof(socket_unix.sun_path), ".%s", name);
480 socket_unix.sun_path[0] = '\0';
481 socket_unix_len = LENGTH_OF_ABSTRACT_SOCKADDR_UN(&socket_unix, name);
483 fprintf(stderr, "Your system does not support abstract sockets!\n");
489 strncpy(socket_unix.sun_path, buf, sizeof(socket_unix.sun_path));
490 socket_unix_len = LENGTH_OF_SOCKADDR_UN(&socket_unix);
493 if (connect(svr->fd, (struct sockaddr *)&socket_unix, socket_unix_len) < 0) goto error;
494 svr->path = strdup(buf);
495 if (!svr->path) goto error;
497 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
498 _ecore_con_cl_handler, svr, NULL, NULL);
499 if (!svr->fd_handler) goto error;
503 /* we got our server! */
504 Ecore_Con_Event_Server_Add *e;
506 e = calloc(1, sizeof(Ecore_Con_Event_Server_Add));
511 ecore_event_add(ECORE_CON_EVENT_SERVER_ADD, e,
512 _ecore_con_event_server_add_free, NULL);
517 svr->name = strdup(name);
518 if (!svr->name) goto error;
519 svr->type = compl_type;
521 svr->data = (void *)data;
523 svr->reject_excess_clients = 0;
524 svr->client_limit = -1;
525 svr->clients = ecore_list_new();
526 ecore_list_append(servers, svr);
527 ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
529 if (type == ECORE_CON_REMOTE_SYSTEM)
531 if (!ecore_con_dns_lookup(svr->name, _ecore_con_cb_dns_lookup, svr))
538 if (svr->name) free(svr->name);
539 if (svr->path) free(svr->path);
540 if (svr->fd >= 0) close(svr->fd);
541 if (svr->fd_handler) ecore_main_fd_handler_del(svr->fd_handler);
543 if (svr->ssl) SSL_free(svr->ssl);
544 if (svr->ssl_ctx) SSL_CTX_free(svr->ssl_ctx);
551 * Closes the connection and frees the given server.
552 * @param svr The given server.
553 * @return Data associated with the server when it was created.
554 * @ingroup Ecore_Con_Server_Group
557 ecore_con_server_del(Ecore_Con_Server *svr)
561 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
563 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_del");
569 if (svr->event_count > 0)
573 ecore_main_fd_handler_del(svr->fd_handler);
574 svr->fd_handler = NULL;
579 _ecore_con_server_free(svr);
580 if (ecore_list_goto(servers, svr)) ecore_list_remove(servers);
586 * Retrieves the data associated with the given server.
587 * @param svr The given server.
588 * @return The associated data.
589 * @ingroup Ecore_Con_Server_Group
592 ecore_con_server_data_get(Ecore_Con_Server *svr)
594 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
596 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_data_get");
603 * Retrieves whether the given server is currently connected.
604 * @todo Check that this function does what the documenter believes it does.
605 * @param svr The given server.
606 * @return @c 1 if the server is connected. @c 0 otherwise.
607 * @ingroup Ecore_Con_Server_Group
610 ecore_con_server_connected_get(Ecore_Con_Server *svr)
612 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
614 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_connected_get");
617 if (svr->connecting) return 0;
622 * Retrieves the current list of clients.
623 * @param svr The given server.
624 * @return The list of clients on this server.
625 * @ingroup Ecore_Con_Server_Group
628 ecore_con_server_clients_get(Ecore_Con_Server *svr)
630 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
632 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_clients_get");
639 * Sends the given data to the given server.
640 * @param svr The given server.
641 * @param data The given data.
642 * @param size Length of the data, in bytes, to send.
643 * @return The number of bytes sent. @c 0 will be returned if there is an
645 * @ingroup Ecore_Con_Server_Group
648 ecore_con_server_send(Ecore_Con_Server *svr, const void *data, int size)
650 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
652 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_send");
655 if (svr->dead) return 0;
657 if (size < 1) return 0;
659 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
662 unsigned char *newbuf;
664 newbuf = realloc(svr->write_buf, svr->write_buf_size + size);
665 if (newbuf) svr->write_buf = newbuf;
667 memcpy(svr->write_buf + svr->write_buf_size, data, size);
668 svr->write_buf_size += size;
672 svr->write_buf = malloc(size);
673 if (!svr->write_buf) return 0;
674 svr->write_buf_size = size;
675 memcpy(svr->write_buf, data, size);
681 * Sets a limit on the number of clients that can be handled concurrently
682 * by the given server, and a policy on what to do if excess clients try to
684 * Beware that if you set this once ecore is already running, you may
685 * already have pending CLIENT_ADD events in your event queue. Those
686 * clients have already connected and will not be affected by this call.
687 * Only clients subsequently trying to connect will be affected.
688 * @param svr The given server.
689 * @param client_limit The maximum number of clients to handle
690 * concurrently. -1 means unlimited (default). 0
691 * effectively disables the server.
692 * @param reject_excess_clients Set to 1 to automatically disconnect
693 * excess clients as soon as they connect if you are
694 * already handling client_limit clients. Set to 0
695 * (default) to just hold off on the "accept()"
696 * system call until the number of active clients
697 * drops. This causes the kernel to queue up to 4096
698 * connections (or your kernel's limit, whichever is
700 * @ingroup Ecore_Con_Server_Group
703 ecore_con_server_client_limit_set(Ecore_Con_Server *svr, int client_limit, char reject_excess_clients)
705 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
707 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_client_limit_set");
710 svr->client_limit = client_limit;
711 svr->reject_excess_clients = reject_excess_clients;
715 * Gets the IP address of a server that has been connected to.
717 * @param svr The given server.
718 * @return A pointer to an internal string that contains the IP address of
719 * the connected server in the form "XXX.YYY.ZZZ.AAA" IP notation.
720 * This string should not be modified or trusted to stay valid after
721 * deletion for the @p svr object. If no IP is known NULL is returned.
722 * @ingroup Ecore_Con_Server_Group
725 ecore_con_server_ip_get(Ecore_Con_Server *svr)
727 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
729 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_ip_get");
736 * Flushes all pending data to the given server. Will return when done.
738 * @param svr The given server.
739 * @ingroup Ecore_Con_Server_Group
742 ecore_con_server_flush(Ecore_Con_Server *svr)
744 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
746 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_flush");
749 _ecore_con_server_flush(svr);
753 * @defgroup Ecore_Con_Client_Group Ecore Connection Client Functions
755 * Functions that operate on Ecore connection client objects.
759 * Sends the given data to the given client.
760 * @param cl The given client.
761 * @param data The given data.
762 * @param size Length of the data, in bytes, to send.
763 * @return The number of bytes sent. @c 0 will be returned if there is an
765 * @ingroup Ecore_Con_Client_Group
768 ecore_con_client_send(Ecore_Con_Client *cl, const void *data, int size)
770 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
772 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_send");
775 if (cl->dead) return 0;
777 if (size < 1) return 0;
779 ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
782 unsigned char *newbuf;
784 newbuf = realloc(cl->buf, cl->buf_size + size);
785 if (newbuf) cl->buf = newbuf;
787 memcpy(cl->buf + cl->buf_size, data, size);
788 cl->buf_size += size;
792 cl->buf = malloc(size);
793 if (!cl->buf) return 0;
795 memcpy(cl->buf, data, size);
801 * Retrieves the server representing the socket the client has
803 * @param cl The given client.
804 * @return The server that the client connected to.
805 * @ingroup Ecore_Con_Client_Group
807 EAPI Ecore_Con_Server *
808 ecore_con_client_server_get(Ecore_Con_Client *cl)
810 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
812 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_server_get");
819 * Closes the connection and frees memory allocated to the given client.
820 * @param cl The given client.
821 * @return Data associated with the client.
822 * @ingroup Ecore_Con_Client_Group
825 ecore_con_client_del(Ecore_Con_Client *cl)
829 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
831 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_del");
837 if (cl->event_count > 0)
841 ecore_main_fd_handler_del(cl->fd_handler);
842 cl->fd_handler = NULL;
847 if (ecore_list_goto(cl->server->clients, cl))
848 ecore_list_remove(cl->server->clients);
849 _ecore_con_client_free(cl);
855 * Sets the data associated with the given client to @p data.
856 * @param cl The given client.
857 * @param data What to set the data to.
858 * @ingroup Ecore_Con_Client_Group
861 ecore_con_client_data_set(Ecore_Con_Client *cl, const void *data)
863 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
865 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_data_set");
868 cl->data = (void *)data;
872 * Retrieves the data associated with the given client.
873 * @param cl The given client.
874 * @return The data associated with @p cl.
875 * @ingroup Ecore_Con_Client_Group
878 ecore_con_client_data_get(Ecore_Con_Client *cl)
880 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
882 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_data_get");
889 * Gets the IP address of a cleint that has connected.
891 * @param cl The given client.
892 * @return A pointer to an internal string that contains the IP address of
893 * the connected client in the form "XXX.YYY.ZZZ.AAA" IP notation.
894 * This string should not be modified or trusted to stay valid after
895 * deletion for the @p cl object. If no IP is known NULL is returned.
896 * @ingroup Ecore_Con_Client_Group
899 ecore_con_client_ip_get(Ecore_Con_Client *cl)
901 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
903 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_ip_get");
910 * Flushes all pending data to the given client. Will return when done.
912 * @param cl The given client.
913 * @ingroup Ecore_Con_Client_Group
916 ecore_con_client_flush(Ecore_Con_Client *cl)
918 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
920 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_flush");
923 _ecore_con_client_flush(cl);
927 * Returns if SSL support is available
928 * @return 1 if SSL is available, 0 if it is not.
929 * @ingroup Ecore_Con_Client_Group
932 ecore_con_ssl_available_get(void)
942 _ecore_con_server_free(Ecore_Con_Server *svr)
946 ECORE_MAGIC_SET(svr, ECORE_MAGIC_NONE);
947 t_start = ecore_time_get();
948 while ((svr->write_buf) && (!svr->dead))
950 _ecore_con_server_flush(svr);
951 t = ecore_time_get();
952 if ((t - t_start) > 0.5)
954 printf("ECORE_CON: EEK - stuck in _ecore_con_server_free() trying\n"
955 " to flush data out from the server, and have been for\n"
956 " %1.1f seconds. This is taking too long. Aborting flush.\n",
961 if (svr->write_buf) free(svr->write_buf);
962 while (!ecore_list_empty_is(svr->clients))
963 _ecore_con_client_free(ecore_list_first_remove(svr->clients));
964 ecore_list_destroy(svr->clients);
965 if ((svr->created) && (svr->path) && (svr->ppid == getpid()))
967 if (svr->fd >= 0) close(svr->fd);
971 SSL_shutdown(svr->ssl);
974 if (svr->ssl_ctx) SSL_CTX_free(svr->ssl_ctx);
976 if (svr->name) free(svr->name);
977 if (svr->path) free(svr->path);
978 if (svr->ip) free(svr->ip);
979 if (svr->fd_handler) ecore_main_fd_handler_del(svr->fd_handler);
984 _ecore_con_client_free(Ecore_Con_Client *cl)
988 ECORE_MAGIC_SET(cl, ECORE_MAGIC_NONE);
989 t_start = ecore_time_get();
990 while ((cl->buf) && (!cl->dead))
992 _ecore_con_client_flush(cl);
993 t = ecore_time_get();
994 if ((t - t_start) > 0.5)
996 printf("ECORE_CON: EEK - stuck in _ecore_con_client_free() trying\n"
997 " to flush data out from the client, and have been for\n"
998 " %1.1f seconds. This is taking too long. Aborting flush.\n",
1003 if (cl->buf) free(cl->buf);
1004 if (cl->fd >= 0) close(cl->fd);
1005 if (cl->fd_handler) ecore_main_fd_handler_del(cl->fd_handler);
1006 if (cl->ip) free(cl->ip);
1011 _ecore_con_svr_handler(void *data, Ecore_Fd_Handler *fd_handler __UNUSED__)
1013 Ecore_Con_Server *svr;
1015 struct sockaddr_in incoming;
1019 if (svr->dead) return 1;
1020 if (svr->delete_me) return 1;
1021 if ((svr->client_limit >= 0) && (!svr->reject_excess_clients))
1023 if (ecore_list_count(svr->clients) >= svr->client_limit) return 1;
1026 size_in = sizeof(struct sockaddr_in);
1027 new_fd = accept(svr->fd, (struct sockaddr *)&incoming, (socklen_t *)&size_in);
1030 Ecore_Con_Client *cl;
1034 if ((svr->client_limit >= 0) && (svr->reject_excess_clients))
1040 cl = calloc(1, sizeof(Ecore_Con_Client));
1046 fcntl(new_fd, F_SETFL, O_NONBLOCK);
1047 fcntl(new_fd, F_SETFD, FD_CLOEXEC);
1051 ecore_main_fd_handler_add(cl->fd, ECORE_FD_READ,
1052 _ecore_con_svr_cl_handler, cl, NULL, NULL);
1053 ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
1054 ecore_list_append(svr->clients, cl);
1057 ip = incoming.sin_addr.s_addr;
1058 snprintf(buf, sizeof(buf),
1064 cl->ip = strdup(buf);
1068 Ecore_Con_Event_Client_Add *e;
1070 e = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
1075 ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD, e,
1076 _ecore_con_event_client_add_free, NULL);
1084 /* Tries to connect an Ecore_Con_Server to an SSL host.
1085 * Returns 1 on success, -1 on fatal errors and 0 if the caller
1086 * should try again later.
1089 svr_try_connect_ssl(Ecore_Con_Server *svr)
1091 int res, ssl_err, flag = 0;
1093 if (!ssl_init_count)
1096 SSL_load_error_strings();
1100 res = SSL_connect(svr->ssl);
1101 if (res == 1) return 1;
1102 ssl_err = SSL_get_error(svr->ssl, res);
1104 if (ssl_err == SSL_ERROR_NONE) return 1;
1105 if (ssl_err == SSL_ERROR_WANT_READ) flag = ECORE_FD_READ;
1106 else if (ssl_err == SSL_ERROR_WANT_WRITE) flag = ECORE_FD_WRITE;
1108 if (svr->fd_handler)
1110 if (flag) ecore_main_fd_handler_active_set(svr->fd_handler, flag);
1117 kill_server(Ecore_Con_Server *svr)
1119 if (!svr->delete_me)
1121 Ecore_Con_Event_Server_Del *e;
1123 e = calloc(1, sizeof(Ecore_Con_Event_Server_Del));
1128 ecore_event_add(ECORE_CON_EVENT_SERVER_DEL, e,
1129 _ecore_con_event_server_del_free, NULL);
1134 if (svr->fd_handler) ecore_main_fd_handler_del(svr->fd_handler);
1135 svr->fd_handler = NULL;
1139 _ecore_con_cb_dns_lookup(void *data, struct hostent *he)
1141 Ecore_Con_Server *svr;
1142 struct sockaddr_in socket_addr;
1149 if (!he) goto error;
1150 svr->fd = socket(AF_INET, SOCK_STREAM, 0);
1151 if (svr->fd < 0) goto error;
1152 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
1153 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
1154 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, &curstate, sizeof(curstate)) < 0)
1156 socket_addr.sin_family = AF_INET;
1157 socket_addr.sin_port = htons(svr->port);
1158 memcpy((struct in_addr *)&socket_addr.sin_addr,
1159 he->h_addr, sizeof(struct in_addr));
1160 if (connect(svr->fd, (struct sockaddr *)&socket_addr, sizeof(struct sockaddr_in)) < 0)
1162 if (errno != EINPROGRESS)
1164 svr->connecting = 1;
1166 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
1167 _ecore_con_cl_handler, svr, NULL, NULL);
1171 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1172 _ecore_con_cl_handler, svr, NULL, NULL);
1174 if (!svr->fd_handler) goto error;
1175 ip = socket_addr.sin_addr.s_addr;
1176 snprintf(buf, sizeof(buf),
1182 svr->ip = strdup(buf);
1185 if (svr->type & ECORE_CON_SSL)
1187 if (!ssl_init_count)
1190 SSL_load_error_strings();
1194 switch (svr->type & ECORE_CON_SSL)
1196 case ECORE_CON_USE_SSL2:
1197 if (!(svr->ssl_ctx = SSL_CTX_new(SSLv2_client_method())))
1200 case ECORE_CON_USE_SSL3:
1201 if (!(svr->ssl_ctx = SSL_CTX_new(SSLv3_client_method())))
1204 case ECORE_CON_USE_TLS:
1205 if (!(svr->ssl_ctx = SSL_CTX_new(TLSv1_client_method())))
1210 if (!(svr->ssl = SSL_new(svr->ssl_ctx)))
1213 SSL_set_fd(svr->ssl, svr->fd);
1224 svr_try_connect_plain(Ecore_Con_Server *svr)
1227 unsigned int size = sizeof(int);
1229 if (getsockopt(svr->fd, SOL_SOCKET, SO_ERROR, &so_err, &size) < 0)
1234 /* we lost our server! */
1239 if (!svr->delete_me)
1241 /* we got our server! */
1242 Ecore_Con_Event_Server_Add *e;
1244 svr->connecting = 0;
1245 e = calloc(1, sizeof(Ecore_Con_Event_Server_Add));
1250 ecore_event_add(ECORE_CON_EVENT_SERVER_ADD, e,
1251 _ecore_con_event_server_add_free, NULL);
1254 if (svr->fd_handler)
1256 if (!svr->write_buf)
1257 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
1260 return (!svr->dead);
1263 /* returns 1 on success, 0 on failure */
1264 static int svr_try_connect(Ecore_Con_Server *svr)
1270 return svr_try_connect_plain(svr);
1274 switch (svr_try_connect_ssl(svr)) {
1276 return svr_try_connect_plain(svr);
1288 _ecore_con_cl_handler(void *data, Ecore_Fd_Handler *fd_handler)
1290 Ecore_Con_Server *svr;
1292 int ssl_err = SSL_ERROR_NONE;
1296 if (svr->dead) return 1;
1297 if (svr->delete_me) return 1;
1298 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
1300 unsigned char *inbuf = NULL;
1303 if (svr->connecting && !svr_try_connect(svr))
1308 int num, lost_server;
1309 char buf[READBUFSIZ];
1316 if ((num = read(svr->fd, buf, READBUFSIZ)) < 1)
1318 lost_server = ((errno == EIO) ||
1321 (errno == EINVAL) ||
1322 (errno == ENOSPC) ||
1324 /* is num == 0 is right - when the server closes us
1325 * off we will get this (as this is called when select
1326 * tells us there is data to read!)
1333 num = SSL_read(svr->ssl, buf, READBUFSIZ);
1336 ssl_err = SSL_get_error(svr->ssl, num);
1337 lost_server = (ssl_err == SSL_ERROR_ZERO_RETURN);
1338 if (ssl_err == SSL_ERROR_SYSCALL)
1340 if (num == 0) lost_server = 1;
1343 lost_server = ((errno == EIO) ||
1346 (errno == EINVAL) ||
1347 (errno == ENOSPC) ||
1348 (errno == ECONNRESET));
1353 ssl_err = SSL_ERROR_NONE;
1360 if (!svr->delete_me)
1362 Ecore_Con_Event_Server_Data *e;
1364 e = calloc(1, sizeof(Ecore_Con_Event_Server_Data));
1370 e->size = inbuf_num;
1371 ecore_event_add(ECORE_CON_EVENT_SERVER_DATA, e,
1372 _ecore_con_event_server_data_free,
1379 /* we lost our server! */
1387 inbuf = realloc(inbuf, inbuf_num + num);
1388 memcpy(inbuf + inbuf_num, buf, num);
1394 if (svr->fd_handler)
1396 if (svr->ssl && ssl_err == SSL_ERROR_WANT_READ)
1397 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
1398 else if (svr->ssl && ssl_err == SSL_ERROR_WANT_WRITE)
1399 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_WRITE);
1403 else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
1405 if (svr->connecting && !svr_try_connect (svr))
1407 _ecore_con_server_flush(svr);
1414 _ecore_con_svr_cl_handler(void *data, Ecore_Fd_Handler *fd_handler)
1416 Ecore_Con_Client *cl;
1419 if (cl->dead) return 1;
1420 if (cl->delete_me) return 1;
1421 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
1423 unsigned char *inbuf = NULL;
1432 num = read(cl->fd, buf, 65536);
1439 Ecore_Con_Event_Client_Data *e;
1441 e = calloc(1, sizeof(Ecore_Con_Event_Client_Data));
1447 e->size = inbuf_num;
1448 ecore_event_add(ECORE_CON_EVENT_CLIENT_DATA, e,
1449 _ecore_con_event_client_data_free,
1454 if ((errno == EIO) || (errno == EBADF) ||
1455 (errno == EPIPE) || (errno == EINVAL) ||
1456 (errno == ENOSPC) || (num == 0)/* is num == 0 right? */)
1460 /* we lost our client! */
1461 Ecore_Con_Event_Client_Del *e;
1463 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
1468 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
1469 _ecore_con_event_client_del_free,
1475 ecore_main_fd_handler_del(cl->fd_handler);
1476 cl->fd_handler = NULL;
1482 inbuf = realloc(inbuf, inbuf_num + num);
1483 memcpy(inbuf + inbuf_num, buf, num);
1488 else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
1489 _ecore_con_client_flush(cl);
1494 _ecore_con_server_flush(Ecore_Con_Server *svr)
1496 int count, num, lost_server = 0;
1498 int ssl_err = SSL_ERROR_NONE;
1501 if (!svr->write_buf) return;
1503 /* check whether we need to write anything at all.
1504 * we must not write zero bytes with SSL_write() since it
1505 * causes undefined behaviour
1507 if (svr->write_buf_size == svr->write_buf_offset)
1510 num = svr->write_buf_size - svr->write_buf_offset;
1515 count = write(svr->fd, svr->write_buf + svr->write_buf_offset, num);
1517 lost_server = (errno == EIO || errno == EBADF ||
1518 errno == EPIPE || errno == EINVAL || errno == ENOSPC);
1523 count = SSL_write(svr->ssl, svr->write_buf + svr->write_buf_offset, num);
1527 ssl_err = SSL_get_error(svr->ssl, count);
1528 lost_server = (ssl_err == SSL_ERROR_ZERO_RETURN);
1535 /* we lost our server! */
1543 if (svr->fd_handler)
1545 if (svr->ssl && ssl_err == SSL_ERROR_WANT_READ)
1546 ecore_main_fd_handler_active_set(svr->fd_handler,
1548 else if (svr->ssl && ssl_err == SSL_ERROR_WANT_WRITE)
1549 ecore_main_fd_handler_active_set(svr->fd_handler,
1556 svr->write_buf_offset += count;
1557 if (svr->write_buf_offset >= svr->write_buf_size)
1559 svr->write_buf_size = 0;
1560 svr->write_buf_offset = 0;
1561 free(svr->write_buf);
1562 svr->write_buf = NULL;
1563 if (svr->fd_handler)
1564 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
1569 _ecore_con_client_flush(Ecore_Con_Client *cl)
1573 if (!cl->buf) return;
1574 num = cl->buf_size - cl->buf_offset;
1575 count = write(cl->fd, cl->buf + cl->buf_offset, num);
1578 if ((errno == EIO) || (errno == EBADF) || (errno == EPIPE) ||
1579 (errno == EINVAL) || (errno == ENOSPC))
1583 /* we lost our client! */
1584 Ecore_Con_Event_Client_Del *e;
1586 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
1591 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
1592 _ecore_con_event_client_del_free, NULL);
1596 ecore_main_fd_handler_del(cl->fd_handler);
1597 cl->fd_handler = NULL;
1602 cl->buf_offset += count;
1603 if (cl->buf_offset >= cl->buf_size)
1610 ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ);
1615 _ecore_con_event_client_add_free(void *data __UNUSED__, void *ev)
1617 Ecore_Con_Event_Client_Add *e;
1620 e->client->event_count--;
1621 if ((e->client->event_count == 0) && (e->client->delete_me))
1622 ecore_con_client_del(e->client);
1627 _ecore_con_event_client_del_free(void *data __UNUSED__, void *ev)
1629 Ecore_Con_Event_Client_Del *e;
1632 e->client->event_count--;
1633 if ((e->client->event_count == 0) && (e->client->delete_me))
1634 ecore_con_client_del(e->client);
1639 _ecore_con_event_client_data_free(void *data __UNUSED__, void *ev)
1641 Ecore_Con_Event_Client_Data *e;
1644 e->client->event_count--;
1645 if (e->data) free(e->data);
1646 if ((e->client->event_count == 0) && (e->client->delete_me))
1647 ecore_con_client_del(e->client);
1652 _ecore_con_event_server_add_free(void *data __UNUSED__, void *ev)
1654 Ecore_Con_Event_Server_Add *e;
1657 e->server->event_count--;
1658 if ((e->server->event_count == 0) && (e->server->delete_me))
1659 ecore_con_server_del(e->server);
1664 _ecore_con_event_server_del_free(void *data __UNUSED__, void *ev)
1666 Ecore_Con_Event_Server_Del *e;
1669 e->server->event_count--;
1670 if ((e->server->event_count == 0) && (e->server->delete_me))
1671 ecore_con_server_del(e->server);
1676 _ecore_con_event_server_data_free(void *data __UNUSED__, void *ev)
1678 Ecore_Con_Event_Server_Data *e;
1681 e->server->event_count--;
1682 if (e->data) free(e->data);
1683 if ((e->server->event_count == 0) && (e->server->delete_me))
1684 ecore_con_server_del(e->server);