2 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
11 #include <sys/types.h>
17 #ifdef HAVE_ARPA_INET_H
18 # include <arpa/inet.h>
21 #ifdef HAVE_NETINET_TCP_H
22 # include <netinet/tcp.h>
25 #ifdef HAVE_NETINET_IN_H
26 # include <netinet/in.h>
29 #ifdef HAVE_SYS_SOCKET_H
30 # include <sys/socket.h>
37 #ifdef HAVE_WS2TCPIP_H
38 # include <ws2tcpip.h>
46 #include "ecore_private.h"
47 #include "Ecore_Con.h"
48 #include "ecore_con_private.h"
50 static void _ecore_con_cb_tcp_connect(void *data, Ecore_Con_Info *info);
51 static void _ecore_con_cb_udp_connect(void *data, Ecore_Con_Info *info);
52 static void _ecore_con_cb_tcp_listen(void *data, Ecore_Con_Info *info);
53 static void _ecore_con_cb_udp_listen(void *data, Ecore_Con_Info *info);
55 static void _ecore_con_server_free(Ecore_Con_Server *svr);
56 static void _ecore_con_client_free(Ecore_Con_Client *cl);
58 static int _ecore_con_svr_handler(void *data, Ecore_Fd_Handler *fd_handler);
59 static int _ecore_con_cl_handler(void *data, Ecore_Fd_Handler *fd_handler);
60 static int _ecore_con_cl_udp_handler(void *data, Ecore_Fd_Handler *fd_handler);
61 static int _ecore_con_svr_udp_handler(void *data, Ecore_Fd_Handler *fd_handler);
62 static int _ecore_con_svr_cl_handler(void *data, Ecore_Fd_Handler *fd_handler);
64 static void _ecore_con_server_flush(Ecore_Con_Server *svr);
65 static void _ecore_con_client_flush(Ecore_Con_Client *cl);
67 static void _ecore_con_event_client_add_free(void *data, void *ev);
68 static void _ecore_con_event_client_del_free(void *data, void *ev);
69 static void _ecore_con_event_client_data_free(void *data, void *ev);
70 static void _ecore_con_event_server_add_free(void *data, void *ev);
71 static void _ecore_con_event_server_del_free(void *data, void *ev);
72 static void _ecore_con_event_server_data_free(void *data, void *ev);
74 EAPI int ECORE_CON_EVENT_CLIENT_ADD = 0;
75 EAPI int ECORE_CON_EVENT_CLIENT_DEL = 0;
76 EAPI int ECORE_CON_EVENT_SERVER_ADD = 0;
77 EAPI int ECORE_CON_EVENT_SERVER_DEL = 0;
78 EAPI int ECORE_CON_EVENT_CLIENT_DATA = 0;
79 EAPI int ECORE_CON_EVENT_SERVER_DATA = 0;
81 static Eina_List *servers = NULL;
82 static int _ecore_con_init_count = 0;
83 int _ecore_con_log_dom = -1;
86 * @defgroup Ecore_Con_Lib_Group Ecore Connection Library Functions
88 * Utility functions that set up and shut down the Ecore Connection
93 * Initialises the Ecore_Con library.
94 * @return Number of times the library has been initialised without being
96 * @ingroup Ecore_Con_Lib_Group
101 if (++_ecore_con_init_count != 1)
102 return _ecore_con_init_count;
106 return --_ecore_con_init_count;
110 return --_ecore_con_init_count;
112 _ecore_con_log_dom = eina_log_domain_register("EcoreCon", ECORE_CON_DEFAULT_LOG_COLOR);
113 if(_ecore_con_log_dom < 0)
115 EINA_LOG_ERR("Impossible to create a log domain for Ecore Con.");
117 return --_ecore_con_init_count;
119 ECORE_CON_EVENT_CLIENT_ADD = ecore_event_type_new();
120 ECORE_CON_EVENT_CLIENT_DEL = ecore_event_type_new();
121 ECORE_CON_EVENT_SERVER_ADD = ecore_event_type_new();
122 ECORE_CON_EVENT_SERVER_DEL = ecore_event_type_new();
123 ECORE_CON_EVENT_CLIENT_DATA = ecore_event_type_new();
124 ECORE_CON_EVENT_SERVER_DATA = ecore_event_type_new();
126 /* TODO Remember return value, if it fails, use gethostbyname() */
127 ecore_con_ssl_init();
128 ecore_con_info_init();
130 return _ecore_con_init_count;
134 * Shuts down the Ecore_Con library.
135 * @return Number of times the library has been initialised without being
137 * @ingroup Ecore_Con_Lib_Group
140 ecore_con_shutdown(void)
142 if (--_ecore_con_init_count != 0)
143 return _ecore_con_init_count;
146 _ecore_con_server_free(eina_list_data_get(servers));
148 ecore_con_info_shutdown();
149 ecore_con_ssl_shutdown();
150 eina_log_domain_unregister(_ecore_con_log_dom);
151 _ecore_con_log_dom = -1;
157 return _ecore_con_init_count;
161 * @defgroup Ecore_Con_Server_Group Ecore Connection Server Functions
163 * Functions that operate on Ecore server objects.
167 * Creates a server to listen for connections.
169 * The socket on which the server listens depends on the connection
171 * @li If @a compl_type is @c ECORE_CON_LOCAL_USER, the server will listen on
172 * the Unix socket "~/.ecore/[name]/[port]".
173 * @li If @a compl_type is @c ECORE_CON_LOCAL_SYSTEM, the server will listen
174 * on Unix socket "/tmp/.ecore_service|[name]|[port]".
175 * @li If @a compl_type is @c ECORE_CON_REMOTE_TCP, the server will listen
176 * on TCP port @c port.
178 * @param compl_type The connection type.
179 * @param name Name to associate with the socket. It is used when
180 * generating the socket name of a Unix socket. Though
181 * it is not used for the TCP socket, it still needs to
182 * be a valid character array. @c NULL will not be
184 * @param port Number to identify socket. When a Unix socket is used,
185 * it becomes part of the socket name. When a TCP socket
186 * is used, it is used as the TCP port.
187 * @param data Data to associate with the created Ecore_Con_Server
189 * @return A new Ecore_Con_Server.
190 * @ingroup Ecore_Con_Server_Group
192 EAPI Ecore_Con_Server *
193 ecore_con_server_add(Ecore_Con_Type compl_type, const char *name, int port,
196 Ecore_Con_Server *svr;
199 if (port < 0 || !name) return NULL;
200 /* local user socket: FILE: ~/.ecore/[name]/[port] */
201 /* local system socket: FILE: /tmp/.ecore_service|[name]|[port] */
202 /* remote system socket: TCP/IP: [name]:[port] */
203 svr = calloc(1, sizeof(Ecore_Con_Server));
204 if (!svr) return NULL;
206 svr->name = strdup(name);
207 if (!svr->name) goto error;
208 svr->type = compl_type;
210 svr->data = (void *)data;
212 svr->reject_excess_clients = 0;
213 svr->client_limit = -1;
215 svr->ppid = getpid();
216 ecore_con_ssl_server_prepare(svr);
218 type = compl_type & ECORE_CON_TYPE;
220 if ((type == ECORE_CON_LOCAL_USER) ||
221 (type == ECORE_CON_LOCAL_SYSTEM) ||
222 (type == ECORE_CON_LOCAL_ABSTRACT))
225 if (!ecore_con_local_listen(svr, _ecore_con_svr_handler, svr)) goto error;
228 if ((type == ECORE_CON_REMOTE_TCP) ||
229 (type == ECORE_CON_REMOTE_NODELAY))
232 if (!ecore_con_info_tcp_listen(svr, _ecore_con_cb_tcp_listen, svr)) goto error;
234 else if ((type == ECORE_CON_REMOTE_MCAST) ||
235 (type == ECORE_CON_REMOTE_UDP))
238 if (!ecore_con_info_udp_listen(svr, _ecore_con_cb_udp_listen, svr)) goto error;
241 servers = eina_list_append(servers, svr);
242 ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
247 if (svr->name) free(svr->name);
248 if (svr->path) free(svr->path);
250 if (svr->fd >= 0) close(svr->fd);
251 if (svr->fd_handler) ecore_main_fd_handler_del(svr->fd_handler);
252 if (svr->write_buf) free(svr->write_buf);
253 if (svr->ip) free(svr->ip);
255 ecore_con_ssl_server_shutdown(svr);
261 * Creates a server object to represent the server listening at the
264 * The socket to which the server connects depends on the connection type:
265 * @li If @a compl_type is @c ECORE_CON_LOCAL_USER, the function will
266 * connect to the server listening on the Unix socket
267 * "~/.ecore/[name]/[port]".
268 * @li If @a compl_type is @c ECORE_CON_LOCAL_SYSTEM, the function will
269 * connect to the server listening on the Unix socket
270 * "/tmp/.ecore_service|[name]|[port]".
271 * @li If @a compl_type is @c ECORE_CON_REMOTE_TCP, the function will
272 * connect to the server listening on the TCP port "[name]:[port]".
274 * @param compl_type The connection type.
275 * @param name Name used when determining what socket to connect to.
276 * It is used to generate the socket name when the socket
277 * is a Unix socket. It is used as the hostname when
278 * connecting with a TCP socket.
279 * @param port Number to identify the socket to connect to. Used when
280 * generating the socket name for a Unix socket, or as the
281 * TCP port when connecting to a TCP socket.
282 * @param data Data to associate with the created Ecore_Con_Server
284 * @return A new Ecore_Con_Server.
285 * @ingroup Ecore_Con_Server_Group
287 EAPI Ecore_Con_Server *
288 ecore_con_server_connect(Ecore_Con_Type compl_type, const char *name, int port,
291 Ecore_Con_Server *svr;
294 if (!name) return NULL;
295 /* local user socket: FILE: ~/.ecore/[name]/[port] */
296 /* local system socket: FILE: /tmp/.ecore_service|[name]|[port] */
297 /* remote system socket: TCP/IP: [name]:[port] */
298 svr = calloc(1, sizeof(Ecore_Con_Server));
299 if (!svr) return NULL;
301 svr->name = strdup(name);
302 if (!svr->name) goto error;
303 svr->type = compl_type;
305 svr->data = (void *)data;
307 svr->reject_excess_clients = 0;
309 svr->client_limit = -1;
310 ecore_con_ssl_server_prepare(svr);
312 type = compl_type & ECORE_CON_TYPE;
314 if (((type == ECORE_CON_REMOTE_TCP) ||
315 (type == ECORE_CON_REMOTE_NODELAY) ||
316 (type == ECORE_CON_REMOTE_UDP) ||
317 (type == ECORE_CON_REMOTE_BROADCAST)) &&
321 if ((type == ECORE_CON_LOCAL_USER) ||
322 (type == ECORE_CON_LOCAL_SYSTEM) ||
323 (type == ECORE_CON_LOCAL_ABSTRACT))
326 if (!ecore_con_local_connect(svr, _ecore_con_cl_handler, svr, _ecore_con_event_server_add_free)) goto error;
329 if ((type == ECORE_CON_REMOTE_TCP) ||
330 (type == ECORE_CON_REMOTE_NODELAY))
333 if (!ecore_con_info_tcp_connect(svr, _ecore_con_cb_tcp_connect, svr)) goto error;
335 else if ((type == ECORE_CON_REMOTE_UDP) ||
336 (type == ECORE_CON_REMOTE_BROADCAST))
339 if (!ecore_con_info_udp_connect(svr, _ecore_con_cb_udp_connect, svr)) goto error;
342 servers = eina_list_append(servers, svr);
343 ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
348 if (svr->name) free(svr->name);
349 if (svr->path) free(svr->path);
350 if (svr->fd >= 0) close(svr->fd);
351 if (svr->fd_handler) ecore_main_fd_handler_del(svr->fd_handler);
352 ecore_con_ssl_server_shutdown(svr);
358 * Closes the connection and frees the given server.
359 * @param svr The given server.
360 * @return Data associated with the server when it was created.
361 * @ingroup Ecore_Con_Server_Group
364 ecore_con_server_del(Ecore_Con_Server *svr)
368 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
370 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_del");
373 if (svr->delete_me) return NULL;
378 if (svr->event_count > 0)
382 ecore_main_fd_handler_del(svr->fd_handler);
383 svr->fd_handler = NULL;
388 _ecore_con_server_free(svr);
394 * Retrieves the data associated with the given server.
395 * @param svr The given server.
396 * @return The associated data.
397 * @ingroup Ecore_Con_Server_Group
400 ecore_con_server_data_get(Ecore_Con_Server *svr)
402 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
404 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_data_get");
411 * Retrieves whether the given server is currently connected.
412 * @todo Check that this function does what the documenter believes it does.
413 * @param svr The given server.
414 * @return @c 1 if the server is connected. @c 0 otherwise.
415 * @ingroup Ecore_Con_Server_Group
418 ecore_con_server_connected_get(Ecore_Con_Server *svr)
420 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
422 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_connected_get");
425 if (svr->connecting) return 0;
430 * Retrieves the current list of clients.
431 * @param svr The given server.
432 * @return The list of clients on this server.
433 * @ingroup Ecore_Con_Server_Group
436 ecore_con_server_clients_get(Ecore_Con_Server *svr)
438 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
440 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_clients_get");
447 * Sends the given data to the given server.
448 * @param svr The given server.
449 * @param data The given data.
450 * @param size Length of the data, in bytes, to send.
451 * @return The number of bytes sent. @c 0 will be returned if there is an
453 * @ingroup Ecore_Con_Server_Group
456 ecore_con_server_send(Ecore_Con_Server *svr, const void *data, int size)
458 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
460 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_send");
463 if (svr->dead) return 0;
465 if (size < 1) return 0;
467 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
470 unsigned char *newbuf;
472 newbuf = realloc(svr->write_buf, svr->write_buf_size + size);
473 if (newbuf) svr->write_buf = newbuf;
475 memcpy(svr->write_buf + svr->write_buf_size, data, size);
476 svr->write_buf_size += size;
480 svr->write_buf = malloc(size);
481 if (!svr->write_buf) return 0;
482 svr->write_buf_size = size;
483 memcpy(svr->write_buf, data, size);
489 * Sets a limit on the number of clients that can be handled concurrently
490 * by the given server, and a policy on what to do if excess clients try to
492 * Beware that if you set this once ecore is already running, you may
493 * already have pending CLIENT_ADD events in your event queue. Those
494 * clients have already connected and will not be affected by this call.
495 * Only clients subsequently trying to connect will be affected.
496 * @param svr The given server.
497 * @param client_limit The maximum number of clients to handle
498 * concurrently. -1 means unlimited (default). 0
499 * effectively disables the server.
500 * @param reject_excess_clients Set to 1 to automatically disconnect
501 * excess clients as soon as they connect if you are
502 * already handling client_limit clients. Set to 0
503 * (default) to just hold off on the "accept()"
504 * system call until the number of active clients
505 * drops. This causes the kernel to queue up to 4096
506 * connections (or your kernel's limit, whichever is
508 * @ingroup Ecore_Con_Server_Group
511 ecore_con_server_client_limit_set(Ecore_Con_Server *svr, int client_limit, char reject_excess_clients)
513 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
515 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_client_limit_set");
518 svr->client_limit = client_limit;
519 svr->reject_excess_clients = reject_excess_clients;
523 * Gets the IP address of a server that has been connected to.
525 * @param svr The given server.
526 * @return A pointer to an internal string that contains the IP address of
527 * the connected server in the form "XXX.YYY.ZZZ.AAA" IP notation.
528 * This string should not be modified or trusted to stay valid after
529 * deletion for the @p svr object. If no IP is known NULL is returned.
530 * @ingroup Ecore_Con_Server_Group
533 ecore_con_server_ip_get(Ecore_Con_Server *svr)
535 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
537 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_ip_get");
544 * Flushes all pending data to the given server. Will return when done.
546 * @param svr The given server.
547 * @ingroup Ecore_Con_Server_Group
550 ecore_con_server_flush(Ecore_Con_Server *svr)
552 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
554 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_flush");
557 _ecore_con_server_flush(svr);
561 * @defgroup Ecore_Con_Client_Group Ecore Connection Client Functions
563 * Functions that operate on Ecore connection client objects.
567 * Sends the given data to the given client.
568 * @param cl The given client.
569 * @param data The given data.
570 * @param size Length of the data, in bytes, to send.
571 * @return The number of bytes sent. @c 0 will be returned if there is an
573 * @ingroup Ecore_Con_Client_Group
576 ecore_con_client_send(Ecore_Con_Client *cl, const void *data, int size)
578 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
580 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_send");
583 if (cl->dead) return 0;
585 if (size < 1) return 0;
587 ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
589 if(cl->server && cl->server->type == ECORE_CON_REMOTE_UDP)
591 sendto(cl->server->fd, data, size, 0, (struct sockaddr *) cl->client_addr, cl->client_addr_len);
595 unsigned char *newbuf;
597 newbuf = realloc(cl->buf, cl->buf_size + size);
598 if (newbuf) cl->buf = newbuf;
600 memcpy(cl->buf + cl->buf_size, data, size);
601 cl->buf_size += size;
605 cl->buf = malloc(size);
606 if (!cl->buf) return 0;
608 memcpy(cl->buf, data, size);
614 * Retrieves the server representing the socket the client has
616 * @param cl The given client.
617 * @return The server that the client connected to.
618 * @ingroup Ecore_Con_Client_Group
620 EAPI Ecore_Con_Server *
621 ecore_con_client_server_get(Ecore_Con_Client *cl)
623 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
625 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_server_get");
632 * Closes the connection and frees memory allocated to the given client.
633 * @param cl The given client.
634 * @return Data associated with the client.
635 * @ingroup Ecore_Con_Client_Group
638 ecore_con_client_del(Ecore_Con_Client *cl)
642 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
644 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_del");
648 if(cl->client_addr && cl->server && (cl->server->type == ECORE_CON_REMOTE_UDP ||
649 cl->server->type == ECORE_CON_REMOTE_MCAST))
650 free(cl->client_addr);
656 if (cl->event_count > 0)
660 ecore_main_fd_handler_del(cl->fd_handler);
661 cl->fd_handler = NULL;
667 cl->server->clients = eina_list_remove(cl->server->clients, cl);
668 _ecore_con_client_free(cl);
674 * Sets the data associated with the given client to @p data.
675 * @param cl The given client.
676 * @param data What to set the data to.
677 * @ingroup Ecore_Con_Client_Group
680 ecore_con_client_data_set(Ecore_Con_Client *cl, const void *data)
682 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
684 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_data_set");
687 cl->data = (void *)data;
691 * Retrieves the data associated with the given client.
692 * @param cl The given client.
693 * @return The data associated with @p cl.
694 * @ingroup Ecore_Con_Client_Group
697 ecore_con_client_data_get(Ecore_Con_Client *cl)
699 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
701 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_data_get");
708 * Gets the IP address of a cleint that has connected.
710 * @param cl The given client.
711 * @return A pointer to an internal string that contains the IP address of
712 * the connected client in the form "XXX.YYY.ZZZ.AAA" IP notation.
713 * This string should not be modified or trusted to stay valid after
714 * deletion for the @p cl object. If no IP is known NULL is returned.
715 * @ingroup Ecore_Con_Client_Group
718 ecore_con_client_ip_get(Ecore_Con_Client *cl)
720 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
722 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_ip_get");
729 * Flushes all pending data to the given client. Will return when done.
731 * @param cl The given client.
732 * @ingroup Ecore_Con_Client_Group
735 ecore_con_client_flush(Ecore_Con_Client *cl)
737 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
739 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_flush");
742 _ecore_con_client_flush(cl);
746 _ecore_con_server_free(Ecore_Con_Server *svr)
748 Ecore_Con_Client *cl;
751 ECORE_MAGIC_SET(svr, ECORE_MAGIC_NONE);
752 t_start = ecore_time_get();
753 while ((svr->write_buf) && (!svr->dead))
755 _ecore_con_server_flush(svr);
756 t = ecore_time_get();
757 if ((t - t_start) > 0.5)
759 WRN("ECORE_CON: EEK - stuck in _ecore_con_server_free() trying\n"
760 " to flush data out from the server, and have been for\n"
761 " %1.1f seconds. This is taking too long. Aborting flush.",
766 if (svr->write_buf) free(svr->write_buf);
767 EINA_LIST_FREE(svr->clients, cl)
768 _ecore_con_client_free(cl);
769 if ((svr->created) && (svr->path) && (svr->ppid == getpid()))
771 if (svr->fd >= 0) close(svr->fd);
772 ecore_con_ssl_server_shutdown(svr);
773 if (svr->name) free(svr->name);
774 if (svr->path) free(svr->path);
775 if (svr->ip) free(svr->ip);
776 if (svr->fd_handler) ecore_main_fd_handler_del(svr->fd_handler);
777 servers = eina_list_remove(servers, svr);
782 _ecore_con_client_free(Ecore_Con_Client *cl)
786 ECORE_MAGIC_SET(cl, ECORE_MAGIC_NONE);
787 t_start = ecore_time_get();
788 while ((cl->buf) && (!cl->dead))
790 _ecore_con_client_flush(cl);
791 t = ecore_time_get();
792 if ((t - t_start) > 0.5)
794 WRN("EEK - stuck in _ecore_con_client_free() trying\n"
795 " to flush data out from the client, and have been for\n"
796 " %1.1f seconds. This is taking too long. Aborting flush.",
801 if (cl->buf) free(cl->buf);
802 if (cl->fd >= 0) close(cl->fd);
803 if (cl->fd_handler) ecore_main_fd_handler_del(cl->fd_handler);
804 if (cl->ip) free(cl->ip);
809 kill_server(Ecore_Con_Server *svr)
813 Ecore_Con_Event_Server_Del *e;
815 e = calloc(1, sizeof(Ecore_Con_Event_Server_Del));
820 ecore_event_add(ECORE_CON_EVENT_SERVER_DEL, e,
821 _ecore_con_event_server_del_free, NULL);
826 if (svr->fd_handler) ecore_main_fd_handler_del(svr->fd_handler);
827 svr->fd_handler = NULL;
831 _ecore_con_cb_tcp_listen(void *data, Ecore_Con_Info *net_info)
833 Ecore_Con_Server *svr;
838 if(!net_info) goto error;
840 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype, net_info->info.ai_protocol);
841 if (svr->fd < 0) goto error;
842 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
843 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
846 if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, (const void *)&lin, sizeof(struct linger)) < 0) goto error;
847 if (svr->type == ECORE_CON_REMOTE_NODELAY)
851 if (setsockopt(svr->fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) < 0)
854 if (bind(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0) goto error;
855 if (listen(svr->fd, 4096) < 0) goto error;
857 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
858 _ecore_con_svr_handler, svr, NULL, NULL);
859 if (!svr->fd_handler) goto error;
864 ecore_con_ssl_server_shutdown(svr);
869 _ecore_con_cb_udp_listen(void *data, Ecore_Con_Info *net_info)
871 Ecore_Con_Server *svr;
874 struct ipv6_mreq mreq6;
879 type &= ECORE_CON_TYPE;
881 if (!net_info) goto error;
883 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype, net_info->info.ai_protocol);
884 if(svr->fd < 0) goto error;
886 if (type == ECORE_CON_REMOTE_MCAST)
888 if (net_info->info.ai_family == AF_INET)
890 if (!inet_pton(net_info->info.ai_family, net_info->ip, &mreq.imr_multiaddr)) goto error;
891 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
892 if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq,sizeof(mreq)) != 0) goto error;
894 else if (net_info->info.ai_family == AF_INET6)
896 if (!inet_pton(net_info->info.ai_family, net_info->ip, &mreq6.ipv6mr_multiaddr)) goto error;
897 mreq6.ipv6mr_interface = htonl(INADDR_ANY);
898 if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq6,sizeof(mreq6)) != 0) goto error;
900 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on, sizeof(on)) != 0) goto error;
903 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
904 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
905 if (bind(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0) goto error;
907 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
908 _ecore_con_svr_udp_handler, svr, NULL, NULL);
909 if (!svr->fd_handler) goto error;
910 svr->ip = strdup(net_info->ip);
915 ecore_con_ssl_server_shutdown(svr);
920 _ecore_con_cb_tcp_connect(void *data, Ecore_Con_Info *net_info)
922 Ecore_Con_Server *svr;
928 if (!net_info) goto error;
929 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype, net_info->info.ai_protocol);
930 if (svr->fd < 0) goto error;
931 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
932 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
933 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&curstate, sizeof(curstate)) < 0)
935 if (svr->type == ECORE_CON_REMOTE_NODELAY)
939 if (setsockopt(svr->fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) < 0)
942 res = connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen);
944 if (res == SOCKET_ERROR)
946 if (WSAGetLastError() != WSAEINPROGRESS)
951 if (errno != EINPROGRESS)
956 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
957 _ecore_con_cl_handler, svr, NULL, NULL);
961 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
962 _ecore_con_cl_handler, svr, NULL, NULL);
964 if (svr->type & ECORE_CON_SSL)
965 if (ecore_con_ssl_server_init(svr))
968 if (!svr->fd_handler) goto error;
969 svr->ip = strdup(net_info->ip);
974 ecore_con_ssl_server_shutdown(svr);
979 _ecore_con_cb_udp_connect(void *data, Ecore_Con_Info *net_info)
981 Ecore_Con_Server *svr;
986 if (!net_info) goto error;
987 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype, net_info->info.ai_protocol);
988 if (svr->fd < 0) goto error;
989 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
990 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
992 if(svr->type == ECORE_CON_REMOTE_BROADCAST)
994 if (setsockopt(svr->fd, SOL_SOCKET, SO_BROADCAST, (const void *)&broadcast, sizeof(broadcast)) < 0) goto error;
998 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&curstate, sizeof(curstate)) < 0) goto error;
1001 if (connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0)
1005 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
1006 _ecore_con_cl_udp_handler, svr, NULL, NULL);
1007 if (!svr->fd_handler) goto error;
1008 svr->ip = strdup(net_info->ip);
1013 ecore_con_ssl_server_shutdown(svr);
1017 static Ecore_Con_State
1018 svr_try_connect_plain(Ecore_Con_Server *svr)
1022 unsigned int size = sizeof(int);
1024 res = getsockopt(svr->fd, SOL_SOCKET, SO_ERROR, (void *)&so_err, &size);
1026 if (res == SOCKET_ERROR)
1029 if (so_err == WSAEINPROGRESS && !svr->dead)
1030 return ECORE_CON_INPROGRESS;
1035 if (so_err == EINPROGRESS && !svr->dead)
1036 return ECORE_CON_INPROGRESS;
1041 /* we lost our server! */
1043 return ECORE_CON_DISCONNECTED;
1047 if (!svr->delete_me)
1049 /* we got our server! */
1050 Ecore_Con_Event_Server_Add *e;
1052 svr->connecting = 0;
1053 e = calloc(1, sizeof(Ecore_Con_Event_Server_Add));
1058 ecore_event_add(ECORE_CON_EVENT_SERVER_ADD, e,
1059 _ecore_con_event_server_add_free, NULL);
1062 if (svr->fd_handler)
1064 if (!svr->write_buf)
1065 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
1070 return ECORE_CON_CONNECTED;
1072 return ECORE_CON_DISCONNECTED;
1075 /* returns 1 on success, 0 on failure */
1076 static Ecore_Con_State svr_try_connect(Ecore_Con_Server *svr)
1078 if (!(svr->type & ECORE_CON_SSL))
1079 return svr_try_connect_plain(svr);
1082 switch (ecore_con_ssl_server_try(svr)) {
1083 case ECORE_CON_CONNECTED:
1084 return svr_try_connect_plain(svr);
1085 case ECORE_CON_DISCONNECTED:
1087 return ECORE_CON_DISCONNECTED;
1089 return ECORE_CON_INPROGRESS;
1095 _ecore_con_pretty_ip(struct sockaddr *client_addr, socklen_t size)
1097 char ipbuf[INET6_ADDRSTRLEN + 1];
1099 /* show v4mapped address in pretty form */
1100 if (client_addr->sa_family == AF_INET6)
1102 struct sockaddr_in6 *sa6;
1104 sa6 = (struct sockaddr_in6 *) client_addr;
1105 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr))
1107 snprintf(ipbuf, sizeof (ipbuf), "%u.%u.%u.%u",
1108 sa6->sin6_addr.s6_addr[12],
1109 sa6->sin6_addr.s6_addr[13],
1110 sa6->sin6_addr.s6_addr[14],
1111 sa6->sin6_addr.s6_addr[15]);
1112 return strdup(ipbuf);
1116 if (getnameinfo(client_addr, size,
1117 ipbuf, sizeof (ipbuf), NULL, 0, NI_NUMERICHOST))
1118 return strdup("0.0.0.0");
1120 ipbuf[sizeof (ipbuf) - 1] = 0;
1121 return strdup(ipbuf);
1125 _ecore_con_svr_handler(void *data, Ecore_Fd_Handler *fd_handler __UNUSED__)
1127 Ecore_Con_Server *svr;
1129 unsigned char incoming[256];
1133 if (svr->dead) return 1;
1134 if (svr->delete_me) return 1;
1135 if ((svr->client_limit >= 0) && (!svr->reject_excess_clients))
1137 if (eina_list_count(svr->clients) >= (unsigned int)svr->client_limit)
1141 size_in = sizeof(incoming);
1143 memset(&incoming, 0, size_in);
1144 new_fd = accept(svr->fd, (struct sockaddr *)&incoming, (socklen_t *)&size_in);
1147 Ecore_Con_Client *cl;
1149 if ((svr->client_limit >= 0) && (svr->reject_excess_clients))
1151 if (eina_list_count(svr->clients) >= (unsigned int)svr->client_limit)
1158 cl = calloc(1, sizeof(Ecore_Con_Client));
1165 fcntl(new_fd, F_SETFL, O_NONBLOCK);
1166 fcntl(new_fd, F_SETFD, FD_CLOEXEC);
1170 if ((svr->type & ECORE_CON_SSL) &&
1171 (ecore_con_ssl_client_init(cl)))
1174 ecore_con_ssl_client_shutdown(cl);
1179 ecore_main_fd_handler_add(cl->fd, ECORE_FD_READ,
1180 _ecore_con_svr_cl_handler, cl, NULL, NULL);
1181 ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
1182 svr->clients = eina_list_append(svr->clients, cl);
1184 cl->ip = _ecore_con_pretty_ip((struct sockaddr *) &incoming, size_in);
1187 Ecore_Con_Event_Client_Add *e;
1189 e = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
1194 ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD, e,
1195 _ecore_con_event_client_add_free, NULL);
1203 _ecore_con_cl_handler(void *data, Ecore_Fd_Handler *fd_handler)
1205 Ecore_Con_Server *svr;
1208 if (svr->dead) return 1;
1209 if (svr->delete_me) return 1;
1210 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
1212 unsigned char *inbuf = NULL;
1215 if (svr->connecting && (svr_try_connect(svr) != ECORE_CON_CONNECTED))
1221 int lost_server = 1;
1222 unsigned char buf[READBUFSIZ];
1224 if (!(svr->type & ECORE_CON_SSL))
1226 if (((num = read(svr->fd, buf, READBUFSIZ)) < 0) &&
1231 if (!(num = ecore_con_ssl_server_read(svr, buf, READBUFSIZ)))
1236 if (inbuf && !svr->delete_me)
1238 Ecore_Con_Event_Server_Data *e;
1240 e = calloc(1, sizeof(Ecore_Con_Event_Server_Data));
1246 e->size = inbuf_num;
1247 ecore_event_add(ECORE_CON_EVENT_SERVER_DATA, e,
1248 _ecore_con_event_server_data_free,
1252 if (lost_server) kill_server(svr);
1256 inbuf = realloc(inbuf, inbuf_num + num);
1257 memcpy(inbuf + inbuf_num, buf, num);
1261 /* #if USE_OPENSSL */
1262 /* if (svr->fd_handler) */
1264 /* if (svr->ssl && ssl_err == SSL_ERROR_WANT_READ) */
1265 /* ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ); */
1266 /* else if (svr->ssl && ssl_err == SSL_ERROR_WANT_WRITE) */
1267 /* ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_WRITE); */
1271 else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
1273 if (svr->connecting && !svr_try_connect (svr))
1275 _ecore_con_server_flush(svr);
1282 _ecore_con_cl_udp_handler(void *data, Ecore_Fd_Handler *fd_handler)
1284 Ecore_Con_Server *svr;
1287 if (svr->dead) return 1;
1288 if (svr->delete_me) return 1;
1289 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
1291 unsigned char buf[65536];
1295 num = read(svr->fd, buf, 65536);
1298 if (!svr->delete_me)
1300 Ecore_Con_Event_Server_Data *e;
1301 unsigned char *inbuf;
1303 inbuf = malloc(num);
1306 memcpy(inbuf, buf, num);
1308 e = calloc(1, sizeof(Ecore_Con_Event_Server_Data));
1315 ecore_event_add(ECORE_CON_EVENT_SERVER_DATA, e,
1316 _ecore_con_event_server_data_free,
1322 else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
1323 _ecore_con_server_flush(svr);
1329 _ecore_con_svr_udp_handler(void *data, Ecore_Fd_Handler *fd_handler)
1331 Ecore_Con_Server *svr;
1332 Ecore_Con_Client *cl = NULL;
1335 if (svr->dead) return 1;
1336 if (svr->delete_me) return 1;
1337 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
1339 unsigned char buf[READBUFSIZ];
1340 unsigned char client_addr[256];
1341 unsigned int client_addr_len = sizeof(client_addr);
1346 num = fcntl(svr->fd, F_SETFL, O_NONBLOCK);
1348 num = recvfrom(svr->fd, buf, sizeof(buf), 0, (struct sockaddr*) &client_addr, &client_addr_len);
1350 num = recvfrom(svr->fd, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr*) &client_addr, &client_addr_len);
1355 if (!svr->delete_me)
1357 Ecore_Con_Event_Client_Data *e;
1358 unsigned char *inbuf;
1360 /* Create a new client for use in the client data event */
1361 cl = calloc(1, sizeof(Ecore_Con_Client));
1366 cl->fd_handler = NULL;
1368 cl->client_addr = calloc(1, client_addr_len);
1369 cl->client_addr_len = client_addr_len;
1370 if(cl->client_addr == NULL)
1375 memcpy(cl->client_addr, &client_addr, client_addr_len);
1376 ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
1377 svr->clients = eina_list_append(svr->clients, cl);
1379 cl->ip = _ecore_con_pretty_ip(cl->client_addr, cl->client_addr_len);
1381 inbuf = malloc(num);
1384 free(cl->client_addr);
1389 memcpy(inbuf, buf, num);
1391 e = calloc(1, sizeof(Ecore_Con_Event_Client_Data));
1398 ecore_event_add(ECORE_CON_EVENT_CLIENT_DATA, e,
1399 _ecore_con_event_client_data_free,
1405 Ecore_Con_Event_Client_Add *add;
1407 add = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
1410 /*cl->event_count++;*/
1412 ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD, add,
1413 _ecore_con_event_client_add_free, NULL);
1417 if ((errno == EIO) || (errno == EBADF) ||
1418 (errno == EPIPE) || (errno == EINVAL) ||
1419 (errno == ENOSPC) || (num == 0)/* is num == 0 right? */)
1421 if (!svr->delete_me)
1423 /* we lost our client! */
1424 Ecore_Con_Event_Client_Del *e;
1426 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
1431 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
1432 _ecore_con_event_client_del_free,
1437 if (svr->fd_handler)
1438 ecore_main_fd_handler_del(svr->fd_handler);
1439 svr->fd_handler = NULL;
1443 else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
1444 _ecore_con_client_flush(cl);
1449 _ecore_con_svr_cl_handler(void *data, Ecore_Fd_Handler *fd_handler)
1451 Ecore_Con_Client *cl;
1454 if (cl->dead) return 1;
1455 if (cl->delete_me) return 1;
1456 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
1458 unsigned char *inbuf = NULL;
1460 int lost_client = 1;
1464 unsigned char buf[65536];
1469 if (!(cl->server->type & ECORE_CON_SSL))
1471 if (((num = read(cl->fd, buf, 65536)) < 0) &&
1476 if (!(num = ecore_con_ssl_client_read(cl, buf, 65536)))
1481 if (inbuf && !cl->delete_me)
1483 Ecore_Con_Event_Client_Data *e;
1485 e = calloc(1, sizeof(Ecore_Con_Event_Client_Data));
1491 e->size = inbuf_num;
1492 ecore_event_add(ECORE_CON_EVENT_CLIENT_DATA, e,
1493 _ecore_con_event_client_data_free,
1502 /* we lost our client! */
1503 Ecore_Con_Event_Client_Del *e;
1505 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
1510 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
1511 _ecore_con_event_client_del_free,
1517 ecore_main_fd_handler_del(cl->fd_handler);
1518 cl->fd_handler = NULL;
1524 inbuf = realloc(inbuf, inbuf_num + num);
1525 memcpy(inbuf + inbuf_num, buf, num);
1530 else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
1531 _ecore_con_client_flush(cl);
1536 _ecore_con_server_flush(Ecore_Con_Server *svr)
1540 if (!svr->write_buf) return;
1542 /* check whether we need to write anything at all.
1543 * we must not write zero bytes with SSL_write() since it
1544 * causes undefined behaviour
1546 if (svr->write_buf_size == svr->write_buf_offset)
1549 num = svr->write_buf_size - svr->write_buf_offset;
1551 if (!(svr->type & ECORE_CON_SSL))
1552 count = write(svr->fd, svr->write_buf + svr->write_buf_offset, num);
1554 count = ecore_con_ssl_server_write(svr, svr->write_buf + svr->write_buf_offset, num);
1558 /* we lost our server! */
1563 svr->write_buf_offset += count;
1564 if (svr->write_buf_offset >= svr->write_buf_size)
1566 svr->write_buf_size = 0;
1567 svr->write_buf_offset = 0;
1568 free(svr->write_buf);
1569 svr->write_buf = NULL;
1570 if (svr->fd_handler)
1571 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
1576 _ecore_con_client_flush(Ecore_Con_Client *cl)
1580 if (!cl->buf) return;
1581 num = cl->buf_size - cl->buf_offset;
1582 if (!(cl->server->type & ECORE_CON_SSL))
1583 count = write(cl->fd, cl->buf + cl->buf_offset, num);
1585 count = ecore_con_ssl_client_write(cl, cl->buf + cl->buf_offset, num);
1588 if ((errno == EIO) || (errno == EBADF) || (errno == EPIPE) ||
1589 (errno == EINVAL) || (errno == ENOSPC))
1593 /* we lost our client! */
1594 Ecore_Con_Event_Client_Del *e;
1596 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
1601 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
1602 _ecore_con_event_client_del_free, NULL);
1606 ecore_main_fd_handler_del(cl->fd_handler);
1607 cl->fd_handler = NULL;
1612 cl->buf_offset += count;
1613 if (cl->buf_offset >= cl->buf_size)
1620 ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ);
1625 _ecore_con_event_client_add_free(void *data __UNUSED__, void *ev)
1627 Ecore_Con_Event_Client_Add *e;
1630 e->client->event_count--;
1631 if ((e->client->event_count == 0) && (e->client->delete_me))
1632 ecore_con_client_del(e->client);
1637 _ecore_con_event_client_del_free(void *data __UNUSED__, void *ev)
1639 Ecore_Con_Event_Client_Del *e;
1642 e->client->event_count--;
1643 if ((e->client->event_count == 0) && (e->client->delete_me))
1644 ecore_con_client_del(e->client);
1649 _ecore_con_event_client_data_free(void *data __UNUSED__, void *ev)
1651 Ecore_Con_Event_Client_Data *e;
1654 e->client->event_count--;
1655 if (e->data) free(e->data);
1656 if (((e->client->event_count == 0) && (e->client->delete_me)) ||
1657 ((e->client->server && (e->client->server->type == ECORE_CON_REMOTE_UDP ||
1658 e->client->server->type == ECORE_CON_REMOTE_MCAST))))
1659 ecore_con_client_del(e->client);
1664 _ecore_con_event_server_add_free(void *data __UNUSED__, void *ev)
1666 Ecore_Con_Event_Server_Add *e;
1669 e->server->event_count--;
1670 if ((e->server->event_count == 0) && (e->server->delete_me))
1671 _ecore_con_server_free(e->server);
1676 _ecore_con_event_server_del_free(void *data __UNUSED__, void *ev)
1678 Ecore_Con_Event_Server_Del *e;
1681 e->server->event_count--;
1682 if ((e->server->event_count == 0) && (e->server->delete_me))
1683 _ecore_con_server_free(e->server);
1688 _ecore_con_event_server_data_free(void *data __UNUSED__, void *ev)
1690 Ecore_Con_Event_Server_Data *e;
1693 e->server->event_count--;
1694 if (e->data) free(e->data);
1695 if ((e->server->event_count == 0) && (e->server->delete_me))
1696 _ecore_con_server_free(e->server);