perlpreben has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Im making a network sniffer, and I am using Sniffer::HTTP.
An awesome module that does the job quick nice.

But im having problems printing parts of the values returned, and i was hoping someone with this experience could show me how to print the hash values.....

Here is snipped from my script (taken from the docs):
my $sniffer = Sniffer::HTTP->new( callbacks => { request => sub { my ($req,$conn) = @_;
And I can get the sniffer URI and such by doing:

print $req->uri;
But i also need the information stored in the $conn variable.
Especially the sequence number and such. And I just cant figure out how to print this information....

Any help would be appreciated.

Here is a full dump of the $conn variable...

$self = bless( { callbacks => { closed => 'V: $self_eclipse_1->{clos +ed}', log => 'V: $self_eclipse_1->{log} +', request => 'V: $self_eclipse_1->{requ +est}', response => 'V: $self_eclipse_1->{resp +onse}', tcp_log => sub { use warnings; use strict 'refs'; print $_[0] if $VERBOSE +> 1; } }, connections => { "53302:80" => 'V: $self_eclipse_1' }, pcap_device => bless( \do { my $v = 140795392 }, 'pcap_ +tPtr' ), stale_connection => sub { use warnings; use strict 'refs'; my($s, $conn, $key); if ($key) { print "Connection stalled .... $key +....\n" if $debug; $s->log->("Connection $key is stale. +"); $s->remove_connection($key); } }, timeout => 300 }, 'Sniffer::HTTP' ); $self_eclipse_1 = bless( { _request => bless( { _content => '', _headers => bless( { accept => + 'text/html,application/xhtml+xml,application/xml;q=0.9, + */*;q=0.8', "accept-charset" => + 'ISO-8859-1,utf-8;q=0.7,*;q=0.7', "accept-encoding" => + 'gzip,deflate', "accept-language" => + 'en-us,en;q=0.5', connection => + 'keep-alive', host => + 'www.example.com', "keep-alive" => + 300, "user-agent" => + 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Ge + cko/2008072820 Firefox/3.0.1' }, 'HTTP::Headers' ), _method => 'GET', _protocol => 'HTTP/1.1', _uri => \do { my $v = 'http:// +www.example.com/?id=' } }, 'HTTP::Request' ), closed => sub { package Sniffer::HTTP; use strict 'refs'; my $key = $_[0]->flow; if (not exists $args{'connections'} +{$key}) { warn 'Error: flow() ne connection +-key!'; $key = join(':', reverse(split(/: +/, $key, 0))); } $_[0]{'log'}("Removing $key"); $self->remove_connection($key); goto \&$user_closed if $user_closed +; }, log => sub { use warnings; use strict 'refs'; print $_[0] if $VERBOSE; }, recv_buffer => \do { my $v = undef }, request => sub { use warnings; use strict 'refs'; my($req, $conn) = @_; Dump($conn); &analyze($req->method, $req->uri, $ +req->header('host'), $req->header('accept'), $req->heade + r('connection'), $req->header('cookie'), $req->head +er('referer'), $req->header('user-agent'), $req->content($post_bytes_ +to_rec eive), $req->protocol); }, response => sub { use warnings; use strict 'refs'; my($res, $req, $conn) = @_; }, sent_buffer => \do { my $v = '' }, tcp_connection => bless( { ack_start => 2620583270, closed => sub { package Sniffer::Con +nection::HTTP; use strict 'refs'; }, dest_port => 80, last_activity => 1234622727, log => $self->{callbacks}{tcp +_log}, received_data => sub { package Sniffer::Con +nection::HTTP; use strict 'refs'; $self_eclipse_1->rec +eived_data(@_); }, sent_data => sub { package Sniffer::Con +nection::HTTP; use strict 'refs'; $self_eclipse_1->sen +t_data(@_); }, sequence_start => 4131145978, src_port => 53302, status => 'SYN_ACK', teardown => sub { package Sniffer::Con +nection::HTTP; use strict 'refs'; $self_eclipse_1->clo +sed->($self_eclipse_1); }, window => { dest => { 2620583270 + => bless( { _frame +=> "\0P\3206\2342\355f\366<Jy\200\20\0006\202\354\0\0\1\ + 1\b\n+\365J\236\t\270\200L", _parent +=> undef, acknum +=> 4131146361, cksum +=> 33516, data +=> '', dest_port +=> 53302, flags +=> 16, hlen +=> 8, options +=> "\1\1\b\n+\365J\236\t\270\200L", reserved +=> 0, seqnum +=> 2620583270, src_port +=> 80, urg +=> 0, winsize +=> 54 }, 'NetPacke +t::TCP' ) }, src => {} } }, 'Sniffer::Connection' ) }, 'Sniffer::Connection::HTTP' ); $self->{callbacks}{closed} = $self_eclipse_1->{closed}; $self->{callbacks}{log} = $self_eclipse_1->{log}; $self->{callbacks}{request} = $self_eclipse_1->{request}; $self->{callbacks}{response} = $self_eclipse_1->{response}; $self->{connections}{"53302:80"} = $self_eclipse_1; $user_closed = undef; %args = ( callbacks => $self->{callbacks}, connections => $self->{connections}, stale_connection => $self->{stale_connection}, timeout => 300 );

Replies are listed 'Best First'.
Re: Cant seem to print perl hash ...
by Corion (Patriarch) on Feb 14, 2009 at 15:17 UTC

    I haven't looked at the source code, but I'm not sure that the sequence number will make much sense after the request has been parsed into a HTTP::Request anymore. You haven't shown any code that works, but from looking at the data structure, I guess that

    $self_eclipse_1->tcp_connection->sequence_start

    or

    $self_eclipse_1->tcp_connection->last_ack

    might give you the numbers. Maybe you want to read the source code or perldsc or References Quick Reference to learn about how to access the values.

      woahh.... i cant beleive i didn get that. I got it to work..! Thanks man!
Re: Cant seem to print perl hash ...
by Bloodnok (Vicar) on Feb 14, 2009 at 15:25 UTC
    I'm afraid I can't see your problem - you ask for ...the information stored in the $conn variable., but later say Here is a full dump of the $conn variable... or put another way, it seems to me you've answered your own question.

    A user level that continues to overstate my experience :-))
      Well thats the thing.. I can see the information is there.... i just dont know how to extract it and work with it :)
        Hmmm, so what you're saying is that the node is not well titled ...

        Anyway, I presume you saw and attempted to use, Corion's very pertinent suggestion, which wasn't a great success ... otherwise, you would have told us.

        As Corion suggests, you'd probably get more help if you'd enlighten us further with e.g. a sample of the code in which you are attempting to use $conn.

        A user level that continues to overstate my experience :-))