Subject: Re: copy hostent from callback

Re: copy hostent from callback

From: Raphael Bauduin <rblists_at_gmail.com>
Date: Tue, 10 Mar 2015 17:45:20 +0100

On Tue, Mar 10, 2015 at 4:47 PM, Daniel Hardman <daniel.hardman_at_gmail.com>
wrote:

> 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.
>

I'm currently collecting the ip addresses as you suggest, and it works
fine. I wondered if there was another simple way, but it seems there isn't.
Thanks for your reply!

Raph

>
> --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
>

-- 
Web database: http://www.myowndb.com
Free Software Developers Meeting: http://www.fosdem.org
Received on 2015-03-10