#!/usr/bin/perl -w 
# make_pkts.pl
#

use strict;
use NF2::TestLib;
use NF2::PacketGen;
use NF2::PacketLib;
use NF21RouterLib;
use NF2::PacketGen ('nf_PCI_read32' , 'nf_PCI_write32', 'nf_dma_data_in',
        'nf_expected_packet', 'nf_expected_dma_data', 'nf_get_sim_reg_access');

use Carp;

# Include the reg_defines.ph header file.
#
# Sets the package to main to ensure all of the functions
# are in the main namespace. Returns to CryptoNICLib before continuing.
package main;
require "reg_defines.ph";
require "reg_defines_hostfilter.ph";

my @interfaces = ("nf2c0", "nf2c1", "nf2c2", "nf2c3", "eth1", "eth2");
nftest_init(\@ARGV,\@interfaces,);
nftest_start(\@interfaces);

my $MAC_1 = '00:ca:fe:00:00:01';
my $MAC_2 = '00:ca:fe:00:00:02';
my $MAC_3 = '00:ca:fe:00:00:03';
my $MAC_4 = '00:ca:fe:00:00:04';

my $IP_1 = '192.168.1.1';
my $IP_2 = '192.168.2.1';
my $IP_3 = '192.168.3.1';
my $IP_4 = '192.168.4.1';

my $length = 100;
my $TTL = 30;
my $DA = 0;
my $SA = 0;
my $dst_ip = 0;
my $src_ip = 0;
my $protocoltcp = 0x06; # UDP
my $protocoludp = 0x06; # UDP
my $src_port = 0xDEAD;
my $dst_port = 0xBEEF;
my $pkttcp;
my $pktudp;
my $pkt_payload;

my $postcpflags = 13;
my $posoffset   = 12;
my $possrcport  = 0;
my $posdstport  = 2;
my $posoptions  = 20;

#
###############################
#

#
###############################
#

# Send an IP packet in port 1
$length = 96;
$DA = $MAC_1;
$SA = $MAC_2;
$dst_ip = $IP_2;
$src_ip = $IP_1;
$src_port = 0x0;
$dst_port = 0x0;
$protocoltcp = 0x06;
$protocoludp = 0x11;

$pkttcp = NF2::UDP_pkt->new(len => $length, 
			DA => $DA, 
			SA => $SA, 
			ttl => $TTL, 
                        proto => $protocoltcp,
			#dst_ip => '192.168.123.132', 
			dst_ip => $dst_ip, 
			src_ip => $src_ip,
                        src_port => $src_port,
                        dst_port => $dst_port
                        );

$pktudp = NF2::UDP_pkt->new(len => $length, 
			DA => $DA, 
			SA => $SA, 
			ttl => $TTL, 
                        proto => $protocoludp,
			dst_ip => $dst_ip, 
			src_ip => $src_ip,
                        src_port => $src_port,
                        dst_port => $dst_port
                        );



#my @payload = map(hex, split(/ /, ${$pkt->{'payload'}}->bytes));

#$payload[$postcpflags] = 0x10;
#$payload[$posoffset] = 0x60;
#$payload[$possrcport] = 0x00;
#$payload[$possrcport+1] = 0x00;
#$payload[$posdstport] = 0x00;
#$payload[$posdstport+1] = 0x00;
#$payload[$posoptions] = 0x00;
#$payload[$posoptions+1] = 0x00;
#$payload[$posoptions+2] = 0x00;
#$payload[$posoptions+3] = 0x00;
#${$pkt->{'payload'}}->set_bytes(@payload);



#$pkt_payload = ${$pkt->{'payload'}}-> bytes;
#substr($pkt_payload, 13 * 3, 2, "12"); # TCP SYN, ACK
#${$pkt->{'payload'}}->set_bytes($pkt_payload);

#$payload[$postcpflags] = 0x00;
#${$pkt->{'payload'}}->set_bytes(@payload);

nftest_regwrite('nf2c0', FILTER_CUTOFF_REG(), 0x3);

nftest_send('eth1', $pktudp->packed);
nftest_expect('nf2c0', $pktudp->packed);
nftest_expect('eth1', $pktudp->packed);

nftest_send('eth1', $pktudp->packed);
nftest_expect('nf2c0', $pktudp->packed);
nftest_expect('eth1', $pktudp->packed);

nftest_send('eth1', $pktudp->packed);
nftest_expect('nf2c0', $pktudp->packed);
nftest_expect('eth1', $pktudp->packed);


nftest_send('eth1', $pktudp->packed);
nftest_expect('eth1', $pktudp->packed);




nftest_regwrite('nf2c0', FILTER_WRITE_REG(), 0x0);
nftest_regwrite('nf2c0', FILTER_KEY0_REG(),  0xC0A80201);
nftest_regwrite('nf2c0', FILTER_KEY1_REG(),  0xC0A80101);
nftest_regwrite('nf2c0', FILTER_KEY2_REG(),  $protocoludp<< 16);
nftest_regwrite('nf2c0', FILTER_KEY3_REG(),  0x0);
nftest_regwrite('nf2c0', FILTER_CMD_REG(),   0x00000000);
nftest_regwrite('nf2c0', FILTER_WRITE_REG(), 0x1);
nftest_regwrite('nf2c0', FILTER_WRITE_REG(), 0x0);
sleep(1);



nftest_send('eth1', $pktudp->packed);
nftest_expect('nf2c0', $pktudp->packed);
nftest_expect('eth1', $pktudp->packed);


#nftest_regwrite('nf2c0', HOST_KEY0_REG(),  0xC0A80101);
#nftest_regwrite('nf2c0', HOST_KEY1_REG(),  0x00060000);
#nftest_regwrite('nf2c0', HOST_CMD_REG(),   0x00000000);
#nftest_regwrite('nf2c0', HOST_WRITE_REG(), 0x1);
#nftest_regwrite('nf2c0', HOST_WRITE_REG(), 0x0);

nftest_send('eth1', $pktudp->packed);
nftest_expect('nf2c0', $pktudp->packed);
nftest_expect('eth1', $pktudp->packed);





#
my $unmatched_hoh = nftest_finish();
my $total_errors = nftest_print_errors($unmatched_hoh);


if ($total_errors==0) {
  print "SUCCESS!\n";
  exit 0;
} else {
  print "FAIL: $total_errors errors\n";
  exit 1;
}
