Keywords: pcap, packet, capture, python

# Part I. Pcapy Reference

### CORE SECURITY TECHNOLOGIES

Copyright © 2016-2018 CORE SECURITY TECHNOLOGIES

Revision History
Revision Revision: 16 | Date: 2018-06-04 | Author: jkohen
2018 updated

# Pcapy Module Reference

## Name

open_live — Obtain a packet capture descriptor to look at packets on the network

## Synopsis

`Reader open_live (` | device, |
| snaplen, |
| promisc, |
| to_ms`)`; |

`string device` ;
`int snaplen` ;
`int promisc` ;
`int to_ms` ;

## DESCRIPTION

`open_live` is used to obtain a packet capture descriptor to look at packets on the network. `device` is a string that specifies the network device to open; on Linux systems with 2.2 or later kernels, a device argument of `any` or `NULL` can be used to capture packets from all interfaces. `snaplen` specifies the maximum number of bytes to capture. `promisc` specifies if the interface is to be put into promiscuous mode. (Note that even if this parameter is false, the interface could well be in promiscuous mode for some other reason.) For now, this doesn't work on the `any` device; if an argument of `any` or `NULL` is supplied, the `promisc` flag is ignored. `to_ms` specifies the read timeout in milliseconds. The read timeout is used to arrange that the read not necessarily return immediately when a packet is seen, but that it wait for some amount of time to allow more packets to arrive and to read multiple packets from the OS kernel in one operation. Not all platforms support a read timeout; on platforms that don't, the read timeout is ignored.

## Name

open_offline — Obtain a packet capture descriptor to look at packets on a savefile

## Synopsis

`Reader open_offline (` | filename`)`; |

`string filename` ;

## DESCRIPTION

`open_offline` is called to open a savefile for reading. `filename` specifies the name of the file to open. The file has the same format as those used by tcpdump(8) and tcpslice(8). The name `-` is a synonym for `stdin`.

## Name

lookupdev — Return a network device suitable for use with `open_live`

## Synopsis

`string lookupdev (` | `)`; |

## DESCRIPTION

`lookupdev` returns the name of a network device suitable for use with `open_live`.

## Name

findalldevs — Obtain the list of available network devices

## Synopsis

`string[] findalldevs (` | `)`; |

## DESCRIPTION

`findalldevs` constructs a list of network devices that can be opened with `open_live`. (Note that there may be network devices that cannot be opened with `open_live`, because, for example, that process might not have sufficient privileges to open them for capturing; if so, those devices will not appear on the list.)

## Name

compile — Compile a BPF filter

## Synopsis

`Bpf compile (` | linktype, |
| snaplen, |
| filter, |
| optimize, |
| netmask`)`; |

`int linktype` ;
`int snaplen` ;
`string filter` ;
`int optimize` ;
`int32 netmask` ;

## DESCRIPTION

`compile` is used to compile the `filter` into a filter program. `snaplen` specifies the maximum number of bytes to capture. `optimize` controls whether optimization on the resulting code is performed. `netmask` specifies the netmask of the local network.

## Name

create — Creates a non-activated packet capture handle to look at packets on the network

## Synopsis

`Reader create (` | device`)`; |

`string device` ;

## DESCRIPTION

`create` is used to create a packet capture handle to look at packets on the network. The returned handle must be activated with `activate()` before packets can be captured with it; options for the capture, such as promiscuous mode, can be set on the handle before activating it.

# Reader Object Reference

## Name

dispatch, loop — Collect and process packets

## Synopsis

`int dispatch (` | maxcant, |
| (* callback)`)`; |

`int maxcant` ;
`void (* callback) `(`Pkthdr, string`)`` ;

`int loop (` | maxcant, |
| (* callback)`)`; |

`int maxcant` ;
`void (* callback) `(`Pkthdr, string`)`` ;

## DESCRIPTION

`dispatch` is used to collect and process packets. `maxcant` specifies the maximum number of packets to process before returning. This is not a minimum number; when reading a live capture, only one bufferful of packets is read at a time, so fewer than `maxcant` packets may be processed. A `cnt` of `-1` processes all the packets received in one buffer when reading a live capture, or all the packets in the file when reading a savefile. `callback` specifies a routine to be called with two arguments: a `Pkthdr` instance describing the data passed and the data itself.

