Skip to content

Conversation

@seven-mile
Copy link

@seven-mile seven-mile commented Dec 31, 2025

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:

>>> host, port = "10.xx.xx.xxx", 48010
>>> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> sock.connect((host, port))
>>> local_addr = sock.getsockname()[0]
# Returns 127.0.0.1
>>> local_hostname = socket.gethostbyaddr(local_addr)[0]
# Returns localhost.sangfor.com.cn

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.getsockname returns 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.

@seven-mile seven-mile marked this pull request as ready for review December 31, 2025 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant