4f8f79230b39667284685dad8d557901426ef9f9
[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    Ecore_Con_Ssl_State ssl_state;
123    Eina_Bool handshaking : 1;
124    Eina_Bool upgrade : 1; /* STARTTLS queued */
125    Eina_Bool delete_me : 1; /* del event has been queued */
126 };
127
128 struct _Ecore_Con_Server
129 {
130    ECORE_MAGIC;
131    int fd;
132    Ecore_Con_Type type;
133    char *name;
134    int port;
135    char *path;
136    void *data;
137    Ecore_Fd_Handler *fd_handler;
138    Eina_List *clients;
139    unsigned int client_count;
140    Eina_Binbuf *buf;
141    unsigned int write_buf_offset;
142    Eina_List *infos;
143    Eina_List *event_count;
144    int client_limit;
145    pid_t ppid;
146    /* socks */
147    Ecore_Con_Socks *ecs;
148    Ecore_Con_Proxy_State ecs_state;
149    int ecs_addrlen;
150    unsigned char ecs_addr[16];
151    unsigned int ecs_buf_offset;
152    Eina_Binbuf *ecs_buf;
153    Eina_Binbuf *ecs_recvbuf;
154    const char *proxyip;
155    int proxyport;
156    /* endsocks */
157    const char *verify_name;
158 #if USE_GNUTLS
159    gnutls_session_t session;
160    gnutls_anon_client_credentials_t anoncred_c;
161    gnutls_anon_server_credentials_t anoncred_s;
162    gnutls_psk_client_credentials_t pskcred_c;
163    gnutls_psk_server_credentials_t pskcred_s;
164    gnutls_certificate_credentials_t cert;
165    char *cert_file;
166    gnutls_dh_params_t dh_params;
167 #elif USE_OPENSSL
168    SSL_CTX *ssl_ctx;
169    SSL *ssl;
170    int ssl_err;
171 #endif
172    double start_time;
173    Ecore_Timer *until_deletion;
174    double disconnect_time;
175    double client_disconnect_time;
176    const char *ip;
177    Eina_Bool created : 1; /* @c EINA_TRUE if server is our listening server */
178    Eina_Bool connecting : 1; /* @c EINA_FALSE if just initialized or connected */
179    Eina_Bool handshaking : 1; /* @c EINA_TRUE if server is ssl handshaking */
180    Eina_Bool upgrade : 1;  /* STARTTLS queued */
181    Eina_Bool ssl_prepared : 1;
182    Eina_Bool use_cert : 1; /* @c EINA_TRUE if using certificate auth */
183    Ecore_Con_Ssl_State ssl_state; /* current state of ssl handshake on the server */
184    Eina_Bool verify : 1; /* @c EINA_TRUE if certificates will be verified */
185    Eina_Bool verify_basic : 1; /* @c EINA_TRUE if certificates will be verified only against the hostname */
186    Eina_Bool reject_excess_clients : 1;
187    Eina_Bool delete_me : 1; /* del event has been queued */
188 #ifdef _WIN32
189    Eina_Bool want_write : 1;
190    Eina_Bool read_stop : 1;
191    Eina_Bool read_stopped : 1;
192    HANDLE pipe;
193    HANDLE thread_read;
194    HANDLE event_read;
195    HANDLE event_peek;
196    DWORD nbr_bytes;
197 #endif
198 };
199
200 #ifdef HAVE_CURL
201 struct _Ecore_Con_Url
202 {
203    ECORE_MAGIC;
204    CURL *curl_easy;
205    struct curl_slist *headers;
206    Eina_List *additional_headers;
207    Eina_List *response_headers;
208    const char *url;
209    long proxy_type;
210    int status;
211
212    Ecore_Timer *timer;
213
214    Ecore_Con_Url_Time time_condition;
215    double timestamp;
216    void *data;
217    
218    void *post_data;
219
220    int received;
221    int write_fd;
222
223    unsigned int event_count;
224    Eina_Bool dead : 1;
225    Eina_Bool multi : 1;
226 };
227 #endif
228
229 struct _Ecore_Con_Info
230 {
231    unsigned int size;
232    struct addrinfo info;
233    char ip[NI_MAXHOST];
234    char service[NI_MAXSERV];
235 };
236
237 struct _Ecore_Con_Lookup
238 {
239    Ecore_Con_Dns_Cb done_cb;
240    const void *data;
241 };
242
243 #define ECORE_CON_SOCKS_CAST_ELSE(X) \
244    Ecore_Con_Socks_v4 *v4 = NULL; \
245    Ecore_Con_Socks_v5 *v5 = NULL; \
246    if ((X) && ((X)->version == 4)) \
247      v4 = (Ecore_Con_Socks_v4*)(X); \
248    else if ((X) && ((X)->version == 5)) \
249      v5 = (Ecore_Con_Socks_v5*)(X); \
250    else
251
252 struct Ecore_Con_Socks /* v4 */
253 {
254    unsigned char version;
255
256    const char *ip;
257    int port;
258    const char *username;
259    unsigned int ulen;
260    Eina_Bool lookup : 1;
261    Eina_Bool bind : 1;
262 };
263
264 struct Ecore_Con_Socks_v5
265 {
266    unsigned char version;
267
268    const char *ip;
269    int port;
270    const char *username;
271    unsigned int ulen;
272    Eina_Bool lookup : 1;
273    Eina_Bool bind : 1;
274    /* v5 only */
275    unsigned char method;
276    const char *password;
277    unsigned int plen;
278 };
279
280 extern Ecore_Con_Socks *_ecore_con_proxy_once;
281 extern Ecore_Con_Socks *_ecore_con_proxy_global;
282 void ecore_con_socks_init(void);
283 void ecore_con_socks_shutdown(void);
284 Eina_Bool ecore_con_socks_svr_init(Ecore_Con_Server *svr);
285 void ecore_con_socks_read(Ecore_Con_Server *svr, unsigned char *buf, int num);
286 void ecore_con_socks_dns_cb(const char *canonname, const char *ip, struct sockaddr *addr, int addrlen, Ecore_Con_Server *svr);
287 /* from ecore_con.c */
288 void ecore_con_server_infos_del(Ecore_Con_Server *svr, void *info);
289 void ecore_con_event_proxy_bind(Ecore_Con_Server *svr);
290 void ecore_con_event_server_data(Ecore_Con_Server *svr, unsigned char *buf, int num, Eina_Bool duplicate);
291 void ecore_con_event_server_del(Ecore_Con_Server *svr);
292 #define ecore_con_event_server_error(svr, error) _ecore_con_event_server_error((svr), (char*)(error), EINA_TRUE)
293 void _ecore_con_event_server_error(Ecore_Con_Server *svr, char *error, Eina_Bool duplicate);
294 void ecore_con_event_client_add(Ecore_Con_Client *cl);
295 void ecore_con_event_client_data(Ecore_Con_Client *cl, unsigned char *buf, int num, Eina_Bool duplicate);
296 void ecore_con_event_client_del(Ecore_Con_Client *cl);
297 void ecore_con_event_client_error(Ecore_Con_Client *cl, const char *error);
298 void _ecore_con_server_kill(Ecore_Con_Server *svr);
299 void _ecore_con_client_kill(Ecore_Con_Client *cl);
300 /* from ecore_local_win32.c */
301 #ifdef _WIN32
302 Eina_Bool ecore_con_local_listen(Ecore_Con_Server *svr);
303 Eina_Bool ecore_con_local_connect(Ecore_Con_Server *svr,
304                             Eina_Bool (*cb_done)(void *data,
305                                                  Ecore_Fd_Handler *fd_handler));
306 Eina_Bool ecore_con_local_win32_server_flush(Ecore_Con_Server *svr);
307 Eina_Bool ecore_con_local_win32_client_flush(Ecore_Con_Client *cl);
308 void      ecore_con_local_win32_server_del(Ecore_Con_Server *svr);
309 void      ecore_con_local_win32_client_del(Ecore_Con_Client *cl);
310 #else
311 /* from ecore_local.c */
312 int ecore_con_local_init(void);
313 int ecore_con_local_shutdown(void);
314 int ecore_con_local_connect(Ecore_Con_Server *svr,
315                             Eina_Bool (*cb_done)(
316                                void *data,
317                                Ecore_Fd_Handler *fd_handler),
318                             void *data);
319 int ecore_con_local_listen(Ecore_Con_Server *svr,
320                            Eina_Bool (*cb_listen)(
321                               void *data,
322                               Ecore_Fd_Handler *fd_handler),
323                            void *data);
324 #endif
325
326 /* from ecore_con_info.c */
327 int                 ecore_con_info_init(void);
328 int                 ecore_con_info_shutdown(void);
329 int                 ecore_con_info_tcp_connect(Ecore_Con_Server *svr,
330                                                Ecore_Con_Info_Cb done_cb,
331                                                void *data);
332 int                 ecore_con_info_tcp_listen(Ecore_Con_Server *svr,
333                                               Ecore_Con_Info_Cb done_cb,
334                                               void *data);
335 int                 ecore_con_info_udp_connect(Ecore_Con_Server *svr,
336                                                Ecore_Con_Info_Cb done_cb,
337                                                void *data);
338 int                 ecore_con_info_udp_listen(Ecore_Con_Server *svr,
339                                               Ecore_Con_Info_Cb done_cb,
340                                               void *data);
341 int                 ecore_con_info_mcast_listen(Ecore_Con_Server *svr,
342                                                 Ecore_Con_Info_Cb done_cb,
343                                                 void *data);
344 void                ecore_con_info_data_clear(void *info);
345
346 void ecore_con_event_server_add(Ecore_Con_Server *svr);
347
348
349 /* from ecore_con_ssl.c */
350 Ecore_Con_Ssl_Error ecore_con_ssl_init(void);
351 Ecore_Con_Ssl_Error ecore_con_ssl_shutdown(void);
352 Ecore_Con_Ssl_Error ecore_con_ssl_server_prepare(Ecore_Con_Server *svr, int ssl_type);
353 Ecore_Con_Ssl_Error ecore_con_ssl_server_init(Ecore_Con_Server *svr);
354 Ecore_Con_Ssl_Error ecore_con_ssl_server_shutdown(Ecore_Con_Server *svr);
355 int                 ecore_con_ssl_server_read(Ecore_Con_Server *svr,
356                                               unsigned char *buf,
357                                               int size);
358 int                 ecore_con_ssl_server_write(Ecore_Con_Server *svr,
359                                                const unsigned char *buf,
360                                                int size);
361 Ecore_Con_Ssl_Error ecore_con_ssl_client_init(Ecore_Con_Client *svr);
362 Ecore_Con_Ssl_Error ecore_con_ssl_client_shutdown(Ecore_Con_Client *svr);
363 int                 ecore_con_ssl_client_read(Ecore_Con_Client *svr,
364                                               unsigned char *buf,
365                                               int size);
366 int                 ecore_con_ssl_client_write(Ecore_Con_Client *svr,
367                                                const unsigned char *buf,
368                                                int size);
369
370 int                 ecore_con_info_get(Ecore_Con_Server *svr,
371                                        Ecore_Con_Info_Cb done_cb,
372                                        void *data,
373                                        struct addrinfo *hints);
374
375
376 #define GENERIC_ALLOC_FREE_HEADER(TYPE, Type) \
377   TYPE *Type##_alloc(void);                   \
378   void Type##_free(TYPE *e);
379
380 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Client_Add, ecore_con_event_client_add);
381 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Client_Del, ecore_con_event_client_del);
382 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Client_Write, ecore_con_event_client_write);
383 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Client_Data, ecore_con_event_client_data);
384 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Server_Error, ecore_con_event_server_error);
385 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Client_Error, ecore_con_event_client_error);
386 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Server_Add, ecore_con_event_server_add);
387 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Server_Del, ecore_con_event_server_del);
388 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Server_Write, ecore_con_event_server_write);
389 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Server_Data, ecore_con_event_server_data);
390 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Proxy_Bind, ecore_con_event_proxy_bind);
391
392 void ecore_con_mempool_init(void);
393 void ecore_con_mempool_shutdown(void);
394
395 #undef GENERIC_ALLOC_FREE_HEADER
396
397 #endif