the Android DNS code is in Bionic/libc/netbsd
(Although NetBSD is an obsolete project, the DNS functional part of the code is used by Android)
NetBSD code obscure, only one side to write the program, while playing log verification
Copy a simple program, under Android with MMM compiled can get py_getaddr executable file, with ADB landed to devices and system/bin down to execute
[Email protected]:~/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 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_ad DR) (->SIN_ADDR), Ipstr,;p rintf ("%s\n", Ipstr);} Freeaddrinfo (answer); exit (0);}
Cat Android.mk local_path:= $ (call My-dir) include $ (clear_vars) local_src_files:= py_getaddr.clocal_module: = Py_ Getaddrlocal_static_libraries: = Libcutils libcinclude $ (build_executable)
To run Py_getadd Sohu.com, the calling process is as follows
_files_getaddrinfo is read/system/etc/hosts retrieval
_dns_getaddrinfo is to access the DNS server
when accessing the construction DNS access, how to find the DNS server, the original in the __res_get_state call back __res_state (This complex structure is defined in bionic/libc/private/resolv_private.h#__res_state )
__res_vinit uses the system setup 8.8.8.8 nameserver (Jeallybean code in bionic/libc/netbsd/resolv/res_init.c#339) during the call
Res_queryn then constructs the DNS query with Res_nmkquery and sends the query with Res_nsend, and gets the result returned.
The print results of the running program and log are as follows:
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
Android DNS Code Analysis