aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ecore/src/lib/ecore_con/ecore_con_info.c
blob: 4ece6b00c15fef40e567413b54366844850e4294 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
 * getaddrinfo with callback
 *
 * man getaddrinfo
 *
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#ifdef HAVE_ALLOCA_H
# include <alloca.h>
#elif defined __GNUC__
# define alloca __builtin_alloca
#elif defined _AIX
# define alloca __alloca
#elif defined _MSC_VER
# include <malloc.h>
# define alloca _alloca
#else
# include <stddef.h>
# ifdef  __cplusplus
extern "C"
# endif
void *alloca(size_t);
#endif

#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <ctype.h>
#ifdef __OpenBSD__
# include <sys/types.h>
#endif

#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif

#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif

#ifdef HAVE_ARPA_NAMESER_H
# include <arpa/nameser.h>
#endif

#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif

#ifdef HAVE_NETDB_H
# include <netdb.h>
#endif

#include <errno.h>

#include "Ecore.h"
#include "ecore_private.h"
#include "ecore_con_private.h"

typedef struct _CB_Data CB_Data;

struct _CB_Data
{
                        EINA_INLIST;
   Ecore_Con_Info_Cb    cb_done;
   void                *data;
   Ecore_Fd_Handler    *fdh;
   pid_t                pid;
   Ecore_Event_Handler *handler;
   int                  fd2;
};

static void      _ecore_con_info_readdata(CB_Data *cbdata);
static void      _ecore_con_info_slave_free(CB_Data *cbdata);
static Eina_Bool _ecore_con_info_data_handler(void             *data,
                                              Ecore_Fd_Handler *fd_handler);
static Eina_Bool _ecore_con_info_exit_handler(void    *data,
                                              int type __UNUSED__,
                                              void    *event);

static int info_init = 0;
static CB_Data *info_slaves = NULL;

int
ecore_con_info_init(void)
{
   info_init++;
   return info_init;
}

int
ecore_con_info_shutdown(void)
{
   info_init--;
   if (info_init == 0)
     while (info_slaves) _ecore_con_info_slave_free(info_slaves);

   return info_init;
}

int
ecore_con_info_tcp_connect(Ecore_Con_Server *svr,
                           Ecore_Con_Info_Cb done_cb,
                           void             *data)
{
   struct addrinfo hints;

   memset(&hints, 0, sizeof(struct addrinfo));
   hints.ai_family = AF_UNSPEC;
   hints.ai_socktype = SOCK_STREAM;
   hints.ai_flags = AI_CANONNAME;
   hints.ai_protocol = IPPROTO_TCP;
   hints.ai_canonname = NULL;
   hints.ai_next = NULL;
   hints.ai_addr = NULL;

   return ecore_con_info_get(svr, done_cb, data, &hints);
}

int
ecore_con_info_tcp_listen(Ecore_Con_Server *svr,
                          Ecore_Con_Info_Cb done_cb,
                          void             *data)
{
   struct addrinfo hints;

   memset(&hints, 0, sizeof(struct addrinfo));
   hints.ai_family = AF_UNSPEC;
   hints.ai_socktype = SOCK_STREAM;
   hints.ai_flags = AI_PASSIVE;
   hints.ai_protocol = IPPROTO_TCP;
   hints.ai_canonname = NULL;
   hints.ai_next = NULL;
   hints.ai_addr = NULL;

   return ecore_con_info_get(svr, done_cb, data, &hints);
}

int
ecore_con_info_udp_connect(Ecore_Con_Server *svr,
                           Ecore_Con_Info_Cb done_cb,
                           void             *data)
{
   struct addrinfo hints;

   memset(&hints, 0, sizeof(struct addrinfo));
   hints.ai_family = AF_UNSPEC;
   hints.ai_socktype = SOCK_DGRAM;
   hints.ai_flags = AI_CANONNAME;
   hints.ai_protocol = IPPROTO_UDP;
   hints.ai_canonname = NULL;
   hints.ai_next = NULL;
   hints.ai_addr = NULL;

   return ecore_con_info_get(svr, done_cb, data, &hints);
}

