This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

gethostbyname2_r returns only one address


Hello.

I am trying to figure out how I can get ALL the ip (v6) addresses for
a given name.

I added four entries in /etc/hosts:
26:26:26::2     foo
26:25:25::2     foo
172.16.96.2    foo
10.16.96.2      foo

When I call gethostbyname2_r("foo", AF_INET6,...) I get back only the
first ipv6 address. The same occur using getaddrinfo. If I invert the
order of the entries in /etc/hosts, gethostbyname2_r and getaddrinfo
returns the other address (It always returns the first).

Is this the normal/correct behaviour?

Thanks.



============================================================================================
                                              D E T A I L S
============================================================================================

----------------------------------------
My system:
----------------------------------------
$ uname -a
Linux bcpp-redhat.ats-ar.com.ar 2.6.18-164.2.1.el5 #1 SMP Mon Sep 21
04:37:42 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux
$ gcc -v
Reading specs from
/home/local/gcc-3.4.3/lib/gcc/x86_64-unknown-linux-gnu/3.4.3/specs
Configured with: ../gcc-3.4.3/configure --prefix=/home/local/gcc-3.4.3
--enable-languages=c,c++ --enable-shared --enable-threads=posix
--enable-__cxa_atexit --with-system-zlib --enable-multilib
Thread model: posix
gcc version 3.4.3
$ ldd test_gethostbyname2_r
        linux-gate.so.1 =>  (0xffffe000)
        libpthread.so.0 => /lib/libpthread.so.0 (0x00862000)
        libdl.so.2 => /lib/libdl.so.2 (0x00833000)
        librt.so.1 => /lib/librt.so.1 (0x0096a000)
        libsctp.so.1 => /usr/lib/libsctp.so.1 (0xf7f1a000)
        libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00a87000)
        libm.so.6 => /lib/libm.so.6 (0x00839000)
        libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x0087b000)
        libc.so.6 => /lib/libc.so.6 (0x006eb000)
        /lib/ld-linux.so.2 (0x006cd000)



----------------------------------------
test_gethostbyname2_r.cpp
----------------------------------------
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#include <arpa/inet.h>
#include <netdb.h>

#include <iostream>
#include <cstring>
#include <cerrno>


int main(int argc, const char *argv[]) {
    char buffer[4096] = { 0 };
    char tmpbuf[4096] = { 0 };
    struct hostent he = { 0 }, *hep = 0;
    int error_num = 0;
    errno = 0;
    int rc = gethostbyname2_r( argv[1], AF_INET6, &he, buffer,
sizeof(buffer), &hep, &error_num );
    int err = errno;
    strerror_r( err, tmpbuf, sizeof(tmpbuf)-1 );
    std::cerr << "    gethostbyname2_r return=" << rc << "
error_num=" << error_num << " errno=" << err
              << "    [" << tmpbuf << "]\n";
    if ( ! hep ) {
        return 0;
    }
    std::cerr << "h_name=" << hep->h_name << "\n";
    for( size_t i = 0; hep->h_aliases && hep->h_aliases[i]; ++i ) {
        std::cerr << "h_aliases=" << hep->h_aliases[i] << "\n";
    }
    std::cerr << "h_addrtype=" << hep->h_addrtype << "\n";
    std::cerr << "h_length=" << hep->h_length << "\n";
    for( size_t i = 0; hep->h_addr_list && hep->h_addr_list[i]; ++i ) {
        std::memset( tmpbuf, 0, sizeof(tmpbuf) );
        std::cerr << "h_addr_list=" << inet_ntop( hep->h_addrtype,
hep->h_addr_list[i], tmpbuf, sizeof(tmpbuf)-1 )
                  << "\n";
    }
    return 0;
}


--------------------------------------------
test_gethostbyname2_r output
--------------------------------------------
$ ./test_gethostbyname2_r  foo
    gethostbyname2_r return=0  error_num=0 errno=0    []
h_name=foo
h_addrtype=10
h_length=16
h_addr_list=26:26:26::2


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]