问题
With IPv4 I can bind() to a specific address to select the interface that will be used to receive the packets (and in some case, also to send, but that's not the point).
On a dual stack IPv6/IPV4 machine I have this problem: I can create a 6 socket and use it to receive 4 traffic, but if I want to bind to a specific interface, I have to choose an IP address to bind to.
If I bind to an IPv6 address, I'll filter out all the IPv4 traffic and vice versa.
How can I receive both IPv4 and IPv6 traffic from a specific interface?
回答1:
There is no standard portable interface for binding a single socket to the unspecified address and limiting the scope to the addresses on a specific interface, much less simultaneously selectively requiring the strong host model for communications with that socket. Every operating system that supports this feature does it differently.
回答2:
If you're using Linux, you should be able to use SO_BINDTODEVICE
socket option to bind to an interface rather than a specific IP address. It's described in the socket man page.
来源:https://stackoverflow.com/questions/5103990/how-to-open-a-socket-on-a-specific-interface-and-receive-both-ipv4-and-ipv6-traf