1 Linux DNS规范
Linux上并没有一个单独的方法可以完成DNS查询工作;没有一个有这样的明确接口的核心系统调用system call。不过,glibc (nss)的getaddrinfo (3), gethostbyname (3)等相关API (RFC3493)提供了DNS查询功能。
1)不支持nscd(name service cache daemon)服务,那么这2个函数接口通过文件/etc/resolv.conf获取DNS服务器的IP地址,然后通过socket通信发送请求到该DNS IP地址去获取待查询域名的IP地址。
2)支持nscd服务(or dnsmasq),这2个函数接口发送请求给nscd,nscd再返回响应给请求者。
3)ping uses glibc gethostbyname()。
2 Android netd DNS
2.1 Android 4.3 DNS
Android 4.3 incorporated a somewhat confusing series of commits under the heading of "dns cache per interface," which effectively causes all Bionic DNS requests to be proxied through netd.
ANDROID_DNS_MODE=local
dumpsys connectivity
dumpsys netd
ndc tether dns set
ndc tether dns list
iptables -t nat -I OUTPUT -p udp \
--dport 53 -j DNAT \
--to-destination 192.168.1.5:53
iptables -t nat -I OUTPUT -p tcp \
--dport 53 -j DNAT \
--to-destination 192.168.1.5:53
This works by intercepting the DNS requests meant for the remote nameserver and redirecting it to the local DNS server.
2.2 Android netd DNS
libcore/ojluni/src/main/java/java/net/InetAddress.java
netd中dns server设置文件:ResolverController.cpp
netd中域名解析文件:DnsProxyListener.cpp
Figure 2-1 Android netd DNS
2.3 libc DNS解析函数
Android bionic: based on OpenBSD, b means BSD
bionic/libc/dns/net/getaddrinfo.c
getaddrinfo(): IPv4 and IPv6
bionic/libc/dns/net/getnameinfo.c
getnameinfo()
bionic/libc/dns/net/gethnamaddr.c
gethostbyname(): only IPv4, gethostbyname_r(), r means reentry
通过netd获取DNS服务器的IP地址(Ubuntu通过进程dnsmasq和配置文件/etc/resolv.conf),然后构建DNS请求包,查询域名对应的IP地址,并且按照netId的分类缓存到netd中:
bionic/libc/dns/net/gethnamaddr.c
gethostbyname_internal_real()
2.4 Android三个网络基础库DNS解析
1) JDK HttpURLConnection
2) Apache HttpClient,Android 6.0之后不再支持HttpClient
3) OkHttp, third-party library
以上3个类库都会调用下面的JDK函数
InetAddress.getByName()
getaddrinfo() - libc DNS parser API
2.5 DHCP
DHCP Offer报文Options中包含了DNS Server地址
Android GB system/core/libnetutils/dhcp_utils.c
int dhcp_do_request(...,
in_addr_t *dns1, in_addr_t *dns2...)
3 Android netId
3.1 获取网络接口的netId
netd中将接口名(wlan0、eth0等)转化为netId的函数:
server/NetworkController.cpp
NetworkController::getNetworkForInterface(
const char* interface)
3.2 代码示例
server/CommandListener.cpp
函数CommandListener::NetworkCommand::runCommand()的最后添加如下的代码:
// 0 1 2
// network query_netid if_name
if (!strcmp(argv[1], "query_netid")) {
if (argc < 3) {
return syntaxError(client,
"Missing argument");
}
int _netId =
gCtls->netCtrl.getNetworkForInterface(
argv[2]);
char msg[16] = {0};
int retval = snprintf(msg, 15, "%d", _netId);
if (retval > 0) {
client->sendMsg(
ResponseCode::CommandOkay,
msg, false);
return 0;
} else {
return operationError(client,
"Can not get netId", retval);
}
}
用法:ndc network query_netid wlan0
4 ndc
4.1 查询可用命令表
ndc interface list
4.2 清除netd DNS缓存
ndc resolver flushif eth0
ndc resolver flushdefaultif -- flush default DNS resolver
Android 8.0没有导出刷新DNS缓存的接口,可以调用如下API添加。
bionic/libc/dns/resolv/res_cache.c
_resolv_flush_cache_for_net(netId)
4.3 设置netd DNS
ndc resolver setnetdns <netid> <domain> <dns1> <dns2> ...
ndc resolver setnetdns eth0 "" 8.8.8.8 192.168.1.1
ndc resolver setnetdns eth0 localdomain 8.8.8.8 192.168.1.1
5 Abbreviations
dnsmasq: DNS masquerade
mdnsd: multicast DNS,组播DNS
ndc:Native Daemon Connector
ojluni:Android libcore中的模块,包含OpenJDK、Language、Util、Net、IO