Skip to content

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

Merged
ReenigneArcher merged 3 commits into
SunshineStream:masterfrom
municool:fix/enet-ipv6-wildcard-bind
Nov 25, 2026
Merged

ReenigneArcher merged 3 commits into
SunshineStream:masterfrom
municool:fix/enet-ipv6-wildcard-bind

Conversation

@municool

Copy link
Copy Markdown
Contributor

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

@municool

Copy link
Copy Markdown
Contributor Author

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

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

@kukuj9224-debug

Copy link
Copy Markdown

I hit the same crash in net::host_create() on Windows 10 x64 (Sunshine v2026.914.233613). With bind_address unset, starting a Moonlight stream caused Sunshine to crash while opening its control socket on 0.0.0.0:47999 (IPv4 UDP). The dump shows 0xc0000005 at host->socket in src/network.cpp:225. Setting bind_address to the host’s Ethernet IPv4 address got streaming working again. I haven’t tested this PR on Windows yet.

municool and others added 3 commits November 25, 2026 00:06
…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>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
errno may be stale or unrelated when enet_host_create() returns null,
and Windows socket failures use Winsock errors that ENet may clear
during cleanup. Log only the address and port, and remove the unused
<cerrno> and <cstring> includes.

Also use the sockaddr_as copy helper in HostCreateBindsAvailablePort to
avoid reading sockaddr_storage through a reinterpret_cast.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ReenigneArcher
ReenigneArcher force-pushed the fix/enet-ipv6-wildcard-bind branch from 5da3b0a to 25607fa Compare November 25, 2026 05:06
@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Bundle Report

Changes will increase total bundle size by 1.5kB (0.15%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
sunshine-esm 1.01MB 1.5kB (0.15%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: sunshine-esm

Assets Changed:

Asset Name Size Change Total Size Change (%)
images/logo-*.svg -661 bytes 1.48kB -30.92%
images/logo-*.svg (New) 2.17kB 2.17kB 100.0% 🚀

@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 51.93%. Comparing base (433c876) to head (25607fa).
✅ All tests successful. No failed tests found.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #6164      +/-   ##
==========================================
+ Coverage   51.85%   51.93%   +0.08%     
==========================================
  Files         150      150              
  Lines       32848    32863      +15     
  Branches    13858    13862       +4     
==========================================
+ Hits        17032    17068      +36     
- Misses      12277    12820     +543     
+ Partials     3539     2975     -564     
Flag Coverage Δ
Alpine-aarch64 35.67% <69.23%> (+0.12%) ⬆️
Alpine-x86_64 35.79% <69.23%> (+0.12%) ⬆️
Archlinux 36.52% <69.23%> (+0.11%) ⬆️
FreeBSD-amd64 36.21% <71.42%> (+0.11%) ⬆️
Homebrew-macos-15 50.00% <92.30%> (+0.15%) ⬆️
Homebrew-macos-26 50.40% <92.30%> (+0.17%) ⬆️
Homebrew-ubuntu-24.04 38.67% <69.23%> (+0.10%) ⬆️
Linux-AppImage-aarch64 41.64% <69.23%> (+0.10%) ⬆️
Linux-AppImage-x86_64 41.77% <69.23%> (-0.01%) ⬇️
PowerShell 100.00% <ø> (ø)
Web-UI 58.14% <ø> (ø)
Windows-AMD64 42.53% <69.23%> (+0.14%) ⬆️
Windows-ARM64 36.12% <71.42%> (+0.16%) ⬆️
macOS-arm64 42.53% <71.42%> (+0.15%) ⬆️
macOS-x86_64 41.45% <71.42%> (+0.15%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/network.cpp 90.79% <100.00%> (+12.41%) ⬆️
src/network.h 100.00% <ø> (+50.00%) ⬆️

... and 24 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 433c876...25607fa. Read the comment docs.

@github-actions

Copy link
Copy Markdown
Last Updated 2026-11-25 05:57:39 UTC
Source Run CI Run #6975
Commit 25607fad76f9ffd64eae64812d816a6191fe3463

Screenshot Comparison

PR #6164 screenshots vs screenshots baseline.

Matrix: AppImage-aarch64

Image Baseline PR
sunshine_tray_initial.png
sunshine_tray_menu.png
sunshine_tray_pairing_request.png
sunshine_tray_paused.png
sunshine_tray_stopped.png
sunshine_tray_streaming.png
sunshine_tray_tooltip.png

Matrix: AppImage-x86_64

Image Baseline PR
sunshine_tray_initial.png
sunshine_tray_menu.png
sunshine_tray_pairing_request.png
sunshine_tray_paused.png
sunshine_tray_stopped.png
sunshine_tray_streaming.png
sunshine_tray_tooltip.png

Matrix: Windows-AMD64

Image Baseline PR
sunshine_tray_initial.png
sunshine_tray_menu.png
sunshine_tray_pairing_request.png
sunshine_tray_paused.png
sunshine_tray_stopped.png
sunshine_tray_streaming.png
sunshine_tray_tooltip.png

Matrix: Windows-ARM64

Image Baseline PR
sunshine_tray_initial.png
sunshine_tray_menu.png
sunshine_tray_pairing_request.png
sunshine_tray_paused.png
sunshine_tray_stopped.png
sunshine_tray_streaming.png
sunshine_tray_tooltip.png

Matrix: macOS-arm64

Image Baseline PR
sunshine_tray_initial.png
sunshine_tray_menu.png
sunshine_tray_pairing_request.png
sunshine_tray_paused.png
sunshine_tray_stopped.png
sunshine_tray_streaming.png
sunshine_tray_tooltip.png

Matrix: macOS-x86_64

Image Baseline PR
sunshine_tray_initial.png
sunshine_tray_menu.png
sunshine_tray_pairing_request.png
sunshine_tray_paused.png
sunshine_tray_stopped.png
sunshine_tray_streaming.png
sunshine_tray_tooltip.png

@ReenigneArcher
ReenigneArcher merged commit b7a0b68 into SunshineStream:master Nov 25, 2026
83 checks passed
@municool
municool deleted the fix/enet-ipv6-wildcard-bind branch November 25, 2026 16:18
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

3 participants