.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Pcap 3pm" .TH Pcap 3pm 2024-03-07 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Net::Pcap \- Interface to the pcap(3) LBL packet capture library .SH VERSION .IX Header "VERSION" Version 0.21 .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Net::Pcap; \& \& my $err = \*(Aq\*(Aq; \& my $dev = pcap_lookupdev(\e$err); # find a device \& \& # open the device for live listening \& my $pcap = pcap_open_live($dev, 1024, 1, 0, \e$err); \& \& # loop over next 10 packets \& pcap_loop($pcap, 10, \e&process_packet, "just for the demo"); \& \& # close the device \& pcap_close($pcap); \& \& sub process_packet { \& my ($user_data, $header, $packet) = @_; \& # do something ... \& } .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" \&\f(CW\*(C`Net::Pcap\*(C'\fR is a Perl binding to the LBL \fBpcap\fR\|(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. .PP Common applications include network statistics collection, security monitoring, network debugging, etc. .SH NOTES .IX Header "NOTES" .SS "Signals handling" .IX Subsection "Signals handling" 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. .PP Since \f(CW\*(C`Net::Pcap\*(C'\fR version 0.08, released in October 2005, the module modified the internal variable \f(CW\*(C`PL_signals\*(C'\fR 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, \f(CW\*(C`Net::Pcap\*(C'\fR no longer modifies \f(CW\*(C`PL_signals\*(C'\fR by itself, but provides facilities so the user has full control of how signals are delivered. .PP First, the \f(CWpcap_perl_settings()\fR function allows one to select how signals are handled: .PP .Vb 3 \& pcap_perl_settings(PERL_SIGNALS_UNSAFE); \& pcap_loop($pcap, 10, \e&process_packet, ""); \& pcap_perl_settings(PERL_SIGNALS_SAFE); .Ve .PP Then, to easily make code interruptable, \f(CW\*(C`Net::Pcap\*(C'\fR provides the \&\f(CW\*(C`UNSAFE_SIGNALS\*(C'\fR pseudo-bloc: .PP .Vb 3 \& UNSAFE_SIGNALS { \& pcap_loop($pcap, 10, \e&process_packet, ""); \& }; .Ve .PP (Stolen from Rafael Garcia-Suarez's \f(CW\*(C`Perl::Unsafe::Signals\*(C'\fR) .SH EXPORTS .IX Header "EXPORTS" \&\f(CW\*(C`Net::Pcap\*(C'\fR supports the following \f(CW\*(C`Exporter\*(C'\fR tags: .IP \(bu 4 \&\f(CW\*(C`:bpf\*(C'\fR exports a few BPF related constants: .Sp .Vb 2 \& BPF_ALIGNMENT BPF_MAJOR_VERSION BPF_MAXBUFSIZE BPF_MAXINSNS \& BPF_MEMWORDS BPF_MINBUFSIZE BPF_MINOR_VERSION BPF_RELEASE .Ve .IP \(bu 4 \&\f(CW\*(C`:datalink\*(C'\fR exports the data link types macros: .Sp .Vb 10 \& 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 .Ve .IP \(bu 4 \&\f(CW\*(C`:pcap\*(C'\fR exports the following \f(CW\*(C`pcap\*(C'\fR constants: .Sp .Vb 2 \& PCAP_ERRBUF_SIZE PCAP_IF_LOOPBACK \& PCAP_VERSION_MAJOR PCAP_VERSION_MINOR .Ve .IP \(bu 4 \&\f(CW\*(C`:mode\*(C'\fR exports the following constants: .Sp .Vb 1 \& MODE_CAPT MODE_MON MODE_STAT .Ve .IP \(bu 4 \&\f(CW\*(C`:openflag\*(C'\fR exports the following constants: .Sp .Vb 1 \& OPENFLAG_PROMISCUOUS OPENFLAG_DATATX_UDP OPENFLAG_NOCAPTURE_RPCAP .Ve .IP \(bu 4 \&\f(CW\*(C`:source\*(C'\fR exports the following constants: .Sp .Vb 1 \& PCAP_SRC_FILE PCAP_SRC_IFLOCAL PCAP_SRC_IFREMOTE .Ve .IP \(bu 4 \&\f(CW\*(C`:sample\*(C'\fR exports the following constants: .Sp .Vb 1 \& PCAP_SAMP_NOSAMP PCAP_SAMP_1_EVERY_N PCAP_SAMP_FIRST_AFTER_N_MS .Ve .IP \(bu 4 \&\f(CW\*(C`:rpcap\*(C'\fR exports the following constants: .Sp .Vb 1 \& RMTAUTH_NULL RMTAUTH_PWD .Ve .IP \(bu 4 \&\f(CW\*(C`:functions\*(C'\fR short names of the functions (without the \f(CW"pcap_"\fR prefix) for those which would not cause a clash with an already defined name. Namely, the following functions are not available in short form: \&\f(CWopen()\fR, \f(CWclose()\fR, \f(CWnext()\fR, \f(CWdump()\fR, \f(CWfile()\fR, \f(CWfileno()\fR. Using these short names is now discouraged, and may be removed in the future. .PP By default, this module exports the symbols from the \f(CW\*(C`:datalink\*(C'\fR and \&\f(CW\*(C`:pcap\*(C'\fR tags, and all the functions, with the same names as the C library. .SH FUNCTIONS .IX Header "FUNCTIONS" All functions defined by \f(CW\*(C`Net::Pcap\*(C'\fR are direct mappings to the libpcap functions. Consult the \fBpcap\fR\|(3) documentation and source code for more information. .PP Arguments that change a parameter, for example \f(CWpcap_lookupdev()\fR, are passed that parameter as a reference. This is to retain compatibility with previous versions of \f(CW\*(C`Net::Pcap\*(C'\fR. .SS "Lookup functions" .IX Subsection "Lookup functions" .IP \fBpcap_lookupdev(\e$err)\fR 4 .IX Item "pcap_lookupdev($err)" Returns the name of a network device that can be used with \&\f(CWpcap_open_live()\fR function. On error, the \f(CW$err\fR parameter is filled with an appropriate error message else it is undefined. .Sp \&\fBExample\fR .Sp .Vb 1 \& $dev = pcap_lookupdev(); .Ve .IP "\fBpcap_findalldevs(\e%devinfo, \e$err)\fR" 4 .IX Item "pcap_findalldevs(%devinfo, $err)" Returns a list of all network device names that can be used with \&\f(CWpcap_open_live()\fR function. On error, the \f(CW$err\fR parameter is filled with an appropriate error message else it is undefined. .Sp \&\fBExample\fR .Sp .Vb 4 \& @devs = pcap_findalldevs(\e%devinfo, \e$err); \& for my $dev (@devs) { \& print "$dev : $devinfo{$dev}\en" \& } .Ve .RS 4 .IP \fBNote\fR 4 .IX Item "Note" For backward compatibility reasons, this function can also be called using the following signatures: .Sp .Vb 1 \& @devs = pcap_findalldevs(\e$err); \& \& @devs = pcap_findalldevs(\e$err, \e%devinfo); .Ve .Sp The first form was introduced by Marco Carnut in \f(CW\*(C`Net::Pcap\*(C'\fR 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 \f(CW\*(C`Net::Pcap\*(C'\fR, in versions 0.04.01 and 0.04.02. .Sp The new syntax has been introduced for consistency with the rest of the Perl API and the C API of \f(CWlibpcap(3)\fR, where \f(CW$err\fR is always the last argument. .RE .RS 4 .RE .IP "\fBpcap_lookupnet($dev, \e$net, \e$mask, \e$err)\fR" 4 .IX Item "pcap_lookupnet($dev, $net, $mask, $err)" Determine the network number and netmask for the device specified in \&\f(CW$dev\fR. The function returns 0 on success and sets the \f(CW$net\fR and \&\f(CW$mask\fR parameters with values. On failure it returns \-1 and the \&\f(CW$err\fR parameter is filled with an appropriate error message. .SS "Packet capture functions" .IX Subsection "Packet capture functions" .ie n .IP "\fBpcap_open_live($dev, \fR\fB$snaplen\fR\fB, \fR\fB$promisc\fR\fB, \fR\fB$to_ms\fR\fB, \e$err)\fR" 4 .el .IP "\fBpcap_open_live($dev, \fR\f(CB$snaplen\fR\fB, \fR\f(CB$promisc\fR\fB, \fR\f(CB$to_ms\fR\fB, \e$err)\fR" 4 .IX Item "pcap_open_live($dev, $snaplen, $promisc, $to_ms, $err)" Returns a packet capture descriptor for looking at packets on the network. The \f(CW$dev\fR parameter specifies which network interface to capture packets from. The \f(CW$snaplen\fR and \f(CW$promisc\fR parameters specify the maximum number of bytes to capture from each packet, and whether to put the interface into promiscuous mode, respectively. The \f(CW$to_ms\fR parameter specifies a read timeout in milliseconds. The packet descriptor will be undefined if an error occurs, and the \f(CW$err\fR parameter will be set with an appropriate error message. .Sp \&\fBExample\fR .Sp .Vb 3 \& $dev = pcap_lookupdev(); \& $pcap = pcap_open_live($dev, 1024, 1, 0, \e$err) \& or die "Can\*(Aqt open device $dev: $err\en"; .Ve .ie n .IP "\fBpcap_open_dead($linktype, \fR\fB$snaplen\fR\fB)\fR" 4 .el .IP "\fBpcap_open_dead($linktype, \fR\f(CB$snaplen\fR\fB)\fR" 4 .IX Item "pcap_open_dead($linktype, $snaplen)" Creates and returns a new packet descriptor to use when calling the other functions in \f(CW\*(C`libpcap\*(C'\fR. It is typically used when just using \f(CW\*(C`libpcap\*(C'\fR for compiling BPF code. .Sp \&\fBExample\fR .Sp .Vb 1 \& $pcap = pcap_open_dead(0, 1024); .Ve .IP "\fBpcap_open_offline($filename, \e$err)\fR" 4 .IX Item "pcap_open_offline($filename, $err)" Return a packet capture descriptor to read from a previously created "savefile". The returned descriptor is undefined if there was an error and in this case the \f(CW$err\fR parameter will be filled. Savefiles are created using the \f(CW\*(C`pcap_dump_*\*(C'\fR commands. .Sp \&\fBExample\fR .Sp .Vb 2 \& $pcap = pcap_open_offline($dump, \e$err) \& or die "Can\*(Aqt read \*(Aq$dump\*(Aq: $err\en"; .Ve .ie n .IP "\fBpcap_loop($pcap, \fR\fB$count\fR\fB, \e&callback, \fR\fB$user_data\fR\fB)\fR" 4 .el .IP "\fBpcap_loop($pcap, \fR\f(CB$count\fR\fB, \e&callback, \fR\f(CB$user_data\fR\fB)\fR" 4 .IX Item "pcap_loop($pcap, $count, &callback, $user_data)" Read \f(CW$count\fR packets from the packet capture descriptor \f(CW$pcap\fR and call the perl function \f(CW&callback\fR with an argument of \f(CW$user_data\fR. If \f(CW$count\fR is negative, then the function loops forever or until an error occurs. Returns 0 if \f(CW$count\fR is exhausted, \-1 on error, and \-2 if the loop terminated due to a call to \fBpcap_breakloop()\fR before any packets were processed. .Sp The callback function is also passed packet header information and packet data like so: .Sp .Vb 2 \& sub process_packet { \& my ($user_data, $header, $packet) = @_; \& \& ... \& } .Ve .Sp The header information is a reference to a hash containing the following fields. .RS 4 .IP \(bu 4 \&\f(CW\*(C`len\*(C'\fR \- the total length of the packet. .IP \(bu 4 \&\f(CW\*(C`caplen\*(C'\fR \- the actual captured length of the packet data. This corresponds to the snapshot length parameter passed to \f(CWopen_live()\fR. .IP \(bu 4 \&\f(CW\*(C`tv_sec\*(C'\fR \- seconds value of the packet timestamp. .IP \(bu 4 \&\f(CW\*(C`tv_usec\*(C'\fR \- microseconds value of the packet timestamp. .RE .RS 4 .Sp \&\fBExample\fR .Sp .Vb 1 \& pcap_loop($pcap, 10, \e&process_packet, "user data"); \& \& sub process_packet { \& my ($user_data, $header, $packet) = @_; \& # ... \& } .Ve .RE .IP \fBpcap_breakloop($pcap)\fR 4 .IX Item "pcap_breakloop($pcap)" Sets a flag that will force \f(CWpcap_dispatch()\fR or \f(CWpcap_loop()\fR to return rather than looping; they will return the number of packets that have been processed so far, or \-2 if no packets have been processed so far. .Sp 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. .Sp Please see the section on \f(CWpcap_breakloop()\fR in \fBpcap\fR\|(3) for more information. .IP \fBpcap_close($pcap)\fR 4 .IX Item "pcap_close($pcap)" Close the packet capture device associated with the descriptor \f(CW$pcap\fR. .ie n .IP "\fBpcap_dispatch($pcap, \fR\fB$count\fR\fB, \e&callback, \fR\fB$user_data\fR\fB)\fR" 4 .el .IP "\fBpcap_dispatch($pcap, \fR\f(CB$count\fR\fB, \e&callback, \fR\f(CB$user_data\fR\fB)\fR" 4 .IX Item "pcap_dispatch($pcap, $count, &callback, $user_data)" Collect \f(CW$count\fR packets and process them with callback function \&\f(CW&callback\fR. if \f(CW$count\fR is \-1, all packets currently buffered are processed. If \f(CW$count\fR is 0, process all packets until an error occurs. .IP "\fBpcap_next($pcap, \e%header)\fR" 4 .IX Item "pcap_next($pcap, %header)" Return the next available packet on the interface associated with packet descriptor \f(CW$pcap\fR. Into the \f(CW%header\fR hash is stored the received packet header. If not packet is available, the return value and header is undefined. .IP "\fBpcap_next_ex($pcap, \e%header, \e$packet)\fR" 4 .IX Item "pcap_next_ex($pcap, %header, $packet)" Reads the next available packet on the interface associated with packet descriptor \f(CW$pcap\fR, stores its header in \f(CW\*(C`\e%header\*(C'\fR and its data in \&\f(CW\*(C`\e$packet\*(C'\fR and returns a success/failure indication: .RS 4 .IP \(bu 4 \&\f(CW1\fR means that the packet was read without problems; .IP \(bu 4 \&\f(CW0\fR means that packets are being read from a live capture, and the timeout expired; .IP \(bu 4 \&\f(CW\-1\fR means that an error occurred while reading the packet; .IP \(bu 4 \&\f(CW\-2\fR packets are being read from a dump file, and there are no more packets to read from the savefile. .RE .RS 4 .RE .ie n .IP "\fBpcap_compile($pcap, \e$filter, \fR\fB$filter_str\fR\fB, \fR\fB$optimize\fR\fB, \fR\fB$netmask\fR\fB)\fR" 4 .el .IP "\fBpcap_compile($pcap, \e$filter, \fR\f(CB$filter_str\fR\fB, \fR\f(CB$optimize\fR\fB, \fR\f(CB$netmask\fR\fB)\fR" 4 .IX Item "pcap_compile($pcap, $filter, $filter_str, $optimize, $netmask)" Compile the filter string contained in \f(CW$filter_str\fR and store it in \&\f(CW$filter\fR. A description of the filter language can be found in the libpcap source code, or the manual page for \fBtcpdump\fR\|(8) . The filter is optimized if the \f(CW$optimize\fR variable is true. The netmask of the network device must be specified in the \f(CW$netmask\fR parameter. The function returns 0 if the compilation was successful, or \-1 if there was a problem. .ie n .IP "\fBpcap_compile_nopcap($snaplen, \fR\fB$linktype\fR\fB, \e$filter, \fR\fB$filter_str\fR\fB, \fR\fB$optimize\fR\fB, \fR\fB$netmask\fR\fB)\fR" 4 .el .IP "\fBpcap_compile_nopcap($snaplen, \fR\f(CB$linktype\fR\fB, \e$filter, \fR\f(CB$filter_str\fR\fB, \fR\f(CB$optimize\fR\fB, \fR\f(CB$netmask\fR\fB)\fR" 4 .IX Item "pcap_compile_nopcap($snaplen, $linktype, $filter, $filter_str, $optimize, $netmask)" Similar to \f(CWcompile()\fR except that instead of passing a \f(CW$pcap\fR descriptor, one passes \f(CW$snaplen\fR and \f(CW$linktype\fR directly. Returns \-1 if there was an error, but the error message is not available. .ie n .IP "\fBpcap_setfilter($pcap, \fR\fB$filter\fR\fB)\fR" 4 .el .IP "\fBpcap_setfilter($pcap, \fR\f(CB$filter\fR\fB)\fR" 4 .IX Item "pcap_setfilter($pcap, $filter)" Associate the compiled filter stored in \f(CW$filter\fR with the packet capture descriptor \f(CW$pcap\fR. .IP \fBpcap_freecode($filter)\fR 4 .IX Item "pcap_freecode($filter)" Used to free the allocated memory used by a compiled filter, as created by \f(CWpcap_compile()\fR. .ie n .IP "\fBpcap_offline_filter($filter, \e%header, \fR\fB$packet\fR\fB)\fR" 4 .el .IP "\fBpcap_offline_filter($filter, \e%header, \fR\f(CB$packet\fR\fB)\fR" 4 .IX Item "pcap_offline_filter($filter, %header, $packet)" Check whether \f(CW$filter\fR matches the packet described by header \f(CW%header\fR and packet data \f(CW$packet\fR. Returns true if the packet matches. .ie n .IP "\fBpcap_setnonblock($pcap, \fR\fB$mode\fR\fB, \e$err)\fR" 4 .el .IP "\fBpcap_setnonblock($pcap, \fR\f(CB$mode\fR\fB, \e$err)\fR" 4 .IX Item "pcap_setnonblock($pcap, $mode, $err)" Set the \fInon-blocking\fR mode of a live capture descriptor, depending on the value of \f(CW$mode\fR (zero to activate and non-zero to deactivate). It has no effect on offline descriptors. If there is an error, it returns \-1 and sets \&\f(CW$err\fR. .Sp In non-blocking mode, an attempt to read from the capture descriptor with \&\f(CWpcap_dispatch()\fR will, if no packets are currently available to be read, return 0 immediately rather than blocking waiting for packets to arrive. \&\f(CWpcap_loop()\fR and \f(CWpcap_next()\fR will not work in non-blocking mode. .IP "\fBpcap_getnonblock($pcap, \e$err)\fR" 4 .IX Item "pcap_getnonblock($pcap, $err)" Returns the \fInon-blocking\fR state of the capture descriptor \f(CW$pcap\fR. Always returns 0 on savefiles. If there is an error, it returns \-1 and sets \f(CW$err\fR. .SS "Savefile commands" .IX Subsection "Savefile commands" .ie n .IP "\fBpcap_dump_open($pcap, \fR\fB$filename\fR\fB)\fR" 4 .el .IP "\fBpcap_dump_open($pcap, \fR\f(CB$filename\fR\fB)\fR" 4 .IX Item "pcap_dump_open($pcap, $filename)" Open a savefile for writing and return a descriptor for doing so. If \&\f(CW$filename\fR is \f(CW"\-"\fR data is written to standard output. On error, the return value is undefined and \f(CWpcap_geterr()\fR can be used to retrieve the error text. .ie n .IP "\fBpcap_dump($dumper, \e%header, \fR\fB$packet\fR\fB)\fR" 4 .el .IP "\fBpcap_dump($dumper, \e%header, \fR\f(CB$packet\fR\fB)\fR" 4 .IX Item "pcap_dump($dumper, %header, $packet)" Dump the packet described by header \f(CW%header\fR and packet data \f(CW$packet\fR to the savefile associated with \f(CW$dumper\fR. The packet header has the same format as that passed to the \f(CWpcap_loop()\fR callback. .Sp \&\fBExample\fR .Sp .Vb 3 \& my $dump_file = \*(Aqnetwork.dmp\*(Aq; \& my $dev = pcap_lookupdev(); \& my $pcap = pcap_open_live($dev, 1024, 1, 0, \e$err); \& \& my $dumper = pcap_dump_open($pcap, $dump_file); \& pcap_loop($pcap, 10, \e&process_packet, \*(Aq\*(Aq); \& pcap_dump_close($dumper); \& \& sub process_packet { \& my ($user_data, $header, $packet) = @_; \& pcap_dump($dumper, $header, $packet); \& } .Ve .IP \fBpcap_dump_file($dumper)\fR 4 .IX Item "pcap_dump_file($dumper)" Returns the filehandle associated with a savefile opened with \&\f(CWpcap_dump_open()\fR. .IP \fBpcap_dump_flush($dumper)\fR 4 .IX Item "pcap_dump_flush($dumper)" Flushes the output buffer to the corresponding save file, so that any packets written with \f(CWpcap_dump()\fR but not yet written to the save file will be written. Returns \-1 on error, 0 on success. .IP \fBpcap_dump_close($dumper)\fR 4 .IX Item "pcap_dump_close($dumper)" Close the savefile associated with the descriptor \f(CW$dumper\fR. .SS "Status functions" .IX Subsection "Status functions" .IP \fBpcap_datalink($pcap)\fR 4 .IX Item "pcap_datalink($pcap)" Returns the link layer type associated with the given pcap descriptor. .Sp \&\fBExample\fR .Sp .Vb 1 \& $linktype = pcap_datalink($pcap); .Ve .ie n .IP "\fBpcap_set_datalink($pcap, \fR\fB$linktype\fR\fB)\fR" 4 .el .IP "\fBpcap_set_datalink($pcap, \fR\f(CB$linktype\fR\fB)\fR" 4 .IX Item "pcap_set_datalink($pcap, $linktype)" Sets the data link type of the given pcap descriptor to the type specified by \f(CW$linktype\fR. Returns \-1 on failure. .IP \fBpcap_datalink_name_to_val($name)\fR 4 .IX Item "pcap_datalink_name_to_val($name)" Translates a data link type name, which is a \f(CW\*(C`DLT_\*(C'\fR name with the \f(CW\*(C`DLT_\*(C'\fR part removed, to the corresponding data link type value. The translation is case-insensitive. Returns \-1 on failure. .Sp \&\fBExample\fR .Sp .Vb 1 \& $linktype = pcap_datalink_name_to_val(\*(AqLTalk\*(Aq); # returns DLT_LTALK .Ve .IP \fBpcap_datalink_val_to_name($linktype)\fR 4 .IX Item "pcap_datalink_val_to_name($linktype)" Translates a data link type value to the corresponding data link type name. .Sp \&\fBExample\fR .Sp .Vb 1 \& $name = pcap_datalink_val_to_name(DLT_LTALK); # returns \*(AqLTALK\*(Aq .Ve .IP \fBpcap_datalink_val_to_description($linktype)\fR 4 .IX Item "pcap_datalink_val_to_description($linktype)" Translates a data link type value to a short description of that data link type. .Sp \&\fBExample\fR .Sp .Vb 1 \& $descr = pcap_datalink_val_to_description(DLT_LTALK); # returns \*(AqLocaltalk\*(Aq .Ve .IP \fBpcap_snapshot($pcap)\fR 4 .IX Item "pcap_snapshot($pcap)" Returns the snapshot length (snaplen) specified in the call to \&\f(CWpcap_open_live()\fR. .IP \fBpcap_is_swapped($pcap)\fR 4 .IX Item "pcap_is_swapped($pcap)" This function returns true if the endianness of the currently open savefile is different from the endianness of the machine. .IP \fBpcap_major_version($pcap)\fR 4 .IX Item "pcap_major_version($pcap)" Return the major version number of the pcap library used to write the currently open savefile. .IP \fBpcap_minor_version($pcap)\fR 4 .IX Item "pcap_minor_version($pcap)" Return the minor version of the pcap library used to write the currently open savefile. .IP "\fBpcap_stats($pcap, \e%stats)\fR" 4 .IX Item "pcap_stats($pcap, %stats)" Returns a hash containing information about the status of packet capture device \f(CW$pcap\fR. The hash contains the following fields. .Sp 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. .RS 4 .IP \(bu 4 \&\f(CW\*(C`ps_recv\*(C'\fR \- the number of packets received by the packet capture software. .IP \(bu 4 \&\f(CW\*(C`ps_drop\*(C'\fR \- the number of packets dropped by the packet capture software. .IP \(bu 4 \&\f(CW\*(C`ps_ifdrop\*(C'\fR \- the number of packets dropped by the network interface. .RE .RS 4 .RE .IP \fBpcap_file($pcap)\fR 4 .IX Item "pcap_file($pcap)" Returns the filehandle associated with a savefile opened with \&\f(CWpcap_open_offline()\fR or \f(CW\*(C`undef\*(C'\fR if the device was opened with \f(CWpcap_open_live()\fR. .IP \fBpcap_fileno($pcap)\fR 4 .IX Item "pcap_fileno($pcap)" Returns the file number of the network device opened with \f(CWpcap_open_live()\fR. .IP \fBpcap_get_selectable_fd($pcap)\fR 4 .IX Item "pcap_get_selectable_fd($pcap)" Returns, on Unix, a file descriptor number for a file descriptor on which one can do a \f(CWselect()\fR or \f(CWpoll()\fR to wait for it to be possible to read packets without blocking, if such a descriptor exists, or \-1, if no such descriptor exists. Some network devices opened with \f(CWpcap_open_live()\fR do not support \f(CWselect()\fR or \f(CWpoll()\fR, so \-1 is returned for those devices. See \fBpcap\fR\|(3) for more details. .SS "Error handling" .IX Subsection "Error handling" .IP \fBpcap_geterr($pcap)\fR 4 .IX Item "pcap_geterr($pcap)" Returns an error message for the last error associated with the packet capture device \f(CW$pcap\fR. .IP \fBpcap_strerror($errno)\fR 4 .IX Item "pcap_strerror($errno)" Returns a string describing error number \f(CW$errno\fR. .ie n .IP "\fBpcap_perror($pcap, \fR\fB$prefix\fR\fB)\fR" 4 .el .IP "\fBpcap_perror($pcap, \fR\f(CB$prefix\fR\fB)\fR" 4 .IX Item "pcap_perror($pcap, $prefix)" Prints the text of the last error associated with descriptor \f(CW$pcap\fR on standard error, prefixed by \f(CW$prefix\fR. .SS Information .IX Subsection "Information" .IP \fBpcap_lib_version()\fR 4 .IX Item "pcap_lib_version()" Returns the name and version of the \f(CW\*(C`pcap\*(C'\fR library the module was linked against. .SS "Perl specific functions" .IX Subsection "Perl specific functions" The following functions are specific to the Perl binding of libpcap. .IP \fBpcap_perl_settings($setting)\fR 4 .IX Item "pcap_perl_settings($setting)" Modify internal behaviour of the Perl interpreter. .RS 4 .IP \(bu 4 \&\f(CW\*(C`PERL_SIGNALS_SAFE\*(C'\fR, \f(CW\*(C`PERL_SIGNALS_UNSAFE\*(C'\fR respectively enable safe or unsafe signals delivery. Returns the previous value of \f(CW\*(C`PL_signals\*(C'\fR. See "Signals handling". .Sp \&\fBExample:\fR .Sp .Vb 2 \& local $SIG{ALRM} = sub { pcap_breakloop() }; \& alarm 60; \& \& pcap_perl_settings(PERL_SIGNALS_UNSAFE); \& pcap_loop($pcap, 10, \e&process_packet, ""); \& pcap_perl_settings(PERL_SIGNALS_SAFE); .Ve .RE .RS 4 .RE .SS "WinPcap specific functions" .IX Subsection "WinPcap specific functions" 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 \&\f(CWcroak()\fR. .ie n .IP "\fBpcap_createsrcstr(\e$source, \fR\fB$type\fR\fB, \fR\fB$host\fR\fB, \fR\fB$port\fR\fB, \fR\fB$name\fR\fB, \e$err)\fR" 4 .el .IP "\fBpcap_createsrcstr(\e$source, \fR\f(CB$type\fR\fB, \fR\f(CB$host\fR\fB, \fR\f(CB$port\fR\fB, \fR\f(CB$name\fR\fB, \e$err)\fR" 4 .IX Item "pcap_createsrcstr($source, $type, $host, $port, $name, $err)" Accepts a set of strings (host name, port, ...), and stores the complete source string according to the new format (e.g. \f(CW"rpcap://1.2.3.4/eth0"\fR) in \f(CW$source\fR. .Sp 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 \fBtcpdump\|(1)\fR, 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 \f(CWpcap_open()\fR function. .Sp Returns 0 if everything is fine, \-1 if some errors occurred. The string containing the complete source is returned in the \f(CW$source\fR variable. .IP "\fBpcap_parsesrcstr($source, \e$type, \e$host, \e$port, \e$name, \e$err)\fR" 4 .IX Item "pcap_parsesrcstr($source, $type, $host, $port, $name, $err)" Parse the source string and stores the pieces in which the source can be split in the corresponding variables. .Sp This call is the other way round of \f(CWpcap_createsrcstr()\fR. It accepts a null-terminated string and it returns the parameters related to the source. This includes: .RS 4 .IP \(bu 4 the type of the source (file, WinPcap on a remote adapter, WinPcap on local adapter), which is determined by the source prefix (\f(CW\*(C`PCAP_SRC_IF_STRING\*(C'\fR and so on); .IP \(bu 4 the host on which the capture has to be started (only for remote captures); .IP \(bu 4 the raw name of the source (file name, name of the remote adapter, name of the local adapter), without the source prefix. The string returned does not include the type of the source itself (i.e. the string returned does not include \f(CW"file://"\fR or \f(CW"rpcap://"\fR or such). .RE .RS 4 .Sp The user can omit some parameters in case it is not interested in them. .Sp 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. .RE .ie n .IP "\fBpcap_open($source, \fR\fB$snaplen\fR\fB, \fR\fB$flags\fR\fB, \fR\fB$read_timeout\fR\fB, \e$auth, \e$err)\fR" 4 .el .IP "\fBpcap_open($source, \fR\f(CB$snaplen\fR\fB, \fR\f(CB$flags\fR\fB, \fR\f(CB$read_timeout\fR\fB, \e$auth, \e$err)\fR" 4 .IX Item "pcap_open($source, $snaplen, $flags, $read_timeout, $auth, $err)" Open a generic source in order to capture / send (WinPcap only) traffic. .Sp The \f(CWpcap_open()\fR replaces all the \f(CWpcap_open_xxx()\fR functions with a single call. .Sp This function hides the differences between the different \f(CWpcap_open_xxx()\fR functions so that the programmer does not have to manage different opening function. In this way, the \fItrue\fR \f(CWopen()\fR function is decided according to the source type, which is included into the source string (in the form of source prefix). .Sp Returns a pointer to a pcap descriptor which can be used as a parameter to the following calls (\f(CWcompile()\fR and so on) and that specifies an opened WinPcap session. In case of problems, it returns \f(CW\*(C`undef\*(C'\fR and the \f(CW$err\fR variable keeps the error message. .ie n .IP "\fBpcap_setbuff($pcap, \fR\fB$dim\fR\fB)\fR" 4 .el .IP "\fBpcap_setbuff($pcap, \fR\f(CB$dim\fR\fB)\fR" 4 .IX Item "pcap_setbuff($pcap, $dim)" Sets the size of the kernel buffer associated with an adapter. \&\f(CW$dim\fR specifies the size of the buffer in bytes. The return value is 0 when the call succeeds, \-1 otherwise. .Sp If an old buffer was already created with a previous call to \&\f(CWsetbuff()\fR, it is deleted and its content is discarded. \&\f(CWopen_live()\fR creates a 1\ MB buffer by default. .ie n .IP "\fBpcap_setmode($pcap, \fR\fB$mode\fR\fB)\fR" 4 .el .IP "\fBpcap_setmode($pcap, \fR\f(CB$mode\fR\fB)\fR" 4 .IX Item "pcap_setmode($pcap, $mode)" Sets the working mode of the interface \f(CW$pcap\fR to \f(CW$mode\fR. Valid values for \f(CW$mode\fR are \f(CW\*(C`MODE_CAPT\*(C'\fR (default capture mode) and \&\f(CW\*(C`MODE_STAT\*(C'\fR (statistical mode). .ie n .IP "\fBpcap_setmintocopy($pcap_t, \fR\fB$size\fR\fB)\fR" 4 .el .IP "\fBpcap_setmintocopy($pcap_t, \fR\f(CB$size\fR\fB)\fR" 4 .IX Item "pcap_setmintocopy($pcap_t, $size)" Changes the minimum amount of data in the kernel buffer that causes a read from the application to return (unless the timeout expires). .IP \fBpcap_getevent($pcap)\fR 4 .IX Item "pcap_getevent($pcap)" Returns the \f(CW\*(C`Win32::Event\*(C'\fR object associated with the interface \&\f(CW$pcap\fR. Can be used to wait until the driver's buffer contains some data without performing a read. See Win32::Event. .ie n .IP "\fBpcap_sendpacket($pcap, \fR\fB$packet\fR\fB)\fR" 4 .el .IP "\fBpcap_sendpacket($pcap, \fR\f(CB$packet\fR\fB)\fR" 4 .IX Item "pcap_sendpacket($pcap, $packet)" Send a raw packet to the network. \f(CW$pcap\fR is the interface that will be used to send the packet, \f(CW$packet\fR contains the data of the packet to send (including the various protocol headers). The MAC CRC doesn't need to be included, because it is transparently calculated and added by the network interface driver. The return value is 0 if the packet is successfully sent, \&\-1 otherwise. .IP \fBpcap_sendqueue_alloc($memsize)\fR 4 .IX Item "pcap_sendqueue_alloc($memsize)" This function allocates and returns a send queue, i.e. a buffer containing a set of raw packets that will be transmitted on the network with \&\f(CWsendqueue_transmit()\fR. .Sp \&\f(CW$memsize\fR 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. .ie n .IP "\fBpcap_sendqueue_queue($queue, \e%header, \fR\fB$packet\fR\fB)\fR" 4 .el .IP "\fBpcap_sendqueue_queue($queue, \e%header, \fR\f(CB$packet\fR\fB)\fR" 4 .IX Item "pcap_sendqueue_queue($queue, %header, $packet)" Adds a packet at the end of the send queue pointed by \f(CW$queue\fR. The packet header \f(CW%header\fR has the same format as that passed to the \f(CWloop()\fR callback. \f(CW$ackekt\fR is a buffer with the data of the packet. .Sp The \f(CW%headerr\fR 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 \&\fIas is\fR. The CRC of the packets needs not to be calculated, because it will be transparently added by the network interface. .ie n .IP "\fBpcap_sendqueue_transmit($pcap, \fR\fB$queue\fR\fB, \fR\fB$sync\fR\fB)\fR" 4 .el .IP "\fBpcap_sendqueue_transmit($pcap, \fR\f(CB$queue\fR\fB, \fR\f(CB$sync\fR\fB)\fR" 4 .IX Item "pcap_sendqueue_transmit($pcap, $queue, $sync)" This function transmits the content of a queue to the wire. \f(CW$pcapt\fR is the interface on which the packets will be sent, \f(CW$queue\fR is to a \&\f(CW\*(C`send_queue\*(C'\fR containing the packets to send, \f(CW$sync\fR determines if the send operation must be synchronized: if it is non-zero, the packets are sent respecting the timestamps, otherwise they are sent as fast as possible. .Sp 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. .SH CONSTANTS .IX Header "CONSTANTS" \&\f(CW\*(C`Net::Pcap\*(C'\fR 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. .PP Here are the descriptions of a few data link types. See \fBpcap\fR\|(3) for a more complete description and semantics associated with each data link. .IP \(bu 4 \&\f(CW\*(C`DLT_NULL\*(C'\fR \- BSD loopback encapsulation .IP \(bu 4 \&\f(CW\*(C`DLT_EN10MB\*(C'\fR \- Ethernet (10Mb, 100Mb, 1000Mb, and up) .IP \(bu 4 \&\f(CW\*(C`DLT_RAW\*(C'\fR \- raw IP .IP \(bu 4 \&\f(CW\*(C`DLT_IEEE802\*(C'\fR \- IEEE 802.5 Token Ring .IP \(bu 4 \&\f(CW\*(C`DLT_IEEE802_11\*(C'\fR \- IEEE 802.11 wireless LAN .IP \(bu 4 \&\f(CW\*(C`DLT_FRELAY\*(C'\fR \- Frame Relay .IP \(bu 4 \&\f(CW\*(C`DLT_FDDI\*(C'\fR \- FDDI .IP \(bu 4 \&\f(CW\*(C`DLT_SLIP\*(C'\fR \- Serial Line IP .IP \(bu 4 \&\f(CW\*(C`DLT_PPP\*(C'\fR \- PPP (Point-to-point Protocol) .IP \(bu 4 \&\f(CW\*(C`DLT_PPP_SERIAL\*(C'\fR \- PPP over serial with HDLC encapsulation .IP \(bu 4 \&\f(CW\*(C`DLT_PPP_ETHER\*(C'\fR \- PPP over Ethernet .IP \(bu 4 \&\f(CW\*(C`DLT_IP_OVER_FC\*(C'\fR \- RFC 2625 IP-over-Fibre Channel .IP \(bu 4 \&\f(CW\*(C`DLT_AX25\*(C'\fR \- Amateur Radio AX.25 .IP \(bu 4 \&\f(CW\*(C`DLT_LINUX_IRDA\*(C'\fR \- Linux-IrDA .IP \(bu 4 \&\f(CW\*(C`DLT_LTALK\*(C'\fR \- Apple LocalTalk .IP \(bu 4 \&\f(CW\*(C`DLT_APPLE_IP_OVER_IEEE1394\*(C'\fR \- Apple IP-over-IEEE 1394 (a.k.a. Firewire) .SH DIAGNOSTICS .IX Header "DIAGNOSTICS" .ie n .IP """arg%d not a scalar ref""" 4 .el .IP "\f(CWarg%d not a scalar ref\fR" 4 .IX Item "arg%d not a scalar ref" .PD 0 .ie n .IP """arg%d not a hash ref""" 4 .el .IP "\f(CWarg%d not a hash ref\fR" 4 .IX Item "arg%d not a hash ref" .ie n .IP """arg%d not a reference""" 4 .el .IP "\f(CWarg%d not a reference\fR" 4 .IX Item "arg%d not a reference" .PD \&\fB(F)\fR These errors occur if you forgot to give a reference to a function which expect one or more of its arguments to be references. .SH BUGS .IX Header "BUGS" Please report any bugs or feature requests to \&\f(CW\*(C`bug\-Net\-Pcap@rt.cpan.org\*(C'\fR, or through the web interface at . I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. .PP Currently known bugs: .IP \(bu 4 the \f(CW\*(C`ps_recv\*(C'\fR field is not correctly set; see \fIt/07\-stats.t\fR .IP \(bu 4 \&\f(CWpcap_file()\fR seems to always returns \f(CW\*(C`undef\*(C'\fR for live connection and causes segmentation fault for dump files; see \fIt/10\-fileno.t\fR .IP \(bu 4 \&\f(CWpcap_fileno()\fR is documented to return \-1 when called on save file, but seems to always return an actual file number. See \fIt/10\-fileno.t\fR .IP \(bu 4 \&\f(CWpcap_dump_file()\fR seems to corrupt something somewhere, and makes scripts dump core. See \fIt/05\-dump.t\fR .SH EXAMPLES .IX Header "EXAMPLES" See the \fIeg/\fR and \fIt/\fR directories of the \f(CW\*(C`Net::Pcap\*(C'\fR distribution for examples on using this module. .SH "SEE ALSO" .IX Header "SEE ALSO" .SS "Perl Modules" .IX Subsection "Perl Modules" the NetPacket or Net::Frame modules to assemble and disassemble packets. .PP Net::Pcap::Reassemble for reassembly of TCP/IP fragments. .PP POE::Component::Pcap for using \f(CW\*(C`Net::Pcap\*(C'\fR within POE-based programs. .PP AnyEvent::Pcap for using \f(CW\*(C`Net::Pcap\*(C'\fR within AnyEvent-based programs. .PP Net::Packet or NetPacket for decoding and creating network packets. .PP Net::Pcap::Easy is a module which provides an easier, more Perl-ish API than \f(CW\*(C`Net::Pcap\*(C'\fR and integrates some facilities from Net::Netmask and NetPacket. .SS "Base Libraries" .IX Subsection "Base Libraries" \&\fBpcap\fR\|(3), \fBtcpdump\fR\|(8) .PP The source code for the \f(CWpcap(3)\fR library is available from .PP The source code and binary for the Win32 version of the pcap library, WinPcap, is available from .SS Articles .IX Subsection "Articles" \&\fIHacking Linux Exposed: Sniffing with Net::Pcap to stealthily managing iptables rules remotely\fR, .PP \&\fIPerlMonks node about Net::Pcap\fR, .SH AUTHORS .IX Header "AUTHORS" Current maintainer is Sébastien Aperghis-Tramoni (SAPER) with the help of Tim Wilde (TWILDE). .PP Complete list of authors & contributors: .IP \(bu 4 Bo Adler (BOADLER) .IP \(bu 4 Craig Davison .IP \(bu 4 David Farrell .IP \(bu 4 David N. Blank-Edelman .IP \(bu 4 James Rouzier (ROUZIER) .IP \(bu 4 Jean-Louis Morel (JLMOREL) .IP \(bu 4 Marco Carnut (KCARNUT) .IP \(bu 4 Patrice Auffret (GOMOR) .IP \(bu 4 Peter Lister (PLISTER) .IP \(bu 4 Rafaël Garcia-Suarez (RGARCIA) .IP \(bu 4 Sébastien Aperghis-Tramoni (SAPER) .IP \(bu 4 Tim Potter (TIMPOTTER) .IP \(bu 4 Tim Wilde (TWILDE) .SH HISTORY .IX Header "HISTORY" The original version of \f(CW\*(C`Net::Pcap\*(C'\fR, version 0.01, was written by Peter Lister using SWIG. .PP Version 0.02 was created by Bo Adler with a few bugfixes but not uploaded to CPAN. It could be found at: .PP Versions 0.03 and 0.04 were created by Tim Potter who entirely rewrote \f(CW\*(C`Net::Pcap\*(C'\fR using XS and wrote the documentation, with the help of David N. Blank-Edelman for testing and general polishing. .PP Version 0.05 was released by Marco Carnut with fixes to make it work with Cygwin and WinPcap. .PP Version 0.04.02 was independently created by Jean-Louis Morel but not uploaded on the CPAN. It can be found here: .PP 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. .SH ACKNOWLEDGEMENTS .IX Header "ACKNOWLEDGEMENTS" 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. .PP To the beta-testers: Jean-Louis Morel, Max Maischen, Philippe Bruhat, David Morel, Scott Lanning, Rafael Garcia-Suarez, Karl Y. Pradene. .SH "COPYRIGHT & LICENSE" .IX Header "COPYRIGHT & LICENSE" Copyright (C) 2005\-2016 Sébastien Aperghis-Tramoni and contributors. All rights reserved. .PP Copyright (C) 2003 Marco Carnut. All rights reserved. .PP Copyright (C) 1999, 2000 Tim Potter. All rights reserved. .PP Copyright (C) 1998 Bo Adler. All rights reserved. .PP Copyright (C) 1997 Peter Lister. All rights reserved. .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.