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

这一生的挚爱 提交于 2019-12-10 16:42:20

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!