aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/ecore_con/ecore_con_ares.c
blob: e1c7934f875583e3d490426d646f0de41d45b8af (plain) (blame)
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

/*
 * This version of ecore_con_info use c-ares to provide asynchronous dns lookup.
 *
 * Note: It doesn't fork nor does it use libc getaddrinfo.
 * http://c-ares.haxx.se/docs.html
 */

#include <string.h>
#include <sys/types.h>

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

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

#include <ares.h>

#include "Ecore.h"
#include "Ecore_Con.h"
#include "ecore_con_private.h"

typedef struct _Ecore_Con_FD    Ecore_Con_FD;
typedef struct _Ecore_Con_CAres Ecore_Con_CAres;

struct _Ecore_Con_FD
{
   Ecore_Fd_Handler *handler;
   Ecore_Timer      *timer;
   int               fd;
};

struct _Ecore_Con_CAres
{
   Ecore_Con_Server *svr;
   Ecore_Con_Info_Cb done_cb;
   void             *data;
   struct addrinfo   hints;
   Ecore_Con_Info   *result;

   union
   {
      struct in_addr  v4;
#ifdef HAVE_IPV6
      struct in6_addr v6;
#endif
   } addr;

   Eina_Bool         byaddr : 1;
   Eina_Bool         isv6 : 1;
};

static ares_channel info_channel;
static int info_init = 0;
static Eina_List *info_fds = NULL;

static void      _ecore_con_info_ares_nameinfo(Ecore_Con_CAres *arg,
                                                int status,
                                                int timeouts,
                                                char *node,
                                                char *service);
static void      _ecore_con_info_ares_host_cb(Ecore_Con_CAres *arg,
                                               int status,
                                               int timeouts,
                                               struct hostent *hostent);
static Eina_Bool _ecore_con_info_cares_fd_cb(Ecore_Con_FD *ecf,
                                              Ecore_Fd_Handler *fd_handler);
static Eina_Bool _ecore_con_info_cares_timeout_cb(void *data);

static void
                 _ecore_con_info_cares_state_cb(void *data,
                               ares_socket_t fd,
                               int readable,
                               int writable);
static int
                 _ecore_con_info_fds_search(const Ecore_Con_FD *fd1,
                           const Ecore_Con_FD *fd2);

int
ecore_con_info_init(void)
{
   struct ares_options opts;

   if (!info_init)
     {
        if (ares_library_init(ARES_LIB_INIT_ALL))
          return 0;

        opts.lookups = "fb"; /* hosts file then dns */
        opts.sock_state_cb = _ecore_con_info_cares_state_cb;

        if (ares_init_options(&info_channel, &opts,
                              ARES_OPT_LOOKUPS | ARES_OPT_SOCK_STATE_CB) != ARES_SUCCESS)
          {
             ares_library_cleanup();
             return 0;
          }
     }

   info_init++;
   return info_init;
}

int
ecore_con_info_shutdown(void)
{
   info_init--;
   if (info_init == 0)
     {
        /* Cancel all ongoing request */
        ares_cancel(info_channel);
        ares_destroy(info_channel);

        /* Shutdown ares */
        ares_library_cleanup();
     }

   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));
#ifdef HAVE_IPV6
   hints.ai_family = AF_INET6;
#else
   hints.ai_family = AF_INET;
#endif
   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));
#ifdef HAVE_IPV6
   hints.ai_family = AF_INET6;
#else
   hints.ai_family = AF_INET;
#endif
   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));
#ifdef HAVE_IPV6
   hints.ai_family = AF_INET6;
#else
   hints.ai_family = AF_INET;
#endif
   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));
#ifdef HAVE_IPV6
   hints.ai_family = AF_INET6;
#else
   hints.ai_family = AF_INET;
#endif
   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));
#ifdef HAVE_IPV6
   hints.ai_family = AF_INET6;
#else
   hints.ai_family = AF_INET;
#endif
   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);
}

