Get IPv6 addresses in linux using ioctl

Yury Bushev picture Yury Bushev · Dec 23, 2013 · Viewed 7.8k times · Source

I trying to get IPv6 addresses in my linux OS like following:

sd = Socket_m(AF_INET6_m, SOCK_DGRAM_m, 0);

ifc.ifc_buf = buffer_p;
ifc.ifc_len = buffSize;
Ioctl_m(sd, SIOCGIFCONF, &ifc);

It works succesfully if any IPv4 address are configured for interface, but if interface has only one IPv6 address it is not returned by ioctl.

For example, I unable to get IPv6 address of the followith interface because only IPv6 address is configured:

br1       Link encap:Ethernet  HWaddr 00:10:18:2D:BB:34  
          inet6 addr: fe80::210:18ff:fe2d:be54/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:194244850 errors:0 dropped:0 overruns:0 frame:0
          TX packets:72005 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:12331900995 (11760.6 Mb)  TX bytes:6192406 (5.9 Mb)

Answer

Nicholas Wilson picture Nicholas Wilson · Dec 23, 2013

Yes, that ioctl is legacy and won't return IPv6. Each platform has a different way of getting the IPv6 ones:

  • Linux, use NETLINK if you're crazy, use getifaddrs if you have a vaguely recent glibc, otherwise read /proc/net/if_inet6 (eg on Android).
  • Darwin or FreeBSD: use getifaddrs.
  • Solaris, use SIOCGLIFCONF.
  • AIX, use SIOCGIFCONF which actually returns IPv4 and IPv6 addresses (because they have an sa_len field in struct sockaddr they can actually support that).