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