简体   繁体   English

android使用pcap库

[英]android use pcap library

I have a general question according an android app, I need to use some pcap functionality in my android app. 我有一个Android应用程序的一般问题,我需要在我的Android应用程序中使用一些pcap功能。 Because java does not give the possibility in raw packet injections and low layer programming (as far as I know, pls correct me if I'm wrong) so I was looking for an alternative. 因为java不提供原始数据包注入和低层编程的可能性(据我所知,如果我错了请纠正我)所以我一直在寻找替代方案。 So far I found the following: 到目前为止我发现了以下内容:

  • ANDROID NDK ANDROID NDK
  • JNETPCAP JNETPCAP

Any suggestions which one I should use or does anyone have other suggestions? 我应该使用的任何建议或任何人都有其他建议吗?

The JNI Solution JNI解决方案

You need to wrap the calls and the logic you need out of libpcap in C or C++ and expose the underlying functions through JNI (Java Native Interface) so your application can call native code in Java. 您需要在C或C ++中用libpcap 包装所需的调用和逻辑,并通过JNI (Java Native Interface) 公开底层函数,以便您的应用程序可以用Java调用本机代码。

The documentation on JNI is pretty complete on internet, a lot of tutorials exists on this subject such as this one . 关于JNI的文档在互联网上非常完整,关于这个主题的许多教程都存在,比如这个

If you want to easily wrap native code in JNI you can use Swig which allow you to automatically generate JNI code based on your C/C++ native headers. 如果要在JNI中轻松包装本机代码,可以使用Swig ,它允许您根据C / C ++本Swig自动生成JNI代码。

The obtained JNI code should be compiled using the Android NDK as a dynamic library (.so). 获取的JNI代码应使用Android NDK作为动态库 (.so)进行编译。 This library is to be placed in your application package under libs/ . 该库将放在libs/下的应用程序包中。 You can then invoke System.loadLibrary(path_to_you_dynamic_library) to load all the symbols contained in the library and use them in Java. 然后,您可以调用System.loadLibrary(path_to_you_dynamic_library)来加载库中包含的所有符号,并在Java中使用它们。

Using a third-party library 使用第三方库

If you're afraid of getting headaches while figuring out how to use JNI, you can look at this library which does the hard work for you, and provides an API to manipulate raw sockets in Java. 如果您在弄清楚如何使用JNI时害怕头疼,可以查看这个为您工作的库,并提供一个API来操作Java中的原始套接字。

http://www.savarese.com/software/rocksaw/ http://www.savarese.com/software/rocksaw/

You need to wrap the calls and the logic you need out of libpcap in C or C++ and expose the underlying functions through JNI (Java Native Interface) so your application can call native code in Java. 您需要在C或C ++中用libpcap包装所需的调用和逻辑,并通过JNI(Java Native Interface)公开底层函数,以便您的应用程序可以用Java调用本机代码。

Or you need to get a library that's already done that, such as, err, umm, jNetPcap . 或者您需要获得已经完成的库,例如,err,umm, jNetPcap

One problem you may have with any attempt to do packet capture on Android - or any other OS using the Linux kernel - is that, by default, the underlying kernel mechanism used by libpcap ( PF_PACKET sockets) requires root privileges. 在Android上或使用Linux内核的任何其他操作系统上进行数据包捕获的任何尝试都可能遇到的一个问题是,默认情况下,libpcap( PF_PACKET套接字)使用的底层内核机制需要root权限。 If there's a way to run your code as root, or to give it CAP_NET_RAW and possibly CAP_NET_ADMIN privileges, it might be possible to make it work. 如果有一种方法可以以root身份运行代码,或者为其提供CAP_NET_RAWCAP_NET_ADMIN权限,则可以使其工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM