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

### 配置和使用DnsmasqAndroid设备 为了在Android设备上配置并使用Dnsmasq,通常需要借助支持自定义ROM或具有root权限的应用程序来实现。以下是具体方法: #### 安装Termux应用 安装Termux是一个不错的选择,因为这个应用程序允许用户在一个类似于Linux环境的终端模拟器中运行命令[^4]。 #### 获取Root权限(如果必要) 某些操作可能需要获取设备的超级用户访问权。这取决于所使用的特定版本以及制造商的安全策略。对于非root用户来说,部分功能可能会受到限制。 #### 下载编译好的二进制文件或者源码编译 由于直接从官方仓库下载最新版可能存在兼容性问题,在大多数情况下建议寻找已经为ARM架构预编译过的静态链接版本;也可以考虑自行编译适用于目标平台的可执行文件。 #### 设置Dnsmasq服务 一旦有了合适的二进制文件之后就可以按照常规方式设置Dnsmasq了。创建相应的配置文件夹并将所需的规则添加进去。例如强制解析特定域名为本地IP地址可以参照如下格式写入/etc/dnsmasq.conf 或者~/.config/dnsmasq.conf 文件内: ```bash # Add domains which you want to force to an IP address here. address=/example.com/192.168.1.100 ``` 启动服务前记得赋予适当读取权限给这些路径下的所有项目,并确保防火墙不会阻止必要的端口通信(默认监听53号TCP/UDP). #### 启动与管理进程 利用`nohup`命令可以让后台持续运行该守护程序直到手动停止它为止。同时还可以编写简单的脚本来简化日常维护工作比如重启网络接口使更改生效等。 ```bash nohup dnsmasq -C ~/.config/dnsmasq.conf & ``` 最后一步就是修改系统的DNS服务器指向localhost即127.0.0.1以便让所有的请求都能被拦截下来处理。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值