Skip to content

Instantly share code, notes, and snippets.

@gpolitis
Created February 16, 2016 06:10
Show Gist options
  • Select an option

  • Save gpolitis/93c65a483a8130d32edd to your computer and use it in GitHub Desktop.

Select an option

Save gpolitis/93c65a483a8130d32edd to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use Getopt::Long qw(:config no_auto_abbrev);
use Net::Pcap;
use NetPacket::Ethernet;
use NetPacket::IP;
use NetPacket::UDP;
use Net::RTP::Packet;
$|=1;
my %options = (
count => -1,
);
GetOptions(\%options, qw{ count|c=i input|i=s output|o=s })
or die "usage: $0 [-c count] [-i file] [-o file]\n";
my $err = '';
my $pcap = pcap_open_offline($options{input}, \$err)
or die "fatal: can't open input file $options{input}: $!\n";
my $dumper;
if ($options{output}) {
$dumper = pcap_dump_open($pcap, $options{output})
or die "fatal: can't write to output file '$options{output}': $!\n";
}
pcap_loop($pcap, $options{count}, \&handle_packet, '');
pcap_close($pcap);
sub handle_packet {
my ($user_data, $header, $packet) = @_;
my $eth_data = NetPacket::Ethernet::strip($packet);
my $ip_data = NetPacket::IP::strip($eth_data);
my $udp_data = NetPacket::UDP::strip($ip_data);
my $rtp = new Net::RTP::Packet();
$rtp->decode($udp_data);
printf "packet: ssrc=%i, time=%s\n", $rtp->ssrc(), $rtp->timestamp();
pcap_dump($dumper, $header, $packet) if $options{output};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment