移植Dnsmasq到Android

本文详细介绍了如何将轻量级DNS代理和DHCP服务软件DNSSmasq移植到Android平台,以解决多网络设备DNS管理和手机作为WiFi AP时的网络参数分配问题。文章涵盖移植目的、移植准备、步骤、编译过程及测试流程,强调了DNSSmasq在Android上的应用潜力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Author: Codejoker    9/26/2009

 

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的管理, 还需要修改代码, 就不在这里介绍了.

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值