and no more segv's in ecore-con and ecore-ipc. see changelog.
[framework/uifw/ecore.git] / src / lib / ecore_con / ecore_con_private.h
1 #ifndef _ECORE_CON_PRIVATE_H
2 #define _ECORE_CON_PRIVATE_H
3
4 #include "ecore_private.h"
5 #include "Ecore_Con.h"
6
7 #define ECORE_MAGIC_CON_SERVER             0x77665544
8 #define ECORE_MAGIC_CON_CLIENT             0x77556677
9 #define ECORE_MAGIC_CON_URL                0x77074255
10
11 #define ECORE_CON_TYPE 0x0f
12 #define ECORE_CON_SSL  0xf0
13
14 #if USE_GNUTLS
15 # include <gnutls/gnutls.h>
16 #elif USE_OPENSSL
17 # include <openssl/ssl.h>
18 #endif
19 #ifdef HAVE_CURL
20 #include <curl/curl.h>
21 #endif
22
23 #define READBUFSIZ 65536
24
25 extern int _ecore_con_log_dom;
26
27 #ifdef ECORE_CON_DEFAULT_LOG_COLOR
28 #undef ECORE_LOG_DEFAULT_LOG_COLOR
29 #endif
30 #define ECORE_CON_DEFAULT_LOG_COLOR EINA_COLOR_BLUE
31
32 #ifdef ERR
33 # undef ERR
34 #endif
35 #define ERR(...) EINA_LOG_DOM_ERR(_ecore_con_log_dom, __VA_ARGS__)
36
37 #ifdef DBG
38 # undef DBG
39 #endif
40 #define DBG(...) EINA_LOG_DOM_DBG(_ecore_con_log_dom, __VA_ARGS__)
41
42 #ifdef INF
43 # undef INF
44 #endif
45 #define INF(...) EINA_LOG_DOM_INFO(_ecore_con_log_dom, __VA_ARGS__)
46
47 #ifdef WRN
48 # undef WRN
49 #endif
50 #define WRN(...) EINA_LOG_DOM_WARN(_ecore_con_log_dom, __VA_ARGS__)
51
52 #ifdef CRIT
53 # undef CRIT
54 #endif
55 #define CRIT(...) EINA_LOG_DOM_CRIT(_ecore_con_log_dom, __VA_ARGS__)
56
57 typedef struct _Ecore_Con_Lookup Ecore_Con_Lookup;
58 typedef struct _Ecore_Con_Info Ecore_Con_Info;
59 typedef struct Ecore_Con_Socks Ecore_Con_Socks_v4;
60 typedef struct Ecore_Con_Socks_v5 Ecore_Con_Socks_v5;
61 typedef void (*Ecore_Con_Info_Cb)(void *data, Ecore_Con_Info *infos);
62
63 typedef enum _Ecore_Con_State
64 {
65    ECORE_CON_CONNECTED,
66    ECORE_CON_DISCONNECTED,
67    ECORE_CON_INPROGRESS
68 } Ecore_Con_State;
69
70 typedef enum _Ecore_Con_Ssl_Error
71 {
72    ECORE_CON_SSL_ERROR_NONE = 0,
73    ECORE_CON_SSL_ERROR_NOT_SUPPORTED,
74    ECORE_CON_SSL_ERROR_INIT_FAILED,
75    ECORE_CON_SSL_ERROR_SERVER_INIT_FAILED,
76    ECORE_CON_SSL_ERROR_SSL2_NOT_SUPPORTED
77 } Ecore_Con_Ssl_Error;
78
79 typedef enum _Ecore_Con_Ssl_Handshake
80 {
81    ECORE_CON_SSL_STATE_DONE = 0,
82    ECORE_CON_SSL_STATE_HANDSHAKING,
83    ECORE_CON_SSL_STATE_INIT
84 } Ecore_Con_Ssl_State;
85
86 typedef enum Ecore_Con_Proxy_State
87 {  /* named PROXY instead of SOCKS in case some handsome and enterprising
88     * developer decides to add HTTP CONNECT support
89     */
90    ECORE_CON_PROXY_STATE_DONE = 0,
91    ECORE_CON_PROXY_STATE_RESOLVED,
92    ECORE_CON_PROXY_STATE_INIT,
93    ECORE_CON_PROXY_STATE_READ,
94    ECORE_CON_PROXY_STATE_AUTH,
95    ECORE_CON_PROXY_STATE_REQUEST,
96    ECORE_CON_PROXY_STATE_CONFIRM,
97 } Ecore_Con_Proxy_State;
98
99 struct _Ecore_Con_Client
100 {
101    ECORE_MAGIC;
102    int fd;
103    Ecore_Con_Server *host_server;
104    void *data;
105    Ecore_Fd_Handler *fd_handler;
106    unsigned int buf_offset;
107    Eina_Binbuf *buf;
108    const char *ip;
109    Eina_List *event_count;
110    struct sockaddr *client_addr;
111    int client_addr_len;
112    double start_time;
113    Ecore_Timer *until_deletion;
114    double disconnect_time;
115 #if USE_GNUTLS
116    gnutls_datum_t session_ticket;
117    gnutls_session_t session;
118 #elif USE_OPENSSL
119    SSL *ssl;
120    int ssl_err;
121 #endif
122    int ref;
123    Ecore_Con_Ssl_State ssl_state;
124    Eina_Bool handshaking : 1;
125    Eina_Bool upgrade : 1; /* STARTTLS queued */
126    Eina_Bool delete_me : 1; /* del event has been queued */
127 };
128
129 struct _Ecore_Con_Server
130 {
131    ECORE_MAGIC;
132    int fd;
133    Ecore_Con_Type type;
134    char *name;
135    int port;
136    char *path;
137    void *data;
138    Ecore_Fd_Handler *fd_handler;
139    Eina_List *clients;
140    unsigned int client_count;
141    Eina_Binbuf *buf;
142    unsigned int write_buf_offset;
143    Eina_List *infos;
144    Eina_List *event_count;
145    int client_limit;
146    pid_t ppid;
147    /* socks */
148    Ecore_Con_Socks *ecs;
149    Ecore_Con_Proxy_State ecs_state;
150    int ecs_addrlen;
151    unsigned char ecs_addr[16];
152    unsigned int ecs_buf_offset;
153    Eina_Binbuf *ecs_buf;
154    Eina_Binbuf *ecs_recvbuf;
155    const char *proxyip;
156    int proxyport;
157    /* endsocks */
158    const char *verify_name;
159 #if USE_GNUTLS
160    gnutls_session_t session;
161    gnutls_anon_client_credentials_t anoncred_c;
162    gnutls_anon_server_credentials_t anoncred_s;
163    gnutls_psk_client_credentials_t pskcred_c;
164    gnutls_psk_server_credentials_t pskcred_s;
165    gnutls_certificate_credentials_t cert;
166    char *cert_file;
167    gnutls_dh_params_t dh_params;
168 #elif USE_OPENSSL
169    SSL_CTX *ssl_ctx;
170    SSL *ssl;
171    int ssl_err;
172 #endif
173    double start_time;
174    Ecore_Timer *until_deletion;
175    double disconnect_time;
176    double client_disconnect_time;
177    const char *ip;
178    Eina_Bool created : 1; /* @c EINA_TRUE if server is our listening server */
179    Eina_Bool connecting : 1; /* @c EINA_FALSE if just initialized or connected */
180    Eina_Bool handshaking : 1; /* @c EINA_TRUE if server is ssl handshaking */
181    Eina_Bool upgrade : 1;  /* STARTTLS queued */
182    Eina_Bool ssl_prepared : 1;
183    Eina_Bool use_cert : 1; /* @c EINA_TRUE if using certificate auth */
184    Ecore_Con_Ssl_State ssl_state; /* current state of ssl handshake on the server */
185    Eina_Bool verify : 1; /* @c EINA_TRUE if certificates will be verified */
186    Eina_Bool verify_basic : 1; /* @c EINA_TRUE if certificates will be verified only against the hostname */
187    Eina_Bool reject_excess_clients : 1;
188    Eina_Bool delete_me : 1; /* del event has been queued */
189 #ifdef _WIN32
190    Eina_Bool want_write : 1;
191    Eina_Bool read_stop : 1;
192    Eina_Bool read_stopped : 1;
193    HANDLE pipe;
194    HANDLE thread_read;
195    HANDLE event_read;
196    HANDLE event_peek;
197    DWORD nbr_bytes;
198 #endif
199 };
200
201 #ifdef HAVE_CURL
202 struct _Ecore_Con_Url
203 {
204    ECORE_MAGIC;
205    CURL *curl_easy;
206    struct curl_slist *headers;
207    Eina_List *additional_headers;
208    Eina_List *response_headers;
209    const char *url;
210    long proxy_type;
211    int status;
212
213    Ecore_Timer *timer;
214
215    Ecore_Con_Url_Time time_condition;
216    double timestamp;
217    void *data;
218    
219    void *post_data;
220
221    int received;
222    int write_fd;
223
224    unsigned int event_count;
225    Eina_Bool dead : 1;
226    Eina_Bool multi : 1;
227 };
228 #endif
229
230 struct _Ecore_Con_Info
231 {
232    unsigned int size;
233    struct addrinfo info;
234    char ip[NI_MAXHOST];
235    char service[NI_MAXSERV];
236 };
237
238 struct _Ecore_Con_Lookup
239 {
240    Ecore_Con_Dns_Cb done_cb;
241    const void *data;
242 };
243
244 #define ECORE_CON_SOCKS_CAST_ELSE(X) \
245    Ecore_Con_Socks_v4 *v4 = NULL; \
246    Ecore_Con_Socks_v5 *v5 = NULL; \
247    if ((X) && ((X)->version == 4)) \
248      v4 = (Ecore_Con_Socks_v4*)(X); \
249    else if ((X) && ((X)->version == 5)) \
250      v5 = (Ecore_Con_Socks_v5*)(X); \
251    else
252
253 struct Ecore_Con_Socks /* v4 */
254 {
255    unsigned char version;
256
257    const char *ip;
258    int port;
259    const char *username;
260    unsigned int ulen;
261    Eina_Bool lookup : 1;
262    Eina_Bool bind : 1;
263 };
264
265 struct Ecore_Con_Socks_v5
266 {
267    unsigned char version;
268
269    const char *ip;
270    int port;
271    const char *username;
272    unsigned int ulen;
273    Eina_Bool lookup : 1;
274    Eina_Bool bind : 1;
275    /* v5 only */
276    unsigned char method;
277    const char *password;
278    unsigned int plen;
279 };
280
281 extern Ecore_Con_Socks *_ecore_con_proxy_once;
282 extern Ecore_Con_Socks *_ecore_con_proxy_global;
283 void ecore_con_socks_init(void);
284 void ecore_con_socks_shutdown(void);
285 Eina_Bool ecore_con_socks_svr_init(Ecore_Con_Server *svr);
286 void ecore_con_socks_read(Ecore_Con_Server *svr, unsigned char *buf, int num);
287 void ecore_con_socks_dns_cb(const char *canonname, const char *ip, struct sockaddr *addr, int addrlen, Ecore_Con_Server *svr);
288 /* from ecore_con.c */
289 void ecore_con_server_infos_del(Ecore_Con_Server *svr, void *info);
290 void ecore_con_event_proxy_bind(Ecore_Con_Server *svr);
291 void ecore_con_event_server_data(Ecore_Con_Server *svr, unsigned char *buf, int num, Eina_Bool duplicate);
292 void ecore_con_event_server_del(Ecore_Con_Server *svr);
293 #define ecore_con_event_server_error(svr, error) _ecore_con_event_server_error((svr), (char*)(error), EINA_TRUE)
294 void _ecore_con_event_server_error(Ecore_Con_Server *svr, char *error, Eina_Bool duplicate);
295 void ecore_con_event_client_add(Ecore_Con_Client *cl);
296 void ecore_con_event_client_data(Ecore_Con_Client *cl, unsigned char *buf, int num, Eina_Bool duplicate);
297 void ecore_con_event_client_del(Ecore_Con_Client *cl);
298 void ecore_con_event_client_error(Ecore_Con_Client *cl, const char *error);
299 void _ecore_con_server_kill(Ecore_Con_Server *svr);
300 void _ecore_con_client_kill(Ecore_Con_Client *cl);
301 /* from ecore_local_win32.c */
302 #ifdef _WIN32
303 Eina_Bool ecore_con_local_listen(Ecore_Con_Server *svr);
304 Eina_Bool ecore_con_local_connect(Ecore_Con_Server *svr,
305                             Eina_Bool (*cb_done)(void *data,
306                                                  Ecore_Fd_Handler *fd_handler));
307 Eina_Bool ecore_con_local_win32_server_flush(Ecore_Con_Server *svr);
308 Eina_Bool ecore_con_local_win32_client_flush(Ecore_Con_Client *cl);
309 void      ecore_con_local_win32_server_del(Ecore_Con_Server *svr);
310 void      ecore_con_local_win32_client_del(Ecore_Con_Client *cl);
311 #else
312 /* from ecore_local.c */
313 int ecore_con_local_init(void);
314 int ecore_con_local_shutdown(void);
315 int ecore_con_local_connect(Ecore_Con_Server *svr,
316                             Eina_Bool (*cb_done)(
317                                void *data,
318                                Ecore_Fd_Handler *fd_handler),
319                             void *data);
320 int ecore_con_local_listen(Ecore_Con_Server *svr,
321                            Eina_Bool (*cb_listen)(
322                               void *data,
323                               Ecore_Fd_Handler *fd_handler),
324                            void *data);
325 #endif
326
327 /* from ecore_con_info.c */
328 int                 ecore_con_info_init(void);
329 int                 ecore_con_info_shutdown(void);
330 int                 ecore_con_info_tcp_connect(Ecore_Con_Server *svr,
331                                                Ecore_Con_Info_Cb done_cb,
332                                                void *data);
333 int                 ecore_con_info_tcp_listen(Ecore_Con_Server *svr,
334                                               Ecore_Con_Info_Cb done_cb,
335                                               void *data);
336 int                 ecore_con_info_udp_connect(Ecore_Con_Server *svr,
337                                                Ecore_Con_Info_Cb done_cb,
338                                                void *data);
339 int                 ecore_con_info_udp_listen(Ecore_Con_Server *svr,
340                                               Ecore_Con_Info_Cb done_cb,
341                                               void *data);
342 int                 ecore_con_info_mcast_listen(Ecore_Con_Server *svr,
343                                                 Ecore_Con_Info_Cb done_cb,
344                                                 void *data);
345 void                ecore_con_info_data_clear(void *info);
346
347 void ecore_con_event_server_add(Ecore_Con_Server *svr);
348
349
350 /* from ecore_con_ssl.c */
351 Ecore_Con_Ssl_Error ecore_con_ssl_init(void);
352 Ecore_Con_Ssl_Error ecore_con_ssl_shutdown(void);
353 Ecore_Con_Ssl_Error ecore_con_ssl_server_prepare(Ecore_Con_Server *svr, int ssl_type);
354 Ecore_Con_Ssl_Error ecore_con_ssl_server_init(Ecore_Con_Server *svr);
355 Ecore_Con_Ssl_Error ecore_con_ssl_server_shutdown(Ecore_Con_Server *svr);
356 int                 ecore_con_ssl_server_read(Ecore_Con_Server *svr,
357                                               unsigned char *buf,
358                                               int size);
359 int                 ecore_con_ssl_server_write(Ecore_Con_Server *svr,
360                                                const unsigned char *buf,
361                                                int size);
362 Ecore_Con_Ssl_Error ecore_con_ssl_client_init(Ecore_Con_Client *svr);
363 Ecore_Con_Ssl_Error ecore_con_ssl_client_shutdown(Ecore_Con_Client *svr);
364 int                 ecore_con_ssl_client_read(Ecore_Con_Client *svr,
365                                               unsigned char *buf,
366                                               int size);
367 int                 ecore_con_ssl_client_write(Ecore_Con_Client *svr,
368                                                const unsigned char *buf,
369                                                int size);
370
371 int                 ecore_con_info_get(Ecore_Con_Server *svr,
372                                        Ecore_Con_Info_Cb done_cb,
373                                        void *data,
374                                        struct addrinfo *hints);
375
376
377 #define GENERIC_ALLOC_FREE_HEADER(TYPE, Type) \
378   TYPE *Type##_alloc(void);                   \
379   void Type##_free(TYPE *e);
380
381 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Client_Add, ecore_con_event_client_add);
382 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Client_Del, ecore_con_event_client_del);
383 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Client_Write, ecore_con_event_client_write);
384 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Client_Data, ecore_con_event_client_data);
385 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Server_Error, ecore_con_event_server_error);
386 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Client_Error, ecore_con_event_client_error);
387 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Server_Add, ecore_con_event_server_add);
388 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Server_Del, ecore_con_event_server_del);
389 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Server_Write, ecore_con_event_server_write);
390 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Server_Data, ecore_con_event_server_data);
391 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Proxy_Bind, ecore_con_event_proxy_bind);
392
393 void ecore_con_mempool_init(void);
394 void ecore_con_mempool_shutdown(void);
395
396 #undef GENERIC_ALLOC_FREE_HEADER
397
398 #endif