`
aswang
  • 浏览: 838406 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

在android平台上编译libpcap-0.9.8 和 jnetpcap-1.2.rc1

 
阅读更多

1、下载源代码

$git clone git://android.git.kernel.org/platform/external/libpcap.git

(从android官方源码站点下载libpcap代码,版本为0.9.8)

 

之前在网上查到jnetpcap1.2版本支持libpcap-0.9.8,所以下载jnetpcap-1.2.rc1.zip

http://sourceforge.net/projects/jnetpcap/files/jnetpcap/1.2/stable/

$wget http://ncu.dl.sourceforge.net/project/jnetpcap/jnetpcap/1.2/stable/jnetpcap-1.2.rc1.zip

 

2、在android的ndk目录下,新建一个目录jnetpcap1.2,其中包括子目录jni

将libpcap的源文件和jnetpcap-1.2.rc1.zip里面的c源文件拷贝到jni目录(即将所有源文件放在同一个目录下)。

 

3、打开jni目录下的Android.mk文件(这个文件是libpcap源码里面带的),将其修改为如下内容:

 

 

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

 

LOCAL_SRC_FILES:=\

bpf_dump.c\

bpf/net/bpf_filter.c\

bpf_image.c\

etherent.c\

fad-gifc.c\

gencode.c\

grammar.c\

inet.c\

nametoaddr.c\

optimize.c\

pcap.c\

pcap-linux.c\

savefile.c\

scanner.c\

version.c

 

LOCAL_CFLAGS:=-O2 -g

LOCAL_CFLAGS+=-DHAVE_CONFIG_H -D_U_="__attribute__((unused))" -Dlinux -D__GLIBC__ -D_GNU_SOURCE

 

LOCAL_MODULE:= libpcap

include $(BUILD_STATIC_LIBRARY)

 

#LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

 

LOCAL_SRC_FILES:=\

jnetpcap.cpp\

jnetpcap_beta.cpp\

jnetpcap_bpf.cpp\

jnetpcap_dumper.cpp\

jnetpcap_ids.cpp\

jnetpcap_pcap_header.cpp\

jnetpcap_utils.cpp\

nio_jbuffer.cpp\

nio_jmemory.cpp\

nio_jnumber.cpp\

packet_jheader.cpp\

packet_jheader_scanner.cpp\

packet_jpacket.cpp\

packet_jscan.cpp\

packet_jsmall_scanner.cpp\

packet_protocol.cpp\

winpcap_ext.cpp\

winpcap_ids.cpp\

winpcap_send_queue.cpp\

winpcap_stat_ex.cpp

 

LOCAL_MODULE:= libjnetpcap

LOCAL_STATIC_LIBRARIES := libpcap

include $(BUILD_SHARED_LIBRARY)

 

 

4、切换到jni目录下,执行如下命令:

$ndk-build V=1 -B NDK_LOG=1

(后面加参数是为里输出日志信息,便于挑错,-B是重新编译)

编译报错:



 意思是找不到对应的头文件。

 

因为jnetpcap 是通过jni对libpcap的封装,所以我们需要为jnetpcap里面的native方法生成头文件。

接下来,首先编译jnetpcap1.2源码中的java源码,得到对应的class文件;

然后,在class目录下,通过javah -classpath . -jni org.jnetpcap.Pcap命令 来为class生成对应的.h头文件

最后,将生成的.h文件全部拷贝到jni目录下,重新编译。编译过程中会出现很多警告,影响不大。

但是结果还是报错了:



上面这个问题,最后是google 解决的,在此谢过:http://comments.gmane.org/gmane.comp.handhelds.android.ndk/4066

 

在 grammar.y 和 grammar.c文件中,注释掉以下内容:

//#ifndef YYBISON
int yyparse(void);

int
pcap_parse()
{
    return (yyparse());
}
//#endif

 

同时在scanner.c和scanner.l中注释掉以下内容:

//#define yylval pcap_lval

 

然后重新运行,$NDK/ndk-build V=1 -B NDK_LOG=1

如果能够看到如下输出信息,则说明编译成功。


 

编译成功后,会在jnetpcap1.2目录下自动创建一个libs目录,在该目录下包含一个子目录armeabi,在这个目录下包含编译生成的共享库:libjnetpcap.so

在java代码中就可以如下来加载这个共享库:

 

System.loadLibrary("jnetpcap");

 

至此,编译工作已完成。

 

使用类似的方法,可以编译成功jnetpcap1.2.r5,但是在编译jnetpcap1.3.0-1会出问题,后面会将编译jnetpcap1.3.0-1的过程写下来。

 

 

 

 

 

 

 

  • 大小: 5 KB
  • 大小: 3.8 KB
  • 大小: 3.4 KB
5
6
分享到:
评论
11 楼 aswang 2015-01-20  
dwjmantou 写道
不知道博主是不是真的通过jni抓包了,反正我是成功不了,一直是找不到网卡列表。原因还是root权限获取的问题,现在只能通过shell提权为root然后开启子进程抓包,这样的话就是两个程序了。后来通过tcpdump抓包,jnetpcap分析的。本来当时想留一个脚印,网站说我注册不到一天不让评论,所以在这里谢了一个blog。
http://blog.csdn.net/dwjmantou/article/details/41984035
因为貌似有点多。另外,看楼上部分同学.h文件的问题,那个貌似好几十个头文件都要生成,我当时记得抄了网上写的一个程序批量生成的,几分钟就能搞定。




这篇文章写的很早了, 当时是在手机上测试通过了才发出来的。现在就不太清楚能不能执行了,毕竟android版本已经更新了好几个版本了。

简单的方法, 就直接使用支持android的tcpdump来抓包,然后使用它进行离线解析。

10 楼 dwjmantou 2015-01-04  
不知道博主是不是真的通过jni抓包了,反正我是成功不了,一直是找不到网卡列表。原因还是root权限获取的问题,现在只能通过shell提权为root然后开启子进程抓包,这样的话就是两个程序了。后来通过tcpdump抓包,jnetpcap分析的。本来当时想留一个脚印,网站说我注册不到一天不让评论,所以在这里谢了一个blog。
http://blog.csdn.net/dwjmantou/article/details/41984035
因为貌似有点多。另外,看楼上部分同学.h文件的问题,那个貌似好几十个头文件都要生成,我当时记得抄了网上写的一个程序批量生成的,几分钟就能搞定。
9 楼 alpc92 2013-12-03  
请问已经编译好的.so文件该怎么用呢???
8 楼 alpc92 2013-12-02  
请问这是什么问题啊??

error: cannot access org.jnetpcap.packet.header.Ip40
class file for org.jnetpcap.packet.header.Ip40 not found
javadoc: error - Class org.jnetpcap.packet.header.Ip40 not found.
Error: No classes were specified on the command line.  Try -help.
Zhous-MacBook-Pro:bin zhouyunfeng$ javah -classpath . -jni org.jnetpcap.packet.header.Ip4$11
error: cannot access org.jnetpcap.packet.header.Ip41
class file for org.jnetpcap.packet.header.Ip41 not found
javadoc: error - Class org.jnetpcap.packet.header.Ip41 not found.
Error: No classes were specified on the command line.  Try -help.
Zhous-MacBook-Pro:bin zhouyunfeng$ javah -classpath . -jni org.jnetpcap.packet.header.Ip4$12
error: cannot access org.jnetpcap.packet.header.Ip42
class file for org.jnetpcap.packet.header.Ip42 not found
javadoc: error - Class org.jnetpcap.packet.header.Ip42 not found.
Error: No classes were specified on the command line.  Try -help.
Zhous-MacBook-Pro:bin zhouyunfeng$ javah -classpath . -jni org.jnetpcap.packet.header.Ip4$13
error: cannot access org.jnetpcap.packet.header.Ip43
class file for org.jnetpcap.packet.header.Ip43 not found
javadoc: error - Class org.jnetpcap.packet.header.Ip43 not found.
Error: No classes were specified on the command line.  Try -help.
Zhous-MacBook-Pro:bin zhouyunfeng$ javah -classpath . -jni org.jnetpcap.packet.header.Ip4$14
error: cannot access org.jnetpcap.packet.header.Ip44
class file for org.jnetpcap.packet.header.Ip44 not found
javadoc: error - Class org.jnetpcap.packet.header.Ip44 not found.
Error: No classes were specified on the command line.  Try -help.
Zhous-MacBook-Pro:bin zhouyunfeng$ javah -classpath . -jni org.jnetpcap.packet.header.Ip4$15
error: cannot access org.jnetpcap.packet.header.Ip45
class file for org.jnetpcap.packet.header.Ip45 not found
javadoc: error - Class org.jnetpcap.packet.header.Ip45 not found.
Error: No classes were specified on the command line.  Try -help.
Zhous-MacBook-Pro:bin zhouyunfeng$ javah -classpath . -jni org.jnetpcap.packet.header.Ip4$16
error: cannot access org.jnetpcap.packet.header.Ip46
class file for org.jnetpcap.packet.header.Ip46 not found
javadoc: error - Class org.jnetpcap.packet.header.Ip46 not found.
Error: No classes were specified on the command line.  Try -help.
Zhous-MacBook-Pro:bin zhouyunfeng$ javah -classpath . -jni org.jnetpcap.packet.header.Ip4$17
error: cannot access org.jnetpcap.packet.header.Ip47
class file for org.jnetpcap.packet.header.Ip47 not found
javadoc: error - Class org.jnetpcap.packet.header.Ip47 not found.
Error: No classes were specified on the command line.  Try -help.
Zhous-MacBook-Pro:bin zhouyunfeng$ javah -classpath . -jni org.jnetpcap.packet.heajavah -classpath -classpath . -jni org.jnetpcap.packet.header.Ip4$19
Error: No classpath was specified on the command line.  Try -help.
Zhous-MacBook-Pro:bin zhouyunfeng$ javah -classpath . -jni org.jnetpcap.packet.header.Ip4$20
error: cannot access org.jnetpcap.packet.header.Ip40
class file for org.jnetpcap.packet.header.Ip40 not found
javadoc: error - Class org.jnetpcap.packet.header.Ip40 not found.
Error: No classes were specified on the command line.  Try -help.
Zhous-MacBook-Pro:bin zhouyunfeng$ javah -classpath . -jni org.jnetpcap.packet.header.Ip4$10
error: cannot access org.jnetpcap.packet.header.Ip40
class file for org.jnetpcap.packet.header.Ip40 not found
javadoc: error - Class org.jnetpcap.packet.header.Ip40 not found.
Error: No classes were specified on the command line.  Try -help.
Zhous-MacBook-Pro:bin zhouyunfeng$ javah -classpath -help
Error: No classpath was specified on the command line.  Try -help.
Zhous-MacBook-Pro:bin zhouyunfeng$ javah -help
7 楼 coswh4 2012-08-16  
No JNI_OnLoad found in /data/data/com.Pcap/lib/libjnetpcap.so 0x40514e88, skipping init
这是怎么回事?

能否发我一份你编译好的.so文件到我邮箱,我的邮箱 qqqwh@126.com
6 楼 rohsuton 2012-08-10  
能否把要用到jnetpcap1.2+libpcap-0.9.8源码发一份给我,官网访问不了
rohsuton@gmail.com
5 楼 metallee 2012-07-04  
aswang 写道



你得确保你的 库 成功被加载。
可以看看logcat的日志信息,里面包含有加载.so库的信息


07-04 08:56:34.082: D/dalvikvm(337): No JNI_OnLoad found in /data/data/org.jnetpcap.Pcap/lib/libjnetpcap.so 0x40514e88, skipping init

还真是,没加载成功,郁闷了,第一次搞NDK,新手。。。。。

能否发我一份你编译好的.so文件到我邮箱,我的邮箱 metallee@sina.com


我的qq 21953354
4 楼 aswang 2012-07-02  
metallee 写道
你好,我最近也在做安卓上的抓包程序,按照你的教程成功编译了libjnetpcap.so文件.System.loadLibrary("jnetpcap"); 也成功了.但是现在在调用函数的时候出了问题.

我是参照jnetpcap.cpp 找到里面的Java_org_jnetpcap_Pcap_libVersion这个函数做测试.代码如下     private native String libVersion();

但是执行libVersion时程序报错,现在不知道问题出在哪,麻烦您指教下.这个问题困扰我3天了,谢谢了.



你得确保你的 库 成功被加载。
可以看看logcat的日志信息,里面包含有加载.so库的信息
3 楼 metallee 2012-07-02  
你好,我最近也在做安卓上的抓包程序,按照你的教程成功编译了libjnetpcap.so文件.System.loadLibrary("jnetpcap"); 也成功了.但是现在在调用函数的时候出了问题.

我是参照jnetpcap.cpp 找到里面的Java_org_jnetpcap_Pcap_libVersion这个函数做测试.代码如下     private native String libVersion();

但是执行libVersion时程序报错,现在不知道问题出在哪,麻烦您指教下.这个问题困扰我3天了,谢谢了.
2 楼 aswang 2011-11-04  
damtou 写道
大哥你好 你能把你编译过的so文件发给我吗 377296294@.qq.com 还有 我根据你说的 我在编译class文件成H文件是出现了一些错误
结果只生成了一个H文件 请问这是什么问题 结果无法生成so文件

希望大哥看到留言后 能给个回复 再次感谢



编译class之后,通过javah命令只会生成对应的头文件,而不会生成其它文件。
然后,你根据头文件去编写相应的C或者C++实现,最后通过ndk将其编译为.so库文件,再导入到android项目使用。
1 楼 damtou 2011-11-04  
大哥你好 你能把你编译过的so文件发给我吗 377296294@.qq.com 还有 我根据你说的 我在编译class文件成H文件是出现了一些错误
结果只生成了一个H文件 请问这是什么问题 结果无法生成so文件

希望大哥看到留言后 能给个回复 再次感谢

相关推荐

Global site tag (gtag.js) - Google Analytics