自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
DNS 解析器
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
DNS 解析器模組可針對 DNS 攔截和設定更新攻擊提供使用者保護,並改善 DNS 解析的網路效能。這個模組包含實作 DNS 存根解析器的程式碼,可將 www.google.com 等名稱轉換為 2001:db8::1 等 IP 位址。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
不是依附元件,因為模組會直接為本機 Socket 服務 /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。您可以呼叫 getaddrinfo()
並使用結尾為 *.local
的主機名稱,以便透明支援 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 是區域網路上目標裝置的主機名稱,例如 adb shell ping6 ipad.local
。
.local 解析會排除 VPN 和行動數據連線。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間: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"]],["上次更新時間: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."]]