aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ecore/src/lib/ecore_con/ecore_con.c
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/ecore/src/lib/ecore_con/ecore_con.c')
-rw-r--r--libraries/ecore/src/lib/ecore_con/ecore_con.c646
1 files changed, 313 insertions, 333 deletions
diff --git a/libraries/ecore/src/lib/ecore_con/ecore_con.c b/libraries/ecore/src/lib/ecore_con/ecore_con.c
index 7bd0358..de291b3 100644
--- a/libraries/ecore/src/lib/ecore_con/ecore_con.c
+++ b/libraries/ecore/src/lib/ecore_con/ecore_con.c
@@ -45,7 +45,6 @@
45 45
46static Eina_Bool _ecore_con_client_timer(Ecore_Con_Client *cl); 46static Eina_Bool _ecore_con_client_timer(Ecore_Con_Client *cl);
47static void _ecore_con_cl_timer_update(Ecore_Con_Client *cl); 47static void _ecore_con_cl_timer_update(Ecore_Con_Client *cl);
48
49static Eina_Bool _ecore_con_server_timer(Ecore_Con_Server *svr); 48static Eina_Bool _ecore_con_server_timer(Ecore_Con_Server *svr);
50static void _ecore_con_server_timer_update(Ecore_Con_Server *svr); 49static void _ecore_con_server_timer_update(Ecore_Con_Server *svr);
51 50
@@ -104,6 +103,45 @@ static void _ecore_con_lookup_done(void *data,
104 103
105static const char * _ecore_con_pretty_ip(struct sockaddr *client_addr); 104static const char * _ecore_con_pretty_ip(struct sockaddr *client_addr);
106 105
106
107void
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 ecore_con_event_client_del(cl);
114 INF("Lost client %s", (cl->ip) ? cl->ip : "");
115 if (cl->fd_handler)
116 ecore_main_fd_handler_del(cl->fd_handler);
117
118 cl->fd_handler = NULL;
119}
120
121void
122_ecore_con_server_kill(Ecore_Con_Server *svr)
123{
124 if (svr->delete_me)
125 DBG("Multi kill request for svr %p", svr);
126 else
127 ecore_con_event_server_del(svr);
128
129 if (svr->fd_handler)
130 ecore_main_fd_handler_del(svr->fd_handler);
131
132 svr->fd_handler = NULL;
133}
134
135#define _ecore_con_server_kill(svr) do { \
136 DBG("KILL %p", (svr)); \
137 _ecore_con_server_kill((svr)); \
138} while (0)
139
140#define _ecore_con_client_kill(cl) do { \
141 DBG("KILL %p", (cl)); \
142 _ecore_con_client_kill((cl)); \
143} while (0)
144
107EAPI int ECORE_CON_EVENT_CLIENT_ADD = 0; 145EAPI int ECORE_CON_EVENT_CLIENT_ADD = 0;
108EAPI int ECORE_CON_EVENT_CLIENT_DEL = 0; 146EAPI int ECORE_CON_EVENT_CLIENT_DEL = 0;
109EAPI int ECORE_CON_EVENT_SERVER_ADD = 0; 147EAPI int ECORE_CON_EVENT_SERVER_ADD = 0;
@@ -114,11 +152,14 @@ EAPI int ECORE_CON_EVENT_CLIENT_WRITE = 0;
114EAPI int ECORE_CON_EVENT_SERVER_WRITE = 0; 152EAPI int ECORE_CON_EVENT_SERVER_WRITE = 0;
115EAPI int ECORE_CON_EVENT_CLIENT_ERROR = 0; 153EAPI int ECORE_CON_EVENT_CLIENT_ERROR = 0;
116EAPI int ECORE_CON_EVENT_SERVER_ERROR = 0; 154EAPI int ECORE_CON_EVENT_SERVER_ERROR = 0;
155EAPI int ECORE_CON_EVENT_PROXY_BIND = 0;
117 156
118static Eina_List *servers = NULL; 157static Eina_List *servers = NULL;
119static int _ecore_con_init_count = 0; 158static int _ecore_con_init_count = 0;
120static int _ecore_con_event_count = 0; 159static int _ecore_con_event_count = 0;
121int _ecore_con_log_dom = -1; 160int _ecore_con_log_dom = -1;
161Ecore_Con_Socks *_ecore_con_proxy_once = NULL;
162Ecore_Con_Socks *_ecore_con_proxy_global = NULL;
122 163
123EAPI int 164EAPI int
124ecore_con_init(void) 165ecore_con_init(void)
@@ -156,6 +197,7 @@ ecore_con_init(void)
156 ECORE_CON_EVENT_SERVER_WRITE = ecore_event_type_new(); 197 ECORE_CON_EVENT_SERVER_WRITE = ecore_event_type_new();
157 ECORE_CON_EVENT_CLIENT_ERROR = ecore_event_type_new(); 198 ECORE_CON_EVENT_CLIENT_ERROR = ecore_event_type_new();
158 ECORE_CON_EVENT_SERVER_ERROR = ecore_event_type_new(); 199 ECORE_CON_EVENT_SERVER_ERROR = ecore_event_type_new();
200 ECORE_CON_EVENT_PROXY_BIND = ecore_event_type_new();
159 201
160 202
161 eina_magic_string_set(ECORE_MAGIC_CON_SERVER, "Ecore_Con_Server"); 203 eina_magic_string_set(ECORE_MAGIC_CON_SERVER, "Ecore_Con_Server");
@@ -163,6 +205,7 @@ ecore_con_init(void)
163 eina_magic_string_set(ECORE_MAGIC_CON_URL, "Ecore_Con_Url"); 205 eina_magic_string_set(ECORE_MAGIC_CON_URL, "Ecore_Con_Url");
164 206
165 /* TODO Remember return value, if it fails, use gethostbyname() */ 207 /* TODO Remember return value, if it fails, use gethostbyname() */
208 ecore_con_socks_init();
166 ecore_con_ssl_init(); 209 ecore_con_ssl_init();
167 ecore_con_info_init(); 210 ecore_con_info_init();
168 211
@@ -182,13 +225,15 @@ ecore_con_shutdown(void)
182 { 225 {
183 Ecore_Con_Event_Server_Add *ev; 226 Ecore_Con_Event_Server_Add *ev;
184 227
185 svr->delete_me = svr->dead = EINA_TRUE; 228 svr->delete_me = EINA_TRUE;
229 INF("svr %p is dead", svr);
186 /* some pointer hacks here to prevent double frees if people are being stupid */ 230 /* some pointer hacks here to prevent double frees if people are being stupid */
187 EINA_LIST_FREE(svr->event_count, ev) 231 EINA_LIST_FREE(svr->event_count, ev)
188 ev->server = NULL; 232 ev->server = NULL;
189 _ecore_con_server_free(svr); 233 _ecore_con_server_free(svr);
190 } 234 }
191 235
236 ecore_con_socks_shutdown();
192 if (!_ecore_con_event_count) ecore_con_mempool_shutdown(); 237 if (!_ecore_con_event_count) ecore_con_mempool_shutdown();
193 238
194 ecore_con_info_shutdown(); 239 ecore_con_info_shutdown();
@@ -301,8 +346,7 @@ ecore_con_server_add(Ecore_Con_Type compl_type,
301 svr->port = port; 346 svr->port = port;
302 svr->data = (void *)data; 347 svr->data = (void *)data;
303 svr->created = EINA_TRUE; 348 svr->created = EINA_TRUE;
304 if (compl_type & ECORE_CON_LOAD_CERT) 349 svr->use_cert = (compl_type & ECORE_CON_SSL & ECORE_CON_LOAD_CERT) == ECORE_CON_LOAD_CERT;
305 svr->use_cert = EINA_TRUE;
306 svr->reject_excess_clients = EINA_FALSE; 350 svr->reject_excess_clients = EINA_FALSE;
307 svr->client_limit = -1; 351 svr->client_limit = -1;
308 svr->clients = NULL; 352 svr->clients = NULL;
@@ -325,7 +369,8 @@ ecore_con_server_add(Ecore_Con_Type compl_type,
325#endif 369#endif
326 370
327 if ((type == ECORE_CON_REMOTE_TCP) || 371 if ((type == ECORE_CON_REMOTE_TCP) ||
328 (type == ECORE_CON_REMOTE_NODELAY)) 372 (type == ECORE_CON_REMOTE_NODELAY) ||
373 (type == ECORE_CON_REMOTE_CORK))
329 { 374 {
330 /* TCP */ 375 /* TCP */
331 if (!ecore_con_info_tcp_listen(svr, _ecore_con_cb_tcp_listen, 376 if (!ecore_con_info_tcp_listen(svr, _ecore_con_cb_tcp_listen,
@@ -395,17 +440,36 @@ ecore_con_server_connect(Ecore_Con_Type compl_type,
395 svr->port = port; 440 svr->port = port;
396 svr->data = (void *)data; 441 svr->data = (void *)data;
397 svr->created = EINA_FALSE; 442 svr->created = EINA_FALSE;
398 svr->use_cert = (compl_type & ECORE_CON_LOAD_CERT); 443 svr->use_cert = (compl_type & ECORE_CON_SSL & ECORE_CON_LOAD_CERT) == ECORE_CON_LOAD_CERT;
399 svr->reject_excess_clients = EINA_FALSE; 444 svr->reject_excess_clients = EINA_FALSE;
400 svr->clients = NULL; 445 svr->clients = NULL;
401 svr->client_limit = -1; 446 svr->client_limit = -1;
402 if (ecore_con_ssl_server_prepare(svr, compl_type & ECORE_CON_SSL))
403 goto error;
404 447
405 type = compl_type & ECORE_CON_TYPE; 448 type = compl_type & ECORE_CON_TYPE;
406 449
450 if (type > ECORE_CON_LOCAL_ABSTRACT)
451 {
452 /* never use proxies on local connections */
453 if (_ecore_con_proxy_once)
454 svr->ecs = _ecore_con_proxy_once;
455 else if (_ecore_con_proxy_global)
456 svr->ecs = _ecore_con_proxy_global;
457 _ecore_con_proxy_once = NULL;
458 if (svr->ecs)
459 {
460 if ((!svr->ecs->lookup) &&
461 (!ecore_con_lookup(svr->name, (Ecore_Con_Dns_Cb)ecore_con_socks_dns_cb, svr)))
462 goto error;
463 if (svr->ecs->lookup)
464 svr->ecs_state = ECORE_CON_SOCKS_STATE_RESOLVED;
465 }
466 }
467 if (ecore_con_ssl_server_prepare(svr, compl_type & ECORE_CON_SSL))
468 goto error;
469
407 if (((type == ECORE_CON_REMOTE_TCP) || 470 if (((type == ECORE_CON_REMOTE_TCP) ||
408 (type == ECORE_CON_REMOTE_NODELAY) || 471 (type == ECORE_CON_REMOTE_NODELAY) ||
472 (type == ECORE_CON_REMOTE_CORK) ||
409 (type == ECORE_CON_REMOTE_UDP) || 473 (type == ECORE_CON_REMOTE_UDP) ||
410 (type == ECORE_CON_REMOTE_BROADCAST)) && 474 (type == ECORE_CON_REMOTE_BROADCAST)) &&
411 (port < 0)) 475 (port < 0))
@@ -424,7 +488,8 @@ ecore_con_server_connect(Ecore_Con_Type compl_type,
424#endif 488#endif
425 489
426 if ((type == ECORE_CON_REMOTE_TCP) || 490 if ((type == ECORE_CON_REMOTE_TCP) ||
427 (type == ECORE_CON_REMOTE_NODELAY)) 491 (type == ECORE_CON_REMOTE_NODELAY) ||
492 (type == ECORE_CON_REMOTE_CORK))
428 { 493 {
429 /* TCP */ 494 /* TCP */
430 if (!ecore_con_info_tcp_connect(svr, _ecore_con_cb_tcp_connect, 495 if (!ecore_con_info_tcp_connect(svr, _ecore_con_cb_tcp_connect,
@@ -492,8 +557,6 @@ ecore_con_server_timeout_get(Ecore_Con_Server *svr)
492EAPI void * 557EAPI void *
493ecore_con_server_del(Ecore_Con_Server *svr) 558ecore_con_server_del(Ecore_Con_Server *svr)
494{ 559{
495 void *data;
496
497 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER)) 560 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
498 { 561 {
499 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_del"); 562 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_del");
@@ -503,20 +566,8 @@ ecore_con_server_del(Ecore_Con_Server *svr)
503 if (svr->delete_me) 566 if (svr->delete_me)
504 return NULL; 567 return NULL;
505 568
506 data = svr->data; 569 _ecore_con_server_kill(svr);
507 svr->delete_me = EINA_TRUE; 570 return svr->data;
508 if (svr->event_count)
509 {
510 if (svr->fd_handler)
511 {
512 ecore_main_fd_handler_del(svr->fd_handler);
513 svr->fd_handler = NULL;
514 }
515 }
516 else
517 _ecore_con_server_free(svr);
518
519 return data;
520} 571}
521 572
522EAPI void * 573EAPI void *
@@ -524,9 +575,7 @@ ecore_con_server_data_get(Ecore_Con_Server *svr)
524{ 575{
525 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER)) 576 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
526 { 577 {
527 ECORE_MAGIC_FAIL(svr, 578 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_data_get");
528 ECORE_MAGIC_CON_SERVER,
529 "ecore_con_server_data_get");
530 return NULL; 579 return NULL;
531 } 580 }
532 581
@@ -541,9 +590,7 @@ ecore_con_server_data_set(Ecore_Con_Server *svr,
541 590
542 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER)) 591 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
543 { 592 {
544 ECORE_MAGIC_FAIL(svr, 593 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_data_get");
545 ECORE_MAGIC_CON_SERVER,
546 "ecore_con_server_data_get");
547 return NULL; 594 return NULL;
548 } 595 }
549 596
@@ -557,8 +604,7 @@ ecore_con_server_connected_get(Ecore_Con_Server *svr)
557{ 604{
558 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER)) 605 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_CON_SERVER))
559 { 606 {
560 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, 607 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_CON_SERVER, "ecore_con_server_connected_get");
561 "ecore_con_server_connected_get");
562 return EINA_FALSE; 608 return EINA_FALSE;
563 } 609 }
564 610
@@ -617,7 +663,7 @@ ecore_con_server_send(Ecore_Con_Server *svr,
617 return 0; 663 return 0;
618 } 664 }
619 665
620 EINA_SAFETY_ON_TRUE_RETURN_VAL(svr->dead, 0); 666 EINA_SAFETY_ON_TRUE_RETURN_VAL(svr->delete_me, 0);
621 667
622 EINA_SAFETY_ON_NULL_RETURN_VAL(data, 0); 668 EINA_SAFETY_ON_NULL_RETURN_VAL(data, 0);
623 669
@@ -630,6 +676,15 @@ ecore_con_server_send(Ecore_Con_Server *svr,
630 { 676 {
631 svr->buf = eina_binbuf_new(); 677 svr->buf = eina_binbuf_new();
632 EINA_SAFETY_ON_NULL_RETURN_VAL(svr->buf, 0); 678 EINA_SAFETY_ON_NULL_RETURN_VAL(svr->buf, 0);
679#ifdef TCP_CORK
680 if ((svr->fd >= 0) && ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_CORK))
681 {
682 int state = 1;
683 if (setsockopt(svr->fd, IPPROTO_TCP, TCP_CORK, (char *)&state, sizeof(int)) < 0)
684 /* realistically this isn't anything serious so we can just log and continue */
685 ERR("corking failed! %s", strerror(errno));
686 }
687#endif
633 } 688 }
634 eina_binbuf_append_length(svr->buf, data, size); 689 eina_binbuf_append_length(svr->buf, data, size);
635 690
@@ -716,7 +771,7 @@ ecore_con_client_send(Ecore_Con_Client *cl,
716 return 0; 771 return 0;
717 } 772 }
718 773
719 EINA_SAFETY_ON_TRUE_RETURN_VAL(cl->dead, 0); 774 EINA_SAFETY_ON_TRUE_RETURN_VAL(cl->delete_me, 0);
720 775
721 EINA_SAFETY_ON_NULL_RETURN_VAL(data, 0); 776 EINA_SAFETY_ON_NULL_RETURN_VAL(data, 0);
722 777
@@ -732,6 +787,15 @@ ecore_con_client_send(Ecore_Con_Client *cl,
732 { 787 {
733 cl->buf = eina_binbuf_new(); 788 cl->buf = eina_binbuf_new();
734 EINA_SAFETY_ON_NULL_RETURN_VAL(cl->buf, 0); 789 EINA_SAFETY_ON_NULL_RETURN_VAL(cl->buf, 0);
790#ifdef TCP_CORK
791 if ((cl->fd >= 0) && ((cl->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_CORK))
792 {
793 int state = 1;
794 if (setsockopt(cl->fd, IPPROTO_TCP, TCP_CORK, (char *)&state, sizeof(int)) < 0)
795 /* realistically this isn't anything serious so we can just log and continue */
796 ERR("corking failed! %s", strerror(errno));
797 }
798#endif
735 } 799 }
736 eina_binbuf_append_length(cl->buf, data, size); 800 eina_binbuf_append_length(cl->buf, data, size);
737 801
@@ -761,7 +825,7 @@ ecore_con_client_connected_get(Ecore_Con_Client *cl)
761 return EINA_FALSE; 825 return EINA_FALSE;
762 } 826 }
763 827
764 return !cl->dead; 828 return !cl->delete_me;
765} 829}
766 830
767EAPI void 831EAPI void
@@ -795,36 +859,14 @@ ecore_con_client_timeout_get(Ecore_Con_Client *cl)
795EAPI void * 859EAPI void *
796ecore_con_client_del(Ecore_Con_Client *cl) 860ecore_con_client_del(Ecore_Con_Client *cl)
797{ 861{
798 void *data = NULL;
799
800 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT)) 862 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
801 { 863 {
802 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_del"); 864 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_del");
803 return NULL; 865 return NULL;
804 } 866 }
805 867
806 data = cl->data; 868 _ecore_con_client_kill(cl);
807 cl->delete_me = EINA_TRUE; 869 return cl->data;
808 if (cl->event_count)
809 {
810 if (cl->fd_handler)
811 {
812 ecore_main_fd_handler_del(cl->fd_handler);
813 cl->fd_handler = NULL;
814 }
815 }
816 else
817 {
818 if (cl->host_server)
819 {
820 cl->host_server->clients = eina_list_remove(cl->host_server->clients, cl);
821 --cl->host_server->client_count;
822 }
823
824 _ecore_con_client_free(cl);
825 }
826
827 return data;
828} 870}
829 871
830EAPI void 872EAPI void
@@ -833,9 +875,7 @@ ecore_con_client_data_set(Ecore_Con_Client *cl,
833{ 875{
834 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT)) 876 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
835 { 877 {
836 ECORE_MAGIC_FAIL(cl, 878 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_data_set");
837 ECORE_MAGIC_CON_CLIENT,
838 "ecore_con_client_data_set");
839 return; 879 return;
840 } 880 }
841 881
@@ -847,9 +887,7 @@ ecore_con_client_data_get(Ecore_Con_Client *cl)
847{ 887{
848 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT)) 888 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_CON_CLIENT))
849 { 889 {
850 ECORE_MAGIC_FAIL(cl, 890 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_CON_CLIENT, "ecore_con_client_data_get");
851 ECORE_MAGIC_CON_CLIENT,
852 "ecore_con_client_data_get");
853 return NULL; 891 return NULL;
854 } 892 }
855 893
@@ -939,6 +977,25 @@ ecore_con_client_fd_get(Ecore_Con_Client *cl)
939 */ 977 */
940 978
941void 979void
980ecore_con_event_proxy_bind(Ecore_Con_Server *svr)
981{
982 Ecore_Con_Event_Proxy_Bind *e;
983 int ev = ECORE_CON_EVENT_PROXY_BIND;
984
985 e = ecore_con_event_proxy_bind_alloc();
986 EINA_SAFETY_ON_NULL_RETURN(e);
987
988 svr->event_count = eina_list_append(svr->event_count, e);
989 _ecore_con_server_timer_update(svr);
990 e->server = svr;
991 e->ip = svr->proxyip;
992 e->port = svr->proxyport;
993 ecore_event_add(ev, e,
994 _ecore_con_event_server_add_free, NULL);
995 _ecore_con_event_count++;
996}
997
998void
942ecore_con_event_server_add(Ecore_Con_Server *svr) 999ecore_con_event_server_add(Ecore_Con_Server *svr)
943{ 1000{
944 /* we got our server! */ 1001 /* we got our server! */
@@ -948,6 +1005,8 @@ ecore_con_event_server_add(Ecore_Con_Server *svr)
948 e = ecore_con_event_server_add_alloc(); 1005 e = ecore_con_event_server_add_alloc();
949 EINA_SAFETY_ON_NULL_RETURN(e); 1006 EINA_SAFETY_ON_NULL_RETURN(e);
950 1007
1008 svr->connecting = EINA_FALSE;
1009 svr->start_time = ecore_time_get();
951 svr->event_count = eina_list_append(svr->event_count, e); 1010 svr->event_count = eina_list_append(svr->event_count, e);
952 _ecore_con_server_timer_update(svr); 1011 _ecore_con_server_timer_update(svr);
953 e->server = svr; 1012 e->server = svr;
@@ -962,12 +1021,20 @@ ecore_con_event_server_del(Ecore_Con_Server *svr)
962{ 1021{
963 Ecore_Con_Event_Server_Del *e; 1022 Ecore_Con_Event_Server_Del *e;
964 1023
1024 svr->delete_me = EINA_TRUE;
1025 INF("svr %p is dead", svr);
965 e = ecore_con_event_server_del_alloc(); 1026 e = ecore_con_event_server_del_alloc();
966 EINA_SAFETY_ON_NULL_RETURN(e); 1027 EINA_SAFETY_ON_NULL_RETURN(e);
967 1028
968 svr->event_count = eina_list_append(svr->event_count, e); 1029 svr->event_count = eina_list_append(svr->event_count, e);
969 _ecore_con_server_timer_update(svr); 1030 _ecore_con_server_timer_update(svr);
970 e->server = svr; 1031 e->server = svr;
1032 if (svr->ecs)
1033 {
1034 svr->ecs_state = svr->ecs->lookup ? ECORE_CON_SOCKS_STATE_RESOLVED : ECORE_CON_SOCKS_STATE_DONE;
1035 eina_stringshare_replace(&svr->proxyip, NULL);
1036 svr->proxyport = 0;
1037 }
971 ecore_event_add(ECORE_CON_EVENT_SERVER_DEL, e, 1038 ecore_event_add(ECORE_CON_EVENT_SERVER_DEL, e,
972 _ecore_con_event_server_del_free, NULL); 1039 _ecore_con_event_server_del_free, NULL);
973 _ecore_con_event_count++; 1040 _ecore_con_event_count++;
@@ -981,6 +1048,7 @@ ecore_con_event_server_write(Ecore_Con_Server *svr, int num)
981 e = ecore_con_event_server_write_alloc(); 1048 e = ecore_con_event_server_write_alloc();
982 EINA_SAFETY_ON_NULL_RETURN(e); 1049 EINA_SAFETY_ON_NULL_RETURN(e);
983 1050
1051 INF("Wrote %d bytes", num);
984 svr->event_count = eina_list_append(svr->event_count, e); 1052 svr->event_count = eina_list_append(svr->event_count, e);
985 e->server = svr; 1053 e->server = svr;
986 e->size = num; 1054 e->size = num;
@@ -1044,6 +1112,8 @@ ecore_con_event_client_del(Ecore_Con_Client *cl)
1044 Ecore_Con_Event_Client_Del *e; 1112 Ecore_Con_Event_Client_Del *e;
1045 1113
1046 if (!cl) return; 1114 if (!cl) return;
1115 cl->delete_me = EINA_TRUE;
1116 INF("cl %p is dead", cl);
1047 e = ecore_con_event_client_del_alloc(); 1117 e = ecore_con_event_client_del_alloc();
1048 EINA_SAFETY_ON_NULL_RETURN(e); 1118 EINA_SAFETY_ON_NULL_RETURN(e);
1049 cl->event_count = eina_list_append(cl->event_count, e); 1119 cl->event_count = eina_list_append(cl->event_count, e);
@@ -1112,7 +1182,7 @@ ecore_con_server_infos_del(Ecore_Con_Server *svr, void *info)
1112} 1182}
1113 1183
1114void 1184void
1115ecore_con_event_server_error(Ecore_Con_Server *svr, const char *error) 1185_ecore_con_event_server_error(Ecore_Con_Server *svr, char *error, Eina_Bool duplicate)
1116{ 1186{
1117 Ecore_Con_Event_Server_Error *e; 1187 Ecore_Con_Event_Server_Error *e;
1118 1188
@@ -1120,7 +1190,7 @@ ecore_con_event_server_error(Ecore_Con_Server *svr, const char *error)
1120 EINA_SAFETY_ON_NULL_RETURN(e); 1190 EINA_SAFETY_ON_NULL_RETURN(e);
1121 1191
1122 e->server = svr; 1192 e->server = svr;
1123 e->error = strdup(error); 1193 e->error = duplicate ? strdup(error) : error;
1124 ERR("%s", error); 1194 ERR("%s", error);
1125 svr->event_count = eina_list_append(svr->event_count, e); 1195 svr->event_count = eina_list_append(svr->event_count, e);
1126 ecore_event_add(ECORE_CON_EVENT_SERVER_ERROR, e, (Ecore_End_Cb)_ecore_con_event_server_error_free, NULL); 1196 ecore_event_add(ECORE_CON_EVENT_SERVER_ERROR, e, (Ecore_End_Cb)_ecore_con_event_server_error_free, NULL);
@@ -1157,16 +1227,9 @@ _ecore_con_server_free(Ecore_Con_Server *svr)
1157 ecore_con_info_data_clear(svr->infos->data); 1227 ecore_con_info_data_clear(svr->infos->data);
1158 svr->infos = eina_list_remove_list(svr->infos, svr->infos); 1228 svr->infos = eina_list_remove_list(svr->infos, svr->infos);
1159 } 1229 }
1160 if ((!svr->buf) && svr->delete_me && (!svr->dead) && (!svr->event_count))
1161 {
1162 /* this is a catch-all for cases when a server is not properly killed. */
1163 svr->dead = EINA_TRUE;
1164 ecore_con_event_server_del(svr);
1165 return;
1166 }
1167 1230
1168 t_start = ecore_time_get(); 1231 t_start = ecore_time_get();
1169 while (svr->buf && (!svr->dead)) 1232 while (svr->buf && (!svr->delete_me))
1170 { 1233 {
1171 _ecore_con_server_flush(svr); 1234 _ecore_con_server_flush(svr);
1172 t = ecore_time_get(); 1235 t = ecore_time_get();
@@ -1196,7 +1259,8 @@ _ecore_con_server_free(Ecore_Con_Server *svr)
1196 /* some pointer hacks here to prevent double frees if people are being stupid */ 1259 /* some pointer hacks here to prevent double frees if people are being stupid */
1197 EINA_LIST_FREE(cl->event_count, ev) 1260 EINA_LIST_FREE(cl->event_count, ev)
1198 ev->server = NULL; 1261 ev->server = NULL;
1199 cl->delete_me = cl->dead = EINA_TRUE; 1262 cl->delete_me = EINA_TRUE;
1263 INF("cl %p is dead", cl);
1200 _ecore_con_client_free(cl); 1264 _ecore_con_client_free(cl);
1201 } 1265 }
1202 if ((svr->created) && (svr->path) && (svr->ppid == getpid())) 1266 if ((svr->created) && (svr->path) && (svr->ppid == getpid()))
@@ -1208,6 +1272,10 @@ _ecore_con_server_free(Ecore_Con_Server *svr)
1208 free(svr->path); 1272 free(svr->path);
1209 1273
1210 eina_stringshare_del(svr->ip); 1274 eina_stringshare_del(svr->ip);
1275 eina_stringshare_del(svr->verify_name);
1276
1277 if (svr->ecs_buf) eina_binbuf_free(svr->ecs_buf);
1278 if (svr->ecs_recvbuf) eina_binbuf_free(svr->ecs_recvbuf);
1211 1279
1212 if (svr->fd_handler) 1280 if (svr->fd_handler)
1213 ecore_main_fd_handler_del(svr->fd_handler); 1281 ecore_main_fd_handler_del(svr->fd_handler);
@@ -1230,17 +1298,8 @@ _ecore_con_client_free(Ecore_Con_Client *cl)
1230 1298
1231 if (cl->event_count) return; 1299 if (cl->event_count) return;
1232 1300
1233 if (cl->delete_me && (!cl->dead) && (!cl->event_count))
1234 {
1235 /* this is a catch-all for cases when a client is not properly killed. */
1236 cl->dead = EINA_TRUE;
1237 ecore_con_event_client_del(cl);
1238 return;
1239 }
1240
1241
1242 t_start = ecore_time_get(); 1301 t_start = ecore_time_get();
1243 while ((cl->buf) && (!cl->dead)) 1302 while ((cl->buf) && (!cl->delete_me))
1244 { 1303 {
1245 _ecore_con_client_flush(cl); 1304 _ecore_con_client_flush(cl);
1246 t = ecore_time_get(); 1305 t = ecore_time_get();
@@ -1284,19 +1343,6 @@ _ecore_con_client_free(Ecore_Con_Client *cl)
1284 return; 1343 return;
1285} 1344}
1286 1345
1287static void
1288_ecore_con_server_kill(Ecore_Con_Server *svr)
1289{
1290 if (!svr->delete_me)
1291 ecore_con_event_server_del(svr);
1292
1293 svr->dead = EINA_TRUE;
1294 if (svr->fd_handler)
1295 ecore_main_fd_handler_del(svr->fd_handler);
1296
1297 svr->fd_handler = NULL;
1298}
1299
1300static Eina_Bool 1346static Eina_Bool
1301_ecore_con_server_timer(Ecore_Con_Server *svr) 1347_ecore_con_server_timer(Ecore_Con_Server *svr)
1302{ 1348{
@@ -1384,40 +1430,25 @@ _ecore_con_cb_tcp_listen(void *data,
1384{ 1430{
1385 Ecore_Con_Server *svr; 1431 Ecore_Con_Server *svr;
1386 struct linger lin; 1432 struct linger lin;
1433 const char *memerr = NULL;
1387 1434
1388 svr = data; 1435 svr = data;
1389 1436
1437 errno = 0;
1390 if (!net_info) /* error message has already been handled */ 1438 if (!net_info) /* error message has already been handled */
1391 goto error; 1439 goto error;
1392 1440
1393 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype, 1441 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1394 net_info->info.ai_protocol); 1442 net_info->info.ai_protocol);
1395 if (svr->fd < 0) 1443 if (svr->fd < 0) goto error;
1396 { 1444 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
1397 ecore_con_event_server_error(svr, strerror(errno)); 1445 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
1398 goto error;
1399 }
1400
1401 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1402 {
1403 ecore_con_event_server_error(svr, strerror(errno));
1404 goto error;
1405 }
1406
1407 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1408 {
1409 ecore_con_event_server_error(svr, strerror(errno));
1410 goto error;
1411 }
1412 1446
1413 lin.l_onoff = 1; 1447 lin.l_onoff = 1;
1414 lin.l_linger = 0; 1448 lin.l_linger = 0;
1415 if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, (const void *)&lin, 1449 if (setsockopt(svr->fd, SOL_SOCKET, SO_LINGER, (const void *)&lin,
1416 sizeof(struct linger)) < 0) 1450 sizeof(struct linger)) < 0)
1417 { 1451 goto error;
1418 ecore_con_event_server_error(svr, strerror(errno));
1419 goto error;
1420 }
1421 1452
1422 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_NODELAY) 1453 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_NODELAY)
1423 { 1454 {
@@ -1428,34 +1459,27 @@ _ecore_con_cb_tcp_listen(void *data,
1428 sizeof(int)) < 0) 1459 sizeof(int)) < 0)
1429#endif 1460#endif
1430 { 1461 {
1431 ecore_con_event_server_error(svr, strerror(errno));
1432 goto error; 1462 goto error;
1433 } 1463 }
1434 } 1464 }
1435 1465
1436 if (bind(svr->fd, net_info->info.ai_addr, 1466 if (bind(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0)
1437 net_info->info.ai_addrlen) < 0) 1467 goto error;
1438 { 1468
1439 ecore_con_event_server_error(svr, strerror(errno)); 1469 if (listen(svr->fd, 4096) < 0) goto error;
1440 goto error;
1441 }
1442 if (listen(svr->fd, 4096) < 0)
1443 {
1444 ecore_con_event_server_error(svr, strerror(errno));
1445 goto error;
1446 }
1447 1470
1448 svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ, 1471 svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1449 _ecore_con_svr_tcp_handler, svr, NULL, NULL); 1472 _ecore_con_svr_tcp_handler, svr, NULL, NULL);
1450 if (!svr->fd_handler) 1473 if (!svr->fd_handler)
1451 { 1474 {
1452 ecore_con_event_server_error(svr, "Memory allocation failure"); 1475 memerr = "Memory allocation failure";
1453 goto error; 1476 goto error;
1454 } 1477 }
1455 1478
1456 return; 1479 return;
1457 1480
1458error: 1481error:
1482 if (errno || memerr) ecore_con_event_server_error(svr, memerr ?: strerror(errno));
1459 ecore_con_ssl_server_shutdown(svr); 1483 ecore_con_ssl_server_shutdown(svr);
1460 _ecore_con_server_kill(svr); 1484 _ecore_con_server_kill(svr);
1461} 1485}
@@ -1471,21 +1495,19 @@ _ecore_con_cb_udp_listen(void *data,
1471 struct ipv6_mreq mreq6; 1495 struct ipv6_mreq mreq6;
1472#endif 1496#endif
1473 const int on = 1; 1497 const int on = 1;
1498 const char *memerr = NULL;
1474 1499
1475 svr = data; 1500 svr = data;
1476 type = svr->type; 1501 type = svr->type;
1477 type &= ECORE_CON_TYPE; 1502 type &= ECORE_CON_TYPE;
1478 1503
1504 errno = 0;
1479 if (!net_info) /* error message has already been handled */ 1505 if (!net_info) /* error message has already been handled */
1480 goto error; 1506 goto error;
1481 1507
1482 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype, 1508 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1483 net_info->info.ai_protocol); 1509 net_info->info.ai_protocol);
1484 if (svr->fd < 0) 1510 if (svr->fd < 0) goto error;
1485 {
1486 ecore_con_event_server_error(svr, strerror(errno));
1487 goto error;
1488 }
1489 1511
1490 if (type == ECORE_CON_REMOTE_MCAST) 1512 if (type == ECORE_CON_REMOTE_MCAST)
1491 { 1513 {
@@ -1493,69 +1515,41 @@ _ecore_con_cb_udp_listen(void *data,
1493 { 1515 {
1494 if (!inet_pton(net_info->info.ai_family, net_info->ip, 1516 if (!inet_pton(net_info->info.ai_family, net_info->ip,
1495 &mreq.imr_multiaddr)) 1517 &mreq.imr_multiaddr))
1496 { 1518 goto error;
1497 ecore_con_event_server_error(svr, strerror(errno));
1498 goto error;
1499 }
1500 1519
1501 mreq.imr_interface.s_addr = htonl(INADDR_ANY); 1520 mreq.imr_interface.s_addr = htonl(INADDR_ANY);
1502 if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, 1521 if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1503 (const void *)&mreq, sizeof(mreq)) != 0) 1522 (const void *)&mreq, sizeof(mreq)) != 0)
1504 { 1523 goto error;
1505 ecore_con_event_server_error(svr, strerror(errno));
1506 goto error;
1507 }
1508 } 1524 }
1509#ifdef HAVE_IPV6 1525#ifdef HAVE_IPV6
1510 else if (net_info->info.ai_family == AF_INET6) 1526 else if (net_info->info.ai_family == AF_INET6)
1511 { 1527 {
1512 if (!inet_pton(net_info->info.ai_family, net_info->ip, 1528 if (!inet_pton(net_info->info.ai_family, net_info->ip,
1513 &mreq6.ipv6mr_multiaddr)) 1529 &mreq6.ipv6mr_multiaddr))
1514 { 1530 goto error;
1515 ecore_con_event_server_error(svr, strerror(errno));
1516 goto error;
1517 }
1518 mreq6.ipv6mr_interface = htonl(INADDR_ANY); 1531 mreq6.ipv6mr_interface = htonl(INADDR_ANY);
1519 if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, 1532 if (setsockopt(svr->fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1520 (const void *)&mreq6, sizeof(mreq6)) != 0) 1533 (const void *)&mreq6, sizeof(mreq6)) != 0)
1521 { 1534 goto error;
1522 ecore_con_event_server_error(svr, strerror(errno));
1523 goto error;
1524 }
1525 } 1535 }
1526#endif 1536#endif
1527 } 1537 }
1528 1538
1529 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on, sizeof(on)) != 0) 1539 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&on, sizeof(on)) != 0)
1530 { 1540 goto error;
1531 ecore_con_event_server_error(svr, strerror(errno)); 1541 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
1532 goto error; 1542 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
1533 }
1534
1535 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1536 {
1537 ecore_con_event_server_error(svr, strerror(errno));
1538 goto error;
1539 }
1540
1541 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1542 {
1543 ecore_con_event_server_error(svr, strerror(errno));
1544 goto error;
1545 }
1546 1543
1547 if (bind(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0) 1544 if (bind(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0)
1548 { 1545 goto error;
1549 ecore_con_event_server_error(svr, strerror(errno));
1550 goto error;
1551 }
1552 1546
1553 svr->fd_handler = 1547 svr->fd_handler =
1554 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ, 1548 ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ,
1555 _ecore_con_svr_udp_handler, svr, NULL, NULL); 1549 _ecore_con_svr_udp_handler, svr, NULL, NULL);
1556 if (!svr->fd_handler) 1550 if (!svr->fd_handler)
1557 { 1551 {
1558 ecore_con_event_server_error(svr, "Memory allocation failure"); 1552 memerr = "Memory allocation failure";
1559 goto error; 1553 goto error;
1560 } 1554 }
1561 1555
@@ -1564,6 +1558,7 @@ _ecore_con_cb_udp_listen(void *data,
1564 return; 1558 return;
1565 1559
1566error: 1560error:
1561 if (errno || memerr) ecore_con_event_server_error(svr, memerr ?: strerror(errno));
1567 ecore_con_ssl_server_shutdown(svr); 1562 ecore_con_ssl_server_shutdown(svr);
1568 _ecore_con_server_kill(svr); 1563 _ecore_con_server_kill(svr);
1569} 1564}
@@ -1575,37 +1570,23 @@ _ecore_con_cb_tcp_connect(void *data,
1575 Ecore_Con_Server *svr; 1570 Ecore_Con_Server *svr;
1576 int res; 1571 int res;
1577 int curstate = 0; 1572 int curstate = 0;
1573 const char *memerr = NULL;
1578 1574
1579 svr = data; 1575 svr = data;
1580 1576
1577 errno = 0;
1581 if (!net_info) /* error message has already been handled */ 1578 if (!net_info) /* error message has already been handled */
1582 goto error; 1579 goto error;
1583 1580
1584 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype, 1581 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1585 net_info->info.ai_protocol); 1582 net_info->info.ai_protocol);
1586 if (svr->fd < 0) 1583 if (svr->fd < 0) goto error;
1587 {
1588 ecore_con_event_server_error(svr, strerror(errno));
1589 goto error;
1590 }
1591 1584
1592 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) 1585 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
1593 { 1586 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
1594 ecore_con_event_server_error(svr, strerror(errno));
1595 goto error;
1596 }
1597
1598 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1599 {
1600 ecore_con_event_server_error(svr, strerror(errno));
1601 goto error;
1602 }
1603 1587
1604 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&curstate, sizeof(curstate)) < 0) 1588 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&curstate, sizeof(curstate)) < 0)
1605 { 1589 goto error;
1606 ecore_con_event_server_error(svr, strerror(errno));
1607 goto error;
1608 }
1609 1590
1610 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_NODELAY) 1591 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_NODELAY)
1611 { 1592 {
@@ -1615,7 +1596,6 @@ _ecore_con_cb_tcp_connect(void *data,
1615 if (setsockopt(svr->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int)) < 0) 1596 if (setsockopt(svr->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int)) < 0)
1616#endif 1597#endif
1617 { 1598 {
1618 ecore_con_event_server_error(svr, strerror(errno));
1619 goto error; 1599 goto error;
1620 } 1600 }
1621 } 1601 }
@@ -1625,17 +1605,19 @@ _ecore_con_cb_tcp_connect(void *data,
1625 if (res == SOCKET_ERROR) 1605 if (res == SOCKET_ERROR)
1626 { 1606 {
1627 if (WSAGetLastError() != WSAEINPROGRESS) 1607 if (WSAGetLastError() != WSAEINPROGRESS)
1628 goto error; /* FIXME: strerror on windows? */ 1608 {
1609 char *err;
1610 err = evil_format_message(WSAGetLastError());
1611 _ecore_con_event_server_error(svr, err, EINA_FALSE);
1612 ecore_con_ssl_server_shutdown(svr);
1613 _ecore_con_server_kill(svr);
1614 return;
1615 }
1629 1616
1630#else 1617#else
1631 if (res < 0) 1618 if (res < 0)
1632 { 1619 {
1633 if (errno != EINPROGRESS) 1620 if (errno != EINPROGRESS) goto error;
1634 {
1635 ecore_con_event_server_error(svr, strerror(errno));
1636 goto error;
1637 }
1638
1639#endif 1621#endif
1640 svr->connecting = EINA_TRUE; 1622 svr->connecting = EINA_TRUE;
1641 svr->fd_handler = 1623 svr->fd_handler =
@@ -1650,22 +1632,24 @@ _ecore_con_cb_tcp_connect(void *data,
1650 { 1632 {
1651 svr->handshaking = EINA_TRUE; 1633 svr->handshaking = EINA_TRUE;
1652 svr->ssl_state = ECORE_CON_SSL_STATE_INIT; 1634 svr->ssl_state = ECORE_CON_SSL_STATE_INIT;
1653 DBG("beginning ssl handshake"); 1635 DBG("%s ssl handshake", svr->ecs_state ? "Queuing" : "Beginning");
1654 if (ecore_con_ssl_server_init(svr)) 1636 if ((!svr->ecs_state) && ecore_con_ssl_server_init(svr))
1655 goto error; 1637 goto error;
1656 } 1638 }
1657 1639
1658 if (!svr->fd_handler) 1640 if (!svr->fd_handler)
1659 { 1641 {
1660 ecore_con_event_server_error(svr, "Memory allocation failure"); 1642 memerr = "Memory allocation failure";
1661 goto error; 1643 goto error;
1662 } 1644 }
1663 1645
1664 svr->ip = eina_stringshare_add(net_info->ip); 1646 if ((!svr->ecs) || (svr->ecs->lookup))
1647 svr->ip = eina_stringshare_add(net_info->ip);
1665 1648
1666 return; 1649 return;
1667 1650
1668error: 1651error:
1652 if (errno || memerr) ecore_con_event_server_error(svr, memerr ?: strerror(errno));
1669 ecore_con_ssl_server_shutdown(svr); 1653 ecore_con_ssl_server_shutdown(svr);
1670 _ecore_con_server_kill(svr); 1654 _ecore_con_server_kill(svr);
1671} 1655}
@@ -1677,68 +1661,50 @@ _ecore_con_cb_udp_connect(void *data,
1677 Ecore_Con_Server *svr; 1661 Ecore_Con_Server *svr;
1678 int curstate = 0; 1662 int curstate = 0;
1679 int broadcast = 1; 1663 int broadcast = 1;
1664 const char *memerr = NULL;
1680 svr = data; 1665 svr = data;
1681 1666
1667 errno = 0;
1682 if (!net_info) /* error message has already been handled */ 1668 if (!net_info) /* error message has already been handled */
1683 goto error; 1669 goto error;
1684 1670
1685 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype, 1671 svr->fd = socket(net_info->info.ai_family, net_info->info.ai_socktype,
1686 net_info->info.ai_protocol); 1672 net_info->info.ai_protocol);
1687 if (svr->fd < 0) 1673 if (svr->fd < 0) goto error;
1688 { 1674 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
1689 ecore_con_event_server_error(svr, strerror(errno)); 1675 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
1690 goto error;
1691 }
1692
1693 if (fcntl(svr->fd, F_SETFL, O_NONBLOCK) < 0)
1694 {
1695 ecore_con_event_server_error(svr, strerror(errno));
1696 goto error;
1697 }
1698
1699 if (fcntl(svr->fd, F_SETFD, FD_CLOEXEC) < 0)
1700 {
1701 ecore_con_event_server_error(svr, strerror(errno));
1702 goto error;
1703 }
1704
1705 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_BROADCAST) 1676 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_BROADCAST)
1706 { 1677 {
1707 if (setsockopt(svr->fd, SOL_SOCKET, SO_BROADCAST, 1678 if (setsockopt(svr->fd, SOL_SOCKET, SO_BROADCAST,
1708 (const void *)&broadcast, 1679 (const void *)&broadcast,
1709 sizeof(broadcast)) < 0) 1680 sizeof(broadcast)) < 0)
1710 { 1681 {
1711 ecore_con_event_server_error(svr, strerror(errno));
1712 goto error; 1682 goto error;
1713 } 1683 }
1714 } 1684 }
1715 else if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR, 1685 if (setsockopt(svr->fd, SOL_SOCKET, SO_REUSEADDR,
1716 (const void *)&curstate, sizeof(curstate)) < 0) 1686 (const void *)&curstate, sizeof(curstate)) < 0)
1717 { 1687 goto error;
1718 ecore_con_event_server_error(svr, strerror(errno));
1719 goto error;
1720 }
1721 1688
1722 if (connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0) 1689 if (connect(svr->fd, net_info->info.ai_addr, net_info->info.ai_addrlen) < 0)
1723 { 1690 goto error;
1724 ecore_con_event_server_error(svr, strerror(errno));
1725 goto error;
1726 }
1727 1691
1728 svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE, 1692 svr->fd_handler = ecore_main_fd_handler_add(svr->fd, ECORE_FD_READ | ECORE_FD_WRITE,
1729 _ecore_con_cl_udp_handler, svr, NULL, NULL); 1693 _ecore_con_cl_udp_handler, svr, NULL, NULL);
1730 1694
1731 if (!svr->fd_handler) 1695 if (!svr->fd_handler)
1732 { 1696 {
1733 ecore_con_event_server_error(svr, "Memory allocation failure"); 1697 memerr = "Memory allocation failure";
1734 goto error; 1698 goto error;
1735 } 1699 }
1736 1700
1737 svr->ip = eina_stringshare_add(net_info->ip); 1701 if ((!svr->ecs) || (svr->ecs->lookup))
1702 svr->ip = eina_stringshare_add(net_info->ip);
1738 1703
1739 return; 1704 return;
1740 1705
1741error: 1706error:
1707 if (errno || memerr) ecore_con_event_server_error(svr, memerr ?: strerror(errno));
1742 ecore_con_ssl_server_shutdown(svr); 1708 ecore_con_ssl_server_shutdown(svr);
1743 _ecore_con_server_kill(svr); 1709 _ecore_con_server_kill(svr);
1744} 1710}
@@ -1755,14 +1721,14 @@ svr_try_connect_plain(Ecore_Con_Server *svr)
1755 if (res == SOCKET_ERROR) 1721 if (res == SOCKET_ERROR)
1756 so_err = WSAGetLastError(); 1722 so_err = WSAGetLastError();
1757 1723
1758 if ((so_err == WSAEINPROGRESS) && !svr->dead) 1724 if ((so_err == WSAEINPROGRESS) && !svr->delete_me)
1759 return ECORE_CON_INPROGRESS; 1725 return ECORE_CON_INPROGRESS;
1760 1726
1761#else 1727#else
1762 if (res < 0) 1728 if (res < 0)
1763 so_err = errno; 1729 so_err = errno;
1764 1730
1765 if ((so_err == EINPROGRESS) && !svr->dead) 1731 if ((so_err == EINPROGRESS) && !svr->delete_me)
1766 return ECORE_CON_INPROGRESS; 1732 return ECORE_CON_INPROGRESS;
1767 1733
1768#endif 1734#endif
@@ -1778,15 +1744,19 @@ svr_try_connect_plain(Ecore_Con_Server *svr)
1778 1744
1779 if ((!svr->delete_me) && (!svr->handshaking) && svr->connecting) 1745 if ((!svr->delete_me) && (!svr->handshaking) && svr->connecting)
1780 { 1746 {
1781 svr->connecting = EINA_FALSE; 1747 if (svr->ecs)
1782 svr->start_time = ecore_time_get(); 1748 {
1783 ecore_con_event_server_add(svr); 1749 if (ecore_con_socks_svr_init(svr))
1750 return ECORE_CON_INPROGRESS;
1751 }
1752 else
1753 ecore_con_event_server_add(svr);
1784 } 1754 }
1785 1755
1786 if (svr->fd_handler && (!svr->buf)) 1756 if (svr->fd_handler && (!svr->buf))
1787 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ); 1757 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
1788 1758
1789 if (!svr->dead) 1759 if (!svr->delete_me)
1790 return ECORE_CON_CONNECTED; 1760 return ECORE_CON_CONNECTED;
1791 else 1761 else
1792 return ECORE_CON_DISCONNECTED; 1762 return ECORE_CON_DISCONNECTED;
@@ -1838,9 +1808,10 @@ _ecore_con_svr_tcp_handler(void *data,
1838 Ecore_Con_Client *cl = NULL; 1808 Ecore_Con_Client *cl = NULL;
1839 unsigned char client_addr[256]; 1809 unsigned char client_addr[256];
1840 unsigned int client_addr_len; 1810 unsigned int client_addr_len;
1811 const char *clerr = NULL;
1841 1812
1842 svr = data; 1813 svr = data;
1843 if (svr->dead) 1814 if (svr->delete_me)
1844 return ECORE_CALLBACK_RENEW; 1815 return ECORE_CALLBACK_RENEW;
1845 1816
1846 if (svr->delete_me) 1817 if (svr->delete_me)
@@ -1863,34 +1834,19 @@ _ecore_con_svr_tcp_handler(void *data,
1863 client_addr_len = sizeof(client_addr); 1834 client_addr_len = sizeof(client_addr);
1864 memset(&client_addr, 0, client_addr_len); 1835 memset(&client_addr, 0, client_addr_len);
1865 cl->fd = accept(svr->fd, (struct sockaddr *)&client_addr, (socklen_t *)&client_addr_len); 1836 cl->fd = accept(svr->fd, (struct sockaddr *)&client_addr, (socklen_t *)&client_addr_len);
1866 if (cl->fd < 0) 1837 if (cl->fd < 0) goto error;
1867 {
1868 ecore_con_event_server_error(svr, strerror(errno));
1869 goto free_cl;
1870 }
1871
1872 if ((svr->client_limit >= 0) && (svr->reject_excess_clients) && 1838 if ((svr->client_limit >= 0) && (svr->reject_excess_clients) &&
1873 (svr->client_count >= (unsigned int)svr->client_limit)) 1839 (svr->client_count >= (unsigned int)svr->client_limit))
1874 { 1840 {
1875 ecore_con_event_server_error(svr, "Maximum client limit reached"); 1841 clerr = "Maximum client limit reached";
1876 goto close_fd; 1842 goto error;
1877 } 1843 }
1878 1844
1879 if (fcntl(cl->fd, F_SETFL, O_NONBLOCK) < 0) 1845 if (fcntl(cl->fd, F_SETFL, O_NONBLOCK) < 0) goto error;
1880 { 1846 if (fcntl(cl->fd, F_SETFD, FD_CLOEXEC) < 0) goto error;
1881 ecore_con_event_server_error(svr, strerror(errno));
1882 goto close_fd;
1883 }
1884 if (fcntl(cl->fd, F_SETFD, FD_CLOEXEC) < 0)
1885 {
1886 ecore_con_event_server_error(svr, strerror(errno));
1887 goto close_fd;
1888 }
1889 cl->fd_handler = ecore_main_fd_handler_add(cl->fd, ECORE_FD_READ, 1847 cl->fd_handler = ecore_main_fd_handler_add(cl->fd, ECORE_FD_READ,
1890 _ecore_con_svr_cl_handler, cl, NULL, NULL); 1848 _ecore_con_svr_cl_handler, cl, NULL, NULL);
1891 if (!cl->fd_handler) 1849 if (!cl->fd_handler) goto error;
1892 goto close_fd;
1893
1894 ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT); 1850 ECORE_MAGIC_SET(cl, ECORE_MAGIC_CON_CLIENT);
1895 1851
1896 if ((!svr->upgrade) && (svr->type & ECORE_CON_SSL)) 1852 if ((!svr->upgrade) && (svr->type & ECORE_CON_SSL))
@@ -1898,14 +1854,14 @@ _ecore_con_svr_tcp_handler(void *data,
1898 cl->handshaking = EINA_TRUE; 1854 cl->handshaking = EINA_TRUE;
1899 cl->ssl_state = ECORE_CON_SSL_STATE_INIT; 1855 cl->ssl_state = ECORE_CON_SSL_STATE_INIT;
1900 if (ecore_con_ssl_client_init(cl)) 1856 if (ecore_con_ssl_client_init(cl))
1901 goto del_handler; 1857 goto error;
1902 } 1858 }
1903 1859
1904 cl->client_addr = malloc(client_addr_len); 1860 cl->client_addr = malloc(client_addr_len);
1905 if (!cl->client_addr) 1861 if (!cl->client_addr)
1906 { 1862 {
1907 ecore_con_event_server_error(svr, "Memory allocation failure when attempting to add a new client"); 1863 clerr = "Memory allocation failure when attempting to add a new client";
1908 goto del_handler; 1864 goto error;
1909 } 1865 }
1910 cl->client_addr_len = client_addr_len; 1866 cl->client_addr_len = client_addr_len;
1911 memcpy(cl->client_addr, &client_addr, client_addr_len); 1867 memcpy(cl->client_addr, &client_addr, client_addr_len);
@@ -1918,29 +1874,28 @@ _ecore_con_svr_tcp_handler(void *data,
1918 1874
1919 return ECORE_CALLBACK_RENEW; 1875 return ECORE_CALLBACK_RENEW;
1920 1876
1921 del_handler: 1877error:
1922 ecore_main_fd_handler_del(cl->fd_handler); 1878 if (cl->fd_handler) ecore_main_fd_handler_del(cl->fd_handler);
1923 close_fd: 1879 if (cl->fd >= 0) close(cl->fd);
1924 close(cl->fd);
1925 free_cl:
1926 free(cl); 1880 free(cl);
1927 1881 if (clerr || errno) ecore_con_event_server_error(svr, clerr ?: strerror(errno));
1928 return ECORE_CALLBACK_RENEW; 1882 return ECORE_CALLBACK_RENEW;
1929} 1883}
1930 1884
1931static void 1885static void
1932_ecore_con_cl_read(Ecore_Con_Server *svr) 1886_ecore_con_cl_read(Ecore_Con_Server *svr)
1933{ 1887{
1934 DBG("svr=%p", svr);
1935 int num = 0; 1888 int num = 0;
1936 Eina_Bool lost_server = EINA_TRUE; 1889 Eina_Bool lost_server = EINA_TRUE;
1937 unsigned char buf[READBUFSIZ]; 1890 unsigned char buf[READBUFSIZ];
1938 1891
1892 DBG("svr=%p", svr);
1893
1939 /* only possible with non-ssl connections */ 1894 /* only possible with non-ssl connections */
1940 if (svr->connecting && (svr_try_connect_plain(svr) != ECORE_CON_CONNECTED)) 1895 if (svr->connecting && (svr_try_connect_plain(svr) != ECORE_CON_CONNECTED))
1941 return; 1896 return;
1942 1897
1943 if (svr->handshaking) 1898 if (svr->handshaking && (!svr->ecs_state))
1944 { 1899 {
1945 DBG("Continuing ssl handshake"); 1900 DBG("Continuing ssl handshake");
1946 if (!ecore_con_ssl_server_init(svr)) 1901 if (!ecore_con_ssl_server_init(svr))
@@ -1948,8 +1903,9 @@ _ecore_con_cl_read(Ecore_Con_Server *svr)
1948 _ecore_con_server_timer_update(svr); 1903 _ecore_con_server_timer_update(svr);
1949 } 1904 }
1950 1905
1951 if (!(svr->type & ECORE_CON_SSL)) 1906 if (svr->ecs_state || !(svr->type & ECORE_CON_SSL))
1952 { 1907 {
1908 errno = 0;
1953 num = read(svr->fd, buf, sizeof(buf)); 1909 num = read(svr->fd, buf, sizeof(buf));
1954 /* 0 is not a valid return value for a tcp socket */ 1910 /* 0 is not a valid return value for a tcp socket */
1955 if ((num > 0) || ((num < 0) && (errno == EAGAIN))) 1911 if ((num > 0) || ((num < 0) && (errno == EAGAIN)))
@@ -1966,7 +1922,12 @@ _ecore_con_cl_read(Ecore_Con_Server *svr)
1966 } 1922 }
1967 1923
1968 if ((!svr->delete_me) && (num > 0)) 1924 if ((!svr->delete_me) && (num > 0))
1969 ecore_con_event_server_data(svr, buf, num, EINA_TRUE); 1925 {
1926 if (svr->ecs_state)
1927 ecore_con_socks_read(svr, buf, num);
1928 else
1929 ecore_con_event_server_data(svr, buf, num, EINA_TRUE);
1930 }
1970 1931
1971 if (lost_server) 1932 if (lost_server)
1972 _ecore_con_server_kill(svr); 1933 _ecore_con_server_kill(svr);
@@ -1980,7 +1941,7 @@ _ecore_con_cl_handler(void *data,
1980 Eina_Bool want_read, want_write; 1941 Eina_Bool want_read, want_write;
1981 1942
1982 svr = data; 1943 svr = data;
1983 if (svr->dead) 1944 if (svr->delete_me)
1984 return ECORE_CALLBACK_RENEW; 1945 return ECORE_CALLBACK_RENEW;
1985 1946
1986 if (svr->delete_me) 1947 if (svr->delete_me)
@@ -1989,7 +1950,7 @@ _ecore_con_cl_handler(void *data,
1989 want_read = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ); 1950 want_read = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ);
1990 want_write = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE); 1951 want_write = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE);
1991 1952
1992 if (svr->handshaking && (want_read || want_write)) 1953 if ((!svr->ecs_state) && svr->handshaking && (want_read || want_write))
1993 { 1954 {
1994 DBG("Continuing ssl handshake: preparing to %s...", want_read ? "read" : "write"); 1955 DBG("Continuing ssl handshake: preparing to %s...", want_read ? "read" : "write");
1995#ifdef ISCOMFITOR 1956#ifdef ISCOMFITOR
@@ -2005,20 +1966,25 @@ _ecore_con_cl_handler(void *data,
2005 { 1966 {
2006 ERR("ssl handshaking failed!"); 1967 ERR("ssl handshaking failed!");
2007 svr->handshaking = EINA_FALSE; 1968 svr->handshaking = EINA_FALSE;
2008
2009 } 1969 }
2010 else if (!svr->ssl_state) 1970 else if (!svr->ssl_state)
1971 ecore_con_event_server_add(svr);
1972 return ECORE_CALLBACK_RENEW;
1973 }
1974 if (svr->ecs && svr->ecs_state && (svr->ecs_state < ECORE_CON_SOCKS_STATE_READ) && (!svr->ecs_buf))
1975 {
1976 if (svr->ecs_state < ECORE_CON_SOCKS_STATE_INIT)
2011 { 1977 {
2012 svr->connecting = EINA_FALSE; 1978 INF("PROXY STATE++");
2013 svr->start_time = ecore_time_get(); 1979 svr->ecs_state++;
2014 ecore_con_event_server_add(svr);
2015 } 1980 }
1981 if (ecore_con_socks_svr_init(svr)) return ECORE_CALLBACK_RENEW;
2016 } 1982 }
2017 else if (want_read) 1983 if (want_read)
2018 _ecore_con_cl_read(svr); 1984 _ecore_con_cl_read(svr);
2019 else if (want_write) /* only possible with non-ssl connections */ 1985 else if (want_write) /* only possible with non-ssl connections */
2020 { 1986 {
2021 if (svr->connecting && (!svr_try_connect_plain(svr))) 1987 if (svr->connecting && (!svr_try_connect_plain(svr)) && (!svr->ecs_state))
2022 return ECORE_CALLBACK_RENEW; 1988 return ECORE_CALLBACK_RENEW;
2023 1989
2024 _ecore_con_server_flush(svr); 1990 _ecore_con_server_flush(svr);
@@ -2040,7 +2006,7 @@ _ecore_con_cl_udp_handler(void *data,
2040 want_write = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE); 2006 want_write = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE);
2041 2007
2042 svr = data; 2008 svr = data;
2043 if (svr->dead || svr->delete_me || ((!want_read) && (!want_write))) 2009 if (svr->delete_me || svr->delete_me || ((!want_read) && (!want_write)))
2044 return ECORE_CALLBACK_RENEW; 2010 return ECORE_CALLBACK_RENEW;
2045 2011
2046 if (want_write) 2012 if (want_write)
@@ -2076,7 +2042,7 @@ _ecore_con_svr_udp_handler(void *data,
2076 2042
2077 svr = data; 2043 svr = data;
2078 2044
2079 if (svr->delete_me || svr->dead) 2045 if (svr->delete_me)
2080 return ECORE_CALLBACK_RENEW; 2046 return ECORE_CALLBACK_RENEW;
2081 2047
2082 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE)) 2048 if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
@@ -2106,9 +2072,7 @@ _ecore_con_svr_udp_handler(void *data,
2106 ecore_con_event_server_error(svr, strerror(errno)); 2072 ecore_con_event_server_error(svr, strerror(errno));
2107 if (!svr->delete_me) 2073 if (!svr->delete_me)
2108 ecore_con_event_client_del(NULL); 2074 ecore_con_event_client_del(NULL);
2109 2075 _ecore_con_server_kill(svr);
2110 svr->dead = EINA_TRUE;
2111 svr->fd_handler = NULL;
2112 return ECORE_CALLBACK_CANCEL; 2076 return ECORE_CALLBACK_CANCEL;
2113 } 2077 }
2114 2078
@@ -2177,18 +2141,7 @@ _ecore_con_svr_cl_read(Ecore_Con_Client *cl)
2177 if ((!cl->delete_me) && (num > 0)) 2141 if ((!cl->delete_me) && (num > 0))
2178 ecore_con_event_client_data(cl, buf, num, EINA_TRUE); 2142 ecore_con_event_client_data(cl, buf, num, EINA_TRUE);
2179 2143
2180 if (lost_client) 2144 if (lost_client) _ecore_con_client_kill(cl);
2181 {
2182 if (!cl->delete_me)
2183 ecore_con_event_client_del(cl);
2184 INF("Lost client %s", (cl->ip) ? cl->ip : "");
2185 cl->dead = EINA_TRUE;
2186 if (cl->fd_handler)
2187 ecore_main_fd_handler_del(cl->fd_handler);
2188
2189 cl->fd_handler = NULL;
2190 return;
2191 }
2192} 2145}
2193 2146
2194static Eina_Bool 2147static Eina_Bool
@@ -2198,9 +2151,6 @@ _ecore_con_svr_cl_handler(void *data,
2198 Ecore_Con_Client *cl; 2151 Ecore_Con_Client *cl;
2199 2152
2200 cl = data; 2153 cl = data;
2201 if (cl->dead)
2202 return ECORE_CALLBACK_RENEW;
2203
2204 if (cl->delete_me) 2154 if (cl->delete_me)
2205 return ECORE_CALLBACK_RENEW; 2155 return ECORE_CALLBACK_RENEW;
2206 2156
@@ -2209,10 +2159,8 @@ _ecore_con_svr_cl_handler(void *data,
2209 if (ecore_con_ssl_client_init(cl)) 2159 if (ecore_con_ssl_client_init(cl))
2210 { 2160 {
2211 ERR("ssl handshaking failed!"); 2161 ERR("ssl handshaking failed!");
2212 cl->handshaking = EINA_FALSE; 2162 _ecore_con_client_kill(cl);
2213 cl->dead = EINA_TRUE; 2163 return ECORE_CALLBACK_RENEW;
2214 INF("Lost client %s", (cl->ip) ? cl->ip : "");
2215 ecore_con_event_client_del(cl);
2216 } 2164 }
2217 else if (!cl->ssl_state) 2165 else if (!cl->ssl_state)
2218 ecore_con_event_client_add(cl); 2166 ecore_con_event_client_add(cl);
@@ -2230,19 +2178,25 @@ static void
2230_ecore_con_server_flush(Ecore_Con_Server *svr) 2178_ecore_con_server_flush(Ecore_Con_Server *svr)
2231{ 2179{
2232 int count, num; 2180 int count, num;
2181 size_t buf_len, buf_offset;
2182 const void *buf;
2233 2183
2184 DBG("(svr=%p,buf=%p)", svr, svr->buf);
2234#ifdef _WIN32 2185#ifdef _WIN32
2235 if (ecore_con_local_win32_server_flush(svr)) 2186 if (ecore_con_local_win32_server_flush(svr))
2236 return; 2187 return;
2237#endif 2188#endif
2238 2189
2239 if ((!svr->buf) && svr->fd_handler) 2190 if ((!svr->buf) && (!svr->ecs_buf) && svr->fd_handler)
2240 { 2191 {
2241 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ); 2192 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
2242 return; 2193 return;
2243 } 2194 }
2244 2195
2245 num = eina_binbuf_length_get(svr->buf) - svr->write_buf_offset; 2196 buf = svr->buf ? eina_binbuf_string_get(svr->buf) : eina_binbuf_string_get(svr->ecs_buf);
2197 buf_len = svr->buf ? eina_binbuf_length_get(svr->buf) : eina_binbuf_length_get(svr->ecs_buf);
2198 buf_offset = svr->buf ? svr->write_buf_offset : svr->ecs_buf_offset;
2199 num = buf_len - buf_offset;
2246 2200
2247 /* check whether we need to write anything at all. 2201 /* check whether we need to write anything at all.
2248 * we must not write zero bytes with SSL_write() since it 2202 * we must not write zero bytes with SSL_write() since it
@@ -2253,7 +2207,7 @@ _ecore_con_server_flush(Ecore_Con_Server *svr)
2253 */ 2207 */
2254 if (num <= 0) return; 2208 if (num <= 0) return;
2255 2209
2256 if (svr->handshaking) 2210 if ((!svr->ecs_state) && svr->handshaking)
2257 { 2211 {
2258 DBG("Continuing ssl handshake"); 2212 DBG("Continuing ssl handshake");
2259 if (ecore_con_ssl_server_init(svr)) 2213 if (ecore_con_ssl_server_init(svr))
@@ -2262,10 +2216,10 @@ _ecore_con_server_flush(Ecore_Con_Server *svr)
2262 return; 2216 return;
2263 } 2217 }
2264 2218
2265 if (!(svr->type & ECORE_CON_SSL)) 2219 if (svr->ecs_state || (!(svr->type & ECORE_CON_SSL)))
2266 count = write(svr->fd, eina_binbuf_string_get(svr->buf) + svr->write_buf_offset, num); 2220 count = write(svr->fd, buf + buf_offset, num);
2267 else 2221 else
2268 count = ecore_con_ssl_server_write(svr, eina_binbuf_string_get(svr->buf) + svr->write_buf_offset, num); 2222 count = ecore_con_ssl_server_write(svr, buf + buf_offset, num);
2269 2223
2270 if (count < 0) 2224 if (count < 0)
2271 { 2225 {
@@ -2277,13 +2231,36 @@ _ecore_con_server_flush(Ecore_Con_Server *svr)
2277 return; 2231 return;
2278 } 2232 }
2279 2233
2280 if (count) ecore_con_event_server_write(svr, count); 2234 if (count && (!svr->ecs_state)) ecore_con_event_server_write(svr, count);
2281 svr->write_buf_offset += count; 2235 if (svr->ecs_buf)
2282 if (svr->write_buf_offset >= eina_binbuf_length_get(svr->buf)) 2236 buf_offset = svr->ecs_buf_offset += count;
2237 else
2238 buf_offset = svr->write_buf_offset += count;
2239 if (buf_offset >= buf_len)
2283 { 2240 {
2284 svr->write_buf_offset = 0; 2241 if (svr->ecs_buf)
2285 eina_binbuf_free(svr->buf); 2242 {
2286 svr->buf = NULL; 2243 svr->ecs_buf_offset = 0;
2244 eina_binbuf_free(svr->ecs_buf);
2245 svr->ecs_buf = NULL;
2246 INF("PROXY STATE++");
2247 svr->ecs_state++;
2248 }
2249 else
2250 {
2251 svr->write_buf_offset = 0;
2252 eina_binbuf_free(svr->buf);
2253 svr->buf = NULL;
2254#ifdef TCP_CORK
2255 if ((svr->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_CORK)
2256 {
2257 int state = 0;
2258 if (setsockopt(svr->fd, IPPROTO_TCP, TCP_CORK, (char *)&state, sizeof(int)) < 0)
2259 /* realistically this isn't anything serious so we can just log and continue */
2260 ERR("uncorking failed! %s", strerror(errno));
2261 }
2262#endif
2263 }
2287 if (svr->fd_handler) 2264 if (svr->fd_handler)
2288 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ); 2265 ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ);
2289 } 2266 }
@@ -2330,13 +2307,7 @@ _ecore_con_client_flush(Ecore_Con_Client *cl)
2330 if ((errno != EAGAIN) && (errno != EINTR) && (!cl->delete_me)) 2307 if ((errno != EAGAIN) && (errno != EINTR) && (!cl->delete_me))
2331 { 2308 {
2332 ecore_con_event_client_error(cl, strerror(errno)); 2309 ecore_con_event_client_error(cl, strerror(errno));
2333 ecore_con_event_client_del(cl); 2310 _ecore_con_client_kill(cl);
2334 cl->dead = EINA_TRUE;
2335 INF("Lost client %s", (cl->ip) ? cl->ip : "");
2336 if (cl->fd_handler)
2337 ecore_main_fd_handler_del(cl->fd_handler);
2338
2339 cl->fd_handler = NULL;
2340 } 2311 }
2341 2312
2342 return; 2313 return;
@@ -2349,6 +2320,15 @@ _ecore_con_client_flush(Ecore_Con_Client *cl)
2349 cl->buf_offset = 0; 2320 cl->buf_offset = 0;
2350 eina_binbuf_free(cl->buf); 2321 eina_binbuf_free(cl->buf);
2351 cl->buf = NULL; 2322 cl->buf = NULL;
2323#ifdef TCP_CORK
2324 if ((cl->host_server->type & ECORE_CON_TYPE) == ECORE_CON_REMOTE_CORK)
2325 {
2326 int state = 0;
2327 if (setsockopt(cl->fd, IPPROTO_TCP, TCP_CORK, (char *)&state, sizeof(int)) < 0)
2328 /* realistically this isn't anything serious so we can just log and continue */
2329 ERR("uncorking failed! %s", strerror(errno));
2330 }
2331#endif
2352 if (cl->fd_handler) 2332 if (cl->fd_handler)
2353 ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ); 2333 ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ);
2354 } 2334 }
@@ -2398,7 +2378,7 @@ _ecore_con_event_client_del_free(Ecore_Con_Server *svr,
2398 if ((!svr->event_count) && (svr->delete_me)) 2378 if ((!svr->event_count) && (svr->delete_me))
2399 _ecore_con_server_free(svr); 2379 _ecore_con_server_free(svr);
2400 } 2380 }
2401 if ((!e->client->event_count) && (e->client->delete_me)) 2381 if (!e->client->event_count)
2402 ecore_con_client_del(e->client); 2382 ecore_con_client_del(e->client);
2403 } 2383 }
2404 ecore_con_event_client_del_free(e); 2384 ecore_con_event_client_del_free(e);
@@ -2490,7 +2470,7 @@ _ecore_con_event_server_del_free(void *data __UNUSED__,
2490 if (e->server) 2470 if (e->server)
2491 { 2471 {
2492 e->server->event_count = eina_list_remove(e->server->event_count, ev); 2472 e->server->event_count = eina_list_remove(e->server->event_count, ev);
2493 if ((!e->server->event_count) && (e->server->delete_me)) 2473 if (!e->server->event_count)
2494 _ecore_con_server_free(e->server); 2474 _ecore_con_server_free(e->server);
2495 } 2475 }
2496 ecore_con_event_server_del_free(e); 2476 ecore_con_event_server_del_free(e);