Net::PcapWriter - simple creation of pcap files from code
use Net::PcapWriter; my $writer = Net::PcapWriter->new('test.pcap'); my $conn = $writer->tcp_conn('1.2.3.4',1234,'5.6.7.8',80); # this will automatically add syn..synack..ack handshake to pcap # each write will be a single packet $conn->write(0,"POST / HTTP/1.0\r\nContent-length: 3\r\n\r\n"); $conn->ack(1); # force ack from server # send another packet w/o forcing ack $conn->write(0,"abc"); # client will no longer write $conn->shutdown(0); # this will automatically add ack to last packet $conn->write(1,"HTTP/1.0 200 Ok\r\nContent-length: 10\r\n\r\n"); $conn->write(1,"0123456789"); # will automatically add remaining FIN+ACK undef $conn; # write some UDP packets with IPv6 $conn = $writer->udp_conn('dead::beaf',1234,'beaf::dead',53); $conn->write(0,"...."); $conn->write(1,"...."); # write a ping exchange (works also with IPv6) $conn = $writer->icmp_echo_conn('1.2.3.4','5.6.7.8',10); $conn->ping(1,"foo"); $conn->ping(2,"bar"); $conn->pong(1,"foo");
With Net::PcapWriter it is possible to create pcap files within a program without capturing any data. This is useful for setting up test data without setting up the needed infrastructure for data creation and capturing.
The following methods are supported:
Creates new object. If file name is given it will be opened for writing, if file handle is given it will be used. Otherwise the pcap data will be written to STDOUT. Will write pcap header for DLT_RAW to pcap file.
Will write raw IP packet $pkt with $timestamp in pcap file. $timestamp can be time_t (seconds), float (like time_t, but with higher resolution) or <[$sec,$msec]> like in <struct timeval>. If $timestamp is not given will use Time::HiRes::gettimeofday.
time_t
<[$sec,$msec]
<struct timeval
Time::HiRes::gettimeofday
Will return Net::PcapWriter::TCP object, which then provides the following methods:
Net::PcapWriter::TCP
Will write the given data for the direction $dir (0 are data from client to server, 1 the other way). Will write TCP handshake if not done yet.
$dir
Will write an empty message with an ACK from direction $dir.
Will add FIN+ACK for shutdown from direction $dir unless already done.
Will call shutdown for both $dir before destroying connection object.
Will return Net::PcapWriter::UDP object, which then provides the following methods:
Net::PcapWriter::UDP
Will write the given data for the direction $dir (0 are data from client to server, 1 the other way).
Will return Net::PcapWriter::ICMP_Echo object which provides a connection with echo request and reply using the identifier $id (default 0). This object can handle echo request/reply for ICMP and ICMPv6. It has the following methods:
Net::PcapWriter::ICMP_Echo
Will write an ICMP echo request from connection source to destination with sequence $seq and data $data.
Will write an ICMP echo reply from connection destination to source with sequence $seq and data $data.
Steffen Ullrich <sullr@cpan.org>
To install Net::PcapWriter, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::PcapWriter
CPAN shell
perl -MCPAN -e shell install Net::PcapWriter
For more information on module installation, please visit the detailed CPAN module installation guide.