Skip to content

fix(network): bind ENet control socket on IPv6 wildcard without IPv6 address - #6164

Open
municool wants to merge 2 commits into
SunshineStream:masterfrom
municool:fix/enet-ipv6-wildcard-bind
Open

municool wants to merge 2 commits into
SunshineStream:masterfrom
municool:fix/enet-ipv6-wildcard-bind

Conversation

@municool

Copy link
Copy Markdown

Description

Sunshine crashes with SIGSEGV as soon as a Moonlight client starts a stream when address_family = both is configured on a host that has no non-loopback IPv6 address.

Cause

  • With address_family = both, net::host_create() binds the ENet control server to ::.
  • It filled the address with enet_address_set_host(), which resolves through getaddrinfo() with AI_ADDRCONFIG. That flag makes getaddrinfo("::") fail with EAI_ADDRFAMILY when the host has no non-loopback IPv6 address.
  • The return value was ignored, so the ENetAddress stayed empty (ss_family = 0, length 0), enet_address_set_port() did nothing, and bind() failed with EINVAL.
  • enet_host_create() then returned null, and host_create() dereferenced it in enet_socket_set_option(host->socket, ...).

RTSP and the HTTP servers bind :: through Boost.Asio, which does not use AI_ADDRCONFIG, so pairing and the initial connection work. Only the ENet control socket fails.

Changes

  • Add net::set_enet_address(). It converts numeric IPv4/IPv6 addresses directly through a Boost.Asio endpoint and enet_address_set_address(), and resolves only host names through ENet.
  • host_create() now returns an empty handle and logs an error when resolving the bind address fails, or when enet_host_create() fails (with strerror(errno)), instead of dereferencing null. The existing caller already handles an empty handle with "Couldn't bind Control server".

Screenshot

Issues Fixed or Closed

Roadmap Issues

Testing

Added tests in tests/unit/test_network.cpp:

  • SetEnetAddressTest.ConvertsIpv6Wildcard, ConvertsIpv4Address, ResolvesHostName, RejectsUnresolvableHostName
  • BindAddressTest.HostCreateBindsAvailablePort
  • BindAddressTest.HostCreateBindsIpv6Wildcard (skips if IPv6 sockets are unavailable)
  • BindAddressTest.HostCreateReturnsEmptyHandleWhenPortIsInUse
  • BindAddressTest.HostCreateReturnsEmptyHandleForUnresolvableAddress

Results on CachyOS (GCC 16.2.1, Debug, no global IPv6 address configured):

  • Against the original host_create(), HostCreateReturnsEmptyHandleWhenPortIsInUse, HostCreateBindsIpv6Wildcard and HostCreateReturnsEmptyHandleForUnresolvableAddress each crash with SIGSEGV (exit 139). With the fix, they pass.
  • Full test_sunshine suite: 971 passed, 7 skipped, 0 failed.
  • uv run --locked lb-update-clang-format made no changes.

Manual validation on the same host (AMD RX 9070 XT, KDE Wayland, portal capture):

  • Before: with address_family = both, the log showed Failed to create ENet host on [::]:47999: Invalid argument; without the null check, Sunshine crashed with SIGSEGV in net::host_create (src/network.cpp).
  • After: Moonlight connects and streams with address_family = both.

Not run: Windows, macOS and FreeBSD builds.

Type of Change

  • feat: New feature (non-breaking change which adds functionality)
  • fix: Bug fix (non-breaking change which fixes an issue)
  • docs: Documentation only changes
  • style: Changes that do not affect the meaning of the code (white-space, formatting, missing semicolons, etc.)
  • refactor: Code change that neither fixes a bug nor adds a feature
  • perf: Code change that improves performance
  • test: Adding missing tests or correcting existing tests
  • build: Changes that affect the build system or external dependencies
  • ci: Changes to CI configuration files and scripts
  • chore: Other changes that don't modify src or test files
  • revert: Reverts a previous commit
  • BREAKING CHANGE: Introduces a breaking change (can be combined with any type above)

Checklist

  • Code follows the style guidelines of this project
  • Code has been self-reviewed
  • Code has been commented, particularly in hard-to-understand areas
  • Code docstring/documentation-blocks for new or existing methods/components have been added or updated
  • Tests have been added or updated for new or modified functionality, including regression tests for bug fixes

AI Usage

See our AI usage policy.

  • AI tools were used while preparing this PR

Claude Code debugged the crash from the core dump and runtime logs, implemented the fix and tests, and prepared this PR. The fix was validated manually with a real Moonlight client.

This pull request was generated by Claude Code, Claude Opus 5.5, low thinking level

🤖 Generated with Claude Code

…address

With address_family = both, the ENet control server binds "::". ENet's
enet_address_set_host() resolves it through getaddrinfo() with
AI_ADDRCONFIG, which fails on hosts without a non-loopback IPv6
address. The failure was ignored, so bind() received an empty address
and returned EINVAL; host_create() then dereferenced the null host and
Sunshine crashed as soon as a client started a stream.

Convert numeric bind addresses directly through a Boost.Asio endpoint,
only resolve host names through ENet, and return an empty handle with
an error log when resolving or binding fails.

Fixes SunshineStream#6161

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@municool

Copy link
Copy Markdown
Author

This pull request was generated by Claude Code, Claude Opus 5.5, low thinking level

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sonarqubecloud

Copy link
Copy Markdown

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

Copy link
Copy Markdown
Member

Thanks for the focused fix and the same-host streaming check. Two small follow-ups on ebb665c:

  1. In src/network.cpp, strerror(errno) can log a stale or unrelated error when enet_host_create() returns null. Windows socket failures use Winsock errors, and ENet can clean up the socket before returning, so switching to WSAGetLastError() at this call site would not be reliable either. Please keep the address and port, omit the error suffix, and remove the now-unused <cerrno> and <cstring> includes from this file:
    if (!host) {
      BOOST_LOG(error) << "Failed to create ENet host on ["sv << bind_addr << "]:"sv << port;
      return host;
    }
  1. In tests/unit/test_network.cpp, HostCreateBindsAvailablePort still reads sockaddr_storage through a reinterpret_cast. Use the new copy helper there too so it avoids the aliasing issue the helper was added for: EXPECT_NE(sockaddr_as<sockaddr_in>(host->address.address).sin_port, 0);

These are source-review suggestions; I did not run the platform tests.

Review completed by Codex | Model: unavailable | Thinking: unavailable

This branch has not been deployed

No deployments
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.

SIGSEGV, Segmentation fault when connecting from Moonlight

2 participants