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的管理, 還需要修改代碼, 就不在這裡介紹了.