How to open a socket on a specific interface and get both IPv4 and IPv6 traffic

With IPv4, I can bind () to a specific address to select the interface that will be used to receive packets (and in some cases also send, but that's not the point).

On a machine with two IPv6 / IPV4 stacks, I have this problem: I can create socket 6 and use it to receive 4 traffic, but if I want to bind to a specific interface, I need to select an IP address to bind to.

If I bind to an IPv6 address, I will filter out all IPv4 traffic and vice versa.

How can I get IPv4 and IPv6 traffic from a specific interface?

+4
source share
2 answers

There is no standard portable interface for binding one socket to an unspecified address and limiting the scope of access to addresses on a specific interface, and even more so at the same time selective requirement of a strong host model for communication with this socket. Each operating system that supports this feature does it differently.

+2
source

If you are using Linux, you should use the SO_BINDTODEVICE socket option to bind to an interface, not a specific IP address. It is described on the socket page.

+5
source

Source: https://habr.com/ru/post/1341123/


All Articles