jNetPcap 1.X

jNetPcap 1.X module: libpcap and packet decoder

MAC Address

Get interface's hardware address (MAC address)

This example gets a list of interfaces then iterates through the list. For each interface it acquires MAC address, formats it and prints it out. The example skips any interfaces that do not have a MAC address assigned such as loop interfaces non datalink interfaces.

Source code: GetInterfaceHardwareAddress.java

package org.jnetpcap.examples;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jnetpcap.Pcap;
import org.jnetpcap.PcapIf;

public class GetInterfaceHardwareAddress {

  public static void main(String[] args) throws IOException {

    List<PcapIf> alldevs = new ArrayList<PcapIf>(); // 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.NOT_OK || alldevs.isEmpty()) {
      System.err.printf("Can't read list of devices, error is %s", errbuf
        .toString());
      return;
    }

    /***************************************************************************
     * Second iterate through all the interface and get the HW addresses
     **************************************************************************/
    for (final PcapIf i : alldevs) {
      final byte[] mac = i.getHardwareAddress();
      if (mac == null) {
        continue; // Interface doesn't have a hardware address
      }
      System.out.printf("%s=%s\n", i.getName(), asString(mac));
    }
  }

  private static String asString(final byte[] mac) {

Send Queue

WinPcap Send Queue Transmit

Download source from SVN: Send Queue Transmit on Win32 platform Example

package org.jnetpcap.examples;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.jnetpcap.Pcap;
import org.jnetpcap.PcapIf;
import org.jnetpcap.PcapPktHdr;
import org.jnetpcap.winpcap.WinPcap;
import org.jnetpcap.winpcap.WinPcapSendQueue;

public class WinPcapSendQueueTransmitExample {
  public static void main(String[] args) {
    List<PcapIf> alldevs = new ArrayList<PcapIf>(); // 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.NOT_OK || alldevs.isEmpty()) {
      System.err.printf("Can't read list of devices, error is %s", errbuf.toString());
      return;
    }
    PcapIf device = alldevs.get(0); // We know we have atleast 1 device

    /***************************************************************************
     * Second we open a network interface
     **************************************************************************/
    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
    WinPcap pcap = WinPcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);

    /***************************************************************************
     * Third we create our crude packet queue we will transmit out 

Send Packet

This example sends a packet using a network interface, one at a time. The packet is bogus, but none the less it will be sent as it uses the data link layer, a low level layer that expects the programmer to create the entire packets from scratch including the data link layer (i.e. the ethernet header.)

Download source from SVN: Packet Send Example

package org.jnetpcap.examples;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.jnetpcap.Pcap;
import org.jnetpcap.PcapIf;

public class PcapSendPacketExample {
  public static void main(String[] args) {
    List<PcapIf> alldevs = new ArrayList<PcapIf>(); // 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.NOT_OK || alldevs.isEmpty()) {
      System.err.printf("Can't read list of devices, error is %s", errbuf.toString());
      return;
    }
    PcapIf device = alldevs.get(0); // We know we have atleast 1 device

    /*****************************************
     * Second we open a network interface
     *****************************************/
    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
    Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);


    /*******************************************************
     * Third we create our crude packet we will transmit out

Pcap Dumper

The pcap dumper example expands on the classic example and creates a PcapDumper object which facilitates copying of captured packets to a file. This is exactly how packet dumping is done with native libpcap library as well.

Download source from SVN:

Classic Example

This example is the classic libpcap example in its entirety, shown in nearly every tutorial on libpcap. It gets a list of network devices, presents a simple ASCII based menu and waits for user to select one of those interfaces. We will just select the first interface in the list instead of taking input to shorten the example. Then it opens that interface for live capture. Using a packet handler it goes into a loop to catch a few packets, say 10. Prints some simple info about the packets, and then closes the pcap handle and exits.

Download Source from SVN:

package org.jnetpcap.examples;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jnetpcap.Pcap;
import org.jnetpcap.PcapIf;
import org.jnetpcap.packet.PcapPacket;
import org.jnetpcap.packet.PcapPacketHandler;

/**
 * Here is the output generated by this example :
 * 
 *  Network devices found:
 *  #0: \Device\NPF_{BC81C4FC-242F-4F1C-9DAD-EA9523CC992D} [Intel(R) PRO/100 VE] 
 *  #1: \Device\NPF_{E048DA7F-D007-4EEF-909D-4238F6344971} [VMware Virtual Ethernet Adapter]
 *  #2: \Device\NPF_{5B62B373-3EC1-460D-8C71-54AA0BF761C7} [VMware Virtual Ethernet Adapter]
Syndicate content