public static final int OK = 0; public static native int PcapActivate(Pcap pcap) throws PcapCloseException; public static native void PcapBreakLoop(Pcap pcap) throws PcapCloseException; public static native int PcapCanSetRfMon(Pcap pcap) throws PcapCloseException, NativeException; public static native int PcapCheckActivated(Pcap pcap); public static native void PcapClose(Pcap pcap) throws PcapCloseException; public static native int PcapCompile(Pcap pcap, BpfProgram fp, String str, int optimize, int netmask) throws PcapCloseException; public static native int PcapCompileNoPcap(int snaplenArg, int linktypeArg, BpfProgram program, String buf, int optimize, int mask)
		throws BpfProgramCloseException; public static native Pcap PcapCreate(String source, StringBuilder errbuf); public static native int PcapDataLink(Pcap pcap) throws PcapCloseException; public static native int PcapDataLinkNameToVal(String name); public static native String PcapDataLinkValToDescription(int dtl); public static native String PcapDataLinkValToName(int dtl); public static native <T> int PcapDispatch(Pcap pcap, int cnt, PcapHandler<T> callback, T user) throws PcapCloseException; @Incubating
public static native <T> int PcapDispatch0(Pcap pcap, int cnt, RawPcapHandler<T> callback, T user) throws PcapCloseException; public static native void PcapDump(PcapDumper pcapDumper, PcapPktHdr h, ByteBuffer sp) throws PcapDumperCloseException; public static native void PcapDumpClose(PcapDumper pcapDumper) throws PcapDumperCloseException; public static native long PcapDumpFTell(PcapDumper pcapDumper) throws PcapDumperCloseException; public static native int PcapDumpFlush(PcapDumper pcapDumper) throws PcapDumperCloseException; public static native PcapDumper PcapDumpOpen(Pcap pcap, String fname) throws PcapCloseException; public static native int PcapFindAllDevs(List<PcapIf> alldevsp, StringBuilder errbuf); public static native void PcapFreeCode(BpfProgram bpfProgram) throws BpfProgramCloseException; public static native String PcapGetErr(Pcap pcap) throws PcapCloseException; public static native int PcapGetNonBlock(Pcap pcap, StringBuilder errbuf) throws PcapCloseException; public static native int PcapGetTStampPrecision(Pcap pcap) throws PcapCloseException, PlatformNotSupportedException; public static native int PcapIsSwapped(Pcap pcap) throws PcapCloseException; public static native String PcapLibVersion(); public static native int PcapListDataLinks(Pcap pcap, List<Integer> dtlBuffer) throws PcapCloseException, PlatformNotSupportedException; public static native int PcapListTStampTypes(Pcap pcap, List<Integer> tstampTypesp) throws PcapCloseException, PlatformNotSupportedException; public static native <T> int PcapLoop(Pcap pcap, int cnt, PcapHandler<T> callback, T user) throws PcapCloseException; @Incubating
public static native <T> int PcapLoop0(Pcap pcap, int cnt, RawPcapHandler<T> callback, T user) throws PcapCloseException; public static native int PcapMajorVersion(Pcap pcap) throws PcapCloseException; public static native int PcapMinorVersion(Pcap pcap) throws PcapCloseException; public static native ByteBuffer PcapNext(Pcap pcap, PcapPktHdr h) throws PcapCloseException; public static native int PcapNextEx(Pcap pcap, PcapPktHdr pktHeader, ByteBuffer pktData) throws PcapCloseException; public static native int PcapOfflineFilter(BpfProgram fp, PcapPktHdr h, ByteBuffer pkt); public static native Pcap PcapOpenDead(int linktype, int snaplen); public static native Pcap PcapOpenDeadWithTStampPrecision(int linktype, int snaplen, int precision) throws PlatformNotSupportedException; public static native Pcap PcapOpenLive(String source, int snaplen, int promisc, int toMs, StringBuilder errbuf); public static native Pcap PcapOpenOffline(String fname, StringBuilder errbuf); public static native Pcap PcapOpenOfflineWithTStampPrecision(String fname, int tstampPrecision, StringBuilder errbuf)
		throws PlatformNotSupportedException; public static native void PcapPError(Pcap pcap, String prefix) throws PcapCloseException; public static native int PcapSetDataLink(Pcap pcap, int dtl) throws PcapCloseException, NativeException; public static native int PcapSetDirection(Pcap pcap, PcapDirection direction) throws PcapCloseException, PlatformNotSupportedException; public static native int PcapSetFilter(Pcap pcap, BpfProgram fp) throws PcapCloseException; public static native int PcapSetImmediateMode(Pcap pcap, int immediateMode) throws PcapCloseException, PlatformNotSupportedException; public static native int PcapSetNonBlock(Pcap pcap, int nonblock, StringBuilder errbuf) throws PcapCloseException; public static native int PcapSetPromisc(Pcap pcap, int promisc) throws PcapCloseException; public static native int PcapSetRfMon(Pcap pcap, int rfmon) throws PcapCloseException; public static native int PcapSetSnaplen(Pcap pcap, int snaplen) throws PcapCloseException; public static native int PcapSetTStampPrecision(Pcap pcap, int tstampPrecision) throws PcapCloseException, PlatformNotSupportedException; public static native int PcapSetTStampType(Pcap pcap, int type) throws PcapCloseException, PlatformNotSupportedException; public static native int PcapSetTimeout(Pcap pcap, int timeout) throws PcapCloseException; public static native int PcapSnapshot(Pcap pcap) throws PcapCloseException; public static native int PcapStats(Pcap pcap, PcapStat pcapStat) throws PcapCloseException; public static native String PcapStatusToStr(int errnum) throws PlatformNotSupportedException; public static native String PcapStrError(int error); public static native int PcapTStampTypeNameToVal(String name) throws PlatformNotSupportedException; public static native String PcapTStampTypeValToDescription(int tstampType) throws PlatformNotSupportedException; public static native String PcapTStampTypeValToName(int tstampType) throws PlatformNotSupportedException; public class DeviceNotFoundException extends RuntimeException {

	private static final long serialVersionUID = -7426666725353676994L;

	public DeviceNotFoundException() {
		this("");
	}

	public DeviceNotFoundException(final String message) {
		this(message, new RuntimeException(message));
	}

	public DeviceNotFoundException(final Throwable cause) {
		this(cause.getMessage(), cause);
	}

	public DeviceNotFoundException(final String message, final Throwable cause) {
		super(message, cause);
	}

} public class NativeException extends RuntimeException {

	private static final long serialVersionUID = -3993889826336916332L;

	public NativeException() {
		this("");
	}

	public NativeException(final String message) {
		this(message, new RuntimeException(message));
	}

	public NativeException(final Throwable cause) {
		this(cause.getMessage(), cause);
	}

	public NativeException(final String message, final Throwable cause) {
		super(message, cause);
	}

} public class PlatformNotSupportedException extends RuntimeException {

    private static final long serialVersionUID = -5714154798477963289L;

    public PlatformNotSupportedException() {
        this("");
    }

    public PlatformNotSupportedException(final String message) {
        this(message, new RuntimeException(message));
    }

    public PlatformNotSupportedException(final Throwable cause) {
        this(cause.getMessage(), cause);
    }

    public PlatformNotSupportedException(final String message, final Throwable cause) {
        super(message, cause);
    }

}
import static com.ardikars.jxnet.Jxnet.OK;
import static com.ardikars.jxnet.Jxnet.PcapActivate;
import static com.ardikars.jxnet.Jxnet.PcapBreakLoop;
import static com.ardikars.jxnet.Jxnet.PcapCanSetRfMon;
import static com.ardikars.jxnet.Jxnet.PcapCheckActivated;
import static com.ardikars.jxnet.Jxnet.PcapClose;
import static com.ardikars.jxnet.Jxnet.PcapCompile;
import static com.ardikars.jxnet.Jxnet.PcapCompileNoPcap;
import static com.ardikars.jxnet.Jxnet.PcapCreate;
import static com.ardikars.jxnet.Jxnet.PcapDataLink;
import static com.ardikars.jxnet.Jxnet.PcapDataLinkNameToVal;
import static com.ardikars.jxnet.Jxnet.PcapDataLinkValToDescription;
import static com.ardikars.jxnet.Jxnet.PcapDataLinkValToName;
import static com.ardikars.jxnet.Jxnet.PcapDispatch;
import static com.ardikars.jxnet.Jxnet.PcapDispatch0;
import static com.ardikars.jxnet.Jxnet.PcapDump;
import static com.ardikars.jxnet.Jxnet.PcapDumpClose;
import static com.ardikars.jxnet.Jxnet.PcapDumpFTell;
import static com.ardikars.jxnet.Jxnet.PcapDumpFlush;
import static com.ardikars.jxnet.Jxnet.PcapDumpOpen;
import static com.ardikars.jxnet.Jxnet.PcapFindAllDevs;
import static com.ardikars.jxnet.Jxnet.PcapFreeCode;
import static com.ardikars.jxnet.Jxnet.PcapGetErr;
import static com.ardikars.jxnet.Jxnet.PcapGetNonBlock;
import static com.ardikars.jxnet.Jxnet.PcapGetTStampPrecision;
import static com.ardikars.jxnet.Jxnet.PcapIsSwapped;
import static com.ardikars.jxnet.Jxnet.PcapLibVersion;
import static com.ardikars.jxnet.Jxnet.PcapListDataLinks;
import static com.ardikars.jxnet.Jxnet.PcapListTStampTypes;
import static com.ardikars.jxnet.Jxnet.PcapLoop;
import static com.ardikars.jxnet.Jxnet.PcapLoop0;
import static com.ardikars.jxnet.Jxnet.PcapMajorVersion;
import static com.ardikars.jxnet.Jxnet.PcapMinorVersion;
import static com.ardikars.jxnet.Jxnet.PcapNext;
import static com.ardikars.jxnet.Jxnet.PcapNextEx;
import static com.ardikars.jxnet.Jxnet.PcapOfflineFilter;
import static com.ardikars.jxnet.Jxnet.PcapOpenDead;
import static com.ardikars.jxnet.Jxnet.PcapOpenDeadWithTStampPrecision;
import static com.ardikars.jxnet.Jxnet.PcapOpenLive;
import static com.ardikars.jxnet.Jxnet.PcapOpenOffline;
import static com.ardikars.jxnet.Jxnet.PcapOpenOfflineWithTStampPrecision;
import static com.ardikars.jxnet.Jxnet.PcapPError;
import static com.ardikars.jxnet.Jxnet.PcapSetDataLink;
import static com.ardikars.jxnet.Jxnet.PcapSetDirection;
import static com.ardikars.jxnet.Jxnet.PcapSetFilter;
import static com.ardikars.jxnet.Jxnet.PcapSetImmediateMode;
import static com.ardikars.jxnet.Jxnet.PcapSetNonBlock;
import static com.ardikars.jxnet.Jxnet.PcapSetPromisc;
import static com.ardikars.jxnet.Jxnet.PcapSetRfMon;
import static com.ardikars.jxnet.Jxnet.PcapSetSnaplen;
import static com.ardikars.jxnet.Jxnet.PcapSetTStampPrecision;
import static com.ardikars.jxnet.Jxnet.PcapSetTStampType;
import static com.ardikars.jxnet.Jxnet.PcapSetTimeout;
import static com.ardikars.jxnet.Jxnet.PcapSnapshot;
import static com.ardikars.jxnet.Jxnet.PcapStats;
import static com.ardikars.jxnet.Jxnet.PcapStatusToStr;
import static com.ardikars.jxnet.Jxnet.PcapStrError;
import static com.ardikars.jxnet.Jxnet.PcapTStampTypeNameToVal;
import static com.ardikars.jxnet.Jxnet.PcapTStampTypeValToDescription;
import static com.ardikars.jxnet.Jxnet.PcapTStampTypeValToName;
import com.ardikars.common.net.Inet4Address;
import com.ardikars.common.net.MacAddress;
import com.ardikars.common.util.Platforms;
import com.ardikars.jxnet.exception.DeviceNotFoundException;
import com.ardikars.jxnet.exception.NativeException;
import com.ardikars.jxnet.exception.PlatformNotSupportedException;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.runners.MethodSorters;
                logger.warning("create:PcapSetImmediateMode(): " + PcapStrError(resultCode));
                PcapClose(pcap);
                return;
            }
        }
        if ((resultCode = PcapCheckActivated(pcap)) == 0) {
            if ((resultCode = PcapActivate(pcap)) != OK) {
                logger.warning("create:PcapActivate(): " + PcapStrError(resultCode));
                PcapClose(pcap);
                return;
            }
        }

        bpfProgram = new BpfProgram();

        File file = File.createTempFile("dump", ".pcap");
        if (!file.delete()) {
            logger.warning("create:File.delete()");
            return;
        }
        dumper = PcapDumpOpen(pcap, file.getAbsolutePath());
        if (dumper == null) {
            logger.warning("create:PcapDumpOpen() ");
            return;
        }
    }

    @Test
    public void Test01_PcapFindAllDevs() {
        if ((resultCode = PcapFindAllDevs(alldevsp, errbuf)) != OK) {
            logger.warning("PcapFindAllDevs:PcapFindAllDevs(): " + errbuf.toString());
        }
        for (PcapIf dev : alldevsp) {
            logger.info("================================================\n\n");
            logger.info("Name                  = " + dev.getName());
            logger.info("Description           = " + dev.getDescription());
            logger.info("Flags                 = " + dev.getFlags());
            logger.info("Is loopback           = " + dev.isLoopback());
            logger.info("Is Up                 = " + dev.isUp());
            logger.info("Is Running            = " + dev.isRunning());
            for (PcapAddr addr : dev.getAddresses()) {
                if (addr.getAddr().getSaFamily() == SockAddr.Family.AF_INET) {
                    logger.info("------------------------------------------------");
                    logger.info("IPv4");
                    logger.info("Addr                   = " + addr.getAddr().toString());
                    logger.info("Netmask                = " + addr.getNetmask().toString());
                    logger.info("BroadAddr              = " + addr.getBroadAddr().toString());
                    logger.info("DstAddr                = " + addr.getDstAddr().toString());
                    logger.info("------------------------------------------------");
                } else if (addr.getAddr().getSaFamily() == SockAddr.Family.AF_INET6) {
                    logger.info("------------------------------------------------");
                    logger.info("IPv6");
                    logger.info("Addr                   = " + addr.getAddr().toString());
                    logger.info("Netmask                = " + addr.getNetmask().toString());
                    logger.info("BroadAddr              = " + addr.getBroadAddr().toString());
                    logger.info("DstAddr                = " + addr.getDstAddr().toString());
                    logger.info("------------------------------------------------");
                } else {
                    logger.info(addr.toString());
                }
                logger.info("Family                 = " + addr.getAddr().getSaFamily().toString());
            }
            logger.info("================================================\n\n");
        }
    }

    @Test
    public void Test02_PcapOpenLiveAndPcapClose() {
        Pcap pcap = PcapOpenLive(source, snaplen, promisc, timeout, errbuf);
        if (pcap == null) {
            logger.warning("PcapOpenLiveAndPcapClose:PcapOpenLive(): " + errbuf.toString());
        } else {
            PcapClose(pcap);
        }
    }

    @Test
    public void Test03_PcapLoopAndPcapStats() {
        if ((resultCode = PcapLoop(pcap, maxPkt, callback, "This Is User Argument")) != OK) {
            logger.warning("PcapLoop:PcapLoop(): " + PcapStrError(resultCode));
            return;
        }
        PcapStat stat = new PcapStat();
        logger.info("Droped by kernel    : " + stat.getPsDrop());
        logger.info("Droped by interface : " + stat.getPsIfdrop());
        logger.info("Recieved            : " + stat.getPsRecv());
        if ((resultCode = PcapStats(pcap, stat)) != OK) {
            logger.warning("PcapLoopAndPcapStats:PcapStats(): " + PcapStrError(resultCode));
            return;
        }
        logger.info(String.valueOf(stat));
    }

    @Test
    public void Test04_PcapDispatchAndPcapSetNonBlock() {
        if ((resultCode = PcapSetNonBlock(pcap, 1, errbuf)) != OK) {
            logger.warning("PcapDispatch:PcapSetNonBlock(): " + PcapStrError(resultCode));
        }
        if ((resultCode = PcapDispatch(pcap, maxPkt, callback, "This Is User Argument")) != OK) {
            logger.warning("PcapDispatch:PcapDispatch(): " + PcapStrError(resultCode));
            return;
        }
        if ((resultCode = PcapDispatch0(pcap, maxPkt, callback0, "This Is User Argument")) != OK) {
            logger.warning("PcapDispatch:PcapDispatch0(): " + PcapStrError(resultCode));
            return;
        }
    }

    @Test
    public void Test05_PcapDumpOpenAndPcapDumpClose() throws IOException {
        File file = File.createTempFile("dump", ".pcap");
        if (!file.delete()) {
            logger.warning("PcapDumpOpenAndClose:File.delete()");
            return;
        }
        PcapDumper dumper = PcapDumpOpen(pcap, file.getAbsolutePath());
        if (dumper == null) {
            logger.warning("PcapDumpOpenAndClose:PcapDumpOpen() ");
            return;
        }
        PcapDumpClose(dumper);