本文整理汇总了Java中org.jnetpcap.Pcap.openOffline方法的典型用法代码示例。如果您正苦于以下问题:Java Pcap.openOffline方法的具体用法?Java Pcap.openOffline怎么用?Java Pcap.openOffline使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jnetpcap.Pcap
的用法示例。
在下文中一共展示了Pcap.openOffline方法的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: 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();
}
示例3: 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));
}
示例4: getOfflinePcap
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
private static final Pcap getOfflinePcap(final String inputFile) throws IOException {
final StringBuilder errbuf = new StringBuilder(); // For any error msgs
final Pcap pcap = Pcap.openOffline(inputFile, errbuf);
if (pcap == null) {
throw new IOException(errbuf.toString());
}
return pcap;
}
示例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: run
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
@Override
protected void run(String ... args) throws Exception {
super.run(args);
StringBuilder err = new StringBuilder();
Pcap pcap = Pcap.openOffline(captureFileName, err);
if (pcap == null)
throw new IllegalArgumentException(err.toString());
runCaptureLoop(pcap, err);
}
示例11: testIpChecksum
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
/**
* Test ip checksum.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public void testIpChecksum() throws IOException {
StringBuilder errbuf = new StringBuilder();
Pcap pcap = Pcap.openOffline(L2TP, errbuf);
if (pcap == null) {
throw new IOException(errbuf.toString());
}
assertTrue(pcap.loop(Pcap.LOOP_INFINATE, new PcapPacketHandler<Pcap>() {
Ip4 ip1 = new Ip4();
Ip4 ip2 = new Ip4();
int i = 0;
public void nextPacket(PcapPacket p1, Pcap user) {
i++;
p1.getHeader(ip1);
int c1 = ip1.calculateChecksum();
PcapPacket p2 = new PcapPacket(p1);
p2.getHeader(ip2);
int c2 = ip2.calculateChecksum();
if (c1 != c2) {
System.out.printf("#%d crc_before=%x crc_after=%x\n", i,
c1, c2);
System.out
.printf("P1: %s\nheader1=%s\n\nstate1=%s\npacket1=%s\n\nip1=%s\n",
p1.toHexdump(), p1.getCaptureHeader()
.toDebugString(), p1.getState()
.toDebugString(), p1
.toDebugString(), ip1
.toDebugString());
System.out.println("---------------------------");
System.out
.printf("P2: %s\nheader2=%s\n\nstate2=%s\npacket2=%s\n\nip2=%s\n\n",
p2.toHexdump(), p2.getCaptureHeader()
.toDebugString(), p2.getState()
.toDebugString(), p2
.toDebugString(), ip2
.toDebugString());
System.out.println("p1-p2.memory.diff=\n"
+ FormatUtils.hexdump(DataUtils.diff(p1, p2)));
System.out.println("ip1-ip2.memory.diff=\n"
+ FormatUtils.hexdump(DataUtils.diff(ip1, ip2)));
user.breakloop();
}
i++;
}
}, pcap) != -2);
pcap.close();
}
示例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: setup
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
public static boolean setup(String filename, int blockLength){
cancel(0);
cancel(1);
if (filename==null){
return false;
}
final StringBuilder errbuf = new StringBuilder();
Pcap pcap = Pcap.openOffline(filename,errbuf);
if (pcap==null){
return false;
}
detailsPacketPosition=0;
detailsPacket = new PcapPacket(JMemory.POINTER);
pcap.nextEx(detailsPacket);
referenceEpochTime=detailsPacket.getCaptureHeader().timestampInNanos();
pcap.close();
PcapFileLoader.blockLength=new IntHolder(blockLength);
PcapFileLoader.blockLower=(blockLength*65)/100;
PcapFileLoader.blockUpper=(blockLength*70)/100;
PcapFileLoader.filename=filename;
findLength();
packetListAdapter=null;
pcapPackets = new PcapPacket[2][blockLength];
for (int i=0;i<blockLength;++i){
pcapPackets[0][i]=null;
pcapPackets[1][i]=null;
}
PcapFileLoader.pcap[0]=null;
PcapFileLoader.pcap[1]=null;
notify[0]=false;
notify[1]=false;
indeterminate[0]=true;
indeterminate[1]=true;
range= new IntHolder[2][2];
range[0][0]=new IntHolder(-1);
range[1][0]=new IntHolder(-1);
range[0][1]=new IntHolder(0);
range[1][1]=new IntHolder(0);
Log.d("setup","true");
return true;
}