How to get all IP Addresses on AIX using C++



I have been trying to get all ip addresses from all interfaces, with
the code listed below.
However, all I get is the 1st IP only. I have configured 2 IPs on a
single interface, I want to get both from my c++ code. I am running my
sample app on AIX ver 5.2
I have also tried the gethostbyname() API, but with no luck.


Sample code:
#include <unistd.h>
#include <stropts.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <net/if.h>
#include <sys/ioctl.h>
//#include <sys/sockio.h>

int main(int argn,char** argv)
{
int sock,n;
struct ifreq *ifr;
struct ifconf ifc;
char buf[1024],addres[16];
unsigned char* adr;


sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
// sock = socket(2, 2, 0);
printf("sock=%d\n", sock);


if (sock == -1)
{
printf("There is trouble of sock!! Exiting\n\n");
return 0;
}


ifc.ifc_buf = buf;
//ifc.ifc_len = 1024;


int retVal = ioctl(sock, SIOCGIFCONF, &ifc);
printf("retval ioctl(sock, SIOCGIFCONF, &ifc)=%d\n\n", retVal);



if (retVal < 0)
{
printf("There is trouble with ioctl\n\n");
close (sock);
return 0;
}


n = ifc.ifc_len/sizeof(struct ifreq);


for (ifr = ifc.ifc_req; n > 0; n--, ifr++)
{
int rval = ioctl( sock, SIOCGIFADDR, ifr);
printf(" ioctl( sock, SIOCGIFADDR, ifr) rval = %d\n",
rval);


if (ifr->ifr_addr.sa_family == AF_INET ||
ifr->ifr_addr.sa_family == AF_INET6)
{
adr = reinterpret_cast<unsigned
char*>(ifr->ifr_ifru.ifru_addr.sa_data+2);


snprintf(addres,16,"%i.%i.%i.%i",adr[0],adr[1],adr[2],adr[3]);
printf("%s = %s\n",ifr->ifr_name,addres);
}
}
close(sock);
return 0;
}

.



Relevant Pages

  • Get list of all IP Addresses on AIX
    ... I have been trying to get all ip addresses from all interfaces, ... int main ... printf("There is trouble of sock!! ... int rval = ioctl(sock, SIOCGIFADDR, ifr); ...
    (comp.unix.aix)
  • [2.6 patch] net/core/: misc possible cleanups
    ... extern unsigned int datagram_poll(struct file *file, struct socket *sock, ... -extern void memcpy_tokerneliovec; ... extern void *sock_kmalloc; ...
    (Linux-Kernel)
  • [2.6 patch] net/core/: misc possible cleanups
    ... extern unsigned int datagram_poll(struct file *file, struct socket *sock, ... -extern int ing_filter(struct sk_buff *skb); ... -extern void memcpy_tokerneliovec; ...
    (Linux-Kernel)
  • [PATCH 1/6] x25: Allow 32 bit socket ioctl in 64 bit kernel
    ... The following patch provides 32 bit userland ioctl support for modular ... socket ioctls in a 64 bit kernel. ... SOCKCALL_WRAP(name, ioctl, (struct socket *sock, unsigned int cmd, \ ... SOCKCALL_WRAP(name, listen, (struct socket *sock, int len), (sock, ...
    (Linux-Kernel)
  • [PATCH] net: Fix sock_wfree() race
    ... We'll see the path trying to delete an already freed sock ... The output of dmesg with this patch appllied is attached. ... int res; ... CPU 2 calling the 'final' sock_put, ...
    (Linux-Kernel)