Home > Mobile >  Linux - Open socket for specific network adapter
Linux - Open socket for specific network adapter

Time:02-16

The problem

I have a computer that is supposed to connect to two machines (or rather sets of individual devices) via TCP. To make things tricky, these two machines share the same IP address range and also have partially identical addresses, only one is connected via one ethernet adapter and the other one via a second ethernet adapter.

Note: The address ranges were not defined by me, but by the manufacturers of these machines. I unfortunately have to live with them, as they are.

The program that should do that job is written in C/C . The connections are outgoing from the program's point of view, so I can't just bind incoming connections and keep their id.

Possible solution

After some research (e.g. here: Problems with SO_BINDTODEVICE Linux socket option), I tried to bind the socket to a device using

setsockopt(socket, SOL_SOCKET, SO_BINDTODEVICE, "adapter name", strlen("adapter name");

As it turns out, this would only work, if the program was run with superuser privileges, which I would try to avoid. Otherwise, the function returns an error code, which translates to permission denied (I forgot the exact phrase).

Other solutions?

Is there any other way, how I could achieve that?

CodePudding user response:

Just pinning the sockets to a specific interface wouldn't do the job, since there are much more things going on... If you connect say to 192.168.0.3, the kernel looks into the routing table to find the right interface to send the packet over. You cannot have two entries in the routing table with the same subnet specification (192.168.0.0/24) if you want to use IP communication. There are two solutions that come into my mind:

  1. setup prerouting NATs for the adapters to map the addresses. For example you can map all source addesses from packages recieved by eth0 into the 192.168.0.0\24 range and all those recieved by eth1 to 192.168.1.0\24. If you add this address translation in the prerouting filter chain, the kernel won't even notice that the subnets internally use the same ip address range.

  2. If there are no duplicated ip addresses (like e.g. 192.168.0.2 in both the networks), you can setup a bridge interface. The bridge behaves like one interface, that is connected to both the networks. A bridge behaves just like a software switch. To the kernel (and to your program) it looks, as if there was only one adapter, where all the devices are plugged into.

Both solutions require superuser priviledges to setup, but after you have setup them once, your program won't need them.

  • Related