本文整理汇总了Java中org.jnetpcap.Pcap.loop方法的典型用法代码示例。如果您正苦于以下问题:Java Pcap.loop方法的具体用法?Java Pcap.loop怎么用?Java Pcap.loop使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jnetpcap.Pcap
的用法示例。
在下文中一共展示了Pcap.loop方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
示例2: 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);
}
示例3: 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();
}
示例4: handleWithMetal
import org.jnetpcap.Pcap; //导入方法依赖的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();
}
}
示例5: dumpFiles
import org.jnetpcap.Pcap; //导入方法依赖的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)));
}
示例6: readOfflineFiles
import org.jnetpcap.Pcap; //导入方法依赖的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();
}
}
示例7: readPacktsFromFile
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
public static ArrayList<byte[]> readPacktsFromFile(String filePath) {
final ArrayList<byte[]> packets = new ArrayList<byte[]>();
StringBuilder errbuf = new StringBuilder();
Pcap pcap = Pcap.openOffline(filePath, errbuf);
ByteBufferHandler<String> handler = new ByteBufferHandler<String>() {
public void nextPacket(PcapHeader arg0, ByteBuffer buffer, String arg2) {
byte[] b = new byte[buffer.capacity()];
buffer.get(b);
packets.add(b);
}
};
pcap.loop(Pcap.LOOP_INFINITE, handler, "");
return packets;
}
示例8: testScanFileJPHandler
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
/**
* Test scan file jp handler.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public void testScanFileJPHandler() throws IOException {
StringBuilder errbuf = new StringBuilder();
final Pcap pcap = Pcap.openOffline("tests/test-vlan.pcap", errbuf);
// long start = System.currentTimeMillis();
@SuppressWarnings("unused")
final TextFormatter out = new TextFormatter();
@SuppressWarnings("unused")
final JScanner scanner = new JScanner();
pcap.loop(Pcap.LOOP_INFINATE, JProtocol.ETHERNET_ID,
new JPacketHandler<String>() {
@SuppressWarnings("unused")
int i = 0;
public void nextPacket(JPacket packet, String user) {
// scanner.scan(packet, JProtocol.ETHERNET_ID);
// try {
// out.setFrameIndex(i++);
// out.format(packet);
// } catch (IOException e) {
// e.printStackTrace();
// }
}
}, "");
// long end = System.currentTimeMillis();
// System.out.printf("time=%d ms\n", (end - start));
pcap.close();
}
示例9: testStressTestPcapPacketHandler
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
/**
* Test stress test pcap packet handler.
*/
public void testStressTestPcapPacketHandler() {
String[] files = DIR.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".pcap");
}
});
for (int i = 0; i < COUNT / 10; i++) {
for (String fname : files) {
Pcap pcap = Pcap.openOffline(DIR.toString() + "/" + fname, errbuf);
assertNotNull(errbuf.toString(), pcap);
pcap.loop(Pcap.LOOP_INFINATE, new PcapPacketHandler<Pcap>() {
public void nextPacket(PcapPacket packet, Pcap user) {
assertNotNull(packet);
}
}, pcap);
pcap.close();
}
if ((i % 80) == 0) {
System.out.println();
}
System.out.printf(".");
System.out.flush();
}
System.out.println();
}
示例10: runCaptureLoop
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
protected void runCaptureLoop(final Pcap pcap, final StringBuilder err) throws IOException {
final ByteSequence2LongMap timestampMap = new KeyValueRingBuffer(CaptureSettings.RING_BUFFER_CAPACITY, CaptureSettings.MAX_CORRELATION_ID_LENGTH); // = new HashMapByteSequence2LongMap(bufferSize);
printSelectedSettings();
final LatencyCollector latencyCollector = createLatencyCollector(timestampMap);
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
System.err.println("Shutting down...");
if (!isClosed)
pcap.breakloop();
latencyCollector.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
});
LatencyMeterPacketHandler<T> handler = createLatencyMeterPacketHandler(latencyCollector, timestampMap);
try {
pcap.loop(-1, handler, null);
} finally {
pcap.close();
latencyCollector.close();
isClosed = true;
}
if (err.length() > 0)
System.err.println(err.toString());
}
示例11: transp
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
public void transp() {
try {
// jNetPcap �ʱ�ȭ
Pcap pcap = packetInit(btn_start);
// ��Ŷ �̺�Ʈ �ڵ鷯
PcapPacketHandler<String> jpacketHandler = new PcapPacketHandler<String>() {
public void nextPacket(PcapPacket packet, String user) {
Ip4 ip = new Ip4();
byte[] dIP = new byte[4], sIP = new byte[4];
if (packet.hasHeader(ip)) {
dIP = packet.getHeader(ip).destination();
sIP = packet.getHeader(ip).source();
} else {
return;
}
// ����� IP
String sourceIP = org.jnetpcap.packet.format.FormatUtils.ip(sIP);
// ������ IP
String destinationIP = org.jnetpcap.packet.format.FormatUtils.ip(dIP);
/**
* <pre>
* << �������� >>
* - ��� ��쿡���� ���� ��(��Ƽ or �δ�) ���� ������ �Է��� ���� ��Ŷ ������ �ݵ�� �´�.
* - ���� ������ ���� ��Ŷ�� ������ ���α��� ���� �ִٸ� ���� ��(��Ƽor�δ�)������ �ݵ�� ������ ���´�.
* - ���� Ȯ������ ������ ij���� ���� �����ϴ� ���� IP�� �� ���ڸ��� �ٸ��� �ϴ�.
* - �������� 37�̰�, �����Ҵ� 39, ��ij�� 38�̴� ��Ȯ�� ��Ģ�� �˱Ⱑ ��ƴ�.
* </pre>
*/
// ��Ŷ ������ �ν��Ͻ�
PacketSniffing sniiffer = new PacketSniffing(txtMsg);
// Ŭ�� -> ����
sniiffer.sendPacketToServer(packet, sourceIP, destinationIP);
// ���� -> Ŭ��
sniiffer.receivePacketToClient(packet, sourceIP, destinationIP);
}
};
// �ǽð����� ��Ŷ�� �����´�.
pcap.loop(-1, jpacketHandler, "jNetPcap rocks!");
pcap.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
示例12: testTcpIp4CRC16UsingHandler
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
/**
* Test tcp ip4 cr c16 using handler.
*/
public void testTcpIp4CRC16UsingHandler() {
StringBuilder errbuf = new StringBuilder();
Pcap pcap = Pcap.openOffline(TestUtils.HTTP, errbuf);
assertNotNull(errbuf.toString(), pcap);
pcap.loop(Pcap.LOOP_INFINATE, new PcapPacketHandler<String>() {
Ip4 ip = new Ip4();
Tcp tcp = new Tcp();
// public void nextPacket(PcapHeader header, JBuffer buffer, String
// user)
// {
public void nextPacket(PcapPacket packet, String user) {
if (packet.hasHeader(tcp) == false) {
return;
}
// PcapPacket packet = new PcapPacket(header, buffer);
long f = packet.getFrameNumber();
assertTrue("#" + f, packet.hasHeader(ip));
final int crc = Checksum.pseudoTcp(packet, ip.getOffset(),
tcp.getOffset());
if (crc != 0 && tcp.checksum() != crc) {
System.out.println(tcp);
System.out.printf("#%d: tcp.crc=%x computed=%x\n", f,
tcp.checksum(), crc);
// System.out.println(ip.toHexdump());
// System.out.println(tcp.toHexdump());
System.exit(0);
}
// assertEquals("Frame #" + f, tcp.checksum(), crc);
}
}, null);
}
示例13: _testScanFileBBHandler
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
/**
* _test scan file bb handler.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public void _testScanFileBBHandler() throws IOException {
StringBuilder errbuf = new StringBuilder();
final Pcap pcap = Pcap.openOffline("tests/test-l2tp.pcap", errbuf);
final JPacket packet = new PcapPacket(Type.POINTER);
final JScanner scanner = new JScanner();
long start = System.currentTimeMillis();
final TextFormatter out = new TextFormatter();
pcap.loop(Pcap.LOOP_INFINATE, new ByteBufferHandler<String>() {
int i = 0;
public void nextPacket(PcapHeader header, ByteBuffer buffer, String user) {
if (i == 200) {
pcap.breakloop();
return;
}
System.out.println("\nPacket #" + i);
try {
packet.peer(buffer);
} catch (PeeringException e) {
e.printStackTrace();
}
scanner.scan(packet, JProtocol.ETHERNET_ID);
// try {
out.setFrameIndex(i++);
// out.format(packet);
// System.out.println(packet.toString());
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
}, "");
long end = System.currentTimeMillis();
System.out.printf("time=%d ms\n", (end - start));
pcap.close();
}
示例14: _testScanFileJBHandler
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
/**
* _test scan file jb handler.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public void _testScanFileJBHandler() throws IOException {
StringBuilder errbuf = new StringBuilder();
final Pcap pcap = Pcap.openOffline("tests/test-l2tp.pcap", errbuf);
final JPacket packet = new PcapPacket(Type.POINTER);
final JScanner scanner = new JScanner();
long start = System.currentTimeMillis();
final TextFormatter out = new TextFormatter();
pcap.loop(Pcap.LOOP_INFINATE, new JBufferHandler<String>() {
int i = 0;
public void nextPacket(PcapHeader header, JBuffer buffer, String user) {
if (i == 200) {
pcap.breakloop();
return;
}
System.out.println("\nPacket #" + i);
packet.peer(buffer);
scanner.scan(packet, JProtocol.ETHERNET_ID);
// try {
out.setFrameIndex(i++);
// out.format(packet);
// System.out.println(packet.toString());
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
}, "");
long end = System.currentTimeMillis();
System.out.printf("time=%d ms\n", (end - start));
pcap.close();
}
示例15: getPackets
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
/**
* Opens the .pcap file which is defined in the preferences and parses all
* the packets that it contains.<br>
* The packet data is copied into the JPacket ArrayList.
*/
private void getPackets() {
StringBuilder errbuf = new StringBuilder();
boolean SDCardIsMounted = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
String sdPath = Environment.getExternalStorageDirectory().getPath();
// Opening the .pcap file.
/*
final Pcap parser = Pcap.openOffline(
"/mnt/sdcard/" + GlobalConstants.dirName + "/"
+ settings.getString("fileText", "diddler_capture.pcap"),
errbuf);
*/
String fname = sdPath ;
String suffix = "/";
if(!sdPath.endsWith(suffix)){
fname = sdPath + suffix;
}
fname = fname +
GlobalConstants.dirName + suffix +
settings.getString("fileText", "diddler_capture.pcap");
final Pcap parser = Pcap.openOffline(fname, errbuf);
// Creating a handler for packet capture.
JPacketHandler<String> handler = new JPacketHandler<String>() {
// Defining the action that will be performed each time a packet is
// read for the file.
@Override
public void nextPacket(JPacket packet, String user) {
packets.add(packet);
}
};
parser.loop(-1, handler, null);
parser.close();
}