移植Dnsmasq到Android

2021年09月15日 阅读数:1
这篇文章主要向大家介绍移植Dnsmasq到Android,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

 

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

 

1. 目的 android

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

  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/ionic

 

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

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 设置编译环境ui

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

上一篇: DNS缓存dnsmasq配置
下一篇: 配置根网桥