将dhcpdump移植到android平台

首先,介绍下dhcpdump这个工具,通过名字就可以看出它应该是和tcpdump类似的工具了,不过它主要是抓取dhcp相关的包,并解析为可读的字符串。输出结果如下:

 


 

然后,下载dhcpdump源码:http://www.mavetju.org/download/dhcpdump-1.8.tar.gz,将其解压到dhcpdump目录中,在其中编写Android.mk文件:

 

 

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

 

LOCAL_SRC_FILES:=\

dhcpdump.c

 

LOCAL_CFLAGS := -O2 -g

LOCAL_CFLAGS += -D__FAVOR_BSD

 

 

LOCAL_C_INCLUDES += \

bionic/libc/include\

external/libpcap

 

LOCAL_STATIC_LIBRARIES += libpcap

 

LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)

 

LOCAL_MODULE_TAGS := eng

 

LOCAL_MODULE := dhcpdump

 

include $(BUILD_EXECUTABLE)

 

这其中有几点需要注意:

1、在android的源码环境中搜索ethernet.h文件(是位于net目录下的那个),然后在dhcpdump源码目录新建net目录,然后将ethernet.h拷贝到net目录

 

2、需要添加LOCAL_CFLAGS += -D__FAVOR_BSD 宏定义,否则报错

 

external/dhcpdump/dhcpdump.c:167: error: 'struct udphdr' has no member named 'uh_ulen'

3、需要定义c头文件位置,因为使用到了pcap以及net等相关头文件:

LOCAL_C_INCLUDES += \

bionic/libc/include\

external/libpcap

 

4、指定所使用的静态库:

LOCAL_STATIC_LIBRARIES += libpcap

 

5、修改源码,添加两个函数,否则报错:

 

external/dhcpdump/dhcpdump.c:161: error: undefined reference to 'ether_ntoa'

函数定义如下:
char *怪蜀黍
ether_ntoa_r (const struct ether_addr *addr, char *buf)
{
  sprintf (buf, "%x:%x:%x:%x:%x:%x",
       addr->ether_addr_octet[0], addr->ether_addr_octet[1],
       addr->ether_addr_octet[2], addr->ether_addr_octet[3],
       addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
  return buf;
}

char *
ether_ntoa (const struct ether_addr *addr)
{
  static char asc[18];

  return ether_ntoa_r (addr, asc);
}

6、在LOCAL_SRC_FILES中不能包含strsep.c文件,否则报错:

 

multiple definition of 'strsep'

最后,到android的源码根目录,如system,执行如下脚本:
$ .  build/envsetup 
$ cd external/dhcpdump
$ mm
输出如下:
Install: out/target/product/generic/system/xbin/dhcpdump
make:离开目录“/home/lql/android/system”
至此,dhcpdump编译成功。
开始测试,可以从上面的目录中将dhcpdump拷贝到android手机的/system/xbin/目录下,并修改权限为可执行。
将手机和电脑连接,cmd,输入adb shell进入shell窗口,
执行dhcpdump -i eth0
当系统开始获取ip地址时,上面的命令即抓取对应包,并解析为可读的字符串,如最开始的截图。
posted @ 2011-10-12 11:28  ctou45  阅读(405)  评论(0编辑  收藏  举报