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