int
ecore_con_info_udp_listen(Ecore_Con_Server *svr,
                          Ecore_Con_Info_Cb done_cb,
                          void             *data)
{
   struct addrinfo hints;

   memset(&hints, 0, sizeof(struct addrinfo));
   hints.ai_family = AF_UNSPEC;
   hints.ai_socktype = SOCK_DGRAM;
   hints.ai_flags = AI_PASSIVE;
   hints.ai_protocol = IPPROTO_UDP;
   hints.ai_canonname = NULL;
   hints.ai_next = NULL;
   hints.ai_addr = NULL;

   return ecore_con_info_get(svr, done_cb, data, &hints);
}

int
ecore_con_info_mcast_listen(Ecore_Con_Server *svr,
                            Ecore_Con_Info_Cb done_cb,
                            void             *data)
{
   struct addrinfo hints;

   memset(&hints, 0, sizeof(struct addrinfo));
   hints.ai_family = AF_UNSPEC;
   hints.ai_socktype = SOCK_DGRAM;
   hints.ai_flags = 0;
   hints.ai_protocol = IPPROTO_UDP;
   hints.ai_canonname = NULL;
   hints.ai_next = NULL;
   hints.ai_addr = NULL;

   return ecore_con_info_get(svr, done_cb, data, &hints);
}

EAPI int
ecore_con_info_get(Ecore_Con_Server *svr,
                   Ecore_Con_Info_Cb done_cb,
                   void             *data,
                   struct addrinfo  *hints)
{
   CB_Data *cbdata;
   int fd[2];

   if (pipe(fd) < 0)
     {
        ecore_con_event_server_error(svr, strerror(errno));
        return 0;
     }

   cbdata = calloc(1, sizeof(CB_Data));
   if (!cbdata)
     {
        close(fd[0]);
        close(fd[1]);
        return 0;
     }

   cbdata->cb_done = done_cb;
   cbdata->data = data;
   cbdata->fd2 = fd[1];
   if (!(cbdata->fdh = ecore_main_fd_handler_add(fd[0], ECORE_FD_READ,
                                                 _ecore_con_info_data_handler,
                                                 cbdata,
                                                 NULL, NULL)))
     {
        ecore_con_event_server_error(svr, "Memory allocation failure");
        free(cbdata);
        close(fd[0]);
        close(fd[1]);
        return 0;
     }

   if ((cbdata->pid = fork()) == 0)
     {
        Ecore_Con_Info *container;
        struct addrinfo *result = NULL;
        char service[NI_MAXSERV] = {0};
        char hbuf[NI_MAXHOST] = {0};
        char sbuf[NI_MAXSERV] = {0};
        unsigned char *tosend = NULL;
        int tosend_len;
        int canonname_len = 0;
        int err;

        eina_convert_itoa(svr->port, service);
        /* CHILD */
        if (!getaddrinfo(svr->name, service, hints, &result) && result)
          {
             if (result->ai_canonname)
               canonname_len = strlen(result->ai_canonname) + 1;

             tosend_len = sizeof(Ecore_Con_Info) + result->ai_addrlen +
               canonname_len;

             tosend = alloca(tosend_len);
             memset(tosend, 0, tosend_len);

             container = (Ecore_Con_Info *)tosend;
             container->size = tosend_len;

             memcpy(&container->info,
                    result,
                    sizeof(struct addrinfo));
             memcpy(tosend + sizeof(Ecore_Con_Info),
                    result->ai_addr,
                    result->ai_addrlen);
             if (result->ai_canonname) /* FIXME: else... */
               memcpy(tosend + sizeof(Ecore_Con_Info) + result->ai_addrlen,
                      result->ai_canonname,
                      canonname_len);

             if (!getnameinfo(result->ai_addr, result->ai_addrlen,
                              hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
                              NI_NUMERICHOST | NI_NUMERICSERV))
               {
                  memcpy(container->ip, hbuf, sizeof(container->ip));
                  memcpy(container->service, sbuf, sizeof(container->service));
               }

             err = write(fd[1], tosend, tosend_len);
          }

        if (result)
          freeaddrinfo(result);

        err = write(fd[1], "", 1);
        close(fd[1]);
#if defined(__USE_ISOC99) && !defined(__UCLIBC__)
        _Exit(0);
#else
        _exit(0);
#endif
     }

   /* PARENT */
   cbdata->handler =
     ecore_event_handler_add(ECORE_EXE_EVENT_DEL, _ecore_con_info_exit_handler,
                             cbdata);
   close(fd[1]);
   if (!cbdata->handler)
     {
        ecore_main_fd_handler_del(cbdata->fdh);
        free(cbdata);
        close(fd[0]);
        return 0;
     }

