Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getnameinfo returning numeric name with "%<interface>"

I'm using getnameinfo as follows:

ifaddrs *ifaddr = NULL, *ifa = NULL;
int rc, family, insize;

rc = getifaddrs(&ifaddr);
...

for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
    family = ifa->ifa_addr->sa_family
    insize = (family == AF_INET) ?
            (int)sizeof(struct sockaddr_in) :
            (int)sizeof(struct sockaddr_in6);

    char host[HOST_NAME_MAX];
    rc = getnameinfo(ifa->ifa_addr, insize,
            host, sizeof(host), NULL, 0, NI_NUMERICHOST);
}
...

When the function returns with an IPv6 address, it include the interface appended to the numeric IP address:

fe80::62a4:4cff:fe05:dc1b%eth0

What's the purpose of appending the interface to the numerical IP address?

Is there a flag available that controls the appending of the interface to the IP address?

like image 659
jww Avatar asked Sep 03 '25 04:09

jww


1 Answers

fe80::* addresses are link-local in scope, which means the address is only valid for that particular network, the same address may reference a difference host on a different network. It is thus meaningless to specify a link-local address without specifying which network adapter. Also, DNS becomes rather meaningless outside of link-local scope such as provided by ZeroConf / multicast-DNS.

Unix hosts tend to specify the adapter by name, Windows hosts will specify the adapter by index. Note that Windows maintains separate interface indexes for IPv4 and IPv6.

like image 169
Steve-o Avatar answered Sep 04 '25 19:09

Steve-o