svn update: 48958 (latest:48959)
[framework/uifw/ecore.git] / src / lib / ecore_con / ecore_con.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4
5 #ifdef HAVE_CONFIG_H
6 # include <config.h>
7 #endif
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <errno.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16
17 #ifdef HAVE_ARPA_INET_H
18 # include <arpa/inet.h>
19 #endif
20
21 #ifdef HAVE_NETINET_TCP_H
22 # include <netinet/tcp.h>
23 #endif
24
25 #ifdef HAVE_NETINET_IN_H
26 # include <netinet/in.h>
27 #endif
28
29 #ifdef HAVE_SYS_SOCKET_H
30 # include <sys/socket.h>
31 #endif
32
33 #ifdef HAVE_SYS_UN_H
34 # include <sys/un.h>
35 #endif
36
37 #ifdef HAVE_WS2TCPIP_H
38 # include <ws2tcpip.h>
39 #endif
40
41 #ifdef HAVE_EVIL
42 # include <Evil.h>
43 #endif
44
45 #include "Ecore.h"
46 #include "ecore_private.h"
47 #include "Ecore_Con.h"
48 #include "ecore_con_private.h"
49
50 static void _ecore_con_cb_tcp_connect(void *data, Ecore_Con_Info *info);
51 static void _ecore_con_cb_udp_connect(void *data, Ecore_Con_Info *info);
52 static void _ecore_con_cb_tcp_listen(void *data, Ecore_Con_Info *info);
53 static void _ecore_con_cb_udp_listen(void *data, Ecore_Con_Info *info);
54
55 static void _ecore_con_server_free(Ecore_Con_Server *svr);
56 static void _ecore_con_client_free(Ecore_Con_Client *cl);
57
58 static int _ecore_con_svr_handler(void *data, Ecore_Fd_Handler *fd_handler);
59 static int _ecore_con_cl_handler(void *data, Ecore_Fd_Handler *fd_handler);
60 static int _ecore_con_cl_udp_handler(void *data, Ecore_Fd_Handler *fd_handler);
61 static int _ecore_con_svr_udp_handler(void *data, Ecore_Fd_Handler *fd_handler);
62 static int _ecore_con_svr_cl_handler(void *data, Ecore_Fd_Handler *fd_handler);
63
64 static void _ecore_con_server_flush(Ecore_Con_Server *svr);
65 static void _ecore_con_client_flush(Ecore_Con_Client *cl);
66
67 static void _ecore_con_event_client_add_free(void *data, void *ev);
68 static void _ecore_con_event_client_del_free(void *data, void *ev);
69 static void _ecore_con_event_client_data_free(void *data, void *ev);
70 static void _ecore_con_event_server_add_free(void *data, void *ev);
71 static void _ecore_con_event_server_del_free(void *data, void *ev);
72 static void _ecore_con_event_server_data_free(void *data, void *ev);
73
74 EAPI int ECORE_CON_EVENT_CLIENT_ADD = 0;
75 EAPI int ECORE_CON_EVENT_CLIENT_DEL = 0;
76 EAPI int ECORE_CON_EVENT_SERVER_ADD = 0;
77 EAPI int ECORE_CON_EVENT_SERVER_DEL = 0;
78 EAPI int ECORE_CON_EVENT_CLIENT_DATA = 0;
79 EAPI int ECORE_CON_EVENT_SERVER_DATA = 0;
80
81 static Eina_List *servers = NULL;
82 static int _ecore_con_init_count = 0;
83 int _ecore_con_log_dom = -1;
84
85 /**
86  * @defgroup Ecore_Con_Lib_Group Ecore Connection Library Functions
87  *
88  * Utility functions that set up and shut down the Ecore Connection
89  * library.
90  */
91
92 /**
93  * Initialises the Ecore_Con library.
94  * @return  Number of times the library has been initialised without being
95  *          shut down.
96  * @ingroup Ecore_Con_Lib_Group
97  */
98 EAPI int
99 ecore_con_init(void)
100 {
101    if (++_ecore_con_init_count != 1)
102      return _ecore_con_init_count;
103
104 #ifdef HAVE_EVIL
105    if (!evil_init())
106      return --_ecore_con_init_count;
107 #endif
108
109    if (!ecore_init())
110      return --_ecore_con_init_count;
111
112    _ecore_con_log_dom = eina_log_domain_register("EcoreCon", ECORE_CON_DEFAULT_LOG_COLOR);
113    if(_ecore_con_log_dom < 0)
114      {
115        EINA_LOG_ERR("Impossible to create a log domain for Ecore Con.");
116        ecore_shutdown();
117        return --_ecore_con_init_count;
118      }
119    ECORE_CON_EVENT_CLIENT_ADD = ecore_event_type_new();
120    ECORE_CON_EVENT_CLIENT_DEL = ecore_event_type_new();
121    ECORE_CON_EVENT_SERVER_ADD = ecore_event_type_new();
122    ECORE_CON_EVENT_SERVER_DEL = ecore_event_type_new();
123    ECORE_CON_EVENT_CLIENT_DATA = ecore_event_type_new();
124    ECORE_CON_EVENT_SERVER_DATA = ecore_event_type_new();
125
126    /* TODO Remember return value, if it fails, use gethostbyname() */
127    ecore_con_ssl_init();
128    ecore_con_info_init();
129
130    return _ecore_con_init_count;
131 }
132
133 /**
134  * Shuts down the Ecore_Con library.
135  * @return  Number of times the library has been initialised without being
136  *          shut down.
137  * @ingroup Ecore_Con_Lib_Group
138  */
139 EAPI int
140 ecore_con_shutdown(void)
141 {
142    if (--_ecore_con_init_count != 0)
143      return _ecore_con_init_count;
144
145    while (servers)
146      _ecore_con_server_free(eina_list_data_get(servers));
147
148    ecore_con_info_shutdown();
149    ecore_con_ssl_shutdown();
150    eina_log_domain_unregister(_ecore_con_log_dom);
151    _ecore_con_log_dom = -1;
152    ecore_shutdown();
153 #ifdef HAVE_EVIL
154    evil_shutdown();
155 #endif
156
157    return _ecore_con_init_count;
158 }
159
160 /**
161  * @defgroup Ecore_Con_Server_Group Ecore Connection Server Functions
162  *
163  * Functions that operate on Ecore server objects.
164  */
165
166 /**
167  * Creates a server to listen for connections.
168  *
169  * The socket on which the server listens depends on the connection
170  * type:
171  * @li If @a compl_type is @c ECORE_CON_LOCAL_USER, the server will listen on
172  *     the Unix socket "~/.ecore/[name]/[port]".
173  * @li If @a compl_type is @c ECORE_CON_LOCAL_SYSTEM, the server will listen
174  *     on Unix socket "/tmp/.ecore_service|[name]|[port]".
175  * @li If @a compl_type is @c ECORE_CON_REMOTE_TCP, the server will listen
176  *     on TCP port @c port.
177  *
178  * @param  compl_type The connection type.
179  * @param  name       Name to associate with the socket.  It is used when
180  *                    generating the socket name of a Unix socket.  Though
181  *                    it is not used for the TCP socket, it still needs to
182  *                    be a valid character array.  @c NULL will not be
183  *                    accepted.
184  * @param  port       Number to identify socket.  When a Unix socket is used,
185  *                    it becomes part of the socket name.  When a TCP socket
186  *                    is used, it is used as the TCP port.
187  * @param  data       Data to associate with the created Ecore_Con_Server
188  *                    object.
189  * @return A new Ecore_Con_Server.
190  * @ingroup Ecore_Con_Server_Group
191  */
192 EAPI Ecore_Con_Server *
193 ecore_con_server_add(Ecore_Con_Type compl_type, const char *name, int port,
194                      const void *data)
195 {
196    Ecore_Con_Server   *svr;
197    Ecore_Con_Type      type;
198
199    if (port < 0 || !name) return NULL;
200    /* local  user   socket: FILE:   ~/.ecore/[name]/[port] */
201    /* local  system socket: FILE:   /tmp/.ecore_service|[name]|[port] */
202    /* remote system socket: TCP/IP: [name]:[port] */
203    svr = calloc(1, sizeof(Ecore_Con_Server));
204    if (!svr) return NULL;
205
206    svr->name = strdup(name);
207    if (!svr->name) goto error;
208    svr->type = compl_type;
209    svr->port = port;
210    svr->data = (void *)data;
211    svr->created = 1;
212    svr->reject_excess_clients = 0;
213    svr->client_limit = -1;
214    svr->clients = NULL;
215    svr->ppid = getpid();
216    ecore_con_ssl_server_prepare(svr);
217
218    type = compl_type & ECORE_CON_TYPE;
219
220    if ((type == ECORE_CON_LOCAL_USER) || 
221        (type == ECORE_CON_LOCAL_SYSTEM) ||
222        (type == ECORE_CON_LOCAL_ABSTRACT))
223      {
224         /* Local */
225         if (!ecore_con_local_listen(svr, _ecore_con_svr_handler, svr)) goto error;
226      }
227    
228    if ((type == ECORE_CON_REMOTE_TCP) || 
229        (type == ECORE_CON_REMOTE_NODELAY))
230      {
231         /* TCP */
232         if (!ecore_con_info_tcp_listen(svr, _ecore_con_cb_tcp_listen, svr)) goto error;
233      }
234    else if ((type == ECORE_CON_REMOTE_MCAST) ||
235             (type == ECORE_CON_REMOTE_UDP))
236      {
237         /* UDP and MCAST */
238         if (!ecore_con_info_udp_listen(svr, _ecore_con_cb_udp_listen, svr)) goto error;
239      }
240    
241    servers = eina_list_append(servers, svr);
242    ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
243
244    return svr;
245
246    error:
247    if (svr->name) free(svr->name);
248    if (svr->path) free(svr->path);
249 #ifndef _WIN32
250    if (svr->fd >= 0) close(svr->fd);
251    if (svr->fd_handler) ecore_main_fd_handler_del(svr->fd_handler);
252    if (svr->write_buf) free(svr->write_buf);
253    if (svr->ip) free(svr->ip);
254 #endif
255    ecore_con_ssl_server_shutdown(svr);
256    free(svr);
257    return NULL;
258 }
259
260 /**
261  * Creates a server object to represent the server listening at the
262  * given port.
263  *
264  * The socket to which the server connects depends on the connection type:
265  * @li If @a compl_type is @c ECORE_CON_LOCAL_USER, the function will
266  *     connect to the server listening on the Unix socket
267  *     "~/.ecore/[name]/[port]".
268  * @li If @a compl_type is @c ECORE_CON_LOCAL_SYSTEM, the function will
269  *     connect to the server listening on the Unix socket
270  *     "/tmp/.ecore_service|[name]|[port]".
271  * @li If @a compl_type is @c ECORE_CON_REMOTE_TCP, the function will
272  *     connect to the server listening on the TCP port "[name]:[port]".
273  *
274  * @param  compl_type The connection type.
275  * @param  name       Name used when determining what socket to connect to.
276  *                    It is used to generate the socket name when the socket
277  *                    is a Unix socket.  It is used as the hostname when
278  *                    connecting with a TCP socket.
279  * @param  port       Number to identify the socket to connect to.  Used when
280  *                    generating the socket name for a Unix socket, or as the
281  *                    TCP port when connecting to a TCP socket.
282  * @param  data       Data to associate with the created Ecore_Con_Server
283  *                    object.
284  * @return A new Ecore_Con_Server.
285  * @ingroup Ecore_Con_Server_Group
286  */
287 EAPI Ecore_Con_Server *
288 ecore_con_server_connect(Ecore_Con_Type compl_type, const char *name, int port,
289                          const void *data)
290 {
291    Ecore_Con_Server   *svr;
292    Ecore_Con_Type      type;
293
294    if (!name) return NULL;
295    /* local  user   socket: FILE:   ~/.ecore/[name]/[port] */
296    /* local  system socket: FILE:   /tmp/.ecore_service|[name]|[port] */
297    /* remote system socket: TCP/IP: [name]:[port] */
298    svr = calloc(1, sizeof(Ecore_Con_Server));
299    if (!svr) return NULL;
300
301    svr->name = strdup(name);
302    if (!svr->name) goto error;
303    svr->type = compl_type;
304    svr->port = port;
305    svr->data = (void *)data;
306    svr->created = 0;
307    svr->reject_excess_clients = 0;
308    svr->clients = NULL;
309    svr->client_limit = -1;
310    ecore_con_ssl_server_prepare(svr);
311
312    type = compl_type & ECORE_CON_TYPE;
313
314    if (((type == ECORE_CON_REMOTE_TCP) || 
315         (type == ECORE_CON_REMOTE_NODELAY) ||
316         (type == ECORE_CON_REMOTE_UDP) || 
317         (type == ECORE_CON_REMOTE_BROADCAST)) &&
318        (port < 0))
319      goto error;
320    
321    if ((type == ECORE_CON_LOCAL_USER) || 
322        (type == ECORE_CON_LOCAL_SYSTEM) ||
323        (type == ECORE_CON_LOCAL_ABSTRACT))
324      {
325         /* Local */
326        if (!ecore_con_local_connect(svr, _ecore_con_cl_handler, svr, _ecore_con_event_server_add_free)) goto error;
327      }
328
329    if ((type == ECORE_CON_REMOTE_TCP) || 
330        (type == ECORE_CON_REMOTE_NODELAY))
331      {
332         /* TCP */
333         if (!ecore_con_info_tcp_connect(svr, _ecore_con_cb_tcp_connect, svr)) goto error;
334      }
335    else if ((type == ECORE_CON_REMOTE_UDP) || 
336             (type == ECORE_CON_REMOTE_BROADCAST))
337      {
338         /* UDP and MCAST */
339         if (!ecore_con_info_udp_connect(svr, _ecore_con_cb_udp_connect, svr)) goto error;
340      }
341
342    servers = eina_list_append(servers, svr);
343    ECORE_MAGIC_SET(svr, ECORE_MAGIC_CON_SERVER);
344
345    return svr;
346
347    error:
348    if (svr->name) free(svr->name);
349    if (svr->path) free(svr->path);
350    if (svr->fd >= 0) close(svr->fd);
351    if (svr->fd_handler) ecore_main_fd_handler_del(svr->fd_handler);
352    ecore_con_ssl_server_shutdown(svr);
353    free(svr);
354    return NULL;
355 }
356
357 /**
358  * Closes the connection and frees the given server.
359  * @param   svr The given server.
360  * @return  Data associated with the server when it was created.
361  * @ingroup Ecore_Con_Server_Group
362  */
363 EAPI void *
364 ecore_con_server_del(Ecore_Con_Server *svr)
365 {
366    void *data;
367
368    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
369      {
370         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_del");
371         return NULL;
372      }
373    if (svr->delete_me) return NULL;
374
375    data = svr->data;
376    svr->data = NULL;
377    svr->delete_me = 1;
378    if (svr->event_count > 0)
379      {
380         if (svr->fd_handler)
381           {
382              ecore_main_fd_handler_del(svr->fd_handler);
383              svr->fd_handler = NULL;
384           }
385      }
386    else
387      {
388         _ecore_con_server_free(svr);
389      }
390    return data;
391 }
392
393 /**
394  * Retrieves the data associated with the given server.
395  * @param   svr The given server.
396  * @return  The associated data.
397  * @ingroup Ecore_Con_Server_Group
398  */
399 EAPI void *
400 ecore_con_server_data_get(Ecore_Con_Server *svr)
401 {
402    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
403      {
404         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_data_get");
405         return NULL;
406      }
407    return svr->data;
408 }
409
410 /**
411  * Retrieves whether the given server is currently connected.
412  * @todo Check that this function does what the documenter believes it does.
413  * @param   svr The given server.
414  * @return  @c 1 if the server is connected.  @c 0 otherwise.
415  * @ingroup Ecore_Con_Server_Group
416  */
417 EAPI int
418 ecore_con_server_connected_get(Ecore_Con_Server *svr)
419 {
420    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
421      {
422         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_connected_get");
423         return 0;
424      }
425    if (svr->connecting) return 0;
426    return 1;
427 }
428
429 /**
430  * Retrieves the current list of clients.
431  * @param   svr The given server.
432  * @return  The list of clients on this server.
433  * @ingroup Ecore_Con_Server_Group
434  */
435 EAPI Eina_List *
436 ecore_con_server_clients_get(Ecore_Con_Server *svr)
437 {
438    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
439      {
440         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_clients_get");
441         return NULL;
442      }
443    return svr->clients;
444 }
445
446 /**
447  * Sends the given data to the given server.
448  * @param   svr  The given server.
449  * @param   data The given data.
450  * @param   size Length of the data, in bytes, to send.
451  * @return  The number of bytes sent.  @c 0 will be returned if there is an
452  *          error.
453  * @ingroup Ecore_Con_Server_Group
454  */
455 EAPI int
456 ecore_con_server_send(Ecore_Con_Server *svr, const void *data, int size)
457 {
458    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
459      {
460         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_send");
461         return 0;
462      }
463    if (svr->dead) return 0;
464    if (!data) return 0;
465    if (size < 1) return 0;
466    if (svr->fd_handler)
467      ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
468    if (svr->write_buf)
469      {
470         unsigned char *newbuf;
471
472         newbuf = realloc(svr->write_buf, svr->write_buf_size + size);
473         if (newbuf) svr->write_buf = newbuf;
474         else return 0;
475         memcpy(svr->write_buf + svr->write_buf_size, data, size);
476         svr->write_buf_size += size;
477      }
478    else
479      {
480         svr->write_buf = malloc(size);
481         if (!svr->write_buf) return 0;
482         svr->write_buf_size = size;
483         memcpy(svr->write_buf, data, size);
484      }
485    return size;
486 }
487
488 /**
489  * Sets a limit on the number of clients that can be handled concurrently
490  * by the given server, and a policy on what to do if excess clients try to
491  * connect.
492  * Beware that if you set this once ecore is already running, you may
493  * already have pending CLIENT_ADD events in your event queue.  Those
494  * clients have already connected and will not be affected by this call.
495  * Only clients subsequently trying to connect will be affected.
496  * @param   svr           The given server.
497  * @param   client_limit  The maximum number of clients to handle
498  *                        concurrently.  -1 means unlimited (default).  0
499  *                        effectively disables the server.
500  * @param   reject_excess_clients  Set to 1 to automatically disconnect
501  *                        excess clients as soon as they connect if you are
502  *                        already handling client_limit clients.  Set to 0
503  *                        (default) to just hold off on the "accept()"
504  *                        system call until the number of active clients
505  *                        drops. This causes the kernel to queue up to 4096
506  *                        connections (or your kernel's limit, whichever is
507  *                        lower).
508  * @ingroup Ecore_Con_Server_Group
509  */
510 EAPI void
511 ecore_con_server_client_limit_set(Ecore_Con_Server *svr, int client_limit, char reject_excess_clients)
512 {
513    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
514      {
515         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_client_limit_set");
516         return;
517      }
518    svr->client_limit = client_limit;
519    svr->reject_excess_clients = reject_excess_clients;
520 }
521
522 /**
523  * Gets the IP address of a server that has been connected to.
524  *
525  * @param   svr           The given server.
526  * @return  A pointer to an internal string that contains the IP address of
527  *          the connected server in the form "XXX.YYY.ZZZ.AAA" IP notation.
528  *          This string should not be modified or trusted to stay valid after
529  *          deletion for the @p svr object. If no IP is known NULL is returned.
530  * @ingroup Ecore_Con_Server_Group
531  */
532 EAPI char *
533 ecore_con_server_ip_get(Ecore_Con_Server *svr)
534 {
535    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
536      {
537         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_ip_get");
538         return NULL;
539      }
540    return svr->ip;
541 }
542
543 /**
544  * Flushes all pending data to the given server. Will return when done.
545  *
546  * @param   svr           The given server.
547  * @ingroup Ecore_Con_Server_Group
548  */
549 EAPI void
550 ecore_con_server_flush(Ecore_Con_Server *svr)
551 {
552    if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
553      {
554         ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_flush");
555         return;
556      }
557    _ecore_con_server_flush(svr);
558 }
559
560 /**
561  * @defgroup Ecore_Con_Client_Group Ecore Connection Client Functions
562  *
563  * Functions that operate on Ecore connection client objects.
564  */
565
566 /**
567  * Sends the given data to the given client.
568  * @param   cl   The given client.
569  * @param   data The given data.
570  * @param   size Length of the data, in bytes, to send.
571  * @return  The number of bytes sent.  @c 0 will be returned if there is an
572  *          error.
573  * @ingroup Ecore_Con_Client_Group
574  */
575 EAPI int
576 ecore_con_client_send(Ecore_Con_Client *cl, const void *data, int size)
577 {
578    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
579      {
580         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_send");
581         return 0;
582      }
583    if (cl->dead) return 0;
584    if (!data) return 0;
585    if (size < 1) return 0;
586    if (cl->fd_handler)
587      ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ | ECORE_FD_WRITE);
588
589    if(cl->server && cl->server->type == ECORE_CON_REMOTE_UDP)
590      {
591        sendto(cl->server->fd, data, size, 0, (struct sockaddr *) cl->client_addr, cl->client_addr_len);
592      }
593    else if (cl->buf)
594      {
595        unsigned char *newbuf;
596
597        newbuf = realloc(cl->buf, cl->buf_size + size);
598        if (newbuf) cl->buf = newbuf;
599        else return 0;
600        memcpy(cl->buf + cl->buf_size, data, size);
601        cl->buf_size += size;
602      }
603    else
604      {
605         cl->buf = malloc(size);
606         if (!cl->buf) return 0;
607         cl->buf_size = size;
608         memcpy(cl->buf, data, size);
609      }
610    return size;
611 }
612
613 /**
614  * Retrieves the server representing the socket the client has
615  * connected to.
616  * @param   cl The given client.
617  * @return  The server that the client connected to.
618  * @ingroup Ecore_Con_Client_Group
619  */
620 EAPI Ecore_Con_Server *
621 ecore_con_client_server_get(Ecore_Con_Client *cl)
622 {
623    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
624      {
625         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_server_get");
626         return NULL;
627      }
628    return cl->server;
629 }
630
631 /**
632  * Closes the connection and frees memory allocated to the given client.
633  * @param   cl The given client.
634  * @return  Data associated with the client.
635  * @ingroup Ecore_Con_Client_Group
636  */
637 EAPI void *
638 ecore_con_client_del(Ecore_Con_Client *cl)
639 {
640    void *data = NULL;
641
642    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
643      {
644         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_del");
645         return NULL;
646      }
647
648    if(cl->client_addr && cl->server && (cl->server->type == ECORE_CON_REMOTE_UDP ||
649                                  cl->server->type == ECORE_CON_REMOTE_MCAST))
650      free(cl->client_addr);
651
652    data = cl->data;
653
654    cl->data = NULL;
655    cl->delete_me = 1;
656    if (cl->event_count > 0)
657      {
658         if (cl->fd_handler)
659           {
660              ecore_main_fd_handler_del(cl->fd_handler);
661              cl->fd_handler = NULL;
662           }
663      }
664    else
665      {
666         if (cl->server)
667           cl->server->clients = eina_list_remove(cl->server->clients, cl);
668         _ecore_con_client_free(cl);
669      }
670    return data;
671 }
672
673 /**
674  * Sets the data associated with the given client to @p data.
675  * @param   cl   The given client.
676  * @param   data What to set the data to.
677  * @ingroup Ecore_Con_Client_Group
678  */
679 EAPI void
680 ecore_con_client_data_set(Ecore_Con_Client *cl, const void *data)
681 {
682    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
683      {
684         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_data_set");
685         return;
686      }
687    cl->data = (void *)data;
688 }
689
690 /**
691  * Retrieves the data associated with the given client.
692  * @param   cl The given client.
693  * @return  The data associated with @p cl.
694  * @ingroup Ecore_Con_Client_Group
695  */
696 EAPI void *
697 ecore_con_client_data_get(Ecore_Con_Client *cl)
698 {
699    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
700      {
701         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_data_get");
702         return NULL;
703      }
704    return cl->data;
705 }
706
707 /**
708  * Gets the IP address of a cleint that has connected.
709  *
710  * @param   cl            The given client.
711  * @return  A pointer to an internal string that contains the IP address of
712  *          the connected client in the form "XXX.YYY.ZZZ.AAA" IP notation.
713  *          This string should not be modified or trusted to stay valid after
714  *          deletion for the @p cl object. If no IP is known NULL is returned.
715  * @ingroup Ecore_Con_Client_Group
716  */
717 EAPI char *
718 ecore_con_client_ip_get(Ecore_Con_Client *cl)
719 {
720    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
721      {
722         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_ip_get");
723         return NULL;
724      }
725    return cl->ip;
726 }
727
728 /**
729  * Flushes all pending data to the given client. Will return when done.
730  *
731  * @param   cl            The given client.
732  * @ingroup Ecore_Con_Client_Group
733  */
734 EAPI void
735 ecore_con_client_flush(Ecore_Con_Client *cl)
736 {
737    if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
738      {
739         ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_flush");
740         return;
741      }
742    _ecore_con_client_flush(cl);
743 }
744
745 static void
746 _ecore_con_server_free(Ecore_Con_Server *svr)
747 {
748    Ecore_Con_Client *cl;
749    double t_start, t;
750
751    ECORE_MAGIC_SET(svr, ECORE_MAGIC_NONE);
752    t_start = ecore_time_get();
753    while ((svr->write_buf) && (!svr->dead))
754      {
755         _ecore_con_server_flush(svr);
756         t = ecore_time_get();
757         if ((t - t_start) > 0.5)
758           {
759             WRN("ECORE_CON: EEK - stuck in _ecore_con_server_free() trying\n"
760                  "  to flush data out from the server, and have been for\n"
761                  "  %1.1f seconds. This is taking too long. Aborting flush.",
762                  (t - t_start));
763              break;
764           }
765      }
766    if (svr->write_buf) free(svr->write_buf);
767    EINA_LIST_FREE(svr->clients, cl)
768        _ecore_con_client_free(cl);
769    if ((svr->created) && (svr->path) && (svr->ppid == getpid()))
770      unlink(svr->path);
771    if (svr->fd >= 0) close(svr->fd);
772    ecore_con_ssl_server_shutdown(svr);
773    if (svr->name) free(svr->name);
774    if (svr->path) free(svr->path);
775    if (svr->ip) free(svr->ip);
776    if (svr->fd_handler) ecore_main_fd_handler_del(svr->fd_handler);
777    servers = eina_list_remove(servers, svr);
778    free(svr);
779 }
780
781 static void
782 _ecore_con_client_free(Ecore_Con_Client *cl)
783 {
784    double t_start, t;
785
786    ECORE_MAGIC_SET(cl, ECORE_MAGIC_NONE);
787    t_start = ecore_time_get();
788    while ((cl->buf) && (!cl->dead))
789      {
790         _ecore_con_client_flush(cl);
791         t = ecore_time_get();
792         if ((t - t_start) > 0.5)
793           {
794              WRN("EEK - stuck in _ecore_con_client_free() trying\n"
795                  "  to flush data out from the client, and have been for\n"
796                  "  %1.1f seconds. This is taking too long. Aborting flush.",
797                  (t - t_start));
798              break;
799           }
800      }
801    if (cl->buf) free(cl->buf);
802    if (cl->fd >= 0) close(cl->fd);
803    if (cl->fd_handler) ecore_main_fd_handler_del(cl->fd_handler);
804    if (cl->ip) free(cl->ip);
805    free(cl);
806 }
807
808 static void
809 kill_server(Ecore_Con_Server *svr)
810 {
811    if (!svr->delete_me)
812      {
813         Ecore_Con_Event_Server_Del *e;
814
815         e = calloc(1, sizeof(Ecore_Con_Event_Server_Del));
816         if (e)
817           {
818              svr->event_count++;
819              e->server = svr;
820              ecore_event_add(ECORE_CON_EVENT_SERVER_DEL, e,
821                              _ecore_con_event_server_del_free, NULL);
822           }
823      }
824
825    svr->dead = 1;
826    if (svr->fd_handler) ecore_main_fd_handler_del(svr->fd_handler);
827    svr->fd_handler = NULL;
828 }
829
830 static void
831 _ecore_con_cb_tcp_listen(void *data, Ecore_Con_Info *net_info)
832 {
833    Ecore_Con_Server *svr;
834    struct linger lin;
835
836    svr = data;
837
838    if(!net_info) goto error;
839
840    svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype, net_info->info.ai_protocol);
841    if (svr->fd < 0) goto error;
842    if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
843    if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
844    lin.l_onoff = 1;
845    lin.l_linger = 0;
846    if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, (const void *)&lin, sizeof(struct linger)) < 0) goto error;
847    if (svr->type == ECORE_CON_REMOTE_NODELAY)
848      {
849         int flag = 1;
850
851         if (setsockopt(svr->fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) < 0)
852           goto error;
853      }
854    if (bind(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0) goto error;
855    if (listen(svr->fd, 4096) < 0) goto error;
856    svr->fd_handler =
857      ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
858                                _ecore_con_svr_handler, svr, NULL, NULL);
859    if (!svr->fd_handler) goto error;
860
861    return;
862
863    error:
864    ecore_con_ssl_server_shutdown(svr);
865    kill_server(svr);
866 }
867
868 static void
869 _ecore_con_cb_udp_listen(void *data, Ecore_Con_Info *net_info)
870 {
871    Ecore_Con_Server *svr;
872    Ecore_Con_Type type;
873    struct ip_mreq mreq;
874    struct ipv6_mreq mreq6;
875    const int on = 1;
876
877    svr = data;
878    type = svr->type;
879    type &= ECORE_CON_TYPE;
880
881    if (!net_info) goto error;
882
883    svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype, net_info->info.ai_protocol);
884    if(svr->fd < 0) goto error;
885
886    if (type == ECORE_CON_REMOTE_MCAST)
887      {
888        if (net_info->info.ai_family == AF_INET)
889          {
890            if (!inet_pton(net_info->info.ai_family, net_info->ip, &mreq.imr_multiaddr)) goto error;
891            mreq.imr_interface.s_addr = htonl(INADDR_ANY);
892            if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq,sizeof(mreq)) != 0) goto error;
893          }
894        else if (net_info->info.ai_family == AF_INET6)
895          {
896            if (!inet_pton(net_info->info.ai_family, net_info->ip, &mreq6.ipv6mr_multiaddr)) goto error;
897            mreq6.ipv6mr_interface = htonl(INADDR_ANY);
898            if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&mreq6,sizeof(mreq6)) != 0) goto error;
899          }
900        if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on, sizeof(on)) != 0) goto error;
901      }
902
903    if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
904    if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
905    if (bind(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0) goto error;
906    svr->fd_handler =
907      ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
908                                _ecore_con_svr_udp_handler, svr, NULL, NULL);
909    if (!svr->fd_handler) goto error;
910    svr->ip = strdup(net_info->ip);
911
912    return;
913
914    error:
915    ecore_con_ssl_server_shutdown(svr);
916    kill_server(svr);
917 }
918
919 static void
920 _ecore_con_cb_tcp_connect(void *data, Ecore_Con_Info *net_info)
921 {
922    Ecore_Con_Server   *svr;
923    int                 res;
924    int                 curstate = 0;
925
926    svr = data;
927
928    if (!net_info) goto error;
929    svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype, net_info->info.ai_protocol);
930    if (svr->fd < 0) goto error;
931    if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
932    if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
933    if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&curstate, sizeof(curstate)) < 0)
934      goto error;
935    if (svr->type == ECORE_CON_REMOTE_NODELAY)
936      {
937         int flag = 1;
938
939         if (setsockopt(svr->fd, IPPROTO_TCP, TCP_NODELAY, (char *) &flag, sizeof(int)) < 0)
940           goto error;
941      }
942    res = connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen);
943 #ifdef _WIN32
944    if (res == SOCKET_ERROR)
945      {
946        if (WSAGetLastError() != WSAEINPROGRESS)
947          goto error;
948 #else
949    if (res < 0)
950      {
951        if (errno != EINPROGRESS)
952          goto error;
953 #endif
954        svr->connecting = 1;
955        svr->fd_handler =
956          ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
957                                    _ecore_con_cl_handler, svr, NULL, NULL);
958      }
959    else
960      svr->fd_handler =
961        ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
962                                  _ecore_con_cl_handler, svr, NULL, NULL);
963
964    if (svr->type & ECORE_CON_SSL)
965      if (ecore_con_ssl_server_init(svr))
966        goto error;
967
968    if (!svr->fd_handler) goto error;
969    svr->ip = strdup(net_info->ip);
970
971    return;
972
973    error:
974    ecore_con_ssl_server_shutdown(svr);
975    kill_server(svr);
976 }
977
978 static void
979 _ecore_con_cb_udp_connect(void *data, Ecore_Con_Info *net_info)
980 {
981    Ecore_Con_Server   *svr;
982    int                 curstate = 0;
983    int                 broadcast = 1;
984    svr = data;
985
986    if (!net_info) goto error;
987    svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype, net_info->info.ai_protocol);
988    if (svr->fd < 0) goto error;
989    if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
990    if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
991
992    if(svr->type == ECORE_CON_REMOTE_BROADCAST)
993      {
994        if (setsockopt(svr->fd, SOL_SOCKET, SO_BROADCAST, (const void *)&broadcast, sizeof(broadcast)) < 0) goto error;
995      }
996    else
997      {
998        if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&curstate, sizeof(curstate)) < 0) goto error;
999      }
1000
1001    if (connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0)
1002      goto error;
1003    else
1004      svr->fd_handler =
1005        ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
1006                                  _ecore_con_cl_udp_handler, svr, NULL, NULL);
1007    if (!svr->fd_handler) goto error;
1008    svr->ip = strdup(net_info->ip);
1009
1010    return;
1011
1012    error:
1013    ecore_con_ssl_server_shutdown(svr);
1014    kill_server(svr);
1015 }
1016
1017 static Ecore_Con_State
1018 svr_try_connect_plain(Ecore_Con_Server *svr)
1019 {
1020    int          res;
1021    int          so_err = 0;
1022    unsigned int size = sizeof(int);
1023
1024    res = getsockopt(svr->fd, SOL_SOCKET, SO_ERROR, (void *)&so_err, &size);
1025 #ifdef _WIN32
1026    if (res == SOCKET_ERROR)
1027      so_err = -1;
1028
1029    if (so_err == WSAEINPROGRESS && !svr->dead)
1030      return ECORE_CON_INPROGRESS;
1031 #else
1032    if (res < 0)
1033      so_err = -1;
1034
1035    if (so_err == EINPROGRESS && !svr->dead)
1036      return ECORE_CON_INPROGRESS;
1037 #endif
1038
1039    if (so_err != 0)
1040      {
1041         /* we lost our server! */
1042         kill_server(svr);
1043         return ECORE_CON_DISCONNECTED;
1044      }
1045    else
1046      {
1047         if (!svr->delete_me)
1048           {
1049              /* we got our server! */
1050              Ecore_Con_Event_Server_Add *e;
1051
1052              svr->connecting = 0;
1053              e = calloc(1, sizeof(Ecore_Con_Event_Server_Add));
1054              if (e)
1055                {
1056                   svr->event_count++;
1057                   e->server = svr;
1058                   ecore_event_add(ECORE_CON_EVENT_SERVER_ADD, e,
1059                                   _ecore_con_event_server_add_free, NULL);
1060                }
1061           }
1062         if (svr->fd_handler)
1063           {
1064              if (!svr->write_buf)
1065                ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
1066           }
1067      }
1068
1069    if (!svr->dead)
1070      return ECORE_CON_CONNECTED;
1071    else
1072      return ECORE_CON_DISCONNECTED;
1073 }
1074
1075 /* returns 1 on success, 0 on failure */
1076 static Ecore_Con_State svr_try_connect(Ecore_Con_Server *svr)
1077 {
1078   if (!(svr->type & ECORE_CON_SSL))
1079     return svr_try_connect_plain(svr);
1080   else
1081     {
1082       switch (ecore_con_ssl_server_try(svr)) {
1083       case ECORE_CON_CONNECTED:
1084         return svr_try_connect_plain(svr);
1085       case ECORE_CON_DISCONNECTED:
1086         kill_server(svr);
1087         return ECORE_CON_DISCONNECTED;
1088       default:
1089         return ECORE_CON_INPROGRESS;
1090       }
1091     }
1092 }
1093
1094 static char *
1095 _ecore_con_pretty_ip(struct sockaddr *client_addr, socklen_t size)
1096 {
1097    char ipbuf[INET6_ADDRSTRLEN + 1];
1098
1099    /* show v4mapped address in pretty form */
1100    if (client_addr->sa_family == AF_INET6)
1101      {
1102         struct sockaddr_in6 *sa6;
1103
1104         sa6 = (struct sockaddr_in6 *) client_addr;
1105         if (IN6_IS_ADDR_V4MAPPED(&sa6->sin6_addr))
1106           {
1107              snprintf(ipbuf, sizeof (ipbuf), "%u.%u.%u.%u",
1108                       sa6->sin6_addr.s6_addr[12],
1109                       sa6->sin6_addr.s6_addr[13],
1110                       sa6->sin6_addr.s6_addr[14],
1111                       sa6->sin6_addr.s6_addr[15]);
1112              return strdup(ipbuf);
1113           }
1114      }
1115
1116    if (getnameinfo(client_addr, size,
1117                    ipbuf, sizeof (ipbuf), NULL, 0, NI_NUMERICHOST))
1118      return strdup("0.0.0.0");
1119
1120    ipbuf[sizeof (ipbuf) - 1] = 0;
1121    return strdup(ipbuf);
1122 }
1123
1124 static int
1125 _ecore_con_svr_handler(void *data, Ecore_Fd_Handler *fd_handler __UNUSED__)
1126 {
1127    Ecore_Con_Server   *svr;
1128    int                 new_fd;
1129    unsigned char       incoming[256];
1130    size_t              size_in;
1131
1132    svr = data;
1133    if (svr->dead) return 1;
1134    if (svr->delete_me) return 1;
1135    if ((svr->client_limit >= 0) && (!svr->reject_excess_clients))
1136      {
1137         if (eina_list_count(svr->clients) >= (unsigned int)svr->client_limit)
1138           return 1;
1139      }
1140    /* a new client */
1141    size_in = sizeof(incoming);
1142
1143    memset(&incoming, 0, size_in);
1144    new_fd = accept(svr->fd, (struct sockaddr *)&incoming, (socklen_t *)&size_in);
1145    if (new_fd >= 0)
1146      {
1147         Ecore_Con_Client *cl;
1148
1149         if ((svr->client_limit >= 0) && (svr->reject_excess_clients))
1150           {
1151             if (eina_list_count(svr->clients) >= (unsigned int)svr->client_limit)
1152               {
1153                 close(new_fd);
1154                 return 1;
1155               }
1156           }
1157
1158         cl = calloc(1, sizeof(Ecore_Con_Client));
1159         if (!cl)
1160           {
1161              close(new_fd);
1162              return 1;
1163           }
1164
1165         fcntl(new_fd, F_SETFL, O_NONBLOCK);
1166         fcntl(new_fd, F_SETFD, FD_CLOEXEC);
1167         cl->fd = new_fd;
1168         cl->server = svr;
1169
1170         if ((svr->type & ECORE_CON_SSL) &&
1171             (ecore_con_ssl_client_init(cl)))
1172           {
1173             close(new_fd);
1174             ecore_con_ssl_client_shutdown(cl);
1175             return 1;
1176           }
1177
1178         cl->fd_handler =
1179           ecore_main_fd_handler_add(cl->fd, ECORE_FD_READ,
1180                                     _ecore_con_svr_cl_handler, cl, NULL, NULL);
1181         ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
1182         svr->clients = eina_list_append(svr->clients, cl);
1183         if (!svr->path)
1184           cl->ip = _ecore_con_pretty_ip((struct sockaddr *) &incoming, size_in);
1185         if (!cl->delete_me)
1186           {
1187              Ecore_Con_Event_Client_Add *e;
1188
1189              e = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
1190              if (e)
1191                {
1192                   cl->event_count++;
1193                   e->client = cl;
1194                   ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD, e,
1195                                   _ecore_con_event_client_add_free, NULL);
1196                }
1197           }
1198      }
1199    return 1;
1200 }
1201
1202 static int
1203 _ecore_con_cl_handler(void *data, Ecore_Fd_Handler *fd_handler)
1204 {
1205    Ecore_Con_Server   *svr;
1206
1207    svr = data;
1208    if (svr->dead) return 1;
1209    if (svr->delete_me) return 1;
1210    if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
1211      {
1212         unsigned char *inbuf = NULL;
1213         int            inbuf_num = 0;
1214
1215         if (svr->connecting && (svr_try_connect(svr) != ECORE_CON_CONNECTED))
1216            return 1;
1217
1218         for (;;)
1219           {
1220             int num;
1221             int lost_server = 1;
1222             unsigned char buf[READBUFSIZ];
1223
1224             if (!(svr->type & ECORE_CON_SSL))
1225               {
1226                 if (((num = read(svr->fd, buf, READBUFSIZ)) < 0) &&
1227                     (errno == EAGAIN))
1228                   lost_server = 0;
1229               }
1230             else
1231               if (!(num = ecore_con_ssl_server_read(svr, buf, READBUFSIZ)))
1232                   lost_server = 0;
1233
1234             if (num < 1)
1235               {
1236                 if (inbuf && !svr->delete_me)
1237                   {
1238                     Ecore_Con_Event_Server_Data *e;
1239
1240                     e = calloc(1, sizeof(Ecore_Con_Event_Server_Data));
1241                     if (e)
1242                       {
1243                         svr->event_count++;
1244                         e->server = svr;
1245                         e->data = inbuf;
1246                         e->size = inbuf_num;
1247                         ecore_event_add(ECORE_CON_EVENT_SERVER_DATA, e,
1248                                         _ecore_con_event_server_data_free,
1249                                         NULL);
1250                       }
1251                   }
1252                 if (lost_server) kill_server(svr);
1253                 break;
1254               }
1255
1256             inbuf = realloc(inbuf, inbuf_num + num);
1257             memcpy(inbuf + inbuf_num, buf, num);
1258             inbuf_num += num;
1259           }
1260
1261 /* #if USE_OPENSSL */
1262 /*      if (svr->fd_handler) */
1263 /*        { */
1264 /*           if (svr->ssl && ssl_err == SSL_ERROR_WANT_READ) */
1265 /*             ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ); */
1266 /*           else if (svr->ssl && ssl_err == SSL_ERROR_WANT_WRITE) */
1267 /*             ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_WRITE); */
1268 /*        } */
1269 /* #endif */
1270      }
1271    else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
1272      {
1273         if (svr->connecting && !svr_try_connect (svr))
1274            return 1;
1275         _ecore_con_server_flush(svr);
1276      }
1277
1278    return 1;
1279 }
1280
1281 static int
1282 _ecore_con_cl_udp_handler(void *data, Ecore_Fd_Handler *fd_handler)
1283 {
1284    Ecore_Con_Server   *svr;
1285
1286    svr = data;
1287    if (svr->dead) return 1;
1288    if (svr->delete_me) return 1;
1289    if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
1290      {
1291        unsigned char buf[65536];
1292        int           num = 0;
1293
1294        errno = 0;
1295        num = read(svr->fd, buf, 65536);
1296        if (num > 0)
1297          {
1298            if (!svr->delete_me)
1299              {
1300                Ecore_Con_Event_Server_Data *e;
1301                unsigned char *inbuf;
1302
1303                inbuf = malloc(num);
1304                if(inbuf == NULL)
1305                  return 1;
1306                memcpy(inbuf, buf, num);
1307
1308                e = calloc(1, sizeof(Ecore_Con_Event_Server_Data));
1309                if (e)
1310                  {
1311                    svr->event_count++;
1312                    e->server = svr;
1313                    e->data = inbuf;
1314                    e->size = num;
1315                    ecore_event_add(ECORE_CON_EVENT_SERVER_DATA, e,
1316                                    _ecore_con_event_server_data_free,
1317                                    NULL);
1318                  }
1319              }
1320          }
1321      }
1322    else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
1323        _ecore_con_server_flush(svr);
1324
1325    return 1;
1326 }
1327
1328 static int
1329 _ecore_con_svr_udp_handler(void *data, Ecore_Fd_Handler *fd_handler)
1330 {
1331    Ecore_Con_Server   *svr;
1332    Ecore_Con_Client *cl = NULL;
1333
1334    svr = data;
1335    if (svr->dead) return 1;
1336    if (svr->delete_me) return 1;
1337    if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
1338      {
1339        unsigned char buf[READBUFSIZ];
1340        unsigned char client_addr[256];
1341        unsigned int client_addr_len = sizeof(client_addr);
1342        int num;
1343
1344        errno = 0;
1345 #ifdef _WIN32
1346        num = fcntl(svr->fd, F_SETFL, O_NONBLOCK);
1347        if (num >= 0)
1348          num = recvfrom(svr->fd, buf, sizeof(buf), 0, (struct sockaddr*) &client_addr, &client_addr_len);
1349 #else
1350        num = recvfrom(svr->fd, buf, sizeof(buf), MSG_DONTWAIT, (struct sockaddr*) &client_addr, &client_addr_len);
1351 #endif
1352
1353        if (num > 0)
1354          {
1355            if (!svr->delete_me)
1356              {
1357                Ecore_Con_Event_Client_Data *e;
1358                unsigned char *inbuf;
1359
1360                /* Create a new client for use in the client data event */
1361                cl = calloc(1, sizeof(Ecore_Con_Client));
1362                if(cl == NULL)
1363                  return 1;
1364                cl->buf = NULL;
1365                cl->fd = 0;
1366                cl->fd_handler = NULL;
1367                cl->server = svr;
1368                cl->client_addr = calloc(1, client_addr_len);
1369                cl->client_addr_len = client_addr_len;
1370                if(cl->client_addr == NULL)
1371                  {
1372                    free(cl);
1373                    return 1;
1374                  }
1375                memcpy(cl->client_addr, &client_addr, client_addr_len);
1376                ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
1377                svr->clients = eina_list_append(svr->clients, cl);
1378
1379                cl->ip = _ecore_con_pretty_ip(cl->client_addr, cl->client_addr_len);
1380
1381                inbuf = malloc(num);
1382                if(inbuf == NULL)
1383                  {
1384                    free(cl->client_addr);
1385                    free(cl);
1386                    return 1;
1387                  }
1388
1389                memcpy(inbuf, buf, num);
1390
1391                e = calloc(1, sizeof(Ecore_Con_Event_Client_Data));
1392                if (e)
1393                  {
1394                    svr->event_count++;
1395                    e->client = cl;
1396                    e->data = inbuf;
1397                    e->size = num;
1398                    ecore_event_add(ECORE_CON_EVENT_CLIENT_DATA, e,
1399                                    _ecore_con_event_client_data_free,
1400                                    NULL);
1401                  }
1402
1403                if(!cl->delete_me)
1404                  {
1405                    Ecore_Con_Event_Client_Add *add;
1406
1407                    add = calloc(1, sizeof(Ecore_Con_Event_Client_Add));
1408                    if(add)
1409                      {
1410                        /*cl->event_count++;*/
1411                        add->client = cl;
1412                        ecore_event_add(ECORE_CON_EVENT_CLIENT_ADD, add,
1413                                        _ecore_con_event_client_add_free, NULL);
1414                      }
1415                  }
1416              }
1417            if ((errno == EIO) ||  (errno == EBADF) ||
1418                (errno == EPIPE) || (errno == EINVAL) ||
1419                (errno == ENOSPC) || (num == 0)/* is num == 0 right? */)
1420              {
1421                if (!svr->delete_me)
1422                  {
1423                    /* we lost our client! */
1424                    Ecore_Con_Event_Client_Del *e;
1425
1426                    e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
1427                    if (e)
1428                      {
1429                        svr->event_count++;
1430                        e->client = cl;
1431                        ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
1432                                        _ecore_con_event_client_del_free,
1433                                        NULL);
1434                      }
1435                  }
1436                svr->dead = 1;
1437                if (svr->fd_handler)
1438                  ecore_main_fd_handler_del(svr->fd_handler);
1439                svr->fd_handler = NULL;
1440              }
1441          }
1442      }
1443    else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
1444      _ecore_con_client_flush(cl);
1445    return 1;
1446 }
1447
1448 static int
1449 _ecore_con_svr_cl_handler(void *data, Ecore_Fd_Handler *fd_handler)
1450 {
1451    Ecore_Con_Client   *cl;
1452
1453    cl = data;
1454    if (cl->dead) return 1;
1455    if (cl->delete_me) return 1;
1456    if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
1457      {
1458         unsigned char *inbuf = NULL;
1459         int            inbuf_num = 0;
1460         int            lost_client = 1;
1461
1462         for (;;)
1463           {
1464              unsigned char buf[65536];
1465              int num;
1466
1467              errno = 0;
1468
1469              if (!(cl->server->type & ECORE_CON_SSL))
1470                {
1471                  if (((num = read(cl->fd, buf, 65536)) < 0) &&
1472                      (errno == EAGAIN))
1473                    lost_client = 0;
1474                }
1475              else
1476                if (!(num = ecore_con_ssl_client_read(cl, buf, 65536)))
1477                  lost_client = 0;
1478
1479              if (num < 1)
1480                {
1481                   if (inbuf && !cl->delete_me)
1482                     {
1483                       Ecore_Con_Event_Client_Data *e;
1484
1485                       e = calloc(1, sizeof(Ecore_Con_Event_Client_Data));
1486                       if (e)
1487                         {
1488                           cl->event_count++;
1489                           e->client = cl;
1490                           e->data = inbuf;
1491                           e->size = inbuf_num;
1492                           ecore_event_add(ECORE_CON_EVENT_CLIENT_DATA, e,
1493                                           _ecore_con_event_client_data_free,
1494                                           NULL);
1495                         }
1496                     }
1497
1498                   if (lost_client)
1499                     {
1500                        if (!cl->delete_me)
1501                          {
1502                             /* we lost our client! */
1503                             Ecore_Con_Event_Client_Del *e;
1504
1505                             e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
1506                             if (e)
1507                               {
1508                                  cl->event_count++;
1509                                  e->client = cl;
1510                                  ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
1511                                                  _ecore_con_event_client_del_free,
1512                                                  NULL);
1513                               }
1514                          }
1515                        cl->dead = 1;
1516                        if (cl->fd_handler)
1517                          ecore_main_fd_handler_del(cl->fd_handler);
1518                        cl->fd_handler = NULL;
1519                     }
1520                   break;
1521                }
1522              else
1523                {
1524                   inbuf = realloc(inbuf, inbuf_num + num);
1525                   memcpy(inbuf + inbuf_num, buf, num);
1526                   inbuf_num += num;
1527                }
1528           }
1529      }
1530    else if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
1531      _ecore_con_client_flush(cl);
1532    return 1;
1533 }
1534
1535 static void
1536 _ecore_con_server_flush(Ecore_Con_Server *svr)
1537 {
1538    int count, num;
1539
1540    if (!svr->write_buf) return;
1541
1542    /* check whether we need to write anything at all.
1543     * we must not write zero bytes with SSL_write() since it
1544     * causes undefined behaviour
1545     */
1546    if (svr->write_buf_size == svr->write_buf_offset)
1547       return;
1548
1549    num = svr->write_buf_size - svr->write_buf_offset;
1550
1551    if (!(svr->type & ECORE_CON_SSL))
1552      count = write(svr->fd, svr->write_buf + svr->write_buf_offset, num);
1553    else
1554      count = ecore_con_ssl_server_write(svr, svr->write_buf + svr->write_buf_offset, num);
1555
1556    if (count < 0)
1557      {
1558         /* we lost our server! */
1559         kill_server(svr);
1560         return;
1561      }
1562
1563    svr->write_buf_offset += count;
1564    if (svr->write_buf_offset >= svr->write_buf_size)
1565      {
1566         svr->write_buf_size = 0;
1567         svr->write_buf_offset = 0;
1568         free(svr->write_buf);
1569         svr->write_buf = NULL;
1570         if (svr->fd_handler)
1571           ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
1572      }
1573 }
1574
1575 static void
1576 _ecore_con_client_flush(Ecore_Con_Client *cl)
1577 {
1578    int count, num;
1579
1580    if (!cl->buf) return;
1581    num = cl->buf_size - cl->buf_offset;
1582    if (!(cl->server->type & ECORE_CON_SSL))
1583      count = write(cl->fd, cl->buf + cl->buf_offset, num);
1584    else
1585      count = ecore_con_ssl_client_write(cl, cl->buf + cl->buf_offset, num);
1586    if (count < 1)
1587      {
1588         if ((errno == EIO) || (errno == EBADF) || (errno == EPIPE) ||
1589             (errno == EINVAL) || (errno == ENOSPC))
1590           {
1591              if (!cl->delete_me)
1592                {
1593                   /* we lost our client! */
1594                   Ecore_Con_Event_Client_Del *e;
1595
1596                   e = calloc(1, sizeof(Ecore_Con_Event_Client_Del));
1597                   if (e)
1598                     {
1599                        cl->event_count++;
1600                        e->client = cl;
1601                        ecore_event_add(ECORE_CON_EVENT_CLIENT_DEL, e,
1602                                        _ecore_con_event_client_del_free, NULL);
1603                     }
1604                   cl->dead = 1;
1605                   if (cl->fd_handler)
1606                     ecore_main_fd_handler_del(cl->fd_handler);
1607                   cl->fd_handler = NULL;
1608                }
1609           }
1610         return;
1611      }
1612    cl->buf_offset += count;
1613    if (cl->buf_offset >= cl->buf_size)
1614      {
1615         cl->buf_size = 0;
1616         cl->buf_offset = 0;
1617         free(cl->buf);
1618         cl->buf = NULL;
1619         if (cl->fd_handler)
1620           ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ);
1621      }
1622 }
1623
1624 static void
1625 _ecore_con_event_client_add_free(void *data __UNUSED__, void *ev)
1626 {
1627    Ecore_Con_Event_Client_Add *e;
1628
1629    e = ev;
1630    e->client->event_count--;
1631    if ((e->client->event_count == 0) && (e->client->delete_me))
1632      ecore_con_client_del(e->client);
1633    free(e);
1634 }
1635
1636 static void
1637 _ecore_con_event_client_del_free(void *data __UNUSED__, void *ev)
1638 {
1639    Ecore_Con_Event_Client_Del *e;
1640
1641    e = ev;
1642    e->client->event_count--;
1643    if ((e->client->event_count == 0) && (e->client->delete_me))
1644      ecore_con_client_del(e->client);
1645    free(e);
1646 }
1647
1648 static void
1649 _ecore_con_event_client_data_free(void *data __UNUSED__, void *ev)
1650 {
1651    Ecore_Con_Event_Client_Data *e;
1652
1653    e = ev;
1654    e->client->event_count--;
1655    if (e->data) free(e->data);
1656    if (((e->client->event_count == 0) && (e->client->delete_me)) ||
1657        ((e->client->server && (e->client->server->type == ECORE_CON_REMOTE_UDP ||
1658                                e->client->server->type == ECORE_CON_REMOTE_MCAST))))
1659      ecore_con_client_del(e->client);
1660    free(e);
1661 }
1662
1663 static void
1664 _ecore_con_event_server_add_free(void *data __UNUSED__, void *ev)
1665 {
1666    Ecore_Con_Event_Server_Add *e;
1667
1668    e = ev;
1669    e->server->event_count--;
1670    if ((e->server->event_count == 0) && (e->server->delete_me))
1671      _ecore_con_server_free(e->server);
1672    free(e);
1673 }
1674
1675 static void
1676 _ecore_con_event_server_del_free(void *data __UNUSED__, void *ev)
1677 {
1678    Ecore_Con_Event_Server_Del *e;
1679
1680    e = ev;
1681    e->server->event_count--;
1682    if ((e->server->event_count == 0) && (e->server->delete_me))
1683      _ecore_con_server_free(e->server);
1684    free(e);
1685 }
1686
1687 static void
1688 _ecore_con_event_server_data_free(void *data __UNUSED__, void *ev)
1689 {
1690    Ecore_Con_Event_Server_Data *e;
1691
1692    e = ev;
1693    e->server->event_count--;
1694    if (e->data) free(e->data);
1695    if ((e->server->event_count == 0) && (e->server->delete_me))
1696      _ecore_con_server_free(e->server);
1697    free(e);
1698 }