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