本文整理汇总了Java中org.jnetpcap.packet.PcapPacketHandler类的典型用法代码示例。如果您正苦于以下问题:Java PcapPacketHandler类的具体用法?Java PcapPacketHandler怎么用?Java PcapPacketHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PcapPacketHandler类属于org.jnetpcap.packet包,在下文中一共展示了PcapPacketHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getByteLimitedHandler
import org.jnetpcap.packet.PcapPacketHandler; //导入依赖的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: remove
import org.jnetpcap.packet.PcapPacketHandler; //导入依赖的package包/类
/**
* Removes the.
*
* @param o
* the o
* @return true, if successful
*/
public boolean remove(PcapPacketHandler<?> o) {
listenersArray = null;
for (Iterator<Entry> i = listeners.iterator(); i.hasNext();) {
Entry e = i.next();
if (o == e.handler) {
i.remove();
listenersArray = null; // reset
return true;
}
}
return false;
}
示例3: main
import org.jnetpcap.packet.PcapPacketHandler; //导入依赖的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();
}
示例4: captureTest
import org.jnetpcap.packet.PcapPacketHandler; //导入依赖的package包/类
@Test
public void captureTest() throws InterruptedException {
Client client = new Client();
client.startClient(TestConstants.CAPTURE_DELAY);
/*
* Break the loop on the first packet we receive.
*/
int ret = pcap.loop(Pcap.LOOP_INFINITE,
new PcapPacketHandler<Object>() {
@Override
public void nextPacket(PcapPacket arg0, Object arg1) {
pcap.breakloop();
}
}, null);
/*
* As we break the loop we check the return value if this was correctly
* done.
*/
assertEquals(Pcap.ERROR_BREAK, ret);
}
示例5: captureTcpSyn
import org.jnetpcap.packet.PcapPacketHandler; //导入依赖的package包/类
@Test
public void captureTcpSyn() throws InterruptedException {
Client client = new Client();
client.startClient(TestConstants.CAPTURE_DELAY);
/*
* Break the loop when we receive a TCP packet with the SYN flag set.
*/
int ret = pcap.loop(Pcap.LOOP_INFINITE,
new PcapPacketHandler<Object>() {
@Override
public void nextPacket(PcapPacket p, Object arg1) {
if (p.hasHeader(tcp)
&& tcp.destination() == TestConstants.TCP_TEST_PORT
&& tcp.flags_SYN()) {
pcap.breakloop();
}
}
}, null);
/*
* As we break the loop we check the return value if this was correctly
* done.
*/
assertEquals(Pcap.ERROR_BREAK, ret);
}
示例6: tcpSynFinSniffTest
import org.jnetpcap.packet.PcapPacketHandler; //导入依赖的package包/类
@Test
public void tcpSynFinSniffTest() throws InterruptedException {
Queue<Packet> queue = new LinkedBlockingQueue<Packet>();
PcapPacketHandler<Object> handler = new TcpSynFinPacketHandler(queue);
Pcap pcap = PcapHelper.createAndActivatePcap(Constants.LOOPBACK_DEVICE);
PacketSniffer sniffer = new PacketSniffer(pcap, handler);
sniffer.start();
Client client = new Client();
client.startClient();
Thread.sleep(TestConstants.CAPTURE_DELAY);
sniffer.stopSniffer();
/*
* 1 TCP Connection ->
* SYN + SYN/ACK + FIN + FIN/ACK == 4 Packets
*/
assertEquals(4, queue.size());
}
示例7: tcpSynFinHandlerTest
import org.jnetpcap.packet.PcapPacketHandler; //导入依赖的package包/类
@Test
public void tcpSynFinHandlerTest() throws InterruptedException {
Queue<Packet> queue = new LinkedBlockingQueue<Packet>();
PcapPacketHandler<Object> handler = new TcpSynFinPacketHandler(queue);
Client client = new Client();
client.startClient(TestConstants.CAPTURE_DELAY);
Thread stopper = new Thread() {
@Override
public void run() {
try {
sleep(3 * TestConstants.CAPTURE_DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
pcap.breakloop();
};
};
stopper.start();
pcap.loop(Pcap.LOOP_INFINITE, handler, null);
assertEquals(4, queue.size());
}
示例8: handleWithMetal
import org.jnetpcap.packet.PcapPacketHandler; //导入依赖的package包/类
public void handleWithMetal(boolean live, boolean timed, String destination, FramePseudonymizer pseudonymizer)
{
long startTime = System.currentTimeMillis();
final Pcap pcap;
final PcapPacketHandler<PseudoPacketHandler> handler;
try {
if (live) {
pcap = getLivePcapStream(_input);
handler = getByteLimitedHandler(pcap, _maxBytes);
}
else {
pcap = getOfflinePcap(_input);
handler = getSimpleHandler();
}
}
catch (IOException ioexc) {
logger.severe("Error while opening device for capture: " +
ioexc.getMessage());
return;
}
PseudoPacketHandler dumpObject = new PEFPseudonymizeDumper(pcap, destination, pseudonymizer);
try {
pcap.loop(Pcap.LOOP_INFINITE, handler, dumpObject);
if (timed) {
// A timer for performance tests.
long stopTime = System.currentTimeMillis();
logger.info("Stopped after " + ((stopTime - startTime) / 1000.0) + " seconds.");
}
}
catch (NullPointerException e) {
logger.severe("Error in capturing loop" + e.getMessage());
}
finally {
dumpObject.close();
pcap.close();
}
}
示例9: getSimpleHandler
import org.jnetpcap.packet.PcapPacketHandler; //导入依赖的package包/类
/**
* Creation of a pseudopackethandler packet handler type.
* Note that used in combination with a infinite loop, will never break.
*
* @return returns a packethandler which just 'handles' the packet.
*/
private static PcapPacketHandler<PseudoPacketHandler> getSimpleHandler() {
// Packet handler. Generic callback function for PseudoPacketHandler.
return new PcapPacketHandler<PseudoPacketHandler>() {
// Handle nextPacket callback.
public void nextPacket(PcapPacket packet, PseudoPacketHandler handler) {
handler.handle(packet);
}
};
}
示例10: getTimedByteLimitedHandler
import org.jnetpcap.packet.PcapPacketHandler; //导入依赖的package包/类
/**
* Initialization of the pseudo packet handler which measures the time to handle the configured amount of bytes.
*
* @param pcap the interface with the pcap library needed to construct the packet handler.
* @param MAX_BYTES the maximum amount of bytes captured
* @return the handler for the packets.
*/
private static PcapPacketHandler<PseudoPacketHandler> getTimedByteLimitedHandler(final Pcap pcap, final long MAX_BYTES) {
// Packet handler. Generic callback function for PseudoPacketHandler.
return new PcapPacketHandler<PseudoPacketHandler>() {
boolean started = false;
long packetBytes = 0;
long startTime;
long stopTime;
// A timer for performance tests.
final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
// Handle nextPacket callback.
public void nextPacket(PcapPacket packet, PseudoPacketHandler handler) {
if (!started) {
startTime = threadMXBean.getCurrentThreadCpuTime();
logger.info("Started at "+ startTime);
started = true;
}
handler.handle(packet);
packetBytes+= packet.size();
if (packetBytes >= MAX_BYTES) {
stopTime = threadMXBean.getCurrentThreadCpuTime();
pcap.breakloop();
logger.info("Stopped after " + ((stopTime - startTime)/10000000000.0) + " seconds and " + packetBytes + " bytes.");
}
}
};
}
示例11: dumpFiles
import org.jnetpcap.packet.PcapPacketHandler; //导入依赖的package包/类
@Test
public void dumpFiles() throws IOException, InvalidKeyException {
final StringBuilder errbuf = new StringBuilder(); // For any error msgs
final String file = _testdir + File.separator + "56packets.pcap";
final Pcap pcap = Pcap.openOffline(file, errbuf);
PcapPacketHandler<PseudoPacketHandler> dumpHandler = new PcapPacketHandler<PseudoPacketHandler>() {
public void nextPacket(PcapPacket packet, PseudoPacketHandler handler) {
handler.handle(packet);
}
};
if (pcap == null) {
System.out.printf("Error while opening device for capture: "
+ errbuf.toString());
}
final FramePseudonymizerBuilder builder = new FramePseudonymizerBuilder();
builder.pseudoIPv4("30313233343536373839414243444546", 16);
final FramePseudonymizer pseudonymizer = builder.build();
final String destination = "pseudo-capture-file.cap";
final PEFPseudonymizeDumper pseudonymizeDumper = new PEFPseudonymizeDumper(pcap, destination, pseudonymizer);
pcap.loop(56, dumpHandler, pseudonymizeDumper);
pseudonymizeDumper.close();
pcap.close();
Assert.assertFalse("The files are not identical!", FileUtils.contentEquals(new File(file), new File(destination)));
}
示例12: readOfflineFiles
import org.jnetpcap.packet.PcapPacketHandler; //导入依赖的package包/类
/**
* Opens the offline Pcap-formatted file.
*
* @return PcapPacketArrayList List of packets in the file
* @throws PcapException Facing any erro in opening the file
*/
public PcapPacketArrayList readOfflineFiles() throws PcapException {
//First, setup error buffer and name for our file
final StringBuilder errbuf = new StringBuilder(); // For any error msgs
//Second ,open up the selected file using openOffline call
Pcap pcap = Pcap.openOffline(file, errbuf);
//Throw exception if it cannot open the file
if (pcap == null) {
throw new PcapException(errbuf.toString());
}
//Next, we create a packet handler which will receive packets from the libpcap loop.
PcapPacketHandler<PcapPacketArrayList> jpacketHandler = (packet, paketsList) -> paketsList.add(packet);
/***************************************************************************
* (From jNetPcap comments:)
* Fourth we enter the loop and tell it to capture unlimited packets. The loop
* method does a mapping of pcap.datalink() DLT value to JProtocol ID, which
* is needed by JScanner. The scanner scans the packet buffer and decodes
* the headers. The mapping is done automatically, although a variation on
* the loop method exists that allows the programmer to specify exactly
* which protocol ID to use as the data link type for this pcap interface.
**************************************************************************/
try {
PcapPacketArrayList packets = new PcapPacketArrayList();
pcap.loop(-1, jpacketHandler, packets);
return packets;
} finally {
pcap.close();
}
}
示例13: dispatch
import org.jnetpcap.packet.PcapPacketHandler; //导入依赖的package包/类
/**
* Dispatch.
*
* @param file
* the file
* @param handler
* the handler
*/
@SuppressWarnings("deprecation")
private void dispatch(String file, PcapPacketHandler<Counter> handler) {
pcap = open(file);
assertEquals(Pcap.OK, pcap.dispatch(Pcap.DISPATCH_BUFFER_FULL, handler,
COUNTER));
pcap.close();
pcap = null;
}
示例14: testDispatchIntIntPcapPacketHandlerOfTTLoop
import org.jnetpcap.packet.PcapPacketHandler; //导入依赖的package包/类
/**
* Test dispatch int int pcap packet handler of tt loop.
*/
public final void testDispatchIntIntPcapPacketHandlerOfTTLoop() {
for (int i = 0; i < COUNT; i++) {
loop(TEST_AFS, new PcapPacketHandler<Counter>() {
public void nextPacket(PcapPacket packet, Counter counter) {
counter.inc();
new PcapPacket(packet);
}
});
}
}
示例15: testDispatchIntIntPcapPacketHandlerOfTTDispatch
import org.jnetpcap.packet.PcapPacketHandler; //导入依赖的package包/类
/**
* Test dispatch int int pcap packet handler of tt dispatch.
*/
public final void testDispatchIntIntPcapPacketHandlerOfTTDispatch() {
for (int i = 0; i < COUNT; i++) {
dispatch(TEST_AFS, new PcapPacketHandler<Counter>() {
public void nextPacket(PcapPacket packet, Counter counter) {
counter.inc();
new PcapPacket(packet);
}
});
}
}