移植Dnsmasq到Android

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.