#!/usr/bin/perl

#
# ipchain_logs_to_db
# Copyright (C) 2000 by John Heidemann <johnh@ficus.cs.ucla.edu>
# $Id: ipchain_logs_to_db,v 1.1 2000/11/06 20:01:57 johnh Exp $
#
# This program is distributed under terms of the GNU general
# public license, version 2.  See the file COPYING
# in $dblib for details.
#

sub usage {
    print STDERR <<END;
usage: $0 [registers] <kitrace_data_stream

Converts log file entrys from ipchains to jdb format.

Command:
ipchain_logs_to_db

Options:
-M : maximal logging

Sample input:
tba

Sample output:
tba
END
    exit 1;
}

my(@orig_argv) = @ARGV;
my($prog) = $0; # &progname;
&usage if ($#ARGV > 0 && $ARGV[0] eq '-?');


# maximal: print "#h month dayofmonth time host kernel packet log chain rule interface proto source dest length tos ipid fragment ttl ruleno\n";

print "#h monthday time chain rule interface proto srcippo destippo srcip srcpo destip destpo length tos ipid fragment ttl flags ruleno\n";

while (<STDIN>) {
	chomp;
	my(@f) = split(/ /);
	$f[10] =~ s/PROTO=//;
	my($srcip, $srcpo) = split(/:/, $f[11]);
	my($destip, $destpo) = split(/:/, $f[12]);
	$f[13] =~ s/L=//;
	$f[14] =~ s/S=//;
	$f[15] =~ s/I=//;
	$f[16] =~ s/F=//;
	$f[17] =~ s/T=//;
	my($flags, $ruleno) = ('-', $f[18]);
	if ($ruleno !~ /^\(/) {
		$flags = $f[18];
		$ruleno = $f[19];
	};
	$ruleno =~ s/\(\#(\d+)\)/$1/;
	my(@of) = ("$f[0]-$f[1]", $f[2], $f[7], $f[8],
			$f[9], $f[10],
			$f[11], $f[12],
			$srcip, $srcpo,
			$destip, $destpo,
			$f[13], $f[14], $f[15], $f[16], $f[17],
			$flags, $ruleno);
	print join(" ", @of, "\n");
}

print "#  | $prog " . join(" ", @orig_argv) . "\n";
exit 0;
