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

%sym_month=
(
'Jan'=>'01',
'Feb'=>'02',
'Mar'=>'03',
'Apr'=>'04',
'May'=>'05',
'Jun'=>'06',
'Jul'=>'07',
'Aug'=>'08',
'Sep'=>'09',
'Oct'=>'10',
'Nov'=>'11',
'Dec'=>'12');
#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
$ipt_log_path="$ipt_log_path.0";
$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'});
tie @array, 'Tie::File', $ipt_log_path,mode=>O_RDONLY;
for(@array)
    {
    $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+).+$/;
    $month=$sym_month{$1};
    $day="$2";
    $time="$3";
    /^.+IN=(\w+).+$/;$in=$1;
    /^.+OUT=(\w+).+$/;$out=$1;
    /^.+SRC=(\d+\.\d+\.\d+\.\d+).+$/;$src=$1;
    /^.+DST=(\d+\.\d+\.\d+\.\d+).+$/;$dst=$1;
    /^.+LEN=(\d+).+$/;$len=$1;
    /^.+TTL=(\d+).+$/;$ttl=$1;
    /^.+PROTO=(\w+).+$/;$proto=$1;
    /^.+SPT=(\d+).+$/;$spt=$1;
    /^.+DPT=(\d+).+$/;$dpt=$1;
    $nip_src=$dbh->selectrow_array("select inet_aton('$src')");
    $nip_dst=$dbh->selectrow_array("select inet_aton('$dst')");
$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-$month-$day $time','$proto','$in',$nip_src,$spt,'$out',$nip_dst,$dpt,$len,$ttl,$syn,$ack,$fin,$rst)");
    }
$dbh->disconnect;
undef $ctest;
untie @array;