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