Loading ...
Sorry, an error occurred while loading the content.

getaddrinfo() on Linux 2.6 : Doesn't return all assigned addresses

Expand Messages
  • Kavan Patil
    Hi, I am trying to retrieve all the IP addresses assigned to a Linux RHEL 4 system using the getaddrinfo() API. But the api always returns only one of the
    Message 1 of 1 , Sep 21, 2008
    • 0 Attachment
      Hi,

      I am trying to retrieve all the IP addresses assigned to a Linux RHEL
      4 system using the getaddrinfo() API. But the api always returns only
      one of the multiple ip addresses available on the system (that too
      three times!) I have even tested this on RHEL 5 and SuSE 8 but without
      success.

      My intention is only to know all the IP addresses on the system and I
      don't intend to open a socket. I would like to know if this is a
      configuration issue only on my systems or if I am missing something
      before I call the API? Any suggestion in this regard will be very
      helpful.

      This is the test program I have written that uses getaddrinfo():

      ------------
      #include <stdio.h>
      #include <string.h>
      #include <sys/types.h>
      #include <sys/socket.h>
      #include <netdb.h>
      #include <arpa/inet.h>

      int main(int argc, char *argv[])
      {
      if(argc<2)
      {
      printf("Please specify a hostname/ipaddress\n");
      return -1;
      }

      char *hostname = argv[1];
      struct addrinfo hints, *res, *addr_single;
      struct in_addr addr;
      struct in6_addr addr6;
      int err;

      memset(&hints, 0, sizeof(hints));
      hints.ai_family = AF_UNSPEC;
      hints.ai_protocol = 0;


      if ((err = getaddrinfo(hostname, NULL, &hints, &res)) != 0)
      {
      printf("error %d\n", err);
      return 1;
      }

      addr_single = res;

      while(addr_single)
      {
      if (addr_single->ai_family == AF_INET)
      {
      addr = ((struct sockaddr_in
      *)(addr_single->ai_addr))->sin_addr;
      char strIPv4Address[INET_ADDRSTRLEN];
      if(inet_ntop(AF_INET, &addr, strIPv4Address ,
      sizeof(strIPv4Address) ) ==(void*) (NULL))
      {
      printf("Failed to convert IPv4 address
      to string\n");
      }
      else
      {
      printf("IPv4 address detected:
      %s\n",strIPv4Address);
      }
      }

      else if (addr_single->ai_family == AF_INET6)
      {
      char strIPv6Address[INET6_ADDRSTRLEN];
      addr6 = ((struct sockaddr_in6
      *)(addr_single->ai_addr))->sin6_addr;
      if(inet_ntop(AF_INET6, &addr6, strIPv6Address
      ,sizeof(strIPv6Address) ) ==(void*) (NULL))
      {
      printf("Failed to convert IPv6 address
      to string\n");
      }
      else
      {
      printf("IPv6 address detected:
      %s\n",strIPv6Address);
      }
      }
      else
      {
      printf("Unknown address family\n");
      }

      addr_single = addr_single->ai_next;

      }

      freeaddrinfo(res);

      return 0;
      }
      ------------

      I have compiled the same on RHEL 4 using g++ (gcc version 3.4.3
      20041212 (Red Hat 3.4.3-9.EL4))

      [root@nt13124 utilities]# g++ getAllAddresses.c -o getAllAddresses

      ------------

      When I run the program by providing the current hostname (nt13124) as
      below the program reports ONLY ONE IPv4 address (3 times) and ONE IPv6
      address (3 times), where as I have two IPv4 addresses and two IPv6
      addresses configured (see ifconfig details below):

      [root@nt13124 utilities]# ./getAllAddresses nt13124
      IPv4 address detected: 15.154.75.51
      IPv4 address detected: 15.154.75.51
      IPv4 address detected: 15.154.75.51
      IPv6 address detected: fe80::204:23ff:fe1d:8bee
      IPv6 address detected: fe80::204:23ff:fe1d:8bee
      IPv6 address detected: fe80::204:23ff:fe1d:8bee

      But for a hostname like www.google.com the program seems to return
      more than one adress (though it returns each address three times!)

      [root@nt13124 utilities]# ./getAllAddresses www.googel.com
      IPv4 address detected: 209.85.171.103
      IPv4 address detected: 209.85.171.103
      IPv4 address detected: 209.85.171.103
      IPv4 address detected: 209.85.171.104
      IPv4 address detected: 209.85.171.104
      IPv4 address detected: 209.85.171.104
      IPv4 address detected: 209.85.171.99
      IPv4 address detected: 209.85.171.99
      IPv4 address detected: 209.85.171.99
      IPv4 address detected: 209.85.171.147
      IPv4 address detected: 209.85.171.147
      IPv4 address detected: 209.85.171.147
      ---------------------

      My system configuration:
      -----------------------
      [root@nt13124 utilities]# ifconfig
      eth0 Link encap:Ethernet HWaddr 00:04:23:1D:8B:EE
      inet addr:15.154.75.51 Bcast:15.154.75.255 Mask:255.255.254.0
      inet6 addr: 2001::200:204:23ff:fe1d:8bee/64 Scope:Global
      inet6 addr: fe80::204:23ff:fe1d:8bee/64 Scope:Link
      UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
      RX packets:1718307 errors:0 dropped:0 overruns:0 frame:0
      TX packets:53008 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000
      RX bytes:122227367 (116.5 MiB) TX bytes:5618561 (5.3 MiB)

      eth0:0 Link encap:Ethernet HWaddr 00:04:23:1D:8B:EE
      inet addr:192.168.1.100 Bcast:15.154.75.255 Mask:255.255.254.0
      UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
      RX packets:0 errors:0 dropped:0 overruns:0 frame:0
      TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:1000
      RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)

      lo Link encap:Local Loopback
      inet addr:127.0.0.1 Mask:255.0.0.0
      inet6 addr: ::1/128 Scope:Host
      UP LOOPBACK RUNNING MTU:16436 Metric:1
      RX packets:4860 errors:0 dropped:0 overruns:0 frame:0
      TX packets:4860 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:0
      RX bytes:4214308 (4.0 MiB) TX bytes:4214308 (4.0 MiB)
      ---------------

      [root@nt13124 utilities]# cat /etc/hosts
      # Do not remove the following line, or various programs
      # that require network functionality will fail.
      127.0.0.1 localhost.localdomain localhost
      fe80::204:23ff:fe1d:8bee nt13124.ind.hp.com nt13124
      2003::200:204:23ff:fe1d:8bee nt13124.ind.hp.com nt13124
      15.154.75.51 nt13124.ind.hp.com nt13124
      192.168.1.100 nt13124.ind.hp.com nt13124

      ---------------

      [root@nt13124 utilities]# cat /etc/nsswitch.conf
      ...
      hosts: db files nisplus nis dns
      ...

      Regards,
      Kavan
    Your message has been successfully submitted and would be delivered to recipients shortly.