I'm trying to set a socket's output interface on a system with two interfaces. I've googled a lot and I found divergent answers. Some people say that I can bind
a socket before calling connect
to choose a specific interface (as in How does a socket know which network interface controller to use?). But other people say that this isn't enough (as in http://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html or How to open a socket on a specific interface and receive both IPv4 and IPv6 traffic).
I have a working implementation using SO_BINDTODEVICE
. However the bind
before connect
solution isn't working. It seems that the source address doesn't affect routing and only the routing table is considered in this case. Someone said that this is caused by Linux's Weak end system model. According to http://wiki.treck.com/Appendix_C:_Strong_End_System_Model_/_Weak_End_System_Model the Source Address does not specify the output interface.
If possible I would like to have a portable solution. I know that SO_BINDTODEVICE
is available only in Linux.