首先,介绍下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'
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'
6、在LOCAL_SRC_FILES中不能包含strsep.c文件,否则报错:
multiple definition of 'strsep'