how to bind a link local address to an ipv6 socket

c, ipv6

Solution

The IPv6 link-local address is not unique on the node it's only unique for the NIC which is why you have to specify the scope-id. In other words it is perfectly valid to have multiple adapters with exactly the same IPv6 address.

This means you should take in as input the scope-id or suitable text form (%eth0, %1) that you can pass to `getaddrinfo()`.

One method is to take in a IPv6 link-local address, enumerate the interfaces and if only one matches use that, if more than one match then bail out with a list of interfaces and get the user to specify which one in full form.

Problem

This question can be treated as a sister question of previous one posted by myself. It is very tedious that when you want to bind a link local address to an IPv6 socket, you need to set the `sin6_scope_id` field of the `sockaddr_in6` struct. I'm wondering if someone can provide a solution following good practice.

Original source

Related problems