Android DNS 代码分析
扫描二维码
随时随地手机看文章
Android DNS 代码都在bionic/libc/netbsd中
(虽然netbsd 是个废弃的项目,但dns功能部分代码被 Android用上了)
netbsd 代码晦涩难懂,只有一边写程序,一边打log验证
照抄了一个简单程序, 在android 下面用 mmm 编译可以得到 py_getaddr 可执行文件, 用adb 登陆到 devices 再到 system/bin 下去执行
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
payne @payne -desktop:~/njb/getaddr$ cat py_getaddr.c #include <stdio.h> #include <stdlib.h> #include <sys socket.h= "" > #include <netinet in.h= "" > #include <netdb.h> #include <string.h> int main( int argc, char **argv) { if (argc != 2 ) { fprintf(stderr, "Usage: %s hostnamen" , 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: &sn" , 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( "%sn" , ipstr); } freeaddrinfo(answer); exit( 0 ); }</string.h></netdb.h></netinet></sys></stdlib.h></stdio.h> |
?
1
2
3
4
5
6
7
8
9
|
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 检索<喎�"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+X2Ruc19nZXRhZGRyaW5mbyDKx7fDzspkbnMgc2VydmVyPC9wPgo8cD7U2rfDzsq5udTsIGRucyC3w87KtcTKsbryo6zI57rO1dK1vSBkbnMgc2VydmVyLCDUrcC01NogX19yZXNfZ2V0X3N0YXRlICC199PDt7W72CBfX3Jlc19zdGF0ZSAgKNXiuPa4tNTTtcS94bm5tqjS5dTaICBiaW9uaWMvbGliYy9wcml2YXRlL3Jlc29sdl9wcml2YXRlLmgjX19yZXNfc3RhdGUgKTwvcD4KPHA+1Nq199PDuf2zzNbQIF9fcmVzX3Zpbml0IMq508PBy8+1zbPJ6NbDtcQgOC44LjguOCBuYW1lc2VydmVyIKOoamVhbGx5YmVhbiC0+sLr1NpiaW9uaWMvbGliYy9uZXRic2QvcmVzb2x2L3Jlc19pbml0LmMjMzM5o6k8L3A+CjxwPjxicj4KPC9wPgo8cD7WrrrzcmVzX3F1ZXJ5TiDTwyByZXNfbm1rcXVlcnkgubnU7GRucyCy6dGvo6yyotPDIHJlc19uc2VuZCC3osvNsunRr6OssqK1w7W9t7W72L3hufuhozwvcD4KPHA+PGltZyBzcmM9"http://www.2cto.com/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