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