Imported Upstream version 1.7.8
[platform/upstream/ecore.git] / src / lib / ecore_con / ecore_con.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #ifdef STDC_HEADERS
6 # include <stdlib.h>
7 # include <stddef.h>
8 #else
9 # ifdef HAVE_STDLIB_H
10 #  include <stdlib.h>
11 # endif
12 #endif
13 #ifdef HAVE_ALLOCA_H
14 # include <alloca.h>
15 #elif !defined alloca
16 # ifdef __GNUC__
17 #  define alloca __builtin_alloca
18 # elif defined _AIX
19 #  define alloca __alloca
20 # elif defined _MSC_VER
21 #  include <malloc.h>
22 #  define alloca _alloca
23 # elif !defined HAVE_ALLOCA
24 #  ifdef  __cplusplus
25 extern "C"
26 #  endif
27 void *alloca (size_t);
28 # endif
29 #endif
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38
39 #ifdef HAVE_NETINET_TCP_H
40 # include <netinet/tcp.h>
41 #endif
42
43 #ifdef HAVE_NETINET_IN_H
44 # include <netinet/in.h>
45 #endif
46
47 #ifdef HAVE_ARPA_INET_H
48 # include <arpa/inet.h>
49 #endif
50
51 #ifdef HAVE_SYS_SOCKET_H
52 # include <sys/socket.h>
53 #endif
54
55 #ifdef HAVE_SYS_UN_H
56 # include <sys/un.h>
57 #endif
58
59 #ifdef HAVE_WS2TCPIP_H
60 # include <ws2tcpip.h>
61 #endif
62
63 #ifdef HAVE_EVIL
64 # include <Evil.h>
65 #endif
66
67 #include "Ecore.h"
68 #include "ecore_private.h"
69 #include "Ecore_Con.h"
70 #include "ecore_con_private.h"
71
72 static Eina_Bool _ecore_con_client_timer(Ecore_Con_Client *cl);
73 static void      _ecore_con_cl_timer_update(Ecore_Con_Client *cl);
74 static Eina_Bool _ecore_con_server_timer(Ecore_Con_Server *svr);
75 static void      _ecore_con_server_timer_update(Ecore_Con_Server *svr);
76
77 static void      _ecore_con_cb_tcp_connect(void           *data,
78                                            Ecore_Con_Info *info);
79 static void      _ecore_con_cb_udp_connect(void           *data,
80                                            Ecore_Con_Info *info);
81 static void      _ecore_con_cb_tcp_listen(void           *data,
82                                           Ecore_Con_Info *info);
83 static void      _ecore_con_cb_udp_listen(void           *data,
84                                           Ecore_Con_Info *info);
85
86 static void      _ecore_con_server_free(Ecore_Con_Server *svr);
87 static void      _ecore_con_client_free(Ecore_Con_Client *cl);
88
89 static void      _ecore_con_cl_read(Ecore_Con_Server *svr);
90 static Eina_Bool _ecore_con_svr_tcp_handler(void             *data,
91                                             Ecore_Fd_Handler *fd_handler);
92 static Eina_Bool _ecore_con_cl_handler(void             *data,
93                                        Ecore_Fd_Handler *fd_handler);
94 static Eina_Bool _ecore_con_cl_udp_handler(void             *data,
95                                            Ecore_Fd_Handler *fd_handler);
96 static Eina_Bool _ecore_con_svr_udp_handler(void             *data,
97                                             Ecore_Fd_Handler *fd_handler);
98
99 static void      _ecore_con_svr_cl_read(Ecore_Con_Client *cl);
100 static Eina_Bool _ecore_con_svr_cl_handler(void             *data,
101                                            Ecore_Fd_Handler *fd_handler);
102
103 static void _ecore_con_server_flush(Ecore_Con_Server *svr);
104 static void _ecore_con_client_flush(Ecore_Con_Client *cl);
105
106 static void _ecore_con_event_client_add_free(Ecore_Con_Server *svr,
107                                              void *ev);
108 static void _ecore_con_event_client_del_free(Ecore_Con_Server *svr,
109                                              void *ev);
110 static void _ecore_con_event_client_data_free(Ecore_Con_Server *svr,
111                                               void *ev);
112 static void _ecore_con_event_server_add_free(void *data,
113                                              void *ev);
114 static void _ecore_con_event_server_del_free(void *data,
115                                              void *ev);
116 static void _ecore_con_event_server_data_free(void *data,
117                                               void *ev);
118 static void _ecore_con_event_server_error_free(void *data,
119                                                Ecore_Con_Event_Server_Error *e);
120 static void _ecore_con_event_client_error_free(Ecore_Con_Server *svr,
121                                                Ecore_Con_Event_Client_Error *e);
122 static void _ecore_con_event_server_write_free(void *data,
123                                                Ecore_Con_Event_Server_Write *e);
124 static void _ecore_con_event_client_write_free(Ecore_Con_Server *svr,
125                                                Ecore_Con_Event_Client_Write *e);
126
127 static void _ecore_con_lookup_done(void           *data,
128                                    Ecore_Con_Info *infos);
129
130 static const char * _ecore_con_pretty_ip(struct sockaddr *client_addr);
131
132
133 void
134 _ecore_con_client_kill(Ecore_Con_Client *cl)
135 {
136    if (cl->delete_me)
137      DBG("Multi kill request for client %p", cl);
138    else
139      {
140         ecore_con_event_client_del(cl);
141         if (cl->buf) return;
142      }
143    INF("Lost client %s", (cl->ip) ? cl->ip : "");
144    if (cl->fd_handler)
145      ecore_main_fd_handler_del(cl->fd_handler);
146
147    cl->fd_handler = NULL;
148 }
149
150 void
151 _ecore_con_server_kill(Ecore_Con_Server *svr)
152 {
153    if (svr->delete_me)
154      DBG("Multi kill request for svr %p", svr);
155    else
156      ecore_con_event_server_del(svr);
157
158    if (svr->fd_handler)
159      ecore_main_fd_handler_del(svr->fd_handler);
160
161    svr->fd_handler = NULL;
162 }
163
164 #define _ecore_con_server_kill(svr) do { \
165    DBG("KILL %p", (svr)); \
166    _ecore_con_server_kill((svr)); \
167 } while (0)
168
169 #define _ecore_con_client_kill(cl) do { \
170    DBG("KILL %p", (cl)); \
171    _ecore_con_client_kill((cl)); \
172 } while (0)
173
174 EAPI int ECORE_CON_EVENT_CLIENT_ADD = 0;
175 EAPI int ECORE_CON_EVENT_CLIENT_DEL = 0;
176 EAPI int ECORE_CON_EVENT_SERVER_ADD = 0;
177 EAPI int ECORE_CON_EVENT_SERVER_DEL = 0;
178 EAPI int ECORE_CON_EVENT_CLIENT_DATA = 0;
179 EAPI int ECORE_CON_EVENT_SERVER_DATA = 0;
180 EAPI int ECORE_CON_EVENT_CLIENT_WRITE = 0;
181 EAPI int ECORE_CON_EVENT_SERVER_WRITE = 0;
182 EAPI int ECORE_CON_EVENT_CLIENT_ERROR = 0;
183 EAPI int ECORE_CON_EVENT_SERVER_ERROR = 0;
184 EAPI int ECORE_CON_EVENT_PROXY_BIND = 0;
185
186 static Eina_List *servers = NULL;
187 static int _ecore_con_init_count = 0;
188 static int _ecore_con_event_count = 0;
189 int _ecore_con_log_dom = -1;
190 Ecore_Con_Socks *_ecore_con_proxy_once = NULL;
191 Ecore_Con_Socks *_ecore_con_proxy_global = NULL;
192
193 EAPI int
194 ecore_con_init(void)
195 {
196    if (++_ecore_con_init_count != 1)
197      return _ecore_con_init_count;
198
199 #ifdef HAVE_EVIL
200    if (!evil_init())
201      return --_ecore_con_init_count;
202
203 #endif
204
205    if (!ecore_init())
206      return --_ecore_con_init_count;
207
208    _ecore_con_log_dom = eina_log_domain_register
209        ("ecore_con", ECORE_CON_DEFAULT_LOG_COLOR);
210    if (_ecore_con_log_dom < 0)
211      {
212         EINA_LOG_ERR("Impossible to create a log domain for Ecore Con.");
213         ecore_shutdown();
214         return --_ecore_con_init_count;
215      }
216
217    ecore_con_mempool_init();
218
219    ECORE_CON_EVENT_CLIENT_ADD = ecore_event_type_new();
220    ECORE_CON_EVENT_CLIENT_DEL = ecore_event_type_new();
221    ECORE_CON_EVENT_SERVER_ADD = ecore_event_type_new();
222    ECORE_CON_EVENT_SERVER_DEL = ecore_event_type_new();
223    ECORE_CON_EVENT_CLIENT_DATA = ecore_event_type_new();
224    ECORE_CON_EVENT_SERVER_DATA = ecore_event_type_new();
225    ECORE_CON_EVENT_CLIENT_WRITE = ecore_event_type_new();
226    ECORE_CON_EVENT_SERVER_WRITE = ecore_event_type_new();
227    ECORE_CON_EVENT_CLIENT_ERROR = ecore_event_type_new();
228    ECORE_CON_EVENT_SERVER_ERROR = ecore_event_type_new();
229    ECORE_CON_EVENT_PROXY_BIND = ecore_event_type_new();
230
231
232    eina_magic_string_set(ECORE_MAGIC_CON_SERVER, "Ecore_Con_Server");
233    eina_magic_string_set(ECORE_MAGIC_CON_CLIENT, "Ecore_Con_Client");
234    eina_magic_string_set(ECORE_MAGIC_CON_URL, "Ecore_Con_Url");
235
236    /* TODO Remember return value, if it fails, use gethostbyname() */
237    ecore_con_socks_init();
238    ecore_con_ssl_init();
239    ecore_con_info_init();
240
241    return _ecore_con_init_count;
242 }
243
244 EAPI int
245 ecore_con_shutdown(void)
246 {
247    Eina_List *l, *l2;
248    Ecore_Con_Server *svr;
249
250    if (--_ecore_con_init_count != 0)
251      return _ecore_con_init_count;
252
253    EINA_LIST_FOREACH_SAFE(servers, l, l2, svr)
254      {
255         Ecore_Con_Event_Server_Add *ev;
256
257         svr->delete_me = EINA_TRUE;
258         INF("svr %p is dead", svr);
259         /* some pointer hacks here to prevent double frees if people are being stupid */
260         EINA_LIST_FREE(svr->event_count, ev)
261           ev->server = NULL;
262         _ecore_con_server_free(svr);
263      }
264
265    ecore_con_socks_shutdown();
266    if (!_ecore_con_event_count) ecore_con_mempool_shutdown();
267
268    ecore_con_info_shutdown();
269    ecore_con_ssl_shutdown();
270    eina_log_domain_unregister(_ecore_con_log_dom);
271    _ecore_con_log_dom = -1;
272    ecore_shutdown();
273 #ifdef HAVE_EVIL
274    evil_shutdown();
275 #endif
276
277    return _ecore_con_init_count;
278 }
279
280 EAPI Eina_Bool
281 ecore_con_lookup(const char      *name,
282                  Ecore_Con_Dns_Cb done_cb,
283                  const void      *data)
284 {
285    Ecore_Con_Server *svr;
286    Ecore_Con_Lookup *lk;
287    struct addrinfo hints;
288
289    if (!name || !done_cb)
290      return EINA_FALSE;
291
292    svr = calloc(1, sizeof(Ecore_Con_Server));
293    if (!svr)
294      return EINA_FALSE;
295
296    lk = malloc(sizeof (Ecore_Con_Lookup));
297    if (!lk)
298      {
299         free(svr);
300         return EINA_FALSE;
301      }
302
303    lk->done_cb = done_cb;
304    lk->data = data;
305
306    svr->name = strdup(name);
307    if (!svr->name)
308      goto on_error;
309
310    svr->type = ECORE_CON_REMOTE_TCP;
311    svr->port = 1025;
312    svr->data = lk;
313    svr->created = EINA_TRUE;
314    svr->reject_excess_clients = EINA_FALSE;
315    svr->client_limit = -1;
316    svr->clients = NULL;
317    svr->ppid = getpid();
318
319    memset(&hints, 0, sizeof(struct addrinfo));
320    hints.ai_family = AF_UNSPEC;
321    hints.ai_socktype = SOCK_STREAM;
322    hints.ai_flags = AI_CANONNAME;
323    hints.ai_protocol = IPPROTO_TCP;
324    hints.ai_canonname = NULL;
325    hints.ai_next = NULL;
326    hints.ai_addr = NULL;
327
328    if (ecore_con_info_get(svr, _ecore_con_lookup_done, svr,
329                           &hints))
330      return EINA_TRUE;
331
332    free(svr->name);
333 on_error:
334    free(lk);
335    free(svr);
336    return EINA_FALSE;
337 }
338
339 /**
340  * @addtogroup Ecore_Con_Server_Group Ecore Connection Server Functions
341  *
342  * Functions that operate on Ecore server objects.
343  *
344  * @{
345  */
346
347 /**
348  * @example ecore_con_server_example.c
349  * Shows how to write a simple server using the Ecore_Con library.
350  */
351
352 EAPI Ecore_Con_Server *
353 ecore_con_server_add(Ecore_Con_Type compl_type,
354                      const char    *name,
355                      int            port,
356                      const void    *data)
357 {
358    Ecore_Con_Server *svr;
359    Ecore_Con_Type type;
360
361    if (port < 0 || !name)
362      return NULL;  /* local  user   socket: FILE:   ~/.ecore/[name]/[port] */
363
364    /* local  system socket: FILE:   /tmp/.ecore_service|[name]|[port] */
365    /* remote system socket: TCP/IP: [name]:[port] */
366    svr = calloc(1, sizeof(Ecore_Con_Server));
367    if (!svr)
368      return NULL;
369
370    svr->name = strdup(name);
371    if (!svr->name)
372      goto error;
373
374    svr->start_time = ecore_time_get();
375    svr->type = compl_type;
376    svr->port = port;
377    svr->data = (void *)data;
378    svr->created = EINA_TRUE;
379    svr->use_cert = (compl_type & ECORE_CON_SSL & ECORE_CON_LOAD_CERT) == ECORE_CON_LOAD_CERT;
380    svr->reject_excess_clients = EINA_FALSE;
381    svr->client_limit = -1;
382    svr->clients = NULL;
383    svr->ppid = getpid();
384    if (ecore_con_ssl_server_prepare(svr, compl_type & ECORE_CON_SSL))
385      goto error;
386
387    type = compl_type & ECORE_CON_TYPE;
388
389    if ((type == ECORE_CON_LOCAL_USER) ||
390        (type == ECORE_CON_LOCAL_SYSTEM) ||
391        (type == ECORE_CON_LOCAL_ABSTRACT))
392      /* Local */
393 #ifdef _WIN32
394      if (!ecore_con_local_listen(svr))
395        goto error;
396 #else
397      if (!ecore_con_local_listen(svr, _ecore_con_svr_tcp_handler, svr))
398        goto error;
399 #endif
400
401    if ((type == ECORE_CON_REMOTE_TCP) ||
402        (type == ECORE_CON_REMOTE_NODELAY) ||
403        (type == ECORE_CON_REMOTE_CORK))
404      {
405         /* TCP */
406          if (!ecore_con_info_tcp_listen(svr, _ecore_con_cb_tcp_listen,
407                                         svr))
408            goto error;
409      }
410    else if ((type == ECORE_CON_REMOTE_MCAST) ||
411             (type == ECORE_CON_REMOTE_UDP))
412      /* UDP and MCAST */
413      if (!ecore_con_info_udp_listen(svr, _ecore_con_cb_udp_listen,
414                                     svr))
415        goto error;
416
417    servers = eina_list_append(servers, svr);
418    ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
419
420    return svr;
421
422 error:
423    if (svr->name)
424      free(svr->name);
425
426    if (svr->path)
427      free(svr->path);
428
429
430    if (svr->fd_handler)
431      ecore_main_fd_handler_del(svr->fd_handler);
432
433    if (svr->fd > 0)
434      close(svr->fd);
435
436    if (svr->buf)
437      eina_binbuf_free(svr->buf);
438
439    if (svr->ip)
440      eina_stringshare_del(svr->ip);
441
442    ecore_con_ssl_server_shutdown(svr);
443    free(svr);
444    return NULL;
445 }
446
447 EAPI Ecore_Con_Server *
448 ecore_con_server_connect(Ecore_Con_Type compl_type,
449                          const char    *name,
450                          int            port,
451                          const void    *data)
452 {
453    Ecore_Con_Server *svr;
454    Ecore_Con_Type type;
455
456    if ((!name) || (!name[0]))
457      return NULL;
458    /* local  user   socket: FILE:   ~/.ecore/[name]/[port] */
459    /* local  system socket: FILE:   /tmp/.ecore_service|[name]|[port] */
460    /* remote system socket: TCP/IP: [name]:[port] */
461    svr = calloc(1, sizeof(Ecore_Con_Server));
462    if (!svr)
463      return NULL;
464
465    svr->name = strdup(name);
466    if (!svr->name)
467      goto error;
468
469    svr->type = compl_type;
470    svr->port = port;
471    svr->data = (void *)data;
472    svr->created = EINA_FALSE;
473    svr->use_cert = (compl_type & ECORE_CON_SSL & ECORE_CON_LOAD_CERT) == ECORE_CON_LOAD_CERT;
474    svr->disable_proxy = (compl_type & ECORE_CON_SUPER_SSL & ECORE_CON_NO_PROXY) == ECORE_CON_NO_PROXY;
475    svr->reject_excess_clients = EINA_FALSE;
476    svr->clients = NULL;
477    svr->client_limit = -1;
478
479    type = compl_type & ECORE_CON_TYPE;
480
481    if ((!svr->disable_proxy) && (type > ECORE_CON_LOCAL_ABSTRACT))
482      {
483         /* never use proxies on local connections */
484         if (_ecore_con_proxy_once)
485           svr->ecs = _ecore_con_proxy_once;
486         else if (_ecore_con_proxy_global)
487           svr->ecs = _ecore_con_proxy_global;
488         _ecore_con_proxy_once = NULL;
489         if (svr->ecs)
490           {
491              if ((!svr->ecs->lookup) &&
492                  (!ecore_con_lookup(svr->name, (Ecore_Con_Dns_Cb)ecore_con_socks_dns_cb, svr)))
493                goto error;
494              if (svr->ecs->lookup)
495                svr->ecs_state = ECORE_CON_PROXY_STATE_RESOLVED;
496           }
497      }
498    EINA_SAFETY_ON_TRUE_GOTO(ecore_con_ssl_server_prepare(svr, compl_type & ECORE_CON_SSL), error);
499
500    EINA_SAFETY_ON_TRUE_GOTO(((type == ECORE_CON_REMOTE_TCP) ||
501         (type == ECORE_CON_REMOTE_NODELAY) ||
502         (type == ECORE_CON_REMOTE_CORK) ||
503         (type == ECORE_CON_REMOTE_UDP) ||
504         (type == ECORE_CON_REMOTE_BROADCAST)) &&
505        (port < 0), error);
506
507    if ((type == ECORE_CON_LOCAL_USER) ||
508        (type == ECORE_CON_LOCAL_SYSTEM) ||
509        (type == ECORE_CON_LOCAL_ABSTRACT))
510      /* Local */
511 #ifdef _WIN32
512      EINA_SAFETY_ON_FALSE_GOTO(ecore_con_local_connect(svr, _ecore_con_cl_handler), error);
513 #else
514      EINA_SAFETY_ON_FALSE_GOTO(ecore_con_local_connect(svr, _ecore_con_cl_handler, svr), error);
515 #endif
516
517    if ((type == ECORE_CON_REMOTE_TCP) ||
518        (type == ECORE_CON_REMOTE_NODELAY) ||
519        (type == ECORE_CON_REMOTE_CORK))
520      {
521         /* TCP */
522          EINA_SAFETY_ON_FALSE_GOTO(ecore_con_info_tcp_connect(svr, _ecore_con_cb_tcp_connect, svr), error);
523      }
524    else if ((type == ECORE_CON_REMOTE_UDP) || (type == ECORE_CON_REMOTE_BROADCAST))
525      /* UDP and MCAST */
526      EINA_SAFETY_ON_FALSE_GOTO(ecore_con_info_udp_connect(svr, _ecore_con_cb_udp_connect, svr), error);
527
528    servers = eina_list_append(servers, svr);
529    ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
530
531    return svr;
532
533 error:
534    if (svr->name)
535      free(svr->name);
536
537    if (svr->path)
538      free(svr->path);
539
540    if (svr->fd_handler)
541      ecore_main_fd_handler_del(svr->fd_handler);
542
543    if (svr->fd > 0)
544      close(svr->fd);
545
546    ecore_con_ssl_server_shutdown(svr);
547    free(svr);
548    return NULL;
549 }
550
551 EAPI void
552 ecore_con_server_timeout_set(Ecore_Con_Server *svr,
553                              double            timeout)
554 {
555    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
556      {
557         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_timeout_set");
558         return;
559      }
560
561    if (svr->created)
562      svr->client_disconnect_time = timeout;
563    else
564      svr->disconnect_time = timeout;
565
566    _ecore_con_server_timer_update(svr);
567 }
568
569 EAPI double
570 ecore_con_server_timeout_get(Ecore_Con_Server *svr)
571 {
572    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
573      {
574         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_timeout_get");
575         return 0;
576      }
577
578    return svr->created ? svr->client_disconnect_time : svr->disconnect_time;
579 }
580
581 EAPI void *
582 ecore_con_server_del(Ecore_Con_Server *svr)
583 {
584    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
585      {
586         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_del");
587         return NULL;
588      }
589
590    if (svr->delete_me)
591      return NULL;
592
593    _ecore_con_server_kill(svr);
594    return svr->data;
595 }
596
597 EAPI void *
598 ecore_con_server_data_get(Ecore_Con_Server *svr)
599 {
600    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
601      {
602         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_data_get");
603         return NULL;
604      }
605
606    return svr->data;
607 }
608
609 EAPI void *
610 ecore_con_server_data_set(Ecore_Con_Server *svr,
611                           void             *data)
612 {
613    void *ret = NULL;
614
615    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
616      {
617         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_data_get");
618         return NULL;
619      }
620
621    ret = svr->data;
622    svr->data = data;
623    return ret;
624 }
625
626 EAPI Eina_Bool
627 ecore_con_server_connected_get(Ecore_Con_Server *svr)
628 {
629    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
630      {
631         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_connected_get");
632         return EINA_FALSE;
633      }
634
635    if (svr->connecting)
636      return EINA_FALSE;
637
638    return EINA_TRUE;
639 }
640
641 EAPI const Eina_List *
642 ecore_con_server_clients_get(Ecore_Con_Server *svr)
643 {
644    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
645      {
646         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
647                          "ecore_con_server_clients_get");
648         return NULL;
649      }
650
651    return svr->clients;
652 }
653
654 EAPI const char *
655 ecore_con_server_name_get(Ecore_Con_Server *svr)
656 {
657    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
658      {
659         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
660                          "ecore_con_server_name_get");
661         return NULL;
662      }
663
664    return svr->name;
665 }
666
667 EAPI int
668 ecore_con_server_port_get(Ecore_Con_Server *svr)
669 {
670    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
671      {
672         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
673                          "ecore_con_server_port_get");
674         return -1;
675      }
676    return svr->port;
677 }
678
679 EAPI int
680 ecore_con_server_send(Ecore_Con_Server *svr,
681                       const void       *data,
682                       int               size)
683 {
684    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
685      {
686         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_send");
687         return 0;
688      }
689
690    EINA_SAFETY_ON_TRUE_RETURN_VAL(svr->delete_me, 0);
691
692    EINA_SAFETY_ON_NULL_RETURN_VAL(data, 0);
693
694    EINA_SAFETY_ON_TRUE_RETURN_VAL(size < 1, 0);
695
696    if (svr->fd_handler)
697      ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
698
699    if (!svr->buf)
700      {
701         svr->buf = eina_binbuf_new();
702         EINA_SAFETY_ON_NULL_RETURN_VAL(svr->buf, 0);
703 #ifdef TCP_CORK
704         if ((svr->fd >= 0) && ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_CORK))
705           {
706              int state = 1;
707              if (setsockopt(svr->fd, IPPROTO_TCP, TCP_CORK, (char *)&state, sizeof(int)) < 0)
708                /* realistically this isn't anything serious so we can just log and continue */
709                ERR("corking failed! %s", strerror(errno));
710           }
711 #endif
712      }
713    eina_binbuf_append_length(svr->buf, data, size);
714
715    return size;
716 }
717
718 EAPI void
719 ecore_con_server_client_limit_set(Ecore_Con_Server *svr,
720                                   int               client_limit,
721                                   char              reject_excess_clients)
722 {
723    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
724      {
725         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER,
726                          "ecore_con_server_client_limit_set");
727         return;
728      }
729
730    svr->client_limit = client_limit;
731    svr->reject_excess_clients = reject_excess_clients;
732 }
733
734 EAPI const char *
735 ecore_con_server_ip_get(Ecore_Con_Server *svr)
736 {
737    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
738      {
739         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_ip_get");
740         return NULL;
741      }
742
743    return svr->ip;
744 }
745
746 EAPI double
747 ecore_con_server_uptime_get(Ecore_Con_Server *svr)
748 {
749    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
750      {
751         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_uptime_get");
752         return -1;
753      }
754
755    return ecore_time_get() - svr->start_time;
756 }
757
758 EAPI void
759 ecore_con_server_flush(Ecore_Con_Server *svr)
760 {
761    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
762      {
763         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_flush");
764         return;
765      }
766
767    _ecore_con_server_flush(svr);
768 }
769
770 /**
771  * @}
772  */
773
774 /**
775  * @addtogroup Ecore_Con_Client_Group Ecore Connection Client Functions
776  *
777  * Functions that operate on Ecore connection client objects.
778  *
779  * @{
780  */
781
782 /**
783  * @example ecore_con_client_example.c
784  * Shows how to write a simple client that connects to the example server.
785  */
786
787 EAPI int
788 ecore_con_client_send(Ecore_Con_Client *cl,
789                       const void       *data,
790                       int               size)
791 {
792    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
793      {
794         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_send");
795         return 0;
796      }
797
798    EINA_SAFETY_ON_TRUE_RETURN_VAL(cl->delete_me, 0);
799
800    EINA_SAFETY_ON_NULL_RETURN_VAL(data, 0);
801
802    EINA_SAFETY_ON_TRUE_RETURN_VAL(size < 1, 0);
803
804    if (cl->fd_handler)
805      ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
806
807    if (cl->host_server && ((cl->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_UDP))
808      sendto(cl->host_server->fd, data, size, 0, (struct sockaddr *)cl->client_addr,
809             cl->client_addr_len);
810    else if (!cl->buf)
811      {
812         cl->buf = eina_binbuf_new();
813         EINA_SAFETY_ON_NULL_RETURN_VAL(cl->buf, 0);
814 #ifdef TCP_CORK
815         if ((cl->fd >= 0) && ((cl->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_CORK))
816           {
817              int state = 1;
818              if (setsockopt(cl->fd, IPPROTO_TCP, TCP_CORK, (char *)&state, sizeof(int)) < 0)
819                /* realistically this isn't anything serious so we can just log and continue */
820                ERR("corking failed! %s", strerror(errno));
821           }
822 #endif
823      }
824    eina_binbuf_append_length(cl->buf, data, size);
825
826    return size;
827 }
828
829 EAPI Ecore_Con_Server *
830 ecore_con_client_server_get(Ecore_Con_Client *cl)
831 {
832    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
833      {
834         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
835                          "ecore_con_client_server_get");
836         return NULL;
837      }
838
839    return cl->host_server;
840 }
841
842 EAPI Eina_Bool
843 ecore_con_client_connected_get(Ecore_Con_Client *cl)
844 {
845    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
846      {
847         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
848                          "ecore_con_client_connected_get");
849         return EINA_FALSE;
850      }
851
852    return !cl->delete_me;
853 }
854
855 EAPI void
856 ecore_con_client_timeout_set(Ecore_Con_Client *cl,
857                              double            timeout)
858 {
859    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
860      {
861         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT,
862                          "ecore_con_client_timeout_set");
863         return;
864      }
865
866    cl->disconnect_time = timeout;
867
868    _ecore_con_cl_timer_update(cl);
869 }
870
871 EAPI double
872 ecore_con_client_timeout_get(Ecore_Con_Client *cl)
873 {
874    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
875      {
876         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_timeout_get");
877         return 0;
878      }
879
880    return cl->disconnect_time;
881 }
882
883 EAPI void *
884 ecore_con_client_del(Ecore_Con_Client *cl)
885 {
886    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
887      {
888         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_del");
889         return NULL;
890      }
891
892    _ecore_con_client_kill(cl);
893    return cl->data;
894 }
895
896 EAPI void
897 ecore_con_client_data_set(Ecore_Con_Client *cl,
898                           const void       *data)
899 {
900    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
901      {
902         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_data_set");
903         return;
904      }
905
906    cl->data = (void *)data;
907 }
908
909 EAPI void *
910 ecore_con_client_data_get(Ecore_Con_Client *cl)
911 {
912    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
913      {
914         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_data_get");
915         return NULL;
916      }
917
918    return cl->data;
919 }
920
921 EAPI const char *
922 ecore_con_client_ip_get(Ecore_Con_Client *cl)
923 {
924    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
925      {
926         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_ip_get");
927         return NULL;
928      }
929    if (!cl->ip)
930      cl->ip = _ecore_con_pretty_ip(cl->client_addr);
931
932    return cl->ip;
933 }
934
935 EAPI int
936 ecore_con_client_port_get(Ecore_Con_Client *cl)
937 {
938    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
939      {
940         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_port_get");
941         return -1;
942      }
943    if (cl->client_addr->sa_family == AF_INET)
944      return ((struct sockaddr_in*)cl->client_addr)->sin_port;
945 #ifdef HAVE_IPV6
946    return ((struct sockaddr_in6*)cl->client_addr)->sin6_port;
947 #else
948    return -1;
949 #endif
950 }
951
952 EAPI double
953 ecore_con_client_uptime_get(Ecore_Con_Client *cl)
954 {
955    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
956      {
957         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_uptime_get");
958         return -1;
959      }
960
961    return ecore_time_get() - cl->start_time;
962 }
963
964 EAPI void
965 ecore_con_client_flush(Ecore_Con_Client *cl)
966 {
967    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
968      {
969         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_flush");
970         return;
971      }
972
973    _ecore_con_client_flush(cl);
974 }
975
976 EAPI int
977 ecore_con_server_fd_get(Ecore_Con_Server *svr)
978 {
979    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
980      {
981         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, __func__);
982         return -1;
983      }
984    if (svr->created) return -1;
985    if (svr->delete_me) return -1;
986    return ecore_main_fd_handler_fd_get(svr->fd_handler);
987 }
988
989 EAPI int
990 ecore_con_client_fd_get(Ecore_Con_Client *cl)
991 {
992    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
993      {
994         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, __func__);
995         return -1;
996      }
997    return ecore_main_fd_handler_fd_get(cl->fd_handler);
998 }
999
1000 /**
1001  * @}
1002  */
1003
1004 void
1005 ecore_con_event_proxy_bind(Ecore_Con_Server *svr)
1006 {
1007     Ecore_Con_Event_Proxy_Bind *e;
1008     int ev = ECORE_CON_EVENT_PROXY_BIND;
1009
1010     e = ecore_con_event_proxy_bind_alloc();
1011     EINA_SAFETY_ON_NULL_RETURN(e);
1012
1013     svr->event_count = eina_list_append(svr->event_count, e);
1014     _ecore_con_server_timer_update(svr);
1015     e->server = svr;
1016     e->ip = svr->proxyip;
1017     e->port = svr->proxyport;
1018     ecore_event_add(ev, e,
1019                     _ecore_con_event_server_add_free, NULL);
1020    _ecore_con_event_count++;
1021 }
1022
1023 void
1024 ecore_con_event_server_add(Ecore_Con_Server *svr)
1025 {
1026     /* we got our server! */
1027     Ecore_Con_Event_Server_Add *e;
1028     int ev = ECORE_CON_EVENT_SERVER_ADD;
1029
1030     e = ecore_con_event_server_add_alloc();
1031     EINA_SAFETY_ON_NULL_RETURN(e);
1032
1033     svr->connecting = EINA_FALSE;
1034     svr->start_time = ecore_time_get();
1035     svr->event_count = eina_list_append(svr->event_count, e);
1036     _ecore_con_server_timer_update(svr);
1037     e->server = svr;
1038     if (svr->upgrade) ev = ECORE_CON_EVENT_SERVER_UPGRADE;
1039     ecore_event_add(ev, e,
1040                     _ecore_con_event_server_add_free, NULL);
1041    _ecore_con_event_count++;
1042 }
1043
1044 void
1045 ecore_con_event_server_del(Ecore_Con_Server *svr)
1046 {
1047     Ecore_Con_Event_Server_Del *e;
1048
1049     svr->delete_me = EINA_TRUE;
1050     INF("svr %p is dead", svr);
1051     e = ecore_con_event_server_del_alloc();
1052     EINA_SAFETY_ON_NULL_RETURN(e);
1053
1054     svr->event_count = eina_list_append(svr->event_count, e);
1055     _ecore_con_server_timer_update(svr);
1056     e->server = svr;
1057     if (svr->ecs)
1058       {
1059          svr->ecs_state = svr->ecs->lookup ? ECORE_CON_PROXY_STATE_RESOLVED : ECORE_CON_PROXY_STATE_DONE;
1060          eina_stringshare_replace(&svr->proxyip, NULL);
1061          svr->proxyport = 0;
1062       }
1063     ecore_event_add(ECORE_CON_EVENT_SERVER_DEL, e,
1064                     _ecore_con_event_server_del_free, NULL);
1065    _ecore_con_event_count++;
1066 }
1067
1068 void
1069 ecore_con_event_server_write(Ecore_Con_Server *svr, int num)
1070 {
1071    Ecore_Con_Event_Server_Write *e;
1072
1073    e = ecore_con_event_server_write_alloc();
1074    EINA_SAFETY_ON_NULL_RETURN(e);
1075
1076    INF("Wrote %d bytes", num);
1077    svr->event_count = eina_list_append(svr->event_count, e);
1078    e->server = svr;
1079    e->size = num;
1080    ecore_event_add(ECORE_CON_EVENT_SERVER_WRITE, e,
1081                    (Ecore_End_Cb)_ecore_con_event_server_write_free, NULL);
1082    _ecore_con_event_count++;
1083 }
1084
1085 void
1086 ecore_con_event_server_data(Ecore_Con_Server *svr, unsigned char *buf, int num, Eina_Bool duplicate)
1087 {
1088    Ecore_Con_Event_Server_Data *e;
1089
1090    e = ecore_con_event_server_data_alloc();
1091    EINA_SAFETY_ON_NULL_RETURN(e);
1092
1093    svr->event_count = eina_list_append(svr->event_count, e);
1094    _ecore_con_server_timer_update(svr);
1095    e->server = svr;
1096    if (duplicate)
1097      {
1098         e->data = malloc(num);
1099         if (!e->data)
1100           {
1101              ERR("server data allocation failure !");
1102              _ecore_con_event_server_data_free(NULL, e);
1103              return;
1104           }
1105         memcpy(e->data, buf, num);
1106      }
1107    else
1108      e->data = buf;
1109    e->size = num;
1110    ecore_event_add(ECORE_CON_EVENT_SERVER_DATA, e,
1111                    _ecore_con_event_server_data_free, NULL);
1112    _ecore_con_event_count++;
1113 }
1114
1115 void
1116 ecore_con_event_client_add(Ecore_Con_Client *cl)
1117 {
1118    Ecore_Con_Event_Client_Add *e;
1119    int ev = ECORE_CON_EVENT_CLIENT_ADD;
1120
1121    e = ecore_con_event_client_add_alloc();
1122    EINA_SAFETY_ON_NULL_RETURN(e);
1123
1124    cl->event_count = eina_list_append(cl->event_count, e);
1125    cl->host_server->event_count = eina_list_append(cl->host_server->event_count, e);
1126    _ecore_con_cl_timer_update(cl);
1127    cl->start_time = ecore_time_get();
1128    e->client = cl;
1129    if (cl->upgrade) ev = ECORE_CON_EVENT_CLIENT_UPGRADE;
1130    ecore_event_add(ev, e,
1131                    (Ecore_End_Cb)_ecore_con_event_client_add_free, cl->host_server);
1132    _ecore_con_event_count++;
1133 }
1134
1135 void
1136 ecore_con_event_client_del(Ecore_Con_Client *cl)
1137 {
1138     Ecore_Con_Event_Client_Del *e;
1139
1140     if (!cl) return;
1141     cl->delete_me = EINA_TRUE;
1142     INF("cl %p is dead", cl);
1143     e = ecore_con_event_client_del_alloc();
1144     EINA_SAFETY_ON_NULL_RETURN(e);
1145     cl->event_count = eina_list_append(cl->event_count, e);
1146
1147     cl->host_server->event_count = eina_list_append(cl->host_server->event_count, e);
1148     _ecore_con_cl_timer_update(cl);
1149     e->client = cl;
1150     ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
1151                     (Ecore_End_Cb)_ecore_con_event_client_del_free, cl->host_server);
1152    _ecore_con_event_count++;
1153 }
1154
1155 void
1156 ecore_con_event_client_write(Ecore_Con_Client *cl, int num)
1157 {
1158    Ecore_Con_Event_Client_Write *e;
1159
1160    e = ecore_con_event_client_write_alloc();
1161    EINA_SAFETY_ON_NULL_RETURN(e);
1162
1163    cl->event_count = eina_list_append(cl->event_count, e);
1164    cl->host_server->event_count = eina_list_append(cl->host_server->event_count, e);
1165    e->client = cl;
1166    e->size = num;
1167    ecore_event_add(ECORE_CON_EVENT_CLIENT_WRITE, e,
1168                    (Ecore_End_Cb)_ecore_con_event_client_write_free, cl->host_server);
1169    _ecore_con_event_count++;
1170 }
1171
1172 void
1173 ecore_con_event_client_data(Ecore_Con_Client *cl, unsigned char *buf, int num, Eina_Bool duplicate)
1174 {
1175    Ecore_Con_Event_Client_Data *e;
1176
1177    e = ecore_con_event_client_data_alloc();
1178    EINA_SAFETY_ON_NULL_RETURN(e);
1179
1180    cl->event_count = eina_list_append(cl->event_count, e);
1181    cl->host_server->event_count = eina_list_append(cl->host_server->event_count, e);
1182    _ecore_con_cl_timer_update(cl);
1183    e->client = cl;
1184    if (duplicate)
1185      {
1186         e->data = malloc(num);
1187         if (!e->data)
1188           {
1189              ERR("client data allocation failure !");
1190              _ecore_con_event_client_data_free(cl->host_server, e);
1191              return;
1192           }
1193         memcpy(e->data, buf, num);
1194      }
1195    else
1196      e->data = buf;
1197    e->size = num;
1198    ecore_event_add(ECORE_CON_EVENT_CLIENT_DATA, e,
1199                    (Ecore_End_Cb)_ecore_con_event_client_data_free, cl->host_server);
1200    _ecore_con_event_count++;
1201 }
1202
1203
1204 void
1205 ecore_con_server_infos_del(Ecore_Con_Server *svr, void *info)
1206 {
1207    svr->infos = eina_list_remove(svr->infos, info);
1208 }
1209
1210 void
1211 _ecore_con_event_server_error(Ecore_Con_Server *svr, char *error, Eina_Bool duplicate)
1212 {
1213    Ecore_Con_Event_Server_Error *e;
1214
1215    e = ecore_con_event_server_error_alloc();
1216    EINA_SAFETY_ON_NULL_RETURN(e);
1217
1218    e->server = svr;
1219    e->error = duplicate ? strdup(error) : error;
1220    ERR("%s", error);
1221    svr->event_count = eina_list_append(svr->event_count, e);
1222    ecore_event_add(ECORE_CON_EVENT_SERVER_ERROR, e, (Ecore_End_Cb)_ecore_con_event_server_error_free, NULL);
1223    _ecore_con_event_count++;
1224 }
1225
1226 void
1227 ecore_con_event_client_error(Ecore_Con_Client *cl, const char *error)
1228 {
1229    Ecore_Con_Event_Client_Error *e;
1230
1231    e = ecore_con_event_client_error_alloc();
1232    EINA_SAFETY_ON_NULL_RETURN(e);
1233
1234    e->client = cl;
1235    e->error = strdup(error);
1236    ERR("%s", error);
1237    cl->event_count = eina_list_append(cl->event_count, e);
1238    cl->host_server->event_count = eina_list_append(cl->host_server->event_count, e);
1239    ecore_event_add(ECORE_CON_EVENT_CLIENT_ERROR, e, (Ecore_End_Cb)_ecore_con_event_client_error_free, cl->host_server);
1240    _ecore_con_event_count++;
1241 }
1242
1243 static void
1244 _ecore_con_server_free(Ecore_Con_Server *svr)
1245 {
1246    Ecore_Con_Client *cl;
1247    double t_start, t;
1248
1249    if (svr->event_count) return;
1250
1251    while (svr->infos)
1252      {
1253         ecore_con_info_data_clear(svr->infos->data);
1254         svr->infos = eina_list_remove_list(svr->infos, svr->infos);
1255      }
1256
1257    t_start = ecore_time_get();
1258    while (svr->buf && (!svr->delete_me))
1259      {
1260         _ecore_con_server_flush(svr);
1261         t = ecore_time_get();
1262         if ((t - t_start) > 0.5)
1263           {
1264              WRN("ECORE_CON: EEK - stuck in _ecore_con_server_free() trying\n"
1265                  "  to flush data out from the server, and have been for\n"
1266                  "  %1.1f seconds. This is taking too long. Aborting flush.",
1267                  (t - t_start));
1268              break;
1269           }
1270      }
1271
1272 #ifdef _WIN32
1273    ecore_con_local_win32_server_del(svr);
1274 #endif
1275    if (svr->event_count) return;
1276    ECORE_MAGIC_SET(svr, ECORE_MAGIC_NONE);
1277
1278    if (svr->buf)
1279      eina_binbuf_free(svr->buf);
1280
1281    EINA_LIST_FREE(svr->clients, cl)
1282      {
1283         Ecore_Con_Event_Server_Add *ev;
1284
1285         /* some pointer hacks here to prevent double frees if people are being stupid */
1286         EINA_LIST_FREE(cl->event_count, ev)
1287           ev->server = NULL;
1288         cl->delete_me = EINA_TRUE;
1289         INF("cl %p is dead", cl);
1290         _ecore_con_client_free(cl);
1291      }
1292    if ((svr->created) && (svr->path) && (svr->ppid == getpid()))
1293      unlink(svr->path);
1294
1295    ecore_con_ssl_server_shutdown(svr);
1296    free(svr->name);
1297
1298    free(svr->path);
1299
1300    eina_stringshare_del(svr->ip);
1301    eina_stringshare_del(svr->verify_name);
1302
1303    if (svr->ecs_buf) eina_binbuf_free(svr->ecs_buf);
1304    if (svr->ecs_recvbuf) eina_binbuf_free(svr->ecs_recvbuf);
1305
1306    if (svr->fd_handler)
1307      ecore_main_fd_handler_del(svr->fd_handler);
1308
1309    if (svr->fd > 0)
1310      close(svr->fd);
1311
1312    if (svr->until_deletion)
1313      ecore_timer_del(svr->until_deletion);
1314
1315    servers = eina_list_remove(servers, svr);
1316    svr->data = NULL;
1317    free(svr);
1318 }
1319
1320 static void
1321 _ecore_con_client_free(Ecore_Con_Client *cl)
1322 {
1323    double t_start, t;
1324
1325    if (cl->event_count) return;
1326
1327    t_start = ecore_time_get();
1328    while ((cl->buf) && (!cl->delete_me))
1329      {
1330         _ecore_con_client_flush(cl);
1331         t = ecore_time_get();
1332         if ((t - t_start) > 0.5)
1333           {
1334              WRN("EEK - stuck in _ecore_con_client_free() trying\n"
1335                  "  to flush data out from the client, and have been for\n"
1336                  "  %1.1f seconds. This is taking too long. Aborting flush.",
1337                  (t - t_start));
1338              break;
1339           }
1340      }
1341    cl->host_server->clients = eina_list_remove(cl->host_server->clients, cl);
1342    --cl->host_server->client_count;
1343
1344 #ifdef _WIN32
1345    ecore_con_local_win32_client_del(cl);
1346 #endif
1347
1348    if (cl->event_count) return;
1349    ECORE_MAGIC_SET(cl, ECORE_MAGIC_NONE);
1350
1351    if (cl->buf) eina_binbuf_free(cl->buf);
1352
1353    if (cl->host_server->type & ECORE_CON_SSL)
1354      ecore_con_ssl_client_shutdown(cl);
1355
1356    if (cl->fd_handler)
1357      ecore_main_fd_handler_del(cl->fd_handler);
1358
1359    if (cl->fd > 0)
1360      close(cl->fd);
1361
1362    free(cl->client_addr);
1363    cl->client_addr = NULL;
1364
1365    if (cl->until_deletion)
1366      ecore_timer_del(cl->until_deletion);
1367
1368    eina_stringshare_del(cl->ip);
1369    cl->data = NULL;
1370    free(cl);
1371    return;
1372 }
1373
1374 static Eina_Bool
1375 _ecore_con_server_timer(Ecore_Con_Server *svr)
1376 {
1377    ecore_con_server_del(svr);
1378
1379    svr->until_deletion = NULL;
1380    return ECORE_CALLBACK_CANCEL;
1381 }
1382
1383 static void
1384 _ecore_con_server_timer_update(Ecore_Con_Server *svr)
1385 {
1386    if (svr->disconnect_time)
1387      {
1388         if (svr->disconnect_time > 0)
1389           {
1390              if (svr->until_deletion)
1391                {
1392                   ecore_timer_interval_set(svr->until_deletion, svr->disconnect_time);
1393                   ecore_timer_reset(svr->until_deletion);
1394                }
1395              else
1396                svr->until_deletion = ecore_timer_add(svr->disconnect_time, (Ecore_Task_Cb)_ecore_con_server_timer, svr);
1397           }
1398         else if (svr->until_deletion)
1399           {
1400              ecore_timer_del(svr->until_deletion);
1401              svr->until_deletion = NULL;
1402           }
1403      }
1404    else
1405      {
1406         if (svr->until_deletion)
1407           {
1408              ecore_timer_del(svr->until_deletion);
1409              svr->until_deletion = NULL;
1410           }
1411      }
1412 }
1413
1414 static Eina_Bool
1415 _ecore_con_client_timer(Ecore_Con_Client *cl)
1416 {
1417    ecore_con_client_del(cl);
1418
1419    cl->until_deletion = NULL;
1420    return ECORE_CALLBACK_CANCEL;
1421 }
1422
1423 static void
1424 _ecore_con_cl_timer_update(Ecore_Con_Client *cl)
1425 {
1426    if (cl->disconnect_time)
1427      {
1428         if (cl->disconnect_time > 0)
1429           {
1430              if (cl->until_deletion)
1431                {
1432                   ecore_timer_interval_set(cl->until_deletion, cl->disconnect_time);
1433                   ecore_timer_reset(cl->until_deletion);
1434                }
1435              else
1436                cl->until_deletion = ecore_timer_add(cl->disconnect_time, (Ecore_Task_Cb)_ecore_con_client_timer, cl);
1437           }
1438         else if (cl->until_deletion)
1439           {
1440              ecore_timer_del(cl->until_deletion);
1441              cl->until_deletion = NULL;
1442           }
1443      }
1444    else
1445      {
1446         if (cl->host_server->client_disconnect_time > 0)
1447           {
1448              if (cl->until_deletion)
1449                {
1450                   ecore_timer_interval_set(cl->until_deletion, cl->host_server->client_disconnect_time);
1451                   ecore_timer_reset(cl->until_deletion);
1452                }
1453              else
1454                cl->until_deletion = ecore_timer_add(cl->host_server->client_disconnect_time, (Ecore_Task_Cb)_ecore_con_client_timer, cl);
1455           }
1456         else if (cl->until_deletion)
1457           {
1458              ecore_timer_del(cl->until_deletion);
1459              cl->until_deletion = NULL;
1460           }
1461      }
1462 }
1463
1464 static void
1465 _ecore_con_cb_tcp_listen(void           *data,
1466                          Ecore_Con_Info *net_info)
1467 {
1468    Ecore_Con_Server *svr;
1469    struct linger lin;
1470    const char *memerr = NULL;
1471
1472    svr = data;
1473
1474    errno = 0;
1475    if (!net_info) /* error message has already been handled */
1476      {
1477         svr->delete_me = EINA_TRUE;
1478         goto error;
1479      }
1480
1481    svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1482                     net_info->info.ai_protocol);
1483    if (svr->fd < 0) goto error;
1484    if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
1485    if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
1486
1487    lin.l_onoff = 1;
1488    lin.l_linger = 0;
1489    if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, (const void *)&lin,
1490                   sizeof(struct linger)) < 0)
1491      goto error;
1492
1493    if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_NODELAY)
1494      {
1495 #ifdef HAVE_NETINET_TCP_H
1496         int flag = 1;
1497
1498         if (setsockopt(svr->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
1499                        sizeof(int)) < 0)
1500 #endif
1501           {
1502              goto error;
1503           }
1504      }
1505
1506    if (bind(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0)
1507      goto error;
1508
1509    if (listen(svr->fd, 4096) < 0) goto error;
1510
1511    svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1512                                                _ecore_con_svr_tcp_handler, svr, NULL, NULL);
1513    if (!svr->fd_handler)
1514      {
1515         memerr = "Memory allocation failure";
1516         goto error;
1517      }
1518
1519    return;
1520
1521 error:
1522    if (errno || memerr) ecore_con_event_server_error(svr, memerr ?: strerror(errno));
1523    ecore_con_ssl_server_shutdown(svr);
1524    _ecore_con_server_kill(svr);
1525 }
1526
1527 static void
1528 _ecore_con_cb_udp_listen(void           *data,
1529                          Ecore_Con_Info *net_info)
1530 {
1531    Ecore_Con_Server *svr;
1532    Ecore_Con_Type type;
1533    struct ip_mreq mreq;
1534 #ifdef HAVE_IPV6
1535    struct ipv6_mreq mreq6;
1536 #endif
1537    const int on = 1;
1538    const char *memerr = NULL;
1539
1540    svr = data;
1541    type = svr->type;
1542    type &= ECORE_CON_TYPE;
1543
1544    errno = 0;
1545    if (!net_info) /* error message has already been handled */
1546      {
1547         svr->delete_me = EINA_TRUE;
1548         goto error;
1549      }
1550
1551    svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1552                     net_info->info.ai_protocol);
1553    if (svr->fd < 0) goto error;
1554
1555    if (type == ECORE_CON_REMOTE_MCAST)
1556      {
1557         if (net_info->info.ai_family == AF_INET)
1558           {
1559              if (!inet_pton(net_info->info.ai_family, net_info->ip,
1560                             &mreq.imr_multiaddr))
1561                goto error;
1562
1563              mreq.imr_interface.s_addr = htonl(INADDR_ANY);
1564              if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1565                             (const void *)&mreq, sizeof(mreq)) != 0)
1566                goto error;
1567           }
1568 #ifdef HAVE_IPV6
1569         else if (net_info->info.ai_family == AF_INET6)
1570           {
1571              if (!inet_pton(net_info->info.ai_family, net_info->ip,
1572                             &mreq6.ipv6mr_multiaddr))
1573                goto error;
1574              mreq6.ipv6mr_interface = htonl(INADDR_ANY);
1575              if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1576                             (const void *)&mreq6, sizeof(mreq6)) != 0)
1577                goto error;
1578           }
1579 #endif
1580      }
1581
1582    if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on, sizeof(on)) != 0)
1583      goto error;
1584    if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
1585    if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
1586
1587    if (bind(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0)
1588      goto error;
1589
1590    svr->fd_handler =
1591      ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1592                                _ecore_con_svr_udp_handler, svr, NULL, NULL);
1593    if (!svr->fd_handler)
1594      {
1595         memerr = "Memory allocation failure";
1596         goto error;
1597      }
1598
1599    svr->ip = eina_stringshare_add(net_info->ip);
1600
1601    return;
1602
1603 error:
1604    if (errno || memerr) ecore_con_event_server_error(svr, memerr ?: strerror(errno));
1605    ecore_con_ssl_server_shutdown(svr);
1606    _ecore_con_server_kill(svr);
1607 }
1608
1609 static void
1610 _ecore_con_cb_tcp_connect(void           *data,
1611                           Ecore_Con_Info *net_info)
1612 {
1613    Ecore_Con_Server *svr;
1614    int res;
1615    int curstate = 0;
1616    const char *memerr = NULL;
1617
1618    svr = data;
1619
1620    errno = 0;
1621    if (!net_info) /* error message has already been handled */
1622      {
1623         svr->delete_me = EINA_TRUE;
1624         goto error;
1625      }
1626
1627    svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1628                     net_info->info.ai_protocol);
1629    if (svr->fd < 0) goto error;
1630
1631    if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
1632    if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
1633
1634    if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&curstate, sizeof(curstate)) < 0)
1635      goto error;
1636
1637    if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_NODELAY)
1638      {
1639 #ifdef HAVE_NETINET_TCP_H
1640         int flag = 1;
1641
1642         if (setsockopt(svr->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int)) < 0)
1643 #endif
1644           {
1645              goto error;
1646           }
1647      }
1648
1649    res = connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen);
1650 #ifdef _WIN32
1651    if (res == SOCKET_ERROR)
1652      {
1653         if (WSAGetLastError() != WSAEINPROGRESS)
1654           {
1655              char *err;
1656              err = evil_format_message(WSAGetLastError());
1657              _ecore_con_event_server_error(svr, err, EINA_FALSE);
1658              ecore_con_ssl_server_shutdown(svr);
1659              _ecore_con_server_kill(svr);
1660              return;
1661           }
1662
1663 #else
1664    if (res < 0)
1665      {
1666         if (errno != EINPROGRESS) goto error;
1667 #endif
1668         svr->connecting = EINA_TRUE;
1669         svr->fd_handler =
1670           ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
1671                                     _ecore_con_cl_handler, svr, NULL, NULL);
1672      }
1673    else
1674      svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1675                                                  _ecore_con_cl_handler, svr, NULL, NULL);
1676
1677    if (svr->type & ECORE_CON_SSL)
1678      {
1679         svr->handshaking = EINA_TRUE;
1680         svr->ssl_state = ECORE_CON_SSL_STATE_INIT;
1681         DBG("%s ssl handshake", svr->ecs_state ? "Queuing" : "Beginning");
1682         if ((!svr->ecs_state) && ecore_con_ssl_server_init(svr))
1683           goto error;
1684      }
1685
1686    if (!svr->fd_handler)
1687      {
1688         memerr = "Memory allocation failure";
1689         goto error;
1690      }
1691
1692    if ((!svr->ecs) || (svr->ecs->lookup))
1693      svr->ip = eina_stringshare_add(net_info->ip);
1694
1695    return;
1696
1697 error:
1698    if (errno || memerr) ecore_con_event_server_error(svr, memerr ?: strerror(errno));
1699    ecore_con_ssl_server_shutdown(svr);
1700    _ecore_con_server_kill(svr);
1701 }
1702
1703 static void
1704 _ecore_con_cb_udp_connect(void           *data,
1705                           Ecore_Con_Info *net_info)
1706 {
1707    Ecore_Con_Server *svr;
1708    int curstate = 0;
1709    int broadcast = 1;
1710    const char *memerr = NULL;
1711    svr = data;
1712
1713    errno = 0;
1714    if (!net_info) /* error message has already been handled */
1715      {
1716         svr->delete_me = EINA_TRUE;
1717         goto error;
1718      }
1719
1720    svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1721                     net_info->info.ai_protocol);
1722    if (svr->fd < 0) goto error;
1723    if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
1724    if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
1725    if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_BROADCAST)
1726      {
1727         if (setsockopt(svr->fd, SOL_SOCKET, SO_BROADCAST,
1728                        (const void *)&broadcast,
1729                        sizeof(broadcast)) < 0)
1730           {
1731              goto error;
1732           }
1733      }
1734    if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR,
1735                   (const void *)&curstate, sizeof(curstate)) < 0)
1736      goto error;
1737
1738    if (connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0)
1739      goto error;
1740
1741    svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
1742                                                _ecore_con_cl_udp_handler, svr, NULL, NULL);
1743
1744    if (!svr->fd_handler)
1745      {
1746         memerr = "Memory allocation failure";
1747         goto error;
1748      }
1749
1750    if ((!svr->ecs) || (svr->ecs->lookup))
1751      svr->ip = eina_stringshare_add(net_info->ip);
1752
1753    return;
1754
1755 error:
1756    if (errno || memerr) ecore_con_event_server_error(svr, memerr ?: strerror(errno));
1757    ecore_con_ssl_server_shutdown(svr);
1758    _ecore_con_server_kill(svr);
1759 }
1760
1761 static Ecore_Con_State
1762 svr_try_connect_plain(Ecore_Con_Server *svr)
1763 {
1764    int res;
1765    int so_err = 0;
1766    socklen_t size = sizeof(int);
1767
1768    res = getsockopt(svr->fd, SOL_SOCKET, SO_ERROR, (void *)&so_err, &size);
1769 #ifdef _WIN32
1770    if (res == SOCKET_ERROR)
1771      so_err = WSAGetLastError();
1772
1773    if ((so_err == WSAEINPROGRESS) && !svr->delete_me)
1774      return ECORE_CON_INPROGRESS;
1775
1776 #else
1777    if (res < 0)
1778      so_err = errno;
1779
1780    if ((so_err == EINPROGRESS) && !svr->delete_me)
1781      return ECORE_CON_INPROGRESS;
1782
1783 #endif
1784
1785    if (so_err)
1786      {
1787         /* we lost our server! */
1788         ecore_con_event_server_error(svr, strerror(so_err));
1789         ERR("Connection lost: %s", strerror(so_err));
1790         _ecore_con_server_kill(svr);
1791         return ECORE_CON_DISCONNECTED;
1792      }
1793
1794    if ((!svr->delete_me) && (!svr->handshaking) && svr->connecting)
1795      {
1796          if (svr->ecs)
1797            {
1798               if (ecore_con_socks_svr_init(svr))
1799                 return ECORE_CON_INPROGRESS;
1800            }
1801          else
1802            ecore_con_event_server_add(svr);
1803      }
1804
1805    if (svr->fd_handler)
1806      {
1807         if (svr->buf)
1808           ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_WRITE);
1809         else
1810           ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
1811      }
1812
1813    if (!svr->delete_me)
1814      return ECORE_CON_CONNECTED;
1815    else
1816      return ECORE_CON_DISCONNECTED;
1817 }
1818
1819 static const char *
1820 _ecore_con_pretty_ip(struct sockaddr *client_addr)
1821 {
1822 #ifndef HAVE_IPV6
1823    char ipbuf[INET_ADDRSTRLEN + 1];
1824 #else
1825    char ipbuf[INET6_ADDRSTRLEN + 1];
1826 #endif
1827    int family = client_addr->sa_family;
1828    void *src;
1829
1830    switch(family)
1831      {
1832        case AF_INET:
1833           src = &(((struct sockaddr_in *)client_addr)->sin_addr);
1834           break;
1835 #ifdef HAVE_IPV6
1836        case AF_INET6:
1837           src = &(((struct sockaddr_in6 *)client_addr)->sin6_addr);
1838
1839           if (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)src))
1840             {
1841                family = AF_INET;
1842                src = (char*)src + 12;
1843             }
1844           break;
1845 #endif
1846        default:
1847           return eina_stringshare_add("0.0.0.0");
1848     }
1849
1850    if (!inet_ntop(family, src, ipbuf, sizeof(ipbuf)))
1851      return eina_stringshare_add("0.0.0.0");
1852
1853    ipbuf[sizeof(ipbuf) - 1] = 0;
1854    return eina_stringshare_add(ipbuf);
1855 }
1856
1857 static Eina_Bool
1858 _ecore_con_svr_tcp_handler(void                        *data,
1859                            Ecore_Fd_Handler *fd_handler __UNUSED__)
1860 {
1861    Ecore_Con_Server *svr;
1862    Ecore_Con_Client *cl = NULL;
1863    unsigned char client_addr[256];
1864    unsigned int client_addr_len;
1865    const char *clerr = NULL;
1866
1867    svr = data;
1868    if (svr->delete_me)
1869      return ECORE_CALLBACK_RENEW;
1870
1871    if ((svr->client_limit >= 0) && (!svr->reject_excess_clients) &&
1872        (svr->client_count >= (unsigned int)svr->client_limit))
1873      return ECORE_CALLBACK_RENEW;
1874
1875    /* a new client */
1876
1877    cl = calloc(1, sizeof(Ecore_Con_Client));
1878    if (!cl)
1879      {
1880         ecore_con_event_server_error(svr, "Memory allocation failure when attempting to add a new client");
1881         return ECORE_CALLBACK_RENEW;
1882      }
1883    cl->host_server = svr;
1884
1885    client_addr_len = sizeof(client_addr);
1886    memset(&client_addr, 0, client_addr_len);
1887    cl->fd = accept(svr->fd, (struct sockaddr *)&client_addr, (socklen_t *)&client_addr_len);
1888    if (cl->fd < 0) goto error;
1889    if ((svr->client_limit >= 0) && (svr->reject_excess_clients) &&
1890        (svr->client_count >= (unsigned int)svr->client_limit))
1891      {
1892         clerr = "Maximum client limit reached";
1893         goto error;
1894      }
1895
1896    if (fcntl(cl->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
1897    if (fcntl(cl->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
1898    cl->fd_handler = ecore_main_fd_handler_add(cl->fd, ECORE_FD_READ,
1899                                               _ecore_con_svr_cl_handler, cl, NULL, NULL);
1900    if (!cl->fd_handler) goto error;
1901    ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
1902
1903    if ((!svr->upgrade) && (svr->type & ECORE_CON_SSL))
1904      {
1905         cl->handshaking = EINA_TRUE;
1906         cl->ssl_state = ECORE_CON_SSL_STATE_INIT;
1907         if (ecore_con_ssl_client_init(cl))
1908           goto error;
1909      }
1910
1911    cl->client_addr = malloc(client_addr_len);
1912    if (!cl->client_addr)
1913      {
1914         clerr = "Memory allocation failure when attempting to add a new client";
1915         goto error;
1916      }
1917    cl->client_addr_len = client_addr_len;
1918    memcpy(cl->client_addr, &client_addr, client_addr_len);
1919
1920    svr->clients = eina_list_append(svr->clients, cl);
1921    svr->client_count++;
1922
1923    if ((!cl->delete_me) && (!cl->handshaking))
1924      ecore_con_event_client_add(cl);
1925
1926    return ECORE_CALLBACK_RENEW;
1927
1928 error:
1929    if (cl->fd_handler) ecore_main_fd_handler_del(cl->fd_handler);
1930    if (cl->fd >= 0) close(cl->fd);
1931    {
1932       Ecore_Event *ev;
1933
1934       EINA_LIST_FREE(cl->event_count, ev)
1935         {
1936            svr->event_count = eina_list_remove(svr->event_count, ev);
1937            ecore_event_del(ev);
1938         }
1939    }
1940    free(cl);
1941    if (clerr || errno) ecore_con_event_server_error(svr, clerr ?: strerror(errno));
1942    return ECORE_CALLBACK_RENEW;
1943 }
1944
1945 static void
1946 _ecore_con_cl_read(Ecore_Con_Server *svr)
1947 {
1948    int num = 0;
1949    Eina_Bool lost_server = EINA_TRUE;
1950    unsigned char buf[READBUFSIZ];
1951
1952    DBG("svr=%p", svr);
1953
1954    /* only possible with non-ssl connections */
1955    if (svr->connecting && (svr_try_connect_plain(svr) != ECORE_CON_CONNECTED))
1956       return;
1957
1958    if (svr->handshaking && (!svr->ecs_state))
1959      {
1960         DBG("Continuing ssl handshake");
1961         if (!ecore_con_ssl_server_init(svr))
1962            lost_server = EINA_FALSE;
1963         _ecore_con_server_timer_update(svr);
1964      }
1965
1966    if (svr->ecs_state || !(svr->type & ECORE_CON_SSL))
1967      {
1968         errno = 0;
1969         num = read(svr->fd, buf, sizeof(buf));
1970         /* 0 is not a valid return value for a tcp socket */
1971         if ((num > 0) || ((num < 0) && (errno == EAGAIN)))
1972            lost_server = EINA_FALSE;
1973         else if (num < 0)
1974           ecore_con_event_server_error(svr, strerror(errno));
1975      }
1976    else
1977      {
1978         num = ecore_con_ssl_server_read(svr, buf, sizeof(buf));
1979         /* this is not an actual 0 return, 0 here just means non-fatal error such as EAGAIN */
1980         if (num >= 0)
1981            lost_server = EINA_FALSE;
1982      }
1983
1984    if ((!svr->delete_me) && (num > 0))
1985      {
1986         if (svr->ecs_state)
1987           ecore_con_socks_read(svr, buf, num);
1988         else
1989           ecore_con_event_server_data(svr, buf, num, EINA_TRUE);
1990      }
1991
1992    if (lost_server)
1993       _ecore_con_server_kill(svr);
1994 }
1995
1996 static Eina_Bool
1997 _ecore_con_cl_handler(void             *data,
1998                       Ecore_Fd_Handler *fd_handler)
1999 {
2000    Ecore_Con_Server *svr;
2001    Eina_Bool want_read, want_write;
2002
2003    svr = data;
2004    if (svr->delete_me)
2005      return ECORE_CALLBACK_RENEW;
2006
2007    if (svr->delete_me)
2008      return ECORE_CALLBACK_RENEW;
2009
2010    want_read = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ);
2011    want_write = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE);
2012
2013    if ((!svr->ecs_state) && svr->handshaking && (want_read || want_write))
2014      {
2015         DBG("Continuing ssl handshake: preparing to %s...", want_read ? "read" : "write");
2016 #ifdef ISCOMFITOR
2017         if (want_read)
2018           {
2019              char buf[READBUFSIZ];
2020              ssize_t len;
2021              len = recv(svr->fd, buf, sizeof(buf), MSG_DONTWAIT | MSG_PEEK);
2022              DBG("%zu bytes in buffer", len);
2023           }
2024 #endif
2025         if (ecore_con_ssl_server_init(svr))
2026           {
2027              ERR("ssl handshaking failed!");
2028              svr->handshaking = EINA_FALSE;
2029           }
2030         else if (!svr->ssl_state)
2031           ecore_con_event_server_add(svr);
2032         return ECORE_CALLBACK_RENEW;
2033      }
2034    if (svr->ecs && svr->ecs_state && (svr->ecs_state < ECORE_CON_PROXY_STATE_READ) && (!svr->ecs_buf))
2035      {
2036         if (svr->ecs_state < ECORE_CON_PROXY_STATE_INIT)
2037           {
2038              INF("PROXY STATE++");
2039              svr->ecs_state++;
2040           }
2041         if (ecore_con_socks_svr_init(svr)) return ECORE_CALLBACK_RENEW;
2042      }
2043    if (want_read)
2044      _ecore_con_cl_read(svr);
2045    else if (want_write) /* only possible with non-ssl connections */
2046      {
2047         if (svr->connecting && (!svr_try_connect_plain(svr)) && (!svr->ecs_state))
2048           return ECORE_CALLBACK_RENEW;
2049
2050         _ecore_con_server_flush(svr);
2051      }
2052
2053    return ECORE_CALLBACK_RENEW;
2054 }
2055
2056 static Eina_Bool
2057 _ecore_con_cl_udp_handler(void             *data,
2058                           Ecore_Fd_Handler *fd_handler)
2059 {
2060    unsigned char buf[READBUFSIZ];
2061    int num;
2062    Ecore_Con_Server *svr;
2063    Eina_Bool want_read, want_write;
2064
2065    want_read = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ);
2066    want_write = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE);
2067
2068    svr = data;
2069    if (svr->delete_me || svr->delete_me || ((!want_read) && (!want_write)))
2070      return ECORE_CALLBACK_RENEW;
2071
2072    if (want_write)
2073      {
2074         _ecore_con_server_flush(svr);
2075         return ECORE_CALLBACK_RENEW;
2076      }
2077
2078    num = read(svr->fd, buf, READBUFSIZ);
2079
2080    if ((!svr->delete_me) && (num > 0))
2081      ecore_con_event_server_data(svr, buf, num, EINA_TRUE);
2082
2083    if (num < 0 && (errno != EAGAIN) && (errno != EINTR))
2084      {
2085         ecore_con_event_server_error(svr, strerror(errno));
2086         _ecore_con_server_kill(svr);
2087      }
2088
2089    return ECORE_CALLBACK_RENEW;
2090 }
2091
2092 static Eina_Bool
2093 _ecore_con_svr_udp_handler(void             *data,
2094                            Ecore_Fd_Handler *fd_handler)
2095 {
2096    unsigned char buf[READBUFSIZ];
2097    unsigned char client_addr[256];
2098    socklen_t client_addr_len = sizeof(client_addr);
2099    int num;
2100    Ecore_Con_Server *svr;
2101    Ecore_Con_Client *cl = NULL;
2102
2103    svr = data;
2104
2105    if (svr->delete_me)
2106      return ECORE_CALLBACK_RENEW;
2107
2108    if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
2109      {
2110         _ecore_con_client_flush(cl);
2111         return ECORE_CALLBACK_RENEW;
2112      }
2113
2114    if (!ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
2115      return ECORE_CALLBACK_RENEW;
2116
2117 #ifdef _WIN32
2118    num = fcntl(svr->fd, F_SETFL, O_NONBLOCK);
2119    if (num >= 0)
2120      num = recvfrom(svr->fd, (char *)buf, sizeof(buf), 0,
2121                 (struct sockaddr *)&client_addr,
2122                 &client_addr_len);
2123
2124 #else
2125    num = recvfrom(svr->fd, buf, sizeof(buf), MSG_DONTWAIT,
2126               (struct sockaddr *)&client_addr,
2127               &client_addr_len);
2128 #endif
2129
2130    if (num < 0 && (errno != EAGAIN) && (errno != EINTR))
2131      {
2132         ecore_con_event_server_error(svr, strerror(errno));
2133         if (!svr->delete_me)
2134           ecore_con_event_client_del(NULL);
2135         _ecore_con_server_kill(svr);
2136         return ECORE_CALLBACK_CANCEL;
2137      }
2138
2139
2140 /* Create a new client for use in the client data event */
2141    cl = calloc(1, sizeof(Ecore_Con_Client));
2142    EINA_SAFETY_ON_NULL_RETURN_VAL(cl, ECORE_CALLBACK_RENEW);
2143
2144    cl->host_server = svr;
2145    cl->client_addr = malloc(client_addr_len);
2146    if (!cl->client_addr)
2147      {
2148         free(cl);
2149         return ECORE_CALLBACK_RENEW;
2150      }
2151    cl->client_addr_len = client_addr_len;
2152
2153    memcpy(cl->client_addr, &client_addr, client_addr_len);
2154    ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
2155    svr->clients = eina_list_append(svr->clients, cl);
2156    svr->client_count++;
2157
2158    ecore_con_event_client_add(cl);
2159    ecore_con_event_client_data(cl, buf, num, EINA_TRUE);
2160
2161    return ECORE_CALLBACK_RENEW;
2162 }
2163
2164 static void
2165 _ecore_con_svr_cl_read(Ecore_Con_Client *cl)
2166 {
2167    int num = 0;
2168    Eina_Bool lost_client = EINA_TRUE;
2169    unsigned char buf[READBUFSIZ];
2170
2171    DBG("cl=%p", cl);
2172
2173    if (cl->handshaking)
2174      {
2175         /* add an extra handshake attempt just before read, even though
2176          * read also attempts to handshake, to try to finish sooner
2177          */
2178         if (ecore_con_ssl_client_init(cl))
2179           lost_client = EINA_FALSE;
2180
2181         _ecore_con_cl_timer_update(cl);
2182      }
2183
2184    if (!(cl->host_server->type & ECORE_CON_SSL) && (!cl->upgrade))
2185      {
2186         num = read(cl->fd, buf, sizeof(buf));
2187         /* 0 is not a valid return value for a tcp socket */
2188         if ((num > 0) || ((num < 0) && ((errno == EAGAIN) || (errno == EINTR))))
2189           lost_client = EINA_FALSE;
2190         else if (num < 0)
2191           ecore_con_event_client_error(cl, strerror(errno));
2192      }
2193    else
2194      {
2195         num = ecore_con_ssl_client_read(cl, buf, sizeof(buf));
2196         /* this is not an actual 0 return, 0 here just means non-fatal error such as EAGAIN */
2197         if (num >= 0)
2198           lost_client = EINA_FALSE;
2199      }
2200
2201    if ((!cl->delete_me) && (num > 0))
2202      ecore_con_event_client_data(cl, buf, num, EINA_TRUE);
2203
2204    if (lost_client) _ecore_con_client_kill(cl);
2205 }
2206
2207 static Eina_Bool
2208 _ecore_con_svr_cl_handler(void             *data,
2209                           Ecore_Fd_Handler *fd_handler)
2210 {
2211    Ecore_Con_Client *cl;
2212
2213    cl = data;
2214    if (cl->delete_me)
2215      return ECORE_CALLBACK_RENEW;
2216
2217    if (cl->handshaking && ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ | ECORE_FD_WRITE))
2218      {
2219         if (ecore_con_ssl_client_init(cl))
2220           {
2221              ERR("ssl handshaking failed!");
2222              _ecore_con_client_kill(cl);
2223              return ECORE_CALLBACK_RENEW;
2224           }
2225         else if (!cl->ssl_state)
2226           ecore_con_event_client_add(cl);
2227      }
2228    else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
2229      _ecore_con_svr_cl_read(cl);
2230
2231    else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
2232      _ecore_con_client_flush(cl);
2233
2234    return ECORE_CALLBACK_RENEW;
2235 }
2236
2237 static void
2238 _ecore_con_server_flush(Ecore_Con_Server *svr)
2239 {
2240    int count, num;
2241    size_t buf_len;
2242    unsigned int *buf_offset;
2243    const unsigned char *buf;
2244    Eina_Binbuf *buf_p;
2245
2246    DBG("(svr=%p,buf=%p)", svr, svr->buf);
2247    if (!svr->fd_handler) return;
2248 #ifdef _WIN32
2249    if (ecore_con_local_win32_server_flush(svr))
2250      return;
2251 #endif
2252
2253    if ((!svr->buf) && (!svr->ecs_buf))
2254      {
2255         ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
2256         return;
2257      }
2258
2259    if (svr->buf)
2260      {
2261         buf_p = svr->buf;
2262         buf_offset = &(svr->write_buf_offset);
2263      }
2264    else
2265      {
2266         buf_p = svr->ecs_buf;
2267         buf_offset = &(svr->ecs_buf_offset);
2268      }
2269    buf = eina_binbuf_string_get(buf_p);
2270    buf_len = eina_binbuf_length_get(buf_p);
2271    num = buf_len - *buf_offset;
2272
2273    /* check whether we need to write anything at all.
2274     * we must not write zero bytes with SSL_write() since it
2275     * causes undefined behaviour
2276     */
2277    /* we thank Tommy[D] for needing to check negative buffer sizes
2278     * here because his system is amazing.
2279     */
2280    if (num <= 0) return;
2281
2282    if ((!svr->ecs_state) && svr->handshaking)
2283      {
2284         DBG("Continuing ssl handshake");
2285         if (ecore_con_ssl_server_init(svr))
2286           _ecore_con_server_kill(svr);
2287         _ecore_con_server_timer_update(svr);
2288         return;
2289      }
2290
2291    if (svr->ecs_state || (!(svr->type & ECORE_CON_SSL)))
2292      count = write(svr->fd, buf + *buf_offset, num);
2293    else
2294      count = ecore_con_ssl_server_write(svr, buf + *buf_offset, num);
2295
2296    if (count < 0)
2297      {
2298          if ((errno != EAGAIN) && (errno != EINTR))
2299            {
2300               ecore_con_event_server_error(svr, strerror(errno));
2301               _ecore_con_server_kill(svr);
2302            }
2303          return;
2304      }
2305
2306    if (count && (!svr->ecs_state)) ecore_con_event_server_write(svr, count);
2307
2308    if (!eina_binbuf_remove(buf_p, 0, count))
2309      *buf_offset += count;
2310    else
2311      {
2312         *buf_offset = 0;
2313         buf_len -= count;
2314      }
2315    if (*buf_offset >= buf_len)
2316      {
2317         *buf_offset = 0;
2318         eina_binbuf_free(buf_p);
2319
2320         if (svr->ecs_buf)
2321           {
2322              svr->ecs_buf = NULL;
2323              INF("PROXY STATE++");
2324              svr->ecs_state++;
2325           }
2326         else
2327           {
2328              svr->buf = NULL;
2329 #ifdef TCP_CORK
2330              if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_CORK)
2331                {
2332                   int state = 0;
2333                   if (setsockopt(svr->fd, IPPROTO_TCP, TCP_CORK, (char *)&state, sizeof(int)) < 0)
2334                     /* realistically this isn't anything serious so we can just log and continue */
2335                     ERR("uncorking failed! %s", strerror(errno));
2336                }
2337 #endif
2338           }
2339         if (svr->fd_handler)
2340           ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
2341      }
2342    else if ((count < num) && svr->fd_handler)
2343      ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_WRITE);
2344 }
2345
2346 static void
2347 _ecore_con_client_flush(Ecore_Con_Client *cl)
2348 {
2349    int num = 0, count = 0;
2350
2351    if (!cl->fd_handler) return;
2352 #ifdef _WIN32
2353    if (ecore_con_local_win32_client_flush(cl))
2354      return;
2355 #endif
2356
2357    if (!cl->buf)
2358      {
2359         ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ);
2360         return;
2361      }
2362
2363    if (cl->handshaking)
2364      {
2365         if (ecore_con_ssl_client_init(cl))
2366           count = -1;
2367
2368         _ecore_con_cl_timer_update(cl);
2369      }
2370
2371    if (!count)
2372      {
2373         if (!cl->buf) return;
2374         num = eina_binbuf_length_get(cl->buf) - cl->buf_offset;
2375         if (num <= 0) return;
2376         if (!(cl->host_server->type & ECORE_CON_SSL) && (!cl->upgrade))
2377           count = write(cl->fd, eina_binbuf_string_get(cl->buf) + cl->buf_offset, num);
2378         else
2379           count = ecore_con_ssl_client_write(cl, eina_binbuf_string_get(cl->buf) + cl->buf_offset, num);
2380      }
2381
2382    if (count < 0)
2383      {
2384         if ((errno != EAGAIN) && (errno != EINTR) && (!cl->delete_me))
2385           {
2386               ecore_con_event_client_error(cl, strerror(errno));
2387               _ecore_con_client_kill(cl);
2388           }
2389
2390         return;
2391      }
2392
2393    if (count) ecore_con_event_client_write(cl, count);
2394    cl->buf_offset += count, num -= count;
2395    if (cl->buf_offset >= eina_binbuf_length_get(cl->buf))
2396      {
2397         cl->buf_offset = 0;
2398         eina_binbuf_free(cl->buf);
2399         cl->buf = NULL;
2400 #ifdef TCP_CORK
2401         if ((cl->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_CORK)
2402           {
2403              int state = 0;
2404              if (setsockopt(cl->fd, IPPROTO_TCP, TCP_CORK, (char *)&state, sizeof(int)) < 0)
2405                /* realistically this isn't anything serious so we can just log and continue */
2406                ERR("uncorking failed! %s", strerror(errno));
2407           }
2408 #endif
2409         if (cl->fd_handler)
2410           ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ);
2411      }
2412    else if (cl->fd_handler && (num >= 0))
2413      ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_WRITE);
2414 }
2415
2416 static void
2417 _ecore_con_event_client_add_free(Ecore_Con_Server *svr,
2418                                  void      *ev)
2419 {
2420    Ecore_Con_Event_Client_Add *e;
2421
2422    e = ev;
2423    if (e->client)
2424      {
2425         Eina_Bool svrfreed = EINA_FALSE;
2426         
2427         e->client->event_count = eina_list_remove(e->client->event_count, e);
2428         if (e->client->host_server)
2429           {
2430              e->client->host_server->event_count = eina_list_remove(e->client->host_server->event_count, ev);
2431              if ((!svr->event_count) && (svr->delete_me))
2432                {
2433                   _ecore_con_server_free(svr);
2434                   svrfreed = EINA_TRUE;
2435                }
2436           }
2437         if (!svrfreed)
2438           {
2439              if ((!e->client->event_count) && (e->client->delete_me))
2440                ecore_con_client_del(e->client);
2441           }
2442      }
2443
2444    ecore_con_event_client_add_free(e);
2445    _ecore_con_event_count--;
2446    if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2447      ecore_con_mempool_shutdown();
2448 }
2449
2450 static void
2451 _ecore_con_event_client_del_free(Ecore_Con_Server *svr,
2452                                  void      *ev)
2453 {
2454    Ecore_Con_Event_Client_Del *e;
2455
2456    e = ev;
2457    if (e->client)
2458      {
2459         Eina_Bool svrfreed = EINA_FALSE;
2460         
2461         e->client->event_count = eina_list_remove(e->client->event_count, e);
2462         if (e->client->host_server)
2463           {
2464              e->client->host_server->event_count = eina_list_remove(e->client->host_server->event_count, ev);
2465              if ((!svr->event_count) && (svr->delete_me))
2466                {
2467                   _ecore_con_server_free(svr);
2468                   svrfreed = EINA_TRUE;
2469                }
2470           }
2471         if (!svrfreed)
2472           {
2473              if (!e->client->event_count)
2474                _ecore_con_client_free(e->client);
2475           }
2476      }
2477    ecore_con_event_client_del_free(e);
2478    _ecore_con_event_count--;
2479    if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2480      ecore_con_mempool_shutdown();
2481 }
2482
2483 static void
2484 _ecore_con_event_client_write_free(Ecore_Con_Server *svr,
2485                                    Ecore_Con_Event_Client_Write *e)
2486 {
2487    if (e->client)
2488      {
2489         Eina_Bool svrfreed = EINA_FALSE;
2490         
2491         e->client->event_count = eina_list_remove(e->client->event_count, e);
2492         if (e->client->host_server)
2493           {
2494              e->client->host_server->event_count = eina_list_remove(e->client->host_server->event_count, e);
2495              if ((!svr->event_count) && (svr->delete_me))
2496                {
2497                   _ecore_con_server_free(svr);
2498                   svrfreed = EINA_TRUE;
2499                }
2500           }
2501         if (!svrfreed)
2502           {
2503              if (((!e->client->event_count) && (e->client->delete_me)) ||
2504                  ((e->client->host_server &&
2505                    ((e->client->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_UDP ||
2506                        (e->client->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_MCAST))))
2507                ecore_con_client_del(e->client);
2508           }
2509      }
2510    ecore_con_event_client_write_free(e);
2511    _ecore_con_event_count--;
2512    if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2513      ecore_con_mempool_shutdown();
2514 }
2515
2516 static void
2517 _ecore_con_event_client_data_free(Ecore_Con_Server *svr,
2518                                   void      *ev)
2519 {
2520    Ecore_Con_Event_Client_Data *e;
2521
2522    e = ev;
2523    if (e->client)
2524      {
2525         Eina_Bool svrfreed = EINA_FALSE;
2526         
2527         e->client->event_count = eina_list_remove(e->client->event_count, e);
2528         if (e->client->host_server)
2529           {
2530              e->client->host_server->event_count = eina_list_remove(e->client->host_server->event_count, ev);
2531           }
2532         if ((!svr->event_count) && (svr->delete_me))
2533           {
2534              _ecore_con_server_free(svr);
2535              svrfreed = EINA_TRUE;
2536           }
2537         if (!svrfreed)
2538           {
2539              if (((!e->client->event_count) && (e->client->delete_me)) ||
2540                  ((e->client->host_server &&
2541                    ((e->client->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_UDP ||
2542                        (e->client->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_MCAST))))
2543                ecore_con_client_del(e->client);
2544           }
2545      }
2546    free(e->data);
2547    ecore_con_event_client_data_free(e);
2548    _ecore_con_event_count--;
2549    if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2550      ecore_con_mempool_shutdown();
2551 }
2552
2553 static void
2554 _ecore_con_event_server_add_free(void *data __UNUSED__,
2555                                  void      *ev)
2556 {
2557    Ecore_Con_Event_Server_Add *e;
2558
2559    e = ev;
2560    if (e->server)
2561      {
2562         e->server->event_count = eina_list_remove(e->server->event_count, ev);
2563         if ((!e->server->event_count) && (e->server->delete_me))
2564           _ecore_con_server_free(e->server);
2565      }
2566    ecore_con_event_server_add_free(e);
2567    _ecore_con_event_count--;
2568    if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2569      ecore_con_mempool_shutdown();
2570 }
2571
2572 static void
2573 _ecore_con_event_server_del_free(void *data __UNUSED__,
2574                                  void      *ev)
2575 {
2576    Ecore_Con_Event_Server_Del *e;
2577
2578    e = ev;
2579    if (e->server)
2580      {
2581         e->server->event_count = eina_list_remove(e->server->event_count, ev);
2582         if (!e->server->event_count)
2583           _ecore_con_server_free(e->server);
2584      }
2585    ecore_con_event_server_del_free(e);
2586    _ecore_con_event_count--;
2587    if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2588      ecore_con_mempool_shutdown();
2589 }
2590
2591 static void
2592 _ecore_con_event_server_write_free(void *data __UNUSED__,
2593                                    Ecore_Con_Event_Server_Write *e)
2594 {
2595    if (e->server)
2596      {
2597         e->server->event_count = eina_list_remove(e->server->event_count, e);
2598         if ((!e->server->event_count) && (e->server->delete_me))
2599           _ecore_con_server_free(e->server);
2600      }
2601
2602    ecore_con_event_server_write_free(e);
2603    _ecore_con_event_count--;
2604    if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2605      ecore_con_mempool_shutdown();
2606 }
2607
2608 static void
2609 _ecore_con_event_server_data_free(void *data __UNUSED__,
2610                                   void      *ev)
2611 {
2612    Ecore_Con_Event_Server_Data *e;
2613
2614    e = ev;
2615    if (e->server)
2616      {
2617         e->server->event_count = eina_list_remove(e->server->event_count, ev);
2618         if ((!e->server->event_count) && (e->server->delete_me))
2619           _ecore_con_server_free(e->server);
2620      }
2621
2622    free(e->data);
2623    ecore_con_event_server_data_free(e);
2624    _ecore_con_event_count--;
2625    if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2626      ecore_con_mempool_shutdown();
2627 }
2628
2629
2630 static void
2631 _ecore_con_event_server_error_free(void *data __UNUSED__, Ecore_Con_Event_Server_Error *e)
2632 {
2633    if (e->server)
2634      {
2635         e->server->event_count = eina_list_remove(e->server->event_count, e);
2636         if ((!e->server->event_count) && (e->server->delete_me))
2637           _ecore_con_server_free(e->server);
2638      }
2639    free(e->error);
2640    ecore_con_event_server_error_free(e);
2641    _ecore_con_event_count--;
2642    if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2643      ecore_con_mempool_shutdown();
2644 }
2645
2646 static void
2647 _ecore_con_event_client_error_free(Ecore_Con_Server *svr, Ecore_Con_Event_Client_Error *e)
2648 {
2649    if (e->client)
2650      {
2651         Eina_Bool svrfreed = EINA_FALSE;
2652         
2653         if (eina_list_data_find(svr->clients, e->client))
2654           {
2655              e->client->event_count = eina_list_remove(e->client->event_count, e);
2656              if ((!e->client->event_count) && (e->client->delete_me))
2657                {
2658                   _ecore_con_client_free(e->client);
2659                   svrfreed = EINA_TRUE;
2660                }
2661           }
2662         svr->event_count = eina_list_remove(svr->event_count, e);
2663         if (!svrfreed)
2664           {
2665              if ((!svr->event_count) && (svr->delete_me))
2666                _ecore_con_server_free(svr);
2667           }
2668      }
2669    free(e->error);
2670    ecore_con_event_client_error_free(e);
2671    _ecore_con_event_count--;
2672    if ((!_ecore_con_event_count) && (!_ecore_con_init_count))
2673      ecore_con_mempool_shutdown();
2674 }
2675
2676 static void
2677 _ecore_con_lookup_done(void           *data,
2678                        Ecore_Con_Info *infos)
2679 {
2680    Ecore_Con_Server *svr;
2681    Ecore_Con_Lookup *lk;
2682
2683    svr = data;
2684    lk = svr->data;
2685
2686    if (infos)
2687      lk->done_cb(infos->info.ai_canonname, infos->ip,
2688                  infos->info.ai_addr, infos->info.ai_addrlen,
2689                  (void *)lk->data);
2690    else
2691      lk->done_cb(NULL, NULL, NULL, 0, (void *)lk->data);
2692
2693    free(svr->name);
2694    free(lk);
2695    free(svr);
2696 }
2697