Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
9 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |