{

$OUT .=<<"HERE";

# Prerouting from chilli (before the server routes paquets from wireless clients)
preroutingFromChilli()\{
    /sbin/iptables -N PREROUTING_FROM_CHILLI -t nat
    /sbin/iptables -A PREROUTING_FROM_CHILLI -t nat -d $LocalIP -j DNAT --to $chillip
HERE

$OUT .="    /sbin/iptables -A PREROUTING_FROM_CHILLI -t nat -d $ExternalIP -j DNAT --to $chillip\n" if $ExternalIP;

# Redirect also addresses specified in RedirectToChilli

foreach my $ip (split(/[;,]/,(${'chilli'}{'RedirectToChilli'} || ''))){
	my $ReIpNum = qr{([01]?\d\d?|2[0-4]\d|25[0-5])};
        my $ReIpAddr = qr{($ReIpNum\.$ReIpNum\.$ReIpNum\.$ReIpNum)};
        # Check the $ip is a valid ip address
        next unless $ip =~ /^${ReIpAddr}$/;
	$OUT .="    /sbin/iptables -A PREROUTING_FROM_CHILLI -t nat -d $ip -j DNAT --to $chillip\n";
}

# Accept other connexions in order to skip other pre-routing rules. Note that packets will be filtered
# in the FORWARD_FROM_CHILLI chain
$OUT .="    /sbin/iptables -t nat -A PREROUTING_FROM_CHILLI -j ACCEPT\n";

$OUT .= "\}\n";

}
