移植Dnsmasq到Android

来源:互联网 发布:精神发育迟缓症状知乎 编辑:程序博客网 时间:2024/06/09 18:15

DNSMAQS 是一款轻量级的,容易配置的DNS代理和DHCP服务软件,可以为一个小型的网络提供DNS服务(或者DHCP)服务. 本文将介绍如何把它移植到Android平台中.

 

1. 目的

  a. 当实现Multi-PDP的时候, 手机中会存在多个虚拟网络设备(网卡)分别连接不同网络, 而不同的网络可能会有不同的DNS服务器. 因此需要一个单独的DNS代理, 来统一管理DNS查询.

  b. 如果用手机做为WiFi的AP, 就需要手机通过DHCP服务来分配IP地址, 网关等网络参数.

  c. AT&T测试要求手机提供比较完整的DNS缓存机制, 而Android平台只提供的机制很简单. 具体可以查看代码中的注释:

bionic/libc/netbsd/resolv/res_cache.c 

It is only used to cache DNS answers for a maximum of CONFIG_SECONDS seconds
in order to reduce DNS traffic. It is not supposed to be a full DNS cache,
since we plan to implement that in the future in a dedicated process running
on the system.

Note that its design is kept simple very intentionally, i.e.:

- it takes raw DNS query packet data as input, and returns raw DNS
answer packet data as output

(this means that two similar queries that encode the DNS name
differently will be treated distinctly).

- the TTLs of answer RRs are ignored. our DNS resolver library does not use
them anyway, but it means that records with a TTL smaller than
CONFIG_SECONDS will be kept in the cache anyway.

this is bad, but we absolutely want to avoid parsing the answer packets
(and should be solved by the later full DNS cache process).

- the implementation is just a (query-data) => (answer-data) hash table
with a trivial least-recently-used expiration policy.

Doing this keeps the code simple and avoids to deal with a lot of things
that a full DNS cache is expected to do.

2. 移植准备 
  a. 下载Android源代码  
  b. 下载DNSMAQS源代码 (本文下载的是: dnsmasq-2.50.tar.gz)
    http://www.thekelleys.org.uk/dnsmasq/

 

3. 移植步骤 
  a. 把Dnsmaqs源代码目录解压到<android>/external/dnsmaqs目录中
  b. 为dnsmaqs编写Android.mk文件

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES := /
    src/bpf.c /
    src/cache.c/
    src/dbus.c /
    src/dhcp.c /
    src/dnsmasq.c /
    src/forward.c /
    src/helper.c /
    src/lease.c /
    src/log.c /
    src/netlink.c /
    src/network.c /
    src/option.c /
    src/rfc1035.c /
    src/rfc2131.c /
    src/tftp.c /
    src/util.c

LOCAL_C_INCLUDES := /
    bionic/libc/private / 
    $(LOCAL_PATH)/src/ 

LOCAL_CFLAGS := -DANDROID_CHANGE 

LOCAL_MODULE_PATH := $(TARGET_OUT_OPTIONAL_EXECUTABLES)
LOCAL_MODULE_TAGS := user
LOCAL_MODULE := dnsmasq

include $(BUILD_EXECUTABLE)

  c. 编译dnsmasq
    c.1 设置编译环境

#cd <android>
#source build/envsetup.sh
#tapas

    c.2 编译dnsmasq

#make dnsmasq

  d. 修正编译错误
    为了使修改的代码不会污染开源代码, 建议把修改用ANDROID_CHANGE宏括起来 , 并把该宏定义在Android.mk文件中.
    
4. 测试 
  a. 把编译生成的dnsmasq可执行文件上传到手机中

#adb push dnsmasq /tmp

  b. 激活手机的GPRS连接
  
  c. 修改system properties, 使应用的DNS请求发向127.0.0.1, 也就是dnsmasq

#setprop net.dns1 127.0.0.1
#setprop net.dns2 127.0.0.1

  d. 登陆手机测试DNS解析, 应该解析失败

#ping -c 2 g.cn

  e. 创建resolv.conf文件, 并上传到手机中/et/目录中

nameserver 221.130.33.52
nameserver 221.130.33.60

  f. 登陆手机, 并以root身份执行dnsmasq

#adb shell
#tmp/dnsmasq -d

  g. 再次测试dns解析, 应该解析成功

#ping -c 2 g.cn

  h. 测试结束
  
5. 总结: 
  a. dnsmasq的依赖比较少, 移植相对简单. 核心应该时Android.mk文件的编写, 和编译错误的修正.
  b. 以上只是移植的第一步, 如果要实现DNS的管理, 还需要修改代码, 就不在这里介绍了

原创粉丝点击