Repository navigation
プライベートDNSサーバーがIPv4クライアントに応答しない問題を修正 - #275
Merged
Merged
Conversation
SpoofAddrFactory collects a default IPv6 address at startup, skipping multicast, link-local and site-local ones. When no IPv6 address is left after that filtering, defaultAddr6 stays null and getSpoofAddr() dereferences it. The NPE propagates to the catch-all in PrivateDNSImp.run(), which calls finish() and returns, so the DNS server dies on the very first query while the GUI still shows it as enabled. Loopback ::1 normally passes the filter, so this is not reachable with default settings. It is reachable when the JVM is started with -Djava.net.preferIPv4Stack=true (no IPv6 address is enumerated at all) or when IPv6 is disabled at the OS level, for example with the Linux kernel parameter ipv6.disable=1. Return null instead of dereferencing. A null spoofing address reaches PrivateDnsResponseBuilder, which adds no answer record, so a AAAA query for a spoofed host is answered with NOERROR and no answer.
On the machine where this was found, new DatagramSocket(port, addr) produces an AF_INET6 dual-stack socket, and replies this socket sends to an IPv4 address on the LAN never arrive, although send() raises nothing. Replies to 127.0.0.1 do arrive, and so do replies from an AF_INET socket. Whether this comes from the OS handling of IPv4-mapped addresses or from the managed firewall on this machine is not established; the firewall is enforced by a profile and cannot be turned off for a test. Measured on macOS 15 (Darwin 24.6.0) with a minimal UDP echo server, querying from the same host: AF_INET6 dual-stack -> 127.0.0.1 reply received AF_INET6 dual-stack -> own LAN address no reply AF_INET only -> 127.0.0.1 reply received AF_INET only -> own LAN address reply received Open a separate socket per family so IPv4 clients are served by an AF_INET socket. DatagramChannel.open(INET6) does not set IPV6_V6ONLY, so whether the two can share a port is platform dependent: on macOS binding 0.0.0.0 and then :: succeeds, while the reverse order fails with EADDRINUSE. Rather than test the platform by name, try to split and decide from how the IPv6 open fails: - UnsupportedOperationException means the JVM or the OS has no IPv6, so keep the IPv4 socket and listen on IPv4 alone. This is the case under -Djava.net.preferIPv4Stack=true. - On any other exception, close the IPv4 socket and try a single dual-stack socket instead, which is what the code did before. If that fails too, the server does not start. The first two paths were checked on macOS. The fall back to a single dual-stack socket was not: it is meant to leave platforms that keep the whole port space for the dual-stack socket with the listening setup they have today, IPv6 included. Only a wildcard bind address is split. A specific IPv4 address gets an AF_INET socket, which is what the dual-stack socket already amounted to, and a specific IPv6 address gets an AF_INET6 socket. Each socket is served by its own thread, which moves the receive buffer out of the instance. The socket list is therefore fixed once built, shutdown is tracked with an AtomicBoolean, and finish() is synchronized so that a concurrent caller waits for every socket to be closed instead of letting restart() race the old sockets for the port. A channel that fails to bind is closed on the spot. s_soc, s_recvPacket, s_sendPacket, s_sAddr and dnsServer are dropped. They were assigned and never read; upstream resolution goes through PrivateDNSClient, which takes the server from ResolverConfig. s_soc also allocated an unused UDP socket per server instance.
funa-tk
approved these changes
Oct 22, 2026
funa-tk
left a comment
Contributor
There was a problem hiding this comment.
ありがとうございます。
マージさせていただきます!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
概要
プライベートDNSサーバーが、GUI上は有効になっているのに問い合わせに応答しない状態になる問題の修正です。
1. IPv4クライアントへの応答が届かない
AF_INET6で開いたソケットは、既定ではIPv6に加えてIPv4の通信も受け付けます。これをデュアルスタックソケットと呼びます。
IPv4のクライアントは、アドレスの先頭に
::ffff:が付いたIPv4-mappedアドレスとして見えます。この振る舞いはソケットオプション
IPV6_V6ONLYで切り替えられますが、Javaの標準のソケットオプションには含まれていません。再現環境では、従来の
DatagramSocketはAF_INET6のデュアルスタックソケットになります。このソケットでは、IPv4のLANアドレス宛ての応答が、
send()で例外が出ないにもかかわらず届きませんでした。一方、
127.0.0.1宛てとAF_INET専用ソケットでは応答を確認しました。OSのIPv4-mapped処理と、Firewallのどちらに起因するかは未特定です。
macOS 15(Darwin 24.6.0)で、最小のUDPエコーサーバーを立てて同一ホストから問い合わせた結果です。
ファミリごとにソケットを分けて、IPv4クライアントにはAF_INETのソケットで応答するようにしました。
受信はソケットごとに専用のスレッドで行います。
ただし
DatagramChannel.open(INET6)はIPV6_V6ONLYを立てないため、IPv4用とIPv6用のソケットが同一ポートを共有できるかはプラットフォームに依存します。macOSでは
0.0.0.0にbindしてから::にbindする順なら成功しますが、逆順ではEADDRINUSEで失敗します。::を先にbindした時点でIPv4側のポートまで確保されるため、続くIPv4のbindが衝突するからです。LinuxとWindowsではデュアルスタックソケットがポート空間全体を確保するため、どちらの順でも分割は失敗する見込みです。
OS毎に分岐させるのではなく、分割を試して、IPv6側がどの例外で失敗したかによって次の挙動を決める実装にしました。
DatagramChannel.open(INET6)がUnsupportedOperationExceptionを投げる場合は、JVMまたはOSにIPv6がありません。bindできたIPv4ソケットをそのまま使い、IPv4のみで待ち受けます。-Djava.net.preferIPv4Stack=trueを付けた場合がこれに該当しますbind()がEADDRINUSEで失敗する場合など、その他の例外ではIPv4ソケットを閉じ、単一のデュアルスタックソケットでの起動を試みます。これも失敗した場合は起動失敗とします条件ごとの構成です。
0.0.0.0:53と[::]:53)-Djava.net.preferIPv4Stack=trueでIPv6が使えない0.0.0.0:53)分割するのはワイルドカードを指定した場合だけです。
特定のIPv4アドレスを指定した場合はAF_INETのソケットになります。
デュアルスタックソケットも、特定のIPv4アドレスにbindすればそのアドレス宛てしか受け付けないので、受け付ける範囲は変わりません。
特定のIPv6アドレスを指定した場合はAF_INET6のソケットになります。
2. 自動選択できるIPv6アドレスがない環境での NullPointerException
書き換え先アドレスを「自動」にすると、
SpoofAddrFactoryが問い合わせ元に応じた書き換え先を選びます。このクラスは起動時にIPv6のデフォルトアドレスを集めますが、マルチキャスト、リンクローカル、サイトローカルを除外します。
フィルタリング後にIPv6アドレスが残らない場合、
defaultAddr6がnullのままになり、getSpoofAddr()がそれを参照します。nullに対してメソッドを呼ぶと
NullPointerExceptionが投げられます。この例外は
PrivateDNSImp.run()の汎用catchまで伝播し、そこでfinish()が呼ばれてreturnします。finish()はソケットを閉じるだけで、有効無効の状態は更新しません。状態を更新するのは
stop()だけなので、GUIは有効の表示を保ったままになります。最初の1問い合わせでサーバーが停止し、ログを見ないと気付けません。
JVMにオプションを付けず、OSでIPv6が有効なら踏みません。
ループバックの
::1が除外条件を通過してdefaultAddr6に入るからです。踏むのは次の場合です。
-Djava.net.preferIPv4Stack=trueを付けた場合。1番の自然な回避策ですが、IPv6が一切列挙されなくなりますipv6.disable=1などです書き換え先アドレスを手動で指定している場合は
getSpoofAddr()を通らないため、この問題は起きません。defaultAddr6がnullでも処理を継続できるようにしました。IPv6の書き換え先がない場合、書き換え対象にしたホストへのAAAA問い合わせにはNOERRORで回答なしの応答を返します。
変更に伴う修正
ソケットごとにスレッドを立てる構成にしたため、受信バッファをインスタンスからスレッドへ移し、
receive()のたびにDatagramPacketの長さを戻しています。getLength()が直前の受信サイズに縮むため、受信上限を明示するためです。複数のスレッドが停止処理を呼ぶようになるので、ソケット一覧は構築後に変更せず、停止状態を
AtomicBooleanで管理します。finish()は同期させ、並行する呼び出しが全ソケットのclose完了を待つようにしています。デットコードの
s_socs_recvPackets_sendPackets_sAddrとdnsServerは削除しました。