This thread can be treated as a sister thread of previous one posted by me as well. It's will be very tedious that when you want to bind a link local adress to a ipv6 socket you need to the set sin6_scope_id field of sockaddr_in6 struct as well. I'm wondering if someone can provide a good practise like solution here for me or anyone who interested in this topic to learn from.
The issue only arises when you hardcode a link local address, which is not really a practical solution for a nontrivial app.
Otherwise, you should be getting your sockaddr
to bind from getifaddrs()
, which will fill out the scope id for you (eg. you can allow your user to specify an interface name, then search through the list returned by getifaddrs()
to find the link-local address associated with that interface).
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.