Skip to content
Browse files

Add json2pcap to write pcap files from JSON input.

Required the ability to open output files and write to them in
different ways.  For example, using Net::Pcap's functions for those
purposes.
  • Loading branch information...
1 parent 4ebaf97 commit 2b51acc36f1abbe5670605952fddc8cfde6450dd @rcaputo committed Jun 13, 2011
View
7 bin/json2pcap
@@ -0,0 +1,7 @@
+#!/usr/bin/perl
+use App::PipeFilter::JsonToPcap;
+exit App::PipeFilter::JsonToPcap->new_with_options()->run();
+
+__END__
+
+# vim: ts=2 sw=2 expandtab
View
2 lib/App/PipeFilter/Generic.pm
@@ -29,7 +29,7 @@ sub filter_file {
while ($self->read_input($ifh, \$buffer)) {
next unless my (@input) = $self->decode_input(\$buffer);
next unless my (@output) = $self->transform(@input);
- print $ofh $_ foreach $self->encode_output(@output);
+ $self->write_output($ofh, $self->encode_output(@output));
}
}
View
1 lib/App/PipeFilter/Generic/Json.pm
@@ -6,6 +6,7 @@ extends 'App::PipeFilter::Generic';
with qw(
App::PipeFilter::Role::Reader::Sysread
+ App::PipeFilter::Role::Writer::Print
App::PipeFilter::Role::Input::Json
App::PipeFilter::Role::Output::Json
);
View
19 lib/App/PipeFilter/JsonToPcap.pm
@@ -0,0 +1,19 @@
+package App::PipeFilter::JsonToPcap;
+
+use Moose;
+extends 'App::PipeFilter::Generic';
+
+with (
+ "App::PipeFilter::Role::Reader::Sysread",
+ "App::PipeFilter::Role::Input::Json",
+ "App::PipeFilter::Role::Transform::None",
+ "App::PipeFilter::Role::Opener::PcapOutput",
+ "App::PipeFilter::Role::Output::Pcap",
+ "App::PipeFilter::Role::Writer::Pcap",
+);
+
+1;
+
+__END__
+
+# vim: ts=2 sw=2 expandtab
View
1 lib/App/PipeFilter/PcapToJson.pm
@@ -6,6 +6,7 @@ extends 'App::PipeFilter::Generic';
with (
"App::PipeFilter::Role::Opener::PcapInput",
"App::PipeFilter::Role::Reader::Pcap",
+ "App::PipeFilter::Role::Writer::Print",
"App::PipeFilter::Role::Input::ArrayBuffer",
"App::PipeFilter::Role::Transform::None",
"App::PipeFilter::Role::Output::Json",
View
27 lib/App/PipeFilter/Role/Opener/PcapOutput.pm
@@ -0,0 +1,27 @@
+package App::PipeFilter::Role::Opener::PcapOutput;
+
+use Moose::Role;
+
+use Net::Pcap qw(DLT_EN10MB pcap_open_dead pcap_dump_open);
+
+sub open_output {
+ my ($self, $filename) = @_;
+
+ my $pcap;
+ unless ($pcap = pcap_open_dead(DLT_EN10MB, 65536)) {
+ die "couldn't pcap_open_dead: $!";
+ }
+
+ my $dumper;
+ unless ($dumper = pcap_dump_open($pcap, $filename)) {
+ die "couldn't open $filename for writing: " . pcap_geterr($pcap);
+ }
+
+ return $dumper;
+}
+
+1;
+
+__END__
+
+# vim: ts=2 sw=2 expandtab
View
31 lib/App/PipeFilter/Role/Output/Pcap.pm
@@ -0,0 +1,31 @@
+package App::PipeFilter::Role::Output::Pcap;
+
+use Moose::Role;
+
+use Net::Pcap qw(pcap_dump);
+
+sub encode_output {
+ # Skips $self in $_[0].
+
+ return(
+ map {
+ my $pcap_rec = $_;
+ my $packet = pack "H*", delete $pcap_rec->{pcap_data};
+
+ +[
+ {
+ map { /^pcap_(\S+)/ ? ($1 => $pcap_rec->{$_}) : () }
+ keys %$pcap_rec
+ },
+ $packet
+ ];
+ }
+ @_[2..$#_]
+ );
+}
+
+1;
+
+__END__
+
+# vim: ts=2 sw=2 expandtab
View
18 lib/App/PipeFilter/Role/Writer/Pcap.pm
@@ -0,0 +1,18 @@
+package App::PipeFilter::Role::Writer::Pcap;
+
+use Moose::Role;
+use Net::Pcap qw(pcap_dump pcap_dump_flush);
+
+sub write_output {
+ my $pcap_dumper = $_[1];
+
+ pcap_dump($pcap_dumper, @$_) foreach @_[2..$#_];
+ pcap_dump_flush($pcap_dumper);
+}
+
+no Moose::Role;
+1;
+
+__END__
+
+# vim: ts=2 sw=2 expandtab
View
13 lib/App/PipeFilter/Role/Writer/Print.pm
@@ -0,0 +1,13 @@
+package App::PipeFilter::Role::Writer::Print;
+
+use Moose::Role;
+
+sub write_output {
+ print { $_[1] } @_[2..$#_];
+}
+
+1;
+
+__END__
+
+# vim: ts=2 sw=2 expandtab

0 comments on commit 2b51acc

Please sign in to comment.
Something went wrong with that request. Please try again.