13 #ifdef HAVE_ARPA_INET_H
14 # include <arpa/inet.h>
17 #ifdef HAVE_NETINET_TCP_H
18 # include <netinet/tcp.h>
21 #ifdef HAVE_NETINET_IN_H
22 # include <netinet/in.h>
25 #ifdef HAVE_SYS_SOCKET_H
26 # include <sys/socket.h>
33 #ifdef HAVE_WS2TCPIP_H
34 # include <ws2tcpip.h>
42 #include "ecore_private.h"
43 #include "Ecore_Con.h"
44 #include "ecore_con_private.h"
46 static Eina_Bool _ecore_con_client_timer(Ecore_Con_Client *cl);
47 static void _ecore_con_cl_timer_update(Ecore_Con_Client *cl);
49 static void _ecore_con_cb_tcp_connect(void *data,
50 Ecore_Con_Info *info);
51 static void _ecore_con_cb_udp_connect(void *data,
52 Ecore_Con_Info *info);
53 static void _ecore_con_cb_tcp_listen(void *data,
54 Ecore_Con_Info *info);
55 static void _ecore_con_cb_udp_listen(void *data,
56 Ecore_Con_Info *info);
58 static void _ecore_con_server_free(Ecore_Con_Server *svr);
59 static void _ecore_con_client_free(Ecore_Con_Client *cl);
61 static void _ecore_con_cl_read(Ecore_Con_Server *svr);
62 static Eina_Bool _ecore_con_svr_tcp_handler(void *data,
63 Ecore_Fd_Handler *fd_handler);
64 static Eina_Bool _ecore_con_cl_handler(void *data,
65 Ecore_Fd_Handler *fd_handler);
66 static Eina_Bool _ecore_con_cl_udp_handler(void *data,
67 Ecore_Fd_Handler *fd_handler);
68 static Eina_Bool _ecore_con_svr_udp_handler(void *data,
69 Ecore_Fd_Handler *fd_handler);
71 static void _ecore_con_svr_cl_read(Ecore_Con_Client *cl);
72 static Eina_Bool _ecore_con_svr_cl_handler(void *data,
73 Ecore_Fd_Handler *fd_handler);
75 static void _ecore_con_server_flush(Ecore_Con_Server *svr);
76 static void _ecore_con_client_flush(Ecore_Con_Client *cl);
78 static void _ecore_con_event_client_add_free(Ecore_Con_Server *svr,
80 static void _ecore_con_event_client_del_free(Ecore_Con_Server *svr,
82 static void _ecore_con_event_client_data_free(Ecore_Con_Server *svr,
84 static void _ecore_con_event_server_add_free(void *data,
86 static void _ecore_con_event_server_del_free(void *data,
88 static void _ecore_con_event_server_data_free(void *data,
90 static void _ecore_con_event_server_error_free(void *data,
91 Ecore_Con_Event_Server_Error *e);
92 static void _ecore_con_event_client_error_free(Ecore_Con_Server *svr,
93 Ecore_Con_Event_Client_Error *e);
95 static void _ecore_con_lookup_done(void *data,
96 Ecore_Con_Info *infos);
99 _ecore_con_pretty_ip(struct sockaddr *client_addr,
102 EAPI int ECORE_CON_EVENT_CLIENT_ADD = 0;
103 EAPI int ECORE_CON_EVENT_CLIENT_DEL = 0;
104 EAPI int ECORE_CON_EVENT_SERVER_ADD = 0;
105 EAPI int ECORE_CON_EVENT_SERVER_DEL = 0;
106 EAPI int ECORE_CON_EVENT_CLIENT_DATA = 0;
107 EAPI int ECORE_CON_EVENT_SERVER_DATA = 0;
108 EAPI int ECORE_CON_EVENT_CLIENT_ERROR = 0;
109 EAPI int ECORE_CON_EVENT_SERVER_ERROR = 0;
111 static Eina_List *servers = NULL;
112 static int _ecore_con_init_count = 0;
113 int _ecore_con_log_dom = -1;
116 * @addtogroup Ecore_Con_Lib_Group Ecore Connection Library Functions
118 * Utility functions that set up and shut down the Ecore Connection
124 * Initialises the Ecore_Con library.
125 * @return Number of times the library has been initialised without being
131 if (++_ecore_con_init_count != 1)
132 return _ecore_con_init_count;
136 return --_ecore_con_init_count;
141 return --_ecore_con_init_count;
143 _ecore_con_log_dom = eina_log_domain_register
144 ("ecore_con", ECORE_CON_DEFAULT_LOG_COLOR);
145 if (_ecore_con_log_dom < 0)
147 EINA_LOG_ERR("Impossible to create a log domain for Ecore Con.");
149 return --_ecore_con_init_count;
152 ECORE_CON_EVENT_CLIENT_ADD = ecore_event_type_new();
153 ECORE_CON_EVENT_CLIENT_DEL = ecore_event_type_new();
154 ECORE_CON_EVENT_SERVER_ADD = ecore_event_type_new();
155 ECORE_CON_EVENT_SERVER_DEL = ecore_event_type_new();
156 ECORE_CON_EVENT_CLIENT_DATA = ecore_event_type_new();
157 ECORE_CON_EVENT_SERVER_DATA = ecore_event_type_new();
158 ECORE_CON_EVENT_CLIENT_ERROR = ecore_event_type_new();
159 ECORE_CON_EVENT_SERVER_ERROR = ecore_event_type_new();
162 eina_magic_string_set(ECORE_MAGIC_CON_SERVER, "Ecore_Con_Server");
163 eina_magic_string_set(ECORE_MAGIC_CON_CLIENT, "Ecore_Con_Server");
164 eina_magic_string_set(ECORE_MAGIC_CON_URL, "Ecore_Con_Url");
166 /* TODO Remember return value, if it fails, use gethostbyname() */
167 ecore_con_ssl_init();
168 ecore_con_info_init();
170 return _ecore_con_init_count;
174 * Shuts down the Ecore_Con library.
175 * @return Number of times the library has been initialised without being
179 ecore_con_shutdown(void)
182 Ecore_Con_Server *svr;
184 if (--_ecore_con_init_count != 0)
185 return _ecore_con_init_count;
187 EINA_LIST_FOREACH_SAFE(servers, l, l2, svr)
188 _ecore_con_server_free(svr);
190 ecore_con_info_shutdown();
191 ecore_con_ssl_shutdown();
192 eina_log_domain_unregister(_ecore_con_log_dom);
193 _ecore_con_log_dom = -1;
199 return _ecore_con_init_count;
203 * Do an asynchronous DNS lookup.
205 * This function performs a DNS lookup on the hostname specified by @p name, then
206 * calls @p done_cb with
208 * @param name IP address or server name to translate.
209 * @param done_cb Callback to notify when done.
210 * @param data User data to be given to done_cb.
211 * @return EINA_TRUE if the request did not fail to be set up, EINA_FALSE if it failed.
214 ecore_con_lookup(const char *name,
215 Ecore_Con_Dns_Cb done_cb,
218 Ecore_Con_Server *svr;
219 Ecore_Con_Lookup *lk;
220 struct addrinfo hints;
222 if (!name || !done_cb)
225 svr = calloc(1, sizeof(Ecore_Con_Server));
229 lk = malloc(sizeof (Ecore_Con_Lookup));
236 lk->done_cb = done_cb;
239 svr->name = strdup(name);
243 svr->type = ECORE_CON_REMOTE_TCP;
246 svr->created = EINA_TRUE;
247 svr->reject_excess_clients = EINA_FALSE;
248 svr->client_limit = -1;
250 svr->ppid = getpid();
252 memset(&hints, 0, sizeof(struct addrinfo));
253 hints.ai_family = AF_INET6;
254 hints.ai_socktype = SOCK_STREAM;
255 hints.ai_flags = AI_CANONNAME;
256 hints.ai_protocol = IPPROTO_TCP;
257 hints.ai_canonname = NULL;
258 hints.ai_next = NULL;
259 hints.ai_addr = NULL;
261 if (ecore_con_info_get(svr, _ecore_con_lookup_done, svr,
277 * @addtogroup Ecore_Con_Server_Group Ecore Connection Server Functions
279 * Functions that operate on Ecore server objects.
285 * @example ecore_con_server_example.c
286 * Shows how to write a simple server using the Ecore_Con library.
290 * Creates a server to listen for connections.
292 * The socket on which the server listens depends on the connection
294 * @li If @a compl_type is @c ECORE_CON_LOCAL_USER, the server will listen on
295 * the Unix socket "~/.ecore/[name]/[port]".
296 * @li If @a compl_type is @c ECORE_CON_LOCAL_SYSTEM, the server will listen
297 * on Unix socket "/tmp/.ecore_service|[name]|[port]".
298 * @li If @a compl_type is @c ECORE_CON_REMOTE_TCP, the server will listen
299 * on TCP port @c port.
301 * @param compl_type The connection type.
302 * @param name Name to associate with the socket. It is used when
303 * generating the socket name of a Unix socket, or for
304 * determining what host to listen on for TCP sockets.
305 * @c NULL will not be accepted.
306 * @param port Number to identify socket. When a Unix socket is used,
307 * it becomes part of the socket name. When a TCP socket
308 * is used, it is used as the TCP port.
309 * @param data Data to associate with the created Ecore_Con_Server
311 * @return A new Ecore_Con_Server.
313 EAPI Ecore_Con_Server *
314 ecore_con_server_add(Ecore_Con_Type compl_type,
319 Ecore_Con_Server *svr;
322 if (port < 0 || !name)
323 return NULL; /* local user socket: FILE: ~/.ecore/[name]/[port] */
325 /* local system socket: FILE: /tmp/.ecore_service|[name]|[port] */
326 /* remote system socket: TCP/IP: [name]:[port] */
327 svr = calloc(1, sizeof(Ecore_Con_Server));
331 svr->name = strdup(name);
335 svr->type = compl_type;
337 svr->data = (void *)data;
338 svr->created = EINA_TRUE;
339 if (compl_type & ECORE_CON_LOAD_CERT)
340 svr->use_cert = EINA_TRUE;
341 svr->reject_excess_clients = EINA_FALSE;
342 svr->client_limit = -1;
344 svr->ppid = getpid();
345 if (ecore_con_ssl_server_prepare(svr, compl_type & ECORE_CON_SSL))
348 type = compl_type & ECORE_CON_TYPE;
350 if ((type == ECORE_CON_LOCAL_USER) ||
351 (type == ECORE_CON_LOCAL_SYSTEM) ||
352 (type == ECORE_CON_LOCAL_ABSTRACT))
355 if (!ecore_con_local_listen(svr))
358 if (!ecore_con_local_listen(svr, _ecore_con_svr_tcp_handler, svr))
362 if ((type == ECORE_CON_REMOTE_TCP) ||
363 (type == ECORE_CON_REMOTE_NODELAY))
366 if (!ecore_con_info_tcp_listen(svr, _ecore_con_cb_tcp_listen,
370 else if ((type == ECORE_CON_REMOTE_MCAST) ||
371 (type == ECORE_CON_REMOTE_UDP))
373 if (!ecore_con_info_udp_listen(svr, _ecore_con_cb_udp_listen,
377 servers = eina_list_append(servers, svr);
378 ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
391 ecore_main_fd_handler_del(svr->fd_handler);
397 free(svr->write_buf);
400 eina_stringshare_del(svr->ip);
402 ecore_con_ssl_server_shutdown(svr);
408 * Creates a connection to the specified server and returns an associated object.
410 * The socket to which the connection is made depends on the connection type:
411 * @li If @a compl_type is @c ECORE_CON_LOCAL_USER, the function will
412 * connect to the server at the Unix socket
413 * "~/.ecore/[name]/[port]".
414 * @li If @a compl_type is @c ECORE_CON_LOCAL_SYSTEM, the function will
415 * connect to the server at the Unix socket
416 * "/tmp/.ecore_service|[name]|[port]".
417 * @li If @a compl_type is @c ECORE_CON_REMOTE_TCP, the function will
418 * connect to the server at the TCP port "[name]:[port]".
420 * @param compl_type The connection type.
421 * @param name Name used when determining what socket to connect to.
422 * It is used to generate the socket name when the socket
423 * is a Unix socket. It is used as the hostname when
424 * connecting with a TCP socket.
425 * @param port Number to identify the socket to connect to. Used when
426 * generating the socket name for a Unix socket, or as the
427 * TCP port when connecting to a TCP socket.
428 * @param data Data to associate with the created Ecore_Con_Server
430 * @return A new Ecore_Con_Server.
432 EAPI Ecore_Con_Server *
433 ecore_con_server_connect(Ecore_Con_Type compl_type,
438 Ecore_Con_Server *svr;
441 if ((!name) || (!name[0]))
443 /* local user socket: FILE: ~/.ecore/[name]/[port] */
444 /* local system socket: FILE: /tmp/.ecore_service|[name]|[port] */
445 /* remote system socket: TCP/IP: [name]:[port] */
446 svr = calloc(1, sizeof(Ecore_Con_Server));
450 svr->name = strdup(name);
454 svr->type = compl_type;
456 svr->data = (void *)data;
457 svr->created = EINA_FALSE;
458 svr->use_cert = (compl_type & ECORE_CON_LOAD_CERT);
459 svr->reject_excess_clients = EINA_FALSE;
461 svr->client_limit = -1;
462 if (ecore_con_ssl_server_prepare(svr, compl_type & ECORE_CON_SSL))
465 type = compl_type & ECORE_CON_TYPE;
467 if (((type == ECORE_CON_REMOTE_TCP) ||
468 (type == ECORE_CON_REMOTE_NODELAY) ||
469 (type == ECORE_CON_REMOTE_UDP) ||
470 (type == ECORE_CON_REMOTE_BROADCAST)) &&
474 if ((type == ECORE_CON_LOCAL_USER) ||
475 (type == ECORE_CON_LOCAL_SYSTEM) ||
476 (type == ECORE_CON_LOCAL_ABSTRACT))
479 if (!ecore_con_local_connect(svr, _ecore_con_cl_handler,
480 _ecore_con_event_server_add_free))
484 if (!ecore_con_local_connect(svr, _ecore_con_cl_handler, svr,
485 _ecore_con_event_server_add_free))
490 if ((type == ECORE_CON_REMOTE_TCP) ||
491 (type == ECORE_CON_REMOTE_NODELAY))
494 if (!ecore_con_info_tcp_connect(svr, _ecore_con_cb_tcp_connect,
498 else if ((type == ECORE_CON_REMOTE_UDP) ||
499 (type == ECORE_CON_REMOTE_BROADCAST))
501 if (!ecore_con_info_udp_connect(svr, _ecore_con_cb_udp_connect,
505 servers = eina_list_append(servers, svr);
506 ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
518 ecore_main_fd_handler_del(svr->fd_handler);
523 ecore_con_ssl_server_shutdown(svr);
529 * Set the default time after which an inactive client will be disconnected
530 * @param svr The server object
531 * @param timeout The timeout, in seconds, to disconnect after
532 * This function is used to set the idle timeout on clients. A value of < 1
533 * disables the idle timeout.
536 ecore_con_server_timeout_set(Ecore_Con_Server *svr,
539 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
541 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_timeout_set");
545 svr->client_disconnect_time = timeout;
549 * Get the default time after which an inactive client will be disconnected
550 * @param svr The server object
551 * @return The timeout, in seconds, to disconnect after
552 * This function is used to get the idle timeout for clients. A value of < 1
553 * means the idle timeout is disabled.
556 ecore_con_server_timeout_get(Ecore_Con_Server *svr)
558 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
560 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_timeout_get");
564 return svr->client_disconnect_time;
568 * Closes the connection and frees the given server.
569 * @param svr The given server.
570 * @return Data associated with the server when it was created.
571 * @see ecore_con_server_add, ecore_con_server_connect
574 ecore_con_server_del(Ecore_Con_Server *svr)
578 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
580 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_del");
588 svr->delete_me = EINA_TRUE;
589 if (svr->event_count > 0)
593 ecore_main_fd_handler_del(svr->fd_handler);
594 svr->fd_handler = NULL;
598 _ecore_con_server_free(svr);
604 * Retrieves the data associated with the given server.
605 * @param svr The given server.
606 * @return The associated data.
609 ecore_con_server_data_get(Ecore_Con_Server *svr)
611 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
613 ECORE_MAGIC_FAIL(svr,
614 ECORE_MAGIC_CON_SERVER,
615 "ecore_con_server_data_get");
623 * Sets the data associated with the given server.
624 * @param svr The given server.
625 * @param data The data to associate with @p svr
626 * @return The previously associated data, if any.
629 ecore_con_server_data_set(Ecore_Con_Server *svr,
634 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
636 ECORE_MAGIC_FAIL(svr,
637 ECORE_MAGIC_CON_SERVER,
638 "ecore_con_server_data_get");
648 * Retrieves whether the given server is currently connected.
649 * @param svr The given server.
650 * @return #EINA_TRUE if the server is connected. #EINA_FALSE otherwise.
653 ecore_con_server_connected_get(Ecore_Con_Server *svr)
655 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
657 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
658 "ecore_con_server_connected_get");
669 * Retrieves the current list of clients.
670 * @param svr The given server.
671 * @return The list of clients on this server.
674 ecore_con_server_clients_get(Ecore_Con_Server *svr)
676 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
678 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
679 "ecore_con_server_clients_get");
687 * Retrieves the name of server.
688 * @param svr The given server.
689 * @return The name of the server.
692 ecore_con_server_name_get(Ecore_Con_Server *svr)
694 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
696 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
697 "ecore_con_server_name_get");
705 * Retrieves the server port in use.
706 * @param svr The given server.
707 * @return The server port in use.
710 ecore_con_server_port_get(Ecore_Con_Server *svr)
712 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
714 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
715 "ecore_con_server_port_get");
722 * Sends the given data to the given server.
723 * @param svr The given server.
724 * @param data The given data.
725 * @param size Length of the data, in bytes, to send.
726 * @return The number of bytes sent. @c 0 will be returned if there is an
730 ecore_con_server_send(Ecore_Con_Server *svr,
734 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
736 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_send");
740 EINA_SAFETY_ON_TRUE_RETURN_VAL(svr->dead, 0);
742 EINA_SAFETY_ON_NULL_RETURN_VAL(data, 0);
744 EINA_SAFETY_ON_TRUE_RETURN_VAL(size < 1, 0);
747 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
751 unsigned char *newbuf;
753 newbuf = realloc(svr->write_buf, svr->write_buf_size + size);
754 EINA_SAFETY_ON_NULL_RETURN_VAL(newbuf, 0);
756 svr->write_buf = newbuf;
757 memcpy(svr->write_buf + svr->write_buf_size, data, size);
758 svr->write_buf_size += size;
762 svr->write_buf = malloc(size);
763 EINA_SAFETY_ON_NULL_RETURN_VAL(svr->write_buf, 0);
765 svr->write_buf_size = size;
766 memcpy(svr->write_buf, data, size);
773 * Sets a limit on the number of clients that can be handled concurrently
774 * by the given server, and a policy on what to do if excess clients try to
776 * Beware that if you set this once ecore is already running, you may
777 * already have pending CLIENT_ADD events in your event queue. Those
778 * clients have already connected and will not be affected by this call.
779 * Only clients subsequently trying to connect will be affected.
780 * @param svr The given server.
781 * @param client_limit The maximum number of clients to handle
782 * concurrently. -1 means unlimited (default). 0
783 * effectively disables the server.
784 * @param reject_excess_clients Set to 1 to automatically disconnect
785 * excess clients as soon as they connect if you are
786 * already handling client_limit clients. Set to 0
787 * (default) to just hold off on the "accept()"
788 * system call until the number of active clients
789 * drops. This causes the kernel to queue up to 4096
790 * connections (or your kernel's limit, whichever is
794 ecore_con_server_client_limit_set(Ecore_Con_Server *svr,
796 char reject_excess_clients)
798 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
800 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
801 "ecore_con_server_client_limit_set");
805 svr->client_limit = client_limit;
806 svr->reject_excess_clients = reject_excess_clients;
810 * Gets the IP address of a server that has been connected to.
812 * @param svr The given server.
813 * @return A pointer to an internal string that contains the IP address of
814 * the connected server in the form "XXX.YYY.ZZZ.AAA" IP notation.
815 * This string should not be modified or trusted to stay valid after
816 * deletion for the @p svr object. If no IP is known NULL is returned.
819 ecore_con_server_ip_get(Ecore_Con_Server *svr)
821 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
823 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_ip_get");
831 * @brief Check how long a server has been connected
832 * @param svr The server to check
833 * @return The total time, in seconds, that the server has been connected/running
834 * This function is used to find out how long a server has been connected/running for.
837 ecore_con_server_uptime_get(Ecore_Con_Server *svr)
839 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
841 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_uptime_get");
845 return ecore_time_get() - svr->start_time;
849 * Flushes all pending data to the given server. Will return when done.
851 * @param svr The given server.
854 ecore_con_server_flush(Ecore_Con_Server *svr)
856 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
858 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_flush");
862 _ecore_con_server_flush(svr);
870 * @addtogroup Ecore_Con_Client_Group Ecore Connection Client Functions
872 * Functions that operate on Ecore connection client objects.
878 * @example ecore_con_client_example.c
879 * Shows how to write a simple client that connects to the example server.
883 * Sends the given data to the given client.
884 * @param cl The given client.
885 * @param data The given data.
886 * @param size Length of the data, in bytes, to send.
887 * @return The number of bytes sent. @c 0 will be returned if there is an
891 ecore_con_client_send(Ecore_Con_Client *cl,
895 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
897 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_send");
901 EINA_SAFETY_ON_TRUE_RETURN_VAL(cl->dead, 0);
903 EINA_SAFETY_ON_NULL_RETURN_VAL(data, 0);
905 EINA_SAFETY_ON_TRUE_RETURN_VAL(size < 1, 0);
908 ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
910 if (cl->host_server && ((cl->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_UDP))
911 sendto(cl->host_server->fd, data, size, 0, (struct sockaddr *)cl->client_addr,
912 cl->client_addr_len);
915 unsigned char *newbuf;
917 newbuf = realloc(cl->buf, cl->buf_size + size);
918 EINA_SAFETY_ON_NULL_RETURN_VAL(newbuf, 0);
922 memcpy(cl->buf + cl->buf_size, data, size);
923 cl->buf_size += size;
927 cl->buf = malloc(size);
928 EINA_SAFETY_ON_NULL_RETURN_VAL(cl->buf, 0);
931 memcpy(cl->buf, data, size);
938 * Retrieves the server representing the socket the client has
940 * @param cl The given client.
941 * @return The server that the client connected to.
943 EAPI Ecore_Con_Server *
944 ecore_con_client_server_get(Ecore_Con_Client *cl)
946 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
948 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
949 "ecore_con_client_server_get");
953 return cl->host_server;
957 * Returns whether the client is still connected
958 * @param cl The given client.
959 * @return #EINA_TRUE if connected, else EINA_FALSE
962 ecore_con_client_connected_get(Ecore_Con_Client *cl)
964 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
966 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
967 "ecore_con_client_connected_get");
975 * Set the time after which the client will be disconnected when inactive
976 * @param cl The client object
977 * @param timeout The timeout, in seconds, to disconnect after
978 * This function is used to set the idle timeout on a client. A value of < 1
979 * disables the idle timeout.
982 ecore_con_client_timeout_set(Ecore_Con_Client *cl,
985 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
987 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
988 "ecore_con_client_timeout_set");
992 cl->disconnect_time = timeout;
994 _ecore_con_cl_timer_update(cl);
998 * Get the default time after which the client will be disconnected when inactive
999 * @param cl The client object
1000 * @return The timeout, in seconds, to disconnect after
1001 * This function is used to get the idle timeout for a client. A value of < 1
1002 * means the idle timeout is disabled.
1005 ecore_con_client_timeout_get(Ecore_Con_Client *cl)
1007 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1009 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_timeout_get");
1013 return cl->disconnect_time;
1017 * Closes the connection and frees memory allocated to the given client.
1018 * @param cl The given client.
1019 * @return Data associated with the client.
1022 ecore_con_client_del(Ecore_Con_Client *cl)
1026 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1028 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_del");
1033 cl->delete_me = EINA_TRUE;
1034 if (cl->event_count > 0)
1038 ecore_main_fd_handler_del(cl->fd_handler);
1039 cl->fd_handler = NULL;
1044 if (cl->host_server)
1046 cl->host_server->clients = eina_list_remove(cl->host_server->clients, cl);
1047 --cl->host_server->client_count;
1050 _ecore_con_client_free(cl);
1057 * Sets the data associated with the given client to @p data.
1058 * @param cl The given client.
1059 * @param data What to set the data to.
1062 ecore_con_client_data_set(Ecore_Con_Client *cl,
1065 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1067 ECORE_MAGIC_FAIL(cl,
1068 ECORE_MAGIC_CON_CLIENT,
1069 "ecore_con_client_data_set");
1073 cl->data = (void *)data;
1077 * Retrieves the data associated with the given client.
1078 * @param cl The given client.
1079 * @return The data associated with @p cl.
1082 ecore_con_client_data_get(Ecore_Con_Client *cl)
1084 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1086 ECORE_MAGIC_FAIL(cl,
1087 ECORE_MAGIC_CON_CLIENT,
1088 "ecore_con_client_data_get");
1096 * Gets the IP address of a client that has connected.
1098 * @param cl The given client.
1099 * @return A pointer to an internal string that contains the IP address of
1100 * the connected client in the form "XXX.YYY.ZZZ.AAA" IP notation.
1101 * This string should not be modified or trusted to stay valid after
1102 * deletion for the @p cl object. If no IP is known NULL is returned.
1105 ecore_con_client_ip_get(Ecore_Con_Client *cl)
1107 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1109 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_ip_get");
1113 cl->ip = _ecore_con_pretty_ip(cl->client_addr, cl->client_addr_len);
1119 * @brief Return the port that the client has connected to
1120 * @param cl The client
1121 * @return The port that @p cl has connected to, or -1 on error
1122 * Use this function to return the port on which a given client has connected.
1125 ecore_con_client_port_get(Ecore_Con_Client *cl)
1127 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1129 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_port_get");
1132 if (cl->client_addr->sa_family == AF_INET)
1133 return ((struct sockaddr_in*)cl->client_addr)->sin_port;
1134 return ((struct sockaddr_in6*)cl->client_addr)->sin6_port;
1138 * @brief Check how long a client has been connected
1139 * @param cl The client to check
1140 * @return The total time, in seconds, that the client has been connected to the server
1141 * This function is used to find out how long a client has been connected for.
1144 ecore_con_client_uptime_get(Ecore_Con_Client *cl)
1146 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1148 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_uptime_get");
1152 return ecore_time_get() - cl->start_time;
1156 * Flushes all pending data to the given client. Will return when done.
1158 * @param cl The given client.
1161 ecore_con_client_flush(Ecore_Con_Client *cl)
1163 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1165 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_flush");
1169 _ecore_con_client_flush(cl);
1177 ecore_con_event_server_add(Ecore_Con_Server *svr)
1179 /* we got our server! */
1180 Ecore_Con_Event_Server_Add *e;
1182 e = calloc(1, sizeof(Ecore_Con_Event_Server_Add));
1183 EINA_SAFETY_ON_NULL_RETURN(e);
1187 ecore_event_add(ECORE_CON_EVENT_SERVER_ADD, e,
1188 _ecore_con_event_server_add_free, NULL);
1192 ecore_con_event_server_del(Ecore_Con_Server *svr)
1194 Ecore_Con_Event_Server_Del *e;
1196 e = calloc(1, sizeof(Ecore_Con_Event_Server_Del));
1197 EINA_SAFETY_ON_NULL_RETURN(e);
1201 ecore_event_add(ECORE_CON_EVENT_SERVER_DEL, e,
1202 _ecore_con_event_server_del_free, NULL);
1206 ecore_con_event_server_data(Ecore_Con_Server *svr, unsigned char *buf, int num, Eina_Bool duplicate)
1208 Ecore_Con_Event_Server_Data *e;
1210 e = malloc(sizeof(Ecore_Con_Event_Server_Data));
1211 EINA_SAFETY_ON_NULL_RETURN(e);
1217 e->data = malloc(num);
1224 memcpy(e->data, buf, num);
1229 ecore_event_add(ECORE_CON_EVENT_SERVER_DATA, e,
1230 _ecore_con_event_server_data_free, NULL);
1234 ecore_con_event_client_add(Ecore_Con_Client *cl)
1236 Ecore_Con_Event_Client_Add *e;
1238 e = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
1239 EINA_SAFETY_ON_NULL_RETURN(e);
1242 cl->host_server->event_count++;
1243 _ecore_con_cl_timer_update(cl);
1245 ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD, e,
1246 (Ecore_End_Cb)_ecore_con_event_client_add_free, cl->host_server);
1251 ecore_con_event_client_del(Ecore_Con_Client *cl)
1253 Ecore_Con_Event_Client_Del *e;
1255 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
1256 EINA_SAFETY_ON_NULL_RETURN(e);
1261 cl->host_server->event_count++;
1262 _ecore_con_cl_timer_update(cl);
1265 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
1266 (Ecore_End_Cb)_ecore_con_event_client_del_free, cl->host_server);
1270 ecore_con_event_client_data(Ecore_Con_Client *cl, unsigned char *buf, int num, Eina_Bool duplicate)
1272 Ecore_Con_Event_Client_Data *e;
1273 e = malloc(sizeof(Ecore_Con_Event_Client_Data));
1274 EINA_SAFETY_ON_NULL_RETURN(e);
1276 cl->host_server->event_count++;
1278 _ecore_con_cl_timer_update(cl);
1282 e->data = malloc(num);
1285 free(cl->client_addr);
1289 memcpy(e->data, buf, num);
1294 ecore_event_add(ECORE_CON_EVENT_CLIENT_DATA, e,
1295 (Ecore_End_Cb)_ecore_con_event_client_data_free, cl->host_server);
1300 ecore_con_server_infos_del(Ecore_Con_Server *svr, void *info)
1302 svr->infos = eina_list_remove(svr->infos, info);
1306 ecore_con_event_server_error(Ecore_Con_Server *svr, const char *error)
1308 Ecore_Con_Event_Server_Error *e;
1310 e = calloc(1, sizeof(Ecore_Con_Event_Server_Error));
1311 EINA_SAFETY_ON_NULL_RETURN(e);
1314 e->error = strdup(error);
1317 ecore_event_add(ECORE_CON_EVENT_SERVER_ERROR, e, (Ecore_End_Cb)_ecore_con_event_server_error_free, NULL);
1321 ecore_con_event_client_error(Ecore_Con_Client *cl, const char *error)
1323 Ecore_Con_Event_Client_Error *e;
1325 e = calloc(1, sizeof(Ecore_Con_Event_Client_Error));
1326 EINA_SAFETY_ON_NULL_RETURN(e);
1329 e->error = strdup(error);
1332 cl->host_server->event_count++;
1333 ecore_event_add(ECORE_CON_EVENT_CLIENT_ERROR, e, (Ecore_End_Cb)_ecore_con_event_client_error_free, cl->host_server);
1337 _ecore_con_server_free(Ecore_Con_Server *svr)
1339 Ecore_Con_Client *cl;
1344 ecore_con_info_data_clear(svr->infos->data);
1345 svr->infos = eina_list_remove_list(svr->infos, svr->infos);
1347 if ((!svr->write_buf) && svr->delete_me && (!svr->dead) && (svr->event_count < 1))
1349 /* this is a catch-all for cases when a server is not properly killed. */
1350 svr->dead = EINA_TRUE;
1351 ecore_con_event_server_del(svr);
1355 if (svr->event_count > 0)
1357 ECORE_MAGIC_SET(svr, ECORE_MAGIC_NONE);
1358 t_start = ecore_time_get();
1359 while ((svr->write_buf) && (!svr->dead))
1361 _ecore_con_server_flush(svr);
1362 t = ecore_time_get();
1363 if ((t - t_start) > 0.5)
1365 WRN("ECORE_CON: EEK - stuck in _ecore_con_server_free() trying\n"
1366 " to flush data out from the server, and have been for\n"
1367 " %1.1f seconds. This is taking too long. Aborting flush.",
1374 ecore_con_local_win32_server_del(svr);
1378 free(svr->write_buf);
1380 EINA_LIST_FREE(svr->clients, cl)
1381 _ecore_con_client_free(cl);
1382 if ((svr->created) && (svr->path) && (svr->ppid == getpid()))
1385 ecore_con_ssl_server_shutdown(svr);
1393 eina_stringshare_del(svr->ip);
1395 if (svr->fd_handler)
1396 ecore_main_fd_handler_del(svr->fd_handler);
1401 servers = eina_list_remove(servers, svr);
1407 _ecore_con_client_free(Ecore_Con_Client *cl)
1411 if (cl->event_count > 0)
1414 if (cl->delete_me && (!cl->dead) && (cl->event_count < 1))
1416 /* this is a catch-all for cases when a client is not properly killed. */
1417 cl->dead = EINA_TRUE;
1418 ecore_con_event_client_del(cl);
1423 ECORE_MAGIC_SET(cl, ECORE_MAGIC_NONE);
1424 t_start = ecore_time_get();
1425 while ((cl->buf) && (!cl->dead))
1427 _ecore_con_client_flush(cl);
1428 t = ecore_time_get();
1429 if ((t - t_start) > 0.5)
1431 WRN("EEK - stuck in _ecore_con_client_free() trying\n"
1432 " to flush data out from the client, and have been for\n"
1433 " %1.1f seconds. This is taking too long. Aborting flush.",
1440 ecore_con_local_win32_client_del(cl);
1446 if (cl->host_server->type & ECORE_CON_SSL)
1447 ecore_con_ssl_client_shutdown(cl);
1450 ecore_main_fd_handler_del(cl->fd_handler);
1455 if (cl->client_addr)
1456 free(cl->client_addr);
1457 cl->client_addr = NULL;
1460 eina_stringshare_del(cl->ip);
1467 _ecore_con_server_kill(Ecore_Con_Server *svr)
1469 if (!svr->delete_me)
1470 ecore_con_event_server_del(svr);
1472 svr->dead = EINA_TRUE;
1473 if (svr->fd_handler)
1474 ecore_main_fd_handler_del(svr->fd_handler);
1476 svr->fd_handler = NULL;
1480 _ecore_con_client_timer(Ecore_Con_Client *cl)
1482 ecore_con_client_del(cl);
1484 return ECORE_CALLBACK_CANCEL;
1488 _ecore_con_cl_timer_update(Ecore_Con_Client *cl)
1490 if (cl->disconnect_time)
1492 if (cl->disconnect_time > 0)
1494 if (cl->until_deletion)
1495 ecore_timer_interval_set(cl->until_deletion, cl->disconnect_time);
1497 cl->until_deletion = ecore_timer_add(cl->disconnect_time, (Ecore_Task_Cb)_ecore_con_client_timer, cl);
1499 else if (cl->until_deletion)
1501 ecore_timer_del(cl->until_deletion);
1502 cl->until_deletion = NULL;
1507 if (cl->host_server->client_disconnect_time > 0)
1509 if (cl->until_deletion)
1510 ecore_timer_interval_set(cl->until_deletion, cl->host_server->client_disconnect_time);
1512 cl->until_deletion = ecore_timer_add(cl->host_server->client_disconnect_time, (Ecore_Task_Cb)_ecore_con_client_timer, cl);
1514 else if (cl->until_deletion)
1516 ecore_timer_del(cl->until_deletion);
1517 cl->until_deletion = NULL;
1523 _ecore_con_cb_tcp_listen(void *data,
1524 Ecore_Con_Info *net_info)
1526 Ecore_Con_Server *svr;
1531 if (!net_info) /* error message has already been handled */
1534 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1535 net_info->info.ai_protocol);
1538 ecore_con_event_server_error(svr, strerror(errno));
1542 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1544 ecore_con_event_server_error(svr, strerror(errno));
1548 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1550 ecore_con_event_server_error(svr, strerror(errno));
1556 if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, (const void *)&lin,
1557 sizeof(struct linger)) < 0)
1559 ecore_con_event_server_error(svr, strerror(errno));
1563 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_NODELAY)
1567 if (setsockopt(svr->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
1570 ecore_con_event_server_error(svr, strerror(errno));
1575 if (bind(svr->fd, net_info->info.ai_addr,
1576 net_info->info.ai_addrlen) < 0)
1578 ecore_con_event_server_error(svr, strerror(errno));
1581 if (listen(svr->fd, 4096) < 0)
1583 ecore_con_event_server_error(svr, strerror(errno));
1587 svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1588 _ecore_con_svr_tcp_handler, svr, NULL, NULL);
1589 if (!svr->fd_handler)
1591 ecore_con_event_server_error(svr, "Memory allocation failure");
1598 ecore_con_ssl_server_shutdown(svr);
1599 _ecore_con_server_kill(svr);
1603 _ecore_con_cb_udp_listen(void *data,
1604 Ecore_Con_Info *net_info)
1606 Ecore_Con_Server *svr;
1607 Ecore_Con_Type type;
1608 struct ip_mreq mreq;
1609 struct ipv6_mreq mreq6;
1614 type &= ECORE_CON_TYPE;
1616 if (!net_info) /* error message has already been handled */
1619 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1620 net_info->info.ai_protocol);
1623 ecore_con_event_server_error(svr, strerror(errno));
1627 if (type == ECORE_CON_REMOTE_MCAST)
1629 if (net_info->info.ai_family == AF_INET)
1631 if (!inet_pton(net_info->info.ai_family, net_info->ip,
1632 &mreq.imr_multiaddr))
1634 ecore_con_event_server_error(svr, strerror(errno));
1638 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
1639 if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1640 (const void *)&mreq, sizeof(mreq)) != 0)
1642 ecore_con_event_server_error(svr, strerror(errno));
1646 else if (net_info->info.ai_family == AF_INET6)
1648 if (!inet_pton(net_info->info.ai_family, net_info->ip,
1649 &mreq6.ipv6mr_multiaddr))
1651 ecore_con_event_server_error(svr, strerror(errno));
1654 mreq6.ipv6mr_interface = htonl(INADDR_ANY);
1655 if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1656 (const void *)&mreq6, sizeof(mreq6)) != 0)
1658 ecore_con_event_server_error(svr, strerror(errno));
1664 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on, sizeof(on)) != 0)
1666 ecore_con_event_server_error(svr, strerror(errno));
1670 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1672 ecore_con_event_server_error(svr, strerror(errno));
1676 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1678 ecore_con_event_server_error(svr, strerror(errno));
1682 if (bind(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0)
1684 ecore_con_event_server_error(svr, strerror(errno));
1689 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1690 _ecore_con_svr_udp_handler, svr, NULL, NULL);
1691 if (!svr->fd_handler)
1693 ecore_con_event_server_error(svr, "Memory allocation failure");
1697 svr->ip = eina_stringshare_add(net_info->ip);
1702 ecore_con_ssl_server_shutdown(svr);
1703 _ecore_con_server_kill(svr);
1707 _ecore_con_cb_tcp_connect(void *data,
1708 Ecore_Con_Info *net_info)
1710 Ecore_Con_Server *svr;
1716 if (!net_info) /* error message has already been handled */
1719 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1720 net_info->info.ai_protocol);
1723 ecore_con_event_server_error(svr, strerror(errno));
1727 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1729 ecore_con_event_server_error(svr, strerror(errno));
1733 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1735 ecore_con_event_server_error(svr, strerror(errno));
1739 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&curstate, sizeof(curstate)) < 0)
1741 ecore_con_event_server_error(svr, strerror(errno));
1745 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_NODELAY)
1749 if (setsockopt(svr->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int)) < 0)
1751 ecore_con_event_server_error(svr, strerror(errno));
1756 res = connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen);
1758 if (res == SOCKET_ERROR)
1760 if (WSAGetLastError() != WSAEINPROGRESS)
1761 goto error; /* FIXME: strerror on windows? */
1766 if (errno != EINPROGRESS)
1768 ecore_con_event_server_error(svr, strerror(errno));
1773 svr->connecting = EINA_TRUE;
1775 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
1776 _ecore_con_cl_handler, svr, NULL, NULL);
1779 svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1780 _ecore_con_cl_handler, svr, NULL, NULL);
1782 if (svr->type & ECORE_CON_SSL)
1784 svr->handshaking = EINA_TRUE;
1785 svr->ssl_state = ECORE_CON_SSL_STATE_INIT;
1786 DBG("beginning ssl handshake");
1787 if (ecore_con_ssl_server_init(svr))
1791 if (!svr->fd_handler)
1793 ecore_con_event_server_error(svr, "Memory allocation failure");
1797 svr->ip = eina_stringshare_add(net_info->ip);
1802 ecore_con_ssl_server_shutdown(svr);
1803 _ecore_con_server_kill(svr);
1807 _ecore_con_cb_udp_connect(void *data,
1808 Ecore_Con_Info *net_info)
1810 Ecore_Con_Server *svr;
1815 if (!net_info) /* error message has already been handled */
1818 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1819 net_info->info.ai_protocol);
1822 ecore_con_event_server_error(svr, strerror(errno));
1826 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1828 ecore_con_event_server_error(svr, strerror(errno));
1832 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1834 ecore_con_event_server_error(svr, strerror(errno));
1838 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_BROADCAST)
1840 if (setsockopt(svr->fd, SOL_SOCKET, SO_BROADCAST,
1841 (const void *)&broadcast,
1842 sizeof(broadcast)) < 0)
1844 ecore_con_event_server_error(svr, strerror(errno));
1848 else if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR,
1849 (const void *)&curstate, sizeof(curstate)) < 0)
1851 ecore_con_event_server_error(svr, strerror(errno));
1855 if (connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0)
1857 ecore_con_event_server_error(svr, strerror(errno));
1861 svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
1862 _ecore_con_cl_udp_handler, svr, NULL, NULL);
1864 if (!svr->fd_handler)
1866 ecore_con_event_server_error(svr, "Memory allocation failure");
1870 svr->ip = eina_stringshare_add(net_info->ip);
1875 ecore_con_ssl_server_shutdown(svr);
1876 _ecore_con_server_kill(svr);
1879 static Ecore_Con_State
1880 svr_try_connect_plain(Ecore_Con_Server *svr)
1884 socklen_t size = sizeof(int);
1886 res = getsockopt(svr->fd, SOL_SOCKET, SO_ERROR, (void *)&so_err, &size);
1888 if (res == SOCKET_ERROR)
1889 so_err = WSAGetLastError();
1891 if ((so_err == WSAEINPROGRESS) && !svr->dead)
1892 return ECORE_CON_INPROGRESS;
1898 if ((so_err == EINPROGRESS) && !svr->dead)
1899 return ECORE_CON_INPROGRESS;
1905 /* we lost our server! */
1906 ecore_con_event_server_error(svr, strerror(errno));
1907 ERR("Connection lost: %s", strerror(so_err));
1908 _ecore_con_server_kill(svr);
1909 return ECORE_CON_DISCONNECTED;
1912 if ((!svr->delete_me) && (!svr->handshaking) && svr->connecting)
1914 svr->connecting = EINA_FALSE;
1915 svr->start_time = ecore_time_get();
1916 ecore_con_event_server_add(svr);
1919 if (svr->fd_handler && (!svr->write_buf))
1920 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
1923 return ECORE_CON_CONNECTED;
1925 return ECORE_CON_DISCONNECTED;
1929 _ecore_con_pretty_ip(struct sockaddr *client_addr,
1932 char ipbuf[INET6_ADDRSTRLEN + 1];
1934 /* show v4mapped address in pretty form */
1935 if (client_addr->sa_family == AF_INET6)
1937 struct sockaddr_in6 *sa6;
1939 sa6 = (struct sockaddr_in6 *)client_addr;
1940 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr))
1942 snprintf(ipbuf, sizeof (ipbuf), "%u.%u.%u.%u",
1943 sa6->sin6_addr.s6_addr[12],
1944 sa6->sin6_addr.s6_addr[13],
1945 sa6->sin6_addr.s6_addr[14],
1946 sa6->sin6_addr.s6_addr[15]);
1947 return eina_stringshare_add(ipbuf);
1951 if (getnameinfo(client_addr, size, ipbuf, sizeof (ipbuf), NULL, 0, NI_NUMERICHOST))
1952 return eina_stringshare_add("0.0.0.0");
1954 ipbuf[sizeof(ipbuf) - 1] = 0;
1955 return eina_stringshare_add(ipbuf);
1959 _ecore_con_svr_tcp_handler(void *data,
1960 Ecore_Fd_Handler *fd_handler __UNUSED__)
1962 Ecore_Con_Server *svr;
1963 Ecore_Con_Client *cl = NULL;
1964 unsigned char client_addr[256];
1965 unsigned int client_addr_len;
1969 return ECORE_CALLBACK_RENEW;
1972 return ECORE_CALLBACK_RENEW;
1974 if ((svr->client_limit >= 0) && (!svr->reject_excess_clients) &&
1975 (svr->client_count >= (unsigned int)svr->client_limit))
1976 return ECORE_CALLBACK_RENEW;
1980 cl = calloc(1, sizeof(Ecore_Con_Client));
1983 ecore_con_event_server_error(svr, "Memory allocation failure when attempting to add a new client");
1984 return ECORE_CALLBACK_RENEW;
1986 cl->host_server = svr;
1988 client_addr_len = sizeof(client_addr);
1989 memset(&client_addr, 0, client_addr_len);
1990 cl->fd = accept(svr->fd, (struct sockaddr *)&client_addr, (socklen_t *)&client_addr_len);
1993 ecore_con_event_server_error(svr, strerror(errno));
1997 if ((svr->client_limit >= 0) && (svr->reject_excess_clients) &&
1998 (svr->client_count >= (unsigned int)svr->client_limit))
2000 ecore_con_event_server_error(svr, "Maximum client limit reached");
2004 if (fcntl(cl->fd, F_SETFL, O_NONBLOCK) < 0)
2006 ecore_con_event_server_error(svr, strerror(errno));
2009 if (fcntl(cl->fd, F_SETFD, FD_CLOEXEC) < 0)
2011 ecore_con_event_server_error(svr, strerror(errno));
2014 cl->fd_handler = ecore_main_fd_handler_add(cl->fd, ECORE_FD_READ,
2015 _ecore_con_svr_cl_handler, cl, NULL, NULL);
2016 if (!cl->fd_handler)
2019 ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
2021 if (svr->type & ECORE_CON_SSL)
2023 cl->handshaking = EINA_TRUE;
2024 cl->ssl_state = ECORE_CON_SSL_STATE_INIT;
2025 if (ecore_con_ssl_client_init(cl))
2029 cl->client_addr = malloc(client_addr_len);
2030 if (!cl->client_addr)
2032 ecore_con_event_server_error(svr, "Memory allocation failure when attempting to add a new client");
2035 cl->client_addr_len = client_addr_len;
2036 memcpy(cl->client_addr, &client_addr, client_addr_len);
2038 svr->clients = eina_list_append(svr->clients, cl);
2039 svr->client_count++;
2041 if ((!cl->delete_me) && (!cl->handshaking))
2042 ecore_con_event_client_add(cl);
2044 return ECORE_CALLBACK_RENEW;
2047 ecore_main_fd_handler_del(cl->fd_handler);
2053 return ECORE_CALLBACK_RENEW;
2057 _ecore_con_cl_read(Ecore_Con_Server *svr)
2061 Eina_Bool lost_server = EINA_TRUE;
2062 unsigned char buf[READBUFSIZ];
2064 /* only possible with non-ssl connections */
2065 if (svr->connecting && (svr_try_connect_plain(svr) != ECORE_CON_CONNECTED))
2068 if (svr->handshaking)
2070 DBG("Continuing ssl handshake");
2071 if (!ecore_con_ssl_server_init(svr))
2072 lost_server = EINA_FALSE;
2075 if (!(svr->type & ECORE_CON_SSL))
2077 num = read(svr->fd, buf, sizeof(buf));
2078 /* 0 is not a valid return value for a tcp socket */
2079 if ((num > 0) || ((num < 0) && (errno == EAGAIN)))
2080 lost_server = EINA_FALSE;
2082 ecore_con_event_server_error(svr, strerror(errno));
2086 num = ecore_con_ssl_server_read(svr, buf, sizeof(buf));
2087 /* this is not an actual 0 return, 0 here just means non-fatal error such as EAGAIN */
2089 lost_server = EINA_FALSE;
2092 if ((!svr->delete_me) && (num > 0))
2093 ecore_con_event_server_data(svr, buf, num, EINA_TRUE);
2096 _ecore_con_server_kill(svr);
2100 _ecore_con_cl_handler(void *data,
2101 Ecore_Fd_Handler *fd_handler)
2103 Ecore_Con_Server *svr;
2104 Eina_Bool want_read, want_write;
2108 return ECORE_CALLBACK_RENEW;
2111 return ECORE_CALLBACK_RENEW;
2113 want_read = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ);
2114 want_write = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE);
2116 if (svr->handshaking && (want_read || want_write))
2118 DBG("Continuing ssl handshake: preparing to %s...", want_read ? "read" : "write");
2122 char buf[READBUFSIZ];
2124 len = recv(svr->fd, buf, sizeof(buf), MSG_DONTWAIT | MSG_PEEK);
2125 DBG("%zu bytes in buffer", len);
2128 if (ecore_con_ssl_server_init(svr))
2130 ERR("ssl handshaking failed!");
2131 svr->handshaking = EINA_FALSE;
2134 else if (!svr->ssl_state)
2136 svr->connecting = EINA_FALSE;
2137 svr->start_time = ecore_time_get();
2138 ecore_con_event_server_add(svr);
2142 _ecore_con_cl_read(svr);
2143 else if (want_write) /* only possible with non-ssl connections */
2145 if (svr->connecting && (!svr_try_connect_plain(svr)))
2146 return ECORE_CALLBACK_RENEW;
2148 _ecore_con_server_flush(svr);
2151 return ECORE_CALLBACK_RENEW;
2155 _ecore_con_cl_udp_handler(void *data,
2156 Ecore_Fd_Handler *fd_handler)
2158 unsigned char buf[READBUFSIZ];
2160 Ecore_Con_Server *svr;
2161 Eina_Bool want_read, want_write;
2163 want_read = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ);
2164 want_write = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE);
2167 if (svr->dead || svr->delete_me || ((!want_read) && (!want_write)))
2168 return ECORE_CALLBACK_RENEW;
2172 _ecore_con_server_flush(svr);
2173 return ECORE_CALLBACK_RENEW;
2176 num = read(svr->fd, buf, READBUFSIZ);
2178 if ((!svr->delete_me) && (num > 0))
2179 ecore_con_event_server_data(svr, buf, num, EINA_TRUE);
2181 if (num < 0 && (errno != EAGAIN) && (errno != EINTR))
2183 ecore_con_event_server_error(svr, strerror(errno));
2184 _ecore_con_server_kill(svr);
2187 return ECORE_CALLBACK_RENEW;
2191 _ecore_con_svr_udp_handler(void *data,
2192 Ecore_Fd_Handler *fd_handler)
2194 unsigned char buf[READBUFSIZ];
2195 unsigned char client_addr[256];
2196 socklen_t client_addr_len = sizeof(client_addr);
2198 Ecore_Con_Server *svr;
2199 Ecore_Con_Client *cl = NULL;
2203 if (svr->delete_me || svr->dead)
2204 return ECORE_CALLBACK_RENEW;
2206 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
2208 _ecore_con_client_flush(cl);
2209 return ECORE_CALLBACK_RENEW;
2212 if (!ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
2213 return ECORE_CALLBACK_RENEW;
2216 num = fcntl(svr->fd, F_SETFL, O_NONBLOCK);
2218 num = recvfrom(svr->fd, (char *)buf, sizeof(buf), 0,
2219 (struct sockaddr *)&client_addr,
2223 num = recvfrom(svr->fd, buf, sizeof(buf), MSG_DONTWAIT,
2224 (struct sockaddr *)&client_addr,
2228 if (num < 0 && (errno != EAGAIN) && (errno != EINTR))
2230 ecore_con_event_server_error(svr, strerror(errno));
2231 if (!svr->delete_me)
2234 ecore_con_event_client_del(NULL);
2237 svr->dead = EINA_TRUE;
2238 svr->fd_handler = NULL;
2239 return ECORE_CALLBACK_CANCEL;
2243 /* Create a new client for use in the client data event */
2244 cl = calloc(1, sizeof(Ecore_Con_Client));
2245 EINA_SAFETY_ON_NULL_RETURN_VAL(cl, ECORE_CALLBACK_RENEW);
2247 cl->host_server = svr;
2248 cl->client_addr = malloc(client_addr_len);
2249 if (!cl->client_addr)
2252 return ECORE_CALLBACK_RENEW;
2254 cl->client_addr_len = client_addr_len;
2256 memcpy(cl->client_addr, &client_addr, client_addr_len);
2257 ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
2258 svr->clients = eina_list_append(svr->clients, cl);
2259 svr->client_count++;
2261 ecore_con_event_client_add(cl);
2262 ecore_con_event_client_data(cl, buf, num, EINA_TRUE);
2264 return ECORE_CALLBACK_RENEW;
2268 _ecore_con_svr_cl_read(Ecore_Con_Client *cl)
2271 Eina_Bool lost_client = EINA_TRUE;
2272 unsigned char buf[READBUFSIZ];
2276 if (cl->handshaking)
2278 /* add an extra handshake attempt just before read, even though
2279 * read also attempts to handshake, to try to finish sooner
2281 if (ecore_con_ssl_client_init(cl))
2282 lost_client = EINA_FALSE;
2284 _ecore_con_cl_timer_update(cl);
2287 if (!(cl->host_server->type & ECORE_CON_SSL))
2289 num = read(cl->fd, buf, sizeof(buf));
2290 /* 0 is not a valid return value for a tcp socket */
2291 if ((num > 0) || ((num < 0) && ((errno == EAGAIN) || (errno == EINTR))))
2292 lost_client = EINA_FALSE;
2294 ecore_con_event_client_error(cl, strerror(errno));
2298 num = ecore_con_ssl_client_read(cl, buf, sizeof(buf));
2299 /* this is not an actual 0 return, 0 here just means non-fatal error such as EAGAIN */
2301 lost_client = EINA_FALSE;
2304 if ((!cl->delete_me) && (num > 0))
2305 ecore_con_event_client_data(cl, buf, num, EINA_TRUE);
2310 ecore_con_event_client_del(cl);
2311 INF("Lost client %s", (cl->ip) ? cl->ip : "");
2312 cl->dead = EINA_TRUE;
2314 ecore_main_fd_handler_del(cl->fd_handler);
2316 cl->fd_handler = NULL;
2322 _ecore_con_svr_cl_handler(void *data,
2323 Ecore_Fd_Handler *fd_handler)
2325 Ecore_Con_Client *cl;
2329 return ECORE_CALLBACK_RENEW;
2332 return ECORE_CALLBACK_RENEW;
2334 if (cl->handshaking && ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ | ECORE_FD_WRITE))
2336 if (ecore_con_ssl_client_init(cl))
2338 ERR("ssl handshaking failed!");
2339 cl->handshaking = EINA_FALSE;
2340 cl->dead = EINA_TRUE;
2341 INF("Lost client %s", (cl->ip) ? cl->ip : "");
2342 ecore_con_event_client_del(cl);
2344 else if (!cl->ssl_state)
2345 ecore_con_event_client_add(cl);
2347 else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
2348 _ecore_con_svr_cl_read(cl);
2350 else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
2351 _ecore_con_client_flush(cl);
2353 return ECORE_CALLBACK_RENEW;
2357 _ecore_con_server_flush(Ecore_Con_Server *svr)
2362 if (ecore_con_local_win32_server_flush(svr))
2366 if (!svr->write_buf && svr->fd_handler)
2368 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
2372 num = svr->write_buf_size - svr->write_buf_offset;
2374 /* check whether we need to write anything at all.
2375 * we must not write zero bytes with SSL_write() since it
2376 * causes undefined behaviour
2378 /* we thank Tommy[D] for needing to check negative buffer sizes
2379 * here because his system is amazing.
2381 if (num <= 0) return;
2383 if (svr->handshaking)
2385 DBG("Continuing ssl handshake");
2386 if (ecore_con_ssl_server_init(svr))
2387 _ecore_con_server_kill(svr);
2391 if (!(svr->type & ECORE_CON_SSL))
2392 count = write(svr->fd, svr->write_buf + svr->write_buf_offset, num);
2394 count = ecore_con_ssl_server_write(svr, svr->write_buf + svr->write_buf_offset, num);
2398 if ((errno != EAGAIN) && (errno != EINTR))
2400 ecore_con_event_server_error(svr, strerror(errno));
2401 _ecore_con_server_kill(svr);
2406 svr->write_buf_offset += count;
2407 if (svr->write_buf_offset >= svr->write_buf_size)
2409 svr->write_buf_size = 0;
2410 svr->write_buf_offset = 0;
2411 free(svr->write_buf);
2412 svr->write_buf = NULL;
2413 if (svr->fd_handler)
2414 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
2416 else if ((count < num) && svr->fd_handler)
2417 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_WRITE);
2421 _ecore_con_client_flush(Ecore_Con_Client *cl)
2426 if (ecore_con_local_win32_client_flush(cl))
2430 if (!cl->buf && cl->fd_handler)
2432 ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ);
2436 if (cl->handshaking)
2438 if (ecore_con_ssl_client_init(cl))
2441 _ecore_con_cl_timer_update(cl);
2446 num = cl->buf_size - cl->buf_offset;
2447 if (num <= 0) return;
2448 if (!(cl->host_server->type & ECORE_CON_SSL))
2449 count = write(cl->fd, cl->buf + cl->buf_offset, num);
2451 count = ecore_con_ssl_client_write(cl, cl->buf + cl->buf_offset, num);
2456 if ((errno != EAGAIN) && (errno != EINTR) && (!cl->delete_me))
2458 ecore_con_event_client_error(cl, strerror(errno));
2459 ecore_con_event_client_del(cl);
2460 cl->dead = EINA_TRUE;
2461 INF("Lost client %s", (cl->ip) ? cl->ip : "");
2463 ecore_main_fd_handler_del(cl->fd_handler);
2465 cl->fd_handler = NULL;
2471 cl->buf_offset += count;
2472 if (cl->buf_offset >= cl->buf_size)
2479 ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ);
2481 else if ((count < num) && cl->fd_handler)
2482 ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_WRITE);
2486 _ecore_con_event_client_add_free(Ecore_Con_Server *svr,
2489 Ecore_Con_Event_Client_Add *e;
2492 e->client->event_count--;
2493 e->client->host_server->event_count--;
2494 if ((e->client->event_count <= 0) && (e->client->delete_me))
2495 ecore_con_client_del(e->client);
2496 if ((svr->event_count <= 0) && (svr->delete_me))
2497 _ecore_con_server_free(svr);
2503 _ecore_con_event_client_del_free(Ecore_Con_Server *svr,
2506 Ecore_Con_Event_Client_Del *e;
2509 if (!e->client) return;
2511 e->client->event_count--;
2512 e->client->host_server->event_count--;
2513 if ((e->client->event_count <= 0) && (e->client->delete_me))
2514 ecore_con_client_del(e->client);
2515 if ((svr->event_count <= 0) && (svr->delete_me))
2516 _ecore_con_server_free(svr);
2522 _ecore_con_event_client_data_free(Ecore_Con_Server *svr,
2525 Ecore_Con_Event_Client_Data *e;
2528 e->client->event_count--;
2529 e->client->host_server->event_count--;
2533 if (((e->client->event_count <= 0) && (e->client->delete_me)) ||
2534 ((e->client->host_server &&
2535 ((e->client->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_UDP ||
2536 (e->client->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_MCAST))))
2537 ecore_con_client_del(e->client);
2538 if ((svr->event_count <= 0) && (svr->delete_me))
2539 _ecore_con_server_free(svr);
2545 _ecore_con_event_server_add_free(void *data __UNUSED__,
2548 Ecore_Con_Event_Server_Add *e;
2551 e->server->event_count--;
2552 if ((e->server->event_count <= 0) && (e->server->delete_me))
2553 _ecore_con_server_free(e->server);
2559 _ecore_con_event_server_del_free(void *data __UNUSED__,
2562 Ecore_Con_Event_Server_Del *e;
2565 e->server->event_count--;
2566 if ((e->server->event_count <= 0) && (e->server->delete_me))
2567 _ecore_con_server_free(e->server);
2573 _ecore_con_event_server_data_free(void *data __UNUSED__,
2576 Ecore_Con_Event_Server_Data *e;
2579 e->server->event_count--;
2583 if ((e->server->event_count <= 0) && (e->server->delete_me))
2584 _ecore_con_server_free(e->server);
2591 _ecore_con_event_server_error_free(void *data __UNUSED__, Ecore_Con_Event_Server_Error *e)
2593 e->server->event_count--;
2594 if ((e->server->event_count <= 0) && (e->server->delete_me))
2595 _ecore_con_server_free(e->server);
2596 if (e->error) free(e->error);
2601 _ecore_con_event_client_error_free(Ecore_Con_Server *svr, Ecore_Con_Event_Client_Error *e)
2603 e->client->event_count--;
2604 e->client->host_server->event_count--;
2605 if ((e->client->event_count <= 0) && (e->client->delete_me))
2606 _ecore_con_client_free(e->client);
2607 if ((svr->event_count <= 0) && (svr->delete_me))
2608 _ecore_con_server_free(svr);
2609 if (e->error) free(e->error);
2615 _ecore_con_lookup_done(void *data,
2616 Ecore_Con_Info *infos)
2618 Ecore_Con_Server *svr;
2619 Ecore_Con_Lookup *lk;
2625 lk->done_cb(infos->info.ai_canonname, infos->ip,
2626 infos->info.ai_addr, infos->info.ai_addrlen,
2629 lk->done_cb(NULL, NULL, NULL, 0, (void *)lk->data);