how to bind a link local address to an ipv6 socket
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.
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.
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).
For IPv6 link-local addresses (fe80::/10
prefix), the sin6_scope_id
member in the sockaddr_in6
structure is the interface number and you can get this number by
if_nametoindex(const char *ifname);
精彩评论