I have a code which listed interfaces on a device and looked for an interface of a given name and type:
ifaddrs * ifAddrs = nullptr;
getifaddrs(&ifAddrs);
for (ifaddrs * it = ifAddrs ; it != nullptr ; it = it->ifa_next)
{
if ((it->ifa_addr->sa_family == AF_INET) && (it->ifa_name == someInterfaceName))
{
// do stuff
}
}
Now I'd like to add IPv6 support and so I modified the code like this (added af
variable):
ifaddrs * ifAddrs = nullptr;
getifaddrs(&ifAddrs);
int af = ipv6Code ? AF_INET : AF_INET6;
for (ifaddrs * it = ifAddrs ; it != nullptr ; it = it->ifa_next)
{
if ((it->ifa_addr->sa_family == af) && (it->ifa_name == someInterfaceName))
{
// do stuff
}
}
But I don't know if it's correct. Namely, if it->ifa_addr->sa_family
can ever be AF_INET6
or if it's always AF_INET
to describe an internet connection (regardless of IPv4 vs IPv6)? I found for instance this page: https://www.tutorialspoint.com/unix_sockets/socket_structures.htm which only lists AF_INET
but then again this page: how to get IPV6 interface address using getifaddr() function uses AF_INET6
. So which one is it?