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>
17 #include "ecore_private.h"
18 #include "Ecore_Con.h"
19 #include "ecore_ipc_private.h"
20 #include "Ecore_Ipc.h"
39 int _ecore_ipc_log_dom = -1;
41 /* byte swappers - for dealing with big vs little endian machines */
43 _ecore_ipc_swap_16(unsigned short v)
47 s = (unsigned char *)(&v);
48 t = s[0]; s[0] = s[1]; s[1] = t;
53 _ecore_ipc_swap_32(unsigned int v)
57 s = (unsigned char *)(&v);
58 t = s[0]; s[0] = s[3]; s[3] = t;
59 t = s[1]; s[1] = s[2]; s[2] = t;
63 EAPI unsigned long long
64 _ecore_ipc_swap_64(unsigned long long v)
68 s = (unsigned char *)(&v);
69 t = s[0]; s[0] = s[7]; s[7] = t;
70 t = s[1]; s[1] = s[6]; s[6] = t;
71 t = s[2]; s[2] = s[5]; s[5] = t;
72 t = s[3]; s[3] = s[4]; s[4] = t;
76 static int _ecore_ipc_dlt_int(int out, int prev, int *mode);
77 static int _ecore_ipc_ddlt_int(int in, int prev, int mode);
80 _ecore_ipc_dlt_int(int out, int prev, int *mode)
90 if (out == (int)0xffffffff)
100 if (out == prev << 1)
105 if (out == prev >> 1)
112 if (!(dlt & 0xffffff00))
118 if (!(dlt & 0xffffff00))
124 if (!(dlt & 0x00ffffff))
127 return (dlt >> 24) & 0xff;
130 if (!(dlt & 0x00ffffff))
133 return (dlt >> 24) & 0xff;
137 if (!(dlt & 0xffff0000))
143 if (!(dlt & 0xffff0000))
149 if (!(dlt & 0x0000ffff))
152 return (dlt >> 16) & 0xffff;
155 if (!(dlt & 0x0000ffff))
158 return (dlt >> 16) & 0xffff;
166 _ecore_ipc_ddlt_int(int in, int prev, int mode)
192 return prev + (in << 24);
195 return prev - (in << 24);
204 return prev + (in << 16);
207 return prev - (in << 16);
224 static Eina_Bool _ecore_ipc_event_client_add(void *data, int ev_type, void *ev);
225 static Eina_Bool _ecore_ipc_event_client_del(void *data, int ev_type, void *ev);
226 static Eina_Bool _ecore_ipc_event_server_add(void *data, int ev_type, void *ev);
227 static Eina_Bool _ecore_ipc_event_server_del(void *data, int ev_type, void *ev);
228 static Eina_Bool _ecore_ipc_event_client_data(void *data, int ev_type, void *ev);
229 static Eina_Bool _ecore_ipc_event_server_data(void *data, int ev_type, void *ev);
230 static void _ecore_ipc_event_client_add_free(void *data, void *ev);
231 static void _ecore_ipc_event_client_del_free(void *data, void *ev);
232 static void _ecore_ipc_event_client_data_free(void *data, void *ev);
233 static void _ecore_ipc_event_server_add_free(void *data, void *ev);
234 static void _ecore_ipc_event_server_del_free(void *data, void *ev);
235 static void _ecore_ipc_event_server_data_free(void *data, void *ev);
237 EAPI int ECORE_IPC_EVENT_CLIENT_ADD = 0;
238 EAPI int ECORE_IPC_EVENT_CLIENT_DEL = 0;
239 EAPI int ECORE_IPC_EVENT_SERVER_ADD = 0;
240 EAPI int ECORE_IPC_EVENT_SERVER_DEL = 0;
241 EAPI int ECORE_IPC_EVENT_CLIENT_DATA = 0;
242 EAPI int ECORE_IPC_EVENT_SERVER_DATA = 0;
244 static int _ecore_ipc_init_count = 0;
245 static Eina_List *servers = NULL;
246 static Ecore_Event_Handler *handler[6];
249 * @defgroup Ecore_IPC_Library_Group IPC Library Functions
251 * Functions that set up and shut down the Ecore IPC Library.
255 * Initialises the Ecore IPC library.
256 * @return Number of times the library has been initialised without
258 * @ingroup Ecore_IPC_Library_Group
265 if (++_ecore_ipc_init_count != 1)
266 return _ecore_ipc_init_count;
267 _ecore_ipc_log_dom = eina_log_domain_register("EcoreIpc", ECORE_IPC_DEFAULT_LOG_COLOR);
268 if(_ecore_ipc_log_dom < 0)
270 EINA_LOG_ERR("Impossible to create a log domain for the Ecore IPC module.");
271 return --_ecore_ipc_init_count;
273 if (!ecore_con_init())
274 return --_ecore_ipc_init_count;
276 ECORE_IPC_EVENT_CLIENT_ADD = ecore_event_type_new();
277 ECORE_IPC_EVENT_CLIENT_DEL = ecore_event_type_new();
278 ECORE_IPC_EVENT_SERVER_ADD = ecore_event_type_new();
279 ECORE_IPC_EVENT_SERVER_DEL = ecore_event_type_new();
280 ECORE_IPC_EVENT_CLIENT_DATA = ecore_event_type_new();
281 ECORE_IPC_EVENT_SERVER_DATA = ecore_event_type_new();
283 handler[i++] = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_ADD,
284 _ecore_ipc_event_client_add, NULL);
285 handler[i++] = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DEL,
286 _ecore_ipc_event_client_del, NULL);
287 handler[i++] = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD,
288 _ecore_ipc_event_server_add, NULL);
289 handler[i++] = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DEL,
290 _ecore_ipc_event_server_del, NULL);
291 handler[i++] = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DATA,
292 _ecore_ipc_event_client_data, NULL);
293 handler[i] = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DATA,
294 _ecore_ipc_event_server_data, NULL);
295 return _ecore_ipc_init_count;
299 * Shuts down the Ecore IPC library.
300 * @return Number of times the library has been initialised without being
302 * @ingroup Ecore_IPC_Library_Group
305 ecore_ipc_shutdown(void)
309 if (--_ecore_ipc_init_count != 0)
310 return _ecore_ipc_init_count;
312 while (servers) ecore_ipc_server_del(eina_list_data_get(servers));
314 for (i = 0; i < 6; i++)
315 ecore_event_handler_del(handler[i]);
317 ecore_con_shutdown();
318 eina_log_domain_unregister(_ecore_ipc_log_dom);
319 _ecore_ipc_log_dom = -1;
320 return _ecore_ipc_init_count;
324 * @defgroup Ecore_IPC_Server_Group IPC Server Functions
326 * Functions the deal with IPC server objects.
330 * Creates an IPC server that listens for connections.
332 * For more details about the @p compl_type, @p name and @p port
333 * parameters, see the @ref ecore_con_server_add documentation.
335 * @param compl_type The connection type.
336 * @param name Name to associate with the socket used for connection.
337 * @param port Number to identify with socket used for connection.
338 * @param data Data to associate with the IPC server.
339 * @return New IPC server. If there is an error, @c NULL is returned.
340 * @ingroup Ecore_IPC_Server_Group
341 * @todo Need to add protocol type parameter to this function.
343 EAPI Ecore_Ipc_Server *
344 ecore_ipc_server_add(Ecore_Ipc_Type compl_type, const char *name, int port, const void *data)
346 Ecore_Ipc_Server *svr;
348 Ecore_Con_Type extra = 0;
350 svr = calloc(1, sizeof(Ecore_Ipc_Server));
351 if (!svr) return NULL;
353 type &= ~ECORE_IPC_USE_SSL;
354 if (compl_type & ECORE_IPC_USE_SSL) extra = ECORE_CON_USE_SSL;
357 case ECORE_IPC_LOCAL_USER:
358 svr->server = ecore_con_server_add(ECORE_CON_LOCAL_USER | extra, name, port, svr);
360 case ECORE_IPC_LOCAL_SYSTEM:
361 svr->server = ecore_con_server_add(ECORE_CON_LOCAL_SYSTEM | extra, name, port, svr);
363 case ECORE_IPC_REMOTE_SYSTEM:
364 svr->server = ecore_con_server_add(ECORE_CON_REMOTE_SYSTEM | extra, name, port, svr);
375 svr->max_buf_size = 32 * 1024;
376 svr->data = (void *)data;
377 servers = eina_list_append(servers, svr);
378 ECORE_MAGIC_SET(svr, ECORE_MAGIC_IPC_SERVER);
383 * Creates an IPC server object to represent the IPC server listening
386 * For more details about the @p compl_type, @p name and @p port
387 * parameters, see the @ref ecore_con_server_connect documentation.
389 * @param compl_type The IPC connection type.
390 * @param name Name used to determine which socket to use for the
392 * @param port Number used to identify the socket to use for the
394 * @param data Data to associate with the server.
395 * @return A new IPC server. @c NULL is returned on error.
396 * @ingroup Ecore_IPC_Server_Group
397 * @todo Need to add protocol type parameter.
399 EAPI Ecore_Ipc_Server *
400 ecore_ipc_server_connect(Ecore_Ipc_Type compl_type, char *name, int port, const void *data)
402 Ecore_Ipc_Server *svr;
404 Ecore_Con_Type extra = 0;
406 svr = calloc(1, sizeof(Ecore_Ipc_Server));
407 if (!svr) return NULL;
409 type &= ~ECORE_IPC_USE_SSL;
410 if (compl_type & ECORE_IPC_USE_SSL) extra = ECORE_CON_USE_SSL;
413 case ECORE_IPC_LOCAL_USER:
414 svr->server = ecore_con_server_connect(ECORE_CON_LOCAL_USER | extra, name, port, svr);
416 case ECORE_IPC_LOCAL_SYSTEM:
417 svr->server = ecore_con_server_connect(ECORE_CON_LOCAL_SYSTEM | extra, name, port, svr);
419 case ECORE_IPC_REMOTE_SYSTEM:
420 svr->server = ecore_con_server_connect(ECORE_CON_REMOTE_SYSTEM | extra, name, port, svr);
431 svr->max_buf_size = -1;
432 svr->data = (void *)data;
433 servers = eina_list_append(servers, svr);
434 ECORE_MAGIC_SET(svr, ECORE_MAGIC_IPC_SERVER);
439 * Closes the connection and frees the given IPC server.
440 * @param svr The given IPC server.
441 * @return The data associated with the server when it was created.
442 * @ingroup Ecore_IPC_Server_Group
445 ecore_ipc_server_del(Ecore_Ipc_Server *svr)
449 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
451 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
452 "ecore_ipc_server_del");
455 if (svr->delete_me) return NULL;
460 if (svr->event_count == 0)
462 Ecore_Ipc_Client *cl;
464 EINA_LIST_FREE(svr->clients, cl)
465 ecore_ipc_client_del(cl);
466 ecore_con_server_del(svr->server);
467 servers = eina_list_remove(servers, svr);
469 if (svr->buf) free(svr->buf);
470 ECORE_MAGIC_SET(svr, ECORE_MAGIC_NONE);
477 * Retrieves the data associated with the given IPC server.
478 * @param svr The given IPC server.
479 * @return The associated data.
480 * @ingroup Ecore_IPC_Server_Group
483 ecore_ipc_server_data_get(Ecore_Ipc_Server *svr)
485 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
487 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
488 "ecore_ipc_server_data_get");
495 * Retrieves whether the given IPC server is currently connected.
496 * @param svr The given IPC server.
497 * @return #EINA_TRUE if the server is connected. #EINA_FALSE otherwise.
498 * @ingroup Ecore_IPC_Server_Group
501 ecore_ipc_server_connected_get(Ecore_Ipc_Server *svr)
503 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
505 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
506 "ecore_ipc_server_connected_get");
509 return ecore_con_server_connected_get(svr->server);
513 * Retrieves the list of clients for this server.
514 * @param svr The given IPC server.
515 * @return An Eina_List with the clients.
516 * @ingroup Ecore_IPC_Server_Group
519 ecore_ipc_server_clients_get(Ecore_Ipc_Server *svr)
521 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
523 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
524 "ecore_ipc_server_clients_get");
527 return svr->client_list;
530 #define SVENC(_member) \
531 d = _ecore_ipc_dlt_int(msg._member, svr->prev.o._member, &md); \
536 dd = (unsigned char *)&v; \
539 *(dat + s + 0) = dd[0]; \
540 *(dat + s + 1) = dd[1]; \
541 *(dat + s + 2) = dd[2]; \
542 *(dat + s + 3) = dd[3]; \
545 else if (md >= DLT_ADD16) \
549 dd = (unsigned char *)&v; \
552 *(dat + s + 0) = dd[0]; \
553 *(dat + s + 1) = dd[1]; \
556 else if (md >= DLT_ADD8) \
558 *(dat + s + 0) = (unsigned char)d; \
563 * Sends a message to the given IPC server.
565 * The content of the parameters, excluding the @p svr paramter, is up to
568 * @param svr The given IPC server.
569 * @param major Major opcode of the message.
570 * @param minor Minor opcode of the message.
571 * @param ref Message reference number.
572 * @param ref_to Reference number of the message this message refers to.
573 * @param response Requires response.
574 * @param data The data to send as part of the message.
575 * @param size Length of the data, in bytes, to send.
576 * @return Number of bytes sent. @c 0 is returned if there is an error.
577 * @ingroup Ecore_IPC_Server_Group
578 * @todo This function needs to become an IPC message.
579 * @todo Fix up the documentation: Make sure what ref_to and response are.
582 ecore_ipc_server_send(Ecore_Ipc_Server *svr, int major, int minor, int ref, int ref_to, int response, const void *data, int size)
584 Ecore_Ipc_Msg_Head msg;
586 int *head, md = 0, d, s;
587 unsigned char dat[sizeof(Ecore_Ipc_Msg_Head)];
589 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
591 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
592 "ecore_ipc_server_send");
595 if (size < 0) size = 0;
600 msg.response = response;
607 *head |= md << (4 * 1);
609 *head |= md << (4 * 2);
611 *head |= md << (4 * 3);
613 *head |= md << (4 * 4);
615 *head |= md << (4 * 5);
616 *head = htonl(*head);
618 ret = ecore_con_server_send(svr->server, dat, s);
619 if (size > 0) ret += ecore_con_server_send(svr->server, data, size);
624 * Sets a limit on the number of clients that can be handled concurrently
625 * by the given server, and a policy on what to do if excess clients try to
627 * Beware that if you set this once ecore is already running, you may
628 * already have pending CLIENT_ADD events in your event queue. Those
629 * clients have already connected and will not be affected by this call.
630 * Only clients subsequently trying to connect will be affected.
631 * @param svr The given server.
632 * @param client_limit The maximum number of clients to handle
633 * concurrently. -1 means unlimited (default). 0
634 * effectively disables the server.
635 * @param reject_excess_clients Set to 1 to automatically disconnect
636 * excess clients as soon as they connect if you are
637 * already handling client_limit clients. Set to 0
638 * (default) to just hold off on the "accept()"
639 * system call until the number of active clients
640 * drops. This causes the kernel to queue up to 4096
641 * connections (or your kernel's limit, whichever is
643 * @ingroup Ecore_Ipc_Server_Group
646 ecore_ipc_server_client_limit_set(Ecore_Ipc_Server *svr, int client_limit, char reject_excess_clients)
648 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
650 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
651 "ecore_ipc_server_client_limit_set");
654 ecore_con_server_client_limit_set(svr->server, client_limit, reject_excess_clients);
658 * Sets the max data payload size for an Ipc message in bytes
660 * @param svr The given server.
661 * @param size The maximum data payload size in bytes.
662 * @ingroup Ecore_Ipc_Server_Group
665 ecore_ipc_server_data_size_max_set(Ecore_Ipc_Server *svr, int size)
667 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
669 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
670 "ecore_ipc_server_data_size_max_set");
673 svr->max_buf_size = size;
677 * Gets the max data payload size for an Ipc message in bytes
679 * @param svr The given server.
680 * @return The maximum data payload in bytes.
681 * @ingroup Ecore_Ipc_Server_Group
684 ecore_ipc_server_data_size_max_get(Ecore_Ipc_Server *svr)
686 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
688 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
689 "ecore_ipc_server_data_size_max_get");
692 return svr->max_buf_size;
696 * Gets the IP address of a server that has been connected to.
698 * @param svr The given server.
699 * @return A pointer to an internal string that contains the IP address of
700 * the connected server in the form "XXX.YYY.ZZZ.AAA" IP notation.
701 * This string should not be modified or trusted to stay valid after
702 * deletion for the @p svr object. If no IP is known NULL is returned.
703 * @ingroup Ecore_Ipc_Server_Group
706 ecore_ipc_server_ip_get(Ecore_Ipc_Server *svr)
708 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
710 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
711 "ecore_ipc_server_ip_get");
714 return ecore_con_server_ip_get(svr->server);
718 * Flushes all pending data to the given server. Will return when done.
720 * @param svr The given server.
721 * @ingroup Ecore_Ipc_Server_Group
724 ecore_ipc_server_flush(Ecore_Ipc_Server *svr)
726 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
728 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
729 "ecore_ipc_server_server_flush");
732 ecore_con_server_flush(svr->server);
735 #define CLENC(_member) \
736 d = _ecore_ipc_dlt_int(msg._member, cl->prev.o._member, &md); \
741 dd = (unsigned char *)&v; \
744 *(dat + s + 0) = dd[0]; \
745 *(dat + s + 1) = dd[1]; \
746 *(dat + s + 2) = dd[2]; \
747 *(dat + s + 3) = dd[3]; \
750 else if (md >= DLT_ADD16) \
754 dd = (unsigned char *)&v; \
757 *(dat + s + 0) = dd[0]; \
758 *(dat + s + 1) = dd[1]; \
761 else if (md >= DLT_ADD8) \
763 *(dat + s) = (unsigned char)d; \
768 * @defgroup Ecore_IPC_Client_Group IPC Client Functions
770 * Functions that deal with IPC client objects.
774 * Sends a message to the given IPC client.
775 * @param cl The given IPC client.
776 * @param major Major opcode of the message.
777 * @param minor Minor opcode of the message.
778 * @param ref Reference number of the message.
779 * @param ref_to Reference number of the message this message refers to.
780 * @param response Requires response.
781 * @param data The data to send as part of the message.
782 * @param size Length of the data, in bytes, to send.
783 * @return The number of bytes sent. @c 0 will be returned if there is
785 * @ingroup Ecore_IPC_Client_Group
786 * @todo This function needs to become an IPC message.
787 * @todo Make sure ref_to and response parameters are described correctly.
790 ecore_ipc_client_send(Ecore_Ipc_Client *cl, int major, int minor, int ref, int ref_to, int response, const void *data, int size)
792 Ecore_Ipc_Msg_Head msg;
794 int *head, md = 0, d, s;
795 unsigned char dat[sizeof(Ecore_Ipc_Msg_Head)];
797 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
799 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
800 "ecore_ipc_client_send");
803 if (size < 0) size = 0;
808 msg.response = response;
815 *head |= md << (4 * 1);
817 *head |= md << (4 * 2);
819 *head |= md << (4 * 3);
821 *head |= md << (4 * 4);
823 *head |= md << (4 * 5);
824 *head = htonl(*head);
826 ret = ecore_con_client_send(cl->client, dat, s);
827 if (size > 0) ret += ecore_con_client_send(cl->client, data, size);
832 * Retrieves the IPC server that the given IPC client is connected to.
833 * @param cl The given IPC client.
834 * @return The IPC server the IPC client is connected to.
835 * @ingroup Ecore_IPC_Client_Group
837 EAPI Ecore_Ipc_Server *
838 ecore_ipc_client_server_get(Ecore_Ipc_Client *cl)
840 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
842 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
843 "ecore_ipc_client_server_get");
846 return (ecore_con_server_data_get(ecore_con_client_server_get(cl->client)));
850 * Closes the connection and frees memory allocated to the given IPC
852 * @param cl The given client.
853 * @return Data associated with the client.
854 * @ingroup Ecore_IPC_Client_Group
857 ecore_ipc_client_del(Ecore_Ipc_Client *cl)
860 Ecore_Ipc_Server *svr;
862 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
864 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
865 "ecore_ipc_client_del");
871 if (cl->event_count == 0)
873 svr = ecore_con_server_data_get(ecore_con_client_server_get(cl->client));
874 ecore_con_client_del(cl->client);
875 svr->clients = eina_list_remove(svr->clients, cl);
876 if (cl->buf) free(cl->buf);
877 ECORE_MAGIC_SET(cl, ECORE_MAGIC_NONE);
884 * Sets the IPC data associated with the given IPC client to @p data.
885 * @param cl The given IPC client.
886 * @param data The data to associate with the IPC client.
887 * @ingroup Ecore_IPC_Client_Group
890 ecore_ipc_client_data_set(Ecore_Ipc_Client *cl, const void *data)
892 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
894 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
895 "ecore_ipc_client_data_set");
898 cl->data = (void *)data;
902 * Retrieves the data that has been associated with the given IPC client.
903 * @param cl The given client.
904 * @return The data associated with the IPC client.
905 * @ingroup Ecore_IPC_Client_Group
908 ecore_ipc_client_data_get(Ecore_Ipc_Client *cl)
910 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
912 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
913 "ecore_ipc_client_data_get");
920 * Sets the max data payload size for an Ipc message in bytes
922 * @param client The given client.
923 * @param size The maximum data payload size in bytes.
924 * @ingroup Ecore_Ipc_Client_Group
927 ecore_ipc_client_data_size_max_set(Ecore_Ipc_Client *cl, int size)
929 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
931 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
932 "ecore_ipc_client_data_size_max_set");
935 cl->max_buf_size = size;
939 * Sets the max data payload size for an Ipc message in bytes
941 * @param cl The given client.
942 * @param size The maximum data payload size in bytes.
943 * @ingroup Ecore_Ipc_Client_Group
946 ecore_ipc_client_data_size_max_get(Ecore_Ipc_Client *cl)
948 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
950 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
951 "ecore_ipc_client_data_size_max_get");
954 return cl->max_buf_size;
958 * Gets the IP address of a client that has been connected to.
960 * @param cl The given client.
961 * @return A pointer to an internal string that contains the IP address of
962 * the connected server in the form "XXX.YYY.ZZZ.AAA" IP notation.
963 * This string should not be modified or trusted to stay valid after
964 * deletion for the @p cl object. If no IP is known NULL is returned.
965 * @ingroup Ecore_Ipc_Client_Group
968 ecore_ipc_client_ip_get(Ecore_Ipc_Client *cl)
970 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
972 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
973 "ecore_ipc_client_ip_get");
976 return ecore_con_client_ip_get(cl->client);
980 * Flushes all pending data to the given client. Will return when done.
982 * @param cl The given client.
983 * @ingroup Ecore_Ipc_Client_Group
986 ecore_ipc_client_flush(Ecore_Ipc_Client *cl)
988 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
990 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
991 "ecore_ipc_client_flush");
994 ecore_con_client_flush(cl->client);
998 * Returns if SSL support is available
999 * @return 1 if SSL is available, 0 if it is not.
1000 * @ingroup Ecore_Con_Client_Group
1003 ecore_ipc_ssl_available_get(void)
1005 return ecore_con_ssl_available_get();
1010 _ecore_ipc_event_client_add(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
1012 Ecore_Con_Event_Client_Add *e;
1015 if (!eina_list_data_find(servers, ecore_con_server_data_get(ecore_con_client_server_get(e->client)))) return ECORE_CALLBACK_RENEW;
1016 /* handling code here */
1018 Ecore_Ipc_Client *cl;
1019 Ecore_Ipc_Server *svr;
1021 cl = calloc(1, sizeof(Ecore_Ipc_Client));
1022 if (!cl) return ECORE_CALLBACK_CANCEL;
1023 svr = ecore_con_server_data_get(ecore_con_client_server_get(e->client));
1024 ECORE_MAGIC_SET(cl, ECORE_MAGIC_IPC_CLIENT);
1025 cl->client = e->client;
1026 cl->max_buf_size = 32 * 1024;
1027 ecore_con_client_data_set(cl->client, (void *)cl);
1028 svr->clients = eina_list_append(svr->clients, cl);
1029 svr->client_list = eina_list_append(svr->client_list, cl);
1032 Ecore_Ipc_Event_Client_Add *e2;
1034 e2 = calloc(1, sizeof(Ecore_Ipc_Event_Client_Add));
1039 ecore_event_add(ECORE_IPC_EVENT_CLIENT_ADD, e2,
1040 _ecore_ipc_event_client_add_free, NULL);
1044 return ECORE_CALLBACK_CANCEL;
1048 _ecore_ipc_event_client_del(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
1050 Ecore_Con_Event_Client_Del *e;
1053 if (!eina_list_data_find(servers, ecore_con_server_data_get(ecore_con_client_server_get(e->client)))) return ECORE_CALLBACK_RENEW;
1054 /* handling code here */
1056 Ecore_Ipc_Client *cl;
1058 cl = ecore_con_client_data_get(e->client);
1060 Ecore_Ipc_Event_Client_Del *e2;
1061 Ecore_Ipc_Server *svr;
1063 svr = ecore_con_server_data_get(ecore_con_client_server_get(e->client));
1064 svr->client_list = eina_list_remove(svr->client_list, cl);
1067 e2 = calloc(1, sizeof(Ecore_Ipc_Event_Client_Del));
1072 ecore_event_add(ECORE_IPC_EVENT_CLIENT_DEL, e2,
1073 _ecore_ipc_event_client_del_free, NULL);
1078 return ECORE_CALLBACK_CANCEL;
1082 _ecore_ipc_event_server_add(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
1084 Ecore_Con_Event_Server_Add *e;
1087 if (!eina_list_data_find(servers, ecore_con_server_data_get(e->server))) return ECORE_CALLBACK_RENEW;
1088 /* handling code here */
1090 Ecore_Ipc_Server *svr;
1092 svr = ecore_con_server_data_get(e->server);
1093 if (!svr->delete_me)
1095 Ecore_Ipc_Event_Server_Add *e2;
1097 e2 = calloc(1, sizeof(Ecore_Ipc_Event_Server_Add));
1102 ecore_event_add(ECORE_IPC_EVENT_SERVER_ADD, e2,
1103 _ecore_ipc_event_server_add_free, NULL);
1107 return ECORE_CALLBACK_CANCEL;
1111 _ecore_ipc_event_server_del(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
1113 Ecore_Con_Event_Server_Del *e;
1116 if (!eina_list_data_find(servers, ecore_con_server_data_get(e->server))) return ECORE_CALLBACK_RENEW;
1117 /* handling code here */
1119 Ecore_Ipc_Server *svr;
1121 svr = ecore_con_server_data_get(e->server);
1122 if (!svr->delete_me)
1124 Ecore_Ipc_Event_Server_Del *e2;
1126 e2 = calloc(1, sizeof(Ecore_Ipc_Event_Server_Del));
1131 ecore_event_add(ECORE_IPC_EVENT_SERVER_DEL, e2,
1132 _ecore_ipc_event_server_del_free, NULL);
1136 return ECORE_CALLBACK_CANCEL;
1140 md = ((head >> (4 * _n)) & 0xf); \
1141 if (md >= DLT_SET) s += 4; \
1142 else if (md >= DLT_ADD16) s += 2; \
1143 else if (md >= DLT_ADD8) s += 1;
1145 #define CLDEC(_n, _member) \
1146 md = ((head >> (4 * _n)) & 0xf); \
1147 if (md >= DLT_SET) \
1150 unsigned char *dv; \
1151 dv = (unsigned char *)&v; \
1152 dv[0] = *(cl->buf + offset + s + 0); \
1153 dv[1] = *(cl->buf + offset + s + 1); \
1154 dv[2] = *(cl->buf + offset + s + 2); \
1155 dv[3] = *(cl->buf + offset + s + 3); \
1156 d = (int)ntohl(v); \
1159 else if (md >= DLT_ADD16) \
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 d = (int)ntohs(v); \
1169 else if (md >= DLT_ADD8) \
1172 unsigned char *dv; \
1173 dv = (unsigned char *)&v; \
1174 dv[0] = *(cl->buf + offset + s + 0); \
1178 msg._member = _ecore_ipc_ddlt_int(d, cl->prev.i._member, md);
1181 _ecore_ipc_event_client_data(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
1183 Ecore_Con_Event_Client_Data *e;
1186 if (!eina_list_data_find(servers, ecore_con_server_data_get(ecore_con_client_server_get(e->client)))) return ECORE_CALLBACK_RENEW;
1187 /* handling code here */
1189 Ecore_Ipc_Client *cl;
1190 Ecore_Ipc_Msg_Head msg;
1194 cl = ecore_con_client_data_get(e->client);
1198 cl->buf_size = e->size;
1200 e->data = NULL; /* take it out of the old event */
1204 buf = realloc(cl->buf, cl->buf_size + e->size);
1210 return ECORE_CALLBACK_CANCEL;
1213 memcpy(cl->buf + cl->buf_size, e->data, e->size);
1214 cl->buf_size += e->size;
1216 /* examine header */
1218 if ((cl->buf_size - offset) >= (int)sizeof(int))
1220 int s, md, d = 0, head;
1223 dd = (unsigned char *)&head;
1224 dd[0] = *(cl->buf + offset + 0);
1225 dd[1] = *(cl->buf + offset + 1);
1226 dd[2] = *(cl->buf + offset + 2);
1227 dd[3] = *(cl->buf + offset + 3);
1229 dd = (unsigned char *)&d;
1237 if ((cl->buf_size - offset) < s)
1239 if (offset > 0) goto scroll;
1240 return ECORE_CALLBACK_CANCEL;
1250 if (msg.size < 0) msg.size = 0;
1251 /* there is enough data in the buffer for a full message */
1252 if ((cl->buf_size - offset) >= (s + msg.size))
1254 Ecore_Ipc_Event_Client_Data *e2;
1255 Ecore_Ipc_Server *svr;
1259 svr = ecore_con_server_data_get(ecore_con_client_server_get(cl->client));
1260 max = svr->max_buf_size;
1261 max2 = cl->max_buf_size;
1262 if ((max >= 0) && (max2 >= 0))
1264 if (max2 < max) max = max2;
1268 if (max < 0) max = max2;
1270 if ((max < 0) || (msg.size <= max))
1274 buf = malloc(msg.size);
1275 if (!buf) return ECORE_CALLBACK_CANCEL;
1276 memcpy(buf, cl->buf + offset + s, msg.size);
1280 e2 = calloc(1, sizeof(Ecore_Ipc_Event_Client_Data));
1285 e2->major = msg.major;
1286 e2->minor = msg.minor;
1288 e2->ref_to = msg.ref_to;
1289 e2->response = msg.response;
1290 e2->size = msg.size;
1292 ecore_event_add(ECORE_IPC_EVENT_CLIENT_DATA, e2,
1293 _ecore_ipc_event_client_data_free,
1299 offset += (s + msg.size);
1300 if (cl->buf_size == offset)
1305 return ECORE_CALLBACK_CANCEL;
1314 buf = malloc(cl->buf_size - offset);
1320 return ECORE_CALLBACK_CANCEL;
1322 memcpy(buf, cl->buf + offset, cl->buf_size - offset);
1325 cl->buf_size -= offset;
1328 return ECORE_CALLBACK_CANCEL;
1332 md = ((head >> (4 * _n)) & 0xf); \
1333 if (md >= DLT_SET) s += 4; \
1334 else if (md >= DLT_ADD16) s += 2; \
1335 else if (md >= DLT_ADD8) s += 1;
1337 #define SVDEC(_n, _member) \
1338 md = ((head >> (4 * _n)) & 0xf); \
1339 if (md >= DLT_SET) \
1342 unsigned char *dv; \
1343 dv = (unsigned char *)&v; \
1344 dv[0] = *(svr->buf + offset + s + 0); \
1345 dv[1] = *(svr->buf + offset + s + 1); \
1346 dv[2] = *(svr->buf + offset + s + 2); \
1347 dv[3] = *(svr->buf + offset + s + 3); \
1348 d = (int)ntohl(v); \
1351 else if (md >= DLT_ADD16) \
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 d = (int)ntohs(v); \
1361 else if (md >= DLT_ADD8) \
1364 unsigned char *dv; \
1365 dv = (unsigned char *)&v; \
1366 dv[0] = *(svr->buf + offset + s + 0); \
1370 msg._member = _ecore_ipc_ddlt_int(d, svr->prev.i._member, md);
1373 _ecore_ipc_event_server_data(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
1375 Ecore_Con_Event_Server_Data *e;
1378 if (!eina_list_data_find(servers, ecore_con_server_data_get(e->server))) return ECORE_CALLBACK_RENEW;
1379 /* handling code here */
1381 Ecore_Ipc_Server *svr;
1382 Ecore_Ipc_Msg_Head msg;
1386 svr = ecore_con_server_data_get(e->server);
1390 svr->buf_size = e->size;
1392 e->data = NULL; /* take it out of the old event */
1396 buf = realloc(svr->buf, svr->buf_size + e->size);
1402 return ECORE_CALLBACK_CANCEL;
1405 memcpy(svr->buf + svr->buf_size, e->data, e->size);
1406 svr->buf_size += e->size;
1408 /* examine header */
1410 if ((svr->buf_size - offset) >= (int)sizeof(int))
1412 int s, md, d = 0, head;
1415 dd = (unsigned char *)&head;
1416 dd[0] = *(svr->buf + offset + 0);
1417 dd[1] = *(svr->buf + offset + 1);
1418 dd[2] = *(svr->buf + offset + 2);
1419 dd[3] = *(svr->buf + offset + 3);
1421 dd = (unsigned char *)&d;
1429 if ((svr->buf_size - offset) < s)
1431 if (offset > 0) goto scroll;
1432 return ECORE_CALLBACK_CANCEL;
1442 if (msg.size < 0) msg.size = 0;
1443 /* there is enough data in the buffer for a full message */
1444 if ((svr->buf_size - offset) >= (s + msg.size))
1446 Ecore_Ipc_Event_Server_Data *e2;
1450 max = svr->max_buf_size;
1451 if ((max < 0) || (msg.size <= max))
1455 buf = malloc(msg.size);
1456 if (!buf) return ECORE_CALLBACK_CANCEL;
1457 memcpy(buf, svr->buf + offset + s, msg.size);
1459 if (!svr->delete_me)
1461 e2 = calloc(1, sizeof(Ecore_Ipc_Event_Server_Data));
1466 e2->major = msg.major;
1467 e2->minor = msg.minor;
1469 e2->ref_to = msg.ref_to;
1470 e2->response = msg.response;
1471 e2->size = msg.size;
1473 ecore_event_add(ECORE_IPC_EVENT_SERVER_DATA, e2,
1474 _ecore_ipc_event_server_data_free,
1480 offset += (s + msg.size);
1481 if (svr->buf_size == offset)
1486 return ECORE_CALLBACK_CANCEL;
1495 buf = malloc(svr->buf_size - offset);
1501 return ECORE_CALLBACK_CANCEL;
1503 memcpy(buf, svr->buf + offset, svr->buf_size - offset);
1506 svr->buf_size -= offset;
1509 return ECORE_CALLBACK_CANCEL;
1513 _ecore_ipc_event_client_add_free(void *data __UNUSED__, void *ev)
1515 Ecore_Ipc_Event_Client_Add *e;
1518 e->client->event_count--;
1519 if ((e->client->event_count == 0) && (e->client->delete_me))
1520 ecore_ipc_client_del(e->client);
1525 _ecore_ipc_event_client_del_free(void *data __UNUSED__, void *ev)
1527 Ecore_Ipc_Event_Client_Del *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_data_free(void *data __UNUSED__, void *ev)
1539 Ecore_Ipc_Event_Client_Data *e;
1542 e->client->event_count--;
1543 if (e->data) free(e->data);
1544 if ((e->client->event_count == 0) && (e->client->delete_me))
1545 ecore_ipc_client_del(e->client);
1550 _ecore_ipc_event_server_add_free(void *data __UNUSED__, void *ev)
1552 Ecore_Ipc_Event_Server_Add *e;
1555 e->server->event_count--;
1556 if ((e->server->event_count == 0) && (e->server->delete_me))
1557 ecore_ipc_server_del(e->server);
1562 _ecore_ipc_event_server_del_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_data_free(void *data __UNUSED__, void *ev)
1576 Ecore_Ipc_Event_Server_Data *e;
1579 if (e->data) free(e->data);
1580 e->server->event_count--;
1581 if ((e->server->event_count == 0) && (e->server->delete_me))
1582 ecore_ipc_server_del(e->server);