Rmck wrote:
>
> Hi
Hello,
> I have the .h file of a program that spits out a data file
> which is Binary Output. The binary file is a series of fixed
> length records. In C, each record has the format which is
> in the script.
>
> I thought I could use unpack to read the data, and I am
> having no success. PLEASE let me know if I'm using unpack
> correctly:
>
> #!/bin/perl
> use strict;
> use POSIX 'strftime';
> use warnings;
>
> #
> #struct binary_log_entry {
> # int major_protocol; /* eg. ETHERTYPE_IP */
> # int minor_protocol; /* eg. IPPROTO_TCP */
> # u_long src_ipaddress; /* Source IP Address */
> # u_long dst_ipaddress; /* Destination IP Address */
> # u_short src_port; /* Source port (if UDP or TCP) */
> # u_short dst_port; /* Desintation port (if UDP or TCP) */
> # time_t quanta_start; /* UNIX GMT time at start of quanta */
> # time_t quanta_end; /* UNIX GMT time at end of quanta */
> # u_long bytes_xfered; /* Number of bytes transfered during quanta */
> #};
Is this running on an Intel compatible CPU? A Spark CPU? An Alpha CPU?
u_long, u_short and time_t are typedef types, not native C types and the
size of int is determined by the CPU's native word size. Try compiling
and running this C program to see what their actual sizes are:
/* sizes.c */
#include <stdio.h>
#include <stdlib.h>
int main ( void )
{
printf( " int: %d\n u_long: %d\nu_short: %d\n time_t: %d\n",
sizeof( int ),
sizeof( u_long ),
sizeof( u_short ),
sizeof( time_t )
);
return EXIT_SUCCESS;
}
/* sizes.c */
> # this is the template we're going to feed to unpack( )
> my $template = "i i A16 A16 s s l l A16";
^^^ ^^^ ^^^
There are no text strings in that C struct. You probably want something
like this:
my $template = 'i i a4 a4 S S l l L';
> #$template = "C*";
>
> # open the file
> open(FILE,"/tmp/testiplog/file.binout") or die "Unable to open FILE:$!\n";
>
> # read it in one record at a time
Do the records end with a newline? If not, you may need to change the
input record separator.
$/ = \32;
> while (<FILE>) {
> # unpack it, using our template
> my
> ($eth,$itcp,$src,$dst,$sport,$dport,$qstart,$qend,$bytes)=unpack($template,$_);
>
> #print "$eth,$itcp,$src,$dst,$sport,$dport,$qstart,$qend,$bytes\n";
> print "$src,$dst,$sport,$dport,$qstart,$qend,$bytes\n";
You are probably going to need Socket::inet_net() to convert $src and
$dst to dotted quad format.
print join( ',',
inet_ntoa( $src ), inet_ntoa( $dst ),
$sport, $dport, $qstart, $qend, $bytes
), "\n";
> }
>
> # close the file
> close(FILE);
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>