Unique NAT and NETMAP application

I’m hoping someone has done this before:

On network (1) I have to test a server app where a PC has an application that needs to communicate to 200 other IP addresses and all requests are made to port 502 of the target IP’s. For example the host PC is on 192.168.1.10 and the destination IP’s are on 192.168.1.20 to 192.168.1.221. The server app cannot be configured for any port other than 502. The server initiates communication to the clients in a master/slave fashion.

On network (2) I have a slave simulator for the data residing on second PC. The IP address of this PC is 192.168.0.10. The simulator software can only listen to 1 IP address (the PC IP address) but it can listen to any number of IP ports simultaneously.

I need to remap the destination IP addresses so that they are all remapped to the same IP address, 192.168.0.10, but in addition each destination IP remapped to a unique IPport.

For example:
destination IP:port Remapped IP:port
192.168.1.20:502 192.168.0.10:9020
192.168.1.21:502 192.168.0.10:9021
192.168.1.22:502 192.168.0.10:9022
192.168.1.23:502 192.168.0.10:9023
.
.
.
192.168.1.221:502 192.168.0.10:9221

Can this be done with Mikrotik? I’m new to this sofware and have tried to configure this but with no luck. I can’t seem to get an IP forwarded let alone remap the port.

Any help would be greatly appreciated.

Yes, you can redirect data

192.168.1.20:502 192.168.0.10:9020
192.168.1.21:502 192.168.0.10:9021
192.168.1.22:502 192.168.0.10:9022
192.168.1.23:502 192.168.0.10:9023
.
.
.
192.168.1.221:502 192.168.0.10:9221

use DST-NAT to accomplish it, but you need to add rules manually, as no command exist to achieve it with the single rule.
Action=netmap creates 1:1 bindings (e.g. 1.1.1.1 to 2.2.2.1, 1.1.1.2 to 2.2.2.2 and so on).

Try this:

:local startport 9020
:local startip 192.168.1.20
:for i from=0 to=100 do={
:log info ("adding dst-nat port " . ($startport + $i))
/ip firewall nat add chain=dstnat dst-address=($startip + $i) dst-port=502 action=dst-nat to-addresses=192.168.0.10 to-ports=($startport + $i)
}

That will create you 100 rules…

Thanks for the tips. I was able to get it to work using the following script

:local startport 9020
:local startip 192.168.1.20
:for i from=0 to=200 do={
:log info ("adding dst-nat port " . ($startport + $i))
/ip firewall nat add chain=dstnat dst-address=($startip + $i) protocol=tcp dst-port=502 action=netmap to-addresses=192.168.0.16 to-ports=($startport + $i)
}

I had to use the netmap and protocol=tcp settings.

But there is a small issue that is giving me a headache. The entire netmap interface seems to ‘reset’ anytime I change the message being sent through the netmap or if I begin a new transaction on any of the remapped addresses. I loose communication for several seconds and also get a partial packet reflected back. This causes a bunch of errors in my host system. If I send the same package everything is fine, its only when I change the package or if I use another IP address.

Anyone seen something like this?