本文整理汇总了Java中org.jnetpcap.Pcap类的典型用法代码示例。如果您正苦于以下问题:Java Pcap类的具体用法?Java Pcap怎么用?Java Pcap使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Pcap类属于org.jnetpcap包,在下文中一共展示了Pcap类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getByteLimitedHandler
import org.jnetpcap.Pcap; //导入依赖的package包/类
/**
* Creation of a pseudopackethandler packet handler type which breaks the loop after.
*
* @return returns a packethandler which handles' the packets and stops if the max_bytes amount is handled.
*/
private static PcapPacketHandler<PseudoPacketHandler> getByteLimitedHandler(final Pcap pcap, final long MAX_BYTES) {
// Packet handler. Generic callback function for PseudoPacketHandler.
return new PcapPacketHandler<PseudoPacketHandler>() {
long packetBytes = 0;
// Handle nextPacket callback.
public void nextPacket(PcapPacket packet, PseudoPacketHandler handler) {
handler.handle(packet);
packetBytes+= packet.size();
if (packetBytes >= MAX_BYTES) {
pcap.breakloop();
}
}
};
}
示例2: getPacketList
import org.jnetpcap.Pcap; //导入依赖的package包/类
/**
* Gets the packet list.
*
* @param file
* the file
* @return the packet list
* @throws IOException
* Signals that an I/O exception has occurred.
*/
private List<PcapPacket> getPacketList(String file) throws IOException {
StringBuilder errbuf = new StringBuilder();
Pcap pcap = Pcap.openOffline(file, errbuf);
if (pcap == null) {
throw new IOException(errbuf.toString());
}
final PcapPacketArrayList list = new PcapPacketArrayList(
(int) new File(file).length() / 100);
pcap.loop(Pcap.LOOP_INFINATE, list, null);
pcap.close();
return list;
}
示例3: init
import org.jnetpcap.Pcap; //导入依赖的package包/类
/**
* Initialize service
*
* @throws IOException
* @throws RouteException
*/
@Override
public void init(final ServiceFactory services) throws Exception {
super.init(services);
if (Env.INSTANCE.getOs() != OS.mac) {
services.updateStartup("init.sniffer.network", true);
try {
final StringBuilder sb = new StringBuilder();
final int r = Pcap.findAllDevs(_devices, sb);
if (r == Pcap.NOT_OK || _devices.isEmpty()) {
throw new IOException(sb.toString());
}
for (final PcapIf net : new ArrayList<>(_devices)) {
if (net.getAddresses().isEmpty()) {
_devices.remove(net);
} else {
_gatewayMac.put(net, net.getHardwareAddress());
}
}
} catch (final Throwable e) {
LOGGER.warn("Cannot find a suitable network device for tracing.", e);
}
}
}
示例4: SKIPtestRemoteOpen
import org.jnetpcap.Pcap; //导入依赖的package包/类
/**
* SKI ptest remote open.
*/
public void SKIPtestRemoteOpen() {
StringBuilder source = new StringBuilder();
int r = WinPcap.createSrcStr(source, WinPcap.SRC_IFREMOTE, rhost, null,
rdevice, errbuf);
if (r != Pcap.OK) {
fail(errbuf.toString());
} else {
System.out.printf("source=%s\n", source);
}
WinPcap pcap = WinPcap.open(source.toString(), snaplen, flags, oneSecond,
null, errbuf);
assertNotNull(errbuf.toString(), pcap);
pcap.loop(10, printTimestampHandler, null);
pcap.close();
}
示例5: testWinPcapMainCommentEx2
import org.jnetpcap.Pcap; //导入依赖的package包/类
/**
* Test win pcap main comment ex2.
*/
public void testWinPcapMainCommentEx2() {
String source = "rpcap://\\Device\\PF_{BC81C4FC-242F-4F1C-9DAD-EA9523CC992D}";
int snaplen = 64 * 1024;
int flags = Pcap.MODE_NON_PROMISCUOUS;
int timeout = 1000;
WinPcapRmtAuth auth = null;
StringBuilder errbuf = new StringBuilder();
WinPcap pcap = WinPcap.open(source, snaplen, flags, timeout, auth, errbuf);
if (pcap == null) {
fail(errbuf.toString());
return;
}
pcap.close();
}
示例6: testWinPcapMainCommentCreateStr
import org.jnetpcap.Pcap; //导入依赖的package包/类
/**
* Test win pcap main comment create str.
*/
public void testWinPcapMainCommentCreateStr() {
String source = "rpcap://";
List<PcapIf> alldevs = new ArrayList<PcapIf>();
int r = WinPcap.findAllDevsEx(source, auth, alldevs, errbuf);
if (r != Pcap.OK) {
fail(errbuf.toString());
return;
}
StringBuilder buf = new StringBuilder();
WinPcap.createSrcStr(buf, WinPcap.SRC_IFLOCAL, null, null, alldevs.get(0)
.getName(), errbuf);
System.out.println("Our source string is " + alldevs.get(0).getName());
}
示例7: 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);
}
示例8: openOffline
import org.jnetpcap.Pcap; //导入依赖的package包/类
/**
* Open offline.
*
* @param file
* the file
* @param handler
* the handler
* @param filter
* the filter
*/
public static void openOffline(String file,
JPacketHandler<Pcap> handler,
String filter) {
StringBuilder errbuf = new StringBuilder();
Pcap pcap;
if ((pcap = Pcap.openOffline(file, errbuf)) == null) {
fail(errbuf.toString());
}
if (filter != null) {
PcapBpfProgram program = new PcapBpfProgram();
if (pcap.compile(program, filter, 0, 0) != Pcap.OK) {
System.err.printf("pcap filter err: %s\n", pcap.getErr());
}
pcap.setFilter(program);
}
pcap.loop(Pcap.LOOP_INFINATE, handler, pcap);
pcap.close();
}
示例9: setFilter
import org.jnetpcap.Pcap; //导入依赖的package包/类
public synchronized void setFilter(String filter_str)throws PcapException {
// pcap.compile to define packet filter operation
final int optimize = 1; // 1 = true
final int netmask = 0; // 0
// We setup the packet filter
packet_filter_program = new PcapBpfProgram();
// We compile the filter
if (pcap.compile(packet_filter_program, filter_str, optimize, netmask) != Pcap.OK)
{
throw new PcapException("failed to compile filter: " + filter_str + pcap.getErr());
}
// We set the filter
if (pcap.setFilter(packet_filter_program) != Pcap.OK)
{
throw new PcapException("failed to set filter: " + filter_str + pcap.getErr());
}
}
示例10: close
import org.jnetpcap.Pcap; //导入依赖的package包/类
public void close() {
stop();
synchronized (pcapLock) {
if (pcap != null) {
if(packet_filter_program != null)
{
Pcap.freecode(packet_filter_program);
packet_filter_program =null;
}
pcap.close();
pcap = null;
}
}
}
示例11: 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);
}
示例12: onPreExecute
import org.jnetpcap.Pcap; //导入依赖的package包/类
protected void onPreExecute(){
blockLength=PcapFileLoader.blockLength;
pcapPackets=new PcapPacket[blockLength.value];
PcapFileLoader.pcapPackets[block]=pcapPackets;
lowerBound=new IntHolder(targetPacket);
if (indeterminate[block] || targetPacket<= PcapFileLoader.range[block][1].value){
if (!indeterminate[block]){
PcapFileLoader.pcap[block].close();
}
JScanner.getThreadLocal().setFrameNumber(0);
final StringBuilder errbuf=new StringBuilder();
pcap = Pcap.openOffline(filename, errbuf);
currentPacket=new IntHolder(0);
PcapFileLoader.pcap[block]=pcap;
PcapFileLoader.range[block][1]=currentPacket;
} else {
pcap=PcapFileLoader.pcap[block];
currentPacket=PcapFileLoader.range[block][1];
}
PcapFileLoader.range[block][0]=lowerBound;
indeterminate[block]=true;
Log.d("PcapBlockLoadTask", "loading block: "+Integer.toString(block)+"|"+Integer.toString(lowerBound.value));
}
示例13: getNetworkInterfaces
import org.jnetpcap.Pcap; //导入依赖的package包/类
/**
* Lists all enabled network interfaces on the system.
*/
public static List<PcapIf> getNetworkInterfaces() {
List<PcapIf> devices = new ArrayList<>();
StringBuilder errorBuilder = new StringBuilder();
if (Pcap.findAllDevs(devices, errorBuilder) == -1) {
HCapUtils.logger.severe("Unable to list interfaces! " + errorBuilder.toString());
return devices;
}
if (!devices.isEmpty()) {
HCapUtils.logger.info("Detected " + devices.size() + " network interfaces.");
} else {
HCapUtils.logger.info("No network interfaces detected.");
}
// not needed - see jnetpecap docs
// Pcap.freeAllDevs(deviceNames, errorBuilder);
return devices;
}
示例14: 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);
}
示例15: main
import org.jnetpcap.Pcap; //导入依赖的package包/类
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
//Create a Queue
BlockingQueue<Packet> queue = new LinkedBlockingQueue<Packet>();
//Create a Packethandler
PcapPacketHandler<Object> handler = new FullPacketHandler(queue);
//Start Sniffer on Loopback-Device
//Not working on Windows 8 (there is no Loopback)
Pcap pcap = PcapHelper.createAndActivatePcap(Constants.LOOPBACK_DEVICE);
PacketSniffer sniffer = new PacketSniffer(pcap, handler);
sniffer.start();
//Create our custom Processor to do something with the packets
QueueEater forwarder = new QueueEater(queue);
CustomPacketProcessor processor = new CustomPacketProcessor();
forwarder.addProcessors(processor);
forwarder.start();
//Sleep 10 Seconds
Thread.sleep(10000);
//Stop everything
sniffer.stopSniffer();
forwarder.stopQueueEating();
}