12

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)
Damodaran
  • 10,882
  • 10
  • 60
  • 81
Yury Bushev
  • 642
  • 9
  • 25

2 Answers2

19

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).
Nicholas Wilson
  • 9,435
  • 1
  • 41
  • 80
  • Thank you very much for the clarifation, could you please help me with understanding "/proc/net/if_inet6" format, how I can determine UP and non-loopback addresses? I read http://www.tldp.org/HOWTO/Linux+IPv6-HOWTO/proc-net.html but it is not clear.. – Yury Bushev Dec 24 '13 at 09:59
  • The docs are fairly clear - it's the "interface flags" you're after. See the values in your system headers ("if.h") for `IFF_LOOPBACK` and `IFF_UP`. If you're trying to parse if_inet6 using shell, you can extract the right field using awk and AND it with the relevant constant using `&`. – Nicholas Wilson Dec 30 '13 at 18:41
4

Get IPv6 addresses in linux using ioctl

This probably won't work.

From man 7 netdevice:

SIOCGIFCONF

Return a list of interface (transport layer) addresses. This currently means only addresses of the AF_INET (IPv4) family for compatibility.

[...]

NOTES

[...]

Local IPv6 IP addresses can be found via /proc/net or via rtnetlink(7).

alk
  • 69,737
  • 10
  • 105
  • 255
  • I can't use routing sockets as proposed by Nicholas Wilson, I need a LSB compatible way, but seens it is a not possible? – Yury Bushev Dec 23 '13 at 12:49
  • @Unmanner: If you need to known the address to which a socket is bound locally use `getsockname()`. If you need to find out all local interfaces' addresses use `getifaddrs()` or read out `/proc/net/if_inet6` as proposed by *Nicholas Wilson* in his answer. – alk Dec 23 '13 at 13:15