#!/usr/bin/perl
use DBI;
use conf;
use Tie::File;

#conf read
$conf_name="billing.conf";
$ctest=conf->var_read($conf_name);
$main_table=$ctest->{'mysql_tables.data'};
$pkt_log=$ctest->{'mysql_tables.ipkt'};
$ipt_log_path=$ctest->{'environment.ipt_log_path'};
#db_connect
$dbh = DBI->connect("DBI:mysql:database=$ctest->{'mysql_connect.db_db'};host=$ctest->{'mysql_connect.db_host'}",$ctest->{'mysql_connect.db_user'},$ctest->{'mysql_connect.db_password'});
#print"$ipt_log_path\n";
$ipt_log_path="$ipt_log_path.0";
print"$ipt_log_path\n";
tie @array, 'Tie::File', $ipt_log_path,mode=>O_RDONLY;
#open ($ipt_log,$ipt_log_path);
#while(<$ipt_log>)
for(@array)
    {
#print".";
    $syn=0;
    $ack=0;
    $fin=0;
    $rst=0;
    if(/^.+SYN.+$/) {$syn=1;}
    if(/^.+ACK.+$/) {$ack=1;}
    if(/^.+FIN.+$/) {$fin=1;}
    if(/^.+RST.+$/) {$rst=1;}
#    /^(\w+)\s(\d+)\s(\d+:\d+:\d+).+IN=(\w+).+OUT=(\w+).+SRC=(\d+\.\d+\.\d+\.\d+).+DST=(\d+\.\d+\.\d+\.\d+).+LEN=(\d+).+TTL=(\d+).+PROTO=(\w+).+SPT=(\d+).+DPT=(\d+).+$/;
/^(\w+)\s+(\d+)\s(\d+:\d+:\d+).+$/;
$time="$3\n";
#print"$time\n";
    /^.+IN=(\w+).+OUT=(\w+).+SRC=(\d+\.\d+\.\d+\.\d+).+DST=(\d+\.\d+\.\d+\.\d+).+LEN=(\d+).+TTL=(\d+).+PROTO=(\w+).+SPT=(\d+).+DPT=(\d+).+$/;
#    /.+IN=(\w+)/;
#print"$_\n";
#print"$1 - $2 - $3 - $4 - $5 - $6 - $7 - $8 - $9\n";
    $nip_src2=$dbh->selectrow_array("select inet_aton('$3')");
    $nip_dst2=$dbh->selectrow_array("select inet_aton('$4')");
if(defined $nip_dst2)    {$dbh->do("insert into $pkt_log (time,proto,int_in,nip_src,prt_src,int_out,nip_dst,prt_dst,len,ttl,syn,ack,fin,rst) values ('0000-00-00 $time','$7','$1',$nip_src2,$8,'$2',$nip_dst2,$9,$5,$6,$syn,$ack,$fin,$rst)");}
#if(defined $nip_dst2)    {print"insert into $pkt_log (time,proto,int_in,nip_src,prt_src,int_out,nip_dst,prt_dst,len,ttl,syn,ack,fin,rst) values ('0000-00-00 00:00:00','$7','$1',$nip_src2,$8,'$2',$nip_dst2,$9,$5,$6,$syn,$ack,$fin,$rst)";}
#if(defined $nip_dst2)    {$dbh->do("insert into $pkt_log (time,proto,int_in,nip_src,prt_src,int_out,nip_dst,prt_dst,len,ttl,syn,ack,fin,rst) values ('0000-00-00 $3','$10','$4',$nip_src2,$11,'$5',$nip_dst2,$12,$8,$9,$syn,$ack,$fin,$rst)");}
#print"insert into $pkt_log (time,proto,int_in,nip_src,prt_src,int_out,nip_dst,prt_dst,len,ttl,syn,ack,fin,rst) values ('0000-00-00 $3','$10','$4',$nip_src2,$11,'$5',$nip_dst2,$12,$8,$9,$syn,$ack,$fin,$rst)\n\n";
    }
#close($ipt_log);
$dbh->disconnect;
undef $ctest;
untie @array;