#!/usr/local/bin/perl
# parse out the tcp dumps so they can be matlabbed.
$filename = $ARGV[0];
open (TCPDUMP, "/usr/local/sbin/tcpdump -r $filename|") or die "$!";
while (defined($line=<TCPDUMP>)) {
	$count++;
	chomp $line;
	if ($line!~m/^\d/) {
		next;
	}
	if ($line!~m/8041/) {
		next;
	}
	($time,$from,$grtr,$to,$flags, $data, @rest) = split(/ /, $line);
	$time =~ m/(\d\d):(\d\d):(\d\d)\.(\d+)/;
	$time_h = $1;
	$time_m = $2;
	$time_s = $3;
	$time_us = $4;
	$from =~ m/([a-z\.]*)\.(\d+)/;
	$fromHost = $1;
	$fromPort = $2;
	$to =~ m/([a-z\.]*)\.(\d+):/;
	$toHost = $1;
	$toPort = $2;
	$id = ($toPort==8041) ? $fromPort : $toPort;
	$syn = ($flags =~ m/S/) ? 1 : 0;
	$push = ($flags =~ m/P/) ? 1 : 0;
	$fin = ($flags =~ m/F/) ? 1 : 0;
	if ($data =~ m/\((\d+)\)/) {
		$dataCount = $1 || 0;
	} else {
		$dataCount = 0;
	}

	$dir = ($fromPort==8041) ? "1" : "0";
	# microseconds since this morning. Breaks for runs across midnight. :v)
	$abs_us = $time_us+1000000*($time_s+60*($time_m+60*($time_h)));
	print "$abs_us $dir $syn $push $fin $dataCount $id\n";
}
#print "processed $count lines\n";