static Eina_Bool
_ecore_con_info_ares_getnameinfo(Ecore_Con_CAres *arg,
                                 int addrtype,
                                 const char *name,
                                 struct sockaddr *addr,
                                 int addrlen)
{
   int length = 0;

   if (name)
     length = strlen(name) + 1;
   else
     length = 1;

   arg->result = malloc(sizeof(Ecore_Con_Info) + length);
   if (!arg->result)
     return EINA_FALSE;

   /* FIXME: What to do when hint is not set ? */
   arg->result->info.ai_flags = arg->hints.ai_flags;
   arg->result->info.ai_socktype = arg->hints.ai_socktype;
   arg->result->info.ai_protocol = arg->hints.ai_protocol;

   arg->result->info.ai_family = addrtype;
   arg->result->info.ai_addrlen = addrlen;
   arg->result->info.ai_addr = addr;
   arg->result->info.ai_canonname = (char *)(arg->result + 1);

   if (!name)
     *arg->result->info.ai_canonname = '\0';
   else
     strcpy(arg->result->info.ai_canonname, name);

   arg->result->info.ai_next = NULL;

   ares_getnameinfo(
     info_channel, addr, addrlen,
     ARES_NI_NUMERICSERV | ARES_NI_NUMERICHOST |
     ARES_NI_LOOKUPSERVICE | ARES_NI_LOOKUPHOST,
     (ares_nameinfo_callback)_ecore_con_info_ares_nameinfo, arg);

   return EINA_TRUE;
}

EAPI int
ecore_con_info_get(Ecore_Con_Server *obj,
                   Ecore_Con_Info_Cb done_cb,
                   void *data,
                   struct addrinfo *hints)
{
   Ecore_Con_Server_Data *svr = eo_data_scope_get(obj, ECORE_CON_SERVER_CLASS);
   Ecore_Con_CAres *cares;
#ifdef HAVE_IPV6
   int ai_family = AF_INET6;
#else
   int ai_family = AF_INET;
#endif

   cares = calloc(1, sizeof(Ecore_Con_CAres));
   if (!cares)
     return 0;

   cares->svr = obj;
   cares->done_cb = done_cb;
   cares->data = data;

   if (hints)
     {
        ai_family = hints->ai_family;
        memcpy(&cares->hints, hints, sizeof(struct addrinfo));
     }

   if (inet_pton(AF_INET, svr->ecs ? svr->ecs->ip : svr->name, &cares->addr.v4) == 1)
     {
        cares->byaddr = EINA_TRUE;
        cares->isv6 = EINA_FALSE;
        ares_gethostbyaddr(info_channel, &cares->addr.v4,
                           sizeof(cares->addr.v4),
                           AF_INET,
                           (ares_host_callback)_ecore_con_info_ares_host_cb,
                           cares);
     }
#ifdef HAVE_IPV6
   else if (inet_pton(AF_INET6, svr->ecs ? svr->ecs->ip : svr->name, &cares->addr.v6) == 1)
     {
        cares->byaddr = EINA_TRUE;
        cares->isv6 = EINA_TRUE;
        ares_gethostbyaddr(info_channel, &cares->addr.v6,
                           sizeof(cares->addr.v6),
                           AF_INET6,
                           (ares_host_callback)_ecore_con_info_ares_host_cb,
                           cares);
     }
#endif
   else
     {
        cares->byaddr = EINA_FALSE;
        ares_gethostbyname(info_channel, svr->ecs ? svr->ecs->ip : svr->name, ai_family,
                           (ares_host_callback)_ecore_con_info_ares_host_cb,
                           cares);
     }

   svr->infos = eina_list_append(svr->infos, cares);
   return 1;
}

void
ecore_con_info_data_clear(void *info)
{
   Ecore_Con_CAres *cares = info;
   if (cares) cares->data = NULL;
}

static Eina_Bool
_ecore_con_info_cares_timeout_cb(void *data EINA_UNUSED)
{
   ares_process_fd(info_channel, ARES_SOCKET_BAD, ARES_SOCKET_BAD);
   return ECORE_CALLBACK_RENEW;
}