The number of packets read is returned. 0 is returned if no packets were read from a live capture (if, for example, they were discarded because they didn't pass the packet filter, or if, on platforms that support a read timeout that starts before any packets arrive, the timeout expires before any packets arrive, or if the file descriptor for the capture device is in non-blocking mode and no packets were available to be read) or if no more packets are available in a savefile.

### Note

When reading a live capture, `dispatch` will not necessarily return when the read times out; on some platforms, the read timeout isn't supported, and, on other platforms, the timer doesn't start until at least one packet arrives. This means that the read timeout should not be used in, for example, an interactive application, to allow the packet capture loop to poll for user input periodically, as there's no guarantee that `dispatch` will return after the timeout expires.

`loop` is similar to `dispatch` except it keeps reading packets until `maxcant` packets are processed or an error occurs. It does not return when live read timeouts occur. Rather, specifying a non-zero read timeout to `open_live` and then calling `dispatch` allows the reception and processing of any packets that arrive when the timeout occurs. A negative `maxcant` causes `loop` to loop forever (or at least until an error occurs). 0 is returned if `maxcant` is exhausted.

## Name

next — Collect the next packet

## Synopsis

`(Pkthdr, string) next (` | `)`; |

## DESCRIPTION

`next` reads the next packet (by calling `dispatch` with a `maxcant` of `1`) and returns a tuple (header, data) where `header` is a `Pkthdr` instance describing the data passed and `data` is the data itself.

## Name

stats — get capture statistics

## Synopsis

`(int32, int32, int32) stats (` | `)`; |

## DESCRIPTION

`stats` returns statistics on the current capture as a tuple (recv, drop, ifdrop)

## Name

setfilter — Specify a filter

## Synopsis

`setfilter (` | filter`)`; |

`string filter` ;

## DESCRIPTION

`setfilter` is used to specify a filter for this object.

## Name

getfd — get a file descriptor on which a select() can be done for a live capture

## Synopsis

`int getfd (` | filter`)`; |

`string filter` ;

## DESCRIPTION

`getfd` returns, on UNIX, a file descriptor number for a file descriptor on which one can do a select(), poll(), epoll_wait(), kevent(), or other such call to wait for it to be possible to read packets without blocking, if such a descriptor exists, or -1, if no such descriptor exists.

## Name

set_snaplen — Set the snapshot length for a not-yet-activated capture handle

## Synopsis

`int set_snaplen (` | snaplen`)`; |

`int snaplen` ;

## DESCRIPTION

`set_snaplen` sets the snapshot length to be used on a capture handle when the handle is activated to snaplen. `set_snaplen` returns 0 on success or PCAP_ERROR_ACTIVATED if called on a capture handle that has been activated.

## Name

set_promisc — Set promiscuous mode for a not-yet-activated capture handle

## Synopsis

`int set_promisc (` | promisc`)`; |

`int promisc` ;

## DESCRIPTION

`set_promisc` sets whether promiscuous mode should be set on a capture handle when the handle is activated. If promisc is non-zero, promiscuous mode will be set, otherwise it will not be set. `set_promisc` returns 0 on success or PCAP_ERROR_ACTIVATED if called on a capture handle that has been activated.

## Name

set_timeout — Set the read timeout for a not-yet-activated capture handle

## Synopsis

`int set_timeout (` | timeout`)`; |

`int timeout` ;

## DESCRIPTION

`set_timeout` sets the read timeout that will be used on a capture handle when the handle is activated to to_ms, which is in units of milliseconds. `set_timeout` returns 0 on success or PCAP_ERROR_ACTIVATED if called on a capture handle that has been activated.

## Name

set_buffer_size — Set the buffer size for a not-yet-activated capture handle capture handle

## Synopsis

`int set_buffer_size (` | buffer_size`)`; |

`int buffer_size` ;

## DESCRIPTION

`set_buffer_size` sets the buffer size that will be used on a capture handle when the handle is activated to buffer_size, which is in units of bytes. `set_buffer_size` returns 0 on success or PCAP_ERROR_ACTIVATED if called on a capture handle that has been activated.

## Name

activate — Activate a capture handle

## Synopsis

`int activate (` | `)`; |

## DESCRIPTION

`activate` is used to activate a packet capture handle to look at packets on the network, with the options that were set on the handle being in effect. `activate` returns 0 on success without warnings, a non-zero positive value on success with warnings, and a negative value on error. A non-zero return value indicates what warning or error condition occurred. has been activated. See https://www.tcpdump.org/manpages/pcap_activate.3pcap.html for all possible return values.

## Name

getnet, getmask — Get the associated network number and mask

## Synopsis

`int32 getnet (` | `)`; |

`int32 getmask (` | `)`; |

## DESCRIPTION

`getnet` and `getmask` are used to determine the network number and mask associated with the network device attached to this `Reader`.

## Name

datalink — Obtain the link layer type

## Synopsis

`int datalink (` | `)`; |

## DESCRIPTION

`datalink` returns the link layer type; link layer types it can return include:

`DLT_NULL`

  BSD loopback encapsulation; the link layer header is a 4-byte field, in host byte order, containing a `PF_` value from `socket.h` for the network-layer protocol of the packet.

  ### Note

  “host byte order” is the byte order of the machine on which the packets are captured, and the `PF_` values are for the OS of the machine on which the packets are captured; if a live capture is being done, “host byte order” is the byte order of the machine capturing the packets, and the `PF_` values are those of the OS of the machine capturing the packets, but if a savefile is being read, the byte order and `PF_` values are not necessarily those of the machine reading the capture file.

`DLT_EN10MB`
  Ethernet (10Mb, 100Mb, 1000Mb, and up)
`DLT_IEEE802`
  IEEE 802.5 Token Ring
`DLT_ARCNET`
  ARCNET
`DLT_SLIP`

  SLIP; the link layer header contains, in order:

  - a 1-byte flag, which is `0` for packets received by the machine and `1` for packets sent by the machine.

  - a 1-byte field, the upper 4 bits of which indicate the type of packet, as per RFC 1144:

    - `0x40`; an unmodified IP datagram (`TYPE_IP`)
    - `0x70`; an uncompressed-TCP/IP datagram (`UNCOMPRESSED_TCP`), with that byte being the first byte of the raw IP header on the wire, containing the connection number in the protocol field
    - `0x80`; a compressed-TCP/IP datagram (`COMPRESSED_TCP`), with that byte being the first byte of the compressed TCP/IP datagram header

  - for `UNCOMPRESSED_TCP`, the rest of the modified IP header, and for `COMPRESSED_TCP`, the compressed TCP/IP datagram header

  for a total of 16 bytes; the uncompressed IP datagram follows the header.

`DLT_PPP`
  PPP; if the first 2 bytes are `0xff` and `0x03`, it's PPP in HDLC-like framing, with the PPP header following those two bytes, otherwise it's PPP without framing, and the packet begins with the PPP header.
`DLT_FDDI`
  FDDI
`DLT_ATM_RFC1483`
  RFC 1483 LLC/SNAP-encapsulated ATM; the packet begins with an IEEE 802.2 LLC header.
`DLT_RAW`
  Raw IP; the packet begins with an IP header.
`DLT_PPP_SERIAL`
  PPP in HDLC-like framing, as per RFC 1662, or Cisco PPP with HDLC framing, as per section 4.3.1 of RFC 1547; the first byte will be `0xFF` for PPP in HDLC-like framing, and will be `0x0F` or `0x8F` for Cisco PPP with HDLC framing.
`DLT_PPP_ETHER`
  PPPoE; the packet begins with a PPPoE header, as per RFC 2516.
`DLT_C_HDLC`
  Cisco PPP with HDLC framing, as per section 4.3.1 of RFC 1547.
`DLT_IEEE802_11`
  IEEE 802.11 wireless LAN.
`DLT_LOOP`

  OpenBSD loopback encapsulation; the link layer header is a 4-byte field, in network byte order, containing a `PF_` value from OpenBSD's `socket.h` for the network-layer protocol of the packet.

  ### Note

  Note that, if a savefile is being read, those `PF_` values are not necessarily those of the machine reading the capture file.

`DLT_LINUX_SLL`

  Linux cooked capture encapsulation; the link layer header contains, in order:

  - a 2-byte "packet type", in network byte order, which is one of:

    - `0`; packet was sent to us by somebody else.
    - `1`; packet was broadcast by somebody else.
    - `2`; packet was multicast, but not broadcast, by somebody else.
    - `3`; packet was sent by somebody else to somebody else.
    - `4`; packet was sent by us.

  - a 2-byte field, in network byte order, containing a Linux `ARPHRD_` value for the link layer device type.
  - a 2-byte field, in network byte order, containing the length of the link layer address of the sender of the packet (which could be 0).
  - an 8-byte field containing that number of bytes of the link layer header (if there are more than 8 bytes, only the first 8 are present).
  - a 2-byte field containing an Ethernet protocol type, in network byte order, or containing `1` for Novell 802.3 frames without an 802.2 LLC header or `4` for frames beginning with an 802.2 LLC header.

`DLT_LTALK`
  Apple LocalTalk; the packet begins with an AppleTalk LLAP header.

## Name

getnonblock / setnonblock — Manipulate the non-blocking flag

## Synopsis

`int getnonblock (` | `)`; |

`setnonblock (` | state`)`; |

`int state` ;

## DESCRIPTION

`getnonblock` returns the current non-blocking state of the capture descriptor; it always returns 0 on savefiles.

## DESCRIPTION

`setnonblock` puts a capture descriptor, opened with `open_live`, into non-blocking mode, or takes it out of non-blocking mode, depending on whether the `state` argument is non-zero or zero. It has no effect on savefiles. In non-blocking mode, an attempt to read from the capture descriptor with `dispatch` will, if no packets are currently available to be read, return 0 immediately rather than blocking waiting for packets to arrive. `loop` and `next` will not work in non-blocking mode.

## Name

dump_open — Create a Dumper object

## Synopsis

`Dumper dump_open (` | filename`)`; |

`string filename` ;

## DESCRIPTION

`dump_open` is called to open a savefile for writing and associate it to a newly created `Dumper` instance. The name `-` is a synonym for `stdout`. `filename` specifies the name of the file to open.

## Name

close — Close a Reader

## Synopsis

`Reader close (` | `)`; |

## DESCRIPTION

`close` closes a `Reader` using pcap_close.

# Dumper Object Reference

## Name

dump — Dump a packet to a savefile

## Synopsis

`dump (` | header, |
| data`)`; |

`Pkthdr header` ;
`string data` ;

## DESCRIPTION

`dump` outputs a packet to the savefile opened with `dump_open` from type `Reader`.

## Name

close — Close a Dumper

## Synopsis

`Dumper close (` | `)`; |

## DESCRIPTION

`close` closes a `Dumper`.

# Pkthdr Object Reference

## Name

getts, getcaplen, getlen — Obtain packet header information

## Synopsis

`(long, long) getts (` | `)`; |

`long getcaplen (` | `)`; |

`long getlen (` | `)`; |

## DESCRIPTION

`getts`, `getcaplen` and `getlen` return the timestamp, capture length and total length fields of the packet header, respectively.

Timestamp is a tuple with two elements: the number of seconds since the Epoch, and the amount of microseconds past the current second. The capture length is the number of bytes of the packet that are available from the capture. Finally, total length gives the length of the packet, in bytes (which might be more than the number of bytes available from the capture, if the length of the packet is larger than the maximum number of bytes to capture).

# Bpf Object Reference

## Name

filter — Test a packet against a compiled filter

## Synopsis

`int filter (` | packet`)`; |

`string packet` ;

## DESCRIPTION

`filter` tests a packet against a compiled filter as returned by `pcapy`'s `compile`. If the packet is allowed to pass through -1 is returned, otherwise `filter` returns 0.

## Bibliography

### Sources

Portions of this work based on pcap(3) by the Lawrence Berkeley National Laboratory, University of California, Berkeley, CA. .