What is the DNS

DNS stands for DomainNameSystem, the system used to name computers and network services organized into a domain hierarchy. A domain name is a string of words or abbreviations separated by dots. Each domain name corresponds to a unique IP address. There is a one-to-one correspondence between domain names and IP addresses on the Internet. DNS naming is used to search for computers and services using user-friendly names on TCP/IP networks such as the Internet. DNS is a core service of the Internet. It serves as a distributed database that maps domain names and IP addresses to each other.

When we make a web request, we first go through the DNS service, which converts the domain name into an IP address, and then establishes a connection through the IP address. The workflow of DNS is shown in the following figure.

What’s wrong with Local DNS

So, what’s wrong with traditional Local DNS? At present, there are mainly several problems:

  • Local DNS hijacking

Domain name hijacking is a kind of Internet attack. By attacking the domain name resolution server (DNS) or forging the DNS, the domain name of the target website is resolved to the wrong address so that users cannot access the target website.

An example of DNS hijacking is shown below:

DNS hijacking will lead to network connection failure or DNS resolution failure on the end, which seriously affects users. Before, the end used the mechanism of alternate domain name to solve this problem, but the effect was not very good.

  • The DNS resolution is slow. Procedure

DNS resolution is classified into recursive query and iterative query.

Recursive query: If the local DNS server queried by the host does not know the IP address of the domain name to be queried, the local DNS server sends query packets to other root DNS servers as DNS clients rather than allowing the host to perform further query by itself.

Iterative query: When the root DNS server receives an iterative query request packet from the local DNS server, it either gives the IP address to be queried or tells the local DNS server which DNS server to query next. Then let the local DNS server do subsequent queries instead of doing subsequent queries for the local DNS server.

Most DNS servers use iterative mode because recursive mode can result in heavy traffic.

Due to the complexity of the network environment on the end, the DNS resolution process is complicated. In some scenarios, DNS resolution takes hundreds of milliseconds, which is very slow for a network request.

What is a HttpDns

HTTPDNS is a domain name resolution product for mobile developers, featuring anti-hijacking, precise scheduling, and other features.

Currently, both Tencent and Alibaba have their own HTTPDNS solutions. HTTPDNS has the following features:

  1. Security: HTTPDNS uses HTTP or HTTPS for direct IP connection resolution, bypassing carrier’s Local DNS and avoiding domain name hijacking
  2. Fast: The hotspot domain name is resolved in advance through the pre-resolution mechanism. The hotspot domain name is directly obtained from the cache during network connection

Android HttpDns Access Guide

In the Aliyun HTTPDNS document, there are so-called “best solutions”, including SNI scenarios, OkHttp scenarios and so on, but for us, the access volume is still too large, and the coverage of scenarios is still limited. If we want easy access and full Java scenario coverage, how do we do it? First, let’s take a look at the call flow of DNS resolution on Android, using API 28 as an example.

InetAddress#getAllByName()
	->Inet6AddressImpl#lookupHostByName()
    	->Libcore.os.android_getaddrinfo()
Copy the code

Libcore code is Libcore source code:

public final class Libcore {
    private Libcore(a) {}/**
     * Direct access to syscalls. Code should strongly prefer using {@link #os}
     * unless it has a strong reason to bypass the helpful checks/guards that it
     * provides.
     */
    public static Os rawOs = new Linux();
    /** * Access to syscalls with helpful checks/guards. */
    public static Os os = new BlockGuardOs(rawOs);
}
Copy the code

A simple code trace shows that they implement the Os interface. Interface? Yeah, dynamic proxy. Yes, we can hook DNS resolution requests initiated by the Java layer through dynamic proxy.

The above code has been open source in KidDNS-Android

Next up

How to use the Hide API on Android P?

Welcome to pay attention to my personal wechat public account Andorid Small Teahouse! The main content is mobile terminal, mainly Android direction, technical exchange, open source analysis, architecture brief talk.