tcpdump源码分析(3)——android系统移植(tcpdump,gdb,netstat)

在android系统中编译运行tcpdump有两个方法,一个是直接在android系统中进行源码编译;另一个是在x86系统上进行交叉编译,然后将编译生成的二进制文件复制到android系统中。本文采用第二种方法。

先编译libpcap,下载地址如下,其实是和tcpdump在同一页面上的:

http://www.tcpdump.org/release/libpcap-1.8.1.tar.gz

将下载得到的tcpdump和libpcap解压后放在同一个文件夹中。

先到libpcap文件夹中,执行如下编译安装命令

 (可能需要安装flex,命令:yum install flex bison)

#./configure --prefix=/root/androidtcpdump/tools --host=arm-linux --target=arm-linux CC=arm-none-linux-gnueabi-gcc --with-pcap=linux

            make && make install

接着编译tcpdump

./configure --prefix=/root/androidtcpdump/tools --host=arm-linux --target=arm-linux CC=arm-none-linux-gnueabi-gcc

make && make install

编译完成后会在~/androidtcpdump/tools/sbin目录下找到独立的可执行文件tcpdump

注意编译两者时需要指定相同的目录, 否则在编译tcpdump时需要指定libpcap的路径

            如果是动态编译,文件放到android系统后就会显示tcpdump: No such file or directory。需要静态编译,Makefile中加入-static参数。

            然后就可以抓包了。

1.1.1.1  编译netstat

关于netstat的源码包和常规编译,参考:

netstat工具源码剖析——网络诊断必备

修改makefile文件,

CC修改为如下:

CC      = arm-none-linux-gnueabi-gcc

增加-static参数,如下

netstat:        $(NET_LIB) netstat.o statistics.o

                $(CC) -static $(LDFLAGS) -o netstat netstat.o statistics.o $(NLIB) $(RESLIB)

            如果缺少if_strip.h和if_tc.h文件,可以将/usr/include/linux/if_strip.h路径上的文件复制到源码中的include中,然后将lib/tr.clib/strip.c中的头文件包含改成

//#include <linux/if_strip.h>

#include <if_strip.h>

…..

//#include <linux/if_tr.h>

#include <if_tr.h>

            #make netstat来完成编译即可。

1.1.1.2  移植gdb

源码下载地址:https://www.gnu.org/software/gdb/download/

            或者:https://ftp.gnu.org/gnu/gdb/

            下载解压后,进行编译

./configure --prefix=/root/androidtcpdump/tools --host=arm-linux --target=arm-none-linux-gnueabi CC=arm-none-linux-gnueabi-gcc AS=arm-none-linux-gnueabi-as AR=arm-none-linux-gnueabi-ar CXX=arm-none-linux-gnueabi-g++ LDFLAGS="-static"

             然后执行: make && make install ,会在 /root/androidtcpdump/tools/bin 文件夹中生产二进制文件,直接复制到 android 系统中就可以使用。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值