jNetPcap 1.X

jNetPcap 1.X module: libpcap and packet decoder

MAC Address

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

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

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

Classic Example

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