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