pcap100 api example error

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

Hi,

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.setSnaplen(snaplen);
pcap.setPromisc(promisc ? 1 : 0);
pcap.setTimeout(timeoutMs);

// specific to libpcap 1.0.0
if (dir != null) {
pcap.setDirection(dir);
}
if (bufsizeBytes != null) {
pcap.setBufferSize(bufsizeBytes);
}

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

return pcap;
}

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

Ahh,
Thanks for pointing that out.

Sly Technologies, Inc.
http://slytechs.com

Comment viewing options

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