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