pcap100 api example error

1 reply [Last post]
Joined: 01/10/2011


There is a bug in PCAP 1.0.0 API example http://jnetpcap.com/examples/pcap100. The thing is Pcap.create() never fails. It is pcap.activate() which can fail. The following function can be used to open PCAP handle and catch errors:

* This function opens a live capture PCAP handle. It requires libpcap 1.0.0 API
* and will throw an exception if it is not available.
* @param ifaceName Name of the interface to open
* @param snaplen Packet capture buffer size. Set to (64*1024) to capture packet in full.
* @param promisc Enable promiscuous mode
* @param timeoutMs Read timeout (affects nextEx-like functions, does not affect loop/dispatch)
* @param dir Direction, optional
* @param bufsizeBytes Ring buffer size, optional
* @return
* @throws PcapException PcapException, with error attribute set to the result code of Pcap.activate().
static Pcap openPcap(String ifaceName,
int snaplen, boolean promisc, int timeoutMs,
Pcap.Direction dir, Integer bufsizeBytes) throws PcapException {
Pcap pcap;
StringBuilder errbuf = new StringBuilder();

if (!Pcap.isPcap100Loaded()) {
throw new PcapException(-1, "libpcap1.0.0 or higher required");

pcap = Pcap.create(ifaceName, errbuf);

// standard properties
pcap.setPromisc(promisc ? 1 : 0);

// specific to libpcap 1.0.0
if (dir != null) {
if (bufsizeBytes != null) {

int res = pcap.activate();
if (res != Innocent {
throw new PcapException(res, "Failed to activate PCAP handle: " + pcap.getErr());

return pcap;

Mark Bednarczyk
Joined: 03/22/2008
Ahh, Thanks for pointing

Thanks for pointing that out.

Sly Technologies, Inc.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.