13 #ifdef HAVE_NETINET_TCP_H
14 # include <netinet/tcp.h>
17 #ifdef HAVE_NETINET_IN_H
18 # include <netinet/in.h>
21 #ifdef HAVE_ARPA_INET_H
22 # include <arpa/inet.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);
48 static Eina_Bool _ecore_con_server_timer(Ecore_Con_Server *svr);
49 static void _ecore_con_server_timer_update(Ecore_Con_Server *svr);
51 static void _ecore_con_cb_tcp_connect(void *data,
52 Ecore_Con_Info *info);
53 static void _ecore_con_cb_udp_connect(void *data,
54 Ecore_Con_Info *info);
55 static void _ecore_con_cb_tcp_listen(void *data,
56 Ecore_Con_Info *info);
57 static void _ecore_con_cb_udp_listen(void *data,
58 Ecore_Con_Info *info);
60 static void _ecore_con_server_free(Ecore_Con_Server *svr);
61 static void _ecore_con_client_free(Ecore_Con_Client *cl);
63 static void _ecore_con_cl_read(Ecore_Con_Server *svr);
64 static Eina_Bool _ecore_con_svr_tcp_handler(void *data,
65 Ecore_Fd_Handler *fd_handler);
66 static Eina_Bool _ecore_con_cl_handler(void *data,
67 Ecore_Fd_Handler *fd_handler);
68 static Eina_Bool _ecore_con_cl_udp_handler(void *data,
69 Ecore_Fd_Handler *fd_handler);
70 static Eina_Bool _ecore_con_svr_udp_handler(void *data,
71 Ecore_Fd_Handler *fd_handler);
73 static void _ecore_con_svr_cl_read(Ecore_Con_Client *cl);
74 static Eina_Bool _ecore_con_svr_cl_handler(void *data,
75 Ecore_Fd_Handler *fd_handler);
77 static void _ecore_con_server_flush(Ecore_Con_Server *svr);
78 static void _ecore_con_client_flush(Ecore_Con_Client *cl);
80 static void _ecore_con_event_client_add_free(Ecore_Con_Server *svr,
82 static void _ecore_con_event_client_del_free(Ecore_Con_Server *svr,
84 static void _ecore_con_event_client_data_free(Ecore_Con_Server *svr,
86 static void _ecore_con_event_server_add_free(void *data,
88 static void _ecore_con_event_server_del_free(void *data,
90 static void _ecore_con_event_server_data_free(void *data,
92 static void _ecore_con_event_server_error_free(void *data,
93 Ecore_Con_Event_Server_Error *e);
94 static void _ecore_con_event_client_error_free(Ecore_Con_Server *svr,
95 Ecore_Con_Event_Client_Error *e);
96 static void _ecore_con_event_server_write_free(void *data,
97 Ecore_Con_Event_Server_Write *e);
98 static void _ecore_con_event_client_write_free(Ecore_Con_Server *svr,
99 Ecore_Con_Event_Client_Write *e);
101 static void _ecore_con_lookup_done(void *data,
102 Ecore_Con_Info *infos);
104 static const char * _ecore_con_pretty_ip(struct sockaddr *client_addr);
108 _ecore_con_client_kill(Ecore_Con_Client *cl)
111 DBG("Multi kill request for client %p", cl);
114 ecore_con_event_client_del(cl);
117 INF("Lost client %s", (cl->ip) ? cl->ip : "");
119 ecore_main_fd_handler_del(cl->fd_handler);
121 cl->fd_handler = NULL;
125 _ecore_con_server_kill(Ecore_Con_Server *svr)
128 DBG("Multi kill request for svr %p", svr);
130 ecore_con_event_server_del(svr);
133 ecore_main_fd_handler_del(svr->fd_handler);
135 svr->fd_handler = NULL;
138 #define _ecore_con_server_kill(svr) do { \
139 DBG("KILL %p", (svr)); \
140 _ecore_con_server_kill((svr)); \
143 #define _ecore_con_client_kill(cl) do { \
144 DBG("KILL %p", (cl)); \
145 _ecore_con_client_kill((cl)); \
148 EAPI int ECORE_CON_EVENT_CLIENT_ADD = 0;
149 EAPI int ECORE_CON_EVENT_CLIENT_DEL = 0;
150 EAPI int ECORE_CON_EVENT_SERVER_ADD = 0;
151 EAPI int ECORE_CON_EVENT_SERVER_DEL = 0;
152 EAPI int ECORE_CON_EVENT_CLIENT_DATA = 0;
153 EAPI int ECORE_CON_EVENT_SERVER_DATA = 0;
154 EAPI int ECORE_CON_EVENT_CLIENT_WRITE = 0;
155 EAPI int ECORE_CON_EVENT_SERVER_WRITE = 0;
156 EAPI int ECORE_CON_EVENT_CLIENT_ERROR = 0;
157 EAPI int ECORE_CON_EVENT_SERVER_ERROR = 0;
158 EAPI int ECORE_CON_EVENT_PROXY_BIND = 0;
160 static Eina_List *servers = NULL;
161 static int _ecore_con_init_count = 0;
162 static int _ecore_con_event_count = 0;
163 int _ecore_con_log_dom = -1;
164 Ecore_Con_Socks *_ecore_con_proxy_once = NULL;
165 Ecore_Con_Socks *_ecore_con_proxy_global = NULL;
170 if (++_ecore_con_init_count != 1)
171 return _ecore_con_init_count;
175 return --_ecore_con_init_count;
180 return --_ecore_con_init_count;
182 _ecore_con_log_dom = eina_log_domain_register
183 ("ecore_con", ECORE_CON_DEFAULT_LOG_COLOR);
184 if (_ecore_con_log_dom < 0)
186 EINA_LOG_ERR("Impossible to create a log domain for Ecore Con.");
188 return --_ecore_con_init_count;
191 ecore_con_mempool_init();
193 ECORE_CON_EVENT_CLIENT_ADD = ecore_event_type_new();
194 ECORE_CON_EVENT_CLIENT_DEL = ecore_event_type_new();
195 ECORE_CON_EVENT_SERVER_ADD = ecore_event_type_new();
196 ECORE_CON_EVENT_SERVER_DEL = ecore_event_type_new();
197 ECORE_CON_EVENT_CLIENT_DATA = ecore_event_type_new();
198 ECORE_CON_EVENT_SERVER_DATA = ecore_event_type_new();
199 ECORE_CON_EVENT_CLIENT_WRITE = ecore_event_type_new();
200 ECORE_CON_EVENT_SERVER_WRITE = ecore_event_type_new();
201 ECORE_CON_EVENT_CLIENT_ERROR = ecore_event_type_new();
202 ECORE_CON_EVENT_SERVER_ERROR = ecore_event_type_new();
203 ECORE_CON_EVENT_PROXY_BIND = ecore_event_type_new();
206 eina_magic_string_set(ECORE_MAGIC_CON_SERVER, "Ecore_Con_Server");
207 eina_magic_string_set(ECORE_MAGIC_CON_CLIENT, "Ecore_Con_Client");
208 eina_magic_string_set(ECORE_MAGIC_CON_URL, "Ecore_Con_Url");
210 /* TODO Remember return value, if it fails, use gethostbyname() */
211 ecore_con_socks_init();
212 ecore_con_ssl_init();
213 ecore_con_info_init();
215 return _ecore_con_init_count;
219 ecore_con_shutdown(void)
222 Ecore_Con_Server *svr;
224 if (--_ecore_con_init_count != 0)
225 return _ecore_con_init_count;
227 EINA_LIST_FOREACH_SAFE(servers, l, l2, svr)
229 Ecore_Con_Event_Server_Add *ev;
231 svr->delete_me = EINA_TRUE;
232 INF("svr %p is dead", svr);
233 /* some pointer hacks here to prevent double frees if people are being stupid */
234 EINA_LIST_FREE(svr->event_count, ev)
236 _ecore_con_server_free(svr);
239 ecore_con_socks_shutdown();
240 if (!_ecore_con_event_count) ecore_con_mempool_shutdown();
242 ecore_con_info_shutdown();
243 ecore_con_ssl_shutdown();
244 eina_log_domain_unregister(_ecore_con_log_dom);
245 _ecore_con_log_dom = -1;
251 return _ecore_con_init_count;
255 ecore_con_lookup(const char *name,
256 Ecore_Con_Dns_Cb done_cb,
259 Ecore_Con_Server *svr;
260 Ecore_Con_Lookup *lk;
261 struct addrinfo hints;
263 if (!name || !done_cb)
266 svr = calloc(1, sizeof(Ecore_Con_Server));
270 lk = malloc(sizeof (Ecore_Con_Lookup));
277 lk->done_cb = done_cb;
280 svr->name = strdup(name);
284 svr->type = ECORE_CON_REMOTE_TCP;
287 svr->created = EINA_TRUE;
288 svr->reject_excess_clients = EINA_FALSE;
289 svr->client_limit = -1;
291 svr->ppid = getpid();
293 memset(&hints, 0, sizeof(struct addrinfo));
294 hints.ai_family = AF_UNSPEC;
295 hints.ai_socktype = SOCK_STREAM;
296 hints.ai_flags = AI_CANONNAME;
297 hints.ai_protocol = IPPROTO_TCP;
298 hints.ai_canonname = NULL;
299 hints.ai_next = NULL;
300 hints.ai_addr = NULL;
302 if (ecore_con_info_get(svr, _ecore_con_lookup_done, svr,
314 * @addtogroup Ecore_Con_Server_Group Ecore Connection Server Functions
316 * Functions that operate on Ecore server objects.
322 * @example ecore_con_server_example.c
323 * Shows how to write a simple server using the Ecore_Con library.
326 EAPI Ecore_Con_Server *
327 ecore_con_server_add(Ecore_Con_Type compl_type,
332 Ecore_Con_Server *svr;
335 if (port < 0 || !name)
336 return NULL; /* local user socket: FILE: ~/.ecore/[name]/[port] */
338 /* local system socket: FILE: /tmp/.ecore_service|[name]|[port] */
339 /* remote system socket: TCP/IP: [name]:[port] */
340 svr = calloc(1, sizeof(Ecore_Con_Server));
344 svr->name = strdup(name);
348 svr->type = compl_type;
350 svr->data = (void *)data;
351 svr->created = EINA_TRUE;
352 svr->use_cert = (compl_type & ECORE_CON_SSL & ECORE_CON_LOAD_CERT) == ECORE_CON_LOAD_CERT;
353 svr->reject_excess_clients = EINA_FALSE;
354 svr->client_limit = -1;
356 svr->ppid = getpid();
357 if (ecore_con_ssl_server_prepare(svr, compl_type & ECORE_CON_SSL))
360 type = compl_type & ECORE_CON_TYPE;
362 if ((type == ECORE_CON_LOCAL_USER) ||
363 (type == ECORE_CON_LOCAL_SYSTEM) ||
364 (type == ECORE_CON_LOCAL_ABSTRACT))
367 if (!ecore_con_local_listen(svr))
370 if (!ecore_con_local_listen(svr, _ecore_con_svr_tcp_handler, svr))
374 if ((type == ECORE_CON_REMOTE_TCP) ||
375 (type == ECORE_CON_REMOTE_NODELAY) ||
376 (type == ECORE_CON_REMOTE_CORK))
379 if (!ecore_con_info_tcp_listen(svr, _ecore_con_cb_tcp_listen,
383 else if ((type == ECORE_CON_REMOTE_MCAST) ||
384 (type == ECORE_CON_REMOTE_UDP))
386 if (!ecore_con_info_udp_listen(svr, _ecore_con_cb_udp_listen,
390 servers = eina_list_append(servers, svr);
391 ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
404 ecore_main_fd_handler_del(svr->fd_handler);
410 eina_binbuf_free(svr->buf);
413 eina_stringshare_del(svr->ip);
415 ecore_con_ssl_server_shutdown(svr);
420 EAPI Ecore_Con_Server *
421 ecore_con_server_connect(Ecore_Con_Type compl_type,
426 Ecore_Con_Server *svr;
429 if ((!name) || (!name[0]))
431 /* local user socket: FILE: ~/.ecore/[name]/[port] */
432 /* local system socket: FILE: /tmp/.ecore_service|[name]|[port] */
433 /* remote system socket: TCP/IP: [name]:[port] */
434 svr = calloc(1, sizeof(Ecore_Con_Server));
438 svr->name = strdup(name);
442 svr->type = compl_type;
444 svr->data = (void *)data;
445 svr->created = EINA_FALSE;
446 svr->use_cert = (compl_type & ECORE_CON_SSL & ECORE_CON_LOAD_CERT) == ECORE_CON_LOAD_CERT;
447 svr->disable_proxy = (compl_type & ECORE_CON_SUPER_SSL & ECORE_CON_NO_PROXY) == ECORE_CON_NO_PROXY;
448 svr->reject_excess_clients = EINA_FALSE;
450 svr->client_limit = -1;
452 type = compl_type & ECORE_CON_TYPE;
454 if ((!svr->disable_proxy) && (type > ECORE_CON_LOCAL_ABSTRACT))
456 /* never use proxies on local connections */
457 if (_ecore_con_proxy_once)
458 svr->ecs = _ecore_con_proxy_once;
459 else if (_ecore_con_proxy_global)
460 svr->ecs = _ecore_con_proxy_global;
461 _ecore_con_proxy_once = NULL;
464 if ((!svr->ecs->lookup) &&
465 (!ecore_con_lookup(svr->name, (Ecore_Con_Dns_Cb)ecore_con_socks_dns_cb, svr)))
467 if (svr->ecs->lookup)
468 svr->ecs_state = ECORE_CON_PROXY_STATE_RESOLVED;
471 EINA_SAFETY_ON_TRUE_GOTO(ecore_con_ssl_server_prepare(svr, compl_type & ECORE_CON_SSL), error);
473 EINA_SAFETY_ON_TRUE_GOTO(((type == ECORE_CON_REMOTE_TCP) ||
474 (type == ECORE_CON_REMOTE_NODELAY) ||
475 (type == ECORE_CON_REMOTE_CORK) ||
476 (type == ECORE_CON_REMOTE_UDP) ||
477 (type == ECORE_CON_REMOTE_BROADCAST)) &&
480 if ((type == ECORE_CON_LOCAL_USER) ||
481 (type == ECORE_CON_LOCAL_SYSTEM) ||
482 (type == ECORE_CON_LOCAL_ABSTRACT))
485 EINA_SAFETY_ON_FALSE_GOTO(ecore_con_local_connect(svr, _ecore_con_cl_handler), error);
487 EINA_SAFETY_ON_FALSE_GOTO(ecore_con_local_connect(svr, _ecore_con_cl_handler, svr), error);
490 if ((type == ECORE_CON_REMOTE_TCP) ||
491 (type == ECORE_CON_REMOTE_NODELAY) ||
492 (type == ECORE_CON_REMOTE_CORK))
495 EINA_SAFETY_ON_FALSE_GOTO(ecore_con_info_tcp_connect(svr, _ecore_con_cb_tcp_connect, svr), error);
497 else if ((type == ECORE_CON_REMOTE_UDP) || (type == ECORE_CON_REMOTE_BROADCAST))
499 EINA_SAFETY_ON_FALSE_GOTO(ecore_con_info_udp_connect(svr, _ecore_con_cb_udp_connect, svr), error);
501 servers = eina_list_append(servers, svr);
502 ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
514 ecore_main_fd_handler_del(svr->fd_handler);
519 ecore_con_ssl_server_shutdown(svr);
525 ecore_con_server_timeout_set(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_timeout_set");
535 svr->client_disconnect_time = timeout;
537 svr->disconnect_time = timeout;
541 ecore_con_server_timeout_get(Ecore_Con_Server *svr)
543 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
545 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_timeout_get");
549 return svr->created ? svr->client_disconnect_time : svr->disconnect_time;
553 ecore_con_server_del(Ecore_Con_Server *svr)
555 if (!svr) return NULL;
556 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
558 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_del");
565 _ecore_con_server_kill(svr);
570 ecore_con_server_data_get(Ecore_Con_Server *svr)
572 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
574 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_data_get");
582 ecore_con_server_data_set(Ecore_Con_Server *svr,
587 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
589 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_data_get");
599 ecore_con_server_connected_get(Ecore_Con_Server *svr)
601 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
603 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_connected_get");
613 EAPI const Eina_List *
614 ecore_con_server_clients_get(Ecore_Con_Server *svr)
616 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
618 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
619 "ecore_con_server_clients_get");
627 ecore_con_server_name_get(Ecore_Con_Server *svr)
629 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
631 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
632 "ecore_con_server_name_get");
640 ecore_con_server_port_get(Ecore_Con_Server *svr)
642 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
644 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
645 "ecore_con_server_port_get");
652 ecore_con_server_send(Ecore_Con_Server *svr,
656 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
658 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_send");
662 EINA_SAFETY_ON_TRUE_RETURN_VAL(svr->delete_me, 0);
664 EINA_SAFETY_ON_NULL_RETURN_VAL(data, 0);
666 EINA_SAFETY_ON_TRUE_RETURN_VAL(size < 1, 0);
669 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
673 svr->buf = eina_binbuf_new();
674 EINA_SAFETY_ON_NULL_RETURN_VAL(svr->buf, 0);
676 if ((svr->fd >= 0) && ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_CORK))
679 if (setsockopt(svr->fd, IPPROTO_TCP, TCP_CORK, (char *)&state, sizeof(int)) < 0)
680 /* realistically this isn't anything serious so we can just log and continue */
681 ERR("corking failed! %s", strerror(errno));
685 eina_binbuf_append_length(svr->buf, data, size);
691 ecore_con_server_client_limit_set(Ecore_Con_Server *svr,
693 char reject_excess_clients)
695 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
697 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
698 "ecore_con_server_client_limit_set");
702 svr->client_limit = client_limit;
703 svr->reject_excess_clients = reject_excess_clients;
707 ecore_con_server_ip_get(Ecore_Con_Server *svr)
709 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
711 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_ip_get");
719 ecore_con_server_uptime_get(Ecore_Con_Server *svr)
721 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
723 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_uptime_get");
727 return ecore_time_get() - svr->start_time;
731 ecore_con_server_flush(Ecore_Con_Server *svr)
733 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
735 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_flush");
739 _ecore_con_server_flush(svr);
747 * @addtogroup Ecore_Con_Client_Group Ecore Connection Client Functions
749 * Functions that operate on Ecore connection client objects.
755 * @example ecore_con_client_example.c
756 * Shows how to write a simple client that connects to the example server.
760 ecore_con_client_send(Ecore_Con_Client *cl,
764 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
766 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_send");
770 EINA_SAFETY_ON_TRUE_RETURN_VAL(cl->delete_me, 0);
772 EINA_SAFETY_ON_NULL_RETURN_VAL(data, 0);
774 EINA_SAFETY_ON_TRUE_RETURN_VAL(size < 1, 0);
777 ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
779 if (cl->host_server && ((cl->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_UDP))
780 sendto(cl->host_server->fd, data, size, 0, (struct sockaddr *)cl->client_addr,
781 cl->client_addr_len);
784 cl->buf = eina_binbuf_new();
785 EINA_SAFETY_ON_NULL_RETURN_VAL(cl->buf, 0);
787 if ((cl->fd >= 0) && ((cl->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_CORK))
790 if (setsockopt(cl->fd, IPPROTO_TCP, TCP_CORK, (char *)&state, sizeof(int)) < 0)
791 /* realistically this isn't anything serious so we can just log and continue */
792 ERR("corking failed! %s", strerror(errno));
796 eina_binbuf_append_length(cl->buf, data, size);
801 EAPI Ecore_Con_Server *
802 ecore_con_client_server_get(Ecore_Con_Client *cl)
804 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
806 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
807 "ecore_con_client_server_get");
811 return cl->host_server;
815 ecore_con_client_connected_get(Ecore_Con_Client *cl)
817 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
819 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
820 "ecore_con_client_connected_get");
824 return !cl->delete_me;
828 ecore_con_client_timeout_set(Ecore_Con_Client *cl,
831 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
833 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
834 "ecore_con_client_timeout_set");
838 cl->disconnect_time = timeout;
840 _ecore_con_cl_timer_update(cl);
844 ecore_con_client_timeout_get(Ecore_Con_Client *cl)
846 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
848 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_timeout_get");
852 return cl->disconnect_time;
856 ecore_con_client_del(Ecore_Con_Client *cl)
858 if (!cl) return NULL;
859 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
861 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_del");
865 _ecore_con_client_kill(cl);
870 ecore_con_client_data_set(Ecore_Con_Client *cl,
873 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
875 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_data_set");
879 cl->data = (void *)data;
883 ecore_con_client_data_get(Ecore_Con_Client *cl)
885 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
887 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_data_get");
895 ecore_con_client_ip_get(Ecore_Con_Client *cl)
897 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
899 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_ip_get");
903 cl->ip = _ecore_con_pretty_ip(cl->client_addr);
909 ecore_con_client_port_get(Ecore_Con_Client *cl)
911 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
913 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_port_get");
916 if (cl->client_addr->sa_family == AF_INET)
917 return ((struct sockaddr_in*)cl->client_addr)->sin_port;
919 return ((struct sockaddr_in6*)cl->client_addr)->sin6_port;
926 ecore_con_client_uptime_get(Ecore_Con_Client *cl)
928 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
930 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_uptime_get");
934 return ecore_time_get() - cl->start_time;
938 ecore_con_client_flush(Ecore_Con_Client *cl)
940 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
942 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_flush");
946 _ecore_con_client_flush(cl);
950 ecore_con_server_fd_get(Ecore_Con_Server *svr)
952 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
954 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, __func__);
957 if (svr->created) return -1;
958 if (svr->delete_me) return -1;
959 return ecore_main_fd_handler_fd_get(svr->fd_handler);
963 ecore_con_client_fd_get(Ecore_Con_Client *cl)
965 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
967 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, __func__);
970 return ecore_main_fd_handler_fd_get(cl->fd_handler);
978 ecore_con_event_proxy_bind(Ecore_Con_Server *svr)
980 Ecore_Con_Event_Proxy_Bind *e;
981 int ev = ECORE_CON_EVENT_PROXY_BIND;
983 e = ecore_con_event_proxy_bind_alloc();
984 EINA_SAFETY_ON_NULL_RETURN(e);
986 svr->event_count = eina_list_append(svr->event_count, e);
987 _ecore_con_server_timer_update(svr);
989 e->ip = svr->proxyip;
990 e->port = svr->proxyport;
991 ecore_event_add(ev, e,
992 _ecore_con_event_server_add_free, NULL);
993 _ecore_con_event_count++;
997 ecore_con_event_server_add(Ecore_Con_Server *svr)
999 /* we got our server! */
1000 Ecore_Con_Event_Server_Add *e;
1001 int ev = ECORE_CON_EVENT_SERVER_ADD;
1003 e = ecore_con_event_server_add_alloc();
1004 EINA_SAFETY_ON_NULL_RETURN(e);
1006 svr->connecting = EINA_FALSE;
1007 svr->start_time = ecore_time_get();
1008 svr->event_count = eina_list_append(svr->event_count, e);
1009 _ecore_con_server_timer_update(svr);
1011 if (svr->upgrade) ev = ECORE_CON_EVENT_SERVER_UPGRADE;
1012 ecore_event_add(ev, e,
1013 _ecore_con_event_server_add_free, NULL);
1014 _ecore_con_event_count++;
1018 ecore_con_event_server_del(Ecore_Con_Server *svr)
1020 Ecore_Con_Event_Server_Del *e;
1022 svr->delete_me = EINA_TRUE;
1023 INF("svr %p is dead", svr);
1024 e = ecore_con_event_server_del_alloc();
1025 EINA_SAFETY_ON_NULL_RETURN(e);
1027 svr->event_count = eina_list_append(svr->event_count, e);
1028 _ecore_con_server_timer_update(svr);
1032 svr->ecs_state = svr->ecs->lookup ? ECORE_CON_PROXY_STATE_RESOLVED : ECORE_CON_PROXY_STATE_DONE;
1033 eina_stringshare_replace(&svr->proxyip, NULL);
1036 ecore_event_add(ECORE_CON_EVENT_SERVER_DEL, e,
1037 _ecore_con_event_server_del_free, NULL);
1038 _ecore_con_event_count++;
1042 ecore_con_event_server_write(Ecore_Con_Server *svr, int num)
1044 Ecore_Con_Event_Server_Write *e;
1046 e = ecore_con_event_server_write_alloc();
1047 EINA_SAFETY_ON_NULL_RETURN(e);
1049 INF("Wrote %d bytes", num);
1050 svr->event_count = eina_list_append(svr->event_count, e);
1053 ecore_event_add(ECORE_CON_EVENT_SERVER_WRITE, e,
1054 (Ecore_End_Cb)_ecore_con_event_server_write_free, NULL);
1055 _ecore_con_event_count++;
1059 ecore_con_event_server_data(Ecore_Con_Server *svr, unsigned char *buf, int num, Eina_Bool duplicate)
1061 Ecore_Con_Event_Server_Data *e;
1063 e = ecore_con_event_server_data_alloc();
1064 EINA_SAFETY_ON_NULL_RETURN(e);
1066 svr->event_count = eina_list_append(svr->event_count, e);
1067 _ecore_con_server_timer_update(svr);
1071 e->data = malloc(num);
1074 ERR("server data allocation failure !");
1075 _ecore_con_event_server_data_free(NULL, e);
1078 memcpy(e->data, buf, num);
1083 ecore_event_add(ECORE_CON_EVENT_SERVER_DATA, e,
1084 _ecore_con_event_server_data_free, NULL);
1085 _ecore_con_event_count++;
1089 ecore_con_event_client_add(Ecore_Con_Client *cl)
1091 Ecore_Con_Event_Client_Add *e;
1092 int ev = ECORE_CON_EVENT_CLIENT_ADD;
1094 e = ecore_con_event_client_add_alloc();
1095 EINA_SAFETY_ON_NULL_RETURN(e);
1097 cl->event_count = eina_list_append(cl->event_count, e);
1098 cl->host_server->event_count = eina_list_append(cl->host_server->event_count, e);
1099 _ecore_con_cl_timer_update(cl);
1101 if (cl->upgrade) ev = ECORE_CON_EVENT_CLIENT_UPGRADE;
1102 ecore_event_add(ev, e,
1103 (Ecore_End_Cb)_ecore_con_event_client_add_free, cl->host_server);
1104 _ecore_con_event_count++;
1108 ecore_con_event_client_del(Ecore_Con_Client *cl)
1110 Ecore_Con_Event_Client_Del *e;
1113 cl->delete_me = EINA_TRUE;
1114 INF("cl %p is dead", cl);
1115 e = ecore_con_event_client_del_alloc();
1116 EINA_SAFETY_ON_NULL_RETURN(e);
1117 cl->event_count = eina_list_append(cl->event_count, e);
1119 cl->host_server->event_count = eina_list_append(cl->host_server->event_count, e);
1120 _ecore_con_cl_timer_update(cl);
1122 ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
1123 (Ecore_End_Cb)_ecore_con_event_client_del_free, cl->host_server);
1124 _ecore_con_event_count++;
1128 ecore_con_event_client_write(Ecore_Con_Client *cl, int num)
1130 Ecore_Con_Event_Client_Write *e;
1132 e = ecore_con_event_client_write_alloc();
1133 EINA_SAFETY_ON_NULL_RETURN(e);
1135 cl->event_count = eina_list_append(cl->event_count, e);
1136 cl->host_server->event_count = eina_list_append(cl->host_server->event_count, e);
1139 ecore_event_add(ECORE_CON_EVENT_CLIENT_WRITE, e,
1140 (Ecore_End_Cb)_ecore_con_event_client_write_free, cl->host_server);
1141 _ecore_con_event_count++;
1145 ecore_con_event_client_data(Ecore_Con_Client *cl, unsigned char *buf, int num, Eina_Bool duplicate)
1147 Ecore_Con_Event_Client_Data *e;
1149 e = ecore_con_event_client_data_alloc();
1150 EINA_SAFETY_ON_NULL_RETURN(e);
1152 cl->event_count = eina_list_append(cl->event_count, e);
1153 cl->host_server->event_count = eina_list_append(cl->host_server->event_count, e);
1154 _ecore_con_cl_timer_update(cl);
1158 e->data = malloc(num);
1161 ERR("client data allocation failure !");
1162 _ecore_con_event_client_data_free(cl->host_server, e);
1165 memcpy(e->data, buf, num);
1170 ecore_event_add(ECORE_CON_EVENT_CLIENT_DATA, e,
1171 (Ecore_End_Cb)_ecore_con_event_client_data_free, cl->host_server);
1172 _ecore_con_event_count++;
1177 ecore_con_server_infos_del(Ecore_Con_Server *svr, void *info)
1179 svr->infos = eina_list_remove(svr->infos, info);
1183 _ecore_con_event_server_error(Ecore_Con_Server *svr, char *error, Eina_Bool duplicate)
1185 Ecore_Con_Event_Server_Error *e;
1187 e = ecore_con_event_server_error_alloc();
1188 EINA_SAFETY_ON_NULL_RETURN(e);
1191 e->error = duplicate ? strdup(error) : error;
1193 svr->event_count = eina_list_append(svr->event_count, e);
1194 ecore_event_add(ECORE_CON_EVENT_SERVER_ERROR, e, (Ecore_End_Cb)_ecore_con_event_server_error_free, NULL);
1195 _ecore_con_event_count++;
1199 ecore_con_event_client_error(Ecore_Con_Client *cl, const char *error)
1201 Ecore_Con_Event_Client_Error *e;
1203 e = ecore_con_event_client_error_alloc();
1204 EINA_SAFETY_ON_NULL_RETURN(e);
1207 e->error = strdup(error);
1208 // givving errors like this is not a good thing - MAYBET his belongs in debug... but not err.
1209 // ERR("%s", error);
1210 cl->event_count = eina_list_append(cl->event_count, e);
1211 cl->host_server->event_count = eina_list_append(cl->host_server->event_count, e);
1212 ecore_event_add(ECORE_CON_EVENT_CLIENT_ERROR, e, (Ecore_End_Cb)_ecore_con_event_client_error_free, cl->host_server);
1213 _ecore_con_event_count++;
1217 _ecore_con_server_free(Ecore_Con_Server *svr)
1219 Ecore_Con_Client *cl;
1222 if (svr->event_count) return;
1226 ecore_con_info_data_clear(svr->infos->data);
1227 svr->infos = eina_list_remove_list(svr->infos, svr->infos);
1230 t_start = ecore_time_get();
1231 while (svr->buf && (!svr->delete_me))
1233 _ecore_con_server_flush(svr);
1234 t = ecore_time_get();
1235 if ((t - t_start) > 0.5)
1237 WRN("ECORE_CON: EEK - stuck in _ecore_con_server_free() trying\n"
1238 " to flush data out from the server, and have been for\n"
1239 " %1.1f seconds. This is taking too long. Aborting flush.",
1246 ecore_con_local_win32_server_del(svr);
1248 if (svr->event_count) return;
1249 ECORE_MAGIC_SET(svr, ECORE_MAGIC_NONE);
1252 eina_binbuf_free(svr->buf);
1254 EINA_LIST_FREE(svr->clients, cl)
1256 Ecore_Con_Event_Server_Add *ev;
1258 /* some pointer hacks here to prevent double frees if people are being stupid */
1259 EINA_LIST_FREE(cl->event_count, ev)
1261 cl->delete_me = EINA_TRUE;
1262 INF("cl %p is dead", cl);
1263 _ecore_con_client_free(cl);
1265 if ((svr->created) && (svr->path) && (svr->ppid == getpid()))
1268 ecore_con_ssl_server_shutdown(svr);
1273 eina_stringshare_del(svr->ip);
1274 eina_stringshare_del(svr->verify_name);
1276 if (svr->ecs_buf) eina_binbuf_free(svr->ecs_buf);
1277 if (svr->ecs_recvbuf) eina_binbuf_free(svr->ecs_recvbuf);
1279 if (svr->fd_handler)
1280 ecore_main_fd_handler_del(svr->fd_handler);
1285 if (svr->until_deletion)
1286 ecore_timer_del(svr->until_deletion);
1288 servers = eina_list_remove(servers, svr);
1294 _ecore_con_client_free(Ecore_Con_Client *cl)
1298 if (cl->event_count) return;
1300 t_start = ecore_time_get();
1301 while ((cl->buf) && (!cl->delete_me))
1303 _ecore_con_client_flush(cl);
1304 t = ecore_time_get();
1305 if ((t - t_start) > 0.5)
1307 WRN("EEK - stuck in _ecore_con_client_free() trying\n"
1308 " to flush data out from the client, and have been for\n"
1309 " %1.1f seconds. This is taking too long. Aborting flush.",
1314 cl->host_server->clients = eina_list_remove(cl->host_server->clients, cl);
1317 ecore_con_local_win32_client_del(cl);
1320 if (cl->event_count) return;
1321 ECORE_MAGIC_SET(cl, ECORE_MAGIC_NONE);
1323 if (cl->buf) eina_binbuf_free(cl->buf);
1325 if (cl->host_server->type & ECORE_CON_SSL)
1326 ecore_con_ssl_client_shutdown(cl);
1329 ecore_main_fd_handler_del(cl->fd_handler);
1334 free(cl->client_addr);
1335 cl->client_addr = NULL;
1337 if (cl->until_deletion)
1338 ecore_timer_del(cl->until_deletion);
1340 eina_stringshare_del(cl->ip);
1347 _ecore_con_server_timer(Ecore_Con_Server *svr)
1349 ecore_con_server_del(svr);
1351 svr->until_deletion = NULL;
1352 return ECORE_CALLBACK_CANCEL;
1356 _ecore_con_server_timer_update(Ecore_Con_Server *svr)
1358 if (svr->disconnect_time)
1360 if (svr->disconnect_time > 0)
1362 if (svr->until_deletion)
1363 ecore_timer_interval_set(svr->until_deletion, svr->disconnect_time);
1365 svr->until_deletion = ecore_timer_add(svr->disconnect_time, (Ecore_Task_Cb)_ecore_con_server_timer, svr);
1367 else if (svr->until_deletion)
1369 ecore_timer_del(svr->until_deletion);
1370 svr->until_deletion = NULL;
1375 if (svr->until_deletion)
1377 ecore_timer_del(svr->until_deletion);
1378 svr->until_deletion = NULL;
1384 _ecore_con_client_timer(Ecore_Con_Client *cl)
1386 ecore_con_client_del(cl);
1388 cl->until_deletion = NULL;
1389 return ECORE_CALLBACK_CANCEL;
1393 _ecore_con_cl_timer_update(Ecore_Con_Client *cl)
1395 if (cl->disconnect_time)
1397 if (cl->disconnect_time > 0)
1399 if (cl->until_deletion)
1400 ecore_timer_interval_set(cl->until_deletion, cl->disconnect_time);
1402 cl->until_deletion = ecore_timer_add(cl->disconnect_time, (Ecore_Task_Cb)_ecore_con_client_timer, cl);
1404 else if (cl->until_deletion)
1406 ecore_timer_del(cl->until_deletion);
1407 cl->until_deletion = NULL;
1412 if (cl->host_server->client_disconnect_time > 0)
1414 if (cl->until_deletion)
1415 ecore_timer_interval_set(cl->until_deletion, cl->host_server->client_disconnect_time);
1417 cl->until_deletion = ecore_timer_add(cl->host_server->client_disconnect_time, (Ecore_Task_Cb)_ecore_con_client_timer, cl);
1419 else if (cl->until_deletion)
1421 ecore_timer_del(cl->until_deletion);
1422 cl->until_deletion = NULL;
1428 _ecore_con_cb_tcp_listen(void *data,
1429 Ecore_Con_Info *net_info)
1431 Ecore_Con_Server *svr;
1433 const char *memerr = NULL;
1438 if (!net_info) /* error message has already been handled */
1440 svr->delete_me = EINA_TRUE;
1444 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1445 net_info->info.ai_protocol);
1446 if (svr->fd < 0) goto error;
1447 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
1448 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
1452 if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, (const void *)&lin,
1453 sizeof(struct linger)) < 0)
1456 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_NODELAY)
1458 #ifdef HAVE_NETINET_TCP_H
1461 if (setsockopt(svr->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
1469 if (bind(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0)
1472 if (listen(svr->fd, 4096) < 0) goto error;
1474 svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1475 _ecore_con_svr_tcp_handler, svr, NULL, NULL);
1476 if (!svr->fd_handler)
1478 memerr = "Memory allocation failure";
1485 if (errno || memerr) ecore_con_event_server_error(svr, memerr ?: strerror(errno));
1486 ecore_con_ssl_server_shutdown(svr);
1487 _ecore_con_server_kill(svr);
1491 _ecore_con_cb_udp_listen(void *data,
1492 Ecore_Con_Info *net_info)
1494 Ecore_Con_Server *svr;
1495 Ecore_Con_Type type;
1496 struct ip_mreq mreq;
1498 struct ipv6_mreq mreq6;
1501 const char *memerr = NULL;
1505 type &= ECORE_CON_TYPE;
1508 if (!net_info) /* error message has already been handled */
1510 svr->delete_me = EINA_TRUE;
1514 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1515 net_info->info.ai_protocol);
1516 if (svr->fd < 0) goto error;
1518 if (type == ECORE_CON_REMOTE_MCAST)
1520 if (net_info->info.ai_family == AF_INET)
1522 if (!inet_pton(net_info->info.ai_family, net_info->ip,
1523 &mreq.imr_multiaddr))
1526 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
1527 if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1528 (const void *)&mreq, sizeof(mreq)) != 0)
1532 else if (net_info->info.ai_family == AF_INET6)
1534 if (!inet_pton(net_info->info.ai_family, net_info->ip,
1535 &mreq6.ipv6mr_multiaddr))
1537 mreq6.ipv6mr_interface = htonl(INADDR_ANY);
1538 if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1539 (const void *)&mreq6, sizeof(mreq6)) != 0)
1545 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on, sizeof(on)) != 0)
1547 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
1548 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
1550 if (bind(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0)
1554 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1555 _ecore_con_svr_udp_handler, svr, NULL, NULL);
1556 if (!svr->fd_handler)
1558 memerr = "Memory allocation failure";
1562 svr->ip = eina_stringshare_add(net_info->ip);
1567 if (errno || memerr) ecore_con_event_server_error(svr, memerr ?: strerror(errno));
1568 ecore_con_ssl_server_shutdown(svr);
1569 _ecore_con_server_kill(svr);
1573 _ecore_con_cb_tcp_connect(void *data,
1574 Ecore_Con_Info *net_info)
1576 Ecore_Con_Server *svr;
1579 const char *memerr = NULL;
1584 if (!net_info) /* error message has already been handled */
1586 svr->delete_me = EINA_TRUE;
1590 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1591 net_info->info.ai_protocol);
1592 if (svr->fd < 0) goto error;
1594 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
1595 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
1597 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&curstate, sizeof(curstate)) < 0)
1600 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_NODELAY)
1602 #ifdef HAVE_NETINET_TCP_H
1605 if (setsockopt(svr->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int)) < 0)
1612 res = connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen);
1614 if (res == SOCKET_ERROR)
1616 if (WSAGetLastError() != WSAEINPROGRESS)
1619 err = evil_format_message(WSAGetLastError());
1620 _ecore_con_event_server_error(svr, err, EINA_FALSE);
1621 ecore_con_ssl_server_shutdown(svr);
1622 _ecore_con_server_kill(svr);
1629 if (errno != EINPROGRESS) goto error;
1631 svr->connecting = EINA_TRUE;
1633 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
1634 _ecore_con_cl_handler, svr, NULL, NULL);
1637 svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1638 _ecore_con_cl_handler, svr, NULL, NULL);
1640 if (svr->type & ECORE_CON_SSL)
1642 svr->handshaking = EINA_TRUE;
1643 svr->ssl_state = ECORE_CON_SSL_STATE_INIT;
1644 DBG("%s ssl handshake", svr->ecs_state ? "Queuing" : "Beginning");
1645 if ((!svr->ecs_state) && ecore_con_ssl_server_init(svr))
1649 if (!svr->fd_handler)
1651 memerr = "Memory allocation failure";
1655 if ((!svr->ecs) || (svr->ecs->lookup))
1656 svr->ip = eina_stringshare_add(net_info->ip);
1661 if (errno || memerr) ecore_con_event_server_error(svr, memerr ?: strerror(errno));
1662 ecore_con_ssl_server_shutdown(svr);
1663 _ecore_con_server_kill(svr);
1667 _ecore_con_cb_udp_connect(void *data,
1668 Ecore_Con_Info *net_info)
1670 Ecore_Con_Server *svr;
1673 const char *memerr = NULL;
1677 if (!net_info) /* error message has already been handled */
1679 svr->delete_me = EINA_TRUE;
1683 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1684 net_info->info.ai_protocol);
1685 if (svr->fd < 0) goto error;
1686 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
1687 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
1688 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_BROADCAST)
1690 if (setsockopt(svr->fd, SOL_SOCKET, SO_BROADCAST,
1691 (const void *)&broadcast,
1692 sizeof(broadcast)) < 0)
1697 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR,
1698 (const void *)&curstate, sizeof(curstate)) < 0)
1701 if (connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0)
1704 svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
1705 _ecore_con_cl_udp_handler, svr, NULL, NULL);
1707 if (!svr->fd_handler)
1709 memerr = "Memory allocation failure";
1713 if ((!svr->ecs) || (svr->ecs->lookup))
1714 svr->ip = eina_stringshare_add(net_info->ip);
1719 if (errno || memerr) ecore_con_event_server_error(svr, memerr ?: strerror(errno));
1720 ecore_con_ssl_server_shutdown(svr);
1721 _ecore_con_server_kill(svr);
1724 static Ecore_Con_State
1725 svr_try_connect_plain(Ecore_Con_Server *svr)
1729 socklen_t size = sizeof(int);
1731 res = getsockopt(svr->fd, SOL_SOCKET, SO_ERROR, (void *)&so_err, &size);
1733 if (res == SOCKET_ERROR)
1734 so_err = WSAGetLastError();
1736 if ((so_err == WSAEINPROGRESS) && !svr->delete_me)
1737 return ECORE_CON_INPROGRESS;
1743 if ((so_err == EINPROGRESS) && !svr->delete_me)
1744 return ECORE_CON_INPROGRESS;
1750 /* we lost our server! */
1751 ecore_con_event_server_error(svr, strerror(so_err));
1752 ERR("Connection lost: %s", strerror(so_err));
1753 _ecore_con_server_kill(svr);
1754 return ECORE_CON_DISCONNECTED;
1757 if ((!svr->delete_me) && (!svr->handshaking) && svr->connecting)
1761 if (ecore_con_socks_svr_init(svr))
1762 return ECORE_CON_INPROGRESS;
1765 ecore_con_event_server_add(svr);
1768 if (svr->fd_handler && (!svr->buf))
1769 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
1771 if (!svr->delete_me)
1772 return ECORE_CON_CONNECTED;
1774 return ECORE_CON_DISCONNECTED;
1778 _ecore_con_pretty_ip(struct sockaddr *client_addr)
1781 char ipbuf[INET_ADDRSTRLEN + 1];
1783 char ipbuf[INET6_ADDRSTRLEN + 1];
1785 int family = client_addr->sa_family;
1791 src = &(((struct sockaddr_in *)client_addr)->sin_addr);
1795 src = &(((struct sockaddr_in6 *)client_addr)->sin6_addr);
1797 if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src))
1800 src = (char*)src + 12;
1805 return eina_stringshare_add("0.0.0.0");
1808 if (!inet_ntop(family, src, ipbuf, sizeof(ipbuf)))
1809 return eina_stringshare_add("0.0.0.0");
1811 ipbuf[sizeof(ipbuf) - 1] = 0;
1812 return eina_stringshare_add(ipbuf);
1816 _ecore_con_svr_tcp_handler(void *data,
1817 Ecore_Fd_Handler *fd_handler __UNUSED__)
1819 Ecore_Con_Server *svr;
1820 Ecore_Con_Client *cl = NULL;
1821 unsigned char client_addr[256];
1822 unsigned int client_addr_len;
1823 const char *clerr = NULL;
1827 return ECORE_CALLBACK_RENEW;
1829 if ((svr->client_limit >= 0) && (!svr->reject_excess_clients) &&
1830 (svr->client_count >= (unsigned int)svr->client_limit))
1831 return ECORE_CALLBACK_RENEW;
1835 cl = calloc(1, sizeof(Ecore_Con_Client));
1838 ecore_con_event_server_error(svr, "Memory allocation failure when attempting to add a new client");
1839 return ECORE_CALLBACK_RENEW;
1841 cl->host_server = svr;
1843 client_addr_len = sizeof(client_addr);
1844 memset(&client_addr, 0, client_addr_len);
1845 cl->fd = accept(svr->fd, (struct sockaddr *)&client_addr, (socklen_t *)&client_addr_len);
1846 if (cl->fd < 0) goto error;
1847 if ((svr->client_limit >= 0) && (svr->reject_excess_clients) &&
1848 (svr->client_count >= (unsigned int)svr->client_limit))
1850 clerr = "Maximum client limit reached";
1854 if (fcntl(cl->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
1855 if (fcntl(cl->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
1856 cl->fd_handler = ecore_main_fd_handler_add(cl->fd, ECORE_FD_READ,
1857 _ecore_con_svr_cl_handler, cl, NULL, NULL);
1858 if (!cl->fd_handler) goto error;
1859 ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
1861 if ((!svr->upgrade) && (svr->type & ECORE_CON_SSL))
1863 cl->handshaking = EINA_TRUE;
1864 cl->ssl_state = ECORE_CON_SSL_STATE_INIT;
1865 if (ecore_con_ssl_client_init(cl))
1869 cl->client_addr = malloc(client_addr_len);
1870 if (!cl->client_addr)
1872 clerr = "Memory allocation failure when attempting to add a new client";
1875 cl->client_addr_len = client_addr_len;
1876 memcpy(cl->client_addr, &client_addr, client_addr_len);
1878 svr->clients = eina_list_append(svr->clients, cl);
1879 svr->client_count++;
1881 if ((!cl->delete_me) && (!cl->handshaking))
1882 ecore_con_event_client_add(cl);
1884 return ECORE_CALLBACK_RENEW;
1887 if (cl->fd_handler) ecore_main_fd_handler_del(cl->fd_handler);
1888 if (cl->fd >= 0) close(cl->fd);
1892 EINA_LIST_FREE(cl->event_count, ev)
1894 svr->event_count = eina_list_remove(svr->event_count, ev);
1895 ecore_event_del(ev);
1899 if (clerr || errno) ecore_con_event_server_error(svr, clerr ?: strerror(errno));
1900 return ECORE_CALLBACK_RENEW;
1904 _ecore_con_cl_read(Ecore_Con_Server *svr)
1907 Eina_Bool lost_server = EINA_TRUE;
1908 unsigned char buf[READBUFSIZ];
1912 /* only possible with non-ssl connections */
1913 if (svr->connecting && (svr_try_connect_plain(svr) != ECORE_CON_CONNECTED))
1916 if (svr->handshaking && (!svr->ecs_state))
1918 DBG("Continuing ssl handshake");
1919 if (!ecore_con_ssl_server_init(svr))
1920 lost_server = EINA_FALSE;
1921 _ecore_con_server_timer_update(svr);
1924 if (svr->ecs_state || !(svr->type & ECORE_CON_SSL))
1927 num = read(svr->fd, buf, sizeof(buf));
1928 /* 0 is not a valid return value for a tcp socket */
1929 if ((num > 0) || ((num < 0) && (errno == EAGAIN)))
1930 lost_server = EINA_FALSE;
1932 ecore_con_event_server_error(svr, strerror(errno));
1936 num = ecore_con_ssl_server_read(svr, buf, sizeof(buf));
1937 /* this is not an actual 0 return, 0 here just means non-fatal error such as EAGAIN */
1939 lost_server = EINA_FALSE;
1942 if ((!svr->delete_me) && (num > 0))
1945 ecore_con_socks_read(svr, buf, num);
1947 ecore_con_event_server_data(svr, buf, num, EINA_TRUE);
1951 _ecore_con_server_kill(svr);
1955 _ecore_con_cl_handler(void *data,
1956 Ecore_Fd_Handler *fd_handler)
1958 Ecore_Con_Server *svr;
1959 Eina_Bool want_read, want_write;
1963 return ECORE_CALLBACK_RENEW;
1966 return ECORE_CALLBACK_RENEW;
1968 want_read = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ);
1969 want_write = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE);
1971 if ((!svr->ecs_state) && svr->handshaking && (want_read || want_write))
1973 DBG("Continuing ssl handshake: preparing to %s...", want_read ? "read" : "write");
1977 char buf[READBUFSIZ];
1979 len = recv(svr->fd, buf, sizeof(buf), MSG_DONTWAIT | MSG_PEEK);
1980 DBG("%zu bytes in buffer", len);
1983 if (ecore_con_ssl_server_init(svr))
1985 ERR("ssl handshaking failed!");
1986 svr->handshaking = EINA_FALSE;
1988 else if (!svr->ssl_state)
1989 ecore_con_event_server_add(svr);
1990 return ECORE_CALLBACK_RENEW;
1992 if (svr->ecs && svr->ecs_state && (svr->ecs_state < ECORE_CON_PROXY_STATE_READ) && (!svr->ecs_buf))
1994 if (svr->ecs_state < ECORE_CON_PROXY_STATE_INIT)
1996 INF("PROXY STATE++");
1999 if (ecore_con_socks_svr_init(svr)) return ECORE_CALLBACK_RENEW;
2002 _ecore_con_cl_read(svr);
2003 else if (want_write) /* only possible with non-ssl connections */
2005 if (svr->connecting && (!svr_try_connect_plain(svr)) && (!svr->ecs_state))
2006 return ECORE_CALLBACK_RENEW;
2008 _ecore_con_server_flush(svr);
2011 return ECORE_CALLBACK_RENEW;
2015 _ecore_con_cl_udp_handler(void *data,
2016 Ecore_Fd_Handler *fd_handler)
2018 unsigned char buf[READBUFSIZ];
2020 Ecore_Con_Server *svr;
2021 Eina_Bool want_read, want_write;
2023 want_read = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ);
2024 want_write = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE);
2027 if (svr->delete_me || ((!want_read) && (!want_write)))
2028 return ECORE_CALLBACK_RENEW;
2032 _ecore_con_server_flush(svr);
2033 return ECORE_CALLBACK_RENEW;
2036 num = read(svr->fd, buf, READBUFSIZ);
2038 if ((!svr->delete_me) && (num > 0))
2039 ecore_con_event_server_data(svr, buf, num, EINA_TRUE);
2041 if (num < 0 && (errno != EAGAIN) && (errno != EINTR))
2043 ecore_con_event_server_error(svr, strerror(errno));
2044 _ecore_con_server_kill(svr);
2047 return ECORE_CALLBACK_RENEW;
2051 _ecore_con_svr_udp_handler(void *data,
2052 Ecore_Fd_Handler *fd_handler)
2054 unsigned char buf[READBUFSIZ];
2055 unsigned char client_addr[256];
2056 socklen_t client_addr_len = sizeof(client_addr);
2058 Ecore_Con_Server *svr;
2059 Ecore_Con_Client *cl = NULL;
2064 return ECORE_CALLBACK_RENEW;
2066 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
2068 _ecore_con_client_flush(cl);
2069 return ECORE_CALLBACK_RENEW;
2072 if (!ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
2073 return ECORE_CALLBACK_RENEW;
2076 num = fcntl(svr->fd, F_SETFL, O_NONBLOCK);
2078 num = recvfrom(svr->fd, (char *)buf, sizeof(buf), 0,
2079 (struct sockaddr *)&client_addr,
2083 num = recvfrom(svr->fd, buf, sizeof(buf), MSG_DONTWAIT,
2084 (struct sockaddr *)&client_addr,
2088 if (num < 0 && (errno != EAGAIN) && (errno != EINTR))
2090 ecore_con_event_server_error(svr, strerror(errno));
2091 if (!svr->delete_me)
2092 ecore_con_event_client_del(NULL);
2093 _ecore_con_server_kill(svr);
2094 return ECORE_CALLBACK_CANCEL;
2098 /* Create a new client for use in the client data event */
2099 cl = calloc(1, sizeof(Ecore_Con_Client));
2100 EINA_SAFETY_ON_NULL_RETURN_VAL(cl, ECORE_CALLBACK_RENEW);
2102 cl->host_server = svr;
2103 cl->client_addr = malloc(client_addr_len);
2104 if (!cl->client_addr)
2107 return ECORE_CALLBACK_RENEW;
2109 cl->client_addr_len = client_addr_len;
2111 memcpy(cl->client_addr, &client_addr, client_addr_len);
2112 ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
2113 svr->clients = eina_list_append(svr->clients, cl);
2114 svr->client_count++;
2116 ecore_con_event_client_add(cl);
2117 ecore_con_event_client_data(cl, buf, num, EINA_TRUE);
2119 return ECORE_CALLBACK_RENEW;
2123 _ecore_con_svr_cl_read(Ecore_Con_Client *cl)
2126 Eina_Bool lost_client = EINA_TRUE;
2127 unsigned char buf[READBUFSIZ];
2131 if (cl->handshaking)
2133 /* add an extra handshake attempt just before read, even though
2134 * read also attempts to handshake, to try to finish sooner
2136 if (ecore_con_ssl_client_init(cl))
2137 lost_client = EINA_FALSE;
2139 _ecore_con_cl_timer_update(cl);
2142 if (!(cl->host_server->type & ECORE_CON_SSL) && (!cl->upgrade))
2144 num = read(cl->fd, buf, sizeof(buf));
2145 /* 0 is not a valid return value for a tcp socket */
2146 if ((num > 0) || ((num < 0) && ((errno == EAGAIN) || (errno == EINTR))))
2147 lost_client = EINA_FALSE;
2149 ecore_con_event_client_error(cl, strerror(errno));
2153 num = ecore_con_ssl_client_read(cl, buf, sizeof(buf));
2154 /* this is not an actual 0 return, 0 here just means non-fatal error such as EAGAIN */
2156 lost_client = EINA_FALSE;
2159 if ((!cl->delete_me) && (num > 0))
2160 ecore_con_event_client_data(cl, buf, num, EINA_TRUE);
2162 if (lost_client) _ecore_con_client_kill(cl);
2166 _ecore_con_svr_cl_handler(void *data,
2167 Ecore_Fd_Handler *fd_handler)
2169 Ecore_Con_Client *cl;
2173 return ECORE_CALLBACK_RENEW;
2175 if (cl->handshaking && ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ | ECORE_FD_WRITE))
2177 if (ecore_con_ssl_client_init(cl))
2179 ERR("ssl handshaking failed!");
2180 _ecore_con_client_kill(cl);
2181 return ECORE_CALLBACK_RENEW;
2183 else if (!cl->ssl_state)
2184 ecore_con_event_client_add(cl);
2186 else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
2187 _ecore_con_svr_cl_read(cl);
2189 else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
2190 _ecore_con_client_flush(cl);
2192 return ECORE_CALLBACK_RENEW;
2196 _ecore_con_server_flush(Ecore_Con_Server *svr)
2199 size_t buf_len, buf_offset;
2200 const unsigned char *buf;
2202 DBG("(svr=%p,buf=%p)", svr, svr->buf);
2204 if (ecore_con_local_win32_server_flush(svr))
2208 if ((!svr->buf) && (!svr->ecs_buf) && svr->fd_handler)
2210 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
2214 buf = svr->buf ? eina_binbuf_string_get(svr->buf) : eina_binbuf_string_get(svr->ecs_buf);
2215 buf_len = svr->buf ? eina_binbuf_length_get(svr->buf) : eina_binbuf_length_get(svr->ecs_buf);
2216 buf_offset = svr->buf ? svr->write_buf_offset : svr->ecs_buf_offset;
2217 num = buf_len - buf_offset;
2219 /* check whether we need to write anything at all.
2220 * we must not write zero bytes with SSL_write() since it
2221 * causes undefined behaviour
2223 /* we thank Tommy[D] for needing to check negative buffer sizes
2224 * here because his system is amazing.
2226 if (num <= 0) return;
2228 if ((!svr->ecs_state) && svr->handshaking)
2230 DBG("Continuing ssl handshake");
2231 if (ecore_con_ssl_server_init(svr))
2232 _ecore_con_server_kill(svr);
2233 _ecore_con_server_timer_update(svr);
2237 if (svr->ecs_state || (!(svr->type & ECORE_CON_SSL)))
2238 count = write(svr->fd, buf + buf_offset, num);
2240 count = ecore_con_ssl_server_write(svr, buf + buf_offset, num);
2244 if ((errno != EAGAIN) && (errno != EINTR))
2246 ecore_con_event_server_error(svr, strerror(errno));
2247 _ecore_con_server_kill(svr);
2252 if (count && (!svr->ecs_state)) ecore_con_event_server_write(svr, count);
2254 buf_offset = svr->ecs_buf_offset += count;
2256 buf_offset = svr->write_buf_offset += count;
2257 if (buf_offset >= buf_len)
2261 svr->ecs_buf_offset = 0;
2262 eina_binbuf_free(svr->ecs_buf);
2263 svr->ecs_buf = NULL;
2264 INF("PROXY STATE++");
2269 svr->write_buf_offset = 0;
2270 eina_binbuf_free(svr->buf);
2273 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_CORK)
2276 if (setsockopt(svr->fd, IPPROTO_TCP, TCP_CORK, (char *)&state, sizeof(int)) < 0)
2277 /* realistically this isn't anything serious so we can just log and continue */
2278 ERR("uncorking failed! %s", strerror(errno));
2282 if (svr->fd_handler)
2283 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
2285 else if ((count < num) && svr->fd_handler)
2286 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_WRITE);
2290 _ecore_con_client_flush(Ecore_Con_Client *cl)
2292 int num = 0, count = 0;
2295 if (ecore_con_local_win32_client_flush(cl))
2299 if (!cl->buf && cl->fd_handler)
2301 ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ);
2305 if (cl->handshaking)
2307 if (ecore_con_ssl_client_init(cl))
2310 _ecore_con_cl_timer_update(cl);
2315 if (!cl->buf) return;
2316 num = eina_binbuf_length_get(cl->buf) - cl->buf_offset;
2317 if (num <= 0) return;
2318 if (!(cl->host_server->type & ECORE_CON_SSL) && (!cl->upgrade))
2319 count = write(cl->fd, eina_binbuf_string_get(cl->buf) + cl->buf_offset, num);
2321 count = ecore_con_ssl_client_write(cl, eina_binbuf_string_get(cl->buf) + cl->buf_offset, num);
2326 if ((errno != EAGAIN) && (errno != EINTR) && (!cl->delete_me))
2328 ecore_con_event_client_error(cl, strerror(errno));
2329 _ecore_con_client_kill(cl);
2335 if (count) ecore_con_event_client_write(cl, count);
2336 cl->buf_offset += count, num -= count;
2337 if (cl->buf_offset >= eina_binbuf_length_get(cl->buf))
2340 eina_binbuf_free(cl->buf);
2343 if ((cl->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_CORK)
2346 if (setsockopt(cl->fd, IPPROTO_TCP, TCP_CORK, (char *)&state, sizeof(int)) < 0)
2347 /* realistically this isn't anything serious so we can just log and continue */
2348 ERR("uncorking failed! %s", strerror(errno));
2352 ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ);
2354 else if (cl->fd_handler && (num >= 0))
2355 ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_WRITE);
2359 _ecore_con_event_client_add_free(Ecore_Con_Server *svr,
2362 Ecore_Con_Event_Client_Add *e;
2367 Eina_Bool svrfreed = EINA_FALSE;
2369 e->client->event_count = eina_list_remove(e->client->event_count, e);
2370 if (e->client->host_server)
2372 e->client->host_server->event_count = eina_list_remove(e->client->host_server->event_count, ev);
2373 if ((!svr->event_count) && (svr->delete_me))
2375 _ecore_con_server_free(svr);
2376 svrfreed = EINA_TRUE;
2381 if ((!e->client->event_count) && (e->client->delete_me))
2382 ecore_con_client_del(e->client);
2386 ecore_con_event_client_add_free(e);
2387 _ecore_con_event_count--;
2388 if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2389 ecore_con_mempool_shutdown();
2393 _ecore_con_event_client_del_free(Ecore_Con_Server *svr,
2396 Ecore_Con_Event_Client_Del *e;
2401 Eina_Bool svrfreed = EINA_FALSE;
2403 e->client->event_count = eina_list_remove(e->client->event_count, e);
2404 if (e->client->host_server)
2406 e->client->host_server->event_count = eina_list_remove(e->client->host_server->event_count, ev);
2407 if ((!svr->event_count) && (svr->delete_me))
2409 _ecore_con_server_free(svr);
2410 svrfreed = EINA_TRUE;
2415 if (!e->client->event_count)
2416 _ecore_con_client_free(e->client);
2419 ecore_con_event_client_del_free(e);
2420 _ecore_con_event_count--;
2421 if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2422 ecore_con_mempool_shutdown();
2426 _ecore_con_event_client_write_free(Ecore_Con_Server *svr,
2427 Ecore_Con_Event_Client_Write *e)
2431 Eina_Bool svrfreed = EINA_FALSE;
2433 e->client->event_count = eina_list_remove(e->client->event_count, e);
2434 if (e->client->host_server)
2436 e->client->host_server->event_count = eina_list_remove(e->client->host_server->event_count, e);
2437 if ((!svr->event_count) && (svr->delete_me))
2439 _ecore_con_server_free(svr);
2440 svrfreed = EINA_TRUE;
2445 if (((!e->client->event_count) && (e->client->delete_me)) ||
2446 ((e->client->host_server &&
2447 ((e->client->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_UDP ||
2448 (e->client->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_MCAST))))
2449 ecore_con_client_del(e->client);
2452 ecore_con_event_client_write_free(e);
2453 _ecore_con_event_count--;
2454 if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2455 ecore_con_mempool_shutdown();
2459 _ecore_con_event_client_data_free(Ecore_Con_Server *svr,
2462 Ecore_Con_Event_Client_Data *e;
2467 Eina_Bool svrfreed = EINA_FALSE;
2469 e->client->event_count = eina_list_remove(e->client->event_count, e);
2470 if (e->client->host_server)
2472 e->client->host_server->event_count = eina_list_remove(e->client->host_server->event_count, ev);
2474 if ((!svr->event_count) && (svr->delete_me))
2476 _ecore_con_server_free(svr);
2477 svrfreed = EINA_TRUE;
2481 if (((!e->client->event_count) && (e->client->delete_me)) ||
2482 ((e->client->host_server &&
2483 ((e->client->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_UDP ||
2484 (e->client->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_MCAST))))
2485 ecore_con_client_del(e->client);
2489 ecore_con_event_client_data_free(e);
2490 _ecore_con_event_count--;
2491 if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2492 ecore_con_mempool_shutdown();
2496 _ecore_con_event_server_add_free(void *data __UNUSED__,
2499 Ecore_Con_Event_Server_Add *e;
2504 e->server->event_count = eina_list_remove(e->server->event_count, ev);
2505 if ((!e->server->event_count) && (e->server->delete_me))
2506 _ecore_con_server_free(e->server);
2508 ecore_con_event_server_add_free(e);
2509 _ecore_con_event_count--;
2510 if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2511 ecore_con_mempool_shutdown();
2515 _ecore_con_event_server_del_free(void *data __UNUSED__,
2518 Ecore_Con_Event_Server_Del *e;
2523 e->server->event_count = eina_list_remove(e->server->event_count, ev);
2524 if (!e->server->event_count)
2525 _ecore_con_server_free(e->server);
2527 ecore_con_event_server_del_free(e);
2528 _ecore_con_event_count--;
2529 if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2530 ecore_con_mempool_shutdown();
2534 _ecore_con_event_server_write_free(void *data __UNUSED__,
2535 Ecore_Con_Event_Server_Write *e)
2539 e->server->event_count = eina_list_remove(e->server->event_count, e);
2540 if ((!e->server->event_count) && (e->server->delete_me))
2541 _ecore_con_server_free(e->server);
2544 ecore_con_event_server_write_free(e);
2545 _ecore_con_event_count--;
2546 if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2547 ecore_con_mempool_shutdown();
2551 _ecore_con_event_server_data_free(void *data __UNUSED__,
2554 Ecore_Con_Event_Server_Data *e;
2559 e->server->event_count = eina_list_remove(e->server->event_count, ev);
2560 if ((!e->server->event_count) && (e->server->delete_me))
2561 _ecore_con_server_free(e->server);
2565 ecore_con_event_server_data_free(e);
2566 _ecore_con_event_count--;
2567 if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2568 ecore_con_mempool_shutdown();
2573 _ecore_con_event_server_error_free(void *data __UNUSED__, Ecore_Con_Event_Server_Error *e)
2577 e->server->event_count = eina_list_remove(e->server->event_count, e);
2578 if ((!e->server->event_count) && (e->server->delete_me))
2579 _ecore_con_server_free(e->server);
2582 ecore_con_event_server_error_free(e);
2583 _ecore_con_event_count--;
2584 if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2585 ecore_con_mempool_shutdown();
2589 _ecore_con_event_client_error_free(Ecore_Con_Server *svr, Ecore_Con_Event_Client_Error *e)
2593 Eina_Bool svrfreed = EINA_FALSE;
2595 if (eina_list_data_find(svr->clients, e->client))
2597 e->client->event_count = eina_list_remove(e->client->event_count, e);
2598 if ((!e->client->event_count) && (e->client->delete_me))
2600 _ecore_con_client_free(e->client);
2601 svrfreed = EINA_TRUE;
2604 svr->event_count = eina_list_remove(svr->event_count, e);
2607 if ((!svr->event_count) && (svr->delete_me))
2608 _ecore_con_server_free(svr);
2612 ecore_con_event_client_error_free(e);
2613 _ecore_con_event_count--;
2614 if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2615 ecore_con_mempool_shutdown();
2619 _ecore_con_lookup_done(void *data,
2620 Ecore_Con_Info *infos)
2622 Ecore_Con_Server *svr;
2623 Ecore_Con_Lookup *lk;
2629 lk->done_cb(infos->info.ai_canonname, infos->ip,
2630 infos->info.ai_addr, infos->info.ai_addrlen,
2633 lk->done_cb(NULL, NULL, NULL, 0, (void *)lk->data);