当前位置: 首页>>代码示例>>Java>>正文


Java Pcap.create方法代码示例

本文整理汇总了Java中org.jnetpcap.Pcap.create方法的典型用法代码示例。如果您正苦于以下问题:Java Pcap.create方法的具体用法?Java Pcap.create怎么用?Java Pcap.create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.jnetpcap.Pcap的用法示例。


在下文中一共展示了Pcap.create方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: openAnyDevice

import org.jnetpcap.Pcap; //导入方法依赖的package包/类
@Test
public void openAnyDevice() {
    List<PcapIf> devices = new ArrayList<PcapIf>();
    StringBuilder err = new StringBuilder();

    if (Pcap.findAllDevs(devices, err) != Pcap.OK || devices.isEmpty()) {
        throw new RuntimeException("Failed to find network devices!");
    }

    Pcap pcap = null;
    for (PcapIf dev : devices) {
        if (dev.getName().equalsIgnoreCase(Constants.ANY_DEVICE)) {
            pcap = Pcap.create(Constants.ANY_DEVICE, err);
        }
    }

    if (pcap == null) {
        throw new RuntimeException("Failed to open \""
                + Constants.ANY_DEVICE + "\" device!");
    }
    
    pcap.close();
}
 
开发者ID:fg-netzwerksicherheit,项目名称:jnetpcapfacade,代码行数:24,代码来源:SimplePcapCaptureTests.java

示例2: _testPcapCanSetRfMond

import org.jnetpcap.Pcap; //导入方法依赖的package包/类
public void _testPcapCanSetRfMond() throws SecurityException,
		NoSuchMethodException {
	List<PcapIf> alldevs = new ArrayList<PcapIf>();
	StringBuilder errbuf = new StringBuilder();

	Pcap.findAllDevs(alldevs, errbuf);
	Pcap pcap = Pcap.create(alldevs.get(0).getName(), errbuf);

	if (Pcap.isLoaded("canSetRfmon")) {
		pcap.canSetRfmon();
	}

	pcap.close();

}
 
开发者ID:pvenne,项目名称:jgoose,代码行数:16,代码来源:TestLibrary.java

示例3: createAndActivatePcap

import org.jnetpcap.Pcap; //导入方法依赖的package包/类
public static Pcap createAndActivatePcap(String device) {
	NativeLibraryLoader.loadNativeLibs();
    Pcap pcap = null;

    List<PcapIf> devices = new ArrayList<PcapIf>();
    StringBuilder err = new StringBuilder();

    if (Pcap.findAllDevs(devices, err) != Pcap.OK || devices.isEmpty()) {
    	log.error("Failed to find network devices");
        throw new RuntimeException("Failed to find network devices!");
    }

    for (PcapIf dev : devices) {
        if (dev.getName().equalsIgnoreCase(device)) {
            pcap = Pcap.create(device, err);
            break;
        }
    }

    if (pcap == null && devices.size() < 1) {
    	log.error("Failed to open device: " + device);
        throw new RuntimeException("Failed to open \"" + device + "\" device!");
    } else if (pcap == null) {
    	log.error("Failed to get requested device: " + device);
        device = devices.get(0).getName();
        log.error("Using first device found: " + device);
        pcap = Pcap.create(device, err);
    }

    pcap.setSnaplen(Constants.SNAPLEN);
    pcap.setPromisc(Constants.FLAGS);
    pcap.setBufferSize(Constants.BUFFER_SIZE);

    if (pcap.activate() != 0) {
    	log.error("Failed to activate device: " + device + " -> " + pcap.getErr());
        throw new RuntimeException("Failed to activate \"" + device + "\" device!\n" + "Pcap error follows:\n" + pcap.getErr());
    }

    return pcap;
}
 
开发者ID:fg-netzwerksicherheit,项目名称:jnetpcapfacade,代码行数:41,代码来源:PcapHelper.java

示例4: openPcap

import org.jnetpcap.Pcap; //导入方法依赖的package包/类
private static Pcap openPcap(String ifaceName) throws PcapException {
    Pcap pcap;
    StringBuilder errbuf = new StringBuilder();

    if (!Pcap.isPcap100Loaded()) {
        throw new PcapException("jnetpcap1.4 + libpcap1.0.0+ are required");
    }
    
    if (WinPcap.isSupported() == true)
    //if (false)
    {
        // This code will be used on a windows platform (winpcap)
    	
    	WinPcapRmtAuth auth = null;
    	pcap = WinPcap.open(ifaceName, snaplen, flags | PCAP_OPENFLAG_NOCAPTURE_LOCAL, timeoutMs, auth, errbuf);
    	
    }
    else
    {
    	// This code will be used on all other platforms
    	
    	pcap = Pcap.create(ifaceName, errbuf);
        if (pcap == null) {
            throw new PcapException("failed to create pcap interface " + ifaceName + ": " + errbuf.toString());
        }
    	
    	// standard properties
        pcap.setSnaplen(snaplen);
        pcap.setPromisc(flags);
        pcap.setTimeout(timeoutMs);

        // specific to libpcap 1.0.0
        
        // pcap.setDirection is not implemented on a windows platform (winpcap)
        // the flag method must be used
        pcap.setDirection(Pcap.Direction.IN);
        pcap.setBufferSize(bufsizeBytes);

        pcap.activate();	
    }

    return pcap;
}
 
开发者ID:pvenne,项目名称:jgoose,代码行数:44,代码来源:PcapPort.java

示例5: activateAnyDevice

import org.jnetpcap.Pcap; //导入方法依赖的package包/类
@Test
public void activateAnyDevice() {
    List<PcapIf> devices = new ArrayList<PcapIf>();
    StringBuilder err = new StringBuilder();

    if (Pcap.findAllDevs(devices, err) != Pcap.OK || devices.isEmpty()) {
        throw new RuntimeException("Failed to find network devices!");
    }

    for (PcapIf dev : devices) {
        if (dev.getName().equalsIgnoreCase(Constants.ANY_DEVICE)) {
            pcap = Pcap.create(Constants.ANY_DEVICE, err);
        }
    }

    if (pcap == null) {
        throw new RuntimeException("Failed to open \""
                + Constants.ANY_DEVICE + "\" device!");
    }

    pcap.setSnaplen(Constants.SNAPLEN);
    pcap.setPromisc(Constants.FLAGS);
    pcap.setBufferSize(Constants.BUFFER_SIZE);

    if (pcap.activate() != 0) {
        throw new RuntimeException("Failed to activate \""
                + Constants.ANY_DEVICE + "\" device!");
    }

    /*
     * The pcap.loop is blocking so we use a thread as timer to stop
     * capturing.
     */
    Thread stopper = new Thread() {
        @Override
        public void run() {
            try {
                sleep(TestConstants.CAPTURE_DELAY);
                pcap.breakloop();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    stopper.start();

    int ret = pcap.loop(Pcap.LOOP_INFINITE,
            new PcapPacketHandler<Object>() {
                @Override
                public void nextPacket(PcapPacket arg0, Object arg1) {
                }
            }, null);

    /*
     * As we break the loop we check the return value if this was correctly
     * done.
     */
    assertEquals(Pcap.ERROR_BREAK, ret);
    pcap.close();
}
 
开发者ID:fg-netzwerksicherheit,项目名称:jnetpcapfacade,代码行数:61,代码来源:SimplePcapCaptureTests.java


注:本文中的org.jnetpcap.Pcap.create方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。