移植Dnsmasq到Android

 

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章