Linux – Bridge between two ports on the same network interface on a Linux server

linuxport-forwarding

I need to build a bridge between two network ports on a server so everything that arrives on port A goes to port B and everything that arrives on port B goes to port A.

I have two devices (dA and dB) that are behind different NATs in different networks, both can make any outbound connection, however all inbound connections are blocked. So they cannot connect directly. It is not possible to configure any port forwarding on those NATs.

So I will put a server S that can listen on all networks ports. It accepts connections on ports A and B and creates a bridge between then, so the traffic flows from A to B as if they were connected directly.

The final result I want to archive is:

  1. Device dA connects to server S on port A. Eg. it connects to server.com:A
  2. Device dB connects to server S on port B. Eg. it connects to server.com:B
  3. dA talks with dB through S

Is there any way of doing that using a Linux server? The server runs a Linux based on Debian wheezy.

It would be great to do it using commands/command line tools, so, if needed, the ports can be changed without editing any file (using an script).

Best Answer

The tool socat does what I need in a simple way.

socat  TCP4-LISTEN:5060,reuseaddr TCP4-LISTEN:5061,reuseaddr

It blocks the terminal, however it is the best solution I could found.