Android DNS Code Analysis

Source: Internet
Author: User
Tags call back

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.