Initialize Tizen 2.3
[framework/uifw/ecore.git] / mobile / 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 #define ECORE_CON_SUPER_SSL  0xf00
14
15 #if USE_GNUTLS
16 # include <gnutls/gnutls.h>
17 #elif USE_OPENSSL
18 # include <openssl/ssl.h>
19 #endif
20 #ifdef HAVE_CURL
21 #include <curl/curl.h>
22 #endif
23
24 #define READBUFSIZ 65536
25
26 extern int _ecore_con_log_dom;
27
28 #ifdef ECORE_CON_DEFAULT_LOG_COLOR
29 #undef ECORE_LOG_DEFAULT_LOG_COLOR
30 #endif
31 #define ECORE_CON_DEFAULT_LOG_COLOR EINA_COLOR_BLUE
32
33 #ifdef ERR
34 # undef ERR
35 #endif
36 #define ERR(...) EINA_LOG_DOM_ERR(_ecore_con_log_dom, __VA_ARGS__)
37
38 #ifdef DBG
39 # undef DBG
40 #endif
41 #define DBG(...) EINA_LOG_DOM_DBG(_ecore_con_log_dom, __VA_ARGS__)
42
43 #ifdef INF
44 # undef INF
45 #endif
46 #define INF(...) EINA_LOG_DOM_INFO(_ecore_con_log_dom, __VA_ARGS__)
47
48 #ifdef WRN
49 # undef WRN
50 #endif
51 #define WRN(...) EINA_LOG_DOM_WARN(_ecore_con_log_dom, __VA_ARGS__)
52
53 #ifdef CRIT
54 # undef CRIT
55 #endif
56 #define CRIT(...) EINA_LOG_DOM_CRIT(_ecore_con_log_dom, __VA_ARGS__)
57
58 typedef struct _Ecore_Con_Lookup Ecore_Con_Lookup;
59 typedef struct _Ecore_Con_Info Ecore_Con_Info;
60 typedef struct Ecore_Con_Socks Ecore_Con_Socks_v4;
61 typedef struct Ecore_Con_Socks_v5 Ecore_Con_Socks_v5;
62 typedef void (*Ecore_Con_Info_Cb)(void *data, Ecore_Con_Info *infos);
63
64 typedef enum _Ecore_Con_State
65 {
66    ECORE_CON_CONNECTED,
67    ECORE_CON_DISCONNECTED,
68    ECORE_CON_INPROGRESS
69 } Ecore_Con_State;
70
71 typedef enum _Ecore_Con_Ssl_Error
72 {
73    ECORE_CON_SSL_ERROR_NONE = 0,
74    ECORE_CON_SSL_ERROR_NOT_SUPPORTED,
75    ECORE_CON_SSL_ERROR_INIT_FAILED,
76    ECORE_CON_SSL_ERROR_SERVER_INIT_FAILED,
77    ECORE_CON_SSL_ERROR_SSL2_NOT_SUPPORTED
78 } Ecore_Con_Ssl_Error;
79
80 typedef enum _Ecore_Con_Ssl_Handshake
81 {
82    ECORE_CON_SSL_STATE_DONE = 0,
83    ECORE_CON_SSL_STATE_HANDSHAKING,
84    ECORE_CON_SSL_STATE_INIT
85 } Ecore_Con_Ssl_State;
86
87 typedef enum Ecore_Con_Proxy_State
88 {  /* named PROXY instead of SOCKS in case some handsome and enterprising
89     * developer decides to add HTTP CONNECT support
90     */
91    ECORE_CON_PROXY_STATE_DONE = 0,
92    ECORE_CON_PROXY_STATE_RESOLVED,
93    ECORE_CON_PROXY_STATE_INIT,
94    ECORE_CON_PROXY_STATE_READ,
95    ECORE_CON_PROXY_STATE_AUTH,
96    ECORE_CON_PROXY_STATE_REQUEST,
97    ECORE_CON_PROXY_STATE_CONFIRM,
98 } Ecore_Con_Proxy_State;
99
100 struct _Ecore_Con_Client
101 {
102    ECORE_MAGIC;
103    int fd;
104    Ecore_Con_Server *host_server;
105    void *data;
106    Ecore_Fd_Handler *fd_handler;
107    unsigned int buf_offset;
108    Eina_Binbuf *buf;
109    const char *ip;
110    Eina_List *event_count;
111    struct sockaddr *client_addr;
112    int client_addr_len;
113    double start_time;
114    Ecore_Timer *until_deletion;
115    double disconnect_time;
116 #if USE_GNUTLS
117    gnutls_datum_t session_ticket;
118    gnutls_session_t session;
119 #elif USE_OPENSSL
120    SSL *ssl;
121    int ssl_err;
122 #endif
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 disable_proxy : 1; /* proxy should never be used with this connection */
183    Eina_Bool ssl_prepared : 1;
184    Eina_Bool use_cert : 1; /* @c EINA_TRUE if using certificate auth */
185    Ecore_Con_Ssl_State ssl_state; /* current state of ssl handshake on the server */
186    Eina_Bool verify : 1; /* @c EINA_TRUE if certificates will be verified */
187    Eina_Bool verify_basic : 1; /* @c EINA_TRUE if certificates will be verified only against the hostname */
188    Eina_Bool reject_excess_clients : 1;
189    Eina_Bool delete_me : 1; /* del event has been queued */
190 #ifdef _WIN32
191    Eina_Bool want_write : 1;
192    Eina_Bool read_stop : 1;
193    Eina_Bool read_stopped : 1;
194    HANDLE pipe;
195    HANDLE thread_read;
196    HANDLE event_read;
197    HANDLE event_peek;
198    DWORD nbr_bytes;
199 #endif
200 };
201
202 #ifdef HAVE_CURL
203 struct _Ecore_Con_Url
204 {
205    ECORE_MAGIC;
206    CURL *curl_easy;
207    struct curl_slist *headers;
208    Eina_List *additional_headers;
209    Eina_List *response_headers;
210    const char *url;
211    long proxy_type;
212    int status;
213
214    Ecore_Timer *timer;
215
216    Ecore_Con_Url_Time time_condition;
217    double timestamp;
218    void *data;
219    
220    void *post_data;
221
222    int received;
223    int write_fd;
224
225    unsigned int event_count;
226    Eina_Bool dead : 1;
227    Eina_Bool multi : 1;
228 };
229 #endif
230
231 struct _Ecore_Con_Info
232 {
233    unsigned int size;
234    struct addrinfo info;
235    char ip[NI_MAXHOST];
236    char service[NI_MAXSERV];
237 };
238
239 struct _Ecore_Con_Lookup
240 {
241    Ecore_Con_Dns_Cb done_cb;
242    const void *data;
243 };
244
245 #define ECORE_CON_SOCKS_CAST_ELSE(X) \
246    Ecore_Con_Socks_v4 *v4 = NULL; \
247    Ecore_Con_Socks_v5 *v5 = NULL; \
248    if ((X) && ((X)->version == 4)) \
249      v4 = (Ecore_Con_Socks_v4*)(X); \
250    else if ((X) && ((X)->version == 5)) \
251      v5 = (Ecore_Con_Socks_v5*)(X); \
252    else
253
254 struct Ecore_Con_Socks /* v4 */
255 {
256    unsigned char version;
257
258    const char *ip;
259    int port;
260    const char *username;
261    unsigned int ulen;
262    Eina_Bool lookup : 1;
263    Eina_Bool bind : 1;
264 };
265
266 struct Ecore_Con_Socks_v5
267 {
268    unsigned char version;
269
270    const char *ip;
271    int port;
272    const char *username;
273    unsigned int ulen;
274    Eina_Bool lookup : 1;
275    Eina_Bool bind : 1;
276    /* v5 only */
277    unsigned char method;
278    const char *password;
279    unsigned int plen;
280 };
281
282 extern Ecore_Con_Socks *_ecore_con_proxy_once;
283 extern Ecore_Con_Socks *_ecore_con_proxy_global;
284 void ecore_con_socks_init(void);
285 void ecore_con_socks_shutdown(void);
286 Eina_Bool ecore_con_socks_svr_init(Ecore_Con_Server *svr);
287 void ecore_con_socks_read(Ecore_Con_Server *svr, unsigned char *buf, int num);
288 void ecore_con_socks_dns_cb(const char *canonname, const char *ip, struct sockaddr *addr, int addrlen, Ecore_Con_Server *svr);
289 /* from ecore_con.c */
290 void ecore_con_server_infos_del(Ecore_Con_Server *svr, void *info);
291 void ecore_con_event_proxy_bind(Ecore_Con_Server *svr);
292 void ecore_con_event_server_data(Ecore_Con_Server *svr, unsigned char *buf, int num, Eina_Bool duplicate);
293 void ecore_con_event_server_del(Ecore_Con_Server *svr);
294 #define ecore_con_event_server_error(svr, error) _ecore_con_event_server_error((svr), (char*)(error), EINA_TRUE)
295 void _ecore_con_event_server_error(Ecore_Con_Server *svr, char *error, Eina_Bool duplicate);
296 void ecore_con_event_client_add(Ecore_Con_Client *cl);
297 void ecore_con_event_client_data(Ecore_Con_Client *cl, unsigned char *buf, int num, Eina_Bool duplicate);
298 void ecore_con_event_client_del(Ecore_Con_Client *cl);
299 void ecore_con_event_client_error(Ecore_Con_Client *cl, const char *error);
300 void _ecore_con_server_kill(Ecore_Con_Server *svr);
301 void _ecore_con_client_kill(Ecore_Con_Client *cl);
302 /* from ecore_local_win32.c */
303 #ifdef _WIN32
304 Eina_Bool ecore_con_local_listen(Ecore_Con_Server *svr);
305 Eina_Bool ecore_con_local_connect(Ecore_Con_Server *svr,
306                             Eina_Bool (*cb_done)(void *data,
307                                                  Ecore_Fd_Handler *fd_handler));
308 Eina_Bool ecore_con_local_win32_server_flush(Ecore_Con_Server *svr);
309 Eina_Bool ecore_con_local_win32_client_flush(Ecore_Con_Client *cl);
310 void      ecore_con_local_win32_server_del(Ecore_Con_Server *svr);
311 void      ecore_con_local_win32_client_del(Ecore_Con_Client *cl);
312 #else
313 /* from ecore_local.c */
314 int ecore_con_local_init(void);
315 int ecore_con_local_shutdown(void);
316 int ecore_con_local_connect(Ecore_Con_Server *svr,
317                             Eina_Bool (*cb_done)(
318                                void *data,
319                                Ecore_Fd_Handler *fd_handler),
320                             void *data);
321 int ecore_con_local_listen(Ecore_Con_Server *svr,
322                            Eina_Bool (*cb_listen)(
323                               void *data,
324                               Ecore_Fd_Handler *fd_handler),
325                            void *data);
326 #endif
327
328 /* from ecore_con_info.c */
329 int                 ecore_con_info_init(void);
330 int                 ecore_con_info_shutdown(void);
331 int                 ecore_con_info_tcp_connect(Ecore_Con_Server *svr,
332                                                Ecore_Con_Info_Cb done_cb,
333                                                void *data);
334 int                 ecore_con_info_tcp_listen(Ecore_Con_Server *svr,
335                                               Ecore_Con_Info_Cb done_cb,
336                                               void *data);
337 int                 ecore_con_info_udp_connect(Ecore_Con_Server *svr,
338                                                Ecore_Con_Info_Cb done_cb,
339                                                void *data);
340 int                 ecore_con_info_udp_listen(Ecore_Con_Server *svr,
341                                               Ecore_Con_Info_Cb done_cb,
342                                               void *data);
343 int                 ecore_con_info_mcast_listen(Ecore_Con_Server *svr,
344                                                 Ecore_Con_Info_Cb done_cb,
345                                                 void *data);
346 void                ecore_con_info_data_clear(void *info);
347
348 void ecore_con_event_server_add(Ecore_Con_Server *svr);
349
350
351 /* from ecore_con_ssl.c */
352 Ecore_Con_Ssl_Error ecore_con_ssl_init(void);
353 Ecore_Con_Ssl_Error ecore_con_ssl_shutdown(void);
354 Ecore_Con_Ssl_Error ecore_con_ssl_server_prepare(Ecore_Con_Server *svr, int ssl_type);
355 Ecore_Con_Ssl_Error ecore_con_ssl_server_init(Ecore_Con_Server *svr);
356 Ecore_Con_Ssl_Error ecore_con_ssl_server_shutdown(Ecore_Con_Server *svr);
357 int                 ecore_con_ssl_server_read(Ecore_Con_Server *svr,
358                                               unsigned char *buf,
359                                               int size);
360 int                 ecore_con_ssl_server_write(Ecore_Con_Server *svr,
361                                                const unsigned char *buf,
362                                                int size);
363 Ecore_Con_Ssl_Error ecore_con_ssl_client_init(Ecore_Con_Client *svr);
364 Ecore_Con_Ssl_Error ecore_con_ssl_client_shutdown(Ecore_Con_Client *svr);
365 int                 ecore_con_ssl_client_read(Ecore_Con_Client *svr,
366                                               unsigned char *buf,
367                                               int size);
368 int                 ecore_con_ssl_client_write(Ecore_Con_Client *svr,
369                                                const unsigned char *buf,
370                                                int size);
371
372 int                 ecore_con_info_get(Ecore_Con_Server *svr,
373                                        Ecore_Con_Info_Cb done_cb,
374                                        void *data,
375                                        struct addrinfo *hints);
376
377
378 #define GENERIC_ALLOC_FREE_HEADER(TYPE, Type) \
379   TYPE *Type##_alloc(void);                   \
380   void Type##_free(TYPE *e);
381
382 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Client_Add, ecore_con_event_client_add);
383 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Client_Del, ecore_con_event_client_del);
384 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Client_Write, ecore_con_event_client_write);
385 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Client_Data, ecore_con_event_client_data);
386 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Server_Error, ecore_con_event_server_error);
387 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Client_Error, ecore_con_event_client_error);
388 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Server_Add, ecore_con_event_server_add);
389 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Server_Del, ecore_con_event_server_del);
390 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Server_Write, ecore_con_event_server_write);
391 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Server_Data, ecore_con_event_server_data);
392 GENERIC_ALLOC_FREE_HEADER(Ecore_Con_Event_Proxy_Bind, ecore_con_event_proxy_bind);
393
394 void ecore_con_mempool_init(void);
395 void ecore_con_mempool_shutdown(void);
396
397 #undef GENERIC_ALLOC_FREE_HEADER
398
399 #endif