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 Eina_Bool _ecore_con_svr_tcp_handler(void *data,
62 Ecore_Fd_Handler *fd_handler);
63 static Eina_Bool _ecore_con_cl_handler(void *data,
64 Ecore_Fd_Handler *fd_handler);
65 static Eina_Bool _ecore_con_cl_udp_handler(void *data,
66 Ecore_Fd_Handler *fd_handler);
67 static Eina_Bool _ecore_con_svr_udp_handler(void *data,
68 Ecore_Fd_Handler *fd_handler);
70 static void _ecore_con_svr_cl_read(Ecore_Con_Client *cl);
71 static Eina_Bool _ecore_con_svr_cl_handler(void *data,
72 Ecore_Fd_Handler *fd_handler);
74 static void _ecore_con_server_flush(Ecore_Con_Server *svr);
75 static void _ecore_con_client_flush(Ecore_Con_Client *cl);
77 static void _ecore_con_event_client_add_free(void *data,
79 static void _ecore_con_event_client_del_free(void *data,
81 static void _ecore_con_event_client_data_free(void *data,
83 static void _ecore_con_event_server_add_free(void *data,
85 static void _ecore_con_event_server_del_free(void *data,
87 static void _ecore_con_event_server_data_free(void *data,
90 static void _ecore_con_lookup_done(void *data,
91 Ecore_Con_Info *infos);
94 _ecore_con_pretty_ip(struct sockaddr *client_addr,
97 EAPI int ECORE_CON_EVENT_CLIENT_ADD = 0;
98 EAPI int ECORE_CON_EVENT_CLIENT_DEL = 0;
99 EAPI int ECORE_CON_EVENT_SERVER_ADD = 0;
100 EAPI int ECORE_CON_EVENT_SERVER_DEL = 0;
101 EAPI int ECORE_CON_EVENT_CLIENT_DATA = 0;
102 EAPI int ECORE_CON_EVENT_SERVER_DATA = 0;
104 static Eina_List *servers = NULL;
105 static int _ecore_con_init_count = 0;
106 int _ecore_con_log_dom = -1;
109 * @addtogroup Ecore_Con_Lib_Group Ecore Connection Library Functions
111 * Utility functions that set up and shut down the Ecore Connection
117 * Initialises the Ecore_Con library.
118 * @return Number of times the library has been initialised without being
124 if (++_ecore_con_init_count != 1)
125 return _ecore_con_init_count;
129 return --_ecore_con_init_count;
134 return --_ecore_con_init_count;
136 _ecore_con_log_dom = eina_log_domain_register
137 ("ecore_con", ECORE_CON_DEFAULT_LOG_COLOR);
138 if (_ecore_con_log_dom < 0)
140 EINA_LOG_ERR("Impossible to create a log domain for Ecore Con.");
142 return --_ecore_con_init_count;
145 ECORE_CON_EVENT_CLIENT_ADD = ecore_event_type_new();
146 ECORE_CON_EVENT_CLIENT_DEL = ecore_event_type_new();
147 ECORE_CON_EVENT_SERVER_ADD = ecore_event_type_new();
148 ECORE_CON_EVENT_SERVER_DEL = ecore_event_type_new();
149 ECORE_CON_EVENT_CLIENT_DATA = ecore_event_type_new();
150 ECORE_CON_EVENT_SERVER_DATA = ecore_event_type_new();
153 eina_magic_string_set(ECORE_MAGIC_CON_SERVER, "Ecore_Con_Server");
154 eina_magic_string_set(ECORE_MAGIC_CON_CLIENT, "Ecore_Con_Server");
155 eina_magic_string_set(ECORE_MAGIC_CON_URL, "Ecore_Con_Url");
157 /* TODO Remember return value, if it fails, use gethostbyname() */
158 ecore_con_ssl_init();
159 ecore_con_info_init();
161 return _ecore_con_init_count;
165 * Shuts down the Ecore_Con library.
166 * @return Number of times the library has been initialised without being
170 ecore_con_shutdown(void)
173 Ecore_Con_Server *svr;
175 if (--_ecore_con_init_count != 0)
176 return _ecore_con_init_count;
178 EINA_LIST_FOREACH_SAFE(servers, l, l2, svr)
179 _ecore_con_server_free(svr);
181 ecore_con_info_shutdown();
182 ecore_con_ssl_shutdown();
183 eina_log_domain_unregister(_ecore_con_log_dom);
184 _ecore_con_log_dom = -1;
190 return _ecore_con_init_count;
194 * Do an asynchronous DNS lookup.
196 * This function performs a DNS lookup on the hostname specified by @p name, then
197 * calls @p done_cb with
199 * @param name IP address or server name to translate.
200 * @param done_cb Callback to notify when done.
201 * @param data User data to be given to done_cb.
202 * @return EINA_TRUE if the request did not fail to be set up, EINA_FALSE if it failed.
205 ecore_con_lookup(const char *name,
206 Ecore_Con_Dns_Cb done_cb,
209 Ecore_Con_Server *svr;
210 Ecore_Con_Lookup *lk;
211 struct addrinfo hints;
213 if (!name || !done_cb)
216 svr = calloc(1, sizeof(Ecore_Con_Server));
220 lk = malloc(sizeof (Ecore_Con_Lookup));
227 lk->done_cb = done_cb;
230 svr->name = strdup(name);
234 svr->type = ECORE_CON_REMOTE_TCP;
237 svr->created = EINA_TRUE;
238 svr->reject_excess_clients = EINA_FALSE;
239 svr->client_limit = -1;
241 svr->ppid = getpid();
243 memset(&hints, 0, sizeof(struct addrinfo));
244 hints.ai_family = AF_INET6;
245 hints.ai_socktype = SOCK_STREAM;
246 hints.ai_flags = AI_CANONNAME;
247 hints.ai_protocol = IPPROTO_TCP;
248 hints.ai_canonname = NULL;
249 hints.ai_next = NULL;
250 hints.ai_addr = NULL;
252 if (ecore_con_info_get(svr, _ecore_con_lookup_done, svr,
268 * @addtogroup Ecore_Con_Server_Group Ecore Connection Server Functions
270 * Functions that operate on Ecore server objects.
276 * @example ecore_con_server_example.c
277 * Shows how to write a simple server using the Ecore_Con library.
281 * Creates a server to listen for connections.
283 * The socket on which the server listens depends on the connection
285 * @li If @a compl_type is @c ECORE_CON_LOCAL_USER, the server will listen on
286 * the Unix socket "~/.ecore/[name]/[port]".
287 * @li If @a compl_type is @c ECORE_CON_LOCAL_SYSTEM, the server will listen
288 * on Unix socket "/tmp/.ecore_service|[name]|[port]".
289 * @li If @a compl_type is @c ECORE_CON_REMOTE_TCP, the server will listen
290 * on TCP port @c port.
292 * @param compl_type The connection type.
293 * @param name Name to associate with the socket. It is used when
294 * generating the socket name of a Unix socket, or for
295 * determining what host to listen on for TCP sockets.
296 * @c NULL will not be accepted.
297 * @param port Number to identify socket. When a Unix socket is used,
298 * it becomes part of the socket name. When a TCP socket
299 * is used, it is used as the TCP port.
300 * @param data Data to associate with the created Ecore_Con_Server
302 * @return A new Ecore_Con_Server.
304 EAPI Ecore_Con_Server *
305 ecore_con_server_add(Ecore_Con_Type compl_type,
310 Ecore_Con_Server *svr;
313 if (port < 0 || !name)
314 return NULL; /* local user socket: FILE: ~/.ecore/[name]/[port] */
316 /* local system socket: FILE: /tmp/.ecore_service|[name]|[port] */
317 /* remote system socket: TCP/IP: [name]:[port] */
318 svr = calloc(1, sizeof(Ecore_Con_Server));
322 svr->name = strdup(name);
326 svr->type = compl_type;
328 svr->data = (void *)data;
329 svr->created = EINA_TRUE;
330 if (compl_type & ECORE_CON_LOAD_CERT)
331 svr->use_cert = EINA_TRUE;
332 svr->reject_excess_clients = EINA_FALSE;
333 svr->client_limit = -1;
335 svr->ppid = getpid();
336 if (ecore_con_ssl_server_prepare(svr, compl_type & ECORE_CON_SSL))
339 type = compl_type & ECORE_CON_TYPE;
341 if ((type == ECORE_CON_LOCAL_USER) ||
342 (type == ECORE_CON_LOCAL_SYSTEM) ||
343 (type == ECORE_CON_LOCAL_ABSTRACT))
345 if (!ecore_con_local_listen(svr, _ecore_con_svr_tcp_handler, svr))
348 if ((type == ECORE_CON_REMOTE_TCP) ||
349 (type == ECORE_CON_REMOTE_NODELAY))
352 if (!ecore_con_info_tcp_listen(svr, _ecore_con_cb_tcp_listen,
356 else if ((type == ECORE_CON_REMOTE_MCAST) ||
357 (type == ECORE_CON_REMOTE_UDP))
359 if (!ecore_con_info_udp_listen(svr, _ecore_con_cb_udp_listen,
363 servers = eina_list_append(servers, svr);
364 ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
380 ecore_main_fd_handler_del(svr->fd_handler);
383 free(svr->write_buf);
386 eina_stringshare_del(svr->ip);
389 ecore_con_ssl_server_shutdown(svr);
395 * Creates a connection to the specified server and returns an associated object.
397 * The socket to which the connection is made depends on the connection type:
398 * @li If @a compl_type is @c ECORE_CON_LOCAL_USER, the function will
399 * connect to the server at the Unix socket
400 * "~/.ecore/[name]/[port]".
401 * @li If @a compl_type is @c ECORE_CON_LOCAL_SYSTEM, the function will
402 * connect to the server at the Unix socket
403 * "/tmp/.ecore_service|[name]|[port]".
404 * @li If @a compl_type is @c ECORE_CON_REMOTE_TCP, the function will
405 * connect to the server at the TCP port "[name]:[port]".
407 * @param compl_type The connection type.
408 * @param name Name used when determining what socket to connect to.
409 * It is used to generate the socket name when the socket
410 * is a Unix socket. It is used as the hostname when
411 * connecting with a TCP socket.
412 * @param port Number to identify the socket to connect to. Used when
413 * generating the socket name for a Unix socket, or as the
414 * TCP port when connecting to a TCP socket.
415 * @param data Data to associate with the created Ecore_Con_Server
417 * @return A new Ecore_Con_Server.
419 EAPI Ecore_Con_Server *
420 ecore_con_server_connect(Ecore_Con_Type compl_type,
425 Ecore_Con_Server *svr;
430 /* local user socket: FILE: ~/.ecore/[name]/[port] */
431 /* local system socket: FILE: /tmp/.ecore_service|[name]|[port] */
432 /* remote system socket: TCP/IP: [name]:[port] */
433 svr = calloc(1, sizeof(Ecore_Con_Server));
437 svr->name = strdup(name);
441 svr->type = compl_type;
443 svr->data = (void *)data;
444 svr->created = EINA_FALSE;
445 svr->use_cert = (compl_type & ECORE_CON_LOAD_CERT);
446 svr->reject_excess_clients = EINA_FALSE;
448 svr->client_limit = -1;
449 if (ecore_con_ssl_server_prepare(svr, compl_type & ECORE_CON_SSL))
452 type = compl_type & ECORE_CON_TYPE;
454 if (((type == ECORE_CON_REMOTE_TCP) ||
455 (type == ECORE_CON_REMOTE_NODELAY) ||
456 (type == ECORE_CON_REMOTE_UDP) ||
457 (type == ECORE_CON_REMOTE_BROADCAST)) &&
461 if ((type == ECORE_CON_LOCAL_USER) ||
462 (type == ECORE_CON_LOCAL_SYSTEM) ||
463 (type == ECORE_CON_LOCAL_ABSTRACT))
465 if (!ecore_con_local_connect(svr, _ecore_con_cl_handler, svr,
466 _ecore_con_event_server_add_free))
470 if ((type == ECORE_CON_REMOTE_TCP) ||
471 (type == ECORE_CON_REMOTE_NODELAY))
474 if (!ecore_con_info_tcp_connect(svr, _ecore_con_cb_tcp_connect,
478 else if ((type == ECORE_CON_REMOTE_UDP) ||
479 (type == ECORE_CON_REMOTE_BROADCAST))
481 if (!ecore_con_info_udp_connect(svr, _ecore_con_cb_udp_connect,
485 servers = eina_list_append(servers, svr);
486 ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
501 ecore_main_fd_handler_del(svr->fd_handler);
503 ecore_con_ssl_server_shutdown(svr);
509 * Set the default time after which an inactive client will be disconnected
510 * @param svr The server object
511 * @param timeout The timeout, in seconds, to disconnect after
512 * This function is used to set the idle timeout on clients. A value of < 1
513 * disables the idle timeout.
516 ecore_con_server_timeout_set(Ecore_Con_Server *svr,
519 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
521 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_timeout_set");
525 svr->client_disconnect_time = timeout;
529 * Get the default time after which an inactive client will be disconnected
530 * @param svr The server object
531 * @return The timeout, in seconds, to disconnect after
532 * This function is used to get the idle timeout for clients. A value of < 1
533 * means the idle timeout is disabled.
536 ecore_con_server_timeout_get(Ecore_Con_Server *svr)
538 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
540 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_timeout_get");
544 return svr->client_disconnect_time;
548 * Closes the connection and frees the given server.
549 * @param svr The given server.
550 * @return Data associated with the server when it was created.
551 * @see ecore_con_server_add, ecore_con_server_connect
554 ecore_con_server_del(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_del");
568 svr->delete_me = EINA_TRUE;
569 if (svr->event_count > 0)
573 ecore_main_fd_handler_del(svr->fd_handler);
574 svr->fd_handler = NULL;
578 _ecore_con_server_free(svr);
584 * Retrieves the data associated with the given server.
585 * @param svr The given server.
586 * @return The associated data.
589 ecore_con_server_data_get(Ecore_Con_Server *svr)
591 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
593 ECORE_MAGIC_FAIL(svr,
594 ECORE_MAGIC_CON_SERVER,
595 "ecore_con_server_data_get");
603 * Sets the data associated with the given server.
604 * @param svr The given server.
605 * @param data The data to associate with @p svr
606 * @return The previously associated data, if any.
609 ecore_con_server_data_set(Ecore_Con_Server *svr,
614 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
616 ECORE_MAGIC_FAIL(svr,
617 ECORE_MAGIC_CON_SERVER,
618 "ecore_con_server_data_get");
628 * Retrieves whether the given server is currently connected.
629 * @param svr The given server.
630 * @return #EINA_TRUE if the server is connected. #EINA_FALSE otherwise.
633 ecore_con_server_connected_get(Ecore_Con_Server *svr)
635 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
637 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
638 "ecore_con_server_connected_get");
649 * Retrieves the current list of clients.
650 * @param svr The given server.
651 * @return The list of clients on this server.
654 ecore_con_server_clients_get(Ecore_Con_Server *svr)
656 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
658 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
659 "ecore_con_server_clients_get");
667 * Retrieves the name of server.
668 * @param svr The given server.
669 * @return The name of the server.
672 ecore_con_server_name_get(Ecore_Con_Server *svr)
674 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
676 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
677 "ecore_con_server_name_get");
685 * Retrieves the server port in use.
686 * @param svr The given server.
687 * @return The server port in use.
690 ecore_con_server_port_get(Ecore_Con_Server *svr)
692 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
694 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
695 "ecore_con_server_port_get");
702 * Sends the given data to the given server.
703 * @param svr The given server.
704 * @param data The given data.
705 * @param size Length of the data, in bytes, to send.
706 * @return The number of bytes sent. @c 0 will be returned if there is an
710 ecore_con_server_send(Ecore_Con_Server *svr,
714 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
716 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_send");
720 EINA_SAFETY_ON_TRUE_RETURN_VAL(svr->dead, 0);
722 EINA_SAFETY_ON_NULL_RETURN_VAL(data, 0);
724 EINA_SAFETY_ON_TRUE_RETURN_VAL(size < 1, 0);
727 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
731 unsigned char *newbuf;
733 newbuf = realloc(svr->write_buf, svr->write_buf_size + size);
734 EINA_SAFETY_ON_NULL_RETURN_VAL(newbuf, 0);
736 svr->write_buf = newbuf;
737 memcpy(svr->write_buf + svr->write_buf_size, data, size);
738 svr->write_buf_size += size;
742 svr->write_buf = malloc(size);
743 EINA_SAFETY_ON_NULL_RETURN_VAL(svr->write_buf, 0);
745 svr->write_buf_size = size;
746 memcpy(svr->write_buf, data, size);
753 * Sets a limit on the number of clients that can be handled concurrently
754 * by the given server, and a policy on what to do if excess clients try to
756 * Beware that if you set this once ecore is already running, you may
757 * already have pending CLIENT_ADD events in your event queue. Those
758 * clients have already connected and will not be affected by this call.
759 * Only clients subsequently trying to connect will be affected.
760 * @param svr The given server.
761 * @param client_limit The maximum number of clients to handle
762 * concurrently. -1 means unlimited (default). 0
763 * effectively disables the server.
764 * @param reject_excess_clients Set to 1 to automatically disconnect
765 * excess clients as soon as they connect if you are
766 * already handling client_limit clients. Set to 0
767 * (default) to just hold off on the "accept()"
768 * system call until the number of active clients
769 * drops. This causes the kernel to queue up to 4096
770 * connections (or your kernel's limit, whichever is
774 ecore_con_server_client_limit_set(Ecore_Con_Server *svr,
776 char reject_excess_clients)
778 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
780 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
781 "ecore_con_server_client_limit_set");
785 svr->client_limit = client_limit;
786 svr->reject_excess_clients = reject_excess_clients;
790 * Gets the IP address of a server that has been connected to.
792 * @param svr The given server.
793 * @return A pointer to an internal string that contains the IP address of
794 * the connected server in the form "XXX.YYY.ZZZ.AAA" IP notation.
795 * This string should not be modified or trusted to stay valid after
796 * deletion for the @p svr object. If no IP is known NULL is returned.
799 ecore_con_server_ip_get(Ecore_Con_Server *svr)
801 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
803 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_ip_get");
811 * @brief Check how long a server has been connected
812 * @param svr The server to check
813 * @return The total time, in seconds, that the server has been connected/running
814 * This function is used to find out how long a server has been connected/running for.
817 ecore_con_server_uptime_get(Ecore_Con_Server *svr)
819 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
821 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_uptime_get");
825 return ecore_time_get() - svr->start_time;
829 * Flushes all pending data to the given server. Will return when done.
831 * @param svr The given server.
834 ecore_con_server_flush(Ecore_Con_Server *svr)
836 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
838 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_flush");
842 _ecore_con_server_flush(svr);
850 * @addtogroup Ecore_Con_Client_Group Ecore Connection Client Functions
852 * Functions that operate on Ecore connection client objects.
858 * @example ecore_con_client_example.c
859 * Shows how to write a simple client that connects to the example server.
863 * Sends the given data to the given client.
864 * @param cl The given client.
865 * @param data The given data.
866 * @param size Length of the data, in bytes, to send.
867 * @return The number of bytes sent. @c 0 will be returned if there is an
871 ecore_con_client_send(Ecore_Con_Client *cl,
875 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
877 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_send");
881 EINA_SAFETY_ON_TRUE_RETURN_VAL(cl->dead, 0);
883 EINA_SAFETY_ON_NULL_RETURN_VAL(data, 0);
885 EINA_SAFETY_ON_TRUE_RETURN_VAL(size < 1, 0);
888 ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
890 if (cl->host_server && ((cl->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_UDP))
891 sendto(cl->host_server->fd, data, size, 0, (struct sockaddr *)cl->client_addr,
892 cl->client_addr_len);
895 unsigned char *newbuf;
897 newbuf = realloc(cl->buf, cl->buf_size + size);
898 EINA_SAFETY_ON_NULL_RETURN_VAL(newbuf, 0);
902 memcpy(cl->buf + cl->buf_size, data, size);
903 cl->buf_size += size;
907 cl->buf = malloc(size);
908 EINA_SAFETY_ON_NULL_RETURN_VAL(cl->buf, 0);
911 memcpy(cl->buf, data, size);
918 * Retrieves the server representing the socket the client has
920 * @param cl The given client.
921 * @return The server that the client connected to.
923 EAPI Ecore_Con_Server *
924 ecore_con_client_server_get(Ecore_Con_Client *cl)
926 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
928 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
929 "ecore_con_client_server_get");
933 return cl->host_server;
937 * Returns whether the client is still connected
938 * @param cl The given client.
939 * @return #EINA_TRUE if connected, else EINA_FALSE
942 ecore_con_client_connected_get(Ecore_Con_Client *cl)
944 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
946 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
947 "ecore_con_client_connected_get");
955 * Set the time after which the client will be disconnected when inactive
956 * @param cl The client object
957 * @param timeout The timeout, in seconds, to disconnect after
958 * This function is used to set the idle timeout on a client. A value of < 1
959 * disables the idle timeout.
962 ecore_con_client_timeout_set(Ecore_Con_Client *cl,
965 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
967 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
968 "ecore_con_client_timeout_set");
972 cl->disconnect_time = timeout;
974 _ecore_con_cl_timer_update(cl);
978 * Get the default time after which the client will be disconnected when inactive
979 * @param cl The client object
980 * @return The timeout, in seconds, to disconnect after
981 * This function is used to get the idle timeout for a client. A value of < 1
982 * means the idle timeout is disabled.
985 ecore_con_client_timeout_get(Ecore_Con_Client *cl)
987 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
989 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_timeout_get");
993 return cl->disconnect_time;
997 * Closes the connection and frees memory allocated to the given client.
998 * @param cl The given client.
999 * @return Data associated with the client.
1002 ecore_con_client_del(Ecore_Con_Client *cl)
1006 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1008 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_del");
1012 if (cl->client_addr)
1013 free(cl->client_addr);
1014 cl->client_addr = NULL;
1017 cl->delete_me = EINA_TRUE;
1018 if (cl->event_count > 0)
1022 ecore_main_fd_handler_del(cl->fd_handler);
1023 cl->fd_handler = NULL;
1028 if (cl->host_server)
1030 cl->host_server->clients = eina_list_remove(cl->host_server->clients, cl);
1031 --cl->host_server->client_count;
1034 _ecore_con_client_free(cl);
1041 * Sets the data associated with the given client to @p data.
1042 * @param cl The given client.
1043 * @param data What to set the data to.
1046 ecore_con_client_data_set(Ecore_Con_Client *cl,
1049 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1051 ECORE_MAGIC_FAIL(cl,
1052 ECORE_MAGIC_CON_CLIENT,
1053 "ecore_con_client_data_set");
1057 cl->data = (void *)data;
1061 * Retrieves the data associated with the given client.
1062 * @param cl The given client.
1063 * @return The data associated with @p cl.
1066 ecore_con_client_data_get(Ecore_Con_Client *cl)
1068 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1070 ECORE_MAGIC_FAIL(cl,
1071 ECORE_MAGIC_CON_CLIENT,
1072 "ecore_con_client_data_get");
1080 * Gets the IP address of a cleint that has connected.
1082 * @param cl The given client.
1083 * @return A pointer to an internal string that contains the IP address of
1084 * the connected client in the form "XXX.YYY.ZZZ.AAA" IP notation.
1085 * This string should not be modified or trusted to stay valid after
1086 * deletion for the @p cl object. If no IP is known NULL is returned.
1089 ecore_con_client_ip_get(Ecore_Con_Client *cl)
1091 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1093 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_ip_get");
1097 cl->ip = _ecore_con_pretty_ip(cl->client_addr, cl->client_addr_len);
1103 * @brief Check how long a client has been connected
1104 * @param cl The client to check
1105 * @return The total time, in seconds, that the client has been connected to the server
1106 * This function is used to find out how long a client has been connected for.
1109 ecore_con_client_uptime_get(Ecore_Con_Client *cl)
1111 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1113 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_uptime_get");
1117 return ecore_time_get() - cl->start_time;
1121 * Flushes all pending data to the given client. Will return when done.
1123 * @param cl The given client.
1126 ecore_con_client_flush(Ecore_Con_Client *cl)
1128 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1130 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_flush");
1134 _ecore_con_client_flush(cl);
1142 _ecore_con_server_free(Ecore_Con_Server *svr)
1144 Ecore_Con_Client *cl;
1147 if ((!svr->write_buf) && svr->delete_me && (!svr->dead) && (svr->event_count < 1))
1149 /* this is a catch-all for cases when a server is not properly killed. */
1151 Ecore_Con_Event_Server_Del *e;
1153 svr->dead = EINA_TRUE;
1154 e = calloc(1, sizeof(Ecore_Con_Event_Server_Del));
1155 EINA_SAFETY_ON_NULL_RETURN(e);
1159 ecore_event_add(ECORE_CON_EVENT_SERVER_DEL, e,
1160 _ecore_con_event_server_del_free, NULL);
1164 if (svr->event_count > 0)
1166 ECORE_MAGIC_SET(svr, ECORE_MAGIC_NONE);
1167 t_start = ecore_time_get();
1168 while ((svr->write_buf) && (!svr->dead))
1170 _ecore_con_server_flush(svr);
1171 t = ecore_time_get();
1172 if ((t - t_start) > 0.5)
1174 WRN("ECORE_CON: EEK - stuck in _ecore_con_server_free() trying\n"
1175 " to flush data out from the server, and have been for\n"
1176 " %1.1f seconds. This is taking too long. Aborting flush.",
1183 free(svr->write_buf);
1185 EINA_LIST_FREE(svr->clients, cl)
1186 _ecore_con_client_free(cl);
1187 if ((svr->created) && (svr->path) && (svr->ppid == getpid()))
1190 ecore_con_ssl_server_shutdown(svr);
1201 eina_stringshare_del(svr->ip);
1203 if (svr->fd_handler)
1204 ecore_main_fd_handler_del(svr->fd_handler);
1206 servers = eina_list_remove(servers, svr);
1212 _ecore_con_client_free(Ecore_Con_Client *cl)
1216 if ((!cl->buf) && cl->delete_me && (!cl->dead) && (cl->event_count < 1))
1218 /* this is a catch-all for cases when a client is not properly killed. */
1220 /* we lost our client! */
1221 Ecore_Con_Event_Client_Del *e;
1223 cl->dead = EINA_TRUE;
1224 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
1225 EINA_SAFETY_ON_NULL_RETURN(e);
1228 _ecore_con_cl_timer_update(cl);
1230 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
1231 _ecore_con_event_client_del_free, NULL);
1235 if (cl->event_count > 0)
1237 ECORE_MAGIC_SET(cl, ECORE_MAGIC_NONE);
1238 t_start = ecore_time_get();
1239 while ((cl->buf) && (!cl->dead))
1241 _ecore_con_client_flush(cl);
1242 t = ecore_time_get();
1243 if ((t - t_start) > 0.5)
1245 WRN("EEK - stuck in _ecore_con_client_free() trying\n"
1246 " to flush data out from the client, and have been for\n"
1247 " %1.1f seconds. This is taking too long. Aborting flush.",
1255 if (cl->host_server->type & ECORE_CON_SSL)
1256 ecore_con_ssl_client_shutdown(cl);
1262 ecore_main_fd_handler_del(cl->fd_handler);
1265 eina_stringshare_del(cl->ip);
1272 _ecore_con_server_kill(Ecore_Con_Server *svr)
1274 if (!svr->delete_me)
1276 Ecore_Con_Event_Server_Del *e;
1278 e = calloc(1, sizeof(Ecore_Con_Event_Server_Del));
1279 EINA_SAFETY_ON_NULL_RETURN(e);
1283 ecore_event_add(ECORE_CON_EVENT_SERVER_DEL, e,
1284 _ecore_con_event_server_del_free, NULL);
1287 svr->dead = EINA_TRUE;
1288 if (svr->fd_handler)
1289 ecore_main_fd_handler_del(svr->fd_handler);
1291 svr->fd_handler = NULL;
1295 _ecore_con_client_timer(Ecore_Con_Client *cl)
1297 ecore_con_client_del(cl);
1299 return ECORE_CALLBACK_CANCEL;
1303 _ecore_con_cl_timer_update(Ecore_Con_Client *cl)
1305 if (cl->disconnect_time)
1307 if (cl->disconnect_time > 0)
1309 if (cl->until_deletion)
1310 ecore_timer_interval_set(cl->until_deletion, cl->disconnect_time);
1312 cl->until_deletion = ecore_timer_add(cl->disconnect_time, (Ecore_Task_Cb)_ecore_con_client_timer, cl);
1314 else if (cl->until_deletion)
1316 ecore_timer_del(cl->until_deletion);
1317 cl->until_deletion = NULL;
1322 if (cl->host_server->client_disconnect_time > 0)
1324 if (cl->until_deletion)
1325 ecore_timer_interval_set(cl->until_deletion, cl->host_server->client_disconnect_time);
1327 cl->until_deletion = ecore_timer_add(cl->host_server->client_disconnect_time, (Ecore_Task_Cb)_ecore_con_client_timer, cl);
1329 else if (cl->until_deletion)
1331 ecore_timer_del(cl->until_deletion);
1332 cl->until_deletion = NULL;
1338 _ecore_con_cb_tcp_listen(void *data,
1339 Ecore_Con_Info *net_info)
1341 Ecore_Con_Server *svr;
1349 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1350 net_info->info.ai_protocol);
1354 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1357 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1362 if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, (const void *)&lin,
1363 sizeof(struct linger)) < 0)
1366 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_NODELAY)
1370 if (setsockopt(svr->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
1375 if (bind(svr->fd, net_info->info.ai_addr,
1376 net_info->info.ai_addrlen) < 0)
1379 if (listen(svr->fd, 4096) < 0)
1382 svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1383 _ecore_con_svr_tcp_handler, svr, NULL, NULL);
1384 if (!svr->fd_handler)
1390 ecore_con_ssl_server_shutdown(svr);
1391 _ecore_con_server_kill(svr);
1395 _ecore_con_cb_udp_listen(void *data,
1396 Ecore_Con_Info *net_info)
1398 Ecore_Con_Server *svr;
1399 Ecore_Con_Type type;
1400 struct ip_mreq mreq;
1401 struct ipv6_mreq mreq6;
1406 type &= ECORE_CON_TYPE;
1411 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1412 net_info->info.ai_protocol);
1416 if (type == ECORE_CON_REMOTE_MCAST)
1418 if (net_info->info.ai_family == AF_INET)
1420 if (!inet_pton(net_info->info.ai_family, net_info->ip,
1421 &mreq.imr_multiaddr))
1424 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
1425 if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1426 (const void *)&mreq, sizeof(mreq)) != 0)
1429 else if (net_info->info.ai_family == AF_INET6)
1431 if (!inet_pton(net_info->info.ai_family, net_info->ip,
1432 &mreq6.ipv6mr_multiaddr))
1435 mreq6.ipv6mr_interface = htonl(INADDR_ANY);
1436 if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1437 (const void *)&mreq6, sizeof(mreq6)) != 0)
1441 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on,
1446 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1449 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1452 if (bind(svr->fd, net_info->info.ai_addr,
1453 net_info->info.ai_addrlen) < 0)
1457 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1458 _ecore_con_svr_udp_handler, svr, NULL, NULL);
1459 if (!svr->fd_handler)
1462 svr->ip = eina_stringshare_add(net_info->ip);
1467 ecore_con_ssl_server_shutdown(svr);
1468 _ecore_con_server_kill(svr);
1472 _ecore_con_cb_tcp_connect(void *data,
1473 Ecore_Con_Info *net_info)
1475 Ecore_Con_Server *svr;
1484 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1485 net_info->info.ai_protocol);
1489 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1492 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1495 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&curstate,
1496 sizeof(curstate)) < 0)
1499 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_NODELAY)
1503 if (setsockopt(svr->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
1508 res = connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen);
1510 if (res == SOCKET_ERROR)
1512 if (WSAGetLastError() != WSAEINPROGRESS)
1518 if (errno != EINPROGRESS)
1522 svr->connecting = EINA_TRUE;
1524 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
1525 _ecore_con_cl_handler, svr, NULL, NULL);
1528 svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1529 _ecore_con_cl_handler, svr, NULL, NULL);
1531 if (svr->type & ECORE_CON_SSL)
1533 svr->handshaking = EINA_TRUE;
1534 svr->ssl_state = ECORE_CON_SSL_STATE_INIT;
1535 DBG("beginning ssl handshake");
1536 if (ecore_con_ssl_server_init(svr))
1540 if (!svr->fd_handler)
1543 svr->ip = eina_stringshare_add(net_info->ip);
1548 ecore_con_ssl_server_shutdown(svr);
1549 _ecore_con_server_kill(svr);
1553 _ecore_con_cb_udp_connect(void *data,
1554 Ecore_Con_Info *net_info)
1556 Ecore_Con_Server *svr;
1564 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1565 net_info->info.ai_protocol);
1569 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1572 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1575 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_BROADCAST)
1577 if (setsockopt(svr->fd, SOL_SOCKET, SO_BROADCAST,
1578 (const void *)&broadcast,
1579 sizeof(broadcast)) < 0)
1582 else if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR,
1583 (const void *)&curstate, sizeof(curstate)) < 0)
1586 if (connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0)
1589 svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
1590 _ecore_con_cl_udp_handler, svr, NULL, NULL);
1592 if (!svr->fd_handler)
1595 svr->ip = eina_stringshare_add(net_info->ip);
1600 ecore_con_ssl_server_shutdown(svr);
1601 _ecore_con_server_kill(svr);
1604 static Ecore_Con_State
1605 svr_try_connect_plain(Ecore_Con_Server *svr)
1609 unsigned int size = sizeof(int);
1612 res = getsockopt(svr->fd, SOL_SOCKET, SO_ERROR, (void *)&so_err, &size);
1614 if (res == SOCKET_ERROR)
1615 so_err = WSAGetLastError();
1617 if ((so_err == WSAEINPROGRESS) && !svr->dead)
1618 return ECORE_CON_INPROGRESS;
1624 if ((so_err == EINPROGRESS) && !svr->dead)
1625 return ECORE_CON_INPROGRESS;
1631 /* we lost our server! */
1632 ERR("Connection lost: %s", strerror(so_err));
1633 _ecore_con_server_kill(svr);
1634 return ECORE_CON_DISCONNECTED;
1637 if ((!svr->delete_me) && (!svr->handshaking) && svr->connecting)
1639 /* we got our server! */
1640 Ecore_Con_Event_Server_Add *e;
1642 svr->connecting = EINA_FALSE;
1643 e = calloc(1, sizeof(Ecore_Con_Event_Server_Add));
1644 EINA_SAFETY_ON_NULL_RETURN_VAL(e, ECORE_CON_CONNECTED);
1647 svr->start_time = ecore_time_get();
1649 ecore_event_add(ECORE_CON_EVENT_SERVER_ADD, e,
1650 _ecore_con_event_server_add_free, NULL);
1653 if (svr->fd_handler && (!svr->write_buf))
1654 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
1657 return ECORE_CON_CONNECTED;
1659 return ECORE_CON_DISCONNECTED;
1663 _ecore_con_pretty_ip(struct sockaddr *client_addr,
1666 char ipbuf[INET6_ADDRSTRLEN + 1];
1668 /* show v4mapped address in pretty form */
1669 if (client_addr->sa_family == AF_INET6)
1671 struct sockaddr_in6 *sa6;
1673 sa6 = (struct sockaddr_in6 *)client_addr;
1674 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr))
1676 snprintf(ipbuf, sizeof (ipbuf), "%u.%u.%u.%u",
1677 sa6->sin6_addr.s6_addr[12],
1678 sa6->sin6_addr.s6_addr[13],
1679 sa6->sin6_addr.s6_addr[14],
1680 sa6->sin6_addr.s6_addr[15]);
1681 return eina_stringshare_add(ipbuf);
1685 if (getnameinfo(client_addr, size, ipbuf, sizeof (ipbuf), NULL, 0, NI_NUMERICHOST))
1686 return eina_stringshare_add("0.0.0.0");
1688 ipbuf[sizeof (ipbuf) - 1] = 0;
1689 return eina_stringshare_add(ipbuf);
1693 _ecore_con_svr_tcp_handler(void *data,
1694 Ecore_Fd_Handler *fd_handler __UNUSED__)
1696 Ecore_Con_Server *svr;
1697 Ecore_Con_Client *cl = NULL;
1698 unsigned char client_addr[256];
1699 unsigned int client_addr_len = sizeof(client_addr);
1704 return ECORE_CALLBACK_RENEW;
1707 return ECORE_CALLBACK_RENEW;
1709 if ((svr->client_limit >= 0) && (!svr->reject_excess_clients) &&
1710 (svr->client_count >= (unsigned int)svr->client_limit))
1711 return ECORE_CALLBACK_RENEW;
1714 memset(&client_addr, 0, client_addr_len);
1715 new_fd = accept(svr->fd, (struct sockaddr *)&client_addr, (socklen_t *)&client_addr_len);
1718 return ECORE_CALLBACK_RENEW;
1720 if ((svr->client_limit >= 0) && (svr->reject_excess_clients) &&
1721 (svr->client_count >= (unsigned int)svr->client_limit))
1724 cl = calloc(1, sizeof(Ecore_Con_Client));
1728 fcntl(new_fd, F_SETFL, O_NONBLOCK);
1729 fcntl(new_fd, F_SETFD, FD_CLOEXEC);
1731 cl->host_server = svr;
1733 cl->fd_handler = ecore_main_fd_handler_add(cl->fd, ECORE_FD_READ,
1734 _ecore_con_svr_cl_handler, cl, NULL, NULL);
1735 ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
1737 if (svr->type & ECORE_CON_SSL)
1739 cl->handshaking = EINA_TRUE;
1740 cl->ssl_state = ECORE_CON_SSL_STATE_INIT;
1741 if (ecore_con_ssl_client_init(cl))
1745 cl->client_addr = malloc(client_addr_len);
1746 if (!cl->client_addr)
1748 cl->client_addr_len = client_addr_len;
1749 memcpy(cl->client_addr, &client_addr, client_addr_len);
1751 svr->clients = eina_list_append(svr->clients, cl);
1752 svr->client_count++;
1754 if ((!cl->delete_me) && (!cl->handshaking))
1756 Ecore_Con_Event_Client_Add *e;
1758 e = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
1759 EINA_SAFETY_ON_NULL_RETURN_VAL(e, ECORE_CALLBACK_RENEW);
1762 _ecore_con_cl_timer_update(cl);
1764 ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD, e,
1765 _ecore_con_event_client_add_free, NULL);
1769 return ECORE_CALLBACK_RENEW;
1774 ecore_main_fd_handler_del(cl->fd_handler);
1775 return ECORE_CALLBACK_RENEW;
1779 _ecore_con_cl_read(Ecore_Con_Server *svr)
1783 Eina_Bool lost_server = EINA_TRUE;
1784 unsigned char buf[READBUFSIZ];
1786 /* only possible with non-ssl connections */
1787 if (svr->connecting && (svr_try_connect_plain(svr) != ECORE_CON_CONNECTED))
1790 if (svr->handshaking)
1792 DBG("Continuing ssl handshake");
1793 if (!ecore_con_ssl_server_init(svr))
1794 lost_server = EINA_FALSE;
1797 if (!(svr->type & ECORE_CON_SSL))
1800 num = read(svr->fd, buf, sizeof(buf));
1801 if ((num > 0) || (errno == EAGAIN))
1802 lost_server = EINA_FALSE;
1806 num = ecore_con_ssl_server_read(svr, buf, sizeof(buf));
1808 lost_server = EINA_FALSE;
1812 _ecore_con_server_kill(svr);
1814 if ((num > 0) && (!svr->delete_me))
1816 Ecore_Con_Event_Server_Data *e;
1818 e = malloc(sizeof(Ecore_Con_Event_Server_Data));
1819 EINA_SAFETY_ON_NULL_RETURN(e);
1823 e->data = malloc(num);
1830 memcpy(e->data, buf, num);
1832 ecore_event_add(ECORE_CON_EVENT_SERVER_DATA, e,
1833 _ecore_con_event_server_data_free, NULL);
1840 _ecore_con_cl_handler(void *data,
1841 Ecore_Fd_Handler *fd_handler)
1843 Ecore_Con_Server *svr;
1844 Eina_Bool want_read, want_write;
1848 return ECORE_CALLBACK_RENEW;
1851 return ECORE_CALLBACK_RENEW;
1853 want_read = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ);
1854 want_write = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE);
1856 if (svr->handshaking && (want_read || want_write))
1858 DBG("Continuing ssl handshake: preparing to %s...", want_read ? "read" : "write");
1862 char buf[READBUFSIZ];
1864 len = recv(svr->fd, buf, sizeof(buf), MSG_DONTWAIT | MSG_PEEK);
1865 DBG("%zu bytes in buffer", len);
1868 if (ecore_con_ssl_server_init(svr))
1870 ERR("ssl handshaking failed!");
1871 svr->handshaking = EINA_FALSE;
1872 Ecore_Con_Event_Server_Del *e;
1874 e = calloc(1, sizeof(Ecore_Con_Event_Server_Del));
1875 EINA_SAFETY_ON_NULL_RETURN_VAL(e, ECORE_CALLBACK_RENEW);
1879 ecore_event_add(ECORE_CON_EVENT_SERVER_DEL, e,
1880 _ecore_con_event_server_del_free, NULL);
1882 else if (!svr->ssl_state)
1884 /* we got our server! */
1885 Ecore_Con_Event_Server_Add *e;
1887 svr->connecting = EINA_FALSE;
1888 e = calloc(1, sizeof(Ecore_Con_Event_Server_Add));
1889 EINA_SAFETY_ON_NULL_RETURN_VAL(e, ECORE_CALLBACK_RENEW);
1892 svr->start_time = ecore_time_get();
1894 ecore_event_add(ECORE_CON_EVENT_SERVER_ADD, e,
1895 _ecore_con_event_server_add_free, NULL);
1899 _ecore_con_cl_read(svr);
1900 else if (want_write) /* only possible with non-ssl connections */
1902 if (svr->connecting && (!svr_try_connect_plain(svr)))
1903 return ECORE_CALLBACK_RENEW;
1905 _ecore_con_server_flush(svr);
1908 return ECORE_CALLBACK_RENEW;
1912 _ecore_con_cl_udp_handler(void *data,
1913 Ecore_Fd_Handler *fd_handler)
1915 Ecore_Con_Event_Server_Data *e;
1916 unsigned char *inbuf;
1917 unsigned char buf[READBUFSIZ];
1919 Ecore_Con_Server *svr;
1920 Eina_Bool want_read, want_write;
1922 want_read = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ);
1923 want_write = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE);
1926 if (svr->dead || svr->delete_me || ((!want_read) && (!want_write)))
1927 return ECORE_CALLBACK_RENEW;
1931 _ecore_con_server_flush(svr);
1932 return ECORE_CALLBACK_RENEW;
1936 num = read(svr->fd, buf, READBUFSIZ);
1938 if ((errno == EIO) || (errno == EBADF) || (errno == EPIPE) || (errno == EINVAL) ||
1939 (errno == ENOSPC) || (errno == ECONNREFUSED))
1940 _ecore_con_server_kill(svr);
1942 if ((num < 1) || (svr->delete_me))
1943 return ECORE_CALLBACK_RENEW;
1945 inbuf = malloc(num);
1947 return ECORE_CALLBACK_RENEW;
1949 memcpy(inbuf, buf, num);
1951 e = calloc(1, sizeof(Ecore_Con_Event_Server_Data));
1952 EINA_SAFETY_ON_NULL_RETURN_VAL(e, ECORE_CALLBACK_RENEW);
1958 ecore_event_add(ECORE_CON_EVENT_SERVER_DATA, e,
1959 _ecore_con_event_server_data_free, NULL);
1961 return ECORE_CALLBACK_RENEW;
1965 _ecore_con_svr_udp_handler(void *data,
1966 Ecore_Fd_Handler *fd_handler)
1968 unsigned char buf[READBUFSIZ];
1969 unsigned char client_addr[256];
1970 unsigned int client_addr_len = sizeof(client_addr);
1972 Ecore_Con_Server *svr;
1973 Ecore_Con_Client *cl = NULL;
1977 if (svr->delete_me || svr->dead)
1978 return ECORE_CALLBACK_RENEW;
1980 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
1982 _ecore_con_client_flush(cl);
1983 return ECORE_CALLBACK_RENEW;
1986 if (!ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
1987 return ECORE_CALLBACK_RENEW;
1991 num = fcntl(svr->fd, F_SETFL, O_NONBLOCK);
1993 num = recvfrom(svr->fd, buf, sizeof(buf), 0,
1994 (struct sockaddr *)&client_addr,
1998 num = recvfrom(svr->fd, buf, sizeof(buf), MSG_DONTWAIT,
1999 (struct sockaddr *)&client_addr,
2003 if ((errno == EIO) || (errno == EBADF) || (errno == EPIPE) ||
2004 (errno == EINVAL) || (errno == ENOSPC) || (errno == ECONNREFUSED))
2006 if (!svr->delete_me)
2008 /* we lost our client! */
2009 Ecore_Con_Event_Client_Del *e;
2011 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
2012 EINA_SAFETY_ON_NULL_RETURN_VAL(e, ECORE_CALLBACK_RENEW);
2015 /* be explicit here */
2017 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
2018 _ecore_con_event_client_del_free, NULL);
2021 svr->dead = EINA_TRUE;
2022 svr->fd_handler = NULL;
2023 return ECORE_CALLBACK_CANCEL;
2027 /* Create a new client for use in the client data event */
2028 cl = calloc(1, sizeof(Ecore_Con_Client));
2029 EINA_SAFETY_ON_NULL_RETURN_VAL(cl, ECORE_CALLBACK_RENEW);
2031 cl->host_server = svr;
2032 cl->client_addr = malloc(client_addr_len);
2033 if (!cl->client_addr)
2036 return ECORE_CALLBACK_RENEW;
2038 cl->client_addr_len = client_addr_len;
2040 memcpy(cl->client_addr, &client_addr, client_addr_len);
2041 ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
2042 svr->clients = eina_list_append(svr->clients, cl);
2043 svr->client_count++;
2045 { /* indent to keep it all nicely separated */
2046 Ecore_Con_Event_Client_Add *add;
2048 add = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
2049 EINA_SAFETY_ON_NULL_RETURN_VAL(add, ECORE_CALLBACK_RENEW);
2051 /*cl->event_count++;*/
2053 _ecore_con_cl_timer_update(cl);
2054 ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD, add,
2055 _ecore_con_event_client_add_free, NULL);
2060 Ecore_Con_Event_Client_Data *e;
2061 e = calloc(1, sizeof(Ecore_Con_Event_Client_Data));
2062 EINA_SAFETY_ON_NULL_RETURN_VAL(e, ECORE_CALLBACK_RENEW);
2065 _ecore_con_cl_timer_update(cl);
2067 e->data = malloc(num);
2070 free(cl->client_addr);
2072 return ECORE_CALLBACK_RENEW;
2075 memcpy(e->data, buf, num);
2077 ecore_event_add(ECORE_CON_EVENT_CLIENT_DATA, e,
2078 _ecore_con_event_client_data_free, NULL);
2081 return ECORE_CALLBACK_RENEW;
2085 _ecore_con_svr_cl_read(Ecore_Con_Client *cl)
2089 Eina_Bool lost_client = EINA_TRUE;
2090 unsigned char buf[READBUFSIZ];
2092 if (cl->handshaking)
2094 /* add an extra handshake attempt just before read, even though
2095 * read also attempts to handshake, to try to finish sooner
2097 if (ecore_con_ssl_client_init(cl))
2098 lost_client = EINA_FALSE;
2100 _ecore_con_cl_timer_update(cl);
2103 if (!(cl->host_server->type & ECORE_CON_SSL))
2106 num = read(cl->fd, buf, sizeof(buf));
2107 if ((num > 0) || (errno == EAGAIN))
2108 lost_client = EINA_FALSE;
2112 num = ecore_con_ssl_client_read(cl, buf, sizeof(buf));
2114 lost_client = EINA_FALSE;
2121 /* we lost our client! */
2122 Ecore_Con_Event_Client_Del *e;
2124 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
2125 EINA_SAFETY_ON_NULL_RETURN(e);
2128 _ecore_con_cl_timer_update(cl);
2130 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
2131 _ecore_con_event_client_del_free, NULL);
2133 INF("Lost client %s", (cl->ip) ? cl->ip : "");
2134 cl->dead = EINA_TRUE;
2136 ecore_main_fd_handler_del(cl->fd_handler);
2138 cl->fd_handler = NULL;
2142 if ((num > 0) && (!cl->delete_me))
2144 Ecore_Con_Event_Client_Data *e;
2146 e = malloc(sizeof(Ecore_Con_Event_Client_Data));
2147 EINA_SAFETY_ON_NULL_RETURN(e);
2150 _ecore_con_cl_timer_update(cl);
2152 e->data = malloc(num);
2159 memcpy(e->data, buf, num);
2161 ecore_event_add(ECORE_CON_EVENT_CLIENT_DATA, e,
2162 _ecore_con_event_client_data_free, NULL);
2169 _ecore_con_svr_cl_handler(void *data,
2170 Ecore_Fd_Handler *fd_handler)
2172 Ecore_Con_Client *cl;
2176 return ECORE_CALLBACK_RENEW;
2179 return ECORE_CALLBACK_RENEW;
2181 if (cl->handshaking && ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ | ECORE_FD_WRITE))
2183 if (ecore_con_ssl_client_init(cl))
2185 ERR("ssl handshaking failed!");
2186 cl->handshaking = EINA_FALSE;
2187 /* we lost our client! */
2188 Ecore_Con_Event_Client_Del *e;
2190 cl->dead = EINA_TRUE;
2191 INF("Lost client %s", (cl->ip) ? cl->ip : "");
2192 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
2193 EINA_SAFETY_ON_NULL_RETURN_VAL(e, ECORE_CALLBACK_RENEW);
2196 _ecore_con_cl_timer_update(cl);
2198 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
2199 _ecore_con_event_client_del_free, NULL);
2201 else if (!cl->ssl_state)
2203 Ecore_Con_Event_Client_Add *e;
2205 e = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
2206 EINA_SAFETY_ON_NULL_RETURN_VAL(e, ECORE_CALLBACK_RENEW);
2210 _ecore_con_cl_timer_update(cl);
2211 ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD, e,
2212 _ecore_con_event_client_add_free, NULL);
2215 else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
2216 _ecore_con_svr_cl_read(cl);
2218 else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
2219 _ecore_con_client_flush(cl);
2221 return ECORE_CALLBACK_RENEW;
2225 _ecore_con_server_flush(Ecore_Con_Server *svr)
2229 if (!svr->write_buf)
2232 /* check whether we need to write anything at all.
2233 * we must not write zero bytes with SSL_write() since it
2234 * causes undefined behaviour
2236 if (svr->write_buf_size == svr->write_buf_offset)
2239 num = svr->write_buf_size - svr->write_buf_offset;
2241 if (svr->handshaking)
2243 DBG("Continuing ssl handshake");
2244 if (ecore_con_ssl_server_init(svr))
2245 _ecore_con_server_kill(svr);
2249 if (!(svr->type & ECORE_CON_SSL))
2250 count = write(svr->fd, svr->write_buf + svr->write_buf_offset, num);
2252 count = ecore_con_ssl_server_write(svr, svr->write_buf + svr->write_buf_offset, num);
2256 /* we lost our server! */
2257 _ecore_con_server_kill(svr);
2261 svr->write_buf_offset += count;
2262 if (svr->write_buf_offset >= svr->write_buf_size)
2264 svr->write_buf_size = 0;
2265 svr->write_buf_offset = 0;
2266 free(svr->write_buf);
2267 svr->write_buf = NULL;
2268 if (svr->fd_handler)
2269 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
2271 else if (count < num)
2272 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_WRITE);
2276 _ecore_con_client_flush(Ecore_Con_Client *cl)
2283 if (cl->handshaking)
2285 if (ecore_con_ssl_client_init(cl))
2288 _ecore_con_cl_timer_update(cl);
2293 num = cl->buf_size - cl->buf_offset;
2294 if (!(cl->host_server->type & ECORE_CON_SSL))
2295 count = write(cl->fd, cl->buf + cl->buf_offset, num);
2297 count = ecore_con_ssl_client_write(cl, cl->buf + cl->buf_offset, num);
2302 if ((errno == EIO) || (errno == EBADF) || (errno == EPIPE) ||
2303 (errno == EINVAL) || (errno == ENOSPC) || (errno == ECONNREFUSED))
2306 /* we lost our client! */
2307 Ecore_Con_Event_Client_Del *e;
2309 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
2310 EINA_SAFETY_ON_NULL_RETURN(e);
2313 _ecore_con_cl_timer_update(cl);
2315 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
2316 _ecore_con_event_client_del_free, NULL);
2318 cl->dead = EINA_TRUE;
2319 INF("Lost client %s", (cl->ip) ? cl->ip : "");
2321 ecore_main_fd_handler_del(cl->fd_handler);
2323 cl->fd_handler = NULL;
2329 cl->buf_offset += count;
2330 if (cl->buf_offset >= cl->buf_size)
2337 ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ);
2339 else if (count < num)
2340 ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_WRITE);
2344 _ecore_con_event_client_add_free(void *data __UNUSED__,
2347 Ecore_Con_Event_Client_Add *e;
2350 e->client->event_count--;
2351 if ((e->client->event_count <= 0) &&
2352 (e->client->delete_me))
2353 ecore_con_client_del(e->client);
2359 _ecore_con_event_client_del_free(void *data __UNUSED__,
2362 Ecore_Con_Event_Client_Del *e;
2365 e->client->event_count--;
2366 if ((e->client->event_count <= 0) && (e->client->delete_me))
2367 ecore_con_client_del(e->client);
2373 _ecore_con_event_client_data_free(void *data __UNUSED__,
2376 Ecore_Con_Event_Client_Data *e;
2379 e->client->event_count--;
2383 if (((e->client->event_count <= 0) && (e->client->delete_me)) ||
2384 ((e->client->host_server &&
2385 ((e->client->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_UDP ||
2386 (e->client->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_MCAST))))
2387 ecore_con_client_del(e->client);
2393 _ecore_con_event_server_add_free(void *data __UNUSED__,
2396 Ecore_Con_Event_Server_Add *e;
2399 e->server->event_count--;
2400 if ((e->server->event_count <= 0) &&
2401 (e->server->delete_me))
2402 _ecore_con_server_free(e->server);
2408 _ecore_con_event_server_del_free(void *data __UNUSED__,
2411 Ecore_Con_Event_Server_Del *e;
2414 e->server->event_count--;
2415 if ((e->server->event_count <= 0) &&
2416 (e->server->delete_me))
2417 _ecore_con_server_free(e->server);
2423 _ecore_con_event_server_data_free(void *data __UNUSED__,
2426 Ecore_Con_Event_Server_Data *e;
2429 e->server->event_count--;
2433 if ((e->server->event_count <= 0) &&
2434 (e->server->delete_me))
2435 _ecore_con_server_free(e->server);
2441 _ecore_con_lookup_done(void *data,
2442 Ecore_Con_Info *infos)
2444 Ecore_Con_Server *svr;
2445 Ecore_Con_Lookup *lk;
2451 lk->done_cb(infos->info.ai_canonname, infos->ip,
2452 infos->info.ai_addr, infos->info.ai_addrlen,
2455 lk->done_cb(NULL, NULL, NULL, 0, (void *)lk->data);