   info_slaves = (CB_Data *)eina_inlist_append(EINA_INLIST_GET(
                                                 info_slaves),
                                               EINA_INLIST_GET(cbdata));
   svr->infos = eina_list_append(svr->infos, cbdata);
   return 1;
}

void
ecore_con_info_data_clear(void *info)
{
   CB_Data *cbdata = info;
   cbdata->data = NULL;
}

static void
_ecore_con_info_readdata(CB_Data *cbdata)
{
   Ecore_Con_Info container;
   Ecore_Con_Info *recv_info;
   unsigned char *torecv;
   int torecv_len;

   ssize_t size;

   size = read(ecore_main_fd_handler_fd_get(cbdata->fdh), &container,
               sizeof(Ecore_Con_Info));
   if (size == sizeof(Ecore_Con_Info))
     {
        torecv_len = container.size;
        torecv = malloc(torecv_len);

        memcpy(torecv, &container, sizeof(Ecore_Con_Info));

        size = read(ecore_main_fd_handler_fd_get(cbdata->fdh),
                    torecv + sizeof(Ecore_Con_Info),
                    torecv_len - sizeof(Ecore_Con_Info));
        if ((size > 0) &&
            ((size_t)size == torecv_len - sizeof(Ecore_Con_Info)))
          {
             recv_info = (Ecore_Con_Info *)torecv;

             recv_info->info.ai_addr =
               (struct sockaddr *)(torecv + sizeof(Ecore_Con_Info));
             if ((size_t)torecv_len !=
                 (sizeof(Ecore_Con_Info) + recv_info->info.ai_addrlen))
               recv_info->info.ai_canonname = (char *)
                 (torecv + sizeof(Ecore_Con_Info) + recv_info->info.ai_addrlen);
             else
               recv_info->info.ai_canonname = NULL;

             recv_info->info.ai_next = NULL;

             if (cbdata->data)
               {
                  cbdata->cb_done(cbdata->data, recv_info);
                  ecore_con_server_infos_del(cbdata->data, cbdata);
               }

             free(torecv);
          }
        else
          {
             if (cbdata->data)
               {
                  cbdata->cb_done(cbdata->data, NULL);
                  ecore_con_server_infos_del(cbdata->data, cbdata);
               }
          }
     }
   else
     {
        if (cbdata->data)
          {
             ecore_con_event_server_error(cbdata->data, strerror(errno));
             cbdata->cb_done(cbdata->data, NULL);
             ecore_con_server_infos_del(cbdata->data, cbdata);
          }
     }

   cbdata->cb_done = NULL;
}

static void
_ecore_con_info_slave_free(CB_Data *cbdata)
{
   info_slaves = (CB_Data *)eina_inlist_remove(EINA_INLIST_GET(info_slaves),
                                               EINA_INLIST_GET(cbdata));
   ecore_main_fd_handler_del(cbdata->fdh);
   ecore_event_handler_del(cbdata->handler);
   close(ecore_main_fd_handler_fd_get(cbdata->fdh));
   if (cbdata->data) ecore_con_server_infos_del(cbdata->data, cbdata);
   free(cbdata);
}

static Eina_Bool
_ecore_con_info_data_handler(void             *data,
                             Ecore_Fd_Handler *fd_handler)
{
   CB_Data *cbdata;

   cbdata = data;
   if (cbdata->cb_done)
     {
        if (ecore_main_fd_handler_active_get(fd_handler,
                                             ECORE_FD_READ))
          _ecore_con_info_readdata(cbdata);
        else
          {
             if (cbdata->data)
               {
                  cbdata->cb_done(cbdata->data, NULL);
                  cbdata->cb_done = NULL;
                  ecore_con_server_infos_del(cbdata->data, cbdata);
               }
          }
     }

   _ecore_con_info_slave_free(cbdata);
   return ECORE_CALLBACK_CANCEL;
}

static Eina_Bool
_ecore_con_info_exit_handler(void    *data,
                             int type __UNUSED__,
                             void    *event)
{
   CB_Data *cbdata;
   Ecore_Exe_Event_Del *ev;

   ev = event;
   cbdata = data;
   if (cbdata->pid != ev->pid)
     return ECORE_CALLBACK_RENEW;

   return ECORE_CALLBACK_CANCEL; /* FIXME: Woot ??? */
   _ecore_con_info_slave_free(cbdata);
   return ECORE_CALLBACK_CANCEL;
}