b420cf23b1e6d0370a9b1f2d9cf63731cae85747
[framework/uifw/ecore.git] / src / lib / ecore_ipc / Ecore_Ipc.h
1 #ifndef _ECORE_IPC_H
2 #define _ECORE_IPC_H
3
4 #ifdef EAPI
5 #undef EAPI
6 #endif
7 #ifdef _MSC_VER
8 # ifdef BUILDING_DLL
9 #  define EAPI __declspec(dllexport)
10 # else
11 #  define EAPI __declspec(dllimport)
12 # endif
13 #else
14 # ifdef __GNUC__
15 #  if __GNUC__ >= 4
16 #   define EAPI __attribute__ ((visibility("default")))
17 #  else
18 #   define EAPI
19 #  endif
20 # else
21 #  define EAPI
22 # endif
23 #endif
24
25 #include <Ecore_Data.h>
26
27 /**
28  * @file Ecore_Ipc.h
29  * @brief Ecore inter-process communication functions.
30  */
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35  
36 #ifndef _ECORE_IPC_PRIVATE_H
37    typedef void Ecore_Ipc_Server; /**< An IPC connection handle */
38    typedef void Ecore_Ipc_Client; /**< An IPC connection handle */
39 #endif
40
41 /**
42  * Macros used for generic data packing
43  */
44 EAPI unsigned short _ecore_ipc_swap_16(unsigned short v);
45 EAPI unsigned int _ecore_ipc_swap_32(unsigned int v);
46 EAPI unsigned long long _ecore_ipc_swap_64(unsigned long long v);
47
48 #ifdef WORDS_BIGENDIAN
49 #define ECORE_IPC_SWAP2NET64(x) _ecore_ipc_swap_64(x)
50 #define ECORE_IPC_SWAP2CPU64(x) _ecore_ipc_swap_64(x)
51 #define ECORE_IPC_SWAP2NET32(x) _ecore_ipc_swap_32(x)
52 #define ECORE_IPC_SWAP2CPU32(x) _ecore_ipc_swap_32(x)
53 #define ECORE_IPC_SWAP2NET16(x) _ecore_ipc_swap_16(x)
54 #define ECORE_IPC_SWAP2CPU16(x) _ecore_ipc_swap_16(x)
55 #define ECORE_IPC_SWAP2NET8(x) (x)
56 #define ECORE_IPC_SWAP2CPU8(x) (x)
57 #else
58 #define ECORE_IPC_SWAP2NET64(x) (x)
59 #define ECORE_IPC_SWAP2CPU64(x) (x)
60 #define ECORE_IPC_SWAP2NET32(x) (x)
61 #define ECORE_IPC_SWAP2CPU32(x) (x)
62 #define ECORE_IPC_SWAP2NET16(x) (x)
63 #define ECORE_IPC_SWAP2CPU16(x) (x)
64 #define ECORE_IPC_SWAP2NET8(x) (x)
65 #define ECORE_IPC_SWAP2CPU8(x) (x)
66 #endif
67
68 /* 1, 2, 4 and 8 byte datatypes */
69 /* unpacking */
70 #define ECORE_IPC_GET64(v)\
71     { \
72         p->v = ECORE_IPC_SWAP2CPU64(*(long long *)(ptr)); \
73         ptr += 8; \
74     }
75 #define ECORE_IPC_GET32(v)\
76     { \
77         p->v = ECORE_IPC_SWAP2CPU32(*(int *)(ptr)); \
78         ptr += 4; \
79     }
80 #define ECORE_IPC_GET16(v)\
81     { \
82         p->v = ECORE_IPC_SWAP2CPU16(*(short *)(ptr)); \
83         ptr += 2; \
84     }
85 #define ECORE_IPC_GET8(v) \
86     { \
87         p->v = ECORE_IPC_SWAP2CPU8(*(char *)(ptr)); \
88         ptr += 1; \
89     }
90 /* packing */
91 #define ECORE_IPC_PUT64(v)\
92     { \
93         *(long long *)(ptr) = ECORE_IPC_SWAP2NET64(p->v); \
94         ptr += 8; \
95     }
96 #define ECORE_IPC_PUT32(v)\
97     { \
98         *(int *)(ptr) = ECORE_IPC_SWAP2NET32(p->v); \
99         ptr += 4; \
100     }
101 #define ECORE_IPC_PUT16(v)\
102     { \
103         *(short *)(ptr) = ECORE_IPC_SWAP2NET16(p->v); \
104         ptr += 2; \
105     }
106 #define ECORE_IPC_PUT8(v) \
107     { \
108         *(char *)(ptr) = ECORE_IPC_SWAP2NET8(p->v); \
109         ptr += 1; \
110     }
111 /* padding data */
112 #define ECORE_IPC_PAD8()   ptr += 1
113 #define ECORE_IPC_PAD16()  ptr += 2
114 #define ECORE_IPC_PAD32()  ptr += 4
115 #define ECORE_IPC_PAD64()  ptr += 8
116
117 /* counting data when encoding lists */
118 #define ECORE_IPC_CNT8()    len += 1
119 #define ECORE_IPC_CNT16()   len += 2
120 #define ECORE_IPC_CNT32()   len += 4
121 #define ECORE_IPC_CNT64()   len += 8
122
123 /* strings */
124 #define ECORE_IPC_CHEKS() if (*((unsigned char *)d + s - 1) != 0) return 0;
125 #define ECORE_IPC_GETS(v) \
126     { \
127         if (ptr < ((unsigned char *)d + s)) \
128             { \
129                 p->v = (char *)ptr; \
130                 ptr += strlen(p->v) + 1; \
131             } \
132     } 
133 #define ECORE_IPC_PUTS(v, l)\
134     { \
135         strcpy((char *)ptr, p->v); \
136         ptr += l + 1; \
137     }
138
139 /* handy to calculate what sized block we need to alloc */
140 #define ECORE_IPC_SLEN(l, v) ((l = strlen(p->v)) + 1)
141 #define ECORE_IPC_CNTS(v)   len += strlen(p->v) + 1
142
143 /* saves typing function headers */
144 #define ECORE_IPC_DEC_STRUCT_PROTO(x) static int x(void *d, int s, void *pp)
145 #define ECORE_IPC_ENC_STRUCT_PROTO(x) static void *x(void *pp, int *s)
146 #define ECORE_IPC_DEC_EVAS_LIST_PROTO(x) static Evas_List *x(void *d, int s)
147 #define ECORE_IPC_ENC_EVAS_LIST_PROTO(x) static void *x(Evas_List *lp, int *s)
148
149
150 /* decoder setup - saves typing. requires data packet of exact size, or fail */
151 #define ECORE_IPC_DEC_STRUCT_HEAD_EXACT(typ, x) \
152     typ *p; \
153     unsigned char *ptr; \
154     p = (typ *)pp; \
155     if (!d) return 0; if (s != (x)) return 0; \
156     ptr = d;
157 /* decoder setup - saves typing. requires data packet of a minimum size */
158 #define ECORE_IPC_DEC_STRUCT_HEAD_MIN(typ, x) \
159     typ *p; \
160     unsigned char *ptr; \
161     p = (typ *)pp; \
162     if (!d) return 0; if (s < (x)) return 0; \
163     ptr = d;
164 /* footer for the hell of it */
165 #define ECORE_IPC_DEC_STRUCT_FOOT() return 1
166 /* header for encoder - gives native strct type and size of flattened packet */
167 #define ECORE_IPC_ENC_STRUCT_HEAD(typ, sz) \
168     typ *p; \
169     unsigned char *d, *ptr; \
170     int len; \
171     *s = 0; \
172     if(!pp) return NULL; \
173     p = (typ *)pp; \
174     len = sz; \
175     d = malloc(len); \
176     if (!d) return NULL; \
177     *s = len; \
178     ptr = d;
179 /* footer for the hell of it */
180 #define ECORE_IPC_ENC_STRUCT_FOOT() return d
181
182 #define ECORE_IPC_DEC_EVAS_LIST_HEAD(typ) \
183     unsigned char *ptr; \
184     Evas_List *l; \
185     typ *p; \
186     l = NULL; \
187     ptr = d; \
188     while(ptr < (unsigned char *)(d + s)) \
189         { \
190             p = malloc(sizeof(typ));
191
192 #define ECORE_IPC_DEC_EVAS_LIST_FOOT() \
193             l = evas_list_append(l, p); \
194         } \
195     return l
196 #define ECORE_IPC_ENC_EVAS_LIST_HEAD_START(typ) \
197     Evas_List *l; \
198     typ *p; \
199     unsigned char *d, *ptr; \
200     int len; \
201     *s = 0; \
202     len = 0; \
203     for (l = lp; l; l = l->next) \
204       { \
205          p = l->data;
206 #define ECORE_IPC_ENC_EVAS_LIST_HEAD_FINISH() \
207       } \
208     d = malloc(len); \
209     if(!d) return NULL; \
210     *s = len; \
211     ptr = d; \
212     for (l = lp; l; l = l->next) \
213       { \
214          p = l->data;
215
216 #define ECORE_IPC_ENC_EVAS_LIST_FOOT() \
217       } \
218    return d
219
220    typedef enum _Ecore_Ipc_Type
221      {
222         ECORE_IPC_LOCAL_USER,
223           ECORE_IPC_LOCAL_SYSTEM,
224           ECORE_IPC_REMOTE_SYSTEM,
225           ECORE_IPC_USE_SSL = 16
226      } Ecore_Ipc_Type;
227    
228    typedef struct _Ecore_Ipc_Event_Client_Add  Ecore_Ipc_Event_Client_Add;
229    typedef struct _Ecore_Ipc_Event_Client_Del  Ecore_Ipc_Event_Client_Del;
230    typedef struct _Ecore_Ipc_Event_Server_Add  Ecore_Ipc_Event_Server_Add;
231    typedef struct _Ecore_Ipc_Event_Server_Del  Ecore_Ipc_Event_Server_Del;
232    typedef struct _Ecore_Ipc_Event_Client_Data Ecore_Ipc_Event_Client_Data;
233    typedef struct _Ecore_Ipc_Event_Server_Data Ecore_Ipc_Event_Server_Data;
234    
235    struct _Ecore_Ipc_Event_Client_Add
236      {
237         Ecore_Ipc_Client *client;
238      };
239    
240    struct _Ecore_Ipc_Event_Client_Del
241      {
242         Ecore_Ipc_Client *client;
243      };
244    
245    struct _Ecore_Ipc_Event_Server_Add
246      {
247         Ecore_Ipc_Server *server;
248      };
249    
250    struct _Ecore_Ipc_Event_Server_Del
251      {
252         Ecore_Ipc_Server *server;
253      };
254    
255    struct _Ecore_Ipc_Event_Client_Data
256      {
257         Ecore_Ipc_Client *client;
258         /* FIXME: this needs to become an ipc message */
259         int               major;
260         int               minor;
261         int               ref;
262         int               ref_to;
263         int               response;
264         void             *data;
265         int               size;
266      };
267    
268    struct _Ecore_Ipc_Event_Server_Data
269      {
270         Ecore_Ipc_Server *server;
271         /* FIXME: this needs to become an ipc message */
272         int               major;
273         int               minor;
274         int               ref;
275         int               ref_to;
276         int               response;
277         void             *data;
278         int               size;
279      };
280    
281    EAPI extern int ECORE_IPC_EVENT_CLIENT_ADD;
282    EAPI extern int ECORE_IPC_EVENT_CLIENT_DEL;
283    EAPI extern int ECORE_IPC_EVENT_SERVER_ADD;
284    EAPI extern int ECORE_IPC_EVENT_SERVER_DEL;
285    EAPI extern int ECORE_IPC_EVENT_CLIENT_DATA;
286    EAPI extern int ECORE_IPC_EVENT_SERVER_DATA;
287    
288    EAPI int               ecore_ipc_init(void);
289    EAPI int               ecore_ipc_shutdown(void);
290    
291    /* FIXME: need to add protocol type parameter */
292    EAPI Ecore_Ipc_Server *ecore_ipc_server_add(Ecore_Ipc_Type type, const char *name, int port, const void *data);
293    
294    /* FIXME: need to add protocol type parameter */
295    EAPI Ecore_Ipc_Server *ecore_ipc_server_connect(Ecore_Ipc_Type type, char *name, int port, const void *data);
296    EAPI void             *ecore_ipc_server_del(Ecore_Ipc_Server *svr);
297    EAPI void             *ecore_ipc_server_data_get(Ecore_Ipc_Server *svr);
298    EAPI int               ecore_ipc_server_connected_get(Ecore_Ipc_Server *svr);
299    EAPI Ecore_List       *ecore_ipc_server_clients_get(Ecore_Ipc_Server *svr);
300    /* FIXME: this needs to become an ipc message */
301    EAPI int               ecore_ipc_server_send(Ecore_Ipc_Server *svr, int major, int minor, int ref, int ref_to, int response, const void *data, int size);
302    EAPI void              ecore_ipc_server_client_limit_set(Ecore_Ipc_Server *svr, int client_limit, char reject_excess_clients);
303    EAPI void              ecore_ipc_server_data_size_max_set(Ecore_Ipc_Server *srv, int size);
304    EAPI int               ecore_ipc_server_data_size_max_get(Ecore_Ipc_Server *srv);
305    EAPI char             *ecore_ipc_server_ip_get(Ecore_Ipc_Server *svr);
306    EAPI void              ecore_ipc_server_flush(Ecore_Ipc_Server *svr);
307        
308    /* FIXME: this needs to become an ipc message */
309    EAPI int               ecore_ipc_client_send(Ecore_Ipc_Client *cl, int major, int minor, int ref, int ref_to, int response, const void *data, int size);
310    EAPI Ecore_Ipc_Server *ecore_ipc_client_server_get(Ecore_Ipc_Client *cl);
311    EAPI void             *ecore_ipc_client_del(Ecore_Ipc_Client *cl);
312    EAPI void              ecore_ipc_client_data_set(Ecore_Ipc_Client *cl, const void *data);
313    EAPI void             *ecore_ipc_client_data_get(Ecore_Ipc_Client *cl);
314    EAPI void              ecore_ipc_client_data_size_max_set(Ecore_Ipc_Client *cl, int size);
315    EAPI int               ecore_ipc_client_data_size_max_get(Ecore_Ipc_Client *cl);
316    EAPI char             *ecore_ipc_client_ip_get(Ecore_Ipc_Client *cl);
317    EAPI void              ecore_ipc_client_flush(Ecore_Ipc_Client *cl);
318    
319    EAPI int               ecore_ipc_ssl_available_get(void);
320    /* FIXME: need to add a callback to "ok" large ipc messages greater than */
321    /*        a certain size (seurity/DOS attack safety) */
322    
323 #ifdef __cplusplus
324 }
325 #endif
326
327 #endif