Subject: Re: copy hostent from callback

Re: copy hostent from callback

From: Daniel Hardman <daniel.hardman_at_gmail.com>
Date: Tue, 10 Mar 2015 09:47:09 -0600

If you truly need a deep copy, then I think you're going to have to do
something like this:

1. figure out how many items are in hostent->h_addr_list.
2. arg->host->h_addr_list = malloc(N * sizeof(struct sockaddr)).
3. iterate through the items; depending on which type of addr you see (if
hostent->h_addr_type == AF_INET vs. AF_INET6), memcpy(arg->host[i],
hostent->h_addr_list[i], sizeof(in_addr)) or memcpy(..., sizeof(in6_addr).

This will work, but it wastes memory and cpu cycles. Is there any way you
can select just the single IP addr that you want, and only copy that? It
would be more efficient.

--Daniel

On Tue, Mar 10, 2015 at 2:54 AM, Raphael Bauduin <rblists_at_gmail.com> wrote:

>
> Hi,
>
> I'm trying to copy the hostent parameter passed to the callback of
> ares_gethostbyname to the user parameter arg also passed to the callback.
> arg is a struct with a field named host of type struct hostent *.
> The doc says about hostent: "the ares library will free it when the
> callback returns". What's the correct way to copy hostent to arg->host as
> it needs a deep copy?
> Is there another, better approach?
>
> Thanks
>
> Raph
>

-- 
Daniel
http://about.me/daniel.hardman
Received on 2015-03-10