4 #include "ecore_private.h"
5 #include "ecore_ipc_private.h"
8 #ifdef HAVE_NETINET_IN_H
9 # include <netinet/in.h>
11 #ifdef HAVE_WINSOCK2_H
12 # include <winsock2.h>
32 /* byte swappers - for dealing with big vs little endian machines */
34 _ecore_ipc_swap_16(unsigned short v)
38 s = (unsigned char *)(&v);
39 t = s[0]; s[0] = s[1]; s[1] = t;
44 _ecore_ipc_swap_32(unsigned int v)
48 s = (unsigned char *)(&v);
49 t = s[0]; s[0] = s[3]; s[3] = t;
50 t = s[1]; s[1] = s[2]; s[2] = t;
54 EAPI unsigned long long
55 _ecore_ipc_swap_64(unsigned long long v)
59 s = (unsigned char *)(&v);
60 t = s[0]; s[0] = s[7]; s[7] = t;
61 t = s[1]; s[1] = s[6]; s[6] = t;
62 t = s[2]; s[2] = s[5]; s[5] = t;
63 t = s[3]; s[3] = s[4]; s[4] = t;
67 static int _ecore_ipc_dlt_int(int out, int prev, int *mode);
68 static int _ecore_ipc_ddlt_int(int in, int prev, int mode);
71 _ecore_ipc_dlt_int(int out, int prev, int *mode)
81 if (out == (int)0xffffffff)
103 if (!(dlt & 0xffffff00))
109 if (!(dlt & 0xffffff00))
115 if (!(dlt & 0x00ffffff))
118 return (dlt >> 24) & 0xff;
121 if (!(dlt & 0x00ffffff))
124 return (dlt >> 24) & 0xff;
128 if (!(dlt & 0xffff0000))
134 if (!(dlt & 0xffff0000))
140 if (!(dlt & 0x0000ffff))
143 return (dlt >> 16) & 0xffff;
146 if (!(dlt & 0x0000ffff))
149 return (dlt >> 16) & 0xffff;
157 _ecore_ipc_ddlt_int(int in, int prev, int mode)
183 return prev + (in << 24);
186 return prev - (in << 24);
195 return prev + (in << 16);
198 return prev - (in << 16);
215 static int _ecore_ipc_event_client_add(void *data, int ev_type, void *ev);
216 static int _ecore_ipc_event_client_del(void *data, int ev_type, void *ev);
217 static int _ecore_ipc_event_server_add(void *data, int ev_type, void *ev);
218 static int _ecore_ipc_event_server_del(void *data, int ev_type, void *ev);
219 static int _ecore_ipc_event_client_data(void *data, int ev_type, void *ev);
220 static int _ecore_ipc_event_server_data(void *data, int ev_type, void *ev);
221 static void _ecore_ipc_event_client_add_free(void *data, void *ev);
222 static void _ecore_ipc_event_client_del_free(void *data, void *ev);
223 static void _ecore_ipc_event_client_data_free(void *data, void *ev);
224 static void _ecore_ipc_event_server_add_free(void *data, void *ev);
225 static void _ecore_ipc_event_server_del_free(void *data, void *ev);
226 static void _ecore_ipc_event_server_data_free(void *data, void *ev);
228 EAPI int ECORE_IPC_EVENT_CLIENT_ADD = 0;
229 EAPI int ECORE_IPC_EVENT_CLIENT_DEL = 0;
230 EAPI int ECORE_IPC_EVENT_SERVER_ADD = 0;
231 EAPI int ECORE_IPC_EVENT_SERVER_DEL = 0;
232 EAPI int ECORE_IPC_EVENT_CLIENT_DATA = 0;
233 EAPI int ECORE_IPC_EVENT_SERVER_DATA = 0;
235 static int init_count = 0;
236 static Ecore_Ipc_Server *servers = NULL;
237 static Ecore_Event_Handler *handler[6];
240 * @defgroup Ecore_IPC_Library_Group IPC Library Functions
242 * Functions that set up and shut down the Ecore IPC Library.
246 * Initialises the Ecore IPC library.
247 * @return Number of times the library has been initialised without
249 * @ingroup Ecore_IPC_Library_Group
256 if (++init_count != 1) return init_count;
260 ECORE_IPC_EVENT_CLIENT_ADD = ecore_event_type_new();
261 ECORE_IPC_EVENT_CLIENT_DEL = ecore_event_type_new();
262 ECORE_IPC_EVENT_SERVER_ADD = ecore_event_type_new();
263 ECORE_IPC_EVENT_SERVER_DEL = ecore_event_type_new();
264 ECORE_IPC_EVENT_CLIENT_DATA = ecore_event_type_new();
265 ECORE_IPC_EVENT_SERVER_DATA = ecore_event_type_new();
267 handler[i++] = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_ADD,
268 _ecore_ipc_event_client_add, NULL);
269 handler[i++] = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DEL,
270 _ecore_ipc_event_client_del, NULL);
271 handler[i++] = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD,
272 _ecore_ipc_event_server_add, NULL);
273 handler[i++] = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DEL,
274 _ecore_ipc_event_server_del, NULL);
275 handler[i++] = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DATA,
276 _ecore_ipc_event_client_data, NULL);
277 handler[i++] = ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DATA,
278 _ecore_ipc_event_server_data, NULL);
283 * Shuts down the Ecore IPC library.
284 * @return Number of times the library has been initialised without being
286 * @ingroup Ecore_IPC_Library_Group
289 ecore_ipc_shutdown(void)
293 if (--init_count != 0) return init_count;
295 while (servers) ecore_ipc_server_del(servers);
297 for (i = 0; i < 6; i++)
298 ecore_event_handler_del(handler[i]);
300 ecore_con_shutdown();
306 * @defgroup Ecore_IPC_Server_Group IPC Server Functions
308 * Functions the deal with IPC server objects.
312 * Creates an IPC server that listens for connections.
314 * For more details about the @p compl_type, @p name and @p port
315 * parameters, see the @ref ecore_con_server_add documentation.
317 * @param compl_type The connection type.
318 * @param name Name to associate with the socket used for connection.
319 * @param port Number to identify with socket used for connection.
320 * @param data Data to associate with the IPC server.
321 * @return New IPC server. If there is an error, @c NULL is returned.
322 * @ingroup Ecore_IPC_Server_Group
323 * @todo Need to add protocol type parameter to this function.
325 EAPI Ecore_Ipc_Server *
326 ecore_ipc_server_add(Ecore_Ipc_Type compl_type, const char *name, int port, const void *data)
328 Ecore_Ipc_Server *svr;
330 Ecore_Con_Type extra = 0;
332 svr = calloc(1, sizeof(Ecore_Ipc_Server));
333 if (!svr) return NULL;
335 type &= ~ECORE_IPC_USE_SSL;
336 if (compl_type & ECORE_IPC_USE_SSL) extra = ECORE_CON_USE_SSL;
339 case ECORE_IPC_LOCAL_USER:
340 svr->server = ecore_con_server_add(ECORE_CON_LOCAL_USER | extra, name, port, svr);
342 case ECORE_IPC_LOCAL_SYSTEM:
343 svr->server = ecore_con_server_add(ECORE_CON_LOCAL_SYSTEM | extra, name, port, svr);
345 case ECORE_IPC_REMOTE_SYSTEM:
346 svr->server = ecore_con_server_add(ECORE_CON_REMOTE_SYSTEM | extra, name, port, svr);
357 svr->max_buf_size = 32 * 1024;
358 svr->data = (void *)data;
359 svr->client_list = ecore_list_new();
360 ecore_list_init(svr->client_list);
361 servers = _ecore_list2_append(servers, svr);
362 ECORE_MAGIC_SET(svr, ECORE_MAGIC_IPC_SERVER);
367 * Creates an IPC server object to represent the IPC server listening
370 * For more details about the @p compl_type, @p name and @p port
371 * parameters, see the @ref ecore_con_server_connect documentation.
373 * @param compl_type The IPC connection type.
374 * @param name Name used to determine which socket to use for the
376 * @param port Number used to identify the socket to use for the
378 * @param data Data to associate with the server.
379 * @return A new IPC server. @c NULL is returned on error.
380 * @ingroup Ecore_IPC_Server_Group
381 * @todo Need to add protocol type parameter.
383 EAPI Ecore_Ipc_Server *
384 ecore_ipc_server_connect(Ecore_Ipc_Type compl_type, char *name, int port, const void *data)
386 Ecore_Ipc_Server *svr;
388 Ecore_Con_Type extra = 0;
390 svr = calloc(1, sizeof(Ecore_Ipc_Server));
391 if (!svr) return NULL;
393 type &= ~ECORE_IPC_USE_SSL;
394 if (compl_type & ECORE_IPC_USE_SSL) extra = ECORE_CON_USE_SSL;
397 case ECORE_IPC_LOCAL_USER:
398 svr->server = ecore_con_server_connect(ECORE_CON_LOCAL_USER | extra, name, port, svr);
400 case ECORE_IPC_LOCAL_SYSTEM:
401 svr->server = ecore_con_server_connect(ECORE_CON_LOCAL_SYSTEM | extra, name, port, svr);
403 case ECORE_IPC_REMOTE_SYSTEM:
404 svr->server = ecore_con_server_connect(ECORE_CON_REMOTE_SYSTEM | extra, name, port, svr);
415 svr->max_buf_size = -1;
416 svr->data = (void *)data;
417 servers = _ecore_list2_append(servers, svr);
418 ECORE_MAGIC_SET(svr, ECORE_MAGIC_IPC_SERVER);
423 * Closes the connection and frees the given IPC server.
424 * @param svr The given IPC server.
425 * @return The data associated with the server when it was created.
426 * @ingroup Ecore_IPC_Server_Group
429 ecore_ipc_server_del(Ecore_Ipc_Server *svr)
433 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
435 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
436 "ecore_ipc_server_del");
442 if (svr->event_count == 0)
445 ecore_ipc_client_del((Ecore_Ipc_Client *)svr->clients);
446 ecore_con_server_del(svr->server);
447 servers = _ecore_list2_remove(servers, svr);
448 if (svr->buf) free(svr->buf);
449 if (svr->client_list) ecore_list_destroy(svr->client_list);
450 ECORE_MAGIC_SET(svr, ECORE_MAGIC_NONE);
457 * Retrieves the data associated with the given IPC server.
458 * @param svr The given IPC server.
459 * @return The associated data.
460 * @ingroup Ecore_IPC_Server_Group
463 ecore_ipc_server_data_get(Ecore_Ipc_Server *svr)
465 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
467 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
468 "ecore_ipc_server_data_get");
475 * Retrieves whether the given IPC server is currently connected.
476 * @param svr The given IPC server.
477 * @return @c 1 if the server is connected. @c 0 otherwise.
478 * @ingroup Ecore_IPC_Server_Group
481 ecore_ipc_server_connected_get(Ecore_Ipc_Server *svr)
483 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
485 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
486 "ecore_ipc_server_connected_get");
489 return ecore_con_server_connected_get(svr->server);
493 * Retrieves the list of clients for this server.
494 * @param svr The given IPC server.
495 * @return An Ecore_List with the clients.
496 * @ingroup Ecore_IPC_Server_Group
499 ecore_ipc_server_clients_get(Ecore_Ipc_Server *svr)
501 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
503 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
504 "ecore_ipc_server_clients_get");
507 return svr->client_list;
510 #define SVENC(_member) \
511 d = _ecore_ipc_dlt_int(msg._member, svr->prev.o._member, &md); \
516 dd = (unsigned char *)&v; \
519 *(dat + s + 0) = dd[0]; \
520 *(dat + s + 1) = dd[1]; \
521 *(dat + s + 2) = dd[2]; \
522 *(dat + s + 3) = dd[3]; \
525 else if (md >= DLT_ADD16) \
529 dd = (unsigned char *)&v; \
532 *(dat + s + 0) = dd[0]; \
533 *(dat + s + 1) = dd[1]; \
536 else if (md >= DLT_ADD8) \
538 *(dat + s + 0) = (unsigned char)d; \
543 * Sends a message to the given IPC server.
545 * The content of the parameters, excluding the @p svr paramter, is up to
548 * @param svr The given IPC server.
549 * @param major Major opcode of the message.
550 * @param minor Minor opcode of the message.
551 * @param ref Message reference number.
552 * @param ref_to Reference number of the message this message refers to.
553 * @param response Requires response.
554 * @param data The data to send as part of the message.
555 * @param size Length of the data, in bytes, to send.
556 * @return Number of bytes sent. @c 0 is returned if there is an error.
557 * @ingroup Ecore_IPC_Server_Group
558 * @todo This function needs to become an IPC message.
559 * @todo Fix up the documentation: Make sure what ref_to and response are.
562 ecore_ipc_server_send(Ecore_Ipc_Server *svr, int major, int minor, int ref, int ref_to, int response, const void *data, int size)
564 Ecore_Ipc_Msg_Head msg;
566 int *head, md = 0, d, s;
567 unsigned char dat[sizeof(Ecore_Ipc_Msg_Head)];
569 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
571 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
572 "ecore_ipc_server_send");
575 if (size < 0) size = 0;
580 msg.response = response;
587 *head |= md << (4 * 1);
589 *head |= md << (4 * 2);
591 *head |= md << (4 * 3);
593 *head |= md << (4 * 4);
595 *head |= md << (4 * 5);
596 *head = htonl(*head);
598 ret = ecore_con_server_send(svr->server, dat, s);
599 if (size > 0) ret += ecore_con_server_send(svr->server, data, size);
604 * Sets a limit on the number of clients that can be handled concurrently
605 * by the given server, and a policy on what to do if excess clients try to
607 * Beware that if you set this once ecore is already running, you may
608 * already have pending CLIENT_ADD events in your event queue. Those
609 * clients have already connected and will not be affected by this call.
610 * Only clients subsequently trying to connect will be affected.
611 * @param svr The given server.
612 * @param client_limit The maximum number of clients to handle
613 * concurrently. -1 means unlimited (default). 0
614 * effectively disables the server.
615 * @param reject_excess_clients Set to 1 to automatically disconnect
616 * excess clients as soon as they connect if you are
617 * already handling client_limit clients. Set to 0
618 * (default) to just hold off on the "accept()"
619 * system call until the number of active clients
620 * drops. This causes the kernel to queue up to 4096
621 * connections (or your kernel's limit, whichever is
623 * @ingroup Ecore_Ipc_Server_Group
626 ecore_ipc_server_client_limit_set(Ecore_Ipc_Server *svr, int client_limit, char reject_excess_clients)
628 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
630 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
631 "ecore_ipc_server_client_limit_set");
634 ecore_con_server_client_limit_set(svr->server, client_limit, reject_excess_clients);
638 * Sets the max data payload size for an Ipc message in bytes
640 * @param svr The given server.
641 * @param size The maximum data payload size in bytes.
642 * @ingroup Ecore_Ipc_Server_Group
645 ecore_ipc_server_data_size_max_set(Ecore_Ipc_Server *svr, int size)
647 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
649 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
650 "ecore_ipc_server_data_size_max_set");
653 svr->max_buf_size = size;
657 * Gets the max data payload size for an Ipc message in bytes
659 * @param svr The given server.
660 * @return The maximum data payload in bytes.
661 * @ingroup Ecore_Ipc_Server_Group
664 ecore_ipc_server_data_size_max_get(Ecore_Ipc_Server *svr)
666 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
668 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
669 "ecore_ipc_server_data_size_max_get");
672 return svr->max_buf_size;
676 * Gets the IP address of a server that has been connected to.
678 * @param svr The given server.
679 * @return A pointer to an internal string that contains the IP address of
680 * the connected server in the form "XXX.YYY.ZZZ.AAA" IP notation.
681 * This string should not be modified or trusted to stay valid after
682 * deletion for the @p svr object. If no IP is known NULL is returned.
683 * @ingroup Ecore_Ipc_Server_Group
686 ecore_ipc_server_ip_get(Ecore_Ipc_Server *svr)
688 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
690 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
691 "ecore_ipc_server_ip_get");
694 return ecore_con_server_ip_get(svr->server);
698 * Flushes all pending data to the given server. Will return when done.
700 * @param svr The given server.
701 * @ingroup Ecore_Ipc_Server_Group
704 ecore_ipc_server_flush(Ecore_Ipc_Server *svr)
706 if (!ECORE_MAGIC_CHECK(svr, ECORE_MAGIC_IPC_SERVER))
708 ECORE_MAGIC_FAIL(svr, ECORE_MAGIC_IPC_SERVER,
709 "ecore_ipc_server_server_flush");
712 ecore_con_server_flush(svr->server);
715 #define CLENC(_member) \
716 d = _ecore_ipc_dlt_int(msg._member, cl->prev.o._member, &md); \
721 dd = (unsigned char *)&v; \
724 *(dat + s + 0) = dd[0]; \
725 *(dat + s + 1) = dd[1]; \
726 *(dat + s + 2) = dd[2]; \
727 *(dat + s + 3) = dd[3]; \
730 else if (md >= DLT_ADD16) \
734 dd = (unsigned char *)&v; \
737 *(dat + s + 0) = dd[0]; \
738 *(dat + s + 1) = dd[1]; \
741 else if (md >= DLT_ADD8) \
743 *(dat + s) = (unsigned char)d; \
748 * @defgroup Ecore_IPC_Client_Group IPC Client Functions
750 * Functions that deal with IPC client objects.
754 * Sends a message to the given IPC client.
755 * @param cl The given IPC client.
756 * @param major Major opcode of the message.
757 * @param minor Minor opcode of the message.
758 * @param ref Reference number of the message.
759 * @param ref_to Reference number of the message this message refers to.
760 * @param response Requires response.
761 * @param data The data to send as part of the message.
762 * @param size Length of the data, in bytes, to send.
763 * @return The number of bytes sent. @c 0 will be returned if there is
765 * @ingroup Ecore_IPC_Client_Group
766 * @todo This function needs to become an IPC message.
767 * @todo Make sure ref_to and response parameters are described correctly.
770 ecore_ipc_client_send(Ecore_Ipc_Client *cl, int major, int minor, int ref, int ref_to, int response, const void *data, int size)
772 Ecore_Ipc_Msg_Head msg;
774 int *head, md = 0, d, s;
775 unsigned char dat[sizeof(Ecore_Ipc_Msg_Head)];
777 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
779 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
780 "ecore_ipc_client_send");
783 if (size < 0) size = 0;
788 msg.response = response;
795 *head |= md << (4 * 1);
797 *head |= md << (4 * 2);
799 *head |= md << (4 * 3);
801 *head |= md << (4 * 4);
803 *head |= md << (4 * 5);
804 *head = htonl(*head);
806 ret = ecore_con_client_send(cl->client, dat, s);
807 if (size > 0) ret += ecore_con_client_send(cl->client, data, size);
812 * Retrieves the IPC server that the given IPC client is connected to.
813 * @param cl The given IPC client.
814 * @return The IPC server the IPC client is connected to.
815 * @ingroup Ecore_IPC_Client_Group
817 EAPI Ecore_Ipc_Server *
818 ecore_ipc_client_server_get(Ecore_Ipc_Client *cl)
820 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
822 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
823 "ecore_ipc_client_server_get");
826 return (ecore_con_server_data_get(ecore_con_client_server_get(cl->client)));
830 * Closes the connection and frees memory allocated to the given IPC
832 * @param cl The given client.
833 * @return Data associated with the client.
834 * @ingroup Ecore_IPC_Client_Group
837 ecore_ipc_client_del(Ecore_Ipc_Client *cl)
840 Ecore_Ipc_Server *svr;
842 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
844 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
845 "ecore_ipc_client_del");
851 if (cl->event_count == 0)
853 svr = ecore_con_server_data_get(ecore_con_client_server_get(cl->client));
854 ecore_con_client_del(cl->client);
855 svr->clients = _ecore_list2_remove(svr->clients, cl);
856 if (cl->buf) free(cl->buf);
857 ECORE_MAGIC_SET(cl, ECORE_MAGIC_NONE);
864 * Sets the IPC data associated with the given IPC client to @p data.
865 * @param cl The given IPC client.
866 * @param data The data to associate with the IPC client.
867 * @ingroup Ecore_IPC_Client_Group
870 ecore_ipc_client_data_set(Ecore_Ipc_Client *cl, const void *data)
872 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
874 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
875 "ecore_ipc_client_data_set");
878 cl->data = (void *)data;
882 * Retrieves the data that has been associated with the given IPC client.
883 * @param cl The given client.
884 * @return The data associated with the IPC client.
885 * @ingroup Ecore_IPC_Client_Group
888 ecore_ipc_client_data_get(Ecore_Ipc_Client *cl)
890 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
892 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
893 "ecore_ipc_client_data_get");
900 * Sets the max data payload size for an Ipc message in bytes
902 * @param client The given client.
903 * @param size The maximum data payload size in bytes.
904 * @ingroup Ecore_Ipc_Client_Group
907 ecore_ipc_client_data_size_max_set(Ecore_Ipc_Client *cl, int size)
909 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
911 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
912 "ecore_ipc_client_data_size_max_set");
915 cl->max_buf_size = size;
919 * Sets the max data payload size for an Ipc message in bytes
921 * @param cl The given client.
922 * @param size The maximum data payload size in bytes.
923 * @ingroup Ecore_Ipc_Client_Group
926 ecore_ipc_client_data_size_max_get(Ecore_Ipc_Client *cl)
928 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
930 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
931 "ecore_ipc_client_data_size_max_get");
934 return cl->max_buf_size;
938 * Gets the IP address of a client that has been connected to.
940 * @param cl The given client.
941 * @return A pointer to an internal string that contains the IP address of
942 * the connected server in the form "XXX.YYY.ZZZ.AAA" IP notation.
943 * This string should not be modified or trusted to stay valid after
944 * deletion for the @p cl object. If no IP is known NULL is returned.
945 * @ingroup Ecore_Ipc_Client_Group
948 ecore_ipc_client_ip_get(Ecore_Ipc_Client *cl)
950 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
952 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
953 "ecore_ipc_client_ip_get");
956 return ecore_con_client_ip_get(cl->client);
960 * Flushes all pending data to the given client. Will return when done.
962 * @param cl The given client.
963 * @ingroup Ecore_Ipc_Client_Group
966 ecore_ipc_client_flush(Ecore_Ipc_Client *cl)
968 if (!ECORE_MAGIC_CHECK(cl, ECORE_MAGIC_IPC_CLIENT))
970 ECORE_MAGIC_FAIL(cl, ECORE_MAGIC_IPC_CLIENT,
971 "ecore_ipc_client_flush");
974 ecore_con_client_flush(cl->client);
978 * Returns if SSL support is available
979 * @return 1 if SSL is available, 0 if it is not.
980 * @ingroup Ecore_Con_Client_Group
983 ecore_ipc_ssl_available_get(void)
985 return ecore_con_ssl_available_get();
990 _ecore_ipc_event_client_add(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
992 Ecore_Con_Event_Client_Add *e;
995 if (!_ecore_list2_find(servers, ecore_con_server_data_get(ecore_con_client_server_get(e->client)))) return 1;
996 /* handling code here */
998 Ecore_Ipc_Client *cl;
999 Ecore_Ipc_Server *svr;
1001 cl = calloc(1, sizeof(Ecore_Ipc_Client));
1003 svr = ecore_con_server_data_get(ecore_con_client_server_get(e->client));
1004 ECORE_MAGIC_SET(cl, ECORE_MAGIC_IPC_CLIENT);
1005 cl->client = e->client;
1006 cl->max_buf_size = 32 * 1024;
1007 ecore_con_client_data_set(cl->client, (void *)cl);
1008 svr->clients = _ecore_list2_append(svr->clients, cl);
1009 ecore_list_append(svr->client_list, cl);
1012 Ecore_Ipc_Event_Client_Add *e2;
1014 e2 = calloc(1, sizeof(Ecore_Ipc_Event_Client_Add));
1019 ecore_event_add(ECORE_IPC_EVENT_CLIENT_ADD, e2,
1020 _ecore_ipc_event_client_add_free, NULL);
1028 _ecore_ipc_event_client_del(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
1030 Ecore_Con_Event_Client_Del *e;
1033 if (!_ecore_list2_find(servers, ecore_con_server_data_get(ecore_con_client_server_get(e->client)))) return 1;
1034 /* handling code here */
1036 Ecore_Ipc_Client *cl;
1038 cl = ecore_con_client_data_get(e->client);
1040 Ecore_Ipc_Event_Client_Del *e2;
1041 Ecore_Ipc_Server *svr;
1043 svr = ecore_con_server_data_get(ecore_con_client_server_get(e->client));
1044 ecore_list_goto(svr->client_list, cl);
1045 ecore_list_remove(svr->client_list);
1046 ecore_list_first_goto(svr->client_list);
1049 e2 = calloc(1, sizeof(Ecore_Ipc_Event_Client_Del));
1054 ecore_event_add(ECORE_IPC_EVENT_CLIENT_DEL, e2,
1055 _ecore_ipc_event_client_del_free, NULL);
1064 _ecore_ipc_event_server_add(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
1066 Ecore_Con_Event_Server_Add *e;
1069 if (!_ecore_list2_find(servers, ecore_con_server_data_get(e->server))) return 1;
1070 /* handling code here */
1072 Ecore_Ipc_Server *svr;
1074 svr = ecore_con_server_data_get(e->server);
1075 if (!svr->delete_me)
1077 Ecore_Ipc_Event_Server_Add *e2;
1079 e2 = calloc(1, sizeof(Ecore_Ipc_Event_Server_Add));
1084 ecore_event_add(ECORE_IPC_EVENT_SERVER_ADD, e2,
1085 _ecore_ipc_event_server_add_free, NULL);
1093 _ecore_ipc_event_server_del(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
1095 Ecore_Con_Event_Server_Del *e;
1098 if (!_ecore_list2_find(servers, ecore_con_server_data_get(e->server))) return 1;
1099 /* handling code here */
1101 Ecore_Ipc_Server *svr;
1103 svr = ecore_con_server_data_get(e->server);
1104 if (!svr->delete_me)
1106 Ecore_Ipc_Event_Server_Del *e2;
1108 e2 = calloc(1, sizeof(Ecore_Ipc_Event_Server_Del));
1113 ecore_event_add(ECORE_IPC_EVENT_SERVER_DEL, e2,
1114 _ecore_ipc_event_server_del_free, NULL);
1122 md = ((head >> (4 * _n)) & 0xf); \
1123 if (md >= DLT_SET) s += 4; \
1124 else if (md >= DLT_ADD16) s += 2; \
1125 else if (md >= DLT_ADD8) s += 1;
1127 #define CLDEC(_n, _member) \
1128 md = ((head >> (4 * _n)) & 0xf); \
1129 if (md >= DLT_SET) \
1132 unsigned char *dv; \
1133 dv = (unsigned char *)&v; \
1134 dv[0] = *(cl->buf + offset + s + 0); \
1135 dv[1] = *(cl->buf + offset + s + 1); \
1136 dv[2] = *(cl->buf + offset + s + 2); \
1137 dv[3] = *(cl->buf + offset + s + 3); \
1138 d = (int)ntohl(v); \
1141 else if (md >= DLT_ADD16) \
1144 unsigned char *dv; \
1145 dv = (unsigned char *)&v; \
1146 dv[0] = *(cl->buf + offset + s + 0); \
1147 dv[1] = *(cl->buf + offset + s + 1); \
1148 d = (int)ntohs(v); \
1151 else if (md >= DLT_ADD8) \
1154 unsigned char *dv; \
1155 dv = (unsigned char *)&v; \
1156 dv[0] = *(cl->buf + offset + s + 0); \
1160 msg._member = _ecore_ipc_ddlt_int(d, cl->prev.i._member, md);
1163 _ecore_ipc_event_client_data(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
1165 Ecore_Con_Event_Client_Data *e;
1168 if (!_ecore_list2_find(servers, ecore_con_server_data_get(ecore_con_client_server_get(e->client)))) return 1;
1169 /* handling code here */
1171 Ecore_Ipc_Client *cl;
1172 Ecore_Ipc_Msg_Head msg;
1176 cl = ecore_con_client_data_get(e->client);
1180 cl->buf_size = e->size;
1182 e->data = NULL; /* take it out of the old event */
1186 buf = realloc(cl->buf, cl->buf_size + e->size);
1195 memcpy(cl->buf + cl->buf_size, e->data, e->size);
1196 cl->buf_size += e->size;
1198 /* examine header */
1200 if ((cl->buf_size - offset) >= (int)sizeof(int))
1202 int s, md, d = 0, head;
1205 dd = (unsigned char *)&head;
1206 dd[0] = *(cl->buf + offset + 0);
1207 dd[1] = *(cl->buf + offset + 1);
1208 dd[2] = *(cl->buf + offset + 2);
1209 dd[3] = *(cl->buf + offset + 3);
1211 dd = (unsigned char *)&d;
1219 if ((cl->buf_size - offset) < s)
1221 if (offset > 0) goto scroll;
1232 if (msg.size < 0) msg.size = 0;
1233 /* there is enough data in the buffer for a full message */
1234 if ((cl->buf_size - offset) >= (s + msg.size))
1236 Ecore_Ipc_Event_Client_Data *e2;
1237 Ecore_Ipc_Server *svr;
1241 svr = ecore_con_server_data_get(ecore_con_client_server_get(cl->client));
1242 max = svr->max_buf_size;
1243 max2 = cl->max_buf_size;
1244 if ((max >= 0) && (max2 >= 0))
1246 if (max2 < max) max = max2;
1250 if (max < 0) max = max2;
1252 if ((max < 0) || (msg.size <= max))
1256 buf = malloc(msg.size);
1258 memcpy(buf, cl->buf + offset + s, msg.size);
1262 e2 = calloc(1, sizeof(Ecore_Ipc_Event_Client_Data));
1267 e2->major = msg.major;
1268 e2->minor = msg.minor;
1270 e2->ref_to = msg.ref_to;
1271 e2->response = msg.response;
1272 e2->size = msg.size;
1274 ecore_event_add(ECORE_IPC_EVENT_CLIENT_DATA, e2,
1275 _ecore_ipc_event_client_data_free,
1281 offset += (s + msg.size);
1282 if (cl->buf_size == offset)
1296 buf = malloc(cl->buf_size - offset);
1304 memcpy(buf, cl->buf + offset, cl->buf_size - offset);
1307 cl->buf_size -= offset;
1314 md = ((head >> (4 * _n)) & 0xf); \
1315 if (md >= DLT_SET) s += 4; \
1316 else if (md >= DLT_ADD16) s += 2; \
1317 else if (md >= DLT_ADD8) s += 1;
1319 #define SVDEC(_n, _member) \
1320 md = ((head >> (4 * _n)) & 0xf); \
1321 if (md >= DLT_SET) \
1324 unsigned char *dv; \
1325 dv = (unsigned char *)&v; \
1326 dv[0] = *(svr->buf + offset + s + 0); \
1327 dv[1] = *(svr->buf + offset + s + 1); \
1328 dv[2] = *(svr->buf + offset + s + 2); \
1329 dv[3] = *(svr->buf + offset + s + 3); \
1330 d = (int)ntohl(v); \
1333 else if (md >= DLT_ADD16) \
1336 unsigned char *dv; \
1337 dv = (unsigned char *)&v; \
1338 dv[0] = *(svr->buf + offset + s + 0); \
1339 dv[1] = *(svr->buf + offset + s + 1); \
1340 d = (int)ntohs(v); \
1343 else if (md >= DLT_ADD8) \
1346 unsigned char *dv; \
1347 dv = (unsigned char *)&v; \
1348 dv[0] = *(svr->buf + offset + s + 0); \
1352 msg._member = _ecore_ipc_ddlt_int(d, svr->prev.i._member, md);
1355 _ecore_ipc_event_server_data(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
1357 Ecore_Con_Event_Server_Data *e;
1360 if (!_ecore_list2_find(servers, ecore_con_server_data_get(e->server))) return 1;
1361 /* handling code here */
1363 Ecore_Ipc_Server *svr;
1364 Ecore_Ipc_Msg_Head msg;
1368 svr = ecore_con_server_data_get(e->server);
1372 svr->buf_size = e->size;
1374 e->data = NULL; /* take it out of the old event */
1378 buf = realloc(svr->buf, svr->buf_size + e->size);
1387 memcpy(svr->buf + svr->buf_size, e->data, e->size);
1388 svr->buf_size += e->size;
1390 /* examine header */
1392 if ((svr->buf_size - offset) >= (int)sizeof(int))
1394 int s, md, d = 0, head;
1397 dd = (unsigned char *)&head;
1398 dd[0] = *(svr->buf + offset + 0);
1399 dd[1] = *(svr->buf + offset + 1);
1400 dd[2] = *(svr->buf + offset + 2);
1401 dd[3] = *(svr->buf + offset + 3);
1403 dd = (unsigned char *)&d;
1411 if ((svr->buf_size - offset) < s)
1413 if (offset > 0) goto scroll;
1424 if (msg.size < 0) msg.size = 0;
1425 /* there is enough data in the buffer for a full message */
1426 if ((svr->buf_size - offset) >= (s + msg.size))
1428 Ecore_Ipc_Event_Server_Data *e2;
1432 max = svr->max_buf_size;
1433 if ((max < 0) || (msg.size <= max))
1437 buf = malloc(msg.size);
1439 memcpy(buf, svr->buf + offset + s, msg.size);
1441 if (!svr->delete_me)
1443 e2 = calloc(1, sizeof(Ecore_Ipc_Event_Server_Data));
1448 e2->major = msg.major;
1449 e2->minor = msg.minor;
1451 e2->ref_to = msg.ref_to;
1452 e2->response = msg.response;
1453 e2->size = msg.size;
1455 ecore_event_add(ECORE_IPC_EVENT_SERVER_DATA, e2,
1456 _ecore_ipc_event_server_data_free,
1462 offset += (s + msg.size);
1463 if (svr->buf_size == offset)
1477 buf = malloc(svr->buf_size - offset);
1485 memcpy(buf, svr->buf + offset, svr->buf_size - offset);
1488 svr->buf_size -= offset;
1495 _ecore_ipc_event_client_add_free(void *data __UNUSED__, void *ev)
1497 Ecore_Ipc_Event_Client_Add *e;
1500 e->client->event_count--;
1501 if ((e->client->event_count == 0) && (e->client->delete_me))
1502 ecore_ipc_client_del(e->client);
1507 _ecore_ipc_event_client_del_free(void *data __UNUSED__, void *ev)
1509 Ecore_Ipc_Event_Client_Del *e;
1512 e->client->event_count--;
1513 if ((e->client->event_count == 0) && (e->client->delete_me))
1514 ecore_ipc_client_del(e->client);
1519 _ecore_ipc_event_client_data_free(void *data __UNUSED__, void *ev)
1521 Ecore_Ipc_Event_Client_Data *e;
1524 e->client->event_count--;
1525 if (e->data) free(e->data);
1526 if ((e->client->event_count == 0) && (e->client->delete_me))
1527 ecore_ipc_client_del(e->client);
1532 _ecore_ipc_event_server_add_free(void *data __UNUSED__, void *ev)
1534 Ecore_Ipc_Event_Server_Add *e;
1537 e->server->event_count--;
1538 if ((e->server->event_count == 0) && (e->server->delete_me))
1539 ecore_ipc_server_del(e->server);
1544 _ecore_ipc_event_server_del_free(void *data __UNUSED__, void *ev)
1546 Ecore_Ipc_Event_Server_Add *e;
1549 e->server->event_count--;
1550 if ((e->server->event_count == 0) && (e->server->delete_me))
1551 ecore_ipc_server_del(e->server);
1556 _ecore_ipc_event_server_data_free(void *data __UNUSED__, void *ev)
1558 Ecore_Ipc_Event_Server_Data *e;
1561 if (e->data) free(e->data);
1562 e->server->event_count--;
1563 if ((e->server->event_count == 0) && (e->server->delete_me))
1564 ecore_ipc_server_del(e->server);