| Pcap(3pm) | User Contributed Perl Documentation | Pcap(3pm) |
Net::Pcap - Interface to the pcap(3) LBL packet capture library
Version 0.21
use Net::Pcap;
my $err = '';
my $dev = pcap_lookupdev(\$err); # find a device
# open the device for live listening
my $pcap = pcap_open_live($dev, 1024, 1, 0, \$err);
# loop over next 10 packets
pcap_loop($pcap, 10, \&process_packet, "just for the demo");
# close the device
pcap_close($pcap);
sub process_packet {
my ($user_data, $header, $packet) = @_;
# do something ...
}
"Net::Pcap" is a Perl binding to the LBL pcap(3) library and its Win32 counterpart, the WinPcap library. Pcap (packet capture) is a portable API to capture network packet: it allows applications to capture packets at link-layer, bypassing the normal protocol stack. It also provides features like kernel-level packet filtering and access to internal statistics.
Common applications include network statistics collection, security monitoring, network debugging, etc.
Since version 5.7.3, Perl uses a mechanism called "deferred signals" to delay signals delivery until "safe" points in the interpreter. See "Deferred Signals (Safe Signals)" in perlipc for a detailed explanation.
Since "Net::Pcap" version 0.08, released in October 2005, the module modified the internal variable "PL_signals" to re-enable immediate signals delivery in Perl 5.8 and later within some XS functions (CPAN-RT #6320). However, it can create situations where the Perl interpreter is less stable and can crash (CPAN-RT #43308). Therefore, as of version 0.17, "Net::Pcap" no longer modifies "PL_signals" by itself, but provides facilities so the user has full control of how signals are delivered.
First, the pcap_perl_settings() function allows one to select how signals are handled:
pcap_perl_settings(PERL_SIGNALS_UNSAFE);
pcap_loop($pcap, 10, \&process_packet, "");
pcap_perl_settings(PERL_SIGNALS_SAFE);
Then, to easily make code interruptable, "Net::Pcap" provides the "UNSAFE_SIGNALS" pseudo-bloc:
UNSAFE_SIGNALS {
pcap_loop($pcap, 10, \&process_packet, "");
};
(Stolen from Rafael Garcia-Suarez's "Perl::Unsafe::Signals")
"Net::Pcap" supports the following "Exporter" tags:
BPF_ALIGNMENT BPF_MAJOR_VERSION BPF_MAXBUFSIZE BPF_MAXINSNS
BPF_MEMWORDS BPF_MINBUFSIZE BPF_MINOR_VERSION BPF_RELEASE
DLT_AIRONET_HEADER DLT_APPLE_IP_OVER_IEEE1394 DLT_ARCNET
DLT_ARCNET_LINUX DLT_ATM_CLIP DLT_ATM_RFC1483 DLT_AURORA
DLT_AX25 DLT_CHAOS DLT_CHDLC DLT_CISCO_IOS DLT_C_HDLC
DLT_DOCSIS DLT_ECONET DLT_EN10MB DLT_EN3MB DLT_ENC DLT_FDDI
DLT_FRELAY DLT_HHDLC DLT_IBM_SN DLT_IBM_SP DLT_IEEE802
DLT_IEEE802_11 DLT_IEEE802_11_RADIO DLT_IEEE802_11_RADIO_AVS
DLT_IPFILTER DLT_IP_OVER_FC DLT_JUNIPER_ATM1 DLT_JUNIPER_ATM2
DLT_JUNIPER_ES DLT_JUNIPER_GGSN DLT_JUNIPER_MFR DLT_JUNIPER_MLFR
DLT_JUNIPER_MLPPP DLT_JUNIPER_MONITOR DLT_JUNIPER_SERVICES
DLT_LINUX_IRDA DLT_LINUX_SLL DLT_LOOP DLT_LTALK DLT_NULL
DLT_OLD_PFLOG DLT_PCI_EXP DLT_PFLOG DLT_PFSYNC DLT_PPP
DLT_PPP_BSDOS DLT_PPP_ETHER DLT_PPP_SERIAL DLT_PRISM_HEADER
DLT_PRONET DLT_RAW DLT_RIO DLT_SLIP DLT_SLIP_BSDOS DLT_SUNATM
DLT_SYMANTEC_FIREWALL DLT_TZSP DLT_USER0 DLT_USER1 DLT_USER2
DLT_USER3 DLT_USER4 DLT_USER5 DLT_USER6 DLT_USER7 DLT_USER8
DLT_USER9 DLT_USER10 DLT_USER11 DLT_USER12 DLT_USER13
DLT_USER14 DLT_USER15
PCAP_ERRBUF_SIZE PCAP_IF_LOOPBACK
PCAP_VERSION_MAJOR PCAP_VERSION_MINOR
MODE_CAPT MODE_MON MODE_STAT
OPENFLAG_PROMISCUOUS OPENFLAG_DATATX_UDP OPENFLAG_NOCAPTURE_RPCAP
PCAP_SRC_FILE PCAP_SRC_IFLOCAL PCAP_SRC_IFREMOTE
PCAP_SAMP_NOSAMP PCAP_SAMP_1_EVERY_N PCAP_SAMP_FIRST_AFTER_N_MS
RMTAUTH_NULL RMTAUTH_PWD
By default, this module exports the symbols from the ":datalink" and ":pcap" tags, and all the functions, with the same names as the C library.
All functions defined by "Net::Pcap" are direct mappings to the libpcap functions. Consult the pcap(3) documentation and source code for more information.
Arguments that change a parameter, for example pcap_lookupdev(), are passed that parameter as a reference. This is to retain compatibility with previous versions of "Net::Pcap".
Example
$dev = pcap_lookupdev();
Example
@devs = pcap_findalldevs(\%devinfo, \$err);
for my $dev (@devs) {
print "$dev : $devinfo{$dev}\n"
}
@devs = pcap_findalldevs(\$err);
@devs = pcap_findalldevs(\$err, \%devinfo);
The first form was introduced by Marco Carnut in "Net::Pcap" version 0.05 and kept intact in versions 0.06 and 0.07. The second form was introduced by Jean-Louis Morel for the Windows only, ActivePerl port of "Net::Pcap", in versions 0.04.01 and 0.04.02.
The new syntax has been introduced for consistency with the rest of the Perl API and the C API of libpcap(3), where $err is always the last argument.
Example
$dev = pcap_lookupdev();
$pcap = pcap_open_live($dev, 1024, 1, 0, \$err)
or die "Can't open device $dev: $err\n";
Example
$pcap = pcap_open_dead(0, 1024);
Example
$pcap = pcap_open_offline($dump, \$err)
or die "Can't read '$dump': $err\n";
The callback function is also passed packet header information and packet data like so:
sub process_packet {
my ($user_data, $header, $packet) = @_;
...
}
The header information is a reference to a hash containing the following fields.
Example
pcap_loop($pcap, 10, \&process_packet, "user data");
sub process_packet {
my ($user_data, $header, $packet) = @_;
# ...
}
This routine is safe to use inside a signal handler on UNIX or a console control handler on Windows, as it merely sets a flag that is checked within the loop.
Please see the section on pcap_breakloop() in pcap(3) for more information.
In non-blocking mode, an attempt to read from the capture descriptor with pcap_dispatch() will, if no packets are currently available to be read, return 0 immediately rather than blocking waiting for packets to arrive. pcap_loop() and pcap_next() will not work in non-blocking mode.
Example
my $dump_file = 'network.dmp';
my $dev = pcap_lookupdev();
my $pcap = pcap_open_live($dev, 1024, 1, 0, \$err);
my $dumper = pcap_dump_open($pcap, $dump_file);
pcap_loop($pcap, 10, \&process_packet, '');
pcap_dump_close($dumper);
sub process_packet {
my ($user_data, $header, $packet) = @_;
pcap_dump($dumper, $header, $packet);
}
Example
$linktype = pcap_datalink($pcap);
Example
$linktype = pcap_datalink_name_to_val('LTalk'); # returns DLT_LTALK
Example
$name = pcap_datalink_val_to_name(DLT_LTALK); # returns 'LTALK'
Example
$descr = pcap_datalink_val_to_description(DLT_LTALK); # returns 'Localtalk'
This function is supported only on live captures, not on savefiles; no statistics are stored in savefiles, so no statistics are available when reading from a savefile.
The following functions are specific to the Perl binding of libpcap.
Example:
local $SIG{ALRM} = sub { pcap_breakloop() };
alarm 60;
pcap_perl_settings(PERL_SIGNALS_UNSAFE);
pcap_loop($pcap, 10, \&process_packet, "");
pcap_perl_settings(PERL_SIGNALS_SAFE);
The following functions are only available with WinPcap, the Win32 port of the Pcap library. If a called function is not available, it will cleanly croak().
This function is provided in order to help the user creating the source string according to the new format. An unique source string is used in order to make easy for old applications to use the remote facilities. Think about tcpdump(1), for example, which has only one way to specify the interface on which the capture has to be started. However, GUI-based programs can find more useful to specify hostname, port and interface name separately. In that case, they can use this function to create the source string before passing it to the pcap_open() function.
Returns 0 if everything is fine, -1 if some errors occurred. The string containing the complete source is returned in the $source variable.
This call is the other way round of pcap_createsrcstr(). It accepts a null-terminated string and it returns the parameters related to the source. This includes:
The user can omit some parameters in case it is not interested in them.
Returns 0 if everything is fine, -1 if some errors occurred. The requested values (host name, network port, type of the source) are returned into the proper variables passed by reference.
The pcap_open() replaces all the pcap_open_xxx() functions with a single call.
This function hides the differences between the different pcap_open_xxx() functions so that the programmer does not have to manage different opening function. In this way, the true open() function is decided according to the source type, which is included into the source string (in the form of source prefix).
Returns a pointer to a pcap descriptor which can be used as a parameter to the following calls (compile() and so on) and that specifies an opened WinPcap session. In case of problems, it returns "undef" and the $err variable keeps the error message.
If an old buffer was already created with a previous call to setbuff(), it is deleted and its content is discarded. open_live() creates a 1 MB buffer by default.
$memsize is the size, in bytes, of the queue, therefore it determines the maximum amount of data that the queue will contain. This memory is automatically deallocated when the queue ceases to exist.
The %headerr header structure is the same used by WinPcap and libpcap to store the packets in a file, therefore sending a capture file is straightforward. "Raw packet" means that the sending application will have to include the protocol headers, since every packet is sent to the network as is. The CRC of the packets needs not to be calculated, because it will be transparently added by the network interface.
The return value is the amount of bytes actually sent. If it is smaller than the size parameter, an error occurred during the send. The error can be caused by a driver/adapter problem or by an inconsistent/bogus send queue.
"Net::Pcap" exports by default the names of several constants in order to ease the development of programs. See "EXPORTS" for details about which constants are exported.
Here are the descriptions of a few data link types. See pcap(3) for a more complete description and semantics associated with each data link.
Please report any bugs or feature requests to "bug-Net-Pcap@rt.cpan.org", or through the web interface at <http://rt.cpan.org/Dist/Display.html?Queue=Net-Pcap>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
Currently known bugs:
See the eg/ and t/ directories of the "Net::Pcap" distribution for examples on using this module.
the NetPacket or Net::Frame modules to assemble and disassemble packets.
Net::Pcap::Reassemble for reassembly of TCP/IP fragments.
POE::Component::Pcap for using "Net::Pcap" within POE-based programs.
AnyEvent::Pcap for using "Net::Pcap" within AnyEvent-based programs.
Net::Packet or NetPacket for decoding and creating network packets.
Net::Pcap::Easy is a module which provides an easier, more Perl-ish API than "Net::Pcap" and integrates some facilities from Net::Netmask and NetPacket.
pcap(3), tcpdump(8)
The source code for the pcap(3) library is available from <http://www.tcpdump.org/>
The source code and binary for the Win32 version of the pcap library, WinPcap, is available from <http://www.winpcap.org/>
Hacking Linux Exposed: Sniffing with Net::Pcap to stealthily managing iptables rules remotely, <http://www.hackinglinuxexposed.com/articles/20030730.html>
PerlMonks node about Net::Pcap, <http://perlmonks.org/?node_id=170648>
Current maintainer is Sébastien Aperghis-Tramoni (SAPER) with the help of Tim Wilde (TWILDE).
Complete list of authors & contributors:
The original version of "Net::Pcap", version 0.01, was written by Peter Lister using SWIG.
Version 0.02 was created by Bo Adler with a few bugfixes but not uploaded to CPAN. It could be found at: <http://www.buttsoft.com/~thumper/software/perl/Net-Pcap/>
Versions 0.03 and 0.04 were created by Tim Potter who entirely rewrote "Net::Pcap" using XS and wrote the documentation, with the help of David N. Blank-Edelman for testing and general polishing.
Version 0.05 was released by Marco Carnut with fixes to make it work with Cygwin and WinPcap.
Version 0.04.02 was independently created by Jean-Louis Morel but not uploaded on the CPAN. It can be found here: <http://www.bribes.org/perl/wnetpcap.html>
Based on Tim Potter's version 0.04, it included fixes for WinPcap and added wrappers for several new libpcap functions as well as WinPcap specific functions.
To Paul Johnson for his module Devel::Cover and his patience for helping me using it with XS code, which revealed very useful for writing more tests.
To the beta-testers: Jean-Louis Morel, Max Maischen, Philippe Bruhat, David Morel, Scott Lanning, Rafael Garcia-Suarez, Karl Y. Pradene.
Copyright (C) 2005-2016 Sébastien Aperghis-Tramoni and contributors. All rights reserved.
Copyright (C) 2003 Marco Carnut. All rights reserved.
Copyright (C) 1999, 2000 Tim Potter. All rights reserved.
Copyright (C) 1998 Bo Adler. All rights reserved.
Copyright (C) 1997 Peter Lister. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| 2024-03-31 | perl v5.38.2 |