problem with gethostent

problem with gethostent

Post by Ryan » Mon, 13 Dec 2010 13:39:08



Hi all,
   I want to get all interfaces address infomations with
gethostent(3)(NOT with gethostbyname of getaddrinfo).And i use the
Ubuntu10.10,the ipv6 is enabled by default.However,with my code
below,only the ipv4 addresses were returned,really confused :-)
will anyone give me a explanation?Thanks ahead.
code here:
#include "unp.h"

int main()
{

        struct hostent          *he;
        char                    **sa;
        char                    **alias;
        char                    buf[BUFSIZ];

        while((he = gethostent())){
                if(he == NULL){
                        perror("gethostent");
                        return -1;
                }
                printf("PROTOCOL: %s\n",he->h_addrtype == AF_INET?\
                                                "AF_INET":"AF_INET6");      
                printf("canonical name:%s\n",he->h_name);
                alias = he->h_aliases;
                for(;*alias != NULL;alias++)
                        printf("alias: %s\n",*alias);

                sa = he->h_addr_list;
                for(;*sa != NULL;sa++)
                        printf("address         %s\n",inet_ntop(he->h_addrtype,*sa,\
                        buf,sizeof(buf)));
        }
        endhostent();
        return EXIT_SUCCESS;

Quote:}

 
 
 

problem with gethostent

Post by Jorgen Grah » Mon, 13 Dec 2010 18:45:11



> Hi all,
>    I want to get all interfaces address infomations with
> gethostent(3)(NOT with gethostbyname of getaddrinfo).And i use the
> Ubuntu10.10,the ipv6 is enabled by default.However,with my code
> below,only the ipv4 addresses were returned,really confused :-)
> will anyone give me a explanation?

Did you read the manual?  In the Linux man page gethostent(3) is barely
mentioned, "does not make much sense", "may be available only when the
library was built without DNS support" and "will ignore ipv6 entries".

Which seems to be a way of saying "we don't believe in providing name
resolvers which only use /etc/hosts".

/Jorgen

--

\X/     snipabacken.se>   O  o   .

 
 
 

problem with gethostent

Post by Richard Kettlewel » Mon, 13 Dec 2010 19:21:44



>   I want to get all interfaces address infomations with
> gethostent(3)(NOT with gethostbyname of getaddrinfo).

gethostent() gives you the contents of the hosts database (usually
/etc/hosts), not interface addresses.

It's essentially useless and has been for the last quarter century.

--
http://www.greenend.org.uk/rjk/

 
 
 

problem with gethostent

Post by Ryan » Mon, 13 Dec 2010 19:26:03




>> Hi all,
>>     I want to get all interfaces address infomations with
>> gethostent(3)(NOT with gethostbyname of getaddrinfo).And i use the
>> Ubuntu10.10,the ipv6 is enabled by default.However,with my code
>> below,only the ipv4 addresses were returned,really confused :-)
>> will anyone give me a explanation?

> Did you read the manual?  In the Linux man page gethostent(3) is barely
> mentioned, "does not make much sense", "may be available only when the
> library was built without DNS support" and "will ignore ipv6 entries".

> Which seems to be a way of saying "we don't believe in providing name
> resolvers which only use /etc/hosts".

> /Jorgen

thanks.I didn't noticed that.