From c57a1628286a502b45358326d551b94596455fda Mon Sep 17 00:00:00 2001 From: Sapan Bhatia Date: Fri, 30 Apr 2010 18:58:50 +0000 Subject: [PATCH] Make sure only one instance of the port-forwarding rule is inserted. --- exec/if_port_forward | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/exec/if_port_forward b/exec/if_port_forward index 36451c9..400591b 100755 --- a/exec/if_port_forward +++ b/exec/if_port_forward @@ -12,7 +12,14 @@ chomp($dst_port); die ("Src and dst ports need to be numbers") if (($src_port=~/[^0-9]/) || ($src_port=~/[^0-9]/)); -$IPTABLES_CMD="/sbin/iptables -t nat -A PREROUTING -m tcp -p tcp -d $slice_ip --dport $src_port -j DNAT --to-destination $slice_ip:$dst_port"; -print "Invoking port forwarding command: $IPTABLES_CMD\n"; -system($IPTABLES_CMD); +$IPTABLES_CMD_1="/sbin/iptables -t nat -D PREROUTING -m tcp -p tcp -d $slice_ip --dport $src_port -j DNAT --to-destination $slice_ip:$dst_port"; + +$IPTABLES_CMD_2="/sbin/iptables -t nat -A PREROUTING -m tcp -p tcp -d $slice_ip --dport $src_port -j DNAT --to-destination $slice_ip:$dst_port"; + +print "Removing any previous instances of this rule: $IPTABLES_CMD_1\n"; +system($IPTABLES_CMD_1); + +print "Adding forwarding rule: $IPTABLES_CMD_2\n"; +system($IPTABLES_CMD_2); + print "Done.\n"; -- 2.43.0