Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get IPv6 addresses in linux using ioctl

Tags:

c

linux

ipv6

ioctl

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)
like image 310
Yury Bushev Avatar asked Dec 23 '13 12:12

Yury Bushev


People also ask

How do I find my IPv6 address in Linux?

To check whether a CS Linux server is running IPv4 or IPv6, use the command ifconfig -a and look at the IP address or addresses in the output.

What is inet6 in Linux?

DESCRIPTION. The inet6 family is an updated version of inet(4) family. While inet(4) implements Internet Protocol version 4, inet6 implements Internet Protocol version 6. inet6 is a collection of protocols layered atop the Internet Protocol version 6 (IPv6) transport layer, and utilizing the IPv6 address format.

Is inet6 the IP address?

An IPv6 address is a 128-bit alphanumeric value that identifies an endpoint device in an Internet Protocol Version 6 (IPv6) network. IPv6 is the successor to a previous addressing infrastructure, IPv4, which had limitations IPv6 was designed to overcome.


2 Answers

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).
like image 73
Nicholas Wilson Avatar answered Sep 21 '22 04:09

Nicholas Wilson


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).

like image 43
alk Avatar answered Sep 24 '22 04:09

alk