inet-framework / inet

INET Framework for the OMNeT++ discrete event simulator
https://inet.omnetpp.org
Other
441 stars 488 forks source link

Network Address Translation in emulated network #948

Closed marsluca closed 9 months ago

marsluca commented 9 months ago

According to the official documentation:

The router will perform network address translation to rewrite the destination address to the address of the receiver host’s EXT/TAP interface. This is required so that the video packets actually enter the simulated network; if they were sent to the receiver host’s EXT/TAP interface, they would go through the loopback interface because the host OS optimizes traffic.

This is the implementation of the mentioned example:

*.router.ipv4.natTable.config = xml("<config> \
                                        <entry type='prerouting' \
                                        packetFilter='has(Ipv4Header) &amp;&amp; Ipv4Header.destAddress.str() == \"192.168.2.99\"' \
                                        srcAddress='192.168.3.99' destAddress='192.168.3.20'/> \
                                     </config>")

Can a proper network address translation be implemented? In that way, more complex scenarios can be implemented, and more hosts will be allowed to talk. Without this, only communication between two hosts will be allowed.

I tried something like this, but it didn't work. There are no available examples beyond that specified or documentation regarding the possible accepted values.

*.router*.ipv4.natTable.config = xml("<config> \
                                        <entry type='prerouting' \
                                        packetFilter='has(Ipv4Header) &amp;&amp; Ipv4Header.destAddress.str() == \"10.0.1.254\"' \
                                        &amp;&amp; has(TcpHeader) &amp;&amp; TcpHeader.destPort == 5002' \
                                        srcAddress='192.168.0.1' destAddress='192.168.0.2'/> \
                                       <entry type='prerouting' \
                                        packetFilter='has(Ipv4Header) &amp;&amp; Ipv4Header.destAddress.str() == \"10.0.1.254\"' \
                                        &amp;&amp; has(TcpHeader) &amp;&amp; TcpHeader.destPort == 5003' \
                                        srcAddress='192.168.0.1' destAddress='192.168.0.3'/> \
                                        <entry type='prerouting' \
                                        packetFilter='has(Ipv4Header) &amp;&amp; Ipv4Header.destAddress.str() == \"10.0.2.254\"' \
                                        &amp;&amp; has(TcpHeader) &amp;&amp; TcpHeader.destPort == 5001' \
                                        srcAddress='192.168.0.2' destAddress='192.168.0.1'/> \
                                     </config>")

Error: <!> Error: Cannot assign parameter 'config': xml(): Parse error: Syntax error at string-content:1 -- at /sirius-dev/omnetpp.ini:77 -- in module (inet::Ipv4NatTable) SiriusNetwork.router1.ipv4.natTable (id=240), during network setup This error can be easily fixed by removing the line &amp;&amp; has(TcpHeader) &amp;&amp; TcpHeader.destPort == 5002' \, but losing the possibility of discriminating the port.

Topology: topology1

levy commented 9 months ago

Your XML is syntactically incorrect, there's an extra quote character at the end of several lines.

For example: Ipv4Header.destAddress.str() == \"10.0.1.254\"' \

marsluca commented 9 months ago

You are totally right! Thanks a lot for your help. I suggest in the future to add the function xmldoc, so external XML file can be passed without making the .ini file to complex.

Currently it returns this error: <!> Error: std::runtime_error: syntax error -- in module (omnetpp::cModule) MyNetwork (id=1), during network initialization

levy commented 9 months ago

You should be able to use xmldoc anywhere where you can use xml (i.e. for any xml parameter).

marsluca commented 9 months ago

You should be able to use xmldoc anywhere where you can use xml (i.e. for any xml parameter).

Thanks for your reply. If I try, I get the mentioned error. This is my XML file:

<config>
     <entry type='prerouting' packetFilter='has(Ipv4Header) &amp;&amp; Ipv4Header.destAddress.str() == \"10.0.1.254\" &amp;&amp; has(TcpHeader) &amp;&amp; TcpHeader.destPort == 5002' srcAddress='192.168.0.1' destAddress='192.168.0.2'/>
     <entry type='prerouting' packetFilter='has(Ipv4Header) &amp;&amp; Ipv4Header.destAddress.str() == \"10.0.1.254\" &amp;&amp; has(TcpHeader) &amp;&amp; TcpHeader.destPort == 5003' srcAddress='192.168.0.1' destAddress='192.168.0.3'/>
     <entry type='prerouting' packetFilter='has(Ipv4Header) &amp;&amp; Ipv4Header.destAddress.str() == \"10.0.2.254\" &amp;&amp; has(TcpHeader) &amp;&amp; TcpHeader.destPort == 5001' srcAddress='192.168.0.2' destAddress='192.168.0.1'/>
     <entry type='prerouting' packetFilter='has(Ipv4Header) &amp;&amp; Ipv4Header.destAddress.str() == \"10.0.2.254\" &amp;&amp; has(TcpHeader) &amp;&amp; TcpHeader.destPort == 5002' srcAddress='192.168.0.2' destAddress='192.168.0.2'/>
</config>
levy commented 9 months ago

You don't need the \ before the quotes. Unfortunately, the parse can't tell you in this case where the problem is, because the attribute itself is parsed separately from a string.