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, Ecore_Con_Info *info);
50 static void _ecore_con_cb_udp_connect(void *data, Ecore_Con_Info *info);
51 static void _ecore_con_cb_tcp_listen(void *data, Ecore_Con_Info *info);
52 static void _ecore_con_cb_udp_listen(void *data, Ecore_Con_Info *info);
54 static void _ecore_con_server_free(Ecore_Con_Server *svr);
55 static void _ecore_con_client_free(Ecore_Con_Client *cl);
57 static Eina_Bool _ecore_con_svr_tcp_handler(void *data, Ecore_Fd_Handler *fd_handler);
58 static Eina_Bool _ecore_con_cl_handler(void *data, Ecore_Fd_Handler *fd_handler);
59 static Eina_Bool _ecore_con_cl_udp_handler(void *data, Ecore_Fd_Handler *fd_handler);
60 static Eina_Bool _ecore_con_svr_udp_handler(void *data, Ecore_Fd_Handler *fd_handler);
62 static void _ecore_con_svr_cl_read(Ecore_Con_Client *cl);
63 static Eina_Bool _ecore_con_svr_cl_handler(void *data, Ecore_Fd_Handler *fd_handler);
65 static void _ecore_con_server_flush(Ecore_Con_Server *svr);
66 static void _ecore_con_client_flush(Ecore_Con_Client *cl);
68 static void _ecore_con_event_client_add_free(void *data, void *ev);
69 static void _ecore_con_event_client_del_free(void *data, void *ev);
70 static void _ecore_con_event_client_data_free(void *data, void *ev);
71 static void _ecore_con_event_server_add_free(void *data, void *ev);
72 static void _ecore_con_event_server_del_free(void *data, void *ev);
73 static void _ecore_con_event_server_data_free(void *data, void *ev);
75 static void _ecore_con_lookup_done(void *data, Ecore_Con_Info *infos);
77 EAPI int ECORE_CON_EVENT_CLIENT_ADD = 0;
78 EAPI int ECORE_CON_EVENT_CLIENT_DEL = 0;
79 EAPI int ECORE_CON_EVENT_SERVER_ADD = 0;
80 EAPI int ECORE_CON_EVENT_SERVER_DEL = 0;
81 EAPI int ECORE_CON_EVENT_CLIENT_DATA = 0;
82 EAPI int ECORE_CON_EVENT_SERVER_DATA = 0;
84 static Eina_List *servers = NULL;
85 static int _ecore_con_init_count = 0;
86 int _ecore_con_log_dom = -1;
89 * @addtogroup Ecore_Con_Lib_Group Ecore Connection Library Functions
91 * Utility functions that set up and shut down the Ecore Connection
97 * Initialises the Ecore_Con library.
98 * @return Number of times the library has been initialised without being
104 if (++_ecore_con_init_count != 1)
105 return _ecore_con_init_count;
109 return --_ecore_con_init_count;
114 return --_ecore_con_init_count;
116 _ecore_con_log_dom = eina_log_domain_register
117 ("ecore_con", ECORE_CON_DEFAULT_LOG_COLOR);
118 if(_ecore_con_log_dom < 0)
120 EINA_LOG_ERR("Impossible to create a log domain for Ecore Con.");
122 return --_ecore_con_init_count;
125 ECORE_CON_EVENT_CLIENT_ADD = ecore_event_type_new();
126 ECORE_CON_EVENT_CLIENT_DEL = ecore_event_type_new();
127 ECORE_CON_EVENT_SERVER_ADD = ecore_event_type_new();
128 ECORE_CON_EVENT_SERVER_DEL = ecore_event_type_new();
129 ECORE_CON_EVENT_CLIENT_DATA = ecore_event_type_new();
130 ECORE_CON_EVENT_SERVER_DATA = ecore_event_type_new();
132 /* TODO Remember return value, if it fails, use gethostbyname() */
133 ecore_con_ssl_init();
134 ecore_con_info_init();
136 return _ecore_con_init_count;
140 * Shuts down the Ecore_Con library.
141 * @return Number of times the library has been initialised without being
145 ecore_con_shutdown(void)
147 if (--_ecore_con_init_count != 0)
148 return _ecore_con_init_count;
151 _ecore_con_server_free(eina_list_data_get(servers));
153 ecore_con_info_shutdown();
154 ecore_con_ssl_shutdown();
155 eina_log_domain_unregister(_ecore_con_log_dom);
156 _ecore_con_log_dom = -1;
162 return _ecore_con_init_count;
166 * Do an asynchronous DNS lookup.
168 * This function performs a DNS lookup on the hostname specified by @p name, then
169 * calls @p done_cb with
171 * @param name IP address or server name to translate.
172 * @param done_cb Callback to notify when done.
173 * @param data User data to be given to done_cb.
174 * @return EINA_TRUE if the request did not fail to be set up, EINA_FALSE if it failed.
177 ecore_con_lookup(const char *name, Ecore_Con_Dns_Cb done_cb, const void *data)
179 Ecore_Con_Server *svr;
180 Ecore_Con_Lookup *lk;
181 struct addrinfo hints;
183 if (!name || !done_cb)
186 svr = calloc(1, sizeof(Ecore_Con_Server));
190 lk = malloc(sizeof (Ecore_Con_Lookup));
197 lk->done_cb = done_cb;
200 svr->name = strdup(name);
204 svr->type = ECORE_CON_REMOTE_TCP;
207 svr->created = EINA_TRUE;
208 svr->reject_excess_clients = EINA_FALSE;
209 svr->client_limit = -1;
211 svr->ppid = getpid();
213 memset(&hints, 0, sizeof(struct addrinfo));
214 hints.ai_family = AF_INET6;
215 hints.ai_socktype = SOCK_STREAM;
216 hints.ai_flags = AI_CANONNAME;
217 hints.ai_protocol = IPPROTO_TCP;
218 hints.ai_canonname = NULL;
219 hints.ai_next = NULL;
220 hints.ai_addr = NULL;
222 if (ecore_con_info_get(svr, _ecore_con_lookup_done, svr,
239 * @addtogroup Ecore_Con_Server_Group Ecore Connection Server Functions
241 * Functions that operate on Ecore server objects.
247 * @example ecore_con_server_example.c
248 * Shows how to write a simple server using the Ecore_Con library.
252 * Creates a server to listen for connections.
254 * The socket on which the server listens depends on the connection
256 * @li If @a compl_type is @c ECORE_CON_LOCAL_USER, the server will listen on
257 * the Unix socket "~/.ecore/[name]/[port]".
258 * @li If @a compl_type is @c ECORE_CON_LOCAL_SYSTEM, the server will listen
259 * on Unix socket "/tmp/.ecore_service|[name]|[port]".
260 * @li If @a compl_type is @c ECORE_CON_REMOTE_TCP, the server will listen
261 * on TCP port @c port.
263 * @param compl_type The connection type.
264 * @param name Name to associate with the socket. It is used when
265 * generating the socket name of a Unix socket, or for
266 * determining what host to listen on for TCP sockets.
267 * @c NULL will not be accepted.
268 * @param port Number to identify socket. When a Unix socket is used,
269 * it becomes part of the socket name. When a TCP socket
270 * is used, it is used as the TCP port.
271 * @param data Data to associate with the created Ecore_Con_Server
273 * @return A new Ecore_Con_Server.
275 EAPI Ecore_Con_Server *
276 ecore_con_server_add(Ecore_Con_Type compl_type,
281 Ecore_Con_Server *svr;
284 if (port < 0 || !name)
285 return NULL; /* local user socket: FILE: ~/.ecore/[name]/[port] */
287 /* local system socket: FILE: /tmp/.ecore_service|[name]|[port] */
288 /* remote system socket: TCP/IP: [name]:[port] */
289 svr = calloc(1, sizeof(Ecore_Con_Server));
293 svr->name = strdup(name);
297 svr->type = compl_type;
299 svr->data = (void *)data;
300 svr->created = EINA_TRUE;
301 if (compl_type & ECORE_CON_LOAD_CERT)
302 svr->use_cert = EINA_TRUE;
303 svr->reject_excess_clients = EINA_FALSE;
304 svr->client_limit = -1;
306 svr->ppid = getpid();
307 if (ecore_con_ssl_server_prepare(svr, compl_type & ECORE_CON_SSL))
310 type = compl_type & ECORE_CON_TYPE;
312 if ((type == ECORE_CON_LOCAL_USER) ||
313 (type == ECORE_CON_LOCAL_SYSTEM) ||
314 (type == ECORE_CON_LOCAL_ABSTRACT))
316 if (!ecore_con_local_listen(svr, _ecore_con_svr_tcp_handler, svr))
319 if ((type == ECORE_CON_REMOTE_TCP) ||
320 (type == ECORE_CON_REMOTE_NODELAY))
323 if (!ecore_con_info_tcp_listen(svr, _ecore_con_cb_tcp_listen,
327 else if ((type == ECORE_CON_REMOTE_MCAST) ||
328 (type == ECORE_CON_REMOTE_UDP))
330 if (!ecore_con_info_udp_listen(svr, _ecore_con_cb_udp_listen,
334 servers = eina_list_append(servers, svr);
335 ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
351 ecore_main_fd_handler_del(svr->fd_handler);
354 free(svr->write_buf);
360 ecore_con_ssl_server_shutdown(svr);
366 * Creates a connection to the specified server and returns an associated object.
368 * The socket to which the connection is made depends on the connection type:
369 * @li If @a compl_type is @c ECORE_CON_LOCAL_USER, the function will
370 * connect to the server at the Unix socket
371 * "~/.ecore/[name]/[port]".
372 * @li If @a compl_type is @c ECORE_CON_LOCAL_SYSTEM, the function will
373 * connect to the server at the Unix socket
374 * "/tmp/.ecore_service|[name]|[port]".
375 * @li If @a compl_type is @c ECORE_CON_REMOTE_TCP, the function will
376 * connect to the server at the TCP port "[name]:[port]".
378 * @param compl_type The connection type.
379 * @param name Name used when determining what socket to connect to.
380 * It is used to generate the socket name when the socket
381 * is a Unix socket. It is used as the hostname when
382 * connecting with a TCP socket.
383 * @param port Number to identify the socket to connect to. Used when
384 * generating the socket name for a Unix socket, or as the
385 * TCP port when connecting to a TCP socket.
386 * @param data Data to associate with the created Ecore_Con_Server
388 * @return A new Ecore_Con_Server.
390 EAPI Ecore_Con_Server *
391 ecore_con_server_connect(Ecore_Con_Type compl_type,
396 Ecore_Con_Server *svr;
401 /* local user socket: FILE: ~/.ecore/[name]/[port] */
402 /* local system socket: FILE: /tmp/.ecore_service|[name]|[port] */
403 /* remote system socket: TCP/IP: [name]:[port] */
404 svr = calloc(1, sizeof(Ecore_Con_Server));
408 svr->name = strdup(name);
412 svr->type = compl_type;
414 svr->data = (void *)data;
415 svr->created = EINA_FALSE;
416 svr->use_cert = (compl_type & ECORE_CON_LOAD_CERT);
417 svr->reject_excess_clients = EINA_FALSE;
419 svr->client_limit = -1;
420 if (ecore_con_ssl_server_prepare(svr, compl_type & ECORE_CON_SSL))
423 type = compl_type & ECORE_CON_TYPE;
425 if (((type == ECORE_CON_REMOTE_TCP) ||
426 (type == ECORE_CON_REMOTE_NODELAY) ||
427 (type == ECORE_CON_REMOTE_UDP) ||
428 (type == ECORE_CON_REMOTE_BROADCAST)) &&
432 if ((type == ECORE_CON_LOCAL_USER) ||
433 (type == ECORE_CON_LOCAL_SYSTEM) ||
434 (type == ECORE_CON_LOCAL_ABSTRACT))
436 if (!ecore_con_local_connect(svr, _ecore_con_cl_handler, svr,
437 _ecore_con_event_server_add_free))
441 if ((type == ECORE_CON_REMOTE_TCP) ||
442 (type == ECORE_CON_REMOTE_NODELAY))
445 if (!ecore_con_info_tcp_connect(svr, _ecore_con_cb_tcp_connect,
449 else if ((type == ECORE_CON_REMOTE_UDP) ||
450 (type == ECORE_CON_REMOTE_BROADCAST))
452 if (!ecore_con_info_udp_connect(svr, _ecore_con_cb_udp_connect,
456 servers = eina_list_append(servers, svr);
457 ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
472 ecore_main_fd_handler_del(svr->fd_handler);
474 ecore_con_ssl_server_shutdown(svr);
480 * Set the default time after which an inactive client will be disconnected
481 * @param svr The server object
482 * @param timeout The timeout, in seconds, to disconnect after
483 * This function is used to set the idle timeout on clients. A value of < 1
484 * disables the idle timeout.
487 ecore_con_server_timeout_set(Ecore_Con_Server *svr, double timeout)
489 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
491 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_timeout_set");
495 svr->client_disconnect_time = timeout;
499 * Get the default time after which an inactive client will be disconnected
500 * @param svr The server object
501 * @return The timeout, in seconds, to disconnect after
502 * This function is used to get the idle timeout for clients. A value of < 1
503 * means the idle timeout is disabled.
506 ecore_con_server_timeout_get(Ecore_Con_Server *svr)
508 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
510 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_timeout_get");
514 return svr->client_disconnect_time;
518 * Closes the connection and frees the given server.
519 * @param svr The given server.
520 * @return Data associated with the server when it was created.
521 * @see ecore_con_server_add, ecore_con_server_connect
524 ecore_con_server_del(Ecore_Con_Server *svr)
528 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
530 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_del");
539 svr->delete_me = EINA_TRUE;
540 if (svr->event_count > 0)
544 ecore_main_fd_handler_del(svr->fd_handler);
545 svr->fd_handler = NULL;
549 _ecore_con_server_free(svr);
555 * Retrieves the data associated with the given server.
556 * @param svr The given server.
557 * @return The associated data.
560 ecore_con_server_data_get(Ecore_Con_Server *svr)
562 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
564 ECORE_MAGIC_FAIL(svr,
565 ECORE_MAGIC_CON_SERVER,
566 "ecore_con_server_data_get");
574 * Sets the data associated with the given server.
575 * @param svr The given server.
576 * @param data The data to associate with @p svr
577 * @return The previously associated data, if any.
580 ecore_con_server_data_set(Ecore_Con_Server *svr, void *data)
584 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
586 ECORE_MAGIC_FAIL(svr,
587 ECORE_MAGIC_CON_SERVER,
588 "ecore_con_server_data_get");
598 * Retrieves whether the given server is currently connected.
599 * @param svr The given server.
600 * @return #EINA_TRUE if the server is connected. #EINA_FALSE otherwise.
603 ecore_con_server_connected_get(Ecore_Con_Server *svr)
605 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
607 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
608 "ecore_con_server_connected_get");
619 * Retrieves the current list of clients.
620 * @param svr The given server.
621 * @return The list of clients on this server.
624 ecore_con_server_clients_get(Ecore_Con_Server *svr)
626 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
628 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
629 "ecore_con_server_clients_get");
637 * Retrieves the name of server.
638 * @param svr The given server.
639 * @return The name of the server.
642 ecore_con_server_name_get(Ecore_Con_Server *svr)
644 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
646 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
647 "ecore_con_server_name_get");
655 * Retrieves the server port in use.
656 * @param svr The given server.
657 * @return The server port in use.
660 ecore_con_server_port_get(Ecore_Con_Server *svr)
662 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
664 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
665 "ecore_con_server_port_get");
672 * Sends the given data to the given server.
673 * @param svr The given server.
674 * @param data The given data.
675 * @param size Length of the data, in bytes, to send.
676 * @return The number of bytes sent. @c 0 will be returned if there is an
680 ecore_con_server_send(Ecore_Con_Server *svr,
684 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
686 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_send");
700 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
704 unsigned char *newbuf;
706 newbuf = realloc(svr->write_buf, svr->write_buf_size + size);
708 svr->write_buf = newbuf;
712 memcpy(svr->write_buf + svr->write_buf_size, data, size);
713 svr->write_buf_size += size;
717 svr->write_buf = malloc(size);
721 svr->write_buf_size = size;
722 memcpy(svr->write_buf, data, size);
729 * Sets a limit on the number of clients that can be handled concurrently
730 * by the given server, and a policy on what to do if excess clients try to
732 * Beware that if you set this once ecore is already running, you may
733 * already have pending CLIENT_ADD events in your event queue. Those
734 * clients have already connected and will not be affected by this call.
735 * Only clients subsequently trying to connect will be affected.
736 * @param svr The given server.
737 * @param client_limit The maximum number of clients to handle
738 * concurrently. -1 means unlimited (default). 0
739 * effectively disables the server.
740 * @param reject_excess_clients Set to 1 to automatically disconnect
741 * excess clients as soon as they connect if you are
742 * already handling client_limit clients. Set to 0
743 * (default) to just hold off on the "accept()"
744 * system call until the number of active clients
745 * drops. This causes the kernel to queue up to 4096
746 * connections (or your kernel's limit, whichever is
750 ecore_con_server_client_limit_set(Ecore_Con_Server *svr,
752 char reject_excess_clients)
754 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
756 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
757 "ecore_con_server_client_limit_set");
761 svr->client_limit = client_limit;
762 svr->reject_excess_clients = reject_excess_clients;
766 * Gets the IP address of a server that has been connected to.
768 * @param svr The given server.
769 * @return A pointer to an internal string that contains the IP address of
770 * the connected server in the form "XXX.YYY.ZZZ.AAA" IP notation.
771 * This string should not be modified or trusted to stay valid after
772 * deletion for the @p svr object. If no IP is known NULL is returned.
775 ecore_con_server_ip_get(Ecore_Con_Server *svr)
777 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
779 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_ip_get");
788 * @brief Check how long a server has been connected
789 * @param svr The server to check
790 * @return The total time, in seconds, that the server has been connected/running
791 * This function is used to find out how long a server has been connected/running for.
794 ecore_con_server_uptime_get(Ecore_Con_Server *svr)
796 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
798 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_uptime_get");
802 return ecore_time_get() - svr->start_time;
805 * Flushes all pending data to the given server. Will return when done.
807 * @param svr The given server.
810 ecore_con_server_flush(Ecore_Con_Server *svr)
812 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
814 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_flush");
818 _ecore_con_server_flush(svr);
827 * @addtogroup Ecore_Con_Client_Group Ecore Connection Client Functions
829 * Functions that operate on Ecore connection client objects.
835 * @example ecore_con_client_example.c
836 * Shows how to write a simple client that connects to the example server.
840 * Sends the given data to the given client.
841 * @param cl The given client.
842 * @param data The given data.
843 * @param size Length of the data, in bytes, to send.
844 * @return The number of bytes sent. @c 0 will be returned if there is an
848 ecore_con_client_send(Ecore_Con_Client *cl,
852 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
854 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_send");
868 ecore_main_fd_handler_active_set(
869 cl->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
871 if(cl->host_server && ((cl->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_UDP))
872 sendto(cl->host_server->fd, data, size, 0, (struct sockaddr *)cl->client_addr,
873 cl->client_addr_len);
876 unsigned char *newbuf;
878 newbuf = realloc(cl->buf, cl->buf_size + size);
884 memcpy(cl->buf + cl->buf_size, data, size);
885 cl->buf_size += size;
889 cl->buf = malloc(size);
894 memcpy(cl->buf, data, size);
901 * Retrieves the server representing the socket the client has
903 * @param cl The given client.
904 * @return The server that the client connected to.
906 EAPI Ecore_Con_Server *
907 ecore_con_client_server_get(Ecore_Con_Client *cl)
909 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
911 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
912 "ecore_con_client_server_get");
916 return cl->host_server;
920 * Set the time after which the client will be disconnected when inactive
921 * @param cl The client object
922 * @param timeout The timeout, in seconds, to disconnect after
923 * This function is used to set the idle timeout on a client. A value of < 1
924 * disables the idle timeout.
927 ecore_con_client_timeout_set(Ecore_Con_Client *cl, double timeout)
929 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
931 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
932 "ecore_con_client_timeout_set");
936 cl->disconnect_time = timeout;
938 _ecore_con_cl_timer_update(cl);
942 * Get the default time after which the client will be disconnected when inactive
943 * @param cl The client object
944 * @return The timeout, in seconds, to disconnect after
945 * This function is used to get the idle timeout for a client. A value of < 1
946 * means the idle timeout is disabled.
949 ecore_con_client_timeout_get(Ecore_Con_Client *cl)
951 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
953 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_timeout_get");
957 return cl->disconnect_time;
961 * Closes the connection and frees memory allocated to the given client.
962 * @param cl The given client.
963 * @return Data associated with the client.
966 ecore_con_client_del(Ecore_Con_Client *cl)
970 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
972 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_del");
976 if (cl->client_addr && cl->host_server &&
977 (((cl->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_UDP) ||
978 ((cl->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_MCAST)))
979 free(cl->client_addr);
984 cl->delete_me = EINA_TRUE;
985 if (cl->event_count > 0)
989 ecore_main_fd_handler_del(cl->fd_handler);
990 cl->fd_handler = NULL;
997 cl->host_server->clients = eina_list_remove(cl->host_server->clients, cl);
998 --cl->host_server->client_count;
1001 _ecore_con_client_free(cl);
1008 * Sets the data associated with the given client to @p data.
1009 * @param cl The given client.
1010 * @param data What to set the data to.
1013 ecore_con_client_data_set(Ecore_Con_Client *cl, const void *data)
1015 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1017 ECORE_MAGIC_FAIL(cl,
1018 ECORE_MAGIC_CON_CLIENT,
1019 "ecore_con_client_data_set");
1023 cl->data = (void *)data;
1027 * Retrieves the data associated with the given client.
1028 * @param cl The given client.
1029 * @return The data associated with @p cl.
1032 ecore_con_client_data_get(Ecore_Con_Client *cl)
1034 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1036 ECORE_MAGIC_FAIL(cl,
1037 ECORE_MAGIC_CON_CLIENT,
1038 "ecore_con_client_data_get");
1046 * Gets the IP address of a cleint that has connected.
1048 * @param cl The given client.
1049 * @return A pointer to an internal string that contains the IP address of
1050 * the connected client in the form "XXX.YYY.ZZZ.AAA" IP notation.
1051 * This string should not be modified or trusted to stay valid after
1052 * deletion for the @p cl object. If no IP is known NULL is returned.
1055 ecore_con_client_ip_get(Ecore_Con_Client *cl)
1057 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1059 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_ip_get");
1067 * @brief Check how long a client has been connected
1068 * @param cl The client to check
1069 * @return The total time, in seconds, that the client has been connected to the server
1070 * This function is used to find out how long a client has been connected for.
1073 ecore_con_client_uptime_get(Ecore_Con_Client *cl)
1075 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1077 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_uptime_get");
1081 return ecore_time_get() - cl->start_time;
1085 * Flushes all pending data to the given client. Will return when done.
1087 * @param cl The given client.
1090 ecore_con_client_flush(Ecore_Con_Client *cl)
1092 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
1094 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_flush");
1098 _ecore_con_client_flush(cl);
1106 _ecore_con_server_free(Ecore_Con_Server *svr)
1108 Ecore_Con_Client *cl;
1111 ECORE_MAGIC_SET(svr, ECORE_MAGIC_NONE);
1112 t_start = ecore_time_get();
1113 while ((svr->write_buf) && (!svr->dead))
1115 _ecore_con_server_flush(svr);
1116 t = ecore_time_get();
1117 if ((t - t_start) > 0.5)
1119 WRN("ECORE_CON: EEK - stuck in _ecore_con_server_free() trying\n"
1120 " to flush data out from the server, and have been for\n"
1121 " %1.1f seconds. This is taking too long. Aborting flush.",
1128 free(svr->write_buf);
1130 EINA_LIST_FREE(svr->clients, cl)
1131 _ecore_con_client_free(cl);
1132 if ((svr->created) && (svr->path) && (svr->ppid == getpid()))
1135 ecore_con_ssl_server_shutdown(svr);
1148 if (svr->fd_handler)
1149 ecore_main_fd_handler_del(svr->fd_handler);
1151 servers = eina_list_remove(servers, svr);
1156 _ecore_con_client_free(Ecore_Con_Client *cl)
1160 if ((!cl->buf) && cl->delete_me && (!cl->dead) && (cl->event_count < 1))
1162 /* this is a catch-all for cases when a client is not properly killed.
1163 * the only example case I've found so far is if a client ssl handshakes
1164 * and then immediately disconnects without sending any further data.
1167 /* we lost our client! */
1168 Ecore_Con_Event_Client_Del *e;
1170 cl->dead = EINA_TRUE;
1171 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
1175 _ecore_con_cl_timer_update(cl);
1177 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
1178 _ecore_con_event_client_del_free, NULL);
1183 ECORE_MAGIC_SET(cl, ECORE_MAGIC_NONE);
1184 t_start = ecore_time_get();
1185 while ((cl->buf) && (!cl->dead))
1187 _ecore_con_client_flush(cl);
1188 t = ecore_time_get();
1189 if ((t - t_start) > 0.5)
1191 WRN("EEK - stuck in _ecore_con_client_free() trying\n"
1192 " to flush data out from the client, and have been for\n"
1193 " %1.1f seconds. This is taking too long. Aborting flush.",
1201 if (cl->host_server->type & ECORE_CON_SSL)
1202 ecore_con_ssl_client_shutdown(cl);
1208 ecore_main_fd_handler_del(cl->fd_handler);
1218 _ecore_con_server_kill(Ecore_Con_Server *svr)
1220 if (!svr->delete_me)
1222 Ecore_Con_Event_Server_Del *e;
1224 e = calloc(1, sizeof(Ecore_Con_Event_Server_Del));
1229 ecore_event_add(ECORE_CON_EVENT_SERVER_DEL, e,
1230 _ecore_con_event_server_del_free, NULL);
1234 svr->dead = EINA_TRUE;
1235 if (svr->fd_handler)
1236 ecore_main_fd_handler_del(svr->fd_handler);
1238 svr->fd_handler = NULL;
1242 _ecore_con_client_timer(Ecore_Con_Client *cl)
1244 ecore_con_client_del(cl);
1246 return ECORE_CALLBACK_CANCEL;
1250 _ecore_con_cl_timer_update(Ecore_Con_Client *cl)
1252 if (cl->disconnect_time)
1254 if (cl->disconnect_time > 0)
1256 if (cl->until_deletion)
1257 ecore_timer_interval_set(cl->until_deletion, cl->disconnect_time);
1259 cl->until_deletion = ecore_timer_add(cl->disconnect_time, (Ecore_Task_Cb)_ecore_con_client_timer, cl);
1261 else if (cl->until_deletion)
1263 ecore_timer_del(cl->until_deletion);
1264 cl->until_deletion = NULL;
1269 if (cl->host_server->client_disconnect_time > 0)
1271 if (cl->until_deletion)
1272 ecore_timer_interval_set(cl->until_deletion, cl->host_server->client_disconnect_time);
1274 cl->until_deletion = ecore_timer_add(cl->host_server->client_disconnect_time, (Ecore_Task_Cb)_ecore_con_client_timer, cl);
1276 else if (cl->until_deletion)
1278 ecore_timer_del(cl->until_deletion);
1279 cl->until_deletion = NULL;
1285 _ecore_con_cb_tcp_listen(void *data, Ecore_Con_Info *net_info)
1287 Ecore_Con_Server *svr;
1295 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1296 net_info->info.ai_protocol);
1300 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1303 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1308 if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, (const void *)&lin,
1309 sizeof(struct linger)) < 0)
1312 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_NODELAY)
1316 if (setsockopt(svr->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
1321 if (bind(svr->fd, net_info->info.ai_addr,
1322 net_info->info.ai_addrlen) < 0)
1325 if (listen(svr->fd, 4096) < 0)
1328 svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1329 _ecore_con_svr_tcp_handler, svr, NULL, NULL);
1330 if (!svr->fd_handler)
1336 ecore_con_ssl_server_shutdown(svr);
1337 _ecore_con_server_kill(svr);
1341 _ecore_con_cb_udp_listen(void *data, Ecore_Con_Info *net_info)
1343 Ecore_Con_Server *svr;
1344 Ecore_Con_Type type;
1345 struct ip_mreq mreq;
1346 struct ipv6_mreq mreq6;
1351 type &= ECORE_CON_TYPE;
1356 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1357 net_info->info.ai_protocol);
1361 if (type == ECORE_CON_REMOTE_MCAST)
1363 if (net_info->info.ai_family == AF_INET)
1365 if (!inet_pton(net_info->info.ai_family, net_info->ip,
1366 &mreq.imr_multiaddr))
1369 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
1370 if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1371 (const void *)&mreq,sizeof(mreq)) != 0)
1374 else if (net_info->info.ai_family == AF_INET6)
1376 if (!inet_pton(net_info->info.ai_family, net_info->ip,
1377 &mreq6.ipv6mr_multiaddr))
1380 mreq6.ipv6mr_interface = htonl(INADDR_ANY);
1381 if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1382 (const void *)&mreq6,sizeof(mreq6)) != 0)
1386 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on,
1391 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1394 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1397 if (bind(svr->fd, net_info->info.ai_addr,
1398 net_info->info.ai_addrlen) < 0)
1402 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1403 _ecore_con_svr_udp_handler, svr, NULL, NULL);
1404 if (!svr->fd_handler)
1407 svr->ip = strdup(net_info->ip);
1412 ecore_con_ssl_server_shutdown(svr);
1413 _ecore_con_server_kill(svr);
1417 _ecore_con_cb_tcp_connect(void *data, Ecore_Con_Info *net_info)
1419 Ecore_Con_Server *svr;
1428 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1429 net_info->info.ai_protocol);
1433 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1436 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1439 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&curstate,
1440 sizeof(curstate)) < 0)
1443 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_NODELAY)
1447 if (setsockopt(svr->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
1452 res = connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen);
1454 if (res == SOCKET_ERROR)
1456 if (WSAGetLastError() != WSAEINPROGRESS)
1462 if (errno != EINPROGRESS)
1466 svr->connecting = EINA_TRUE;
1468 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
1469 _ecore_con_cl_handler, svr, NULL, NULL);
1472 svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1473 _ecore_con_cl_handler, svr, NULL, NULL);
1475 if (svr->type & ECORE_CON_SSL)
1477 svr->handshaking = EINA_TRUE;
1478 svr->ssl_state = ECORE_CON_SSL_STATE_INIT;
1479 DBG("beginning ssl handshake");
1480 if (ecore_con_ssl_server_init(svr))
1484 if (!svr->fd_handler)
1487 svr->ip = strdup(net_info->ip);
1492 ecore_con_ssl_server_shutdown(svr);
1493 _ecore_con_server_kill(svr);
1497 _ecore_con_cb_udp_connect(void *data, Ecore_Con_Info *net_info)
1499 Ecore_Con_Server *svr;
1507 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1508 net_info->info.ai_protocol);
1512 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1515 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1518 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_BROADCAST)
1520 if (setsockopt(svr->fd, SOL_SOCKET, SO_BROADCAST,
1521 (const void *)&broadcast,
1522 sizeof(broadcast)) < 0)
1525 else if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR,
1526 (const void *)&curstate, sizeof(curstate)) < 0)
1529 if (connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0)
1532 svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
1533 _ecore_con_cl_udp_handler, svr, NULL, NULL);
1535 if (!svr->fd_handler)
1538 svr->ip = strdup(net_info->ip);
1543 ecore_con_ssl_server_shutdown(svr);
1544 _ecore_con_server_kill(svr);
1547 static Ecore_Con_State
1548 svr_try_connect_plain(Ecore_Con_Server *svr)
1552 unsigned int size = sizeof(int);
1554 res = getsockopt(svr->fd, SOL_SOCKET, SO_ERROR, (void *)&so_err, &size);
1556 if (res == SOCKET_ERROR)
1559 if (so_err == WSAEINPROGRESS && !svr->dead)
1560 return ECORE_CON_INPROGRESS;
1566 if (so_err == EINPROGRESS && !svr->dead)
1567 return ECORE_CON_INPROGRESS;
1573 /* we lost our server! */
1574 _ecore_con_server_kill(svr);
1575 return ECORE_CON_DISCONNECTED;
1578 if ((!svr->delete_me) && (!svr->handshaking) && svr->connecting)
1580 /* we got our server! */
1581 Ecore_Con_Event_Server_Add *e;
1583 svr->connecting = EINA_FALSE;
1584 e = calloc(1, sizeof(Ecore_Con_Event_Server_Add));
1588 svr->start_time = ecore_time_get();
1590 ecore_event_add(ECORE_CON_EVENT_SERVER_ADD, e,
1591 _ecore_con_event_server_add_free, NULL);
1595 if (svr->fd_handler && (!svr->write_buf))
1596 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
1599 return ECORE_CON_CONNECTED;
1601 return ECORE_CON_DISCONNECTED;
1605 _ecore_con_pretty_ip(struct sockaddr *client_addr, socklen_t size)
1607 char ipbuf[INET6_ADDRSTRLEN + 1];
1609 /* show v4mapped address in pretty form */
1610 if (client_addr->sa_family == AF_INET6)
1612 struct sockaddr_in6 *sa6;
1614 sa6 = (struct sockaddr_in6 *)client_addr;
1615 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr))
1617 snprintf(ipbuf, sizeof (ipbuf), "%u.%u.%u.%u",
1618 sa6->sin6_addr.s6_addr[12],
1619 sa6->sin6_addr.s6_addr[13],
1620 sa6->sin6_addr.s6_addr[14],
1621 sa6->sin6_addr.s6_addr[15]);
1622 return strdup(ipbuf);
1626 if (getnameinfo(client_addr, size,
1627 ipbuf, sizeof (ipbuf), NULL, 0,
1629 return strdup("0.0.0.0");
1631 ipbuf[sizeof (ipbuf) - 1] = 0;
1632 return strdup(ipbuf);
1636 _ecore_con_svr_tcp_handler(void *data, Ecore_Fd_Handler *fd_handler __UNUSED__)
1638 Ecore_Con_Server *svr;
1639 Ecore_Con_Client *cl = NULL;
1641 unsigned char incoming[256];
1646 return ECORE_CALLBACK_RENEW;
1649 return ECORE_CALLBACK_RENEW;
1651 if ((svr->client_limit >= 0) && (!svr->reject_excess_clients) &&
1652 (svr->client_count >= (unsigned int)svr->client_limit))
1653 return ECORE_CALLBACK_RENEW;
1657 size_in = sizeof(incoming);
1659 memset(&incoming, 0, size_in);
1660 new_fd = accept(svr->fd, (struct sockaddr *)&incoming, (socklen_t *)&size_in);
1663 return ECORE_CALLBACK_RENEW;
1665 if ((svr->client_limit >= 0) && (svr->reject_excess_clients) &&
1666 (svr->client_count >= (unsigned int)svr->client_limit))
1669 cl = calloc(1, sizeof(Ecore_Con_Client));
1673 fcntl(new_fd, F_SETFL, O_NONBLOCK);
1674 fcntl(new_fd, F_SETFD, FD_CLOEXEC);
1676 cl->host_server = svr;
1678 cl->fd_handler = ecore_main_fd_handler_add(cl->fd, ECORE_FD_READ,
1679 _ecore_con_svr_cl_handler, cl, NULL, NULL);
1680 ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
1683 if (svr->type & ECORE_CON_SSL)
1685 cl->handshaking = EINA_TRUE;
1686 cl->ssl_state = ECORE_CON_SSL_STATE_INIT;
1687 if (ecore_con_ssl_client_init(cl))
1691 svr->clients = eina_list_append(svr->clients, cl);
1692 svr->client_count++;
1694 cl->ip = _ecore_con_pretty_ip((struct sockaddr *)&incoming, size_in);
1696 if ((!cl->delete_me) && (!cl->handshaking))
1698 Ecore_Con_Event_Client_Add *e;
1700 e = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
1704 _ecore_con_cl_timer_update(cl);
1706 ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD, e,
1707 _ecore_con_event_client_add_free, NULL);
1711 return ECORE_CALLBACK_RENEW;
1716 ecore_main_fd_handler_del(cl->fd_handler);
1717 return ECORE_CALLBACK_RENEW;
1722 _ecore_con_cl_read(Ecore_Con_Server *svr)
1724 unsigned char *inbuf = NULL;
1728 /* only possible with non-ssl connections */
1729 if (svr->connecting && (svr_try_connect_plain(svr) != ECORE_CON_CONNECTED))
1732 for (tries = 0; tries < 16; tries++)
1735 Eina_Bool lost_server = EINA_TRUE;
1736 unsigned char buf[READBUFSIZ];
1738 if (svr->handshaking)
1740 DBG("Continuing ssl handshake");
1741 if (!ecore_con_ssl_server_init(svr))
1742 lost_server = EINA_FALSE;
1745 if (!(svr->type & ECORE_CON_SSL))
1747 num = read(svr->fd, buf, READBUFSIZ);
1748 if ((num < 0) && (errno == EAGAIN))
1749 lost_server = EINA_FALSE;
1751 else if (!(num = ecore_con_ssl_server_read(svr, buf, READBUFSIZ)))
1752 lost_server = EINA_FALSE;
1756 if (inbuf && (!svr->delete_me))
1758 Ecore_Con_Event_Server_Data *e;
1760 e = malloc(sizeof(Ecore_Con_Event_Server_Data));
1766 e->size = inbuf_num;
1767 ecore_event_add(ECORE_CON_EVENT_SERVER_DATA, e,
1768 _ecore_con_event_server_data_free, NULL);
1773 _ecore_con_server_kill(svr);
1778 inbuf = realloc(inbuf, inbuf_num + num);
1779 memcpy(inbuf + inbuf_num, buf, num);
1783 /* #if USE_OPENSSL */
1784 /* if (svr->fd_handler) */
1786 /* if (svr->ssl && ssl_err == SSL_ERROR_WANT_READ) */
1787 /* ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ); */
1788 /* else if (svr->ssl && ssl_err == SSL_ERROR_WANT_WRITE) */
1789 /* ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_WRITE); */
1795 _ecore_con_cl_handler(void *data, Ecore_Fd_Handler *fd_handler)
1797 Ecore_Con_Server *svr;
1798 Eina_Bool want_read, want_write;
1802 return ECORE_CALLBACK_RENEW;
1805 return ECORE_CALLBACK_RENEW;
1807 want_read = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ);
1808 want_write = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE);
1810 if (svr->handshaking && (want_read || want_write))
1812 DBG("Continuing ssl handshake: preparing to %s...", want_read ? "read" : "write");
1813 #ifdef PRINT_LOTS_OF_DEBUG
1818 len = recv(svr->fd, buf, sizeof(buf), MSG_DONTWAIT | MSG_PEEK);
1819 DBG("%zu bytes in buffer", len);
1822 if (ecore_con_ssl_server_init(svr))
1824 ERR("ssl handshaking failed!");
1825 Ecore_Con_Event_Server_Del *e;
1827 e = calloc(1, sizeof(Ecore_Con_Event_Server_Del));
1832 ecore_event_add(ECORE_CON_EVENT_SERVER_DEL, e,
1833 _ecore_con_event_server_del_free, NULL);
1836 else if (!svr->ssl_state)
1838 /* we got our server! */
1839 Ecore_Con_Event_Server_Add *e;
1841 svr->connecting = EINA_FALSE;
1842 e = calloc(1, sizeof(Ecore_Con_Event_Server_Add));
1846 svr->start_time = ecore_time_get();
1848 ecore_event_add(ECORE_CON_EVENT_SERVER_ADD, e,
1849 _ecore_con_event_server_add_free, NULL);
1856 _ecore_con_cl_read(svr);
1857 else if (want_write)
1858 { /* only possible with non-ssl connections */
1859 if (svr->connecting && (!svr_try_connect_plain(svr)))
1860 return ECORE_CALLBACK_RENEW;
1862 _ecore_con_server_flush(svr);
1865 return ECORE_CALLBACK_RENEW;
1869 _ecore_con_cl_udp_handler(void *data, Ecore_Fd_Handler *fd_handler)
1871 Ecore_Con_Server *svr;
1875 return ECORE_CALLBACK_RENEW;
1878 return ECORE_CALLBACK_RENEW;
1880 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
1882 unsigned char buf[65536];
1886 num = read(svr->fd, buf, 65536);
1889 if (!svr->delete_me)
1891 Ecore_Con_Event_Server_Data *e;
1892 unsigned char *inbuf;
1894 inbuf = malloc(num);
1898 memcpy(inbuf, buf, num);
1900 e = calloc(1, sizeof(Ecore_Con_Event_Server_Data));
1907 ecore_event_add(ECORE_CON_EVENT_SERVER_DATA, e,
1908 _ecore_con_event_server_data_free,
1913 else if ((errno == EIO) || (errno == EBADF) ||
1914 (errno == EPIPE) || (errno == EINVAL) ||
1915 (errno == ENOSPC) || (errno == ECONNREFUSED))
1916 _ecore_con_server_kill(svr);
1918 else if (ecore_main_fd_handler_active_get(fd_handler,
1920 _ecore_con_server_flush(svr);
1922 return ECORE_CALLBACK_RENEW;
1926 _ecore_con_svr_udp_handler(void *data, Ecore_Fd_Handler *fd_handler)
1928 Ecore_Con_Server *svr;
1929 Ecore_Con_Client *cl = NULL;
1933 return ECORE_CALLBACK_RENEW;
1936 return ECORE_CALLBACK_RENEW;
1938 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
1940 unsigned char buf[READBUFSIZ];
1941 unsigned char client_addr[256];
1942 unsigned int client_addr_len = sizeof(client_addr);
1947 num = fcntl(svr->fd, F_SETFL, O_NONBLOCK);
1950 recvfrom(svr->fd, buf, sizeof(buf), 0,
1951 (struct sockaddr *)&client_addr,
1956 recvfrom(svr->fd, buf, sizeof(buf), MSG_DONTWAIT,
1957 (struct sockaddr *)&client_addr,
1963 if (!svr->delete_me)
1965 Ecore_Con_Event_Client_Data *e;
1966 unsigned char *inbuf;
1968 /* Create a new client for use in the client data event */
1969 cl = calloc(1, sizeof(Ecore_Con_Client));
1971 return ECORE_CALLBACK_RENEW;
1975 cl->fd_handler = NULL;
1976 cl->host_server = svr;
1977 cl->client_addr = calloc(1, client_addr_len);
1978 cl->client_addr_len = client_addr_len;
1979 if(!cl->client_addr)
1982 return ECORE_CALLBACK_RENEW;
1985 memcpy(cl->client_addr, &client_addr, client_addr_len);
1986 ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
1987 svr->clients = eina_list_append(svr->clients, cl);
1988 svr->client_count++;
1990 cl->ip = _ecore_con_pretty_ip(cl->client_addr,
1991 cl->client_addr_len);
1993 inbuf = malloc(num);
1996 free(cl->client_addr);
1998 return ECORE_CALLBACK_RENEW;
2001 memcpy(inbuf, buf, num);
2003 e = calloc(1, sizeof(Ecore_Con_Event_Client_Data));
2007 _ecore_con_cl_timer_update(cl);
2011 ecore_event_add(ECORE_CON_EVENT_CLIENT_DATA, e,
2012 _ecore_con_event_client_data_free,
2018 Ecore_Con_Event_Client_Add *add;
2020 add = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
2023 /*cl->event_count++;*/
2025 _ecore_con_cl_timer_update(cl);
2026 ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD, add,
2027 _ecore_con_event_client_add_free, NULL);
2032 else if ((errno == EIO) || (errno == EBADF) ||
2033 (errno == EPIPE) || (errno == EINVAL) ||
2034 (errno == ENOSPC) || (errno == ECONNREFUSED))
2036 if (!svr->delete_me)
2038 /* we lost our client! */
2039 Ecore_Con_Event_Client_Del *e;
2041 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
2045 /* be explicit here */
2047 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
2048 _ecore_con_event_client_del_free, NULL);
2052 svr->dead = EINA_TRUE;
2053 if (svr->fd_handler)
2054 ecore_main_fd_handler_del(svr->fd_handler);
2056 svr->fd_handler = NULL;
2059 else if (ecore_main_fd_handler_active_get(fd_handler,
2061 _ecore_con_client_flush(cl);
2063 return ECORE_CALLBACK_RENEW;
2067 _ecore_con_svr_cl_read(Ecore_Con_Client *cl)
2069 unsigned char *inbuf = NULL;
2073 for (tries = 0; tries < 16; tries++)
2076 Eina_Bool lost_client = EINA_TRUE;
2077 unsigned char buf[READBUFSIZ];
2081 if (cl->handshaking)
2083 if (ecore_con_ssl_client_init(cl))
2084 lost_client = EINA_FALSE;
2086 _ecore_con_cl_timer_update(cl);
2089 if (!(cl->host_server->type & ECORE_CON_SSL))
2091 if ((num = read(cl->fd, buf, READBUFSIZ)) <= 0)
2092 if ((num < 0) && (errno == EAGAIN))
2093 lost_client = EINA_FALSE;
2096 else if (!(num = ecore_con_ssl_client_read(cl, buf, READBUFSIZ)))
2097 lost_client = EINA_FALSE;
2101 inbuf = realloc(inbuf, inbuf_num + num);
2102 memcpy(inbuf + inbuf_num, buf, num);
2107 if (inbuf && (!cl->delete_me))
2109 Ecore_Con_Event_Client_Data *e;
2111 e = malloc(sizeof(Ecore_Con_Event_Client_Data));
2115 _ecore_con_cl_timer_update(cl);
2118 e->size = inbuf_num;
2119 ecore_event_add(ECORE_CON_EVENT_CLIENT_DATA, e,
2120 _ecore_con_event_client_data_free, NULL);
2124 if (lost_client && (!cl->delete_me))
2126 /* we lost our client! */
2127 Ecore_Con_Event_Client_Del *e;
2129 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
2133 _ecore_con_cl_timer_update(cl);
2135 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
2136 _ecore_con_event_client_del_free, NULL);
2142 cl->dead = EINA_TRUE;
2144 ecore_main_fd_handler_del(cl->fd_handler);
2146 cl->fd_handler = NULL;
2154 _ecore_con_svr_cl_handler(void *data, Ecore_Fd_Handler *fd_handler)
2156 Ecore_Con_Client *cl;
2160 return ECORE_CALLBACK_RENEW;
2163 return ECORE_CALLBACK_RENEW;
2165 if (cl->handshaking && ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ | ECORE_FD_WRITE))
2167 if (ecore_con_ssl_client_init(cl))
2169 ERR("ssl handshaking failed!");
2170 /* we lost our client! */
2171 Ecore_Con_Event_Client_Del *e;
2173 cl->dead = EINA_TRUE;
2174 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
2178 _ecore_con_cl_timer_update(cl);
2180 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
2181 _ecore_con_event_client_del_free, NULL);
2185 else if (!cl->ssl_state)
2187 Ecore_Con_Event_Client_Add *add;
2189 add = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
2192 /*cl->event_count++;*/
2194 _ecore_con_cl_timer_update(cl);
2195 ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD, add,
2196 _ecore_con_event_client_add_free, NULL);
2201 else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
2202 _ecore_con_svr_cl_read(cl);
2204 else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
2205 _ecore_con_client_flush(cl);
2207 return ECORE_CALLBACK_RENEW;
2211 _ecore_con_server_flush(Ecore_Con_Server *svr)
2215 if (!svr->write_buf)
2218 /* check whether we need to write anything at all.
2219 * we must not write zero bytes with SSL_write() since it
2220 * causes undefined behaviour
2222 if (svr->write_buf_size == svr->write_buf_offset)
2225 num = svr->write_buf_size - svr->write_buf_offset;
2227 if (svr->handshaking)
2229 DBG("Continuing ssl handshake");
2230 if (ecore_con_ssl_server_init(svr))
2231 _ecore_con_server_kill(svr);
2235 if (!(svr->type & ECORE_CON_SSL))
2236 count = write(svr->fd, svr->write_buf + svr->write_buf_offset, num);
2238 count = ecore_con_ssl_server_write(svr, svr->write_buf + svr->write_buf_offset, num);
2242 /* we lost our server! */
2243 _ecore_con_server_kill(svr);
2247 svr->write_buf_offset += count;
2248 if (svr->write_buf_offset >= svr->write_buf_size)
2250 svr->write_buf_size = 0;
2251 svr->write_buf_offset = 0;
2252 free(svr->write_buf);
2253 svr->write_buf = NULL;
2254 if (svr->fd_handler)
2255 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
2260 _ecore_con_client_flush(Ecore_Con_Client *cl)
2267 if (cl->handshaking)
2269 if (ecore_con_ssl_client_init(cl))
2272 _ecore_con_cl_timer_update(cl);
2278 num = cl->buf_size - cl->buf_offset;
2279 if (!(cl->host_server->type & ECORE_CON_SSL))
2280 count = write(cl->fd, cl->buf + cl->buf_offset, num);
2282 count = ecore_con_ssl_client_write(cl, cl->buf + cl->buf_offset, num);
2287 if ((errno == EIO) || (errno == EBADF) || (errno == EPIPE) ||
2288 (errno == EINVAL) || (errno == ENOSPC) || (errno == ECONNREFUSED))
2291 /* we lost our client! */
2292 Ecore_Con_Event_Client_Del *e;
2294 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
2298 _ecore_con_cl_timer_update(cl);
2300 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
2301 _ecore_con_event_client_del_free, NULL);
2304 cl->dead = EINA_TRUE;
2306 ecore_main_fd_handler_del(cl->fd_handler);
2308 cl->fd_handler = NULL;
2314 cl->buf_offset += count;
2315 if (cl->buf_offset >= cl->buf_size)
2322 ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ);
2327 _ecore_con_event_client_add_free(void *data __UNUSED__, void *ev)
2329 Ecore_Con_Event_Client_Add *e;
2332 e->client->event_count--;
2333 if ((e->client->event_count == 0) &&
2334 (e->client->delete_me))
2335 ecore_con_client_del(e->client);
2341 _ecore_con_event_client_del_free(void *data __UNUSED__, void *ev)
2343 Ecore_Con_Event_Client_Del *e;
2346 e->client->event_count--;
2347 if ((e->client->event_count == 0) && (e->client->delete_me))
2348 ecore_con_client_del(e->client);
2354 _ecore_con_event_client_data_free(void *data __UNUSED__, void *ev)
2356 Ecore_Con_Event_Client_Data *e;
2359 e->client->event_count--;
2363 if (((e->client->event_count == 0) && (e->client->delete_me)) ||
2364 ((e->client->host_server &&
2365 ((e->client->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_UDP ||
2366 (e->client->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_MCAST))))
2367 ecore_con_client_del(e->client);
2373 _ecore_con_event_server_add_free(void *data __UNUSED__, void *ev)
2375 Ecore_Con_Event_Server_Add *e;
2378 e->server->event_count--;
2379 if ((e->server->event_count == 0) &&
2380 (e->server->delete_me))
2381 _ecore_con_server_free(e->server);
2387 _ecore_con_event_server_del_free(void *data __UNUSED__, void *ev)
2389 Ecore_Con_Event_Server_Del *e;
2392 e->server->event_count--;
2393 if ((e->server->event_count == 0) &&
2394 (e->server->delete_me))
2395 _ecore_con_server_free(e->server);
2401 _ecore_con_event_server_data_free(void *data __UNUSED__, void *ev)
2403 Ecore_Con_Event_Server_Data *e;
2406 e->server->event_count--;
2410 if ((e->server->event_count == 0) &&
2411 (e->server->delete_me))
2412 _ecore_con_server_free(e->server);
2418 _ecore_con_lookup_done(void *data, Ecore_Con_Info *infos)
2420 Ecore_Con_Server *svr;
2421 Ecore_Con_Lookup *lk;
2427 lk->done_cb(infos->info.ai_canonname, infos->ip,
2428 infos->info.ai_addr, infos->info.ai_addrlen,
2431 lk->done_cb(NULL, NULL, NULL, 0, (void *)lk->data);