I have two Systems A and B. A is a TCP Client and sends a message to TCP Server on B.
------------------ --------------------------
System A System B
192.168.0.5 wlan0 192.168.0.3 wlan0
127.0.0.1 lo 127.0.0.1 lo
TCP Client <------------> TCP Server on 127.0.0.1
------------------ ----------------------------
The TCP Client sends message to 192.168.0.3.
This should be redirected to the local interface of B as the TCP Server is running on 127.0.0.1 at Port 8000 of System B.
Therefore, I wrote the following ip table rules, however my Server at B doesn't receive any messages. Oh btw these two systems are Ubuntu linux systems.
Here is what I did on System B:
#Enable IP Forwarding for NAT
echo "1" > /proc/sys/net/ipv4/ip_forward
#Flush all iptable chains and start afresh
sudo iptables -F
#Forward incoming packets on 192.168.0.3 at wlan0 interface to 127.0.0.1
sudo iptables -t nat -A PREROUTING -p tcp -i wlan0 -d 192.168.0.3 --dport 8000 -j DNAT --to 127.0.0.1:8000
#Explicitly allow incoming connections on port 8000
sudo iptables -A INPUT -i wlan0 -p tcp --dport 8000 -m state --state NEW,ESTABLISHED -j ACCEPT
#Explicitly allow outgoing messages from port 8000
sudo iptables -A OUTPUT -o wlan0 -p tcp --sport 8000 -m state --state ESTABLISHED -j ACCEPT
Then I start the Server on B and send a message from TCP Client on A. I can see the packets on wireshark from 192.168.0.5 on wlan0 but they never get forwarded :(
Please help.
UPDATE:
After inputs from experts here, I have made a more realistic "NAT" scenario for applying the forwarding rules but I have still issues: I have explained this in my newer post: Iptables: Forwarding packets doesn't work