Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android DNS 代碼分析

Android DNS 代碼分析

編輯:關於Android編程

Android DNS 代碼都在bionic/libc/netbsd中

(雖然netbsd 是個廢棄的項目,但dns功能部分代碼被 Android用上了)

netbsd 代碼晦澀難懂,只有一邊寫程序,一邊打log驗證


照抄了一個簡單程序, 在android 下面用 mmm 編譯可以得到 py_getaddr 可執行文件, 用adb 登陸到 devices 再到 system/bin 下去執行

payne@payne-desktop:~/njb/getaddr$ cat py_getaddr.c 
#include 
#include 
#include 
#include 
#include 
#include 
int main(int argc, char **argv)
{
if (argc != 2) {
fprintf(stderr, "Usage: %s hostname\n",
argv[1]);
exit(1);   
}

struct addrinfo *answer, hint, *curr;
char ipstr[16];   
bzero(&hint, sizeof(hint));
hint.ai_family = AF_INET;
hint.ai_socktype = SOCK_STREAM;

int ret = getaddrinfo(argv[1], NULL, &hint, &answer);
if (ret != 0) {
fprintf(stderr,"getaddrinfo: &s\n",
gai_strerror(ret));
exit(1);
}

for (curr = answer; curr != NULL; curr = curr->ai_next) {
inet_ntop(AF_INET,
&(((struct sockaddr_in *)(curr->ai_addr))->sin_addr),
ipstr, 16);
printf("%s\n", ipstr);
}

freeaddrinfo(answer);
exit(0);
}


cat Android.mk 
LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES:= py_getaddr.c
LOCAL_MODULE := py_getaddr
LOCAL_STATIC_LIBRARIES := libcutils libc
include $(BUILD_EXECUTABLE)

到運行 py_getadd sohu.com 的時候, 調用過程如下

\


_files_getaddrinfo 是讀取/system/etc/hosts 檢索<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+X2Ruc19nZXRhZGRyaW5mbyDKx7fDzspkbnMgc2VydmVyPC9wPgo8cD7U2rfDzsq5udTsIGRucyC3w87KtcTKsbryo6zI57rO1dK1vSBkbnMgc2VydmVyLCDUrcC01NogX19yZXNfZ2V0X3N0YXRlICC199PDt7W72CBfX3Jlc19zdGF0ZSAgKNXiuPa4tNTTtcS94bm5tqjS5dTaICBiaW9uaWMvbGliYy9wcml2YXRlL3Jlc29sdl9wcml2YXRlLmgjX19yZXNfc3RhdGUgKTwvcD4KPHA+1Nq199PDuf2zzNbQIF9fcmVzX3Zpbml0IMq508PBy8+1zbPJ6NbDtcQgOC44LjguOCBuYW1lc2VydmVyIKOoamVhbGx5YmVhbiC0+sLr1NpiaW9uaWMvbGliYy9uZXRic2QvcmVzb2x2L3Jlc19pbml0LmMjMzM5o6k8L3A+CjxwPjxicj4KPC9wPgo8cD7WrrrzcmVzX3F1ZXJ5TiDTwyByZXNfbm1rcXVlcnkgubnU7GRucyCy6dGvo6yyotPDIHJlc19uc2VuZCC3osvNsunRr6OssqK1w7W9t7W72L3hufuhozwvcD4KPHA+PGltZyBzcmM9"/uploadfile/Collfiles/20141126/20141126084453260.png" alt="">


運行程序和 log 的打印結果如下:

py_getaddr sohu.com
======================= _files_getaddrinfo
======================= _dns_getaddrinfo
=========== getaddrinfo cp 8.8.8.8, subf 53
================ res_searchN sohu.com
================ res_querydomainN name sohu.com, domain (null)
================ res_queryN sohu.com
;; res_nmkquery(QUERY, sohu.com, IN, A)
================getanswer sohu.com
220.181.90.240
123.125.116.19

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved