2

I receive connections to my server from several ip addresses I want to route these connections just like rinetd does but based on the ip the connection is coming from to connect to a specified host.

Just like this:

IP 10.10.12.1    => CONNECTS TO MY SERVER => MY SERVER REDIRECTS IT TO 82.12.12.1
IP 10.10.12.2    => CONNECTS TO MY SERVER => MY SERVER REDIRECTS IT TO 81.121.12.10

etc

Is it possible or do I need to write my own daemon to achieve this functionality ?

1 Answer 1

4

iptables can do that:

sudo iptables -t nat -A PREROUTING -s 10.10.12.1 -j DNAT --to 82.12.12.1
sudo iptables -t nat -A POSTROUTING -s 10.10.12.1 -d 82.12.12.1 -j MASQUERADE

The first command changes the packet's destination address, redirecting it to the external host. The second command changes the source address, so the external host can send a reply to your server.

Probably you'll want to redirect only the packets destinated to specific ports, e.g. if 10.10.12.1 talks to 82.12.12.1 only via TCP ports 80 and 1234 the first iptables command would be:

sudo iptables -t nat -A PREROUTING -s 10.10.12.1 -p tcp -m multiport --destination-ports 80,1234 -j DNAT --to 82.12.12.1
6
  • The incoming connections are from a vpn and i want to forward them to the internet do the routing works on other interfaces ?
    – opc0de
    Jul 3, 2013 at 12:10
  • What VPN is it? Do you know if communication between client and server is done via a single (TCP/UDP) port? If you redirect all the ports (the first command in my answer), 10.10.12.1 will not be able to connect to your server, all its traffic will be redirected to the Internet. Is that a problem for you? If it is, then you'll have specify the ports you want to redirect. If the internal hosts can reach your server and your server can reach the Internet there will be no routing problem. Jul 3, 2013 at 12:23
  • I have issued the commands but nothing happens when I type the command iptables -L -n no rule is set...
    – opc0de
    Jul 3, 2013 at 12:26
  • @opc0de iptables -t nat -L -n Jul 3, 2013 at 12:58
  • Still nothing in the listing... :(
    – opc0de
    Jul 3, 2013 at 13:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .