本文整理汇总了Java中org.jnetpcap.Pcap.close方法的典型用法代码示例。如果您正苦于以下问题:Java Pcap.close方法的具体用法?Java Pcap.close怎么用?Java Pcap.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jnetpcap.Pcap
的用法示例。
在下文中一共展示了Pcap.close方法的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: openAnyDevice
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
@Test
public void openAnyDevice() {
List<PcapIf> devices = new ArrayList<PcapIf>();
StringBuilder err = new StringBuilder();
if (Pcap.findAllDevs(devices, err) != Pcap.OK || devices.isEmpty()) {
throw new RuntimeException("Failed to find network devices!");
}
Pcap pcap = null;
for (PcapIf dev : devices) {
if (dev.getName().equalsIgnoreCase(Constants.ANY_DEVICE)) {
pcap = Pcap.create(Constants.ANY_DEVICE, err);
}
}
if (pcap == null) {
throw new RuntimeException("Failed to open \""
+ Constants.ANY_DEVICE + "\" device!");
}
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: 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;
}
示例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: _testPcapCanSetRfMond
import org.jnetpcap.Pcap; //导入方法依赖的package包/类
public void _testPcapCanSetRfMond() throws SecurityException,
NoSuchMethodException {
List<PcapIf> alldevs = new ArrayList<PcapIf>();
StringBuilder errbuf = new StringBuilder();
Pcap.findAllDevs(alldevs, errbuf);
Pcap pcap = Pcap.create(alldevs.get(0).getName(), errbuf);
if (Pcap.isLoaded("canSetRfmon")) {
pcap.canSetRfmon();
}
pcap.close();
}
示例11: 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());
}
示例12: 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());
}
}
示例13: 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();
}
示例14: _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();
}
示例15: _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();
}