2009-12-03 02:26:40 -08:00
|
|
|
#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 <arpa/inet.h>
|
|
|
|
#include <ares.h>
|
|
|
|
|
|
|
|
#include "Ecore.h"
|
|
|
|
#include "Ecore_Con.h"
|
|
|
|
#include "ecore_con_private.h"
|
|
|
|
|
2010-11-03 10:58:37 -07:00
|
|
|
typedef struct _Ecore_Con_FD Ecore_Con_FD;
|
2009-12-03 02:26:40 -08:00
|
|
|
typedef struct _Ecore_Con_CAres Ecore_Con_CAres;
|
|
|
|
|
|
|
|
struct _Ecore_Con_FD
|
|
|
|
{
|
|
|
|
Ecore_Fd_Handler *handler;
|
2010-11-03 10:58:37 -07:00
|
|
|
int active;
|
|
|
|
int fd;
|
2009-12-03 02:26:40 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _Ecore_Con_CAres
|
|
|
|
{
|
|
|
|
Ecore_Con_Server *svr;
|
|
|
|
Ecore_Con_Info_Cb done_cb;
|
2010-11-03 10:58:37 -07:00
|
|
|
void *data;
|
|
|
|
struct addrinfo hints;
|
|
|
|
Ecore_Con_Info *result;
|
2009-12-03 02:26:40 -08:00
|
|
|
|
|
|
|
union {
|
2010-11-03 10:58:37 -07:00
|
|
|
struct in_addr v4;
|
2009-12-03 02:26:40 -08:00
|
|
|
struct in6_addr v6;
|
|
|
|
} addr;
|
|
|
|
|
2010-06-07 06:08:19 -07:00
|
|
|
Eina_Bool byaddr : 1;
|
|
|
|
Eina_Bool isv6 : 1;
|
2009-12-03 02:26:40 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
static ares_channel info_channel;
|
|
|
|
static int info_init = 0;
|
|
|
|
static Eina_List *info_fds = NULL;
|
|
|
|
static int active = 0;
|
|
|
|
static Ecore_Timer *tm = NULL;
|
|
|
|
static fd_set info_readers, info_writers;
|
|
|
|
|
2010-11-03 10:58:37 -07:00
|
|
|
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(void *data,
|
2010-07-26 23:30:27 -07:00
|
|
|
Ecore_Fd_Handler *fd_handler);
|
2010-06-24 09:15:56 -07:00
|
|
|
static Eina_Bool _ecore_con_info_cares_timeout_cb(void *data);
|
2010-07-26 23:30:27 -07:00
|
|
|
static void _ecore_con_info_cares_clean(void);
|
2009-12-03 02:26:40 -08:00
|
|
|
|
2010-01-04 11:35:21 -08:00
|
|
|
int
|
2009-12-03 02:26:40 -08:00
|
|
|
ecore_con_info_init(void)
|
|
|
|
{
|
|
|
|
if (info_init == 0)
|
|
|
|
{
|
2010-07-26 23:30:27 -07:00
|
|
|
if (ares_library_init(ARES_LIB_INIT_ALL) != 0)
|
2010-11-03 10:58:37 -07:00
|
|
|
return 0;
|
2010-07-26 23:30:27 -07:00
|
|
|
|
|
|
|
if (ares_init(&info_channel) != ARES_SUCCESS)
|
|
|
|
{
|
|
|
|
ares_library_cleanup();
|
|
|
|
return 0;
|
|
|
|
}
|
2009-12-03 02:26:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
info_init++;
|
|
|
|
return info_init;
|
|
|
|
}
|
|
|
|
|
2010-01-04 11:35:21 -08:00
|
|
|
int
|
2009-12-03 02:26:40 -08:00
|
|
|
ecore_con_info_shutdown(void)
|
|
|
|
{
|
|
|
|
info_init--;
|
|
|
|
if (info_init == 0)
|
|
|
|
{
|
2010-07-26 23:30:27 -07:00
|
|
|
/* Cancel all ongoing request */
|
2010-11-03 10:58:37 -07:00
|
|
|
ares_cancel(info_channel);
|
|
|
|
ares_destroy(info_channel);
|
2009-12-03 02:26:40 -08:00
|
|
|
|
2010-11-03 10:58:37 -07:00
|
|
|
/* Destroy FD handler here. */
|
|
|
|
/* Shutdown ares */
|
|
|
|
ares_library_cleanup();
|
2009-12-03 02:26:40 -08:00
|
|
|
}
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2009-12-03 02:26:40 -08:00
|
|
|
return info_init;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ecore_con_info_tcp_connect(Ecore_Con_Server *svr,
|
2010-07-26 23:30:27 -07:00
|
|
|
Ecore_Con_Info_Cb done_cb,
|
2010-11-03 10:58:37 -07:00
|
|
|
void *data)
|
2009-12-03 02:26:40 -08:00
|
|
|
{
|
|
|
|
struct addrinfo hints;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
2010-06-07 06:08:19 -07:00
|
|
|
hints.ai_family = AF_INET6;
|
2009-12-03 02:26:40 -08:00
|
|
|
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,
|
2010-07-26 23:30:27 -07:00
|
|
|
Ecore_Con_Info_Cb done_cb,
|
2010-11-03 10:58:37 -07:00
|
|
|
void *data)
|
2009-12-03 02:26:40 -08:00
|
|
|
{
|
|
|
|
struct addrinfo hints;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
2010-06-07 06:08:19 -07:00
|
|
|
hints.ai_family = AF_INET6;
|
2009-12-03 02:26:40 -08:00
|
|
|
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,
|
2010-07-26 23:30:27 -07:00
|
|
|
Ecore_Con_Info_Cb done_cb,
|
2010-11-03 10:58:37 -07:00
|
|
|
void *data)
|
2009-12-03 02:26:40 -08:00
|
|
|
{
|
|
|
|
struct addrinfo hints;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
2010-06-07 06:08:19 -07:00
|
|
|
hints.ai_family = AF_INET6;
|
2009-12-03 02:26:40 -08:00
|
|
|
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,
|
2010-07-26 23:30:27 -07:00
|
|
|
Ecore_Con_Info_Cb done_cb,
|
2010-11-03 10:58:37 -07:00
|
|
|
void *data)
|
2009-12-03 02:26:40 -08:00
|
|
|
{
|
|
|
|
struct addrinfo hints;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
2010-06-07 06:08:19 -07:00
|
|
|
hints.ai_family = AF_INET6;
|
2009-12-03 02:26:40 -08:00
|
|
|
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,
|
2010-07-26 23:30:27 -07:00
|
|
|
Ecore_Con_Info_Cb done_cb,
|
2010-11-03 10:58:37 -07:00
|
|
|
void *data)
|
2009-12-03 02:26:40 -08:00
|
|
|
{
|
|
|
|
struct addrinfo hints;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
2010-06-07 06:08:19 -07:00
|
|
|
hints.ai_family = AF_INET6;
|
2009-12-03 02:26:40 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-06-07 06:08:19 -07:00
|
|
|
static Eina_Bool
|
|
|
|
_ecore_con_info_ares_getnameinfo(Ecore_Con_CAres *arg,
|
2010-11-03 10:58:37 -07:00
|
|
|
int addrtype,
|
|
|
|
const char *name,
|
|
|
|
struct sockaddr *addr,
|
|
|
|
int addrlen)
|
2010-06-07 06:08:19 -07:00
|
|
|
{
|
|
|
|
int length = 0;
|
|
|
|
|
|
|
|
if (name)
|
2010-11-03 10:58:37 -07:00
|
|
|
length = strlen(name) + 1;
|
2010-06-07 06:08:19 -07:00
|
|
|
else
|
2010-11-03 10:58:37 -07:00
|
|
|
length = 1;
|
2010-06-07 06:08:19 -07:00
|
|
|
|
2010-11-30 20:42:29 -08:00
|
|
|
arg->result = malloc(sizeof(Ecore_Con_Info) + length);
|
2010-06-07 06:08:19 -07:00
|
|
|
if (!arg->result)
|
2010-11-03 10:58:37 -07:00
|
|
|
return EINA_FALSE;
|
2010-06-07 06:08:19 -07:00
|
|
|
|
|
|
|
/* 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;
|
2010-07-26 23:30:27 -07:00
|
|
|
arg->result->info.ai_canonname = (char *)(arg->result + 1);
|
2010-06-07 06:08:19 -07:00
|
|
|
|
|
|
|
if (!name)
|
2010-11-03 10:58:37 -07:00
|
|
|
*arg->result->info.ai_canonname = '\0';
|
2010-06-07 06:08:19 -07:00
|
|
|
else
|
2010-11-03 10:58:37 -07:00
|
|
|
strcpy(arg->result->info.ai_canonname, name);
|
2010-06-07 06:08:19 -07:00
|
|
|
|
|
|
|
arg->result->info.ai_next = NULL;
|
|
|
|
|
2010-07-26 23:30:27 -07:00
|
|
|
ares_getnameinfo(
|
2010-11-03 10:58:37 -07:00
|
|
|
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);
|
2010-06-07 06:08:19 -07:00
|
|
|
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
2009-12-03 02:26:40 -08:00
|
|
|
EAPI int
|
|
|
|
ecore_con_info_get(Ecore_Con_Server *svr,
|
2010-07-26 23:30:27 -07:00
|
|
|
Ecore_Con_Info_Cb done_cb,
|
2010-11-03 10:58:37 -07:00
|
|
|
void *data,
|
|
|
|
struct addrinfo *hints)
|
2009-12-03 02:26:40 -08:00
|
|
|
{
|
|
|
|
Ecore_Con_CAres *cares;
|
2010-06-07 06:08:19 -07:00
|
|
|
int ai_family = AF_INET6;
|
2009-12-03 02:26:40 -08:00
|
|
|
|
2010-11-30 20:42:29 -08:00
|
|
|
cares = calloc(1, sizeof(Ecore_Con_CAres));
|
2010-07-26 23:30:27 -07:00
|
|
|
if (!cares)
|
2010-11-03 10:58:37 -07:00
|
|
|
return 0;
|
2009-12-03 02:26:40 -08:00
|
|
|
|
|
|
|
cares->svr = svr;
|
|
|
|
cares->done_cb = done_cb;
|
|
|
|
cares->data = data;
|
|
|
|
|
|
|
|
if (hints)
|
|
|
|
{
|
2010-07-26 23:30:27 -07:00
|
|
|
ai_family = hints->ai_family;
|
2010-11-30 20:42:29 -08:00
|
|
|
memcpy(&cares->hints, hints, sizeof(struct addrinfo));
|
2009-12-03 02:26:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (inet_pton(AF_INET, svr->name, &cares->addr.v4) == 1)
|
|
|
|
{
|
2010-07-26 23:30:27 -07:00
|
|
|
cares->byaddr = EINA_TRUE;
|
|
|
|
cares->isv6 = EINA_FALSE;
|
|
|
|
ares_gethostbyaddr(info_channel, &cares->addr.v4,
|
2010-11-30 20:42:29 -08:00
|
|
|
sizeof(cares->addr.v4),
|
2010-07-26 23:30:27 -07:00
|
|
|
AF_INET,
|
|
|
|
(ares_host_callback)_ecore_con_info_ares_host_cb,
|
|
|
|
cares);
|
2009-12-03 02:26:40 -08:00
|
|
|
}
|
|
|
|
else if (inet_pton(AF_INET6, svr->name, &cares->addr.v6) == 1)
|
|
|
|
{
|
2010-07-26 23:30:27 -07:00
|
|
|
cares->byaddr = EINA_TRUE;
|
|
|
|
cares->isv6 = EINA_TRUE;
|
|
|
|
ares_gethostbyaddr(info_channel, &cares->addr.v6,
|
2010-11-30 20:42:29 -08:00
|
|
|
sizeof(cares->addr.v6),
|
2010-07-26 23:30:27 -07:00
|
|
|
AF_INET6,
|
|
|
|
(ares_host_callback)_ecore_con_info_ares_host_cb,
|
|
|
|
cares);
|
2009-12-03 02:26:40 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-07-26 23:30:27 -07:00
|
|
|
cares->byaddr = EINA_FALSE;
|
|
|
|
ares_gethostbyname(info_channel, svr->name, ai_family,
|
|
|
|
(ares_host_callback)_ecore_con_info_ares_host_cb,
|
|
|
|
cares);
|
2009-12-03 02:26:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
_ecore_con_info_cares_clean();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2010-11-03 10:58:37 -07:00
|
|
|
_ecore_con_info_fds_search(const Ecore_Con_FD *fd1,
|
|
|
|
const Ecore_Con_FD *fd2)
|
2009-12-03 02:26:40 -08:00
|
|
|
{
|
|
|
|
return fd1->fd - fd2->fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_Bool
|
|
|
|
_ecore_con_info_fds_lookup(int fd)
|
|
|
|
{
|
|
|
|
Ecore_Con_FD fdl;
|
|
|
|
Ecore_Con_FD *search;
|
|
|
|
|
|
|
|
fdl.fd = fd;
|
|
|
|
|
2010-07-26 23:30:27 -07:00
|
|
|
search = eina_list_search_unsorted(
|
2010-11-03 10:58:37 -07:00
|
|
|
info_fds, (Eina_Compare_Cb)_ecore_con_info_fds_search, &fdl);
|
2009-12-03 02:26:40 -08:00
|
|
|
|
|
|
|
if (search)
|
|
|
|
{
|
2010-07-26 23:30:27 -07:00
|
|
|
search->active = active;
|
|
|
|
return EINA_TRUE;
|
2009-12-03 02:26:40 -08:00
|
|
|
}
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2009-12-03 02:26:40 -08:00
|
|
|
return EINA_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_ecore_con_info_cares_clean(void)
|
|
|
|
{
|
|
|
|
fd_set readers, writers;
|
|
|
|
Eina_List *l, *l_next;
|
|
|
|
Ecore_Con_FD *ecf;
|
|
|
|
int nfds;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
FD_ZERO(&readers);
|
|
|
|
FD_ZERO(&writers);
|
|
|
|
nfds = ares_fds(info_channel, &readers, &writers);
|
|
|
|
|
|
|
|
active++;
|
|
|
|
for (i = 0; i < nfds; ++i)
|
|
|
|
{
|
2010-07-26 23:30:27 -07:00
|
|
|
int flags = 0;
|
|
|
|
|
|
|
|
if (FD_ISSET(i, &readers))
|
2010-11-03 10:58:37 -07:00
|
|
|
flags |= ECORE_FD_READ;
|
2010-07-26 23:30:27 -07:00
|
|
|
|
|
|
|
if (FD_ISSET(i, &writers))
|
2010-11-03 10:58:37 -07:00
|
|
|
flags |= ECORE_FD_WRITE;
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2010-11-30 20:42:29 -08:00
|
|
|
if (flags && (!_ecore_con_info_fds_lookup(i)))
|
|
|
|
{
|
|
|
|
ecf = malloc(sizeof(Ecore_Con_FD));
|
|
|
|
if (ecf)
|
|
|
|
{
|
|
|
|
ecf->fd = i;
|
|
|
|
ecf->active = active;
|
|
|
|
ecf->handler = ecore_main_fd_handler_add(
|
|
|
|
i, ECORE_FD_WRITE | ECORE_FD_READ,
|
|
|
|
_ecore_con_info_cares_fd_cb,
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
info_fds = eina_list_append(info_fds, ecf);
|
|
|
|
}
|
|
|
|
}
|
2009-12-03 02:26:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
info_readers = readers;
|
|
|
|
info_writers = writers;
|
|
|
|
|
|
|
|
EINA_LIST_FOREACH_SAFE(info_fds, l, l_next, ecf)
|
2010-11-03 10:58:37 -07:00
|
|
|
{
|
|
|
|
if (ecf->active != active)
|
|
|
|
{
|
2010-12-04 23:30:17 -08:00
|
|
|
if (ecf->handler) ecore_main_fd_handler_del(ecf->handler);
|
2010-11-03 10:58:37 -07:00
|
|
|
free(ecf);
|
|
|
|
info_fds = eina_list_remove_list(info_fds, l);
|
|
|
|
}
|
|
|
|
}
|
2009-12-03 02:26:40 -08:00
|
|
|
|
|
|
|
if (!info_fds)
|
|
|
|
{
|
2010-07-26 23:30:27 -07:00
|
|
|
if (tm)
|
2010-11-03 10:58:37 -07:00
|
|
|
ecore_timer_del(tm);
|
2010-07-26 23:30:27 -07:00
|
|
|
|
|
|
|
tm = NULL;
|
2009-12-03 02:26:40 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-07-26 23:30:27 -07:00
|
|
|
struct timeval tv;
|
2009-12-03 02:26:40 -08:00
|
|
|
|
2010-07-26 23:30:27 -07:00
|
|
|
ares_timeout(info_channel, NULL, &tv);
|
2009-12-03 02:26:40 -08:00
|
|
|
|
2010-07-26 23:30:27 -07:00
|
|
|
if (tm)
|
2010-11-03 10:58:37 -07:00
|
|
|
ecore_timer_delay(tm, tv.tv_sec);
|
2010-07-26 23:30:27 -07:00
|
|
|
else
|
2010-11-03 10:58:37 -07:00
|
|
|
tm =
|
|
|
|
ecore_timer_add((double)tv.tv_sec,
|
|
|
|
_ecore_con_info_cares_timeout_cb,
|
|
|
|
NULL);
|
2009-12-03 02:26:40 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-24 09:15:56 -07:00
|
|
|
static Eina_Bool
|
2010-11-16 22:41:41 -08:00
|
|
|
_ecore_con_info_cares_timeout_cb(void *data __UNUSED__)
|
2009-12-03 02:26:40 -08:00
|
|
|
{
|
|
|
|
ares_process(info_channel, &info_readers, &info_writers);
|
|
|
|
_ecore_con_info_cares_clean();
|
|
|
|
|
2010-06-24 09:15:56 -07:00
|
|
|
return ECORE_CALLBACK_RENEW;
|
2009-12-03 02:26:40 -08:00
|
|
|
}
|
|
|
|
|
2010-06-24 09:15:56 -07:00
|
|
|
static Eina_Bool
|
2010-11-16 22:41:41 -08:00
|
|
|
_ecore_con_info_cares_fd_cb(void *data __UNUSED__,
|
|
|
|
Ecore_Fd_Handler *fd_handler __UNUSED__)
|
2009-12-03 02:26:40 -08:00
|
|
|
{
|
|
|
|
ares_process(info_channel, &info_readers, &info_writers);
|
|
|
|
_ecore_con_info_cares_clean();
|
|
|
|
|
2010-06-24 09:15:56 -07:00
|
|
|
return ECORE_CALLBACK_RENEW;
|
2009-12-03 02:26:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-11-03 10:58:37 -07:00
|
|
|
_ecore_con_info_ares_host_cb(Ecore_Con_CAres *arg,
|
|
|
|
int status,
|
2010-11-16 22:41:41 -08:00
|
|
|
int timeouts __UNUSED__,
|
2010-11-03 10:58:37 -07:00
|
|
|
struct hostent *hostent)
|
2009-12-03 02:26:40 -08:00
|
|
|
{
|
|
|
|
struct sockaddr *addr;
|
|
|
|
int addrlen;
|
|
|
|
|
|
|
|
/* Found something ? */
|
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case ARES_SUCCESS:
|
2010-11-03 10:58:37 -07:00
|
|
|
if (!hostent->h_addr_list[0])
|
|
|
|
{
|
2010-11-28 13:51:07 -08:00
|
|
|
ERR("No IP found");
|
2010-11-03 10:58:37 -07:00
|
|
|
goto on_error;
|
|
|
|
}
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2010-11-03 10:58:37 -07:00
|
|
|
switch (hostent->h_addrtype)
|
|
|
|
{
|
|
|
|
case AF_INET:
|
2010-07-26 23:30:27 -07:00
|
|
|
{
|
2010-11-03 10:58:37 -07:00
|
|
|
struct sockaddr_in *addri;
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2010-11-30 20:42:29 -08:00
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
2010-11-03 10:58:37 -07:00
|
|
|
addri = malloc(addrlen);
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2010-11-03 10:58:37 -07:00
|
|
|
if (!addri)
|
|
|
|
goto on_mem_error;
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2010-11-03 10:58:37 -07:00
|
|
|
addri->sin_family = AF_INET;
|
|
|
|
addri->sin_port = htons(arg->svr->port);
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2010-11-03 10:58:37 -07:00
|
|
|
memcpy(&addri->sin_addr.s_addr,
|
2010-11-30 20:42:29 -08:00
|
|
|
hostent->h_addr_list[0], sizeof(struct in_addr));
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2010-11-03 10:58:37 -07:00
|
|
|
addr = (struct sockaddr *)addri;
|
|
|
|
break;
|
|
|
|
}
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2010-11-03 10:58:37 -07:00
|
|
|
case AF_INET6:
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 *addri6;
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2010-11-30 20:42:29 -08:00
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
2010-11-03 10:58:37 -07:00
|
|
|
addri6 = malloc(addrlen);
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2010-11-03 10:58:37 -07:00
|
|
|
if (!addri6)
|
|
|
|
goto on_mem_error;
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2010-11-03 10:58:37 -07:00
|
|
|
addri6->sin6_family = AF_INET6;
|
|
|
|
addri6->sin6_port = htons(arg->svr->port);
|
|
|
|
addri6->sin6_flowinfo = 0;
|
|
|
|
addri6->sin6_scope_id = 0;
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2010-11-03 10:58:37 -07:00
|
|
|
memcpy(&addri6->sin6_addr.s6_addr,
|
2010-11-30 20:42:29 -08:00
|
|
|
hostent->h_addr_list[0], sizeof(struct in6_addr));
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2010-11-03 10:58:37 -07:00
|
|
|
addr = (struct sockaddr *)addri6;
|
|
|
|
break;
|
2010-07-26 23:30:27 -07:00
|
|
|
}
|
|
|
|
|
2010-11-03 10:58:37 -07:00
|
|
|
default:
|
2010-11-28 13:51:07 -08:00
|
|
|
ERR("Unknown addrtype %i", hostent->h_addrtype);
|
2010-11-03 10:58:37 -07:00
|
|
|
goto on_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_ecore_con_info_ares_getnameinfo(arg, hostent->h_addrtype,
|
|
|
|
hostent->h_name,
|
|
|
|
addr, addrlen))
|
|
|
|
goto on_error;
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2010-11-03 10:58:37 -07:00
|
|
|
break;
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2010-06-07 06:08:19 -07:00
|
|
|
case ARES_ENOTFOUND: /* address notfound */
|
2010-11-03 10:58:37 -07:00
|
|
|
if (arg->byaddr)
|
|
|
|
{
|
|
|
|
/* This happen when host doesn't have a reverse. */
|
2010-07-26 23:30:27 -07:00
|
|
|
if (arg->isv6)
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 *addri6;
|
|
|
|
|
2010-11-30 20:42:29 -08:00
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
2010-07-26 23:30:27 -07:00
|
|
|
addri6 = malloc(addrlen);
|
|
|
|
|
|
|
|
if (!addri6)
|
2010-11-03 10:58:37 -07:00
|
|
|
goto on_mem_error;
|
2009-12-03 02:26:40 -08:00
|
|
|
|
2010-07-26 23:30:27 -07:00
|
|
|
addri6->sin6_family = AF_INET6;
|
|
|
|
addri6->sin6_port = htons(arg->svr->port);
|
|
|
|
addri6->sin6_flowinfo = 0;
|
|
|
|
addri6->sin6_scope_id = 0;
|
2009-12-03 02:26:40 -08:00
|
|
|
|
2010-07-26 23:30:27 -07:00
|
|
|
memcpy(&addri6->sin6_addr.s6_addr,
|
2010-11-30 20:42:29 -08:00
|
|
|
&arg->addr.v6, sizeof(struct in6_addr));
|
2009-12-03 02:26:40 -08:00
|
|
|
|
2010-07-26 23:30:27 -07:00
|
|
|
addr = (struct sockaddr *)addri6;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct sockaddr_in *addri;
|
2009-12-03 02:26:40 -08:00
|
|
|
|
2010-11-30 20:42:29 -08:00
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
2010-07-26 23:30:27 -07:00
|
|
|
addri = malloc(addrlen);
|
2009-12-03 02:26:40 -08:00
|
|
|
|
2010-07-26 23:30:27 -07:00
|
|
|
if (!addri)
|
2010-11-03 10:58:37 -07:00
|
|
|
goto on_mem_error;
|
2010-06-07 06:08:19 -07:00
|
|
|
|
2010-07-26 23:30:27 -07:00
|
|
|
addri->sin_family = AF_INET;
|
|
|
|
addri->sin_port = htons(arg->svr->port);
|
2010-06-07 06:08:19 -07:00
|
|
|
|
2010-07-26 23:30:27 -07:00
|
|
|
memcpy(&addri->sin_addr.s_addr,
|
2010-11-30 20:42:29 -08:00
|
|
|
&arg->addr.v4, sizeof(struct in_addr));
|
2010-06-07 06:08:19 -07:00
|
|
|
|
2010-07-26 23:30:27 -07:00
|
|
|
addr = (struct sockaddr *)addri;
|
|
|
|
}
|
2010-06-07 06:08:19 -07:00
|
|
|
|
2010-07-26 23:30:27 -07:00
|
|
|
if (!_ecore_con_info_ares_getnameinfo(arg,
|
|
|
|
arg->isv6 ? AF_INET6 :
|
|
|
|
AF_INET,
|
|
|
|
NULL, addr,
|
|
|
|
addrlen))
|
2010-11-03 10:58:37 -07:00
|
|
|
goto on_error;
|
2010-06-07 06:08:19 -07:00
|
|
|
|
2010-07-26 23:30:27 -07:00
|
|
|
break;
|
2010-11-03 10:58:37 -07:00
|
|
|
}
|
2010-06-07 06:08:19 -07:00
|
|
|
|
2009-12-03 02:26:40 -08:00
|
|
|
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 */
|
2010-11-03 10:58:37 -07:00
|
|
|
goto on_error;
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2009-12-03 02:26:40 -08:00
|
|
|
default:
|
2010-11-28 13:51:07 -08:00
|
|
|
ERR("Unknown status returned by c-ares: %i assuming error",
|
2010-11-03 10:58:37 -07:00
|
|
|
status);
|
|
|
|
goto on_error;
|
2009-12-03 02:26:40 -08:00
|
|
|
}
|
|
|
|
|
2010-07-26 23:30:27 -07:00
|
|
|
return;
|
2009-12-03 02:26:40 -08:00
|
|
|
|
2010-07-26 23:30:27 -07:00
|
|
|
on_mem_error:
|
2010-11-28 13:51:07 -08:00
|
|
|
ERR("Not enough memory");
|
2010-06-07 06:08:19 -07:00
|
|
|
|
2010-07-26 23:30:27 -07:00
|
|
|
on_error:
|
2009-12-03 02:26:40 -08:00
|
|
|
arg->done_cb(arg->data, NULL);
|
|
|
|
free(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-11-03 10:58:37 -07:00
|
|
|
_ecore_con_info_ares_nameinfo(Ecore_Con_CAres *arg,
|
|
|
|
int status,
|
2010-11-16 22:41:41 -08:00
|
|
|
int timeouts __UNUSED__,
|
2010-11-03 10:58:37 -07:00
|
|
|
char *node,
|
|
|
|
char *service)
|
2009-12-03 02:26:40 -08:00
|
|
|
{
|
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case ARES_SUCCESS:
|
2010-11-03 10:58:37 -07:00
|
|
|
if (node)
|
|
|
|
strcpy(arg->result->ip, node);
|
|
|
|
else
|
|
|
|
*arg->result->ip = '\0';
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2010-11-03 10:58:37 -07:00
|
|
|
if (service)
|
|
|
|
strcpy(arg->result->service, service);
|
|
|
|
else
|
|
|
|
*arg->result->service = '\0';
|
2010-07-26 23:30:27 -07:00
|
|
|
|
2010-11-03 10:58:37 -07:00
|
|
|
arg->done_cb(arg->data, arg->result);
|
|
|
|
break;
|
2009-12-03 02:26:40 -08:00
|
|
|
|
|
|
|
case ARES_ENOTIMP:
|
|
|
|
case ARES_ENOTFOUND:
|
|
|
|
case ARES_ENOMEM:
|
|
|
|
case ARES_EDESTRUCTION:
|
|
|
|
case ARES_EBADFLAGS:
|
2010-11-03 10:58:37 -07:00
|
|
|
arg->done_cb(arg->data, NULL);
|
|
|
|
break;
|
2009-12-03 02:26:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
free(arg->result->info.ai_addr);
|
|
|
|
free(arg->result);
|
|
|
|
free(arg);
|
|
|
|
}
|
2010-11-03 10:58:37 -07:00
|
|
|
|