1

Having multiple IP addresses, is it possible to make an arbitrary network application use only specific IP address when establishing connections to the outside world?

System-wide, routes can be used. Some applications are configurable for this. But is there a generic way?

Say, a browser should always use an IP bound to 'eth0', and a torrent-client should use an IP bound to 'eth1'.

Technically, this should explicitly pass a specific IP address to bind(2) call when a client socket is created.

kolypto
  • 10,738
  • 12
  • 51
  • 66
  • An app that makes connections to the outside world is a client app. And bind() is specific to server sockets. – Anonymous Dec 14 '09 at 21:08
  • You,re wrong: bind() is ok for client sockets, but is not obligatory: it's used when when an application desires to use a specific IP address or port for outbound connections. I'm 100% sure it works so :) – kolypto Dec 15 '09 at 00:10

2 Answers2

1

My only suggestion would be to utilize iptables to redirect the traffic from one interface to another.

Here's a link to someone else who did something similar: http://straylink.wordpress.com/2006/08/16/using-iptables-to-redirect-packets/

Essentially, the command was this:

iptables -t nat -A PREROUTING -p tcp -d 198.168.1.254 --dport 80 -j REDIRECT --to-ports 8080

To completely steal the content (in case it disappears),

The above rule adds a PREROUTING command to your nat table, stating any TCP packets destined for 192.168.1.254 on port 80 should be redirected to localhost port 8080. So breaking down the above example into template format, you have

 iptables -t nat -A PREROUTING -p PROTO -d DEST_IP --dport DEST_PORT -j REDIRECT --to-ports LOCAL_PORTS
Matt Simmons
  • 20,218
  • 10
  • 67
  • 114
1

You could take advantage of the LD_PRELOAD environment variable and "function interposition" to modify the behavior of your network programs. See this article for an example, and Google function interposition for more information.

You could, for example, override the "socket" system call so that every socket() call was followed by an appropriate bind() operation.

NB: This technique can be lots of fun to play with (for example, I once wrote something that would let me "open" URLs for editing with arbitrary text editors), but can be tricky to get right.

larsks
  • 41,276
  • 13
  • 117
  • 170