0

I just wrote this snippet:

#include <iostream>
#include <netdb.h>
#include <arpa/inet.h>

int main(void)
{
    sockaddr_in self;
    hostent *he;
    char local[HOST_NAME_MAX];
    gethostname(local, sizeof(local));
    he = gethostbyname(local);

    if (he)
    {
        std::cout << "Addresses: " << std::endl;
        unsigned int i = 0;
        while(he->h_addr_list[i] != NULL)
        {
            std::cout << "\t" << i << " --> ";
            memcpy(&self.sin_addr, he->h_addr_list[i], he->h_length);
            std::cout << self.sin_addr.s_addr;
            std::cout << " ( " << inet_ntoa( *( struct in_addr*)( he-> h_addr_list[i])) << " )" << std::endl;
            i++;
        }
    }
    else
        std::cout << "gethostname failed" << std::endl;
}

When I run it on an example host I get something like this (i made up these addresses here though):

Addresses: 0 --> 1307150150 ( 10.23.215.61 )

whereas if I run ifconfig I get much more outuput, and in particular the above address corresponds to only the first displayed interface from ifconfig when there are at least two more before the eth0 ones. How come? I would have expected this to run through all of the possible network addresses in this host...

After the answer below, I did the following (using getaddrinfo):

int main(void)
{
    struct addrinfo *result;
    struct addrinfo *res;
    int error;

    char local[HOST_NAME_MAX];
    gethostname(local, sizeof(local));

    error = getaddrinfo(local, NULL, NULL, &result);
    if (error != 0)
    {
        fprintf(stderr, "error in getaddrinfo: %s\n", gai_strerror(error));
        return 1;
    }

    for (res = result; res != NULL; res = res->ai_next)
    {
        struct in_addr addr;
        addr.s_addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
        std::cout << addr.s_addr
                  << " ("
                  << inet_ntoa(addr)
                  << " )"
                  << std::endl;
    }

    freeaddrinfo(result);
}

I now get the same output as before..except repeated the "correct" number of times. (i.e. from ifconfig I see three interfaces from which I could presumably send from in multicast and I get the inet address of the first one repeated thrice as output of above.

Palace Chan
  • 8,845
  • 11
  • 41
  • 93

1 Answers1

1

The function gethostname returns the name the host has been configured with. You are free to configure your computer with wathever name you choose.

The function gethostbyname does the “normal” name resolution process for your OS. Typically it will first look into your /etc/hosts and failing that query your DNS resolver.

To get gethostbyname to return all your configured IP address you will have to either put them in your /etc/hosts file or ask your DNS administrator to update your server.

By the way, gethostbyname is deprecated in favor of getaddrinfo.

Community
  • 1
  • 1
kmkaplan
  • 18,655
  • 4
  • 51
  • 65
  • should note that `gethostbyname` can only return IPv4 addresses. – fbynite Dec 24 '12 at 07:15
  • the /etc/hosts contains only this it seems: # Do not remove the following line, or various programs # that require network functionality will fail. 127.0.0.1 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6 seems useless for my example – Palace Chan Dec 24 '12 at 07:45
  • @PalaceChan Then it probably comes from your DNS. What OS are you on? Try `host `. – kmkaplan Dec 24 '12 at 07:48
  • Old old Linux 2.6.18 - red hat if not mistaken. I also tried getaddrinfo - results in the edit i just made to the question. Running host shows the address both approaches above show. – Palace Chan Dec 24 '12 at 08:04
  • @PalaceChan then it means you should ask your DNS administrator to add the other addresses you want in the DNS server. – kmkaplan Dec 24 '12 at 08:42