Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OS X equivalent of SO_BINDTODEVICE

Linux allows to execute following code to bind a socket to some specific network interface. So, the data sent through this socket will always be channeled through the original interface.

setsockopt(socket, SOL_SOCKET, SO_BINDTODEVICE, name, strlen(name))

As I understand, this functionality is used in VPN clients. A socket gets connected to a remote server and bound to a network interface. This way, the traffic from VPN client itself won't be looped back into VPN client.

Is there OS X equivalent of doing this? Either

  • Binding a socket to some specific interface

  • Marking a socket in VPN client to not be looped back.

BTW. I found similar question, but I didn't understand the answer: Writing an OS X kernel extension to implement Linux's SO_BINDTODEVICE socket option

Update 1

I found out that some of VPN clients use TUN/TAP devices to prevent loopback problem. http://backreference.org/2010/03/26/tuntap-interface-tutorial/

However, I am not that that all OS X VPN uses that.

like image 957
Victor Ronin Avatar asked Oct 20 '25 03:10

Victor Ronin


1 Answers

Yep, use IP_BOUND_IF

int idx = if_nametoindex("en0");
setsockopt(sockfd, IPPROTO_IP, IP_BOUND_IF, &idx, sizeof(idx))

However, you can just use bind() with the IP address of the interface instead, that's often easier.

like image 87
horseyguy Avatar answered Oct 21 '25 22:10

horseyguy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!