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 void _ecore_con_cb_tcp_connect(void *data, Ecore_Con_Info *info);
47 static void _ecore_con_cb_udp_connect(void *data, Ecore_Con_Info *info);
48 static void _ecore_con_cb_tcp_listen(void *data, Ecore_Con_Info *info);
49 static void _ecore_con_cb_udp_listen(void *data, Ecore_Con_Info *info);
51 static void _ecore_con_server_free(Ecore_Con_Server *svr);
52 static void _ecore_con_client_free(Ecore_Con_Client *cl);
54 static Eina_Bool _ecore_con_svr_handler(void *data,
55 Ecore_Fd_Handler *fd_handler);
56 static Eina_Bool _ecore_con_cl_handler(void *data, Ecore_Fd_Handler *fd_handler);
57 static Eina_Bool _ecore_con_cl_udp_handler(void *data,
58 Ecore_Fd_Handler *fd_handler);
59 static Eina_Bool _ecore_con_svr_udp_handler(void *data,
60 Ecore_Fd_Handler *fd_handler);
61 static Eina_Bool _ecore_con_svr_cl_handler(void *data,
62 Ecore_Fd_Handler *fd_handler);
64 static void _ecore_con_server_flush(Ecore_Con_Server *svr);
65 static void _ecore_con_client_flush(Ecore_Con_Client *cl);
67 static void _ecore_con_event_client_add_free(void *data, void *ev);
68 static void _ecore_con_event_client_del_free(void *data, void *ev);
69 static void _ecore_con_event_client_data_free(void *data, void *ev);
70 static void _ecore_con_event_server_add_free(void *data, void *ev);
71 static void _ecore_con_event_server_del_free(void *data, void *ev);
72 static void _ecore_con_event_server_data_free(void *data, void *ev);
74 static void _ecore_con_lookup_done(void *data, Ecore_Con_Info *infos);
76 EAPI int ECORE_CON_EVENT_CLIENT_ADD = 0;
77 EAPI int ECORE_CON_EVENT_CLIENT_DEL = 0;
78 EAPI int ECORE_CON_EVENT_SERVER_ADD = 0;
79 EAPI int ECORE_CON_EVENT_SERVER_DEL = 0;
80 EAPI int ECORE_CON_EVENT_CLIENT_DATA = 0;
81 EAPI int ECORE_CON_EVENT_SERVER_DATA = 0;
83 static Eina_List *servers = NULL;
84 static int _ecore_con_init_count = 0;
85 int _ecore_con_log_dom = -1;
88 * @addtogroup Ecore_Con_Lib_Group Ecore Connection Library Functions
90 * Utility functions that set up and shut down the Ecore Connection
96 * Initialises the Ecore_Con library.
97 * @return Number of times the library has been initialised without being
103 if (++_ecore_con_init_count != 1)
104 return _ecore_con_init_count;
108 return --_ecore_con_init_count;
113 return --_ecore_con_init_count;
115 _ecore_con_log_dom = eina_log_domain_register("EcoreCon",
116 ECORE_CON_DEFAULT_LOG_COLOR);
117 if(_ecore_con_log_dom < 0)
119 EINA_LOG_ERR("Impossible to create a log domain for Ecore Con.");
121 return --_ecore_con_init_count;
124 ECORE_CON_EVENT_CLIENT_ADD = ecore_event_type_new();
125 ECORE_CON_EVENT_CLIENT_DEL = ecore_event_type_new();
126 ECORE_CON_EVENT_SERVER_ADD = ecore_event_type_new();
127 ECORE_CON_EVENT_SERVER_DEL = ecore_event_type_new();
128 ECORE_CON_EVENT_CLIENT_DATA = ecore_event_type_new();
129 ECORE_CON_EVENT_SERVER_DATA = ecore_event_type_new();
131 /* TODO Remember return value, if it fails, use gethostbyname() */
132 ecore_con_ssl_init();
133 ecore_con_info_init();
135 return _ecore_con_init_count;
139 * Shuts down the Ecore_Con library.
140 * @return Number of times the library has been initialised without being
144 ecore_con_shutdown(void)
146 if (--_ecore_con_init_count != 0)
147 return _ecore_con_init_count;
150 _ecore_con_server_free(eina_list_data_get(servers));
152 ecore_con_info_shutdown();
153 ecore_con_ssl_shutdown();
154 eina_log_domain_unregister(_ecore_con_log_dom);
155 _ecore_con_log_dom = -1;
161 return _ecore_con_init_count;
167 * @addtogroup Ecore_Con_Server_Group Ecore Connection Server Functions
169 * Functions that operate on Ecore server objects.
174 * Creates a server to listen for connections.
176 * The socket on which the server listens depends on the connection
178 * @li If @a compl_type is @c ECORE_CON_LOCAL_USER, the server will listen on
179 * the Unix socket "~/.ecore/[name]/[port]".
180 * @li If @a compl_type is @c ECORE_CON_LOCAL_SYSTEM, the server will listen
181 * on Unix socket "/tmp/.ecore_service|[name]|[port]".
182 * @li If @a compl_type is @c ECORE_CON_REMOTE_TCP, the server will listen
183 * on TCP port @c port.
185 * @param compl_type The connection type.
186 * @param name Name to associate with the socket. It is used when
187 * generating the socket name of a Unix socket, or for
188 * determining what host to listen on for TCP sockets.
189 * @c NULL will not be accepted.
190 * @param port Number to identify socket. When a Unix socket is used,
191 * it becomes part of the socket name. When a TCP socket
192 * is used, it is used as the TCP port.
193 * @param data Data to associate with the created Ecore_Con_Server
195 * @return A new Ecore_Con_Server.
197 EAPI Ecore_Con_Server *
198 ecore_con_server_add(Ecore_Con_Type compl_type, const char *name, int port,
201 Ecore_Con_Server *svr;
204 if (port < 0 || !name)
205 return NULL; /* local user socket: FILE: ~/.ecore/[name]/[port] */
207 /* local system socket: FILE: /tmp/.ecore_service|[name]|[port] */
208 /* remote system socket: TCP/IP: [name]:[port] */
209 svr = calloc(1, sizeof(Ecore_Con_Server));
213 svr->name = strdup(name);
217 svr->type = compl_type;
219 svr->data = (void *)data;
221 svr->reject_excess_clients = 0;
222 svr->client_limit = -1;
224 svr->ppid = getpid();
225 ecore_con_ssl_server_prepare(svr);
227 type = compl_type & ECORE_CON_TYPE;
229 if ((type == ECORE_CON_LOCAL_USER) ||
230 (type == ECORE_CON_LOCAL_SYSTEM) ||
231 (type == ECORE_CON_LOCAL_ABSTRACT))
233 if (!ecore_con_local_listen(svr, _ecore_con_svr_handler, svr))
236 if ((type == ECORE_CON_REMOTE_TCP) ||
237 (type == ECORE_CON_REMOTE_NODELAY))
240 if (!ecore_con_info_tcp_listen(svr, _ecore_con_cb_tcp_listen,
244 else if ((type == ECORE_CON_REMOTE_MCAST) ||
245 (type == ECORE_CON_REMOTE_UDP))
247 if (!ecore_con_info_udp_listen(svr, _ecore_con_cb_udp_listen,
251 servers = eina_list_append(servers, svr);
252 ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
268 ecore_main_fd_handler_del(svr->fd_handler);
271 free(svr->write_buf);
277 ecore_con_ssl_server_shutdown(svr);
283 * Creates a connection to the specified server and returns an associated object.
285 * The socket to which the connection is made depends on the connection type:
286 * @li If @a compl_type is @c ECORE_CON_LOCAL_USER, the function will
287 * connect to the server at the Unix socket
288 * "~/.ecore/[name]/[port]".
289 * @li If @a compl_type is @c ECORE_CON_LOCAL_SYSTEM, the function will
290 * connect to the server at the Unix socket
291 * "/tmp/.ecore_service|[name]|[port]".
292 * @li If @a compl_type is @c ECORE_CON_REMOTE_TCP, the function will
293 * connect to the server at the TCP port "[name]:[port]".
295 * @param compl_type The connection type.
296 * @param name Name used when determining what socket to connect to.
297 * It is used to generate the socket name when the socket
298 * is a Unix socket. It is used as the hostname when
299 * connecting with a TCP socket.
300 * @param port Number to identify the socket to connect to. Used when
301 * generating the socket name for a Unix socket, or as the
302 * TCP port when connecting to a TCP socket.
303 * @param data Data to associate with the created Ecore_Con_Server
305 * @return A new Ecore_Con_Server.
307 EAPI Ecore_Con_Server *
308 ecore_con_server_connect(Ecore_Con_Type compl_type, const char *name, int port,
311 Ecore_Con_Server *svr;
315 return NULL; /* local user socket: FILE: ~/.ecore/[name]/[port] */
317 /* local system socket: FILE: /tmp/.ecore_service|[name]|[port] */
318 /* remote system socket: TCP/IP: [name]:[port] */
319 svr = calloc(1, sizeof(Ecore_Con_Server));
323 svr->name = strdup(name);
327 svr->type = compl_type;
329 svr->data = (void *)data;
331 svr->reject_excess_clients = 0;
333 svr->client_limit = -1;
334 ecore_con_ssl_server_prepare(svr);
336 type = compl_type & ECORE_CON_TYPE;
338 if (((type == ECORE_CON_REMOTE_TCP) ||
339 (type == ECORE_CON_REMOTE_NODELAY) ||
340 (type == ECORE_CON_REMOTE_UDP) ||
341 (type == ECORE_CON_REMOTE_BROADCAST)) &&
345 if ((type == ECORE_CON_LOCAL_USER) ||
346 (type == ECORE_CON_LOCAL_SYSTEM) ||
347 (type == ECORE_CON_LOCAL_ABSTRACT))
349 if (!ecore_con_local_connect(svr, _ecore_con_cl_handler, svr,
350 _ecore_con_event_server_add_free))
354 if ((type == ECORE_CON_REMOTE_TCP) ||
355 (type == ECORE_CON_REMOTE_NODELAY))
358 if (!ecore_con_info_tcp_connect(svr, _ecore_con_cb_tcp_connect,
362 else if ((type == ECORE_CON_REMOTE_UDP) ||
363 (type == ECORE_CON_REMOTE_BROADCAST))
365 if (!ecore_con_info_udp_connect(svr, _ecore_con_cb_udp_connect,
369 servers = eina_list_append(servers, svr);
370 ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
385 ecore_main_fd_handler_del(svr->fd_handler);
387 ecore_con_ssl_server_shutdown(svr);
393 * Closes the connection and frees the given server.
394 * @param svr The given server.
395 * @return Data associated with the server when it was created.
396 * @see ecore_con_server_add, ecore_con_server_connect
399 ecore_con_server_del(Ecore_Con_Server *svr)
403 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
405 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_del");
415 if (svr->event_count > 0)
419 ecore_main_fd_handler_del(svr->fd_handler);
420 svr->fd_handler = NULL;
424 _ecore_con_server_free(svr);
430 * Retrieves the data associated with the given server.
431 * @param svr The given server.
432 * @return The associated data.
435 ecore_con_server_data_get(Ecore_Con_Server *svr)
437 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
439 ECORE_MAGIC_FAIL(svr,
440 ECORE_MAGIC_CON_SERVER,
441 "ecore_con_server_data_get");
449 * Sets the data associated with the given server.
450 * @param svr The given server.
451 * @param data The data to associate with @p svr
452 * @return The previously associated data, if any.
455 ecore_con_server_data_set(Ecore_Con_Server *svr, void *data)
459 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
461 ECORE_MAGIC_FAIL(svr,
462 ECORE_MAGIC_CON_SERVER,
463 "ecore_con_server_data_get");
473 * Retrieves whether the given server is currently connected.
474 * @todo Check that this function does what the documenter believes it does.
475 * @param svr The given server.
476 * @return @c 1 if the server is connected. @c 0 otherwise.
479 ecore_con_server_connected_get(Ecore_Con_Server *svr)
481 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
483 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
484 "ecore_con_server_connected_get");
495 * Retrieves the current list of clients.
496 * @param svr The given server.
497 * @return The list of clients on this server.
500 ecore_con_server_clients_get(Ecore_Con_Server *svr)
502 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
504 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
505 "ecore_con_server_clients_get");
513 * Retrieves the name of server.
514 * @param svr The given server.
515 * @return The name of the server.
518 ecore_con_server_name_get(Ecore_Con_Server *svr)
520 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
522 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
523 "ecore_con_server_name_get");
531 * Retrieves the server port in use.
532 * @param svr The given server.
533 * @return The server port in use.
536 ecore_con_server_port_get(Ecore_Con_Server *svr)
538 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
540 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
541 "ecore_con_server_port_get");
548 * Sends the given data to the given server.
549 * @param svr The given server.
550 * @param data The given data.
551 * @param size Length of the data, in bytes, to send.
552 * @return The number of bytes sent. @c 0 will be returned if there is an
556 ecore_con_server_send(Ecore_Con_Server *svr, const void *data, int size)
558 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
560 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_send");
574 ecore_main_fd_handler_active_set(
575 svr->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
579 unsigned char *newbuf;
581 newbuf = realloc(svr->write_buf, svr->write_buf_size + size);
583 svr->write_buf = newbuf;
587 memcpy(svr->write_buf + svr->write_buf_size, data, size);
588 svr->write_buf_size += size;
592 svr->write_buf = malloc(size);
596 svr->write_buf_size = size;
597 memcpy(svr->write_buf, data, size);
604 * Sets a limit on the number of clients that can be handled concurrently
605 * by the given server, and a policy on what to do if excess clients try to
607 * Beware that if you set this once ecore is already running, you may
608 * already have pending CLIENT_ADD events in your event queue. Those
609 * clients have already connected and will not be affected by this call.
610 * Only clients subsequently trying to connect will be affected.
611 * @param svr The given server.
612 * @param client_limit The maximum number of clients to handle
613 * concurrently. -1 means unlimited (default). 0
614 * effectively disables the server.
615 * @param reject_excess_clients Set to 1 to automatically disconnect
616 * excess clients as soon as they connect if you are
617 * already handling client_limit clients. Set to 0
618 * (default) to just hold off on the "accept()"
619 * system call until the number of active clients
620 * drops. This causes the kernel to queue up to 4096
621 * connections (or your kernel's limit, whichever is
625 ecore_con_server_client_limit_set(Ecore_Con_Server *svr, int client_limit,
626 char reject_excess_clients)
628 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
630 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
631 "ecore_con_server_client_limit_set");
635 svr->client_limit = client_limit;
636 svr->reject_excess_clients = reject_excess_clients;
640 * Gets the IP address of a server that has been connected to.
642 * @param svr The given server.
643 * @return A pointer to an internal string that contains the IP address of
644 * the connected server in the form "XXX.YYY.ZZZ.AAA" IP notation.
645 * This string should not be modified or trusted to stay valid after
646 * deletion for the @p svr object. If no IP is known NULL is returned.
649 ecore_con_server_ip_get(Ecore_Con_Server *svr)
651 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
653 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_ip_get");
661 * Flushes all pending data to the given server. Will return when done.
663 * @param svr The given server.
666 ecore_con_server_flush(Ecore_Con_Server *svr)
668 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
670 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_flush");
674 _ecore_con_server_flush(svr);
678 * Add an ssl certificate for use in ecore_con_server functions.
680 * Use this function to add a SSL PEM certificate.
681 * Simply specify the cert here to make it available,
682 * then OR the @ref ECORE_CON_LOAD_CERT flag into the @ref ecore_con_server_connect
683 * call to use it. If there is an error loading the certificate upon creating
684 * the connection, an error will be automatically logged.
685 * @param cert The path to the certificate.
686 * @return EINA_FALSE if the file cannot be loaded,
687 * otherwise EINA_TRUE.
688 * @note Currently certificate verification is not implemented.
692 ecore_con_server_ssl_cert_add(const char *cert)
695 if (!eina_str_has_extension(cert, "pem"))
698 return ecore_con_ssl_server_cert_add(cert);
705 * @addtogroup Ecore_Con_Client_Group Ecore Connection Client Functions
707 * Functions that operate on Ecore connection client objects.
712 * Sends the given data to the given client.
713 * @param cl The given client.
714 * @param data The given data.
715 * @param size Length of the data, in bytes, to send.
716 * @return The number of bytes sent. @c 0 will be returned if there is an
720 ecore_con_client_send(Ecore_Con_Client *cl, const void *data, int size)
722 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
724 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_send");
738 ecore_main_fd_handler_active_set(
739 cl->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
741 if(cl->server && ((cl->server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_UDP))
742 sendto(cl->server->fd, data, size, 0, (struct sockaddr *)cl->client_addr,
743 cl->client_addr_len);
746 unsigned char *newbuf;
748 newbuf = realloc(cl->buf, cl->buf_size + size);
754 memcpy(cl->buf + cl->buf_size, data, size);
755 cl->buf_size += size;
759 cl->buf = malloc(size);
764 memcpy(cl->buf, data, size);
771 * Retrieves the server representing the socket the client has
773 * @param cl The given client.
774 * @return The server that the client connected to.
776 EAPI Ecore_Con_Server *
777 ecore_con_client_server_get(Ecore_Con_Client *cl)
779 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
781 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
782 "ecore_con_client_server_get");
790 * Closes the connection and frees memory allocated to the given client.
791 * @param cl The given client.
792 * @return Data associated with the client.
795 ecore_con_client_del(Ecore_Con_Client *cl)
799 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
801 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_del");
805 if (cl->client_addr && cl->server &&
806 (((cl->server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_UDP) ||
807 ((cl->server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_MCAST)))
808 free(cl->client_addr);
814 if (cl->event_count > 0)
818 ecore_main_fd_handler_del(cl->fd_handler);
819 cl->fd_handler = NULL;
825 cl->server->clients = eina_list_remove(
826 cl->server->clients, cl);
828 _ecore_con_client_free(cl);
835 * Sets the data associated with the given client to @p data.
836 * @param cl The given client.
837 * @param data What to set the data to.
840 ecore_con_client_data_set(Ecore_Con_Client *cl, const void *data)
842 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
845 ECORE_MAGIC_CON_CLIENT,
846 "ecore_con_client_data_set");
850 cl->data = (void *)data;
854 * Retrieves the data associated with the given client.
855 * @param cl The given client.
856 * @return The data associated with @p cl.
859 ecore_con_client_data_get(Ecore_Con_Client *cl)
861 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
864 ECORE_MAGIC_CON_CLIENT,
865 "ecore_con_client_data_get");
873 * Gets the IP address of a cleint that has connected.
875 * @param cl The given client.
876 * @return A pointer to an internal string that contains the IP address of
877 * the connected client in the form "XXX.YYY.ZZZ.AAA" IP notation.
878 * This string should not be modified or trusted to stay valid after
879 * deletion for the @p cl object. If no IP is known NULL is returned.
882 ecore_con_client_ip_get(Ecore_Con_Client *cl)
884 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
886 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_ip_get");
894 * Flushes all pending data to the given client. Will return when done.
896 * @param cl The given client.
899 ecore_con_client_flush(Ecore_Con_Client *cl)
901 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
903 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_flush");
907 _ecore_con_client_flush(cl);
911 * @brief Add an ssl certificate for use in ecore_con_client functions.
913 * Use this function to add a SSL PEM certificate.
914 * Simply specify the cert here to make it available,
915 * then OR the @ref ECORE_CON_LOAD_CERT flag into the @ref ecore_con_server_add
916 * call to use it. If there is an error loading the certificate upon creating
917 * the connection, an error will be automatically logged.
918 * @param cert_file The path to the certificate.
919 * @param crl_file The path to the CRL file
920 * @param key_file The path to the private key file. If not specified, the private key
921 * from the cert will be loaded.
922 * @return EINA_FALSE if the file cannot be loaded,
923 * otherwise EINA_TRUE.
924 * @note Currently CRL file adding and certificate verification is not implemented,
925 * so specifying it here has no effect.
929 ecore_con_client_ssl_cert_add(const char *cert_file,
930 const char *crl_file,
931 const char *key_file)
934 if (!eina_str_has_extension(cert_file, "pem"))
937 return ecore_con_ssl_client_cert_add(cert_file, crl_file, key_file);
945 * Do an asynchronous DNS lookup.
947 * This function performs a DNS lookup on the hostname specified by @p name, then
948 * calls @p done_cb with
950 * @params name IP address or server name to translate.
951 * @params done_cb Callback to notify when done.
952 * @params data User data to be given to done_cb.
953 * @return EINA_TRUE if the request did not fail to be set up, EINA_FALSE if it failed.
956 ecore_con_lookup(const char *name, Ecore_Con_Dns_Cb done_cb, const void *data)
958 Ecore_Con_Server *svr;
959 Ecore_Con_Lookup *lk;
960 struct addrinfo hints;
962 if (!name || !done_cb)
965 svr = calloc(1, sizeof(Ecore_Con_Server));
969 lk = malloc(sizeof (Ecore_Con_Lookup));
976 lk->done_cb = done_cb;
979 svr->name = strdup(name);
983 svr->type = ECORE_CON_REMOTE_TCP;
987 svr->reject_excess_clients = 0;
988 svr->client_limit = -1;
990 svr->ppid = getpid();
992 memset(&hints, 0, sizeof(struct addrinfo));
993 hints.ai_family = AF_INET6;
994 hints.ai_socktype = SOCK_STREAM;
995 hints.ai_flags = AI_CANONNAME;
996 hints.ai_protocol = IPPROTO_TCP;
997 hints.ai_canonname = NULL;
998 hints.ai_next = NULL;
999 hints.ai_addr = NULL;
1001 if (ecore_con_info_get(svr, _ecore_con_lookup_done, svr,
1013 _ecore_con_server_free(Ecore_Con_Server *svr)
1015 Ecore_Con_Client *cl;
1018 ECORE_MAGIC_SET(svr, ECORE_MAGIC_NONE);
1019 t_start = ecore_time_get();
1020 while ((svr->write_buf) && (!svr->dead))
1022 _ecore_con_server_flush(svr);
1023 t = ecore_time_get();
1024 if ((t - t_start) > 0.5)
1026 WRN("ECORE_CON: EEK - stuck in _ecore_con_server_free() trying\n"
1027 " to flush data out from the server, and have been for\n"
1028 " %1.1f seconds. This is taking too long. Aborting flush.",
1034 free(svr->write_buf);
1036 EINA_LIST_FREE(svr->clients, cl)
1037 _ecore_con_client_free(cl);
1038 if ((svr->created) && (svr->path) && (svr->ppid == getpid()))
1045 ecore_con_ssl_server_shutdown(svr);
1055 if (svr->fd_handler)
1056 ecore_main_fd_handler_del(svr->fd_handler);
1058 servers = eina_list_remove(servers, svr);
1063 _ecore_con_client_free(Ecore_Con_Client *cl)
1067 ECORE_MAGIC_SET(cl, ECORE_MAGIC_NONE);
1068 t_start = ecore_time_get();
1069 while ((cl->buf) && (!cl->dead))
1071 _ecore_con_client_flush(cl);
1072 t = ecore_time_get();
1073 if ((t - t_start) > 0.5)
1075 WRN("EEK - stuck in _ecore_con_client_free() trying\n"
1076 " to flush data out from the client, and have been for\n"
1077 " %1.1f seconds. This is taking too long. Aborting flush.",
1089 ecore_main_fd_handler_del(cl->fd_handler);
1098 kill_server(Ecore_Con_Server *svr)
1100 if (!svr->delete_me)
1102 Ecore_Con_Event_Server_Del *e;
1104 e = calloc(1, sizeof(Ecore_Con_Event_Server_Del));
1109 ecore_event_add(ECORE_CON_EVENT_SERVER_DEL, e,
1110 _ecore_con_event_server_del_free, NULL);
1115 if (svr->fd_handler)
1116 ecore_main_fd_handler_del(svr->fd_handler);
1118 svr->fd_handler = NULL;
1122 _ecore_con_cb_tcp_listen(void *data, Ecore_Con_Info *net_info)
1124 Ecore_Con_Server *svr;
1132 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1133 net_info->info.ai_protocol);
1137 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1140 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1145 if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, (const void *)&lin,
1146 sizeof(struct linger)) < 0)
1149 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_NODELAY)
1153 if (setsockopt(svr->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
1158 if (bind(svr->fd, net_info->info.ai_addr,
1159 net_info->info.ai_addrlen) < 0)
1162 if (listen(svr->fd, 4096) < 0)
1166 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1167 _ecore_con_svr_handler, svr, NULL, NULL);
1168 if (!svr->fd_handler)
1174 ecore_con_ssl_server_shutdown(svr);
1179 _ecore_con_cb_udp_listen(void *data, Ecore_Con_Info *net_info)
1181 Ecore_Con_Server *svr;
1182 Ecore_Con_Type type;
1183 struct ip_mreq mreq;
1184 struct ipv6_mreq mreq6;
1189 type &= ECORE_CON_TYPE;
1194 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1195 net_info->info.ai_protocol);
1199 if (type == ECORE_CON_REMOTE_MCAST)
1201 if (net_info->info.ai_family == AF_INET)
1203 if (!inet_pton(net_info->info.ai_family, net_info->ip,
1204 &mreq.imr_multiaddr))
1207 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
1208 if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1209 (const void *)&mreq,sizeof(mreq)) != 0)
1212 else if (net_info->info.ai_family == AF_INET6)
1214 if (!inet_pton(net_info->info.ai_family, net_info->ip,
1215 &mreq6.ipv6mr_multiaddr))
1218 mreq6.ipv6mr_interface = htonl(INADDR_ANY);
1219 if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1220 (const void *)&mreq6,sizeof(mreq6)) != 0)
1224 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on,
1229 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1232 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1235 if (bind(svr->fd, net_info->info.ai_addr,
1236 net_info->info.ai_addrlen) < 0)
1240 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1241 _ecore_con_svr_udp_handler, svr, NULL, NULL);
1242 if (!svr->fd_handler)
1245 svr->ip = strdup(net_info->ip);
1250 ecore_con_ssl_server_shutdown(svr);
1255 _ecore_con_cb_tcp_connect(void *data, Ecore_Con_Info *net_info)
1257 Ecore_Con_Server *svr;
1266 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1267 net_info->info.ai_protocol);
1271 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1274 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1277 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&curstate,
1278 sizeof(curstate)) < 0)
1281 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_NODELAY)
1285 if (setsockopt(svr->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
1290 res = connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen);
1292 if (res == SOCKET_ERROR)
1294 if (WSAGetLastError() != WSAEINPROGRESS)
1300 if (errno != EINPROGRESS)
1304 svr->connecting = 1;
1306 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
1307 _ecore_con_cl_handler, svr, NULL, NULL);
1311 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1312 _ecore_con_cl_handler, svr, NULL, NULL);
1314 if (svr->type & ECORE_CON_SSL)
1315 if (ecore_con_ssl_server_init(svr))
1318 if (!svr->fd_handler)
1321 svr->ip = strdup(net_info->ip);
1326 ecore_con_ssl_server_shutdown(svr);
1331 _ecore_con_cb_udp_connect(void *data, Ecore_Con_Info *net_info)
1333 Ecore_Con_Server *svr;
1341 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1342 net_info->info.ai_protocol);
1346 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1349 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1352 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_BROADCAST)
1354 if (setsockopt(svr->fd, SOL_SOCKET, SO_BROADCAST,
1355 (const void *)&broadcast,
1356 sizeof(broadcast)) < 0)
1359 else if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR,
1360 (const void *)&curstate,
1361 sizeof(curstate)) < 0)
1364 if (connect(svr->fd, net_info->info.ai_addr,
1365 net_info->info.ai_addrlen) < 0)
1369 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
1370 _ecore_con_cl_udp_handler, svr, NULL, NULL);
1372 if (!svr->fd_handler)
1375 svr->ip = strdup(net_info->ip);
1380 ecore_con_ssl_server_shutdown(svr);
1384 static Ecore_Con_State
1385 svr_try_connect_plain(Ecore_Con_Server *svr)
1389 unsigned int size = sizeof(int);
1391 res = getsockopt(svr->fd, SOL_SOCKET, SO_ERROR, (void *)&so_err, &size);
1393 if (res == SOCKET_ERROR)
1396 if (so_err == WSAEINPROGRESS && !svr->dead)
1397 return ECORE_CON_INPROGRESS;
1403 if (so_err == EINPROGRESS && !svr->dead)
1404 return ECORE_CON_INPROGRESS;
1410 /* we lost our server! */
1412 return ECORE_CON_DISCONNECTED;
1416 if (!svr->delete_me)
1418 /* we got our server! */
1419 Ecore_Con_Event_Server_Add *e;
1421 svr->connecting = 0;
1422 e = calloc(1, sizeof(Ecore_Con_Event_Server_Add));
1427 ecore_event_add(ECORE_CON_EVENT_SERVER_ADD, e,
1428 _ecore_con_event_server_add_free, NULL);
1432 if (svr->fd_handler)
1433 if (!svr->write_buf)
1434 ecore_main_fd_handler_active_set(svr->fd_handler,
1440 return ECORE_CON_CONNECTED;
1442 return ECORE_CON_DISCONNECTED;
1445 /* returns 1 on success, 0 on failure */
1446 static Ecore_Con_State svr_try_connect(Ecore_Con_Server *svr)
1448 if (!(svr->type & ECORE_CON_SSL))
1449 return svr_try_connect_plain(svr);
1452 switch (ecore_con_ssl_server_try(svr)) {
1453 case ECORE_CON_CONNECTED:
1454 return svr_try_connect_plain(svr);
1456 case ECORE_CON_DISCONNECTED:
1458 return ECORE_CON_DISCONNECTED;
1461 return ECORE_CON_INPROGRESS;
1467 _ecore_con_pretty_ip(struct sockaddr *client_addr, socklen_t size)
1469 char ipbuf[INET6_ADDRSTRLEN + 1];
1471 /* show v4mapped address in pretty form */
1472 if (client_addr->sa_family == AF_INET6)
1474 struct sockaddr_in6 *sa6;
1476 sa6 = (struct sockaddr_in6 *)client_addr;
1477 if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr))
1479 snprintf(ipbuf, sizeof (ipbuf), "%u.%u.%u.%u",
1480 sa6->sin6_addr.s6_addr[12],
1481 sa6->sin6_addr.s6_addr[13],
1482 sa6->sin6_addr.s6_addr[14],
1483 sa6->sin6_addr.s6_addr[15]);
1484 return strdup(ipbuf);
1488 if (getnameinfo(client_addr, size,
1489 ipbuf, sizeof (ipbuf), NULL, 0,
1491 return strdup("0.0.0.0");
1493 ipbuf[sizeof (ipbuf) - 1] = 0;
1494 return strdup(ipbuf);
1498 _ecore_con_svr_handler(void *data, Ecore_Fd_Handler *fd_handler __UNUSED__)
1500 Ecore_Con_Server *svr;
1502 unsigned char incoming[256];
1507 return ECORE_CALLBACK_RENEW;
1510 return ECORE_CALLBACK_RENEW;
1512 if ((svr->client_limit >= 0) && (!svr->reject_excess_clients))
1513 if (eina_list_count(svr->clients) >=
1514 (unsigned int)svr->client_limit)
1515 return ECORE_CALLBACK_RENEW;
1519 size_in = sizeof(incoming);
1521 memset(&incoming, 0, size_in);
1522 new_fd = accept(svr->fd, (struct sockaddr *)&incoming, (socklen_t *)&size_in);
1525 Ecore_Con_Client *cl;
1527 if ((svr->client_limit >= 0) && (svr->reject_excess_clients))
1528 if (eina_list_count(svr->clients) >= (unsigned int)svr->client_limit)
1531 return ECORE_CALLBACK_RENEW;
1534 cl = calloc(1, sizeof(Ecore_Con_Client));
1538 return ECORE_CALLBACK_RENEW;
1541 fcntl(new_fd, F_SETFL, O_NONBLOCK);
1542 fcntl(new_fd, F_SETFD, FD_CLOEXEC);
1546 if ((svr->type & ECORE_CON_SSL) &&
1547 (ecore_con_ssl_client_init(cl)))
1550 ecore_con_ssl_client_shutdown(cl);
1551 return ECORE_CALLBACK_RENEW;
1555 ecore_main_fd_handler_add(cl->fd, ECORE_FD_READ,
1556 _ecore_con_svr_cl_handler, cl, NULL, NULL);
1557 ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
1558 svr->clients = eina_list_append(svr->clients, cl);
1560 cl->ip = _ecore_con_pretty_ip(
1561 (struct sockaddr *)&incoming, size_in);
1565 Ecore_Con_Event_Client_Add *e;
1567 e = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
1572 ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD, e,
1573 _ecore_con_event_client_add_free, NULL);
1578 return ECORE_CALLBACK_RENEW;
1582 _ecore_con_cl_handler(void *data, Ecore_Fd_Handler *fd_handler)
1584 Ecore_Con_Server *svr;
1588 return ECORE_CALLBACK_RENEW;
1591 return ECORE_CALLBACK_RENEW;
1593 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
1595 unsigned char *inbuf = NULL;
1599 if (svr->connecting &&
1600 (svr_try_connect(svr) !=
1601 ECORE_CON_CONNECTED))
1602 return ECORE_CALLBACK_RENEW;
1604 for (tries = 0; tries < 16; tries++)
1607 int lost_server = 1;
1608 unsigned char buf[READBUFSIZ];
1610 if (!(svr->type & ECORE_CON_SSL))
1612 if ((num = read(svr->fd, buf, READBUFSIZ)) <= 0)
1613 if ((num < 0) && (errno == EAGAIN))
1618 ecore_con_ssl_server_read(svr, buf,
1624 if (inbuf && !svr->delete_me)
1626 Ecore_Con_Event_Server_Data *e;
1628 e = calloc(1, sizeof(Ecore_Con_Event_Server_Data));
1634 e->size = inbuf_num;
1635 ecore_event_add(ECORE_CON_EVENT_SERVER_DATA, e,
1636 _ecore_con_event_server_data_free,
1647 inbuf = realloc(inbuf, inbuf_num + num);
1648 memcpy(inbuf + inbuf_num, buf, num);
1652 /* #if USE_OPENSSL */
1653 /* if (svr->fd_handler) */
1655 /* if (svr->ssl && ssl_err == SSL_ERROR_WANT_READ) */
1656 /* ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ); */
1657 /* else if (svr->ssl && ssl_err == SSL_ERROR_WANT_WRITE) */
1658 /* ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_WRITE); */
1662 else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
1664 if (svr->connecting &&
1665 !svr_try_connect (svr))
1666 return ECORE_CALLBACK_RENEW;
1668 _ecore_con_server_flush(svr);
1671 return ECORE_CALLBACK_RENEW;
1675 _ecore_con_cl_udp_handler(void *data, Ecore_Fd_Handler *fd_handler)
1677 Ecore_Con_Server *svr;
1681 return ECORE_CALLBACK_RENEW;
1684 return ECORE_CALLBACK_RENEW;
1686 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
1688 unsigned char buf[65536];
1692 num = read(svr->fd, buf, 65536);
1695 if (!svr->delete_me)
1697 Ecore_Con_Event_Server_Data *e;
1698 unsigned char *inbuf;
1700 inbuf = malloc(num);
1704 memcpy(inbuf, buf, num);
1706 e = calloc(1, sizeof(Ecore_Con_Event_Server_Data));
1713 ecore_event_add(ECORE_CON_EVENT_SERVER_DATA, e,
1714 _ecore_con_event_server_data_free,
1719 else if ((errno == EIO) || (errno == EBADF) ||
1720 (errno == EPIPE) || (errno == EINVAL) ||
1721 (errno == ENOSPC) || (errno == ECONNREFUSED))
1724 else if (ecore_main_fd_handler_active_get(fd_handler,
1726 _ecore_con_server_flush(svr);
1728 return ECORE_CALLBACK_RENEW;
1732 _ecore_con_svr_udp_handler(void *data, Ecore_Fd_Handler *fd_handler)
1734 Ecore_Con_Server *svr;
1735 Ecore_Con_Client *cl = NULL;
1739 return ECORE_CALLBACK_RENEW;
1742 return ECORE_CALLBACK_RENEW;
1744 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
1746 unsigned char buf[READBUFSIZ];
1747 unsigned char client_addr[256];
1748 unsigned int client_addr_len = sizeof(client_addr);
1753 num = fcntl(svr->fd, F_SETFL, O_NONBLOCK);
1756 recvfrom(svr->fd, buf, sizeof(buf), 0,
1757 (struct sockaddr *)&client_addr,
1762 recvfrom(svr->fd, buf, sizeof(buf), MSG_DONTWAIT,
1763 (struct sockaddr *)&client_addr,
1769 if (!svr->delete_me)
1771 Ecore_Con_Event_Client_Data *e;
1772 unsigned char *inbuf;
1774 /* Create a new client for use in the client data event */
1775 cl = calloc(1, sizeof(Ecore_Con_Client));
1777 return ECORE_CALLBACK_RENEW;
1781 cl->fd_handler = NULL;
1783 cl->client_addr = calloc(1, client_addr_len);
1784 cl->client_addr_len = client_addr_len;
1785 if(!cl->client_addr)
1788 return ECORE_CALLBACK_RENEW;
1791 memcpy(cl->client_addr, &client_addr, client_addr_len);
1792 ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
1793 svr->clients = eina_list_append(svr->clients, cl);
1795 cl->ip = _ecore_con_pretty_ip(cl->client_addr,
1796 cl->client_addr_len);
1798 inbuf = malloc(num);
1801 free(cl->client_addr);
1803 return ECORE_CALLBACK_RENEW;
1806 memcpy(inbuf, buf, num);
1808 e = calloc(1, sizeof(Ecore_Con_Event_Client_Data));
1815 ecore_event_add(ECORE_CON_EVENT_CLIENT_DATA, e,
1816 _ecore_con_event_client_data_free,
1822 Ecore_Con_Event_Client_Add *add;
1824 add = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
1827 /*cl->event_count++;*/
1829 ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD,
1831 _ecore_con_event_client_add_free,
1837 else if ((errno == EIO) || (errno == EBADF) ||
1838 (errno == EPIPE) || (errno == EINVAL) ||
1839 (errno == ENOSPC) || (errno == ECONNREFUSED))
1841 if (!svr->delete_me)
1843 /* we lost our client! */
1844 Ecore_Con_Event_Client_Del *e;
1846 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
1850 /* be explicit here */
1852 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
1853 _ecore_con_event_client_del_free,
1859 if (svr->fd_handler)
1860 ecore_main_fd_handler_del(svr->fd_handler);
1862 svr->fd_handler = NULL;
1865 else if (ecore_main_fd_handler_active_get(fd_handler,
1867 _ecore_con_client_flush(cl);
1869 return ECORE_CALLBACK_RENEW;
1873 _ecore_con_svr_cl_handler(void *data, Ecore_Fd_Handler *fd_handler)
1875 Ecore_Con_Client *cl;
1879 return ECORE_CALLBACK_RENEW;
1882 return ECORE_CALLBACK_RENEW;
1884 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
1886 unsigned char *inbuf = NULL;
1890 for (tries = 0; tries < 16; tries++)
1893 int lost_client = 1;
1894 unsigned char buf[READBUFSIZ];
1898 if (!(cl->server->type & ECORE_CON_SSL))
1900 if ((num = read(cl->fd, buf, READBUFSIZ)) <= 0)
1901 if ((num < 0) && (errno == EAGAIN))
1906 ecore_con_ssl_client_read(cl, buf,
1912 if (inbuf && !cl->delete_me)
1914 Ecore_Con_Event_Client_Data *e;
1916 e = calloc(1, sizeof(Ecore_Con_Event_Client_Data));
1922 e->size = inbuf_num;
1923 ecore_event_add(ECORE_CON_EVENT_CLIENT_DATA, e,
1924 _ecore_con_event_client_data_free,
1933 /* we lost our client! */
1934 Ecore_Con_Event_Client_Del *e;
1936 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
1942 ECORE_CON_EVENT_CLIENT_DEL,
1944 _ecore_con_event_client_del_free,
1951 ecore_main_fd_handler_del(cl->fd_handler);
1953 cl->fd_handler = NULL;
1960 inbuf = realloc(inbuf, inbuf_num + num);
1961 memcpy(inbuf + inbuf_num, buf, num);
1966 else if (ecore_main_fd_handler_active_get(fd_handler,
1968 _ecore_con_client_flush(cl);
1970 return ECORE_CALLBACK_RENEW;
1974 _ecore_con_server_flush(Ecore_Con_Server *svr)
1978 if (!svr->write_buf)
1981 /* check whether we need to write anything at all.
1982 * we must not write zero bytes with SSL_write() since it
1983 * causes undefined behaviour
1985 if (svr->write_buf_size == svr->write_buf_offset)
1988 num = svr->write_buf_size - svr->write_buf_offset;
1990 if (!(svr->type & ECORE_CON_SSL))
1992 svr->fd, svr->write_buf + svr->write_buf_offset, num);
1994 count = ecore_con_ssl_server_write(
1995 svr, svr->write_buf + svr->write_buf_offset, num);
1999 /* we lost our server! */
2004 svr->write_buf_offset += count;
2005 if (svr->write_buf_offset >= svr->write_buf_size)
2007 svr->write_buf_size = 0;
2008 svr->write_buf_offset = 0;
2009 free(svr->write_buf);
2010 svr->write_buf = NULL;
2011 if (svr->fd_handler)
2012 ecore_main_fd_handler_active_set(svr->fd_handler,
2018 _ecore_con_client_flush(Ecore_Con_Client *cl)
2025 num = cl->buf_size - cl->buf_offset;
2026 if (!(cl->server->type & ECORE_CON_SSL))
2028 cl->fd, cl->buf + cl->buf_offset, num);
2030 count = ecore_con_ssl_client_write(cl, cl->buf + cl->buf_offset, num);
2034 if ((errno == EIO) || (errno == EBADF) || (errno == EPIPE) ||
2035 (errno == EINVAL) || (errno == ENOSPC) || (errno == ECONNREFUSED))
2038 /* we lost our client! */
2039 Ecore_Con_Event_Client_Del *e;
2041 e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
2046 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
2047 _ecore_con_event_client_del_free, NULL);
2052 ecore_main_fd_handler_del(cl->fd_handler);
2054 cl->fd_handler = NULL;
2060 cl->buf_offset += count;
2061 if (cl->buf_offset >= cl->buf_size)
2068 ecore_main_fd_handler_active_set(cl->fd_handler,
2074 _ecore_con_event_client_add_free(void *data __UNUSED__, void *ev)
2076 Ecore_Con_Event_Client_Add *e;
2079 e->client->event_count--;
2080 if ((e->client->event_count == 0) &&
2081 (e->client->delete_me))
2082 ecore_con_client_del(e->client);
2088 _ecore_con_event_client_del_free(void *data __UNUSED__, void *ev)
2090 Ecore_Con_Event_Client_Del *e;
2093 e->client->event_count--;
2094 if ((e->client->event_count == 0) &&
2095 (e->client->delete_me))
2096 ecore_con_client_del(e->client);
2102 _ecore_con_event_client_data_free(void *data __UNUSED__, void *ev)
2104 Ecore_Con_Event_Client_Data *e;
2107 e->client->event_count--;
2111 if (((e->client->event_count == 0) && (e->client->delete_me)) ||
2112 ((e->client->server &&
2113 ((e->client->server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_UDP ||
2114 (e->client->server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_MCAST))))
2115 ecore_con_client_del(e->client);
2121 _ecore_con_event_server_add_free(void *data __UNUSED__, void *ev)
2123 Ecore_Con_Event_Server_Add *e;
2126 e->server->event_count--;
2127 if ((e->server->event_count == 0) &&
2128 (e->server->delete_me))
2129 _ecore_con_server_free(e->server);
2135 _ecore_con_event_server_del_free(void *data __UNUSED__, void *ev)
2137 Ecore_Con_Event_Server_Del *e;
2140 e->server->event_count--;
2141 if ((e->server->event_count == 0) &&
2142 (e->server->delete_me))
2143 _ecore_con_server_free(e->server);
2149 _ecore_con_event_server_data_free(void *data __UNUSED__, void *ev)
2151 Ecore_Con_Event_Server_Data *e;
2154 e->server->event_count--;
2158 if ((e->server->event_count == 0) &&
2159 (e->server->delete_me))
2160 _ecore_con_server_free(e->server);
2166 _ecore_con_lookup_done(void *data, Ecore_Con_Info *infos)
2168 Ecore_Con_Server *svr;
2169 Ecore_Con_Lookup *lk;
2175 lk->done_cb(infos->info.ai_canonname, infos->ip,
2176 infos->info.ai_addr, infos->info.ai_addrlen,
2179 lk->done_cb(NULL, NULL, NULL, 0, (void *)lk->data);