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