fix: use UDP connect to probe local address #121
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.
Background
ENET, which is based on UDP, requires a local address to bind and receive datagrams. Previously, we populated and recorded the local address during the RTSP handshake, which is usually bound to a TCP socket. While this approach generally works well, I discovered that in certain network environments—such as when using "aTrust VPN"—the discrepancy between TCP and UDP (ENET) behavior causes issues (see issue #1300).
The root cause is that some VPN software redirects TCP traffic to a loopback proxy for forwarding. This leads to unexpected behavior when calling socket APIs:
While this behavior does not break the TCP connection itself, it causes failures when we apply the local address retrieved from a TCP socket to a UDP socket.
Approach
The solution is to use a UDP socket (
SOCK_DGRAM) to probe the address instead.udp_sock.getsocknamereturns the correct local interface address in these environments.Since the exact implementation of the RTSP handshake can vary, I prefer performing the local address resolution immediately after
STAGE_NAME_RESOLUTION, once the remote address is available.Note
As this repository serves as a common library for various Moonlight projects, I do not have the necessary environment to cover every possible code path, platform, and network configuration. Any guidance on conducting more thorough or automated tests would be greatly appreciated.