static Eina_Bool
_ecore_con_info_cares_fd_cb(Ecore_Con_FD *ecf,
                            Ecore_Fd_Handler *fd_handler)
{
   ares_socket_t read_fd, write_fd;

   read_fd = write_fd = ARES_SOCKET_BAD;

   if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
     read_fd = ecf->fd;
   if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE))
     write_fd = ecf->fd;

   ares_process_fd(info_channel, read_fd, write_fd);

   return ECORE_CALLBACK_RENEW;
}

static int
_ecore_con_info_fds_search(const Ecore_Con_FD *fd1,
                           const Ecore_Con_FD *fd2)
{
   return fd1->fd - fd2->fd;
}

static void
_ecore_con_info_cares_state_cb(void *data EINA_UNUSED,
                               ares_socket_t fd,
                               int readable,
                               int writable)
{
   int flags = 0;
   Ecore_Con_FD *search = NULL, *ecf = NULL;

   search = eina_list_search_unsorted(info_fds,
                                      (Eina_Compare_Cb)_ecore_con_info_fds_search, &ecf);

   if (!(readable | writable))
     {
        ares_process_fd(info_channel, ARES_SOCKET_BAD, ARES_SOCKET_BAD);
        if (search)
          {
             info_fds = eina_list_remove(info_fds, search);
             ecore_timer_del(search->timer);
             ecore_main_fd_handler_del(search->handler);
             free(search);
          }
        return;
     }

   if (!search)
     {
        search = malloc(sizeof(Ecore_Con_FD));
        EINA_SAFETY_ON_NULL_RETURN(search);

        search->fd = fd;
        search->handler = ecore_main_fd_handler_add(fd, ECORE_FD_WRITE | ECORE_FD_READ,
                                                    (Ecore_Fd_Cb)_ecore_con_info_cares_fd_cb, search, NULL, NULL);
        /* c-ares default timeout is 5 seconds */
        search->timer = ecore_timer_add(5, _ecore_con_info_cares_timeout_cb, NULL);
        info_fds = eina_list_append(info_fds, search);
     }

   if (readable) flags |= ECORE_FD_READ;
   if (writable) flags |= ECORE_FD_WRITE;
   ecore_main_fd_handler_active_set(search->handler, flags);
}

static void
_ecore_con_info_ares_host_cb(Ecore_Con_CAres *arg,
                             int status,
                             int timeouts EINA_UNUSED,
                             struct hostent *hostent)
{
   struct sockaddr *addr;
   Ecore_Con_Server_Data *svr;
   int addrlen;

   svr = eo_data_scope_get(arg->svr, ECORE_CON_SERVER_CLASS);

