Skip to content

[socket] Fix IPv6 compilation error on host platform#14101

Merged
swoboda1337 merged 1 commit intoesphome:devfrom
swoboda1337:fix/socket-ipv6-host-compilation
Feb 19, 2026
Merged

[socket] Fix IPv6 compilation error on host platform#14101
swoboda1337 merged 1 commit intoesphome:devfrom
swoboda1337:fix/socket-ipv6-host-compilation

Conversation

@swoboda1337
Copy link
Member

What does this implement/fix?

When network: enable_ipv6: true is set on the host platform, compilation fails because format_sockaddr_to in socket.cpp accesses sin6_addr.un.u32_addr — an LWIP-specific struct layout — under a #ifndef USE_SOCKET_IMPL_LWIP_TCP guard that includes the host platform. The host platform uses POSIX struct in6_addr which only has s6_addr (no LWIP union).

ESP32-IDF is unaffected despite also using BSD sockets, because its LWIP headers provide the un.u32_addr union layout.

The fix narrows the preprocessor guard to USE_HOST and uses the standard IN6_IS_ADDR_V4MAPPED() macro with s6_addr[] for the host code path only. The existing LWIP code path for ESP32-IDF and LWIP sockets is unchanged.

Also adds socket component build tests for host and ESP32-IDF, with and without IPv6.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Developer breaking change (an API change that could break external components)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable):

Pull request in esphome-docs with documentation (if applicable):

  • N/A

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx
  • LN882x
  • nRF52840

Example entry for config.yaml:

esphome:
  name: test-host-ipv6

host:

network:
  enable_ipv6: true

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

The format_sockaddr_to function accessed sin6_addr.un.u32_addr (an
LWIP-specific struct layout) under a guard that included the host
platform, which uses POSIX struct in6_addr without the LWIP union.

Narrow the preprocessor guard to USE_HOST and use the standard
IN6_IS_ADDR_V4MAPPED() macro with s6_addr[] for the host code path.
ESP32-IDF is unaffected since its LWIP headers provide the union layout
even with BSD sockets.

Also adds socket component build tests for host and ESP32-IDF, with and
without IPv6.

Closes esphome#14097

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 19, 2026 15:06
@swoboda1337 swoboda1337 requested a review from a team as a code owner February 19, 2026 15:06
@github-actions
Copy link
Contributor

To use the changes from this PR as an external component, add the following to your ESPHome configuration YAML file:

external_components:
  - source: github://pr#14101
    components: [socket]
    refresh: 1h

(Added by the PR bot)

@github-actions
Copy link
Contributor

👋 Hi there! This PR modifies 1 file(s) with codeowners.

@esphome/core - As codeowner(s) of the affected files, your review would be appreciated! 🙏

Note: Automatic review request may have failed, but you're still welcome to review.

@bdraco bdraco added this to the 2026.2.1 milestone Feb 19, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a compilation error when using IPv6 on the host platform. The issue stems from the host platform using POSIX struct in6_addr (which only has s6_addr[]) while the existing code attempted to access LWIP-specific un.u32_addr union members. ESP32-IDF is unaffected because it includes LWIP headers that provide the union layout despite using BSD sockets.

Changes:

  • Updated IPv4-mapped IPv6 address detection to use standard POSIX macros for host platform
  • Added platform-specific preprocessor guards to separate host and ESP32-IDF code paths
  • Added build tests for socket component on host and ESP32-IDF platforms with and without IPv6

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
esphome/components/socket/socket.cpp Added USE_HOST-specific code path using IN6_IS_ADDR_V4MAPPED() and s6_addr[12] for IPv4-mapped address detection
tests/components/socket/common.yaml Shared test configuration with substitution for IPv6 enable flag
tests/components/socket/test.host.yaml Build test for socket component on host platform without IPv6
tests/components/socket/test.esp32-idf.yaml Build test for socket component on ESP32-IDF platform without IPv6
tests/components/socket/test-ipv6.host.yaml Build test for socket component on host platform with IPv6 enabled
tests/components/socket/test-ipv6.esp32-idf.yaml Build test for socket component on ESP32-IDF platform with IPv6 enabled

@codecov-commenter
Copy link

codecov-commenter commented Feb 19, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.11%. Comparing base (4d05e4d) to head (f24c178).
⚠️ Report is 7 commits behind head on dev.

Additional details and impacted files
@@           Coverage Diff           @@
##              dev   #14101   +/-   ##
=======================================
  Coverage   74.11%   74.11%           
=======================================
  Files          55       55           
  Lines       11590    11590           
  Branches     1578     1578           
=======================================
  Hits         8590     8590           
  Misses       2598     2598           
  Partials      402      402           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@swoboda1337
Copy link
Member Author

Thanks

@swoboda1337 swoboda1337 enabled auto-merge (squash) February 19, 2026 15:34
@swoboda1337 swoboda1337 merged commit 5304750 into esphome:dev Feb 19, 2026
51 checks passed
swoboda1337 added a commit that referenced this pull request Feb 20, 2026
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
@swoboda1337 swoboda1337 mentioned this pull request Feb 20, 2026
@github-actions github-actions bot locked and limited conversation to collaborators Feb 21, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

network: enabling IPv6 when using host platform results in uncaught error

4 participants