本文整理汇总了Java中org.jnetpcap.Pcap.OK属性的典型用法代码示例。如果您正苦于以下问题:Java Pcap.OK属性的具体用法?Java Pcap.OK怎么用?Java Pcap.OK使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类org.jnetpcap.Pcap
的用法示例。
在下文中一共展示了Pcap.OK属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: SKIPtestRemoteOpen
/**
* 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();
}
示例2: testWinPcapMainCommentCreateStr
/**
* 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());
}
示例3: openLive
/**
* 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);
}
示例4: openOffline
/**
* 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();
}
示例5: setFilter
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());
}
}
示例6: listDevices
public static String listDevices() {
String ret = "";
NativeLibraryLoader.loadNativeLibs();
List<PcapIf> devices = new ArrayList<PcapIf>();
StringBuilder err = new StringBuilder();
if (Pcap.findAllDevs(devices, err) != Pcap.OK || devices.isEmpty()) {
log.error("Failed to find network devices");
throw new RuntimeException("Failed to find network devices!");
}
for (PcapIf dev : devices) {
ret += dev.getName() + "\n";
}
return ret;
}
示例7: openAnyDevice
@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();
}
示例8: compile
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;
}
示例9: getAllPcapDevices
/**
* This function returns a list of all available <code>PcapIf</code>s.
*
* @param rescan
* @return
* @throws PcapException
*/
public static List<PcapIf> getAllPcapDevices(boolean rescan) throws PcapException {
if (rescan || allPcapDevices == null) {
allPcapDevices = new ArrayList<PcapIf>();
StringBuilder errbuf = new StringBuilder();
int r = Pcap.findAllDevs(allPcapDevices, errbuf);
if (r != Pcap.OK) {
throw new PcapException("Can't read list of devices: " + errbuf.toString());
}
}
return Collections.unmodifiableList(allPcapDevices);
}
示例10: setupFilter
private static void setupFilter(Pcap pcap, String expression) throws Exception {
PcapBpfProgram program = new PcapBpfProgram();
final int netmask = (int)Long.parseLong(CaptureSettings.FILTER_NETWORK_MASK_HEX.toLowerCase(), 16);
if (pcap.compile(program, expression, CaptureSettings.OPTIMIZE_FILTER ? 1 : 0, netmask) != Pcap.OK)
throw new Exception("Error compiling LIBPCAP filter: " + pcap.getErr());
if (pcap.setFilter(program) != Pcap.OK)
throw new Exception("Error setting LIBPCAP filter: " + pcap.getErr());
}
示例11: setFilter
/**
*
* Sets a filter for a given {@link Pcap} instance. The filterExpression is
* a String following the format as explained e.g. in PCAP-FILTER(7).
*
* @param pcap
* @param filterExpression
*/
public static void setFilter(Pcap pcap, String filterExpression) {
PcapBpfProgram filter = new PcapBpfProgram();
if (pcap.compile(filter, filterExpression, 1, 0) != Pcap.OK) {
log.error("Filter not compilable: " + pcap.getErr());
throw new RuntimeException("Failed to compile filter: "
+ pcap.getErr());
}
if (pcap.setFilter(filter) != Pcap.OK) {
log.error("Filter not settable: " + pcap.getErr());
throw new RuntimeException("Failed to set filter: " + pcap.getErr());
}
}
示例12: createAndActivatePcap
public static Pcap createAndActivatePcap(String device) {
NativeLibraryLoader.loadNativeLibs();
Pcap pcap = null;
List<PcapIf> devices = new ArrayList<PcapIf>();
StringBuilder err = new StringBuilder();
if (Pcap.findAllDevs(devices, err) != Pcap.OK || devices.isEmpty()) {
log.error("Failed to find network devices");
throw new RuntimeException("Failed to find network devices!");
}
for (PcapIf dev : devices) {
if (dev.getName().equalsIgnoreCase(device)) {
pcap = Pcap.create(device, err);
break;
}
}
if (pcap == null && devices.size() < 1) {
log.error("Failed to open device: " + device);
throw new RuntimeException("Failed to open \"" + device + "\" device!");
} else if (pcap == null) {
log.error("Failed to get requested device: " + device);
device = devices.get(0).getName();
log.error("Using first device found: " + device);
pcap = Pcap.create(device, err);
}
pcap.setSnaplen(Constants.SNAPLEN);
pcap.setPromisc(Constants.FLAGS);
pcap.setBufferSize(Constants.BUFFER_SIZE);
if (pcap.activate() != 0) {
log.error("Failed to activate device: " + device + " -> " + pcap.getErr());
throw new RuntimeException("Failed to activate \"" + device + "\" device!\n" + "Pcap error follows:\n" + pcap.getErr());
}
return pcap;
}
示例13: getDeviceList
@Test
public void getDeviceList() {
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!");
}
for (PcapIf dev : devices) {
System.out.println(dev.getName() + " " + dev.getDescription());
}
}
示例14: filterTcpSyn
@Test
public void filterTcpSyn() throws InterruptedException {
PcapBpfProgram filter = new PcapBpfProgram();
if (pcap.compile(filter, "tcp[tcpflags] & tcp-syn != 0", 1, 0) != Pcap.OK) {
throw new RuntimeException("Failed to compile filter: "
+ pcap.getErr());
}
if (pcap.setFilter(filter) != Pcap.OK) {
throw new RuntimeException("Failed to set filter: " + pcap.getErr());
}
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);
}
示例15: filterTcpFin
@Test
public void filterTcpFin() throws InterruptedException {
PcapBpfProgram filter = new PcapBpfProgram();
if (pcap.compile(filter, "tcp[tcpflags] & tcp-fin != 0", 1, 0) != Pcap.OK) {
throw new RuntimeException("Failed to compile filter: "
+ pcap.getErr());
}
if (pcap.setFilter(filter) != Pcap.OK) {
throw new RuntimeException("Failed to set filter: " + pcap.getErr());
}
Client client = new Client();
client.startClient(TestConstants.CAPTURE_DELAY);
/*
* Break the loop when we receive a TCP packet with the FIN 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_FIN()) {
pcap.breakloop();
}
}
}, null);
/*
* As we break the loop we check the return value if this was correctly
* done.
*/
assertEquals(Pcap.ERROR_BREAK, ret);
}