自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
DNS 解析器
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
DNS 解析器模块可保护用户免受 DNS 拦截和配置更新攻击,并改进了 DNS 解析的网络性能。此模块包含用于实现 DNS 桩解析器的代码,该解析器可将 www.google.com 等名称转换为 IP 地址(例如 2001:db8::1)。DNS 桩解析器支持 Java API 元素(如 InetAddress#getAllByName 和 Network#getAllByName)以及原生网络功能,且可发送和接收 DNS 查询以及缓存结果。
Android 10 中的变化
在搭载 Android 9 及更低版本的设备上,DNS 解析器代码分布在 Bionic 和 netd
上。DNS 查找操作集中在 netd
守护程序中,以便进行系统级缓存,而应用在 Bionic 中调用函数(例如 getaddrinfo
)。查询会通过 UNIX 套接字发送到 /dev/socket/dnsproxyd
,再到 netd
守护程序,该守护程序会解析请求并再次调用 getaddrinfo
以发出 DNS 查找请求,然后它会缓存结果以供其他应用使用。DNS 解析器实现主要包含在 bionic/libc/dns/
中,部分包含在 system/netd/server/dns
中。
Android 10 将 DNS 解析器代码移至 system/netd/resolv,
,将其转换为 C++,然后对代码进行翻新和重构。由于应用兼容性方面的原因,Bionic 中的代码继续存在,但系统不会再对其进行调用。以下源文件路径受到重构的影响:
bionic/libc/dns
system/netd/client
system/netd/server/dns
system/netd/server/DnsProxyListener
system/netd/server/ResolverController
system/netd/resolv
DNS 解析器模块(“com.android.resolv”)以 APEX 文件的形式提供,并由 netd
动态链接;但是 netd
不是依赖项,因为模块会直接提供本地套接字 /dev/socket/dnsproxyd
。解析器配置的 Binder 端点已从 netd
移至解析器,这意味着,系统服务可以直接调用解析器模块,无需通过 netd
。
DNS 解析器模块依赖于 libc
(Bionic) 并静态链接其依赖项;不需要使用其他库。
mDNS .local 解析
自 2021 年 11 月起,Android 解析器支持 mDNS .local 解析,后者可在 RFC 6762 中实现“5.1 单次模式多播 DNS 查询”,以便不加辨识地将标准 DNS 查询发送至 224.0.0.251:5353 或 [FF02::FB]:5353。只需使用以 *.local
结尾的主机名调用 getaddrinfo()
即可透明地支持 mDNS 解析。
mDNS .local 解析增强了 getaddrinfo()
获取地址的现有功能。如果设备支持 mDNS .local 解析,那么 getaddrinfo()
API 会将 mDNS 查询发送到 224.0.0.251:5353 或 [FF02::FB]:5353,并返回本地地址。如果设备不支持 mDNS .local 解析,那么 getaddrinfo()
API 方法会向 DNS 服务器发送 DNS 查询。
代码位于 AOSP 的 packages/modules/DnsResolver
中。用户可以保留其当前的 mDNS 设计来获取地址,或改用 getaddrinfo()
。此功能的行为类似于发送到 mDNS 多播地址的常规 DNS 查询。此功能对系统运行状况没有影响。
用户可以使用 adb shell ping6 HOSTNAME.local
命令,其中 HOSTNAME 是 LAN 中目标设备的主机名,例如 adb shell ping6 ipad.local
。
VPN 和移动网络连接不在 .local 解析范围内。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# DNS Resolver\n\nThe DNS Resolver module provides user protection for DNS interception\nand configuration update attacks and improved network performance for DNS\nresolutions. The module contains the code that implements the DNS stub\nresolver, which translates names such as **www.google.com** to IP\naddresses such as **2001:db8::1** . The DNS stub resolver backs\nJava API elements such as\n[InetAddress#getAllByName](https://developer.android.com/reference/java/net/InetAddress#getAllByName(java.lang.String)) and\n[Network#getAllByName](https://developer.android.com/reference/android/net/Network#getAllByName(java.lang.String)), as well as\n[native networking functions](https://developer.android.com/ndk/reference/group/networking), and implements sending and\nreceiving DNS queries and caching the results.\n\nChanges in Android 10\n---------------------\n\n\nOn devices running Android 9 and lower, the DNS resolver code is spread across\nBionic and `netd`. DNS lookups are centralized in the\n`netd` daemon to allow for system-wide caching, while apps\ncall functions (such as `getaddrinfo`) in Bionic. The query is sent\nover a UNIX socket to `/dev/socket/dnsproxyd` to the\n`netd` daemon, which parses the request and calls\n`getaddrinfo` again to issue DNS lookups, then caches the results\nso that other apps can use them. The DNS resolver implementation was mostly\ncontained in `bionic/libc/dns/` and partly in\n`system/netd/server/dns`.\n\n\nAndroid 10 moves the DNS resolver code to\n`system/netd/resolv,` converts it to C++, then modernizes and\nrefactors the code. The code in Bionic continues to exist for app\ncompatibility reasons, but is no longer called by the system. These source\npaths are affected by the refactoring:\n\n- `bionic/libc/dns`\n- `system/netd/client`\n- `system/netd/server/dns`\n- `system/netd/server/DnsProxyListener`\n- `system/netd/server/ResolverController`\n- `system/netd/resolv`\n\nFormat and dependencies\n-----------------------\n\n\nThe DNS Resolver module (\\`com.android.resolv\\`) is delivered as an\n[APEX](/docs/core/ota/apex) file and is dynamically linked by\n`netd`; however, `netd` is **not** a\ndependency as the module serves the local socket\n`/dev/socket/dnsproxyd` directly. The Binder endpoint for the\nresolver configuration was moved from `netd` to the resolver,\nmeaning that the system service can call directly into the resolver module\nwithout going through `netd`.\n\n\nThe DNS Resolver module depends on `libc` (Bionic) and\nstatically links its dependencies; no other libraries are required.\n\nmDNS .local resolution\n----------------------\n\nStarting from November 2021, Android resolver supports mDNS .local resolution, which implements\n\"5.1 One-Shot multicast DNS Queries\" in RFC 6762 to send standard DNS queries blindly to\n224.0.0.251:5353 or \\[FF02::FB\\]:5353. mDNS resolution is transparently supported\nby calling `getaddrinfo()` with a hostname ending in `*.local`.\n\nmDNS .local resolution augments the existing functionality of `getaddrinfo()`\nto get the addresses. If a device supports mDNS .local resolution, then the\n`getaddrinfo()` API sends mDNS queries to 224.0.0.251:5353 or \\[FF02::FB\\]:5353\nand returns the local addresses. If a device doesn't support mDNS .local\nresolution, then the `getaddrinfo()` API method sends a DNS query to the DNS\nserver.\n\nThe code is in AOSP, located in `packages/modules/DnsResolver`. Users can keep their\ncurrent mDNS design to get the addresses, or use `getaddrinfo()` instead. The behavior of\nthis feature is like a regular DNS query sent to the mDNS multicast addresses. This feature has no\nimpact on system health.\n\nUsers can use the command `adb shell ping6 `\u003cvar translate=\"no\"\u003eHOSTNAME\u003c/var\u003e`.local`,\nwhere \u003cvar translate=\"no\"\u003eHOSTNAME\u003c/var\u003e is the hostname of a target device on the LAN, for example,\n`adb shell ping6 ipad.local`.\n\nVPN and mobile data connections are excluded from .local resolution."]]