New libpcap 1.0.0 API calls example

Pcap.openLivePcap.openOfflinePcap.closepcap_open_livepcap_open_offlinepcap_closepcap_createpcap_activatepcap_createpcap_activatePcapPcapPcap.isPcap100Loadedpcap_createpcap_activatePcap.createPcap.activatePcap.isPcap100LoadedUnsatisfiedLinkExceptionPcap.openLive
Pcap pcap;
if (Pcap.isPcap100Loaded()) {
	pcap = Pcap.create(deviceName, errbuf);
	// Rest of the logic goes here
	pcap.activate();
} else {
	pcap = Pcap.openLive(deviceName, snaplen, timeout, flags, errbuf);
}

try {
	// Our main application logic goes here
} finally {
	pcap.close();
}
Pcap.isPcap100LoadedPcap.createPcap.activate
public class LibpcapAPIVersionsExample {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		List alldevs = new ArrayList(); // Will be filled with NICs
		StringBuilder errbuf = new StringBuilder(); // For any error msgs

		/***************************************************************************
		 * First get a list of devices on this system
		 **************************************************************************/
		int r = Pcap.findAllDevs(alldevs, errbuf);
		if (r != Pcap.OK || alldevs.isEmpty()) {
			System.err.printf("Can't read list of devices, error is %s",
					errbuf.toString());
			return;
		}

		System.out.println("Network devices found:");

		int i = 0;
		for (PcapIf device : alldevs) {
			String description =
					(device.getDescription() != null) ? device.getDescription()
							: "No description available";
			System.out.printf("#%d: %s [%s]\n", i++, device.getName(), description);
		}

		PcapIf device = alldevs.get(0); // We know we have atleast 1 device
		System.out
				.printf("\nChoosing '%s' on your behalf:\n",
						(device.getDescription() != null) ? device.getDescription()
								: device.getName());

		/***************************************************************************
		 * Second we open up the selected device
		 **************************************************************************/
		int snaplen = 64 * 1024; // Capture all packets, no trucation
		int flags = Pcap.MODE_PROMISCUOUS; // capture all packets
		int timeout = 10 * 1000; // 10 seconds in millis

		/*
		 * Display a little message so that we know which API we are using in this
		 * example
		 */
		System.out.printf("Is 'pcap' library API 1.0.0 or above loaded? %s%n",
				Pcap.isPcap100Loaded());

		/*
		 * Now lets open a 'pcap' handle to a live network interface and start
		 * capturing, no matter which API version is available to us.
		 */
		Pcap pcap;
		if (Pcap.isPcap100Loaded()) {
			pcap = Pcap.create(device.getName(), errbuf);
			if (pcap == null) {
				System.err.printf("Error while opening device for capture: "
						+ errbuf.toString());
				return;
			}

			/* Set our standard properties */
			pcap.setSnaplen(snaplen);
			pcap.setPromisc(flags);
			pcap.setTimeout(timeout);

			/* Here are some new ones */
			pcap.setDirection(Direction.INOUT); // We now have IN, OUT or INOUT
			pcap.setBufferSize(128 * 1024 * 1024); // Set ring-buffer to 128Mb

			pcap.activate(); // Make our handle active and start capturing

		} else {
			pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);

			if (pcap == null) {
				System.err.printf("Error while opening device for capture: "
						+ errbuf.toString());
				return;
			}
		}

		/*
		 * The rest of the example is normal and we correctly handled different
		 * versions of 'pcap' library API available to us at runtime, on any
		 * platform.
		 */

		/***************************************************************************
		 * Third we create a packet handler which will receive packets from the
		 * libpcap loop.
		 **************************************************************************/
		PcapPacketHandler jpacketHandler = new PcapPacketHandler() {

			public void nextPacket(PcapPacket packet, String user) {

				System.out.printf("Received packet at %s caplen=%-4d len=%-4d %s\n",
						new Date(packet.getCaptureHeader().timestampInMillis()),
						packet.getCaptureHeader().caplen(), // Length actually captured
						packet.getCaptureHeader().wirelen(), // Original length
						user // User supplied object
						);
			}
		};

		try {
			/*************************************************************************
			 * Fourth we enter the loop and tell it to capture 10 packets. The loop
			 * method does a mapping of pcap.datalink() DLT value to JProtocol ID,
			 * which is needed by JScanner. The scanner scans the packet buffer and
			 * decodes the headers. The mapping is done automatically, although a
			 * variation on the loop method exists that allows the programmer to
			 * specify exactly which protocol ID to use as the data link type for this
			 * pcap interface.
			 ************************************************************************/
			pcap.loop(10, jpacketHandler, "jNetPcap rocks!");

			/*************************************************************************
			 * Last thing to do is close the pcap handle
			 ************************************************************************/
		} finally {
			pcap.close();
		}
	}
}