7 #ifdef HAVE_NETINET_IN_H
8 # include <sys/types.h>
9 # include <netinet/in.h>
12 #ifdef HAVE_WINSOCK2_H
13 # include <winsock2.h>
16 #if USE_GNUTLS_OPENSSL
17 # include <gnutls/openssl.h>
19 # include <openssl/ssl.h>
23 #include <ecore_private.h>
24 #include <Ecore_Con.h>
26 #include "Ecore_Ipc.h"
27 #include "ecore_ipc_private.h"
46 int _ecore_ipc_log_dom = -1;
49 _ecore_ipc_swap_16(unsigned short v)
53 s = (unsigned char *)(&v);
54 t = s[0]; s[0] = s[1]; s[1] = t;
59 _ecore_ipc_swap_32(unsigned int v)
63 s = (unsigned char *)(&v);
64 t = s[0]; s[0] = s[3]; s[3] = t;
65 t = s[1]; s[1] = s[2]; s[2] = t;
69 EAPI unsigned long long
70 _ecore_ipc_swap_64(unsigned long long v)
74 s = (unsigned char *)(&v);
75 t = s[0]; s[0] = s[7]; s[7] = t;
76 t = s[1]; s[1] = s[6]; s[6] = t;
77 t = s[2]; s[2] = s[5]; s[5] = t;
78 t = s[3]; s[3] = s[4]; s[4] = t;
82 static int _ecore_ipc_dlt_int(int out, int prev, int *mode);
83 static int _ecore_ipc_ddlt_int(int in, int prev, int mode);
86 _ecore_ipc_dlt_int(int out, int prev, int *mode)
96 if (out == (int)0xffffffff)
106 if (out == prev << 1)
111 if (out == prev >> 1)
118 if (!(dlt & 0xffffff00))
124 if (!(dlt & 0xffffff00))
130 if (!(dlt & 0x00ffffff))
133 return (dlt >> 24) & 0xff;
136 if (!(dlt & 0x00ffffff))
139 return (dlt >> 24) & 0xff;
143 if (!(dlt & 0xffff0000))
149 if (!(dlt & 0xffff0000))
155 if (!(dlt & 0x0000ffff))
158 return (dlt >> 16) & 0xffff;
161 if (!(dlt & 0x0000ffff))
164 return (dlt >> 16) & 0xffff;
172 _ecore_ipc_ddlt_int(int in, int prev, int mode)
198 return prev + (in << 24);
201 return prev - (in << 24);
210 return prev + (in << 16);
213 return prev - (in << 16);
230 static Eina_Bool _ecore_ipc_event_client_add(void *data, int ev_type, void *ev);
231 static Eina_Bool _ecore_ipc_event_client_del(void *data, int ev_type, void *ev);
232 static Eina_Bool _ecore_ipc_event_server_add(void *data, int ev_type, void *ev);
233 static Eina_Bool _ecore_ipc_event_server_del(void *data, int ev_type, void *ev);
234 static Eina_Bool _ecore_ipc_event_client_data(void *data, int ev_type, void *ev);
235 static Eina_Bool _ecore_ipc_event_server_data(void *data, int ev_type, void *ev);
236 static void _ecore_ipc_event_client_add_free(void *data, void *ev);
237 static void _ecore_ipc_event_client_del_free(void *data, void *ev);
238 static void _ecore_ipc_event_client_data_free(void *data, void *ev);
239 static void _ecore_ipc_event_server_add_free(void *data, void *ev);
240 static void _ecore_ipc_event_server_del_free(void *data, void *ev);
241 static void _ecore_ipc_event_server_data_free(void *data, void *ev);
243 EAPI int ECORE_IPC_EVENT_CLIENT_ADD = 0;
244 EAPI int ECORE_IPC_EVENT_CLIENT_DEL = 0;
245 EAPI int ECORE_IPC_EVENT_SERVER_ADD = 0;
246 EAPI int ECORE_IPC_EVENT_SERVER_DEL = 0;
247 EAPI int ECORE_IPC_EVENT_CLIENT_DATA = 0;
248 EAPI int ECORE_IPC_EVENT_SERVER_DATA = 0;
250 static int _ecore_ipc_init_count = 0;
251 static Eina_List *servers = NULL;
252 static Ecore_Event_Handler *handler[6];
255 * @defgroup Ecore_IPC_Library_Group IPC Library Functions
257 * Functions that set up and shut down the Ecore IPC Library.
261 * Initialises the Ecore IPC library.
262 * @return Number of times the library has been initialised without
264 * @ingroup Ecore_IPC_Library_Group
271 if (++_ecore_ipc_init_count != 1)
272 return _ecore_ipc_init_count;
273 _ecore_ipc_log_dom = eina_log_domain_register
274 ("ecore_ipc", ECORE_IPC_DEFAULT_LOG_COLOR);
275 if(_ecore_ipc_log_dom < 0)
277 EINA_LOG_ERR("Impossible to create a log domain for the Ecore IPC module.");
278 return --_ecore_ipc_init_count;
280 if (!ecore_con_init())
281 return --_ecore_ipc_init_count;
283 ECORE_IPC_EVENT_CLIENT_ADD = ecore_event_type_new();
284 ECORE_IPC_EVENT_CLIENT_DEL = ecore_event_type_new();
285 ECORE_IPC_EVENT_SERVER_ADD = ecore_event_type_new();
286 ECORE_IPC_EVENT_SERVER_DEL = ecore_event_type_new();
287 ECORE_IPC_EVENT_CLIENT_DATA = ecore_event_type_new();
288 ECORE_IPC_EVENT_SERVER_DATA = ecore_event_type_new();
290 handler[i++] = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_ADD,
291 _ecore_ipc_event_client_add, NULL);
292 handler[i++] = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DEL,
293 _ecore_ipc_event_client_del, NULL);
294 handler[i++] = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD,
295 _ecore_ipc_event_server_add, NULL);
296 handler[i++] = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DEL,
297 _ecore_ipc_event_server_del, NULL);
298 handler[i++] = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DATA,
299 _ecore_ipc_event_client_data, NULL);
300 handler[i] = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DATA,
301 _ecore_ipc_event_server_data, NULL);
302 return _ecore_ipc_init_count;
306 * Shuts down the Ecore IPC library.
307 * @return Number of times the library has been initialised without being
309 * @ingroup Ecore_IPC_Library_Group
312 ecore_ipc_shutdown(void)
316 if (--_ecore_ipc_init_count != 0)
317 return _ecore_ipc_init_count;
320 Ecore_Ipc_Server *svr;
321 EINA_LIST_FOREACH_SAFE(servers, l, l2, svr)
322 ecore_ipc_server_del(svr);
324 for (i = 0; i < 6; i++)
325 ecore_event_handler_del(handler[i]);
327 ecore_con_shutdown();
328 eina_log_domain_unregister(_ecore_ipc_log_dom);
329 _ecore_ipc_log_dom = -1;
330 return _ecore_ipc_init_count;
334 * @defgroup Ecore_IPC_Server_Group IPC Server Functions
336 * Functions the deal with IPC server objects.
340 * Creates an IPC server that listens for connections.
342 * For more details about the @p compl_type, @p name and @p port
343 * parameters, see the @ref ecore_con_server_add documentation.
345 * @param compl_type The connection type.
346 * @param name Name to associate with the socket used for connection.
347 * @param port Number to identify with socket used for connection.
348 * @param data Data to associate with the IPC server.
349 * @return New IPC server. If there is an error, @c NULL is returned.
350 * @ingroup Ecore_IPC_Server_Group
351 * @todo Need to add protocol type parameter to this function.
353 EAPI Ecore_Ipc_Server *
354 ecore_ipc_server_add(Ecore_Ipc_Type compl_type, const char *name, int port, const void *data)
356 Ecore_Ipc_Server *svr;
358 Ecore_Con_Type extra = 0;
360 svr = calloc(1, sizeof(Ecore_Ipc_Server));
361 if (!svr) return NULL;
363 type &= ~ECORE_IPC_USE_SSL;
364 if (compl_type & ECORE_IPC_USE_SSL) extra = ECORE_CON_USE_SSL;
367 case ECORE_IPC_LOCAL_USER:
368 svr->server = ecore_con_server_add(ECORE_CON_LOCAL_USER | extra, name, port, svr);
370 case ECORE_IPC_LOCAL_SYSTEM:
371 svr->server = ecore_con_server_add(ECORE_CON_LOCAL_SYSTEM | extra, name, port, svr);
373 case ECORE_IPC_REMOTE_SYSTEM:
374 svr->server = ecore_con_server_add(ECORE_CON_REMOTE_SYSTEM | extra, name, port, svr);
385 svr->max_buf_size = 32 * 1024;
386 svr->data = (void *)data;
387 servers = eina_list_append(servers, svr);
388 ECORE_MAGIC_SET(svr, ECORE_MAGIC_IPC_SERVER);
393 * Creates an IPC server object to represent the IPC server listening
396 * For more details about the @p compl_type, @p name and @p port
397 * parameters, see the @ref ecore_con_server_connect documentation.
399 * @param compl_type The IPC connection type.
400 * @param name Name used to determine which socket to use for the
402 * @param port Number used to identify the socket to use for the
404 * @param data Data to associate with the server.
405 * @return A new IPC server. @c NULL is returned on error.
406 * @ingroup Ecore_IPC_Server_Group
407 * @todo Need to add protocol type parameter.
409 EAPI Ecore_Ipc_Server *
410 ecore_ipc_server_connect(Ecore_Ipc_Type compl_type, char *name, int port, const void *data)
412 Ecore_Ipc_Server *svr;
414 Ecore_Con_Type extra = 0;
416 svr = calloc(1, sizeof(Ecore_Ipc_Server));
417 if (!svr) return NULL;
419 type &= ~ECORE_IPC_USE_SSL;
420 if (compl_type & ECORE_IPC_USE_SSL) extra = ECORE_CON_USE_SSL;
423 case ECORE_IPC_LOCAL_USER:
424 svr->server = ecore_con_server_connect(ECORE_CON_LOCAL_USER | extra, name, port, svr);
426 case ECORE_IPC_LOCAL_SYSTEM:
427 svr->server = ecore_con_server_connect(ECORE_CON_LOCAL_SYSTEM | extra, name, port, svr);
429 case ECORE_IPC_REMOTE_SYSTEM:
430 svr->server = ecore_con_server_connect(ECORE_CON_REMOTE_SYSTEM | extra, name, port, svr);
441 svr->max_buf_size = -1;
442 svr->data = (void *)data;
443 servers = eina_list_append(servers, svr);
444 ECORE_MAGIC_SET(svr, ECORE_MAGIC_IPC_SERVER);
449 * Closes the connection and frees the given IPC server.
450 * @param svr The given IPC server.
451 * @return The data associated with the server when it was created.
452 * @ingroup Ecore_IPC_Server_Group
455 ecore_ipc_server_del(Ecore_Ipc_Server *svr)
459 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
461 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
462 "ecore_ipc_server_del");
465 if (svr->delete_me) return NULL;
470 if (svr->event_count == 0)
472 Ecore_Ipc_Client *cl;
474 EINA_LIST_FREE(svr->clients, cl)
475 ecore_ipc_client_del(cl);
476 ecore_con_server_del(svr->server);
477 servers = eina_list_remove(servers, svr);
479 if (svr->buf) free(svr->buf);
480 ECORE_MAGIC_SET(svr, ECORE_MAGIC_NONE);
487 * Retrieves the data associated with the given IPC server.
488 * @param svr The given IPC server.
489 * @return The associated data.
490 * @ingroup Ecore_IPC_Server_Group
493 ecore_ipc_server_data_get(Ecore_Ipc_Server *svr)
495 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
497 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
498 "ecore_ipc_server_data_get");
505 * Retrieves whether the given IPC server is currently connected.
506 * @param svr The given IPC server.
507 * @return @c EINA_TRUE if the server is connected, @c EINA_FALSE otherwise.
508 * @ingroup Ecore_IPC_Server_Group
511 ecore_ipc_server_connected_get(Ecore_Ipc_Server *svr)
513 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
515 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
516 "ecore_ipc_server_connected_get");
519 return ecore_con_server_connected_get(svr->server);
523 * Retrieves the list of clients for this server.
524 * @param svr The given IPC server.
525 * @return An Eina_List with the clients.
526 * @ingroup Ecore_IPC_Server_Group
529 ecore_ipc_server_clients_get(Ecore_Ipc_Server *svr)
531 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
533 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
534 "ecore_ipc_server_clients_get");
537 return svr->client_list;
540 #define SVENC(_member) \
541 d = _ecore_ipc_dlt_int(msg._member, svr->prev.o._member, &md); \
546 dd = (unsigned char *)&v; \
549 *(dat + s + 0) = dd[0]; \
550 *(dat + s + 1) = dd[1]; \
551 *(dat + s + 2) = dd[2]; \
552 *(dat + s + 3) = dd[3]; \
555 else if (md >= DLT_ADD16) \
559 dd = (unsigned char *)&v; \
562 *(dat + s + 0) = dd[0]; \
563 *(dat + s + 1) = dd[1]; \
566 else if (md >= DLT_ADD8) \
568 *(dat + s + 0) = (unsigned char)d; \
573 * Sends a message to the given IPC server.
575 * The content of the parameters, excluding the @p svr paramter, is up to
578 * @param svr The given IPC server.
579 * @param major Major opcode of the message.
580 * @param minor Minor opcode of the message.
581 * @param ref Message reference number.
582 * @param ref_to Reference number of the message this message refers to.
583 * @param response Requires response.
584 * @param data The data to send as part of the message.
585 * @param size Length of the data, in bytes, to send.
586 * @return Number of bytes sent. @c 0 is returned if there is an error.
587 * @ingroup Ecore_IPC_Server_Group
588 * @todo This function needs to become an IPC message.
589 * @todo Fix up the documentation: Make sure what ref_to and response are.
592 ecore_ipc_server_send(Ecore_Ipc_Server *svr, int major, int minor, int ref, int ref_to, int response, const void *data, int size)
594 Ecore_Ipc_Msg_Head msg;
596 int *head, md = 0, d, s;
597 unsigned char dat[sizeof(Ecore_Ipc_Msg_Head)];
599 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
601 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
602 "ecore_ipc_server_send");
605 if (size < 0) size = 0;
610 msg.response = response;
617 *head |= md << (4 * 1);
619 *head |= md << (4 * 2);
621 *head |= md << (4 * 3);
623 *head |= md << (4 * 4);
625 *head |= md << (4 * 5);
626 *head = htonl(*head);
628 ret = ecore_con_server_send(svr->server, dat, s);
629 if (size > 0) ret += ecore_con_server_send(svr->server, data, size);
634 * Sets a limit on the number of clients that can be handled concurrently
635 * by the given server, and a policy on what to do if excess clients try to
637 * Beware that if you set this once ecore is already running, you may
638 * already have pending CLIENT_ADD events in your event queue. Those
639 * clients have already connected and will not be affected by this call.
640 * Only clients subsequently trying to connect will be affected.
641 * @param svr The given server.
642 * @param client_limit The maximum number of clients to handle
643 * concurrently. -1 means unlimited (default). 0
644 * effectively disables the server.
645 * @param reject_excess_clients Set to 1 to automatically disconnect
646 * excess clients as soon as they connect if you are
647 * already handling client_limit clients. Set to 0
648 * (default) to just hold off on the "accept()"
649 * system call until the number of active clients
650 * drops. This causes the kernel to queue up to 4096
651 * connections (or your kernel's limit, whichever is
653 * @ingroup Ecore_Ipc_Server_Group
656 ecore_ipc_server_client_limit_set(Ecore_Ipc_Server *svr, int client_limit, char reject_excess_clients)
658 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
660 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
661 "ecore_ipc_server_client_limit_set");
664 ecore_con_server_client_limit_set(svr->server, client_limit, reject_excess_clients);
668 * Sets the max data payload size for an Ipc message in bytes
670 * @param svr The given server.
671 * @param size The maximum data payload size in bytes.
672 * @ingroup Ecore_Ipc_Server_Group
675 ecore_ipc_server_data_size_max_set(Ecore_Ipc_Server *svr, int size)
677 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
679 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
680 "ecore_ipc_server_data_size_max_set");
683 svr->max_buf_size = size;
687 * Gets the max data payload size for an Ipc message in bytes
689 * @param svr The given server.
690 * @return The maximum data payload in bytes.
691 * @ingroup Ecore_Ipc_Server_Group
694 ecore_ipc_server_data_size_max_get(Ecore_Ipc_Server *svr)
696 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
698 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
699 "ecore_ipc_server_data_size_max_get");
702 return svr->max_buf_size;
706 * Gets the IP address of a server that has been connected to.
708 * @param svr The given server.
709 * @return A pointer to an internal string that contains the IP address of
710 * the connected server in the form "XXX.YYY.ZZZ.AAA" IP notation.
711 * This string should not be modified or trusted to stay valid after
712 * deletion for the @p svr object. If no IP is known NULL is returned.
713 * @ingroup Ecore_Ipc_Server_Group
716 ecore_ipc_server_ip_get(Ecore_Ipc_Server *svr)
718 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
720 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
721 "ecore_ipc_server_ip_get");
724 return ecore_con_server_ip_get(svr->server);
728 * Flushes all pending data to the given server. Will return when done.
730 * @param svr The given server.
731 * @ingroup Ecore_Ipc_Server_Group
734 ecore_ipc_server_flush(Ecore_Ipc_Server *svr)
736 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
738 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
739 "ecore_ipc_server_server_flush");
742 ecore_con_server_flush(svr->server);
745 #define CLENC(_member) \
746 d = _ecore_ipc_dlt_int(msg._member, cl->prev.o._member, &md); \
751 dd = (unsigned char *)&v; \
754 *(dat + s + 0) = dd[0]; \
755 *(dat + s + 1) = dd[1]; \
756 *(dat + s + 2) = dd[2]; \
757 *(dat + s + 3) = dd[3]; \
760 else if (md >= DLT_ADD16) \
764 dd = (unsigned char *)&v; \
767 *(dat + s + 0) = dd[0]; \
768 *(dat + s + 1) = dd[1]; \
771 else if (md >= DLT_ADD8) \
773 *(dat + s) = (unsigned char)d; \
778 * @defgroup Ecore_IPC_Client_Group IPC Client Functions
780 * Functions that deal with IPC client objects.
784 * Sends a message to the given IPC client.
785 * @param cl The given IPC client.
786 * @param major Major opcode of the message.
787 * @param minor Minor opcode of the message.
788 * @param ref Reference number of the message.
789 * @param ref_to Reference number of the message this message refers to.
790 * @param response Requires response.
791 * @param data The data to send as part of the message.
792 * @param size Length of the data, in bytes, to send.
793 * @return The number of bytes sent. @c 0 will be returned if there is
795 * @ingroup Ecore_IPC_Client_Group
796 * @todo This function needs to become an IPC message.
797 * @todo Make sure ref_to and response parameters are described correctly.
800 ecore_ipc_client_send(Ecore_Ipc_Client *cl, int major, int minor, int ref, int ref_to, int response, const void *data, int size)
802 Ecore_Ipc_Msg_Head msg;
804 int *head, md = 0, d, s;
805 unsigned char dat[sizeof(Ecore_Ipc_Msg_Head)];
807 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
809 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
810 "ecore_ipc_client_send");
813 if (size < 0) size = 0;
818 msg.response = response;
825 *head |= md << (4 * 1);
827 *head |= md << (4 * 2);
829 *head |= md << (4 * 3);
831 *head |= md << (4 * 4);
833 *head |= md << (4 * 5);
834 *head = htonl(*head);
836 ret = ecore_con_client_send(cl->client, dat, s);
837 if (size > 0) ret += ecore_con_client_send(cl->client, data, size);
842 * Retrieves the IPC server that the given IPC client is connected to.
843 * @param cl The given IPC client.
844 * @return The IPC server the IPC client is connected to.
845 * @ingroup Ecore_IPC_Client_Group
847 EAPI Ecore_Ipc_Server *
848 ecore_ipc_client_server_get(Ecore_Ipc_Client *cl)
850 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
852 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
853 "ecore_ipc_client_server_get");
856 return (ecore_con_server_data_get(ecore_con_client_server_get(cl->client)));
860 * Closes the connection and frees memory allocated to the given IPC
862 * @param cl The given client.
863 * @return Data associated with the client.
864 * @ingroup Ecore_IPC_Client_Group
867 ecore_ipc_client_del(Ecore_Ipc_Client *cl)
870 Ecore_Ipc_Server *svr;
872 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
874 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
875 "ecore_ipc_client_del");
881 if (cl->event_count == 0)
883 svr = ecore_con_server_data_get(ecore_con_client_server_get(cl->client));
884 ecore_con_client_unref(cl->client);
885 svr->clients = eina_list_remove(svr->clients, cl);
886 if (cl->buf) free(cl->buf);
887 ECORE_MAGIC_SET(cl, ECORE_MAGIC_NONE);
894 * Sets the IPC data associated with the given IPC client to @p data.
895 * @param cl The given IPC client.
896 * @param data The data to associate with the IPC client.
897 * @ingroup Ecore_IPC_Client_Group
900 ecore_ipc_client_data_set(Ecore_Ipc_Client *cl, const void *data)
902 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
904 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
905 "ecore_ipc_client_data_set");
908 cl->data = (void *)data;
912 * Retrieves the data that has been associated with the given IPC client.
913 * @param cl The given client.
914 * @return The data associated with the IPC client.
915 * @ingroup Ecore_IPC_Client_Group
918 ecore_ipc_client_data_get(Ecore_Ipc_Client *cl)
920 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
922 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
923 "ecore_ipc_client_data_get");
930 * Sets the max data payload size for an Ipc message in bytes
932 * @param cl The given client.
933 * @param size The maximum data payload size in bytes.
934 * @ingroup Ecore_Ipc_Client_Group
937 ecore_ipc_client_data_size_max_set(Ecore_Ipc_Client *cl, int size)
939 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
941 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
942 "ecore_ipc_client_data_size_max_set");
945 cl->max_buf_size = size;
949 * Gets the max data payload size for an Ipc message in bytes
951 * @param cl The given client.
952 * @return The maximum data payload size in bytes on success, @c -1 on failure.
953 * @ingroup Ecore_Ipc_Client_Group
956 ecore_ipc_client_data_size_max_get(Ecore_Ipc_Client *cl)
958 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
960 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
961 "ecore_ipc_client_data_size_max_get");
964 return cl->max_buf_size;
968 * Gets the IP address of a client that has been connected to.
970 * @param cl The given client.
971 * @return A pointer to an internal string that contains the IP address of
972 * the connected server in the form "XXX.YYY.ZZZ.AAA" IP notation.
973 * This string should not be modified or trusted to stay valid after
974 * deletion for the @p cl object. If no IP is known @c NULL is
976 * @ingroup Ecore_Ipc_Client_Group
979 ecore_ipc_client_ip_get(Ecore_Ipc_Client *cl)
981 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
983 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
984 "ecore_ipc_client_ip_get");
987 return ecore_con_client_ip_get(cl->client);
991 * Flushes all pending data to the given client. Will return when done.
993 * @param cl The given client.
994 * @ingroup Ecore_Ipc_Client_Group
997 ecore_ipc_client_flush(Ecore_Ipc_Client *cl)
999 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
1001 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
1002 "ecore_ipc_client_flush");
1005 ecore_con_client_flush(cl->client);
1009 * Returns if SSL support is available
1010 * @return 1 if SSL is available, 0 if it is not.
1011 * @ingroup Ecore_Con_Client_Group
1014 ecore_ipc_ssl_available_get(void)
1016 return ecore_con_ssl_available_get();
1021 _ecore_ipc_event_client_add(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
1023 Ecore_Con_Event_Client_Add *e;
1026 if (!eina_list_data_find(servers, ecore_con_server_data_get(ecore_con_client_server_get(e->client)))) return ECORE_CALLBACK_RENEW;
1027 /* handling code here */
1029 Ecore_Ipc_Client *cl;
1030 Ecore_Ipc_Server *svr;
1032 cl = calloc(1, sizeof(Ecore_Ipc_Client));
1033 if (!cl) return ECORE_CALLBACK_CANCEL;
1034 svr = ecore_con_server_data_get(ecore_con_client_server_get(e->client));
1035 ECORE_MAGIC_SET(cl, ECORE_MAGIC_IPC_CLIENT);
1036 cl->client = e->client;
1037 ecore_con_client_ref(cl->client);
1038 cl->max_buf_size = 32 * 1024;
1039 ecore_con_client_data_set(cl->client, (void *)cl);
1040 svr->clients = eina_list_append(svr->clients, cl);
1041 svr->client_list = eina_list_append(svr->client_list, cl);
1044 Ecore_Ipc_Event_Client_Add *e2;
1046 e2 = calloc(1, sizeof(Ecore_Ipc_Event_Client_Add));
1051 ecore_event_add(ECORE_IPC_EVENT_CLIENT_ADD, e2,
1052 _ecore_ipc_event_client_add_free, NULL);
1056 return ECORE_CALLBACK_CANCEL;
1060 _ecore_ipc_event_client_del(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
1062 Ecore_Con_Event_Client_Del *e;
1065 if (!eina_list_data_find(servers, ecore_con_server_data_get(ecore_con_client_server_get(e->client)))) return ECORE_CALLBACK_RENEW;
1066 /* handling code here */
1068 Ecore_Ipc_Client *cl;
1070 cl = ecore_con_client_data_get(e->client);
1072 Ecore_Ipc_Event_Client_Del *e2;
1073 Ecore_Ipc_Server *svr;
1075 svr = ecore_con_server_data_get(ecore_con_client_server_get(e->client));
1076 svr->client_list = eina_list_remove(svr->client_list, cl);
1079 e2 = calloc(1, sizeof(Ecore_Ipc_Event_Client_Del));
1084 ecore_event_add(ECORE_IPC_EVENT_CLIENT_DEL, e2,
1085 _ecore_ipc_event_client_del_free, NULL);
1090 return ECORE_CALLBACK_CANCEL;
1094 _ecore_ipc_event_server_add(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
1096 Ecore_Con_Event_Server_Add *e;
1099 if (!eina_list_data_find(servers, ecore_con_server_data_get(e->server))) return ECORE_CALLBACK_RENEW;
1100 /* handling code here */
1102 Ecore_Ipc_Server *svr;
1104 svr = ecore_con_server_data_get(e->server);
1105 if (!svr->delete_me)
1107 Ecore_Ipc_Event_Server_Add *e2;
1109 e2 = calloc(1, sizeof(Ecore_Ipc_Event_Server_Add));
1114 ecore_event_add(ECORE_IPC_EVENT_SERVER_ADD, e2,
1115 _ecore_ipc_event_server_add_free, NULL);
1119 return ECORE_CALLBACK_CANCEL;
1123 _ecore_ipc_event_server_del(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
1125 Ecore_Con_Event_Server_Del *e;
1128 if (!eina_list_data_find(servers, ecore_con_server_data_get(e->server))) return ECORE_CALLBACK_RENEW;
1129 /* handling code here */
1131 Ecore_Ipc_Server *svr;
1133 svr = ecore_con_server_data_get(e->server);
1134 if (!svr->delete_me)
1136 Ecore_Ipc_Event_Server_Del *e2;
1138 e2 = calloc(1, sizeof(Ecore_Ipc_Event_Server_Del));
1143 ecore_event_add(ECORE_IPC_EVENT_SERVER_DEL, e2,
1144 _ecore_ipc_event_server_del_free, NULL);
1148 return ECORE_CALLBACK_CANCEL;
1152 md = ((head >> (4 * _n)) & 0xf); \
1153 if (md >= DLT_SET) s += 4; \
1154 else if (md >= DLT_ADD16) s += 2; \
1155 else if (md >= DLT_ADD8) s += 1;
1157 #define CLDEC(_n, _member) \
1158 md = ((head >> (4 * _n)) & 0xf); \
1159 if (md >= DLT_SET) \
1162 unsigned char *dv; \
1163 dv = (unsigned char *)&v; \
1164 dv[0] = *(cl->buf + offset + s + 0); \
1165 dv[1] = *(cl->buf + offset + s + 1); \
1166 dv[2] = *(cl->buf + offset + s + 2); \
1167 dv[3] = *(cl->buf + offset + s + 3); \
1168 d = (int)ntohl(v); \
1171 else if (md >= DLT_ADD16) \
1174 unsigned char *dv; \
1175 dv = (unsigned char *)&v; \
1176 dv[0] = *(cl->buf + offset + s + 0); \
1177 dv[1] = *(cl->buf + offset + s + 1); \
1178 d = (int)ntohs(v); \
1181 else if (md >= DLT_ADD8) \
1184 unsigned char *dv; \
1185 dv = (unsigned char *)&v; \
1186 dv[0] = *(cl->buf + offset + s + 0); \
1190 msg._member = _ecore_ipc_ddlt_int(d, cl->prev.i._member, md);
1193 _ecore_ipc_event_client_data(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
1195 Ecore_Con_Event_Client_Data *e;
1198 if (!eina_list_data_find(servers, ecore_con_server_data_get(ecore_con_client_server_get(e->client)))) return ECORE_CALLBACK_RENEW;
1199 /* handling code here */
1201 Ecore_Ipc_Client *cl;
1202 Ecore_Ipc_Msg_Head msg;
1206 cl = ecore_con_client_data_get(e->client);
1210 cl->buf_size = e->size;
1212 e->data = NULL; /* take it out of the old event */
1216 buf = realloc(cl->buf, cl->buf_size + e->size);
1222 return ECORE_CALLBACK_CANCEL;
1225 memcpy(cl->buf + cl->buf_size, e->data, e->size);
1226 cl->buf_size += e->size;
1228 /* examine header */
1230 if ((cl->buf_size - offset) >= (int)sizeof(int))
1232 int s, md, d = 0, head;
1235 dd = (unsigned char *)&head;
1236 dd[0] = *(cl->buf + offset + 0);
1237 dd[1] = *(cl->buf + offset + 1);
1238 dd[2] = *(cl->buf + offset + 2);
1239 dd[3] = *(cl->buf + offset + 3);
1241 dd = (unsigned char *)&d;
1249 if ((cl->buf_size - offset) < s)
1251 if (offset > 0) goto scroll;
1252 return ECORE_CALLBACK_CANCEL;
1262 if (msg.size < 0) msg.size = 0;
1263 /* there is enough data in the buffer for a full message */
1264 if ((cl->buf_size - offset) >= (s + msg.size))
1266 Ecore_Ipc_Event_Client_Data *e2;
1267 Ecore_Ipc_Server *svr;
1271 svr = ecore_con_server_data_get(ecore_con_client_server_get(cl->client));
1272 max = svr->max_buf_size;
1273 max2 = cl->max_buf_size;
1274 if ((max >= 0) && (max2 >= 0))
1276 if (max2 < max) max = max2;
1280 if (max < 0) max = max2;
1282 if ((max < 0) || (msg.size <= max))
1286 buf = malloc(msg.size);
1287 if (!buf) return ECORE_CALLBACK_CANCEL;
1288 memcpy(buf, cl->buf + offset + s, msg.size);
1292 e2 = calloc(1, sizeof(Ecore_Ipc_Event_Client_Data));
1297 e2->major = msg.major;
1298 e2->minor = msg.minor;
1300 e2->ref_to = msg.ref_to;
1301 e2->response = msg.response;
1302 e2->size = msg.size;
1304 ecore_event_add(ECORE_IPC_EVENT_CLIENT_DATA, e2,
1305 _ecore_ipc_event_client_data_free,
1311 offset += (s + msg.size);
1312 if (cl->buf_size == offset)
1317 return ECORE_CALLBACK_CANCEL;
1326 buf = malloc(cl->buf_size - offset);
1332 return ECORE_CALLBACK_CANCEL;
1334 memcpy(buf, cl->buf + offset, cl->buf_size - offset);
1337 cl->buf_size -= offset;
1340 return ECORE_CALLBACK_CANCEL;
1344 md = ((head >> (4 * _n)) & 0xf); \
1345 if (md >= DLT_SET) s += 4; \
1346 else if (md >= DLT_ADD16) s += 2; \
1347 else if (md >= DLT_ADD8) s += 1;
1349 #define SVDEC(_n, _member) \
1350 md = ((head >> (4 * _n)) & 0xf); \
1351 if (md >= DLT_SET) \
1354 unsigned char *dv; \
1355 dv = (unsigned char *)&v; \
1356 dv[0] = *(svr->buf + offset + s + 0); \
1357 dv[1] = *(svr->buf + offset + s + 1); \
1358 dv[2] = *(svr->buf + offset + s + 2); \
1359 dv[3] = *(svr->buf + offset + s + 3); \
1360 d = (int)ntohl(v); \
1363 else if (md >= DLT_ADD16) \
1366 unsigned char *dv; \
1367 dv = (unsigned char *)&v; \
1368 dv[0] = *(svr->buf + offset + s + 0); \
1369 dv[1] = *(svr->buf + offset + s + 1); \
1370 d = (int)ntohs(v); \
1373 else if (md >= DLT_ADD8) \
1376 unsigned char *dv; \
1377 dv = (unsigned char *)&v; \
1378 dv[0] = *(svr->buf + offset + s + 0); \
1382 msg._member = _ecore_ipc_ddlt_int(d, svr->prev.i._member, md);
1385 _ecore_ipc_event_server_data(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
1387 Ecore_Con_Event_Server_Data *e;
1390 if (!eina_list_data_find(servers, ecore_con_server_data_get(e->server))) return ECORE_CALLBACK_RENEW;
1391 /* handling code here */
1393 Ecore_Ipc_Server *svr;
1394 Ecore_Ipc_Msg_Head msg;
1398 svr = ecore_con_server_data_get(e->server);
1402 svr->buf_size = e->size;
1404 e->data = NULL; /* take it out of the old event */
1408 buf = realloc(svr->buf, svr->buf_size + e->size);
1414 return ECORE_CALLBACK_CANCEL;
1417 memcpy(svr->buf + svr->buf_size, e->data, e->size);
1418 svr->buf_size += e->size;
1420 /* examine header */
1422 if ((svr->buf_size - offset) >= (int)sizeof(int))
1424 int s, md, d = 0, head;
1427 dd = (unsigned char *)&head;
1428 dd[0] = *(svr->buf + offset + 0);
1429 dd[1] = *(svr->buf + offset + 1);
1430 dd[2] = *(svr->buf + offset + 2);
1431 dd[3] = *(svr->buf + offset + 3);
1433 dd = (unsigned char *)&d;
1441 if ((svr->buf_size - offset) < s)
1443 if (offset > 0) goto scroll;
1444 return ECORE_CALLBACK_CANCEL;
1454 if (msg.size < 0) msg.size = 0;
1455 /* there is enough data in the buffer for a full message */
1456 if ((svr->buf_size - offset) >= (s + msg.size))
1458 Ecore_Ipc_Event_Server_Data *e2;
1462 max = svr->max_buf_size;
1463 if ((max < 0) || (msg.size <= max))
1467 buf = malloc(msg.size);
1468 if (!buf) return ECORE_CALLBACK_CANCEL;
1469 memcpy(buf, svr->buf + offset + s, msg.size);
1471 if (!svr->delete_me)
1473 e2 = calloc(1, sizeof(Ecore_Ipc_Event_Server_Data));
1478 e2->major = msg.major;
1479 e2->minor = msg.minor;
1481 e2->ref_to = msg.ref_to;
1482 e2->response = msg.response;
1483 e2->size = msg.size;
1485 ecore_event_add(ECORE_IPC_EVENT_SERVER_DATA, e2,
1486 _ecore_ipc_event_server_data_free,
1492 offset += (s + msg.size);
1493 if (svr->buf_size == offset)
1498 return ECORE_CALLBACK_CANCEL;
1507 buf = malloc(svr->buf_size - offset);
1513 return ECORE_CALLBACK_CANCEL;
1515 memcpy(buf, svr->buf + offset, svr->buf_size - offset);
1518 svr->buf_size -= offset;
1521 return ECORE_CALLBACK_CANCEL;
1525 _ecore_ipc_event_client_add_free(void *data __UNUSED__, void *ev)
1527 Ecore_Ipc_Event_Client_Add *e;
1530 e->client->event_count--;
1531 if ((e->client->event_count == 0) && (e->client->delete_me))
1532 ecore_ipc_client_del(e->client);
1537 _ecore_ipc_event_client_del_free(void *data __UNUSED__, void *ev)
1539 Ecore_Ipc_Event_Client_Del *e;
1542 e->client->event_count--;
1543 if ((e->client->event_count == 0) && (e->client->delete_me))
1544 ecore_ipc_client_del(e->client);
1549 _ecore_ipc_event_client_data_free(void *data __UNUSED__, void *ev)
1551 Ecore_Ipc_Event_Client_Data *e;
1554 e->client->event_count--;
1555 if (e->data) free(e->data);
1556 if ((e->client->event_count == 0) && (e->client->delete_me))
1557 ecore_ipc_client_del(e->client);
1562 _ecore_ipc_event_server_add_free(void *data __UNUSED__, void *ev)
1564 Ecore_Ipc_Event_Server_Add *e;
1567 e->server->event_count--;
1568 if ((e->server->event_count == 0) && (e->server->delete_me))
1569 ecore_ipc_server_del(e->server);
1574 _ecore_ipc_event_server_del_free(void *data __UNUSED__, void *ev)
1576 Ecore_Ipc_Event_Server_Add *e;
1579 e->server->event_count--;
1580 if ((e->server->event_count == 0) && (e->server->delete_me))
1581 ecore_ipc_server_del(e->server);
1586 _ecore_ipc_event_server_data_free(void *data __UNUSED__, void *ev)
1588 Ecore_Ipc_Event_Server_Data *e;
1591 if (e->data) free(e->data);
1592 e->server->event_count--;
1593 if ((e->server->event_count == 0) && (e->server->delete_me))
1594 ecore_ipc_server_del(e->server);