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