   /* Found something ? */
   switch (status)
     {
      case ARES_SUCCESS:
        if (!hostent->h_addr_list[0])
          {
             ERR("No IP found");
             goto on_error;
          }

        switch (hostent->h_addrtype)
          {
           case AF_INET:
           {
              struct sockaddr_in *addri;

              addrlen = sizeof(struct sockaddr_in);
              addri = malloc(addrlen);

              if (!addri)
                goto on_mem_error;

              addri->sin_family = AF_INET;
              addri->sin_port = htons(svr->ecs ? svr->ecs->port : svr->port);

              memcpy(&addri->sin_addr.s_addr,
                     hostent->h_addr_list[0], sizeof(struct in_addr));

              addr = (struct sockaddr *)addri;
              break;
           }

#ifdef HAVE_IPV6
           case AF_INET6:
           {
              struct sockaddr_in6 *addri6;

              addrlen = sizeof(struct sockaddr_in6);
              addri6 = malloc(addrlen);

              if (!addri6)
                goto on_mem_error;

              addri6->sin6_family = AF_INET6;
              addri6->sin6_port = htons(svr->ecs ? svr->ecs->port : svr->port);
              addri6->sin6_flowinfo = 0;
              addri6->sin6_scope_id = 0;

              memcpy(&addri6->sin6_addr.s6_addr,
                     hostent->h_addr_list[0], sizeof(struct in6_addr));

              addr = (struct sockaddr *)addri6;
              break;
           }

#endif
           default:
             ERR("Unknown addrtype %i", hostent->h_addrtype);
             goto on_error;
          }

        if (!_ecore_con_info_ares_getnameinfo(arg, hostent->h_addrtype,
                                              hostent->h_name,
                                              addr, addrlen))
          goto on_error;

        break;

      case ARES_ENOTFOUND: /* address notfound */
        if (arg->byaddr)
          {
#ifdef HAVE_IPV6
             /* This happen when host doesn't have a reverse. */
             if (arg->isv6)
               {
                  struct sockaddr_in6 *addri6;

                  addrlen = sizeof(struct sockaddr_in6);
                  addri6 = malloc(addrlen);

                  if (!addri6)
                    goto on_mem_error;

                  addri6->sin6_family = AF_INET6;
                  addri6->sin6_port = htons(svr->ecs ? svr->ecs->port : svr->port);
                  addri6->sin6_flowinfo = 0;
                  addri6->sin6_scope_id = 0;

                  memcpy(&addri6->sin6_addr.s6_addr,
                         &arg->addr.v6, sizeof(struct in6_addr));

                  addr = (struct sockaddr *)addri6;
               }
             else
#endif
             {
                struct sockaddr_in *addri;

                addrlen = sizeof(struct sockaddr_in);
                addri = malloc(addrlen);

                if (!addri)
                  goto on_mem_error;

                addri->sin_family = AF_INET;
                addri->sin_port = htons(svr->ecs ? svr->ecs->port : svr->port);

                memcpy(&addri->sin_addr.s_addr,
                       &arg->addr.v4, sizeof(struct in_addr));

                addr = (struct sockaddr *)addri;
             }

             if (!_ecore_con_info_ares_getnameinfo(arg,
#ifdef HAVE_IPV6
                                                   arg->isv6 ? AF_INET6 :
#endif
                                                   AF_INET,
                                                   NULL, addr,
                                                   addrlen))
               goto on_error;

             break;
          }

      case ARES_ENOTIMP: /* unknown family */
      case ARES_EBADNAME: /* not a valid internet address */
      case ARES_ENOMEM: /* not enough memory */
      case ARES_EDESTRUCTION: /* request canceled, shuting down */
      case ARES_ENODATA: /* no data returned */
      case ARES_ECONNREFUSED: /* connection refused */
      case ARES_ETIMEOUT: /* connection timed out */
        ecore_con_event_server_error(arg->svr, ares_strerror(status));
        goto on_error;

      default:
        ERR("Unknown status returned by c-ares: %i assuming error", status);
        ecore_con_event_server_error(arg->svr, ares_strerror(status));
        goto on_error;
     }

   return;

on_mem_error:
   ERR("Not enough memory");

on_error:
   if (arg->data)
     {
        ecore_con_server_infos_del(arg->data, arg);
        arg->done_cb(arg->data, NULL);
     }
   free(arg);
}

static void
_ecore_con_info_ares_nameinfo(Ecore_Con_CAres *arg,
                              int status,
                              int timeouts EINA_UNUSED,
                              char *node,
                              char *service)
{
   switch (status)
     {
      case ARES_SUCCESS:
        if (node)
          strcpy(arg->result->ip, node);
        else
          *arg->result->ip = '\0';

        if (service)
          strcpy(arg->result->service, service);
        else
          *arg->result->service = '\0';

        if (arg->data) arg->done_cb(arg->data, arg->result);
        break;

      case ARES_ENOTIMP:
      case ARES_ENOTFOUND:
      case ARES_ENOMEM:
      case ARES_EDESTRUCTION:
      case ARES_EBADFLAGS:
        ecore_con_event_server_error(arg->svr, ares_strerror(status));
        if (arg->data) arg->done_cb(arg->data, NULL);
        break;
     }

   free(arg->result->info.ai_addr);
   free(arg->result);
   if (arg->data) ecore_con_server_infos_del(arg->data, arg);
   free(arg);
}