[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[Libevent-users] Re: Some evdns question
in case it is useful to anyone, I will just answer my own question:
1) no need to free evdns_request* and evdns_getaddrinfo_request*
2) I have a somewhat ip-agnostic version of reverse resolution example here.
handling IN6ADDR_LOOPBACK and INADDR_LOOPBACK is left as reader exercise.
typedef struct addr_name_query_s {
sockaddr_storage addr;
socklen_t addrlen;
struct evdns_request *req;
} addr_name_query_t;
// Reverse DNS lookup.
void on_addr_name_result(int err, char type, int count,
int ttl, void *addresses, void *arg)
{
auto query = (addr_name_query_t *)arg;
if (err) {
debug(dns, "DNS reverse lookup fail: %s.\n", evdns_err_to_string(err));
} else if (count == 0) {
debug(dns, "DNS reverse lookup returns no result.\n");
} else {
auto result = *((char **)addresses);
debug(dns, "DNS reverse lookup result: %d: %s\n", type, result);
// Got result, do something with it.
}
delete query;
}
// Start a reverse lookup.
void query_name_by_addr(object_t *ob)
{
auto query = new addr_name_query_t;
const char *addr = query_ip_number(ob);
debug(dns, "query_name_by_addr: starting lookup for %s.\n", addr);
free_string(addr);
// By the time resolve finish, ob may be already gone, we have to
// copy the address.
memcpy(&query->addr, &ob->interactive->addr, ob->interactive->addrlen);
query->addrlen = ob->interactive->addrlen;
// Check for mapped/compact v4 address, if we are querying for v6 address.
if (query->addr.ss_family == AF_INET6) {
in6_addr *addr6 = &(((sockaddr_in6 *)(&query->addr))->sin6_addr);
if (IN6_IS_ADDR_V4MAPPED(addr6) || IN6_IS_ADDR_V4COMPAT(addr6)) {
in_addr *addr4 = &((in_addr *)(addr6))[3];
debug(dns, "Found mapped v4 address, using extracted v4 address
to resolve.\n")
query->req = evdns_base_resolve_reverse(
g_dns_base, addr4, 0,
on_addr_name_result, query);
} else {
query->req = evdns_base_resolve_reverse_ipv6(
g_dns_base, addr6, 0,
on_addr_name_result, query);
}
} else {
in_addr *addr4 = &((sockaddr_in *)&query->addr)->sin_addr;
query->req = evdns_base_resolve_reverse(
g_dns_base, addr4, 0, on_addr_name_result, query);
}
}
On Wed, Jun 26, 2013 at 7:54 PM, åéé <sunyucong@xxxxxxxxx> wrote:
>
> Hi,
>
> There are a bunch of questions that I couldn't find answers from the
> document, hope someone can share some insight
>
> 1) do I need to FREE returned struct evdns_request* in the callback?
> what about struct evdns_getaddrinfo_request * ? these things are not
> very clear in the doc, and I by chance found evutil_freeaddrinfo ,
> then I kinda guess I need to free that too.
>
> 2) since there is no evdns_getnameinfo (why?), what function could I
> use to reverse resolve address protocol independently ? More
> importantly , neither evdns_resolve_reverse or
> evdns_resolve_reverse_ipv6 works with /etc/hosts file, so things like
> "::1" and "127.0.0.1" is probmatic.
>
> Thanks.
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users in the body.