本文整理汇总了Java中org.jnetpcap.Pcap.openLive方法的典型用法代码示例。如果您正苦于以下问题:Java Pcap.openLive方法的具体用法?Java Pcap.openLive怎么用?Java Pcap.openLive使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jnetpcap.Pcap
的用法示例。
在下文中一共展示了Pcap.openLive方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: openLive
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
/**
* Open live.
*
* @param count
* the count
* @param handler
* the handler
*/
public static void openLive(long count, JPacketHandler<Pcap> handler) {
StringBuilder errbuf = new StringBuilder();
List<PcapIf> alldevs = new ArrayList<PcapIf>();
if (Pcap.findAllDevs(alldevs, errbuf) != Pcap.OK) {
throw new IllegalStateException(errbuf.toString());
}
Pcap pcap =
Pcap.openLive(alldevs.get(0).getName(),
Pcap.DEFAULT_SNAPLEN,
Pcap.DEFAULT_PROMISC,
Pcap.DEFAULT_TIMEOUT,
errbuf);
if (pcap == null) {
throw new IllegalArgumentException(errbuf.toString());
}
pcap.loop((int) count, handler, pcap);
}
示例2: getPcapDevices
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
/**
* This function returns a list of <code>PcapIf</code>s with certain
* DLT.
*
* @param rescan If true, don't use cached data
* @param dlt DLT constant, for example PcapDLT.CONST_EN10MB
* @return
* @throws PcapException
*/
public static List<PcapIf> getPcapDevices(boolean rescan, int dlt) throws PcapException {
List<PcapIf> pcapDevices = getAllPcapDevices(rescan);
List<PcapIf> pcapDevicesWithRightDlt = new ArrayList<PcapIf>();
for (PcapIf pcapIf : pcapDevices) {
StringBuilder errbuf = new StringBuilder();
Pcap pcap = Pcap.openLive(pcapIf.getName(), 0, 0, 0, errbuf);
if (pcap != null && pcap.datalink() == dlt) {
pcapDevicesWithRightDlt.add(pcapIf);
}
}
return Collections.unmodifiableList(pcapDevicesWithRightDlt);
}
示例3: run
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
@Override
protected void run(String ... args) throws Exception {
super.run(args);
if (interfaceId == -1) {
printNetworkInterfaces();
interfaceId = 0;
}
PcapIf device = selectPcapIf(interfaceId);
System.out.println("Recording from: " + device.getName() + " (" + device.getDescription() + ')');
StringBuilder err = new StringBuilder();
final Pcap pcap = Pcap.openLive(device.getName(), CaptureSettings.PACKET_SNAP_LENGTH, Pcap.MODE_NON_PROMISCUOUS, CaptureSettings.OPEN_LIVE_TIMEOUT_MILLIS, err);
if (pcap == null)
throw new IllegalArgumentException(err.toString());
if (captureFilter != null)
setupFilter(pcap, captureFilter);
runCaptureLoop(pcap, err);
}
示例4: getLivePcapStream
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
private static final Pcap getLivePcapStream(final String inputDevice) throws IOException {
final PcapIf device = getNetworkDevice(inputDevice, false);
if (device == null) {
throw new IOException("No suitable device found, select an existing device.");
}
// Configuration constants.
final int snaplen = 64 * 1024; // Set buffer large enough for complete packets, no truncation
final int flags = Pcap.MODE_PROMISCUOUS; // Capture all packets that are found.
final int timeout = 10 * 1000; // 10 seconds in millis
// Buffer for c-type errors.
final StringBuilder errbuf = new StringBuilder();
// Open the live stream.
final Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);
if (pcap == null) {
throw new IOException(errbuf.toString());
}
return pcap;
}
示例5: compile
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
public static boolean compile(String interfaceName, String filter) {
StringBuilder errBuf = new StringBuilder();
Pcap pcap = Pcap.openLive(interfaceName,
Pcap.DEFAULT_SNAPLEN,
Pcap.DEFAULT_PROMISC,
Pcap.DEFAULT_TIMEOUT,
errBuf);
if (pcap == null) {
System.err.println("Error while open device interface:" + errBuf);
return false;
}
PcapBpfProgram program = new PcapBpfProgram();
int optimize = 0;
int netmask = 0xFFFFFF00;
if (pcap.compile(program, filter, optimize, netmask) != Pcap.OK) {
return false;
}
pcap.close();
return true;
// int len = 64 * 1024;
// int datalinkType = Ethernet.EthernetType.IP4.getId();
// int optimize = 0;
// int netmask = 0xFFFFFF00;
// filterProgram = new PcapBpfProgram();
// if (Pcap.compileNoPcap(len, datalinkType, filterProgram, filter, 0, netmask) != Pcap.OK) {
// filterProgram = null; // 如果编译失败将其置为null,避免错误使用
// System.err.println("error");
// return false;
// }
// return true;
}
示例6: Send_Frame
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
public void Send_Frame(PcapIf device,ByteBuffer a){
Pcap pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);
if (pcap.sendPacket(a) != Pcap.OK) {
JOptionPane.showMessageDialog(null, "Not sent \n"+ pcap.getErr(), "Error in sending Farmes", JOptionPane.ERROR_MESSAGE);
}
}
示例7: captureLive
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
public void captureLive(OnCapturePacketListener listener) {
if (networkAdapter == null || networkAdapter.getPcapIf() == null) {
System.err.println("Network adapter is null");
return ;
}
stopCapture();
PcapIf pcapIf = networkAdapter.getPcapIf();
StringBuilder errBuf = new StringBuilder();
pcap = Pcap.openLive(pcapIf.getName(),
Pcap.DEFAULT_SNAPLEN,
Pcap.DEFAULT_PROMISC,
Pcap.DEFAULT_TIMEOUT,
errBuf);
if (pcap == null) {
System.err.println("Error while open device interface:" + errBuf);
return ;
}
if (filterExp != null && !filterExp.isEmpty()) {
int optimize = 1;
int netmask = 0xFFFFFF00;
PcapBpfProgram program = new PcapBpfProgram();
if (pcap.compile(program, filterExp, optimize, netmask) != Pcap.OK) {
System.err.println("Invalid capture filter expression!");
} else {
pcap.setFilter(program);
}
}
task = new Task<Void>() {
@Override
protected Void call() throws Exception {
PcapPacketHandler<Void> handler = new PcapPacketHandler<Void>() {
@Override
public void nextPacket(PcapPacket pcapPacket, Void aVoid) {
// if (Config.getTimestamp() <= Config.DEFAULT_TIMESTAMP) {
// Config.setTimestamp(pcapPacket.getCaptureHeader().timestampInMicros());
// }
// packetInfos.add(new PacketInfo(pcapPacket));
// // 通知更新
// updateProgress(packetInfos.size(), Long.MAX_VALUE);
if (listener != null) {
listener.onCapture(pcapPacket);
}
}
};
pcap.loop(-1, handler, null);
return null;
}
};
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();
}
示例8: startCapture
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
private void startCapture(PcapIf device, PcapPacketHandler<Void> packetHandler, StringBuilder errbuf)
{
pcap = Pcap.openLive(device.getName(), snaplen, flags, timeout, errbuf);
if (pcap == null)
{
logger.log(Level.SEVERE, "Error while opening device for capture: " + errbuf.toString());
return;
}
pcap.loop(Pcap.LOOP_INFINITE, packetHandler, null);
}
示例9: testNegativeSnaplen
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
/**
* Test negative snaplen.
*/
public void testNegativeSnaplen() {
try {
Pcap.openLive("", -1, 0, 0, new StringBuilder());
fail("expected an IllegalArgumentException for snaplen < 0");
} catch (IllegalArgumentException e) {
// SUCCESS
}
}